@effect/platform-node-shared 0.57.0 → 4.0.0-beta.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.
- package/README.md +3 -3
- package/dist/NodeChildProcessSpawner.d.ts +37 -0
- package/dist/NodeChildProcessSpawner.d.ts.map +1 -0
- package/dist/NodeChildProcessSpawner.js +567 -0
- package/dist/NodeChildProcessSpawner.js.map +1 -0
- package/dist/{dts/NodeClusterSocket.d.ts → NodeClusterSocket.d.ts} +4 -7
- package/dist/NodeClusterSocket.d.ts.map +1 -0
- package/dist/{esm/NodeClusterSocket.js → NodeClusterSocket.js} +9 -10
- package/dist/NodeClusterSocket.js.map +1 -0
- package/dist/NodeFileSystem.d.ts +8 -0
- package/dist/NodeFileSystem.d.ts.map +1 -0
- package/dist/{esm/internal/fileSystem.js → NodeFileSystem.js} +125 -96
- package/dist/NodeFileSystem.js.map +1 -0
- package/dist/NodePath.d.ts +18 -0
- package/dist/NodePath.d.ts.map +1 -0
- package/dist/NodePath.js +56 -0
- package/dist/NodePath.js.map +1 -0
- package/dist/NodeRuntime.d.ts +28 -0
- package/dist/NodeRuntime.d.ts.map +1 -0
- package/dist/{esm/internal/runtime.js → NodeRuntime.js} +8 -8
- package/dist/NodeRuntime.js.map +1 -0
- package/dist/NodeSink.d.ts +40 -0
- package/dist/NodeSink.d.ts.map +1 -0
- package/dist/NodeSink.js +50 -0
- package/dist/NodeSink.js.map +1 -0
- package/dist/{dts/NodeSocket.d.ts → NodeSocket.d.ts} +10 -10
- package/dist/NodeSocket.d.ts.map +1 -0
- package/dist/{esm/NodeSocket.js → NodeSocket.js} +51 -39
- package/dist/NodeSocket.js.map +1 -0
- package/dist/{dts/NodeSocketServer.d.ts → NodeSocketServer.d.ts} +8 -10
- package/dist/NodeSocketServer.d.ts.map +1 -0
- package/dist/NodeSocketServer.js +192 -0
- package/dist/NodeSocketServer.js.map +1 -0
- package/dist/NodeStdio.d.ts +11 -0
- package/dist/NodeStdio.d.ts.map +1 -0
- package/dist/NodeStdio.js +43 -0
- package/dist/NodeStdio.js.map +1 -0
- package/dist/NodeStream.d.ts +127 -0
- package/dist/NodeStream.d.ts.map +1 -0
- package/dist/NodeStream.js +249 -0
- package/dist/NodeStream.js.map +1 -0
- package/dist/NodeTerminal.d.ts +15 -0
- package/dist/NodeTerminal.d.ts.map +1 -0
- package/dist/{esm/internal/terminal.js → NodeTerminal.js} +28 -21
- package/dist/NodeTerminal.js.map +1 -0
- package/dist/internal/utils.d.ts +2 -0
- package/dist/internal/utils.d.ts.map +1 -0
- package/dist/{esm/internal/error.js → internal/utils.js} +4 -5
- package/dist/internal/utils.js.map +1 -0
- package/package.json +53 -124
- package/src/NodeChildProcessSpawner.ts +713 -0
- package/src/NodeClusterSocket.ts +12 -13
- package/src/NodeFileSystem.ts +632 -5
- package/src/NodePath.ts +48 -9
- package/src/NodeRuntime.ts +53 -4
- package/src/NodeSink.ts +65 -62
- package/src/NodeSocket.ts +65 -49
- package/src/NodeSocketServer.ts +108 -88
- package/src/NodeStdio.ts +49 -0
- package/src/NodeStream.ts +324 -83
- package/src/NodeTerminal.ts +100 -9
- package/src/internal/{error.ts → utils.ts} +6 -7
- package/NodeClusterSocket/package.json +0 -6
- package/NodeCommandExecutor/package.json +0 -6
- package/NodeFileSystem/ParcelWatcher/package.json +0 -6
- package/NodeFileSystem/package.json +0 -6
- package/NodeKeyValueStore/package.json +0 -6
- package/NodeMultipart/package.json +0 -6
- package/NodePath/package.json +0 -6
- package/NodeRuntime/package.json +0 -6
- package/NodeSink/package.json +0 -6
- package/NodeSocket/package.json +0 -6
- package/NodeSocketServer/package.json +0 -6
- package/NodeStream/package.json +0 -6
- package/NodeTerminal/package.json +0 -6
- package/dist/cjs/NodeClusterSocket.js +0 -50
- package/dist/cjs/NodeClusterSocket.js.map +0 -1
- package/dist/cjs/NodeCommandExecutor.js +0 -14
- package/dist/cjs/NodeCommandExecutor.js.map +0 -1
- package/dist/cjs/NodeFileSystem/ParcelWatcher.js +0 -20
- package/dist/cjs/NodeFileSystem/ParcelWatcher.js.map +0 -1
- package/dist/cjs/NodeFileSystem.js +0 -18
- package/dist/cjs/NodeFileSystem.js.map +0 -1
- package/dist/cjs/NodeKeyValueStore.js +0 -18
- package/dist/cjs/NodeKeyValueStore.js.map +0 -1
- package/dist/cjs/NodeMultipart.js +0 -24
- package/dist/cjs/NodeMultipart.js.map +0 -1
- package/dist/cjs/NodePath.js +0 -28
- package/dist/cjs/NodePath.js.map +0 -1
- package/dist/cjs/NodeRuntime.js +0 -14
- package/dist/cjs/NodeRuntime.js.map +0 -1
- package/dist/cjs/NodeSink.js +0 -50
- package/dist/cjs/NodeSink.js.map +0 -1
- package/dist/cjs/NodeSocket.js +0 -153
- package/dist/cjs/NodeSocket.js.map +0 -1
- package/dist/cjs/NodeSocketServer.js +0 -178
- package/dist/cjs/NodeSocketServer.js.map +0 -1
- package/dist/cjs/NodeStream.js +0 -76
- package/dist/cjs/NodeStream.js.map +0 -1
- package/dist/cjs/NodeTerminal.js +0 -19
- package/dist/cjs/NodeTerminal.js.map +0 -1
- package/dist/cjs/internal/commandExecutor.js +0 -153
- package/dist/cjs/internal/commandExecutor.js.map +0 -1
- package/dist/cjs/internal/error.js +0 -45
- package/dist/cjs/internal/error.js.map +0 -1
- package/dist/cjs/internal/fileSystem/parcelWatcher.js +0 -68
- package/dist/cjs/internal/fileSystem/parcelWatcher.js.map +0 -1
- package/dist/cjs/internal/fileSystem.js +0 -400
- package/dist/cjs/internal/fileSystem.js.map +0 -1
- package/dist/cjs/internal/multipart.js +0 -147
- package/dist/cjs/internal/multipart.js.map +0 -1
- package/dist/cjs/internal/path.js +0 -53
- package/dist/cjs/internal/path.js.map +0 -1
- package/dist/cjs/internal/runtime.js +0 -37
- package/dist/cjs/internal/runtime.js.map +0 -1
- package/dist/cjs/internal/sink.js +0 -28
- package/dist/cjs/internal/sink.js.map +0 -1
- package/dist/cjs/internal/stream.js +0 -233
- package/dist/cjs/internal/stream.js.map +0 -1
- package/dist/cjs/internal/terminal.js +0 -86
- package/dist/cjs/internal/terminal.js.map +0 -1
- package/dist/dts/NodeClusterSocket.d.ts.map +0 -1
- package/dist/dts/NodeCommandExecutor.d.ts +0 -12
- package/dist/dts/NodeCommandExecutor.d.ts.map +0 -1
- package/dist/dts/NodeFileSystem/ParcelWatcher.d.ts +0 -13
- package/dist/dts/NodeFileSystem/ParcelWatcher.d.ts.map +0 -1
- package/dist/dts/NodeFileSystem.d.ts +0 -11
- package/dist/dts/NodeFileSystem.d.ts.map +0 -1
- package/dist/dts/NodeKeyValueStore.d.ts +0 -12
- package/dist/dts/NodeKeyValueStore.d.ts.map +0 -1
- package/dist/dts/NodeMultipart.d.ts +0 -27
- package/dist/dts/NodeMultipart.d.ts.map +0 -1
- package/dist/dts/NodePath.d.ts +0 -21
- package/dist/dts/NodePath.d.ts.map +0 -1
- package/dist/dts/NodeRuntime.d.ts +0 -10
- package/dist/dts/NodeRuntime.d.ts.map +0 -1
- package/dist/dts/NodeSink.d.ts +0 -36
- package/dist/dts/NodeSink.d.ts.map +0 -1
- package/dist/dts/NodeSocket.d.ts.map +0 -1
- package/dist/dts/NodeSocketServer.d.ts.map +0 -1
- package/dist/dts/NodeStream.d.ts +0 -119
- package/dist/dts/NodeStream.d.ts.map +0 -1
- package/dist/dts/NodeTerminal.d.ts +0 -18
- package/dist/dts/NodeTerminal.d.ts.map +0 -1
- package/dist/dts/internal/commandExecutor.d.ts +0 -2
- package/dist/dts/internal/commandExecutor.d.ts.map +0 -1
- package/dist/dts/internal/error.d.ts +0 -2
- package/dist/dts/internal/error.d.ts.map +0 -1
- package/dist/dts/internal/fileSystem/parcelWatcher.d.ts +0 -4
- package/dist/dts/internal/fileSystem/parcelWatcher.d.ts.map +0 -1
- package/dist/dts/internal/fileSystem.d.ts +0 -2
- package/dist/dts/internal/fileSystem.d.ts.map +0 -1
- package/dist/dts/internal/multipart.d.ts +0 -2
- package/dist/dts/internal/multipart.d.ts.map +0 -1
- package/dist/dts/internal/path.d.ts +0 -2
- package/dist/dts/internal/path.d.ts.map +0 -1
- package/dist/dts/internal/runtime.d.ts +0 -2
- package/dist/dts/internal/runtime.d.ts.map +0 -1
- package/dist/dts/internal/sink.d.ts +0 -2
- package/dist/dts/internal/sink.d.ts.map +0 -1
- package/dist/dts/internal/stream.d.ts +0 -2
- package/dist/dts/internal/stream.d.ts.map +0 -1
- package/dist/dts/internal/terminal.d.ts +0 -2
- package/dist/dts/internal/terminal.d.ts.map +0 -1
- package/dist/esm/NodeClusterSocket.js.map +0 -1
- package/dist/esm/NodeCommandExecutor.js +0 -7
- package/dist/esm/NodeCommandExecutor.js.map +0 -1
- package/dist/esm/NodeFileSystem/ParcelWatcher.js +0 -12
- package/dist/esm/NodeFileSystem/ParcelWatcher.js.map +0 -1
- package/dist/esm/NodeFileSystem.js +0 -10
- package/dist/esm/NodeFileSystem.js.map +0 -1
- package/dist/esm/NodeKeyValueStore.js +0 -10
- package/dist/esm/NodeKeyValueStore.js.map +0 -1
- package/dist/esm/NodeMultipart.js +0 -17
- package/dist/esm/NodeMultipart.js.map +0 -1
- package/dist/esm/NodePath.js +0 -20
- package/dist/esm/NodePath.js.map +0 -1
- package/dist/esm/NodeRuntime.js +0 -7
- package/dist/esm/NodeRuntime.js.map +0 -1
- package/dist/esm/NodeSink.js +0 -43
- package/dist/esm/NodeSink.js.map +0 -1
- package/dist/esm/NodeSocket.js.map +0 -1
- package/dist/esm/NodeSocketServer.js +0 -167
- package/dist/esm/NodeSocketServer.js.map +0 -1
- package/dist/esm/NodeStream.js +0 -69
- package/dist/esm/NodeStream.js.map +0 -1
- package/dist/esm/NodeTerminal.js +0 -12
- package/dist/esm/NodeTerminal.js.map +0 -1
- package/dist/esm/internal/commandExecutor.js +0 -146
- package/dist/esm/internal/commandExecutor.js.map +0 -1
- package/dist/esm/internal/error.js.map +0 -1
- package/dist/esm/internal/fileSystem/parcelWatcher.js +0 -61
- package/dist/esm/internal/fileSystem/parcelWatcher.js.map +0 -1
- package/dist/esm/internal/fileSystem.js.map +0 -1
- package/dist/esm/internal/multipart.js +0 -137
- package/dist/esm/internal/multipart.js.map +0 -1
- package/dist/esm/internal/path.js +0 -46
- package/dist/esm/internal/path.js.map +0 -1
- package/dist/esm/internal/runtime.js.map +0 -1
- package/dist/esm/internal/sink.js +0 -19
- package/dist/esm/internal/sink.js.map +0 -1
- package/dist/esm/internal/stream.js +0 -217
- package/dist/esm/internal/stream.js.map +0 -1
- package/dist/esm/internal/terminal.js.map +0 -1
- package/dist/esm/package.json +0 -4
- package/src/NodeCommandExecutor.ts +0 -13
- package/src/NodeFileSystem/ParcelWatcher.ts +0 -15
- package/src/NodeKeyValueStore.ts +0 -20
- package/src/NodeMultipart.ts +0 -40
- package/src/internal/commandExecutor.ts +0 -251
- package/src/internal/fileSystem/parcelWatcher.ts +0 -64
- package/src/internal/fileSystem.ts +0 -648
- package/src/internal/multipart.ts +0 -141
- package/src/internal/path.ts +0 -63
- package/src/internal/runtime.ts +0 -34
- package/src/internal/sink.ts +0 -57
- package/src/internal/stream.ts +0 -375
- package/src/internal/terminal.ts +0 -100
package/src/NodePath.ts
CHANGED
|
@@ -1,25 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
+
import * as Effect from "effect/Effect"
|
|
5
|
+
import * as Layer from "effect/Layer"
|
|
6
|
+
import { Path, TypeId } from "effect/Path"
|
|
7
|
+
import { BadArgument } from "effect/PlatformError"
|
|
8
|
+
import * as NodePath from "node:path"
|
|
9
|
+
import * as NodeUrl from "node:url"
|
|
4
10
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
const fromFileUrl = (url: URL): Effect.Effect<string, BadArgument> =>
|
|
12
|
+
Effect.try({
|
|
13
|
+
try: () => NodeUrl.fileURLToPath(url),
|
|
14
|
+
catch: (cause) =>
|
|
15
|
+
new BadArgument({
|
|
16
|
+
module: "Path",
|
|
17
|
+
method: "fromFileUrl",
|
|
18
|
+
cause
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const toFileUrl = (path: string): Effect.Effect<URL, BadArgument> =>
|
|
23
|
+
Effect.try({
|
|
24
|
+
try: () => NodeUrl.pathToFileURL(path),
|
|
25
|
+
catch: (cause) =>
|
|
26
|
+
new BadArgument({
|
|
27
|
+
module: "Path",
|
|
28
|
+
method: "toFileUrl",
|
|
29
|
+
cause
|
|
30
|
+
})
|
|
31
|
+
})
|
|
8
32
|
|
|
9
33
|
/**
|
|
10
34
|
* @since 1.0.0
|
|
11
|
-
* @category
|
|
35
|
+
* @category Layers
|
|
12
36
|
*/
|
|
13
|
-
export const
|
|
37
|
+
export const layerPosix: Layer.Layer<Path> = Layer.succeed(Path)({
|
|
38
|
+
[TypeId]: TypeId,
|
|
39
|
+
...NodePath.posix,
|
|
40
|
+
fromFileUrl,
|
|
41
|
+
toFileUrl
|
|
42
|
+
})
|
|
14
43
|
|
|
15
44
|
/**
|
|
16
45
|
* @since 1.0.0
|
|
17
|
-
* @category
|
|
46
|
+
* @category Layers
|
|
18
47
|
*/
|
|
19
|
-
export const
|
|
48
|
+
export const layerWin32: Layer.Layer<Path> = Layer.succeed(Path)({
|
|
49
|
+
[TypeId]: TypeId,
|
|
50
|
+
...NodePath.win32,
|
|
51
|
+
fromFileUrl,
|
|
52
|
+
toFileUrl
|
|
53
|
+
})
|
|
20
54
|
|
|
21
55
|
/**
|
|
22
56
|
* @since 1.0.0
|
|
23
|
-
* @category
|
|
57
|
+
* @category Layers
|
|
24
58
|
*/
|
|
25
|
-
export const
|
|
59
|
+
export const layer: Layer.Layer<Path> = Layer.succeed(Path)({
|
|
60
|
+
[TypeId]: TypeId,
|
|
61
|
+
...NodePath,
|
|
62
|
+
fromFileUrl,
|
|
63
|
+
toFileUrl
|
|
64
|
+
})
|
package/src/NodeRuntime.ts
CHANGED
|
@@ -1,11 +1,60 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import type {
|
|
5
|
-
import * as
|
|
4
|
+
import type { Effect } from "effect/Effect"
|
|
5
|
+
import * as Runtime from "effect/Runtime"
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @since 1.0.0
|
|
9
|
-
* @category
|
|
9
|
+
* @category Run main
|
|
10
10
|
*/
|
|
11
|
-
export const runMain:
|
|
11
|
+
export const runMain: {
|
|
12
|
+
/**
|
|
13
|
+
* @since 1.0.0
|
|
14
|
+
* @category Run main
|
|
15
|
+
*/
|
|
16
|
+
(
|
|
17
|
+
options?: {
|
|
18
|
+
readonly disableErrorReporting?: boolean | undefined
|
|
19
|
+
readonly teardown?: Runtime.Teardown | undefined
|
|
20
|
+
}
|
|
21
|
+
): <E, A>(effect: Effect<A, E>) => void
|
|
22
|
+
/**
|
|
23
|
+
* @since 1.0.0
|
|
24
|
+
* @category Run main
|
|
25
|
+
*/
|
|
26
|
+
<E, A>(
|
|
27
|
+
effect: Effect<A, E>,
|
|
28
|
+
options?: {
|
|
29
|
+
readonly disableErrorReporting?: boolean | undefined
|
|
30
|
+
readonly teardown?: Runtime.Teardown | undefined
|
|
31
|
+
}
|
|
32
|
+
): void
|
|
33
|
+
} = Runtime.makeRunMain(({
|
|
34
|
+
fiber,
|
|
35
|
+
teardown
|
|
36
|
+
}) => {
|
|
37
|
+
let receivedSignal = false
|
|
38
|
+
|
|
39
|
+
fiber.addObserver((exit) => {
|
|
40
|
+
if (!receivedSignal) {
|
|
41
|
+
process.removeListener("SIGINT", onSigint)
|
|
42
|
+
process.removeListener("SIGTERM", onSigint)
|
|
43
|
+
}
|
|
44
|
+
teardown(exit, (code) => {
|
|
45
|
+
if (receivedSignal || code !== 0) {
|
|
46
|
+
process.exit(code)
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
function onSigint() {
|
|
52
|
+
receivedSignal = true
|
|
53
|
+
process.removeListener("SIGINT", onSigint)
|
|
54
|
+
process.removeListener("SIGTERM", onSigint)
|
|
55
|
+
fiber.interruptUnsafe(fiber.id)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
process.on("SIGINT", onSigint)
|
|
59
|
+
process.on("SIGTERM", onSigint)
|
|
60
|
+
})
|
package/src/NodeSink.ts
CHANGED
|
@@ -1,77 +1,80 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import type {
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import type
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import type { FromWritableOptions } from "./NodeStream.js"
|
|
4
|
+
import type { NonEmptyReadonlyArray } from "effect/Array"
|
|
5
|
+
import * as Cause from "effect/Cause"
|
|
6
|
+
import * as Channel from "effect/Channel"
|
|
7
|
+
import * as Effect from "effect/Effect"
|
|
8
|
+
import { identity, type LazyArg } from "effect/Function"
|
|
9
|
+
import * as Pull from "effect/Pull"
|
|
10
|
+
import * as Sink from "effect/Sink"
|
|
11
|
+
import type { Writable } from "node:stream"
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
|
-
* @category
|
|
14
|
+
* @category constructors
|
|
16
15
|
* @since 1.0.0
|
|
17
16
|
*/
|
|
18
|
-
export const fromWritable
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
export const fromWritable = <E, A = Uint8Array | string>(
|
|
18
|
+
options: {
|
|
19
|
+
readonly evaluate: LazyArg<Writable | NodeJS.WritableStream>
|
|
20
|
+
readonly onError: (error: unknown) => E
|
|
21
|
+
readonly endOnDone?: boolean | undefined
|
|
22
|
+
readonly encoding?: BufferEncoding | undefined
|
|
23
|
+
}
|
|
24
|
+
): Sink.Sink<void, A, never, E> =>
|
|
25
|
+
Sink.fromChannel(Channel.mapDone(fromWritableChannel<never, E, A>(options), (_) => [_]))
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
|
-
* @category
|
|
28
|
+
* @category constructors
|
|
26
29
|
* @since 1.0.0
|
|
27
30
|
*/
|
|
28
|
-
export const fromWritableChannel
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
export const fromWritableChannel = <IE, E, A = Uint8Array | string>(
|
|
32
|
+
options: {
|
|
33
|
+
readonly evaluate: LazyArg<Writable | NodeJS.WritableStream>
|
|
34
|
+
readonly onError: (error: unknown) => E
|
|
35
|
+
readonly endOnDone?: boolean | undefined
|
|
36
|
+
readonly encoding?: BufferEncoding | undefined
|
|
37
|
+
}
|
|
38
|
+
): Channel.Channel<never, IE | E, void, NonEmptyReadonlyArray<A>, IE> =>
|
|
39
|
+
Channel.fromTransform((pull: Pull.Pull<NonEmptyReadonlyArray<A>, IE, unknown>) => {
|
|
40
|
+
const writable = options.evaluate() as Writable
|
|
41
|
+
return Effect.succeed(pullIntoWritable({ ...options, writable, pull }))
|
|
42
|
+
})
|
|
33
43
|
|
|
34
44
|
/**
|
|
35
|
-
* @category stdio
|
|
36
45
|
* @since 1.0.0
|
|
37
46
|
*/
|
|
38
|
-
export const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
module: "Stream",
|
|
73
|
-
method: "stdin",
|
|
74
|
-
reason: "Unknown",
|
|
75
|
-
cause
|
|
76
|
-
})
|
|
77
|
-
)
|
|
47
|
+
export const pullIntoWritable = <A, IE, E>(options: {
|
|
48
|
+
readonly pull: Pull.Pull<NonEmptyReadonlyArray<A>, IE, unknown>
|
|
49
|
+
readonly writable: Writable
|
|
50
|
+
readonly onError: (error: unknown) => E
|
|
51
|
+
readonly endOnDone?: boolean | undefined
|
|
52
|
+
readonly encoding?: BufferEncoding | undefined
|
|
53
|
+
}): Pull.Pull<never, IE | E, unknown> =>
|
|
54
|
+
options.pull.pipe(
|
|
55
|
+
Effect.flatMap((chunk) => {
|
|
56
|
+
let i = 0
|
|
57
|
+
return Effect.callback<void, E>(function loop(resume) {
|
|
58
|
+
for (; i < chunk.length;) {
|
|
59
|
+
const success = options.writable.write(chunk[i++], options.encoding as any)
|
|
60
|
+
if (!success) {
|
|
61
|
+
options.writable.once("drain", () => (loop as any)(resume))
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
resume(Effect.void)
|
|
66
|
+
})
|
|
67
|
+
}),
|
|
68
|
+
Effect.forever({ disableYield: true }),
|
|
69
|
+
options.endOnDone !== false ?
|
|
70
|
+
Pull.catchDone((_) => {
|
|
71
|
+
if ("closed" in options.writable && options.writable.closed) {
|
|
72
|
+
return Cause.done(_)
|
|
73
|
+
}
|
|
74
|
+
return Effect.callback<never, E | Cause.Done<unknown>>((resume) => {
|
|
75
|
+
options.writable.once("finish", () => resume(Cause.done(_)))
|
|
76
|
+
options.writable.end()
|
|
77
|
+
})
|
|
78
|
+
}) :
|
|
79
|
+
identity
|
|
80
|
+
)
|
package/src/NodeSocket.ts
CHANGED
|
@@ -1,35 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import
|
|
4
|
+
import type { Array } from "effect"
|
|
5
5
|
import * as Channel from "effect/Channel"
|
|
6
|
-
import type * as Chunk from "effect/Chunk"
|
|
7
|
-
import * as Context from "effect/Context"
|
|
8
6
|
import * as Deferred from "effect/Deferred"
|
|
9
7
|
import type * as Duration from "effect/Duration"
|
|
10
8
|
import * as Effect from "effect/Effect"
|
|
11
9
|
import * as FiberSet from "effect/FiberSet"
|
|
10
|
+
import * as Function from "effect/Function"
|
|
12
11
|
import { identity } from "effect/Function"
|
|
13
12
|
import * as Layer from "effect/Layer"
|
|
14
13
|
import * as Scope from "effect/Scope"
|
|
14
|
+
import * as ServiceMap from "effect/ServiceMap"
|
|
15
|
+
import * as Socket from "effect/unstable/socket/Socket"
|
|
15
16
|
import * as Net from "node:net"
|
|
16
17
|
import type { Duplex } from "node:stream"
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* @since 1.0.0
|
|
20
|
-
* @category
|
|
21
|
+
* @category re-exports
|
|
21
22
|
*/
|
|
22
|
-
export
|
|
23
|
-
readonly _: unique symbol
|
|
24
|
-
}
|
|
23
|
+
export * as NodeWS from "ws"
|
|
25
24
|
|
|
26
25
|
/**
|
|
27
26
|
* @since 1.0.0
|
|
28
27
|
* @category tags
|
|
29
28
|
*/
|
|
30
|
-
export
|
|
29
|
+
export class NetSocket extends ServiceMap.Service<NetSocket, Net.Socket>()(
|
|
31
30
|
"@effect/platform-node/NodeSocket/NetSocket"
|
|
32
|
-
)
|
|
31
|
+
) {}
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
34
|
* @since 1.0.0
|
|
@@ -39,13 +38,13 @@ export const makeNet = (
|
|
|
39
38
|
options: Net.NetConnectOpts & {
|
|
40
39
|
readonly openTimeout?: Duration.DurationInput | undefined
|
|
41
40
|
}
|
|
42
|
-
): Effect.Effect<Socket.Socket
|
|
41
|
+
): Effect.Effect<Socket.Socket> =>
|
|
43
42
|
fromDuplex(
|
|
44
|
-
Effect.
|
|
43
|
+
Effect.servicesWith((services: ServiceMap.ServiceMap<Scope.Scope>) => {
|
|
45
44
|
let conn: Net.Socket | undefined
|
|
46
45
|
return Effect.flatMap(
|
|
47
46
|
Scope.addFinalizer(
|
|
48
|
-
|
|
47
|
+
ServiceMap.get(services, Scope.Scope),
|
|
49
48
|
Effect.sync(() => {
|
|
50
49
|
if (!conn) return
|
|
51
50
|
if (conn.closed === false) {
|
|
@@ -58,13 +57,17 @@ export const makeNet = (
|
|
|
58
57
|
})
|
|
59
58
|
),
|
|
60
59
|
() =>
|
|
61
|
-
Effect.
|
|
60
|
+
Effect.callback<Net.Socket, Socket.SocketError, never>((resume) => {
|
|
62
61
|
conn = Net.createConnection(options)
|
|
63
62
|
conn.once("connect", () => {
|
|
64
63
|
resume(Effect.succeed(conn!))
|
|
65
64
|
})
|
|
66
|
-
conn.on("error", (cause) => {
|
|
67
|
-
resume(Effect.fail(
|
|
65
|
+
conn.on("error", (cause: Error) => {
|
|
66
|
+
resume(Effect.fail(
|
|
67
|
+
new Socket.SocketError({
|
|
68
|
+
reason: new Socket.SocketOpenError({ kind: "Unknown", cause })
|
|
69
|
+
})
|
|
70
|
+
))
|
|
68
71
|
})
|
|
69
72
|
})
|
|
70
73
|
)
|
|
@@ -82,18 +85,18 @@ export const fromDuplex = <RO>(
|
|
|
82
85
|
readonly openTimeout?: Duration.DurationInput | undefined
|
|
83
86
|
}
|
|
84
87
|
): Effect.Effect<Socket.Socket, never, Exclude<RO, Scope.Scope>> =>
|
|
85
|
-
Effect.
|
|
88
|
+
Effect.withFiber<Socket.Socket, never, Exclude<RO, Scope.Scope>>((fiber) => {
|
|
86
89
|
let currentSocket: Duplex | undefined
|
|
87
|
-
const latch = Effect.
|
|
88
|
-
const
|
|
90
|
+
const latch = Effect.makeLatchUnsafe(false)
|
|
91
|
+
const openServices = fiber.services as ServiceMap.ServiceMap<RO>
|
|
92
|
+
|
|
89
93
|
const run = <R, E, _>(handler: (_: Uint8Array) => Effect.Effect<_, E, R> | void, opts?: {
|
|
90
94
|
readonly onOpen?: Effect.Effect<void> | undefined
|
|
91
95
|
}) =>
|
|
92
96
|
Effect.scopedWith(Effect.fnUntraced(function*(scope) {
|
|
93
97
|
const fiberSet = yield* FiberSet.make<any, E | Socket.SocketError>().pipe(
|
|
94
|
-
Scope.
|
|
98
|
+
Scope.provide(scope)
|
|
95
99
|
)
|
|
96
|
-
|
|
97
100
|
let conn: Duplex | undefined = undefined
|
|
98
101
|
yield* Scope.addFinalizer(
|
|
99
102
|
scope,
|
|
@@ -105,26 +108,30 @@ export const fromDuplex = <RO>(
|
|
|
105
108
|
conn.off("close", onClose)
|
|
106
109
|
})
|
|
107
110
|
)
|
|
108
|
-
|
|
109
|
-
conn = yield* Scope.extend(open, scope).pipe(
|
|
111
|
+
conn = yield* Scope.provide(open, scope).pipe(
|
|
110
112
|
options?.openTimeout ?
|
|
111
|
-
Effect.
|
|
113
|
+
Effect.timeoutOrElse({
|
|
112
114
|
duration: options.openTimeout,
|
|
113
115
|
onTimeout: () =>
|
|
114
|
-
|
|
116
|
+
Effect.fail(
|
|
117
|
+
new Socket.SocketError({
|
|
118
|
+
reason: new Socket.SocketOpenError({ kind: "Timeout", cause: new Error("Connection timed out") })
|
|
119
|
+
})
|
|
120
|
+
)
|
|
115
121
|
}) :
|
|
116
122
|
identity
|
|
117
123
|
)
|
|
118
124
|
conn.on("end", onEnd)
|
|
119
125
|
conn.on("error", onError)
|
|
120
126
|
conn.on("close", onClose)
|
|
121
|
-
|
|
122
127
|
const run = yield* Effect.provideService(FiberSet.runtime(fiberSet)<R>(), NetSocket, conn as Net.Socket)
|
|
123
128
|
conn.on("data", onData)
|
|
124
129
|
|
|
125
130
|
currentSocket = conn
|
|
126
|
-
|
|
127
|
-
if (opts?.onOpen)
|
|
131
|
+
latch.openUnsafe()
|
|
132
|
+
if (opts?.onOpen) {
|
|
133
|
+
yield* opts.onOpen
|
|
134
|
+
}
|
|
128
135
|
|
|
129
136
|
return yield* FiberSet.join(fiberSet)
|
|
130
137
|
|
|
@@ -135,36 +142,40 @@ export const fromDuplex = <RO>(
|
|
|
135
142
|
}
|
|
136
143
|
}
|
|
137
144
|
function onEnd() {
|
|
138
|
-
Deferred.
|
|
145
|
+
Deferred.doneUnsafe(fiberSet.deferred, Effect.void)
|
|
139
146
|
}
|
|
140
147
|
function onError(cause: Error) {
|
|
141
|
-
Deferred.
|
|
148
|
+
Deferred.doneUnsafe(
|
|
142
149
|
fiberSet.deferred,
|
|
143
|
-
Effect.fail(
|
|
150
|
+
Effect.fail(
|
|
151
|
+
new Socket.SocketError({
|
|
152
|
+
reason: new Socket.SocketReadError({ cause })
|
|
153
|
+
})
|
|
154
|
+
)
|
|
144
155
|
)
|
|
145
156
|
}
|
|
146
157
|
function onClose(hadError: boolean) {
|
|
147
|
-
Deferred.
|
|
158
|
+
Deferred.doneUnsafe(
|
|
148
159
|
fiberSet.deferred,
|
|
149
160
|
Effect.fail(
|
|
150
|
-
new Socket.
|
|
151
|
-
reason:
|
|
152
|
-
code: hadError ? 1006 : 1000
|
|
161
|
+
new Socket.SocketError({
|
|
162
|
+
reason: new Socket.SocketCloseError({ code: hadError ? 1006 : 1000 })
|
|
153
163
|
})
|
|
154
164
|
)
|
|
155
165
|
)
|
|
156
166
|
}
|
|
157
167
|
})).pipe(
|
|
158
|
-
Effect.
|
|
159
|
-
Effect.
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
168
|
+
Effect.updateServices((input: ServiceMap.ServiceMap<R>) => ServiceMap.merge(openServices, input)),
|
|
169
|
+
Effect.onExit(() =>
|
|
170
|
+
Effect.sync(() => {
|
|
171
|
+
latch.closeUnsafe()
|
|
172
|
+
currentSocket = undefined
|
|
173
|
+
})
|
|
174
|
+
)
|
|
164
175
|
)
|
|
165
176
|
|
|
166
177
|
const write = (chunk: Uint8Array | string | Socket.CloseEvent) =>
|
|
167
|
-
latch.whenOpen(Effect.
|
|
178
|
+
latch.whenOpen(Effect.callback<void, Socket.SocketError>((resume) => {
|
|
168
179
|
const conn = currentSocket!
|
|
169
180
|
if (Socket.isCloseEvent(chunk)) {
|
|
170
181
|
conn.destroy(chunk.code > 1000 ? new Error(`closed with code ${chunk.code}`) : undefined)
|
|
@@ -173,7 +184,11 @@ export const fromDuplex = <RO>(
|
|
|
173
184
|
currentSocket!.write(chunk, (cause) => {
|
|
174
185
|
resume(
|
|
175
186
|
cause
|
|
176
|
-
? Effect.fail(
|
|
187
|
+
? Effect.fail(
|
|
188
|
+
new Socket.SocketError({
|
|
189
|
+
reason: new Socket.SocketWriteError({ cause: cause! })
|
|
190
|
+
})
|
|
191
|
+
)
|
|
177
192
|
: Effect.void
|
|
178
193
|
)
|
|
179
194
|
})
|
|
@@ -203,14 +218,13 @@ export const fromDuplex = <RO>(
|
|
|
203
218
|
export const makeNetChannel = <IE = never>(
|
|
204
219
|
options: Net.NetConnectOpts
|
|
205
220
|
): Channel.Channel<
|
|
206
|
-
|
|
207
|
-
Chunk.Chunk<Uint8Array | string | Socket.CloseEvent>,
|
|
221
|
+
Array.NonEmptyReadonlyArray<Uint8Array>,
|
|
208
222
|
Socket.SocketError | IE,
|
|
209
|
-
IE,
|
|
210
223
|
void,
|
|
211
|
-
|
|
224
|
+
Array.NonEmptyReadonlyArray<Uint8Array | string | Socket.CloseEvent>,
|
|
225
|
+
IE
|
|
212
226
|
> =>
|
|
213
|
-
Channel.
|
|
227
|
+
Channel.unwrap(
|
|
214
228
|
Effect.map(makeNet(options), Socket.toChannelWith<IE>())
|
|
215
229
|
)
|
|
216
230
|
|
|
@@ -218,5 +232,7 @@ export const makeNetChannel = <IE = never>(
|
|
|
218
232
|
* @since 1.0.0
|
|
219
233
|
* @category layers
|
|
220
234
|
*/
|
|
221
|
-
export const layerNet
|
|
222
|
-
|
|
235
|
+
export const layerNet: (options: Net.NetConnectOpts) => Layer.Layer<
|
|
236
|
+
Socket.Socket,
|
|
237
|
+
Socket.SocketError
|
|
238
|
+
> = Function.flow(makeNet, Layer.effect(Socket.Socket))
|