Rolled back classical classes for SeaArray & SafeBuffer

This commit is contained in:
Mika 2018-02-05 08:49:07 +02:00 committed by GitHub
parent 012fd7f18c
commit d8a8df7ae7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

38
sea.js
View File

@ -51,9 +51,12 @@
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) {
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)
@ -70,21 +73,20 @@
return btoa(this)
}
}
}
// This is Buffer implementation used in SEA. Functionality is mostly
// compatible with NodeJS 'safe-buffer' and is used for encoding conversions
// 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
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