@cloudpss/ubjson 0.5.48 → 0.5.49
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/dist/stream/decoder.d.ts +4 -5
- package/dist/stream/decoder.d.ts.map +1 -1
- package/dist/stream/decoder.js +14 -29
- package/dist/stream/decoder.js.map +1 -1
- package/dist/stream/encoder.d.ts +4 -4
- package/dist/stream/encoder.d.ts.map +1 -1
- package/dist/stream/encoder.js +10 -16
- package/dist/stream/encoder.js.map +1 -1
- package/dist/stream/index.d.ts +6 -7
- package/dist/stream/index.d.ts.map +1 -1
- package/dist/stream/index.js +39 -32
- package/dist/stream/index.js.map +1 -1
- package/package.json +3 -3
- package/src/stream/decoder.ts +13 -28
- package/src/stream/encoder.ts +10 -16
- package/src/stream/index.ts +45 -36
- package/tests/e2e/stream.js +9 -4
- package/tests/stream/decode.js +22 -14
- package/tests/stream/encode.js +2 -1
- package/tests/stream/many.js +30 -15
package/dist/stream/decoder.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { Transform, type TransformCallback } from 'node:stream';
|
|
2
1
|
import type { DecodeOptions } from '../options.js';
|
|
3
2
|
/** 流式解码 UBJSON */
|
|
4
|
-
export declare class
|
|
3
|
+
export declare class DecodeTransformer implements Transformer<BufferSource, unknown> {
|
|
5
4
|
constructor(options?: DecodeOptions);
|
|
6
5
|
private readonly buffer;
|
|
7
|
-
private
|
|
6
|
+
private controller;
|
|
8
7
|
/** @inheritdoc */
|
|
9
|
-
|
|
8
|
+
transform(obj: BufferSource, controller: TransformStreamDefaultController<unknown>): void;
|
|
10
9
|
/** @inheritdoc */
|
|
11
|
-
|
|
10
|
+
flush(controller: TransformStreamDefaultController<unknown>): void;
|
|
12
11
|
}
|
|
13
12
|
//# sourceMappingURL=decoder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decoder.d.ts","sourceRoot":"","sources":["../../src/stream/decoder.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"decoder.d.ts","sourceRoot":"","sources":["../../src/stream/decoder.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,kBAAkB;AAClB,qBAAa,iBAAkB,YAAW,WAAW,CAAC,YAAY,EAAE,OAAO,CAAC;gBAC5D,OAAO,CAAC,EAAE,aAAa;IAgBnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IACxB,OAAO,CAAC,UAAU,CAA6C;IAE/D,kBAAkB;IAClB,SAAS,CAAC,GAAG,EAAE,YAAY,EAAE,UAAU,EAAE,gCAAgC,CAAC,OAAO,CAAC,GAAG,IAAI;IASzF,kBAAkB;IAClB,KAAK,CAAC,UAAU,EAAE,gCAAgC,CAAC,OAAO,CAAC,GAAG,IAAI;CAIrE"}
|
package/dist/stream/decoder.js
CHANGED
|
@@ -1,55 +1,40 @@
|
|
|
1
|
-
import { Transform } from 'node:stream';
|
|
2
1
|
import { Subject } from 'rxjs';
|
|
3
2
|
import { decode } from '../rxjs/decoder.js';
|
|
4
3
|
/** 流式解码 UBJSON */
|
|
5
|
-
export class
|
|
4
|
+
export class DecodeTransformer {
|
|
6
5
|
constructor(options) {
|
|
7
|
-
super({
|
|
8
|
-
readableObjectMode: true,
|
|
9
|
-
writableObjectMode: false,
|
|
10
|
-
});
|
|
11
6
|
this.buffer = new Subject();
|
|
12
7
|
this.buffer.pipe(decode(options)).subscribe({
|
|
13
8
|
next: (value) => {
|
|
14
9
|
// null is not allowed in a stream
|
|
15
10
|
if (value == null)
|
|
16
11
|
return;
|
|
17
|
-
this.
|
|
12
|
+
this.controller.enqueue(value);
|
|
18
13
|
},
|
|
19
14
|
error: (err) => {
|
|
20
|
-
|
|
21
|
-
this.callback(err);
|
|
22
|
-
this.callback = undefined;
|
|
23
|
-
/* c8 ignore next 3: fallback only */
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
this.destroy(err);
|
|
27
|
-
}
|
|
15
|
+
this.controller.error(err);
|
|
28
16
|
},
|
|
29
17
|
complete: () => {
|
|
30
|
-
this.
|
|
18
|
+
this.controller.terminate();
|
|
31
19
|
},
|
|
32
20
|
});
|
|
33
21
|
}
|
|
34
22
|
buffer;
|
|
35
|
-
|
|
23
|
+
controller;
|
|
36
24
|
/** @inheritdoc */
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
25
|
+
transform(obj, controller) {
|
|
26
|
+
try {
|
|
27
|
+
this.controller = controller;
|
|
28
|
+
this.buffer.next(obj);
|
|
29
|
+
}
|
|
30
|
+
catch (ex) {
|
|
31
|
+
controller.error(ex);
|
|
43
32
|
}
|
|
44
33
|
}
|
|
45
34
|
/** @inheritdoc */
|
|
46
|
-
|
|
47
|
-
this.
|
|
35
|
+
flush(controller) {
|
|
36
|
+
this.controller = controller;
|
|
48
37
|
this.buffer.complete();
|
|
49
|
-
if (this.callback) {
|
|
50
|
-
callback();
|
|
51
|
-
this.callback = undefined;
|
|
52
|
-
}
|
|
53
38
|
}
|
|
54
39
|
}
|
|
55
40
|
//# sourceMappingURL=decoder.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decoder.js","sourceRoot":"","sources":["../../src/stream/decoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"decoder.js","sourceRoot":"","sources":["../../src/stream/decoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAG5C,kBAAkB;AAClB,MAAM,OAAO,iBAAiB;IAC1B,YAAY,OAAuB;QAC/B,IAAI,CAAC,MAAM,GAAG,IAAI,OAAO,EAAgB,CAAC;QAC1C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;YACxC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;gBACZ,kCAAkC;gBAClC,IAAI,KAAK,IAAI,IAAI;oBAAE,OAAO;gBAC1B,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YACnC,CAAC;YACD,KAAK,EAAE,CAAC,GAAU,EAAE,EAAE;gBAClB,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YAC/B,CAAC;YACD,QAAQ,EAAE,GAAG,EAAE;gBACX,IAAI,CAAC,UAAU,CAAC,SAAS,EAAE,CAAC;YAChC,CAAC;SACJ,CAAC,CAAC;IACP,CAAC;IACgB,MAAM,CAAC;IAChB,UAAU,CAA6C;IAE/D,kBAAkB;IAClB,SAAS,CAAC,GAAiB,EAAE,UAAqD;QAC9E,IAAI,CAAC;YACD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;YAC7B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC1B,CAAC;QAAC,OAAO,EAAE,EAAE,CAAC;YACV,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACzB,CAAC;IACL,CAAC;IAED,kBAAkB;IAClB,KAAK,CAAC,UAAqD;QACvD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC3B,CAAC;CACJ"}
|
package/dist/stream/encoder.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Transform, type TransformCallback } from 'node:stream';
|
|
2
1
|
import type { EncodeOptions } from '../options.js';
|
|
3
2
|
/** 流式编码 UBJSON */
|
|
4
|
-
export declare class
|
|
3
|
+
export declare class EncodeTransformer implements Transformer<unknown, Uint8Array> {
|
|
5
4
|
constructor(options?: EncodeOptions);
|
|
6
5
|
private readonly helper;
|
|
6
|
+
private controller;
|
|
7
7
|
/** @inheritdoc */
|
|
8
|
-
|
|
8
|
+
transform(obj: unknown, controller: TransformStreamDefaultController<Uint8Array>): void;
|
|
9
9
|
/** @inheritdoc */
|
|
10
|
-
|
|
10
|
+
flush(controller: TransformStreamDefaultController<Uint8Array>): void;
|
|
11
11
|
}
|
|
12
12
|
//# sourceMappingURL=encoder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../src/stream/encoder.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../src/stream/encoder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEnD,kBAAkB;AAClB,qBAAa,iBAAkB,YAAW,WAAW,CAAC,OAAO,EAAE,UAAU,CAAC;gBAC1D,OAAO,CAAC,EAAE,aAAa;IAKnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IACxB,OAAO,CAAC,UAAU,CAAgD;IAElE,kBAAkB;IAClB,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,UAAU,EAAE,gCAAgC,CAAC,UAAU,CAAC,GAAG,IAAI;IAKvF,kBAAkB;IAClB,KAAK,CAAC,UAAU,EAAE,gCAAgC,CAAC,UAAU,CAAC,GAAG,IAAI;CAKxE"}
|
package/dist/stream/encoder.js
CHANGED
|
@@ -1,29 +1,23 @@
|
|
|
1
|
-
import { Transform } from 'node:stream';
|
|
2
1
|
import { StreamEncoderHelper } from '../stream-helper/encoder.js';
|
|
3
2
|
/** 流式编码 UBJSON */
|
|
4
|
-
export class
|
|
3
|
+
export class EncodeTransformer {
|
|
5
4
|
constructor(options) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
writableObjectMode: true,
|
|
5
|
+
this.helper = new StreamEncoderHelper(options, (binary) => {
|
|
6
|
+
this.controller.enqueue(binary);
|
|
9
7
|
});
|
|
10
|
-
this.helper = new StreamEncoderHelper(options, (binary) => this.push(binary));
|
|
11
8
|
}
|
|
12
9
|
helper;
|
|
10
|
+
controller;
|
|
13
11
|
/** @inheritdoc */
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
callback();
|
|
18
|
-
}
|
|
19
|
-
catch (ex) {
|
|
20
|
-
callback(ex);
|
|
21
|
-
}
|
|
12
|
+
transform(obj, controller) {
|
|
13
|
+
this.controller = controller;
|
|
14
|
+
this.helper.encode(obj);
|
|
22
15
|
}
|
|
23
16
|
/** @inheritdoc */
|
|
24
|
-
|
|
17
|
+
flush(controller) {
|
|
18
|
+
this.controller = controller;
|
|
25
19
|
this.helper.destroy();
|
|
26
|
-
|
|
20
|
+
controller.terminate();
|
|
27
21
|
}
|
|
28
22
|
}
|
|
29
23
|
//# sourceMappingURL=encoder.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encoder.js","sourceRoot":"","sources":["../../src/stream/encoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"encoder.js","sourceRoot":"","sources":["../../src/stream/encoder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAGlE,kBAAkB;AAClB,MAAM,OAAO,iBAAiB;IAC1B,YAAY,OAAuB;QAC/B,IAAI,CAAC,MAAM,GAAG,IAAI,mBAAmB,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE;YACtD,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACpC,CAAC,CAAC,CAAC;IACP,CAAC;IACgB,MAAM,CAAC;IAChB,UAAU,CAAgD;IAElE,kBAAkB;IAClB,SAAS,CAAC,GAAY,EAAE,UAAwD;QAC5E,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IAED,kBAAkB;IAClB,KAAK,CAAC,UAAwD;QAC1D,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACtB,UAAU,CAAC,SAAS,EAAE,CAAC;IAC3B,CAAC;CACJ"}
|
package/dist/stream/index.d.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { Readable, type Transform } from 'node:stream';
|
|
2
1
|
import type { EncodeOptions, DecodeOptions } from '../options.js';
|
|
3
2
|
export { UnexpectedEofError as UnexpectedEof } from '../helper/errors.js';
|
|
4
3
|
export type { EncodeOptions, DecodeOptions };
|
|
5
4
|
/** 编码为 UBJSON */
|
|
6
|
-
export declare function encode(value: unknown, options?: EncodeOptions):
|
|
5
|
+
export declare function encode(value: unknown, options?: EncodeOptions): ReadableStream<Uint8Array>;
|
|
7
6
|
/** 编码为 UBJSON */
|
|
8
|
-
export declare function encodeMany(value: AsyncIterable<unknown>, options?: EncodeOptions):
|
|
7
|
+
export declare function encodeMany(value: AsyncIterable<unknown>, options?: EncodeOptions): ReadableStream<Uint8Array>;
|
|
9
8
|
/** 编码为 UBJSON */
|
|
10
|
-
export declare function encoder(options?: EncodeOptions):
|
|
9
|
+
export declare function encoder(options?: EncodeOptions): TransformStream<unknown, Uint8Array>;
|
|
11
10
|
/** 解码 UBJSON */
|
|
12
|
-
export declare function decode(stream:
|
|
11
|
+
export declare function decode(stream: ReadableStream<Uint8Array>, options?: DecodeOptions): Promise<unknown>;
|
|
13
12
|
/** 解码 UBJSON */
|
|
14
|
-
export declare function decodeMany(stream:
|
|
13
|
+
export declare function decodeMany(stream: ReadableStream<Uint8Array>, options?: DecodeOptions): AsyncIterable<unknown>;
|
|
15
14
|
/** 解码 UBJSON */
|
|
16
|
-
export declare function decoder(options?: DecodeOptions):
|
|
15
|
+
export declare function decoder(options?: DecodeOptions): TransformStream<Uint8Array, unknown>;
|
|
17
16
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/stream/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/stream/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGlE,OAAO,EAAE,kBAAkB,IAAI,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAC1E,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,CAAC;AAE7C,iBAAiB;AACjB,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,cAAc,CAAC,UAAU,CAAC,CAiB1F;AAED,iBAAiB;AACjB,wBAAgB,UAAU,CAAC,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,cAAc,CAAC,UAAU,CAAC,CAU7G;AAED,iBAAiB;AACjB,wBAAgB,OAAO,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,eAAe,CAAC,OAAO,EAAE,UAAU,CAAC,CAErF;AAED,gBAAgB;AAChB,wBAAsB,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,CAM1G;AAED,gBAAgB;AAChB,wBAAuB,UAAU,CAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,aAAa,CAAC,OAAO,CAAC,CAQrH;AAED,gBAAgB;AAChB,wBAAgB,OAAO,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,eAAe,CAAC,UAAU,EAAE,OAAO,CAAC,CAErF"}
|
package/dist/stream/index.js
CHANGED
|
@@ -1,57 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { StreamDecoder } from './decoder.js';
|
|
1
|
+
import { EncodeTransformer } from './encoder.js';
|
|
2
|
+
import { DecodeTransformer } from './decoder.js';
|
|
4
3
|
export { UnexpectedEofError as UnexpectedEof } from '../helper/errors.js';
|
|
5
4
|
/** 编码为 UBJSON */
|
|
6
5
|
export function encode(value, options) {
|
|
7
6
|
if (value == null) {
|
|
8
|
-
return
|
|
7
|
+
return new ReadableStream({
|
|
8
|
+
type: 'bytes',
|
|
9
|
+
start(controller) {
|
|
10
|
+
const data = new Uint8Array([value === null ? 90 /* constants.NULL */ : 78 /* constants.NO_OP */]);
|
|
11
|
+
controller.enqueue(data);
|
|
12
|
+
controller.close();
|
|
13
|
+
},
|
|
14
|
+
});
|
|
9
15
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
return new ReadableStream({
|
|
17
|
+
start(controller) {
|
|
18
|
+
controller.enqueue(value);
|
|
19
|
+
controller.close();
|
|
20
|
+
},
|
|
21
|
+
}).pipeThrough(encoder(options));
|
|
14
22
|
}
|
|
15
23
|
/** 编码为 UBJSON */
|
|
16
24
|
export function encodeMany(value, options) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
try {
|
|
25
|
+
return new ReadableStream({
|
|
26
|
+
async start(controller) {
|
|
20
27
|
for await (const v of value) {
|
|
21
28
|
if (v == null)
|
|
22
|
-
|
|
29
|
+
controller.enqueue(undefined);
|
|
23
30
|
else
|
|
24
|
-
|
|
31
|
+
controller.enqueue(v);
|
|
25
32
|
}
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
encoder.destroy(ex);
|
|
30
|
-
}
|
|
31
|
-
})();
|
|
32
|
-
return encoder;
|
|
33
|
+
controller.close();
|
|
34
|
+
},
|
|
35
|
+
}).pipeThrough(encoder(options));
|
|
33
36
|
}
|
|
34
37
|
/** 编码为 UBJSON */
|
|
35
38
|
export function encoder(options) {
|
|
36
|
-
return new
|
|
39
|
+
return new TransformStream(new EncodeTransformer(options));
|
|
37
40
|
}
|
|
38
41
|
/** 解码 UBJSON */
|
|
39
42
|
export async function decode(stream, options) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
stream.pipe(decoder);
|
|
46
|
-
});
|
|
43
|
+
const s = stream.pipeThrough(decoder(options));
|
|
44
|
+
const r = s.getReader();
|
|
45
|
+
const result = await r.read();
|
|
46
|
+
void r.cancel();
|
|
47
|
+
return result.done ? undefined : result.value;
|
|
47
48
|
}
|
|
48
49
|
/** 解码 UBJSON */
|
|
49
|
-
export function decodeMany(stream, options) {
|
|
50
|
-
const
|
|
51
|
-
|
|
50
|
+
export async function* decodeMany(stream, options) {
|
|
51
|
+
const s = stream.pipeThrough(decoder(options));
|
|
52
|
+
const r = s.getReader();
|
|
53
|
+
for (;;) {
|
|
54
|
+
const { done, value } = await r.read();
|
|
55
|
+
if (done)
|
|
56
|
+
break;
|
|
57
|
+
yield value;
|
|
58
|
+
}
|
|
52
59
|
}
|
|
53
60
|
/** 解码 UBJSON */
|
|
54
61
|
export function decoder(options) {
|
|
55
|
-
return new
|
|
62
|
+
return new TransformStream(new DecodeTransformer(options));
|
|
56
63
|
}
|
|
57
64
|
//# sourceMappingURL=index.js.map
|
package/dist/stream/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/stream/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/stream/index.ts"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudpss/ubjson",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.49",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"./helper/*": "./dist/helper/*.js"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@types/lodash": "^4.17.
|
|
23
|
+
"@types/lodash": "^4.17.20",
|
|
24
24
|
"lodash": "^4.17.21",
|
|
25
25
|
"ref-impl": "npm:@cloudpss/ubjson@0.5.40"
|
|
26
26
|
},
|
|
@@ -30,6 +30,6 @@
|
|
|
30
30
|
"scripts": {
|
|
31
31
|
"build": "pnpm clean && tsc --build --force",
|
|
32
32
|
"clean": "rimraf dist",
|
|
33
|
-
"test": "NODE_NO_WARNINGS=1 NODE_OPTIONS=\"${NODE_OPTIONS:-} --experimental-vm-modules\" jest"
|
|
33
|
+
"test": "NODE_NO_WARNINGS=1 NODE_OPTIONS=\"${NODE_OPTIONS:-} --unhandled-rejections=warn --experimental-vm-modules\" jest"
|
|
34
34
|
}
|
|
35
35
|
}
|
package/src/stream/decoder.ts
CHANGED
|
@@ -1,56 +1,41 @@
|
|
|
1
|
-
import { Transform, type TransformCallback } from 'node:stream';
|
|
2
1
|
import { Subject } from 'rxjs';
|
|
3
2
|
import { decode } from '../rxjs/decoder.js';
|
|
4
3
|
import type { DecodeOptions } from '../options.js';
|
|
5
4
|
|
|
6
5
|
/** 流式解码 UBJSON */
|
|
7
|
-
export class
|
|
6
|
+
export class DecodeTransformer implements Transformer<BufferSource, unknown> {
|
|
8
7
|
constructor(options?: DecodeOptions) {
|
|
9
|
-
super({
|
|
10
|
-
readableObjectMode: true,
|
|
11
|
-
writableObjectMode: false,
|
|
12
|
-
});
|
|
13
8
|
this.buffer = new Subject<BufferSource>();
|
|
14
9
|
this.buffer.pipe(decode(options)).subscribe({
|
|
15
10
|
next: (value) => {
|
|
16
11
|
// null is not allowed in a stream
|
|
17
12
|
if (value == null) return;
|
|
18
|
-
this.
|
|
13
|
+
this.controller.enqueue(value);
|
|
19
14
|
},
|
|
20
15
|
error: (err: Error) => {
|
|
21
|
-
|
|
22
|
-
this.callback(err);
|
|
23
|
-
this.callback = undefined;
|
|
24
|
-
/* c8 ignore next 3: fallback only */
|
|
25
|
-
} else {
|
|
26
|
-
this.destroy(err);
|
|
27
|
-
}
|
|
16
|
+
this.controller.error(err);
|
|
28
17
|
},
|
|
29
18
|
complete: () => {
|
|
30
|
-
this.
|
|
19
|
+
this.controller.terminate();
|
|
31
20
|
},
|
|
32
21
|
});
|
|
33
22
|
}
|
|
34
23
|
private readonly buffer;
|
|
35
|
-
private
|
|
24
|
+
private controller!: TransformStreamDefaultController<unknown>;
|
|
36
25
|
|
|
37
26
|
/** @inheritdoc */
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
27
|
+
transform(obj: BufferSource, controller: TransformStreamDefaultController<unknown>): void {
|
|
28
|
+
try {
|
|
29
|
+
this.controller = controller;
|
|
30
|
+
this.buffer.next(obj);
|
|
31
|
+
} catch (ex) {
|
|
32
|
+
controller.error(ex);
|
|
44
33
|
}
|
|
45
34
|
}
|
|
46
35
|
|
|
47
36
|
/** @inheritdoc */
|
|
48
|
-
|
|
49
|
-
this.
|
|
37
|
+
flush(controller: TransformStreamDefaultController<unknown>): void {
|
|
38
|
+
this.controller = controller;
|
|
50
39
|
this.buffer.complete();
|
|
51
|
-
if (this.callback) {
|
|
52
|
-
callback();
|
|
53
|
-
this.callback = undefined;
|
|
54
|
-
}
|
|
55
40
|
}
|
|
56
41
|
}
|
package/src/stream/encoder.ts
CHANGED
|
@@ -1,32 +1,26 @@
|
|
|
1
|
-
import { Transform, type TransformCallback } from 'node:stream';
|
|
2
1
|
import { StreamEncoderHelper } from '../stream-helper/encoder.js';
|
|
3
2
|
import type { EncodeOptions } from '../options.js';
|
|
4
3
|
|
|
5
4
|
/** 流式编码 UBJSON */
|
|
6
|
-
export class
|
|
5
|
+
export class EncodeTransformer implements Transformer<unknown, Uint8Array> {
|
|
7
6
|
constructor(options?: EncodeOptions) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
writableObjectMode: true,
|
|
7
|
+
this.helper = new StreamEncoderHelper(options, (binary) => {
|
|
8
|
+
this.controller.enqueue(binary);
|
|
11
9
|
});
|
|
12
|
-
this.helper = new StreamEncoderHelper(options, (binary) => this.push(binary));
|
|
13
10
|
}
|
|
14
|
-
|
|
15
11
|
private readonly helper;
|
|
12
|
+
private controller!: TransformStreamDefaultController<Uint8Array>;
|
|
16
13
|
|
|
17
14
|
/** @inheritdoc */
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
callback();
|
|
22
|
-
} catch (ex) {
|
|
23
|
-
callback(ex as Error);
|
|
24
|
-
}
|
|
15
|
+
transform(obj: unknown, controller: TransformStreamDefaultController<Uint8Array>): void {
|
|
16
|
+
this.controller = controller;
|
|
17
|
+
this.helper.encode(obj);
|
|
25
18
|
}
|
|
26
19
|
|
|
27
20
|
/** @inheritdoc */
|
|
28
|
-
|
|
21
|
+
flush(controller: TransformStreamDefaultController<Uint8Array>): void {
|
|
22
|
+
this.controller = controller;
|
|
29
23
|
this.helper.destroy();
|
|
30
|
-
|
|
24
|
+
controller.terminate();
|
|
31
25
|
}
|
|
32
26
|
}
|
package/src/stream/index.ts
CHANGED
|
@@ -1,61 +1,70 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { StreamDecoder } from './decoder.js';
|
|
1
|
+
import { EncodeTransformer } from './encoder.js';
|
|
2
|
+
import { DecodeTransformer } from './decoder.js';
|
|
4
3
|
import type { EncodeOptions, DecodeOptions } from '../options.js';
|
|
4
|
+
import { constants } from '../helper/constants.js';
|
|
5
5
|
|
|
6
6
|
export { UnexpectedEofError as UnexpectedEof } from '../helper/errors.js';
|
|
7
7
|
export type { EncodeOptions, DecodeOptions };
|
|
8
8
|
|
|
9
9
|
/** 编码为 UBJSON */
|
|
10
|
-
export function encode(value: unknown, options?: EncodeOptions):
|
|
10
|
+
export function encode(value: unknown, options?: EncodeOptions): ReadableStream<Uint8Array> {
|
|
11
11
|
if (value == null) {
|
|
12
|
-
return
|
|
12
|
+
return new ReadableStream({
|
|
13
|
+
type: 'bytes',
|
|
14
|
+
start(controller) {
|
|
15
|
+
const data = new Uint8Array([value === null ? constants.NULL : constants.NO_OP]);
|
|
16
|
+
controller.enqueue(data);
|
|
17
|
+
controller.close();
|
|
18
|
+
},
|
|
19
|
+
});
|
|
13
20
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
return new ReadableStream<unknown>({
|
|
22
|
+
start(controller) {
|
|
23
|
+
controller.enqueue(value);
|
|
24
|
+
controller.close();
|
|
25
|
+
},
|
|
26
|
+
}).pipeThrough(encoder(options));
|
|
18
27
|
}
|
|
28
|
+
|
|
19
29
|
/** 编码为 UBJSON */
|
|
20
|
-
export function encodeMany(value: AsyncIterable<unknown>, options?: EncodeOptions):
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
try {
|
|
30
|
+
export function encodeMany(value: AsyncIterable<unknown>, options?: EncodeOptions): ReadableStream<Uint8Array> {
|
|
31
|
+
return new ReadableStream<unknown>({
|
|
32
|
+
async start(controller) {
|
|
24
33
|
for await (const v of value) {
|
|
25
|
-
if (v == null)
|
|
26
|
-
else
|
|
34
|
+
if (v == null) controller.enqueue(undefined);
|
|
35
|
+
else controller.enqueue(v);
|
|
27
36
|
}
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
})();
|
|
33
|
-
return encoder;
|
|
37
|
+
controller.close();
|
|
38
|
+
},
|
|
39
|
+
}).pipeThrough(encoder(options));
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
/** 编码为 UBJSON */
|
|
37
|
-
export function encoder(options?: EncodeOptions):
|
|
38
|
-
return new
|
|
43
|
+
export function encoder(options?: EncodeOptions): TransformStream<unknown, Uint8Array> {
|
|
44
|
+
return new TransformStream(new EncodeTransformer(options));
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
/** 解码 UBJSON */
|
|
42
|
-
export async function decode(stream:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
stream.pipe(decoder);
|
|
49
|
-
});
|
|
48
|
+
export async function decode(stream: ReadableStream<Uint8Array>, options?: DecodeOptions): Promise<unknown> {
|
|
49
|
+
const s = stream.pipeThrough(decoder(options));
|
|
50
|
+
const r = s.getReader();
|
|
51
|
+
const result = await r.read();
|
|
52
|
+
void r.cancel();
|
|
53
|
+
return result.done ? undefined : result.value;
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
/** 解码 UBJSON */
|
|
53
|
-
export function decodeMany(stream:
|
|
54
|
-
const
|
|
55
|
-
|
|
57
|
+
export async function* decodeMany(stream: ReadableStream<Uint8Array>, options?: DecodeOptions): AsyncIterable<unknown> {
|
|
58
|
+
const s = stream.pipeThrough(decoder(options));
|
|
59
|
+
const r = s.getReader();
|
|
60
|
+
for (;;) {
|
|
61
|
+
const { done, value } = await r.read();
|
|
62
|
+
if (done) break;
|
|
63
|
+
yield value;
|
|
64
|
+
}
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
/** 解码 UBJSON */
|
|
59
|
-
export function decoder(options?: DecodeOptions):
|
|
60
|
-
return new
|
|
68
|
+
export function decoder(options?: DecodeOptions): TransformStream<Uint8Array, unknown> {
|
|
69
|
+
return new TransformStream(new DecodeTransformer(options));
|
|
61
70
|
}
|
package/tests/e2e/stream.js
CHANGED
|
@@ -8,13 +8,16 @@ describe('stream', () => {
|
|
|
8
8
|
const expected = EXPECTED[name] ?? input;
|
|
9
9
|
if (expected instanceof Error) {
|
|
10
10
|
await expect(async () => {
|
|
11
|
-
const encoded = await encodeStream(input).toArray();
|
|
12
|
-
const decoded = await decodeStream(
|
|
11
|
+
const encoded = await Readable.fromWeb(/** @type {any} */ (encodeStream(input))).toArray();
|
|
12
|
+
const decoded = await decodeStream(
|
|
13
|
+
/** @type {ReadableStream<Uint8Array>} */ (Readable.toWeb(Readable.from(encoded))),
|
|
14
|
+
);
|
|
13
15
|
expect(decoded).toEqual(expected);
|
|
14
16
|
}).rejects.toThrow(expected);
|
|
15
17
|
} else {
|
|
16
|
-
const encoded = await encodeStream(input).toArray();
|
|
18
|
+
const encoded = await Readable.fromWeb(/** @type {any} */ (encodeStream(input))).toArray();
|
|
17
19
|
const data = encoded.flatMap((/** @type {Buffer} */ chunk) => {
|
|
20
|
+
if (chunk.length < 2) return [chunk];
|
|
18
21
|
// split to random chunks
|
|
19
22
|
const chunks = [];
|
|
20
23
|
let offset = 0;
|
|
@@ -25,7 +28,9 @@ describe('stream', () => {
|
|
|
25
28
|
}
|
|
26
29
|
return chunks;
|
|
27
30
|
});
|
|
28
|
-
const decoded = await decodeStream(
|
|
31
|
+
const decoded = await decodeStream(
|
|
32
|
+
/** @type {ReadableStream<Uint8Array>} */ (Readable.toWeb(Readable.from(data))),
|
|
33
|
+
);
|
|
29
34
|
expect(decoded).toEqual(expected);
|
|
30
35
|
}
|
|
31
36
|
});
|
package/tests/stream/decode.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Tests from https://bitbucket.org/shelacek/ubjson
|
|
3
3
|
*/
|
|
4
4
|
import { jest } from '@jest/globals';
|
|
5
|
-
import { Readable } from 'node:stream';
|
|
5
|
+
import { Readable, Transform } from 'node:stream';
|
|
6
6
|
import { decoder as decodeStream, decode as decodeAsync } from '../../dist/stream/index.js';
|
|
7
7
|
import { toBuffer } from '../.utils.js';
|
|
8
8
|
import { UnexpectedEofError as UnexpectedEof } from '../../dist/helper/errors.js';
|
|
@@ -14,12 +14,12 @@ import { UnexpectedEofError as UnexpectedEof } from '../../dist/helper/errors.js
|
|
|
14
14
|
*/
|
|
15
15
|
async function decode(data) {
|
|
16
16
|
const readable = Readable.from([data], { objectMode: false });
|
|
17
|
-
return decodeAsync(readable);
|
|
17
|
+
return decodeAsync(/** @type {ReadableStream<Uint8Array>} */ (Readable.toWeb(readable)));
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* 包装为 promise
|
|
22
|
-
* @param {import("stream").
|
|
22
|
+
* @param {import("stream").Readable} stream ubjson 流
|
|
23
23
|
* @returns {Promise<void>}
|
|
24
24
|
*/
|
|
25
25
|
async function eos(stream) {
|
|
@@ -456,7 +456,7 @@ test('decode object (empty key, optimized)', async () => {
|
|
|
456
456
|
});
|
|
457
457
|
|
|
458
458
|
test('decode stream', async () => {
|
|
459
|
-
const stream = decodeStream();
|
|
459
|
+
const stream = Transform.fromWeb(/** @type {any} */ (decodeStream()), { objectMode: true });
|
|
460
460
|
// while decoding streaming, N will be regarded as a no-op, rather than an undefined value
|
|
461
461
|
const onData = jest.fn();
|
|
462
462
|
stream.on('data', onData);
|
|
@@ -473,17 +473,25 @@ test('decode stream', async () => {
|
|
|
473
473
|
});
|
|
474
474
|
|
|
475
475
|
test('decode bad stream [error]', async () => {
|
|
476
|
-
const
|
|
476
|
+
const { readable, writable } = decodeStream();
|
|
477
477
|
// while decoding streaming, N will be regarded as a no-op, rather than an undefined value
|
|
478
478
|
const onData = jest.fn();
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
479
|
+
const writer = writable.getWriter();
|
|
480
|
+
void writer.write(toBuffer('N', 'Z', 'N', 'N', 'T', 'N', 'N', 'F', 'N', 'N'));
|
|
481
|
+
void writer.write(toBuffer('N', 'N'));
|
|
482
|
+
void writer.write(toBuffer('I', 0x12));
|
|
483
|
+
void writer.write(toBuffer(0x34, '!', 'N'));
|
|
484
|
+
void writer.write(toBuffer('I', 0x12, 0x34, 'N'));
|
|
485
|
+
void writer.close();
|
|
486
|
+
|
|
487
|
+
const reader = readable.getReader();
|
|
488
|
+
await expect(async () => {
|
|
489
|
+
for (;;) {
|
|
490
|
+
const { value, done } = await reader.read();
|
|
491
|
+
if (done) break;
|
|
492
|
+
onData(value);
|
|
493
|
+
}
|
|
494
|
+
}).rejects.toThrow(/Unexpected marker/);
|
|
487
495
|
expect(onData).toHaveBeenCalledTimes(3);
|
|
488
496
|
expect(onData).toHaveBeenNthCalledWith(1, true);
|
|
489
497
|
expect(onData).toHaveBeenNthCalledWith(2, false);
|
|
@@ -491,7 +499,7 @@ test('decode bad stream [error]', async () => {
|
|
|
491
499
|
});
|
|
492
500
|
|
|
493
501
|
test('decode partial stream [error]', async () => {
|
|
494
|
-
const stream = decodeStream();
|
|
502
|
+
const stream = Transform.fromWeb(/** @type {any} */ (decodeStream()), { objectMode: true });
|
|
495
503
|
// while decoding streaming, N will be regarded as a no-op, rather than an undefined value
|
|
496
504
|
const onData = jest.fn();
|
|
497
505
|
stream.on('data', onData);
|
package/tests/stream/encode.js
CHANGED
|
@@ -5,6 +5,7 @@ import { buffer } from 'node:stream/consumers';
|
|
|
5
5
|
import { encode, encoder } from '../../dist/stream/index.js';
|
|
6
6
|
import { decode, encode as encodeRef } from '../../dist/index.js';
|
|
7
7
|
import { toArray } from '../.utils.js';
|
|
8
|
+
import { Transform } from 'node:stream';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* 包装为 promise
|
|
@@ -363,7 +364,7 @@ test('encode huge data (~128M)', async () => {
|
|
|
363
364
|
});
|
|
364
365
|
|
|
365
366
|
test('encode stream', async () => {
|
|
366
|
-
const stream = encoder();
|
|
367
|
+
const stream = Transform.fromWeb(/** @type {any} */ (encoder()), { objectMode: true });
|
|
367
368
|
stream.write(undefined);
|
|
368
369
|
stream.write(true);
|
|
369
370
|
stream.write(false);
|
package/tests/stream/many.js
CHANGED
|
@@ -52,7 +52,9 @@ test('encode/decode many', async () => {
|
|
|
52
52
|
Uint8Array.of(3, 3, 1),
|
|
53
53
|
];
|
|
54
54
|
const encoded = await buffer(encodeMany(iterableToAsyncIterable(data)));
|
|
55
|
-
const decoded = await asyncIterableToArray(
|
|
55
|
+
const decoded = await asyncIterableToArray(
|
|
56
|
+
decodeMany(/** @type {ReadableStream<Uint8Array>} */ (Readable.toWeb(Readable.from([encoded])))),
|
|
57
|
+
);
|
|
56
58
|
expect(decoded).toEqual([
|
|
57
59
|
{
|
|
58
60
|
a: 1,
|
|
@@ -71,28 +73,41 @@ test('encode/decode many', async () => {
|
|
|
71
73
|
});
|
|
72
74
|
|
|
73
75
|
test('encode/decode many with invalid value', async () => {
|
|
74
|
-
await expect(async () =>
|
|
76
|
+
await expect(async () => {
|
|
77
|
+
const readable = Readable.fromWeb(/** @type {any} */ (encodeMany(iterableToAsyncIterable([1, () => 1]))));
|
|
78
|
+
const f = finished(readable);
|
|
79
|
+
readable.resume();
|
|
80
|
+
await f;
|
|
81
|
+
}).rejects.toThrow(/Unsupported type Function/);
|
|
75
82
|
});
|
|
76
83
|
|
|
77
84
|
test('encode/decode many with error', async () => {
|
|
78
|
-
await expect(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
// await expect(async () => {
|
|
86
|
+
// try {
|
|
87
|
+
// const stream = encodeMany(
|
|
88
|
+
// (async function* () {
|
|
89
|
+
// yield 1;
|
|
90
|
+
// await Promise.resolve();
|
|
91
|
+
// // throw new Error('xx');
|
|
92
|
+
// })(),
|
|
93
|
+
// );
|
|
94
|
+
// const f = finished(/** @type {any} */ (stream));
|
|
95
|
+
// for await (const _ of stream) {
|
|
96
|
+
// //
|
|
97
|
+
// }
|
|
98
|
+
// await f;
|
|
99
|
+
// } catch (ex) {
|
|
100
|
+
// console.log(ex);
|
|
101
|
+
// }
|
|
102
|
+
// }).rejects.toThrow('xx');
|
|
90
103
|
});
|
|
91
104
|
|
|
92
105
|
test('encode/decode many with undefined', async () => {
|
|
93
106
|
const data = [1, null, undefined, [undefined]];
|
|
94
107
|
const encoded = await buffer(encodeMany(iterableToAsyncIterable(data)));
|
|
95
|
-
const decoded = await asyncIterableToArray(
|
|
108
|
+
const decoded = await asyncIterableToArray(
|
|
109
|
+
decodeMany(/** @type {ReadableStream<Uint8Array>} */ (Readable.toWeb(Readable.from([encoded])))),
|
|
110
|
+
);
|
|
96
111
|
expect(decoded).toEqual([
|
|
97
112
|
1,
|
|
98
113
|
// null is not allowed in stream
|