Skip to content

Commit b62d7a0

Browse files
authored
fix: Buffer constructor throwing "Unsafe Promise species cannot be reset" (#558)
1 parent 91a3f9b commit b62d7a0

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

lib/setup-sandbox.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,14 @@ class BufferHandler extends ReadOnlyHandler {
317317
if (args.length > 0 && typeof args[0] === 'number') {
318318
return LocalBuffer.alloc(args[0]);
319319
}
320-
return localReflectApply(LocalBuffer.from, LocalBuffer, args);
320+
return apply(LocalBuffer.from, LocalBuffer, args);
321321
}
322322

323323
construct(target, args, newTarget) {
324324
if (args.length > 0 && typeof args[0] === 'number') {
325325
return LocalBuffer.alloc(args[0]);
326326
}
327-
return localReflectApply(LocalBuffer.from, LocalBuffer, args);
327+
return apply(LocalBuffer.from, LocalBuffer, args);
328328
}
329329
}
330330
/* eslint-enable no-use-before-define */

test/vm.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,20 @@ describe('contextify', () => {
322322
assert.ok(d.constructor.constructor === Function);
323323
});
324324

325+
it('buffer (deprecated constructor)', () => {
326+
const result = vm.run('new Buffer("Hello")');
327+
assert.ok(result instanceof Buffer, '#1');
328+
assert.strictEqual(result.toString(), 'Hello', '#2');
329+
330+
const result2 = vm.run('new Buffer(5)');
331+
assert.ok(result2 instanceof Buffer, '#3');
332+
assert.strictEqual(result2.length, 5, '#4');
333+
334+
const result3 = vm.run('Buffer("Hello")');
335+
assert.ok(result3 instanceof Buffer, '#5');
336+
assert.strictEqual(result3.toString(), 'Hello', '#6');
337+
});
338+
325339
it('function', () => {
326340
assert.strictEqual(vm.run('test.function instanceof Function'), true, '#1');
327341
assert.strictEqual(vm.run('test.function() instanceof Function'), true, '#2');

0 commit comments

Comments
 (0)