Skip to content

Commit

Permalink
Unify buffer module
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Sep 24, 2024
1 parent c76fa3d commit 9251063
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 67 deletions.
61 changes: 7 additions & 54 deletions lib/vendor/buffer/buffer.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,10 @@
function SpecificBuffer (_Buffer) {
const Buffer = function () {
if (typeof arguments[0] === 'number') {
return _Buffer.alloc(...arguments);
}

return _Buffer.from(...arguments);
}

Buffer.poolSize = _Buffer.poolSize;

Object.defineProperty(Buffer, Symbol.hasInstance, {
value: function (instance) {
return instance instanceof _Buffer;
}
});

Buffer.isBuffer = function () {
return _Buffer.isBuffer(...arguments);
}

Buffer.alloc = function () {
return _Buffer.alloc(...arguments);
}

Buffer.allocUnsafe = function () {
return _Buffer.allocUnsafe(...arguments);
}

Buffer.allocUnsafeSlow = function () {
return _Buffer.allocUnsafeSlow(...arguments);
function getBufferModule (buffer) {
return {
Buffer: buffer.Buffer,
SlowBuffer: buffer.SlowBuffer,
INSPECT_MAX_BYTES: buffer.INSPECT_MAX_BYTES,
kMaxLength: buffer.kMaxLength
}

Buffer.from = function () {
return _Buffer.from(...arguments);
}

Buffer.compare = function () {
return _Buffer.compare(...arguments);
}

Buffer.isEncoding = function () {
return _Buffer.isEncoding(...arguments);
}

Buffer.concat = function () {
return _Buffer.concat(...arguments);
}

Buffer.byteLength = function () {
return _Buffer.byteLength(...arguments);
}

return Buffer;
}

module.exports = SpecificBuffer;
module.exports = getBufferModule;
13 changes: 6 additions & 7 deletions lib/vendor/buffer/index.browser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const SpecificBuffer = require('./buffer');
const getBufferModule = require('./buffer');
const SpecificBuffer = require('./specific-buffer');
const buffer = require('buffer/');

module.exports = {
Buffer: SpecificBuffer(buffer.Buffer),
SlowBuffer: buffer.SlowBuffer,
INSPECT_MAX_BYTES: buffer.INSPECT_MAX_BYTES,
kMaxLength: buffer.kMaxLength
}
module.exports = getBufferModule({
...buffer,
Buffer: SpecificBuffer(buffer.Buffer)
})
11 changes: 5 additions & 6 deletions lib/vendor/buffer/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const SpecificBuffer = require('./buffer');
const getBufferModule = require('./buffer');
const SpecificBuffer = require('./specific-buffer');
const buffer = globalThis._nodeRequires.buffer;

module.exports = {
module.exports = getBufferModule({
...buffer,
Buffer: SpecificBuffer(globalThis.Buffer),
SlowBuffer: buffer.SlowBuffer,
INSPECT_MAX_BYTES: buffer.INSPECT_MAX_BYTES,
kMaxLength: buffer.kMaxLength
}
});
57 changes: 57 additions & 0 deletions lib/vendor/buffer/specific-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
function SpecificBuffer (_Buffer) {
const Buffer = function () {
if (typeof arguments[0] === 'number') {
return _Buffer.alloc(...arguments);
}

return _Buffer.from(...arguments);
}

Buffer.poolSize = _Buffer.poolSize;

Object.defineProperty(Buffer, Symbol.hasInstance, {
value: function (instance) {
return instance instanceof _Buffer;
}
});

Buffer.isBuffer = function () {
return _Buffer.isBuffer(...arguments);
};

Buffer.alloc = function () {
return _Buffer.alloc(...arguments);
};

Buffer.allocUnsafe = function () {
return _Buffer.allocUnsafe(...arguments);
};

Buffer.allocUnsafeSlow = function () {
return _Buffer.allocUnsafeSlow(...arguments);
};

Buffer.from = function () {
return _Buffer.from(...arguments);
};

Buffer.compare = function () {
return _Buffer.compare(...arguments);
};

Buffer.isEncoding = function () {
return _Buffer.isEncoding(...arguments);
};

Buffer.concat = function () {
return _Buffer.concat(...arguments);
};

Buffer.byteLength = function () {
return _Buffer.byteLength(...arguments);
};

return Buffer;
}

module.exports = SpecificBuffer;

0 comments on commit 9251063

Please sign in to comment.