Skip to content

Commit

Permalink
Use old-style functions and use safe-buffer
Browse files Browse the repository at this point in the history
* err => {} notation doesn't work on Node 4.
* Use Buffer.alloc polyfill from safe-buffer

PR-URL: #122
Fixes: #120
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
Gabriel Schulhof authored and mhdawson committed Aug 25, 2017
1 parent a870338 commit 790de9f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
],
"dependencies": {},
"description": "Node.js API (N-API)",
"devDependencies": {},
"devDependencies": {
"safe-buffer": "^5.1.1"
},
"directories": {},
"homepage": "https://github.com/nodejs/node-addon-api",
"license": "MIT",
Expand Down
3 changes: 2 additions & 1 deletion test/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const testUtil = require('./testUtil');
const safeBuffer = require('safe-buffer');

test(require(`./build/${buildType}/binding.node`));
test(require(`./build/${buildType}/binding_noexcept.node`));
Expand All @@ -14,7 +15,7 @@ function test(binding) {
binding.buffer.checkBuffer(test);
assert.ok(test instanceof Buffer);

const test2 = Buffer.alloc(test.length);
const test2 = safeBuffer.Buffer.alloc(test.length);
test.copy(test2);
binding.buffer.checkBuffer(test2);
},
Expand Down
16 changes: 8 additions & 8 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ test(`./build/${buildType}/binding_noexcept.node`);
function test(bindingPath) {
const binding = require(bindingPath);

assert.throws(() => binding.error.throwApiError('test'), err => {
assert.throws(() => binding.error.throwApiError('test'), function(err) {
return err instanceof Error && err.message.includes('Invalid');
});

assert.throws(() => binding.error.throwJSError('test'), err => {
assert.throws(() => binding.error.throwJSError('test'), function(err) {
return err instanceof Error && err.message === 'test';
});

assert.throws(() => binding.error.throwTypeError('test'), err => {
assert.throws(() => binding.error.throwTypeError('test'), function(err) {
return err instanceof TypeError && err.message === 'test';
});

assert.throws(() => binding.error.throwRangeError('test'), err => {
assert.throws(() => binding.error.throwRangeError('test'), function(err) {
return err instanceof RangeError && err.message === 'test';
});

Expand All @@ -35,7 +35,7 @@ function test(bindingPath) {
() => {
throw new TypeError('test');
}),
err => {
function(err) {
return err instanceof TypeError && err.message === 'test' && !err.caught;
});

Expand All @@ -44,7 +44,7 @@ function test(bindingPath) {
() => {
throw new TypeError('test');
}),
err => {
function(err) {
return err instanceof TypeError && err.message === 'test' && err.caught;
});

Expand All @@ -57,11 +57,11 @@ function test(bindingPath) {
() => { throw new TypeError('test'); });
assert.strictEqual(msg, 'test');

assert.throws(() => binding.error.throwErrorThatEscapesScope('test'), err => {
assert.throws(() => binding.error.throwErrorThatEscapesScope('test'), function(err) {
return err instanceof Error && err.message === 'test';
});

assert.throws(() => binding.error.catchAndRethrowErrorThatEscapesScope('test'), err => {
assert.throws(() => binding.error.catchAndRethrowErrorThatEscapesScope('test'), function(err) {
return err instanceof Error && err.message === 'test' && err.caught;
});

Expand Down

0 comments on commit 790de9f

Please sign in to comment.