Skip to content

Commit ee1aa03

Browse files
committed
Remove id encoding
1 parent 61af770 commit ee1aa03

File tree

12 files changed

+57
-107
lines changed

12 files changed

+57
-107
lines changed

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,16 @@ For example, on `leveldown` a call like `db.put(key, { x: 3 }, { valueEncoding:
5757

5858
Various modules in the ecosystem, in and outside of level, can be used with `level-transcoder`.
5959

60-
| Module | Format | Interface | Named |
61-
|:-------------------------------------------|:-------------|:-----------------------------|:------|
62-
| [`protocol-buffers`][protocol-buffers] | buffer | `level-codec` ||
63-
| [`charwise`][charwise] | utf8 | `level-codec` ||
64-
| [`bytewise`][bytewise] | buffer | `level-codec` ||
65-
| [`lexicographic-integer-encoding`][lexint] | buffer, utf8 | `level-codec` ||
66-
| [`codecs`][mafintosh-codecs] | buffer | `codecs` ||
67-
| [`abstract-encoding`][abstract-encoding] | buffer | `abstract-encoding` ||
68-
| [`multiformats`][js-multiformats] | view | [`multiformats`][blockcodec] ||
69-
| [`base32-codecs`][base32-codecs] | buffer | `codecs` ||
70-
| [`level-codec`][level-codec] | buffer, utf8 | `level-codec` ||
60+
| Module | Format | Interface | Named |
61+
|:-------------------------------------------|:-------------|:------------------------------------|:------|
62+
| [`protocol-buffers`][protocol-buffers] | buffer | [`level-codec`][level-codec] ||
63+
| [`charwise`][charwise] | utf8 | [`level-codec`][level-codec] ||
64+
| [`bytewise`][bytewise] | buffer | [`level-codec`][level-codec] ||
65+
| [`lexicographic-integer-encoding`][lexint] | buffer, utf8 | [`level-codec`][level-codec] ||
66+
| [`codecs`][mafintosh-codecs] | buffer | [`codecs`][mafintosh-codecs] ||
67+
| [`abstract-encoding`][abstract-enc] | buffer | [`abstract-encoding`][abstract-enc] ||
68+
| [`multiformats`][js-multiformats] | view | [`multiformats`][blockcodec] ||
69+
| [`base32-codecs`][base32-codecs] | buffer | [`codecs`][mafintosh-codecs] ||
7170

7271
Common between the interfaces is that they have `encode()` and `decode()` methods. The terms "codec" and "encoding" are used interchangeably. Passing these encodings through `Transcoder#encoding()` (which is done implicitly when used in an `abstract-level` database) results in normalized encoding objects that follow [the interface](./lib/encoding.d.ts) of `level-transcoder`.
7372

@@ -206,7 +205,7 @@ Support us with a monthly donation on [Open Collective](https://opencollective.c
206205

207206
[mafintosh-codecs]: https://github.com/mafintosh/codecs
208207

209-
[abstract-encoding]: https://github.com/mafintosh/abstract-encoding
208+
[abstract-enc]: https://github.com/mafintosh/abstract-encoding
210209

211210
[js-multiformats]: https://github.com/multiformats/js-multiformats
212211

UPGRADING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ WIP notes, describing the differences from `level-codec`.
88

99
- Throws error if encoding is not found, rather than falling back to `id` encoding
1010
- The `binary` encoding has been renamed to `buffer`, with `binary` as an alias
11-
- The `utf8` encoding will always return a string. It previously did not touch Buffers. Now it will call `buffer.toString('utf8')` for consistency. Consumers can (selectively) use the `buffer` or `view` encoding to avoid this conversion.
11+
- The `utf8` encoding previously did not touch Buffers. Now it will call `buffer.toString('utf8')` for consistency. Consumers can use the `buffer` encoding to avoid this conversion.
12+
- The `id` encoding (aliased as `none`) which wasn't supported by any active `abstract-leveldown` implementation, has been removed.
1213
- The `ascii`, `ucs2` and `utf16le` encodings are not supported.
1314

1415
## 10.0.0

index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ declare class Transcoder<T = any> {
55
* Create a Transcoder.
66
* @param formats Formats supported by consumer.
77
*/
8-
constructor (formats: Iterable<string>)
8+
constructor (formats: Array<'buffer'|'view'|'utf8'>)
99

1010
/**
1111
* Get supported encoding objects.
@@ -26,7 +26,6 @@ declare class Transcoder<T = any> {
2626
encoding (encoding: 'json'): Encoding<any, T, any>
2727
encoding (encoding: 'hex'): Encoding<Buffer | string, T, string>
2828
encoding (encoding: 'base64'): Encoding<Buffer | string, T, string>
29-
encoding (encoding: 'id'): Encoding<any, any, any>
3029
encoding (encoding: string): Encoding<any, T, any>
3130
}
3231

index.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,25 @@ const { BufferFormat, ViewFormat, UTF8Format } = require('./lib/formats')
77

88
const kFormats = Symbol('formats')
99
const kEncodings = Symbol('encodings')
10+
const validFormats = new Set(['buffer', 'view', 'utf8'])
1011

1112
/** @template T */
1213
class Transcoder {
1314
/**
14-
* @param {Iterable<string>} formats
15+
* @param {Array<'buffer'|'view'|'utf8'>} formats
1516
*/
1617
constructor (formats) {
17-
if (formats == null ||
18-
typeof formats[Symbol.iterator] !== 'function' ||
19-
typeof formats === 'string') {
20-
throw new TypeError("The first argument 'formats' must be an Iterable")
18+
if (!Array.isArray(formats)) {
19+
throw new TypeError("The first argument 'formats' must be an array")
20+
} else if (!formats.every(f => validFormats.has(f))) {
21+
// Note: we only only support aliases in key- and valueEncoding options (where we already did)
22+
throw new TypeError("Format must be one of 'buffer', 'view', 'utf8'")
2123
}
2224

2325
/** @type {Map<string|Encoding<any, any, any>|EncodingOptions<any, any, any>, Encoding<any, any, any>>} */
2426
this[kEncodings] = new Map()
2527
this[kFormats] = new Set(formats)
2628

27-
// Only support aliases in key- and valueEncoding options (where we already did)
28-
for (const [alias, { name }] of Object.entries(aliases)) {
29-
if (this[kFormats].has(alias)) {
30-
throw new ModuleError(`The '${alias}' alias is not supported here; use '${name}' instead`, {
31-
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
32-
})
33-
}
34-
}
35-
3629
// Register encodings (done early in order to populate encodings())
3730
for (const k in encodings) {
3831
try {
@@ -83,7 +76,7 @@ class Transcoder {
8376
} else if (this[kFormats].has('buffer')) {
8477
resolved = resolved.createBufferTranscoder()
8578
} else {
86-
throw new ModuleError(`Encoding '${name}' is not supported`, {
79+
throw new ModuleError(`Encoding '${name}' cannot be transcoded to 'utf8'`, {
8780
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
8881
})
8982
}
@@ -112,9 +105,7 @@ function from (options) {
112105
case 'utf8': return new UTF8Format(options)
113106
case 'buffer': return new BufferFormat(options)
114107
default: {
115-
throw new ModuleError(`Encoding '${format}' is not supported`, {
116-
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
117-
})
108+
throw new TypeError("Format must be one of 'buffer', 'view', 'utf8'")
118109
}
119110
}
120111
}
@@ -147,8 +138,7 @@ function detectFormat ({ format, buffer, code }) {
147138
*/
148139
const aliases = {
149140
binary: encodings.buffer,
150-
'utf-8': encodings.utf8,
151-
none: encodings.id
141+
'utf-8': encodings.utf8
152142
}
153143

154144
/**

lib/encoding.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export abstract class Encoding<TIn, TFormat, TOut> {
2525

2626
/**
2727
* The name of the (lower-level) encoding used by the return value of
28-
* {@link encode}. Typically one of 'buffer', 'view', 'utf8'.
28+
* {@link encode}. One of 'buffer', 'view', 'utf8'.
2929
*/
30-
format: string
30+
format: 'buffer' | 'view' | 'utf8'
3131

3232
/**
3333
* Create a new encoding that transcodes {@link TFormat} to a view.
@@ -58,10 +58,10 @@ export interface EncodingOptions<TIn, TFormat, TOut> {
5858

5959
/**
6060
* The name of the (lower-level) encoding used by the return value of
61-
* {@link encode}. Typically one of 'buffer', 'view', 'utf8'. Defaults to
62-
* 'buffer' if the {@link buffer} and {@link code} options are also undefined.
61+
* {@link encode}. One of 'buffer', 'view', 'utf8'. Defaults to 'buffer'
62+
* if the {@link buffer} and {@link code} options are also undefined.
6363
*/
64-
format?: string | undefined
64+
format?: 'buffer' | 'view' | 'utf8' | undefined
6565

6666
/**
6767
* Legacy `level-codec` option that means the same as `format: 'buffer'`

lib/encoding.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const ModuleError = require('module-error')
4+
const formats = new Set(['buffer', 'view', 'utf8'])
45

56
/**
67
* @template TIn, TFormat, TOut
@@ -23,8 +24,8 @@ class Encoding {
2324
throw new TypeError("The 'name' option must be a string or undefined")
2425
}
2526

26-
if (typeof options.format !== 'string' || options.format === '') {
27-
throw new TypeError("The 'format' option must be a non-empty string")
27+
if (typeof options.format !== 'string' || !formats.has(options.format)) {
28+
throw new TypeError("The 'format' option must be one of 'buffer', 'view', 'utf8'")
2829
}
2930

3031
// Loosely typed for ecosystem compatibility

lib/encodings.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { BufferFormat, ViewFormat, UTF8Format, IdentityFormat } from './formats'
1+
import { BufferFormat, ViewFormat, UTF8Format } from './formats'
22

33
export const utf8: UTF8Format<string | Uint8Array | Buffer, string>
44
export const json: UTF8Format<any, any>
55
export const buffer: BufferFormat<string | Uint8Array | Buffer, Buffer>
66
export const view: ViewFormat<Uint8Array | string, Uint8Array>
7-
export const id: IdentityFormat
87
export const hex: BufferFormat<string | Buffer, string>
98
export const base64: BufferFormat<string | Buffer, string>

lib/encodings.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { Buffer } = require('buffer') || {}
44
const { textEncoder, textDecoder } = require('./text-endec')()
5-
const { BufferFormat, ViewFormat, UTF8Format, IdentityFormat } = require('./formats')
5+
const { BufferFormat, ViewFormat, UTF8Format } = require('./formats')
66

77
/**
88
* @type {UTF8Format<string|Buffer|Uint8Array, string>}
@@ -101,10 +101,6 @@ exports.view = new ViewFormat({
101101
}
102102
})
103103

104-
exports.id = new IdentityFormat({
105-
name: 'id'
106-
})
107-
108104
/**
109105
* @type {BufferFormat<Buffer|string, string>}
110106
*/

lib/formats.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ import { Encoding } from './encoding'
33
export class BufferFormat<TIn, TOut> extends Encoding<TIn, Buffer, TOut> {}
44
export class ViewFormat<TIn, TOut> extends Encoding<TIn, Uint8Array, TOut> {}
55
export class UTF8Format<TIn, TOut> extends Encoding<TIn, string, TOut> {}
6-
export class IdentityFormat extends Encoding<any, any, any> {}

lib/formats.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,9 @@ class UTF8Format extends Encoding {
9696
}
9797
}
9898

99-
/**
100-
* @extends {Encoding<any,any,any>}
101-
*/
102-
class IdentityFormat extends Encoding {
103-
/**
104-
* @param {EncodingOptions<any, any, any>} options
105-
*/
106-
constructor (options) {
107-
super({ ...options, format: 'id' })
108-
}
109-
}
110-
11199
exports.BufferFormat = BufferFormat
112100
exports.ViewFormat = ViewFormat
113101
exports.UTF8Format = UTF8Format
114-
exports.IdentityFormat = IdentityFormat
115102

116103
/**
117104
* @typedef {import('./encoding').EncodingOptions<TIn,TFormat,TOut>} EncodingOptions

0 commit comments

Comments
 (0)