-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbinary.js
More file actions
119 lines (111 loc) · 3.4 KB
/
Copy pathbinary.js
File metadata and controls
119 lines (111 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Packr, Unpackr } from 'msgpackr';
import * as msgpackOfficial from '@msgpack/msgpack';
import { Encoder as CborXEncoder, Decoder as CborXDecoder } from 'cbor-x';
import cbor from 'cbor';
import { BSON } from 'bson';
import bser from 'bser';
import { pkgVersion, baseSupports, asBuffer } from './common.js';
// Reuse encoder instances (msgpackr docs: Packr/Unpackr are stateful and reusable).
const packr = new Packr({ useRecords: false, structuredClone: false });
const unpackr = new Unpackr({ useRecords: false });
export const msgpackrSer = {
name: 'msgpackr',
version: pkgVersion('msgpackr'),
category: 'binary',
supports: baseSupports,
prepare() {},
serialize(value) {
return packr.pack(value);
},
deserialize(buf) {
return unpackr.unpack(asBuffer(buf));
},
};
export const msgpackOffSer = {
name: '@msgpack/msgpack',
version: pkgVersion('@msgpack/msgpack'),
category: 'binary',
supports: baseSupports,
prepare() {},
serialize(value) {
// encode returns Uint8Array; avoid Buffer.from copy (runner accepts length).
return msgpackOfficial.encode(value);
},
deserialize(buf) {
// decode accepts Uint8Array / ArrayBuffer views without requiring Buffer.
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
return msgpackOfficial.decode(u8);
},
};
const cborxEnc = new CborXEncoder();
const cborxDec = new CborXDecoder();
export const cborxSer = {
name: 'cbor-x',
version: pkgVersion('cbor-x'),
category: 'binary',
supports: baseSupports,
prepare() {},
serialize(value) {
return cborxEnc.encode(value);
},
deserialize(buf) {
return cborxDec.decode(buf instanceof Uint8Array ? buf : new Uint8Array(buf));
},
};
export const cborSer = {
name: 'cbor',
version: pkgVersion('cbor'),
category: 'binary',
supports: baseSupports,
prepare() {},
serialize(value) {
// encodeOne: single-value encode (avoids accidental multi-value framing).
// https://github.com/hildjj/node-cbor
return typeof cbor.encodeOne === 'function' ? cbor.encodeOne(value) : cbor.encode(value);
},
deserialize(buf) {
const b = asBuffer(buf);
// decodeFirstSync is the recommended sync path for a complete buffer.
return cbor.decodeFirstSync(b);
},
};
export const bsonSer = {
name: 'bson',
version: pkgVersion('bson'),
category: 'binary',
// BSON top-level must be a document (V2 types are always plain objects / arrays of objects).
supports: baseSupports,
_wrapped: false,
prepare(_dataName, value) {
this._wrapped = Array.isArray(value) || typeof value !== 'object' || value === null;
},
serialize(value) {
const doc =
typeof value === 'object' && value !== null && !Array.isArray(value) ? value : { v: value };
return BSON.serialize(doc);
},
deserialize(buf) {
// BSON.serialize returns Buffer; deserialize accepts Buffer/Uint8Array.
const doc = BSON.deserialize(asBuffer(buf));
if (this._wrapped && doc && Object.prototype.hasOwnProperty.call(doc, 'v')) {
return doc.v;
}
return doc;
},
};
export const bserSer = {
name: 'bser',
version: pkgVersion('bser'),
category: 'binary',
supports: baseSupports,
prepare() {},
serialize(value) {
return bser.dumpToBuffer(value);
},
deserialize(buf) {
return bser.loadFromBuffer(asBuffer(buf));
},
};
export function binarySerializers() {
return [msgpackrSer, msgpackOffSer, cborxSer, cborSer, bsonSer, bserSer];
}