Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Commit

Permalink
Use NAPI_MODULE_INIT
Browse files Browse the repository at this point in the history
This allows iltorb to be loaded multiple times during the lifetime of the Node.js process.

Fix #78
  • Loading branch information
nstepien committed Oct 26, 2018
1 parent 25ead22 commit 9f05abe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/iltorb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
#include "dec/stream_decode.h"
#include "enc/stream_encode.h"

#ifdef NAPI_MODULE_INIT
NAPI_MODULE_INIT() {
StreamDecode::Init(env, exports);
StreamEncode::Init(env, exports);
return exports;
}
#else
napi_value Init(napi_env env, napi_value exports) {
StreamDecode::Init(env, exports);
StreamEncode::Init(env, exports);
return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
#endif
15 changes: 15 additions & 0 deletions test/iltorb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import test from 'ava';

test('compress an empty buffer', async function(t) {
const brotli1 = require('../');
const buf1 = await brotli1.compress(Buffer.alloc(0));
t.truthy(Buffer.isBuffer(buf1));

delete require.cache[require.resolve('..')];
delete require.cache[require.resolve('../build/bindings/iltorb.node')];

const brotli2 = require('../');
const buf2 = await brotli2.compress(Buffer.alloc(0));
t.truthy(Buffer.isBuffer(buf2));
t.truthy(Buffer.compare(buf1, buf2) === 0);
});

0 comments on commit 9f05abe

Please sign in to comment.