Skip to content

Commit 9abd1c7

Browse files
anonrigtargos
authored andcommitted
zlib: remove prototype primordials usage
# Conflicts: # lib/zlib.js PR-URL: #54695 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruy Adorno <ruy@vlt.sh> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
1 parent 31f0ef6 commit 9abd1c7

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

doc/contributing/primordials.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ important than reliability against prototype pollution:
1010
* `node:http`
1111
* `node:http2`
1212
* `node:tls`
13+
* `node:zlib`
1314

1415
Usage of primordials should be preferred for new code in other areas, but
1516
replacing current code with primordials should be

lib/eslint.config_partial.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ export default [
494494
'lib/internal/http.js',
495495
'lib/internal/http2/*.js',
496496
'lib/tls.js',
497+
'lib/zlib.js',
497498
],
498499
rules: {
499500
'no-restricted-syntax': [

lib/zlib.js

+23-27
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,16 @@
2323

2424
const {
2525
ArrayBuffer,
26-
ArrayPrototypeForEach,
27-
ArrayPrototypeMap,
28-
ArrayPrototypePush,
29-
FunctionPrototypeBind,
30-
MathMaxApply,
26+
MathMax,
3127
NumberIsNaN,
3228
ObjectDefineProperties,
3329
ObjectDefineProperty,
30+
ObjectEntries,
3431
ObjectFreeze,
3532
ObjectKeys,
3633
ObjectSetPrototypeOf,
3734
ReflectApply,
38-
StringPrototypeStartsWith,
3935
Symbol,
40-
TypedArrayPrototypeFill,
4136
Uint32Array,
4237
} = primordials;
4338

@@ -130,10 +125,11 @@ function zlibBuffer(engine, buffer, callback) {
130125
}
131126

132127
function zlibBufferOnData(chunk) {
133-
if (!this.buffers)
128+
if (!this.buffers) {
134129
this.buffers = [chunk];
135-
else
136-
ArrayPrototypePush(this.buffers, chunk);
130+
} else {
131+
this.buffers.push(chunk);
132+
}
137133
this.nread += chunk.length;
138134
if (this.nread > this._maxOutputLength) {
139135
this.close();
@@ -442,7 +438,7 @@ function processChunkSync(self, chunk, flushFlag) {
442438
if (have > 0) {
443439
const out = buffer.slice(offset, offset + have);
444440
offset += have;
445-
ArrayPrototypePush(buffers, out);
441+
buffers.push(out);
446442
nread += out.byteLength;
447443

448444
if (nread > self._maxOutputLength) {
@@ -700,9 +696,10 @@ Zlib.prototype.params = function params(level, strategy, callback) {
700696
checkRangesOrGetDefault(strategy, 'strategy', Z_DEFAULT_STRATEGY, Z_FIXED);
701697

702698
if (this._level !== level || this._strategy !== strategy) {
703-
this.flush(Z_SYNC_FLUSH,
704-
FunctionPrototypeBind(paramsAfterFlushCallback, this,
705-
level, strategy, callback));
699+
this.flush(
700+
Z_SYNC_FLUSH,
701+
paramsAfterFlushCallback.bind(this, level, strategy, callback),
702+
);
706703
} else {
707704
process.nextTick(callback);
708705
}
@@ -782,13 +779,10 @@ function createConvenienceMethod(ctor, sync) {
782779
};
783780
}
784781

785-
const kMaxBrotliParam = MathMaxApply(ArrayPrototypeMap(
786-
ObjectKeys(constants),
787-
(key) => (StringPrototypeStartsWith(key, 'BROTLI_PARAM_') ?
788-
constants[key] :
789-
0),
790-
));
791-
782+
const kMaxBrotliParam = MathMax(
783+
...ObjectEntries(constants)
784+
.map(({ 0: key, 1: value }) => (key.startsWith('BROTLI_PARAM_') ? value : 0)),
785+
);
792786
const brotliInitParamsArray = new Uint32Array(kMaxBrotliParam + 1);
793787

794788
const brotliDefaultOpts = {
@@ -799,9 +793,9 @@ const brotliDefaultOpts = {
799793
function Brotli(opts, mode) {
800794
assert(mode === BROTLI_DECODE || mode === BROTLI_ENCODE);
801795

802-
TypedArrayPrototypeFill(brotliInitParamsArray, -1);
796+
brotliInitParamsArray.fill(-1);
803797
if (opts?.params) {
804-
ArrayPrototypeForEach(ObjectKeys(opts.params), (origKey) => {
798+
ObjectKeys(opts.params).forEach((origKey) => {
805799
const key = +origKey;
806800
if (NumberIsNaN(key) || key < 0 || key > kMaxBrotliParam ||
807801
(brotliInitParamsArray[key] | 0) !== -1) {
@@ -939,10 +933,12 @@ ObjectDefineProperties(module.exports, {
939933

940934
// These should be considered deprecated
941935
// expose all the zlib constants
942-
for (const bkey of ObjectKeys(constants)) {
943-
if (StringPrototypeStartsWith(bkey, 'BROTLI')) continue;
944-
ObjectDefineProperty(module.exports, bkey, {
936+
for (const { 0: key, 1: value } of ObjectEntries(constants)) {
937+
if (key.startsWith('BROTLI')) continue;
938+
ObjectDefineProperty(module.exports, key, {
945939
__proto__: null,
946-
enumerable: false, value: constants[bkey], writable: false,
940+
enumerable: false,
941+
value,
942+
writable: false,
947943
});
948944
}

0 commit comments

Comments
 (0)