This commit is contained in:
2025-01-04 00:34:03 +01:00
parent 41829408dc
commit 0ca14bbc19
18111 changed files with 1871397 additions and 0 deletions

21
resources/app/node_modules/level-transcoder/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright © 2012 The contributors to level-transcoder and level-codec.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,13 @@
# Upgrade Guide
This document describes breaking changes and how to upgrade. For a complete list of changes including minor and patch releases, please refer to the [changelog](CHANGELOG.md).
## 1.0.0
This is the initial release of `level-transcoder`, which was forked from `level-codec`. Ultimately `level-transcoder` got a completely different API, so the two modules are not interchangeable. That said, here are the high-level differences from `level-codec` just for the record:
- Throws if an encoding is not found, rather than falling back to `'id'` encoding
- The `'binary'` encoding has been renamed to `'buffer'`, with `'binary'` as an alias
- The `'utf8'` encoding of `level-codec` did not touch Buffers. In `level-transcoder` the same encoding will call `buffer.toString('utf8')` for consistency. Consumers can use the `'buffer'` encoding to avoid this conversion.
- The `'id'` encoding (aliased as `'none'`) which wasn't supported by any active `abstract-leveldown` implementation, has been removed.
- The `'ascii'`, `'ucs2'` and `'utf16le'` encodings are not supported.

158
resources/app/node_modules/level-transcoder/index.js generated vendored Normal file
View File

@@ -0,0 +1,158 @@
'use strict'
const ModuleError = require('module-error')
const encodings = require('./lib/encodings')
const { Encoding } = require('./lib/encoding')
const { BufferFormat, ViewFormat, UTF8Format } = require('./lib/formats')
const kFormats = Symbol('formats')
const kEncodings = Symbol('encodings')
const validFormats = new Set(['buffer', 'view', 'utf8'])
/** @template T */
class Transcoder {
/**
* @param {Array<'buffer'|'view'|'utf8'>} formats
*/
constructor (formats) {
if (!Array.isArray(formats)) {
throw new TypeError("The first argument 'formats' must be an array")
} else if (!formats.every(f => validFormats.has(f))) {
// Note: we only only support aliases in key- and valueEncoding options (where we already did)
throw new TypeError("Format must be one of 'buffer', 'view', 'utf8'")
}
/** @type {Map<string|MixedEncoding<any, any, any>, Encoding<any, any, any>>} */
this[kEncodings] = new Map()
this[kFormats] = new Set(formats)
// Register encodings (done early in order to populate encodings())
for (const k in encodings) {
try {
this.encoding(k)
} catch (err) {
/* istanbul ignore if: assertion */
if (err.code !== 'LEVEL_ENCODING_NOT_SUPPORTED') throw err
}
}
}
/**
* @returns {Array<Encoding<any,T,any>>}
*/
encodings () {
return Array.from(new Set(this[kEncodings].values()))
}
/**
* @param {string|MixedEncoding<any, any, any>} encoding
* @returns {Encoding<any, T, any>}
*/
encoding (encoding) {
let resolved = this[kEncodings].get(encoding)
if (resolved === undefined) {
if (typeof encoding === 'string' && encoding !== '') {
resolved = lookup[encoding]
if (!resolved) {
throw new ModuleError(`Encoding '${encoding}' is not found`, {
code: 'LEVEL_ENCODING_NOT_FOUND'
})
}
} else if (typeof encoding !== 'object' || encoding === null) {
throw new TypeError("First argument 'encoding' must be a string or object")
} else {
resolved = from(encoding)
}
const { name, format } = resolved
if (!this[kFormats].has(format)) {
if (this[kFormats].has('view')) {
resolved = resolved.createViewTranscoder()
} else if (this[kFormats].has('buffer')) {
resolved = resolved.createBufferTranscoder()
} else if (this[kFormats].has('utf8')) {
resolved = resolved.createUTF8Transcoder()
} else {
throw new ModuleError(`Encoding '${name}' cannot be transcoded`, {
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
})
}
}
for (const k of [encoding, name, resolved.name, resolved.commonName]) {
this[kEncodings].set(k, resolved)
}
}
return resolved
}
}
exports.Transcoder = Transcoder
/**
* @param {MixedEncoding<any, any, any>} options
* @returns {Encoding<any, any, any>}
*/
function from (options) {
if (options instanceof Encoding) {
return options
}
// Loosely typed for ecosystem compatibility
const maybeType = 'type' in options && typeof options.type === 'string' ? options.type : undefined
const name = options.name || maybeType || `anonymous-${anonymousCount++}`
switch (detectFormat(options)) {
case 'view': return new ViewFormat({ ...options, name })
case 'utf8': return new UTF8Format({ ...options, name })
case 'buffer': return new BufferFormat({ ...options, name })
default: {
throw new TypeError("Format must be one of 'buffer', 'view', 'utf8'")
}
}
}
/**
* If format is not provided, fallback to detecting `level-codec`
* or `multiformats` encodings, else assume a format of buffer.
* @param {MixedEncoding<any, any, any>} options
* @returns {string}
*/
function detectFormat (options) {
if ('format' in options && options.format !== undefined) {
return options.format
} else if ('buffer' in options && typeof options.buffer === 'boolean') {
return options.buffer ? 'buffer' : 'utf8' // level-codec
} else if ('code' in options && Number.isInteger(options.code)) {
return 'view' // multiformats
} else {
return 'buffer'
}
}
/**
* @typedef {import('./lib/encoding').MixedEncoding<TIn,TFormat,TOut>} MixedEncoding
* @template TIn, TFormat, TOut
*/
/**
* @type {Object.<string, Encoding<any, any, any>>}
*/
const aliases = {
binary: encodings.buffer,
'utf-8': encodings.utf8
}
/**
* @type {Object.<string, Encoding<any, any, any>>}
*/
const lookup = {
...encodings,
...aliases
}
let anonymousCount = 0

View File

@@ -0,0 +1,105 @@
'use strict'
const ModuleError = require('module-error')
const formats = new Set(['buffer', 'view', 'utf8'])
/**
* @template TIn, TFormat, TOut
* @abstract
*/
class Encoding {
/**
* @param {IEncoding<TIn,TFormat,TOut>} options
*/
constructor (options) {
/** @type {(data: TIn) => TFormat} */
this.encode = options.encode || this.encode
/** @type {(data: TFormat) => TOut} */
this.decode = options.decode || this.decode
/** @type {string} */
this.name = options.name || this.name
/** @type {string} */
this.format = options.format || this.format
if (typeof this.encode !== 'function') {
throw new TypeError("The 'encode' property must be a function")
}
if (typeof this.decode !== 'function') {
throw new TypeError("The 'decode' property must be a function")
}
this.encode = this.encode.bind(this)
this.decode = this.decode.bind(this)
if (typeof this.name !== 'string' || this.name === '') {
throw new TypeError("The 'name' property must be a string")
}
if (typeof this.format !== 'string' || !formats.has(this.format)) {
throw new TypeError("The 'format' property must be one of 'buffer', 'view', 'utf8'")
}
if (options.createViewTranscoder) {
this.createViewTranscoder = options.createViewTranscoder
}
if (options.createBufferTranscoder) {
this.createBufferTranscoder = options.createBufferTranscoder
}
if (options.createUTF8Transcoder) {
this.createUTF8Transcoder = options.createUTF8Transcoder
}
}
get commonName () {
return /** @type {string} */ (this.name.split('+')[0])
}
/** @return {BufferFormat<TIn,TOut>} */
createBufferTranscoder () {
throw new ModuleError(`Encoding '${this.name}' cannot be transcoded to 'buffer'`, {
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
})
}
/** @return {ViewFormat<TIn,TOut>} */
createViewTranscoder () {
throw new ModuleError(`Encoding '${this.name}' cannot be transcoded to 'view'`, {
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
})
}
/** @return {UTF8Format<TIn,TOut>} */
createUTF8Transcoder () {
throw new ModuleError(`Encoding '${this.name}' cannot be transcoded to 'utf8'`, {
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
})
}
}
exports.Encoding = Encoding
/**
* @typedef {import('./encoding').IEncoding<TIn,TFormat,TOut>} IEncoding
* @template TIn, TFormat, TOut
*/
/**
* @typedef {import('./formats').BufferFormat<TIn,TOut>} BufferFormat
* @template TIn, TOut
*/
/**
* @typedef {import('./formats').ViewFormat<TIn,TOut>} ViewFormat
* @template TIn, TOut
*/
/**
* @typedef {import('./formats').UTF8Format<TIn,TOut>} UTF8Format
* @template TIn, TOut
*/

View File

@@ -0,0 +1,135 @@
'use strict'
const { Buffer } = require('buffer') || { Buffer: { isBuffer: () => false } }
const { textEncoder, textDecoder } = require('./text-endec')()
const { BufferFormat, ViewFormat, UTF8Format } = require('./formats')
/** @type {<T>(v: T) => v} */
const identity = (v) => v
/**
* @type {typeof import('./encodings').utf8}
*/
exports.utf8 = new UTF8Format({
encode: function (data) {
// On node 16.9.1 buffer.toString() is 5x faster than TextDecoder
return Buffer.isBuffer(data)
? data.toString('utf8')
: ArrayBuffer.isView(data)
? textDecoder.decode(data)
: String(data)
},
decode: identity,
name: 'utf8',
createViewTranscoder () {
return new ViewFormat({
encode: function (data) {
return ArrayBuffer.isView(data) ? data : textEncoder.encode(data)
},
decode: function (data) {
return textDecoder.decode(data)
},
name: `${this.name}+view`
})
},
createBufferTranscoder () {
return new BufferFormat({
encode: function (data) {
return Buffer.isBuffer(data)
? data
: ArrayBuffer.isView(data)
? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
: Buffer.from(String(data), 'utf8')
},
decode: function (data) {
return data.toString('utf8')
},
name: `${this.name}+buffer`
})
}
})
/**
* @type {typeof import('./encodings').json}
*/
exports.json = new UTF8Format({
encode: JSON.stringify,
decode: JSON.parse,
name: 'json'
})
/**
* @type {typeof import('./encodings').buffer}
*/
exports.buffer = new BufferFormat({
encode: function (data) {
return Buffer.isBuffer(data)
? data
: ArrayBuffer.isView(data)
? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
: Buffer.from(String(data), 'utf8')
},
decode: identity,
name: 'buffer',
createViewTranscoder () {
return new ViewFormat({
encode: function (data) {
return ArrayBuffer.isView(data) ? data : Buffer.from(String(data), 'utf8')
},
decode: function (data) {
return Buffer.from(data.buffer, data.byteOffset, data.byteLength)
},
name: `${this.name}+view`
})
}
})
/**
* @type {typeof import('./encodings').view}
*/
exports.view = new ViewFormat({
encode: function (data) {
return ArrayBuffer.isView(data) ? data : textEncoder.encode(data)
},
decode: identity,
name: 'view',
createBufferTranscoder () {
return new BufferFormat({
encode: function (data) {
return Buffer.isBuffer(data)
? data
: ArrayBuffer.isView(data)
? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
: Buffer.from(String(data), 'utf8')
},
decode: identity,
name: `${this.name}+buffer`
})
}
})
/**
* @type {typeof import('./encodings').hex}
*/
exports.hex = new BufferFormat({
encode: function (data) {
return Buffer.isBuffer(data) ? data : Buffer.from(String(data), 'hex')
},
decode: function (buffer) {
return buffer.toString('hex')
},
name: 'hex'
})
/**
* @type {typeof import('./encodings').base64}
*/
exports.base64 = new BufferFormat({
encode: function (data) {
return Buffer.isBuffer(data) ? data : Buffer.from(String(data), 'base64')
},
decode: function (buffer) {
return buffer.toString('base64')
},
name: 'base64'
})

View File

@@ -0,0 +1,111 @@
'use strict'
const { Buffer } = require('buffer') || {}
const { Encoding } = require('./encoding')
const textEndec = require('./text-endec')
/**
* @template TIn, TOut
* @extends {Encoding<TIn,Buffer,TOut>}
*/
class BufferFormat extends Encoding {
/**
* @param {Omit<IEncoding<TIn, Buffer, TOut>, 'format'>} options
*/
constructor (options) {
super({ ...options, format: 'buffer' })
}
/** @override */
createViewTranscoder () {
return new ViewFormat({
encode: this.encode, // Buffer is a view (UInt8Array)
decode: (data) => this.decode(
Buffer.from(data.buffer, data.byteOffset, data.byteLength)
),
name: `${this.name}+view`
})
}
/** @override */
createBufferTranscoder () {
return this
}
}
/**
* @extends {Encoding<TIn,Uint8Array,TOut>}
* @template TIn, TOut
*/
class ViewFormat extends Encoding {
/**
* @param {Omit<IEncoding<TIn, Uint8Array, TOut>, 'format'>} options
*/
constructor (options) {
super({ ...options, format: 'view' })
}
/** @override */
createBufferTranscoder () {
return new BufferFormat({
encode: (data) => {
const view = this.encode(data)
return Buffer.from(view.buffer, view.byteOffset, view.byteLength)
},
decode: this.decode, // Buffer is a view (UInt8Array)
name: `${this.name}+buffer`
})
}
/** @override */
createViewTranscoder () {
return this
}
}
/**
* @extends {Encoding<TIn,string,TOut>}
* @template TIn, TOut
*/
class UTF8Format extends Encoding {
/**
* @param {Omit<IEncoding<TIn, string, TOut>, 'format'>} options
*/
constructor (options) {
super({ ...options, format: 'utf8' })
}
/** @override */
createBufferTranscoder () {
return new BufferFormat({
encode: (data) => Buffer.from(this.encode(data), 'utf8'),
decode: (data) => this.decode(data.toString('utf8')),
name: `${this.name}+buffer`
})
}
/** @override */
createViewTranscoder () {
const { textEncoder, textDecoder } = textEndec()
return new ViewFormat({
encode: (data) => textEncoder.encode(this.encode(data)),
decode: (data) => this.decode(textDecoder.decode(data)),
name: `${this.name}+view`
})
}
/** @override */
createUTF8Transcoder () {
return this
}
}
exports.BufferFormat = BufferFormat
exports.ViewFormat = ViewFormat
exports.UTF8Format = UTF8Format
/**
* @typedef {import('./encoding').IEncoding<TIn,TFormat,TOut>} IEncoding
* @template TIn, TFormat, TOut
*/

View File

@@ -0,0 +1,19 @@
'use strict'
/** @type {{ textEncoder: TextEncoder, textDecoder: TextDecoder }|null} */
let lazy = null
/**
* Get semi-global instances of TextEncoder and TextDecoder.
* @returns {{ textEncoder: TextEncoder, textDecoder: TextDecoder }}
*/
module.exports = function () {
if (lazy === null) {
lazy = {
textEncoder: new TextEncoder(),
textDecoder: new TextDecoder()
}
}
return lazy
}

View File

@@ -0,0 +1,37 @@
{
"name": "level-transcoder",
"version": "1.0.1",
"description": "Encode data with built-in or custom encodings",
"license": "MIT",
"main": "index.js",
"types": "./index.d.ts",
"files": [
"lib",
"index.js",
"index.d.ts",
"CHANGELOG.md",
"LICENSE",
"UPGRADING.md"
],
"dependencies": {
"buffer": "^6.0.3",
"module-error": "^1.0.1"
},
"devDependencies": {
"@types/node": "^16.11.10",
"@voxpelli/tsconfig": "^3.1.0",
"airtap": "^4.0.3",
"airtap-playwright": "^1.0.1",
"hallmark": "^4.0.0",
"nyc": "^15.1.0",
"standard": "^16.0.3",
"tape": "^5.3.2",
"ts-standard": "^11.0.0",
"typescript": "^4.5.2"
},
"repository": "Level/transcoder",
"homepage": "https://github.com/Level/transcoder",
"engines": {
"node": ">=12"
}
}