nv-buf-storage 1.3.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.
package/mthds/cmmn.js ADDED
@@ -0,0 +1,576 @@
1
+ const _TD = new TextDecoder();
2
+ const _TE = new TextEncoder();
3
+ const {tacls_or_abcls_to_mthd_hint,taclsnm_to_tbrief} = require("nv-facutil-slct-ta");
4
+ const _v8serde = require("nv-buf-serde");
5
+ const { creat_sz_buf_for_dump,_COND_IS_NODE_ENV } = require("../util");
6
+
7
+
8
+ function _calc_datasz_for_dump(compress=true) {
9
+ var datasz;
10
+ if(compress !== true) {
11
+ datasz = this._data.byteLength;
12
+ } else {
13
+ var [si,ei] = this._fl.lst_free_rng;
14
+ if(ei === this._fl.max_alocable_unit_cnt_v) {
15
+ datasz = si;
16
+ } else {
17
+ datasz= this._data.byteLength;
18
+ }
19
+ }
20
+ return datasz;
21
+ }
22
+
23
+ async function seperated_dump (dst_fn,compress=true) {
24
+ var [cond,fl_u8avw] = await this._fl.dump(undefined,compress);
25
+ if(cond) {
26
+ var datasz = this._calc_datasz_for_dump(compress);
27
+ var data_u8avw = this._data.subarray(0,datasz);
28
+ if(_COND_IS_NODE_ENV && typeof(dst_fn)=== "string") {
29
+ const _path = require("path");const _fs = require("fs");dst_fn = _path.resolve(dst_fn);const _wfile = require("nv-file-write2").simple;
30
+ var fl_dst_fn = dst_fn+".fl";
31
+ var data_dst_fn = dst_fn+".data";
32
+ var p0 = _wfile(fl_dst_fn,fl_u8avw,Infinity,0,{_fs,_path});
33
+ var p1 = _wfile(data_dst_fn,data_u8avw,Infinity,0,{_fs,_path});
34
+ await Promise.all([p0,p1]);
35
+ var pair0 = await p0;
36
+ var pair1 = await p1;
37
+ if(pair0[0] && pair1[0]) {
38
+ return [true,{fl:pair0[1],data:pair1[1]}];
39
+ } else {
40
+ return [false,{fl:pair0[1],data:pair1[1]}];
41
+ }
42
+ } else {
43
+ return [true,{fl:fl_u8avw,data:data_u8avw}];
44
+ }
45
+ } else {
46
+ return [false,fl_u8avw]
47
+ }
48
+ }
49
+
50
+
51
+ async function dump (dst_fn,compress=true) {
52
+ var [cond,fl_u8avw] = await this._fl.dump(undefined,compress);
53
+ if(cond) {
54
+ var flsz = fl_u8avw.byteLength;
55
+ var datasz = this._calc_datasz_for_dump(compress);
56
+ var data_u8avw = this._data.subarray(0,datasz);
57
+ if(_COND_IS_NODE_ENV && typeof(dst_fn)=== "string") {
58
+ const _path = require("path");const _fs = require("fs");dst_fn = _path.resolve(dst_fn);const _wfile = require("nv-file-write2").simple_many;
59
+ var flszu8a = creat_sz_buf_for_dump(flsz);
60
+ var dataszu8a = creat_sz_buf_for_dump(datasz);
61
+ return await _wfile(dst_fn,[flszu8a,fl_u8avw,dataszu8a,data_u8avw],65536,0,{_fs,_path});
62
+ } else {
63
+ var sz = 8 + flsz + 8 + datasz;
64
+ var u8a = new Uint8Array(sz);
65
+ var dv = new DataView(u8a.buffer);
66
+ dv.setFloat64(0,flsz,true);
67
+ u8a.set(fl_u8avw,8);
68
+ dv.setFloat64(8 + flsz,datasz,true);
69
+ u8a.set(data_u8avw , 8+ flsz+8);
70
+ return [true,u8a];
71
+ }
72
+ } else {
73
+ return [false,fl_u8avw]
74
+ }
75
+ }
76
+ dump.HELP = ["flsz(f64le)","fl(u8a)","datasz(f64le)","data(u8a)"];
77
+
78
+ function dloc([si,ei]) {
79
+ return this._fl.dloc(si,ei);
80
+ }
81
+ function rstr([si,ei]) {return _TD.decode(this._data.subarray(si,ei))}
82
+ function wstr(s) {
83
+ var mx_sz = s.length*3;
84
+ var [si,ei] = this.aloc(mx_sz);
85
+ if(ei!== 0) {
86
+ var {__,written} = _TE.encodeInto(s,this._data.subarray(si,ei));
87
+ this._fl.dloc(si+written,ei);
88
+ return [si,si+written]
89
+ } else {
90
+ return [0,0]
91
+ }
92
+ }
93
+ function rjson([si,ei]) {return JSON.parse(this.rstr([si,ei]));}
94
+ function wjson(j) {return this.wstr(JSON.stringify(j) );}
95
+
96
+
97
+ function wta(ta,include_ab=true) {
98
+ var hint = tacls_or_abcls_to_mthd_hint(ta.constructor);
99
+ if(hint["?"]) {
100
+ return this?.[hint.w]?.(ta);
101
+ } else {
102
+ return this[hint.w](ta);
103
+ }
104
+ }
105
+
106
+ function rta(Cls,rng) {
107
+ var hint = tacls_or_abcls_to_mthd_hint(Cls);
108
+ if(hint["?"]) {
109
+ return this?.[hint.r]?.(rng);
110
+ } else {
111
+ var ta= this[hint.r](rng);
112
+ if(Cls === globalThis.Buffer){
113
+ return globalThis.Buffer.from(ta);
114
+ } else {
115
+ return ta;
116
+ }
117
+ }
118
+ }
119
+
120
+
121
+ function wo(o) {
122
+ var ab = _v8serde.ser(o);
123
+ var u8a = new Uint8Array(ab);
124
+ var sz = u8a.byteLength;
125
+ var [si,ei] = this.aloc(sz);
126
+ if(ei!== 0) {
127
+ this._data.set(u8a,si);
128
+ return [si,ei]
129
+ } else {
130
+ return [0,0]
131
+ }
132
+ }
133
+ function ro([si,ei]) {
134
+ var u8a = this._data.subarray(si,ei);
135
+ return _v8serde.der(u8a);
136
+ }
137
+
138
+ // undefined null true false 在type 里 可以表示 不需要内存
139
+
140
+
141
+ //number-like
142
+ function ru8([si,ei]) {return this._dv.getUint8(si,true);}
143
+ function ru16([si,ei]) {return this._dv.getUint16(si,true);}
144
+ function ru32([si,ei]) {return this._dv.getUint32(si,true);}
145
+ function ru64([si,ei]) {return this._dv.getBigUint64(si,true);}
146
+ function ri8([si,ei]) {return this._dv.getInt8(si,true);}
147
+ function ri16([si,ei]) {return this._dv.getInt16(si,true);}
148
+ function ri32([si,ei]) {return this._dv.getInt32(si,true);}
149
+ function ri64([si,ei]) {return this._dv.getBigInt64(si,true) ;}
150
+ function rf32([si,ei]) {return this._dv.getFloat32(si,true);}
151
+ function rf64([si,ei]) {return this._dv.getFloat64(si,true);}
152
+
153
+ function wu8(num) {
154
+ var [si,ei]= this.aloc(1);
155
+ if(ei!== 0) {
156
+ this._dv.setUint8(si,num,true);
157
+ return [si,ei];
158
+ } else {
159
+ return [0,0]
160
+ }
161
+ }
162
+
163
+
164
+ function wu16(num,algn=false) {
165
+ var si=0;ei=0;
166
+ if(algn) {
167
+ [si,ei]= this.algn_aloc(2,2);
168
+ } else {
169
+ [si,ei]= this.aloc(2);
170
+ }
171
+ if(ei!== 0) {
172
+ this._dv.setUint16(si,num,true);
173
+ return [si,ei];
174
+ } else {
175
+ return [0,0]
176
+ }
177
+
178
+ }
179
+ function wu32(num,algn=false) {
180
+ var si=0;ei=0;
181
+ if(algn) {
182
+ [si,ei]= this.algn_aloc(4,4);
183
+ } else {
184
+ [si,ei]= this.aloc(4);
185
+ }
186
+ if(ei!== 0) {
187
+ this._dv.setUint32(si,num,true);
188
+ return [si,ei];
189
+ } else {
190
+ return [0,0]
191
+ }
192
+ }
193
+ function wu64(bi,algn=false) {
194
+ var si=0;ei=0;
195
+ if(algn) {
196
+ [si,ei]= this.algn_aloc(8,8);
197
+ } else {
198
+ [si,ei]= this.aloc(8);
199
+ }
200
+ if(ei!== 0) {
201
+ this._dv.setBigUint64(si,BigInt(bi),true);
202
+ return [si,ei];
203
+ } else {
204
+ return [0,0]
205
+ }
206
+
207
+ }
208
+ function wi8(num) {
209
+ var [si,ei] = this.aloc(1);
210
+ if(ei !== 0) {
211
+ this._dv.setInt8(si,num,true);
212
+ return [si,ei];
213
+ } else {
214
+ return [0,0];
215
+ }
216
+ }
217
+
218
+ function wi16(num,algn=false) {
219
+ var si=0,ei=0;
220
+ if(algn) {
221
+ [si,ei] = this.algn_aloc(2,2);
222
+ } else {
223
+ [si,ei] = this.aloc(2);
224
+ }
225
+ if(ei !== 0) {
226
+ this._dv.setInt16(si,num,true);
227
+ return [si,ei];
228
+ } else {
229
+ return [0,0];
230
+ }
231
+ }
232
+
233
+ function wi32(num,algn=false) {
234
+ var si=0,ei=0;
235
+ if(algn) {
236
+ [si,ei] = this.algn_aloc(4,4);
237
+ } else {
238
+ [si,ei] = this.aloc(4);
239
+ }
240
+ if(ei !== 0) {
241
+ this._dv.setInt32(si,num,true);
242
+ return [si,ei];
243
+ } else {
244
+ return [0,0];
245
+ }
246
+ }
247
+
248
+ function wi64(bi,algn=false) {
249
+ var si=0;ei=0;
250
+ if(algn) {
251
+ [si,ei]= this.algn_aloc(8,8);
252
+ } else {
253
+ [si,ei]= this.aloc(8);
254
+ }
255
+ if(ei!== 0) {
256
+ this._dv.setBigInt64(si,BigInt(bi),true);
257
+ return [si,ei];
258
+ } else {
259
+ return [0,0]
260
+ }
261
+
262
+ }
263
+ function wf32(num,algn=false) {
264
+ var si=0;ei=0;
265
+ if(algn) {
266
+ [si,ei]= this.algn_aloc(4,4);
267
+ } else {
268
+ [si,ei]= this.aloc(4);
269
+ }
270
+ if(ei!== 0) {
271
+ this._dv.setFloat32(si,num,true);
272
+ return [si,ei];
273
+ } else {
274
+ return [0,0]
275
+ }
276
+ }
277
+
278
+ function wf64(num,algn=false) {
279
+ var si=0;ei=0;
280
+ if(algn) {
281
+ [si,ei]= this.algn_aloc(8,8);
282
+ } else {
283
+ [si,ei]= this.aloc(8);
284
+ }
285
+ if(ei!== 0) {
286
+ this._dv.setFloat64(si,num,true);
287
+ return [si,ei];
288
+ } else {
289
+ return [0,0]
290
+ }
291
+
292
+ }
293
+
294
+ ////typed-array
295
+ function ru8a([si,ei]) {return new Uint8Array (this._data.buffer,this._data.byteOffset+si, ei-si) ;}
296
+ function ru8ca([si,ei]) {return new Uint8ClampedArray (this._data.buffer,this._data.byteOffset+si, ei-si) ;}
297
+ function ru16a([si,ei]) {return new Uint16Array (this._data.buffer,this._data.byteOffset+si,(ei-si)/2);}
298
+ function ru32a([si,ei]) {return new Uint32Array (this._data.buffer,this._data.byteOffset+si,(ei-si)/4);}
299
+ function ru64a([si,ei]) {return new BigUint64Array (this._data.buffer,this._data.byteOffset+si,(ei-si)/8);}
300
+ function ri8a([si,ei]) {return new Int8Array (this._data.buffer,this._data.byteOffset+si, ei-si) ;}
301
+ function ri16a([si,ei]) {return new Int16Array (this._data.buffer,this._data.byteOffset+si,(ei-si)/2);}
302
+ function ri32a([si,ei]) {return new Int32Array (this._data.buffer,this._data.byteOffset+si,(ei-si)/4);}
303
+ function ri64a([si,ei]) {return new BigInt64Array (this._data.buffer,this._data.byteOffset+si,(ei-si)/8);}
304
+ function rf32a([si,ei]) {return new globalThis.Float32Array(this._data.buffer,this._data.byteOffset+si,(ei-si)/4);}
305
+ function rf64a([si,ei]) {return new globalThis.Float64Array(this._data.buffer,this._data.byteOffset+si,(ei-si)/8);}
306
+ function rdv([si,ei]) {return new DataView (this._data.buffer,this._data.byteOffset+si, ei-si) ;}
307
+ function _write_no_need_algn_typed_array(ta1b) {
308
+ var [si,ei] = this.aloc(ta1b.byteLength);
309
+ if(ei !== 0) {
310
+ var src_u8a = new Uint8Array(ta1b.buffer,ta1b.byteOffset,ta1b.byteLength)
311
+ this._data.set(src_u8a,si);
312
+ return [si,ei];
313
+ } else {
314
+ return [0,0];
315
+ }
316
+ }
317
+ function _write_need_algn_typed_array(ta) {
318
+ var [si,ei]= this.algn_aloc(ta.byteLength,ta.constructor.BYTES_PER_ELEMENT);
319
+ if(ei!== 0) {
320
+ var src_u8a = new Uint8Array(ta.buffer,ta.byteOffset,ta.byteLength);
321
+ this._data.set(src_u8a,si);
322
+ return [si,ei];
323
+ } else {
324
+ return [0,0]
325
+ }
326
+ }
327
+
328
+ //----仅测试,此接口速度很慢,应该显式调用 this.rxxx([si,ei])
329
+
330
+ const DEBUG_TBRIEF_TO_MTHD_HINT = {
331
+ "s": { r: "rstr", w: "wstr", "?": false },
332
+ "json": { r: "rjson", w: "wjson", "?": false },
333
+ "f16": { r: "rf16", w: "wf16", "?": true },
334
+ "f32": { r: "rf32", w: "wf32", "?": false },
335
+ "f64": { r: "rf64", w: "wf64", "?": false },
336
+ "u8": { r: "ru8", w: "wu8", "?": false },
337
+ "u16": { r: "ru16", w: "wu16", "?": false },
338
+ "u32": { r: "ru32", w: "wu32", "?": false },
339
+ "u64": { r: "ru64", w: "wu64", "?": false },
340
+ "i8": { r: "ri8", w: "wi8", "?": false },
341
+ "i16": { r: "ri16", w: "wi16", "?": false },
342
+ "i32": { r: "ri32", w: "wi32", "?": false },
343
+ "i64": { r: "ri64", w: "wi64", "?": false },
344
+ "u8a": { r: "ru8a", w: "wu8a", "?": false },
345
+ "u8ca": { r: "ru8ca", w: "wu8ca", "?": false },
346
+ "u16a": { r: "ru16a", w: "wu16a", "?": false },
347
+ "u32a": { r: "ru32a", w: "wu32a", "?": false },
348
+ "u64a": { r: "ru64a", w: "wu64a", "?": false },
349
+ "i8a": { r: "ri8a", w: "wi8a", "?": false },
350
+ "i16a": { r: "ri16a", w: "wi16a", "?": false },
351
+ "i32a": { r: "ri32a", w: "wi32a", "?": false },
352
+ "i64a": { r: "ri64a", w: "wi64a", "?": false },
353
+ "f16a": { r: "rf16a", w: "wf16a", "?": true },
354
+ "f32a": { r: "rf32a", w: "wf32a", "?": false },
355
+ "f64a": { r: "rf64a", w: "wf64a", "?": false },
356
+ "dv": { r: "rdv", w: "wdv", "?": false } ,
357
+ ////
358
+ "u": {v:undefined},
359
+ "n": {v:null},
360
+ "t": {v:true},
361
+ "f": {v:false},
362
+ "ly_undef" : {v:Symbol()},
363
+ ////
364
+ "gy":{f:(self,si,ei)=>Symbol.for(self.rstr([si,ei]))},
365
+ "ly":{f:(self,si,ei)=>Symbol(self.rstr([si,ei]))},
366
+ ////
367
+ "flike":{unsupported:true},
368
+ "wkst":{unsupported:true},
369
+ "wkmp":{unsupported:true},
370
+ "wkref":{unsupported:true},
371
+ "prms":{unsupported:true},
372
+ "iter":{unsupported:true},
373
+ "sg":{unsupported:true},
374
+ "ag":{unsupported:true},
375
+ "unsupported":{unsupported:true},
376
+ ////
377
+ };
378
+
379
+ function debug_read(T,[si,ei]) {
380
+ var instruction = DEBUG_TBRIEF_TO_MTHD_HINT[T];
381
+ if(instruction?.r!== undefined) {
382
+ if(instruction["?"]) {
383
+ return this?.[instruction.r]?.([si,ei]);
384
+ } else {
385
+ return this[instruction.r]([si,ei]);
386
+ }
387
+ } else if(instruction?.hasOwnProperty?.("v")) {
388
+ return instruction.v;
389
+ } else if(instruction?.hasOwnProperty?.("f")) {
390
+ return instruction.f(this,si,ei);
391
+ } else if(instruction?.hasOwnProperty?.unsupported) {
392
+ throw("unsupported");
393
+ } else if(ei === 0) {
394
+ throw("not_exists");
395
+ } else {
396
+ return this.ro([si,ei]);
397
+ }
398
+ }
399
+
400
+
401
+
402
+ ////----仅测试,此接口速度很慢,应该显式调用 this.wxxx(o);
403
+ function debug_wnum(o) {
404
+ if(Number.isInteger(o)) {
405
+ if(Object.is(o,-0)) { return ["i8", this.wi8(o)];}
406
+ if(o>=0) {
407
+ if(o<256) { return ["u8", this.wu8(o)];
408
+ } else if(o<65536) { return ["u16",this.wu16(o)];
409
+ } else if(o<2**32) { return ["u32",this.wu32(o)];
410
+ } else {
411
+ return ["f64",this.wf64(o)];
412
+ }
413
+ } else {
414
+ if(o>=-128) { return ["i8", this.wi8(o)];
415
+ } else if(o>=-32768) { return ["i16", this.wi16(o)];
416
+ } else if(o>=-(2**31)) { return ["i16", this.wi32(o)];
417
+ } else {
418
+ return ["f64",this.wf64(o)];
419
+ }
420
+ }
421
+ } else {
422
+ return ["f64",this.wf64(o)];
423
+ }
424
+ }
425
+
426
+ function debug_wbi(o) {
427
+ if(o>=0n && o< 2n**64n) {
428
+ return ["u64",this.wu64(o)];
429
+ } else if(o>= -(2n**63n) && o<0n) {
430
+ return ["i64",this.wi64(o)];
431
+ } else {
432
+ return ["bi",this.wo(o)];
433
+ }
434
+ }
435
+
436
+ function debug_wsym(o) {
437
+ var key_for = Symbol.keyFor(o);
438
+ if(key_for !== undefined) {
439
+ return ["gy",this.wstr(o.description)];
440
+ } else {
441
+ if(o.description !== undefined) {
442
+ return ["ly",this.wstr(o.description)];
443
+ } else {
444
+ return ["ly_undef",[0,0]];
445
+ }
446
+ }
447
+ }
448
+
449
+ const V8SERIALABLE_NONTA_OBJ_CLSNMS = [
450
+ "ArrayBuffer" ,
451
+ "SharedArrayBuffer" ,
452
+ "Map" ,
453
+ "Set" ,
454
+ "Date" ,
455
+ "RegExp" ,
456
+ "String" ,
457
+ "Number" ,
458
+ "Boolean" ,
459
+ "Error" ,
460
+ "AggregateError" ,
461
+ "EvalError" ,
462
+ "RangeError" ,
463
+ "ReferenceError" ,
464
+ "URIError" ,
465
+ "TypeError" ,
466
+ "SyntaxError" ,
467
+ ];
468
+
469
+ function debug_write(o) {
470
+ if(o === undefined) {
471
+ return ['u',[0,0]];
472
+ } else if(o === null) {
473
+ return ['n',[0,0]];
474
+ } else if(o === true) {
475
+ return ['t',[0,0]];
476
+ } else if(o === false) {
477
+ return ['f',[0,0]];
478
+ } else {
479
+ var tnm = typeof(o);
480
+ if(tnm === "string") {
481
+ return ["s",this.wstr(o)];
482
+ } else if(tnm === "number") {
483
+ return this.debug_wnum(o);
484
+ } else if(tnm === "bigint") {
485
+ return this.debug_wbi(o);
486
+ } else if(tnm === "symbol") {
487
+ return this.debug_wsym(o);
488
+ } else if(tnm === "function") { //unsupported
489
+ return ["flike",[0,0]];
490
+ } else if(tnm === "object") {
491
+ var clsnm = o.constructor.name;
492
+ var tbrief = taclsnm_to_tbrief(clsnm);
493
+ if(tbrief !== undefined) {
494
+ var hint = DEBUG_TBRIEF_TO_MTHD_HINT[tbrief];
495
+ if(hint["?"]) {
496
+ return [tbrief,this?.[hint.w]?.(o)];
497
+ } else {
498
+ return [tbrief,this[hint.w](o)];
499
+ }
500
+ } else if(V8SERIALABLE_NONTA_OBJ_CLSNMS.includes(clsnm)) {
501
+ return ["other",this.wo(o)]
502
+ } else if(
503
+ clsnm === "WeakSet" ||
504
+ clsnm === "WeakMap" ||
505
+ clsnm === "Promise" ||
506
+ clsnm === "Iterator" ||
507
+ clsnm === "WeakRef" ||
508
+ o[Symbol.toStringTag] === "Generator" ||
509
+ o[Symbol.toStringTag] === "AyncGenerator"
510
+ ) {
511
+ return ["unsupported",[0,0]]
512
+ } else {
513
+ return ["other",this.wo(o)]
514
+ }
515
+ } else {
516
+ return ["other",this.wo(o)]
517
+ }
518
+ }
519
+ }
520
+
521
+ module.exports = {
522
+ _calc_datasz_for_dump,
523
+ seperated_dump, dump,
524
+ dloc,
525
+ ////
526
+ rstr,wstr,rjson,wjson,rta,wta,ro,wo,
527
+ ////
528
+ ru8,ru16,ru32,ru64,
529
+ ri8,ri16,ri32,ri64,
530
+ wu8,wu16,wu32,wu64,
531
+ wi8,wi16,wi32,wi64,
532
+ rf32,rf64,
533
+ wf32,wf64,
534
+ ////
535
+ ru8a,ru8ca,ru16a,ru32a,ru64a,
536
+ ri8a, ri16a,ri32a,ri64a,
537
+ rf32a,rf64a,
538
+ rdv,
539
+ wu8a:_write_no_need_algn_typed_array,
540
+ wu8ca:_write_no_need_algn_typed_array,
541
+ wi8a:_write_no_need_algn_typed_array,
542
+ wdv:_write_no_need_algn_typed_array,
543
+ wu16a:_write_need_algn_typed_array,
544
+ wu32a:_write_need_algn_typed_array,
545
+ wu64a:_write_need_algn_typed_array,
546
+ wi16a:_write_need_algn_typed_array,
547
+ wi32a:_write_need_algn_typed_array,
548
+ wi64a:_write_need_algn_typed_array,
549
+ wf32a:_write_need_algn_typed_array,
550
+ wf64a:_write_need_algn_typed_array,
551
+ ////
552
+ debug_read,
553
+ debug_wnum,debug_wbi,debug_wsym,
554
+ debug_write,
555
+ }
556
+
557
+ if(DataView.prototype.getFloat16 !== undefined) {
558
+ module.exports.rf16 = function([si,ei]) {return this._dv.getFloat16(si,true);}
559
+ module.exports.wf16 = function(num,algn=false) {
560
+ var si=0;ei=0;
561
+ if(algn) {
562
+ [si,ei]= this.algn_aloc(2,2);
563
+ } else {
564
+ [si,ei]= this.aloc(2);
565
+ }
566
+ if(ei!== 0) {
567
+ this._dv.setFloat16(si,num,true);
568
+ return [si,ei];
569
+ } else {
570
+ return [0,0]
571
+ }
572
+ }
573
+ module.exports.rf16a = function([si,ei]) {return new globalThis.Float16Array(this._data.buffer,this._data.byteOffset+si,(ei-si)/2);}
574
+ module.exports.wf16a = _write_need_algn_typed_array;
575
+ }
576
+
@@ -0,0 +1,9 @@
1
+
2
+ function aloc(sz) { return this._fl.aloc(sz);}
3
+
4
+ function algn_aloc(sz,algn) { return this._fl.algn_aloc(sz,algn);}
5
+
6
+ module.exports = {
7
+ aloc,
8
+ algn_aloc
9
+ }
@@ -0,0 +1,36 @@
1
+ const _resize_data = require("nv-buf-resize-util");
2
+
3
+ function aloc(sz) {
4
+ var [si,ei] = this._fl.aloc(sz);
5
+ if(ei!==0) { } else {
6
+ var [cond,incr_unit_cnt] = this._fl.expand(this._fl.max_alocable_unit_cnt_v + sz);
7
+ if(cond) {
8
+ this._data = _resize_data.expand(this._data,incr_unit_cnt);
9
+ this._dv = new DataView(this._data.buffer,this._data.byteOffset,this._data.byteLength);
10
+ [si,ei] = this._fl.aloc(sz);
11
+ } else {
12
+ return [0,0]
13
+ }
14
+ }
15
+ return [si,ei]
16
+ }
17
+
18
+ function algn_aloc(sz,algn) {
19
+ var [si,ei] = this._fl.algn_aloc(sz,algn);
20
+ if(ei!==0) { } else {
21
+ var [cond,incr_unit_cnt] = this._fl.expand(this._fl.max_alocable_unit_cnt_v + sz+algn);
22
+ if(cond) {
23
+ this._data = _resize_data.expand(this._data,incr_unit_cnt);
24
+ this._dv = new DataView(this._data.buffer,this._data.byteOffset,this._data.byteLength);
25
+ [si,ei] = this._fl.algn_aloc(sz,algn);
26
+ } else {
27
+ return [0,0]
28
+ }
29
+ }
30
+ return [si,ei]
31
+ }
32
+
33
+ module.exports = {
34
+ aloc,
35
+ algn_aloc
36
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "nv-buf-storage",
3
+ "version": "1.3.2",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "author": "",
9
+ "license": "ISC",
10
+ "description": "",
11
+ "dependencies": {
12
+ "nv-buf-free-list2": "^1.2.1",
13
+ "nv-buf-resize-util": "^1.0.0",
14
+ "nv-buf-serde": "^1.0.0",
15
+ "nv-facutil-slct-ta": "^1.0.10",
16
+ "nv-file-read2": "^1.0.2",
17
+ "nv-file-write2": "^1.0.11"
18
+ },
19
+ "devDependencies": {
20
+ "nv-random-bufvw": "^1.0.4",
21
+ "nv-random-date": "^1.0.1",
22
+ "nv-random-rgx": "^1.0.0",
23
+ "nv-random-sml-json": "^1.0.2"
24
+ }
25
+ }
package/util.js ADDED
@@ -0,0 +1,63 @@
1
+ const {get_cls_from_dump_fst_byte} = require("nv-facutil-slct-ta");
2
+
3
+ const creat_sz_buf_for_dump = (sz)=>{
4
+ var f64a = new Float64Array(1);
5
+ f64a[0] = sz;
6
+ return new Uint8Array(f64a.buffer);
7
+ }
8
+ const read_sz_from_sz_buf = (szbuf) => {
9
+ var f64a = new Float64Array(szbuf.buffer);
10
+ return f64a[0];
11
+ }
12
+
13
+ //从文件si 开始读取 sz 内容
14
+ const sync_read_segment_from_file = (src,si,sz,_fs)=>{
15
+ var buf = new Uint8Array(sz);
16
+ const fd = _fs.openSync(src, 'r');
17
+ _fs.readSync(fd, buf, 0, sz, si); // fs.readSync(fd, buffer, 0, 32, 0); // fd, buffer, bufferOffset, length, position
18
+ _fs.closeSync(fd);
19
+ return buf;
20
+ }
21
+
22
+ const PRE_LOAD_BYTSZ = 8 + 8*9 +8;
23
+ const read_preload_info = (src,_fs)=>{
24
+ var buf;
25
+ if(typeof(src)==="string") {
26
+ buf = sync_read_segment_from_file(src,0,PRE_LOAD_BYTSZ,_fs);
27
+ } else {
28
+ buf = src;
29
+ }
30
+ var dv = new DataView(buf.buffer);
31
+ var flsz = dv.getFloat64(0,true);
32
+ ////
33
+ buf = buf.subarray(8);
34
+ var TaCls = get_cls_from_dump_fst_byte(buf[0]);
35
+ var preload = new TaCls(buf.buffer,8,9);
36
+ return {
37
+ flsz,
38
+ max_alocable_unit_cnt_v: preload[5],
39
+ }
40
+ }
41
+
42
+ const read_brief_info_from_dump = (src,_fs)=>{
43
+ var {flsz,max_alocable_unit_cnt_v} = read_preload_info(src,_fs);
44
+ var dataszu8a = sync_read_segment_from_file(src,8+flsz,8,_fs);
45
+ var datasz = (new Float64Array(dataszu8a.buffer))[0];
46
+ return {
47
+ flsz,
48
+ max_alocable_unit_cnt_v,
49
+ datasz,
50
+ is_data_compressed: (datasz < max_alocable_unit_cnt_v),
51
+ }
52
+ }
53
+ const _COND_IS_NODE_ENV = typeof globalThis.process === "object" && typeof require === "function";
54
+
55
+ module.exports = {
56
+ PRE_LOAD_BYTSZ,
57
+ creat_sz_buf_for_dump,
58
+ read_sz_from_sz_buf,
59
+ sync_read_segment_from_file,
60
+ read_preload_info,
61
+ read_brief_info_from_dump,
62
+ _COND_IS_NODE_ENV,
63
+ }