mirror of
https://github.com/amark/gun.git
synced 2025-06-07 14:46:44 +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.sessionStorage = sessionStorage
|
||||||
global.localStorage = localStorage
|
global.localStorage = localStorage
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
||||||
class SeaArray extends Array {
|
function SeaArray() {}
|
||||||
toString(enc = 'utf8', start = 0, end) {
|
Object.assign(SeaArray, { from: Array.from })
|
||||||
const { length } = this
|
SeaArray.prototype = Object.create(Array.prototype)
|
||||||
if (enc === 'hex') {
|
SeaArray.prototype.toString = function(enc = 'utf8', start = 0, end) {
|
||||||
const buf = new Uint8Array(this)
|
const { length } = this
|
||||||
return [ ...Array(((end && (end + 1)) || length) - start).keys()]
|
if (enc === 'hex') {
|
||||||
.map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('')
|
const buf = new Uint8Array(this)
|
||||||
}
|
return [ ...Array(((end && (end + 1)) || length) - start).keys()]
|
||||||
if (enc === 'utf8') {
|
.map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('')
|
||||||
return Array.from(
|
}
|
||||||
{ length: (end || length) - start },
|
if (enc === 'utf8') {
|
||||||
(_, i) => String.fromCharCode(this[ i + start])
|
return Array.from(
|
||||||
).join('')
|
{ length: (end || length) - start },
|
||||||
}
|
(_, i) => String.fromCharCode(this[ i + start])
|
||||||
if (enc === 'base64') {
|
).join('')
|
||||||
return btoa(this)
|
}
|
||||||
}
|
if (enc === 'base64') {
|
||||||
|
return btoa(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,14 +79,14 @@
|
|||||||
// between binary and 'hex' | 'utf8' | 'base64'
|
// between binary and 'hex' | 'utf8' | 'base64'
|
||||||
// See documentation and validation for safe implementation in:
|
// See documentation and validation for safe implementation in:
|
||||||
// https://github.com/feross/safe-buffer#update
|
// https://github.com/feross/safe-buffer#update
|
||||||
class SafeBuffer extends SeaArray {
|
function SafeBuffer(...props) {
|
||||||
constructor(...props) {
|
console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')
|
||||||
super()
|
return SafeBuffer.from(...props)
|
||||||
this.from = 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'
|
// (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'
|
||||||
static from() {
|
from() {
|
||||||
if (!Object.keys(arguments).length) {
|
if (!Object.keys(arguments).length) {
|
||||||
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
|
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
|
||||||
}
|
}
|
||||||
@ -102,13 +104,13 @@
|
|||||||
} else if (enc === 'utf8') {
|
} else if (enc === 'utf8') {
|
||||||
const { length } = input
|
const { length } = input
|
||||||
const words = new Uint16Array(length)
|
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)
|
buf = SeaArray.from(words)
|
||||||
} else if (enc === 'base64') {
|
} else if (enc === 'base64') {
|
||||||
const dec = atob(input)
|
const dec = atob(input)
|
||||||
const { length } = dec
|
const { length } = dec
|
||||||
const bytes = new Uint8Array(length)
|
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)
|
buf = SeaArray.from(bytes)
|
||||||
} else if (enc === 'binary') {
|
} else if (enc === 'binary') {
|
||||||
buf = SeaArray.from(input)
|
buf = SeaArray.from(input)
|
||||||
@ -125,23 +127,25 @@
|
|||||||
}
|
}
|
||||||
return SeaArray.from(buf || input)
|
return SeaArray.from(buf || input)
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
// This is 'safe-buffer.alloc' sans encoding support
|
// 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)))
|
return SeaArray.from(new Uint8Array(Array.from({ length }, () => fill)))
|
||||||
}
|
},
|
||||||
// This is normal UNSAFE 'buffer.alloc' or 'new Buffer(length)' - don't use!
|
// 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 })))
|
return SeaArray.from(new Uint8Array(Array.from({ length })))
|
||||||
}
|
},
|
||||||
// This puts together array of array like members
|
// This puts together array of array like members
|
||||||
static concat(arr) { // octet array
|
concat(arr) { // octet array
|
||||||
if (!Array.isArray(arr)) {
|
if (!Array.isArray(arr)) {
|
||||||
throw new TypeError('First argument must be Array containing ArrayBuffer or Uint8Array instances.')
|
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)), []))
|
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
|
const Buffer = SafeBuffer
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user