Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,34 @@
);
};

/**
* Same as Uint8Array.fromHex(hexstring), but returns instance of Buffer.
* Not same as Buffer.from(hexstring), as it performs different validations.
* @param {string} str
* @returns {Buffer}
*/
Buffer.fromHex = function fromHex(str) {
const buf = Uint8Array.fromHex(str);

Check failure on line 346 in lib/buffer.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use `const { Uint8ArrayFromHex } = primordials;` instead of the global
return fromArrayBuffer(
TypedArrayPrototypeGetBuffer(buf),
TypedArrayPrototypeGetByteOffset(buf),
TypedArrayPrototypeGetByteLength(buf));
};

/**
* Same as Uint8Array.fromBase64(base64string, options), but returns instance of Buffer.
* @param {string} str
* @param {object} [options]
* @returns {Buffer}
*/
Buffer.fromBase64 = function fromBase64(str, options) {
const buf = Uint8Array.fromBase64(str, options);

Check failure on line 360 in lib/buffer.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Use `const { Uint8ArrayFromBase64 } = primordials;` instead of the global
return fromArrayBuffer(
TypedArrayPrototypeGetBuffer(buf),
TypedArrayPrototypeGetByteOffset(buf),
TypedArrayPrototypeGetByteLength(buf));
};

/**
* Creates the Buffer as a copy of the underlying ArrayBuffer of the view
* rather than the contents of the view.
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-buffer-fromhex-frombase64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
require('../common');
const assert = require('assert');
const { Buffer } = require('buffer');

assert.deepStrictEqual(Buffer.fromHex('f00dcafe'), Buffer.from('f00dcafe', 'hex'));
assert.deepStrictEqual(Buffer.fromHex('F00DCAFE'), Buffer.from('f00dcafe', 'hex'));
assert.deepStrictEqual(Buffer.fromHex(''), Buffer.from('', 'hex'));

assert.throws(() => Buffer.fromHex('0x'), { name: 'SyntaxError' });
assert.throws(() => Buffer.fromHex('a'), { name: 'SyntaxError' });
assert.throws(() => Buffer.fromHex(123), { name: 'TypeError' });
assert.throws(() => Buffer.fromHex('abggcd00'), { name: 'SyntaxError' });

assert.deepStrictEqual(Buffer.fromBase64('SGVsbG8='), Buffer.from('SGVsbG8=', 'base64'));
assert.deepStrictEqual(Buffer.fromBase64('SGV sbG8='), Buffer.from('SGVsbG8=', 'base64'));

assert.deepStrictEqual(
Buffer.fromBase64('PGJsZXA-PC9ibGVwPg', { alphabet: 'base64url' }),
Buffer.from('PGJsZXA+PC9ibGVwPg==', 'base64'),
);

assert.deepStrictEqual(Buffer.fromBase64('SGVsbG8=', { lastChunkHandling: 'strict' }), Buffer.from('Hello'));
assert.throws(() => Buffer.fromBase64('SGVsbG8', { lastChunkHandling: 'strict' }), { name: 'SyntaxError' });

assert.deepStrictEqual(
Buffer.fromBase64('SGVsbG8', { lastChunkHandling: 'stop-before-partial' }),
Buffer.from('SGVs', 'base64'),
);

assert.throws(() => Buffer.fromBase64('SGV$sbG8=', {}), { name: 'SyntaxError' });
assert.throws(() => Buffer.fromBase64('S', {}), { name: 'SyntaxError' });
assert.throws(() => Buffer.fromBase64(123), { name: 'TypeError' });
assert.throws(() => Buffer.fromBase64('SGVsbG8=', { alphabet: 'unknown' }), { name: 'TypeError' });
Loading