effect-cf 0.41.0 → 0.42.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.
Files changed (67) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +28 -0
  3. package/dist/AiGateway.d.mts +2 -2
  4. package/dist/AnalyticsEngine.d.mts +2 -2
  5. package/dist/Artifacts.d.mts +2 -2
  6. package/dist/BrowserRendering.d.mts +2 -2
  7. package/dist/ContainerNamespace.d.mts +2 -2
  8. package/dist/ContainerNamespace.mjs +23 -20
  9. package/dist/D1.d.mts +9 -12
  10. package/dist/DurableObject.d.mts +5 -3
  11. package/dist/DurableObject.mjs +4 -3
  12. package/dist/DurableObjectDefinition.d.mts +15 -12
  13. package/dist/DurableObjectDefinition.mjs +5 -3
  14. package/dist/DurableObjectNamespace.d.mts +10 -8
  15. package/dist/DurableObjectNamespace.mjs +1 -1
  16. package/dist/Email.d.mts +2 -2
  17. package/dist/Hyperdrive.d.mts +2 -2
  18. package/dist/Images.d.mts +2 -2
  19. package/dist/Kv.d.mts +2 -2
  20. package/dist/Queue.d.mts +5 -4
  21. package/dist/QueueDefinition.d.mts +4 -4
  22. package/dist/R2.d.mts +2 -2
  23. package/dist/RpcDefinition.d.mts +26 -12
  24. package/dist/RpcDefinition.mjs +22 -5
  25. package/dist/RpcTargets.mjs +1 -2
  26. package/dist/Sandbox.d.mts +1 -1
  27. package/dist/Sandbox.mjs +87 -65
  28. package/dist/ServiceBinding.d.mts +17 -18
  29. package/dist/Vectorize.d.mts +2 -2
  30. package/dist/Worker.d.mts +5 -3
  31. package/dist/Worker.mjs +5 -4
  32. package/dist/WorkerDefinition.d.mts +15 -11
  33. package/dist/WorkerDefinition.mjs +5 -3
  34. package/dist/WorkersAi.d.mts +2 -2
  35. package/dist/Workflow.d.mts +5 -4
  36. package/dist/WorkflowDefinition.d.mts +4 -4
  37. package/dist/index.d.mts +2 -2
  38. package/dist/index.mjs +1 -1
  39. package/dist/internal/RpcTargets.mjs +5 -0
  40. package/package.json +1 -1
  41. package/src/AiGateway.ts +2 -0
  42. package/src/AnalyticsEngine.ts +2 -0
  43. package/src/Artifacts.ts +2 -0
  44. package/src/BrowserRendering.ts +2 -0
  45. package/src/ContainerNamespace.ts +34 -39
  46. package/src/D1.ts +16 -8
  47. package/src/DurableObject.ts +14 -1
  48. package/src/DurableObjectDefinition.ts +33 -17
  49. package/src/DurableObjectNamespace.ts +19 -35
  50. package/src/Email.ts +2 -0
  51. package/src/Hyperdrive.ts +2 -0
  52. package/src/Images.ts +2 -0
  53. package/src/Kv.ts +2 -0
  54. package/src/Queue.ts +4 -7
  55. package/src/QueueDefinition.ts +7 -4
  56. package/src/R2.ts +2 -0
  57. package/src/RpcDefinition.ts +64 -25
  58. package/src/RpcTargets.ts +2 -8
  59. package/src/Sandbox.ts +133 -146
  60. package/src/ServiceBinding.ts +39 -34
  61. package/src/Vectorize.ts +2 -0
  62. package/src/Worker.ts +12 -1
  63. package/src/WorkerDefinition.ts +28 -17
  64. package/src/WorkersAi.ts +2 -0
  65. package/src/Workflow.ts +9 -8
  66. package/src/WorkflowDefinition.ts +12 -4
  67. package/src/internal/RpcTargets.ts +10 -0
package/src/Sandbox.ts CHANGED
@@ -44,6 +44,10 @@ import * as Binding from "./Binding";
44
44
  import type { ContainerStartOptions, ContainerStopSignal } from "./ContainerNamespace";
45
45
  import { WorkerEnvironment } from "./Environment";
46
46
  import * as ErrorMessage from "./internal/ErrorMessage";
47
+ import { CurrentTargets } from "./internal/RpcTargets";
48
+ import * as RpcTargets from "./RpcTargets";
49
+
50
+ export type { BindingNotFoundError, BindingValidationError } from "./Binding";
47
51
 
48
52
  export type {
49
53
  BackupOptions,
@@ -370,6 +374,7 @@ const trySandboxPromise = <A>(
370
374
  instance: string,
371
375
  operation: string,
372
376
  evaluate: (signal: AbortSignal) => PromiseLike<A>,
377
+ target?: SandboxClientResource | SandboxNamespaceResource,
373
378
  ): Effect.Effect<A, SandboxOperationError> =>
374
379
  Effect.tryPromise({
375
380
  try: evaluate,
@@ -380,16 +385,19 @@ const trySandboxPromise = <A>(
380
385
  operation,
381
386
  cause,
382
387
  }),
383
- });
388
+ }).pipe(
389
+ Effect.tapCause(() => (target === undefined ? Effect.void : RpcTargets.invalidate(target))),
390
+ );
384
391
 
385
392
  const sandboxStream = <A>(
386
393
  definition: SandboxDefinition,
387
394
  instance: string,
388
395
  operation: string,
389
396
  acquire: (signal: AbortSignal) => PromiseLike<ReadableStream<A>>,
397
+ target: SandboxClientResource | SandboxNamespaceResource,
390
398
  ): Stream.Stream<A, SandboxOperationError> =>
391
399
  Stream.unwrap(
392
- Effect.map(trySandboxPromise(definition, instance, operation, acquire), (readable) =>
400
+ Effect.map(trySandboxPromise(definition, instance, operation, acquire, target), (readable) =>
393
401
  Stream.fromReadableStream({
394
402
  evaluate: () => readable,
395
403
  onError: (cause) =>
@@ -401,7 +409,7 @@ const sandboxStream = <A>(
401
409
  }),
402
410
  }),
403
411
  ),
404
- );
412
+ ).pipe(Stream.tapCause(() => RpcTargets.invalidate(target)));
405
413
 
406
414
  const decodeSseData = <A>(payload: string): ReadonlyArray<A> => {
407
415
  if (payload === "[DONE]" || payload.trim() === "") {
@@ -451,7 +459,14 @@ const makeProcessHandle = (
451
459
  definition: SandboxDefinition,
452
460
  instance: string,
453
461
  process: SandboxProcess,
462
+ target: SandboxClientResource | SandboxNamespaceResource,
454
463
  ): SandboxProcessHandle => {
464
+ const attempt = <A>(operation: string, evaluate: (signal: AbortSignal) => PromiseLike<A>) =>
465
+ trySandboxPromise(definition, instance, operation, evaluate, target);
466
+ const stream = <A>(
467
+ operation: string,
468
+ acquire: (signal: AbortSignal) => PromiseLike<ReadableStream<A>>,
469
+ ) => sandboxStream(definition, instance, operation, acquire, target);
455
470
  const spanOptions = (operation: string) => ({
456
471
  attributes: {
457
472
  binding: definition.binding,
@@ -465,24 +480,19 @@ const makeProcessHandle = (
465
480
  id: process.id,
466
481
  pid: process.pid,
467
482
  rawUnsafe: Effect.succeed(process),
468
- status: trySandboxPromise(definition, instance, "process.status", () => process.status()).pipe(
483
+ status: attempt("process.status", () => process.status()).pipe(
469
484
  Effect.withSpan("Sandbox.process.status", spanOptions("process.status")),
470
485
  ),
471
- exitCode: trySandboxPromise(
472
- definition,
473
- instance,
474
- "process.exitCode",
475
- () => process.exitCode,
476
- ).pipe(Effect.withSpan("Sandbox.process.exitCode", spanOptions("process.exitCode"))),
486
+ exitCode: attempt("process.exitCode", () => process.exitCode).pipe(
487
+ Effect.withSpan("Sandbox.process.exitCode", spanOptions("process.exitCode")),
488
+ ),
477
489
  logs: (options?: Omit<ProcessLogsOptions, "signal">) =>
478
- sandboxStream(definition, instance, "process.logs", (signal) =>
479
- process.logs({ ...options, signal }),
480
- ),
490
+ stream("process.logs", (signal) => process.logs({ ...options, signal })),
481
491
  waitForLog: Effect.fn(
482
492
  "Sandbox.process.waitForLog",
483
493
  spanOptions("process.waitForLog"),
484
494
  )(function* (pattern: string | RegExp, options?: Omit<WaitForLogOptions, "signal">) {
485
- return yield* trySandboxPromise(definition, instance, "process.waitForLog", (signal) =>
495
+ return yield* attempt("process.waitForLog", (signal) =>
486
496
  process.waitForLog(pattern, { ...options, signal }),
487
497
  );
488
498
  }),
@@ -490,7 +500,7 @@ const makeProcessHandle = (
490
500
  "Sandbox.process.waitForExit",
491
501
  spanOptions("process.waitForExit"),
492
502
  )(function* (options?: Omit<WaitForExitOptions, "signal">) {
493
- return yield* trySandboxPromise(definition, instance, "process.waitForExit", (signal) =>
503
+ return yield* attempt("process.waitForExit", (signal) =>
494
504
  process.waitForExit({ ...options, signal }),
495
505
  );
496
506
  }),
@@ -498,15 +508,13 @@ const makeProcessHandle = (
498
508
  "Sandbox.process.output",
499
509
  spanOptions("process.output"),
500
510
  )(function* (options?: Omit<ProcessOutputOptions, "signal">) {
501
- return yield* trySandboxPromise(definition, instance, "process.output", (signal) =>
502
- process.output({ ...options, signal }),
503
- );
511
+ return yield* attempt("process.output", (signal) => process.output({ ...options, signal }));
504
512
  }),
505
513
  outputText: Effect.fn(
506
514
  "Sandbox.process.outputText",
507
515
  spanOptions("process.outputText"),
508
516
  )(function* (options?: Omit<ProcessOutputOptions, "signal">) {
509
- return yield* trySandboxPromise(definition, instance, "process.outputText", (signal) =>
517
+ return yield* attempt("process.outputText", (signal) =>
510
518
  process.output({ ...options, encoding: "utf8", signal }),
511
519
  );
512
520
  }),
@@ -514,7 +522,7 @@ const makeProcessHandle = (
514
522
  "Sandbox.process.waitForPort",
515
523
  spanOptions("process.waitForPort"),
516
524
  )(function* (port: number, options?: Omit<WaitForPortOptions, "signal">) {
517
- return yield* trySandboxPromise(definition, instance, "process.waitForPort", (signal) =>
525
+ return yield* attempt("process.waitForPort", (signal) =>
518
526
  process.waitForPort(port, { ...options, signal }),
519
527
  );
520
528
  }),
@@ -522,9 +530,7 @@ const makeProcessHandle = (
522
530
  "Sandbox.process.kill",
523
531
  spanOptions("process.kill"),
524
532
  )(function* (signal?: number) {
525
- return yield* trySandboxPromise(definition, instance, "process.kill", () =>
526
- process.kill(signal),
527
- );
533
+ return yield* attempt("process.kill", () => process.kill(signal));
528
534
  }),
529
535
  };
530
536
  };
@@ -533,7 +539,14 @@ const makeTerminalHandle = (
533
539
  definition: SandboxDefinition,
534
540
  instance: string,
535
541
  terminal: Terminal,
542
+ target: SandboxClientResource | SandboxNamespaceResource,
536
543
  ): SandboxTerminalHandle => {
544
+ const attempt = <A>(operation: string, evaluate: (signal: AbortSignal) => PromiseLike<A>) =>
545
+ trySandboxPromise(definition, instance, operation, evaluate, target);
546
+ const stream = <A>(
547
+ operation: string,
548
+ acquire: (signal: AbortSignal) => PromiseLike<ReadableStream<A>>,
549
+ ) => sandboxStream(definition, instance, operation, acquire, target);
537
550
  const spanOptions = (operation: string) => ({
538
551
  attributes: {
539
552
  binding: definition.binding,
@@ -546,43 +559,37 @@ const makeTerminalHandle = (
546
559
  return {
547
560
  id: terminal.id,
548
561
  rawUnsafe: Effect.succeed(terminal),
549
- snapshot: trySandboxPromise(definition, instance, "terminal.snapshot", () =>
550
- terminal.getSnapshot(),
551
- ).pipe(Effect.withSpan("Sandbox.terminal.snapshot", spanOptions("terminal.snapshot"))),
562
+ snapshot: attempt("terminal.snapshot", () => terminal.getSnapshot()).pipe(
563
+ Effect.withSpan("Sandbox.terminal.snapshot", spanOptions("terminal.snapshot")),
564
+ ),
552
565
  write: Effect.fn(
553
566
  "Sandbox.terminal.write",
554
567
  spanOptions("terminal.write"),
555
568
  )(function* (data: Uint8Array) {
556
- return yield* trySandboxPromise(definition, instance, "terminal.write", () =>
557
- terminal.write(data),
558
- );
569
+ return yield* attempt("terminal.write", () => terminal.write(data));
559
570
  }),
560
571
  resize: Effect.fn(
561
572
  "Sandbox.terminal.resize",
562
573
  spanOptions("terminal.resize"),
563
574
  )(function* (cols: number, rows: number) {
564
- return yield* trySandboxPromise(definition, instance, "terminal.resize", () =>
565
- terminal.resize(cols, rows),
566
- );
575
+ return yield* attempt("terminal.resize", () => terminal.resize(cols, rows));
567
576
  }),
568
577
  output: (options?: Omit<TerminalOutputOptions, "signal">) =>
569
- sandboxStream(definition, instance, "terminal.output", (signal) =>
570
- terminal.output({ ...options, signal }),
571
- ),
578
+ stream("terminal.output", (signal) => terminal.output({ ...options, signal })),
572
579
  waitForExit: Effect.fn(
573
580
  "Sandbox.terminal.waitForExit",
574
581
  spanOptions("terminal.waitForExit"),
575
582
  )(function* (options?: Omit<WaitForExitOptions, "signal">) {
576
- return yield* trySandboxPromise(definition, instance, "terminal.waitForExit", (signal) =>
583
+ return yield* attempt("terminal.waitForExit", (signal) =>
577
584
  terminal.waitForExit({ ...options, signal }),
578
585
  );
579
586
  }),
580
- interrupt: trySandboxPromise(definition, instance, "terminal.interrupt", () =>
581
- terminal.interrupt(),
582
- ).pipe(Effect.withSpan("Sandbox.terminal.interrupt", spanOptions("terminal.interrupt"))),
583
- terminate: trySandboxPromise(definition, instance, "terminal.terminate", () =>
584
- terminal.terminate(),
585
- ).pipe(Effect.withSpan("Sandbox.terminal.terminate", spanOptions("terminal.terminate"))),
587
+ interrupt: attempt("terminal.interrupt", () => terminal.interrupt()).pipe(
588
+ Effect.withSpan("Sandbox.terminal.interrupt", spanOptions("terminal.interrupt")),
589
+ ),
590
+ terminate: attempt("terminal.terminate", () => terminal.terminate()).pipe(
591
+ Effect.withSpan("Sandbox.terminal.terminate", spanOptions("terminal.terminate")),
592
+ ),
586
593
  connect: Effect.fn(
587
594
  "Sandbox.terminal.connect",
588
595
  spanOptions("terminal.connect"),
@@ -594,9 +601,7 @@ const makeTerminalHandle = (
594
601
  readonly rows?: number;
595
602
  },
596
603
  ) {
597
- return yield* trySandboxPromise(definition, instance, "terminal.connect", () =>
598
- terminal.connect(request, options),
599
- );
604
+ return yield* attempt("terminal.connect", () => terminal.connect(request, options));
600
605
  }),
601
606
  };
602
607
  };
@@ -605,7 +610,20 @@ export const fromSandboxClient = (
605
610
  client: SandboxClientResource,
606
611
  definition: SandboxDefinition,
607
612
  instance: string,
613
+ ): SandboxInstanceClient => makeSandboxClient(client, definition, instance, client);
614
+
615
+ const makeSandboxClient = (
616
+ client: SandboxClientResource,
617
+ definition: SandboxDefinition,
618
+ instance: string,
619
+ target: SandboxClientResource | SandboxNamespaceResource,
608
620
  ): SandboxInstanceClient => {
621
+ const attempt = <A>(operation: string, evaluate: (signal: AbortSignal) => PromiseLike<A>) =>
622
+ trySandboxPromise(definition, instance, operation, evaluate, target);
623
+ const stream = <A>(
624
+ operation: string,
625
+ acquire: (signal: AbortSignal) => PromiseLike<ReadableStream<A>>,
626
+ ) => sandboxStream(definition, instance, operation, acquire, target);
609
627
  const spanOptions = (operation: string) => ({
610
628
  attributes: { binding: definition.binding, instance, operation },
611
629
  });
@@ -616,27 +634,23 @@ export const fromSandboxClient = (
616
634
  "Sandbox.exec",
617
635
  spanOptions("exec"),
618
636
  )(function* (command: SandboxCommand, options?: ExecOptions) {
619
- const process = yield* trySandboxPromise(definition, instance, "exec", () =>
620
- client.exec(command, options),
621
- );
637
+ const process = yield* attempt("exec", () => client.exec(command, options));
622
638
 
623
- return makeProcessHandle(definition, instance, process);
639
+ return makeProcessHandle(definition, instance, process, target);
624
640
  }),
625
641
  getProcess: Effect.fn(
626
642
  "Sandbox.getProcess",
627
643
  spanOptions("getProcess"),
628
644
  )(function* (id: string) {
629
- const process = yield* trySandboxPromise(definition, instance, "getProcess", () =>
630
- client.getProcess(id),
631
- );
645
+ const process = yield* attempt("getProcess", () => client.getProcess(id));
632
646
 
633
647
  return Option.map(Option.fromNullishOr(process), (found) =>
634
- makeProcessHandle(definition, instance, found),
648
+ makeProcessHandle(definition, instance, found, target),
635
649
  );
636
650
  }),
637
- listProcesses: trySandboxPromise(definition, instance, "listProcesses", () =>
638
- client.listProcesses(),
639
- ).pipe(Effect.withSpan("Sandbox.listProcesses", spanOptions("listProcesses"))),
651
+ listProcesses: attempt("listProcesses", () => client.listProcesses()).pipe(
652
+ Effect.withSpan("Sandbox.listProcesses", spanOptions("listProcesses")),
653
+ ),
640
654
  writeFile: Effect.fn(
641
655
  "Sandbox.writeFile",
642
656
  spanOptions("writeFile"),
@@ -647,9 +661,7 @@ export const fromSandboxClient = (
647
661
  ) {
648
662
  yield* Effect.annotateCurrentSpan("path", path);
649
663
 
650
- return yield* trySandboxPromise(definition, instance, "writeFile", () =>
651
- client.writeFile(path, content, options),
652
- );
664
+ return yield* attempt("writeFile", () => client.writeFile(path, content, options));
653
665
  }),
654
666
  readFile: Effect.fn(
655
667
  "Sandbox.readFile",
@@ -657,21 +669,16 @@ export const fromSandboxClient = (
657
669
  )(function* (path: string, options?: { readonly encoding?: ReadFileEncoding }) {
658
670
  yield* Effect.annotateCurrentSpan("path", path);
659
671
 
660
- return yield* trySandboxPromise(definition, instance, "readFile", () =>
661
- client.readFile(path, options),
662
- );
672
+ return yield* attempt("readFile", () => client.readFile(path, options));
663
673
  }),
664
- readFileStream: (path: string) =>
665
- sandboxStream(definition, instance, "readFileStream", () => client.readFileStream(path)),
674
+ readFileStream: (path: string) => stream("readFileStream", () => client.readFileStream(path)),
666
675
  mkdir: Effect.fn(
667
676
  "Sandbox.mkdir",
668
677
  spanOptions("mkdir"),
669
678
  )(function* (path: string, options?: { readonly recursive?: boolean }) {
670
679
  yield* Effect.annotateCurrentSpan("path", path);
671
680
 
672
- return yield* trySandboxPromise(definition, instance, "mkdir", () =>
673
- client.mkdir(path, options),
674
- );
681
+ return yield* attempt("mkdir", () => client.mkdir(path, options));
675
682
  }),
676
683
  deleteFile: Effect.fn(
677
684
  "Sandbox.deleteFile",
@@ -679,9 +686,7 @@ export const fromSandboxClient = (
679
686
  )(function* (path: string) {
680
687
  yield* Effect.annotateCurrentSpan("path", path);
681
688
 
682
- return yield* trySandboxPromise(definition, instance, "deleteFile", () =>
683
- client.deleteFile(path),
684
- );
689
+ return yield* attempt("deleteFile", () => client.deleteFile(path));
685
690
  }),
686
691
  renameFile: Effect.fn(
687
692
  "Sandbox.renameFile",
@@ -689,9 +694,7 @@ export const fromSandboxClient = (
689
694
  )(function* (oldPath: string, newPath: string) {
690
695
  yield* Effect.annotateCurrentSpan({ oldPath, newPath });
691
696
 
692
- return yield* trySandboxPromise(definition, instance, "renameFile", () =>
693
- client.renameFile(oldPath, newPath),
694
- );
697
+ return yield* attempt("renameFile", () => client.renameFile(oldPath, newPath));
695
698
  }),
696
699
  moveFile: Effect.fn(
697
700
  "Sandbox.moveFile",
@@ -699,9 +702,7 @@ export const fromSandboxClient = (
699
702
  )(function* (sourcePath: string, destinationPath: string) {
700
703
  yield* Effect.annotateCurrentSpan({ sourcePath, destinationPath });
701
704
 
702
- return yield* trySandboxPromise(definition, instance, "moveFile", () =>
703
- client.moveFile(sourcePath, destinationPath),
704
- );
705
+ return yield* attempt("moveFile", () => client.moveFile(sourcePath, destinationPath));
705
706
  }),
706
707
  listFiles: Effect.fn(
707
708
  "Sandbox.listFiles",
@@ -709,9 +710,7 @@ export const fromSandboxClient = (
709
710
  )(function* (path: string, options?: ListFilesOptions) {
710
711
  yield* Effect.annotateCurrentSpan("path", path);
711
712
 
712
- return yield* trySandboxPromise(definition, instance, "listFiles", () =>
713
- client.listFiles(path, options),
714
- );
713
+ return yield* attempt("listFiles", () => client.listFiles(path, options));
715
714
  }),
716
715
  exists: Effect.fn(
717
716
  "Sandbox.exists",
@@ -719,11 +718,11 @@ export const fromSandboxClient = (
719
718
  )(function* (path: string) {
720
719
  yield* Effect.annotateCurrentSpan("path", path);
721
720
 
722
- return yield* trySandboxPromise(definition, instance, "exists", () => client.exists(path));
721
+ return yield* attempt("exists", () => client.exists(path));
723
722
  }),
724
723
  watch: (path: string, options?: WatchOptions) =>
725
724
  decodeSseEvents<FileWatchSSEEvent, SandboxOperationError, never>(
726
- sandboxStream(definition, instance, "watch", () => client.watch(path, options)),
725
+ stream("watch", () => client.watch(path, options)),
727
726
  ),
728
727
  checkChanges: Effect.fn(
729
728
  "Sandbox.checkChanges",
@@ -731,17 +730,13 @@ export const fromSandboxClient = (
731
730
  )(function* (path: string, options?: CheckChangesOptions) {
732
731
  yield* Effect.annotateCurrentSpan("path", path);
733
732
 
734
- return yield* trySandboxPromise(definition, instance, "checkChanges", () =>
735
- client.checkChanges(path, options),
736
- );
733
+ return yield* attempt("checkChanges", () => client.checkChanges(path, options));
737
734
  }),
738
735
  setEnvVars: Effect.fn(
739
736
  "Sandbox.setEnvVars",
740
737
  spanOptions("setEnvVars"),
741
738
  )(function* (envVars: Record<string, string | undefined>) {
742
- return yield* trySandboxPromise(definition, instance, "setEnvVars", () =>
743
- client.setEnvVars(envVars),
744
- );
739
+ return yield* attempt("setEnvVars", () => client.setEnvVars(envVars));
745
740
  }),
746
741
  exposePort: Effect.fn(
747
742
  "Sandbox.exposePort",
@@ -749,9 +744,7 @@ export const fromSandboxClient = (
749
744
  )(function* (port: number, options: ExposePortOptions) {
750
745
  yield* Effect.annotateCurrentSpan("port", port);
751
746
 
752
- return yield* trySandboxPromise(definition, instance, "exposePort", () =>
753
- client.exposePort(port, options),
754
- );
747
+ return yield* attempt("exposePort", () => client.exposePort(port, options));
755
748
  }),
756
749
  unexposePort: Effect.fn(
757
750
  "Sandbox.unexposePort",
@@ -759,17 +752,13 @@ export const fromSandboxClient = (
759
752
  )(function* (port: number) {
760
753
  yield* Effect.annotateCurrentSpan("port", port);
761
754
 
762
- return yield* trySandboxPromise(definition, instance, "unexposePort", () =>
763
- client.unexposePort(port),
764
- );
755
+ return yield* attempt("unexposePort", () => client.unexposePort(port));
765
756
  }),
766
757
  getExposedPorts: Effect.fn(
767
758
  "Sandbox.getExposedPorts",
768
759
  spanOptions("getExposedPorts"),
769
760
  )(function* (hostname: string) {
770
- return yield* trySandboxPromise(definition, instance, "getExposedPorts", () =>
771
- client.getExposedPorts(hostname),
772
- );
761
+ return yield* attempt("getExposedPorts", () => client.getExposedPorts(hostname));
773
762
  }),
774
763
  isPortExposed: Effect.fn(
775
764
  "Sandbox.isPortExposed",
@@ -777,9 +766,7 @@ export const fromSandboxClient = (
777
766
  )(function* (port: number) {
778
767
  yield* Effect.annotateCurrentSpan("port", port);
779
768
 
780
- return yield* trySandboxPromise(definition, instance, "isPortExposed", () =>
781
- client.isPortExposed(port),
782
- );
769
+ return yield* attempt("isPortExposed", () => client.isPortExposed(port));
783
770
  }),
784
771
  validatePortToken: Effect.fn(
785
772
  "Sandbox.validatePortToken",
@@ -787,17 +774,13 @@ export const fromSandboxClient = (
787
774
  )(function* (port: number, token: string) {
788
775
  yield* Effect.annotateCurrentSpan("port", port);
789
776
 
790
- return yield* trySandboxPromise(definition, instance, "validatePortToken", () =>
791
- client.validatePortToken(port, token),
792
- );
777
+ return yield* attempt("validatePortToken", () => client.validatePortToken(port, token));
793
778
  }),
794
779
  containerFetch: Effect.fn(
795
780
  "Sandbox.containerFetch",
796
781
  spanOptions("containerFetch"),
797
782
  )(function* (requestOrUrl: Request | string | URL, port?: number) {
798
- return yield* trySandboxPromise(definition, instance, "containerFetch", () =>
799
- client.containerFetch(requestOrUrl, port),
800
- );
783
+ return yield* attempt("containerFetch", () => client.containerFetch(requestOrUrl, port));
801
784
  }),
802
785
  wsConnect: Effect.fn(
803
786
  "Sandbox.wsConnect",
@@ -805,9 +788,7 @@ export const fromSandboxClient = (
805
788
  )(function* (request: Request, port: number) {
806
789
  yield* Effect.annotateCurrentSpan("port", port);
807
790
 
808
- return yield* trySandboxPromise(definition, instance, "wsConnect", () =>
809
- client.wsConnect(request, port),
810
- );
791
+ return yield* attempt("wsConnect", () => client.wsConnect(request, port));
811
792
  }),
812
793
  mountBucket: Effect.fn(
813
794
  "Sandbox.mountBucket",
@@ -815,9 +796,7 @@ export const fromSandboxClient = (
815
796
  )(function* (bucket: string, mountPath: string, options: MountBucketOptions) {
816
797
  yield* Effect.annotateCurrentSpan({ bucket, mountPath });
817
798
 
818
- return yield* trySandboxPromise(definition, instance, "mountBucket", () =>
819
- client.mountBucket(bucket, mountPath, options),
820
- );
799
+ return yield* attempt("mountBucket", () => client.mountBucket(bucket, mountPath, options));
821
800
  }),
822
801
  unmountBucket: Effect.fn(
823
802
  "Sandbox.unmountBucket",
@@ -825,9 +804,7 @@ export const fromSandboxClient = (
825
804
  )(function* (mountPath: string) {
826
805
  yield* Effect.annotateCurrentSpan("mountPath", mountPath);
827
806
 
828
- return yield* trySandboxPromise(definition, instance, "unmountBucket", () =>
829
- client.unmountBucket(mountPath),
830
- );
807
+ return yield* attempt("unmountBucket", () => client.unmountBucket(mountPath));
831
808
  }),
832
809
  createBackup: Effect.fn(
833
810
  "Sandbox.createBackup",
@@ -835,9 +812,7 @@ export const fromSandboxClient = (
835
812
  )(function* (options: BackupOptions) {
836
813
  yield* Effect.annotateCurrentSpan("dir", options.dir);
837
814
 
838
- return yield* trySandboxPromise(definition, instance, "createBackup", () =>
839
- client.createBackup(options),
840
- );
815
+ return yield* attempt("createBackup", () => client.createBackup(options));
841
816
  }),
842
817
  restoreBackup: Effect.fn(
843
818
  "Sandbox.restoreBackup",
@@ -845,37 +820,29 @@ export const fromSandboxClient = (
845
820
  )(function* (backup: DirectoryBackup) {
846
821
  yield* Effect.annotateCurrentSpan({ backupId: backup.id, dir: backup.dir });
847
822
 
848
- return yield* trySandboxPromise(definition, instance, "restoreBackup", () =>
849
- client.restoreBackup(backup),
850
- );
823
+ return yield* attempt("restoreBackup", () => client.restoreBackup(backup));
851
824
  }),
852
825
  createTerminal: Effect.fn(
853
826
  "Sandbox.createTerminal",
854
827
  spanOptions("createTerminal"),
855
828
  )(function* (options: CreateTerminalOptions) {
856
- const terminal = yield* trySandboxPromise(definition, instance, "createTerminal", () =>
857
- client.createTerminal(options),
858
- );
829
+ const terminal = yield* attempt("createTerminal", () => client.createTerminal(options));
859
830
 
860
- return makeTerminalHandle(definition, instance, terminal);
831
+ return makeTerminalHandle(definition, instance, terminal, target);
861
832
  }),
862
833
  getTerminal: Effect.fn(
863
834
  "Sandbox.getTerminal",
864
835
  spanOptions("getTerminal"),
865
836
  )(function* (id: string) {
866
- const terminal = yield* trySandboxPromise(definition, instance, "getTerminal", () =>
867
- client.getTerminal(id),
868
- );
837
+ const terminal = yield* attempt("getTerminal", () => client.getTerminal(id));
869
838
 
870
839
  return Option.map(Option.fromNullishOr(terminal), (found) =>
871
- makeTerminalHandle(definition, instance, found),
840
+ makeTerminalHandle(definition, instance, found, target),
872
841
  );
873
842
  }),
874
- listTerminals: trySandboxPromise(definition, instance, "listTerminals", () =>
875
- client.listTerminals(),
876
- ).pipe(
843
+ listTerminals: attempt("listTerminals", () => client.listTerminals()).pipe(
877
844
  Effect.map((terminals) =>
878
- terminals.map((terminal) => makeTerminalHandle(definition, instance, terminal)),
845
+ terminals.map((terminal) => makeTerminalHandle(definition, instance, terminal, target)),
879
846
  ),
880
847
  Effect.withSpan("Sandbox.listTerminals", spanOptions("listTerminals")),
881
848
  ),
@@ -886,35 +853,31 @@ export const fromSandboxClient = (
886
853
  )(function* (port: number, options?: TunnelOptions) {
887
854
  yield* Effect.annotateCurrentSpan("port", port);
888
855
 
889
- return yield* trySandboxPromise(definition, instance, "tunnels.get", () =>
890
- client.tunnels.get(port, options),
891
- );
856
+ return yield* attempt("tunnels.get", () => client.tunnels.get(port, options));
892
857
  }),
893
- list: trySandboxPromise(definition, instance, "tunnels.list", () =>
894
- client.tunnels.list(),
895
- ).pipe(Effect.withSpan("Sandbox.tunnels.list", spanOptions("tunnels.list"))),
858
+ list: attempt("tunnels.list", () => client.tunnels.list()).pipe(
859
+ Effect.withSpan("Sandbox.tunnels.list", spanOptions("tunnels.list")),
860
+ ),
896
861
  destroy: Effect.fn(
897
862
  "Sandbox.tunnels.destroy",
898
863
  spanOptions("tunnels.destroy"),
899
864
  )(function* (portOrInfo: number | TunnelInfo) {
900
- return yield* trySandboxPromise(definition, instance, "tunnels.destroy", () =>
901
- client.tunnels.destroy(portOrInfo),
902
- );
865
+ return yield* attempt("tunnels.destroy", () => client.tunnels.destroy(portOrInfo));
903
866
  }),
904
867
  },
905
868
  start: Effect.fn(
906
869
  "Sandbox.start",
907
870
  spanOptions("start"),
908
871
  )(function* (options?: ContainerStartOptions) {
909
- return yield* trySandboxPromise(definition, instance, "start", () => client.start(options));
872
+ return yield* attempt("start", () => client.start(options));
910
873
  }),
911
874
  stop: Effect.fn(
912
875
  "Sandbox.stop",
913
876
  spanOptions("stop"),
914
877
  )(function* (signal?: ContainerStopSignal) {
915
- return yield* trySandboxPromise(definition, instance, "stop", () => client.stop(signal));
878
+ return yield* attempt("stop", () => client.stop(signal));
916
879
  }),
917
- destroy: trySandboxPromise(definition, instance, "destroy", () => client.destroy()).pipe(
880
+ destroy: attempt("destroy", () => client.destroy()).pipe(
918
881
  Effect.withSpan("Sandbox.destroy", spanOptions("destroy")),
919
882
  ),
920
883
  };
@@ -958,7 +921,31 @@ export const makeClient =
958
921
  yield* Effect.annotateCurrentSpan("instance", name);
959
922
 
960
923
  const sandboxModule = yield* importSandboxModule(definition, name);
961
- const nativeNamespace: unknown = namespace;
924
+ const targets = yield* Effect.serviceOption(CurrentTargets);
925
+ const scopedNamespace = yield* RpcTargets.get(namespace, "sandbox-namespace", () => {
926
+ const adapter: SandboxNamespaceResource = {
927
+ idFromName: (name) => namespace.idFromName(name),
928
+ get: (id, options) =>
929
+ Option.isSome(targets)
930
+ ? targets.value.get(adapter, JSON.stringify([id.toString(), options]), () =>
931
+ namespace.get(id, options),
932
+ )
933
+ : namespace.get(id, options),
934
+ };
935
+
936
+ return adapter;
937
+ }).pipe(
938
+ Effect.mapError(
939
+ (error) =>
940
+ new SandboxOperationError({
941
+ binding: definition.binding,
942
+ instance: name,
943
+ operation: "get",
944
+ cause: error.cause,
945
+ }),
946
+ ),
947
+ );
948
+ const nativeNamespace: unknown = scopedNamespace;
962
949
  const client = yield* Effect.try({
963
950
  try: () =>
964
951
  sandboxModule.getSandbox(
@@ -979,7 +966,7 @@ export const makeClient =
979
966
  }),
980
967
  });
981
968
 
982
- return fromSandboxClient(client, definition, name);
969
+ return makeSandboxClient(client, definition, name, scopedNamespace);
983
970
  });
984
971
 
985
972
  return {