@mdbase-dev/sdk 0.3.0 → 0.3.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.
- package/dist/application/worker.js +82 -20
- package/dist/browser/mdbase-sdk.js +83 -21
- package/dist/browser/mdbase-sdk.min.js +45 -45
- package/dist/files.d.ts +4 -1
- package/dist/files.js +89 -28
- package/dist/files.js.map +1 -1
- package/dist/native/provenance.json +1 -1
- package/package.json +1 -1
|
@@ -18508,6 +18508,19 @@ async function withRequestDeadline(timeoutMs, caller, operation) {
|
|
|
18508
18508
|
|
|
18509
18509
|
// src/files.ts
|
|
18510
18510
|
var REGION_BUSY_WAIT_MS = 9e4;
|
|
18511
|
+
var UPLOAD_STALL_MS = 12e4;
|
|
18512
|
+
function waitForUpload(pending, signal) {
|
|
18513
|
+
if (!signal) return pending;
|
|
18514
|
+
return new Promise((resolve, reject) => {
|
|
18515
|
+
const stop = () => reject(mdbaseError("cancelled", "upload aborted"));
|
|
18516
|
+
if (signal.aborted) {
|
|
18517
|
+
stop();
|
|
18518
|
+
return;
|
|
18519
|
+
}
|
|
18520
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
18521
|
+
pending.then(resolve, reject).finally(() => signal.removeEventListener("abort", stop));
|
|
18522
|
+
});
|
|
18523
|
+
}
|
|
18511
18524
|
function regionBusy(e, until) {
|
|
18512
18525
|
return isMdbaseError(e, "unavailable") && e.reason === "hosted_chunk_busy" && Date.now() < until;
|
|
18513
18526
|
}
|
|
@@ -18760,47 +18773,94 @@ var FilesApi = class {
|
|
|
18760
18773
|
* Upload a file. Resumable: on a dropped link it reconnects and sends only the chunks
|
|
18761
18774
|
* the replica lacks. Returns the `file_put` write: `confirmed` means the bytes are
|
|
18762
18775
|
* durable in the collection. Concurrent calls run in FIFO order on the
|
|
18763
|
-
* session's single upload lane
|
|
18776
|
+
* session's single upload lane. A queued caller can cancel immediately. Two
|
|
18777
|
+
* minutes without progress ends the active transfer with `upload_stalled`.
|
|
18778
|
+
* Confirmation runs outside the lane.
|
|
18764
18779
|
*/
|
|
18765
18780
|
async upload(path, source, o = {}) {
|
|
18766
18781
|
const options = { ...o };
|
|
18767
|
-
const
|
|
18768
|
-
|
|
18782
|
+
const previous = this.uploading;
|
|
18783
|
+
const upload = waitForUpload(previous, options.signal).then(() => this.uploadQueued(path, source, options));
|
|
18784
|
+
const settled = upload.then(() => {
|
|
18769
18785
|
}, () => {
|
|
18770
18786
|
});
|
|
18787
|
+
this.uploading = previous.then(() => settled);
|
|
18771
18788
|
return upload;
|
|
18772
18789
|
}
|
|
18773
18790
|
async uploadQueued(path, source, o) {
|
|
18774
18791
|
gates.get(this)?.check(o.fileId ? "replace" : "add");
|
|
18775
18792
|
if (o.signal?.aborted) throw mdbaseError("cancelled", "upload aborted");
|
|
18793
|
+
const stalled = new AbortController();
|
|
18794
|
+
const signal = o.signal ? AbortSignal.any([o.signal, stalled.signal]) : stalled.signal;
|
|
18795
|
+
let timer;
|
|
18796
|
+
let active = true;
|
|
18797
|
+
const progress2 = () => {
|
|
18798
|
+
if (!active || signal.aborted) return;
|
|
18799
|
+
clearTimeout(timer);
|
|
18800
|
+
timer = setTimeout(() => stalled.abort(), UPLOAD_STALL_MS);
|
|
18801
|
+
};
|
|
18802
|
+
const mutationId = o.mutationId ?? uuidv7();
|
|
18803
|
+
let commitSent = false;
|
|
18804
|
+
progress2();
|
|
18805
|
+
try {
|
|
18806
|
+
return await this.transfer(path, source, { ...o, signal, mutationId }, progress2, () => {
|
|
18807
|
+
commitSent = true;
|
|
18808
|
+
});
|
|
18809
|
+
} catch (error4) {
|
|
18810
|
+
if (signal.aborted && signal.reason === stalled.signal.reason && isMdbaseError(error4, "cancelled")) {
|
|
18811
|
+
throw withMutation(mdbaseError(
|
|
18812
|
+
commitSent ? "outcome_unknown" : "unavailable",
|
|
18813
|
+
"File upload made no progress for two minutes",
|
|
18814
|
+
"upload_stalled"
|
|
18815
|
+
), "upload", { mutationId });
|
|
18816
|
+
}
|
|
18817
|
+
throw error4;
|
|
18818
|
+
} finally {
|
|
18819
|
+
active = false;
|
|
18820
|
+
clearTimeout(timer);
|
|
18821
|
+
}
|
|
18822
|
+
}
|
|
18823
|
+
async transfer(path, source, o, progress2, committing) {
|
|
18776
18824
|
const size2 = sizeOf(source);
|
|
18777
18825
|
const h = sha256.create();
|
|
18778
18826
|
const step = 4 * 1024 * 1024;
|
|
18779
|
-
for (let off = 0; off < size2; off += step)
|
|
18827
|
+
for (let off = 0; off < size2; off += step) {
|
|
18828
|
+
h.update(await waitForUpload(sliceBytes(source, off, Math.min(size2, off + step)), o.signal));
|
|
18829
|
+
progress2();
|
|
18830
|
+
}
|
|
18780
18831
|
const digest2 = hashFromBytes(h.digest());
|
|
18781
18832
|
const transfer = o.transferId ?? uuidv7();
|
|
18782
|
-
const open2 = { transfer, path, size: size2, digest: digest2, mutationId: o.mutationId
|
|
18833
|
+
const open2 = { transfer, path, size: size2, digest: digest2, mutationId: o.mutationId };
|
|
18783
18834
|
if (o.fileId) open2.fileId = o.fileId;
|
|
18784
18835
|
if (o.ifRevision) open2.ifRevision = o.ifRevision;
|
|
18785
|
-
|
|
18836
|
+
const observed = /* @__PURE__ */ new Map();
|
|
18837
|
+
const onProgress = (p) => {
|
|
18838
|
+
if (p.done > (observed.get(p.phase) ?? -1)) {
|
|
18839
|
+
observed.set(p.phase, p.done);
|
|
18840
|
+
progress2();
|
|
18841
|
+
}
|
|
18842
|
+
o.onProgress?.(p);
|
|
18843
|
+
};
|
|
18844
|
+
this.progress.set(transfer, onProgress);
|
|
18786
18845
|
const busyUntil = Date.now() + REGION_BUSY_WAIT_MS;
|
|
18787
|
-
|
|
18788
|
-
|
|
18789
|
-
|
|
18790
|
-
|
|
18791
|
-
|
|
18792
|
-
|
|
18846
|
+
try {
|
|
18847
|
+
for (; ; ) {
|
|
18848
|
+
try {
|
|
18849
|
+
return await this.uploadOnce(source, size2, open2, o, onProgress, committing);
|
|
18850
|
+
} catch (e) {
|
|
18851
|
+
if (!o.signal.aborted && isMdbaseError(e, "unavailable") && this.client.link === "reconnecting") continue;
|
|
18852
|
+
if (!regionBusy(e, busyUntil)) throw e;
|
|
18793
18853
|
await regionPause(e, "upload", o.signal);
|
|
18794
|
-
continue;
|
|
18795
18854
|
}
|
|
18796
|
-
this.progress.delete(transfer);
|
|
18797
|
-
await this.releaseUpload(transfer, AbortSignal.timeout(1e3)).catch(() => {
|
|
18798
|
-
});
|
|
18799
|
-
throw withMutation(e, "upload", { mutationId: open2.mutationId });
|
|
18800
18855
|
}
|
|
18856
|
+
} catch (e) {
|
|
18857
|
+
this.progress.delete(transfer);
|
|
18858
|
+
await this.releaseUpload(transfer, AbortSignal.timeout(1e3)).catch(() => {
|
|
18859
|
+
});
|
|
18860
|
+
throw withMutation(e, "upload", { mutationId: open2.mutationId });
|
|
18801
18861
|
}
|
|
18802
18862
|
}
|
|
18803
|
-
async uploadOnce(source, size2, open2, o) {
|
|
18863
|
+
async uploadOnce(source, size2, open2, o, onProgress, committing) {
|
|
18804
18864
|
const s = await this.session(o.signal);
|
|
18805
18865
|
const sig = o.signal ? { signal: o.signal } : {};
|
|
18806
18866
|
const opened = await s.request("open_upload", openUploadParams.enc(open2), { codec: openUploadResult, ...sig });
|
|
@@ -18822,10 +18882,10 @@ var FilesApi = class {
|
|
|
18822
18882
|
for (let i = 0; i < chunks; i++) {
|
|
18823
18883
|
if (have.has(i)) continue;
|
|
18824
18884
|
const start = i * opened.chunkSize;
|
|
18825
|
-
const bytes10 = await sliceBytes(source, start, Math.min(size2, start + opened.chunkSize));
|
|
18885
|
+
const bytes10 = await waitForUpload(sliceBytes(source, start, Math.min(size2, start + opened.chunkSize)), o.signal);
|
|
18826
18886
|
const p = s.request("upload_chunk", uploadChunkParams.enc({ transfer: open2.transfer, index: i, bytes: bytes10 }), sig).then(() => {
|
|
18827
18887
|
done += bytes10.length;
|
|
18828
|
-
|
|
18888
|
+
onProgress({ id: open2.transfer, phase: "receiving", done: Math.min(done, size2), total: size2 });
|
|
18829
18889
|
});
|
|
18830
18890
|
inflight.add(p);
|
|
18831
18891
|
void p.finally(() => inflight.delete(p)).catch(() => {
|
|
@@ -18833,12 +18893,14 @@ var FilesApi = class {
|
|
|
18833
18893
|
if (inflight.size >= 4) await Promise.race(inflight);
|
|
18834
18894
|
}
|
|
18835
18895
|
await Promise.all(inflight);
|
|
18896
|
+
committing();
|
|
18836
18897
|
const r = await s.request("commit_upload", m([[0, uuid.enc(open2.transfer)]]), {
|
|
18837
18898
|
codec: plainAnswer(receipt),
|
|
18838
18899
|
...sig
|
|
18839
18900
|
});
|
|
18840
18901
|
const w = Object.assign(this.client._track(r), { fileId: opened.fileId });
|
|
18841
18902
|
if (o.onProgress) {
|
|
18903
|
+
this.progress.set(open2.transfer, o.onProgress);
|
|
18842
18904
|
void w.confirmed.finally(() => this.progress.delete(open2.transfer)).catch(() => {
|
|
18843
18905
|
});
|
|
18844
18906
|
} else {
|
|
@@ -4050,6 +4050,18 @@ var MdbaseSDK = (() => {
|
|
|
4050
4050
|
});
|
|
4051
4051
|
|
|
4052
4052
|
// src/files.ts
|
|
4053
|
+
function waitForUpload(pending, signal) {
|
|
4054
|
+
if (!signal) return pending;
|
|
4055
|
+
return new Promise((resolve2, reject) => {
|
|
4056
|
+
const stop = () => reject(mdbaseError("cancelled", "upload aborted"));
|
|
4057
|
+
if (signal.aborted) {
|
|
4058
|
+
stop();
|
|
4059
|
+
return;
|
|
4060
|
+
}
|
|
4061
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
4062
|
+
pending.then(resolve2, reject).finally(() => signal.removeEventListener("abort", stop));
|
|
4063
|
+
});
|
|
4064
|
+
}
|
|
4053
4065
|
function regionBusy(e, until) {
|
|
4054
4066
|
return isMdbaseError(e, "unavailable") && e.reason === "hosted_chunk_busy" && Date.now() < until;
|
|
4055
4067
|
}
|
|
@@ -4104,7 +4116,7 @@ var MdbaseSDK = (() => {
|
|
|
4104
4116
|
if (src instanceof Uint8Array || src instanceof ArrayBuffer) return src.byteLength;
|
|
4105
4117
|
return src.size;
|
|
4106
4118
|
}
|
|
4107
|
-
var REGION_BUSY_WAIT_MS, MAX_CONVENIENCE_BYTES, EARLY_CHUNK_BYTES, FILE_ACTION_CAPABILITY, gates, FilesApi;
|
|
4119
|
+
var REGION_BUSY_WAIT_MS, UPLOAD_STALL_MS, MAX_CONVENIENCE_BYTES, EARLY_CHUNK_BYTES, FILE_ACTION_CAPABILITY, gates, FilesApi;
|
|
4108
4120
|
var init_files = __esm({
|
|
4109
4121
|
"src/files.ts"() {
|
|
4110
4122
|
"use strict";
|
|
@@ -4116,6 +4128,7 @@ var MdbaseSDK = (() => {
|
|
|
4116
4128
|
init_values();
|
|
4117
4129
|
init_wire();
|
|
4118
4130
|
REGION_BUSY_WAIT_MS = 9e4;
|
|
4131
|
+
UPLOAD_STALL_MS = 12e4;
|
|
4119
4132
|
MAX_CONVENIENCE_BYTES = 64 * 1024 * 1024;
|
|
4120
4133
|
EARLY_CHUNK_BYTES = 8 * 1024 * 1024;
|
|
4121
4134
|
FILE_ACTION_CAPABILITY = [
|
|
@@ -4317,47 +4330,94 @@ var MdbaseSDK = (() => {
|
|
|
4317
4330
|
* Upload a file. Resumable: on a dropped link it reconnects and sends only the chunks
|
|
4318
4331
|
* the replica lacks. Returns the `file_put` write: `confirmed` means the bytes are
|
|
4319
4332
|
* durable in the collection. Concurrent calls run in FIFO order on the
|
|
4320
|
-
* session's single upload lane
|
|
4333
|
+
* session's single upload lane. A queued caller can cancel immediately. Two
|
|
4334
|
+
* minutes without progress ends the active transfer with `upload_stalled`.
|
|
4335
|
+
* Confirmation runs outside the lane.
|
|
4321
4336
|
*/
|
|
4322
4337
|
async upload(path, source, o = {}) {
|
|
4323
4338
|
const options = { ...o };
|
|
4324
|
-
const
|
|
4325
|
-
|
|
4339
|
+
const previous = this.uploading;
|
|
4340
|
+
const upload = waitForUpload(previous, options.signal).then(() => this.uploadQueued(path, source, options));
|
|
4341
|
+
const settled2 = upload.then(() => {
|
|
4326
4342
|
}, () => {
|
|
4327
4343
|
});
|
|
4344
|
+
this.uploading = previous.then(() => settled2);
|
|
4328
4345
|
return upload;
|
|
4329
4346
|
}
|
|
4330
4347
|
async uploadQueued(path, source, o) {
|
|
4331
4348
|
gates.get(this)?.check(o.fileId ? "replace" : "add");
|
|
4332
4349
|
if (o.signal?.aborted) throw mdbaseError("cancelled", "upload aborted");
|
|
4350
|
+
const stalled = new AbortController();
|
|
4351
|
+
const signal = o.signal ? AbortSignal.any([o.signal, stalled.signal]) : stalled.signal;
|
|
4352
|
+
let timer2;
|
|
4353
|
+
let active = true;
|
|
4354
|
+
const progress2 = () => {
|
|
4355
|
+
if (!active || signal.aborted) return;
|
|
4356
|
+
clearTimeout(timer2);
|
|
4357
|
+
timer2 = setTimeout(() => stalled.abort(), UPLOAD_STALL_MS);
|
|
4358
|
+
};
|
|
4359
|
+
const mutationId = o.mutationId ?? uuidv7();
|
|
4360
|
+
let commitSent = false;
|
|
4361
|
+
progress2();
|
|
4362
|
+
try {
|
|
4363
|
+
return await this.transfer(path, source, { ...o, signal, mutationId }, progress2, () => {
|
|
4364
|
+
commitSent = true;
|
|
4365
|
+
});
|
|
4366
|
+
} catch (error5) {
|
|
4367
|
+
if (signal.aborted && signal.reason === stalled.signal.reason && isMdbaseError(error5, "cancelled")) {
|
|
4368
|
+
throw withMutation(mdbaseError(
|
|
4369
|
+
commitSent ? "outcome_unknown" : "unavailable",
|
|
4370
|
+
"File upload made no progress for two minutes",
|
|
4371
|
+
"upload_stalled"
|
|
4372
|
+
), "upload", { mutationId });
|
|
4373
|
+
}
|
|
4374
|
+
throw error5;
|
|
4375
|
+
} finally {
|
|
4376
|
+
active = false;
|
|
4377
|
+
clearTimeout(timer2);
|
|
4378
|
+
}
|
|
4379
|
+
}
|
|
4380
|
+
async transfer(path, source, o, progress2, committing) {
|
|
4333
4381
|
const size2 = sizeOf(source);
|
|
4334
4382
|
const h = sha256.create();
|
|
4335
4383
|
const step = 4 * 1024 * 1024;
|
|
4336
|
-
for (let off = 0; off < size2; off += step)
|
|
4384
|
+
for (let off = 0; off < size2; off += step) {
|
|
4385
|
+
h.update(await waitForUpload(sliceBytes(source, off, Math.min(size2, off + step)), o.signal));
|
|
4386
|
+
progress2();
|
|
4387
|
+
}
|
|
4337
4388
|
const digest2 = hashFromBytes(h.digest());
|
|
4338
4389
|
const transfer = o.transferId ?? uuidv7();
|
|
4339
|
-
const open2 = { transfer, path, size: size2, digest: digest2, mutationId: o.mutationId
|
|
4390
|
+
const open2 = { transfer, path, size: size2, digest: digest2, mutationId: o.mutationId };
|
|
4340
4391
|
if (o.fileId) open2.fileId = o.fileId;
|
|
4341
4392
|
if (o.ifRevision) open2.ifRevision = o.ifRevision;
|
|
4342
|
-
|
|
4393
|
+
const observed = /* @__PURE__ */ new Map();
|
|
4394
|
+
const onProgress = (p) => {
|
|
4395
|
+
if (p.done > (observed.get(p.phase) ?? -1)) {
|
|
4396
|
+
observed.set(p.phase, p.done);
|
|
4397
|
+
progress2();
|
|
4398
|
+
}
|
|
4399
|
+
o.onProgress?.(p);
|
|
4400
|
+
};
|
|
4401
|
+
this.progress.set(transfer, onProgress);
|
|
4343
4402
|
const busyUntil = Date.now() + REGION_BUSY_WAIT_MS;
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4403
|
+
try {
|
|
4404
|
+
for (; ; ) {
|
|
4405
|
+
try {
|
|
4406
|
+
return await this.uploadOnce(source, size2, open2, o, onProgress, committing);
|
|
4407
|
+
} catch (e) {
|
|
4408
|
+
if (!o.signal.aborted && isMdbaseError(e, "unavailable") && this.client.link === "reconnecting") continue;
|
|
4409
|
+
if (!regionBusy(e, busyUntil)) throw e;
|
|
4350
4410
|
await regionPause(e, "upload", o.signal);
|
|
4351
|
-
continue;
|
|
4352
4411
|
}
|
|
4353
|
-
this.progress.delete(transfer);
|
|
4354
|
-
await this.releaseUpload(transfer, AbortSignal.timeout(1e3)).catch(() => {
|
|
4355
|
-
});
|
|
4356
|
-
throw withMutation(e, "upload", { mutationId: open2.mutationId });
|
|
4357
4412
|
}
|
|
4413
|
+
} catch (e) {
|
|
4414
|
+
this.progress.delete(transfer);
|
|
4415
|
+
await this.releaseUpload(transfer, AbortSignal.timeout(1e3)).catch(() => {
|
|
4416
|
+
});
|
|
4417
|
+
throw withMutation(e, "upload", { mutationId: open2.mutationId });
|
|
4358
4418
|
}
|
|
4359
4419
|
}
|
|
4360
|
-
async uploadOnce(source, size2, open2, o) {
|
|
4420
|
+
async uploadOnce(source, size2, open2, o, onProgress, committing) {
|
|
4361
4421
|
const s = await this.session(o.signal);
|
|
4362
4422
|
const sig = o.signal ? { signal: o.signal } : {};
|
|
4363
4423
|
const opened = await s.request("open_upload", openUploadParams.enc(open2), { codec: openUploadResult, ...sig });
|
|
@@ -4379,10 +4439,10 @@ var MdbaseSDK = (() => {
|
|
|
4379
4439
|
for (let i = 0; i < chunks; i++) {
|
|
4380
4440
|
if (have.has(i)) continue;
|
|
4381
4441
|
const start = i * opened.chunkSize;
|
|
4382
|
-
const bytes10 = await sliceBytes(source, start, Math.min(size2, start + opened.chunkSize));
|
|
4442
|
+
const bytes10 = await waitForUpload(sliceBytes(source, start, Math.min(size2, start + opened.chunkSize)), o.signal);
|
|
4383
4443
|
const p = s.request("upload_chunk", uploadChunkParams.enc({ transfer: open2.transfer, index: i, bytes: bytes10 }), sig).then(() => {
|
|
4384
4444
|
done += bytes10.length;
|
|
4385
|
-
|
|
4445
|
+
onProgress({ id: open2.transfer, phase: "receiving", done: Math.min(done, size2), total: size2 });
|
|
4386
4446
|
});
|
|
4387
4447
|
inflight.add(p);
|
|
4388
4448
|
void p.finally(() => inflight.delete(p)).catch(() => {
|
|
@@ -4390,12 +4450,14 @@ var MdbaseSDK = (() => {
|
|
|
4390
4450
|
if (inflight.size >= 4) await Promise.race(inflight);
|
|
4391
4451
|
}
|
|
4392
4452
|
await Promise.all(inflight);
|
|
4453
|
+
committing();
|
|
4393
4454
|
const r = await s.request("commit_upload", m([[0, uuid.enc(open2.transfer)]]), {
|
|
4394
4455
|
codec: plainAnswer(receipt),
|
|
4395
4456
|
...sig
|
|
4396
4457
|
});
|
|
4397
4458
|
const w = Object.assign(this.client._track(r), { fileId: opened.fileId });
|
|
4398
4459
|
if (o.onProgress) {
|
|
4460
|
+
this.progress.set(open2.transfer, o.onProgress);
|
|
4399
4461
|
void w.confirmed.finally(() => this.progress.delete(open2.transfer)).catch(() => {
|
|
4400
4462
|
});
|
|
4401
4463
|
} else {
|