@zyro-sec/util 1.0.1 → 1.0.3

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.
Files changed (78) hide show
  1. package/docs/layer-node.md +155 -155
  2. package/package.json +5 -1
  3. package/script/build.ts +9 -9
  4. package/script/pack.ts +5 -5
  5. package/src/abort.ts +9 -9
  6. package/src/activity-calendar.ts +50 -50
  7. package/src/artifact.ts +95 -95
  8. package/src/base64.ts +29 -29
  9. package/src/binary.ts +30 -30
  10. package/src/bom.ts +46 -46
  11. package/src/cross-spawn-spawner.ts +694 -694
  12. package/src/duration.ts +22 -22
  13. package/src/effect/app-node-platform.ts +40 -40
  14. package/src/effect/app-node.ts +14 -14
  15. package/src/effect/layer-node.ts +360 -360
  16. package/src/effect-flock.ts +313 -313
  17. package/src/encode.ts +39 -39
  18. package/src/execute-output.ts +23 -23
  19. package/src/flock.ts +346 -346
  20. package/src/fs-util.ts +271 -271
  21. package/src/glob.ts +38 -38
  22. package/src/global-roots.ts +19 -19
  23. package/src/global-roots.workerd.ts +16 -16
  24. package/src/global.ts +88 -88
  25. package/src/hash.ts +11 -11
  26. package/src/heap-snapshot.ts +44 -44
  27. package/src/no-timeout-fetch.ts +5 -5
  28. package/src/npm-config.ts +41 -41
  29. package/src/npm.ts +557 -557
  30. package/src/observability/logging.ts +179 -179
  31. package/src/observability/otlp.ts +109 -109
  32. package/src/observability/shared.ts +8 -8
  33. package/src/observability.ts +60 -60
  34. package/src/open.ts +12 -12
  35. package/src/patch.ts +382 -382
  36. package/src/path.ts +149 -149
  37. package/src/process-signal.ts +23 -23
  38. package/src/process.ts +270 -270
  39. package/src/qr.ts +323 -323
  40. package/src/record.ts +3 -3
  41. package/src/retry.ts +51 -51
  42. package/src/runtime/import.bun.ts +10 -10
  43. package/src/runtime/import.node.ts +61 -61
  44. package/src/runtime/import.workerd.ts +9 -9
  45. package/src/runtime-import.ts +1 -1
  46. package/src/session-mention.ts +24 -24
  47. package/src/session-title-fallback.ts +36 -36
  48. package/src/session-turn.ts +82 -82
  49. package/src/teardown.ts +19 -19
  50. package/src/text-boundary.ts +36 -36
  51. package/src/uuid.ts +15 -15
  52. package/test/fixtures/qr.json +625 -625
  53. package/tsconfig.build.json +12 -12
  54. package/tsconfig.dist.json +10 -10
  55. package/tsconfig.json +14 -14
  56. package/src/abort.test.ts +0 -16
  57. package/src/activity-calendar.test.ts +0 -136
  58. package/src/artifact.test.ts +0 -29
  59. package/src/base64.test.ts +0 -12
  60. package/src/binary.test.ts +0 -19
  61. package/src/bom.test.ts +0 -21
  62. package/src/encode.test.ts +0 -32
  63. package/src/execute-output.test.ts +0 -36
  64. package/src/no-timeout-fetch.test.ts +0 -33
  65. package/src/open.test.ts +0 -16
  66. package/src/path.test.ts +0 -89
  67. package/src/process-signal.test.ts +0 -38
  68. package/src/retry.test.ts +0 -87
  69. package/src/session-mention.test.ts +0 -19
  70. package/src/session-turn.test.ts +0 -131
  71. package/src/text-boundary.test.ts +0 -39
  72. package/src/uuid.test.ts +0 -80
  73. package/test/glob.test.ts +0 -11
  74. package/test/global-roots.test.ts +0 -62
  75. package/test/global.test.ts +0 -99
  76. package/test/observability-otlp.test.ts +0 -45
  77. package/test/qr.test.ts +0 -113
  78. package/test/session-title-fallback.test.ts +0 -49
@@ -1,16 +1,16 @@
1
- import os from "os"
2
- import path from "path"
3
-
4
- // workerd has no home directory or XDG base dirs and only tmp is writable, so
5
- // every global path roots under one directory there. Nothing durable lives in
6
- // these: on workerd the database is on Durable Object storage.
7
- export function roots(app: string) {
8
- const root = path.join(os.tmpdir(), app)
9
- return {
10
- data: path.join(root, "data"),
11
- cache: path.join(root, "cache"),
12
- config: path.join(root, "config"),
13
- state: path.join(root, "state"),
14
- tmp: path.join(root, "tmp"),
15
- }
16
- }
1
+ import os from "os"
2
+ import path from "path"
3
+
4
+ // workerd has no home directory or XDG base dirs and only tmp is writable, so
5
+ // every global path roots under one directory there. Nothing durable lives in
6
+ // these: on workerd the database is on Durable Object storage.
7
+ export function roots(app: string) {
8
+ const root = path.join(os.tmpdir(), app)
9
+ return {
10
+ data: path.join(root, "data"),
11
+ cache: path.join(root, "cache"),
12
+ config: path.join(root, "config"),
13
+ state: path.join(root, "state"),
14
+ tmp: path.join(root, "tmp"),
15
+ }
16
+ }
package/src/global.ts CHANGED
@@ -1,88 +1,88 @@
1
- import path from "path"
2
- import fs from "fs"
3
- import os from "os"
4
- import { Context, Effect, Layer } from "effect"
5
- // XDG on runtimes with a home directory; one tmp-rooted directory on workerd.
6
- // The variants resolve through the `workerd` bundle condition, like the
7
- // native-module stubs, so no runtime sniffing happens here.
8
- import { roots } from "#global-roots"
9
- import { Flock } from "./flock.js"
10
- import { makeGlobalNode } from "./effect/app-node.js"
11
-
12
- const app = "zyrosec"
13
- const { data, cache, config, state, tmp } = roots(app)
14
-
15
- const paths = {
16
- get home() {
17
- return process.env.ZYROSEC_TEST_HOME ?? process.env.OPENCODE_TEST_HOME ?? os.homedir()
18
- },
19
- data,
20
- bin: path.join(cache, "bin"),
21
- log: path.join(data, "log"),
22
- repos: path.join(data, "repos"),
23
- cache,
24
- config,
25
- state,
26
- tmp,
27
- }
28
-
29
- export const Path = paths
30
-
31
- Flock.setGlobal({ state })
32
-
33
- export class Service extends Context.Service<Service, Interface>()("@zyro-sec/Global") {}
34
-
35
- export interface Interface {
36
- readonly home: string
37
- readonly data: string
38
- readonly cache: string
39
- readonly config: string
40
- readonly state: string
41
- readonly tmp: string
42
- readonly bin: string
43
- readonly log: string
44
- readonly repos: string
45
- }
46
-
47
- export function make(input: Partial<Interface> = {}): Interface {
48
- // The acquired service canonicalizes default tmp; use it instead of Path.tmp for path comparisons.
49
- return {
50
- home: Path.home,
51
- data: Path.data,
52
- cache: Path.cache,
53
- config: Path.config,
54
- state: Path.state,
55
- tmp: input.tmp ?? Path.tmp,
56
- bin: Path.bin,
57
- log: Path.log,
58
- repos: Path.repos,
59
- ...input,
60
- }
61
- }
62
-
63
- const acquire = (input: Partial<Interface>) =>
64
- Effect.gen(function* () {
65
- const service = Service.of(make(input))
66
- yield* Effect.promise(() =>
67
- Promise.all(
68
- [service.data, service.config, service.state, service.log, service.bin, service.repos, service.tmp].map(
69
- (directory) => fs.promises.mkdir(directory, { recursive: true }),
70
- ),
71
- ),
72
- )
73
- const canonicalTmp = yield* Effect.promise(() => fs.promises.realpath(service.tmp))
74
- return Service.of({ ...service, tmp: input.tmp ?? canonicalTmp })
75
- })
76
-
77
- const layer = Layer.effect(
78
- Service,
79
- Effect.suspend(() =>
80
- acquire({ config: process.env.ZYROSEC_CONFIG_DIR ?? process.env.OPENCODE_CONFIG_DIR ?? Path.config }),
81
- ),
82
- )
83
-
84
- export const node = makeGlobalNode({ service: Service, layer: layer, deps: [] })
85
-
86
- export const layerWith = (input: Partial<Interface>) => Layer.effect(Service, acquire(input))
87
-
88
- export * as Global from "./global.js"
1
+ import path from "path"
2
+ import fs from "fs"
3
+ import os from "os"
4
+ import { Context, Effect, Layer } from "effect"
5
+ // XDG on runtimes with a home directory; one tmp-rooted directory on workerd.
6
+ // The variants resolve through the `workerd` bundle condition, like the
7
+ // native-module stubs, so no runtime sniffing happens here.
8
+ import { roots } from "#global-roots"
9
+ import { Flock } from "./flock.js"
10
+ import { makeGlobalNode } from "./effect/app-node.js"
11
+
12
+ const app = "zyrosec"
13
+ const { data, cache, config, state, tmp } = roots(app)
14
+
15
+ const paths = {
16
+ get home() {
17
+ return process.env.ZYROSEC_TEST_HOME ?? process.env.OPENCODE_TEST_HOME ?? os.homedir()
18
+ },
19
+ data,
20
+ bin: path.join(cache, "bin"),
21
+ log: path.join(data, "log"),
22
+ repos: path.join(data, "repos"),
23
+ cache,
24
+ config,
25
+ state,
26
+ tmp,
27
+ }
28
+
29
+ export const Path = paths
30
+
31
+ Flock.setGlobal({ state })
32
+
33
+ export class Service extends Context.Service<Service, Interface>()("@zyro-sec/Global") {}
34
+
35
+ export interface Interface {
36
+ readonly home: string
37
+ readonly data: string
38
+ readonly cache: string
39
+ readonly config: string
40
+ readonly state: string
41
+ readonly tmp: string
42
+ readonly bin: string
43
+ readonly log: string
44
+ readonly repos: string
45
+ }
46
+
47
+ export function make(input: Partial<Interface> = {}): Interface {
48
+ // The acquired service canonicalizes default tmp; use it instead of Path.tmp for path comparisons.
49
+ return {
50
+ home: Path.home,
51
+ data: Path.data,
52
+ cache: Path.cache,
53
+ config: Path.config,
54
+ state: Path.state,
55
+ tmp: input.tmp ?? Path.tmp,
56
+ bin: Path.bin,
57
+ log: Path.log,
58
+ repos: Path.repos,
59
+ ...input,
60
+ }
61
+ }
62
+
63
+ const acquire = (input: Partial<Interface>) =>
64
+ Effect.gen(function* () {
65
+ const service = Service.of(make(input))
66
+ yield* Effect.promise(() =>
67
+ Promise.all(
68
+ [service.data, service.config, service.state, service.log, service.bin, service.repos, service.tmp].map(
69
+ (directory) => fs.promises.mkdir(directory, { recursive: true }),
70
+ ),
71
+ ),
72
+ )
73
+ const canonicalTmp = yield* Effect.promise(() => fs.promises.realpath(service.tmp))
74
+ return Service.of({ ...service, tmp: input.tmp ?? canonicalTmp })
75
+ })
76
+
77
+ const layer = Layer.effect(
78
+ Service,
79
+ Effect.suspend(() =>
80
+ acquire({ config: process.env.ZYROSEC_CONFIG_DIR ?? process.env.OPENCODE_CONFIG_DIR ?? Path.config }),
81
+ ),
82
+ )
83
+
84
+ export const node = makeGlobalNode({ service: Service, layer: layer, deps: [] })
85
+
86
+ export const layerWith = (input: Partial<Interface>) => Layer.effect(Service, acquire(input))
87
+
88
+ export * as Global from "./global.js"
package/src/hash.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { createHash } from "crypto"
2
-
3
- export namespace Hash {
4
- export function fast(input: string | Buffer): string {
5
- return createHash("sha1").update(input).digest("hex")
6
- }
7
-
8
- export function sha256(input: string | Buffer): string {
9
- return createHash("sha256").update(input).digest("hex")
10
- }
11
- }
1
+ import { createHash } from "crypto"
2
+
3
+ export namespace Hash {
4
+ export function fast(input: string | Buffer): string {
5
+ return createHash("sha1").update(input).digest("hex")
6
+ }
7
+
8
+ export function sha256(input: string | Buffer): string {
9
+ return createHash("sha256").update(input).digest("hex")
10
+ }
11
+ }
@@ -1,44 +1,44 @@
1
- import { Clock, Effect, Semaphore } from "effect"
2
- import { mkdir, open } from "node:fs/promises"
3
- import path from "node:path"
4
-
5
- // One process-wide permit: the signal listener and the HTTP endpoint share it, so concurrent
6
- // requests queue instead of serializing two heaps at once.
7
- const lock = Semaphore.makeUnsafe(1)
8
-
9
- /**
10
- * Writes a V8 heap snapshot of this process into `directory` and returns once the file is complete, with the
11
- * process's memory just before the snapshot: the snapshot holds only the garbage-collected heap, and writing it
12
- * raises the process's memory. Blocks the event loop while the heap serializes. The file can contain secrets, so it
13
- * is created owner-only before the runtime writes into it.
14
- */
15
- export const write = Effect.fn("HeapSnapshot.write")(
16
- function* (directory: string) {
17
- const v8 = yield* Effect.tryPromise(() => import("node:v8"))
18
- const time = new Date(yield* Clock.currentTimeMillis).toISOString().replace(/[:.]/g, "")
19
- const file = path.join(directory, `heap-${process.pid}-${time}.heapsnapshot`)
20
- yield* Effect.tryPromise(async () => {
21
- await mkdir(directory, { recursive: true })
22
- const handle = await open(file, "wx", 0o600)
23
- await handle.close()
24
- })
25
- const memory = yield* Effect.sync(() => process.memoryUsage())
26
- yield* Effect.logInfo("writing heap snapshot", { path: file, rss: memory.rss, heapUsed: memory.heapUsed })
27
- yield* Effect.try(() => v8.writeHeapSnapshot(file))
28
- yield* Effect.logInfo("heap snapshot written", { path: file })
29
- return {
30
- path: file,
31
- pid: process.pid,
32
- memory: {
33
- rss: memory.rss,
34
- heapTotal: memory.heapTotal,
35
- heapUsed: memory.heapUsed,
36
- external: memory.external,
37
- arrayBuffers: memory.arrayBuffers,
38
- },
39
- }
40
- },
41
- (effect) => lock.withPermit(effect),
42
- )
43
-
44
- export * as HeapSnapshot from "./heap-snapshot.js"
1
+ import { Clock, Effect, Semaphore } from "effect"
2
+ import { mkdir, open } from "node:fs/promises"
3
+ import path from "node:path"
4
+
5
+ // One process-wide permit: the signal listener and the HTTP endpoint share it, so concurrent
6
+ // requests queue instead of serializing two heaps at once.
7
+ const lock = Semaphore.makeUnsafe(1)
8
+
9
+ /**
10
+ * Writes a V8 heap snapshot of this process into `directory` and returns once the file is complete, with the
11
+ * process's memory just before the snapshot: the snapshot holds only the garbage-collected heap, and writing it
12
+ * raises the process's memory. Blocks the event loop while the heap serializes. The file can contain secrets, so it
13
+ * is created owner-only before the runtime writes into it.
14
+ */
15
+ export const write = Effect.fn("HeapSnapshot.write")(
16
+ function* (directory: string) {
17
+ const v8 = yield* Effect.tryPromise(() => import("node:v8"))
18
+ const time = new Date(yield* Clock.currentTimeMillis).toISOString().replace(/[:.]/g, "")
19
+ const file = path.join(directory, `heap-${process.pid}-${time}.heapsnapshot`)
20
+ yield* Effect.tryPromise(async () => {
21
+ await mkdir(directory, { recursive: true })
22
+ const handle = await open(file, "wx", 0o600)
23
+ await handle.close()
24
+ })
25
+ const memory = yield* Effect.sync(() => process.memoryUsage())
26
+ yield* Effect.logInfo("writing heap snapshot", { path: file, rss: memory.rss, heapUsed: memory.heapUsed })
27
+ yield* Effect.try(() => v8.writeHeapSnapshot(file))
28
+ yield* Effect.logInfo("heap snapshot written", { path: file })
29
+ return {
30
+ path: file,
31
+ pid: process.pid,
32
+ memory: {
33
+ rss: memory.rss,
34
+ heapTotal: memory.heapTotal,
35
+ heapUsed: memory.heapUsed,
36
+ external: memory.external,
37
+ arrayBuffers: memory.arrayBuffers,
38
+ },
39
+ }
40
+ },
41
+ (effect) => lock.withPermit(effect),
42
+ )
43
+
44
+ export * as HeapSnapshot from "./heap-snapshot.js"
@@ -1,5 +1,5 @@
1
- // Bun's fetch aborts requests after its default idle deadline (about five minutes). Clients that
2
- // hold requests open for long-running work (session waits, event streams, plugin commands,
3
- // shell calls) opt out while keeping every other init option, including `signal`.
4
- export const noTimeoutFetch = ((request: Parameters<typeof fetch>[0], init?: RequestInit) =>
5
- fetch(request, { ...init, timeout: false } as BunFetchRequestInit)) as typeof fetch
1
+ // Bun's fetch aborts requests after its default idle deadline (about five minutes). Clients that
2
+ // hold requests open for long-running work (session waits, event streams, plugin commands,
3
+ // shell calls) opt out while keeping every other init option, including `signal`.
4
+ export const noTimeoutFetch = ((request: Parameters<typeof fetch>[0], init?: RequestInit) =>
5
+ fetch(request, { ...init, timeout: false } as BunFetchRequestInit)) as typeof fetch
package/src/npm-config.ts CHANGED
@@ -1,41 +1,41 @@
1
- export * as NpmConfig from "./npm-config.js"
2
-
3
- import { fileURLToPath } from "url"
4
- import { Effect } from "effect"
5
-
6
- export const load = (dir: string) =>
7
- Effect.tryPromise({
8
- try: async () => {
9
- // @ts-expect-error npm does not publish types for this internal config API.
10
- const { default: Config } = await import("@npmcli/config")
11
- // @ts-expect-error npm does not publish types for this internal config API.
12
- const { default: npmDefinitions } = await import("@npmcli/config/lib/definitions/index.js")
13
- const { definitions, flatten, nerfDarts, shorthands } = npmDefinitions
14
- const config = new Config({
15
- // Resolved per call: on workerd import.meta.url is undefined and building
16
- // this URL at module scope fails startup validation; npm config never runs there.
17
- npmPath: fileURLToPath(new URL("..", import.meta.url)),
18
- cwd: dir,
19
- env: { ...process.env },
20
- argv: [process.execPath, process.execPath, "--prefix", dir],
21
- execPath: process.execPath,
22
- platform: process.platform,
23
- definitions,
24
- flatten,
25
- nerfDarts,
26
- shorthands,
27
- warn: false,
28
- })
29
- await config.load()
30
- return config.flat as Record<string, unknown>
31
- },
32
- catch: (cause) => cause,
33
- }).pipe(Effect.orElseSucceed(() => ({}) as Record<string, unknown>))
34
-
35
- export const registry = (dir: string) =>
36
- load(dir).pipe(
37
- Effect.map((config) => {
38
- const registry = typeof config.registry === "string" ? config.registry : "https://registry.npmjs.org"
39
- return registry.endsWith("/") ? registry.slice(0, -1) : registry
40
- }),
41
- )
1
+ export * as NpmConfig from "./npm-config.js"
2
+
3
+ import { fileURLToPath } from "url"
4
+ import { Effect } from "effect"
5
+
6
+ export const load = (dir: string) =>
7
+ Effect.tryPromise({
8
+ try: async () => {
9
+ // @ts-expect-error npm does not publish types for this internal config API.
10
+ const { default: Config } = await import("@npmcli/config")
11
+ // @ts-expect-error npm does not publish types for this internal config API.
12
+ const { default: npmDefinitions } = await import("@npmcli/config/lib/definitions/index.js")
13
+ const { definitions, flatten, nerfDarts, shorthands } = npmDefinitions
14
+ const config = new Config({
15
+ // Resolved per call: on workerd import.meta.url is undefined and building
16
+ // this URL at module scope fails startup validation; npm config never runs there.
17
+ npmPath: fileURLToPath(new URL("..", import.meta.url)),
18
+ cwd: dir,
19
+ env: { ...process.env },
20
+ argv: [process.execPath, process.execPath, "--prefix", dir],
21
+ execPath: process.execPath,
22
+ platform: process.platform,
23
+ definitions,
24
+ flatten,
25
+ nerfDarts,
26
+ shorthands,
27
+ warn: false,
28
+ })
29
+ await config.load()
30
+ return config.flat as Record<string, unknown>
31
+ },
32
+ catch: (cause) => cause,
33
+ }).pipe(Effect.orElseSucceed(() => ({}) as Record<string, unknown>))
34
+
35
+ export const registry = (dir: string) =>
36
+ load(dir).pipe(
37
+ Effect.map((config) => {
38
+ const registry = typeof config.registry === "string" ? config.registry : "https://registry.npmjs.org"
39
+ return registry.endsWith("/") ? registry.slice(0, -1) : registry
40
+ }),
41
+ )