@tinycloud/sdk-services 2.3.0 → 2.4.0-beta.2
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 +70 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +70 -29
- package/dist/index.js.map +1 -1
- package/dist/kv/index.cjs +70 -29
- package/dist/kv/index.cjs.map +1 -1
- package/dist/kv/index.d.cts +28 -0
- package/dist/kv/index.d.ts +28 -0
- package/dist/kv/index.js +70 -29
- package/dist/kv/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -983,6 +983,31 @@ var KVService = class extends BaseService {
|
|
|
983
983
|
}
|
|
984
984
|
return void 0;
|
|
985
985
|
}
|
|
986
|
+
/**
|
|
987
|
+
* Classify a KV 404 by reading the response body once.
|
|
988
|
+
*
|
|
989
|
+
* The server returns 404 both for a genuinely missing key AND for an
|
|
990
|
+
* un-hosted space (body "Space not found"). Previously get/head/delete
|
|
991
|
+
* collapsed every 404 to KV_NOT_FOUND before reading the body, so an
|
|
992
|
+
* un-hosted-space read was indistinguishable from a missing key. We now
|
|
993
|
+
* preserve status + the "Space not found" body for the un-hosted case (so the
|
|
994
|
+
* CLI/SDK can normalize it to SPACE_NOT_HOSTED, matching put/list/sql), and
|
|
995
|
+
* fall through to KV_NOT_FOUND for a real missing key.
|
|
996
|
+
*/
|
|
997
|
+
async classifyNotFound(response, key) {
|
|
998
|
+
const errorText = await response.text();
|
|
999
|
+
if (/space not found/i.test(errorText)) {
|
|
1000
|
+
return err(
|
|
1001
|
+
serviceError(
|
|
1002
|
+
ErrorCodes.KV_NOT_FOUND,
|
|
1003
|
+
`KV ${response.status} - ${errorText}`,
|
|
1004
|
+
"kv",
|
|
1005
|
+
{ meta: { status: response.status, statusText: response.statusText } }
|
|
1006
|
+
)
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
return err(serviceError(ErrorCodes.KV_NOT_FOUND, `Key not found: ${key}`, "kv"));
|
|
1010
|
+
}
|
|
986
1011
|
/**
|
|
987
1012
|
* Get the full path with optional prefix.
|
|
988
1013
|
*
|
|
@@ -1033,6 +1058,38 @@ var KVService = class extends BaseService {
|
|
|
1033
1058
|
signal: this.combineSignals(signal)
|
|
1034
1059
|
});
|
|
1035
1060
|
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Serialize a single put value into a fetch body.
|
|
1063
|
+
*
|
|
1064
|
+
* Binary values (Blob/ArrayBuffer/typed-array, incl. Node Buffer) are sent as
|
|
1065
|
+
* raw bytes (as a Blob) so they round-trip byte-identically — without this a
|
|
1066
|
+
* Buffer would be JSON.stringify'd into `{"type":"Buffer","data":[...]}`.
|
|
1067
|
+
* Strings are returned unchanged (preserving prior behavior); other values are
|
|
1068
|
+
* JSON-encoded. `contentType` overrides the inferred type for binary values.
|
|
1069
|
+
*/
|
|
1070
|
+
serializePutValue(value, contentType) {
|
|
1071
|
+
if (value instanceof Blob) {
|
|
1072
|
+
if (!contentType || value.type === contentType) {
|
|
1073
|
+
return value;
|
|
1074
|
+
}
|
|
1075
|
+
return new Blob([value], { type: contentType });
|
|
1076
|
+
}
|
|
1077
|
+
if (value instanceof ArrayBuffer) {
|
|
1078
|
+
return new Blob([value], {
|
|
1079
|
+
type: contentType ?? "application/octet-stream"
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
if (ArrayBuffer.isView(value)) {
|
|
1083
|
+
const view = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
1084
|
+
return new Blob([view], {
|
|
1085
|
+
type: contentType ?? "application/octet-stream"
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
if (typeof value === "string") {
|
|
1089
|
+
return contentType ? new Blob([value], { type: contentType }) : value;
|
|
1090
|
+
}
|
|
1091
|
+
return JSON.stringify(value);
|
|
1092
|
+
}
|
|
1036
1093
|
serializeBatchPutValue(item) {
|
|
1037
1094
|
const contentType = item.contentType;
|
|
1038
1095
|
if (item.value instanceof Blob) {
|
|
@@ -1102,10 +1159,13 @@ var KVService = class extends BaseService {
|
|
|
1102
1159
|
* @param raw - Whether to return raw text
|
|
1103
1160
|
* @returns Parsed data
|
|
1104
1161
|
*/
|
|
1105
|
-
async parseResponse(response, raw = false) {
|
|
1162
|
+
async parseResponse(response, raw = false, binary = false) {
|
|
1106
1163
|
if (!response.ok) {
|
|
1107
1164
|
return void 0;
|
|
1108
1165
|
}
|
|
1166
|
+
if (binary) {
|
|
1167
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
1168
|
+
}
|
|
1109
1169
|
if (raw) {
|
|
1110
1170
|
return await response.text();
|
|
1111
1171
|
}
|
|
@@ -1194,13 +1254,7 @@ var KVService = class extends BaseService {
|
|
|
1194
1254
|
}));
|
|
1195
1255
|
}
|
|
1196
1256
|
if (response.status === 404) {
|
|
1197
|
-
return
|
|
1198
|
-
serviceError(
|
|
1199
|
-
ErrorCodes.KV_NOT_FOUND,
|
|
1200
|
-
`Key not found: ${key}`,
|
|
1201
|
-
"kv"
|
|
1202
|
-
)
|
|
1203
|
-
);
|
|
1257
|
+
return this.classifyNotFound(response, key);
|
|
1204
1258
|
}
|
|
1205
1259
|
const errorText = await response.text();
|
|
1206
1260
|
return err(
|
|
@@ -1212,7 +1266,11 @@ var KVService = class extends BaseService {
|
|
|
1212
1266
|
)
|
|
1213
1267
|
);
|
|
1214
1268
|
}
|
|
1215
|
-
const data = await this.parseResponse(
|
|
1269
|
+
const data = await this.parseResponse(
|
|
1270
|
+
response,
|
|
1271
|
+
options?.raw,
|
|
1272
|
+
options?.binary
|
|
1273
|
+
);
|
|
1216
1274
|
return ok({
|
|
1217
1275
|
data,
|
|
1218
1276
|
headers: this.createResponseHeaders(response.headers)
|
|
@@ -1231,12 +1289,7 @@ var KVService = class extends BaseService {
|
|
|
1231
1289
|
return err(authRequiredError("kv"));
|
|
1232
1290
|
}
|
|
1233
1291
|
const path = this.getFullPath(key, options?.prefix);
|
|
1234
|
-
|
|
1235
|
-
if (typeof value === "string") {
|
|
1236
|
-
body = value;
|
|
1237
|
-
} else {
|
|
1238
|
-
body = JSON.stringify(value);
|
|
1239
|
-
}
|
|
1292
|
+
const body = this.serializePutValue(value, options?.contentType);
|
|
1240
1293
|
try {
|
|
1241
1294
|
const response = await this.invokeOperation(
|
|
1242
1295
|
path,
|
|
@@ -1462,13 +1515,7 @@ var KVService = class extends BaseService {
|
|
|
1462
1515
|
}));
|
|
1463
1516
|
}
|
|
1464
1517
|
if (response.status === 404) {
|
|
1465
|
-
return
|
|
1466
|
-
serviceError(
|
|
1467
|
-
ErrorCodes.KV_NOT_FOUND,
|
|
1468
|
-
`Key not found: ${key}`,
|
|
1469
|
-
"kv"
|
|
1470
|
-
)
|
|
1471
|
-
);
|
|
1518
|
+
return this.classifyNotFound(response, key);
|
|
1472
1519
|
}
|
|
1473
1520
|
const errorText = await response.text();
|
|
1474
1521
|
return err(
|
|
@@ -1513,13 +1560,7 @@ var KVService = class extends BaseService {
|
|
|
1513
1560
|
}));
|
|
1514
1561
|
}
|
|
1515
1562
|
if (response.status === 404) {
|
|
1516
|
-
return
|
|
1517
|
-
serviceError(
|
|
1518
|
-
ErrorCodes.KV_NOT_FOUND,
|
|
1519
|
-
`Key not found: ${key}`,
|
|
1520
|
-
"kv"
|
|
1521
|
-
)
|
|
1522
|
-
);
|
|
1563
|
+
return this.classifyNotFound(response, key);
|
|
1523
1564
|
}
|
|
1524
1565
|
const errorText = await response.text();
|
|
1525
1566
|
return err(
|