@effect/platform-node-shared 4.0.0-beta.4 → 4.0.0-beta.40

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.
@@ -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
  })
@@ -341,7 +341,7 @@ const makeFile = (() => {
341
341
  Error.systemError({
342
342
  module: "FileSystem",
343
343
  method: "writeAll",
344
- kind: "WriteZero",
344
+ _tag: "WriteZero",
345
345
  pathOrDescriptor: this.fd,
346
346
  description: "write returned 0 bytes written"
347
347
  })
@@ -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": {
@@ -553,7 +555,7 @@ const watchNode = (path: string) =>
553
555
  Cause.fail(
554
556
  Error.systemError({
555
557
  module: "FileSystem",
556
- kind: "Unknown",
558
+ _tag: "Unknown",
557
559
  method: "watch",
558
560
  pathOrDescriptor: path,
559
561
  cause: error
@@ -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
  }))
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
@@ -9,6 +9,7 @@ import * as Effect from "effect/Effect"
9
9
  import * as FiberSet from "effect/FiberSet"
10
10
  import * as Function from "effect/Function"
11
11
  import { identity } from "effect/Function"
12
+ import * as Latch from "effect/Latch"
12
13
  import * as Layer from "effect/Layer"
13
14
  import * as Scope from "effect/Scope"
14
15
  import * as ServiceMap from "effect/ServiceMap"
@@ -36,7 +37,7 @@ 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(
@@ -82,12 +83,12 @@ 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 latch = Latch.makeUnsafe(false)
91
92
  const openServices = fiber.services as ServiceMap.ServiceMap<RO>
92
93
 
93
94
  const run = <R, E, _>(handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void, opts?: {
@@ -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") })
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,33 +15,38 @@ 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
- kind: "Unknown",
24
- cause
25
- })
26
- }),
27
- stderr: fromWritable({
28
- evaluate: () => process.stderr,
29
- onError: (cause) =>
30
- systemError({
31
- module: "Stdio",
32
- method: "stderr",
33
- kind: "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) =>
40
46
  systemError({
41
47
  module: "Stdio",
42
48
  method: "stdin",
43
- kind: "Unknown",
49
+ _tag: "Unknown",
44
50
  cause
45
51
  }),
46
52
  closeOnDone: false
package/src/NodeStream.ts CHANGED
@@ -9,6 +9,7 @@ import * as Exit from "effect/Exit"
9
9
  import * as Fiber from "effect/Fiber"
10
10
  import type { SizeInput } from "effect/FileSystem"
11
11
  import { dual, type LazyArg } from "effect/Function"
12
+ import * as Latch from "effect/Latch"
12
13
  import * as MutableRef from "effect/MutableRef"
13
14
  import * as Pull from "effect/Pull"
14
15
  import * as Scope from "effect/Scope"
@@ -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,7 +357,7 @@ 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(
@@ -362,7 +365,7 @@ class StreamAdapter<E, R> extends Readable {
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()
@@ -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)
@@ -1,4 +1,4 @@
1
- import type { SystemError, SystemErrorKind } from "effect/PlatformError"
1
+ import type { SystemError, SystemErrorTag } from "effect/PlatformError"
2
2
  import * as PlatformError from "effect/PlatformError"
3
3
  import type { PathLike } from "node:fs"
4
4
 
@@ -8,7 +8,7 @@ export const handleErrnoException = (module: SystemError["module"], method: stri
8
8
  err: NodeJS.ErrnoException,
9
9
  [path]: [path: PathLike | number, ...args: Array<any>]
10
10
  ): PlatformError.PlatformError => {
11
- let reason: SystemErrorKind = "Unknown"
11
+ let reason: SystemErrorTag = "Unknown"
12
12
 
13
13
  switch (err.code) {
14
14
  case "ENOENT":
@@ -41,7 +41,7 @@ export const handleErrnoException = (module: SystemError["module"], method: stri
41
41
  }
42
42
 
43
43
  return PlatformError.systemError({
44
- kind: reason,
44
+ _tag: reason,
45
45
  module,
46
46
  method,
47
47
  pathOrDescriptor: path as string | number,