@effect/platform 0.79.4 → 0.80.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 (79) hide show
  1. package/ChannelSchema/package.json +6 -0
  2. package/MsgPack/package.json +6 -0
  3. package/Ndjson/package.json +6 -0
  4. package/SocketServer/package.json +6 -0
  5. package/dist/cjs/ChannelSchema.js +69 -0
  6. package/dist/cjs/ChannelSchema.js.map +1 -0
  7. package/dist/cjs/Headers.js +1 -1
  8. package/dist/cjs/Headers.js.map +1 -1
  9. package/dist/cjs/HttpBody.js.map +1 -1
  10. package/dist/cjs/HttpRouter.js.map +1 -1
  11. package/dist/cjs/MsgPack.js +148 -0
  12. package/dist/cjs/MsgPack.js.map +1 -0
  13. package/dist/cjs/Ndjson.js +152 -0
  14. package/dist/cjs/Ndjson.js.map +1 -0
  15. package/dist/cjs/SocketServer.js +43 -0
  16. package/dist/cjs/SocketServer.js.map +1 -0
  17. package/dist/cjs/Transferable.js +6 -5
  18. package/dist/cjs/Transferable.js.map +1 -1
  19. package/dist/cjs/WorkerRunner.js.map +1 -1
  20. package/dist/cjs/index.js +9 -1
  21. package/dist/cjs/internal/httpRouter.js +2 -0
  22. package/dist/cjs/internal/httpRouter.js.map +1 -1
  23. package/dist/cjs/internal/worker.js +1 -1
  24. package/dist/cjs/internal/worker.js.map +1 -1
  25. package/dist/dts/ChannelSchema.d.ts +69 -0
  26. package/dist/dts/ChannelSchema.d.ts.map +1 -0
  27. package/dist/dts/Headers.d.ts +1 -1
  28. package/dist/dts/Headers.d.ts.map +1 -1
  29. package/dist/dts/HttpBody.d.ts +1 -1
  30. package/dist/dts/HttpBody.d.ts.map +1 -1
  31. package/dist/dts/HttpRouter.d.ts +3 -0
  32. package/dist/dts/HttpRouter.d.ts.map +1 -1
  33. package/dist/dts/MsgPack.d.ts +103 -0
  34. package/dist/dts/MsgPack.d.ts.map +1 -0
  35. package/dist/dts/Ndjson.d.ts +169 -0
  36. package/dist/dts/Ndjson.d.ts.map +1 -0
  37. package/dist/dts/SocketServer.d.ts +70 -0
  38. package/dist/dts/SocketServer.d.ts.map +1 -0
  39. package/dist/dts/Transferable.d.ts +2 -2
  40. package/dist/dts/Transferable.d.ts.map +1 -1
  41. package/dist/dts/WorkerRunner.d.ts +3 -1
  42. package/dist/dts/WorkerRunner.d.ts.map +1 -1
  43. package/dist/dts/index.d.ts +16 -0
  44. package/dist/dts/index.d.ts.map +1 -1
  45. package/dist/dts/internal/httpRouter.d.ts.map +1 -1
  46. package/dist/esm/ChannelSchema.js +59 -0
  47. package/dist/esm/ChannelSchema.js.map +1 -0
  48. package/dist/esm/Headers.js +1 -1
  49. package/dist/esm/Headers.js.map +1 -1
  50. package/dist/esm/HttpBody.js.map +1 -1
  51. package/dist/esm/HttpRouter.js.map +1 -1
  52. package/dist/esm/MsgPack.js +137 -0
  53. package/dist/esm/MsgPack.js.map +1 -0
  54. package/dist/esm/Ndjson.js +134 -0
  55. package/dist/esm/Ndjson.js.map +1 -0
  56. package/dist/esm/SocketServer.js +32 -0
  57. package/dist/esm/SocketServer.js.map +1 -0
  58. package/dist/esm/Transferable.js +6 -5
  59. package/dist/esm/Transferable.js.map +1 -1
  60. package/dist/esm/WorkerRunner.js.map +1 -1
  61. package/dist/esm/index.js +16 -0
  62. package/dist/esm/index.js.map +1 -1
  63. package/dist/esm/internal/httpRouter.js +2 -0
  64. package/dist/esm/internal/httpRouter.js.map +1 -1
  65. package/dist/esm/internal/worker.js +1 -1
  66. package/dist/esm/internal/worker.js.map +1 -1
  67. package/package.json +35 -2
  68. package/src/ChannelSchema.ts +267 -0
  69. package/src/Headers.ts +2 -2
  70. package/src/HttpBody.ts +1 -1
  71. package/src/HttpRouter.ts +9 -0
  72. package/src/MsgPack.ts +372 -0
  73. package/src/Ndjson.ts +493 -0
  74. package/src/SocketServer.ts +79 -0
  75. package/src/Transferable.ts +9 -8
  76. package/src/WorkerRunner.ts +3 -1
  77. package/src/index.ts +20 -0
  78. package/src/internal/httpRouter.ts +2 -0
  79. package/src/internal/worker.ts +1 -1
package/src/MsgPack.ts ADDED
@@ -0,0 +1,372 @@
1
+ /**
2
+ * @since 1.0.0
3
+ */
4
+ import * as Channel from "effect/Channel"
5
+ import * as Chunk from "effect/Chunk"
6
+ import * as Data from "effect/Data"
7
+ import * as Effect from "effect/Effect"
8
+ import { dual } from "effect/Function"
9
+ import type { ParseError } from "effect/ParseResult"
10
+ import * as ParseResult from "effect/ParseResult"
11
+ import * as Predicate from "effect/Predicate"
12
+ import * as Schema from "effect/Schema"
13
+ import { Packr, Unpackr } from "msgpackr"
14
+ import * as Msgpackr from "msgpackr"
15
+ import * as ChannelSchema from "./ChannelSchema.js"
16
+
17
+ export {
18
+ /**
19
+ * @since 1.0.0
20
+ * @category re-exports
21
+ */
22
+ Msgpackr
23
+ }
24
+
25
+ /**
26
+ * @since 1.0.0
27
+ * @category errors
28
+ */
29
+ export const ErrorTypeId: unique symbol = Symbol.for("@effect/platform/MsgPack/MsgPackError")
30
+
31
+ /**
32
+ * @since 1.0.0
33
+ * @category errors
34
+ */
35
+ export type ErrorTypeId = typeof ErrorTypeId
36
+
37
+ /**
38
+ * @since 1.0.0
39
+ * @category errors
40
+ */
41
+ export class MsgPackError extends Data.TaggedError("MsgPackError")<{
42
+ readonly reason: "Pack" | "Unpack"
43
+ readonly cause: unknown
44
+ }> {
45
+ /**
46
+ * @since 1.0.0
47
+ */
48
+ readonly [ErrorTypeId]: ErrorTypeId = ErrorTypeId
49
+
50
+ /**
51
+ * @since 1.0.0
52
+ */
53
+ get message() {
54
+ return this.reason
55
+ }
56
+ }
57
+
58
+ /**
59
+ * @since 1.0.0
60
+ * @category constructors
61
+ */
62
+ export const pack = <IE = never, Done = unknown>(): Channel.Channel<
63
+ Chunk.Chunk<Uint8Array>,
64
+ Chunk.Chunk<unknown>,
65
+ IE | MsgPackError,
66
+ IE,
67
+ Done,
68
+ Done
69
+ > =>
70
+ Channel.suspend(() => {
71
+ const packr = new Packr()
72
+ const loop: Channel.Channel<Chunk.Chunk<Uint8Array>, Chunk.Chunk<unknown>, IE | MsgPackError, IE, Done, Done> =
73
+ Channel
74
+ .readWithCause({
75
+ onInput: (input) =>
76
+ Channel.zipRight(
77
+ Channel.flatMap(
78
+ Effect.try({
79
+ try: () => Chunk.of(packr.pack(Chunk.toReadonlyArray(input))),
80
+ catch: (cause) => new MsgPackError({ reason: "Pack", cause })
81
+ }),
82
+ Channel.write
83
+ ),
84
+ loop
85
+ ),
86
+ onFailure: (cause) => Channel.failCause(cause),
87
+ onDone: Channel.succeed
88
+ })
89
+ return loop
90
+ })
91
+
92
+ /**
93
+ * @since 1.0.0
94
+ * @category constructors
95
+ */
96
+ export const packSchema = <A, I, R>(
97
+ schema: Schema.Schema<A, I, R>
98
+ ) =>
99
+ <IE = never, Done = unknown>(): Channel.Channel<
100
+ Chunk.Chunk<Uint8Array>,
101
+ Chunk.Chunk<A>,
102
+ IE | MsgPackError | ParseError,
103
+ IE,
104
+ Done,
105
+ Done,
106
+ R
107
+ > => Channel.pipeTo(ChannelSchema.encode(schema)(), pack())
108
+
109
+ /**
110
+ * @since 1.0.0
111
+ * @category constructors
112
+ */
113
+ export const unpack = <IE = never, Done = unknown>(): Channel.Channel<
114
+ Chunk.Chunk<unknown>,
115
+ Chunk.Chunk<Uint8Array>,
116
+ IE | MsgPackError,
117
+ IE,
118
+ Done,
119
+ Done
120
+ > =>
121
+ Channel.flatMap(
122
+ Channel.sync(() => new Unpackr()),
123
+ (packr) => {
124
+ let incomplete: Uint8Array | undefined = undefined
125
+ const unpack = (value: Chunk.Chunk<Uint8Array>) =>
126
+ Effect.try({
127
+ try: () =>
128
+ Chunk.flatMap(value, (buf) => {
129
+ if (incomplete !== undefined) {
130
+ const chunk = new Uint8Array(incomplete.length + buf.length)
131
+ chunk.set(incomplete)
132
+ chunk.set(buf, incomplete.length)
133
+ buf = chunk
134
+ incomplete = undefined
135
+ }
136
+ try {
137
+ return Chunk.unsafeFromArray(packr.unpackMultiple(buf).flat())
138
+ } catch (error_) {
139
+ const error: any = error_
140
+ if (error.incomplete) {
141
+ incomplete = buf.subarray(error.lastPosition)
142
+ return Chunk.unsafeFromArray(error.values ?? [])
143
+ }
144
+ throw error
145
+ }
146
+ }),
147
+ catch: (cause) => new MsgPackError({ reason: "Unpack", cause })
148
+ })
149
+
150
+ const loop: Channel.Channel<Chunk.Chunk<unknown>, Chunk.Chunk<Uint8Array>, IE | MsgPackError, IE, Done, Done> =
151
+ Channel.readWithCause({
152
+ onInput: (input: Chunk.Chunk<Uint8Array>) =>
153
+ Channel.zipRight(
154
+ Channel.flatMap(unpack(input), Channel.write),
155
+ loop
156
+ ),
157
+ onFailure: (cause) => Channel.failCause(cause),
158
+ onDone: Channel.succeed
159
+ })
160
+
161
+ return loop
162
+ }
163
+ )
164
+
165
+ /**
166
+ * @since 1.0.0
167
+ * @category constructors
168
+ */
169
+ export const unpackSchema = <A, I, R>(
170
+ schema: Schema.Schema<A, I, R>
171
+ ) =>
172
+ <IE = never, Done = unknown>(): Channel.Channel<
173
+ Chunk.Chunk<A>,
174
+ Chunk.Chunk<Uint8Array>,
175
+ MsgPackError | ParseError | IE,
176
+ IE,
177
+ Done,
178
+ Done,
179
+ R
180
+ > => Channel.pipeTo(unpack(), ChannelSchema.decodeUnknown(schema)())
181
+
182
+ /**
183
+ * @since 1.0.0
184
+ * @category combinators
185
+ */
186
+ export const duplex = <R, IE, OE, OutDone, InDone>(
187
+ self: Channel.Channel<Chunk.Chunk<Uint8Array>, Chunk.Chunk<Uint8Array>, OE, IE | MsgPackError, OutDone, InDone, R>
188
+ ): Channel.Channel<Chunk.Chunk<unknown>, Chunk.Chunk<unknown>, MsgPackError | OE, IE, OutDone, InDone, R> =>
189
+ Channel.pipeTo(
190
+ Channel.pipeTo(pack(), self),
191
+ unpack()
192
+ )
193
+
194
+ /**
195
+ * @since 1.0.0
196
+ * @category combinators
197
+ */
198
+ export const duplexSchema: {
199
+ /**
200
+ * @since 1.0.0
201
+ * @category combinators
202
+ */
203
+ <IA, II, IR, OA, OI, OR>(
204
+ options: {
205
+ readonly inputSchema: Schema.Schema<IA, II, IR>
206
+ readonly outputSchema: Schema.Schema<OA, OI, OR>
207
+ }
208
+ ): <R, InErr, OutErr, OutDone, InDone>(
209
+ self: Channel.Channel<
210
+ Chunk.Chunk<Uint8Array>,
211
+ Chunk.Chunk<Uint8Array>,
212
+ OutErr,
213
+ MsgPackError | ParseError | InErr,
214
+ OutDone,
215
+ InDone,
216
+ R
217
+ >
218
+ ) => Channel.Channel<
219
+ Chunk.Chunk<OA>,
220
+ Chunk.Chunk<IA>,
221
+ MsgPackError | ParseError | OutErr,
222
+ InErr,
223
+ OutDone,
224
+ InDone,
225
+ IR | OR | R
226
+ >
227
+ /**
228
+ * @since 1.0.0
229
+ * @category combinators
230
+ */
231
+ <R, InErr, OutErr, OutDone, InDone, IA, II, IR, OA, OI, OR>(
232
+ self: Channel.Channel<
233
+ Chunk.Chunk<Uint8Array>,
234
+ Chunk.Chunk<Uint8Array>,
235
+ OutErr,
236
+ MsgPackError | ParseError | InErr,
237
+ OutDone,
238
+ InDone,
239
+ R
240
+ >,
241
+ options: {
242
+ readonly inputSchema: Schema.Schema<IA, II, IR>
243
+ readonly outputSchema: Schema.Schema<OA, OI, OR>
244
+ }
245
+ ): Channel.Channel<
246
+ Chunk.Chunk<OA>,
247
+ Chunk.Chunk<IA>,
248
+ MsgPackError | ParseError | OutErr,
249
+ InErr,
250
+ OutDone,
251
+ InDone,
252
+ R | IR | OR
253
+ >
254
+ } = dual<
255
+ /**
256
+ * @since 1.0.0
257
+ * @category combinators
258
+ */
259
+ <IA, II, IR, OA, OI, OR>(
260
+ options: {
261
+ readonly inputSchema: Schema.Schema<IA, II, IR>
262
+ readonly outputSchema: Schema.Schema<OA, OI, OR>
263
+ }
264
+ ) => <R, InErr, OutErr, OutDone, InDone>(
265
+ self: Channel.Channel<
266
+ Chunk.Chunk<Uint8Array>,
267
+ Chunk.Chunk<Uint8Array>,
268
+ OutErr,
269
+ MsgPackError | ParseError | InErr,
270
+ OutDone,
271
+ InDone,
272
+ R
273
+ >
274
+ ) => Channel.Channel<
275
+ Chunk.Chunk<OA>,
276
+ Chunk.Chunk<IA>,
277
+ MsgPackError | ParseError | OutErr,
278
+ InErr,
279
+ OutDone,
280
+ InDone,
281
+ R | IR | OR
282
+ >,
283
+ /**
284
+ * @since 1.0.0
285
+ * @category combinators
286
+ */
287
+ <R, InErr, OutErr, OutDone, InDone, IA, II, IR, OA, OI, OR>(
288
+ self: Channel.Channel<
289
+ Chunk.Chunk<Uint8Array>,
290
+ Chunk.Chunk<Uint8Array>,
291
+ OutErr,
292
+ MsgPackError | ParseError | InErr,
293
+ OutDone,
294
+ InDone,
295
+ R
296
+ >,
297
+ options: {
298
+ readonly inputSchema: Schema.Schema<IA, II, IR>
299
+ readonly outputSchema: Schema.Schema<OA, OI, OR>
300
+ }
301
+ ) => Channel.Channel<
302
+ Chunk.Chunk<OA>,
303
+ Chunk.Chunk<IA>,
304
+ MsgPackError | ParseError | OutErr,
305
+ InErr,
306
+ OutDone,
307
+ InDone,
308
+ R | IR | OR
309
+ >
310
+ >(2, <R, InErr, OutErr, OutDone, InDone, IA, II, IR, OA, OI, OR>(
311
+ self: Channel.Channel<
312
+ Chunk.Chunk<Uint8Array>,
313
+ Chunk.Chunk<Uint8Array>,
314
+ OutErr,
315
+ MsgPackError | ParseError | InErr,
316
+ OutDone,
317
+ InDone,
318
+ R
319
+ >,
320
+ options: {
321
+ readonly inputSchema: Schema.Schema<IA, II, IR>
322
+ readonly outputSchema: Schema.Schema<OA, OI, OR>
323
+ }
324
+ ): Channel.Channel<
325
+ Chunk.Chunk<OA>,
326
+ Chunk.Chunk<IA>,
327
+ MsgPackError | ParseError | OutErr,
328
+ InErr,
329
+ OutDone,
330
+ InDone,
331
+ R | IR | OR
332
+ > => ChannelSchema.duplexUnknown(duplex(self), options))
333
+
334
+ /**
335
+ * @since 1.0.0
336
+ * @category schemas
337
+ */
338
+ export interface schema<S extends Schema.Schema.Any> extends Schema.transformOrFail<Schema.Schema<Uint8Array>, S> {}
339
+
340
+ /**
341
+ * @since 1.0.0
342
+ * @category schemas
343
+ */
344
+ export const schema = <S extends Schema.Schema.Any>(schema: S): schema<S> =>
345
+ Schema.transformOrFail(
346
+ Schema.Uint8ArrayFromSelf,
347
+ schema,
348
+ {
349
+ decode(fromA, _, ast) {
350
+ return ParseResult.try({
351
+ try: () => Msgpackr.decode(fromA) as Schema.Schema.Encoded<S>,
352
+ catch: (cause) =>
353
+ new ParseResult.Type(
354
+ ast,
355
+ fromA,
356
+ Predicate.hasProperty(cause, "message") ? String(cause.message) : String(cause)
357
+ )
358
+ })
359
+ },
360
+ encode(toI, _, ast) {
361
+ return ParseResult.try({
362
+ try: () => Msgpackr.encode(toI),
363
+ catch: (cause) =>
364
+ new ParseResult.Type(
365
+ ast,
366
+ toI,
367
+ Predicate.hasProperty(cause, "message") ? String(cause.message) : String(cause)
368
+ )
369
+ })
370
+ }
371
+ }
372
+ )