Skip to content

Commit

Permalink
Remove Napi using-directive
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Jan 21, 2024
1 parent 13f2cae commit 3ec4855
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions argon2_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
#include <napi.h>
#include <vector>

using namespace Napi;
using ustring = std::vector<uint8_t>;

static ustring from_buffer(const Value &value) {
const auto &buf = value.As<Buffer<uint8_t>>();
static ustring from_buffer(const Napi::Value &value) {
const auto &buf = value.As<Napi::Buffer<uint8_t>>();
const auto &data = buf.Data();
return {data, data + buf.Length()};
}
Expand Down Expand Up @@ -53,14 +52,15 @@ static argon2_context make_context(uint8_t *buf, ustring &plain, ustring &salt,
return ctx;
}

class HashWorker final : public AsyncWorker {
class HashWorker final : public Napi::AsyncWorker {
public:
HashWorker(const Env &env, ustring &&plain, ustring &&salt, Options &&opts)
HashWorker(const Napi::Env &env, ustring &&plain, ustring &&salt,
Options &&opts)
: AsyncWorker{env, "argon2:HashWorker"}, deferred{env},
plain{std::move(plain)}, salt{std::move(salt)},
opts{std::move(opts)} {}

Promise GetPromise() { return deferred.Promise(); }
Napi::Promise GetPromise() { return deferred.Promise(); }

protected:
void Execute() override {
Expand All @@ -78,21 +78,23 @@ class HashWorker final : public AsyncWorker {

void OnOK() override {
deferred.Resolve(
Buffer<uint8_t>::Copy(Env(), hash.get(), opts.hash_length));
Napi::Buffer<uint8_t>::Copy(Env(), hash.get(), opts.hash_length));
}

void OnError(const Error &err) override { deferred.Reject(err.Value()); }
void OnError(const Napi::Error &err) override {
deferred.Reject(err.Value());
}

private:
Promise::Deferred deferred;
Napi::Promise::Deferred deferred;
ustring plain;
ustring salt;
Options opts;

std::unique_ptr<uint8_t[]> hash;
};

static Options extract_opts(const Object &opts) {
static Options extract_opts(const Napi::Object &opts) {
return {
opts.Has("secret") ? from_buffer(opts["secret"]) : ustring{},
opts.Has("associatedData") ? from_buffer(opts["associatedData"])
Expand All @@ -106,20 +108,20 @@ static Options extract_opts(const Object &opts) {
};
}

static Value Hash(const CallbackInfo &info) {
static Napi::Value Hash(const Napi::CallbackInfo &info) {
assert(info.Length() == 4 && info[0].IsBuffer() && info[1].IsBuffer() &&
info[2].IsObject());

auto worker =
new HashWorker{info.Env(), from_buffer(info[0]), from_buffer(info[1]),
extract_opts(info[2].As<Object>())};
extract_opts(info[2].As<Napi::Object>())};

worker->Queue();
return worker->GetPromise();
}

static Object init(Env env, Object exports) {
exports["hash"] = Function::New(env, Hash);
static Napi::Object init(Napi::Env env, Napi::Object exports) {
exports["hash"] = Napi::Function::New(env, Hash);
return exports;
}

Expand Down

0 comments on commit 3ec4855

Please sign in to comment.