@effect/platform 0.58.27 → 0.59.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.
Files changed (107) hide show
  1. package/dist/cjs/Error.js +1 -25
  2. package/dist/cjs/Error.js.map +1 -1
  3. package/dist/cjs/HttpApp.js +4 -1
  4. package/dist/cjs/HttpApp.js.map +1 -1
  5. package/dist/cjs/HttpClientError.js +5 -6
  6. package/dist/cjs/HttpClientError.js.map +1 -1
  7. package/dist/cjs/HttpServerError.js +6 -5
  8. package/dist/cjs/HttpServerError.js.map +1 -1
  9. package/dist/cjs/HttpServerResponse.js.map +1 -1
  10. package/dist/cjs/Multipart.js.map +1 -1
  11. package/dist/cjs/Socket.js +11 -11
  12. package/dist/cjs/Socket.js.map +1 -1
  13. package/dist/cjs/Worker.js +10 -1
  14. package/dist/cjs/Worker.js.map +1 -1
  15. package/dist/cjs/WorkerError.js +13 -4
  16. package/dist/cjs/WorkerError.js.map +1 -1
  17. package/dist/cjs/WorkerRunner.js +6 -1
  18. package/dist/cjs/WorkerRunner.js.map +1 -1
  19. package/dist/cjs/internal/httpClient.js +7 -7
  20. package/dist/cjs/internal/httpClient.js.map +1 -1
  21. package/dist/cjs/internal/httpClientResponse.js +13 -13
  22. package/dist/cjs/internal/httpClientResponse.js.map +1 -1
  23. package/dist/cjs/internal/httpServerRequest.js +16 -16
  24. package/dist/cjs/internal/httpServerRequest.js.map +1 -1
  25. package/dist/cjs/internal/httpServerResponse.js +4 -3
  26. package/dist/cjs/internal/httpServerResponse.js.map +1 -1
  27. package/dist/cjs/internal/multipart.js +16 -16
  28. package/dist/cjs/internal/multipart.js.map +1 -1
  29. package/dist/cjs/internal/path.js +461 -11
  30. package/dist/cjs/internal/path.js.map +1 -1
  31. package/dist/cjs/internal/worker.js +126 -59
  32. package/dist/cjs/internal/worker.js.map +1 -1
  33. package/dist/cjs/internal/workerRunner.js +39 -65
  34. package/dist/cjs/internal/workerRunner.js.map +1 -1
  35. package/dist/dts/Error.d.ts +0 -10
  36. package/dist/dts/Error.d.ts.map +1 -1
  37. package/dist/dts/HttpApp.d.ts +1 -1
  38. package/dist/dts/HttpApp.d.ts.map +1 -1
  39. package/dist/dts/HttpClientError.d.ts +6 -8
  40. package/dist/dts/HttpClientError.d.ts.map +1 -1
  41. package/dist/dts/HttpServerError.d.ts +10 -13
  42. package/dist/dts/HttpServerError.d.ts.map +1 -1
  43. package/dist/dts/HttpServerResponse.d.ts +5 -1
  44. package/dist/dts/HttpServerResponse.d.ts.map +1 -1
  45. package/dist/dts/Multipart.d.ts +3 -2
  46. package/dist/dts/Multipart.d.ts.map +1 -1
  47. package/dist/dts/Socket.d.ts +2 -4
  48. package/dist/dts/Socket.d.ts.map +1 -1
  49. package/dist/dts/Worker.d.ts +27 -27
  50. package/dist/dts/Worker.d.ts.map +1 -1
  51. package/dist/dts/WorkerError.d.ts +2 -2
  52. package/dist/dts/WorkerError.d.ts.map +1 -1
  53. package/dist/dts/WorkerRunner.d.ts +8 -5
  54. package/dist/dts/WorkerRunner.d.ts.map +1 -1
  55. package/dist/esm/Error.js +0 -23
  56. package/dist/esm/Error.js.map +1 -1
  57. package/dist/esm/HttpApp.js +4 -1
  58. package/dist/esm/HttpApp.js.map +1 -1
  59. package/dist/esm/HttpClientError.js +5 -6
  60. package/dist/esm/HttpClientError.js.map +1 -1
  61. package/dist/esm/HttpServerError.js +7 -6
  62. package/dist/esm/HttpServerError.js.map +1 -1
  63. package/dist/esm/HttpServerResponse.js.map +1 -1
  64. package/dist/esm/Multipart.js.map +1 -1
  65. package/dist/esm/Socket.js +12 -12
  66. package/dist/esm/Socket.js.map +1 -1
  67. package/dist/esm/Worker.js +9 -0
  68. package/dist/esm/Worker.js.map +1 -1
  69. package/dist/esm/WorkerError.js +13 -4
  70. package/dist/esm/WorkerError.js.map +1 -1
  71. package/dist/esm/WorkerRunner.js +5 -0
  72. package/dist/esm/WorkerRunner.js.map +1 -1
  73. package/dist/esm/internal/httpClient.js +7 -7
  74. package/dist/esm/internal/httpClient.js.map +1 -1
  75. package/dist/esm/internal/httpClientResponse.js +13 -13
  76. package/dist/esm/internal/httpClientResponse.js.map +1 -1
  77. package/dist/esm/internal/httpServerRequest.js +16 -16
  78. package/dist/esm/internal/httpServerRequest.js.map +1 -1
  79. package/dist/esm/internal/httpServerResponse.js +4 -3
  80. package/dist/esm/internal/httpServerResponse.js.map +1 -1
  81. package/dist/esm/internal/multipart.js +17 -17
  82. package/dist/esm/internal/multipart.js.map +1 -1
  83. package/dist/esm/internal/path.js +461 -10
  84. package/dist/esm/internal/path.js.map +1 -1
  85. package/dist/esm/internal/worker.js +124 -57
  86. package/dist/esm/internal/worker.js.map +1 -1
  87. package/dist/esm/internal/workerRunner.js +38 -65
  88. package/dist/esm/internal/workerRunner.js.map +1 -1
  89. package/package.json +5 -6
  90. package/src/Error.ts +0 -38
  91. package/src/HttpApp.ts +2 -2
  92. package/src/HttpClientError.ts +13 -6
  93. package/src/HttpServerError.ts +17 -6
  94. package/src/HttpServerResponse.ts +8 -1
  95. package/src/Multipart.ts +3 -2
  96. package/src/Socket.ts +10 -9
  97. package/src/Worker.ts +32 -29
  98. package/src/WorkerError.ts +15 -8
  99. package/src/WorkerRunner.ts +15 -8
  100. package/src/internal/httpClient.ts +7 -7
  101. package/src/internal/httpClientResponse.ts +13 -13
  102. package/src/internal/httpServerRequest.ts +15 -15
  103. package/src/internal/httpServerResponse.ts +7 -3
  104. package/src/internal/multipart.ts +16 -15
  105. package/src/internal/path.ts +510 -14
  106. package/src/internal/worker.ts +193 -146
  107. package/src/internal/workerRunner.ts +106 -139
package/src/Error.ts CHANGED
@@ -54,44 +54,6 @@ export const TypeIdError = <const TypeId extends symbol, const Tag extends strin
54
54
  return Base as any
55
55
  }
56
56
 
57
- /**
58
- * @since 1.0.0
59
- * @category error
60
- */
61
- export const RefailError = <const TypeId extends symbol, const Tag extends string>(
62
- typeId: TypeId,
63
- tag: Tag
64
- ): new<A extends Record<string, any>>(
65
- args: Simplify<A & { readonly error: unknown }>
66
- ) =>
67
- & Cause.YieldableError
68
- & Record<TypeId, TypeId>
69
- & { readonly _tag: Tag; readonly error: unknown }
70
- & Readonly<A> =>
71
- {
72
- class Base extends Data.Error<{
73
- readonly error: unknown
74
- }> {
75
- readonly _tag = tag
76
- constructor(props: any) {
77
- super(props)
78
- if (Predicate.hasProperty(this.error, "stack")) {
79
- ;(this as any).stack = this.error.stack
80
- }
81
- }
82
- get message() {
83
- return Predicate.hasProperty(this.error, "message")
84
- ? this.error.message as string
85
- : Predicate.hasProperty(this.error, "toJSON") && typeof this.error.toJSON === "function"
86
- ? JSON.stringify(this.error.toJSON())
87
- : String(this.error)
88
- }
89
- }
90
- ;(Base.prototype as any)[typeId] = typeId
91
- ;(Base.prototype as any).name = tag
92
- return Base as any
93
- }
94
-
95
57
  /**
96
58
  * @since 1.0.0
97
59
  */
package/src/HttpApp.ts CHANGED
@@ -138,12 +138,12 @@ export const withPreResponseHandler = dual<
138
138
  */
139
139
  export const toWebHandlerRuntime = <R>(runtime: Runtime.Runtime<R>) => {
140
140
  const run = Runtime.runFork(runtime)
141
- return <E>(self: Default<E, R | Scope.Scope>, middleware: HttpMiddleware | undefined) =>
141
+ return <E>(self: Default<E, R | Scope.Scope>, middleware?: HttpMiddleware | undefined) =>
142
142
  (request: Request): Promise<Response> =>
143
143
  new Promise((resolve) => {
144
144
  const fiber = run(Effect.provideService(
145
145
  toHandled(self, (request, response) => {
146
- resolve(ServerResponse.toWeb(response, request.method === "HEAD"))
146
+ resolve(ServerResponse.toWeb(response, { withoutBody: request.method === "HEAD", runtime }))
147
147
  return Effect.void
148
148
  }, middleware),
149
149
  ServerRequest.HttpServerRequest,
@@ -28,17 +28,20 @@ export type HttpClientError = RequestError | ResponseError
28
28
  * @since 1.0.0
29
29
  * @category error
30
30
  */
31
- export class RequestError extends Error.RefailError(TypeId, "RequestError")<{
31
+ export class RequestError extends Error.TypeIdError(TypeId, "RequestError")<{
32
32
  readonly request: ClientRequest.HttpClientRequest
33
33
  readonly reason: "Transport" | "Encode" | "InvalidUrl"
34
+ readonly cause?: unknown
35
+ readonly description?: string
34
36
  }> {
35
37
  get methodAndUrl() {
36
38
  return `${this.request.method} ${this.request.url}`
37
39
  }
38
40
 
39
41
  get message() {
40
- const errorString = super.message
41
- return `${this.reason} error (${this.methodAndUrl}): ${errorString}`
42
+ return this.description ?
43
+ `${this.reason}: ${this.description} (${this.methodAndUrl})` :
44
+ `${this.reason} error (${this.methodAndUrl})`
42
45
  }
43
46
  }
44
47
 
@@ -46,17 +49,21 @@ export class RequestError extends Error.RefailError(TypeId, "RequestError")<{
46
49
  * @since 1.0.0
47
50
  * @category error
48
51
  */
49
- export class ResponseError extends Error.RefailError(TypeId, "ResponseError")<{
52
+ export class ResponseError extends Error.TypeIdError(TypeId, "ResponseError")<{
50
53
  readonly request: ClientRequest.HttpClientRequest
51
54
  readonly response: ClientResponse.HttpClientResponse
52
55
  readonly reason: "StatusCode" | "Decode" | "EmptyBody"
56
+ readonly cause?: unknown
57
+ readonly description?: string
53
58
  }> {
54
59
  get methodAndUrl() {
55
60
  return `${this.request.method} ${this.request.url}`
56
61
  }
57
62
 
58
63
  get message() {
59
- const errorString = super.message
60
- return `${this.reason} error (${this.response.status} ${this.methodAndUrl}): ${errorString}`
64
+ const info = `${this.response.status} ${this.methodAndUrl}`
65
+ return this.description ?
66
+ `${this.reason}: ${this.description} (${info})` :
67
+ `${this.reason} error (${info})`
61
68
  }
62
69
  }
@@ -6,7 +6,7 @@ import type * as Effect from "effect/Effect"
6
6
  import type * as Exit from "effect/Exit"
7
7
  import type * as FiberId from "effect/FiberId"
8
8
  import type * as Option from "effect/Option"
9
- import { RefailError, TypeIdError } from "./Error.js"
9
+ import { TypeIdError } from "./Error.js"
10
10
  import type * as ServerRequest from "./HttpServerRequest.js"
11
11
  import * as Respondable from "./HttpServerRespondable.js"
12
12
  import * as ServerResponse from "./HttpServerResponse.js"
@@ -34,9 +34,11 @@ export type HttpServerError = RequestError | ResponseError | RouteNotFound | Ser
34
34
  * @since 1.0.0
35
35
  * @category error
36
36
  */
37
- export class RequestError extends RefailError(TypeId, "RequestError")<{
37
+ export class RequestError extends TypeIdError(TypeId, "RequestError")<{
38
38
  readonly request: ServerRequest.HttpServerRequest
39
39
  readonly reason: "Transport" | "Decode"
40
+ readonly cause?: unknown
41
+ readonly description?: string
40
42
  }> implements Respondable.Respondable {
41
43
  /**
42
44
  * @since 1.0.0
@@ -50,7 +52,9 @@ export class RequestError extends RefailError(TypeId, "RequestError")<{
50
52
  }
51
53
 
52
54
  get message() {
53
- return `${this.reason} error (${this.methodAndUrl}): ${super.message}`
55
+ return this.description ?
56
+ `${this.reason}: ${this.description} (${this.methodAndUrl})` :
57
+ `${this.reason} error (${this.methodAndUrl})`
54
58
  }
55
59
  }
56
60
 
@@ -86,10 +90,12 @@ export class RouteNotFound extends TypeIdError(TypeId, "RouteNotFound")<{
86
90
  * @since 1.0.0
87
91
  * @category error
88
92
  */
89
- export class ResponseError extends RefailError(TypeId, "ResponseError")<{
93
+ export class ResponseError extends TypeIdError(TypeId, "ResponseError")<{
90
94
  readonly request: ServerRequest.HttpServerRequest
91
95
  readonly response: ServerResponse.HttpServerResponse
92
96
  readonly reason: "Decode"
97
+ readonly cause?: unknown
98
+ readonly description?: string
93
99
  }> {
94
100
  /**
95
101
  * @since 1.0.0
@@ -103,7 +109,10 @@ export class ResponseError extends RefailError(TypeId, "ResponseError")<{
103
109
  }
104
110
 
105
111
  get message() {
106
- return `${this.reason} error (${this.response.status} ${this.methodAndUrl}): ${super.message}`
112
+ const info = `${this.response.status} ${this.methodAndUrl}`
113
+ return this.description ?
114
+ `${this.description} (${info})` :
115
+ `${this.reason} error (${info})`
107
116
  }
108
117
  }
109
118
 
@@ -111,7 +120,9 @@ export class ResponseError extends RefailError(TypeId, "ResponseError")<{
111
120
  * @since 1.0.0
112
121
  * @category error
113
122
  */
114
- export class ServeError extends RefailError(TypeId, "ServeError")<{}> {}
123
+ export class ServeError extends TypeIdError(TypeId, "ServeError")<{
124
+ readonly cause: unknown
125
+ }> {}
115
126
 
116
127
  /**
117
128
  * @since 1.0.0
@@ -5,6 +5,7 @@ import type { ParseOptions } from "@effect/schema/AST"
5
5
  import type * as Schema from "@effect/schema/Schema"
6
6
  import type * as Effect from "effect/Effect"
7
7
  import type { Inspectable } from "effect/Inspectable"
8
+ import type * as Runtime from "effect/Runtime"
8
9
  import type * as Stream from "effect/Stream"
9
10
  import type { Cookie, Cookies, CookiesError } from "./Cookies.js"
10
11
  import type * as PlatformError from "./Error.js"
@@ -353,4 +354,10 @@ export const setStatus: {
353
354
  * @since 1.0.0
354
355
  * @category conversions
355
356
  */
356
- export const toWeb: (response: HttpServerResponse, withoutBody?: boolean | undefined) => Response = internal.toWeb
357
+ export const toWeb: (
358
+ response: HttpServerResponse,
359
+ options?: {
360
+ readonly withoutBody?: boolean | undefined
361
+ readonly runtime?: Runtime.Runtime<never> | undefined
362
+ }
363
+ ) => Response = internal.toWeb
package/src/Multipart.ts CHANGED
@@ -137,7 +137,8 @@ export interface MultipartError extends YieldableError {
137
137
  readonly [ErrorTypeId]: ErrorTypeId
138
138
  readonly _tag: "MultipartError"
139
139
  readonly reason: "FileTooLarge" | "FieldTooLarge" | "BodyTooLarge" | "TooManyParts" | "InternalError" | "Parse"
140
- readonly error: unknown
140
+ readonly message: string
141
+ readonly cause: unknown
141
142
  }
142
143
 
143
144
  /**
@@ -147,7 +148,7 @@ export interface MultipartError extends YieldableError {
147
148
  export const MultipartError: new(
148
149
  options: {
149
150
  readonly reason: MultipartError["reason"]
150
- readonly error: unknown
151
+ readonly cause: unknown
151
152
  }
152
153
  ) => MultipartError = internal.MultipartError
153
154
 
package/src/Socket.ts CHANGED
@@ -18,7 +18,7 @@ import * as Predicate from "effect/Predicate"
18
18
  import * as Queue from "effect/Queue"
19
19
  import * as Scope from "effect/Scope"
20
20
  import type * as AsyncProducer from "effect/SingleProducerAsyncInput"
21
- import { RefailError, TypeIdError } from "./Error.js"
21
+ import { TypeIdError } from "./Error.js"
22
22
 
23
23
  /**
24
24
  * @since 1.0.0
@@ -125,11 +125,12 @@ export type SocketError = SocketGenericError | SocketCloseError
125
125
  * @since 1.0.0
126
126
  * @category errors
127
127
  */
128
- export class SocketGenericError extends RefailError(SocketErrorTypeId, "SocketError")<{
128
+ export class SocketGenericError extends TypeIdError(SocketErrorTypeId, "SocketError")<{
129
129
  readonly reason: "Write" | "Read" | "Open" | "OpenTimeout"
130
+ readonly cause: unknown
130
131
  }> {
131
132
  get message() {
132
- return `${this.reason}: ${super.message}`
133
+ return `An error occurred during ${this.reason}`
133
134
  }
134
135
  }
135
136
 
@@ -383,10 +384,10 @@ export const fromWebSocket = <R>(
383
384
  )
384
385
  )
385
386
  }
386
- ws.onerror = (error) => {
387
+ ws.onerror = (cause) => {
387
388
  Deferred.unsafeDone(
388
389
  fiberSet.deferred,
389
- Effect.fail(new SocketGenericError({ reason: open ? "Read" : "Open", error }))
390
+ Effect.fail(new SocketGenericError({ reason: open ? "Read" : "Open", cause }))
390
391
  )
391
392
  }
392
393
 
@@ -407,7 +408,7 @@ export const fromWebSocket = <R>(
407
408
  Effect.timeoutFail({
408
409
  duration: options?.openTimeout ?? 10000,
409
410
  onTimeout: () =>
410
- new SocketGenericError({ reason: "OpenTimeout", error: "timeout waiting for \"open\"" })
411
+ new SocketGenericError({ reason: "OpenTimeout", cause: "timeout waiting for \"open\"" })
411
412
  }),
412
413
  Effect.raceFirst(FiberSet.join(fiberSet))
413
414
  )
@@ -430,7 +431,7 @@ export const fromWebSocket = <R>(
430
431
  Effect.try({
431
432
  try: () =>
432
433
  ws.send(chunk),
433
- catch: (error) => new SocketGenericError({ reason: "Write", error })
434
+ catch: (cause) => new SocketGenericError({ reason: "Write", cause })
434
435
  })
435
436
  ),
436
437
  Effect.forever,
@@ -585,7 +586,7 @@ export const fromTransformStream = <R>(acquire: Effect.Effect<InputTransformStre
585
586
  writer.write(chunk)
586
587
  }
587
588
  },
588
- catch: (error) => new SocketGenericError({ reason: "Write", error })
589
+ catch: (cause) => new SocketGenericError({ reason: "Write", cause })
589
590
  })
590
591
  }),
591
592
  Effect.forever,
@@ -595,7 +596,7 @@ export const fromTransformStream = <R>(acquire: Effect.Effect<InputTransformStre
595
596
  Effect.tap(({ fiberSet, reader }) =>
596
597
  Effect.tryPromise({
597
598
  try: () => reader.read(),
598
- catch: (error) => new SocketGenericError({ reason: "Read", error })
599
+ catch: (cause) => new SocketGenericError({ reason: "Read", cause })
599
600
  }).pipe(
600
601
  Effect.tap((result) => {
601
602
  if (result.done) {
package/src/Worker.ts CHANGED
@@ -5,17 +5,14 @@ import type * as ParseResult from "@effect/schema/ParseResult"
5
5
  import type * as Schema from "@effect/schema/Schema"
6
6
  import type * as Serializable from "@effect/schema/Serializable"
7
7
  import type * as Context from "effect/Context"
8
+ import type * as Deferred from "effect/Deferred"
8
9
  import type * as Duration from "effect/Duration"
9
10
  import type * as Effect from "effect/Effect"
10
- import type * as Fiber from "effect/Fiber"
11
11
  import type { LazyArg } from "effect/Function"
12
12
  import type * as Layer from "effect/Layer"
13
- import type * as Option from "effect/Option"
14
13
  import type * as Pool from "effect/Pool"
15
- import type * as Queue from "effect/Queue"
16
14
  import type * as Scope from "effect/Scope"
17
15
  import type * as Stream from "effect/Stream"
18
- import type * as Tracer from "effect/Tracer"
19
16
  import * as internal from "./internal/worker.js"
20
17
  import type { WorkerError, WorkerErrorFrom } from "./WorkerError.js"
21
18
 
@@ -24,9 +21,10 @@ import type { WorkerError, WorkerErrorFrom } from "./WorkerError.js"
24
21
  * @category models
25
22
  */
26
23
  export interface BackingWorker<I, O> {
27
- readonly fiber: Fiber.Fiber<never, WorkerError>
28
24
  readonly send: (message: I, transfers?: ReadonlyArray<unknown>) => Effect.Effect<void, WorkerError>
29
- readonly queue: Queue.Dequeue<BackingWorker.Message<O>>
25
+ readonly run: <A, E, R>(
26
+ handler: (_: BackingWorker.Message<O>) => Effect.Effect<A, E, R>
27
+ ) => Effect.Effect<never, E | WorkerError, R>
30
28
  }
31
29
 
32
30
  /**
@@ -59,9 +57,28 @@ export type PlatformWorkerTypeId = typeof PlatformWorkerTypeId
59
57
  */
60
58
  export interface PlatformWorker {
61
59
  readonly [PlatformWorkerTypeId]: PlatformWorkerTypeId
62
- readonly spawn: <I, O>(worker: unknown) => Effect.Effect<BackingWorker<I, O>, WorkerError, Scope.Scope>
60
+ readonly spawn: <I, O>(id: number) => Effect.Effect<BackingWorker<I, O>, WorkerError, Spawner>
63
61
  }
64
62
 
63
+ /**
64
+ * @since 1.0.0
65
+ */
66
+ export const makePlatform: <W>() => <
67
+ P extends { readonly postMessage: (message: any, transfers?: any | undefined) => void }
68
+ >(
69
+ options: {
70
+ readonly setup: (options: { readonly worker: W; readonly scope: Scope.Scope }) => Effect.Effect<P>
71
+ readonly listen: (
72
+ options: {
73
+ readonly port: P
74
+ readonly emit: (data: any) => void
75
+ readonly deferred: Deferred.Deferred<never, WorkerError>
76
+ readonly scope: Scope.Scope
77
+ }
78
+ ) => Effect.Effect<void>
79
+ }
80
+ ) => PlatformWorker = internal.makePlatform
81
+
65
82
  /**
66
83
  * @since 1.0.0
67
84
  * @category tags
@@ -86,6 +103,12 @@ export interface Spawner {
86
103
  readonly _: unique symbol
87
104
  }
88
105
 
106
+ /**
107
+ * @since 1.0.0
108
+ * @category tags
109
+ */
110
+ export const Spawner: Context.Tag<Spawner, SpawnerFn<unknown>> = internal.Spawner
111
+
89
112
  /**
90
113
  * @since 1.0.0
91
114
  * @category models
@@ -105,8 +128,6 @@ export declare namespace Worker {
105
128
  */
106
129
  export interface Options<I> {
107
130
  readonly encode?: ((message: I) => Effect.Effect<unknown, WorkerError>) | undefined
108
- readonly transfers?: ((message: I) => ReadonlyArray<unknown>) | undefined
109
- readonly queue?: WorkerQueue<I> | undefined
110
131
  readonly initialMessage?: LazyArg<I> | undefined
111
132
  }
112
133
 
@@ -173,16 +194,6 @@ export declare namespace WorkerPool {
173
194
  })
174
195
  }
175
196
 
176
- /**
177
- * @category models
178
- * @since 1.0.0
179
- */
180
- export interface WorkerQueue<I> {
181
- readonly offer: (id: number, item: I, span: Option.Option<Tracer.Span>) => Effect.Effect<void>
182
- readonly take: Effect.Effect<readonly [id: number, item: I, span: Option.Option<Tracer.Span>]>
183
- readonly shutdown: Effect.Effect<void>
184
- }
185
-
186
197
  /**
187
198
  * @since 1.0.0
188
199
  * @category type ids
@@ -268,20 +279,12 @@ export declare namespace SerializedWorker {
268
279
  * @since 1.0.0
269
280
  * @category models
270
281
  */
271
- export type Options<I> = Extract<I, { readonly _tag: "InitialMessage" }> extends never ? BaseOptions<I> & {
282
+ export type Options<I> = Extract<I, { readonly _tag: "InitialMessage" }> extends never ? {
272
283
  readonly initialMessage?: LazyArg<I>
273
284
  }
274
- : BaseOptions<I> & {
285
+ : {
275
286
  readonly initialMessage: LazyArg<Extract<I, { readonly _tag: "InitialMessage" }>>
276
287
  }
277
-
278
- /**
279
- * @since 1.0.0
280
- * @category models
281
- */
282
- export interface BaseOptions<I> {
283
- readonly queue?: WorkerQueue<I> | undefined
284
- }
285
288
  }
286
289
 
287
290
  /**
@@ -3,7 +3,6 @@
3
3
  */
4
4
  import * as Schema from "@effect/schema/Schema"
5
5
  import type * as Cause from "effect/Cause"
6
- import * as Inspectable from "effect/Inspectable"
7
6
  import * as Predicate from "effect/Predicate"
8
7
  import * as internal from "./internal/workerError.js"
9
8
 
@@ -31,7 +30,7 @@ export const isWorkerError = (u: unknown): u is WorkerError => Predicate.hasProp
31
30
  */
32
31
  export class WorkerError extends Schema.TaggedError<WorkerError>()("WorkerError", {
33
32
  reason: Schema.Literal("spawn", "decode", "send", "unknown", "encode"),
34
- error: Schema.CauseDefectUnknown
33
+ cause: Schema.CauseDefectUnknown
35
34
  }) {
36
35
  /**
37
36
  * @since 1.0.0
@@ -61,11 +60,19 @@ export class WorkerError extends Schema.TaggedError<WorkerError>()("WorkerError"
61
60
  /**
62
61
  * @since 1.0.0
63
62
  */
64
- get message() {
65
- const message = Predicate.hasProperty(this.error, "message")
66
- ? this.error.message
67
- : Inspectable.toStringUnknown(this.error, undefined)
68
- return `${this.reason}: ${message}`
63
+ get message(): string {
64
+ switch (this.reason) {
65
+ case "send":
66
+ return "An error occurred calling .postMessage"
67
+ case "spawn":
68
+ return "An error occurred while spawning a worker"
69
+ case "decode":
70
+ return "An error occurred during decoding"
71
+ case "encode":
72
+ return "An error occurred during encoding"
73
+ case "unknown":
74
+ return "An unexpected error occurred"
75
+ }
69
76
  }
70
77
  }
71
78
 
@@ -76,5 +83,5 @@ export class WorkerError extends Schema.TaggedError<WorkerError>()("WorkerError"
76
83
  export interface WorkerErrorFrom {
77
84
  readonly _tag: "WorkerError"
78
85
  readonly reason: "spawn" | "decode" | "send" | "unknown" | "encode"
79
- readonly error: unknown
86
+ readonly cause: unknown
80
87
  }
@@ -6,7 +6,6 @@ import type * as Serializable from "@effect/schema/Serializable"
6
6
  import type * as Context from "effect/Context"
7
7
  import type * as Effect from "effect/Effect"
8
8
  import type * as Layer from "effect/Layer"
9
- import type * as Queue from "effect/Queue"
10
9
  import type * as Scope from "effect/Scope"
11
10
  import type * as Stream from "effect/Stream"
12
11
  import * as internal from "./internal/workerRunner.js"
@@ -17,7 +16,9 @@ import type { WorkerError } from "./WorkerError.js"
17
16
  * @category models
18
17
  */
19
18
  export interface BackingRunner<I, O> {
20
- readonly queue: Queue.Dequeue<readonly [portId: number, message: I]>
19
+ readonly run: <A, E, R>(
20
+ handler: (portId: number, message: I) => Effect.Effect<A, E, R>
21
+ ) => Effect.Effect<never, E | WorkerError, R>
21
22
  readonly send: (
22
23
  portId: number,
23
24
  message: O,
@@ -55,9 +56,7 @@ export type PlatformRunnerTypeId = typeof PlatformRunnerTypeId
55
56
  */
56
57
  export interface PlatformRunner {
57
58
  readonly [PlatformRunnerTypeId]: PlatformRunnerTypeId
58
- readonly start: <I, O>(
59
- shutdown: Effect.Effect<void>
60
- ) => Effect.Effect<BackingRunner<I, O>, WorkerError, Scope.Scope>
59
+ readonly start: <I, O>() => Effect.Effect<BackingRunner<I, O>, WorkerError>
61
60
  }
62
61
 
63
62
  /**
@@ -87,18 +86,26 @@ export declare namespace Runner {
87
86
  request: I,
88
87
  error: E
89
88
  ) => Effect.Effect<unknown, WorkerError>
90
- readonly transfers?: (message: O | E) => ReadonlyArray<unknown>
91
89
  }
92
90
  }
93
91
 
92
+ /**
93
+ * @since 1.0.0
94
+ * @category constructors
95
+ */
96
+ export const run: <I, E, R, O>(
97
+ process: (request: I) => Stream.Stream<O, E, R> | Effect.Effect<O, E, R>,
98
+ options?: Runner.Options<I, O, E>
99
+ ) => Effect.Effect<never, WorkerError, PlatformRunner | R> = internal.run
100
+
94
101
  /**
95
102
  * @since 1.0.0
96
103
  * @category constructors
97
104
  */
98
105
  export const make: <I, E, R, O>(
99
106
  process: (request: I) => Stream.Stream<O, E, R> | Effect.Effect<O, E, R>,
100
- options?: Runner.Options<I, O, E> | undefined
101
- ) => Effect.Effect<void, WorkerError, Scope.Scope | R | PlatformRunner> = internal.make
107
+ options?: Runner.Options<I, O, E>
108
+ ) => Effect.Effect<void, WorkerError, PlatformRunner | R | Scope.Scope> = internal.make
102
109
 
103
110
  /**
104
111
  * @since 1.0.0
@@ -128,7 +128,7 @@ export const makeDefault = (
128
128
  const addAbort = Scope.addFinalizer(scope, Effect.sync(() => controller.abort()))
129
129
  const urlResult = UrlParams.makeUrl(request.url, request.urlParams, request.hash)
130
130
  if (urlResult._tag === "Left") {
131
- return Effect.fail(new Error.RequestError({ request, reason: "InvalidUrl", error: urlResult.left }))
131
+ return Effect.fail(new Error.RequestError({ request, reason: "InvalidUrl", cause: urlResult.left }))
132
132
  }
133
133
  const url = urlResult.right
134
134
  const tracerDisabled = !fiber.getFiberRef(FiberRef.currentTracerEnabled) ||
@@ -211,11 +211,11 @@ export const fetch: Client.HttpClient.Default = makeDefault((request, url, signa
211
211
  duplex: request.body._tag === "Stream" ? "half" : undefined,
212
212
  signal
213
213
  } as any),
214
- catch: (error) =>
214
+ catch: (cause) =>
215
215
  new Error.RequestError({
216
216
  request,
217
217
  reason: "Transport",
218
- error
218
+ cause
219
219
  })
220
220
  }),
221
221
  (response) => internalResponse.fromWeb(request, response)
@@ -276,7 +276,7 @@ export const filterStatus = dual<
276
276
  request,
277
277
  response,
278
278
  reason: "StatusCode",
279
- error: "invalid status code"
279
+ description: "invalid status code"
280
280
  })
281
281
  )))
282
282
 
@@ -293,7 +293,7 @@ export const filterStatusOk = <E, R>(
293
293
  request,
294
294
  response,
295
295
  reason: "StatusCode",
296
- error: "non 2xx status code"
296
+ description: "non 2xx status code"
297
297
  })
298
298
  ))
299
299
 
@@ -675,11 +675,11 @@ export const schemaFunction = dual<
675
675
  Effect.flatMap(
676
676
  Effect.tryMap(encode(a), {
677
677
  try: (body) => new TextEncoder().encode(JSON.stringify(body)),
678
- catch: (error) =>
678
+ catch: (cause) =>
679
679
  new Error.RequestError({
680
680
  request,
681
681
  reason: "Encode",
682
- error
682
+ cause
683
683
  })
684
684
  }),
685
685
  (body) =>
@@ -67,19 +67,19 @@ class ClientResponseImpl extends Inspectable.Class implements ClientResponse.Htt
67
67
 
68
68
  get stream(): Stream.Stream<Uint8Array, Error.ResponseError> {
69
69
  return this.source.body
70
- ? Stream.fromReadableStream(() => this.source.body!, (_) =>
70
+ ? Stream.fromReadableStream(() => this.source.body!, (cause) =>
71
71
  new Error.ResponseError({
72
72
  request: this.request,
73
73
  response: this,
74
74
  reason: "Decode",
75
- error: _
75
+ cause
76
76
  }))
77
77
  : Stream.fail(
78
78
  new Error.ResponseError({
79
79
  request: this.request,
80
80
  response: this,
81
81
  reason: "EmptyBody",
82
- error: "can not create stream from empty body"
82
+ description: "can not create stream from empty body"
83
83
  })
84
84
  )
85
85
  }
@@ -87,12 +87,12 @@ class ClientResponseImpl extends Inspectable.Class implements ClientResponse.Htt
87
87
  get json(): Effect.Effect<unknown, Error.ResponseError> {
88
88
  return Effect.tryMap(this.text, {
89
89
  try: (text) => text === "" ? null : JSON.parse(text) as unknown,
90
- catch: (_) =>
90
+ catch: (cause) =>
91
91
  new Error.ResponseError({
92
92
  request: this.request,
93
93
  response: this,
94
94
  reason: "Decode",
95
- error: _
95
+ cause
96
96
  })
97
97
  })
98
98
  }
@@ -101,12 +101,12 @@ class ClientResponseImpl extends Inspectable.Class implements ClientResponse.Htt
101
101
  get text(): Effect.Effect<string, Error.ResponseError> {
102
102
  return this.textBody ??= Effect.tryPromise({
103
103
  try: () => this.source.text(),
104
- catch: (_) =>
104
+ catch: (cause) =>
105
105
  new Error.ResponseError({
106
106
  request: this.request,
107
107
  response: this,
108
108
  reason: "Decode",
109
- error: _
109
+ cause
110
110
  })
111
111
  }).pipe(Effect.cached, Effect.runSync)
112
112
  }
@@ -115,12 +115,12 @@ class ClientResponseImpl extends Inspectable.Class implements ClientResponse.Htt
115
115
  return Effect.flatMap(this.text, (_) =>
116
116
  Effect.try({
117
117
  try: () => UrlParams.fromInput(new URLSearchParams(_)),
118
- catch: (_) =>
118
+ catch: (cause) =>
119
119
  new Error.ResponseError({
120
120
  request: this.request,
121
121
  response: this,
122
122
  reason: "Decode",
123
- error: _
123
+ cause
124
124
  })
125
125
  }))
126
126
  }
@@ -129,12 +129,12 @@ class ClientResponseImpl extends Inspectable.Class implements ClientResponse.Htt
129
129
  get formData(): Effect.Effect<FormData, Error.ResponseError> {
130
130
  return this.formDataBody ??= Effect.tryPromise({
131
131
  try: () => this.source.formData(),
132
- catch: (_) =>
132
+ catch: (cause) =>
133
133
  new Error.ResponseError({
134
134
  request: this.request,
135
135
  response: this,
136
136
  reason: "Decode",
137
- error: _
137
+ cause
138
138
  })
139
139
  }).pipe(Effect.cached, Effect.runSync)
140
140
  }
@@ -143,12 +143,12 @@ class ClientResponseImpl extends Inspectable.Class implements ClientResponse.Htt
143
143
  get arrayBuffer(): Effect.Effect<ArrayBuffer, Error.ResponseError> {
144
144
  return this.arrayBufferBody ??= Effect.tryPromise({
145
145
  try: () => this.source.arrayBuffer(),
146
- catch: (_) =>
146
+ catch: (cause) =>
147
147
  new Error.ResponseError({
148
148
  request: this.request,
149
149
  response: this,
150
150
  reason: "Decode",
151
- error: _
151
+ cause
152
152
  })
153
153
  }).pipe(Effect.cached, Effect.runSync)
154
154
  }