@livestore/utils 0.4.0-dev.2 → 0.4.0-dev.21
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/NoopTracer.d.ts.map +1 -1
- package/dist/NoopTracer.js +5 -1
- package/dist/NoopTracer.js.map +1 -1
- package/dist/browser/Opfs/Opfs.d.ts +51 -0
- package/dist/browser/Opfs/Opfs.d.ts.map +1 -0
- package/dist/browser/Opfs/Opfs.js +345 -0
- package/dist/browser/Opfs/Opfs.js.map +1 -0
- package/dist/browser/Opfs/debug-utils.d.ts +20 -0
- package/dist/browser/Opfs/debug-utils.d.ts.map +1 -0
- package/dist/browser/Opfs/debug-utils.js +94 -0
- package/dist/browser/Opfs/debug-utils.js.map +1 -0
- package/dist/browser/Opfs/mod.d.ts +4 -0
- package/dist/browser/Opfs/mod.d.ts.map +1 -0
- package/dist/browser/Opfs/mod.js +4 -0
- package/dist/browser/Opfs/mod.js.map +1 -0
- package/dist/browser/Opfs/utils.d.ts +71 -0
- package/dist/browser/Opfs/utils.d.ts.map +1 -0
- package/dist/browser/Opfs/utils.js +218 -0
- package/dist/browser/Opfs/utils.js.map +1 -0
- package/dist/browser/QuotaExceededError.d.ts +59 -0
- package/dist/browser/QuotaExceededError.d.ts.map +1 -0
- package/dist/browser/QuotaExceededError.js +2 -0
- package/dist/browser/QuotaExceededError.js.map +1 -0
- package/dist/browser/WebChannelBrowser.d.ts +22 -0
- package/dist/browser/WebChannelBrowser.d.ts.map +1 -0
- package/dist/browser/WebChannelBrowser.js +76 -0
- package/dist/browser/WebChannelBrowser.js.map +1 -0
- package/dist/browser/WebError.d.ts +425 -0
- package/dist/browser/WebError.d.ts.map +1 -0
- package/dist/browser/WebError.js +414 -0
- package/dist/browser/WebError.js.map +1 -0
- package/dist/browser/WebError.test.d.ts +2 -0
- package/dist/browser/WebError.test.d.ts.map +1 -0
- package/dist/browser/WebError.test.js +46 -0
- package/dist/browser/WebError.test.js.map +1 -0
- package/dist/browser/WebLock.d.ts.map +1 -0
- package/dist/browser/WebLock.js.map +1 -0
- package/dist/{browser.d.ts → browser/detect.d.ts} +1 -1
- package/dist/browser/detect.d.ts.map +1 -0
- package/dist/{browser.js → browser/detect.js} +1 -1
- package/dist/browser/detect.js.map +1 -0
- package/dist/browser/mod.d.ts +8 -0
- package/dist/browser/mod.d.ts.map +1 -0
- package/dist/browser/mod.js +8 -0
- package/dist/browser/mod.js.map +1 -0
- package/dist/effect/Debug.d.ts +36 -0
- package/dist/effect/Debug.d.ts.map +1 -0
- package/dist/effect/Debug.js +346 -0
- package/dist/effect/Debug.js.map +1 -0
- package/dist/effect/Effect.d.ts +9 -3
- package/dist/effect/Effect.d.ts.map +1 -1
- package/dist/effect/Effect.js +4 -2
- package/dist/effect/Effect.js.map +1 -1
- package/dist/effect/Error.d.ts +1 -1
- package/dist/effect/Error.js.map +1 -1
- package/dist/effect/Logger.d.ts +4 -1
- package/dist/effect/Logger.d.ts.map +1 -1
- package/dist/effect/Logger.js +12 -3
- package/dist/effect/Logger.js.map +1 -1
- package/dist/effect/OtelTracer.d.ts +5 -0
- package/dist/effect/OtelTracer.d.ts.map +1 -0
- package/dist/effect/OtelTracer.js +8 -0
- package/dist/effect/OtelTracer.js.map +1 -0
- package/dist/effect/RpcClient.d.ts +32 -0
- package/dist/effect/RpcClient.d.ts.map +1 -0
- package/dist/effect/RpcClient.js +149 -0
- package/dist/effect/RpcClient.js.map +1 -0
- package/dist/effect/Schema/debug-diff.test.js +1 -1
- package/dist/effect/Schema/debug-diff.test.js.map +1 -1
- package/dist/effect/Schema/index.d.ts +2 -2
- package/dist/effect/Schema/index.d.ts.map +1 -1
- package/dist/effect/Schema/index.js +12 -2
- package/dist/effect/Schema/index.js.map +1 -1
- package/dist/effect/Stream.d.ts +73 -2
- package/dist/effect/Stream.d.ts.map +1 -1
- package/dist/effect/Stream.js +68 -1
- package/dist/effect/Stream.js.map +1 -1
- package/dist/effect/Stream.test.d.ts +2 -0
- package/dist/effect/Stream.test.d.ts.map +1 -0
- package/dist/effect/Stream.test.js +84 -0
- package/dist/effect/Stream.test.js.map +1 -0
- package/dist/effect/SubscriptionRef.d.ts +2 -2
- package/dist/effect/SubscriptionRef.d.ts.map +1 -1
- package/dist/effect/SubscriptionRef.js +6 -1
- package/dist/effect/SubscriptionRef.js.map +1 -1
- package/dist/effect/WebChannel/WebChannel.d.ts +2 -21
- package/dist/effect/WebChannel/WebChannel.d.ts.map +1 -1
- package/dist/effect/WebChannel/WebChannel.js +5 -81
- package/dist/effect/WebChannel/WebChannel.js.map +1 -1
- package/dist/effect/WebChannel/WebChannel.test.js +1 -1
- package/dist/effect/WebChannel/WebChannel.test.js.map +1 -1
- package/dist/effect/WebChannel/common.d.ts +1 -1
- package/dist/effect/WebChannel/common.d.ts.map +1 -1
- package/dist/effect/WebSocket.d.ts.map +1 -1
- package/dist/effect/WebSocket.js +12 -12
- package/dist/effect/WebSocket.js.map +1 -1
- package/dist/effect/mod.d.ts +32 -0
- package/dist/effect/mod.d.ts.map +1 -0
- package/dist/effect/mod.js +35 -0
- package/dist/effect/mod.js.map +1 -0
- package/dist/global.d.ts +4 -0
- package/dist/global.d.ts.map +1 -1
- package/dist/global.js.map +1 -1
- package/dist/guards.d.ts +14 -0
- package/dist/guards.d.ts.map +1 -1
- package/dist/guards.js +14 -0
- package/dist/guards.js.map +1 -1
- package/dist/misc.js +1 -1
- package/dist/misc.js.map +1 -1
- package/dist/mod.d.ts +197 -3
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +153 -4
- package/dist/mod.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunner.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunner.js +66 -10
- package/dist/node/ChildProcessRunner/ChildProcessRunner.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.js +177 -3
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.d.ts +14 -5
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.js +7 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/schema.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js +13 -3
- package/dist/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.js.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessWorker.d.ts +16 -0
- package/dist/node/ChildProcessRunner/ChildProcessWorker.d.ts.map +1 -1
- package/dist/node/ChildProcessRunner/ChildProcessWorker.js +98 -2
- package/dist/node/ChildProcessRunner/ChildProcessWorker.js.map +1 -1
- package/dist/node/mod.d.ts +8 -2
- package/dist/node/mod.d.ts.map +1 -1
- package/dist/node/mod.js +11 -3
- package/dist/node/mod.js.map +1 -1
- package/dist/qr.d.ts +38 -0
- package/dist/qr.d.ts.map +1 -0
- package/dist/qr.js +109 -0
- package/dist/qr.js.map +1 -0
- package/package.json +54 -44
- package/src/NoopTracer.ts +5 -1
- package/src/browser/Opfs/Opfs.ts +428 -0
- package/src/browser/Opfs/debug-utils.ts +151 -0
- package/src/browser/Opfs/mod.ts +3 -0
- package/src/browser/Opfs/utils.ts +286 -0
- package/src/browser/QuotaExceededError.ts +59 -0
- package/src/browser/WebChannelBrowser.ts +131 -0
- package/src/browser/WebError.test.ts +66 -0
- package/src/browser/WebError.ts +599 -0
- package/src/browser/mod.ts +8 -0
- package/src/effect/Debug.ts +452 -0
- package/src/effect/Effect.ts +31 -4
- package/src/effect/Error.ts +1 -1
- package/src/effect/Logger.ts +14 -4
- package/src/effect/OtelTracer.ts +11 -0
- package/src/effect/RpcClient.ts +212 -0
- package/src/effect/Schema/debug-diff.test.ts +2 -2
- package/src/effect/Schema/index.ts +17 -3
- package/src/effect/Stream.test.ts +127 -0
- package/src/effect/Stream.ts +111 -2
- package/src/effect/SubscriptionRef.ts +14 -2
- package/src/effect/WebChannel/WebChannel.test.ts +1 -1
- package/src/effect/WebChannel/WebChannel.ts +13 -135
- package/src/effect/WebChannel/common.ts +1 -1
- package/src/effect/WebSocket.ts +11 -10
- package/src/effect/{index.ts → mod.ts} +45 -15
- package/src/global.ts +5 -0
- package/src/guards.ts +15 -0
- package/src/misc.ts +1 -1
- package/src/mod.ts +206 -5
- package/src/node/ChildProcessRunner/ChildProcessRunner.ts +71 -10
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/ChildProcessRunner.test.ts +258 -3
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/schema.ts +14 -1
- package/src/node/ChildProcessRunner/ChildProcessRunnerTest/serializedWorker.ts +16 -3
- package/src/node/ChildProcessRunner/ChildProcessWorker.ts +111 -3
- package/src/node/mod.ts +13 -6
- package/src/qr.ts +125 -0
- package/dist/browser.d.ts.map +0 -1
- package/dist/browser.js.map +0 -1
- package/dist/effect/Schema/msgpack.d.ts +0 -3
- package/dist/effect/Schema/msgpack.d.ts.map +0 -1
- package/dist/effect/Schema/msgpack.js +0 -7
- package/dist/effect/Schema/msgpack.js.map +0 -1
- package/dist/effect/WebLock.d.ts.map +0 -1
- package/dist/effect/WebLock.js.map +0 -1
- package/dist/effect/index.d.ts +0 -27
- package/dist/effect/index.d.ts.map +0 -1
- package/dist/effect/index.js +0 -31
- package/dist/effect/index.js.map +0 -1
- package/src/effect/Schema/msgpack.ts +0 -8
- /package/dist/{effect → browser}/WebLock.d.ts +0 -0
- /package/dist/{effect → browser}/WebLock.js +0 -0
- /package/src/{effect → browser}/WebLock.ts +0 -0
- /package/src/{browser.ts → browser/detect.ts} +0 -0
package/src/mod.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export { default as prettyBytes } from 'pretty-bytes'
|
|
2
2
|
export * as base64 from './base64.ts'
|
|
3
3
|
export * from './binary.ts'
|
|
4
|
-
export * from './browser.ts'
|
|
5
4
|
export * from './Deferred.ts'
|
|
6
5
|
export * from './env.ts'
|
|
7
6
|
export * from './fast-deep-equal.ts'
|
|
@@ -10,6 +9,7 @@ export * from './misc.ts'
|
|
|
10
9
|
export * from './NoopTracer.ts'
|
|
11
10
|
export * from './object/index.ts'
|
|
12
11
|
export * from './promise.ts'
|
|
12
|
+
export * as QR from './qr.ts'
|
|
13
13
|
export * from './set.ts'
|
|
14
14
|
export * from './string.ts'
|
|
15
15
|
export * from './time.ts'
|
|
@@ -19,37 +19,102 @@ import type { Types } from 'effect'
|
|
|
19
19
|
|
|
20
20
|
import { objectToString } from './misc.ts'
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Recursively expands type aliases for better IDE hover display.
|
|
24
|
+
*
|
|
25
|
+
* Transforms `{ a: string } & { b: number }` into `{ a: string; b: number }`.
|
|
26
|
+
*/
|
|
22
27
|
export type Prettify<T> = T extends infer U ? { [K in keyof U]: Prettify<U[K]> } : never
|
|
23
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Type-level equality check. Returns `true` if `A` and `B` are exactly the same type.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* type Test1 = TypeEq<string, string> // true
|
|
35
|
+
* type Test2 = TypeEq<string, number> // false
|
|
36
|
+
* type Test3 = TypeEq<{ a: 1 }, { a: 1 }> // true
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
24
39
|
export type TypeEq<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false
|
|
25
40
|
|
|
26
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* Type-level subtype check. Returns `true` if `A` extends `B`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* type Test1 = IsSubtype<'foo', string> // true
|
|
47
|
+
* type Test2 = IsSubtype<string, 'foo'> // false
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
27
50
|
export type IsSubtype<A, B> = A extends B ? true : false
|
|
51
|
+
|
|
52
|
+
/** Compile-time assertion that `T` is `true`. Useful for type tests. */
|
|
28
53
|
export type AssertTrue<T extends true> = T
|
|
29
54
|
|
|
55
|
+
/** Removes `readonly` modifier from all properties of `T`. */
|
|
30
56
|
export type Writeable<T> = { -readonly [P in keyof T]: T[P] }
|
|
57
|
+
|
|
58
|
+
/** Recursively removes `readonly` modifier from all properties. */
|
|
31
59
|
export type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> }
|
|
32
60
|
|
|
61
|
+
/** Makes all properties of `T` nullable (allows `null`). */
|
|
33
62
|
export type Nullable<T> = { [K in keyof T]: T[K] | null }
|
|
34
63
|
|
|
64
|
+
/** Union of JavaScript primitive types. */
|
|
35
65
|
export type Primitive = null | undefined | string | number | boolean | symbol | bigint
|
|
36
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Creates a union type that allows specific literals while still accepting the base type.
|
|
69
|
+
* Useful for string/number enums with autocomplete support.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* type Status = LiteralUnion<'pending' | 'active', string>
|
|
74
|
+
* // Allows 'pending', 'active', or any other string
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
37
77
|
export type LiteralUnion<LiteralType, BaseType extends Primitive> = LiteralType | (BaseType & Record<never, never>)
|
|
38
78
|
|
|
79
|
+
/** Extracts the value type for key `K` from object type `T`, or `never` if key doesn't exist. */
|
|
39
80
|
export type GetValForKey<T, K> = K extends keyof T ? T[K] : never
|
|
40
81
|
|
|
82
|
+
/** Accepts either a single value or a readonly array of values. */
|
|
41
83
|
export type SingleOrReadonlyArray<T> = T | ReadonlyArray<T>
|
|
42
84
|
|
|
85
|
+
/** Returns a Promise that resolves after `ms` milliseconds. */
|
|
43
86
|
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
44
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Creates a mutable reference object with a `current` property.
|
|
90
|
+
* Similar to React's `useRef` but works outside of React.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const counter = ref(0)
|
|
95
|
+
* counter.current += 1
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
45
98
|
export const ref = <T>(val: T): { current: T } => ({ current: val })
|
|
46
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Calls a function `n` times with the current index.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* times(3, (i) => console.log(i)) // logs 0, 1, 2
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
47
108
|
export const times = (n: number, fn: (index: number) => {}): void => {
|
|
48
109
|
for (let i = 0; i < n; i++) {
|
|
49
110
|
fn(i)
|
|
50
111
|
}
|
|
51
112
|
}
|
|
52
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Wraps a function call in a try/catch that triggers the debugger on error.
|
|
116
|
+
* Useful for debugging exceptions during development.
|
|
117
|
+
*/
|
|
53
118
|
export const debugCatch = <T>(try_: () => T): T => {
|
|
54
119
|
try {
|
|
55
120
|
return try_()
|
|
@@ -60,6 +125,10 @@ export const debugCatch = <T>(try_: () => T): T => {
|
|
|
60
125
|
}
|
|
61
126
|
}
|
|
62
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Recursively removes `undefined` values from an object or array in place.
|
|
130
|
+
* Mutates the input value.
|
|
131
|
+
*/
|
|
63
132
|
export const recRemoveUndefinedValues = (val: any): void => {
|
|
64
133
|
if (Array.isArray(val)) {
|
|
65
134
|
val.forEach(recRemoveUndefinedValues)
|
|
@@ -79,13 +148,24 @@ export const recRemoveUndefinedValues = (val: any): void => {
|
|
|
79
148
|
*/
|
|
80
149
|
export const sluggify = (str: string, separator = '-') => str.replace(/[^a-zA-Z0-9]/g, separator)
|
|
81
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Creates a property accessor function for use in pipelines.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* const users = [{ name: 'Alice' }, { name: 'Bob' }]
|
|
157
|
+
* const names = users.map(prop('name')) // ['Alice', 'Bob']
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
82
160
|
export const prop =
|
|
83
161
|
<T extends {}, K extends keyof T>(key: K) =>
|
|
84
162
|
(obj: T): T[K] =>
|
|
85
163
|
obj[key]
|
|
86
164
|
|
|
165
|
+
/** Capitalizes the first letter of a string. */
|
|
87
166
|
export const capitalizeFirstLetter = (str: string): string => str.charAt(0).toUpperCase() + str.slice(1)
|
|
88
167
|
|
|
168
|
+
/** Type guard that checks if a value is a readonly array. */
|
|
89
169
|
export const isReadonlyArray = <I, T>(value: ReadonlyArray<I> | T): value is ReadonlyArray<I> => Array.isArray(value)
|
|
90
170
|
|
|
91
171
|
/**
|
|
@@ -99,6 +179,14 @@ export function casesHandled(unexpectedCase: never): never {
|
|
|
99
179
|
throw new Error(`A case was not handled for value: ${truncate(objectToString(unexpectedCase), 1000)}`)
|
|
100
180
|
}
|
|
101
181
|
|
|
182
|
+
/**
|
|
183
|
+
* Throws if the condition is false. Use for runtime assertions that should never fail.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* assertNever(user !== undefined, 'User must be loaded')
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
102
190
|
export const assertNever = (failIfFalse: boolean, msg?: string): void => {
|
|
103
191
|
if (failIfFalse === false) {
|
|
104
192
|
// biome-ignore lint/suspicious/noDebugger: debugging
|
|
@@ -107,6 +195,14 @@ export const assertNever = (failIfFalse: boolean, msg?: string): void => {
|
|
|
107
195
|
}
|
|
108
196
|
}
|
|
109
197
|
|
|
198
|
+
/**
|
|
199
|
+
* Identity function that triggers the debugger. Useful for debugging pipelines.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* data.pipe(transform, debuggerPipe, format) // Pauses debugger here
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
110
206
|
export const debuggerPipe = <T>(val: T): T => {
|
|
111
207
|
// biome-ignore lint/suspicious/noDebugger: debugging
|
|
112
208
|
debugger
|
|
@@ -121,16 +217,40 @@ const truncate = (str: string, length: number): string => {
|
|
|
121
217
|
}
|
|
122
218
|
}
|
|
123
219
|
|
|
220
|
+
/**
|
|
221
|
+
* Placeholder for unimplemented code paths. Triggers debugger and throws.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* const parseFormat = (format: Format) => {
|
|
226
|
+
* switch (format) {
|
|
227
|
+
* case 'json': return parseJson
|
|
228
|
+
* case 'xml': return notYetImplemented('XML parsing')
|
|
229
|
+
* }
|
|
230
|
+
* }
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
124
233
|
export const notYetImplemented = (msg?: string): never => {
|
|
125
234
|
// biome-ignore lint/suspicious/noDebugger: debugging
|
|
126
235
|
debugger
|
|
127
236
|
throw new Error(`Not yet implemented: ${msg}`)
|
|
128
237
|
}
|
|
129
238
|
|
|
239
|
+
/** A function that does nothing. Useful as a default callback. */
|
|
130
240
|
export const noop = () => {}
|
|
131
241
|
|
|
242
|
+
/** A function that returns a value of type `T`. */
|
|
132
243
|
export type Thunk<T> = () => T
|
|
133
244
|
|
|
245
|
+
/**
|
|
246
|
+
* If the input is a function, calls it and returns the result. Otherwise returns the value directly.
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* unwrapThunk(5) // 5
|
|
251
|
+
* unwrapThunk(() => 5) // 5
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
134
254
|
export const unwrapThunk = <T>(_: T | (() => T)): T => {
|
|
135
255
|
if (typeof _ === 'function') {
|
|
136
256
|
return (_ as any)()
|
|
@@ -139,6 +259,10 @@ export const unwrapThunk = <T>(_: T | (() => T)): T => {
|
|
|
139
259
|
}
|
|
140
260
|
}
|
|
141
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Transforms nullable fields (those that include `null`) into optional fields.
|
|
264
|
+
* Useful for converting database schemas to TypeScript types.
|
|
265
|
+
*/
|
|
142
266
|
export type NullableFieldsToOptional<T> = Types.Simplify<
|
|
143
267
|
Partial<T> & {
|
|
144
268
|
[K in keyof T as null extends T[K] ? K : never]?: Exclude<T[K], null>
|
|
@@ -147,12 +271,33 @@ export type NullableFieldsToOptional<T> = Types.Simplify<
|
|
|
147
271
|
}
|
|
148
272
|
>
|
|
149
273
|
|
|
150
|
-
/**
|
|
274
|
+
/**
|
|
275
|
+
* Creates an array of numbers from `start` (inclusive) to `end` (exclusive).
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* range(0, 5) // [0, 1, 2, 3, 4]
|
|
280
|
+
* range(3, 7) // [3, 4, 5, 6]
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
151
283
|
export const range = (start: number, end: number): number[] => {
|
|
152
284
|
const length = end - start
|
|
153
285
|
return Array.from({ length }, (_, i) => start + i)
|
|
154
286
|
}
|
|
155
287
|
|
|
288
|
+
/**
|
|
289
|
+
* Rate-limits function calls to at most once per `ms` milliseconds.
|
|
290
|
+
* Trailing calls are preserved—if called during the wait period, the function
|
|
291
|
+
* will be called again after the timeout.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* const throttledSave = throttle(() => saveData(), 1000)
|
|
296
|
+
* throttledSave() // Executes immediately
|
|
297
|
+
* throttledSave() // Queued, executes after 1 second
|
|
298
|
+
* throttledSave() // Ignored (already queued)
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
156
301
|
export const throttle = (fn: () => void, ms: number) => {
|
|
157
302
|
let shouldWait = false
|
|
158
303
|
let shouldCallAgain = false
|
|
@@ -179,13 +324,26 @@ export const throttle = (fn: () => void, ms: number) => {
|
|
|
179
324
|
}
|
|
180
325
|
}
|
|
181
326
|
|
|
327
|
+
/**
|
|
328
|
+
* Generates a W3C Trace Context `traceparent` header from an OpenTelemetry span.
|
|
329
|
+
* @see https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers
|
|
330
|
+
*/
|
|
182
331
|
export const getTraceParentHeader = (parentSpan: otel.Span) => {
|
|
183
332
|
const spanContext = parentSpan.spanContext()
|
|
184
|
-
// Format: {version}-{trace_id}-{span_id}-{trace_flags}
|
|
185
|
-
// https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers
|
|
186
333
|
return `00-${spanContext.traceId}-${spanContext.spanId}-01`
|
|
187
334
|
}
|
|
188
335
|
|
|
336
|
+
/**
|
|
337
|
+
* Asserts that a tagged union value has a specific tag, narrowing its type.
|
|
338
|
+
* Throws if the tag doesn't match.
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* ```ts
|
|
342
|
+
* type Result = { _tag: 'ok'; value: number } | { _tag: 'error'; message: string }
|
|
343
|
+
* const result: Result = ...
|
|
344
|
+
* const ok = assertTag(result, 'ok') // Type is { _tag: 'ok'; value: number }
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
189
347
|
export const assertTag = <TObj extends { _tag: string }, TTag extends TObj['_tag']>(
|
|
190
348
|
obj: TObj,
|
|
191
349
|
tag: TTag,
|
|
@@ -197,6 +355,20 @@ export const assertTag = <TObj extends { _tag: string }, TTag extends TObj['_tag
|
|
|
197
355
|
return obj as any
|
|
198
356
|
}
|
|
199
357
|
|
|
358
|
+
/**
|
|
359
|
+
* Memoizes a function by JSON-stringifying its arguments as the cache key.
|
|
360
|
+
* Suitable for functions with serializable arguments.
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```ts
|
|
364
|
+
* const expensiveCalc = memoizeByStringifyArgs((a: number, b: number) => {
|
|
365
|
+
* console.log('Computing...')
|
|
366
|
+
* return a + b
|
|
367
|
+
* })
|
|
368
|
+
* expensiveCalc(1, 2) // logs 'Computing...', returns 3
|
|
369
|
+
* expensiveCalc(1, 2) // returns 3 (cached, no log)
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
200
372
|
export const memoizeByStringifyArgs = <T extends (...args: any[]) => any>(fn: T): T => {
|
|
201
373
|
const cache = new Map<string, ReturnType<T>>()
|
|
202
374
|
|
|
@@ -212,6 +384,18 @@ export const memoizeByStringifyArgs = <T extends (...args: any[]) => any>(fn: T)
|
|
|
212
384
|
}) as any
|
|
213
385
|
}
|
|
214
386
|
|
|
387
|
+
/**
|
|
388
|
+
* Memoizes a single-argument function using reference equality for cache lookup.
|
|
389
|
+
* Suitable for functions where arguments are objects that should be compared by reference.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```ts
|
|
393
|
+
* const processUser = memoizeByRef((user: User) => expensiveTransform(user))
|
|
394
|
+
* processUser(userA) // Computes
|
|
395
|
+
* processUser(userA) // Returns cached (same reference)
|
|
396
|
+
* processUser(userB) // Computes (different reference)
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
215
399
|
export const memoizeByRef = <T extends (arg: any) => any>(fn: T): T => {
|
|
216
400
|
const cache = new Map<Parameters<T>[0], ReturnType<T>>()
|
|
217
401
|
|
|
@@ -226,12 +410,29 @@ export const memoizeByRef = <T extends (arg: any) => any>(fn: T): T => {
|
|
|
226
410
|
}) as any
|
|
227
411
|
}
|
|
228
412
|
|
|
413
|
+
/** Type guard that checks if a value is a non-empty string. */
|
|
229
414
|
export const isNonEmptyString = (str: string | undefined | null): str is string => {
|
|
230
415
|
return typeof str === 'string' && str.length > 0
|
|
231
416
|
}
|
|
232
417
|
|
|
418
|
+
/** Type guard that checks if a value is a Promise (has a `then` method). */
|
|
233
419
|
export const isPromise = (value: any): value is Promise<unknown> => typeof value?.then === 'function'
|
|
234
420
|
|
|
421
|
+
/** Type guard that checks if a value is iterable (has a `Symbol.iterator` method). */
|
|
235
422
|
export const isIterable = <T>(value: any): value is Iterable<T> => typeof value?.[Symbol.iterator] === 'function'
|
|
236
423
|
|
|
424
|
+
/**
|
|
425
|
+
* Type-level utility that removes `undefined` from all property types.
|
|
426
|
+
* Used for compatibility with libraries that don't type optionals as `| undefined`.
|
|
427
|
+
*
|
|
428
|
+
* Note: This is a type-level lie—the runtime value is unchanged.
|
|
429
|
+
*/
|
|
430
|
+
export const omitUndefineds = <T extends Record<keyof any, unknown>>(
|
|
431
|
+
rec: T,
|
|
432
|
+
): {
|
|
433
|
+
[K in keyof T]: Exclude<T[K], undefined>
|
|
434
|
+
} => {
|
|
435
|
+
return rec as never
|
|
436
|
+
}
|
|
437
|
+
|
|
237
438
|
export { objectToString as errorToString } from './misc.ts'
|
|
@@ -13,6 +13,53 @@ import * as Layer from 'effect/Layer'
|
|
|
13
13
|
import * as Runtime from 'effect/Runtime'
|
|
14
14
|
import * as Scope from 'effect/Scope'
|
|
15
15
|
|
|
16
|
+
// Parent death monitoring setup
|
|
17
|
+
let parentDeathDetectionEnabled = false
|
|
18
|
+
let parentDeathTimer: NodeJS.Timeout | null = null
|
|
19
|
+
|
|
20
|
+
const stopParentDeathMonitoring = () => {
|
|
21
|
+
parentDeathDetectionEnabled = false
|
|
22
|
+
if (parentDeathTimer) {
|
|
23
|
+
clearTimeout(parentDeathTimer)
|
|
24
|
+
parentDeathTimer = null
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const setupParentDeathMonitoring = (parentPid: number) => {
|
|
29
|
+
if (parentDeathDetectionEnabled) return
|
|
30
|
+
parentDeathDetectionEnabled = true
|
|
31
|
+
|
|
32
|
+
let consecutiveFailures = 0
|
|
33
|
+
const maxFailures = 3 // Require 3 consecutive failures before self-terminating
|
|
34
|
+
|
|
35
|
+
// Check if parent is still alive every 2 seconds (more conservative)
|
|
36
|
+
const checkParentAlive = () => {
|
|
37
|
+
if (!parentDeathDetectionEnabled) return
|
|
38
|
+
try {
|
|
39
|
+
// Send signal 0 to check if process exists (doesn't actually send signal)
|
|
40
|
+
process.kill(parentPid, 0)
|
|
41
|
+
// If we reach here, parent is still alive, reset failure counter and check again later
|
|
42
|
+
consecutiveFailures = 0
|
|
43
|
+
parentDeathTimer = setTimeout(checkParentAlive, 2000)
|
|
44
|
+
} catch {
|
|
45
|
+
consecutiveFailures++
|
|
46
|
+
console.warn(`[Worker ${process.pid}] Parent check failed (${consecutiveFailures}/${maxFailures})`)
|
|
47
|
+
|
|
48
|
+
if (consecutiveFailures >= maxFailures) {
|
|
49
|
+
// Parent process has been gone for multiple checks, self-terminate
|
|
50
|
+
console.error(`[Worker ${process.pid}] Parent process ${parentPid} confirmed dead, self-terminating`)
|
|
51
|
+
process.exit(0)
|
|
52
|
+
} else {
|
|
53
|
+
// Try again sooner on failure
|
|
54
|
+
parentDeathTimer = setTimeout(checkParentAlive, 1000)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Start monitoring after a longer initial delay to let things settle
|
|
60
|
+
parentDeathTimer = setTimeout(checkParentAlive, 5000)
|
|
61
|
+
}
|
|
62
|
+
|
|
16
63
|
const platformRunnerImpl = Runner.PlatformRunner.of({
|
|
17
64
|
[Runner.PlatformRunnerTypeId]: Runner.PlatformRunnerTypeId,
|
|
18
65
|
start<I, O>(closeLatch: typeof CloseLatch.Service) {
|
|
@@ -43,18 +90,32 @@ const platformRunnerImpl = Runner.PlatformRunner.of({
|
|
|
43
90
|
Deferred.unsafeDone(closeLatch, Exit.die(exit.cause))
|
|
44
91
|
}
|
|
45
92
|
}
|
|
46
|
-
port.on('message', (message: Runner.BackingRunner.Message<I>) => {
|
|
93
|
+
port.on('message', (message: Runner.BackingRunner.Message<I> | any) => {
|
|
47
94
|
// console.log('message', message)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
95
|
+
|
|
96
|
+
// Handle parent death detection setup messages
|
|
97
|
+
if (Array.isArray(message) && message[0] === 'setup-parent-death-detection' && message[1]?.parentPid) {
|
|
98
|
+
const parentPid = message[1].parentPid
|
|
99
|
+
// console.log(`[Worker ${process.pid}] Setting up parent death detection for parent ${parentPid}`)
|
|
100
|
+
setupParentDeathMonitoring(parentPid)
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Handle normal Effect worker messages
|
|
105
|
+
if (Array.isArray(message) && typeof message[0] === 'number') {
|
|
106
|
+
if (message[0] === 0) {
|
|
107
|
+
const result = handler(0, message[1])
|
|
108
|
+
if (Effect.isEffect(result)) {
|
|
109
|
+
const fiber = runFork(result)
|
|
110
|
+
fiber.addObserver(onExit)
|
|
111
|
+
FiberSet.unsafeAdd(fiberSet, fiber)
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
// Graceful shutdown requested by parent: stop monitoring and close port
|
|
115
|
+
stopParentDeathMonitoring()
|
|
116
|
+
Deferred.unsafeDone(closeLatch, Exit.void)
|
|
117
|
+
port.close()
|
|
54
118
|
}
|
|
55
|
-
} else {
|
|
56
|
-
Deferred.unsafeDone(closeLatch, Exit.void)
|
|
57
|
-
port.close()
|
|
58
119
|
}
|
|
59
120
|
})
|
|
60
121
|
port.on('messageerror', (cause) => {
|