@dxos/teleport-extension-object-sync 0.8.3 → 0.8.4-main.84f28bd

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/teleport-extension-object-sync",
3
- "version": "0.8.3",
3
+ "version": "0.8.4-main.84f28bd",
4
4
  "description": "Teleport extension to synchronize opaque data objects.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -24,17 +24,17 @@
24
24
  "src"
25
25
  ],
26
26
  "dependencies": {
27
- "@dxos/async": "0.8.3",
28
- "@dxos/crypto": "0.8.3",
29
- "@dxos/invariant": "0.8.3",
30
- "@dxos/log": "0.8.3",
31
- "@dxos/keys": "0.8.3",
32
- "@dxos/node-std": "0.8.3",
33
- "@dxos/protocols": "0.8.3",
34
- "@dxos/context": "0.8.3",
35
- "@dxos/random-access-storage": "0.8.3",
36
- "@dxos/teleport": "0.8.3",
37
- "@dxos/util": "0.8.3"
27
+ "@dxos/async": "0.8.4-main.84f28bd",
28
+ "@dxos/context": "0.8.4-main.84f28bd",
29
+ "@dxos/crypto": "0.8.4-main.84f28bd",
30
+ "@dxos/invariant": "0.8.4-main.84f28bd",
31
+ "@dxos/keys": "0.8.4-main.84f28bd",
32
+ "@dxos/log": "0.8.4-main.84f28bd",
33
+ "@dxos/protocols": "0.8.4-main.84f28bd",
34
+ "@dxos/node-std": "0.8.4-main.84f28bd",
35
+ "@dxos/random-access-storage": "0.8.4-main.84f28bd",
36
+ "@dxos/teleport": "0.8.4-main.84f28bd",
37
+ "@dxos/util": "0.8.4-main.84f28bd"
38
38
  },
39
39
  "publishConfig": {
40
40
  "access": "public"
@@ -1,664 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var node_exports = {};
30
- __export(node_exports, {
31
- BlobStore: () => BlobStore,
32
- BlobSync: () => BlobSync,
33
- BlobSyncExtension: () => BlobSyncExtension,
34
- DEFAULT_CHUNK_SIZE: () => DEFAULT_CHUNK_SIZE
35
- });
36
- module.exports = __toCommonJS(node_exports);
37
- var import_async = require("@dxos/async");
38
- var import_context = require("@dxos/context");
39
- var import_invariant = require("@dxos/invariant");
40
- var import_log = require("@dxos/log");
41
- var import_protocols = require("@dxos/protocols");
42
- var import_proto = require("@dxos/protocols/proto");
43
- var import_teleport = require("@dxos/teleport");
44
- var import_util = require("@dxos/util");
45
- var import_async2 = require("@dxos/async");
46
- var import_context2 = require("@dxos/context");
47
- var import_invariant2 = require("@dxos/invariant");
48
- var import_keys = require("@dxos/keys");
49
- var import_log2 = require("@dxos/log");
50
- var import_blob = require("@dxos/protocols/proto/dxos/echo/blob");
51
- var import_util2 = require("@dxos/util");
52
- var import_node_path = __toESM(require("node:path"));
53
- var import_async3 = require("@dxos/async");
54
- var import_crypto = require("@dxos/crypto");
55
- var import_invariant3 = require("@dxos/invariant");
56
- var import_keys2 = require("@dxos/keys");
57
- var import_proto2 = require("@dxos/protocols/proto");
58
- var import_blob2 = require("@dxos/protocols/proto/dxos/echo/blob");
59
- var import_util3 = require("@dxos/util");
60
- function _ts_decorate(decorators, target, key, desc) {
61
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
62
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
63
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
64
- return c > 3 && r && Object.defineProperty(target, key, r), r;
65
- }
66
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport-extension-object-sync/src/blob-sync-extension.ts";
67
- var MIN_WANT_LIST_UPDATE_INTERVAL = process.env.NODE_ENV === "test" ? 5 : 500;
68
- var MAX_CONCURRENT_UPLOADS = 20;
69
- var BlobSyncExtension = class extends import_teleport.RpcExtension {
70
- constructor(_params) {
71
- super({
72
- exposed: {
73
- BlobSyncService: import_proto.schema.getService("dxos.mesh.teleport.blobsync.BlobSyncService")
74
- },
75
- requested: {
76
- BlobSyncService: import_proto.schema.getService("dxos.mesh.teleport.blobsync.BlobSyncService")
77
- },
78
- timeout: 2e4,
79
- encodingOptions: {
80
- preserveAny: true
81
- }
82
- }), this._params = _params, this._ctx = new import_context.Context({
83
- onError: (err) => import_log.log.catch(err, void 0, {
84
- F: __dxlog_file,
85
- L: 35,
86
- S: this,
87
- C: (f, a) => f(...a)
88
- })
89
- }, {
90
- F: __dxlog_file,
91
- L: 35
92
- }), this._lastWantListUpdate = 0, this._localWantList = {
93
- blobs: []
94
- }, this._updateWantList = new import_async.DeferredTask(this._ctx, async () => {
95
- if (this._lastWantListUpdate + MIN_WANT_LIST_UPDATE_INTERVAL > Date.now()) {
96
- await (0, import_async.sleep)(this._lastWantListUpdate + MIN_WANT_LIST_UPDATE_INTERVAL - Date.now());
97
- if (this._ctx.disposed) {
98
- return;
99
- }
100
- }
101
- (0, import_log.log)("want", {
102
- list: this._localWantList
103
- }, {
104
- F: __dxlog_file,
105
- L: 49,
106
- S: this,
107
- C: (f, a) => f(...a)
108
- });
109
- await this.rpc.BlobSyncService.want(this._localWantList);
110
- this._lastWantListUpdate = Date.now();
111
- }), this._currentUploads = 0, this._upload = new import_async.DeferredTask(this._ctx, async () => {
112
- if (this._currentUploads >= MAX_CONCURRENT_UPLOADS) {
113
- return;
114
- }
115
- const blobChunks = await this._pickBlobChunks(MAX_CONCURRENT_UPLOADS - this._currentUploads);
116
- if (!blobChunks) {
117
- return;
118
- }
119
- for (const blobChunk of blobChunks) {
120
- if (this._ctx.disposed) {
121
- break;
122
- }
123
- this._currentUploads++;
124
- this.push(blobChunk).catch((err) => {
125
- if (err instanceof import_protocols.RpcClosedError) {
126
- return;
127
- }
128
- import_log.log.warn("push failed", {
129
- err
130
- }, {
131
- F: __dxlog_file,
132
- L: 76,
133
- S: this,
134
- C: (f, a) => f(...a)
135
- });
136
- }).finally(() => {
137
- this._currentUploads--;
138
- this.reconcileUploads();
139
- });
140
- }
141
- }), this.remoteWantList = {
142
- blobs: []
143
- };
144
- }
145
- async onOpen(context) {
146
- (0, import_log.log)("open", void 0, {
147
- F: __dxlog_file,
148
- L: 108,
149
- S: this,
150
- C: (f, a) => f(...a)
151
- });
152
- await super.onOpen(context);
153
- await this._params.onOpen();
154
- }
155
- async onClose(err) {
156
- (0, import_log.log)("close", void 0, {
157
- F: __dxlog_file,
158
- L: 114,
159
- S: this,
160
- C: (f, a) => f(...a)
161
- });
162
- await this._ctx.dispose();
163
- await this._params.onClose();
164
- await super.onClose(err);
165
- }
166
- async onAbort(err) {
167
- (0, import_log.log)("abort", void 0, {
168
- F: __dxlog_file,
169
- L: 121,
170
- S: this,
171
- C: (f, a) => f(...a)
172
- });
173
- await this._ctx.dispose();
174
- await this._params.onAbort();
175
- await super.onAbort(err);
176
- }
177
- async getHandlers() {
178
- return {
179
- BlobSyncService: {
180
- want: async (wantList) => {
181
- (0, import_log.log)("remote want", {
182
- remoteWantList: wantList
183
- }, {
184
- F: __dxlog_file,
185
- L: 131,
186
- S: this,
187
- C: (f, a) => f(...a)
188
- });
189
- this.remoteWantList = wantList;
190
- this.reconcileUploads();
191
- },
192
- push: async (data) => {
193
- (0, import_log.log)("received", {
194
- data
195
- }, {
196
- F: __dxlog_file,
197
- L: 136,
198
- S: this,
199
- C: (f, a) => f(...a)
200
- });
201
- await this._params.onPush(data);
202
- }
203
- }
204
- };
205
- }
206
- async push(data) {
207
- if (this._ctx.disposed) {
208
- return;
209
- }
210
- (0, import_log.log)("push", {
211
- data
212
- }, {
213
- F: __dxlog_file,
214
- L: 148,
215
- S: this,
216
- C: (f, a) => f(...a)
217
- });
218
- await this.rpc.BlobSyncService.push(data);
219
- }
220
- updateWantList(wantList) {
221
- if (this._ctx.disposed) {
222
- return;
223
- }
224
- this._localWantList = wantList;
225
- this._updateWantList.schedule();
226
- }
227
- reconcileUploads() {
228
- if (this._ctx.disposed) {
229
- return;
230
- }
231
- this._upload.schedule();
232
- }
233
- async _pickBlobChunks(amount = 1) {
234
- if (this._ctx.disposed) {
235
- return;
236
- }
237
- if (!this.remoteWantList.blobs || this.remoteWantList.blobs?.length === 0) {
238
- return;
239
- }
240
- const shuffled = [
241
- ...this.remoteWantList.blobs
242
- ].sort(() => Math.random() - 0.5);
243
- const chunks = [];
244
- for (const header of shuffled) {
245
- const meta = await this._params.blobStore.getMeta(header.id);
246
- if (!meta) {
247
- continue;
248
- }
249
- (0, import_invariant.invariant)(meta.bitfield, void 0, {
250
- F: __dxlog_file,
251
- L: 187,
252
- S: this,
253
- A: [
254
- "meta.bitfield",
255
- ""
256
- ]
257
- });
258
- (0, import_invariant.invariant)(meta.chunkSize, void 0, {
259
- F: __dxlog_file,
260
- L: 188,
261
- S: this,
262
- A: [
263
- "meta.chunkSize",
264
- ""
265
- ]
266
- });
267
- (0, import_invariant.invariant)(meta.length, void 0, {
268
- F: __dxlog_file,
269
- L: 189,
270
- S: this,
271
- A: [
272
- "meta.length",
273
- ""
274
- ]
275
- });
276
- if (header.chunkSize && header.chunkSize !== meta.chunkSize) {
277
- import_log.log.warn("Invalid chunk size", {
278
- header,
279
- meta
280
- }, {
281
- F: __dxlog_file,
282
- L: 192,
283
- S: this,
284
- C: (f, a) => f(...a)
285
- });
286
- continue;
287
- }
288
- const requestBitfield = header.bitfield ?? import_util.BitField.ones(meta.length / meta.chunkSize);
289
- const presentData = import_util.BitField.and(requestBitfield, meta.bitfield);
290
- const chunkIndices = import_util.BitField.findIndexes(presentData).sort(() => Math.random() - 0.5);
291
- for (const idx of chunkIndices) {
292
- const chunkData = await this._params.blobStore.get(header.id, {
293
- offset: idx * meta.chunkSize,
294
- length: Math.min(meta.chunkSize, meta.length - idx * meta.chunkSize)
295
- });
296
- chunks.push({
297
- id: header.id,
298
- totalLength: meta.length,
299
- chunkSize: meta.chunkSize,
300
- chunkOffset: idx * meta.chunkSize,
301
- payload: chunkData
302
- });
303
- if (chunks.length >= amount) {
304
- return chunks;
305
- }
306
- }
307
- }
308
- return chunks;
309
- }
310
- };
311
- _ts_decorate([
312
- import_async.synchronized
313
- ], BlobSyncExtension.prototype, "push", null);
314
- function _ts_decorate2(decorators, target, key, desc) {
315
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
316
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
317
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
318
- return c > 3 && r && Object.defineProperty(target, key, r), r;
319
- }
320
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport-extension-object-sync/src/blob-sync.ts";
321
- var BlobSync = class {
322
- constructor(_params) {
323
- this._params = _params;
324
- this._ctx = new import_context2.Context(void 0, {
325
- F: __dxlog_file2,
326
- L: 30
327
- });
328
- this._mutex = new import_async2.Mutex();
329
- this._downloadRequests = new import_util2.ComplexMap((key) => import_keys.PublicKey.from(key).toHex());
330
- this._extensions = /* @__PURE__ */ new Set();
331
- }
332
- async open() {
333
- }
334
- async close() {
335
- await this._ctx.dispose();
336
- }
337
- /**
338
- * Resolves when the object with the given id is fully downloaded in the blob store.
339
- *
340
- * @param id hex-encoded id of the object to download.
341
- */
342
- async download(ctx, id) {
343
- (0, import_log2.log)("download", {
344
- id
345
- }, {
346
- F: __dxlog_file2,
347
- L: 53,
348
- S: this,
349
- C: (f, a) => f(...a)
350
- });
351
- const request = await this._mutex.executeSynchronized(async () => {
352
- const existingRequest = this._downloadRequests.get(id);
353
- if (existingRequest) {
354
- existingRequest.counter++;
355
- return existingRequest;
356
- }
357
- const meta = await this._params.blobStore.getMeta(id);
358
- const request2 = {
359
- trigger: new import_async2.Trigger(),
360
- counter: 1,
361
- want: {
362
- id,
363
- chunkSize: meta?.chunkSize,
364
- bitfield: meta?.bitfield && Uint8Array.from(import_util2.BitField.invert(meta.bitfield))
365
- }
366
- };
367
- if (meta?.state === import_blob.BlobMeta.State.FULLY_PRESENT) {
368
- request2.trigger.wake();
369
- } else {
370
- this._downloadRequests.set(id, request2);
371
- this._updateExtensionsWantList();
372
- }
373
- return request2;
374
- });
375
- ctx?.onDispose(() => this._mutex.executeSynchronized(async () => {
376
- const request2 = this._downloadRequests.get(id);
377
- if (!request2) {
378
- return;
379
- }
380
- if (--request2.counter === 0) {
381
- this._downloadRequests.delete(id);
382
- }
383
- this._updateExtensionsWantList();
384
- }));
385
- return ctx ? (0, import_context2.cancelWithContext)(ctx, request.trigger.wait()) : request.trigger.wait();
386
- }
387
- createExtension() {
388
- const extension = new BlobSyncExtension({
389
- blobStore: this._params.blobStore,
390
- onOpen: async () => {
391
- (0, import_log2.log)("extension opened", void 0, {
392
- F: __dxlog_file2,
393
- L: 105,
394
- S: this,
395
- C: (f, a) => f(...a)
396
- });
397
- this._extensions.add(extension);
398
- extension.updateWantList(this._getWantList());
399
- },
400
- onClose: async () => {
401
- (0, import_log2.log)("extension closed", void 0, {
402
- F: __dxlog_file2,
403
- L: 110,
404
- S: this,
405
- C: (f, a) => f(...a)
406
- });
407
- this._extensions.delete(extension);
408
- },
409
- onAbort: async () => {
410
- (0, import_log2.log)("extension aborted", void 0, {
411
- F: __dxlog_file2,
412
- L: 114,
413
- S: this,
414
- C: (f, a) => f(...a)
415
- });
416
- this._extensions.delete(extension);
417
- },
418
- onPush: async (blobChunk) => {
419
- if (!this._downloadRequests.has(blobChunk.id)) {
420
- return;
421
- }
422
- (0, import_log2.log)("received", {
423
- blobChunk
424
- }, {
425
- F: __dxlog_file2,
426
- L: 121,
427
- S: this,
428
- C: (f, a) => f(...a)
429
- });
430
- const meta = await this._params.blobStore.setChunk(blobChunk);
431
- if (meta.state === import_blob.BlobMeta.State.FULLY_PRESENT) {
432
- this._downloadRequests.get(blobChunk.id)?.trigger.wake();
433
- this._downloadRequests.delete(blobChunk.id);
434
- } else {
435
- (0, import_invariant2.invariant)(meta.bitfield, void 0, {
436
- F: __dxlog_file2,
437
- L: 127,
438
- S: this,
439
- A: [
440
- "meta.bitfield",
441
- ""
442
- ]
443
- });
444
- this._downloadRequests.get(blobChunk.id).want.bitfield = import_util2.BitField.invert(meta.bitfield);
445
- }
446
- this._updateExtensionsWantList();
447
- this._reconcileUploads();
448
- }
449
- });
450
- return extension;
451
- }
452
- /**
453
- * Notify extensions that a blob with the given id was added to the blob store.
454
- */
455
- async notifyBlobAdded(_id) {
456
- this._reconcileUploads();
457
- }
458
- _getWantList() {
459
- return {
460
- blobs: Array.from(this._downloadRequests.values()).map((request) => request.want)
461
- };
462
- }
463
- _reconcileUploads() {
464
- for (const extension of this._extensions) {
465
- extension.reconcileUploads();
466
- }
467
- }
468
- _updateExtensionsWantList() {
469
- for (const extension of this._extensions) {
470
- extension.updateWantList(this._getWantList());
471
- }
472
- }
473
- };
474
- BlobSync = _ts_decorate2([
475
- (0, import_async2.trackLeaks)("open", "close")
476
- ], BlobSync);
477
- function _ts_decorate3(decorators, target, key, desc) {
478
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
479
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
480
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
481
- return c > 3 && r && Object.defineProperty(target, key, r), r;
482
- }
483
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/teleport-extension-object-sync/src/blob-store.ts";
484
- var DEFAULT_CHUNK_SIZE = 4096;
485
- var BlobMetaCodec = import_proto2.schema.getCodecForType("dxos.echo.blob.BlobMeta");
486
- var BlobStore = class {
487
- constructor(_directory) {
488
- this._directory = _directory;
489
- }
490
- async getMeta(id) {
491
- return this._getMeta(id);
492
- }
493
- /**
494
- * @throws If range is not available.
495
- */
496
- async get(id, options = {}) {
497
- const metadata = await this._getMeta(id);
498
- if (!metadata) {
499
- throw new Error("Blob not available");
500
- }
501
- const { offset = 0, length = metadata.length } = options;
502
- if (offset + length > metadata.length) {
503
- throw new Error("Invalid range");
504
- }
505
- if (metadata.state === import_blob2.BlobMeta.State.FULLY_PRESENT) {
506
- const file2 = this._getDataFile(id);
507
- return file2.read(offset, length);
508
- } else if (options.offset === void 0 && options.length === void 0) {
509
- throw new Error("Blob not available");
510
- }
511
- const beginChunk = Math.floor(offset / metadata.chunkSize);
512
- const endChunk = Math.ceil((offset + length) / metadata.chunkSize);
513
- (0, import_invariant3.invariant)(metadata.bitfield, "Bitfield not present", {
514
- F: __dxlog_file3,
515
- L: 61,
516
- S: this,
517
- A: [
518
- "metadata.bitfield",
519
- "'Bitfield not present'"
520
- ]
521
- });
522
- (0, import_invariant3.invariant)(metadata.bitfield.length * 8 >= endChunk, "Invalid bitfield length", {
523
- F: __dxlog_file3,
524
- L: 62,
525
- S: this,
526
- A: [
527
- "metadata.bitfield.length * 8 >= endChunk",
528
- "'Invalid bitfield length'"
529
- ]
530
- });
531
- const present = import_util3.BitField.count(metadata.bitfield, beginChunk, endChunk) === endChunk - beginChunk;
532
- if (!present) {
533
- throw new Error("Blob not available");
534
- }
535
- const file = this._getDataFile(id);
536
- return file.read(offset, length);
537
- }
538
- async list() {
539
- const files = new Set((await this._directory.list()).map((f) => f.split("_")[0]));
540
- const res = [];
541
- for (const file of files) {
542
- const id = import_keys2.PublicKey.from(file).asUint8Array();
543
- const meta = await this._getMeta(id);
544
- if (meta) {
545
- res.push(meta);
546
- }
547
- }
548
- return res;
549
- }
550
- async set(data) {
551
- const id = new Uint8Array(await import_crypto.subtleCrypto.digest("SHA-256", data));
552
- const bitfield = import_util3.BitField.ones(data.length / DEFAULT_CHUNK_SIZE);
553
- const meta = {
554
- id,
555
- state: import_blob2.BlobMeta.State.FULLY_PRESENT,
556
- length: data.length,
557
- chunkSize: DEFAULT_CHUNK_SIZE,
558
- bitfield,
559
- created: /* @__PURE__ */ new Date(),
560
- updated: /* @__PURE__ */ new Date()
561
- };
562
- await this._getDataFile(id).write(0, (0, import_util3.arrayToBuffer)(data));
563
- await this._writeMeta(id, meta);
564
- return meta;
565
- }
566
- // TODO(dmaretskyi): Optimize locking.
567
- async setChunk(chunk) {
568
- let meta = await this._getMeta(chunk.id);
569
- if (!meta) {
570
- (0, import_invariant3.invariant)(chunk.totalLength, "totalLength is not present", {
571
- F: __dxlog_file3,
572
- L: 124,
573
- S: this,
574
- A: [
575
- "chunk.totalLength",
576
- "'totalLength is not present'"
577
- ]
578
- });
579
- meta = {
580
- id: chunk.id,
581
- state: import_blob2.BlobMeta.State.PARTIALLY_PRESENT,
582
- length: chunk.totalLength,
583
- chunkSize: chunk.chunkSize ?? DEFAULT_CHUNK_SIZE,
584
- created: /* @__PURE__ */ new Date()
585
- };
586
- meta.bitfield = import_util3.BitField.zeros(meta.length / meta.chunkSize);
587
- }
588
- if (chunk.chunkSize && chunk.chunkSize !== meta.chunkSize) {
589
- throw new Error("Invalid chunk size");
590
- }
591
- (0, import_invariant3.invariant)(meta.bitfield, "Bitfield not present", {
592
- F: __dxlog_file3,
593
- L: 139,
594
- S: this,
595
- A: [
596
- "meta.bitfield",
597
- "'Bitfield not present'"
598
- ]
599
- });
600
- (0, import_invariant3.invariant)(chunk.chunkOffset !== void 0, "chunkOffset is not present", {
601
- F: __dxlog_file3,
602
- L: 140,
603
- S: this,
604
- A: [
605
- "chunk.chunkOffset !== undefined",
606
- "'chunkOffset is not present'"
607
- ]
608
- });
609
- await this._getDataFile(chunk.id).write(chunk.chunkOffset, (0, import_util3.arrayToBuffer)(chunk.payload));
610
- import_util3.BitField.set(meta.bitfield, Math.floor(chunk.chunkOffset / meta.chunkSize), true);
611
- if (import_util3.BitField.count(meta.bitfield, 0, meta.length) * meta.chunkSize >= meta.length) {
612
- meta.state = import_blob2.BlobMeta.State.FULLY_PRESENT;
613
- }
614
- meta.updated = /* @__PURE__ */ new Date();
615
- await this._writeMeta(chunk.id, meta);
616
- return meta;
617
- }
618
- async _writeMeta(id, meta) {
619
- const encoded = (0, import_util3.arrayToBuffer)(BlobMetaCodec.encode(meta));
620
- const data = Buffer.alloc(encoded.length + 4);
621
- data.writeUInt32LE(encoded.length, 0);
622
- encoded.copy(data, 4);
623
- await this._getMetaFile(id).write(0, data);
624
- }
625
- async _getMeta(id) {
626
- const file = this._getMetaFile(id);
627
- const size = (await file.stat()).size;
628
- if (size === 0) {
629
- return;
630
- }
631
- const data = await file.read(0, size);
632
- const protoSize = data.readUInt32LE(0);
633
- return BlobMetaCodec.decode(data.subarray(4, protoSize + 4));
634
- }
635
- _getMetaFile(id) {
636
- return this._directory.getOrCreateFile(import_node_path.default.join((0, import_util3.arrayToBuffer)(id).toString("hex"), "meta"));
637
- }
638
- _getDataFile(id) {
639
- return this._directory.getOrCreateFile(import_node_path.default.join((0, import_util3.arrayToBuffer)(id).toString("hex"), "data"));
640
- }
641
- };
642
- _ts_decorate3([
643
- import_async3.synchronized
644
- ], BlobStore.prototype, "getMeta", null);
645
- _ts_decorate3([
646
- import_async3.synchronized
647
- ], BlobStore.prototype, "get", null);
648
- _ts_decorate3([
649
- import_async3.synchronized
650
- ], BlobStore.prototype, "list", null);
651
- _ts_decorate3([
652
- import_async3.synchronized
653
- ], BlobStore.prototype, "set", null);
654
- _ts_decorate3([
655
- import_async3.synchronized
656
- ], BlobStore.prototype, "setChunk", null);
657
- // Annotate the CommonJS export names for ESM import in node:
658
- 0 && (module.exports = {
659
- BlobStore,
660
- BlobSync,
661
- BlobSyncExtension,
662
- DEFAULT_CHUNK_SIZE
663
- });
664
- //# sourceMappingURL=index.cjs.map