nv-buf-serde 0.0.2

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.
Files changed (52) hide show
  1. package/DIST/dist.js +25 -0
  2. package/README.md +382 -0
  3. package/TEST/common.js +435 -0
  4. package/TEST/hole-tst.js +31 -0
  5. package/TEST/nd-benchmark.js +17 -0
  6. package/TEST/r-1bstr.js +33 -0
  7. package/TEST/r-2bstr.js +33 -0
  8. package/TEST/r-ab-and-abvw.js +48 -0
  9. package/TEST/r-bi.js +33 -0
  10. package/TEST/r-date.js +28 -0
  11. package/TEST/r-double.js +33 -0
  12. package/TEST/r-int-not-smi.js +28 -0
  13. package/TEST/r-mp-st-circular.js +105 -0
  14. package/TEST/r-odd-ball.js +33 -0
  15. package/TEST/r-packed-double.js +38 -0
  16. package/TEST/r-packed-smi.js +35 -0
  17. package/TEST/r-packed-with-attr.js +40 -0
  18. package/TEST/r-prim-wrap.js +35 -0
  19. package/TEST/r-rgx.js +28 -0
  20. package/TEST/r-smi.js +28 -0
  21. package/TEST/read-bi-contents.js +20 -0
  22. package/TEST/run.js +43 -0
  23. package/TEST/run.sh +29 -0
  24. package/TEST/serde-benchmark.js +17 -0
  25. package/TEST/tst.json +114 -0
  26. package/TEST/tst.v8ser +0 -0
  27. package/TEST/w-1bstr.js +34 -0
  28. package/TEST/w-2bstr.js +34 -0
  29. package/TEST/w-ab-and-abvw.js +60 -0
  30. package/TEST/w-bi.js +33 -0
  31. package/TEST/w-date.js +33 -0
  32. package/TEST/w-double.js +35 -0
  33. package/TEST/w-int-not-smi.js +37 -0
  34. package/TEST/w-mp-st-circular.js +60 -0
  35. package/TEST/w-odd-ball.js +43 -0
  36. package/TEST/w-packed-double.js +40 -0
  37. package/TEST/w-packed-smi.js +38 -0
  38. package/TEST/w-packed-with-attr.js +41 -0
  39. package/TEST/w-prim-wrap.js +41 -0
  40. package/TEST/w-rgx.js +33 -0
  41. package/TEST/w-smi.js +36 -0
  42. package/build.sh +1 -0
  43. package/const.js +181 -0
  44. package/ctx.js +89 -0
  45. package/fixed-cfg.js +6 -0
  46. package/index.js +27 -0
  47. package/misc.js +112 -0
  48. package/package.json +22 -0
  49. package/r.js +646 -0
  50. package/restrict.js +48 -0
  51. package/w.js +510 -0
  52. package/zero-nid.js +21 -0
package/const.js ADDED
@@ -0,0 +1,181 @@
1
+ const {_u,_n,_t,_f} = require("nv-facutil-untf");
2
+
3
+
4
+ const SerializationTag = {
5
+ kVersion: 255,
6
+ kPadding: 0,
7
+ kVerifyObjectCount: 63,
8
+ kTheHole: 45,
9
+ kUndefined: 95,
10
+ kNull: 48,
11
+ kTrue: 84,
12
+ kFalse: 70,
13
+ kInt32: 73,
14
+ kUint32: 85,
15
+ kDouble: 78,
16
+ kBigInt: 90,
17
+ kUtf8String: 83,
18
+ kOneByteString: 34,
19
+ kTwoByteString: 99,
20
+ kObjectReference: 94,
21
+ kBeginJSObject: 111,
22
+ kEndJSObject: 123,
23
+ kBeginSparseJSArray: 97,
24
+ kEndSparseJSArray: 64,
25
+ kBeginDenseJSArray: 65,
26
+ kEndDenseJSArray: 36,
27
+ kDate: 68,
28
+ kTrueObject: 121,
29
+ kFalseObject: 120,
30
+ kNumberObject: 110,
31
+ kBigIntObject: 122,
32
+ kStringObject: 115,
33
+ kRegExp: 82,
34
+ kBeginJSMap: 59,
35
+ kEndJSMap: 58,
36
+ kBeginJSSet: 39,
37
+ kEndJSSet: 44,
38
+ kArrayBuffer: 66, //B 0x42
39
+ kResizableArrayBuffer: 126,
40
+ kArrayBufferTransfer: 116, // unsupport
41
+ kArrayBufferView: 86, //V 0x56 --------------- did not use , use nodejs delegate kHostObject +
42
+ kSharedArrayBuffer: 117, // unsupport
43
+ kSharedObject: 112, // unsupport
44
+ kWasmModuleTransfer: 119, // unsupport
45
+ kHostObject: 92,
46
+ kWasmMemoryTransfer: 109, //unsupport
47
+ kError: 114,
48
+ kLegacyReservedMessagePort: 77,
49
+ kLegacyReservedBlob: 98,
50
+ kLegacyReservedBlobIndex: 105,
51
+ kLegacyReservedFile: 102,
52
+ kLegacyReservedFileIndex: 101,
53
+ kLegacyReservedDOMFileSystem: 100,
54
+ kLegacyReservedFileList: 108,
55
+ kLegacyReservedFileListIndex: 76,
56
+ kLegacyReservedImageData: 35,
57
+ kLegacyReservedImageBitmap: 103,
58
+ kLegacyReservedImageBitmapTransfer: 71,
59
+ kLegacyReservedOffscreenCanvas: 72,
60
+ kLegacyReservedCryptoKey: 75,
61
+ kLegacyReservedRTCCertificate: 107
62
+ }
63
+
64
+ const V8_ArrayBufferViewTag = {
65
+ kInt8Array: 98,
66
+ kUint8Array: 66,
67
+ kUint8ClampedArray: 67,
68
+ kInt16Array: 119,
69
+ kUint16Array: 87,
70
+ kInt32Array: 100,
71
+ kUint32Array: 68,
72
+ kFloat32Array: 102,
73
+ kFloat64Array: 70,
74
+ kBigInt64Array: 113,
75
+ kBigUint64Array: 81,
76
+ kDataView: 63
77
+ }
78
+ Object.defineProperty(V8_ArrayBufferViewTag,"UNUSED_",{get:function(){return(_t)}})
79
+
80
+
81
+ const kNodeFastBufferSym = Symbol("#kNodeFastBuffer");
82
+
83
+ const ArrayBufferViewTag = {
84
+ kInt8Array : 0,
85
+ kUint8Array : 1,
86
+ kUint8ClampedArray : 2,
87
+ kInt16Array : 3,
88
+ kUint16Array : 4,
89
+ kInt32Array : 5,
90
+ kUint32Array : 6,
91
+ kFloat32Array : 7,
92
+ kFloat64Array : 8,
93
+ kDataView : 9,
94
+ kNodeFastBuffer : 10, //for compatible with node-js-fast-buffer, treated-as kUint8Array just add a
95
+ kBigInt64Array: 11,
96
+ kBigUint64Array: 12,
97
+ }
98
+
99
+ class NodeFastBuffer extends Uint8Array {
100
+ [kNodeFastBufferSym] = _t;
101
+ }
102
+
103
+ const IsNodeFastBuffer = (o)=>o[kNodeFastBufferSym] === _t;
104
+
105
+ const ObjectPrototypeToString = (o) => Object.prototype.toString.call(o);
106
+
107
+
108
+ //<copied FROM nodejs/lib>
109
+ const arrayBufferViewTypeToIndex = (abView) => {
110
+ const type = ObjectPrototypeToString(abView);
111
+ if (type === '[object Int8Array]') return 0;
112
+ if (type === '[object Uint8Array]') return 1;
113
+ if (type === '[object Uint8ClampedArray]') return 2;
114
+ if (type === '[object Int16Array]') return 3;
115
+ if (type === '[object Uint16Array]') return 4;
116
+ if (type === '[object Int32Array]') return 5;
117
+ if (type === '[object Uint32Array]') return 6;
118
+ if (type === '[object Float32Array]') return 7;
119
+ if (type === '[object Float64Array]') return 8;
120
+ if (type === '[object DataView]') return 9;
121
+ // Index 10 is FastBuffer.
122
+ if (type === '[object BigInt64Array]') return 11;
123
+ if (type === '[object BigUint64Array]') return 12;
124
+ return -1;
125
+ }
126
+ //</copied FROM nodejs/lib >
127
+
128
+ const ArrayBufferViewIndexToCtor = [
129
+ Int8Array,
130
+ Uint8Array,
131
+ Uint8ClampedArray,
132
+ Int16Array,
133
+ Uint16Array,
134
+ Int32Array,
135
+ Uint32Array,
136
+ Float32Array,
137
+ Float64Array,
138
+ DataView,
139
+ NodeFastBuffer,
140
+ BigInt64Array,
141
+ BigUint64Array
142
+ ];
143
+
144
+
145
+ const ErrorTag = {
146
+ kEvalErrorPrototype: 69,
147
+ kRangeErrorPrototype: 82,
148
+ kReferenceErrorPrototype: 70,
149
+ kSyntaxErrorPrototype: 83,
150
+ kTypeErrorPrototype: 84,
151
+ kUriErrorPrototype: 85,
152
+ kMessage: 109,
153
+ kCause: 99,
154
+ kStack: 115,
155
+ kEnd: 46
156
+ }
157
+
158
+ const kCantBeSered = "#___kCantBeSered___";
159
+ const CreatFakeCantBeSered = (id,hint)=>({id,kCantBeSered:hint});
160
+
161
+ const kUnknown = "#___kUnknown___";
162
+ const CreatUnknown = ()=>({kUnknown:_u});
163
+
164
+
165
+ const CODEC_NAME = "v8ser";
166
+
167
+ module.exports = {
168
+ CODEC_NAME, kNodeFastBufferSym,
169
+ IsNodeFastBuffer,
170
+ ////
171
+ SerializationTag,
172
+ V8_ArrayBufferViewTag, //unused
173
+ ArrayBufferViewTag, ArrayBufferViewIndexToCtor, arrayBufferViewTypeToIndex,
174
+ ErrorTag,
175
+ ////
176
+ kCantBeSered,
177
+ CreatFakeCantBeSered,
178
+ ////
179
+ kUnknown,
180
+ CreatUnknown,
181
+ }
package/ctx.js ADDED
@@ -0,0 +1,89 @@
1
+ const _A = Array;
2
+
3
+ const {_u,_n,_t,_f} = require("nv-facutil-untf");
4
+
5
+ const {creat_nid} = require("./zero-nid");
6
+
7
+ class WIdMap extends WeakMap {
8
+ #id = creat_nid();
9
+ ////
10
+ FindOrInsert(o) {
11
+ let idx = this.get(o);
12
+ if(idx === _u) {
13
+ let nidx = this.#id.next();
14
+ this.set(o,nidx);
15
+ return([_f,nidx]) // not obj-ref
16
+ } else {
17
+ return([_t,idx]) // is-obj-ref
18
+ }
19
+ }
20
+ }
21
+
22
+ const creat_widmap = () => new WIdMap();
23
+
24
+ class Wctx {
25
+ //zone = _u; //not-support
26
+ idmap = new WIdMap();
27
+ //array_buffer_transfer_map = _u; //not-support
28
+ //delegate = _u; //not-support
29
+ //shared_object_conveyor = _u; //not-support
30
+ }
31
+
32
+ const creat_wctx = ()=> new Wctx();
33
+
34
+
35
+ class RIdMap extends _A {
36
+ HasObjectWithID(id) {return(this[id]!==_u)}
37
+ GetObjectWithID(id) {return(this[id]) }
38
+ AddObjectWithID(o) {this.push(o)}
39
+ get nid_() {return(this.length)}
40
+ }
41
+
42
+ const creat_ridmap = ()=>new RIdMap();
43
+
44
+ /*
45
+ // for feature using
46
+
47
+ const ENTRY_TYPED = {
48
+ unknown:0,
49
+ ae:1,
50
+ dpair:2,
51
+ dk:3,
52
+ dp:4,
53
+ ste:5,
54
+ mpair:6,
55
+ mpk:7,
56
+ mpv:8
57
+ }
58
+
59
+ class Entry {
60
+ ref = _u;
61
+ type = ENTRY_TYPED.unknown;
62
+ pr = _u;
63
+ twin = _u;
64
+ constructor(ref,type,pr,twin) {
65
+ this.ref = ref;
66
+ this.type = type;
67
+ this.pr = pr;
68
+ this.twin = twin;
69
+ }
70
+ }
71
+ class Rctx {
72
+ idmap = new RIdMap();
73
+ stack = []
74
+ push(o,pr,type,twin) {
75
+ let e = new Entry(o,pr,type,twin);
76
+ this.stack.push(e)
77
+ }
78
+ get top_() {return(this[this.stack.length-1])}
79
+ }
80
+
81
+ const creat_rctx = ()=>new Rctx();
82
+ */
83
+
84
+ module.exports = {
85
+ creat_widmap,
86
+ creat_wctx,
87
+ creat_ridmap,
88
+ creat_rctx : creat_ridmap,
89
+ }
package/fixed-cfg.js ADDED
@@ -0,0 +1,6 @@
1
+ const {_u,_n,_t,_f} = require("nv-facutil-untf");
2
+
3
+ module.exports = {
4
+ treat_array_buffer_views_as_host_objects_ :_t, // FIXED CANT CHANGE , for compatible with nodejs-impl
5
+ suppress_deserialization_errors_ :_t, // FIXED CANT CHANGE , this pkg did NOT do any check/validate/verify
6
+ }
package/index.js ADDED
@@ -0,0 +1,27 @@
1
+ const _fixed_cfg = require("./fixed-cfg");
2
+ const _restrict = require("./restrict");
3
+ const _const = require("./const");
4
+ const _misc = require("./misc");
5
+ const _zero_nid = require("./zero-nid");
6
+ const _ctx = require("./ctx");
7
+ const _w = require("./w");
8
+ const _r = require("./r");
9
+
10
+
11
+
12
+
13
+
14
+
15
+ module.exports = {
16
+ fixed_cfg : _fixed_cfg,
17
+ restrict : _restrict,
18
+ const : _const,
19
+ misc : _misc,
20
+ zero_nid : _zero_nid, // v8 impl require ref-id from 0
21
+ ctx : _ctx,
22
+ w : _w,
23
+ r : _r,
24
+ ////----
25
+ ser: _w.encd, // (o:JSValue,kLatestVersion=15)-> ArrayBuffer
26
+ der: _r.decd, // (ab:ArrayBuffer)->JSValue
27
+ }
package/misc.js ADDED
@@ -0,0 +1,112 @@
1
+
2
+ const _S = String;
3
+ const _is = Object.is;
4
+ const {istof,is_num} = require("nv-facutil-typis");
5
+ const {_u,_n,_t,_f} = require("nv-facutil-untf");
6
+ const _zigzag = require("nv-number-zigzag");
7
+ const _utf16 = require("nv-buf-jstr");
8
+
9
+ const BytesNeededForVarint = (value) => {
10
+ let result = 0;
11
+ do {
12
+ result++;
13
+ value >>= 7;
14
+ } while(value);
15
+ return(result);
16
+ }
17
+
18
+ const IsOneByte = (s) => {
19
+ for(let i=0;i<s.length;++i) {
20
+ if(s[i].charCodeAt(0) <256) {
21
+ } else {
22
+ return(_f)
23
+ }
24
+ }
25
+ return(_t)
26
+ }
27
+
28
+ const IsTwoByte = (s) => !IsOneByte(s);
29
+
30
+ const IsNaNAt = (u8a,si) => {
31
+ let dv = new DataView(u8a.buffer);
32
+ let r = dv.getFloat64(si,_t);
33
+ return(_is(r,NaN))
34
+ }
35
+
36
+
37
+ const kFastElementsKindSequence = {
38
+ PACKED_SMI_ELEMENTS:0,
39
+ HOLEY_SMI_ELEMENTS:1,
40
+ PACKED_DOUBLE_ELEMENTS:2,
41
+ HOLEY_DOUBLE_ELEMENTS:3,
42
+ PACKED_ELEMENTS:4,
43
+ HOLEY_ELEMENTS:5,
44
+ }
45
+
46
+ const GetArrayForEachLength = (arr) => {
47
+ let cnt = 0;
48
+ arr.forEach(r=> {++cnt;});
49
+ return(cnt)
50
+ }
51
+ const IsArrayHasHole = (arr)=> GetArrayForOfLength(arr)!== arr.length;
52
+
53
+ const IsSmi = (n) => _zigzag.is_smi(n) && Number.isInteger(n) && (!Object.is(n,-0));
54
+
55
+ const GetArrayPackedInfo = (arr)=> {
56
+ let is_packed_smi = _t;
57
+ let is_packed_double = _t;
58
+ let i=0;
59
+ while(i<arr.length){
60
+ if(is_packed_smi && IsSmi(arr[i])) {
61
+
62
+ } else if(is_num(arr[i])) {
63
+ is_packed_smi = _f;
64
+ } else {
65
+ is_packed_smi = _f;
66
+ is_packed_double = _f;
67
+ break;
68
+ }
69
+ ++i;
70
+ }
71
+ return({
72
+ is_packed_smi,
73
+ is_packed_double
74
+ })
75
+ }
76
+
77
+ const UTF8_DECODER = new TextDecoder();
78
+
79
+ const Utf8U8aToStr = (u8a,si,sz,decd) => {
80
+ let nu8a = new Uint8Array(u8a.buffer,si,sz);
81
+ if(decd) {
82
+ return(decd(nu8a))
83
+ } else {
84
+ return(UTF8_DECODER.decode(nu8a))
85
+ }
86
+ }
87
+
88
+ const LATIN1_DECODER = new TextDecoder("latin1");
89
+ const Latin1U8aToStr = (u8a,si,sz) => LATIN1_DECODER.decode(u8a.subarray(si,si+sz));
90
+
91
+
92
+ const UTF16_DECODER=new TextDecoder("utf-16");
93
+ const JstrU8aToStr = (u8a,si,sz) => UTF16_DECODER.decode(u8a.subarray(si,si+sz));
94
+
95
+
96
+
97
+ module.exports = {
98
+ BytesNeededForVarint,
99
+ IsOneByte,
100
+ IsTwoByte,
101
+ IsNaNAt,
102
+ IsSmi,
103
+ ////
104
+ kFastElementsKindSequence,
105
+ GetArrayForEachLength,
106
+ IsArrayHasHole,
107
+ GetArrayPackedInfo,
108
+ ////
109
+ Utf8U8aToStr,
110
+ Latin1U8aToStr,
111
+ JstrU8aToStr
112
+ }
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "dependencies": {
3
+ "nv-buf-bi": "^1.0.5",
4
+ "nv-buf-jstr": "^1.0.8",
5
+ "nv-facutil-ab": "^1.0.0",
6
+ "nv-facutil-istis": "^1.0.6",
7
+ "nv-facutil-ppgflike-is": "^1.0.1",
8
+ "nv-facutil-thrw": "^1.0.2",
9
+ "nv-number-zigzag": "^1.0.1",
10
+ "nv-regexp-flags": "^1.0.1"
11
+ },
12
+ "version": "0.0.2",
13
+ "name": "nv-buf-serde",
14
+ "description": "nv-buf-serde ======================= - rewrite v8/src/objects/value-serializer.cc to JS - to let you use nodejs'v8.serialize AND nodejs'v8.deserialize in browser, - similiar to structuredClone AND MessageChannel-transfer, but can be used to send/recv data to server-side",
15
+ "main": "index.js",
16
+ "devDependencies": {},
17
+ "scripts": {
18
+ "test": "echo \"Error: no test specified\" && exit 1"
19
+ },
20
+ "author": "",
21
+ "license": "ISC"
22
+ }