@dual-os/sdk 0.9.2 → 0.10.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/src/Http.ts CHANGED
@@ -150,14 +150,17 @@ function sniff(bytes: Uint8Array) {
150
150
  );
151
151
  }
152
152
 
153
+ function fileContentType(bytes: Uint8Array, declared: string | null) {
154
+ return declared === null || pipe(declared, String.startsWith("application/octet-stream"))
155
+ ? (sniff(bytes) ?? declared)
156
+ : declared;
157
+ }
158
+
153
159
  function toolFile(bytes: Uint8Array, declared: string | null, filename: string | null) {
154
160
  return {
155
161
  _tag: "ToolFile",
156
162
  contentBase64: Base64.encode(bytes),
157
- contentType:
158
- declared === null || pipe(declared, String.startsWith("application/octet-stream"))
159
- ? (sniff(bytes) ?? declared)
160
- : declared,
163
+ contentType: fileContentType(bytes, declared),
161
164
  filename,
162
165
  sizeBytes: bytes.byteLength,
163
166
  } satisfies ToolFile.File;
@@ -213,14 +216,30 @@ function textOf(carrier: Carrier, content: Content) {
213
216
 
214
217
  function fileFrom(carrier: Carrier) {
215
218
  return Schema.String.pipe(
216
- Schema.decodeTo(ToolFile.Schema, {
219
+ Schema.decodeTo(Files.File, {
217
220
  decode: SchemaGetter.compose(
218
221
  contentOf(carrier),
219
- SchemaGetter.transform(content => toolFile(content.bytes, content.contentType, null)),
222
+ SchemaGetter.transformEffect((content, options) =>
223
+ pipe(
224
+ Files.write({
225
+ bytes: content.bytes,
226
+ contentType: fileContentType(content.bytes, content.contentType),
227
+ name: "file",
228
+ }),
229
+ Effect.mapError(
230
+ error => new SchemaIssue.InvalidValue({ expected: error.message }, content, options),
231
+ ),
232
+ ),
233
+ ),
220
234
  ),
221
- encode: SchemaGetter.transformEffect((file: ToolFile.File, options) =>
222
- Effect.map(decodeBytes(file.contentBase64, options), bytes =>
223
- textOf(carrier, { bytes, contentType: file.contentType }),
235
+ encode: SchemaGetter.transformEffect((file: Files.File, options) =>
236
+ pipe(
237
+ Files.read(file),
238
+ Effect.mapBoth({
239
+ onSuccess: bytes => textOf(carrier, { bytes, contentType: file.contentType }),
240
+ onFailure: error =>
241
+ new SchemaIssue.InvalidValue({ expected: error.message }, file, options),
242
+ }),
224
243
  ),
225
244
  ),
226
245
  }),
@@ -228,25 +247,11 @@ function fileFrom(carrier: Carrier) {
228
247
  }
229
248
 
230
249
  function encodedFrom(carrier: Carrier) {
231
- return File.pipe(
232
- Schema.decodeTo(Schema.String, {
233
- decode: SchemaGetter.transform(file =>
234
- textOf(carrier, { bytes: file.contentBase64, contentType: file.contentType ?? null }),
235
- ),
236
- encode: SchemaGetter.compose(
237
- contentOf(carrier),
238
- SchemaGetter.transform(content => ({
239
- contentBase64: content.bytes,
240
- contentType: content.contentType,
241
- })),
242
- ),
243
- }),
244
- );
250
+ return Schema.flip(fileFrom(carrier));
245
251
  }
246
252
 
247
253
  /**
248
- * A file the API returns as a Base64 string inside JSON, decoded into a ToolFile with its content
249
- * type recognized from the bytes.
254
+ * A file the API returns as a Base64 string inside JSON, stored by the host and returned by reference.
250
255
  *
251
256
  * @since 0.0.0
252
257
  * @category schemas
@@ -262,7 +267,7 @@ export const FileFromBase64 = fileFrom("base64");
262
267
  export const FileFromBase64Url = fileFrom("base64url");
263
268
 
264
269
  /**
265
- * A {@link File} input the API reads as a Base64 string inside JSON.
270
+ * A {@link Files.File} input the API reads as a Base64 string inside JSON.
266
271
  *
267
272
  * @since 0.0.0
268
273
  * @category schemas
@@ -783,12 +788,9 @@ const read = Effect.fnUntraced(function* (
783
788
  const bytes = new Uint8Array(yield* response.arrayBuffer);
784
789
  const name = dispositionName(response.headers["content-disposition"]);
785
790
  if (body === "file") {
791
+ const declared = response.headers["content-type"] ?? null;
786
792
  return yield* pipe(
787
- Files.write({
788
- bytes,
789
- contentType: Option.getOrNull(Option.fromUndefinedOr(response.headers["content-type"])),
790
- name: name ?? "file",
791
- }),
793
+ Files.write({ bytes, contentType: fileContentType(bytes, declared), name: name ?? "file" }),
792
794
  Effect.mapError(fileFailed),
793
795
  );
794
796
  }
@@ -244,6 +244,7 @@ const FileFailure = Schema.Struct({ message: Schema.String });
244
244
 
245
245
  // The host's file routes do the reading and storing, as the caller the token names.
246
246
  function filesService(access: FileAccess, http: HttpClient.HttpClient) {
247
+ let writes = 0;
247
248
  const client = HttpClient.mapRequest(
248
249
  http,
249
250
  flow(
@@ -281,12 +282,16 @@ function filesService(access: FileAccess, http: HttpClient.HttpClient) {
281
282
  Effect.map(response.arrayBuffer, buffer => new Uint8Array(buffer)),
282
283
  ),
283
284
  write: file =>
284
- call(
285
- pipe(
286
- HttpClientRequest.post("", { urlParams: { name: file.name } }),
287
- HttpClientRequest.bodyUint8Array(file.bytes, file.contentType ?? undefined),
285
+ Effect.suspend(() =>
286
+ call(
287
+ pipe(
288
+ HttpClientRequest.post("", {
289
+ urlParams: { name: file.name, writeIndex: `${writes++}` },
290
+ }),
291
+ HttpClientRequest.bodyUint8Array(file.bytes, file.contentType ?? undefined),
292
+ ),
293
+ response => Effect.flatMap(response.json, Schema.decodeUnknownEffect(Files.File)),
288
294
  ),
289
- response => Effect.flatMap(response.json, Schema.decodeUnknownEffect(Files.File)),
290
295
  ),
291
296
  });
292
297
  }