@tinycloud/sdk-services 2.3.0 → 2.4.0-beta.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/index.cjs CHANGED
@@ -1163,6 +1163,38 @@ var KVService = class extends BaseService {
1163
1163
  signal: this.combineSignals(signal)
1164
1164
  });
1165
1165
  }
1166
+ /**
1167
+ * Serialize a single put value into a fetch body.
1168
+ *
1169
+ * Binary values (Blob/ArrayBuffer/typed-array, incl. Node Buffer) are sent as
1170
+ * raw bytes (as a Blob) so they round-trip byte-identically — without this a
1171
+ * Buffer would be JSON.stringify'd into `{"type":"Buffer","data":[...]}`.
1172
+ * Strings are returned unchanged (preserving prior behavior); other values are
1173
+ * JSON-encoded. `contentType` overrides the inferred type for binary values.
1174
+ */
1175
+ serializePutValue(value, contentType) {
1176
+ if (value instanceof Blob) {
1177
+ if (!contentType || value.type === contentType) {
1178
+ return value;
1179
+ }
1180
+ return new Blob([value], { type: contentType });
1181
+ }
1182
+ if (value instanceof ArrayBuffer) {
1183
+ return new Blob([value], {
1184
+ type: contentType ?? "application/octet-stream"
1185
+ });
1186
+ }
1187
+ if (ArrayBuffer.isView(value)) {
1188
+ const view = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
1189
+ return new Blob([view], {
1190
+ type: contentType ?? "application/octet-stream"
1191
+ });
1192
+ }
1193
+ if (typeof value === "string") {
1194
+ return contentType ? new Blob([value], { type: contentType }) : value;
1195
+ }
1196
+ return JSON.stringify(value);
1197
+ }
1166
1198
  serializeBatchPutValue(item) {
1167
1199
  const contentType = item.contentType;
1168
1200
  if (item.value instanceof Blob) {
@@ -1232,10 +1264,13 @@ var KVService = class extends BaseService {
1232
1264
  * @param raw - Whether to return raw text
1233
1265
  * @returns Parsed data
1234
1266
  */
1235
- async parseResponse(response, raw = false) {
1267
+ async parseResponse(response, raw = false, binary = false) {
1236
1268
  if (!response.ok) {
1237
1269
  return void 0;
1238
1270
  }
1271
+ if (binary) {
1272
+ return new Uint8Array(await response.arrayBuffer());
1273
+ }
1239
1274
  if (raw) {
1240
1275
  return await response.text();
1241
1276
  }
@@ -1342,7 +1377,11 @@ var KVService = class extends BaseService {
1342
1377
  )
1343
1378
  );
1344
1379
  }
1345
- const data = await this.parseResponse(response, options?.raw);
1380
+ const data = await this.parseResponse(
1381
+ response,
1382
+ options?.raw,
1383
+ options?.binary
1384
+ );
1346
1385
  return ok({
1347
1386
  data,
1348
1387
  headers: this.createResponseHeaders(response.headers)
@@ -1361,12 +1400,7 @@ var KVService = class extends BaseService {
1361
1400
  return err(authRequiredError("kv"));
1362
1401
  }
1363
1402
  const path = this.getFullPath(key, options?.prefix);
1364
- let body;
1365
- if (typeof value === "string") {
1366
- body = value;
1367
- } else {
1368
- body = JSON.stringify(value);
1369
- }
1403
+ const body = this.serializePutValue(value, options?.contentType);
1370
1404
  try {
1371
1405
  const response = await this.invokeOperation(
1372
1406
  path,