@livestore/utils 0.3.0-dev.4 → 0.3.0-dev.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo.json +1 -1
- package/dist/Deferred.d.ts.map +1 -1
- package/dist/base64.d.ts.map +1 -1
- package/dist/bun/mod.d.ts +5 -0
- package/dist/bun/mod.d.ts.map +1 -0
- package/dist/bun/mod.js +9 -0
- package/dist/bun/mod.js.map +1 -0
- package/dist/cuid/cuid.browser.d.ts.map +1 -1
- package/dist/cuid/cuid.node.d.ts.map +1 -1
- package/dist/effect/BucketQueue.d.ts +15 -2
- package/dist/effect/BucketQueue.d.ts.map +1 -1
- package/dist/effect/BucketQueue.js +24 -6
- package/dist/effect/BucketQueue.js.map +1 -1
- package/dist/effect/Effect.d.ts +4 -2
- package/dist/effect/Effect.d.ts.map +1 -1
- package/dist/effect/Effect.js +21 -18
- package/dist/effect/Effect.js.map +1 -1
- package/dist/effect/Logger.d.ts +2 -0
- package/dist/effect/Logger.d.ts.map +1 -1
- package/dist/effect/Logger.js +16 -1
- package/dist/effect/Logger.js.map +1 -1
- package/dist/effect/Scheduler.d.ts.map +1 -1
- package/dist/effect/Schema/debug-diff.d.ts.map +1 -1
- package/dist/effect/Schema/index.d.ts +3 -0
- package/dist/effect/Schema/index.d.ts.map +1 -1
- package/dist/effect/Schema/index.js +19 -0
- package/dist/effect/Schema/index.js.map +1 -1
- package/dist/effect/Schema/msgpack.d.ts +1 -1
- package/dist/effect/Schema/msgpack.d.ts.map +1 -1
- package/dist/effect/ServiceContext.d.ts.map +1 -1
- package/dist/effect/Stream.d.ts.map +1 -1
- package/dist/effect/Subscribable.d.ts +76 -0
- package/dist/effect/Subscribable.d.ts.map +1 -0
- package/dist/effect/Subscribable.js +80 -0
- package/dist/effect/Subscribable.js.map +1 -0
- package/dist/effect/TaskTracing.d.ts.map +1 -1
- package/dist/effect/{WebChannel.d.ts → WebChannel/WebChannel.d.ts} +29 -16
- package/dist/effect/WebChannel/WebChannel.d.ts.map +1 -0
- package/dist/effect/WebChannel/WebChannel.js +264 -0
- package/dist/effect/WebChannel/WebChannel.js.map +1 -0
- package/dist/effect/WebChannel/WebChannel.test.d.ts +2 -0
- package/dist/effect/WebChannel/WebChannel.test.d.ts.map +1 -0
- package/dist/effect/WebChannel/WebChannel.test.js +62 -0
- package/dist/effect/WebChannel/WebChannel.test.js.map +1 -0
- package/dist/effect/WebChannel/broadcastChannelWithAck.d.ts +5 -6
- package/dist/effect/WebChannel/broadcastChannelWithAck.d.ts.map +1 -1
- package/dist/effect/WebChannel/broadcastChannelWithAck.js +12 -9
- package/dist/effect/WebChannel/broadcastChannelWithAck.js.map +1 -1
- package/dist/effect/WebChannel/common.d.ts +16 -1
- package/dist/effect/WebChannel/common.d.ts.map +1 -1
- package/dist/effect/WebChannel/common.js +19 -1
- package/dist/effect/WebChannel/common.js.map +1 -1
- package/dist/effect/WebChannel/mod.d.ts +4 -0
- package/dist/effect/WebChannel/mod.d.ts.map +1 -0
- package/dist/effect/WebChannel/mod.js +4 -0
- package/dist/effect/WebChannel/mod.js.map +1 -0
- package/dist/effect/WebLock.d.ts.map +1 -1
- package/dist/effect/WebSocket.d.ts +3 -2
- package/dist/effect/WebSocket.d.ts.map +1 -1
- package/dist/effect/WebSocket.js +45 -19
- package/dist/effect/WebSocket.js.map +1 -1
- package/dist/effect/WebSocket.test.d.ts +2 -0
- package/dist/effect/WebSocket.test.d.ts.map +1 -0
- package/dist/effect/WebSocket.test.js +11 -0
- package/dist/effect/WebSocket.test.js.map +1 -0
- package/dist/effect/index.d.ts +5 -3
- package/dist/effect/index.d.ts.map +1 -1
- package/dist/effect/index.js +8 -5
- package/dist/effect/index.js.map +1 -1
- package/dist/env.d.ts +4 -0
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +10 -0
- package/dist/env.js.map +1 -1
- package/dist/fast-deep-equal.d.ts.map +1 -1
- package/dist/guards.d.ts.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/misc.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunner.d.ts +0 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunner.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunner.js +21 -11
- package/dist/node/ChildProcessRunner/ChildProcessRunner.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.d.ts +2 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.d.ts.map +1 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.js +39 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.js.map +1 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.d.ts +75 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.d.ts.map +1 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.js +62 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.js.map +1 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.d.ts +2 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.d.ts.map +1 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js +42 -0
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js.map +1 -0
- package/dist/node/ChildProcessRunner/ChildProcessWorker.d.ts +1 -4
- package/dist/node/ChildProcessRunner/ChildProcessWorker.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessWorker.js +1 -4
- package/dist/node/ChildProcessRunner/ChildProcessWorker.js.map +1 -1
- package/dist/node/mod.d.ts +4 -16
- package/dist/node/mod.d.ts.map +1 -1
- package/dist/node/mod.js +25 -69
- package/dist/node/mod.js.map +1 -1
- package/dist/object/index.d.ts.map +1 -1
- package/dist/object/omit.d.ts.map +1 -1
- package/dist/object/pick.d.ts.map +1 -1
- package/dist/promise.d.ts.map +1 -1
- package/dist/set.d.ts.map +1 -1
- package/dist/string.d.ts.map +1 -1
- package/dist/time.d.ts.map +1 -1
- package/package.json +59 -51
- package/src/bun/mod.ts +12 -0
- package/src/effect/BucketQueue.ts +33 -6
- package/src/effect/Effect.ts +33 -20
- package/src/effect/Logger.ts +23 -1
- package/src/effect/Schema/index.ts +27 -0
- package/src/effect/Subscribable.ts +155 -0
- package/src/effect/WebChannel/WebChannel.test.ts +106 -0
- package/src/effect/WebChannel/WebChannel.ts +438 -0
- package/src/effect/WebChannel/broadcastChannelWithAck.ts +86 -83
- package/src/effect/WebChannel/common.ts +49 -2
- package/src/effect/WebChannel/mod.ts +3 -0
- package/src/effect/WebSocket.test.ts +15 -0
- package/src/effect/WebSocket.ts +75 -36
- package/src/effect/index.ts +18 -2
- package/src/env.ts +15 -0
- package/src/index.ts +6 -2
- package/src/node/ChildProcessRunner/ChildProcessRunner.ts +40 -29
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.ts +52 -0
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/schema.ts +65 -0
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.ts +53 -0
- package/src/node/ChildProcessRunner/ChildProcessWorker.ts +3 -6
- package/src/node/mod.ts +32 -94
- package/dist/effect/WebChannel.d.ts.map +0 -1
- package/dist/effect/WebChannel.js +0 -162
- package/dist/effect/WebChannel.js.map +0 -1
- package/dist/nanoid/index.browser.d.ts +0 -2
- package/dist/nanoid/index.browser.d.ts.map +0 -1
- package/dist/nanoid/index.browser.js +0 -3
- package/dist/nanoid/index.browser.js.map +0 -1
- package/src/effect/WebChannel.ts +0 -290
- package/src/nanoid/index.browser.ts +0 -2
- package/tmp/effect-deferred-repro.ts +0 -29
- package/tmp/effect-semaphore-repro.ts +0 -93
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FetchHttpClient } from '@effect/platform'
|
|
2
|
+
import * as Vitest from '@effect/vitest'
|
|
3
|
+
import { Effect, Exit } from 'effect'
|
|
4
|
+
|
|
5
|
+
import { makeWebSocket } from './WebSocket.js'
|
|
6
|
+
|
|
7
|
+
Vitest.describe('WebSocket', () => {
|
|
8
|
+
Vitest.scopedLive(
|
|
9
|
+
'should create a WebSocket connection',
|
|
10
|
+
Effect.fn(function* () {
|
|
11
|
+
const exit = yield* makeWebSocket({ url: 'ws://localhost:1000' }).pipe(Effect.timeout(500), Effect.exit)
|
|
12
|
+
Vitest.expect(Exit.isFailure(exit)).toBe(true)
|
|
13
|
+
}, Effect.provide(FetchHttpClient.layer)),
|
|
14
|
+
)
|
|
15
|
+
})
|
package/src/effect/WebSocket.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HttpClient } from '@effect/platform'
|
|
1
2
|
import type { Schedule, Scope } from 'effect'
|
|
2
3
|
import { Effect, Exit, identity, Schema } from 'effect'
|
|
3
4
|
|
|
@@ -20,53 +21,91 @@ export const makeWebSocket = ({
|
|
|
20
21
|
}: {
|
|
21
22
|
url: string
|
|
22
23
|
reconnect?: Schedule.Schedule<unknown> | false
|
|
23
|
-
}): Effect.Effect<globalThis.WebSocket, WebSocketError, Scope.Scope> =>
|
|
24
|
+
}): Effect.Effect<globalThis.WebSocket, WebSocketError, Scope.Scope | HttpClient.HttpClient> =>
|
|
24
25
|
Effect.gen(function* () {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
yield* validateUrl(url)
|
|
27
|
+
|
|
28
|
+
const socket = yield* Effect.async<globalThis.WebSocket, WebSocketError>((cb, signal) => {
|
|
29
|
+
try {
|
|
28
30
|
const socket = new globalThis.WebSocket(url)
|
|
29
31
|
|
|
30
32
|
if (socket.readyState === globalThis.WebSocket.OPEN) {
|
|
31
|
-
|
|
33
|
+
cb(Effect.succeed(socket))
|
|
34
|
+
return
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
socket.
|
|
36
|
-
// eslint-disable-next-line unicorn/prefer-add-event-listener
|
|
37
|
-
socket.onerror = (event) => reject(event)
|
|
37
|
+
signal.addEventListener('abort', () => {
|
|
38
|
+
socket.close(3000, 'abort signal')
|
|
38
39
|
})
|
|
39
|
-
},
|
|
40
|
-
catch: (errorEvent: any) => {
|
|
41
|
-
if (errorEvent.currentTarget != null && errorEvent.currentTarget instanceof globalThis.WebSocket) {
|
|
42
|
-
errorEvent.currentTarget.close(3000, `closing websocket connection due to error: ${errorEvent.toString()}`)
|
|
43
|
-
}
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
socket.addEventListener('open', () => cb(Effect.succeed(socket)), { once: true })
|
|
42
|
+
|
|
43
|
+
socket.addEventListener(
|
|
44
|
+
'error',
|
|
45
|
+
(event) => {
|
|
46
|
+
cb(Effect.fail(new WebSocketError({ cause: event })))
|
|
47
|
+
},
|
|
48
|
+
{ once: true },
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
socket.addEventListener(
|
|
52
|
+
'close',
|
|
53
|
+
(event) => {
|
|
54
|
+
// console.log('makeWebSocket:socket:onclose', event)
|
|
55
|
+
return cb(Effect.fail(new WebSocketError({ cause: event })))
|
|
56
|
+
},
|
|
57
|
+
{ once: true },
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
// console.log('makeWebSocket:socket:waiting for open', url)
|
|
61
|
+
} catch (error) {
|
|
62
|
+
cb(Effect.fail(new WebSocketError({ cause: error })))
|
|
63
|
+
}
|
|
47
64
|
}).pipe(
|
|
48
|
-
|
|
49
|
-
* Common WebSocket close codes: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close
|
|
50
|
-
* 1000: Normal closure
|
|
51
|
-
* 1001: Endpoint is going away, a server is terminating the connection because it has received a request that indicates the client is ending the connection.
|
|
52
|
-
* 1002: Protocol error, a server is terminating the connection because it has received data on the connection that was not consistent with the type of the connection.
|
|
53
|
-
* 1011: Internal server error, a server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.
|
|
54
|
-
*
|
|
55
|
-
* For reference, here are the valid WebSocket close code ranges:
|
|
56
|
-
* 1000-1999: Reserved for protocol usage
|
|
57
|
-
* 2000-2999: Reserved for WebSocket extensions
|
|
58
|
-
* 3000-3999: Available for libraries and frameworks
|
|
59
|
-
* 4000-4999: Available for applications
|
|
60
|
-
*/
|
|
61
|
-
Effect.acquireRelease((socket, exit) =>
|
|
62
|
-
Effect.sync(() =>
|
|
63
|
-
Exit.isFailure(exit)
|
|
64
|
-
? socket.close(3000, `closing webmesh websocket connection due to error: ${exit.cause.toString()}`)
|
|
65
|
-
: socket.close(1000, 'closing webmesh websocket connection gracefully'),
|
|
66
|
-
),
|
|
67
|
-
),
|
|
65
|
+
Effect.tapErrorTag('WebSocketError', () => tryLogWebsocketConnectError(url)),
|
|
68
66
|
reconnect ? Effect.retry(reconnect) : identity,
|
|
69
67
|
)
|
|
70
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Common WebSocket close codes: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close
|
|
71
|
+
* 1000: Normal closure
|
|
72
|
+
* 1001: Endpoint is going away, a server is terminating the connection because it has received a request that indicates the client is ending the connection.
|
|
73
|
+
* 1002: Protocol error, a server is terminating the connection because it has received data on the connection that was not consistent with the type of the connection.
|
|
74
|
+
* 1011: Internal server error, a server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.
|
|
75
|
+
*
|
|
76
|
+
* For reference, here are the valid WebSocket close code ranges:
|
|
77
|
+
* 1000-1999: Reserved for protocol usage
|
|
78
|
+
* 2000-2999: Reserved for WebSocket extensions
|
|
79
|
+
* 3000-3999: Available for libraries and frameworks
|
|
80
|
+
* 4000-4999: Available for applications
|
|
81
|
+
*/
|
|
82
|
+
yield* Effect.addFinalizer(
|
|
83
|
+
Effect.fn(function* (exit) {
|
|
84
|
+
try {
|
|
85
|
+
if (Exit.isFailure(exit)) {
|
|
86
|
+
socket.close(3000)
|
|
87
|
+
} else {
|
|
88
|
+
socket.close(1000)
|
|
89
|
+
}
|
|
90
|
+
} catch (error) {
|
|
91
|
+
yield* Effect.die(new WebSocketError({ cause: error }))
|
|
92
|
+
}
|
|
93
|
+
}),
|
|
94
|
+
)
|
|
95
|
+
|
|
71
96
|
return socket
|
|
72
97
|
})
|
|
98
|
+
|
|
99
|
+
const validateUrl = (url: string) =>
|
|
100
|
+
Effect.try({
|
|
101
|
+
try: () => new URL(url),
|
|
102
|
+
catch: (error) => new WebSocketError({ cause: error }),
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const tryLogWebsocketConnectError = (url: string) =>
|
|
106
|
+
Effect.gen(function* () {
|
|
107
|
+
const client = yield* HttpClient.HttpClient
|
|
108
|
+
const res = yield* client.get(url)
|
|
109
|
+
const responseBody = yield* res.text
|
|
110
|
+
yield* Effect.logError(`Failed to connect to '${url}' (status: ${res.status}). Error:`, responseBody)
|
|
111
|
+
}).pipe(Effect.ignore)
|
package/src/effect/index.ts
CHANGED
|
@@ -37,6 +37,7 @@ export {
|
|
|
37
37
|
SortedMap,
|
|
38
38
|
HashMap,
|
|
39
39
|
HashSet,
|
|
40
|
+
ManagedRuntime,
|
|
40
41
|
MutableHashSet,
|
|
41
42
|
MutableHashMap,
|
|
42
43
|
TQueue,
|
|
@@ -49,13 +50,21 @@ export {
|
|
|
49
50
|
TRef,
|
|
50
51
|
Channel,
|
|
51
52
|
Predicate,
|
|
53
|
+
// Subscribable,
|
|
52
54
|
pipe,
|
|
53
55
|
identity,
|
|
54
56
|
GlobalValue,
|
|
55
57
|
Match,
|
|
56
58
|
TestServices,
|
|
59
|
+
Mailbox,
|
|
60
|
+
ExecutionStrategy,
|
|
61
|
+
PrimaryKey,
|
|
62
|
+
Types,
|
|
63
|
+
Cache,
|
|
57
64
|
} from 'effect'
|
|
58
65
|
|
|
66
|
+
export * as StandardSchema from '@standard-schema/spec'
|
|
67
|
+
|
|
59
68
|
export { dual } from 'effect/Function'
|
|
60
69
|
|
|
61
70
|
export * as Stream from './Stream.js'
|
|
@@ -63,10 +72,11 @@ export * as Stream from './Stream.js'
|
|
|
63
72
|
export * as BucketQueue from './BucketQueue.js'
|
|
64
73
|
|
|
65
74
|
export * as SubscriptionRef from './SubscriptionRef.js'
|
|
75
|
+
export * as Subscribable from './Subscribable.js'
|
|
66
76
|
|
|
67
77
|
export * as Logger from './Logger.js'
|
|
68
78
|
|
|
69
|
-
export * as WebChannel from './WebChannel.js'
|
|
79
|
+
export * as WebChannel from './WebChannel/mod.js'
|
|
70
80
|
export * as WebSocket from './WebSocket.js'
|
|
71
81
|
|
|
72
82
|
export * as SchemaAST from 'effect/SchemaAST'
|
|
@@ -75,7 +85,6 @@ export { ParseResult, Pretty } from 'effect'
|
|
|
75
85
|
export type { Serializable, SerializableWithResult } from 'effect/Schema'
|
|
76
86
|
export * as Schema from './Schema/index.js'
|
|
77
87
|
export * as OtelTracer from '@effect/opentelemetry/Tracer'
|
|
78
|
-
// export * as OtelResource from '@effect/opentelemetry/Resource'
|
|
79
88
|
export * as TaskTracing from './TaskTracing.js'
|
|
80
89
|
|
|
81
90
|
export {
|
|
@@ -92,6 +101,13 @@ export {
|
|
|
92
101
|
HttpClientResponse,
|
|
93
102
|
FetchHttpClient,
|
|
94
103
|
Socket,
|
|
104
|
+
UrlParams,
|
|
105
|
+
HttpServerRequest,
|
|
106
|
+
Headers,
|
|
107
|
+
HttpMiddleware,
|
|
108
|
+
HttpRouter,
|
|
109
|
+
HttpServerResponse,
|
|
110
|
+
Command,
|
|
95
111
|
} from '@effect/platform'
|
|
96
112
|
export { BrowserWorker, BrowserWorkerRunner } from '@effect/platform-browser'
|
|
97
113
|
|
package/src/env.ts
CHANGED
|
@@ -23,9 +23,24 @@ export const isDevEnv = () => {
|
|
|
23
23
|
return import.meta.env.DEV
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
// @ts-expect-error Only exists in Expo / RN
|
|
27
|
+
if (typeof globalThis !== 'undefined' && globalThis.__DEV__) {
|
|
28
|
+
return true
|
|
29
|
+
}
|
|
30
|
+
|
|
26
31
|
return false
|
|
27
32
|
}
|
|
28
33
|
|
|
34
|
+
// export const TRACE_VERBOSE = true
|
|
29
35
|
export const TRACE_VERBOSE = env('LS_TRACE_VERBOSE') !== undefined || env('VITE_LS_TRACE_VERBOSE') !== undefined
|
|
30
36
|
|
|
37
|
+
/** Only set when developing LiveStore itself. */
|
|
31
38
|
export const LS_DEV = env('LS_DEV') !== undefined || env('VITE_LS_DEV') !== undefined
|
|
39
|
+
|
|
40
|
+
const envTruish = (env: string | undefined) => env !== undefined && env !== 'false' && env !== '0'
|
|
41
|
+
|
|
42
|
+
export const IS_CI = envTruish(env('CI'))
|
|
43
|
+
|
|
44
|
+
export const IS_BUN = typeof Bun !== 'undefined'
|
|
45
|
+
|
|
46
|
+
export const IS_REACT_NATIVE = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
|
package/src/index.ts
CHANGED
|
@@ -14,12 +14,12 @@ export * as base64 from './base64.js'
|
|
|
14
14
|
export { default as prettyBytes } from 'pretty-bytes'
|
|
15
15
|
|
|
16
16
|
import type * as otel from '@opentelemetry/api'
|
|
17
|
+
import type { Types } from 'effect'
|
|
17
18
|
|
|
18
19
|
import { isDevEnv } from './env.js'
|
|
19
20
|
import { objectToString } from './misc.js'
|
|
20
21
|
|
|
21
22
|
export type Prettify<T> = T extends infer U ? { [K in keyof U]: Prettify<U[K]> } : never
|
|
22
|
-
export type PrettifyFlat<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
|
|
23
23
|
|
|
24
24
|
export type TypeEq<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false
|
|
25
25
|
|
|
@@ -30,12 +30,16 @@ export type AssertTrue<T extends true> = T
|
|
|
30
30
|
export type Writeable<T> = { -readonly [P in keyof T]: T[P] }
|
|
31
31
|
export type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> }
|
|
32
32
|
|
|
33
|
+
export type Nullable<T> = { [K in keyof T]: T[K] | null }
|
|
34
|
+
|
|
33
35
|
export type Primitive = null | undefined | string | number | boolean | symbol | bigint
|
|
34
36
|
|
|
35
37
|
export type LiteralUnion<LiteralType, BaseType extends Primitive> = LiteralType | (BaseType & Record<never, never>)
|
|
36
38
|
|
|
37
39
|
export type GetValForKey<T, K> = K extends keyof T ? T[K] : never
|
|
38
40
|
|
|
41
|
+
export type SingleOrReadonlyArray<T> = T | ReadonlyArray<T>
|
|
42
|
+
|
|
39
43
|
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
40
44
|
|
|
41
45
|
export const ref = <T>(val: T): { current: T } => ({ current: val })
|
|
@@ -134,7 +138,7 @@ export const unwrapThunk = <T>(_: T | (() => T)): T => {
|
|
|
134
138
|
}
|
|
135
139
|
}
|
|
136
140
|
|
|
137
|
-
export type NullableFieldsToOptional<T> =
|
|
141
|
+
export type NullableFieldsToOptional<T> = Types.Simplify<
|
|
138
142
|
Partial<T> & {
|
|
139
143
|
[K in keyof T as null extends T[K] ? K : never]?: Exclude<T[K], null>
|
|
140
144
|
} & {
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
import process from 'node:process'
|
|
3
3
|
|
|
4
4
|
import { WorkerError } from '@effect/platform/WorkerError'
|
|
5
|
+
import type { CloseLatch } from '@effect/platform/WorkerRunner'
|
|
5
6
|
import * as Runner from '@effect/platform/WorkerRunner'
|
|
6
|
-
import
|
|
7
|
+
import * as Cause from 'effect/Cause'
|
|
7
8
|
import * as Context from 'effect/Context'
|
|
8
9
|
import * as Deferred from 'effect/Deferred'
|
|
9
10
|
import * as Effect from 'effect/Effect'
|
|
@@ -15,7 +16,7 @@ import * as Scope from 'effect/Scope'
|
|
|
15
16
|
|
|
16
17
|
const platformRunnerImpl = Runner.PlatformRunner.of({
|
|
17
18
|
[Runner.PlatformRunnerTypeId]: Runner.PlatformRunnerTypeId,
|
|
18
|
-
start<I, O>() {
|
|
19
|
+
start<I, O>(closeLatch: typeof CloseLatch.Service) {
|
|
19
20
|
return Effect.gen(function* () {
|
|
20
21
|
if (!process.send) {
|
|
21
22
|
return yield* new WorkerError({ reason: 'spawn', cause: new Error('not in a child process') })
|
|
@@ -27,37 +28,47 @@ const platformRunnerImpl = Runner.PlatformRunner.of({
|
|
|
27
28
|
}
|
|
28
29
|
const send = (_portId: number, message: O, _transfers?: ReadonlyArray<unknown>) =>
|
|
29
30
|
Effect.sync(() => port.postMessage([1, message] /*, transfers as any*/))
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
31
|
+
|
|
32
|
+
const run = Effect.fnUntraced(function* <A, E, R>(
|
|
33
|
+
handler: (portId: number, message: I) => Effect.Effect<A, E, R> | void,
|
|
34
|
+
) {
|
|
35
|
+
const runtime = (yield* Effect.interruptible(Effect.runtime<R | Scope.Scope>())).pipe(
|
|
36
|
+
Runtime.updateContext(Context.omit(Scope.Scope)),
|
|
37
|
+
) as Runtime.Runtime<R>
|
|
38
|
+
const fiberSet = yield* FiberSet.make<any, WorkerError | E>()
|
|
39
|
+
const runFork = Runtime.runFork(runtime)
|
|
40
|
+
const onExit = (exit: Exit.Exit<any, E>) => {
|
|
41
|
+
if (exit._tag === 'Failure' && !Cause.isInterruptedOnly(exit.cause)) {
|
|
42
|
+
// Deferred.unsafeDone(closeLatch, Exit.die(Cause.squash(exit.cause)))
|
|
43
|
+
Deferred.unsafeDone(closeLatch, Exit.die(exit.cause))
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
port.on('message', (message: Runner.BackingRunner.Message<I>) => {
|
|
47
|
+
// console.log('message', message)
|
|
48
|
+
if (message[0] === 0) {
|
|
49
|
+
const result = handler(0, message[1])
|
|
50
|
+
if (Effect.isEffect(result)) {
|
|
51
|
+
const fiber = runFork(result)
|
|
52
|
+
fiber.addObserver(onExit)
|
|
53
|
+
FiberSet.unsafeAdd(fiberSet, fiber)
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
Deferred.unsafeDone(closeLatch, Exit.void)
|
|
57
|
+
port.close()
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
port.on('messageerror', (cause) => {
|
|
61
|
+
Deferred.unsafeDone(closeLatch, new WorkerError({ reason: 'decode', cause }))
|
|
62
|
+
})
|
|
63
|
+
port.on('error', (cause) => {
|
|
64
|
+
Deferred.unsafeDone(closeLatch, new WorkerError({ reason: 'unknown', cause }))
|
|
65
|
+
})
|
|
66
|
+
port.postMessage([0])
|
|
67
|
+
})
|
|
56
68
|
|
|
57
69
|
return { run, send }
|
|
58
70
|
})
|
|
59
71
|
},
|
|
60
72
|
})
|
|
61
73
|
|
|
62
|
-
/** @internal */
|
|
63
74
|
export const layer = Layer.succeed(Runner.PlatformRunner, platformRunnerImpl)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// import * as WorkerThreads from 'node:worker_threads'
|
|
2
|
+
import * as ChildProcess from 'node:child_process'
|
|
3
|
+
|
|
4
|
+
import * as EffectWorker from '@effect/platform/Worker'
|
|
5
|
+
import { assert, describe, it } from '@effect/vitest'
|
|
6
|
+
import { Chunk, Effect, Stream } from 'effect'
|
|
7
|
+
|
|
8
|
+
import * as ChildProcessWorker from '../ChildProcessWorker.js'
|
|
9
|
+
import type { WorkerMessage } from './schema.js'
|
|
10
|
+
import { GetPersonById, GetUserById, InitialMessage, Person, User } from './schema.js'
|
|
11
|
+
|
|
12
|
+
const WorkerLive = ChildProcessWorker.layer(() =>
|
|
13
|
+
ChildProcess.fork(
|
|
14
|
+
new URL('../../../../dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js', import.meta.url),
|
|
15
|
+
),
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
// const WorkerLive = NodeWorker.layer(
|
|
19
|
+
// () =>
|
|
20
|
+
// new WorkerThreads.Worker(
|
|
21
|
+
// new URL('../../../../dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js', import.meta.url),
|
|
22
|
+
// ),
|
|
23
|
+
// )
|
|
24
|
+
|
|
25
|
+
describe('ChildProcessRunner', { timeout: 10_000 }, () => {
|
|
26
|
+
it('Serialized', () =>
|
|
27
|
+
Effect.gen(function* () {
|
|
28
|
+
const pool = yield* EffectWorker.makePoolSerialized({ size: 1 })
|
|
29
|
+
const people = yield* pool.execute(new GetPersonById({ id: 123 })).pipe(Stream.runCollect)
|
|
30
|
+
assert.deepStrictEqual(Chunk.toReadonlyArray(people), [
|
|
31
|
+
new Person({ id: 123, name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
32
|
+
new Person({ id: 123, name: 'ing', data: new Uint8Array([4, 5, 6]) }),
|
|
33
|
+
])
|
|
34
|
+
}).pipe(Effect.scoped, Effect.provide(WorkerLive), Effect.runPromise))
|
|
35
|
+
|
|
36
|
+
it('Serialized with initialMessage', () =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const pool = yield* EffectWorker.makePoolSerialized<WorkerMessage>({
|
|
39
|
+
size: 1,
|
|
40
|
+
initialMessage: () => new InitialMessage({ name: 'custom', data: new Uint8Array([1, 2, 3]) }),
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
let user = yield* pool.executeEffect(new GetUserById({ id: 123 }))
|
|
44
|
+
user = yield* pool.executeEffect(new GetUserById({ id: 123 }))
|
|
45
|
+
assert.deepStrictEqual(user, new User({ id: 123, name: 'custom' }))
|
|
46
|
+
const people = yield* pool.execute(new GetPersonById({ id: 123 })).pipe(Stream.runCollect)
|
|
47
|
+
assert.deepStrictEqual(Chunk.toReadonlyArray(people), [
|
|
48
|
+
new Person({ id: 123, name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
49
|
+
new Person({ id: 123, name: 'ing', data: new Uint8Array([4, 5, 6]) }),
|
|
50
|
+
])
|
|
51
|
+
}).pipe(Effect.scoped, Effect.provide(WorkerLive), Effect.runPromise))
|
|
52
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// import * as Transferable from '@effect/platform/Transferable'
|
|
2
|
+
import * as Schema from 'effect/Schema'
|
|
3
|
+
|
|
4
|
+
export class User extends Schema.Class<User>('User')({
|
|
5
|
+
id: Schema.Number,
|
|
6
|
+
name: Schema.String,
|
|
7
|
+
}) {}
|
|
8
|
+
|
|
9
|
+
export class GetUserById extends Schema.TaggedRequest<GetUserById>()('GetUserById', {
|
|
10
|
+
failure: Schema.Never,
|
|
11
|
+
success: User,
|
|
12
|
+
payload: {
|
|
13
|
+
id: Schema.Number,
|
|
14
|
+
},
|
|
15
|
+
}) {}
|
|
16
|
+
|
|
17
|
+
export class Person extends Schema.Class<Person>('Person')({
|
|
18
|
+
id: Schema.Number,
|
|
19
|
+
name: Schema.String,
|
|
20
|
+
// data: Transferable.Uint8Array,
|
|
21
|
+
data: Schema.Uint8Array,
|
|
22
|
+
}) {}
|
|
23
|
+
|
|
24
|
+
export class GetPersonById extends Schema.TaggedRequest<GetPersonById>()('GetPersonById', {
|
|
25
|
+
failure: Schema.Never,
|
|
26
|
+
success: Person,
|
|
27
|
+
payload: {
|
|
28
|
+
id: Schema.Number,
|
|
29
|
+
},
|
|
30
|
+
}) {}
|
|
31
|
+
|
|
32
|
+
export class RunnerInterrupt extends Schema.TaggedRequest<RunnerInterrupt>()('RunnerInterrupt', {
|
|
33
|
+
failure: Schema.Never,
|
|
34
|
+
success: Schema.Void,
|
|
35
|
+
payload: {},
|
|
36
|
+
}) {}
|
|
37
|
+
|
|
38
|
+
export class InitialMessage extends Schema.TaggedRequest<InitialMessage>()('InitialMessage', {
|
|
39
|
+
failure: Schema.Never,
|
|
40
|
+
success: Schema.Void,
|
|
41
|
+
payload: {
|
|
42
|
+
name: Schema.String,
|
|
43
|
+
data: Schema.Uint8Array,
|
|
44
|
+
// data: Transferable.Uint8Array,
|
|
45
|
+
},
|
|
46
|
+
}) {}
|
|
47
|
+
|
|
48
|
+
export class GetSpan extends Schema.TaggedRequest<GetSpan>()('GetSpan', {
|
|
49
|
+
failure: Schema.Never,
|
|
50
|
+
success: Schema.Struct({
|
|
51
|
+
name: Schema.String,
|
|
52
|
+
traceId: Schema.String,
|
|
53
|
+
spanId: Schema.String,
|
|
54
|
+
parent: Schema.Option(
|
|
55
|
+
Schema.Struct({
|
|
56
|
+
traceId: Schema.String,
|
|
57
|
+
spanId: Schema.String,
|
|
58
|
+
}),
|
|
59
|
+
),
|
|
60
|
+
}),
|
|
61
|
+
payload: {},
|
|
62
|
+
}) {}
|
|
63
|
+
|
|
64
|
+
export const WorkerMessage = Schema.Union(GetUserById, GetPersonById, InitialMessage, GetSpan, RunnerInterrupt)
|
|
65
|
+
export type WorkerMessage = Schema.Schema.Type<typeof WorkerMessage>
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as Runner from '@effect/platform/WorkerRunner'
|
|
2
|
+
import { Context, Effect, Layer, Option, Stream } from 'effect'
|
|
3
|
+
|
|
4
|
+
// import { NodeRuntime, NodeWorkerRunner } from '@effect/platform-node'
|
|
5
|
+
import { PlatformNode } from '../../mod.js'
|
|
6
|
+
import * as ChildProcessRunner from '../ChildProcessRunner.js'
|
|
7
|
+
import { Person, User, WorkerMessage } from './schema.js'
|
|
8
|
+
|
|
9
|
+
interface Name {
|
|
10
|
+
readonly _: unique symbol
|
|
11
|
+
}
|
|
12
|
+
const Name = Context.GenericTag<Name, string>('Name')
|
|
13
|
+
|
|
14
|
+
const WorkerLive = Runner.layerSerialized(WorkerMessage, {
|
|
15
|
+
GetPersonById: (req) => {
|
|
16
|
+
return Stream.make(
|
|
17
|
+
new Person({ id: req.id, name: 'test', data: new Uint8Array([1, 2, 3]) }),
|
|
18
|
+
new Person({ id: req.id, name: 'ing', data: new Uint8Array([4, 5, 6]) }),
|
|
19
|
+
)
|
|
20
|
+
},
|
|
21
|
+
GetUserById: (req) => Effect.map(Name, (name) => new User({ id: req.id, name })),
|
|
22
|
+
// InitialMessage: (req) => Layer.succeed(Name, req.name),
|
|
23
|
+
InitialMessage: (req) =>
|
|
24
|
+
Effect.gen(function* () {
|
|
25
|
+
yield* Effect.addFinalizer(() => Effect.log('closing worker scope'))
|
|
26
|
+
return Layer.succeed(Name, req.name)
|
|
27
|
+
}).pipe(Layer.unwrapScoped),
|
|
28
|
+
// InitialMessage: (req) =>
|
|
29
|
+
// Layer.scoped(
|
|
30
|
+
// Name,
|
|
31
|
+
// Effect.gen(function* () {
|
|
32
|
+
// yield* Effect.addFinalizer(() => Effect.log('closing worker scope'))
|
|
33
|
+
// return req.name
|
|
34
|
+
// }),
|
|
35
|
+
// ),
|
|
36
|
+
GetSpan: (_) =>
|
|
37
|
+
Effect.gen(function* (_) {
|
|
38
|
+
const span = yield* _(Effect.currentSpan, Effect.orDie)
|
|
39
|
+
return {
|
|
40
|
+
traceId: span.traceId,
|
|
41
|
+
spanId: span.spanId,
|
|
42
|
+
name: span.name,
|
|
43
|
+
parent: Option.map(span.parent, (span) => ({
|
|
44
|
+
traceId: span.traceId,
|
|
45
|
+
spanId: span.spanId,
|
|
46
|
+
})),
|
|
47
|
+
}
|
|
48
|
+
}).pipe(Effect.withSpan('GetSpan')),
|
|
49
|
+
RunnerInterrupt: () => Effect.interrupt,
|
|
50
|
+
}).pipe(Layer.provide(ChildProcessRunner.layer))
|
|
51
|
+
// }).pipe(Layer.provide(PlatformNode.NodeWorkerRunner.layer))
|
|
52
|
+
|
|
53
|
+
PlatformNode.NodeRuntime.runMain(Runner.launch(WorkerLive))
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/* eslint-disable prefer-arrow/prefer-arrow-functions */
|
|
2
2
|
|
|
3
|
-
import type * as ChildProcess from 'node:child_process'
|
|
4
|
-
|
|
5
3
|
import * as Worker from '@effect/platform/Worker'
|
|
6
4
|
import { WorkerError } from '@effect/platform/WorkerError'
|
|
5
|
+
// eslint-disable-next-line unicorn/prefer-node-protocol
|
|
6
|
+
import type * as ChildProcess from 'child_process'
|
|
7
7
|
import * as Deferred from 'effect/Deferred'
|
|
8
8
|
import * as Effect from 'effect/Effect'
|
|
9
9
|
import * as Exit from 'effect/Exit'
|
|
@@ -23,8 +23,8 @@ const platformWorkerImpl = Worker.makePlatform<ChildProcess.ChildProcess>()({
|
|
|
23
23
|
childProcess.send([1])
|
|
24
24
|
return Deferred.await(exitDeferred)
|
|
25
25
|
}).pipe(
|
|
26
|
-
Effect.interruptible,
|
|
27
26
|
Effect.timeout(5000),
|
|
27
|
+
Effect.interruptible,
|
|
28
28
|
Effect.catchAllCause(() => Effect.sync(() => childProcess.kill())),
|
|
29
29
|
),
|
|
30
30
|
),
|
|
@@ -55,12 +55,9 @@ const platformWorkerImpl = Worker.makePlatform<ChildProcess.ChildProcess>()({
|
|
|
55
55
|
},
|
|
56
56
|
})
|
|
57
57
|
|
|
58
|
-
/** @internal */
|
|
59
58
|
export const layerWorker = Layer.succeed(Worker.PlatformWorker, platformWorkerImpl)
|
|
60
59
|
|
|
61
|
-
/** @internal */
|
|
62
60
|
export const layerManager = Layer.provide(Worker.layerManager, layerWorker)
|
|
63
61
|
|
|
64
|
-
/** @internal */
|
|
65
62
|
export const layer = (spawn: (id: number) => ChildProcess.ChildProcess) =>
|
|
66
63
|
Layer.merge(layerManager, Worker.layerSpawner(spawn))
|