@hiraokahypertools/pst-extractor 0.1.0-alpha.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.
Files changed (90) hide show
  1. package/dist/BTHeap.d.ts +15 -0
  2. package/dist/BTHeap.js +76 -0
  3. package/dist/CollectionAsyncProvider.d.ts +15 -0
  4. package/dist/CollectionAsyncProvider.js +44 -0
  5. package/dist/KeyedDelay.d.ts +8 -0
  6. package/dist/KeyedDelay.js +37 -0
  7. package/dist/LZFu.class.d.ts +12 -0
  8. package/dist/LZFu.class.js +95 -0
  9. package/dist/NodeMap.class.d.ts +35 -0
  10. package/dist/NodeMap.class.js +86 -0
  11. package/dist/OutlookProperties.d.ts +278 -0
  12. package/dist/OutlookProperties.js +284 -0
  13. package/dist/PAUtil.d.ts +17 -0
  14. package/dist/PAUtil.js +145 -0
  15. package/dist/PHNodeHeap.d.ts +16 -0
  16. package/dist/PHNodeHeap.js +2 -0
  17. package/dist/PHNodeHeapReader.d.ts +6 -0
  18. package/dist/PHNodeHeapReader.js +5 -0
  19. package/dist/PHUtil.d.ts +6 -0
  20. package/dist/PHUtil.js +103 -0
  21. package/dist/PLMisc.d.ts +8 -0
  22. package/dist/PLMisc.js +16 -0
  23. package/dist/PLNode.d.ts +11 -0
  24. package/dist/PLNode.js +5 -0
  25. package/dist/PLStore.d.ts +9 -0
  26. package/dist/PLStore.js +5 -0
  27. package/dist/PLSubNode.d.ts +8 -0
  28. package/dist/PLSubNode.js +5 -0
  29. package/dist/PLUtil.d.ts +29 -0
  30. package/dist/PLUtil.js +683 -0
  31. package/dist/PSTActivity.class.d.ts +103 -0
  32. package/dist/PSTActivity.class.js +144 -0
  33. package/dist/PSTAppointment.class.d.ts +275 -0
  34. package/dist/PSTAppointment.class.js +381 -0
  35. package/dist/PSTAttachment.class.d.ts +168 -0
  36. package/dist/PSTAttachment.class.js +286 -0
  37. package/dist/PSTContact.class.d.ts +900 -0
  38. package/dist/PSTContact.class.js +1253 -0
  39. package/dist/PSTFile.class.d.ts +144 -0
  40. package/dist/PSTFile.class.js +221 -0
  41. package/dist/PSTFolder.class.d.ts +111 -0
  42. package/dist/PSTFolder.class.js +269 -0
  43. package/dist/PSTMessage.class.d.ts +773 -0
  44. package/dist/PSTMessage.class.js +1264 -0
  45. package/dist/PSTMessageStore.class.d.ts +16 -0
  46. package/dist/PSTMessageStore.class.js +18 -0
  47. package/dist/PSTObject.class.d.ts +107 -0
  48. package/dist/PSTObject.class.js +208 -0
  49. package/dist/PSTOpts.d.ts +24 -0
  50. package/dist/PSTOpts.js +2 -0
  51. package/dist/PSTRecipient.class.d.ts +68 -0
  52. package/dist/PSTRecipient.class.js +105 -0
  53. package/dist/PSTTask.class.d.ts +146 -0
  54. package/dist/PSTTask.class.js +206 -0
  55. package/dist/PSTUtil.class.d.ts +134 -0
  56. package/dist/PSTUtil.class.js +804 -0
  57. package/dist/Property.d.ts +38 -0
  58. package/dist/Property.js +2 -0
  59. package/dist/PropertyContext.d.ts +6 -0
  60. package/dist/PropertyContext.js +2 -0
  61. package/dist/PropertyContextUtil.d.ts +4 -0
  62. package/dist/PropertyContextUtil.js +77 -0
  63. package/dist/PropertyTypeObject.d.ts +12 -0
  64. package/dist/PropertyTypeObject.js +21 -0
  65. package/dist/PropertyValueResolver.d.ts +4 -0
  66. package/dist/PropertyValueResolver.js +2 -0
  67. package/dist/PropertyValueResolverV1.d.ts +7 -0
  68. package/dist/PropertyValueResolverV1.js +253 -0
  69. package/dist/RawProperty.d.ts +8 -0
  70. package/dist/RawProperty.js +2 -0
  71. package/dist/RecurrencePattern.class.d.ts +50 -0
  72. package/dist/RecurrencePattern.class.js +120 -0
  73. package/dist/RootProvider.d.ts +12 -0
  74. package/dist/RootProvider.js +2 -0
  75. package/dist/SingleAsyncProvider.d.ts +5 -0
  76. package/dist/SingleAsyncProvider.js +30 -0
  77. package/dist/TableContext.d.ts +4 -0
  78. package/dist/TableContext.js +2 -0
  79. package/dist/TableContextUtil.d.ts +4 -0
  80. package/dist/TableContextUtil.js +147 -0
  81. package/dist/TableRow.d.ts +6 -0
  82. package/dist/TableRow.js +2 -0
  83. package/dist/index.d.ts +15 -0
  84. package/dist/index.js +28 -0
  85. package/dist/msftUuidStringify.d.ts +17 -0
  86. package/dist/msftUuidStringify.js +48 -0
  87. package/dist/openPstFile.d.ts +19 -0
  88. package/dist/openPstFile.js +67 -0
  89. package/package.json +105 -0
  90. package/readme.md +16 -0
package/dist/PLUtil.js ADDED
@@ -0,0 +1,683 @@
1
+ "use strict";
2
+ /**
3
+ * PST lower level utilities
4
+ */
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
18
+ }) : function(o, v) {
19
+ o["default"] = v;
20
+ });
21
+ var __importStar = (this && this.__importStar) || function (mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
+ __setModuleDefault(result, mod);
26
+ return result;
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __importDefault = (this && this.__importDefault) || function (mod) {
38
+ return (mod && mod.__esModule) ? mod : { "default": mod };
39
+ };
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ exports.openLowPst = exports.readLong = exports.readNumber64 = void 0;
42
+ const long_1 = __importDefault(require("long"));
43
+ const PSTUtil_class_1 = require("./PSTUtil.class");
44
+ const zlib = __importStar(require("zlib"));
45
+ function surelyReader(readFile) {
46
+ return (buffer, offset, length, position) => __awaiter(this, void 0, void 0, function* () {
47
+ const bytesRead = yield readFile(buffer, offset, length, position);
48
+ if (bytesRead !== length) {
49
+ throw new Error(`file reader must provide buffer ${length} bytes from position ${position}, while we got ${bytesRead} bytes`);
50
+ }
51
+ return length;
52
+ });
53
+ }
54
+ /**
55
+ * @internal
56
+ */
57
+ function readNumber64(view, offset) {
58
+ return new long_1.default(view.getUint32(offset, true), view.getUint32(offset + 4, true), true)
59
+ .toNumber();
60
+ }
61
+ exports.readNumber64 = readNumber64;
62
+ /**
63
+ * @internal
64
+ */
65
+ function readLong(view, offset) {
66
+ return new long_1.default(view.getUint32(offset, true), view.getUint32(offset + 4, true), true);
67
+ }
68
+ exports.readLong = readLong;
69
+ function passThru1(data) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ return data;
72
+ });
73
+ }
74
+ /**
75
+ *
76
+ * @internal
77
+ */
78
+ function willUnzip1(data) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ if (data.byteLength >= 4) {
81
+ const view = new DataView(data);
82
+ if (view.getUint16(0, true) === 0x9c78) {
83
+ const arrayBuffer = yield new Promise((resolve, reject) => zlib.unzip(Buffer.from(data), (error, result) => {
84
+ if (error) {
85
+ reject(error);
86
+ }
87
+ else {
88
+ resolve(result.buffer.slice(result.byteOffset, result.byteLength));
89
+ }
90
+ }));
91
+ return arrayBuffer;
92
+ }
93
+ }
94
+ return data;
95
+ });
96
+ }
97
+ var ptr32;
98
+ (function (ptr32) {
99
+ function readBlockPtr(view, offset, footer) {
100
+ const array = [];
101
+ for (let x = 0; x < footer.itemCount; x++) {
102
+ const top = offset + 12 * x;
103
+ const blockId = view.getUint32(top + 0, true);
104
+ array.push({
105
+ blockId,
106
+ offset: new long_1.default(view.getUint32(top + 4, true)),
107
+ size: view.getUint16(top + 8, true),
108
+ isData: (blockId & 2) === 0,
109
+ });
110
+ }
111
+ return array;
112
+ }
113
+ ptr32.readBlockPtr = readBlockPtr;
114
+ function readTablePtr(view, offset, footer) {
115
+ const array = [];
116
+ for (let x = 0; x < footer.itemCount; x++) {
117
+ const top = offset + 12 * x;
118
+ array.push({
119
+ start: new long_1.default(view.getUint32(top + 0, true)),
120
+ u1: new long_1.default(view.getUint32(top + 4, true)),
121
+ offset: new long_1.default(view.getUint32(top + 8, true)),
122
+ });
123
+ }
124
+ return array;
125
+ }
126
+ ptr32.readTablePtr = readTablePtr;
127
+ function readNodePtr(view, offset, footer) {
128
+ const array = [];
129
+ for (let x = 0; x < footer.itemCount; x++) {
130
+ const top = offset + 16 * x;
131
+ array.push({
132
+ nodeId: view.getUint32(top + 0, true),
133
+ blockId: view.getUint32(top + 4, true),
134
+ subBlockId: view.getUint32(top + 8, true),
135
+ parentNodeId: view.getUint32(top + 12, true),
136
+ });
137
+ }
138
+ return array;
139
+ }
140
+ ptr32.readNodePtr = readNodePtr;
141
+ function readSLBlock(view, offset) {
142
+ const count = view.getUint16(offset + 2, true);
143
+ const entries = [];
144
+ for (let x = 0; x < count; x++) {
145
+ const top = offset + 4 + 12 * x;
146
+ entries.push({
147
+ nodeId: view.getUint32(top + 0, true),
148
+ blockId: view.getUint32(top + 4, true),
149
+ subBlockId: view.getUint32(top + 8, true),
150
+ });
151
+ }
152
+ return {
153
+ entries
154
+ };
155
+ }
156
+ ptr32.readSLBlock = readSLBlock;
157
+ function readXBlock(view, offset, itemCount) {
158
+ const entries = [];
159
+ for (let x = 0; x < itemCount; x++) {
160
+ const top = offset + 8 + 4 * x;
161
+ entries.push({
162
+ blockId: view.getUint32(top + 0, true),
163
+ });
164
+ }
165
+ return entries;
166
+ }
167
+ ptr32.readXBlock = readXBlock;
168
+ function readXXBlock(view, offset, itemCount) {
169
+ const entries = [];
170
+ for (let x = 0; x < itemCount; x++) {
171
+ const top = offset + 8 + 4 * x;
172
+ entries.push({
173
+ blockId: view.getUint32(top + 0, true),
174
+ });
175
+ }
176
+ return entries;
177
+ }
178
+ ptr32.readXXBlock = readXXBlock;
179
+ function readSIBlock(view, offset) {
180
+ const count = view.getUint16(offset + 2, true);
181
+ const entries = [];
182
+ for (let x = 0; x < count; x++) {
183
+ const top = offset + 4 + 12 * x;
184
+ entries.push({
185
+ nodeId: view.getUint32(top + 0, true),
186
+ blockId: view.getUint32(top + 4, true),
187
+ });
188
+ }
189
+ return {
190
+ entries
191
+ };
192
+ }
193
+ ptr32.readSIBlock = readSIBlock;
194
+ })(ptr32 || (ptr32 = {}));
195
+ var ptr64;
196
+ (function (ptr64) {
197
+ function readBlockPtr(view, offset, footer) {
198
+ const array = [];
199
+ for (let x = 0; x < footer.itemCount; x++) {
200
+ const top = offset + 24 * x;
201
+ const blockId = readNumber64(view, top + 0);
202
+ array.push({
203
+ blockId,
204
+ offset: readLong(view, top + 8),
205
+ size: view.getUint16(top + 16, true),
206
+ isData: (blockId & 2) === 0,
207
+ });
208
+ }
209
+ return array;
210
+ }
211
+ ptr64.readBlockPtr = readBlockPtr;
212
+ function readTablePtr(view, offset, footer) {
213
+ const array = [];
214
+ for (let x = 0; x < footer.itemCount; x++) {
215
+ const top = offset + 24 * x;
216
+ array.push({
217
+ start: readLong(view, top + 0),
218
+ u1: readLong(view, top + 8),
219
+ offset: readLong(view, top + 16),
220
+ });
221
+ }
222
+ return array;
223
+ }
224
+ ptr64.readTablePtr = readTablePtr;
225
+ function readNodePtr(view, offset, footer) {
226
+ const array = [];
227
+ for (let x = 0; x < footer.itemCount; x++) {
228
+ const top = offset + 32 * x;
229
+ array.push({
230
+ nodeId: readNumber64(view, top + 0),
231
+ blockId: readNumber64(view, top + 8),
232
+ subBlockId: readNumber64(view, top + 16),
233
+ parentNodeId: view.getUint32(top + 24, true),
234
+ });
235
+ }
236
+ return array;
237
+ }
238
+ ptr64.readNodePtr = readNodePtr;
239
+ function readSLBlock(view, offset) {
240
+ const count = view.getUint16(offset + 2, true);
241
+ const entries = [];
242
+ for (let x = 0; x < count; x++) {
243
+ const top = offset + 8 + 24 * x;
244
+ entries.push({
245
+ nodeId: view.getUint32(top + 0, true),
246
+ blockId: readNumber64(view, top + 8),
247
+ subBlockId: readNumber64(view, top + 16),
248
+ });
249
+ }
250
+ return {
251
+ entries
252
+ };
253
+ }
254
+ ptr64.readSLBlock = readSLBlock;
255
+ function readXBlock(view, offset, itemCount) {
256
+ const entries = [];
257
+ for (let x = 0; x < itemCount; x++) {
258
+ const top = offset + 8 + 8 * x;
259
+ entries.push({
260
+ blockId: readNumber64(view, top + 0),
261
+ });
262
+ }
263
+ return entries;
264
+ }
265
+ ptr64.readXBlock = readXBlock;
266
+ function readXXBlock(view, offset, itemCount) {
267
+ const entries = [];
268
+ for (let x = 0; x < itemCount; x++) {
269
+ const top = offset + 8 + 8 * x;
270
+ entries.push({
271
+ blockId: readNumber64(view, top + 0),
272
+ });
273
+ }
274
+ return entries;
275
+ }
276
+ ptr64.readXXBlock = readXXBlock;
277
+ function readSIBlock(view, offset) {
278
+ const count = view.getUint16(offset + 2, true);
279
+ const entries = [];
280
+ for (let x = 0; x < count; x++) {
281
+ const top = offset + 8 + 24 * x;
282
+ entries.push({
283
+ nodeId: view.getUint32(top + 0, true),
284
+ blockId: readNumber64(view, top + 8),
285
+ });
286
+ }
287
+ return {
288
+ entries
289
+ };
290
+ }
291
+ ptr64.readSIBlock = readSIBlock;
292
+ })(ptr64 || (ptr64 = {}));
293
+ const ver0x0e = {
294
+ BACKLINK_OFFSET: 0x1f8,
295
+ LEVEL_INDICATOR_OFFSET: 0x1f3,
296
+ ITEM_COUNT_OFFSET: 0x1f0,
297
+ ENC_OFFSET: 0x1cd,
298
+ SECOND_POINTER_COUNT: 0xB8,
299
+ SECOND_POINTER: 0xBC,
300
+ INDEX_POINTER_COUNT: 0xC0,
301
+ INDEX_POINTER: 0xC4,
302
+ BlockSize: 512,
303
+ readId: (view, offset) => new long_1.default(view.getUint32(offset, true)),
304
+ readBlockPtr: ptr32.readBlockPtr,
305
+ readTablePtr: ptr32.readTablePtr,
306
+ readNodePtr: ptr32.readNodePtr,
307
+ readSLBlock: ptr32.readSLBlock,
308
+ readSIBlock: ptr32.readSIBlock,
309
+ readXBlock: ptr32.readXBlock,
310
+ readXXBlock: ptr32.readXXBlock,
311
+ unzipHook: passThru1,
312
+ };
313
+ const ver0x17 = {
314
+ BACKLINK_OFFSET: 0x1f8,
315
+ LEVEL_INDICATOR_OFFSET: 0x1eb,
316
+ ITEM_COUNT_OFFSET: 0x1e8,
317
+ ENC_OFFSET: 0x201,
318
+ SECOND_POINTER_COUNT: 0xD8,
319
+ SECOND_POINTER: 0xE0,
320
+ INDEX_POINTER_COUNT: 0xE8,
321
+ INDEX_POINTER: 0xF0,
322
+ BlockSize: 512,
323
+ readId: readLong,
324
+ readBlockPtr: ptr64.readBlockPtr,
325
+ readTablePtr: ptr64.readTablePtr,
326
+ readNodePtr: ptr64.readNodePtr,
327
+ readSLBlock: ptr64.readSLBlock,
328
+ readSIBlock: ptr64.readSIBlock,
329
+ readXBlock: ptr64.readXBlock,
330
+ readXXBlock: ptr64.readXXBlock,
331
+ unzipHook: passThru1,
332
+ };
333
+ const ver0x24 = {
334
+ BACKLINK_OFFSET: 0xff0,
335
+ LEVEL_INDICATOR_OFFSET: 0xfdd,
336
+ ITEM_COUNT_OFFSET: 0xfd8,
337
+ ENC_OFFSET: 0x201,
338
+ SECOND_POINTER_COUNT: 0xD8,
339
+ SECOND_POINTER: 0xE0,
340
+ INDEX_POINTER_COUNT: 0xE8,
341
+ INDEX_POINTER: 0xF0,
342
+ BlockSize: 4096,
343
+ readId: readLong,
344
+ readBlockPtr: ptr64.readBlockPtr,
345
+ readTablePtr: ptr64.readTablePtr,
346
+ readNodePtr: ptr64.readNodePtr,
347
+ readSLBlock: ptr64.readSLBlock,
348
+ readSIBlock: ptr64.readSIBlock,
349
+ readXBlock: ptr64.readXBlock,
350
+ readXXBlock: ptr64.readXXBlock,
351
+ unzipHook: willUnzip1,
352
+ };
353
+ function openLowPst(api) {
354
+ return __awaiter(this, void 0, void 0, function* () {
355
+ const surelyRead = surelyReader(api.readFile);
356
+ const buffer = new ArrayBuffer(1024);
357
+ const view = new DataView(buffer, 0, 1024);
358
+ yield surelyRead(buffer, 0, 1024, 0);
359
+ const key = '!BDN';
360
+ if (false
361
+ || view.getUint8(0) !== key.charCodeAt(0)
362
+ || view.getUint8(1) !== key.charCodeAt(1)
363
+ || view.getUint8(2) !== key.charCodeAt(2)
364
+ || view.getUint8(3) !== key.charCodeAt(3)) {
365
+ throw new Error('PSTFile::open Invalid file header (expected: "!BDN"): ' + buffer.slice(0, 4));
366
+ }
367
+ const version = view.getUint8(10);
368
+ let trait;
369
+ if (false) { }
370
+ else if (version === 0x0e) {
371
+ trait = ver0x0e;
372
+ }
373
+ else if (version === 0x17) {
374
+ trait = ver0x17;
375
+ }
376
+ else if (version === 0x24) {
377
+ trait = ver0x24;
378
+ }
379
+ else {
380
+ throw new Error('PSTFile::open Unrecognised PST File version: ' + version);
381
+ }
382
+ const encryptionType = view.getUint8(trait.ENC_OFFSET);
383
+ if (encryptionType === 0x02) {
384
+ throw new Error('PSTFile::open PST is encrypted');
385
+ }
386
+ const unzipHook = trait.unzipHook;
387
+ function pst_read_block_size(position, size, decrypt) {
388
+ return __awaiter(this, void 0, void 0, function* () {
389
+ const buffer = new ArrayBuffer(size);
390
+ yield surelyRead(buffer, 0, size, position.toNumber());
391
+ if (decrypt) {
392
+ if (false) { }
393
+ else if (encryptionType === 0) {
394
+ // plain
395
+ }
396
+ else if (encryptionType === 1) {
397
+ PSTUtil_class_1.PSTUtil.decodeArray(new Uint8Array(buffer, 0, size));
398
+ }
399
+ else {
400
+ throw new Error(`Unknown encryptionType ${encryptionType}`);
401
+ }
402
+ }
403
+ return yield unzipHook(buffer);
404
+ });
405
+ }
406
+ function readBlock(block, decrypt) {
407
+ return __awaiter(this, void 0, void 0, function* () {
408
+ return yield unzipHook(yield pst_read_block_size(block.offset, block.size, block.isData && decrypt));
409
+ });
410
+ }
411
+ const blockMap = new Map;
412
+ const nodeMap = new Map;
413
+ function loadBlockTree(offset, linku1, start_val) {
414
+ return __awaiter(this, void 0, void 0, function* () {
415
+ const buf = yield pst_read_block_size(offset, trait.BlockSize, false);
416
+ const view = new DataView(buf);
417
+ const footer = {
418
+ itemCount: view.getUint8(trait.ITEM_COUNT_OFFSET),
419
+ level: view.getUint8(trait.LEVEL_INDICATOR_OFFSET),
420
+ thisId: trait.readId(view, trait.BACKLINK_OFFSET),
421
+ };
422
+ if (footer.thisId.neq(linku1)) {
423
+ throw new Error(`block buffer seems to be broken. footer id is ${footer.thisId.toNumber()}, while ${linku1.toNumber()} is expected`);
424
+ }
425
+ if (footer.level === 0) {
426
+ const array = trait.readBlockPtr(view, 0, footer);
427
+ for (let x = 0; x < footer.itemCount; x++) {
428
+ if (x === 0 && start_val.neq(0) && start_val.neq(array[x].blockId)) {
429
+ throw new Error(`block buffer seems to be broken. first blockId is ${array[x].blockId}, while ${start_val.toNumber()} is expected`);
430
+ }
431
+ if (array[x].blockId === 0) {
432
+ throw new Error(`blockId must not be 0`);
433
+ }
434
+ blockMap.set(array[x].blockId & ~1, array[x]);
435
+ }
436
+ }
437
+ else {
438
+ const array = trait.readTablePtr(view, 0, footer);
439
+ for (let x = 0; x < footer.itemCount; x++) {
440
+ if (x === 0 && start_val.neq(0) && start_val.neq(array[x].start)) {
441
+ throw new Error(`block buffer seems to be broken. first table id is ${array[x].start.toNumber()}, while ${start_val.toNumber()} is expected`);
442
+ }
443
+ if (array[x].start.eq(0)) {
444
+ throw new Error(`table id must not be 0`);
445
+ }
446
+ yield loadBlockTree(array[x].offset, array[x].u1, array[x].start);
447
+ }
448
+ }
449
+ });
450
+ }
451
+ const block_btree_count = trait.readId(view, trait.INDEX_POINTER_COUNT);
452
+ const block_btree = trait.readId(view, trait.INDEX_POINTER);
453
+ yield loadBlockTree(block_btree, block_btree_count, long_1.default.ZERO);
454
+ function loadNodeTree(offset, linku1, start_val, prevLevel) {
455
+ return __awaiter(this, void 0, void 0, function* () {
456
+ const buf = yield pst_read_block_size(offset, trait.BlockSize, false);
457
+ const view = new DataView(buf);
458
+ const footer = {
459
+ itemCount: view.getUint8(trait.ITEM_COUNT_OFFSET),
460
+ level: view.getUint8(trait.LEVEL_INDICATOR_OFFSET),
461
+ thisId: trait.readId(view, trait.BACKLINK_OFFSET),
462
+ };
463
+ if (prevLevel !== Infinity) {
464
+ if (footer.level !== prevLevel - 1) {
465
+ throw new Error(`node level descending mismatched`);
466
+ }
467
+ }
468
+ if (footer.thisId.neq(linku1)) {
469
+ throw new Error(`node buffer seems to be broken. footer id is ${footer.thisId.toNumber()}, while ${linku1.toNumber()} is expected`);
470
+ }
471
+ if (footer.level === 0) {
472
+ const array = trait.readNodePtr(view, 0, footer);
473
+ for (let x = 0; x < footer.itemCount; x++) {
474
+ if (x === 0 && start_val !== (0) && start_val !== (array[x].nodeId)) {
475
+ throw new Error(`node buffer seems to be broken. first nodeId is ${array[x].nodeId}, while ${start_val} is expected`);
476
+ }
477
+ if (array[x].nodeId === 0) {
478
+ break;
479
+ }
480
+ nodeMap.set(array[x].nodeId, array[x]);
481
+ }
482
+ }
483
+ else {
484
+ const array = trait.readTablePtr(view, 0, footer);
485
+ for (let x = 0; x < footer.itemCount; x++) {
486
+ if (x === 0 && start_val !== (0) && array[x].start.neq(start_val)) {
487
+ throw new Error(`node buffer seems to be broken. table id is ${array[x].start.toNumber()}, while ${start_val} is expected`);
488
+ }
489
+ if (array[x].start.isZero()) {
490
+ throw new Error(`table id must not be 0`);
491
+ }
492
+ yield loadNodeTree(array[x].offset, array[x].u1, array[x].start.toNumber(), footer.level);
493
+ }
494
+ }
495
+ });
496
+ }
497
+ const node_btree_count = trait.readId(view, trait.SECOND_POINTER_COUNT);
498
+ const node_btree = trait.readId(view, trait.SECOND_POINTER);
499
+ yield loadNodeTree(node_btree, node_btree_count, 0x21, Infinity);
500
+ function loadMainBlockTo(blockId, consumer) {
501
+ return __awaiter(this, void 0, void 0, function* () {
502
+ if (blockId === 0) {
503
+ return;
504
+ }
505
+ const block = blockMap.get(blockId & ~1);
506
+ if (block === undefined) {
507
+ throw new Error(`blockId=${blockId} not found`);
508
+ }
509
+ if (block.isData) {
510
+ yield consumer(block);
511
+ }
512
+ else {
513
+ const buf = yield readBlock(block, true);
514
+ const view = new DataView(buf);
515
+ const bType = view.getUint8(0);
516
+ if (bType !== 1) {
517
+ throw new Error("btype != 1");
518
+ }
519
+ const level = view.getUint8(1);
520
+ const itemCount = view.getUint16(2, true);
521
+ if (false) { }
522
+ else if (level === 1) {
523
+ //XBLOCK
524
+ const entries = trait.readXBlock(view, 0, itemCount);
525
+ for (let x = 0; x < entries.length; x++) {
526
+ yield loadMainBlockTo(entries[x].blockId, consumer);
527
+ }
528
+ }
529
+ else if (level === 2) {
530
+ //XXBLOCK
531
+ const entries = trait.readXXBlock(view, 0, itemCount);
532
+ for (let x = 0; x < entries.length; x++) {
533
+ yield loadMainBlockTo(entries[x].blockId, consumer);
534
+ }
535
+ }
536
+ else {
537
+ throw new Error(`invalid level ${level}`);
538
+ }
539
+ }
540
+ });
541
+ }
542
+ function readSubNode(blockId, subNodeMap) {
543
+ return __awaiter(this, void 0, void 0, function* () {
544
+ if (blockId === 0) {
545
+ return;
546
+ }
547
+ const block = blockMap.get(blockId);
548
+ if (block === undefined) {
549
+ throw new Error(`blockId ${blockId} not found`);
550
+ }
551
+ const buf = yield readBlock(block, true);
552
+ const view = new DataView(buf);
553
+ if (view.getUint8(0) !== 2) {
554
+ throw new Error("btype != 2");
555
+ }
556
+ const level = view.getUint8(1);
557
+ if (level === 0) {
558
+ //SLBLOCK
559
+ const { entries } = trait.readSLBlock(view, 0);
560
+ for (let index = 0; index < entries.length; index++) {
561
+ subNodeMap.set(entries[index].nodeId, {
562
+ dataBlockId: entries[index].blockId,
563
+ subBlockId: entries[index].subBlockId,
564
+ });
565
+ }
566
+ }
567
+ else {
568
+ //SIBLOCK
569
+ const struc = trait.readSIBlock(view, 0);
570
+ const { entries } = struc;
571
+ for (let index = 0; index < entries.length; index++) {
572
+ yield readSubNode(entries[index].blockId, subNodeMap);
573
+ }
574
+ }
575
+ });
576
+ }
577
+ //const nodeMap = processNameToIDMap();
578
+ function getOneNodeBy(nodeId) {
579
+ if (nodeId === 0) {
580
+ return undefined;
581
+ }
582
+ const ptr = nodeMap.get(nodeId);
583
+ if (ptr === undefined) {
584
+ return undefined;
585
+ }
586
+ const { blockId } = ptr;
587
+ function getData() {
588
+ return __awaiter(this, void 0, void 0, function* () {
589
+ const array = [];
590
+ yield loadMainBlockTo(blockId, (block) => __awaiter(this, void 0, void 0, function* () {
591
+ const buf = yield readBlock(block, true);
592
+ array.push(buf);
593
+ }));
594
+ if (array.length === 0) {
595
+ return new ArrayBuffer(0);
596
+ }
597
+ else {
598
+ return array[0];
599
+ }
600
+ });
601
+ }
602
+ function getDataOf(blockId) {
603
+ return __awaiter(this, void 0, void 0, function* () {
604
+ const array = [];
605
+ yield loadMainBlockTo(blockId, (block) => __awaiter(this, void 0, void 0, function* () {
606
+ array.push(yield readBlock(block, true));
607
+ }));
608
+ return array;
609
+ });
610
+ }
611
+ function getChildOf(blockId, childNodeId, parentToString) {
612
+ return __awaiter(this, void 0, void 0, function* () {
613
+ if (blockId === 0) {
614
+ return undefined;
615
+ }
616
+ const block = blockMap.get(blockId & ~1);
617
+ if (block === undefined) {
618
+ throw new Error(`blockId=${blockId}`
619
+ + ` for childNodeId=0x${childNodeId.toString(16)}`
620
+ + ` of ${parentToString}`
621
+ + ` not found`);
622
+ }
623
+ const subNodeMap = new Map();
624
+ yield readSubNode(block.blockId, subNodeMap);
625
+ const thisToString = `childNodeId=0x${childNodeId.toString(16)}`
626
+ + `,nidType=0x${(childNodeId & 0x1f).toString(16)}`
627
+ + ` of ${parentToString}`;
628
+ const subNode = subNodeMap.get(childNodeId);
629
+ if (subNode === undefined) {
630
+ return undefined;
631
+ }
632
+ return {
633
+ nodeId: childNodeId,
634
+ getChildBy: (childNodeId) => __awaiter(this, void 0, void 0, function* () {
635
+ return yield getChildOf(subNode.subBlockId, childNodeId, thisToString);
636
+ }),
637
+ getData: () => __awaiter(this, void 0, void 0, function* () { return yield getDataOf(subNode.dataBlockId); }),
638
+ toString: () => thisToString,
639
+ };
640
+ });
641
+ }
642
+ function getSubNodeOf(nodeId) {
643
+ const node = nodeMap.get(nodeId);
644
+ if (node === undefined) {
645
+ throw new Error(`nodeId=${nodeId} not found`);
646
+ }
647
+ const thisToString = `subNode of nodeId=${nodeId},nidType=${nodeId & 0x1f}`;
648
+ return {
649
+ nodeId: nodeId,
650
+ getChildBy: (childNodeId) => __awaiter(this, void 0, void 0, function* () {
651
+ return getChildOf(node.subBlockId, childNodeId, thisToString);
652
+ }),
653
+ getData: () => __awaiter(this, void 0, void 0, function* () { return yield getDataOf(node.blockId); }),
654
+ toString: () => thisToString,
655
+ };
656
+ }
657
+ return {
658
+ nodeId: (ptr.nodeId),
659
+ getParent: () => getOneNodeBy((ptr.parentNodeId)),
660
+ getChildren: () => Array.from(nodeMap.values())
661
+ .filter(it => it.parentNodeId === ptr.nodeId && it.nodeId !== ptr.nodeId)
662
+ .map(it => getOneNodeBy(it.nodeId))
663
+ .filter(it => it !== undefined),
664
+ getSubNode: () => getSubNodeOf(ptr.nodeId),
665
+ getSiblingNode: (nidType) => getOneNodeBy((nodeId & ~0x1f) | (nidType & 0x1f)),
666
+ };
667
+ }
668
+ ;
669
+ function getOneNodeByOrError(nodeId) {
670
+ const node = getOneNodeBy(nodeId);
671
+ if (node === undefined) {
672
+ throw new Error(`node ${nodeId} must exist and must be valid`);
673
+ }
674
+ return node;
675
+ }
676
+ return {
677
+ getOneNodeBy,
678
+ getOneNodeByOrError,
679
+ close: () => api.close(),
680
+ };
681
+ });
682
+ }
683
+ exports.openLowPst = openLowPst;