@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.js CHANGED
@@ -1033,6 +1033,38 @@ var KVService = class extends BaseService {
1033
1033
  signal: this.combineSignals(signal)
1034
1034
  });
1035
1035
  }
1036
+ /**
1037
+ * Serialize a single put value into a fetch body.
1038
+ *
1039
+ * Binary values (Blob/ArrayBuffer/typed-array, incl. Node Buffer) are sent as
1040
+ * raw bytes (as a Blob) so they round-trip byte-identically — without this a
1041
+ * Buffer would be JSON.stringify'd into `{"type":"Buffer","data":[...]}`.
1042
+ * Strings are returned unchanged (preserving prior behavior); other values are
1043
+ * JSON-encoded. `contentType` overrides the inferred type for binary values.
1044
+ */
1045
+ serializePutValue(value, contentType) {
1046
+ if (value instanceof Blob) {
1047
+ if (!contentType || value.type === contentType) {
1048
+ return value;
1049
+ }
1050
+ return new Blob([value], { type: contentType });
1051
+ }
1052
+ if (value instanceof ArrayBuffer) {
1053
+ return new Blob([value], {
1054
+ type: contentType ?? "application/octet-stream"
1055
+ });
1056
+ }
1057
+ if (ArrayBuffer.isView(value)) {
1058
+ const view = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
1059
+ return new Blob([view], {
1060
+ type: contentType ?? "application/octet-stream"
1061
+ });
1062
+ }
1063
+ if (typeof value === "string") {
1064
+ return contentType ? new Blob([value], { type: contentType }) : value;
1065
+ }
1066
+ return JSON.stringify(value);
1067
+ }
1036
1068
  serializeBatchPutValue(item) {
1037
1069
  const contentType = item.contentType;
1038
1070
  if (item.value instanceof Blob) {
@@ -1102,10 +1134,13 @@ var KVService = class extends BaseService {
1102
1134
  * @param raw - Whether to return raw text
1103
1135
  * @returns Parsed data
1104
1136
  */
1105
- async parseResponse(response, raw = false) {
1137
+ async parseResponse(response, raw = false, binary = false) {
1106
1138
  if (!response.ok) {
1107
1139
  return void 0;
1108
1140
  }
1141
+ if (binary) {
1142
+ return new Uint8Array(await response.arrayBuffer());
1143
+ }
1109
1144
  if (raw) {
1110
1145
  return await response.text();
1111
1146
  }
@@ -1212,7 +1247,11 @@ var KVService = class extends BaseService {
1212
1247
  )
1213
1248
  );
1214
1249
  }
1215
- const data = await this.parseResponse(response, options?.raw);
1250
+ const data = await this.parseResponse(
1251
+ response,
1252
+ options?.raw,
1253
+ options?.binary
1254
+ );
1216
1255
  return ok({
1217
1256
  data,
1218
1257
  headers: this.createResponseHeaders(response.headers)
@@ -1231,12 +1270,7 @@ var KVService = class extends BaseService {
1231
1270
  return err(authRequiredError("kv"));
1232
1271
  }
1233
1272
  const path = this.getFullPath(key, options?.prefix);
1234
- let body;
1235
- if (typeof value === "string") {
1236
- body = value;
1237
- } else {
1238
- body = JSON.stringify(value);
1239
- }
1273
+ const body = this.serializePutValue(value, options?.contentType);
1240
1274
  try {
1241
1275
  const response = await this.invokeOperation(
1242
1276
  path,