@graftland/plugin-remote 0.0.0-stage → 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.
- package/LICENSE +202 -0
- package/README.md +76 -2
- package/README.zh-CN.md +71 -0
- package/README.zh-TW.md +71 -0
- package/dist/Auth-BElTC9_X.js +6 -0
- package/dist/Auth.d.ts +49 -0
- package/dist/Auth.d.ts.map +1 -0
- package/dist/Auth.js +8 -0
- package/dist/Auth.js.map +1 -0
- package/dist/Bootstrap-MCwivGN1.js +9 -0
- package/dist/ConfigDirectory-BCC6q2R4.js +5 -0
- package/dist/Contract-CcpSIJm4.js +121 -0
- package/dist/Contract.d.ts +71 -0
- package/dist/Contract.d.ts.map +1 -0
- package/dist/Contract.js +1 -0
- package/dist/Contract.js.map +1 -0
- package/dist/Errors-oPsTLY7l.js +45 -0
- package/dist/Errors.d.ts +67 -0
- package/dist/Errors.d.ts.map +1 -0
- package/dist/Errors.js +47 -0
- package/dist/Errors.js.map +1 -0
- package/dist/FailureGroup-BcH0XOBi.js +53 -0
- package/dist/Framing-zaYdOJnT.js +61 -0
- package/dist/Graft-BsYq3F-J.js +985 -0
- package/dist/Instance.d.ts +7 -0
- package/dist/Instance.d.ts.map +1 -0
- package/dist/Instance.js +14 -0
- package/dist/Instance.js.map +1 -0
- package/dist/Packages-B1TPk8pa.js +111 -0
- package/dist/Peer-IJgFK6mD.js +342 -0
- package/dist/ProcessObservation-DEGrEMfP.js +38 -0
- package/dist/ServeLifecycle-CJM0gG5B.js +24 -0
- package/dist/Testing.d.ts +26 -0
- package/dist/Testing.d.ts.map +1 -0
- package/dist/Testing.js +69 -0
- package/dist/Testing.js.map +1 -0
- package/dist/Transport-DULzpsgR.js +6 -0
- package/dist/Transport.d.ts +39 -0
- package/dist/Transport.d.ts.map +1 -0
- package/dist/Transport.js +8 -0
- package/dist/Transport.js.map +1 -0
- package/dist/WatchdogProtocol-BZmVxrBP.js +17 -0
- package/dist/_virtual/_rolldown/runtime.js +12 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +28275 -0
- package/dist/bin.js.map +1 -0
- package/dist/childRuntime-DSqtFN9o.js +188 -0
- package/dist/cleanup-BPWr-5U9.js +13 -0
- package/dist/command-9zrHdbwT.js +7 -0
- package/dist/connectionEnded-DUgcobEd.js +9 -0
- package/dist/directTransport-DrexuPSm.js +5 -0
- package/dist/entry-SYsx9VNb.js +708 -0
- package/dist/host/auth/bearer.d.ts +5 -0
- package/dist/host/auth/bearer.d.ts.map +1 -0
- package/dist/host/auth/bearer.js +4 -0
- package/dist/host/auth/bearer.js.map +1 -0
- package/dist/host/auth/jwt.d.ts +5 -0
- package/dist/host/auth/jwt.d.ts.map +1 -0
- package/dist/host/auth/jwt.js +4 -0
- package/dist/host/auth/jwt.js.map +1 -0
- package/dist/host/auth/mtls.d.ts +8 -0
- package/dist/host/auth/mtls.d.ts.map +1 -0
- package/dist/host/auth/mtls.js +34 -0
- package/dist/host/auth/mtls.js.map +1 -0
- package/dist/host/auth/none.d.ts +3 -0
- package/dist/host/auth/none.d.ts.map +1 -0
- package/dist/host/auth/none.js +13 -0
- package/dist/host/auth/none.js.map +1 -0
- package/dist/host/command.d.ts +22 -0
- package/dist/host/command.d.ts.map +1 -0
- package/dist/host/command.js +61 -0
- package/dist/host/command.js.map +1 -0
- package/dist/host/local.d.ts +4 -0
- package/dist/host/local.d.ts.map +1 -0
- package/dist/host/local.js +52 -0
- package/dist/host/local.js.map +1 -0
- package/dist/host/unix.d.ts +11 -0
- package/dist/host/unix.d.ts.map +1 -0
- package/dist/host/unix.js +39 -0
- package/dist/host/unix.js.map +1 -0
- package/dist/host/websocket.d.ts +13 -0
- package/dist/host/websocket.d.ts.map +1 -0
- package/dist/host/websocket.js +36 -0
- package/dist/host/websocket.js.map +1 -0
- package/dist/host/worker.d.ts +4 -0
- package/dist/host/worker.d.ts.map +1 -0
- package/dist/host/worker.js +52 -0
- package/dist/host/worker.js.map +1 -0
- package/dist/importer-D7zSgqxq.js +347 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +86 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/Certificates.js +182 -0
- package/dist/internal/Certificates.js.map +1 -0
- package/dist/internal/FailureGroup.js +55 -0
- package/dist/internal/FailureGroup.js.map +1 -0
- package/dist/internal/Limits.d.ts +1 -0
- package/dist/internal/Limits.js +17 -0
- package/dist/internal/Limits.js.map +1 -0
- package/dist/internal/Peer.js +6 -0
- package/dist/internal/Peer.js.map +1 -0
- package/dist/internal/ServeLifecycle.js +26 -0
- package/dist/internal/ServeLifecycle.js.map +1 -0
- package/dist/internal/Supervisor.js +7 -0
- package/dist/internal/Supervisor.js.map +1 -0
- package/dist/internal/assembly.d.ts +5 -0
- package/dist/internal/assembly.js +138 -0
- package/dist/internal/assembly.js.map +1 -0
- package/dist/internal/auth/mtls.js +23 -0
- package/dist/internal/auth/mtls.js.map +1 -0
- package/dist/internal/auth/token.js +19 -0
- package/dist/internal/auth/token.js.map +1 -0
- package/dist/internal/auth/tokenCredentials.d.ts +8 -0
- package/dist/internal/auth/tokenCredentials.d.ts.map +1 -0
- package/dist/internal/auth/tokenCredentials.js +33 -0
- package/dist/internal/auth/tokenCredentials.js.map +1 -0
- package/dist/internal/child.js +125 -0
- package/dist/internal/child.js.map +1 -0
- package/dist/internal/childErrors.d.ts +33 -0
- package/dist/internal/childErrors.d.ts.map +1 -0
- package/dist/internal/childErrors.js +29 -0
- package/dist/internal/childErrors.js.map +1 -0
- package/dist/internal/childRuntime.js +59 -0
- package/dist/internal/childRuntime.js.map +1 -0
- package/dist/internal/cleanup.js +15 -0
- package/dist/internal/cleanup.js.map +1 -0
- package/dist/internal/config.js +6 -0
- package/dist/internal/config.js.map +1 -0
- package/dist/internal/control.js +42 -0
- package/dist/internal/control.js.map +1 -0
- package/dist/internal/credentials.d.ts +44 -0
- package/dist/internal/credentials.d.ts.map +1 -0
- package/dist/internal/credentials.js +54 -0
- package/dist/internal/credentials.js.map +1 -0
- package/dist/internal/directTransport.d.ts +3 -0
- package/dist/internal/directTransport.js +7 -0
- package/dist/internal/directTransport.js.map +1 -0
- package/dist/internal/entry.d.ts +9 -0
- package/dist/internal/entry.d.ts.map +1 -0
- package/dist/internal/entry.js +144 -0
- package/dist/internal/entry.js.map +1 -0
- package/dist/internal/entryConfig.d.ts +202 -0
- package/dist/internal/entryConfig.d.ts.map +1 -0
- package/dist/internal/entryConfig.js +176 -0
- package/dist/internal/entryConfig.js.map +1 -0
- package/dist/internal/execution.d.ts +5 -0
- package/dist/internal/execution.js +43 -0
- package/dist/internal/execution.js.map +1 -0
- package/dist/internal/follow.js +11 -0
- package/dist/internal/follow.js.map +1 -0
- package/dist/internal/forkTree.js +7 -0
- package/dist/internal/forkTree.js.map +1 -0
- package/dist/internal/host/local.js +28 -0
- package/dist/internal/host/local.js.map +1 -0
- package/dist/internal/host/runner.js +100 -0
- package/dist/internal/host/runner.js.map +1 -0
- package/dist/internal/host.js +71 -0
- package/dist/internal/host.js.map +1 -0
- package/dist/internal/importer.js +18 -0
- package/dist/internal/importer.js.map +1 -0
- package/dist/internal/listenerStartup.js +17 -0
- package/dist/internal/listenerStartup.js.map +1 -0
- package/dist/internal/loadModule.js +20 -0
- package/dist/internal/loadModule.js.map +1 -0
- package/dist/internal/platform.js +9 -0
- package/dist/internal/platform.js.map +1 -0
- package/dist/internal/port.d.ts +13 -0
- package/dist/internal/port.d.ts.map +1 -0
- package/dist/internal/port.js +275 -0
- package/dist/internal/port.js.map +1 -0
- package/dist/internal/release.js +25 -0
- package/dist/internal/release.js.map +1 -0
- package/dist/internal/runner/Bootstrap.js +11 -0
- package/dist/internal/runner/Bootstrap.js.map +1 -0
- package/dist/internal/runner/Connection.d.ts +11 -0
- package/dist/internal/runner/Connection.js +56 -0
- package/dist/internal/runner/Connection.js.map +1 -0
- package/dist/internal/runner/Execution.js +115 -0
- package/dist/internal/runner/Execution.js.map +1 -0
- package/dist/internal/runner/Framing.d.ts +11 -0
- package/dist/internal/runner/Framing.d.ts.map +1 -0
- package/dist/internal/runner/Framing.js +63 -0
- package/dist/internal/runner/Framing.js.map +1 -0
- package/dist/internal/runner/Local.js +46 -0
- package/dist/internal/runner/Local.js.map +1 -0
- package/dist/internal/runner/Lock.js +25 -0
- package/dist/internal/runner/Lock.js.map +1 -0
- package/dist/internal/runner/Multiplex.d.ts +5 -0
- package/dist/internal/runner/Multiplex.js +49 -0
- package/dist/internal/runner/Multiplex.js.map +1 -0
- package/dist/internal/runner/Packages.d.ts +3 -0
- package/dist/internal/runner/Packages.js +108 -0
- package/dist/internal/runner/Packages.js.map +1 -0
- package/dist/internal/runner/ProcessObservation.js +40 -0
- package/dist/internal/runner/ProcessObservation.js.map +1 -0
- package/dist/internal/runner/Processes.js +83 -0
- package/dist/internal/runner/Processes.js.map +1 -0
- package/dist/internal/runner/Retention.js +13 -0
- package/dist/internal/runner/Retention.js.map +1 -0
- package/dist/internal/runner/Store.js +78 -0
- package/dist/internal/runner/Store.js.map +1 -0
- package/dist/internal/runner/Supervisor.js +375 -0
- package/dist/internal/runner/Supervisor.js.map +1 -0
- package/dist/internal/runner/Watchdog.js +47 -0
- package/dist/internal/runner/Watchdog.js.map +1 -0
- package/dist/internal/runner/WatchdogProtocol.js +19 -0
- package/dist/internal/runner/WatchdogProtocol.js.map +1 -0
- package/dist/internal/runner/Wire.d.ts +5 -0
- package/dist/internal/runner/Wire.js +64 -0
- package/dist/internal/runner/Wire.js.map +1 -0
- package/dist/internal/runner/accept.js +57 -0
- package/dist/internal/runner/accept.js.map +1 -0
- package/dist/internal/runner/command.js +9 -0
- package/dist/internal/runner/command.js.map +1 -0
- package/dist/internal/runner/connectionEnded.js +11 -0
- package/dist/internal/runner/connectionEnded.js.map +1 -0
- package/dist/internal/runner/model.d.ts +17 -0
- package/dist/internal/runner/model.d.ts.map +1 -0
- package/dist/internal/runner/model.js +231 -0
- package/dist/internal/runner/model.js.map +1 -0
- package/dist/internal/serve.js +60 -0
- package/dist/internal/serve.js.map +1 -0
- package/dist/internal/services.d.ts +18 -0
- package/dist/internal/services.d.ts.map +1 -0
- package/dist/internal/services.js +22 -0
- package/dist/internal/services.js.map +1 -0
- package/dist/internal/socketChannel.js +24 -0
- package/dist/internal/socketChannel.js.map +1 -0
- package/dist/internal/stdioChannel.js +23 -0
- package/dist/internal/stdioChannel.js.map +1 -0
- package/dist/internal/supervisorPlugin.js +1 -0
- package/dist/internal/supervisorPlugin.js.map +1 -0
- package/dist/internal/transportOptions.d.ts +18 -0
- package/dist/internal/transportOptions.d.ts.map +1 -0
- package/dist/internal/watchdogMain.d.ts +1 -0
- package/dist/internal/watchdogMain.js +55 -0
- package/dist/internal/watchdogMain.js.map +1 -0
- package/dist/internal/websocket.js +46 -0
- package/dist/internal/websocket.js.map +1 -0
- package/dist/internal/websocketServer.js +61 -0
- package/dist/internal/websocketServer.js.map +1 -0
- package/dist/internal/workerBootstrap.js +9 -0
- package/dist/internal/workerBootstrap.js.map +1 -0
- package/dist/internal/workerChannel.js +24 -0
- package/dist/internal/workerChannel.js.map +1 -0
- package/dist/internal/workerMain.d.ts +1 -0
- package/dist/internal/workerMain.js +18 -0
- package/dist/internal/workerMain.js.map +1 -0
- package/dist/port-Blbh8tms.js +285 -0
- package/dist/release-BY4Mlluy.js +23 -0
- package/dist/rolldown-runtime-DYf_mQCB.js +34 -0
- package/dist/runner/auth/bearer.d.ts +7 -0
- package/dist/runner/auth/bearer.d.ts.map +1 -0
- package/dist/runner/auth/bearer.js +42 -0
- package/dist/runner/auth/bearer.js.map +1 -0
- package/dist/runner/auth/jwt.d.ts +19 -0
- package/dist/runner/auth/jwt.d.ts.map +1 -0
- package/dist/runner/auth/jwt.js +65 -0
- package/dist/runner/auth/jwt.js.map +1 -0
- package/dist/runner/auth/mtls.d.ts +11 -0
- package/dist/runner/auth/mtls.d.ts.map +1 -0
- package/dist/runner/auth/mtls.js +48 -0
- package/dist/runner/auth/mtls.js.map +1 -0
- package/dist/runner/auth/none.d.ts +3 -0
- package/dist/runner/auth/none.d.ts.map +1 -0
- package/dist/runner/auth/none.js +13 -0
- package/dist/runner/auth/none.js.map +1 -0
- package/dist/runner/index.d.ts +3 -0
- package/dist/runner/index.d.ts.map +1 -0
- package/dist/runner/index.js +71 -0
- package/dist/runner/index.js.map +1 -0
- package/dist/runner/stdio.d.ts +7 -0
- package/dist/runner/stdio.d.ts.map +1 -0
- package/dist/runner/stdio.js +39 -0
- package/dist/runner/stdio.js.map +1 -0
- package/dist/runner/unix.d.ts +7 -0
- package/dist/runner/unix.d.ts.map +1 -0
- package/dist/runner/unix.js +42 -0
- package/dist/runner/unix.js.map +1 -0
- package/dist/runner/websocket.d.ts +28 -0
- package/dist/runner/websocket.d.ts.map +1 -0
- package/dist/runner/websocket.js +77 -0
- package/dist/runner/websocket.js.map +1 -0
- package/dist/socketChannel-KmtzF-9G.js +3658 -0
- package/dist/supervisorPlugin-Inb1S5IV.js +737 -0
- package/dist/token-v5ne2I0-.js +17 -0
- package/dist/tokenCredentials-WUJm2AKm.js +31 -0
- package/dist/websocketServer-MXcCJx0v.js +58 -0
- package/dist/workerChannel-P82YdNjh.js +27 -0
- package/package.json +69 -3
- package/src/Auth.ts +62 -0
- package/src/Contract.ts +100 -0
- package/src/Errors.ts +82 -0
- package/src/Instance.ts +12 -0
- package/src/Testing.ts +135 -0
- package/src/Transport.ts +52 -0
- package/src/bin.ts +145 -0
- package/src/host/auth/bearer.ts +10 -0
- package/src/host/auth/jwt.ts +10 -0
- package/src/host/auth/mtls.ts +48 -0
- package/src/host/auth/none.ts +13 -0
- package/src/host/command.ts +131 -0
- package/src/host/local.ts +41 -0
- package/src/host/unix.ts +65 -0
- package/src/host/websocket.ts +66 -0
- package/src/host/worker.ts +90 -0
- package/src/index.ts +115 -0
- package/src/internal/Certificates.ts +330 -0
- package/src/internal/FailureGroup.ts +176 -0
- package/src/internal/Limits.ts +12 -0
- package/src/internal/Peer.ts +4 -0
- package/src/internal/ServeLifecycle.ts +53 -0
- package/src/internal/Supervisor.ts +14 -0
- package/src/internal/assembly.ts +337 -0
- package/src/internal/auth/mtls.ts +40 -0
- package/src/internal/auth/token.ts +26 -0
- package/src/internal/auth/tokenCredentials.ts +65 -0
- package/src/internal/child.ts +323 -0
- package/src/internal/childErrors.ts +44 -0
- package/src/internal/childRuntime.ts +111 -0
- package/src/internal/cleanup.ts +34 -0
- package/src/internal/config.ts +8 -0
- package/src/internal/control.ts +49 -0
- package/src/internal/credentials.ts +112 -0
- package/src/internal/directTransport.ts +38 -0
- package/src/internal/entry.ts +288 -0
- package/src/internal/entryConfig.ts +190 -0
- package/src/internal/execution.ts +121 -0
- package/src/internal/follow.ts +17 -0
- package/src/internal/forkTree.ts +13 -0
- package/src/internal/host/local.ts +53 -0
- package/src/internal/host/runner.ts +187 -0
- package/src/internal/host.ts +209 -0
- package/src/internal/importer.ts +27 -0
- package/src/internal/listenerStartup.ts +55 -0
- package/src/internal/loadModule.ts +23 -0
- package/src/internal/platform.ts +12 -0
- package/src/internal/port.ts +623 -0
- package/src/internal/release.ts +71 -0
- package/src/internal/runner/Bootstrap.ts +9 -0
- package/src/internal/runner/Connection.ts +184 -0
- package/src/internal/runner/Execution.ts +244 -0
- package/src/internal/runner/Framing.ts +122 -0
- package/src/internal/runner/Local.ts +73 -0
- package/src/internal/runner/Lock.ts +29 -0
- package/src/internal/runner/Multiplex.ts +120 -0
- package/src/internal/runner/Packages.ts +178 -0
- package/src/internal/runner/ProcessObservation.ts +63 -0
- package/src/internal/runner/Processes.ts +109 -0
- package/src/internal/runner/Retention.ts +49 -0
- package/src/internal/runner/Store.ts +160 -0
- package/src/internal/runner/Supervisor.ts +901 -0
- package/src/internal/runner/Watchdog.ts +83 -0
- package/src/internal/runner/WatchdogProtocol.ts +12 -0
- package/src/internal/runner/Wire.ts +99 -0
- package/src/internal/runner/accept.ts +103 -0
- package/src/internal/runner/command.ts +15 -0
- package/src/internal/runner/connectionEnded.ts +28 -0
- package/src/internal/runner/model.ts +364 -0
- package/src/internal/serve.ts +117 -0
- package/src/internal/services.ts +74 -0
- package/src/internal/socketChannel.ts +64 -0
- package/src/internal/stdioChannel.ts +25 -0
- package/src/internal/supervisorPlugin.ts +67 -0
- package/src/internal/transportOptions.ts +17 -0
- package/src/internal/watchdogMain.ts +72 -0
- package/src/internal/websocket.ts +81 -0
- package/src/internal/websocketServer.ts +152 -0
- package/src/internal/workerBootstrap.ts +6 -0
- package/src/internal/workerChannel.ts +54 -0
- package/src/internal/workerMain.ts +23 -0
- package/src/runner/auth/bearer.ts +66 -0
- package/src/runner/auth/jwt.ts +114 -0
- package/src/runner/auth/mtls.ts +37 -0
- package/src/runner/auth/none.ts +13 -0
- package/src/runner/index.ts +97 -0
- package/src/runner/stdio.ts +76 -0
- package/src/runner/unix.ts +53 -0
- package/src/runner/websocket.ts +143 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Cause, Effect, FiberSet, Queue, Sink, Stream } from "effect"
|
|
2
|
+
import { TransportError } from "../Errors"
|
|
3
|
+
import type { Channel } from "./port"
|
|
4
|
+
|
|
5
|
+
/** Both ends send owned ArrayBuffers; null is the byte stream's EOF. */
|
|
6
|
+
export interface Endpoint {
|
|
7
|
+
readonly postMessage: (
|
|
8
|
+
value: ArrayBuffer | null,
|
|
9
|
+
transferList: ArrayBuffer[],
|
|
10
|
+
) => void
|
|
11
|
+
readonly on: (
|
|
12
|
+
event: "message",
|
|
13
|
+
listener: (value: ArrayBuffer | null) => void,
|
|
14
|
+
) => unknown
|
|
15
|
+
readonly off: (
|
|
16
|
+
event: "message",
|
|
17
|
+
listener: (value: ArrayBuffer | null) => void,
|
|
18
|
+
) => unknown
|
|
19
|
+
}
|
|
20
|
+
export const make = Effect.fnUntraced(function* (endpoint: Endpoint) {
|
|
21
|
+
// Port owns admission and RPC chunk backpressure; this queue only bridges message events.
|
|
22
|
+
const input = yield* Queue.unbounded<Uint8Array, Cause.Done>()
|
|
23
|
+
const callbacks = yield* FiberSet.make()
|
|
24
|
+
const run = yield* FiberSet.runtime(callbacks)()
|
|
25
|
+
const receive = (value: ArrayBuffer | null) =>
|
|
26
|
+
run(
|
|
27
|
+
value === null
|
|
28
|
+
? Queue.end(input)
|
|
29
|
+
: Queue.offer(input, new Uint8Array(value)),
|
|
30
|
+
)
|
|
31
|
+
yield* Effect.acquireRelease(
|
|
32
|
+
Effect.sync(() => endpoint.on("message", receive)),
|
|
33
|
+
() =>
|
|
34
|
+
Effect.sync(() => endpoint.off("message", receive)).pipe(
|
|
35
|
+
Effect.andThen(Queue.shutdown(input)),
|
|
36
|
+
),
|
|
37
|
+
)
|
|
38
|
+
const send = (bytes: Uint8Array | null) =>
|
|
39
|
+
Effect.try({
|
|
40
|
+
try: () => {
|
|
41
|
+
// Copy even Buffer slices: only this fresh, exact-sized backing store is transferable.
|
|
42
|
+
const buffer = bytes === null ? null : new Uint8Array(bytes).buffer
|
|
43
|
+
endpoint.postMessage(buffer, buffer === null ? [] : [buffer])
|
|
44
|
+
},
|
|
45
|
+
catch: (cause) => TransportError.make({ cause: Cause.die(cause) }),
|
|
46
|
+
})
|
|
47
|
+
return {
|
|
48
|
+
input: Stream.fromQueue(input),
|
|
49
|
+
output: Sink.forEach((bytes: Uint8Array) => send(bytes)).pipe(
|
|
50
|
+
// Closing the output half always sends EOF, including when its writer is interrupted.
|
|
51
|
+
Sink.ensuring(send(null)),
|
|
52
|
+
),
|
|
53
|
+
} satisfies Channel
|
|
54
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Executable entry for a direct worker; platform services belong to this thread. */
|
|
2
|
+
import { Graft } from "@graftland/graft"
|
|
3
|
+
import { NodeRuntime } from "@effect/platform-node"
|
|
4
|
+
import { Effect, Layer, Schema } from "effect"
|
|
5
|
+
import { parentPort, workerData } from "node:worker_threads"
|
|
6
|
+
import { load, run } from "./childRuntime"
|
|
7
|
+
import { Bootstrap } from "./workerBootstrap"
|
|
8
|
+
import * as Channel from "./workerChannel"
|
|
9
|
+
import { platform } from "./platform"
|
|
10
|
+
|
|
11
|
+
const main = Effect.scoped(
|
|
12
|
+
Effect.gen(function* () {
|
|
13
|
+
const port = yield* Effect.fromNullishOr(parentPort)
|
|
14
|
+
yield* Effect.addFinalizer(() => Effect.sync(() => port.close()))
|
|
15
|
+
const bootstrap = yield* Schema.decodeUnknownEffect(Bootstrap)(workerData)
|
|
16
|
+
const channel = yield* Channel.make(port)
|
|
17
|
+
const contract = yield* load(bootstrap.contractModule)
|
|
18
|
+
yield* run(contract, channel, bootstrap.session)
|
|
19
|
+
}),
|
|
20
|
+
)
|
|
21
|
+
NodeRuntime.runMain(
|
|
22
|
+
main.pipe(Effect.provide(Graft.layer.pipe(Layer.provide(platform)))),
|
|
23
|
+
)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Option } from "effect"
|
|
2
|
+
import { Graft, Plugin } from "@graftland/graft"
|
|
3
|
+
import {
|
|
4
|
+
Array,
|
|
5
|
+
Effect,
|
|
6
|
+
FileSystem,
|
|
7
|
+
Path,
|
|
8
|
+
Record,
|
|
9
|
+
Redacted,
|
|
10
|
+
Schema,
|
|
11
|
+
} from "effect"
|
|
12
|
+
import { Digest } from "effect/crypto"
|
|
13
|
+
import { Hex } from "effect/encoding"
|
|
14
|
+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Token digests use Node's constant-time comparison
|
|
15
|
+
import { timingSafeEqual } from "node:crypto"
|
|
16
|
+
import { Authenticator } from "../../Auth"
|
|
17
|
+
import { ConfigDirectory } from "@graftland/loader"
|
|
18
|
+
import * as Token from "../../internal/auth/token"
|
|
19
|
+
|
|
20
|
+
export const Config = Schema.Struct({ tokens: Schema.String })
|
|
21
|
+
const Tokens = Schema.fromJsonString(
|
|
22
|
+
Schema.Record(
|
|
23
|
+
Schema.String.check(Schema.isPattern(/^[0-9a-fA-F]{64}$/)),
|
|
24
|
+
Schema.NonEmptyString,
|
|
25
|
+
),
|
|
26
|
+
)
|
|
27
|
+
export default Plugin.make({
|
|
28
|
+
name: "remote-bearer-authenticator",
|
|
29
|
+
config: Config,
|
|
30
|
+
inject: [FileSystem.FileSystem, Path.Path, Digest.Digest],
|
|
31
|
+
apply: Effect.fnUntraced(function* (config) {
|
|
32
|
+
const fs = yield* FileSystem.FileSystem
|
|
33
|
+
const path = yield* Path.Path
|
|
34
|
+
const directory = yield* ConfigDirectory.pipe(
|
|
35
|
+
Effect.map(Option.getOrElse(() => ".")),
|
|
36
|
+
)
|
|
37
|
+
const digest = yield* Digest.Digest
|
|
38
|
+
const file = yield* fs.readFileString(
|
|
39
|
+
path.resolve(directory, config.tokens),
|
|
40
|
+
)
|
|
41
|
+
const mapping = yield* Schema.decodeEffect(Tokens)(file)
|
|
42
|
+
const tokens = yield* Effect.forEach(
|
|
43
|
+
Record.toEntries(mapping),
|
|
44
|
+
([hex, identity]) =>
|
|
45
|
+
Effect.fromResult(Hex.decode(hex)).pipe(
|
|
46
|
+
Effect.map((hash) => ({ hash, identity })),
|
|
47
|
+
),
|
|
48
|
+
)
|
|
49
|
+
yield* Graft.provide(Authenticator, {
|
|
50
|
+
_tag: "Header",
|
|
51
|
+
authenticate: Effect.fnUntraced(function* (authorization) {
|
|
52
|
+
const token = yield* Token.read(authorization)
|
|
53
|
+
const hash = yield* digest.digest(
|
|
54
|
+
Digest.sha256,
|
|
55
|
+
new TextEncoder().encode(Redacted.value(token)),
|
|
56
|
+
)
|
|
57
|
+
const found = Array.findFirst(tokens, (entry) =>
|
|
58
|
+
timingSafeEqual(hash, entry.hash),
|
|
59
|
+
)
|
|
60
|
+
return yield* Effect.fromOption(found, Token.invalid).pipe(
|
|
61
|
+
Effect.map((entry) => entry.identity),
|
|
62
|
+
)
|
|
63
|
+
}),
|
|
64
|
+
})
|
|
65
|
+
}),
|
|
66
|
+
})
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { Option } from "effect"
|
|
2
|
+
import { Graft, Plugin } from "@graftland/graft"
|
|
3
|
+
import {
|
|
4
|
+
Cause,
|
|
5
|
+
Duration,
|
|
6
|
+
Effect,
|
|
7
|
+
FileSystem,
|
|
8
|
+
Layer,
|
|
9
|
+
Path,
|
|
10
|
+
Schema,
|
|
11
|
+
} from "effect"
|
|
12
|
+
import { Jwk, Jwks, Jwt } from "effect/jose"
|
|
13
|
+
import { Signature } from "effect/crypto"
|
|
14
|
+
import { HttpClient } from "effect/http"
|
|
15
|
+
import { Authenticator } from "../../Auth"
|
|
16
|
+
import { ConfigDirectory } from "@graftland/loader"
|
|
17
|
+
import { AuthenticationFailed } from "../../Errors"
|
|
18
|
+
import * as Token from "../../internal/auth/token"
|
|
19
|
+
|
|
20
|
+
export const ClockTolerance = Schema.DurationFromString.check(
|
|
21
|
+
Schema.makeFilter(
|
|
22
|
+
(duration) =>
|
|
23
|
+
Duration.isFinite(duration) && Duration.toMillis(duration) >= 0,
|
|
24
|
+
{ expected: "a finite, non-negative duration" },
|
|
25
|
+
),
|
|
26
|
+
)
|
|
27
|
+
export const Config = Schema.Struct({
|
|
28
|
+
issuer: Schema.NonEmptyString,
|
|
29
|
+
audience: Schema.NonEmptyArray(Schema.NonEmptyString),
|
|
30
|
+
algorithms: Schema.NonEmptyArray(Jwk.AsymmetricAlgorithm),
|
|
31
|
+
identityClaim: Schema.NonEmptyString.pipe(
|
|
32
|
+
Schema.withDecodingDefaultKey(Effect.succeed("sub")),
|
|
33
|
+
),
|
|
34
|
+
clockTolerance: ClockTolerance.pipe(
|
|
35
|
+
Schema.withDecodingDefaultKey(Effect.succeed("5 seconds")),
|
|
36
|
+
),
|
|
37
|
+
keys: Schema.Union([
|
|
38
|
+
Schema.Struct({
|
|
39
|
+
jwksUrl: Schema.String,
|
|
40
|
+
publicKey: Schema.optionalKey(Schema.Never),
|
|
41
|
+
}),
|
|
42
|
+
Schema.Struct({
|
|
43
|
+
publicKey: Schema.String,
|
|
44
|
+
jwksUrl: Schema.optionalKey(Schema.Never),
|
|
45
|
+
}),
|
|
46
|
+
]),
|
|
47
|
+
})
|
|
48
|
+
const keys = Effect.fnUntraced(function* (config: typeof Config.Type) {
|
|
49
|
+
if (config.keys.jwksUrl !== undefined)
|
|
50
|
+
return Jwks.layerRemote({ url: config.keys.jwksUrl })
|
|
51
|
+
const fs = yield* FileSystem.FileSystem
|
|
52
|
+
const path = yield* Path.Path
|
|
53
|
+
const directory = yield* ConfigDirectory.pipe(
|
|
54
|
+
Effect.map(Option.getOrElse(() => ".")),
|
|
55
|
+
)
|
|
56
|
+
const pem = yield* fs.readFileString(
|
|
57
|
+
path.resolve(directory, config.keys.publicKey),
|
|
58
|
+
)
|
|
59
|
+
// A single PEM must support the allowed algorithms; use JWKS for multiple key types.
|
|
60
|
+
const keys = yield* Effect.forEach(config.algorithms, (algorithm) =>
|
|
61
|
+
Jwk.importSpki(pem, algorithm).pipe(Effect.flatMap(Jwk.exportPublic)),
|
|
62
|
+
)
|
|
63
|
+
return Jwks.layerLocal({ keys })
|
|
64
|
+
})
|
|
65
|
+
export default Plugin.make({
|
|
66
|
+
name: "remote-jwt-authenticator",
|
|
67
|
+
config: Config,
|
|
68
|
+
inject: [
|
|
69
|
+
FileSystem.FileSystem,
|
|
70
|
+
Path.Path,
|
|
71
|
+
Signature.Signature,
|
|
72
|
+
HttpClient.HttpClient,
|
|
73
|
+
],
|
|
74
|
+
apply: Effect.fnUntraced(function* (config) {
|
|
75
|
+
const keySet = yield* keys(config)
|
|
76
|
+
const services = yield* Layer.build(keySet)
|
|
77
|
+
const signature = yield* Signature.Signature
|
|
78
|
+
yield* Graft.provide(Authenticator, {
|
|
79
|
+
_tag: "Header",
|
|
80
|
+
issuer: config.issuer,
|
|
81
|
+
authenticate: (authorization) =>
|
|
82
|
+
Token.read(authorization).pipe(
|
|
83
|
+
Effect.flatMap((token) =>
|
|
84
|
+
Jwt.verifyWithKeySet(
|
|
85
|
+
Schema.Record(Schema.String, Schema.Unknown),
|
|
86
|
+
token,
|
|
87
|
+
{
|
|
88
|
+
issuer: config.issuer,
|
|
89
|
+
audience: config.audience,
|
|
90
|
+
algorithms: config.algorithms,
|
|
91
|
+
clockTolerance: config.clockTolerance,
|
|
92
|
+
requiredClaims: ["exp"],
|
|
93
|
+
},
|
|
94
|
+
).pipe(
|
|
95
|
+
Effect.flatMap((verified) =>
|
|
96
|
+
Schema.decodeUnknownEffect(Schema.NonEmptyString)(
|
|
97
|
+
verified.claims[config.identityClaim],
|
|
98
|
+
),
|
|
99
|
+
),
|
|
100
|
+
Effect.mapError((error) =>
|
|
101
|
+
AuthenticationFailed.make({
|
|
102
|
+
reason: "invalid",
|
|
103
|
+
challenge: 'Bearer error="invalid_token"',
|
|
104
|
+
cause: Cause.fail(error),
|
|
105
|
+
}),
|
|
106
|
+
),
|
|
107
|
+
Effect.provideService(Signature.Signature, signature),
|
|
108
|
+
Effect.provideContext(services),
|
|
109
|
+
),
|
|
110
|
+
),
|
|
111
|
+
),
|
|
112
|
+
})
|
|
113
|
+
}),
|
|
114
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Graft, Plugin } from "@graftland/graft"
|
|
2
|
+
import { Effect, FileSystem, Option, Path, Schema } from "effect"
|
|
3
|
+
import { Authenticator, type AuthenticatorApi } from "../../Auth"
|
|
4
|
+
import { ConfigDirectory } from "@graftland/loader"
|
|
5
|
+
import { identityFromSubjectAltName } from "../../internal/auth/mtls"
|
|
6
|
+
|
|
7
|
+
export const Config = Schema.Struct({ ca: Schema.String })
|
|
8
|
+
const read = Effect.fnUntraced(function* (ca: string) {
|
|
9
|
+
const fs = yield* FileSystem.FileSystem
|
|
10
|
+
const path = yield* Path.Path
|
|
11
|
+
const directory = yield* ConfigDirectory.pipe(
|
|
12
|
+
Effect.map(Option.getOrElse(() => ".")),
|
|
13
|
+
)
|
|
14
|
+
return yield* fs.readFileString(path.resolve(directory, ca))
|
|
15
|
+
})
|
|
16
|
+
export const make = (
|
|
17
|
+
ca: string,
|
|
18
|
+
): Extract<AuthenticatorApi, { readonly _tag: "Mtls" }> => ({
|
|
19
|
+
_tag: "Mtls",
|
|
20
|
+
tls: {
|
|
21
|
+
ca: [ca],
|
|
22
|
+
minVersion: "TLSv1.3",
|
|
23
|
+
requestCert: true,
|
|
24
|
+
rejectUnauthorized: true,
|
|
25
|
+
},
|
|
26
|
+
authenticate: (peer) =>
|
|
27
|
+
identityFromSubjectAltName(Option.getOrUndefined(peer.subjectAltName)),
|
|
28
|
+
})
|
|
29
|
+
export default Plugin.make({
|
|
30
|
+
name: "remote-certificate-authenticator",
|
|
31
|
+
config: Config,
|
|
32
|
+
inject: [FileSystem.FileSystem, Path.Path],
|
|
33
|
+
apply: ({ ca }) =>
|
|
34
|
+
read(ca).pipe(
|
|
35
|
+
Effect.flatMap((ca) => Graft.provide(Authenticator, make(ca))),
|
|
36
|
+
),
|
|
37
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Graft, Plugin } from "@graftland/graft"
|
|
2
|
+
import { Effect, Schema } from "effect"
|
|
3
|
+
import { Authenticator } from "../../Auth"
|
|
4
|
+
|
|
5
|
+
export default Plugin.make({
|
|
6
|
+
name: "remote-none-authenticator",
|
|
7
|
+
config: Schema.Struct({
|
|
8
|
+
identity: Schema.String.pipe(
|
|
9
|
+
Schema.withDecodingDefaultKey(Effect.succeed("anonymous")),
|
|
10
|
+
),
|
|
11
|
+
}),
|
|
12
|
+
apply: (config) => Graft.provide(Authenticator, { _tag: "None", ...config }),
|
|
13
|
+
})
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Graft, Plugin } from "@graftland/graft"
|
|
2
|
+
import { SecureRandom } from "effect/crypto"
|
|
3
|
+
import { Path } from "effect"
|
|
4
|
+
import { ModuleResolver } from "@graftland/loader"
|
|
5
|
+
import { Deferred, Effect, Schema, Stream } from "effect"
|
|
6
|
+
import * as Configuration from "../internal/entryConfig"
|
|
7
|
+
import { boundary, resolve } from "../internal/entry"
|
|
8
|
+
import * as Assembly from "../internal/assembly"
|
|
9
|
+
import * as Startup from "../internal/listenerStartup"
|
|
10
|
+
import { follow } from "../internal/follow"
|
|
11
|
+
import { Supervisor } from "../internal/Supervisor"
|
|
12
|
+
import supervisor, {
|
|
13
|
+
Config as SupervisorConfig,
|
|
14
|
+
} from "../internal/supervisorPlugin"
|
|
15
|
+
|
|
16
|
+
const startListener = Effect.fnUntraced(function* (
|
|
17
|
+
input: Configuration.Listen,
|
|
18
|
+
index: number,
|
|
19
|
+
begin: Deferred.Deferred<void>,
|
|
20
|
+
) {
|
|
21
|
+
const field = `listen[${index}]`
|
|
22
|
+
const transport = yield* resolve(
|
|
23
|
+
Configuration.runnerTransport(input),
|
|
24
|
+
field + ".transport",
|
|
25
|
+
)
|
|
26
|
+
const auth = yield* resolve(
|
|
27
|
+
Configuration.runnerAuthentication(input),
|
|
28
|
+
field + ".auth",
|
|
29
|
+
)
|
|
30
|
+
const signals = yield* Startup.signals
|
|
31
|
+
const consumer = Assembly.makeListenerConsumer(
|
|
32
|
+
(properties, auth) =>
|
|
33
|
+
Assembly.directAuth(properties, input.auth !== undefined).pipe(
|
|
34
|
+
Effect.andThen(
|
|
35
|
+
Deferred.succeed(signals.description, { properties, auth }),
|
|
36
|
+
),
|
|
37
|
+
Effect.andThen(Deferred.await(begin)),
|
|
38
|
+
),
|
|
39
|
+
Deferred.succeed(signals.ready, undefined).pipe(Effect.asVoid),
|
|
40
|
+
)
|
|
41
|
+
const fork = yield* Assembly.runner({ transport, auth }, consumer)
|
|
42
|
+
return { fork, signals, field }
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
const owner = Plugin.make({
|
|
46
|
+
name: "remote-supervised-runtime",
|
|
47
|
+
config: Configuration.Runner,
|
|
48
|
+
inject: [ModuleResolver.ModuleResolver, Path.Path, SecureRandom.SecureRandom],
|
|
49
|
+
apply: Effect.fnUntraced(function* (config) {
|
|
50
|
+
const options = yield* Schema.encodeEffect(SupervisorConfig)(config)
|
|
51
|
+
const engine = yield* Graft.plugin(supervisor, options)
|
|
52
|
+
const fail = yield* boundary("supervisor")
|
|
53
|
+
yield* follow(engine, fail)
|
|
54
|
+
const begin = yield* Deferred.make<void>()
|
|
55
|
+
const listeners = yield* Effect.forEach(config.listen, (input, index) =>
|
|
56
|
+
startListener(input, index, begin),
|
|
57
|
+
)
|
|
58
|
+
const descriptions = yield* Effect.forEach(
|
|
59
|
+
listeners,
|
|
60
|
+
({ fork, signals }) => Startup.wait(fork, signals.description),
|
|
61
|
+
{ concurrency: "unbounded" },
|
|
62
|
+
)
|
|
63
|
+
yield* Startup.validate(descriptions)
|
|
64
|
+
yield* Deferred.succeed(begin, undefined)
|
|
65
|
+
yield* Effect.forEach(
|
|
66
|
+
listeners,
|
|
67
|
+
({ fork, signals }) => Startup.wait(fork, signals.ready),
|
|
68
|
+
{ concurrency: "unbounded" },
|
|
69
|
+
)
|
|
70
|
+
yield* Effect.forEach(
|
|
71
|
+
listeners,
|
|
72
|
+
({ fork, field }) =>
|
|
73
|
+
Plugin.fork(
|
|
74
|
+
fork.state.pipe(
|
|
75
|
+
Stream.runForEach((state) =>
|
|
76
|
+
state._tag === "Failed"
|
|
77
|
+
? Effect.logWarning("Remote listener failed", state.cause).pipe(
|
|
78
|
+
Effect.annotateLogs({ "remote.listener": field }),
|
|
79
|
+
)
|
|
80
|
+
: Effect.void,
|
|
81
|
+
),
|
|
82
|
+
),
|
|
83
|
+
),
|
|
84
|
+
{ discard: true },
|
|
85
|
+
)
|
|
86
|
+
}),
|
|
87
|
+
})
|
|
88
|
+
export default Plugin.make({
|
|
89
|
+
name: "remote-server",
|
|
90
|
+
config: Configuration.Runner,
|
|
91
|
+
apply: Effect.fnUntraced(function* (config) {
|
|
92
|
+
const encoded = yield* Schema.encodeEffect(Configuration.Runner)(config)
|
|
93
|
+
const fork = yield* Graft.plugin(owner, encoded, { isolate: [Supervisor] })
|
|
94
|
+
const fail = yield* boundary("runner")
|
|
95
|
+
yield* follow(fork, fail)
|
|
96
|
+
}),
|
|
97
|
+
})
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Graft, Plugin } from "@graftland/graft"
|
|
2
|
+
import {
|
|
3
|
+
Cause,
|
|
4
|
+
Deferred,
|
|
5
|
+
Effect,
|
|
6
|
+
Exit,
|
|
7
|
+
Option,
|
|
8
|
+
Schema,
|
|
9
|
+
Sink,
|
|
10
|
+
Stdio,
|
|
11
|
+
Stream,
|
|
12
|
+
} from "effect"
|
|
13
|
+
import { Listener, type Accept } from "../Transport"
|
|
14
|
+
import { TransportError } from "../Errors"
|
|
15
|
+
import * as Release from "../internal/release"
|
|
16
|
+
|
|
17
|
+
export const Config = Schema.Struct({ auth: Schema.optionalKey(Schema.Never) })
|
|
18
|
+
export default Plugin.make({
|
|
19
|
+
name: "remote-stdio-listener",
|
|
20
|
+
config: Config,
|
|
21
|
+
inject: [Stdio.Stdio],
|
|
22
|
+
apply: Effect.fnUntraced(function* () {
|
|
23
|
+
const stdio = yield* Stdio.Stdio
|
|
24
|
+
yield* Graft.provide(Listener, {
|
|
25
|
+
properties: {
|
|
26
|
+
protocol: "stdio",
|
|
27
|
+
encrypted: false,
|
|
28
|
+
network: false,
|
|
29
|
+
carries: [],
|
|
30
|
+
},
|
|
31
|
+
bind: Effect.fnUntraced(function* () {
|
|
32
|
+
const ready = yield* Deferred.make<void>()
|
|
33
|
+
const eof = yield* Deferred.make<void>()
|
|
34
|
+
const channel = {
|
|
35
|
+
input: stdio.stdin.pipe(
|
|
36
|
+
Stream.onExit((exit) =>
|
|
37
|
+
Exit.isSuccess(exit)
|
|
38
|
+
? Deferred.succeed(eof, undefined)
|
|
39
|
+
: Effect.void,
|
|
40
|
+
),
|
|
41
|
+
Stream.mapError((error) =>
|
|
42
|
+
TransportError.make({ cause: Cause.fail(error) }),
|
|
43
|
+
),
|
|
44
|
+
),
|
|
45
|
+
output: stdio
|
|
46
|
+
.stdout({ endOnDone: false })
|
|
47
|
+
.pipe(
|
|
48
|
+
Sink.mapError((error) =>
|
|
49
|
+
TransportError.make({ cause: Cause.fail(error) }),
|
|
50
|
+
),
|
|
51
|
+
),
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
ready: Deferred.await(ready),
|
|
55
|
+
serve: (accept: Accept) =>
|
|
56
|
+
accept({
|
|
57
|
+
authorization: Option.none(),
|
|
58
|
+
certificate: Option.none(),
|
|
59
|
+
}).pipe(
|
|
60
|
+
Effect.mapError((error) =>
|
|
61
|
+
TransportError.make({ cause: Cause.fail(error) }),
|
|
62
|
+
),
|
|
63
|
+
Effect.flatMap((session) =>
|
|
64
|
+
Deferred.succeed(ready, undefined).pipe(
|
|
65
|
+
Effect.andThen(session(channel)),
|
|
66
|
+
),
|
|
67
|
+
),
|
|
68
|
+
Release.scoped,
|
|
69
|
+
// Successful session completion alone is not EOF.
|
|
70
|
+
Effect.andThen(Deferred.await(eof)),
|
|
71
|
+
),
|
|
72
|
+
}
|
|
73
|
+
}),
|
|
74
|
+
})
|
|
75
|
+
}),
|
|
76
|
+
})
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Graft, Plugin } from "@graftland/graft"
|
|
2
|
+
import { ConfigDirectory } from "@graftland/loader"
|
|
3
|
+
import { Cause, Console, Effect, Option, Path, Schema } from "effect"
|
|
4
|
+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- NodeHttpServer takes a factory for an HTTP/1.1 server on the Unix socket
|
|
5
|
+
import { createServer } from "node:http"
|
|
6
|
+
import { Listener } from "../Transport"
|
|
7
|
+
import { TransportError } from "../Errors"
|
|
8
|
+
import * as Server from "../internal/websocketServer"
|
|
9
|
+
|
|
10
|
+
export const Config = Schema.Struct({ path: Schema.String })
|
|
11
|
+
const Listening = Schema.fromJsonString(
|
|
12
|
+
Schema.Struct({ event: Schema.Literal("Listening"), path: Schema.String }),
|
|
13
|
+
)
|
|
14
|
+
export default Plugin.make({
|
|
15
|
+
name: "remote-unix-listener",
|
|
16
|
+
config: Config,
|
|
17
|
+
inject: [Path.Path],
|
|
18
|
+
apply: Effect.fnUntraced(function* (config) {
|
|
19
|
+
const path = yield* Path.Path
|
|
20
|
+
const directory = yield* ConfigDirectory
|
|
21
|
+
const socket = path.resolve(
|
|
22
|
+
Option.getOrElse(directory, () => "."),
|
|
23
|
+
config.path,
|
|
24
|
+
)
|
|
25
|
+
yield* Graft.provide(Listener, {
|
|
26
|
+
properties: {
|
|
27
|
+
protocol: "unix",
|
|
28
|
+
encrypted: false,
|
|
29
|
+
network: false,
|
|
30
|
+
carries: ["header"],
|
|
31
|
+
},
|
|
32
|
+
// Node rejects an occupied path and removes its own socket file when the server closes.
|
|
33
|
+
bind: () =>
|
|
34
|
+
Server.bind(createServer, { path: socket }).pipe(
|
|
35
|
+
Effect.mapBoth({
|
|
36
|
+
onSuccess: (server) => ({
|
|
37
|
+
serve: server.serve,
|
|
38
|
+
ready: server.ready.pipe(
|
|
39
|
+
Effect.andThen(
|
|
40
|
+
Schema.encodeEffect(Listening)({
|
|
41
|
+
event: "Listening",
|
|
42
|
+
path: socket,
|
|
43
|
+
}).pipe(Effect.orDie, Effect.flatMap(Console.log)),
|
|
44
|
+
),
|
|
45
|
+
),
|
|
46
|
+
}),
|
|
47
|
+
onFailure: (error) =>
|
|
48
|
+
TransportError.make({ cause: Cause.fail(error) }),
|
|
49
|
+
}),
|
|
50
|
+
),
|
|
51
|
+
})
|
|
52
|
+
}),
|
|
53
|
+
})
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Graft, Plugin } from "@graftland/graft"
|
|
2
|
+
import {
|
|
3
|
+
Cause,
|
|
4
|
+
Console,
|
|
5
|
+
Effect,
|
|
6
|
+
FileSystem,
|
|
7
|
+
Option,
|
|
8
|
+
Path,
|
|
9
|
+
Schema,
|
|
10
|
+
String,
|
|
11
|
+
} from "effect"
|
|
12
|
+
import { ConfigDirectory } from "@graftland/loader"
|
|
13
|
+
import { createSecureServer } from "node:http2"
|
|
14
|
+
import { Url, path as upgradePath } from "../internal/websocket"
|
|
15
|
+
import { Listener } from "../Transport"
|
|
16
|
+
import type { TlsPolicy } from "../Auth"
|
|
17
|
+
import { TransportError } from "../Errors"
|
|
18
|
+
import * as Server from "../internal/websocketServer"
|
|
19
|
+
import { properties } from "../internal/websocket"
|
|
20
|
+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- NodeHttpServer takes a factory for an HTTP/1.1 upgrade server
|
|
21
|
+
import { createServer } from "node:http"
|
|
22
|
+
|
|
23
|
+
const url = (protocol: "ws:" | "wss:") =>
|
|
24
|
+
Url.check(
|
|
25
|
+
Schema.makeFilter((url) => url.protocol === protocol, {
|
|
26
|
+
expected: protocol + "//",
|
|
27
|
+
}),
|
|
28
|
+
Schema.makeFilter(
|
|
29
|
+
(url) => url.pathname === "/" || url.pathname === upgradePath,
|
|
30
|
+
{
|
|
31
|
+
expected: "a listener URL with no path or /graft",
|
|
32
|
+
},
|
|
33
|
+
),
|
|
34
|
+
)
|
|
35
|
+
const ws = {
|
|
36
|
+
auth: Schema.optionalKey(Schema.Never),
|
|
37
|
+
url: url("ws:"),
|
|
38
|
+
cert: Schema.optionalKey(Schema.Never),
|
|
39
|
+
key: Schema.optionalKey(Schema.Never),
|
|
40
|
+
}
|
|
41
|
+
const wss = {
|
|
42
|
+
auth: Schema.optionalKey(Schema.Never),
|
|
43
|
+
url: url("wss:"),
|
|
44
|
+
cert: Schema.String,
|
|
45
|
+
key: Schema.String,
|
|
46
|
+
}
|
|
47
|
+
export const Config = Schema.Union([
|
|
48
|
+
Schema.Struct({
|
|
49
|
+
...ws,
|
|
50
|
+
}).pipe(
|
|
51
|
+
Schema.extendTo(
|
|
52
|
+
{ _tag: Schema.Literal("ws") },
|
|
53
|
+
{ _tag: () => Option.some<"ws">("ws") },
|
|
54
|
+
),
|
|
55
|
+
),
|
|
56
|
+
Schema.Struct({
|
|
57
|
+
...wss,
|
|
58
|
+
}).pipe(
|
|
59
|
+
Schema.extendTo(
|
|
60
|
+
{ _tag: Schema.Literal("wss") },
|
|
61
|
+
{ _tag: () => Option.some<"wss">("wss") },
|
|
62
|
+
),
|
|
63
|
+
),
|
|
64
|
+
])
|
|
65
|
+
|
|
66
|
+
const factory = Effect.fnUntraced(function* (
|
|
67
|
+
config: typeof Config.Type,
|
|
68
|
+
tls: Option.Option<TlsPolicy>,
|
|
69
|
+
) {
|
|
70
|
+
if (config._tag === "ws") return createServer
|
|
71
|
+
const fs = yield* FileSystem.FileSystem
|
|
72
|
+
const path = yield* Path.Path
|
|
73
|
+
const directory = yield* ConfigDirectory.pipe(
|
|
74
|
+
Effect.map(Option.getOrElse(() => ".")),
|
|
75
|
+
)
|
|
76
|
+
const cert = yield* fs.readFileString(path.resolve(directory, config.cert))
|
|
77
|
+
const key = yield* fs.readFileString(path.resolve(directory, config.key))
|
|
78
|
+
return () =>
|
|
79
|
+
createSecureServer({
|
|
80
|
+
allowHTTP1: true,
|
|
81
|
+
cert,
|
|
82
|
+
key,
|
|
83
|
+
minVersion: "TLSv1.3",
|
|
84
|
+
...Option.match(tls, {
|
|
85
|
+
onNone: () => ({}),
|
|
86
|
+
onSome: (tls) => ({ ...tls, ca: [...tls.ca] }),
|
|
87
|
+
}),
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
/** Configured Listener; authentication is supplied to bind, never injected here. */
|
|
91
|
+
export default Plugin.make({
|
|
92
|
+
name: "remote-websocket-capability",
|
|
93
|
+
config: Config,
|
|
94
|
+
inject: [FileSystem.FileSystem, Path.Path],
|
|
95
|
+
apply: Effect.fnUntraced(function* (config) {
|
|
96
|
+
const fs = yield* FileSystem.FileSystem
|
|
97
|
+
const path = yield* Path.Path
|
|
98
|
+
const directory = yield* ConfigDirectory.pipe(
|
|
99
|
+
Effect.map(Option.getOrElse(() => ".")),
|
|
100
|
+
)
|
|
101
|
+
yield* Graft.provide(Listener, {
|
|
102
|
+
properties: properties(config.url),
|
|
103
|
+
bind: (tls) =>
|
|
104
|
+
factory(config, tls).pipe(
|
|
105
|
+
Effect.provideService(FileSystem.FileSystem, fs),
|
|
106
|
+
Effect.provideService(Path.Path, path),
|
|
107
|
+
Effect.provideService(ConfigDirectory, Option.some(directory)),
|
|
108
|
+
Effect.flatMap((server) =>
|
|
109
|
+
Server.bind(server, {
|
|
110
|
+
host: String.replace(/^\[|\]$/g, "")(config.url.hostname),
|
|
111
|
+
port: Number(
|
|
112
|
+
config.url.port || (config._tag === "wss" ? 443 : 80),
|
|
113
|
+
),
|
|
114
|
+
}),
|
|
115
|
+
),
|
|
116
|
+
Effect.mapBoth({
|
|
117
|
+
onSuccess: (server) => ({
|
|
118
|
+
ready: server.ready.pipe(
|
|
119
|
+
Effect.andThen(
|
|
120
|
+
Schema.encodeEffect(Listening)({
|
|
121
|
+
event: "Listening",
|
|
122
|
+
port:
|
|
123
|
+
server.address._tag === "UnixPathAddress"
|
|
124
|
+
? null
|
|
125
|
+
: server.address.port,
|
|
126
|
+
}).pipe(Effect.orDie, Effect.flatMap(Console.log)),
|
|
127
|
+
),
|
|
128
|
+
),
|
|
129
|
+
serve: server.serve,
|
|
130
|
+
}),
|
|
131
|
+
onFailure: (error) =>
|
|
132
|
+
TransportError.make({ cause: Cause.fail(error) }),
|
|
133
|
+
}),
|
|
134
|
+
),
|
|
135
|
+
})
|
|
136
|
+
}),
|
|
137
|
+
})
|
|
138
|
+
const Listening = Schema.fromJsonString(
|
|
139
|
+
Schema.Struct({
|
|
140
|
+
event: Schema.Literal("Listening"),
|
|
141
|
+
port: Schema.NullOr(Schema.Int),
|
|
142
|
+
}),
|
|
143
|
+
)
|