@effect/platform-node 0.1.1 → 0.3.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 (61) hide show
  1. package/Command.d.ts +111 -0
  2. package/Command.d.ts.map +1 -0
  3. package/Command.js +103 -0
  4. package/Command.js.map +1 -0
  5. package/CommandExecutor.d.ts +44 -0
  6. package/CommandExecutor.d.ts.map +1 -0
  7. package/CommandExecutor.js +23 -0
  8. package/CommandExecutor.js.map +1 -0
  9. package/NodeContext.d.ts +16 -0
  10. package/NodeContext.d.ts.map +1 -0
  11. package/NodeContext.js +24 -0
  12. package/NodeContext.js.map +1 -0
  13. package/internal/commandExecutor.d.ts +2 -0
  14. package/internal/commandExecutor.d.ts.map +1 -0
  15. package/internal/commandExecutor.js +145 -0
  16. package/internal/commandExecutor.js.map +1 -0
  17. package/internal/error.d.ts +2 -0
  18. package/internal/error.d.ts.map +1 -0
  19. package/internal/error.js +44 -0
  20. package/internal/error.js.map +1 -0
  21. package/internal/fileSystem.js +28 -62
  22. package/internal/fileSystem.js.map +1 -1
  23. package/internal/path.js +16 -10
  24. package/internal/path.js.map +1 -1
  25. package/internal/runtime.js +2 -2
  26. package/internal/runtime.js.map +1 -1
  27. package/internal/sink.js +3 -3
  28. package/internal/sink.js.map +1 -1
  29. package/internal/stream.js +6 -1
  30. package/internal/stream.js.map +1 -1
  31. package/mjs/Command.mjs +85 -0
  32. package/mjs/Command.mjs.map +1 -0
  33. package/mjs/CommandExecutor.mjs +13 -0
  34. package/mjs/CommandExecutor.mjs.map +1 -0
  35. package/mjs/NodeContext.mjs +15 -0
  36. package/mjs/NodeContext.mjs.map +1 -0
  37. package/mjs/internal/commandExecutor.mjs +136 -0
  38. package/mjs/internal/commandExecutor.mjs.map +1 -0
  39. package/mjs/internal/error.mjs +37 -0
  40. package/mjs/internal/error.mjs.map +1 -0
  41. package/mjs/internal/fileSystem.mjs +28 -62
  42. package/mjs/internal/fileSystem.mjs.map +1 -1
  43. package/mjs/internal/path.mjs +16 -10
  44. package/mjs/internal/path.mjs.map +1 -1
  45. package/mjs/internal/runtime.mjs +2 -2
  46. package/mjs/internal/runtime.mjs.map +1 -1
  47. package/mjs/internal/sink.mjs +3 -3
  48. package/mjs/internal/sink.mjs.map +1 -1
  49. package/mjs/internal/stream.mjs +6 -1
  50. package/mjs/internal/stream.mjs.map +1 -1
  51. package/package.json +5 -5
  52. package/src/Command.ts +114 -0
  53. package/src/CommandExecutor.ts +49 -0
  54. package/src/NodeContext.ts +26 -0
  55. package/src/internal/commandExecutor.ts +202 -0
  56. package/src/internal/error.ts +51 -0
  57. package/src/internal/fileSystem.ts +28 -76
  58. package/src/internal/path.ts +8 -8
  59. package/src/internal/runtime.ts +2 -2
  60. package/src/internal/sink.ts +3 -3
  61. package/src/internal/stream.ts +6 -1
@@ -2,6 +2,7 @@ import { identity, pipe } from "@effect/data/Function"
2
2
  import * as Option from "@effect/data/Option"
3
3
  import * as Effect from "@effect/io/Effect"
4
4
  import * as Layer from "@effect/io/Layer"
5
+ import { handleErrnoException } from "@effect/platform-node/internal/error"
5
6
  import { effectify } from "@effect/platform/Effectify"
6
7
  import * as Error from "@effect/platform/Error"
7
8
  import * as FileSystem from "@effect/platform/FileSystem"
@@ -10,55 +11,6 @@ import * as NFS from "node:fs"
10
11
  import * as OS from "node:os"
11
12
  import * as Path from "node:path"
12
13
 
13
- // == errors
14
-
15
- const handleErrnoException = (method: string) =>
16
- (
17
- err: NodeJS.ErrnoException,
18
- [path]: [path: NFS.PathLike | number, ...args: Array<any>]
19
- ) => {
20
- let reason: Error.SystemErrorReason = "Unknown"
21
-
22
- switch (err.code) {
23
- case "ENOENT":
24
- reason = "NotFound"
25
- break
26
-
27
- case "EACCES":
28
- reason = "PermissionDenied"
29
- break
30
-
31
- case "EEXIST":
32
- reason = "AlreadyExists"
33
- break
34
-
35
- case "EISDIR":
36
- reason = "BadResource"
37
- break
38
-
39
- case "ENOTDIR":
40
- reason = "BadResource"
41
- break
42
-
43
- case "EBUSY":
44
- reason = "Busy"
45
- break
46
-
47
- case "ELOOP":
48
- reason = "BadResource"
49
- break
50
- }
51
-
52
- return Error.SystemError({
53
- reason,
54
- module: "FileSystem",
55
- method,
56
- pathOrDescriptor: path as string | number,
57
- syscall: err.syscall,
58
- message: err.message
59
- })
60
- }
61
-
62
14
  const handleBadArgument = (method: string) =>
63
15
  (err: unknown) =>
64
16
  Error.BadArgument({
@@ -72,7 +24,7 @@ const handleBadArgument = (method: string) =>
72
24
  const access = (() => {
73
25
  const nodeAccess = effectify(
74
26
  NFS.access,
75
- handleErrnoException("access"),
27
+ handleErrnoException("FileSystem", "access"),
76
28
  handleBadArgument("access")
77
29
  )
78
30
  return (path: string, options?: FileSystem.AccessFileOptions) => {
@@ -92,7 +44,7 @@ const access = (() => {
92
44
  const copy = (() => {
93
45
  const nodeCp = effectify(
94
46
  NFS.cp,
95
- handleErrnoException("copy"),
47
+ handleErrnoException("FileSystem", "copy"),
96
48
  handleBadArgument("copy")
97
49
  )
98
50
  return (fromPath: string, toPath: string, options?: FileSystem.CopyOptions) =>
@@ -108,7 +60,7 @@ const copy = (() => {
108
60
  const copyFile = (() => {
109
61
  const nodeCopyFile = effectify(
110
62
  NFS.copyFile,
111
- handleErrnoException("copyFile"),
63
+ handleErrnoException("FileSystem", "copyFile"),
112
64
  handleBadArgument("copyFile")
113
65
  )
114
66
  return (fromPath: string, toPath: string) => nodeCopyFile(fromPath, toPath)
@@ -119,7 +71,7 @@ const copyFile = (() => {
119
71
  const chmod = (() => {
120
72
  const nodeChmod = effectify(
121
73
  NFS.chmod,
122
- handleErrnoException("chmod"),
74
+ handleErrnoException("FileSystem", "chmod"),
123
75
  handleBadArgument("chmod")
124
76
  )
125
77
  return (path: string, mode: number) => nodeChmod(path, mode)
@@ -130,7 +82,7 @@ const chmod = (() => {
130
82
  const chown = (() => {
131
83
  const nodeChown = effectify(
132
84
  NFS.chown,
133
- handleErrnoException("chown"),
85
+ handleErrnoException("FileSystem", "chown"),
134
86
  handleBadArgument("chown")
135
87
  )
136
88
  return (path: string, uid: number, gid: number) => nodeChown(path, uid, gid)
@@ -141,7 +93,7 @@ const chown = (() => {
141
93
  const link = (() => {
142
94
  const nodeLink = effectify(
143
95
  NFS.link,
144
- handleErrnoException("link"),
96
+ handleErrnoException("FileSystem", "link"),
145
97
  handleBadArgument("link")
146
98
  )
147
99
  return (existingPath: string, newPath: string) => nodeLink(existingPath, newPath)
@@ -152,7 +104,7 @@ const link = (() => {
152
104
  const makeDirectory = (() => {
153
105
  const nodeMkdir = effectify(
154
106
  NFS.mkdir,
155
- handleErrnoException("makeDirectory"),
107
+ handleErrnoException("FileSystem", "makeDirectory"),
156
108
  handleBadArgument("makeDirectory")
157
109
  )
158
110
  return (path: string, options?: FileSystem.MakeDirectoryOptions) =>
@@ -167,7 +119,7 @@ const makeDirectory = (() => {
167
119
  const makeTempDirectoryFactory = (method: string) => {
168
120
  const nodeMkdtemp = effectify(
169
121
  NFS.mkdtemp,
170
- handleErrnoException(method),
122
+ handleErrnoException("FileSystem", method),
171
123
  handleBadArgument(method)
172
124
  )
173
125
  return (options?: FileSystem.MakeTempDirectoryOptions) =>
@@ -187,7 +139,7 @@ const makeTempDirectory = makeTempDirectoryFactory("makeTempDirectory")
187
139
  const removeFactory = (method: string) => {
188
140
  const nodeRm = effectify(
189
141
  NFS.rm,
190
- handleErrnoException(method),
142
+ handleErrnoException("FileSystem", method),
191
143
  handleBadArgument(method)
192
144
  )
193
145
  return (path: string, options?: FileSystem.RemoveOptions) =>
@@ -217,12 +169,12 @@ const makeTempDirectoryScoped = (() => {
217
169
  const openFactory = (method: string) => {
218
170
  const nodeOpen = effectify(
219
171
  NFS.open,
220
- handleErrnoException(method),
172
+ handleErrnoException("FileSystem", method),
221
173
  handleBadArgument(method)
222
174
  )
223
175
  const nodeClose = effectify(
224
176
  NFS.close,
225
- handleErrnoException(method),
177
+ handleErrnoException("FileSystem", method),
226
178
  handleBadArgument(method)
227
179
  )
228
180
 
@@ -241,26 +193,26 @@ const makeFile = (() => {
241
193
  const nodeReadFactory = (method: string) =>
242
194
  effectify(
243
195
  NFS.read,
244
- handleErrnoException(method),
196
+ handleErrnoException("FileSystem", method),
245
197
  handleBadArgument(method)
246
198
  )
247
199
  const nodeRead = nodeReadFactory("read")
248
200
  const nodeReadAlloc = nodeReadFactory("readAlloc")
249
201
  const nodeStat = effectify(
250
202
  NFS.fstat,
251
- handleErrnoException("stat"),
203
+ handleErrnoException("FileSystem", "stat"),
252
204
  handleBadArgument("stat")
253
205
  )
254
206
  const nodeTruncate = effectify(
255
207
  NFS.ftruncate,
256
- handleErrnoException("truncate"),
208
+ handleErrnoException("FileSystem", "truncate"),
257
209
  handleBadArgument("truncate")
258
210
  )
259
211
 
260
212
  const nodeWriteFactory = (method: string) =>
261
213
  effectify(
262
214
  NFS.write,
263
- handleErrnoException(method),
215
+ handleErrnoException("FileSystem", method),
264
216
  handleBadArgument(method)
265
217
  )
266
218
  const nodeWrite = nodeWriteFactory("write")
@@ -341,7 +293,7 @@ const makeFile = (() => {
341
293
  } else if (bytesWritten < buffer.length) {
342
294
  return this.writeAll(buffer.subarray(bytesWritten))
343
295
  }
344
- return Effect.unit()
296
+ return Effect.unit
345
297
  }
346
298
  )
347
299
  }
@@ -382,7 +334,7 @@ const makeTempFileScoped = (() => {
382
334
  const readDirectory = (() => {
383
335
  const nodeReadDirectory = effectify(
384
336
  NFS.readdir,
385
- handleErrnoException("readDirectory"),
337
+ handleErrnoException("FileSystem", "readDirectory"),
386
338
  handleBadArgument("readDirectory")
387
339
  )
388
340
 
@@ -399,7 +351,7 @@ const readFile = (path: string) =>
399
351
  try {
400
352
  NFS.readFile(path, { signal: controller.signal }, (err, data) => {
401
353
  if (err) {
402
- resume(Effect.fail(handleErrnoException("readFile")(err, [path])))
354
+ resume(Effect.fail(handleErrnoException("FileSystem", "readFile")(err, [path])))
403
355
  } else {
404
356
  resume(Effect.succeed(data))
405
357
  }
@@ -416,7 +368,7 @@ const readFile = (path: string) =>
416
368
  const readLink = (() => {
417
369
  const nodeReadLink = effectify(
418
370
  NFS.readlink,
419
- handleErrnoException("readLink"),
371
+ handleErrnoException("FileSystem", "readLink"),
420
372
  handleBadArgument("readLink")
421
373
  )
422
374
  return (path: string) => nodeReadLink(path)
@@ -427,7 +379,7 @@ const readLink = (() => {
427
379
  const realPath = (() => {
428
380
  const nodeRealPath = effectify(
429
381
  NFS.realpath,
430
- handleErrnoException("realPath"),
382
+ handleErrnoException("FileSystem", "realPath"),
431
383
  handleBadArgument("realPath")
432
384
  )
433
385
  return (path: string) => nodeRealPath(path)
@@ -438,7 +390,7 @@ const realPath = (() => {
438
390
  const rename = (() => {
439
391
  const nodeRename = effectify(
440
392
  NFS.rename,
441
- handleErrnoException("rename"),
393
+ handleErrnoException("FileSystem", "rename"),
442
394
  handleBadArgument("rename")
443
395
  )
444
396
  return (oldPath: string, newPath: string) => nodeRename(oldPath, newPath)
@@ -479,7 +431,7 @@ const makeFileInfo = (stat: NFS.Stats): FileSystem.File.Info => ({
479
431
  const stat = (() => {
480
432
  const nodeStat = effectify(
481
433
  NFS.stat,
482
- handleErrnoException("stat"),
434
+ handleErrnoException("FileSystem", "stat"),
483
435
  handleBadArgument("stat")
484
436
  )
485
437
  return (path: string) => Effect.map(nodeStat(path), makeFileInfo)
@@ -490,7 +442,7 @@ const stat = (() => {
490
442
  const symlink = (() => {
491
443
  const nodeSymlink = effectify(
492
444
  NFS.symlink,
493
- handleErrnoException("symlink"),
445
+ handleErrnoException("FileSystem", "symlink"),
494
446
  handleBadArgument("symlink")
495
447
  )
496
448
  return (target: string, path: string) => nodeSymlink(target, path)
@@ -501,7 +453,7 @@ const symlink = (() => {
501
453
  const truncate = (() => {
502
454
  const nodeTruncate = effectify(
503
455
  NFS.truncate,
504
- handleErrnoException("truncate"),
456
+ handleErrnoException("FileSystem", "truncate"),
505
457
  handleBadArgument("truncate")
506
458
  )
507
459
  return (path: string, length?: FileSystem.Size) => nodeTruncate(path, Number(length))
@@ -512,7 +464,7 @@ const truncate = (() => {
512
464
  const utimes = (() => {
513
465
  const nodeUtimes = effectify(
514
466
  NFS.utimes,
515
- handleErrnoException("utime"),
467
+ handleErrnoException("FileSystem", "utime"),
516
468
  handleBadArgument("utime")
517
469
  )
518
470
  return (path: string, atime: number | Date, mtime: number | Date) => nodeUtimes(path, atime, mtime)
@@ -530,9 +482,9 @@ const writeFile = (path: string, data: Uint8Array, options?: FileSystem.WriteFil
530
482
  mode: options?.mode
531
483
  }, (err) => {
532
484
  if (err) {
533
- resume(Effect.fail(handleErrnoException("writeFile")(err, [path])))
485
+ resume(Effect.fail(handleErrnoException("FileSystem", "writeFile")(err, [path])))
534
486
  } else {
535
- resume(Effect.unit())
487
+ resume(Effect.unit)
536
488
  }
537
489
  })
538
490
  } catch (err) {
@@ -10,26 +10,26 @@ import * as NodeUrl from "node:url"
10
10
  export const Path = Tag<_Path>()
11
11
 
12
12
  const fromFileUrl = (url: URL): Effect.Effect<never, BadArgument, string> =>
13
- Effect.tryCatch(
14
- () => NodeUrl.fileURLToPath(url),
15
- (error) =>
13
+ Effect.try({
14
+ try: () => NodeUrl.fileURLToPath(url),
15
+ catch: (error) =>
16
16
  BadArgument({
17
17
  module: "Path",
18
18
  method: "fromFileUrl",
19
19
  message: `${error}`
20
20
  })
21
- )
21
+ })
22
22
 
23
23
  const toFileUrl = (path: string): Effect.Effect<never, BadArgument, URL> =>
24
- Effect.tryCatch(
25
- () => NodeUrl.pathToFileURL(path),
26
- (error) =>
24
+ Effect.try({
25
+ try: () => NodeUrl.pathToFileURL(path),
26
+ catch: (error) =>
27
27
  BadArgument({
28
28
  module: "Path",
29
29
  method: "toFileUrl",
30
30
  message: `${error}`
31
31
  })
32
- )
32
+ })
33
33
 
34
34
  /** @internal */
35
35
  export const layerPosix = Layer.succeed(
@@ -30,9 +30,9 @@ export const runMain: RunMain = <E, A>(
30
30
  }
31
31
 
32
32
  const interruptAll = (id: FiberId.FiberId) =>
33
- Effect.flatMap(Fiber.roots(), (roots) => {
33
+ Effect.flatMap(Fiber.roots, (roots) => {
34
34
  if (roots.length === 0) {
35
- return Effect.unit()
35
+ return Effect.unit
36
36
  }
37
37
 
38
38
  return Fiber.interruptAllAs(roots, id)
@@ -36,11 +36,11 @@ const makeSinkWithRelease = <E, A>(
36
36
  const endWritable = (stream: Writable) =>
37
37
  Effect.async<never, never, void>((resume) => {
38
38
  if (stream.closed) {
39
- resume(Effect.unit())
39
+ resume(Effect.unit)
40
40
  return
41
41
  }
42
42
 
43
- stream.end(() => resume(Effect.unit()))
43
+ stream.end(() => resume(Effect.unit))
44
44
  })
45
45
 
46
46
  const write = <E, A>(stream: Writable, onError: (error: unknown) => E, encoding?: BufferEncoding) =>
@@ -50,7 +50,7 @@ const write = <E, A>(stream: Writable, onError: (error: unknown) => E, encoding?
50
50
  if (err) {
51
51
  resume(Effect.fail(onError(err)))
52
52
  } else {
53
- resume(Effect.unit())
53
+ resume(Effect.unit)
54
54
  }
55
55
  }
56
56
 
@@ -28,7 +28,12 @@ export const fromReadable = <E, A>(
28
28
  emit.fail(onError(err))
29
29
  })
30
30
 
31
- stream.once("end", () => {
31
+ // The 'close' event is emitted after a process has ended and the stdio
32
+ // streams of a child process have been closed. This is distinct from
33
+ // the 'exit' event, since multiple processes might share the same
34
+ // stdio streams. The 'close' event will always emit after 'exit' was
35
+ // already emitted, or 'error' if the child failed to spawn.
36
+ stream.once("close", () => {
32
37
  emit.end()
33
38
  })
34
39