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