mirror of
https://github.com/amark/gun.git
synced 2025-06-07 22:56:42 +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
|
global.localStorage = localStorage
|
||||||
}
|
}
|
||||||
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
||||||
function SeaArray() {}
|
class SeaArray extends Array {
|
||||||
Object.assign(SeaArray, { from: Array.from })
|
toString(enc = 'utf8', start = 0, end) {
|
||||||
SeaArray.prototype = Object.create(Array.prototype)
|
|
||||||
SeaArray.prototype.toString = function(enc = 'utf8', start = 0, end) {
|
|
||||||
const { length } = this
|
const { length } = this
|
||||||
if (enc === 'hex') {
|
if (enc === 'hex') {
|
||||||
const buf = new Uint8Array(this)
|
const buf = new Uint8Array(this)
|
||||||
@ -72,6 +70,7 @@
|
|||||||
return btoa(this)
|
return btoa(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This is Buffer implementation used in SEA. Functionality is mostly
|
// This is Buffer implementation used in SEA. Functionality is mostly
|
||||||
// compatible with NodeJS 'safe-buffer' and is used for encoding conversions
|
// 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()')
|
console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')
|
||||||
return SafeBuffer.from(...props)
|
return SafeBuffer.from(...props)
|
||||||
}
|
}
|
||||||
SafeBuffer.prototype = Object.create(Array.prototype)
|
SafeBuffer.prototype = Object.create(SeaArray.prototype)
|
||||||
Object.assign(SafeBuffer, {
|
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'
|
||||||
from() {
|
from() {
|
||||||
@ -103,13 +102,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)
|
||||||
Array.from({ length }, (_, i) => words[i] = input.charCodeAt(i))
|
SeaArray.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)
|
||||||
Array.from({ length }, (_, i) => bytes[i] = dec.charCodeAt(i))
|
SeaArray.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)
|
||||||
@ -144,7 +143,6 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
SafeBuffer.prototype.from = SafeBuffer.from
|
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