@effect/platform-node-shared 0.0.0-snapshot-c0ae728e57df2c572ea803e1bb7121088cd67b49
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.
- package/Http/Etag/package.json +6 -0
- package/Http/Multipart/package.json +6 -0
- package/LICENSE +21 -0
- package/NodeCommandExecutor/package.json +6 -0
- package/NodeFileSystem/package.json +6 -0
- package/NodeKeyValueStore/package.json +6 -0
- package/NodePath/package.json +6 -0
- package/NodeRuntime/package.json +6 -0
- package/NodeSink/package.json +6 -0
- package/NodeStream/package.json +6 -0
- package/NodeTerminal/package.json +6 -0
- package/README.md +64 -0
- package/dist/cjs/Http/Etag.js +43 -0
- package/dist/cjs/Http/Etag.js.map +1 -0
- package/dist/cjs/Http/Multipart.js +48 -0
- package/dist/cjs/Http/Multipart.js.map +1 -0
- package/dist/cjs/NodeCommandExecutor.js +38 -0
- package/dist/cjs/NodeCommandExecutor.js.map +1 -0
- package/dist/cjs/NodeFileSystem.js +42 -0
- package/dist/cjs/NodeFileSystem.js.map +1 -0
- package/dist/cjs/NodeKeyValueStore.js +42 -0
- package/dist/cjs/NodeKeyValueStore.js.map +1 -0
- package/dist/cjs/NodePath.js +52 -0
- package/dist/cjs/NodePath.js.map +1 -0
- package/dist/cjs/NodeRuntime.js +38 -0
- package/dist/cjs/NodeRuntime.js.map +1 -0
- package/dist/cjs/NodeSink.js +43 -0
- package/dist/cjs/NodeSink.js.map +1 -0
- package/dist/cjs/NodeStream.js +68 -0
- package/dist/cjs/NodeStream.js.map +1 -0
- package/dist/cjs/NodeTerminal.js +43 -0
- package/dist/cjs/NodeTerminal.js.map +1 -0
- package/dist/cjs/internal/commandExecutor.js +151 -0
- package/dist/cjs/internal/commandExecutor.js.map +1 -0
- package/dist/cjs/internal/error.js +44 -0
- package/dist/cjs/internal/error.js.map +1 -0
- package/dist/cjs/internal/fileSystem.js +370 -0
- package/dist/cjs/internal/fileSystem.js.map +1 -0
- package/dist/cjs/internal/http/etag.js +74 -0
- package/dist/cjs/internal/http/etag.js.map +1 -0
- package/dist/cjs/internal/http/multipart.js +119 -0
- package/dist/cjs/internal/http/multipart.js.map +1 -0
- package/dist/cjs/internal/path.js +72 -0
- package/dist/cjs/internal/path.js.map +1 -0
- package/dist/cjs/internal/runtime.js +59 -0
- package/dist/cjs/internal/runtime.js.map +1 -0
- package/dist/cjs/internal/sink.js +52 -0
- package/dist/cjs/internal/sink.js.map +1 -0
- package/dist/cjs/internal/stream.js +208 -0
- package/dist/cjs/internal/stream.js.map +1 -0
- package/dist/cjs/internal/terminal.js +121 -0
- package/dist/cjs/internal/terminal.js.map +1 -0
- package/dist/dts/Http/Etag.d.ts +16 -0
- package/dist/dts/Http/Etag.d.ts.map +1 -0
- package/dist/dts/Http/Multipart.d.ts +29 -0
- package/dist/dts/Http/Multipart.d.ts.map +1 -0
- package/dist/dts/NodeCommandExecutor.d.ts +12 -0
- package/dist/dts/NodeCommandExecutor.d.ts.map +1 -0
- package/dist/dts/NodeFileSystem.d.ts +11 -0
- package/dist/dts/NodeFileSystem.d.ts.map +1 -0
- package/dist/dts/NodeKeyValueStore.d.ts +12 -0
- package/dist/dts/NodeKeyValueStore.d.ts.map +1 -0
- package/dist/dts/NodePath.d.ts +21 -0
- package/dist/dts/NodePath.d.ts.map +1 -0
- package/dist/dts/NodeRuntime.d.ts +10 -0
- package/dist/dts/NodeRuntime.d.ts.map +1 -0
- package/dist/dts/NodeSink.d.ts +22 -0
- package/dist/dts/NodeSink.d.ts.map +1 -0
- package/dist/dts/NodeStream.d.ts +79 -0
- package/dist/dts/NodeStream.d.ts.map +1 -0
- package/dist/dts/NodeTerminal.d.ts +18 -0
- package/dist/dts/NodeTerminal.d.ts.map +1 -0
- package/dist/dts/internal/commandExecutor.d.ts +2 -0
- package/dist/dts/internal/commandExecutor.d.ts.map +1 -0
- package/dist/dts/internal/error.d.ts +2 -0
- package/dist/dts/internal/error.d.ts.map +1 -0
- package/dist/dts/internal/fileSystem.d.ts +2 -0
- package/dist/dts/internal/fileSystem.d.ts.map +1 -0
- package/dist/dts/internal/http/etag.d.ts +2 -0
- package/dist/dts/internal/http/etag.d.ts.map +1 -0
- package/dist/dts/internal/http/multipart.d.ts +2 -0
- package/dist/dts/internal/http/multipart.d.ts.map +1 -0
- package/dist/dts/internal/path.d.ts +2 -0
- package/dist/dts/internal/path.d.ts.map +1 -0
- package/dist/dts/internal/runtime.d.ts +2 -0
- package/dist/dts/internal/runtime.d.ts.map +1 -0
- package/dist/dts/internal/sink.d.ts +2 -0
- package/dist/dts/internal/sink.d.ts.map +1 -0
- package/dist/dts/internal/stream.d.ts +2 -0
- package/dist/dts/internal/stream.d.ts.map +1 -0
- package/dist/dts/internal/terminal.d.ts +2 -0
- package/dist/dts/internal/terminal.d.ts.map +1 -0
- package/dist/esm/Http/Etag.js +12 -0
- package/dist/esm/Http/Etag.js.map +1 -0
- package/dist/esm/Http/Multipart.js +17 -0
- package/dist/esm/Http/Multipart.js.map +1 -0
- package/dist/esm/NodeCommandExecutor.js +7 -0
- package/dist/esm/NodeCommandExecutor.js.map +1 -0
- package/dist/esm/NodeFileSystem.js +10 -0
- package/dist/esm/NodeFileSystem.js.map +1 -0
- package/dist/esm/NodeKeyValueStore.js +10 -0
- package/dist/esm/NodeKeyValueStore.js.map +1 -0
- package/dist/esm/NodePath.js +20 -0
- package/dist/esm/NodePath.js.map +1 -0
- package/dist/esm/NodeRuntime.js +7 -0
- package/dist/esm/NodeRuntime.js.map +1 -0
- package/dist/esm/NodeSink.js +12 -0
- package/dist/esm/NodeSink.js.map +1 -0
- package/dist/esm/NodeStream.js +37 -0
- package/dist/esm/NodeStream.js.map +1 -0
- package/dist/esm/NodeTerminal.js +12 -0
- package/dist/esm/NodeTerminal.js.map +1 -0
- package/dist/esm/internal/commandExecutor.js +120 -0
- package/dist/esm/internal/commandExecutor.js.map +1 -0
- package/dist/esm/internal/error.js +37 -0
- package/dist/esm/internal/error.js.map +1 -0
- package/dist/esm/internal/fileSystem.js +339 -0
- package/dist/esm/internal/fileSystem.js.map +1 -0
- package/dist/esm/internal/http/etag.js +43 -0
- package/dist/esm/internal/http/etag.js.map +1 -0
- package/dist/esm/internal/http/multipart.js +85 -0
- package/dist/esm/internal/http/multipart.js.map +1 -0
- package/dist/esm/internal/path.js +41 -0
- package/dist/esm/internal/path.js.map +1 -0
- package/dist/esm/internal/runtime.js +27 -0
- package/dist/esm/internal/runtime.js.map +1 -0
- package/dist/esm/internal/sink.js +19 -0
- package/dist/esm/internal/sink.js.map +1 -0
- package/dist/esm/internal/stream.js +170 -0
- package/dist/esm/internal/stream.js.map +1 -0
- package/dist/esm/internal/terminal.js +89 -0
- package/dist/esm/internal/terminal.js.map +1 -0
- package/dist/esm/package.json +4 -0
- package/package.json +105 -0
- package/src/Http/Etag.ts +18 -0
- package/src/Http/Multipart.ts +40 -0
- package/src/NodeCommandExecutor.ts +13 -0
- package/src/NodeFileSystem.ts +13 -0
- package/src/NodeKeyValueStore.ts +20 -0
- package/src/NodePath.ts +25 -0
- package/src/NodeRuntime.ts +11 -0
- package/src/NodeSink.ts +30 -0
- package/src/NodeStream.ts +109 -0
- package/src/NodeTerminal.ts +20 -0
- package/src/internal/commandExecutor.ts +203 -0
- package/src/internal/error.ts +51 -0
- package/src/internal/fileSystem.ts +575 -0
- package/src/internal/http/etag.ts +44 -0
- package/src/internal/http/multipart.ts +107 -0
- package/src/internal/path.ts +58 -0
- package/src/internal/runtime.ts +36 -0
- package/src/internal/sink.ts +57 -0
- package/src/internal/stream.ts +325 -0
- package/src/internal/terminal.ts +123 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import * as Multipart from "@effect/platform/Http/Multipart"
|
|
2
|
+
import * as Effect from "effect/Effect"
|
|
3
|
+
import { pipe } from "effect/Function"
|
|
4
|
+
import * as Stream from "effect/Stream"
|
|
5
|
+
import type { MultipartError, PartInfo } from "multipasta"
|
|
6
|
+
import { decodeField } from "multipasta"
|
|
7
|
+
import * as MP from "multipasta/node"
|
|
8
|
+
import * as NFS from "node:fs"
|
|
9
|
+
import type { IncomingHttpHeaders } from "node:http"
|
|
10
|
+
import type { Readable } from "node:stream"
|
|
11
|
+
import * as NodeStreamP from "node:stream/promises"
|
|
12
|
+
import * as NodeStream from "../stream.js"
|
|
13
|
+
|
|
14
|
+
/** @internal */
|
|
15
|
+
export const stream = (
|
|
16
|
+
source: Readable,
|
|
17
|
+
headers: IncomingHttpHeaders
|
|
18
|
+
): Stream.Stream<Multipart.Part, Multipart.MultipartError> =>
|
|
19
|
+
pipe(
|
|
20
|
+
Multipart.makeConfig(headers as any),
|
|
21
|
+
Effect.map(
|
|
22
|
+
(config) =>
|
|
23
|
+
NodeStream.fromReadable<Multipart.MultipartError, MP.Part>(() => {
|
|
24
|
+
const parser = MP.make(config)
|
|
25
|
+
source.pipe(parser)
|
|
26
|
+
return parser
|
|
27
|
+
}, (error) => convertError(error as any))
|
|
28
|
+
),
|
|
29
|
+
Stream.unwrap,
|
|
30
|
+
Stream.map(convertPart)
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
/** @internal */
|
|
34
|
+
export const persisted = (
|
|
35
|
+
source: Readable,
|
|
36
|
+
headers: IncomingHttpHeaders
|
|
37
|
+
) =>
|
|
38
|
+
Multipart.toPersisted(stream(source, headers), (path, file) =>
|
|
39
|
+
Effect.tryPromise({
|
|
40
|
+
try: (signal) => NodeStreamP.pipeline((file as FileImpl).file, NFS.createWriteStream(path), { signal }),
|
|
41
|
+
catch: (error) => Multipart.MultipartError("InternalError", error)
|
|
42
|
+
}))
|
|
43
|
+
|
|
44
|
+
const convertPart = (part: MP.Part): Multipart.Part =>
|
|
45
|
+
part._tag === "Field" ? new FieldImpl(part.info, part.value) : new FileImpl(part)
|
|
46
|
+
|
|
47
|
+
class FieldImpl implements Multipart.Field {
|
|
48
|
+
readonly [Multipart.TypeId]: Multipart.TypeId
|
|
49
|
+
readonly _tag = "Field"
|
|
50
|
+
readonly key: string
|
|
51
|
+
readonly contentType: string
|
|
52
|
+
readonly value: string
|
|
53
|
+
|
|
54
|
+
constructor(
|
|
55
|
+
info: PartInfo,
|
|
56
|
+
value: Uint8Array
|
|
57
|
+
) {
|
|
58
|
+
this[Multipart.TypeId] = Multipart.TypeId
|
|
59
|
+
this.key = info.name
|
|
60
|
+
this.contentType = info.contentType
|
|
61
|
+
this.value = decodeField(info, value)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
class FileImpl implements Multipart.File {
|
|
66
|
+
readonly _tag = "File"
|
|
67
|
+
readonly [Multipart.TypeId]: Multipart.TypeId
|
|
68
|
+
readonly key: string
|
|
69
|
+
readonly name: string
|
|
70
|
+
readonly contentType: string
|
|
71
|
+
readonly content: Stream.Stream<Uint8Array, Multipart.MultipartError>
|
|
72
|
+
|
|
73
|
+
constructor(readonly file: MP.FileStream) {
|
|
74
|
+
this[Multipart.TypeId] = Multipart.TypeId
|
|
75
|
+
this.key = file.info.name
|
|
76
|
+
this.name = file.filename ?? file.info.name
|
|
77
|
+
this.contentType = file.info.contentType
|
|
78
|
+
this.content = NodeStream.fromReadable(() => file, (error) => Multipart.MultipartError("InternalError", error))
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @internal */
|
|
83
|
+
export const fileToReadable = (file: Multipart.File): Readable => (file as FileImpl).file
|
|
84
|
+
|
|
85
|
+
function convertError(error: MultipartError): Multipart.MultipartError {
|
|
86
|
+
switch (error._tag) {
|
|
87
|
+
case "ReachedLimit": {
|
|
88
|
+
switch (error.limit) {
|
|
89
|
+
case "MaxParts": {
|
|
90
|
+
return Multipart.MultipartError("TooManyParts", error)
|
|
91
|
+
}
|
|
92
|
+
case "MaxFieldSize": {
|
|
93
|
+
return Multipart.MultipartError("FieldTooLarge", error)
|
|
94
|
+
}
|
|
95
|
+
case "MaxPartSize": {
|
|
96
|
+
return Multipart.MultipartError("FileTooLarge", error)
|
|
97
|
+
}
|
|
98
|
+
case "MaxTotalSize": {
|
|
99
|
+
return Multipart.MultipartError("BodyTooLarge", error)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
default: {
|
|
104
|
+
return Multipart.MultipartError("Parse", error)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { BadArgument } from "@effect/platform/Error"
|
|
2
|
+
import { Path } from "@effect/platform/Path"
|
|
3
|
+
import * as Effect from "effect/Effect"
|
|
4
|
+
import * as Layer from "effect/Layer"
|
|
5
|
+
import * as NodePath from "node:path"
|
|
6
|
+
import * as NodeUrl from "node:url"
|
|
7
|
+
|
|
8
|
+
const fromFileUrl = (url: URL): Effect.Effect<string, BadArgument> =>
|
|
9
|
+
Effect.try({
|
|
10
|
+
try: () => NodeUrl.fileURLToPath(url),
|
|
11
|
+
catch: (error) =>
|
|
12
|
+
BadArgument({
|
|
13
|
+
module: "Path",
|
|
14
|
+
method: "fromFileUrl",
|
|
15
|
+
message: `${error}`
|
|
16
|
+
})
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const toFileUrl = (path: string): Effect.Effect<URL, BadArgument> =>
|
|
20
|
+
Effect.try({
|
|
21
|
+
try: () => NodeUrl.pathToFileURL(path),
|
|
22
|
+
catch: (error) =>
|
|
23
|
+
BadArgument({
|
|
24
|
+
module: "Path",
|
|
25
|
+
method: "toFileUrl",
|
|
26
|
+
message: `${error}`
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
/** @internal */
|
|
31
|
+
export const layerPosix = Layer.succeed(
|
|
32
|
+
Path,
|
|
33
|
+
Path.of({
|
|
34
|
+
...NodePath.posix,
|
|
35
|
+
fromFileUrl,
|
|
36
|
+
toFileUrl
|
|
37
|
+
})
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
/** @internal */
|
|
41
|
+
export const layerWin32 = Layer.succeed(
|
|
42
|
+
Path,
|
|
43
|
+
Path.of({
|
|
44
|
+
...NodePath.win32,
|
|
45
|
+
fromFileUrl,
|
|
46
|
+
toFileUrl
|
|
47
|
+
})
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
/** @internal */
|
|
51
|
+
export const layer = Layer.succeed(
|
|
52
|
+
Path,
|
|
53
|
+
Path.of({
|
|
54
|
+
...NodePath,
|
|
55
|
+
fromFileUrl,
|
|
56
|
+
toFileUrl
|
|
57
|
+
})
|
|
58
|
+
)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { defaultTeardown, type RunMain } from "@effect/platform/Runtime"
|
|
2
|
+
import * as Cause from "effect/Cause"
|
|
3
|
+
import * as Effect from "effect/Effect"
|
|
4
|
+
|
|
5
|
+
/** @internal */
|
|
6
|
+
export const runMain: RunMain = <E, A>(
|
|
7
|
+
effect: Effect.Effect<A, E>,
|
|
8
|
+
teardown = defaultTeardown
|
|
9
|
+
) => {
|
|
10
|
+
const keepAlive = setInterval(() => {}, 2 ** 31 - 1)
|
|
11
|
+
|
|
12
|
+
const fiber = Effect.runFork(
|
|
13
|
+
Effect.tapErrorCause(effect, (cause) => {
|
|
14
|
+
if (Cause.isInterruptedOnly(cause)) {
|
|
15
|
+
return Effect.unit
|
|
16
|
+
}
|
|
17
|
+
return Effect.logError(cause)
|
|
18
|
+
})
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
fiber.addObserver((exit) => {
|
|
22
|
+
clearInterval(keepAlive)
|
|
23
|
+
teardown(exit, (code) => {
|
|
24
|
+
process.exit(code)
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
function onSigint() {
|
|
29
|
+
process.removeListener("SIGINT", onSigint)
|
|
30
|
+
process.removeListener("SIGTERM", onSigint)
|
|
31
|
+
fiber.unsafeInterruptAsFork(fiber.id())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
process.once("SIGINT", onSigint)
|
|
35
|
+
process.once("SIGTERM", onSigint)
|
|
36
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as Channel from "effect/Channel"
|
|
2
|
+
import type * as Chunk from "effect/Chunk"
|
|
3
|
+
import * as Deferred from "effect/Deferred"
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import type { LazyArg } from "effect/Function"
|
|
6
|
+
import * as Sink from "effect/Sink"
|
|
7
|
+
import type { Writable } from "node:stream"
|
|
8
|
+
import type { FromWritableOptions } from "../NodeStream.js"
|
|
9
|
+
import { writeInput } from "./stream.js"
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
export const fromWritable = <E, A = Uint8Array | string>(
|
|
13
|
+
evaluate: LazyArg<Writable | NodeJS.WritableStream>,
|
|
14
|
+
onError: (error: unknown) => E,
|
|
15
|
+
options?: FromWritableOptions
|
|
16
|
+
): Sink.Sink<never, E, A, never, void> => Sink.fromChannel(fromWritableChannel(evaluate, onError, options))
|
|
17
|
+
|
|
18
|
+
/** @internal */
|
|
19
|
+
export const fromWritableChannel = <IE, OE, A>(
|
|
20
|
+
writable: LazyArg<Writable | NodeJS.WritableStream>,
|
|
21
|
+
onError: (error: unknown) => OE,
|
|
22
|
+
options?: FromWritableOptions
|
|
23
|
+
): Channel.Channel<never, IE, Chunk.Chunk<A>, unknown, IE | OE, Chunk.Chunk<never>, void> =>
|
|
24
|
+
Channel.flatMap(
|
|
25
|
+
Effect.zip(
|
|
26
|
+
Effect.sync(() => writable()),
|
|
27
|
+
Deferred.make<IE | OE, void>()
|
|
28
|
+
),
|
|
29
|
+
([writable, deferred]) =>
|
|
30
|
+
Channel.embedInput(
|
|
31
|
+
writableOutput(writable, deferred, onError),
|
|
32
|
+
writeInput<IE, A>(
|
|
33
|
+
writable,
|
|
34
|
+
(cause) => Deferred.failCause(deferred, cause),
|
|
35
|
+
options,
|
|
36
|
+
Deferred.complete(deferred, Effect.unit)
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
const writableOutput = <IE, E>(
|
|
42
|
+
writable: Writable | NodeJS.WritableStream,
|
|
43
|
+
deferred: Deferred.Deferred<IE | E, void>,
|
|
44
|
+
onError: (error: unknown) => E
|
|
45
|
+
) =>
|
|
46
|
+
Effect.suspend(() => {
|
|
47
|
+
function handleError(err: unknown) {
|
|
48
|
+
Deferred.unsafeDone(deferred, Effect.fail(onError(err)))
|
|
49
|
+
}
|
|
50
|
+
writable.on("error", handleError)
|
|
51
|
+
return Effect.ensuring(
|
|
52
|
+
Deferred.await(deferred),
|
|
53
|
+
Effect.sync(() => {
|
|
54
|
+
writable.removeListener("error", handleError)
|
|
55
|
+
})
|
|
56
|
+
)
|
|
57
|
+
})
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { type PlatformError, SystemError } from "@effect/platform/Error"
|
|
2
|
+
import type { SizeInput } from "@effect/platform/FileSystem"
|
|
3
|
+
import type * as Cause from "effect/Cause"
|
|
4
|
+
import * as Channel from "effect/Channel"
|
|
5
|
+
import * as Chunk from "effect/Chunk"
|
|
6
|
+
import * as Effect from "effect/Effect"
|
|
7
|
+
import * as Either from "effect/Either"
|
|
8
|
+
import * as Exit from "effect/Exit"
|
|
9
|
+
import type { LazyArg } from "effect/Function"
|
|
10
|
+
import { dual } from "effect/Function"
|
|
11
|
+
import * as Queue from "effect/Queue"
|
|
12
|
+
import type * as AsyncInput from "effect/SingleProducerAsyncInput"
|
|
13
|
+
import * as Stream from "effect/Stream"
|
|
14
|
+
import type { Duplex, Readable, Writable } from "node:stream"
|
|
15
|
+
import type { FromReadableOptions, FromWritableOptions } from "../NodeStream.js"
|
|
16
|
+
|
|
17
|
+
/** @internal */
|
|
18
|
+
export const fromReadable = <E, A = Uint8Array>(
|
|
19
|
+
evaluate: LazyArg<Readable | NodeJS.ReadableStream>,
|
|
20
|
+
onError: (error: unknown) => E,
|
|
21
|
+
{ chunkSize }: FromReadableOptions = {}
|
|
22
|
+
): Stream.Stream<A, E> =>
|
|
23
|
+
Stream.fromChannel(
|
|
24
|
+
fromReadableChannel<E, A>(evaluate, onError, chunkSize ? Number(chunkSize) : undefined)
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
/** @internal */
|
|
28
|
+
export const toString = <E>(
|
|
29
|
+
readable: LazyArg<Readable | NodeJS.ReadableStream>,
|
|
30
|
+
options: {
|
|
31
|
+
readonly onFailure: (error: unknown) => E
|
|
32
|
+
readonly encoding?: BufferEncoding | undefined
|
|
33
|
+
readonly maxBytes?: SizeInput | undefined
|
|
34
|
+
}
|
|
35
|
+
): Effect.Effect<string, E> => {
|
|
36
|
+
const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined
|
|
37
|
+
return Effect.acquireUseRelease(
|
|
38
|
+
Effect.sync(() => {
|
|
39
|
+
const stream = readable()
|
|
40
|
+
stream.setEncoding(options.encoding ?? "utf8")
|
|
41
|
+
return stream
|
|
42
|
+
}),
|
|
43
|
+
(stream) =>
|
|
44
|
+
Effect.async((resume) => {
|
|
45
|
+
let string = ""
|
|
46
|
+
let bytes = 0
|
|
47
|
+
stream.once("error", (err) => {
|
|
48
|
+
resume(Effect.fail(options.onFailure(err)))
|
|
49
|
+
})
|
|
50
|
+
stream.once("end", () => {
|
|
51
|
+
resume(Effect.succeed(string))
|
|
52
|
+
})
|
|
53
|
+
stream.on("data", (chunk) => {
|
|
54
|
+
string += chunk
|
|
55
|
+
bytes += Buffer.byteLength(chunk)
|
|
56
|
+
if (maxBytesNumber && bytes > maxBytesNumber) {
|
|
57
|
+
resume(Effect.fail(options.onFailure(new Error("maxBytes exceeded"))))
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
}),
|
|
61
|
+
(stream) =>
|
|
62
|
+
Effect.sync(() => {
|
|
63
|
+
stream.removeAllListeners()
|
|
64
|
+
if ("closed" in stream && !stream.closed) {
|
|
65
|
+
stream.destroy()
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @internal */
|
|
72
|
+
export const toUint8Array = <E>(
|
|
73
|
+
readable: LazyArg<Readable | NodeJS.ReadableStream>,
|
|
74
|
+
options: {
|
|
75
|
+
readonly onFailure: (error: unknown) => E
|
|
76
|
+
readonly maxBytes?: SizeInput | undefined
|
|
77
|
+
}
|
|
78
|
+
): Effect.Effect<Uint8Array, E> => {
|
|
79
|
+
const maxBytesNumber = options.maxBytes ? Number(options.maxBytes) : undefined
|
|
80
|
+
return Effect.acquireUseRelease(
|
|
81
|
+
Effect.sync(readable),
|
|
82
|
+
(stream) =>
|
|
83
|
+
Effect.async((resume) => {
|
|
84
|
+
let buffer = Buffer.alloc(0)
|
|
85
|
+
let bytes = 0
|
|
86
|
+
stream.once("error", (err) => {
|
|
87
|
+
resume(Effect.fail(options.onFailure(err)))
|
|
88
|
+
})
|
|
89
|
+
stream.once("end", () => {
|
|
90
|
+
resume(Effect.succeed(buffer))
|
|
91
|
+
})
|
|
92
|
+
stream.on("data", (chunk) => {
|
|
93
|
+
buffer = Buffer.concat([buffer, chunk])
|
|
94
|
+
bytes += chunk.length
|
|
95
|
+
if (maxBytesNumber && bytes > maxBytesNumber) {
|
|
96
|
+
resume(Effect.fail(options.onFailure(new Error("maxBytes exceeded"))))
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
}),
|
|
100
|
+
(stream) =>
|
|
101
|
+
Effect.sync(() => {
|
|
102
|
+
stream.removeAllListeners()
|
|
103
|
+
if ("closed" in stream && !stream.closed) {
|
|
104
|
+
stream.destroy()
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** @internal */
|
|
111
|
+
export const fromDuplex = <IE, E, I = Uint8Array | string, O = Uint8Array>(
|
|
112
|
+
evaluate: LazyArg<Duplex>,
|
|
113
|
+
onError: (error: unknown) => E,
|
|
114
|
+
options: FromReadableOptions & FromWritableOptions = {}
|
|
115
|
+
): Channel.Channel<never, IE, Chunk.Chunk<I>, unknown, IE | E, Chunk.Chunk<O>, void> =>
|
|
116
|
+
Channel.acquireUseRelease(
|
|
117
|
+
Effect.tap(
|
|
118
|
+
Effect.zip(
|
|
119
|
+
Effect.sync(evaluate),
|
|
120
|
+
Queue.unbounded<Either.Either<Exit.Exit<void, IE | E>, void>>()
|
|
121
|
+
),
|
|
122
|
+
([duplex, queue]) => readableOffer(duplex, queue, onError)
|
|
123
|
+
),
|
|
124
|
+
([duplex, queue]) =>
|
|
125
|
+
Channel.embedInput(
|
|
126
|
+
readableTake(duplex, queue, options.chunkSize ? Number(options.chunkSize) : undefined),
|
|
127
|
+
writeInput(
|
|
128
|
+
duplex,
|
|
129
|
+
(cause) => Queue.offer(queue, Either.left(Exit.failCause(cause))),
|
|
130
|
+
options
|
|
131
|
+
)
|
|
132
|
+
),
|
|
133
|
+
([duplex, queue]) =>
|
|
134
|
+
Effect.zipRight(
|
|
135
|
+
Effect.sync(() => {
|
|
136
|
+
duplex.removeAllListeners()
|
|
137
|
+
if (!duplex.closed) {
|
|
138
|
+
duplex.destroy()
|
|
139
|
+
}
|
|
140
|
+
}),
|
|
141
|
+
Queue.shutdown(queue)
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
/** @internal */
|
|
146
|
+
export const pipeThroughDuplex = dual<
|
|
147
|
+
<E2, B = Uint8Array>(
|
|
148
|
+
duplex: LazyArg<Duplex>,
|
|
149
|
+
onError: (error: unknown) => E2,
|
|
150
|
+
options?: FromReadableOptions & FromWritableOptions
|
|
151
|
+
) => <R, E, A>(self: Stream.Stream<A, E, R>) => Stream.Stream<B, E | E2, R>,
|
|
152
|
+
<R, E, A, E2, B = Uint8Array>(
|
|
153
|
+
self: Stream.Stream<A, E, R>,
|
|
154
|
+
duplex: LazyArg<Duplex>,
|
|
155
|
+
onError: (error: unknown) => E2,
|
|
156
|
+
options?: FromReadableOptions & FromWritableOptions
|
|
157
|
+
) => Stream.Stream<B, E | E2, R>
|
|
158
|
+
>(
|
|
159
|
+
(args) => Stream.StreamTypeId in args[0],
|
|
160
|
+
(self, duplex, onError, options) =>
|
|
161
|
+
Stream.pipeThroughChannelOrFail(
|
|
162
|
+
self,
|
|
163
|
+
fromDuplex(duplex, onError, options)
|
|
164
|
+
)
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
/** @internal */
|
|
168
|
+
export const pipeThroughSimple = dual<
|
|
169
|
+
(
|
|
170
|
+
duplex: LazyArg<Duplex>
|
|
171
|
+
) => <R, E>(self: Stream.Stream<string | Uint8Array, E, R>) => Stream.Stream<Uint8Array, E | PlatformError, R>,
|
|
172
|
+
<R, E>(
|
|
173
|
+
self: Stream.Stream<string | Uint8Array, E, R>,
|
|
174
|
+
duplex: LazyArg<Duplex>
|
|
175
|
+
) => Stream.Stream<Uint8Array, E | PlatformError, R>
|
|
176
|
+
>(
|
|
177
|
+
2,
|
|
178
|
+
(self, duplex) =>
|
|
179
|
+
Stream.pipeThroughChannelOrFail(
|
|
180
|
+
self,
|
|
181
|
+
fromDuplex(duplex, (error) =>
|
|
182
|
+
SystemError({
|
|
183
|
+
module: "Stream",
|
|
184
|
+
method: "pipeThroughSimple",
|
|
185
|
+
pathOrDescriptor: "",
|
|
186
|
+
reason: "Unknown",
|
|
187
|
+
message: String(error)
|
|
188
|
+
}))
|
|
189
|
+
)
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
/** @internal */
|
|
193
|
+
export const fromReadableChannel = <E, A = Uint8Array>(
|
|
194
|
+
evaluate: LazyArg<Readable | NodeJS.ReadableStream>,
|
|
195
|
+
onError: (error: unknown) => E,
|
|
196
|
+
chunkSize: number | undefined
|
|
197
|
+
): Channel.Channel<never, unknown, unknown, unknown, E, Chunk.Chunk<A>, void> =>
|
|
198
|
+
Channel.acquireUseRelease(
|
|
199
|
+
Effect.tap(
|
|
200
|
+
Effect.zip(
|
|
201
|
+
Effect.sync(evaluate),
|
|
202
|
+
Queue.unbounded<Either.Either<Exit.Exit<void, E>, void>>()
|
|
203
|
+
),
|
|
204
|
+
([readable, queue]) => readableOffer(readable, queue, onError)
|
|
205
|
+
),
|
|
206
|
+
([readable, queue]) => readableTake(readable, queue, chunkSize),
|
|
207
|
+
([readable, queue]) =>
|
|
208
|
+
Effect.zipRight(
|
|
209
|
+
Effect.sync(() => {
|
|
210
|
+
readable.removeAllListeners()
|
|
211
|
+
if ("closed" in readable && !readable.closed) {
|
|
212
|
+
readable.destroy()
|
|
213
|
+
}
|
|
214
|
+
}),
|
|
215
|
+
Queue.shutdown(queue)
|
|
216
|
+
)
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
/** @internal */
|
|
220
|
+
export const writeInput = <IE, A>(
|
|
221
|
+
writable: Writable | NodeJS.WritableStream,
|
|
222
|
+
onFailure: (cause: Cause.Cause<IE>) => Effect.Effect<void>,
|
|
223
|
+
{ encoding, endOnDone = true }: FromWritableOptions = {},
|
|
224
|
+
onDone = Effect.unit
|
|
225
|
+
): AsyncInput.AsyncInputProducer<IE, Chunk.Chunk<A>, unknown> => {
|
|
226
|
+
const write = writeEffect(writable, encoding)
|
|
227
|
+
const close = endOnDone
|
|
228
|
+
? Effect.async<void, never, never>((resume) => {
|
|
229
|
+
if ("closed" in writable && writable.closed) {
|
|
230
|
+
resume(Effect.unit)
|
|
231
|
+
} else {
|
|
232
|
+
writable.once("finish", () => resume(Effect.unit))
|
|
233
|
+
writable.end()
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
: Effect.unit
|
|
237
|
+
return {
|
|
238
|
+
awaitRead: () => Effect.unit,
|
|
239
|
+
emit: write,
|
|
240
|
+
error: (cause) => Effect.zipRight(close, onFailure(cause)),
|
|
241
|
+
done: (_) => Effect.zipRight(close, onDone)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** @internal */
|
|
246
|
+
export const writeEffect = <A>(
|
|
247
|
+
writable: Writable | NodeJS.WritableStream,
|
|
248
|
+
encoding?: BufferEncoding
|
|
249
|
+
) =>
|
|
250
|
+
(chunk: Chunk.Chunk<A>) =>
|
|
251
|
+
chunk.length === 0 ?
|
|
252
|
+
Effect.unit :
|
|
253
|
+
Effect.async<void, never, never>((resume) => {
|
|
254
|
+
const iterator = chunk[Symbol.iterator]()
|
|
255
|
+
let next = iterator.next()
|
|
256
|
+
function loop() {
|
|
257
|
+
const item = next
|
|
258
|
+
next = iterator.next()
|
|
259
|
+
const success = writable.write(item.value, encoding as any)
|
|
260
|
+
if (next.done) {
|
|
261
|
+
resume(Effect.unit)
|
|
262
|
+
} else if (success) {
|
|
263
|
+
loop()
|
|
264
|
+
} else {
|
|
265
|
+
writable.once("drain", loop)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
loop()
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
const readableOffer = <E>(
|
|
272
|
+
readable: Readable | NodeJS.ReadableStream,
|
|
273
|
+
queue: Queue.Queue<Either.Either<Exit.Exit<void, E>, void>>,
|
|
274
|
+
onError: (error: unknown) => E
|
|
275
|
+
) =>
|
|
276
|
+
Effect.sync(() => {
|
|
277
|
+
readable.on("readable", () => {
|
|
278
|
+
const size = queue.unsafeSize()
|
|
279
|
+
if (size._tag === "Some" && size.value <= 0) {
|
|
280
|
+
queue.unsafeOffer(Either.right(void 0))
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
readable.on("error", (err) => {
|
|
284
|
+
queue.unsafeOffer(Either.left(Exit.fail(onError(err))))
|
|
285
|
+
})
|
|
286
|
+
readable.on("end", () => {
|
|
287
|
+
queue.unsafeOffer(Either.left(Exit.unit))
|
|
288
|
+
})
|
|
289
|
+
if (readable.readable) {
|
|
290
|
+
queue.unsafeOffer(Either.right(void 0))
|
|
291
|
+
}
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
const readableTake = <E, A>(
|
|
295
|
+
readable: Readable | NodeJS.ReadableStream,
|
|
296
|
+
queue: Queue.Queue<Either.Either<Exit.Exit<void, E>, void>>,
|
|
297
|
+
chunkSize: number | undefined
|
|
298
|
+
) => {
|
|
299
|
+
const read = readChunkChannel<A>(readable, chunkSize)
|
|
300
|
+
const loop: Channel.Channel<never, unknown, unknown, unknown, E, Chunk.Chunk<A>, void> = Channel.flatMap(
|
|
301
|
+
Queue.take(queue),
|
|
302
|
+
Either.match({
|
|
303
|
+
onLeft: Exit.match({
|
|
304
|
+
onFailure: Channel.failCause,
|
|
305
|
+
onSuccess: (_) => Channel.unit
|
|
306
|
+
}),
|
|
307
|
+
onRight: (_) => Channel.flatMap(read, () => loop)
|
|
308
|
+
})
|
|
309
|
+
)
|
|
310
|
+
return loop
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const readChunkChannel = <A>(
|
|
314
|
+
readable: Readable | NodeJS.ReadableStream,
|
|
315
|
+
chunkSize: number | undefined
|
|
316
|
+
) =>
|
|
317
|
+
Channel.suspend(() => {
|
|
318
|
+
const arr: Array<A> = []
|
|
319
|
+
let chunk = readable.read(chunkSize)
|
|
320
|
+
while (chunk !== null) {
|
|
321
|
+
arr.push(chunk)
|
|
322
|
+
chunk = readable.read(chunkSize)
|
|
323
|
+
}
|
|
324
|
+
return Channel.write(Chunk.unsafeFromArray(arr))
|
|
325
|
+
})
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import * as Error from "@effect/platform/Error"
|
|
2
|
+
import * as Terminal from "@effect/platform/Terminal"
|
|
3
|
+
import * as Effect from "effect/Effect"
|
|
4
|
+
import * as Layer from "effect/Layer"
|
|
5
|
+
import * as Option from "effect/Option"
|
|
6
|
+
import * as readline from "node:readline"
|
|
7
|
+
|
|
8
|
+
const defaultShouldQuit = (input: Terminal.UserInput): boolean =>
|
|
9
|
+
input.key.ctrl && (input.key.name === "c" || input.key.name === "d")
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
export const make = (
|
|
13
|
+
shouldQuit: (input: Terminal.UserInput) => boolean = defaultShouldQuit
|
|
14
|
+
) =>
|
|
15
|
+
Effect.gen(function*(_) {
|
|
16
|
+
const input = yield* _(Effect.sync(() => globalThis.process.stdin))
|
|
17
|
+
const output = yield* _(Effect.sync(() => globalThis.process.stdout))
|
|
18
|
+
|
|
19
|
+
// Acquire a readline interface
|
|
20
|
+
const acquireReadlineInterface = Effect.sync(() =>
|
|
21
|
+
readline.createInterface({
|
|
22
|
+
input,
|
|
23
|
+
escapeCodeTimeout: 50
|
|
24
|
+
})
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
// Uses the readline interface to force `stdin` to emit keypress events
|
|
28
|
+
const emitKeypressEvents = (rl: readline.Interface): readline.Interface => {
|
|
29
|
+
readline.emitKeypressEvents(input, rl)
|
|
30
|
+
if (input.isTTY) {
|
|
31
|
+
input.setRawMode(true)
|
|
32
|
+
}
|
|
33
|
+
return rl
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Close the `readline` interface
|
|
37
|
+
const releaseReadlineInterface = (rl: readline.Interface) =>
|
|
38
|
+
Effect.sync(() => {
|
|
39
|
+
if (input.isTTY) {
|
|
40
|
+
input.setRawMode(false)
|
|
41
|
+
}
|
|
42
|
+
rl.close()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
// Handle the `"keypress"` event emitted by `stdin` (forced by readline)
|
|
46
|
+
const handleKeypressEvent = (input: typeof globalThis.process.stdin) =>
|
|
47
|
+
Effect.async<Terminal.UserInput, Terminal.QuitException, never>((resume) => {
|
|
48
|
+
const handleKeypress = (input: string | undefined, key: readline.Key) => {
|
|
49
|
+
const userInput: Terminal.UserInput = {
|
|
50
|
+
input: Option.fromNullable(input),
|
|
51
|
+
key: {
|
|
52
|
+
name: key.name || "",
|
|
53
|
+
ctrl: key.ctrl || false,
|
|
54
|
+
meta: key.meta || false,
|
|
55
|
+
shift: key.shift || false
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (shouldQuit(userInput)) {
|
|
59
|
+
resume(Effect.fail(new Terminal.QuitException()))
|
|
60
|
+
} else {
|
|
61
|
+
resume(Effect.succeed(userInput))
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
input.once("keypress", handleKeypress)
|
|
65
|
+
return Effect.sync(() => {
|
|
66
|
+
input.removeListener("keypress", handleKeypress)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
// Handle the `"line"` event emitted by the readline interface
|
|
71
|
+
const handleLineEvent = (rl: readline.Interface) =>
|
|
72
|
+
Effect.async<string, Terminal.QuitException, never>((resume) => {
|
|
73
|
+
const handleLine = (line: string) => {
|
|
74
|
+
resume(Effect.succeed(line))
|
|
75
|
+
}
|
|
76
|
+
rl.on("line", handleLine)
|
|
77
|
+
return Effect.sync(() => {
|
|
78
|
+
rl.removeListener("line", handleLine)
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const readInput = Effect.acquireUseRelease(
|
|
83
|
+
acquireReadlineInterface.pipe(Effect.map(emitKeypressEvents)),
|
|
84
|
+
() => handleKeypressEvent(input),
|
|
85
|
+
releaseReadlineInterface
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
const readLine = Effect.acquireUseRelease(
|
|
89
|
+
acquireReadlineInterface,
|
|
90
|
+
(rl) => handleLineEvent(rl),
|
|
91
|
+
releaseReadlineInterface
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
const display = (prompt: string): Effect.Effect<void, Error.PlatformError> =>
|
|
95
|
+
Effect.uninterruptible(
|
|
96
|
+
Effect.async((resume) => {
|
|
97
|
+
output.write(prompt, (err) => {
|
|
98
|
+
if (err) {
|
|
99
|
+
resume(Effect.fail(Error.BadArgument({
|
|
100
|
+
module: "Terminal",
|
|
101
|
+
method: "display",
|
|
102
|
+
message: (err as Error).message ?? String(err)
|
|
103
|
+
})))
|
|
104
|
+
}
|
|
105
|
+
resume(Effect.unit)
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
return Terminal.Terminal.of({
|
|
111
|
+
// The columns property can be undefined if stdout was redirected
|
|
112
|
+
columns: Effect.sync(() => output.columns || 0),
|
|
113
|
+
readInput,
|
|
114
|
+
readLine,
|
|
115
|
+
display
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
/** @internal */
|
|
120
|
+
export const layer: Layer.Layer<Terminal.Terminal> = Layer.scoped(
|
|
121
|
+
Terminal.Terminal,
|
|
122
|
+
make(defaultShouldQuit)
|
|
123
|
+
)
|