Skip to content
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ instance methods, and class methods that are supported.
To use this module directly (without browserify), install it:

```bash
npm install buffer
npm install @caspertech/buffer
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want this merged, please remove this

Suggested change
npm install @caspertech/buffer
npm install buffer

```

This module was previously called **native-buffer-browserify**, but please use **buffer**
Expand Down
17 changes: 9 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class Buffer extends Uint8Array {
compare(otherBuffer: Uint8Array, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): number;
copy(targetBuffer: Buffer, targetStart?: number, sourceStart?: number, sourceEnd?: number): number;
slice(start?: number, end?: number): Buffer;
subarray(start?: number, end?: number): Buffer;
writeUIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
writeUIntBE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
writeIntLE(value: number, offset: number, byteLength: number, noAssert?: boolean): number;
Expand All @@ -20,15 +21,15 @@ export class Buffer extends Uint8Array {
readUInt16BE(offset: number, noAssert?: boolean): number;
readUInt32LE(offset: number, noAssert?: boolean): number;
readUInt32BE(offset: number, noAssert?: boolean): number;
readBigUInt64LE(offset: number): BigInt;
readBigUInt64BE(offset: number): BigInt;
readBigUInt64LE(offset: number): bigint;
readBigUInt64BE(offset: number): bigint;
readInt8(offset: number, noAssert?: boolean): number;
readInt16LE(offset: number, noAssert?: boolean): number;
readInt16BE(offset: number, noAssert?: boolean): number;
readInt32LE(offset: number, noAssert?: boolean): number;
readInt32BE(offset: number, noAssert?: boolean): number;
readBigInt64LE(offset: number): BigInt;
readBigInt64BE(offset: number): BigInt;
readBigInt64LE(offset: number): bigint;
readBigInt64BE(offset: number): bigint;
readFloatLE(offset: number, noAssert?: boolean): number;
readFloatBE(offset: number, noAssert?: boolean): number;
readDoubleLE(offset: number, noAssert?: boolean): number;
Expand All @@ -42,15 +43,15 @@ export class Buffer extends Uint8Array {
writeUInt16BE(value: number, offset: number, noAssert?: boolean): number;
writeUInt32LE(value: number, offset: number, noAssert?: boolean): number;
writeUInt32BE(value: number, offset: number, noAssert?: boolean): number;
writeBigUInt64LE(value: number, offset: number): BigInt;
writeBigUInt64BE(value: number, offset: number): BigInt;
writeBigUInt64LE(value: bigint, offset: number): number;
writeBigUInt64BE(value: bigint, offset: number): number;
writeInt8(value: number, offset: number, noAssert?: boolean): number;
writeInt16LE(value: number, offset: number, noAssert?: boolean): number;
writeInt16BE(value: number, offset: number, noAssert?: boolean): number;
writeInt32LE(value: number, offset: number, noAssert?: boolean): number;
writeInt32BE(value: number, offset: number, noAssert?: boolean): number;
writeBigInt64LE(value: number, offset: number): BigInt;
writeBigInt64BE(value: number, offset: number): BigInt;
writeBigInt64LE(value: bigint, offset: number): number;
writeBigInt64BE(value: bigint, offset: number): number;
writeFloatLE(value: number, offset: number, noAssert?: boolean): number;
writeFloatBE(value: number, offset: number, noAssert?: boolean): number;
writeDoubleLE(value: number, offset: number, noAssert?: boolean): number;
Expand Down
125 changes: 91 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ const customInspectSymbol =
? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
: null

const decoderUTF8 = new TextDecoder('utf8')

exports.Buffer = Buffer
exports.SlowBuffer = SlowBuffer
exports.INSPECT_MAX_BYTES = 50

const K_MAX_LENGTH = 0x7fffffff
exports.kMaxLength = K_MAX_LENGTH

// not used, but value is added for maintain api compatability
// max length will vary from browser to browser, but using a likely expected value circa Node v8
const K_STRING_MAX_LENGTH = 2**28 - 1
exports.kStringMaxLength = K_STRING_MAX_LENGTH

exports.constants = {
MAX_LENGTH: K_MAX_LENGTH,
MAX_STRING_LENGTH: K_STRING_MAX_LENGTH
}

/**
* If `Buffer.TYPED_ARRAY_SUPPORT`:
* === true Use Uint8Array implementation (fastest)
Expand Down Expand Up @@ -376,6 +388,7 @@ Buffer.isEncoding = function isEncoding (encoding) {
case 'ascii':
case 'latin1':
case 'binary':
case 'base64url':
case 'base64':
case 'ucs2':
case 'ucs-2':
Expand Down Expand Up @@ -533,8 +546,9 @@ function slowToString (encoding, start, end) {
case 'binary':
return latin1Slice(this, start, end)

case 'base64url':
case 'base64':
return base64Slice(this, start, end)
return base64Slice(this, start, end, encoding)

case 'ucs2':
case 'ucs-2':
Expand Down Expand Up @@ -844,9 +858,12 @@ function hexWrite (buf, string, offset, length) {
}
let i
for (i = 0; i < length; ++i) {
const parsed = parseInt(string.substr(i * 2, 2), 16)
if (numberIsNaN(parsed)) return i
buf[offset + i] = parsed
const a = hexCharValueTable[string[i * 2]]
const b = hexCharValueTable[string[i * 2 + 1]]
if (a === undefined || b === undefined) {
return i
}
buf[offset + i] = a << 4 | b
}
return i
}
Expand All @@ -859,8 +876,9 @@ function asciiWrite (buf, string, offset, length) {
return blitBuffer(asciiToBytes(string), buf, offset, length)
}

function base64Write (buf, string, offset, length) {
return blitBuffer(base64ToBytes(string), buf, offset, length)
function base64Write (buf, string, offset, length, encoding) {
const b64 = encoding === 'base64url' ? base64urlToBase64(string) : string
return blitBuffer(base64ToBytes(b64), buf, offset, length)
}

function ucs2Write (buf, string, offset, length) {
Expand Down Expand Up @@ -918,9 +936,11 @@ Buffer.prototype.write = function write (string, offset, length, encoding) {
case 'binary':
return asciiWrite(this, string, offset, length)

case 'base64url':
case 'base64':
// console.log(encoding, '::', string)
// Warning: maxLength not taken into account in base64Write
return base64Write(this, string, offset, length)
return base64Write(this, string, offset, length, encoding)

case 'ucs2':
case 'ucs-2':
Expand All @@ -943,15 +963,24 @@ Buffer.prototype.toJSON = function toJSON () {
}
}

function base64Slice (buf, start, end) {
function base64Slice (buf, start, end, encoding) {
let b64
if (start === 0 && end === buf.length) {
return base64.fromByteArray(buf)
b64 = base64.fromByteArray(buf)
} else {
return base64.fromByteArray(buf.slice(start, end))
b64 = base64.fromByteArray(buf.slice(start, end))
}
return encoding === 'base64url' ? base64urlFromBase64(b64) : b64
}

// For smaller buffers than this TextDecoder#decode appears
// to have more overhead than doing it in Javascript directly.
const TEXT_DECODER_THRESHOLD = 7000

function utf8Slice (buf, start, end) {
if ((end - start) > TEXT_DECODER_THRESHOLD) {
return decoderUTF8.decode(buf.slice(start, end))
}
end = Math.min(buf.length, end)
const res = []

Expand Down Expand Up @@ -1215,14 +1244,14 @@ Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE (
}

const lo = first +
this[++offset] * 2 ** 8 +
this[++offset] * 2 ** 16 +
this[++offset] * 2 ** 24
this[++offset] * 256 +
this[++offset] * 65536 +
this[++offset] * 16777216

const hi = this[++offset] +
this[++offset] * 2 ** 8 +
this[++offset] * 2 ** 16 +
last * 2 ** 24
this[++offset] * 256 +
this[++offset] * 65536 +
last * 16777216

return BigInt(lo) + (BigInt(hi) << BigInt(32))
})
Expand All @@ -1236,14 +1265,14 @@ Buffer.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE (
boundsError(offset, this.length - 8)
}

const hi = first * 2 ** 24 +
this[++offset] * 2 ** 16 +
this[++offset] * 2 ** 8 +
const hi = first * 16777216 +
this[++offset] * 65536 +
this[++offset] * 256 +
this[++offset]

const lo = this[++offset] * 2 ** 24 +
this[++offset] * 2 ** 16 +
this[++offset] * 2 ** 8 +
const lo = this[++offset] * 16777216 +
this[++offset] * 65536 +
this[++offset] * 256 +
last

return (BigInt(hi) << BigInt(32)) + BigInt(lo)
Expand Down Expand Up @@ -1336,15 +1365,15 @@ Buffer.prototype.readBigInt64LE = defineBigIntMethod(function readBigInt64LE (of
}

const val = this[offset + 4] +
this[offset + 5] * 2 ** 8 +
this[offset + 6] * 2 ** 16 +
this[offset + 5] * 256 +
this[offset + 6] * 65536 +
(last << 24) // Overflow

return (BigInt(val) << BigInt(32)) +
BigInt(first +
this[++offset] * 2 ** 8 +
this[++offset] * 2 ** 16 +
this[++offset] * 2 ** 24)
this[++offset] * 256 +
this[++offset] * 65536 +
this[++offset] * 16777216)
})

Buffer.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE (offset) {
Expand All @@ -1357,14 +1386,14 @@ Buffer.prototype.readBigInt64BE = defineBigIntMethod(function readBigInt64BE (of
}

const val = (first << 24) + // Overflow
this[++offset] * 2 ** 16 +
this[++offset] * 2 ** 8 +
this[++offset] * 65536 +
this[++offset] * 256 +
this[++offset]

return (BigInt(val) << BigInt(32)) +
BigInt(this[++offset] * 2 ** 24 +
this[++offset] * 2 ** 16 +
this[++offset] * 2 ** 8 +
BigInt(this[++offset] * 16777216 +
this[++offset] * 65536 +
this[++offset] * 256 +
last)
})

Expand Down Expand Up @@ -1861,11 +1890,11 @@ E('ERR_OUT_OF_RANGE',
function (str, range, input) {
let msg = `The value of "${str}" is out of range.`
let received = input
if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) {
if (Number.isInteger(input) && Math.abs(input) > 4294967296) {
received = addNumericalSeparator(String(input))
} else if (typeof input === 'bigint') {
received = String(input)
if (input > BigInt(2) ** BigInt(32) || input < -(BigInt(2) ** BigInt(32))) {
if (input > BigInt(4294967296) || input < -(BigInt(4294967296))) {
received = addNumericalSeparator(received)
}
received += 'n'
Expand Down Expand Up @@ -1939,6 +1968,23 @@ function boundsError (value, length, type) {

const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g

const BASE64_CHAR_62 = '+'
const BASE64_CHAR_63 = '/'
const BASE64URL_CHAR_62 = '-'
const BASE64URL_CHAR_63 = '_'

function base64urlToBase64 (str) {
return str
.replaceAll(BASE64URL_CHAR_62, BASE64_CHAR_62)
.replaceAll(BASE64URL_CHAR_63, BASE64_CHAR_63)
}

function base64urlFromBase64 (str) {
return str
.replaceAll(BASE64_CHAR_62, BASE64URL_CHAR_62)
.replaceAll(BASE64_CHAR_63, BASE64URL_CHAR_63)
}

function base64clean (str) {
// Node takes equal signs as end of the Base64 encoding
str = str.split('=')[0]
Expand Down Expand Up @@ -2098,6 +2144,17 @@ const hexSliceLookupTable = (function () {
return table
})()

// Lookup table for Buffer.from(x, 'hex')
const hexCharValueTable = (function () {
const alphabet = '0123456789abcdefABCDEF'
const table = {}
for (let i = 0; i < 22; ++i) {
// ABCDEF should be same value as abcdef
table[alphabet[i]] = i < 16 ? i : i - 6
}
return table
})()

// Return not function with Error if BigInt not supported
function defineBigIntMethod (fn) {
return typeof BigInt === 'undefined' ? BufferBigIntNotDefined : fn
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "buffer",
"name": "@caspertech/buffer",
"description": "Node.js Buffer API, for the browser",
"version": "6.0.3",
"version": "6.0.8",
"author": {
"name": "Feross Aboukhadijeh",
"email": "feross@feross.org",
Expand Down Expand Up @@ -66,7 +66,9 @@
"test-browser-new": "airtap -- test/*.js test/node/*.js",
"test-browser-new-local": "airtap --local -- test/*.js test/node/*.js",
"test-node": "tape test/*.js test/node/*.js",
"update-authors": "./bin/update-authors.sh"
"update-authors": "./bin/update-authors.sh",
"lint": "standard",
"lint:fix": "standard --fix"
},
"standard": {
"ignore": [
Expand Down
30 changes: 20 additions & 10 deletions perf/readUtf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ var suite = util.suite()
// 256 random bytes
var array = [ 152, 130, 206, 23, 243, 238, 197, 44, 27, 86, 208, 36, 163, 184, 164, 21, 94, 242, 178, 46, 25, 26, 253, 178, 72, 147, 207, 112, 236, 68, 179, 190, 29, 83, 239, 147, 125, 55, 143, 19, 157, 68, 157, 58, 212, 224, 150, 39, 128, 24, 94, 225, 120, 121, 75, 192, 112, 19, 184, 142, 203, 36, 43, 85, 26, 147, 227, 139, 242, 186, 57, 78, 11, 102, 136, 117, 180, 210, 241, 92, 3, 215, 54, 167, 249, 1, 44, 225, 146, 86, 2, 42, 68, 21, 47, 238, 204, 153, 216, 252, 183, 66, 222, 255, 15, 202, 16, 51, 134, 1, 17, 19, 209, 76, 238, 38, 76, 19, 7, 103, 249, 5, 107, 137, 64, 62, 170, 57, 16, 85, 179, 193, 97, 86, 166, 196, 36, 148, 138, 193, 210, 69, 187, 38, 242, 97, 195, 219, 252, 244, 38, 1, 197, 18, 31, 246, 53, 47, 134, 52, 105, 72, 43, 239, 128, 203, 73, 93, 199, 75, 222, 220, 166, 34, 63, 236, 11, 212, 76, 243, 171, 110, 78, 39, 205, 204, 6, 177, 233, 212, 243, 0, 33, 41, 122, 118, 92, 252, 0, 157, 108, 120, 70, 137, 100, 223, 243, 171, 232, 66, 126, 111, 142, 33, 3, 39, 117, 27, 107, 54, 1, 217, 227, 132, 13, 166, 3, 73, 53, 127, 225, 236, 134, 219, 98, 214, 125, 148, 24, 64, 142, 111, 231, 194, 42, 150, 185, 10, 182, 163, 244, 19, 4, 59, 135, 16 ]

var browserBuffer = new BrowserBuffer(array)
var nodeBuffer = new Buffer(array)
function addTest(size) {
let arr = array;
for(var i = 0; i < size; i++) {
arr = arr.concat(array);
}

suite
.add('BrowserBuffer#readUtf8', function () {
browserBuffer.toString()
})
var browserBuffer = BrowserBuffer.from(arr)
var nodeBuffer = Buffer.from(arr)
suite
.add('BrowserBuffer#readUtf8 ' + nodeBuffer.byteLength, function () {
browserBuffer.toString()
})

if (!process.browser) suite
.add('NodeBuffer#readUtf8', function () {
nodeBuffer.toString()
})
if (!process.browser) suite
.add('NodeBuffer#readUtf8 ' + nodeBuffer.byteLength, function () {
nodeBuffer.toString()
})
}

for(var i = 0; i < 6; i++) {
addTest(i * 10);
}
18 changes: 18 additions & 0 deletions test/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,21 @@ test('base64: high byte', function (t) {
)
t.end()
})

test('base64url: convert to/from base64', function (t) {
const base64url = '8J-Ps--4j_Cfj7PvuI8='
const base64 = '8J+Ps++4j/Cfj7PvuI8='
const text = '🏳️🏳️'

const base64urlBuf = new B(base64url, 'base64url')
t.equal(base64urlBuf.toString('base64'), base64)
t.equal(base64urlBuf.toString(), text)

const base64Buf = new B(base64, 'base64')
t.equal(base64Buf.toString('base64url'), base64url)
t.equal(base64Buf.toString(), text)

const buf = new B(text)
t.equal(buf.toString('base64url'), base64url)
t.end()
})
Loading