Skip to content

Commit 8172f45

Browse files
bengljasnell
authored andcommitted
buffer: move setupBufferJS to internal
Stashing it away in internal/buffer so that it can't be used in userland, but can still be used in internals. PR-URL: #16391 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3621889 commit 8172f45

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

lib/buffer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const {
3535
readDoubleLE: _readDoubleLE,
3636
readFloatBE: _readFloatBE,
3737
readFloatLE: _readFloatLE,
38-
setupBufferJS,
3938
swap16: _swap16,
4039
swap32: _swap32,
4140
swap64: _swap64,
@@ -63,6 +62,8 @@ const errors = require('internal/errors');
6362

6463
const internalBuffer = require('internal/buffer');
6564

65+
const { setupBufferJS } = internalBuffer;
66+
6667
const bindingObj = {};
6768

6869
class FastBuffer extends Uint8Array {

lib/internal/bootstrap_node.js

+4
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@
292292
}
293293
});
294294

295+
// This, as side effect, removes `setupBufferJS` from the buffer binding,
296+
// and exposes it on `internal/buffer`.
297+
NativeModule.require('internal/buffer');
298+
295299
global.Buffer = NativeModule.require('buffer').Buffer;
296300
process.domain = null;
297301
process._exiting = false;

lib/internal/buffer.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
'use strict';
22

3-
// This is needed still for FastBuffer
4-
module.exports = {};
3+
const binding = process.binding('buffer');
4+
const { setupBufferJS } = binding;
5+
6+
// Remove from the binding so that function is only available as exported here.
7+
// (That is, for internal use only.)
8+
delete binding.setupBufferJS;
9+
10+
// FastBuffer wil be inserted here by lib/buffer.js
11+
module.exports = {
12+
setupBufferJS
13+
};

test/parallel/test-buffer-bindingobj-no-zerofill.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,26 @@ const assert = require('assert');
1111
const buffer = require('buffer');
1212

1313
// Monkey-patch setupBufferJS() to have an undefined zeroFill.
14-
const process = require('process');
15-
const originalBinding = process.binding;
14+
const internalBuffer = require('internal/buffer');
1615

17-
const binding = originalBinding('buffer');
18-
const originalSetup = binding.setupBufferJS;
16+
const originalSetup = internalBuffer.setupBufferJS;
1917

20-
binding.setupBufferJS = (proto, obj) => {
18+
internalBuffer.setupBufferJS = (proto, obj) => {
2119
originalSetup(proto, obj);
2220
assert.strictEqual(obj.zeroFill[0], 1);
2321
delete obj.zeroFill;
2422
};
2523

2624
const bindingObj = {};
2725

28-
binding.setupBufferJS(Buffer.prototype, bindingObj);
26+
internalBuffer.setupBufferJS(Buffer.prototype, bindingObj);
2927
assert.strictEqual(bindingObj.zeroFill, undefined);
3028

31-
process.binding = (bindee) => {
32-
if (bindee === 'buffer')
33-
return binding;
34-
return originalBinding(bindee);
35-
};
36-
3729
// Load from file system because internal buffer is already loaded and we're
3830
// testing code that runs on first load only.
3931
// Do not move this require() to top of file. It is important that
40-
// `process.binding('buffer').setupBufferJS` be monkey-patched before this runs.
32+
// `require('internal/buffer').setupBufferJS` be monkey-patched before this
33+
// runs.
4134
const monkeyPatchedBuffer = require('../../lib/buffer');
4235

4336
// On unpatched buffer, allocUnsafe() should not zero fill memory. It's always
@@ -51,3 +44,6 @@ while (uninitialized.every((val) => val === 0))
5144
// zero-fill in that case.
5245
const zeroFilled = monkeyPatchedBuffer.Buffer.allocUnsafe(1024);
5346
assert(zeroFilled.every((val) => val === 0));
47+
48+
// setupBufferJS shouldn't still be exposed on the binding
49+
assert(!('setupBufferJs' in process.binding('buffer')));

0 commit comments

Comments
 (0)