mirror of
https://github.com/amark/gun.git
synced 2025-06-05 21:56:51 +00:00
Rolled back classical classes for SeaArray & SafeBuffer
This commit is contained in:
parent
012fd7f18c
commit
d8a8df7ae7
70
sea.js
70
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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user