@calimero-network/mero-js 33.1.0 → 33.2.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/client/calimero-client.d.ts +23 -0
- package/dist/client/calimero-client.js +111 -1
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.js +1 -1
- package/dist/index.browser.mjs +3 -3
- package/dist/index.browser.mjs.map +3 -3
- package/dist/index.cjs +97 -0
- package/dist/index.cjs.map +2 -2
- package/dist/index.mjs +97 -0
- package/dist/index.mjs.map +2 -2
- package/dist/runtime/transport.d.ts +4 -1
- package/dist/runtime/transport.js +4 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -48,6 +48,7 @@ __export(index_exports, {
|
|
|
48
48
|
DEFAULT_RELEASE_MIRROR: () => DEFAULT_RELEASE_MIRROR,
|
|
49
49
|
DEFAULT_RETRY: () => DEFAULT_RETRY,
|
|
50
50
|
DirectoryError: () => DirectoryError,
|
|
51
|
+
FILE_PART_BYTES: () => FILE_PART_BYTES,
|
|
51
52
|
FeatureNotActive: () => FeatureNotActive,
|
|
52
53
|
GITHUB_RELEASES: () => GITHUB_RELEASES,
|
|
53
54
|
HANDSHAKE_PATH: () => HANDSHAKE_PATH,
|
|
@@ -1971,6 +1972,10 @@ var NodeTransport = class {
|
|
|
1971
1972
|
};
|
|
1972
1973
|
this.raw = raw;
|
|
1973
1974
|
}
|
|
1975
|
+
/** Whether `error` is a `fetch` that got no answer: the node could not be reached or dropped the connection. */
|
|
1976
|
+
isNetworkFailure(error) {
|
|
1977
|
+
return error instanceof TypeError && this.networkFailures.has(error);
|
|
1978
|
+
}
|
|
1974
1979
|
/** Re-read the node list from `source` when it is due, and after a failover. */
|
|
1975
1980
|
useDirectory(source) {
|
|
1976
1981
|
this.source = source;
|
|
@@ -4431,6 +4436,11 @@ function upgradeConsent(app, toVersion, returnTo) {
|
|
|
4431
4436
|
}
|
|
4432
4437
|
|
|
4433
4438
|
// src/client/calimero-client.ts
|
|
4439
|
+
var FILE_PART_BYTES = 16 * 1024 * 1024;
|
|
4440
|
+
var PART_RESENDS = 1;
|
|
4441
|
+
var MAX_FAILURES = 8;
|
|
4442
|
+
var MAX_JUMPS = 16;
|
|
4443
|
+
var CALL_RETRIES = 8;
|
|
4434
4444
|
function deployerKey(deployer) {
|
|
4435
4445
|
return "account" in deployer ? `account:${deployer.account}` : `org:${deployer.org}`;
|
|
4436
4446
|
}
|
|
@@ -4461,6 +4471,15 @@ function toHex(bytes) {
|
|
|
4461
4471
|
function presenceState(state) {
|
|
4462
4472
|
return state === null ? null : toHex(state);
|
|
4463
4473
|
}
|
|
4474
|
+
async function readSlice(data, start, end) {
|
|
4475
|
+
if (data instanceof Blob) return new Uint8Array(await data.slice(start, end).arrayBuffer());
|
|
4476
|
+
return data instanceof ArrayBuffer ? new Uint8Array(data, start, end - start) : data.subarray(start, end);
|
|
4477
|
+
}
|
|
4478
|
+
function nextPart(answer, parts) {
|
|
4479
|
+
const next = answer?.next;
|
|
4480
|
+
if (typeof next === "number" && Number.isInteger(next) && next >= 0 && next <= parts) return next;
|
|
4481
|
+
throw new Error(`the node answered a bad next: ${JSON.stringify(answer)}`);
|
|
4482
|
+
}
|
|
4464
4483
|
var CalimeroClient = class _CalimeroClient {
|
|
4465
4484
|
/**
|
|
4466
4485
|
* A client for the Calimero Cloud cluster named `cluster`: its node list read
|
|
@@ -4697,6 +4716,84 @@ var CalimeroClient = class _CalimeroClient {
|
|
|
4697
4716
|
async downloadBlobGranted(app, id, grant) {
|
|
4698
4717
|
return new Uint8Array(await (await this.apps.blobDownload({ app, id, grant })).arrayBuffer());
|
|
4699
4718
|
}
|
|
4719
|
+
/**
|
|
4720
|
+
* Upload a sealed file for component app `app`; answers `{ id, blobId, size, sha256, index }`.
|
|
4721
|
+
* Up to {@link FILE_PART_BYTES} it is one request, sent once (the node mints a new
|
|
4722
|
+
* file for each, so one that may have reached it is never repeated); a larger file
|
|
4723
|
+
* is a session of parts, each sent again after a dropped connection or a `503`.
|
|
4724
|
+
* A session the node lost is begun again once; any other failure aborts it.
|
|
4725
|
+
* `413 TOO_LARGE` and `429 QUOTA_EXCEEDED` are the node's refusals.
|
|
4726
|
+
*/
|
|
4727
|
+
async uploadFile(app, data) {
|
|
4728
|
+
const size = data instanceof Blob ? data.size : data.byteLength;
|
|
4729
|
+
if (size <= FILE_PART_BYTES) return this.apps.filesUpload({ app, body: await readSlice(data, 0, size) });
|
|
4730
|
+
try {
|
|
4731
|
+
return await this.uploadSession(app, data, size);
|
|
4732
|
+
} catch (error) {
|
|
4733
|
+
if (!(error instanceof CalimeroError) || error.code !== "UPLOAD_UNKNOWN") throw error;
|
|
4734
|
+
return this.uploadSession(app, data, size);
|
|
4735
|
+
}
|
|
4736
|
+
}
|
|
4737
|
+
/** Begin a session, send its parts and complete it, aborting it if that fails for good. */
|
|
4738
|
+
async uploadSession(app, data, size) {
|
|
4739
|
+
const begin = await this.untilAvailable(() => this.apps.uploadBegin({ app, size }));
|
|
4740
|
+
const { uploadId: upload, partBytes, parts } = begin;
|
|
4741
|
+
if (!upload || !partBytes || partBytes <= 0 || parts !== Math.ceil(size / partBytes)) {
|
|
4742
|
+
throw new Error(`the node began an upload it cannot take: ${JSON.stringify(begin)}`);
|
|
4743
|
+
}
|
|
4744
|
+
try {
|
|
4745
|
+
await this.sendParts(app, upload, data, size, partBytes, parts);
|
|
4746
|
+
return await this.untilAvailable(() => this.apps.uploadComplete({ app, upload }));
|
|
4747
|
+
} catch (error) {
|
|
4748
|
+
if (!(error instanceof CalimeroError && error.code === "UPLOAD_UNKNOWN")) await this.apps.uploadAbort({ app, upload }).catch(() => void 0);
|
|
4749
|
+
throw error;
|
|
4750
|
+
}
|
|
4751
|
+
}
|
|
4752
|
+
/** Send every part, in order, from wherever the node says the session is. */
|
|
4753
|
+
async sendParts(app, upload, data, size, partBytes, parts) {
|
|
4754
|
+
let [next, failures, jumps] = [0, 0, 0];
|
|
4755
|
+
while (next < parts) {
|
|
4756
|
+
try {
|
|
4757
|
+
const body = await readSlice(data, next * partBytes, Math.min((next + 1) * partBytes, size));
|
|
4758
|
+
next = nextPart(await this.apps.uploadPart({ app, upload, k: String(next), body }), parts);
|
|
4759
|
+
failures = 0;
|
|
4760
|
+
continue;
|
|
4761
|
+
} catch (error) {
|
|
4762
|
+
if (error instanceof CalimeroError && error.code === "PART_OUT_OF_ORDER") {
|
|
4763
|
+
jumps += 1;
|
|
4764
|
+
if (jumps > MAX_JUMPS) throw error;
|
|
4765
|
+
next = nextPart(error.details, parts);
|
|
4766
|
+
continue;
|
|
4767
|
+
}
|
|
4768
|
+
if (!this.isTransient(error) || ++failures > MAX_FAILURES) throw error;
|
|
4769
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelayMs(failures - 1, this.transport.retry)));
|
|
4770
|
+
if (failures > PART_RESENDS) {
|
|
4771
|
+
try {
|
|
4772
|
+
const at = nextPart(await this.apps.uploadStatus({ app, upload }), parts);
|
|
4773
|
+
if (at > next) failures = 0;
|
|
4774
|
+
next = at;
|
|
4775
|
+
} catch (asked) {
|
|
4776
|
+
if (!this.isTransient(asked)) throw asked;
|
|
4777
|
+
}
|
|
4778
|
+
}
|
|
4779
|
+
}
|
|
4780
|
+
}
|
|
4781
|
+
}
|
|
4782
|
+
/** `send` again, after a backoff, while the node is unavailable or cannot be reached. */
|
|
4783
|
+
async untilAvailable(send) {
|
|
4784
|
+
for (let retry = 0; ; retry++) {
|
|
4785
|
+
try {
|
|
4786
|
+
return await send();
|
|
4787
|
+
} catch (error) {
|
|
4788
|
+
if (!this.isTransient(error) || retry >= CALL_RETRIES) throw error;
|
|
4789
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelayMs(retry, this.transport.retry)));
|
|
4790
|
+
}
|
|
4791
|
+
}
|
|
4792
|
+
}
|
|
4793
|
+
/** Whether the same request may succeed if sent again: the node answered `503`, or gave no answer. */
|
|
4794
|
+
isTransient(error) {
|
|
4795
|
+
return error instanceof UnavailableError || this.transport.isNetworkFailure(error);
|
|
4796
|
+
}
|
|
4700
4797
|
// ---------------------------------------------------------------- packages
|
|
4701
4798
|
// ---------------------------------------------------------------- app instances
|
|
4702
4799
|
/** Committed events from index `from`, then live, until `signal` aborts. */
|