From d8a8df7ae747f4318919b1255298d013a1ecf48e Mon Sep 17 00:00:00 2001 From: Mika Date: Mon, 5 Feb 2018 08:49:07 +0200 Subject: [PATCH] Rolled back classical classes for SeaArray & SafeBuffer --- sea.js | 70 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/sea.js b/sea.js index cf75e84a..f87de1ae 100644 --- a/sea.js +++ b/sea.js @@ -51,24 +51,26 @@ global.sessionStorage = sessionStorage global.localStorage = localStorage } + // This is Array extended to have .toString(['utf8'|'hex'|'base64']) - class SeaArray extends Array { - toString(enc = 'utf8', start = 0, end) { - const { length } = this - if (enc === 'hex') { - const buf = new Uint8Array(this) - return [ ...Array(((end && (end + 1)) || length) - start).keys()] - .map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('') - } - if (enc === 'utf8') { - return Array.from( - { length: (end || length) - start }, - (_, i) => String.fromCharCode(this[ i + start]) - ).join('') - } - if (enc === 'base64') { - return btoa(this) - } + function SeaArray() {} + Object.assign(SeaArray, { from: Array.from }) + SeaArray.prototype = Object.create(Array.prototype) + SeaArray.prototype.toString = function(enc = 'utf8', start = 0, end) { + const { length } = this + if (enc === 'hex') { + const buf = new Uint8Array(this) + return [ ...Array(((end && (end + 1)) || length) - start).keys()] + .map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('') + } + if (enc === 'utf8') { + return Array.from( + { length: (end || length) - start }, + (_, i) => String.fromCharCode(this[ i + start]) + ).join('') + } + if (enc === 'base64') { + return btoa(this) } } @@ -77,14 +79,14 @@ // between binary and 'hex' | 'utf8' | 'base64' // See documentation and validation for safe implementation in: // https://github.com/feross/safe-buffer#update - class SafeBuffer extends SeaArray { - constructor(...props) { - super() - this.from = SafeBuffer.from - return SafeBuffer.from(...props) - } + function SafeBuffer(...props) { + console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()') + return SafeBuffer.from(...props) + } + SafeBuffer.prototype = Object.create(Array.prototype) + Object.assign(SafeBuffer, { // (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64' - static from() { + from() { if (!Object.keys(arguments).length) { throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.') } @@ -102,13 +104,13 @@ } else if (enc === 'utf8') { const { length } = input const words = new Uint16Array(length) - SeaArray.from({ length }, (_, i) => words[i] = input.charCodeAt(i)) + Array.from({ length }, (_, i) => words[i] = input.charCodeAt(i)) buf = SeaArray.from(words) } else if (enc === 'base64') { const dec = atob(input) const { length } = dec const bytes = new Uint8Array(length) - SeaArray.from({ length }, (_, i) => bytes[i] = dec.charCodeAt(i)) + Array.from({ length }, (_, i) => bytes[i] = dec.charCodeAt(i)) buf = SeaArray.from(bytes) } else if (enc === 'binary') { buf = SeaArray.from(input) @@ -125,23 +127,25 @@ } return SeaArray.from(buf || input) } - } + }, // This is 'safe-buffer.alloc' sans encoding support - static alloc(length, fill = 0 /*, enc*/ ) { + alloc(length, fill = 0 /*, enc*/ ) { return SeaArray.from(new Uint8Array(Array.from({ length }, () => fill))) - } + }, // This is normal UNSAFE 'buffer.alloc' or 'new Buffer(length)' - don't use! - static allocUnsafe(length) { + allocUnsafe(length) { return SeaArray.from(new Uint8Array(Array.from({ length }))) - } + }, // This puts together array of array like members - static concat(arr) { // octet array + concat(arr) { // octet array if (!Array.isArray(arr)) { throw new TypeError('First argument must be Array containing ArrayBuffer or Uint8Array instances.') } return SeaArray.from(arr.reduce((ret, item) => ret.concat(Array.from(item)), [])) } - } + }) + SafeBuffer.prototype.from = SafeBuffer.from + SafeBuffer.prototype.toString = SeaArray.prototype.toString const Buffer = SafeBuffer