@effect/platform-node-shared 4.0.0-beta.5 → 4.0.0-beta.50

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 (44) hide show
  1. package/dist/NodeChildProcessSpawner.d.ts.map +1 -1
  2. package/dist/NodeChildProcessSpawner.js +91 -115
  3. package/dist/NodeChildProcessSpawner.js.map +1 -1
  4. package/dist/NodeClusterSocket.d.ts.map +1 -1
  5. package/dist/NodeClusterSocket.js +4 -3
  6. package/dist/NodeClusterSocket.js.map +1 -1
  7. package/dist/NodeFileSystem.d.ts.map +1 -1
  8. package/dist/NodeFileSystem.js +18 -22
  9. package/dist/NodeFileSystem.js.map +1 -1
  10. package/dist/NodeRuntime.d.ts.map +1 -1
  11. package/dist/NodeRuntime.js +2 -6
  12. package/dist/NodeRuntime.js.map +1 -1
  13. package/dist/NodeSink.d.ts.map +1 -1
  14. package/dist/NodeSink.js +7 -1
  15. package/dist/NodeSink.js.map +1 -1
  16. package/dist/NodeSocket.d.ts +4 -4
  17. package/dist/NodeSocket.d.ts.map +1 -1
  18. package/dist/NodeSocket.js +9 -8
  19. package/dist/NodeSocket.js.map +1 -1
  20. package/dist/NodeSocketServer.d.ts +2 -2
  21. package/dist/NodeSocketServer.d.ts.map +1 -1
  22. package/dist/NodeSocketServer.js +6 -6
  23. package/dist/NodeSocketServer.js.map +1 -1
  24. package/dist/NodeStdio.d.ts +0 -3
  25. package/dist/NodeStdio.d.ts.map +1 -1
  26. package/dist/NodeStdio.js +8 -4
  27. package/dist/NodeStdio.js.map +1 -1
  28. package/dist/NodeStream.d.ts.map +1 -1
  29. package/dist/NodeStream.js +8 -6
  30. package/dist/NodeStream.js.map +1 -1
  31. package/dist/NodeTerminal.d.ts.map +1 -1
  32. package/dist/NodeTerminal.js +2 -1
  33. package/dist/NodeTerminal.js.map +1 -1
  34. package/package.json +6 -6
  35. package/src/NodeChildProcessSpawner.ts +121 -135
  36. package/src/NodeClusterSocket.ts +4 -3
  37. package/src/NodeFileSystem.ts +25 -24
  38. package/src/NodeRuntime.ts +2 -6
  39. package/src/NodeSink.ts +7 -0
  40. package/src/NodeSocket.ts +11 -10
  41. package/src/NodeSocketServer.ts +6 -6
  42. package/src/NodeStdio.ts +26 -20
  43. package/src/NodeStream.ts +11 -8
  44. package/src/NodeTerminal.ts +2 -1
@@ -285,19 +285,19 @@ const makeFile = (() => {
285
285
  const position = this.position
286
286
  return Effect.map(
287
287
  nodeReadAlloc(this.fd, { buffer, position }),
288
- (bytesRead): Buffer | undefined => {
288
+ (bytesRead): Option.Option<Buffer> => {
289
289
  if (bytesRead === 0) {
290
- return undefined
290
+ return Option.none()
291
291
  }
292
292
 
293
293
  this.position = position + BigInt(bytesRead)
294
294
  if (bytesRead === sizeNumber) {
295
- return buffer
295
+ return Option.some(buffer)
296
296
  }
297
297
 
298
298
  const dst = Buffer.allocUnsafeSlow(bytesRead)
299
299
  buffer.copy(dst, 0, 0, bytesRead)
300
- return dst
300
+ return Option.some(dst)
301
301
  }
302
302
  )
303
303
  })
@@ -468,19 +468,19 @@ const makeFileInfo = (stat: NFS.Stats): FileSystem.File.Info => ({
468
468
  stat.isSocket() ?
469
469
  "Socket" :
470
470
  "Unknown",
471
- mtime: stat.mtime,
472
- atime: stat.atime,
473
- birthtime: stat.birthtime,
471
+ mtime: Option.fromNullishOr(stat.mtime),
472
+ atime: Option.fromNullishOr(stat.atime),
473
+ birthtime: Option.fromNullishOr(stat.birthtime),
474
474
  dev: stat.dev,
475
- rdev: stat.rdev,
476
- ino: stat.ino,
475
+ rdev: Option.fromNullishOr(stat.rdev),
476
+ ino: Option.fromNullishOr(stat.ino),
477
477
  mode: stat.mode,
478
- nlink: stat.nlink,
479
- uid: stat.uid,
480
- gid: stat.gid,
478
+ nlink: Option.fromNullishOr(stat.nlink),
479
+ uid: Option.fromNullishOr(stat.uid),
480
+ gid: Option.fromNullishOr(stat.gid),
481
481
  size: FileSystem.Size(stat.size),
482
- blksize: FileSystem.Size(stat.blksize),
483
- blocks: stat.blocks
482
+ blksize: stat.blksize !== undefined ? Option.some(FileSystem.Size(stat.blksize)) : Option.none(),
483
+ blocks: Option.fromNullishOr(stat.blocks)
484
484
  })
485
485
  const stat = (() => {
486
486
  const nodeStat = effectify(
@@ -531,7 +531,9 @@ const watchNode = (path: string) =>
531
531
  Stream.callback<FileSystem.WatchEvent, Error.PlatformError>((queue) =>
532
532
  Effect.acquireRelease(
533
533
  Effect.sync(() => {
534
- const watcher = NFS.watch(path, {}, (event, path) => {
534
+ const watcher = NFS.watch(path, {
535
+ recursive: true
536
+ }, (event, path) => {
535
537
  if (!path) return
536
538
  switch (event) {
537
539
  case "rename": {
@@ -570,15 +572,14 @@ const watchNode = (path: string) =>
570
572
  )
571
573
  )
572
574
 
573
- const watch = (backend: FileSystem.WatchBackend["Service"] | undefined, path: string) =>
575
+ const watch = (backend: Option.Option<FileSystem.WatchBackend["Service"]>, path: string) =>
574
576
  stat(path).pipe(
575
- Effect.map((stat) => {
576
- if (backend) {
577
- const stream = backend.register(path, stat)
578
- if (stream) return stream
579
- }
580
- return watchNode(path)
581
- }),
577
+ Effect.map((stat) =>
578
+ backend.pipe(
579
+ Option.flatMap((_) => _.register(path, stat)),
580
+ Option.getOrElse(() => watchNode(path))
581
+ )
582
+ ),
582
583
  Stream.unwrap
583
584
  )
584
585
 
@@ -628,7 +629,7 @@ const makeFileSystem = Effect.map(Effect.serviceOption(FileSystem.WatchBackend),
628
629
  truncate,
629
630
  utimes,
630
631
  watch(path) {
631
- return watch(Option.getOrUndefined(backend), path)
632
+ return watch(backend, path)
632
633
  },
633
634
  writeFile
634
635
  }))
@@ -37,10 +37,8 @@ export const runMain: {
37
37
  let receivedSignal = false
38
38
 
39
39
  fiber.addObserver((exit) => {
40
- if (!receivedSignal) {
41
- process.removeListener("SIGINT", onSigint)
42
- process.removeListener("SIGTERM", onSigint)
43
- }
40
+ process.removeListener("SIGINT", onSigint)
41
+ process.removeListener("SIGTERM", onSigint)
44
42
  teardown(exit, (code) => {
45
43
  if (receivedSignal || code !== 0) {
46
44
  process.exit(code)
@@ -50,8 +48,6 @@ export const runMain: {
50
48
 
51
49
  function onSigint() {
52
50
  receivedSignal = true
53
- process.removeListener("SIGINT", onSigint)
54
- process.removeListener("SIGTERM", onSigint)
55
51
  fiber.interruptUnsafe(fiber.id)
56
52
  }
57
53
 
package/src/NodeSink.ts CHANGED
@@ -66,6 +66,13 @@ export const pullIntoWritable = <A, IE, E>(options: {
66
66
  })
67
67
  }),
68
68
  Effect.forever({ disableYield: true }),
69
+ Effect.raceFirst(Effect.callback<never, E>((resume) => {
70
+ const onError = (error: unknown) => resume(Effect.fail(options.onError(error)))
71
+ options.writable.once("error", onError)
72
+ return Effect.sync(() => {
73
+ options.writable.off("error", onError)
74
+ })
75
+ })),
69
76
  options.endOnDone !== false ?
70
77
  Pull.catchDone((_) => {
71
78
  if ("closed" in options.writable && options.writable.closed) {
package/src/NodeSocket.ts CHANGED
@@ -3,15 +3,16 @@
3
3
  */
4
4
  import type { Array } from "effect"
5
5
  import * as Channel from "effect/Channel"
6
+ import * as Context from "effect/Context"
6
7
  import * as Deferred from "effect/Deferred"
7
8
  import type * as Duration from "effect/Duration"
8
9
  import * as Effect from "effect/Effect"
9
10
  import * as FiberSet from "effect/FiberSet"
10
11
  import * as Function from "effect/Function"
11
12
  import { identity } from "effect/Function"
13
+ import * as Latch from "effect/Latch"
12
14
  import * as Layer from "effect/Layer"
13
15
  import * as Scope from "effect/Scope"
14
- import * as ServiceMap from "effect/ServiceMap"
15
16
  import * as Socket from "effect/unstable/socket/Socket"
16
17
  import * as Net from "node:net"
17
18
  import type { Duplex } from "node:stream"
@@ -26,7 +27,7 @@ export * as NodeWS from "ws"
26
27
  * @since 1.0.0
27
28
  * @category tags
28
29
  */
29
- export class NetSocket extends ServiceMap.Service<NetSocket, Net.Socket>()(
30
+ export class NetSocket extends Context.Service<NetSocket, Net.Socket>()(
30
31
  "@effect/platform-node/NodeSocket/NetSocket"
31
32
  ) {}
32
33
 
@@ -36,15 +37,15 @@ export class NetSocket extends ServiceMap.Service<NetSocket, Net.Socket>()(
36
37
  */
37
38
  export const makeNet = (
38
39
  options: Net.NetConnectOpts & {
39
- readonly openTimeout?: Duration.DurationInput | undefined
40
+ readonly openTimeout?: Duration.Input | undefined
40
41
  }
41
42
  ): Effect.Effect<Socket.Socket> =>
42
43
  fromDuplex(
43
- Effect.servicesWith((services: ServiceMap.ServiceMap<Scope.Scope>) => {
44
+ Effect.contextWith((context: Context.Context<Scope.Scope>) => {
44
45
  let conn: Net.Socket | undefined
45
46
  return Effect.flatMap(
46
47
  Scope.addFinalizer(
47
- ServiceMap.get(services, Scope.Scope),
48
+ Context.get(context, Scope.Scope),
48
49
  Effect.sync(() => {
49
50
  if (!conn) return
50
51
  if (conn.closed === false) {
@@ -82,13 +83,13 @@ export const makeNet = (
82
83
  export const fromDuplex = <RO>(
83
84
  open: Effect.Effect<Duplex, Socket.SocketError, RO>,
84
85
  options?: {
85
- readonly openTimeout?: Duration.DurationInput | undefined
86
+ readonly openTimeout?: Duration.Input | undefined
86
87
  }
87
88
  ): Effect.Effect<Socket.Socket, never, Exclude<RO, Scope.Scope>> =>
88
89
  Effect.withFiber<Socket.Socket, never, Exclude<RO, Scope.Scope>>((fiber) => {
89
90
  let currentSocket: Duplex | undefined
90
- const latch = Effect.makeLatchUnsafe(false)
91
- const openServices = fiber.services as ServiceMap.ServiceMap<RO>
91
+ const latch = Latch.makeUnsafe(false)
92
+ const openServices = fiber.context as Context.Context<RO>
92
93
 
93
94
  const run = <R, E, _>(handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void, opts?: {
94
95
  readonly onOpen?: Effect.Effect<void> | undefined
@@ -112,7 +113,7 @@ export const fromDuplex = <RO>(
112
113
  options?.openTimeout ?
113
114
  Effect.timeoutOrElse({
114
115
  duration: options.openTimeout,
115
- onTimeout: () =>
116
+ orElse: () =>
116
117
  Effect.fail(
117
118
  new Socket.SocketError({
118
119
  reason: new Socket.SocketOpenError({ kind: "Timeout", cause: new Error("Connection timed out") })
@@ -165,7 +166,7 @@ export const fromDuplex = <RO>(
165
166
  )
166
167
  }
167
168
  })).pipe(
168
- Effect.updateServices((input: ServiceMap.ServiceMap<R>) => ServiceMap.merge(openServices, input)),
169
+ Effect.updateContext((input: Context.Context<R>) => Context.merge(openServices, input)),
169
170
  Effect.onExit(() =>
170
171
  Effect.sync(() => {
171
172
  latch.closeUnsafe()
@@ -2,6 +2,7 @@
2
2
  * @since 1.0.0
3
3
  */
4
4
  import type { Cause } from "effect/Cause"
5
+ import * as Context from "effect/Context"
5
6
  import * as Deferred from "effect/Deferred"
6
7
  import * as Effect from "effect/Effect"
7
8
  import * as Exit from "effect/Exit"
@@ -11,7 +12,6 @@ import * as Function from "effect/Function"
11
12
  import * as Layer from "effect/Layer"
12
13
  import * as References from "effect/References"
13
14
  import * as Scope from "effect/Scope"
14
- import * as ServiceMap from "effect/ServiceMap"
15
15
  import * as Socket from "effect/unstable/socket/Socket"
16
16
  import * as SocketServer from "effect/unstable/socket/SocketServer"
17
17
  import type * as Http from "node:http"
@@ -23,7 +23,7 @@ import { NodeWS } from "./NodeSocket.ts"
23
23
  * @since 1.0.0
24
24
  * @category tags
25
25
  */
26
- export class IncomingMessage extends ServiceMap.Service<
26
+ export class IncomingMessage extends Context.Service<
27
27
  IncomingMessage,
28
28
  Http.IncomingMessage
29
29
  >()("@effect/platform-node-shared/NodeSocketServer/IncomingMessage") {}
@@ -69,7 +69,7 @@ export const make = Effect.fnUntraced(function*(
69
69
 
70
70
  const run = Effect.fnUntraced(function*<R, E, _>(handler: (socket: Socket.Socket) => Effect.Effect<_, E, R>) {
71
71
  const scope = yield* Scope.make()
72
- const services = ServiceMap.omit(Scope.Scope)(yield* Effect.services<R>()) as ServiceMap.ServiceMap<R>
72
+ const services = Context.omit(Scope.Scope)(yield* Effect.context<R>()) as Context.Context<R>
73
73
  const trackFiber = Fiber.runIn(scope)
74
74
  const prevOnConnection = onConnection
75
75
  onConnection = function(conn: Net.Socket) {
@@ -109,7 +109,7 @@ export const make = Effect.fnUntraced(function*(
109
109
  ),
110
110
  Effect.flatMap(handler),
111
111
  Effect.catchCause(reportUnhandledError),
112
- Effect.runForkWith(ServiceMap.add(services, NodeSocket.NetSocket, conn)),
112
+ Effect.runForkWith(Context.add(services, NodeSocket.NetSocket, conn)),
113
113
  trackFiber
114
114
  )
115
115
  }
@@ -202,7 +202,7 @@ export const makeWebSocket: (
202
202
 
203
203
  const run = Effect.fnUntraced(function*<R, E, _>(handler: (socket: Socket.Socket) => Effect.Effect<_, E, R>) {
204
204
  const scope = yield* Scope.make()
205
- const services = ServiceMap.omit(Scope.Scope)(yield* Effect.services<R>()) as ServiceMap.ServiceMap<R>
205
+ const services = Context.omit(Scope.Scope)(yield* Effect.context<R>()) as Context.Context<R>
206
206
  const trackFiber = Fiber.runIn(scope)
207
207
  const prevOnConnection = onConnection
208
208
  onConnection = function(conn: globalThis.WebSocket, req: Http.IncomingMessage) {
@@ -221,7 +221,7 @@ export const makeWebSocket: (
221
221
  ),
222
222
  Effect.flatMap(handler),
223
223
  Effect.catchCause(reportUnhandledError),
224
- Effect.runForkWith(ServiceMap.makeUnsafe(map)),
224
+ Effect.runForkWith(Context.makeUnsafe(map)),
225
225
  trackFiber
226
226
  )
227
227
  }
package/src/NodeStdio.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * @since 1.0.0
3
3
  */
4
+ import * as Effect from "effect/Effect"
4
5
  import * as Layer from "effect/Layer"
5
6
  import { systemError } from "effect/PlatformError"
6
7
  import * as Stdio from "effect/Stdio"
@@ -14,26 +15,31 @@ import { fromReadable } from "./NodeStream.ts"
14
15
  export const layer: Layer.Layer<Stdio.Stdio> = Layer.succeed(
15
16
  Stdio.Stdio,
16
17
  Stdio.make({
17
- stdout: fromWritable({
18
- evaluate: () => process.stdout,
19
- onError: (cause) =>
20
- systemError({
21
- module: "Stdio",
22
- method: "stdout",
23
- _tag: "Unknown",
24
- cause
25
- })
26
- }),
27
- stderr: fromWritable({
28
- evaluate: () => process.stderr,
29
- onError: (cause) =>
30
- systemError({
31
- module: "Stdio",
32
- method: "stderr",
33
- _tag: "Unknown",
34
- cause
35
- })
36
- }),
18
+ args: Effect.sync(() => process.argv.slice(2)),
19
+ stdout: (options) =>
20
+ fromWritable({
21
+ evaluate: () => process.stdout,
22
+ onError: (cause) =>
23
+ systemError({
24
+ module: "Stdio",
25
+ method: "stdout",
26
+ _tag: "Unknown",
27
+ cause
28
+ }),
29
+ endOnDone: options?.endOnDone ?? false
30
+ }),
31
+ stderr: (options) =>
32
+ fromWritable({
33
+ evaluate: () => process.stderr,
34
+ onError: (cause) =>
35
+ systemError({
36
+ module: "Stdio",
37
+ method: "stderr",
38
+ _tag: "Unknown",
39
+ cause
40
+ }),
41
+ endOnDone: options?.endOnDone ?? false
42
+ }),
37
43
  stdin: fromReadable({
38
44
  evaluate: () => process.stdin,
39
45
  onError: (cause) =>
package/src/NodeStream.ts CHANGED
@@ -4,15 +4,16 @@
4
4
  import * as Arr from "effect/Array"
5
5
  import * as Cause from "effect/Cause"
6
6
  import * as Channel from "effect/Channel"
7
+ import * as Context from "effect/Context"
7
8
  import * as Effect from "effect/Effect"
8
9
  import * as Exit from "effect/Exit"
9
10
  import * as Fiber from "effect/Fiber"
10
11
  import type { SizeInput } from "effect/FileSystem"
11
12
  import { dual, type LazyArg } from "effect/Function"
13
+ import * as Latch from "effect/Latch"
12
14
  import * as MutableRef from "effect/MutableRef"
13
15
  import * as Pull from "effect/Pull"
14
16
  import * as Scope from "effect/Scope"
15
- import * as ServiceMap from "effect/ServiceMap"
16
17
  import * as Stream from "effect/Stream"
17
18
  import type { Duplex } from "node:stream"
18
19
  import { Readable } from "node:stream"
@@ -169,7 +170,7 @@ export const pipeThroughSimple: {
169
170
  */
170
171
  export const toReadable = <E, R>(stream: Stream.Stream<string | Uint8Array, E, R>): Effect.Effect<Readable, never, R> =>
171
172
  Effect.map(
172
- Effect.services<R>(),
173
+ Effect.context<R>(),
173
174
  (context) => new StreamAdapter(context, stream)
174
175
  )
175
176
 
@@ -179,7 +180,7 @@ export const toReadable = <E, R>(stream: Stream.Stream<string | Uint8Array, E, R
179
180
  */
180
181
  export const toReadableNever = <E>(stream: Stream.Stream<string | Uint8Array, E, never>): Readable =>
181
182
  new StreamAdapter(
182
- ServiceMap.empty(),
183
+ Context.empty(),
183
184
  stream
184
185
  )
185
186
 
@@ -301,9 +302,11 @@ const readableToPullUnsafe = <A, E>(options: {
301
302
  readonly closeOnDone?: boolean | undefined
302
303
  }) => {
303
304
  const readable = options.readable as Readable
305
+ if (readable.readableEnded) return Effect.succeed(Cause.done())
306
+
304
307
  const closeOnDone = options.closeOnDone ?? true
305
308
  const exit = options.exit ?? MutableRef.make(undefined)
306
- const latch = Effect.makeLatchUnsafe(false)
309
+ const latch = Latch.makeUnsafe(false)
307
310
  function onReadable() {
308
311
  latch.openUnsafe()
309
312
  }
@@ -354,15 +357,15 @@ const readableToPullUnsafe = <A, E>(options: {
354
357
  }
355
358
 
356
359
  class StreamAdapter<E, R> extends Readable {
357
- private readonly readLatch: Effect.Latch
360
+ private readonly readLatch: Latch.Latch
358
361
  private fiber: Fiber.Fiber<void, E> | undefined = undefined
359
362
 
360
363
  constructor(
361
- context: ServiceMap.ServiceMap<R>,
364
+ context: Context.Context<R>,
362
365
  stream: Stream.Stream<Uint8Array | string, E, R>
363
366
  ) {
364
367
  super({})
365
- this.readLatch = Effect.makeLatchUnsafe(false)
368
+ this.readLatch = Latch.makeUnsafe(false)
366
369
  this.fiber = Stream.runForEachArray(stream, (chunk) =>
367
370
  this.readLatch.whenOpen(Effect.sync(() => {
368
371
  this.readLatch.closeUnsafe()
@@ -376,7 +379,7 @@ class StreamAdapter<E, R> extends Readable {
376
379
  }
377
380
  }))).pipe(
378
381
  this.readLatch.whenOpen,
379
- Effect.provideServices(context),
382
+ Effect.provideContext(context),
380
383
  Effect.runFork
381
384
  )
382
385
  this.fiber.addObserver((exit) => {
@@ -4,6 +4,7 @@
4
4
  import type * as Cause from "effect/Cause"
5
5
  import * as Effect from "effect/Effect"
6
6
  import * as Layer from "effect/Layer"
7
+ import * as Option from "effect/Option"
7
8
  import { badArgument, type PlatformError } from "effect/PlatformError"
8
9
  import * as Predicate from "effect/Predicate"
9
10
  import * as Queue from "effect/Queue"
@@ -52,7 +53,7 @@ export const make: (
52
53
  const queue = yield* Queue.make<Terminal.UserInput, Cause.Done>()
53
54
  const handleKeypress = (s: string | undefined, k: readline.Key) => {
54
55
  const userInput = {
55
- input: s,
56
+ input: Option.fromUndefinedOr(s),
56
57
  key: { name: k.name ?? "", ctrl: !!k.ctrl, meta: !!k.meta, shift: !!k.shift }
57
58
  }
58
59
  Queue.offerUnsafe(queue, userInput)