mirror of
https://github.com/amark/gun.git
synced 2025-06-07 06:36:46 +00:00
Converted SeaArray to ES6 class & some cleanup
This commit is contained in:
parent
5f29bd5a54
commit
a5bb0716c3
14
sea.js
14
sea.js
@ -52,10 +52,8 @@
|
||||
global.localStorage = localStorage
|
||||
}
|
||||
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
||||
function SeaArray() {}
|
||||
Object.assign(SeaArray, { from: Array.from })
|
||||
SeaArray.prototype = Object.create(Array.prototype)
|
||||
SeaArray.prototype.toString = function(enc = 'utf8', start = 0, end) {
|
||||
class SeaArray extends Array {
|
||||
toString(enc = 'utf8', start = 0, end) {
|
||||
const { length } = this
|
||||
if (enc === 'hex') {
|
||||
const buf = new Uint8Array(this)
|
||||
@ -72,6 +70,7 @@
|
||||
return btoa(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This is Buffer implementation used in SEA. Functionality is mostly
|
||||
// compatible with NodeJS 'safe-buffer' and is used for encoding conversions
|
||||
@ -82,7 +81,7 @@
|
||||
console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')
|
||||
return SafeBuffer.from(...props)
|
||||
}
|
||||
SafeBuffer.prototype = Object.create(Array.prototype)
|
||||
SafeBuffer.prototype = Object.create(SeaArray.prototype)
|
||||
Object.assign(SafeBuffer, {
|
||||
// (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'
|
||||
from() {
|
||||
@ -103,13 +102,13 @@
|
||||
} else if (enc === 'utf8') {
|
||||
const { length } = input
|
||||
const words = new Uint16Array(length)
|
||||
Array.from({ length }, (_, i) => words[i] = input.charCodeAt(i))
|
||||
SeaArray.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)
|
||||
Array.from({ length }, (_, i) => bytes[i] = dec.charCodeAt(i))
|
||||
SeaArray.from({ length }, (_, i) => bytes[i] = dec.charCodeAt(i))
|
||||
buf = SeaArray.from(bytes)
|
||||
} else if (enc === 'binary') {
|
||||
buf = SeaArray.from(input)
|
||||
@ -144,7 +143,6 @@
|
||||
}
|
||||
})
|
||||
SafeBuffer.prototype.from = SafeBuffer.from
|
||||
SafeBuffer.prototype.toString = SeaArray.prototype.toString
|
||||
|
||||
const Buffer = SafeBuffer
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user