cbor-x 1.3.3 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,12 +1,61 @@
1
- import { Options } from './decode'
2
- export { Decoder, decode, addExtension, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode'
3
- export { Encoder, encode } from './encode'
4
- import { Transform, Readable } from 'stream'
5
-
6
- export as namespace CBOR;
7
- export class DecoderStream extends Transform {
8
- constructor(options?: Options | { highWaterMark: number, emitClose: boolean, allowHalfOpen: boolean })
9
- }
10
- export class EncoderStream extends Transform {
11
- constructor(options?: Options | { highWaterMark: number, emitClose: boolean, allowHalfOpen: boolean })
12
- }
1
+ export enum FLOAT32_OPTIONS {
2
+ NEVER = 0,
3
+ ALWAYS = 1,
4
+ DECIMAL_ROUND = 3,
5
+ DECIMAL_FIT = 4
6
+ }
7
+
8
+ export interface Options {
9
+ useFloat32?: FLOAT32_OPTIONS
10
+ useRecords?: boolean
11
+ structures?: {}[]
12
+ structuredClone?: boolean
13
+ mapsAsObjects?: boolean
14
+ variableMapSize?: boolean
15
+ copyBuffers?: boolean
16
+ bundleStrings?: boolean
17
+ useTimestamp32?: boolean
18
+ largeBigIntToFloat?: boolean
19
+ encodeUndefinedAsNil?: boolean
20
+ maxSharedStructures?: number
21
+ maxOwnStructures?: number
22
+ useSelfDescribedHeader?: boolean
23
+ keyMap?: {}
24
+ shouldShareStructure?: (keys: string[]) => boolean
25
+ getStructures?(): {}[]
26
+ saveStructures?(structures: {}[]): boolean | void
27
+ onInvalidDate?: () => any
28
+ tagUint8Array?: boolean
29
+ }
30
+ interface Extension {
31
+ Class: Function
32
+ tag: number
33
+ encode(value: any): Buffer | Uint8Array
34
+ decode(messagePack: Buffer | Uint8Array): any
35
+ }
36
+ export class Decoder {
37
+ constructor(options?: Options)
38
+ decode(messagePack: Buffer | Uint8Array): any
39
+ decodeMultiple(messagePack: Buffer | Uint8Array, forEach?: (value: any) => any): [] | void
40
+ }
41
+ export function decode(messagePack: Buffer | Uint8Array): any
42
+ export function decodeMultiple(messagePack: Buffer | Uint8Array, forEach?: (value: any) => any): [] | void
43
+ export function addExtension(extension: Extension): void
44
+ export function clearSource(): void
45
+ export function roundFloat32(float32Number: number): number
46
+ export let isNativeAccelerationEnabled: boolean
47
+
48
+ export class Encoder extends Decoder {
49
+ encode(value: any): Buffer
50
+ }
51
+ export function encode(value: any): Buffer
52
+
53
+ import { Transform, Readable } from 'stream'
54
+
55
+ export as namespace CBOR;
56
+ export class DecoderStream extends Transform {
57
+ constructor(options?: Options | { highWaterMark: number, emitClose: boolean, allowHalfOpen: boolean })
58
+ }
59
+ export class EncoderStream extends Transform {
60
+ constructor(options?: Options | { highWaterMark: number, emitClose: boolean, allowHalfOpen: boolean })
61
+ }
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { Encoder, addExtension, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './encode.js'
2
- export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
3
- export { decodeIter, encodeIter } from './iterators.js'
1
+ export { Encoder, addExtension, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './encode.js'
2
+ export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
3
+ export { decodeIter, encodeIter } from './iterators.js'
package/iterators.js CHANGED
@@ -1,85 +1,85 @@
1
- import { Encoder } from './encode.js'
2
- import { Decoder } from './decode.js'
3
-
4
- /**
5
- * Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer
6
- * If the argument is only Async Iterable, the return value will be an Async Iterable.
7
- * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
8
- * @param {options} [options] - cbor-x Encoder options
9
- * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
10
- */
11
- export function encodeIter (objectIterator, options = {}) {
12
- if (!objectIterator || typeof objectIterator !== 'object') {
13
- throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
14
- } else if (typeof objectIterator[Symbol.iterator] === 'function') {
15
- return encodeIterSync(objectIterator, options)
16
- } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
17
- return encodeIterAsync(objectIterator, options)
18
- } else {
19
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
20
- }
21
- }
22
-
23
- function * encodeIterSync (objectIterator, options) {
24
- const encoder = new Encoder(options)
25
- for (const value of objectIterator) {
26
- yield encoder.encode(value)
27
- }
28
- }
29
-
30
- async function * encodeIterAsync (objectIterator, options) {
31
- const encoder = new Encoder(options)
32
- for await (const value of objectIterator) {
33
- yield encoder.encode(value)
34
- }
35
- }
36
-
37
- /**
38
- * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
39
- * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
40
- * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
41
- * @param {object} [options] - Decoder options
42
- * @returns {IterableIterator|Promise.<AsyncIterableIterator}
43
- */
44
- export function decodeIter (bufferIterator, options = {}) {
45
- if (!bufferIterator || typeof bufferIterator !== 'object') {
46
- throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
47
- }
48
-
49
- const decoder = new Decoder(options)
50
- let incomplete
51
- const parser = (chunk) => {
52
- let yields
53
- // if there's incomplete data from previous chunk, concatinate and try again
54
- if (incomplete) {
55
- chunk = Buffer.concat([incomplete, chunk])
56
- incomplete = undefined
57
- }
58
-
59
- try {
60
- yields = decoder.decodeMultiple(chunk)
61
- } catch (err) {
62
- if (err.incomplete) {
63
- incomplete = chunk.slice(err.lastPosition)
64
- yields = err.values
65
- } else {
66
- throw err
67
- }
68
- }
69
- return yields
70
- }
71
-
72
- if (typeof bufferIterator[Symbol.iterator] === 'function') {
73
- return (function * iter () {
74
- for (const value of bufferIterator) {
75
- yield * parser(value)
76
- }
77
- })()
78
- } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
79
- return (async function * iter () {
80
- for await (const value of bufferIterator) {
81
- yield * parser(value)
82
- }
83
- })()
84
- }
85
- }
1
+ import { Encoder } from './encode.js'
2
+ import { Decoder } from './decode.js'
3
+
4
+ /**
5
+ * Given an Iterable first argument, returns an Iterable where each value is encoded as a Buffer
6
+ * If the argument is only Async Iterable, the return value will be an Async Iterable.
7
+ * @param {Iterable|Iterator|AsyncIterable|AsyncIterator} objectIterator - iterable source, like a Readable object stream, an array, Set, or custom object
8
+ * @param {options} [options] - cbor-x Encoder options
9
+ * @returns {IterableIterator|Promise.<AsyncIterableIterator>}
10
+ */
11
+ export function encodeIter (objectIterator, options = {}) {
12
+ if (!objectIterator || typeof objectIterator !== 'object') {
13
+ throw new Error('first argument must be an Iterable, Async Iterable, or a Promise for an Async Iterable')
14
+ } else if (typeof objectIterator[Symbol.iterator] === 'function') {
15
+ return encodeIterSync(objectIterator, options)
16
+ } else if (typeof objectIterator.then === 'function' || typeof objectIterator[Symbol.asyncIterator] === 'function') {
17
+ return encodeIterAsync(objectIterator, options)
18
+ } else {
19
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a Promise')
20
+ }
21
+ }
22
+
23
+ function * encodeIterSync (objectIterator, options) {
24
+ const encoder = new Encoder(options)
25
+ for (const value of objectIterator) {
26
+ yield encoder.encode(value)
27
+ }
28
+ }
29
+
30
+ async function * encodeIterAsync (objectIterator, options) {
31
+ const encoder = new Encoder(options)
32
+ for await (const value of objectIterator) {
33
+ yield encoder.encode(value)
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Given an Iterable/Iterator input which yields buffers, returns an IterableIterator which yields sync decoded objects
39
+ * Or, given an Async Iterable/Iterator which yields promises resolving in buffers, returns an AsyncIterableIterator.
40
+ * @param {Iterable|Iterator|AsyncIterable|AsyncIterableIterator} bufferIterator
41
+ * @param {object} [options] - Decoder options
42
+ * @returns {IterableIterator|Promise.<AsyncIterableIterator}
43
+ */
44
+ export function decodeIter (bufferIterator, options = {}) {
45
+ if (!bufferIterator || typeof bufferIterator !== 'object') {
46
+ throw new Error('first argument must be an Iterable, Async Iterable, Iterator, Async Iterator, or a promise')
47
+ }
48
+
49
+ const decoder = new Decoder(options)
50
+ let incomplete
51
+ const parser = (chunk) => {
52
+ let yields
53
+ // if there's incomplete data from previous chunk, concatinate and try again
54
+ if (incomplete) {
55
+ chunk = Buffer.concat([incomplete, chunk])
56
+ incomplete = undefined
57
+ }
58
+
59
+ try {
60
+ yields = decoder.decodeMultiple(chunk)
61
+ } catch (err) {
62
+ if (err.incomplete) {
63
+ incomplete = chunk.slice(err.lastPosition)
64
+ yields = err.values
65
+ } else {
66
+ throw err
67
+ }
68
+ }
69
+ return yields
70
+ }
71
+
72
+ if (typeof bufferIterator[Symbol.iterator] === 'function') {
73
+ return (function * iter () {
74
+ for (const value of bufferIterator) {
75
+ yield * parser(value)
76
+ }
77
+ })()
78
+ } else if (typeof bufferIterator[Symbol.asyncIterator] === 'function') {
79
+ return (async function * iter () {
80
+ for await (const value of bufferIterator) {
81
+ yield * parser(value)
82
+ }
83
+ })()
84
+ }
85
+ }
package/node-index.js CHANGED
@@ -1,24 +1,24 @@
1
- export { Encoder, addExtension, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './encode.js'
2
- export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
3
- export { EncoderStream, DecoderStream } from './stream.js'
4
- export { decodeIter, encodeIter } from './iterators.js'
5
- export const useRecords = false
6
- export const mapsAsObjects = true
7
- import { setExtractor } from './decode.js'
8
- import { createRequire } from 'module'
9
-
10
- const nativeAccelerationDisabled = process.env.CBOR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.CBOR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
11
-
12
- if (!nativeAccelerationDisabled) {
13
- let extractor
14
- try {
15
- if (typeof require == 'function')
16
- extractor = require('cbor-extract')
17
- else
18
- extractor = createRequire(import.meta.url)('cbor-extract')
19
- if (extractor)
20
- setExtractor(extractor.extractStrings)
21
- } catch (error) {
22
- // native module is optional
23
- }
24
- }
1
+ export { Encoder, addExtension, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './encode.js'
2
+ export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32, isNativeAccelerationEnabled } from './decode.js'
3
+ export { EncoderStream, DecoderStream } from './stream.js'
4
+ export { decodeIter, encodeIter } from './iterators.js'
5
+ export const useRecords = false
6
+ export const mapsAsObjects = true
7
+ import { setExtractor } from './decode.js'
8
+ import { createRequire } from 'module'
9
+
10
+ const nativeAccelerationDisabled = process.env.CBOR_NATIVE_ACCELERATION_DISABLED !== undefined && process.env.CBOR_NATIVE_ACCELERATION_DISABLED.toLowerCase() === 'true';
11
+
12
+ if (!nativeAccelerationDisabled) {
13
+ let extractor
14
+ try {
15
+ if (typeof require == 'function')
16
+ extractor = require('cbor-extract')
17
+ else
18
+ extractor = createRequire(import.meta.url)('cbor-extract')
19
+ if (extractor)
20
+ setExtractor(extractor.extractStrings)
21
+ } catch (error) {
22
+ // native module is optional
23
+ }
24
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cbor-x",
3
3
  "author": "Kris Zyp",
4
- "version": "1.3.3",
4
+ "version": "1.4.1",
5
5
  "description": "Ultra-fast and conformant CBOR (RFC 8949) implementation with support for numerous tag extensions including records and structured cloning",
6
6
  "license": "MIT",
7
7
  "types": "./index.d.ts",
@@ -31,6 +31,10 @@
31
31
  "require": "./dist/node.cjs",
32
32
  "import": "./node-index.js"
33
33
  },
34
+ "types": {
35
+ "require": "./index.d.cts",
36
+ "import": "./index.d.ts"
37
+ },
34
38
  "default": "./index.js"
35
39
  },
36
40
  "./encode": {
@@ -69,6 +73,7 @@
69
73
  "cbor": "^5",
70
74
  "cbor-sync": "^1.0.4",
71
75
  "chai": "^4.3.4",
76
+ "cpy-cli": "^4.1.0",
72
77
  "esm": "^3.2.25",
73
78
  "mocha": "^8.1.3",
74
79
  "rollup": "^1.20.3",
package/rollup.config.js CHANGED
@@ -1,45 +1,45 @@
1
- import minify from "rollup-plugin-babel-minify";
2
- import json from "@rollup/plugin-json";
3
-
4
- export default [
5
- {
6
- input: "node-index.js",
7
- output: [
8
- {
9
- file: "dist/node.cjs",
10
- format: "cjs"
11
- }
12
- ]
13
- },
14
- {
15
- input: "index.js",
16
- output: {
17
- file: "dist/index.js",
18
- format: "umd",
19
- name: "CBOR"
20
- }
21
- },
22
- {
23
- input: "index.js",
24
- plugins: [minify({
25
- })],
26
- output: {
27
- file: "dist/index.min.js",
28
- format: "umd",
29
- name: "CBOR"
30
- }
31
- },
32
- {
33
- input: "tests/test.js",
34
- plugins: [json()],
35
- external: ['chai', '../index.js'],
36
- output: {
37
- file: "dist/test.js",
38
- format: "iife",
39
- globals: {
40
- chai: 'chai',
41
- './index.js': 'CBOR',
42
- },
43
- }
44
- }
45
- ];
1
+ import minify from "rollup-plugin-babel-minify";
2
+ import json from "@rollup/plugin-json";
3
+
4
+ export default [
5
+ {
6
+ input: "node-index.js",
7
+ output: [
8
+ {
9
+ file: "dist/node.cjs",
10
+ format: "cjs"
11
+ }
12
+ ]
13
+ },
14
+ {
15
+ input: "index.js",
16
+ output: {
17
+ file: "dist/index.js",
18
+ format: "umd",
19
+ name: "CBOR"
20
+ }
21
+ },
22
+ {
23
+ input: "index.js",
24
+ plugins: [minify({
25
+ })],
26
+ output: {
27
+ file: "dist/index.min.js",
28
+ format: "umd",
29
+ name: "CBOR"
30
+ }
31
+ },
32
+ {
33
+ input: "tests/test.js",
34
+ plugins: [json()],
35
+ external: ['chai', '../index.js'],
36
+ output: {
37
+ file: "dist/test.js",
38
+ format: "iife",
39
+ globals: {
40
+ chai: 'chai',
41
+ './index.js': 'CBOR',
42
+ },
43
+ }
44
+ }
45
+ ];
package/stream.js CHANGED
@@ -1,57 +1,57 @@
1
- import { Transform } from 'stream'
2
- import { Encoder } from './encode.js'
3
- import { checkedRead, getPosition, Decoder, clearSource } from './decode.js'
4
- var DEFAULT_OPTIONS = {objectMode: true}
5
-
6
- export class EncoderStream extends Transform {
7
- constructor(options) {
8
- if (!options)
9
- options = {}
10
- options.writableObjectMode = true
11
- super(options)
12
- options.sequential = true
13
- this.encoder = options.encoder || new Encoder(options)
14
- }
15
- _transform(value, encoding, callback) {
16
- this.push(this.encoder.encode(value))
17
- callback()
18
- }
19
- }
20
-
21
- export class DecoderStream extends Transform {
22
- constructor(options) {
23
- if (!options)
24
- options = {}
25
- options.objectMode = true
26
- super(options)
27
- options.structures = []
28
- this.decoder = options.decoder || new Decoder(options)
29
- }
30
- _transform(chunk, encoding, callback) {
31
- if (this.incompleteBuffer) {
32
- chunk = Buffer.concat([this.incompleteBuffer, chunk])
33
- this.incompleteBuffer = null
34
- }
35
- let values
36
- try {
37
- values = this.decoder.decodeMultiple(chunk)
38
- } catch(error) {
39
- if (error.incomplete) {
40
- this.incompleteBuffer = chunk.slice(error.lastPosition)
41
- values = error.values
42
- }
43
- else
44
- throw error
45
- } finally {
46
- for (let value of values || []) {
47
- if (value === null)
48
- value = this.getNullValue()
49
- this.push(value)
50
- }
51
- }
52
- if (callback) callback()
53
- }
54
- getNullValue() {
55
- return Symbol.for(null)
56
- }
57
- }
1
+ import { Transform } from 'stream'
2
+ import { Encoder } from './encode.js'
3
+ import { checkedRead, getPosition, Decoder, clearSource } from './decode.js'
4
+ var DEFAULT_OPTIONS = {objectMode: true}
5
+
6
+ export class EncoderStream extends Transform {
7
+ constructor(options) {
8
+ if (!options)
9
+ options = {}
10
+ options.writableObjectMode = true
11
+ super(options)
12
+ options.sequential = true
13
+ this.encoder = options.encoder || new Encoder(options)
14
+ }
15
+ _transform(value, encoding, callback) {
16
+ this.push(this.encoder.encode(value))
17
+ callback()
18
+ }
19
+ }
20
+
21
+ export class DecoderStream extends Transform {
22
+ constructor(options) {
23
+ if (!options)
24
+ options = {}
25
+ options.objectMode = true
26
+ super(options)
27
+ options.structures = []
28
+ this.decoder = options.decoder || new Decoder(options)
29
+ }
30
+ _transform(chunk, encoding, callback) {
31
+ if (this.incompleteBuffer) {
32
+ chunk = Buffer.concat([this.incompleteBuffer, chunk])
33
+ this.incompleteBuffer = null
34
+ }
35
+ let values
36
+ try {
37
+ values = this.decoder.decodeMultiple(chunk)
38
+ } catch(error) {
39
+ if (error.incomplete) {
40
+ this.incompleteBuffer = chunk.slice(error.lastPosition)
41
+ values = error.values
42
+ }
43
+ else
44
+ throw error
45
+ } finally {
46
+ for (let value of values || []) {
47
+ if (value === null)
48
+ value = this.getNullValue()
49
+ this.push(value)
50
+ }
51
+ }
52
+ if (callback) callback()
53
+ }
54
+ getNullValue() {
55
+ return Symbol.for(null)
56
+ }
57
+ }
package/webpack.config.js CHANGED
@@ -1,19 +1,19 @@
1
- var webpack = require('webpack')
2
- var path = require('path')
3
- module.exports = {
4
- entry: {
5
- index: './browser.js'
6
- },
7
- output: {
8
- path: path.join(__dirname, 'dist'),
9
- library: 'CBOR',
10
- libraryTarget: 'umd'
11
- },
12
- node: { Buffer: false },
13
- devtool: 'source-map',
14
- optimization: {
15
- minimize: true
16
- },
17
- //mode: 'development'
18
- mode: 'production'
19
- };
1
+ var webpack = require('webpack')
2
+ var path = require('path')
3
+ module.exports = {
4
+ entry: {
5
+ index: './browser.js'
6
+ },
7
+ output: {
8
+ path: path.join(__dirname, 'dist'),
9
+ library: 'CBOR',
10
+ libraryTarget: 'umd'
11
+ },
12
+ node: { Buffer: false },
13
+ devtool: 'source-map',
14
+ optimization: {
15
+ minimize: true
16
+ },
17
+ //mode: 'development'
18
+ mode: 'production'
19
+ };