@tinycloud/sdk-services 3.1.0-beta.13 → 3.1.0-beta.14
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/index.cjs +47 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +47 -8
- package/dist/index.js.map +1 -1
- package/dist/kv/index.cjs +47 -8
- package/dist/kv/index.cjs.map +1 -1
- package/dist/kv/index.d.cts +10 -0
- package/dist/kv/index.d.ts +10 -0
- package/dist/kv/index.js +47 -8
- package/dist/kv/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1713,6 +1713,18 @@ var MAX_KV_BATCH_READ_ITEMS = 100;
|
|
|
1713
1713
|
function isJsonObject(value) {
|
|
1714
1714
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1715
1715
|
}
|
|
1716
|
+
var pathByteEncoder = new TextEncoder();
|
|
1717
|
+
function comparePathsByBytes(a, b) {
|
|
1718
|
+
const left = pathByteEncoder.encode(a);
|
|
1719
|
+
const right = pathByteEncoder.encode(b);
|
|
1720
|
+
const length = Math.min(left.length, right.length);
|
|
1721
|
+
for (let index = 0; index < length; index++) {
|
|
1722
|
+
if (left[index] !== right[index]) {
|
|
1723
|
+
return left[index] - right[index];
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
return left.length - right.length;
|
|
1727
|
+
}
|
|
1716
1728
|
var REVOKED_BODY = /^(?:Invalid invocation: )?delegation-(?:(revoked): [A-Za-z0-9]+|(ancestor-revoked): ancestor=[A-Za-z0-9]+ invoked=[A-Za-z0-9]+)$/;
|
|
1717
1729
|
function revocationCodeOf(body) {
|
|
1718
1730
|
const match = REVOKED_BODY.exec(body.trim());
|
|
@@ -1946,15 +1958,25 @@ var KVService = class extends BaseService {
|
|
|
1946
1958
|
)?.[1];
|
|
1947
1959
|
return contentType?.includes("application/json") ? JSON.parse(text) : text;
|
|
1948
1960
|
}
|
|
1961
|
+
/**
|
|
1962
|
+
* Validate a batch read response and return its items in the caller's
|
|
1963
|
+
* request order. The node flattens invocation abilities from a `BTreeMap`,
|
|
1964
|
+
* so results arrive sorted by UTF-8 byte order of the path, not in request
|
|
1965
|
+
* order. Items that carry their path in `key` are matched by path; if no
|
|
1966
|
+
* item carries a key, items are assumed to be in the node's byte-sorted
|
|
1967
|
+
* order and the request is aligned the same way. Any mix of keyed and
|
|
1968
|
+
* keyless items, a missing requested path, an unrequested path, or a
|
|
1969
|
+
* malformed item fails closed (returns undefined).
|
|
1970
|
+
*/
|
|
1949
1971
|
normalizeBatchReadResponse(data, paths, requireData) {
|
|
1950
1972
|
if (!data || typeof data !== "object") return void 0;
|
|
1951
1973
|
const response = data;
|
|
1952
|
-
|
|
1974
|
+
const items = response.results;
|
|
1975
|
+
if (!Array.isArray(items) || items.length !== paths.length) {
|
|
1953
1976
|
return void 0;
|
|
1954
1977
|
}
|
|
1955
|
-
for (
|
|
1956
|
-
|
|
1957
|
-
if (!item || typeof item !== "object" || item.key !== paths[index] || typeof item.ok !== "boolean") {
|
|
1978
|
+
for (const item of items) {
|
|
1979
|
+
if (!item || typeof item !== "object" || typeof item.ok !== "boolean") {
|
|
1958
1980
|
return void 0;
|
|
1959
1981
|
}
|
|
1960
1982
|
if (item.ok && (!item.headers || typeof item.headers !== "object" || Object.values(item.headers).some((value) => typeof value !== "string") || requireData && typeof item.dataBase64 !== "string")) {
|
|
@@ -1964,7 +1986,24 @@ var KVService = class extends BaseService {
|
|
|
1964
1986
|
return void 0;
|
|
1965
1987
|
}
|
|
1966
1988
|
}
|
|
1967
|
-
|
|
1989
|
+
if (items.every((item) => typeof item.key === "string")) {
|
|
1990
|
+
const byPath = new Map(items.map((item) => [item.key, item]));
|
|
1991
|
+
if (byPath.size !== items.length) return void 0;
|
|
1992
|
+
const aligned2 = [];
|
|
1993
|
+
for (const path of paths) {
|
|
1994
|
+
const item = byPath.get(path);
|
|
1995
|
+
if (!item) return void 0;
|
|
1996
|
+
aligned2.push(item);
|
|
1997
|
+
}
|
|
1998
|
+
return aligned2;
|
|
1999
|
+
}
|
|
2000
|
+
if (items.some((item) => item.key !== void 0)) return void 0;
|
|
2001
|
+
const requestOrder = paths.map((_, index) => index).sort((a, b) => comparePathsByBytes(paths[a], paths[b]));
|
|
2002
|
+
const aligned = new Array(items.length);
|
|
2003
|
+
for (let position = 0; position < requestOrder.length; position++) {
|
|
2004
|
+
aligned[requestOrder[position]] = items[position];
|
|
2005
|
+
}
|
|
2006
|
+
return aligned;
|
|
1968
2007
|
}
|
|
1969
2008
|
async batchRead(keys, action, options) {
|
|
1970
2009
|
if (!this.requireAuth()) return err(authRequiredError("kv"));
|
|
@@ -2057,19 +2096,19 @@ var KVService = class extends BaseService {
|
|
|
2057
2096
|
{ meta: { status: response.status, statusText: response.statusText } }
|
|
2058
2097
|
));
|
|
2059
2098
|
}
|
|
2060
|
-
const
|
|
2099
|
+
const items = this.normalizeBatchReadResponse(
|
|
2061
2100
|
await response.json(),
|
|
2062
2101
|
paths,
|
|
2063
2102
|
action === KVAction.GET
|
|
2064
2103
|
);
|
|
2065
|
-
if (!
|
|
2104
|
+
if (!items) {
|
|
2066
2105
|
return err(serviceError(
|
|
2067
2106
|
ErrorCodes.NETWORK_ERROR,
|
|
2068
2107
|
"KV batch read response did not match the requested keys",
|
|
2069
2108
|
"kv"
|
|
2070
2109
|
));
|
|
2071
2110
|
}
|
|
2072
|
-
const results =
|
|
2111
|
+
const results = items.map((item, index) => {
|
|
2073
2112
|
if (!item.ok) {
|
|
2074
2113
|
return {
|
|
2075
2114
|
key: keys[index],
|