@zyro-sec/util 1.0.2 → 1.0.4

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 +1 -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
package/src/path.ts CHANGED
@@ -1,149 +1,149 @@
1
- export function getFilename(path: string | undefined) {
2
- if (!path) return ""
3
- const trimmed = path.replace(/[/\\]+$/, "")
4
- const index = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"))
5
- return trimmed.slice(index + 1)
6
- }
7
-
8
- export function getDirectory(path: string | undefined) {
9
- if (!path) return ""
10
- const trimmed = path.replace(/[/\\]+$/, "")
11
- const parts = trimmed.split(/[/\\]/)
12
- return parts.slice(0, parts.length - 1).join("/") + "/"
13
- }
14
-
15
- export function getFilenameTruncated(path: string | undefined, maxLength = 20) {
16
- const filename = getFilename(path)
17
- if (filename.length <= maxLength) return filename
18
- const lastDot = filename.lastIndexOf(".")
19
- const extension = lastDot <= 0 ? "" : filename.slice(lastDot)
20
- const available = maxLength - extension.length - 1
21
- if (available <= 0) return filename.slice(0, maxLength - 1) + "…"
22
- return filename.slice(0, available) + "…" + extension
23
- }
24
-
25
- export function truncateMiddle(text: string, maxLength = 20) {
26
- if (maxLength <= 0) return ""
27
- if (text.length <= maxLength) return text
28
- const available = maxLength - 1
29
- const start = Math.ceil(available / 2)
30
- const end = Math.floor(available / 2)
31
- if (end <= 0) return text.slice(0, start) + "…"
32
- return text.slice(0, start) + "…" + text.slice(-end)
33
- }
34
-
35
- const isDrive = (value: string) => {
36
- if (value.length !== 2) return false
37
- const code = value.charCodeAt(0)
38
- return value[1] === ":" && ((code >= 65 && code <= 90) || (code >= 97 && code <= 122))
39
- }
40
-
41
- const trimTrailingSlashes = (value: string) => {
42
- for (let i = value.length - 1; i >= 0; i--) {
43
- if (value[i] !== "/") return value.slice(0, i + 1)
44
- }
45
- return ""
46
- }
47
-
48
- const isWindowsPath = (value: string) => value[1] === ":" || value.startsWith("\\\\")
49
-
50
- /** A comparable form of a directory path: forward slashes for Windows paths, no trailing slash except on roots. */
51
- export function comparablePath(path: string) {
52
- const value = isWindowsPath(path) ? path.replaceAll("\\", "/") : path
53
- const trimmed = trimTrailingSlashes(value)
54
- if (!trimmed && value.startsWith("/")) return "/"
55
- if (isDrive(trimmed)) return `${trimmed}/`
56
- return trimmed
57
- }
58
-
59
- /** `child` is `parent` or inside it. Windows drive and UNC paths compare case-insensitively. */
60
- export function containsDirectory(parent: string, child: string) {
61
- const normalize = (value: string) => {
62
- const key = comparablePath(value)
63
- return /^[a-z]:\//i.test(key) || key.startsWith("//") ? key.toLowerCase() : key
64
- }
65
- const root = normalize(parent)
66
- const target = normalize(child)
67
- return target === root || target.startsWith(root.endsWith("/") ? root : `${root}/`)
68
- }
69
-
70
- export function sameDirectory(a: string, b: string) {
71
- return containsDirectory(a, b) && containsDirectory(b, a)
72
- }
73
-
74
- export function encodeFilePath(filepath: string): string {
75
- // Normalize Windows paths: convert backslashes to forward slashes
76
- const normalized = filepath.replace(/\\/g, "/")
77
-
78
- // Handle Windows absolute paths (D:/path -> /D:/path for proper file:// URLs)
79
- const rooted = /^[A-Za-z]:/.test(normalized) ? "/" + normalized : normalized
80
-
81
- // Encode each path segment (preserving forward slashes as path separators)
82
- // Keep the colon in Windows drive letters (`/C:/...`) so downstream file URL parsers
83
- // can reliably detect drives.
84
- return rooted
85
- .split("/")
86
- .map((segment, index) => {
87
- if (index === 1 && /^[A-Za-z]:$/.test(segment)) return segment
88
- return encodeURIComponent(segment)
89
- })
90
- .join("/")
91
- }
92
-
93
- const MAX_LINE_NUMBER = 1_000_000
94
-
95
- const lineHashPattern = /^#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i
96
-
97
- const lineHashSuffixPattern = /^(.*?)#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i
98
-
99
- const lineColonSuffixPattern = /^(.*?):(\d+)(?::\d+)?(?:-(\d+)(?::\d+)?)?:?$/
100
-
101
- function toLineRange(first: number, second: number) {
102
- if (
103
- !Number.isSafeInteger(first) ||
104
- !Number.isSafeInteger(second) ||
105
- first < 1 ||
106
- second < 1 ||
107
- first > MAX_LINE_NUMBER ||
108
- second > MAX_LINE_NUMBER
109
- ) {
110
- return undefined
111
- }
112
-
113
- return {
114
- start: Math.min(first, second),
115
- end: Math.max(first, second),
116
- }
117
- }
118
-
119
- export function isLineRangeHash(hash: string) {
120
- return lineHashPattern.test(hash)
121
- }
122
-
123
- export function parsePathLineSuffix(input: string) {
124
- const hashMatch = input.match(lineHashSuffixPattern)
125
-
126
- if (hashMatch) {
127
- const first = Number(hashMatch[2])
128
- const second = hashMatch[3] ? Number(hashMatch[3]) : first
129
-
130
- return {
131
- path: hashMatch[1] ?? "",
132
- selection: toLineRange(first, second),
133
- }
134
- }
135
-
136
- const colonMatch = input.match(lineColonSuffixPattern)
137
-
138
- if (colonMatch && !/^[a-z]:?$/i.test(colonMatch[1] ?? "")) {
139
- const first = Number(colonMatch[2])
140
- const second = colonMatch[3] ? Number(colonMatch[3]) : first
141
-
142
- return {
143
- path: colonMatch[1] ?? "",
144
- selection: toLineRange(first, second),
145
- }
146
- }
147
-
148
- return { path: input, selection: undefined }
149
- }
1
+ export function getFilename(path: string | undefined) {
2
+ if (!path) return ""
3
+ const trimmed = path.replace(/[/\\]+$/, "")
4
+ const index = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"))
5
+ return trimmed.slice(index + 1)
6
+ }
7
+
8
+ export function getDirectory(path: string | undefined) {
9
+ if (!path) return ""
10
+ const trimmed = path.replace(/[/\\]+$/, "")
11
+ const parts = trimmed.split(/[/\\]/)
12
+ return parts.slice(0, parts.length - 1).join("/") + "/"
13
+ }
14
+
15
+ export function getFilenameTruncated(path: string | undefined, maxLength = 20) {
16
+ const filename = getFilename(path)
17
+ if (filename.length <= maxLength) return filename
18
+ const lastDot = filename.lastIndexOf(".")
19
+ const extension = lastDot <= 0 ? "" : filename.slice(lastDot)
20
+ const available = maxLength - extension.length - 1
21
+ if (available <= 0) return filename.slice(0, maxLength - 1) + "…"
22
+ return filename.slice(0, available) + "…" + extension
23
+ }
24
+
25
+ export function truncateMiddle(text: string, maxLength = 20) {
26
+ if (maxLength <= 0) return ""
27
+ if (text.length <= maxLength) return text
28
+ const available = maxLength - 1
29
+ const start = Math.ceil(available / 2)
30
+ const end = Math.floor(available / 2)
31
+ if (end <= 0) return text.slice(0, start) + "…"
32
+ return text.slice(0, start) + "…" + text.slice(-end)
33
+ }
34
+
35
+ const isDrive = (value: string) => {
36
+ if (value.length !== 2) return false
37
+ const code = value.charCodeAt(0)
38
+ return value[1] === ":" && ((code >= 65 && code <= 90) || (code >= 97 && code <= 122))
39
+ }
40
+
41
+ const trimTrailingSlashes = (value: string) => {
42
+ for (let i = value.length - 1; i >= 0; i--) {
43
+ if (value[i] !== "/") return value.slice(0, i + 1)
44
+ }
45
+ return ""
46
+ }
47
+
48
+ const isWindowsPath = (value: string) => value[1] === ":" || value.startsWith("\\\\")
49
+
50
+ /** A comparable form of a directory path: forward slashes for Windows paths, no trailing slash except on roots. */
51
+ export function comparablePath(path: string) {
52
+ const value = isWindowsPath(path) ? path.replaceAll("\\", "/") : path
53
+ const trimmed = trimTrailingSlashes(value)
54
+ if (!trimmed && value.startsWith("/")) return "/"
55
+ if (isDrive(trimmed)) return `${trimmed}/`
56
+ return trimmed
57
+ }
58
+
59
+ /** `child` is `parent` or inside it. Windows drive and UNC paths compare case-insensitively. */
60
+ export function containsDirectory(parent: string, child: string) {
61
+ const normalize = (value: string) => {
62
+ const key = comparablePath(value)
63
+ return /^[a-z]:\//i.test(key) || key.startsWith("//") ? key.toLowerCase() : key
64
+ }
65
+ const root = normalize(parent)
66
+ const target = normalize(child)
67
+ return target === root || target.startsWith(root.endsWith("/") ? root : `${root}/`)
68
+ }
69
+
70
+ export function sameDirectory(a: string, b: string) {
71
+ return containsDirectory(a, b) && containsDirectory(b, a)
72
+ }
73
+
74
+ export function encodeFilePath(filepath: string): string {
75
+ // Normalize Windows paths: convert backslashes to forward slashes
76
+ const normalized = filepath.replace(/\\/g, "/")
77
+
78
+ // Handle Windows absolute paths (D:/path -> /D:/path for proper file:// URLs)
79
+ const rooted = /^[A-Za-z]:/.test(normalized) ? "/" + normalized : normalized
80
+
81
+ // Encode each path segment (preserving forward slashes as path separators)
82
+ // Keep the colon in Windows drive letters (`/C:/...`) so downstream file URL parsers
83
+ // can reliably detect drives.
84
+ return rooted
85
+ .split("/")
86
+ .map((segment, index) => {
87
+ if (index === 1 && /^[A-Za-z]:$/.test(segment)) return segment
88
+ return encodeURIComponent(segment)
89
+ })
90
+ .join("/")
91
+ }
92
+
93
+ const MAX_LINE_NUMBER = 1_000_000
94
+
95
+ const lineHashPattern = /^#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i
96
+
97
+ const lineHashSuffixPattern = /^(.*?)#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i
98
+
99
+ const lineColonSuffixPattern = /^(.*?):(\d+)(?::\d+)?(?:-(\d+)(?::\d+)?)?:?$/
100
+
101
+ function toLineRange(first: number, second: number) {
102
+ if (
103
+ !Number.isSafeInteger(first) ||
104
+ !Number.isSafeInteger(second) ||
105
+ first < 1 ||
106
+ second < 1 ||
107
+ first > MAX_LINE_NUMBER ||
108
+ second > MAX_LINE_NUMBER
109
+ ) {
110
+ return undefined
111
+ }
112
+
113
+ return {
114
+ start: Math.min(first, second),
115
+ end: Math.max(first, second),
116
+ }
117
+ }
118
+
119
+ export function isLineRangeHash(hash: string) {
120
+ return lineHashPattern.test(hash)
121
+ }
122
+
123
+ export function parsePathLineSuffix(input: string) {
124
+ const hashMatch = input.match(lineHashSuffixPattern)
125
+
126
+ if (hashMatch) {
127
+ const first = Number(hashMatch[2])
128
+ const second = hashMatch[3] ? Number(hashMatch[3]) : first
129
+
130
+ return {
131
+ path: hashMatch[1] ?? "",
132
+ selection: toLineRange(first, second),
133
+ }
134
+ }
135
+
136
+ const colonMatch = input.match(lineColonSuffixPattern)
137
+
138
+ if (colonMatch && !/^[a-z]:?$/i.test(colonMatch[1] ?? "")) {
139
+ const first = Number(colonMatch[2])
140
+ const second = colonMatch[3] ? Number(colonMatch[3]) : first
141
+
142
+ return {
143
+ path: colonMatch[1] ?? "",
144
+ selection: toLineRange(first, second),
145
+ }
146
+ }
147
+
148
+ return { path: input, selection: undefined }
149
+ }
@@ -1,23 +1,23 @@
1
- export * as ProcessSignal from "./process-signal.js"
2
-
3
- const handlers = new Map<NodeJS.Signals, Set<() => void>>()
4
-
5
- /** Install once at process startup. Removing subscribers never restores the signal's default action. */
6
- export function install(signal: NodeJS.Signals, unavailable: () => void) {
7
- if (handlers.has(signal)) return
8
- const listeners = new Set<() => void>()
9
- handlers.set(signal, listeners)
10
- process.on(signal, () => {
11
- if (!listeners.size) return unavailable()
12
- listeners.forEach((listener) => listener())
13
- })
14
- }
15
-
16
- /** Subscribe only to an installed signal. The returned cleanup removes this subscriber, not the process handler. */
17
- export function subscribe(signal: NodeJS.Signals, listener: () => void) {
18
- const listeners = handlers.get(signal)
19
- listeners?.add(listener)
20
- return () => {
21
- listeners?.delete(listener)
22
- }
23
- }
1
+ export * as ProcessSignal from "./process-signal.js"
2
+
3
+ const handlers = new Map<NodeJS.Signals, Set<() => void>>()
4
+
5
+ /** Install once at process startup. Removing subscribers never restores the signal's default action. */
6
+ export function install(signal: NodeJS.Signals, unavailable: () => void) {
7
+ if (handlers.has(signal)) return
8
+ const listeners = new Set<() => void>()
9
+ handlers.set(signal, listeners)
10
+ process.on(signal, () => {
11
+ if (!listeners.size) return unavailable()
12
+ listeners.forEach((listener) => listener())
13
+ })
14
+ }
15
+
16
+ /** Subscribe only to an installed signal. The returned cleanup removes this subscriber, not the process handler. */
17
+ export function subscribe(signal: NodeJS.Signals, listener: () => void) {
18
+ const listeners = handlers.get(signal)
19
+ listeners?.add(listener)
20
+ return () => {
21
+ listeners?.delete(listener)
22
+ }
23
+ }