Skip to content

Commit c2f669d

Browse files
committed
buffer: add Buffer.fromHex() and Buffer.fromBase64()
1 parent 55600e6 commit c2f669d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/buffer.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,34 @@ Buffer.from = function from(value, encodingOrOffset, length) {
336336
);
337337
};
338338

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+
* @param {string} str
343+
* @returns {Buffer}
344+
*/
345+
Buffer.fromHex = function fromHex(str) {
346+
const buf = Uint8Array.fromHex(str);
347+
return fromArrayBuffer(
348+
TypedArrayPrototypeGetBuffer(buf),
349+
TypedArrayPrototypeGetByteOffset(buf),
350+
TypedArrayPrototypeGetByteLength(buf));
351+
};
352+
353+
/**
354+
* Same as Uint8Array.fromBase64(base64string, options), but returns instance of Buffer.
355+
* @param {string} str
356+
* @param {object} [options]
357+
* @returns {Buffer}
358+
*/
359+
Buffer.fromBase64 = function fromBase64(str, options) {
360+
const buf = Uint8Array.fromBase64(str, options);
361+
return fromArrayBuffer(
362+
TypedArrayPrototypeGetBuffer(buf),
363+
TypedArrayPrototypeGetByteOffset(buf),
364+
TypedArrayPrototypeGetByteLength(buf));
365+
};
366+
339367
/**
340368
* Creates the Buffer as a copy of the underlying ArrayBuffer of the view
341369
* rather than the contents of the view.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { Buffer } = require('buffer');
5+
6+
assert.deepStrictEqual(Buffer.fromHex('f00dcafe'), Buffer.from('f00dcafe', 'hex'));
7+
assert.deepStrictEqual(Buffer.fromHex('F00DCAFE'), Buffer.from('f00dcafe', 'hex'));
8+
assert.deepStrictEqual(Buffer.fromHex(''), Buffer.from('', 'hex'));
9+
10+
assert.throws(() => Buffer.fromHex('0x'), { name: 'SyntaxError' });
11+
assert.throws(() => Buffer.fromHex('a'), { name: 'SyntaxError' });
12+
assert.throws(() => Buffer.fromHex(123), { name: 'TypeError' });
13+
assert.throws(() => Buffer.fromHex('abggcd00'), { name: 'SyntaxError' });
14+
15+
assert.deepStrictEqual(Buffer.fromBase64('SGVsbG8='), Buffer.from('SGVsbG8=', 'base64'));
16+
assert.deepStrictEqual(Buffer.fromBase64('SGV sbG8='), Buffer.from('SGVsbG8=', 'base64'));
17+
18+
assert.deepStrictEqual(
19+
Buffer.fromBase64('PGJsZXA-PC9ibGVwPg', { alphabet: 'base64url' }),
20+
Buffer.from('PGJsZXA+PC9ibGVwPg==', 'base64'),
21+
);
22+
23+
assert.deepStrictEqual(Buffer.fromBase64('SGVsbG8=', { lastChunkHandling: 'strict' }), Buffer.from('Hello'));
24+
assert.throws(() => Buffer.fromBase64('SGVsbG8', { lastChunkHandling: 'strict' }), { name: 'SyntaxError' });
25+
26+
assert.deepStrictEqual(
27+
Buffer.fromBase64('SGVsbG8', { lastChunkHandling: 'stop-before-partial' }),
28+
Buffer.from('SGVs', 'base64'),
29+
);
30+
31+
assert.throws(() => Buffer.fromBase64('SGV$sbG8=', {}), { name: 'SyntaxError' });
32+
assert.throws(() => Buffer.fromBase64('S', {}), { name: 'SyntaxError' });
33+
assert.throws(() => Buffer.fromBase64(123), { name: 'TypeError' });
34+
assert.throws(() => Buffer.fromBase64('SGVsbG8=', { alphabet: 'unknown' }), { name: 'TypeError' });

0 commit comments

Comments
 (0)