Skip to content

Commit 1f40104

Browse files
committed
Remove NativeFormat
Was just a brief idea. Might have panned out but I don't want to commit to it yet.
1 parent ae194ef commit 1f40104

File tree

9 files changed

+16
-71
lines changed

9 files changed

+16
-71
lines changed

UPGRADING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
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).
44

5+
## Upcoming
6+
7+
WIP notes, describing the differences from `level-codec`.
8+
9+
- Throws error if encoding is not found, rather than falling back to `id` encoding
10+
- 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.
12+
- The `ascii`, `ucs2` and `utf16le` encodings are not supported.
13+
514
## 10.0.0
615

716
Legacy range options have been removed ([Level/community#86](https://github.com/Level/community/issues/86)). If you previously did:

index.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const ModuleError = require('module-error')
44
const encodings = require('./lib/encodings')
55
const { Encoding } = require('./lib/encoding')
6-
const { BufferFormat, ViewFormat, UTF8Format, NativeFormat } = require('./lib/formats')
6+
const { BufferFormat, ViewFormat, UTF8Format } = require('./lib/formats')
77

88
const kFormats = Symbol('formats')
99
const kEncodings = Symbol('encodings')
@@ -57,7 +57,6 @@ class Transcoder {
5757
return Array.from(types)
5858
}
5959

60-
// TODO: document that we don't fallback to 'id' anymore if encoding is not found
6160
/**
6261
* @param {string|Encoding<any, any, any>|EncodingOptions<any, any, any>} encoding
6362
* @returns {Encoding<any, T, any>}
@@ -87,22 +86,18 @@ class Transcoder {
8786
resolved = new BufferFormat(encoding)
8887
}
8988

90-
const { type, idempotent, format } = resolved
89+
const { type, format } = resolved
9190

92-
if (this[kFormats].has(type)) {
93-
// If idempotent, run data through it to normalize
94-
if (!idempotent) resolved = new NativeFormat(type)
95-
} else if (!this[kFormats].has(format)) {
91+
if (!this[kFormats].has(format)) {
9692
if (this[kFormats].has('view')) {
9793
resolved = resolved.transcode('view')
9894
} else if (this[kFormats].has('buffer')) {
9995
resolved = resolved.transcode('buffer')
10096
} else {
10197
// TODO: improve error message (see tests, it's inconsistent)
102-
throw new ModuleError(
103-
`Encoding '${type}' is not supported`,
104-
{ code: 'LEVEL_ENCODING_NOT_SUPPORTED' }
105-
)
98+
throw new ModuleError(`Encoding '${type}' is not supported`, {
99+
code: 'LEVEL_ENCODING_NOT_SUPPORTED'
100+
})
106101
}
107102
}
108103

lib/encoding.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@ export interface EncodingOptions<TIn, TFormat, TOut> {
5757
*/
5858
format?: string | undefined
5959

60-
/**
61-
* Whether {@link encode} and {@link decode} are idempotent
62-
* functions, such that `f(x) == f(f(x))`.
63-
*/
64-
idempotent?: boolean | undefined
65-
6660
/**
6761
* Legacy property that means the same as `format: 'buffer'`.
6862
* Used only when `format` is not provided.

lib/encoding.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class Encoding {
1515
decode,
1616
type,
1717
format,
18-
idempotent,
1918
buffer,
2019
createViewTranscoder,
2120
createBufferTranscoder
@@ -36,10 +35,6 @@ class Encoding {
3635
throw new TypeError("The 'format' option must be a string or undefined")
3736
}
3837

39-
if (typeof idempotent !== 'boolean' && idempotent !== undefined) {
40-
throw new TypeError("The 'idempotent' option must be a boolean or undefined")
41-
}
42-
4338
/** @type {(data: TIn) => TFormat} */
4439
this.encode = (encode || this.encode || identity).bind(this)
4540

@@ -53,9 +48,7 @@ class Encoding {
5348
this.format = format || (buffer === false ? 'utf8' : 'buffer')
5449

5550
/** @type {boolean} */
56-
this.idempotent = idempotent !== undefined
57-
? idempotent
58-
: this.type === this.format
51+
this.idempotent = this.type === this.format
5952

6053
if (createViewTranscoder) {
6154
this.createViewTranscoder = createViewTranscoder

lib/encodings.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const { BufferFormat, ViewFormat, UTF8Format, IdentityFormat } = require('./form
99
*/
1010
exports.utf8 = new UTF8Format({
1111
encode: function (data) {
12-
// TODO: document that this now always returns a string
1312
// On node 16.9.1 buffer.toString() is 5x faster than TextDecoder
1413
return Buffer.isBuffer(data)
1514
? data.toString('utf8')
@@ -106,9 +105,6 @@ exports.id = new IdentityFormat({
106105
type: 'id'
107106
})
108107

109-
// TODO: document removed encodings (ascii, ucs2/ucs-2, utf16le/utf-16le)
110-
// TODO: perhaps even remove all of them
111-
112108
/**
113109
* @type {BufferFormat<Buffer|string, string>}
114110
*/

lib/formats.d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,3 @@ 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> {}
66
export class IdentityFormat extends Encoding<any, any, any> {}
7-
8-
/**
9-
* Special pass-through encoding for externally implemented encodings.
10-
* @experimental May get removed without warning.
11-
*/
12-
export class NativeFormat extends Encoding<any, any, any> {
13-
constructor (nativeType: string)
14-
}

lib/formats.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,10 @@ class IdentityFormat extends Encoding {
108108
}
109109
}
110110

111-
/**
112-
* @extends {Encoding<any,any,any>}
113-
*/
114-
class NativeFormat extends Encoding {
115-
/**
116-
* @param {string} nativeType
117-
*/
118-
constructor (nativeType) {
119-
super({ type: `${nativeType}+native`, idempotent: true, format: nativeType })
120-
}
121-
}
122-
123111
exports.BufferFormat = BufferFormat
124112
exports.ViewFormat = ViewFormat
125113
exports.UTF8Format = UTF8Format
126114
exports.IdentityFormat = IdentityFormat
127-
exports.NativeFormat = NativeFormat
128115

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

test/encoding.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ test('Encoding() throws on invalid type or format option', function (t) {
3535
}
3636
})
3737

38-
test('Encoding() throws on invalid idempotent option', function (t) {
39-
t.plan(6 * 2)
40-
41-
for (const invalid of ['', 'true', null, {}, () => {}, []]) {
42-
try {
43-
// eslint-disable-next-line no-new
44-
new Encoding({ idempotent: invalid })
45-
} catch (err) {
46-
t.is(err.name, 'TypeError', 'is a TypeError')
47-
t.is(err.message, "The 'idempotent' option must be a boolean or undefined", 'correct message')
48-
}
49-
}
50-
})
51-
5238
test('Encoding() sets format based on format option or legacy buffer option', function (t) {
5339
t.is(new Encoding({ buffer: true }).format, 'buffer')
5440
t.is(new Encoding({ buffer: false }).format, 'utf8')

test/transcoder.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ test('transcoder.types()', function (t) {
3434
t.same(transcoder.types(), ['utf8', 'json', 'buffer', 'view', 'hex', 'base64'])
3535
t.same(transcoder.types(true), ['utf8+view', 'json+view', 'buffer+view', 'view', 'hex+view', 'base64+view'])
3636

37-
transcoder = new Transcoder(['json'])
38-
t.same(transcoder.types(), ['json'])
39-
t.same(transcoder.types(true), ['json+native'])
40-
4137
transcoder = new Transcoder(['id'])
4238
t.same(transcoder.types(), ['id'])
4339
t.same(transcoder.types(true), ['id'])
@@ -218,9 +214,6 @@ test('transcoder.encoding() wraps custom anonymous encoding', function (t) {
218214
t.is(e.format, 'buffer', 'ignores invalid legacy buffer option')
219215
}
220216

221-
verify(transcoder.encoding({ ...opts, idempotent: true }), true)
222-
verify(transcoder.encoding({ ...opts, idempotent: false }), false)
223-
224217
t.end()
225218
})
226219

0 commit comments

Comments
 (0)