Skip to content

Commit d2d0ae2

Browse files
committed
ci-test: apply #61157 (comment)
1 parent 9c4e4c2 commit d2d0ae2

File tree

1 file changed

+37
-74
lines changed

1 file changed

+37
-74
lines changed

lib/buffer.js

Lines changed: 37 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ const {
5454
Uint8Array,
5555
} = primordials;
5656

57+
// TODO(LiviaMedeiros): replace with primordial once `--js-base-64` is not optional
58+
// eslint-disable-next-line node-core/prefer-primordials
59+
const { fromHex: Uint8ArrayFromHex, fromBase64: Uint8ArrayFromBase64 } = Uint8Array;
60+
5761
const {
5862
byteLengthUtf8,
5963
compare: _compare,
@@ -336,81 +340,40 @@ Buffer.from = function from(value, encodingOrOffset, length) {
336340
);
337341
};
338342

339-
/**
340-
* Same as Uint8Array.fromHex(hexstring), but returns instance of Buffer.
341-
* Not same as Buffer.from(hexstring), as it performs different validations.
342-
* @function Buffer.fromHex
343-
* @param {string} str
344-
* @returns {Buffer}
345-
*/
346-
ObjectDefineProperty(Buffer, 'fromHex', {
347-
__proto__: null,
348-
configurable: true,
349-
enumerable: false,
350-
get() {
351-
// TODO(LiviaMedeiros): make unconditional and use primordial once `--js-base-64` is not optional
352-
// eslint-disable-next-line node-core/prefer-primordials
353-
const Uint8ArrayFromHex = Uint8Array.fromHex;
354-
const value = Uint8ArrayFromHex === undefined ?
355-
undefined :
356-
function fromHex(str) {
357-
const buf = Uint8ArrayFromHex(str);
358-
return fromArrayBuffer(
359-
TypedArrayPrototypeGetBuffer(buf),
360-
TypedArrayPrototypeGetByteOffset(buf),
361-
TypedArrayPrototypeGetByteLength(buf),
362-
);
363-
};
364-
365-
ObjectDefineProperty(Buffer, 'fromHex', {
366-
__proto__: null,
367-
value,
368-
configurable: true,
369-
enumerable: false,
370-
writable: true,
371-
});
372-
373-
return value;
374-
},
375-
});
343+
// eslint-disable-next-line node-core/prefer-primordials
344+
if (Uint8ArrayFromHex) {
345+
/**
346+
* Same as Uint8Array.fromHex(hexstring), but returns instance of Buffer.
347+
* Not same as Buffer.from(hexstring), as it performs different validations.
348+
* @param {string} str
349+
* @returns {Buffer}
350+
*/
351+
Buffer.fromHex = function fromHex(str) {
352+
const buf = Uint8ArrayFromHex(str);
353+
return fromArrayBuffer(
354+
TypedArrayPrototypeGetBuffer(buf),
355+
TypedArrayPrototypeGetByteOffset(buf),
356+
TypedArrayPrototypeGetByteLength(buf));
357+
};
358+
}
359+
360+
// eslint-disable-next-line node-core/prefer-primordials
361+
if (Uint8ArrayFromBase64) {
362+
/**
363+
* Same as Uint8Array.fromBase64(base64string, options), but returns instance of Buffer.
364+
* @param {string} str
365+
* @param {object} [options]
366+
* @returns {Buffer}
367+
*/
368+
Buffer.fromBase64 = function fromBase64(str, options) {
369+
const buf = Uint8ArrayFromBase64(str, options);
370+
return fromArrayBuffer(
371+
TypedArrayPrototypeGetBuffer(buf),
372+
TypedArrayPrototypeGetByteOffset(buf),
373+
TypedArrayPrototypeGetByteLength(buf));
374+
};
375+
}
376376

377-
/**
378-
* Same as Uint8Array.fromBase64(base64string, options), but returns instance of Buffer.
379-
* @function Buffer.fromBase64
380-
* @param {string} str
381-
* @param {object} [options]
382-
* @returns {Buffer}
383-
*/
384-
ObjectDefineProperty(Buffer, 'fromBase64', {
385-
__proto__: null,
386-
configurable: true,
387-
enumerable: false,
388-
get() {
389-
// TODO(LiviaMedeiros): make unconditional and use primordial once `--js-base-64` is not optional
390-
// eslint-disable-next-line node-core/prefer-primordials
391-
const Uint8ArrayFromBase64 = Uint8Array.fromBase64;
392-
const value = Uint8ArrayFromBase64 === undefined ?
393-
undefined :
394-
function fromBase64(str, options) {
395-
const buf = Uint8ArrayFromBase64(str, options);
396-
return fromArrayBuffer(
397-
TypedArrayPrototypeGetBuffer(buf),
398-
TypedArrayPrototypeGetByteOffset(buf),
399-
TypedArrayPrototypeGetByteLength(buf),
400-
);
401-
};
402-
403-
ObjectDefineProperty(Buffer, 'fromBase64', {
404-
__proto__: null,
405-
value,
406-
configurable: true,
407-
enumerable: false,
408-
writable: true,
409-
});
410-
411-
return value;
412-
},
413-
});
414377

415378
/**
416379
* Creates the Buffer as a copy of the underlying ArrayBuffer of the view

0 commit comments

Comments
 (0)