Initial import with skill sheet working

This commit is contained in:
2024-12-04 00:11:23 +01:00
commit 9050c80ab4
4488 changed files with 671048 additions and 0 deletions

15
node_modules/level-transcoder/CHANGELOG.md generated vendored Normal file
View File

@ -0,0 +1,15 @@
# Changelog
## [1.0.1] - 2021-11-26
### Fixed
- Remove `codecs` from supported interfaces in `README` ([`3b6b003`](https://github.com/Level/transcoder/commit/3b6b003))
## [1.0.0] - 2021-11-07
_:seedling: Initial release._
[1.0.1]: https://github.com/Level/transcoder/releases/tag/v1.0.1
[1.0.0]: https://github.com/Level/transcoder/releases/tag/v1.0.0

21
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.

214
node_modules/level-transcoder/README.md generated vendored Normal file
View File

@ -0,0 +1,214 @@
# level-transcoder
**Encode data with built-in or custom encodings.** The successor to [`level-codec`][level-codec] that transcodes encodings from and to internal data formats supported by a database. This allows a database to store data in a format of its choice (Buffer, Uint8Array or String) with zero-effort support of known encodings. That includes other encoding interfaces in the ecosystem like [`abstract-encoding`][abstract-enc] and [`multiformats`][blockcodec].
[![level badge][level-badge]](https://github.com/Level/awesome)
[![npm](https://img.shields.io/npm/v/level-transcoder.svg)](https://www.npmjs.com/package/level-transcoder)
[![Node version](https://img.shields.io/node/v/level-transcoder.svg)](https://www.npmjs.com/package/level-transcoder)
[![Test](https://img.shields.io/github/workflow/status/Level/transcoder/Test?label=test)](https://github.com/Level/transcoder/actions/workflows/test.yml)
[![Coverage](https://img.shields.io/codecov/c/github/Level/transcoder?label=\&logo=codecov\&logoColor=fff)](https://codecov.io/gh/Level/transcoder)
[![Standard](https://img.shields.io/badge/standard-informational?logo=javascript\&logoColor=fff)](https://standardjs.com)
[![Common Changelog](https://common-changelog.org/badge.svg)](https://common-changelog.org)
[![Donate](https://img.shields.io/badge/donate-orange?logo=open-collective\&logoColor=fff)](https://opencollective.com/level)
## Usage
Create a transcoder, passing a desired format:
```js
const { Transcoder } = require('level-transcoder')
const transcoder1 = new Transcoder(['view'])
const transcoder2 = new Transcoder(['buffer'])
const transcoder3 = new Transcoder(['utf8'])
```
Then select an encoding and encode some data:
```js
// Uint8Array(3) [ 49, 50, 51 ]
console.log(transcoder1.encoding('json').encode(123))
// <Buffer 31 32 33>
console.log(transcoder2.encoding('json').encode(123))
// '123'
console.log(transcoder3.encoding('json').encode(123))
```
If the `Transcoder` constructor is given multiple formats then `Transcoder#encoding()` selects an encoding with the best fitting format. Consider a database like [`leveldown`][leveldown] which has the ability to return data as a Buffer or string. If an `encoding.decode(data)` function needs a string, we'll want to fetch that `data` from the database as a string. This avoids the cost of having to convert a Buffer to a string. So we'd use the following transcoder:
```js
const transcoder = new Transcoder(['buffer', 'utf8'])
```
Then, knowing for example that the return value of `JSON.stringify(data)` is a UTF-8 string which matches one of the given formats, the `'json'` encoding will return a string here:
```js
// '123'
console.log(transcoder.encoding('json').encode(123))
```
In contrast, data encoded as a `'view'` (for now that just means Uint8Array) would get transcoded into the `'buffer'` encoding. Copying of data is avoided where possible, like how the underlying ArrayBuffer of a view can be passed to `Buffer.from(..)` without a copy.
Lastly, encodings returned by `Transcoder#encoding()` have a `format` property to be used to forward information to an underlying store. For example: an input value of `{ x: 3 }` using the `'json'` encoding which has a `format` of `'utf8'`, can be forwarded as value `'{"x":3}'` with encoding `'utf8'`. Vice versa for output.
## Encodings
### Built-in Encodings
These encodings can be used out of the box and are to be selected by name.
In this table, the _input_ is what `encode()` accepts. The _format_ is what `encode()` returns as well as what `decode()` accepts. The _output_ is what `decode()` returns. The TypeScript typings of `level-transcoder` have generic type parameters with matching names: `TIn`, `TFormat` and `TOut`.
| Name | Input | Format | Output |
| :---------------------- | :------------------------- | :--------- | :-------------- |
| `'buffer'` <sup>1</sup> | Buffer, Uint8Array, String | `'buffer'` | Buffer |
| `'view'` | Uint8Array, Buffer, String | `'view'` | Uint8Array |
| `'utf8'` | String, Buffer, Uint8Array | `'utf8'` | String |
| `'json'` | Any JSON type | `'utf8'` | As input |
| `'hex'` | String (hex), Buffer | `'buffer'` | String (hex) |
| `'base64'` | String (base64), Buffer | `'buffer'` | String (base64) |
<sup>1</sup> Aliased as `'binary'`. Use of this alias does not affect the ability to transcode.
### Transcoder Encodings
It's not necessary to use or reference the below encodings directly. They're listed here for implementation notes and to show how input and output is the same; it's the format that differs.
Custom encodings are transcoded in the same way and require no additional setup. For example: if a custom encoding has `{ name: 'example', format: 'utf8' }` then `level-transcoder` will create transcoder encodings on demand with names `'example+buffer'` and `'example+view'`.
| Name | Input | Format | Output |
| :--------------------------- | :------------------------- | :--------- | :-------------- |
| `'buffer+view'` | Buffer, Uint8Array, String | `'view'` | Buffer |
| `'view+buffer'` | Uint8Array, Buffer, String | `'buffer'` | Uint8Array |
| `'utf8+view'` | String, Buffer, Uint8Array | `'view'` | String |
| `'utf8+buffer'` | String, Buffer, Uint8Array | `'buffer'` | String |
| `'json+view'` | Any JSON type | `'view'` | As input |
| `'json+buffer'` | Any JSON type | `'buffer'` | As input |
| `'hex+view'` <sup>1</sup> | String (hex), Buffer | `'view'` | String (hex) |
| `'base64+view'` <sup>1</sup> | String (base64), Buffer | `'view'` | String (base64) |
<sup>1</sup> Unlike other encodings that transcode to `'view'`, these depend on Buffer at the moment and thus don't work in browsers if a [shim](https://github.com/feross/buffer) is not included by JavaScript bundlers like Webpack and Browserify.
### Ecosystem Encodings
Various modules in the ecosystem, in and outside of Level, can be used with `level-transcoder` although they follow different interfaces. Common between the interfaces is that they have `encode()` and `decode()` methods. The terms "codec" and "encoding" are used interchangeably in the ecosystem. Passing these encodings through `Transcoder#encoding()` (which is done implicitly when used in an `abstract-level` database) results in normalized encoding objects as described further below.
| Module | Format | Interface | Named |
| :----------------------------------------- | :--------------- | :---------------------------------- | :---- |
| [`protocol-buffers`][protocol-buffers] | `buffer` | [`level-codec`][level-codec] | ❌ |
| [`charwise`][charwise] | `utf8` | [`level-codec`][level-codec] | ✅ |
| [`bytewise`][bytewise] | `buffer` | [`level-codec`][level-codec] | ✅ |
| [`lexicographic-integer-encoding`][lexint] | `buffer`, `utf8` | [`level-codec`][level-codec] | ✅ |
| [`abstract-encoding`][abstract-enc] | `buffer` | [`abstract-encoding`][abstract-enc] | ❌ |
| [`multiformats`][js-multiformats] | `view` | [`multiformats`][blockcodec] | ✅ |
Those marked as not named are modules that export or generate encodings that don't have a `name` property (or `type` as an alias). We call these _anonymous encodings_. They can only be used as objects and not by name. Passing an anonymous encoding through `Transcoder#encoding()` does give it a `name` property for compatibility, but the value of `name` is not deterministic.
## API
### `Transcoder`
#### `transcoder = new Transcoder(formats)`
Create a new transcoder, providing the formats that are supported by a database (or other). The `formats` argument must be an array containing one or more of `'buffer'`, `'view'`, `'utf8'`. The returned `transcoder` instance is stateful, in that it contains a set of cached encoding objects.
#### `encoding = transcoder.encoding(encoding)`
Returns the given `encoding` argument as a normalized encoding object that follows the `level-transcoder` encoding interface. The `encoding` argument may be:
- A string to select a known encoding by its name
- An object that follows one of the following interfaces: [`level-transcoder`](#encoding-interface), [`level-codec`](https://github.com/Level/codec#encoding-format), [`abstract-encoding`][abstract-enc], [`multiformats`][blockcodec]
- A previously normalized encoding, such that `encoding(x)` equals `encoding(encoding(x))`.
Results are cached. If the `encoding` argument is an object and it has a name then subsequent calls can refer to that encoding by name.
Depending on the `formats` provided to the `Transcoder` constructor, this method may return a _transcoder encoding_ that translates the desired encoding from / to a supported format. Its `encode()` and `decode()` methods will have respectively the same input and output types as a non-transcoded encoding, but its `name` property will differ.
#### `encodings = transcoder.encodings()`
Get an array of encoding objects. This includes:
- Encodings for the `formats` that were passed to the `Transcoder` constructor
- Custom encodings that were passed to `transcoder.encoding()`
- Transcoder encodings for either.
### `Encoding`
#### `data = encoding.encode(data)`
Encode data.
#### `data = encoding.decode(data)`
Decode data.
#### `encoding.name`
Unique name. A string.
#### `encoding.commonName`
Common name, computed from `name`. If this encoding is a transcoder encoding, `name` will be for example `'json+view'` and `commonName` will be just `'json'`. Else `name` will equal `commonName`.
#### `encoding.format`
Name of the (lower-level) encoding used by the return value of `encode()`. One of `'buffer'`, `'view'`, `'utf8'`. If `name` equals `format` then the encoding can be assumed to be idempotent, such that `encode(x)` equals `encode(encode(x))`.
## Encoding Interface
Custom encodings must follow the following interface:
```ts
interface IEncoding<TIn, TFormat, TOut> {
name: string
format: 'buffer' | 'view' | 'utf8'
encode: (data: TIn) => TFormat
decode: (data: TFormat) => TOut
}
```
## Install
With [npm](https://npmjs.org) do:
```
npm install level-transcoder
```
## Contributing
[`Level/transcoder`](https://github.com/Level/transcoder) is an **OPEN Open Source Project**. This means that:
> Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
See the [Contribution Guide](https://github.com/Level/community/blob/master/CONTRIBUTING.md) for more details.
## Donate
Support us with a monthly donation on [Open Collective](https://opencollective.com/level) and help us continue our work.
## License
[MIT](LICENSE)
[level-badge]: https://leveljs.org/img/badge.svg
[level-codec]: https://github.com/Level/codec
[leveldown]: https://github.com/Level/leveldown
[protocol-buffers]: https://github.com/mafintosh/protocol-buffers
[charwise]: https://github.com/dominictarr/charwise
[bytewise]: https://github.com/deanlandolt/bytewise
[lexint]: https://github.com/vweevers/lexicographic-integer-encoding
[abstract-enc]: https://github.com/mafintosh/abstract-encoding
[js-multiformats]: https://github.com/multiformats/js-multiformats
[blockcodec]: https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts

13
node_modules/level-transcoder/UPGRADING.md generated vendored Normal file
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.

27
node_modules/level-transcoder/index.d.ts generated vendored Normal file
View File

@ -0,0 +1,27 @@
import { Encoding, MixedEncoding, KnownEncoding, KnownEncodingName } from './lib/encoding'
export class Transcoder<T = any> {
/**
* Create a Transcoder.
* @param formats Formats supported by consumer.
*/
constructor (formats: Array<'buffer'|'view'|'utf8'>)
/**
* Get an array of supported encoding objects.
*/
encodings (): Array<Encoding<any, T, any>>
/**
* Get the given encoding, creating a transcoder encoding if necessary.
* @param encoding Named encoding or encoding object.
*/
encoding<TIn, TFormat, TOut> (
encoding: MixedEncoding<TIn, TFormat, TOut>
): Encoding<TIn, T, TOut>
encoding<N extends KnownEncodingName> (encoding: N): KnownEncoding<N, T>
encoding (encoding: string): Encoding<any, T, any>
}
export * from './lib/encoding'

158
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

158
node_modules/level-transcoder/lib/encoding.d.ts generated vendored Normal file
View File

@ -0,0 +1,158 @@
import { BufferFormat, ViewFormat, UTF8Format } from './formats'
/**
* Encodes {@link TIn} to {@link TFormat} and decodes
* {@link TFormat} to {@link TOut}.
*/
export abstract class Encoding<TIn, TFormat, TOut> implements IEncoding<TIn, TFormat, TOut> {
constructor (options: IEncoding<TIn, TFormat, TOut>)
encode: (data: TIn) => TFormat
decode: (data: TFormat) => TOut
name: string
format: 'buffer' | 'view' | 'utf8'
createViewTranscoder (): ViewFormat<TIn, TOut>
createBufferTranscoder (): BufferFormat<TIn, TOut>
createUTF8Transcoder (): UTF8Format<TIn, TOut>
/**
* Common name, computed from {@link name}. If this encoding is a
* transcoder encoding, {@link name} will be for example 'json+view'
* and {@link commonName} will be just 'json'. Else {@link name}
* will equal {@link commonName}.
*/
get commonName (): string
}
export interface IEncoding<TIn, TFormat, TOut> {
/**
* Encode data.
*/
encode: (data: TIn) => TFormat
/**
* Decode data.
*/
decode: (data: TFormat) => TOut
/**
* Unique name.
*/
name: string
/**
* The name of the (lower-level) encoding used by the return value of
* {@link encode}. One of 'buffer', 'view', 'utf8'.
*/
format: 'buffer' | 'view' | 'utf8'
/**
* Create a new encoding that transcodes {@link TFormat} from / to a view.
*/
createViewTranscoder?: (() => ViewFormat<TIn, TOut>) | undefined
/**
* Create a new encoding that transcodes {@link TFormat} from / to a buffer.
*/
createBufferTranscoder?: (() => BufferFormat<TIn, TOut>) | undefined
/**
* Create a new encoding that transcodes {@link TFormat} from / to a UTF-8 string.
*/
createUTF8Transcoder?: (() => UTF8Format<TIn, TOut>) | undefined
}
export interface IExternalEncoding<TIn, TFormat, TOut> {
/**
* Encode data.
*/
encode: (data: TIn) => TFormat
/**
* Decode data.
*/
decode: (data: TFormat) => TOut
/**
* Unique name.
*/
name?: string | undefined
/**
* Legacy `level-codec` option that means the same as `format: 'buffer'`
* if true or `format: 'utf8'` if false.
*/
buffer?: boolean | undefined
/**
* Legacy `level-codec` alias for {@link name}. Used only when the
* {@link name} option is undefined.
*/
type?: any
/**
* To detect `multiformats`. If a number, then the encoding is
* assumed to have a {@link format} of 'view'.
* @see https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts
*/
code?: any
}
/**
* Names of built-in encodings.
*/
export type KnownEncodingName = 'utf8' | 'buffer' | 'view' | 'json' | 'hex' | 'base64'
/**
* One of the supported encoding interfaces.
*/
export type MixedEncoding<TIn, TFormat, TOut> =
IEncoding<TIn, TFormat, TOut> |
IExternalEncoding<TIn, TFormat, TOut>
/**
* Type utility to cast a built-in encoding identified by its name to an {@link Encoding}.
*/
export type KnownEncoding<N extends KnownEncodingName, TFormat>
= Encoding<KnownEncodingInput<N>, TFormat, KnownEncodingOutput<N>>
/**
* Type utility to get the input type of a built-in encoding identified by its name.
*/
export type KnownEncodingInput<N extends KnownEncodingName>
= N extends 'utf8' ? string | Buffer | Uint8Array
: N extends 'buffer' ? Buffer | Uint8Array | string
: N extends 'view' ? Uint8Array | string
: N extends 'json' ? any
: N extends 'hex' ? Buffer | string
: N extends 'base64' ? Buffer | string
: never
/**
* Type utility to get the output type of a built-in encoding identified by its name.
*/
export type KnownEncodingOutput<N extends KnownEncodingName>
= N extends 'utf8' ? string
: N extends 'buffer' ? Buffer
: N extends 'view' ? Uint8Array
: N extends 'json' ? any
: N extends 'hex' ? string
: N extends 'base64' ? string
: never
/**
* Type utility to use a {@link MixedEncoding} with an untyped format.
*/
export type PartialEncoding<TIn, TOut = TIn> = MixedEncoding<TIn, any, TOut>
/**
* Type utility to use a {@link MixedEncoding} with an untyped format and output.
* For when only the encoding side is needed.
*/
export type PartialEncoder<TIn> = MixedEncoding<TIn, any, any>
/**
* Type utility to use a {@link MixedEncoding} with an untyped input and format.
* For when only the decoding side is needed.
*/
export type PartialDecoder<TOut> = MixedEncoding<any, any, TOut>

105
node_modules/level-transcoder/lib/encoding.js generated vendored Normal file
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
*/

9
node_modules/level-transcoder/lib/encodings.d.ts generated vendored Normal file
View File

@ -0,0 +1,9 @@
import { KnownEncodingInput, KnownEncodingOutput } from './encoding'
import { BufferFormat, ViewFormat, UTF8Format } from './formats'
export const utf8: UTF8Format<KnownEncodingInput<'utf8'>, KnownEncodingOutput<'utf8'>>
export const json: UTF8Format<KnownEncodingInput<'json'>, KnownEncodingOutput<'json'>>
export const buffer: BufferFormat<KnownEncodingInput<'buffer'>, KnownEncodingOutput<'buffer'>>
export const view: ViewFormat<KnownEncodingInput<'view'>, KnownEncodingOutput<'view'>>
export const hex: BufferFormat<KnownEncodingInput<'hex'>, KnownEncodingOutput<'hex'>>
export const base64: BufferFormat<KnownEncodingInput<'base64'>, KnownEncodingOutput<'base64'>>

135
node_modules/level-transcoder/lib/encodings.js generated vendored Normal file
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'
})

13
node_modules/level-transcoder/lib/formats.d.ts generated vendored Normal file
View File

@ -0,0 +1,13 @@
import { Encoding, IEncoding } from './encoding'
export class BufferFormat<TIn, TOut> extends Encoding<TIn, Buffer, TOut> {
constructor (options: Omit<IEncoding<TIn, Buffer, TOut>, 'format'>)
}
export class ViewFormat<TIn, TOut> extends Encoding<TIn, Uint8Array, TOut> {
constructor (options: Omit<IEncoding<TIn, Uint8Array, TOut>, 'format'>)
}
export class UTF8Format<TIn, TOut> extends Encoding<TIn, string, TOut> {
constructor (options: Omit<IEncoding<TIn, string, TOut>, 'format'>)
}

111
node_modules/level-transcoder/lib/formats.js generated vendored Normal file
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
*/

6
node_modules/level-transcoder/lib/text-endec.d.ts generated vendored Normal file
View File

@ -0,0 +1,6 @@
declare function textEndec (): {
textEncoder: TextEncoder
textDecoder: TextDecoder
}
export = textEndec

19
node_modules/level-transcoder/lib/text-endec.js generated vendored Normal file
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
}

46
node_modules/level-transcoder/package.json generated vendored Normal file
View File

@ -0,0 +1,46 @@
{
"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",
"scripts": {
"test": "ts-standard && tsc && standard && hallmark && nyc tape test/*.js",
"test-browsers-local": "airtap --coverage --verbose test/*.js",
"coverage": "nyc report -r lcovonly",
"hallmark": "hallmark --fix"
},
"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",
"keywords": [
"level"
],
"engines": {
"node": ">=12"
}
}