@zyro-sec/util 1.0.2 → 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.
- package/docs/layer-node.md +155 -155
- package/package.json +1 -1
- package/script/build.ts +9 -9
- package/script/pack.ts +5 -5
- package/src/abort.ts +9 -9
- package/src/activity-calendar.ts +50 -50
- package/src/artifact.ts +95 -95
- package/src/base64.ts +29 -29
- package/src/binary.ts +30 -30
- package/src/bom.ts +46 -46
- package/src/cross-spawn-spawner.ts +694 -694
- package/src/duration.ts +22 -22
- package/src/effect/app-node-platform.ts +40 -40
- package/src/effect/app-node.ts +14 -14
- package/src/effect/layer-node.ts +360 -360
- package/src/effect-flock.ts +313 -313
- package/src/encode.ts +39 -39
- package/src/execute-output.ts +23 -23
- package/src/flock.ts +346 -346
- package/src/fs-util.ts +271 -271
- package/src/glob.ts +38 -38
- package/src/global-roots.ts +19 -19
- package/src/global-roots.workerd.ts +16 -16
- package/src/global.ts +88 -88
- package/src/hash.ts +11 -11
- package/src/heap-snapshot.ts +44 -44
- package/src/no-timeout-fetch.ts +5 -5
- package/src/npm-config.ts +41 -41
- package/src/npm.ts +557 -557
- package/src/observability/logging.ts +179 -179
- package/src/observability/otlp.ts +109 -109
- package/src/observability/shared.ts +8 -8
- package/src/observability.ts +60 -60
- package/src/open.ts +12 -12
- package/src/patch.ts +382 -382
- package/src/path.ts +149 -149
- package/src/process-signal.ts +23 -23
- package/src/process.ts +270 -270
- package/src/qr.ts +323 -323
- package/src/record.ts +3 -3
- package/src/retry.ts +51 -51
- package/src/runtime/import.bun.ts +10 -10
- package/src/runtime/import.node.ts +61 -61
- package/src/runtime/import.workerd.ts +9 -9
- package/src/runtime-import.ts +1 -1
- package/src/session-mention.ts +24 -24
- package/src/session-title-fallback.ts +36 -36
- package/src/session-turn.ts +82 -82
- package/src/teardown.ts +19 -19
- package/src/text-boundary.ts +36 -36
- package/src/uuid.ts +15 -15
- package/test/fixtures/qr.json +625 -625
- package/tsconfig.build.json +12 -12
- package/tsconfig.dist.json +10 -10
- package/tsconfig.json +14 -14
- package/src/abort.test.ts +0 -16
- package/src/activity-calendar.test.ts +0 -136
- package/src/artifact.test.ts +0 -29
- package/src/base64.test.ts +0 -12
- package/src/binary.test.ts +0 -19
- package/src/bom.test.ts +0 -21
- package/src/encode.test.ts +0 -32
- package/src/execute-output.test.ts +0 -36
- package/src/no-timeout-fetch.test.ts +0 -33
- package/src/open.test.ts +0 -16
- package/src/path.test.ts +0 -89
- package/src/process-signal.test.ts +0 -38
- package/src/retry.test.ts +0 -87
- package/src/session-mention.test.ts +0 -19
- package/src/session-turn.test.ts +0 -131
- package/src/text-boundary.test.ts +0 -39
- package/src/uuid.test.ts +0 -80
- package/test/glob.test.ts +0 -11
- package/test/global-roots.test.ts +0 -62
- package/test/global.test.ts +0 -99
- package/test/observability-otlp.test.ts +0 -45
- package/test/qr.test.ts +0 -113
- package/test/session-title-fallback.test.ts +0 -49
package/src/fs-util.ts
CHANGED
|
@@ -1,271 +1,271 @@
|
|
|
1
|
-
import path, { dirname, join, type PlatformPath } from "path"
|
|
2
|
-
import { realpathSync } from "fs"
|
|
3
|
-
import { lookup } from "mime-types"
|
|
4
|
-
import { Context, Effect, FileSystem, Layer, Schema } from "effect"
|
|
5
|
-
import type { PlatformError } from "effect/PlatformError"
|
|
6
|
-
import { Glob } from "./glob.js"
|
|
7
|
-
import { makeGlobalNode } from "./effect/app-node.js"
|
|
8
|
-
import { filesystem } from "./effect/app-node-platform.js"
|
|
9
|
-
import { sameDirectory } from "./path.js"
|
|
10
|
-
|
|
11
|
-
export namespace FSUtil {
|
|
12
|
-
export class FileSystemError extends Schema.TaggedError<FileSystemError>()("FileSystemError", {
|
|
13
|
-
method: Schema.String,
|
|
14
|
-
cause: Schema.optional(Schema.Defect()),
|
|
15
|
-
}) {
|
|
16
|
-
override get message() {
|
|
17
|
-
const detail = this.cause instanceof Error ? this.cause.message : this.cause && String(this.cause)
|
|
18
|
-
return `Filesystem operation failed: ${this.method}${detail ? `: ${detail}` : ""}`
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type Error = PlatformError | FileSystemError
|
|
23
|
-
|
|
24
|
-
export interface UpOptions {
|
|
25
|
-
readonly targets: string[]
|
|
26
|
-
readonly start: string
|
|
27
|
-
readonly stop?: string
|
|
28
|
-
readonly mode?: "all" | "first"
|
|
29
|
-
/** Only match regular files or directories (following symlinks). By default any existing path matches. */
|
|
30
|
-
readonly type?: "file" | "directory"
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface Interface extends FileSystem.FileSystem {
|
|
34
|
-
readonly isDir: (path: string) => Effect.Effect<boolean>
|
|
35
|
-
readonly isFile: (path: string) => Effect.Effect<boolean>
|
|
36
|
-
readonly existsSafe: (path: string) => Effect.Effect<boolean>
|
|
37
|
-
readonly readFileStringSafe: (path: string) => Effect.Effect<string | undefined, Error>
|
|
38
|
-
readonly readJson: (path: string) => Effect.Effect<unknown, Error>
|
|
39
|
-
readonly writeJson: (path: string, data: unknown, mode?: number) => Effect.Effect<void, Error>
|
|
40
|
-
readonly ensureDir: (path: string) => Effect.Effect<void, Error>
|
|
41
|
-
readonly writeWithDirs: (path: string, content: string | Uint8Array, mode?: number) => Effect.Effect<void, Error>
|
|
42
|
-
readonly resolve: (path: string) => Effect.Effect<string>
|
|
43
|
-
readonly findUp: (target: string, start: string, stop?: string) => Effect.Effect<string[], Error>
|
|
44
|
-
readonly up: (options: UpOptions) => Effect.Effect<string[], Error>
|
|
45
|
-
readonly globUp: (pattern: string, start: string, stop?: string) => Effect.Effect<string[], Error>
|
|
46
|
-
readonly scan: (pattern: string, options?: Glob.Options) => Effect.Effect<string[], Error>
|
|
47
|
-
readonly globMatch: (pattern: string, filepath: string) => boolean
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export class Service extends Context.Service<Service, Interface>()("@zyro-sec/FSUtil") {}
|
|
51
|
-
|
|
52
|
-
// Exported so simulation can wrap this layer and override the methods that
|
|
53
|
-
// bypass the injected FileSystem (scan, globUp).
|
|
54
|
-
export const layer = Layer.effect(
|
|
55
|
-
Service,
|
|
56
|
-
Effect.gen(function* () {
|
|
57
|
-
const fs = yield* FileSystem.FileSystem
|
|
58
|
-
|
|
59
|
-
const existsSafe = Effect.fn("FileSystem.existsSafe")(function* (path: string) {
|
|
60
|
-
return yield* fs.exists(path).pipe(Effect.orElseSucceed(() => false))
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
const readFileStringSafe = Effect.fn("FileSystem.readFileStringSafe")(function* (path: string) {
|
|
64
|
-
return yield* fs.readFileString(path).pipe(
|
|
65
|
-
Effect.catchIf(
|
|
66
|
-
(error) => error.reason._tag === "NotFound" || isAccessDenied(error),
|
|
67
|
-
() => Effect.undefined,
|
|
68
|
-
),
|
|
69
|
-
)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
const isDir = Effect.fn("FileSystem.isDir")(function* (path: string) {
|
|
73
|
-
const info = yield* fs.stat(path).pipe(Effect.catch(() => Effect.void))
|
|
74
|
-
return info?.type === "Directory"
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
const isFile = Effect.fn("FileSystem.isFile")(function* (path: string) {
|
|
78
|
-
const info = yield* fs.stat(path).pipe(Effect.catch(() => Effect.void))
|
|
79
|
-
return info?.type === "File"
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
const resolve = Effect.fnUntraced(function* (input: string) {
|
|
83
|
-
const resolved = path.resolve(windowsPath(input))
|
|
84
|
-
// Sandboxes deny reads outside granted paths; canonicalize what we can see.
|
|
85
|
-
return yield* fs.realPath(resolved).pipe(
|
|
86
|
-
Effect.map((real) =>
|
|
87
|
-
process.platform === "win32" && real.toLowerCase() === resolved.toLowerCase() ? resolved : real,
|
|
88
|
-
),
|
|
89
|
-
Effect.catchIf(
|
|
90
|
-
(error) => error.reason._tag === "NotFound" || isAccessDenied(error),
|
|
91
|
-
() => Effect.succeed(resolved),
|
|
92
|
-
),
|
|
93
|
-
Effect.orDie,
|
|
94
|
-
)
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
const readJson = Effect.fn("FileSystem.readJson")(function* (path: string) {
|
|
98
|
-
const text = yield* fs.readFileString(path)
|
|
99
|
-
return yield* Effect.try({
|
|
100
|
-
try: () => JSON.parse(text),
|
|
101
|
-
catch: (cause) => new FileSystemError({ method: "readJson", cause }),
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
const writeJson = Effect.fn("FileSystem.writeJson")(function* (path: string, data: unknown, mode?: number) {
|
|
106
|
-
const content = JSON.stringify(data, null, 2)
|
|
107
|
-
yield* fs.writeFileString(path, content)
|
|
108
|
-
if (mode) yield* fs.chmod(path, mode)
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
const ensureDir = Effect.fn("FileSystem.ensureDir")(function* (path: string) {
|
|
112
|
-
yield* fs.makeDirectory(path, { recursive: true }).pipe(
|
|
113
|
-
// Bun on Windows can throw EEXIST here despite recursive mode.
|
|
114
|
-
// https://github.com/oven-sh/bun/issues/21901
|
|
115
|
-
Effect.catchIf(
|
|
116
|
-
(error) => error.reason._tag === "AlreadyExists",
|
|
117
|
-
(error) => isDir(path).pipe(Effect.flatMap((exists) => (exists ? Effect.void : Effect.fail(error)))),
|
|
118
|
-
),
|
|
119
|
-
)
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
const writeWithDirs = Effect.fn("FileSystem.writeWithDirs")(function* (
|
|
123
|
-
path: string,
|
|
124
|
-
content: string | Uint8Array,
|
|
125
|
-
mode?: number,
|
|
126
|
-
) {
|
|
127
|
-
const write = typeof content === "string" ? fs.writeFileString(path, content) : fs.writeFile(path, content)
|
|
128
|
-
|
|
129
|
-
yield* write.pipe(
|
|
130
|
-
Effect.catchIf(
|
|
131
|
-
(e) => e.reason._tag === "NotFound",
|
|
132
|
-
() =>
|
|
133
|
-
Effect.gen(function* () {
|
|
134
|
-
yield* fs.makeDirectory(dirname(path), { recursive: true })
|
|
135
|
-
yield* write
|
|
136
|
-
}),
|
|
137
|
-
),
|
|
138
|
-
)
|
|
139
|
-
if (mode) yield* fs.chmod(path, mode)
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
const scan = Effect.fn("FileSystem.scan")(function* (pattern: string, options?: Glob.Options) {
|
|
143
|
-
return yield* Effect.tryPromise({
|
|
144
|
-
try: () => Glob.scan(pattern, options),
|
|
145
|
-
catch: (cause) => new FileSystemError({ method: "glob", cause }),
|
|
146
|
-
})
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
const up = Effect.fn("FileSystem.up")(function* (options: UpOptions) {
|
|
150
|
-
const result: string[] = []
|
|
151
|
-
let current = options.start
|
|
152
|
-
while (true) {
|
|
153
|
-
for (const target of options.targets) {
|
|
154
|
-
const search = join(current, target)
|
|
155
|
-
const found =
|
|
156
|
-
options.type === "file"
|
|
157
|
-
? yield* isFile(search)
|
|
158
|
-
: options.type === "directory"
|
|
159
|
-
? yield* isDir(search)
|
|
160
|
-
: yield* fs.exists(search)
|
|
161
|
-
if (found) {
|
|
162
|
-
result.push(search)
|
|
163
|
-
if (options.mode === "first") return result
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
if (options.stop && sameDirectory(options.stop, current)) break
|
|
167
|
-
const parent = dirname(current)
|
|
168
|
-
if (parent === current) break
|
|
169
|
-
current = parent
|
|
170
|
-
}
|
|
171
|
-
return result
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
const findUp = Effect.fn("FileSystem.findUp")((target: string, start: string, stop?: string) =>
|
|
175
|
-
up({ targets: [target], start, stop }),
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
const globUp = Effect.fn("FileSystem.globUp")(function* (pattern: string, start: string, stop?: string) {
|
|
179
|
-
const result: string[] = []
|
|
180
|
-
let current = start
|
|
181
|
-
while (true) {
|
|
182
|
-
const matches = yield* scan(pattern, { cwd: current, absolute: true, include: "file", dot: true }).pipe(
|
|
183
|
-
Effect.orElseSucceed(() => [] as string[]),
|
|
184
|
-
)
|
|
185
|
-
result.push(...matches)
|
|
186
|
-
if (stop && sameDirectory(stop, current)) break
|
|
187
|
-
const parent = dirname(current)
|
|
188
|
-
if (parent === current) break
|
|
189
|
-
current = parent
|
|
190
|
-
}
|
|
191
|
-
return result
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
return Service.of({
|
|
195
|
-
...fs,
|
|
196
|
-
existsSafe,
|
|
197
|
-
readFileStringSafe,
|
|
198
|
-
isDir,
|
|
199
|
-
isFile,
|
|
200
|
-
resolve,
|
|
201
|
-
readJson,
|
|
202
|
-
writeJson,
|
|
203
|
-
ensureDir,
|
|
204
|
-
writeWithDirs,
|
|
205
|
-
findUp,
|
|
206
|
-
up,
|
|
207
|
-
globUp,
|
|
208
|
-
scan,
|
|
209
|
-
globMatch: Glob.match,
|
|
210
|
-
})
|
|
211
|
-
}),
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
export const node = makeGlobalNode({ service: Service, layer: layer, deps: [filesystem] })
|
|
215
|
-
|
|
216
|
-
/** Permission denial, including macOS sandbox and privacy denials that surface as Unknown with an EPERM cause. */
|
|
217
|
-
export function isAccessDenied(error: PlatformError) {
|
|
218
|
-
if (error.reason._tag === "PermissionDenied") return true
|
|
219
|
-
const cause = error.reason.cause
|
|
220
|
-
return (
|
|
221
|
-
error.reason._tag === "Unknown" && cause instanceof globalThis.Error && "code" in cause && cause.code === "EPERM"
|
|
222
|
-
)
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// Pure helpers that don't need Effect (path manipulation, sync operations)
|
|
226
|
-
export function mimeType(p: string): string {
|
|
227
|
-
return lookup(p) || "application/octet-stream"
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export function normalizePath(p: string): string {
|
|
231
|
-
if (process.platform !== "win32") return p
|
|
232
|
-
const resolved = path.resolve(windowsPath(p))
|
|
233
|
-
try {
|
|
234
|
-
return realpathSync.native(resolved)
|
|
235
|
-
} catch {
|
|
236
|
-
return resolved
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export function resolve(p: string): string {
|
|
241
|
-
const resolved = path.resolve(windowsPath(p))
|
|
242
|
-
try {
|
|
243
|
-
return normalizePath(realpathSync(resolved))
|
|
244
|
-
} catch (e: any) {
|
|
245
|
-
if (["ENOENT", "ENOTDIR", "EPERM", "EACCES"].includes(e?.code)) return normalizePath(resolved)
|
|
246
|
-
throw e
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export function windowsPath(p: string): string {
|
|
251
|
-
if (process.platform !== "win32") return p
|
|
252
|
-
return p
|
|
253
|
-
.replace(/^\/([a-zA-Z]):(?:[\\/]|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
254
|
-
.replace(/^\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
255
|
-
.replace(/^\/cygdrive\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
256
|
-
.replace(/^\/mnt\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
export const ignoredDirectories = ["node_modules", ".git", ".venv", "venv", "__pycache__"]
|
|
260
|
-
|
|
261
|
-
export const ignoreGlobs = [...ignoredDirectories, `**/{${ignoredDirectories.join(",")}}/**`]
|
|
262
|
-
|
|
263
|
-
export function overlaps(a: string, b: string) {
|
|
264
|
-
return contains(a, b) || contains(b, a)
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export function contains(parent: string, child: string, platform: PlatformPath = path) {
|
|
268
|
-
const result = platform.relative(parent, child)
|
|
269
|
-
return result === "" || (!platform.isAbsolute(result) && result !== ".." && !result.startsWith(`..${platform.sep}`))
|
|
270
|
-
}
|
|
271
|
-
}
|
|
1
|
+
import path, { dirname, join, type PlatformPath } from "path"
|
|
2
|
+
import { realpathSync } from "fs"
|
|
3
|
+
import { lookup } from "mime-types"
|
|
4
|
+
import { Context, Effect, FileSystem, Layer, Schema } from "effect"
|
|
5
|
+
import type { PlatformError } from "effect/PlatformError"
|
|
6
|
+
import { Glob } from "./glob.js"
|
|
7
|
+
import { makeGlobalNode } from "./effect/app-node.js"
|
|
8
|
+
import { filesystem } from "./effect/app-node-platform.js"
|
|
9
|
+
import { sameDirectory } from "./path.js"
|
|
10
|
+
|
|
11
|
+
export namespace FSUtil {
|
|
12
|
+
export class FileSystemError extends Schema.TaggedError<FileSystemError>()("FileSystemError", {
|
|
13
|
+
method: Schema.String,
|
|
14
|
+
cause: Schema.optional(Schema.Defect()),
|
|
15
|
+
}) {
|
|
16
|
+
override get message() {
|
|
17
|
+
const detail = this.cause instanceof Error ? this.cause.message : this.cause && String(this.cause)
|
|
18
|
+
return `Filesystem operation failed: ${this.method}${detail ? `: ${detail}` : ""}`
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type Error = PlatformError | FileSystemError
|
|
23
|
+
|
|
24
|
+
export interface UpOptions {
|
|
25
|
+
readonly targets: string[]
|
|
26
|
+
readonly start: string
|
|
27
|
+
readonly stop?: string
|
|
28
|
+
readonly mode?: "all" | "first"
|
|
29
|
+
/** Only match regular files or directories (following symlinks). By default any existing path matches. */
|
|
30
|
+
readonly type?: "file" | "directory"
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface Interface extends FileSystem.FileSystem {
|
|
34
|
+
readonly isDir: (path: string) => Effect.Effect<boolean>
|
|
35
|
+
readonly isFile: (path: string) => Effect.Effect<boolean>
|
|
36
|
+
readonly existsSafe: (path: string) => Effect.Effect<boolean>
|
|
37
|
+
readonly readFileStringSafe: (path: string) => Effect.Effect<string | undefined, Error>
|
|
38
|
+
readonly readJson: (path: string) => Effect.Effect<unknown, Error>
|
|
39
|
+
readonly writeJson: (path: string, data: unknown, mode?: number) => Effect.Effect<void, Error>
|
|
40
|
+
readonly ensureDir: (path: string) => Effect.Effect<void, Error>
|
|
41
|
+
readonly writeWithDirs: (path: string, content: string | Uint8Array, mode?: number) => Effect.Effect<void, Error>
|
|
42
|
+
readonly resolve: (path: string) => Effect.Effect<string>
|
|
43
|
+
readonly findUp: (target: string, start: string, stop?: string) => Effect.Effect<string[], Error>
|
|
44
|
+
readonly up: (options: UpOptions) => Effect.Effect<string[], Error>
|
|
45
|
+
readonly globUp: (pattern: string, start: string, stop?: string) => Effect.Effect<string[], Error>
|
|
46
|
+
readonly scan: (pattern: string, options?: Glob.Options) => Effect.Effect<string[], Error>
|
|
47
|
+
readonly globMatch: (pattern: string, filepath: string) => boolean
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class Service extends Context.Service<Service, Interface>()("@zyro-sec/FSUtil") {}
|
|
51
|
+
|
|
52
|
+
// Exported so simulation can wrap this layer and override the methods that
|
|
53
|
+
// bypass the injected FileSystem (scan, globUp).
|
|
54
|
+
export const layer = Layer.effect(
|
|
55
|
+
Service,
|
|
56
|
+
Effect.gen(function* () {
|
|
57
|
+
const fs = yield* FileSystem.FileSystem
|
|
58
|
+
|
|
59
|
+
const existsSafe = Effect.fn("FileSystem.existsSafe")(function* (path: string) {
|
|
60
|
+
return yield* fs.exists(path).pipe(Effect.orElseSucceed(() => false))
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
const readFileStringSafe = Effect.fn("FileSystem.readFileStringSafe")(function* (path: string) {
|
|
64
|
+
return yield* fs.readFileString(path).pipe(
|
|
65
|
+
Effect.catchIf(
|
|
66
|
+
(error) => error.reason._tag === "NotFound" || isAccessDenied(error),
|
|
67
|
+
() => Effect.undefined,
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const isDir = Effect.fn("FileSystem.isDir")(function* (path: string) {
|
|
73
|
+
const info = yield* fs.stat(path).pipe(Effect.catch(() => Effect.void))
|
|
74
|
+
return info?.type === "Directory"
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
const isFile = Effect.fn("FileSystem.isFile")(function* (path: string) {
|
|
78
|
+
const info = yield* fs.stat(path).pipe(Effect.catch(() => Effect.void))
|
|
79
|
+
return info?.type === "File"
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const resolve = Effect.fnUntraced(function* (input: string) {
|
|
83
|
+
const resolved = path.resolve(windowsPath(input))
|
|
84
|
+
// Sandboxes deny reads outside granted paths; canonicalize what we can see.
|
|
85
|
+
return yield* fs.realPath(resolved).pipe(
|
|
86
|
+
Effect.map((real) =>
|
|
87
|
+
process.platform === "win32" && real.toLowerCase() === resolved.toLowerCase() ? resolved : real,
|
|
88
|
+
),
|
|
89
|
+
Effect.catchIf(
|
|
90
|
+
(error) => error.reason._tag === "NotFound" || isAccessDenied(error),
|
|
91
|
+
() => Effect.succeed(resolved),
|
|
92
|
+
),
|
|
93
|
+
Effect.orDie,
|
|
94
|
+
)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
const readJson = Effect.fn("FileSystem.readJson")(function* (path: string) {
|
|
98
|
+
const text = yield* fs.readFileString(path)
|
|
99
|
+
return yield* Effect.try({
|
|
100
|
+
try: () => JSON.parse(text),
|
|
101
|
+
catch: (cause) => new FileSystemError({ method: "readJson", cause }),
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const writeJson = Effect.fn("FileSystem.writeJson")(function* (path: string, data: unknown, mode?: number) {
|
|
106
|
+
const content = JSON.stringify(data, null, 2)
|
|
107
|
+
yield* fs.writeFileString(path, content)
|
|
108
|
+
if (mode) yield* fs.chmod(path, mode)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const ensureDir = Effect.fn("FileSystem.ensureDir")(function* (path: string) {
|
|
112
|
+
yield* fs.makeDirectory(path, { recursive: true }).pipe(
|
|
113
|
+
// Bun on Windows can throw EEXIST here despite recursive mode.
|
|
114
|
+
// https://github.com/oven-sh/bun/issues/21901
|
|
115
|
+
Effect.catchIf(
|
|
116
|
+
(error) => error.reason._tag === "AlreadyExists",
|
|
117
|
+
(error) => isDir(path).pipe(Effect.flatMap((exists) => (exists ? Effect.void : Effect.fail(error)))),
|
|
118
|
+
),
|
|
119
|
+
)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
const writeWithDirs = Effect.fn("FileSystem.writeWithDirs")(function* (
|
|
123
|
+
path: string,
|
|
124
|
+
content: string | Uint8Array,
|
|
125
|
+
mode?: number,
|
|
126
|
+
) {
|
|
127
|
+
const write = typeof content === "string" ? fs.writeFileString(path, content) : fs.writeFile(path, content)
|
|
128
|
+
|
|
129
|
+
yield* write.pipe(
|
|
130
|
+
Effect.catchIf(
|
|
131
|
+
(e) => e.reason._tag === "NotFound",
|
|
132
|
+
() =>
|
|
133
|
+
Effect.gen(function* () {
|
|
134
|
+
yield* fs.makeDirectory(dirname(path), { recursive: true })
|
|
135
|
+
yield* write
|
|
136
|
+
}),
|
|
137
|
+
),
|
|
138
|
+
)
|
|
139
|
+
if (mode) yield* fs.chmod(path, mode)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
const scan = Effect.fn("FileSystem.scan")(function* (pattern: string, options?: Glob.Options) {
|
|
143
|
+
return yield* Effect.tryPromise({
|
|
144
|
+
try: () => Glob.scan(pattern, options),
|
|
145
|
+
catch: (cause) => new FileSystemError({ method: "glob", cause }),
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
const up = Effect.fn("FileSystem.up")(function* (options: UpOptions) {
|
|
150
|
+
const result: string[] = []
|
|
151
|
+
let current = options.start
|
|
152
|
+
while (true) {
|
|
153
|
+
for (const target of options.targets) {
|
|
154
|
+
const search = join(current, target)
|
|
155
|
+
const found =
|
|
156
|
+
options.type === "file"
|
|
157
|
+
? yield* isFile(search)
|
|
158
|
+
: options.type === "directory"
|
|
159
|
+
? yield* isDir(search)
|
|
160
|
+
: yield* fs.exists(search)
|
|
161
|
+
if (found) {
|
|
162
|
+
result.push(search)
|
|
163
|
+
if (options.mode === "first") return result
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (options.stop && sameDirectory(options.stop, current)) break
|
|
167
|
+
const parent = dirname(current)
|
|
168
|
+
if (parent === current) break
|
|
169
|
+
current = parent
|
|
170
|
+
}
|
|
171
|
+
return result
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
const findUp = Effect.fn("FileSystem.findUp")((target: string, start: string, stop?: string) =>
|
|
175
|
+
up({ targets: [target], start, stop }),
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
const globUp = Effect.fn("FileSystem.globUp")(function* (pattern: string, start: string, stop?: string) {
|
|
179
|
+
const result: string[] = []
|
|
180
|
+
let current = start
|
|
181
|
+
while (true) {
|
|
182
|
+
const matches = yield* scan(pattern, { cwd: current, absolute: true, include: "file", dot: true }).pipe(
|
|
183
|
+
Effect.orElseSucceed(() => [] as string[]),
|
|
184
|
+
)
|
|
185
|
+
result.push(...matches)
|
|
186
|
+
if (stop && sameDirectory(stop, current)) break
|
|
187
|
+
const parent = dirname(current)
|
|
188
|
+
if (parent === current) break
|
|
189
|
+
current = parent
|
|
190
|
+
}
|
|
191
|
+
return result
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
return Service.of({
|
|
195
|
+
...fs,
|
|
196
|
+
existsSafe,
|
|
197
|
+
readFileStringSafe,
|
|
198
|
+
isDir,
|
|
199
|
+
isFile,
|
|
200
|
+
resolve,
|
|
201
|
+
readJson,
|
|
202
|
+
writeJson,
|
|
203
|
+
ensureDir,
|
|
204
|
+
writeWithDirs,
|
|
205
|
+
findUp,
|
|
206
|
+
up,
|
|
207
|
+
globUp,
|
|
208
|
+
scan,
|
|
209
|
+
globMatch: Glob.match,
|
|
210
|
+
})
|
|
211
|
+
}),
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
export const node = makeGlobalNode({ service: Service, layer: layer, deps: [filesystem] })
|
|
215
|
+
|
|
216
|
+
/** Permission denial, including macOS sandbox and privacy denials that surface as Unknown with an EPERM cause. */
|
|
217
|
+
export function isAccessDenied(error: PlatformError) {
|
|
218
|
+
if (error.reason._tag === "PermissionDenied") return true
|
|
219
|
+
const cause = error.reason.cause
|
|
220
|
+
return (
|
|
221
|
+
error.reason._tag === "Unknown" && cause instanceof globalThis.Error && "code" in cause && cause.code === "EPERM"
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Pure helpers that don't need Effect (path manipulation, sync operations)
|
|
226
|
+
export function mimeType(p: string): string {
|
|
227
|
+
return lookup(p) || "application/octet-stream"
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function normalizePath(p: string): string {
|
|
231
|
+
if (process.platform !== "win32") return p
|
|
232
|
+
const resolved = path.resolve(windowsPath(p))
|
|
233
|
+
try {
|
|
234
|
+
return realpathSync.native(resolved)
|
|
235
|
+
} catch {
|
|
236
|
+
return resolved
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function resolve(p: string): string {
|
|
241
|
+
const resolved = path.resolve(windowsPath(p))
|
|
242
|
+
try {
|
|
243
|
+
return normalizePath(realpathSync(resolved))
|
|
244
|
+
} catch (e: any) {
|
|
245
|
+
if (["ENOENT", "ENOTDIR", "EPERM", "EACCES"].includes(e?.code)) return normalizePath(resolved)
|
|
246
|
+
throw e
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function windowsPath(p: string): string {
|
|
251
|
+
if (process.platform !== "win32") return p
|
|
252
|
+
return p
|
|
253
|
+
.replace(/^\/([a-zA-Z]):(?:[\\/]|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
254
|
+
.replace(/^\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
255
|
+
.replace(/^\/cygdrive\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
256
|
+
.replace(/^\/mnt\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export const ignoredDirectories = ["node_modules", ".git", ".venv", "venv", "__pycache__"]
|
|
260
|
+
|
|
261
|
+
export const ignoreGlobs = [...ignoredDirectories, `**/{${ignoredDirectories.join(",")}}/**`]
|
|
262
|
+
|
|
263
|
+
export function overlaps(a: string, b: string) {
|
|
264
|
+
return contains(a, b) || contains(b, a)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function contains(parent: string, child: string, platform: PlatformPath = path) {
|
|
268
|
+
const result = platform.relative(parent, child)
|
|
269
|
+
return result === "" || (!platform.isAbsolute(result) && result !== ".." && !result.startsWith(`..${platform.sep}`))
|
|
270
|
+
}
|
|
271
|
+
}
|
package/src/glob.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { glob, type GlobOptionsWithFileTypesFalse } from "glob"
|
|
2
|
-
import { Minimatch, minimatch } from "minimatch"
|
|
3
|
-
|
|
4
|
-
export namespace Glob {
|
|
5
|
-
export interface Options {
|
|
6
|
-
cwd?: string
|
|
7
|
-
absolute?: boolean
|
|
8
|
-
include?: "file" | "all"
|
|
9
|
-
dot?: boolean
|
|
10
|
-
symlink?: boolean
|
|
11
|
-
ignore?: string[]
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function toGlobOptions(options: Options): GlobOptionsWithFileTypesFalse {
|
|
15
|
-
return {
|
|
16
|
-
cwd: options.cwd,
|
|
17
|
-
absolute: options.absolute,
|
|
18
|
-
dot: options.dot,
|
|
19
|
-
follow: options.symlink ?? false,
|
|
20
|
-
nodir: options.include !== "all",
|
|
21
|
-
ignore: options.ignore,
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export async function scan(pattern: string, options: Options = {}): Promise<string[]> {
|
|
26
|
-
return glob(pattern, toGlobOptions(options))
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function match(pattern: string, filepath: string, options: { dot?: boolean } = {}): boolean {
|
|
30
|
-
return minimatch(filepath, pattern, { dot: options.dot ?? true })
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** `match` for one pattern, compiled on first use and reused for every later path. */
|
|
34
|
-
export function matcher(pattern: string, options: { dot?: boolean } = {}): (filepath: string) => boolean {
|
|
35
|
-
let compiled: Minimatch | undefined
|
|
36
|
-
return (filepath) => (compiled ??= new Minimatch(pattern, { dot: options.dot ?? true })).match(filepath)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
import { glob, type GlobOptionsWithFileTypesFalse } from "glob"
|
|
2
|
+
import { Minimatch, minimatch } from "minimatch"
|
|
3
|
+
|
|
4
|
+
export namespace Glob {
|
|
5
|
+
export interface Options {
|
|
6
|
+
cwd?: string
|
|
7
|
+
absolute?: boolean
|
|
8
|
+
include?: "file" | "all"
|
|
9
|
+
dot?: boolean
|
|
10
|
+
symlink?: boolean
|
|
11
|
+
ignore?: string[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function toGlobOptions(options: Options): GlobOptionsWithFileTypesFalse {
|
|
15
|
+
return {
|
|
16
|
+
cwd: options.cwd,
|
|
17
|
+
absolute: options.absolute,
|
|
18
|
+
dot: options.dot,
|
|
19
|
+
follow: options.symlink ?? false,
|
|
20
|
+
nodir: options.include !== "all",
|
|
21
|
+
ignore: options.ignore,
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function scan(pattern: string, options: Options = {}): Promise<string[]> {
|
|
26
|
+
return glob(pattern, toGlobOptions(options))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function match(pattern: string, filepath: string, options: { dot?: boolean } = {}): boolean {
|
|
30
|
+
return minimatch(filepath, pattern, { dot: options.dot ?? true })
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** `match` for one pattern, compiled on first use and reused for every later path. */
|
|
34
|
+
export function matcher(pattern: string, options: { dot?: boolean } = {}): (filepath: string) => boolean {
|
|
35
|
+
let compiled: Minimatch | undefined
|
|
36
|
+
return (filepath) => (compiled ??= new Minimatch(pattern, { dot: options.dot ?? true })).match(filepath)
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/global-roots.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import os from "os"
|
|
2
|
-
import path from "path"
|
|
3
|
-
|
|
4
|
-
const home = os.homedir()
|
|
5
|
-
const data = process.env.XDG_DATA_HOME || (home ? path.join(home, ".local", "share") : undefined)
|
|
6
|
-
const cache = process.env.XDG_CACHE_HOME || (home ? path.join(home, ".cache") : undefined)
|
|
7
|
-
const config = process.env.XDG_CONFIG_HOME || (home ? path.join(home, ".config") : undefined)
|
|
8
|
-
const state = process.env.XDG_STATE_HOME || (home ? path.join(home, ".local", "state") : undefined)
|
|
9
|
-
|
|
10
|
-
/** The XDG base directories that root Zyro Sec's global paths. */
|
|
11
|
-
export function roots(app: string) {
|
|
12
|
-
return {
|
|
13
|
-
data: path.join(data!, app),
|
|
14
|
-
cache: path.join(cache!, app),
|
|
15
|
-
config: path.join(config!, app),
|
|
16
|
-
state: path.join(state!, app),
|
|
17
|
-
tmp: path.join(os.tmpdir(), app),
|
|
18
|
-
}
|
|
19
|
-
}
|
|
1
|
+
import os from "os"
|
|
2
|
+
import path from "path"
|
|
3
|
+
|
|
4
|
+
const home = os.homedir()
|
|
5
|
+
const data = process.env.XDG_DATA_HOME || (home ? path.join(home, ".local", "share") : undefined)
|
|
6
|
+
const cache = process.env.XDG_CACHE_HOME || (home ? path.join(home, ".cache") : undefined)
|
|
7
|
+
const config = process.env.XDG_CONFIG_HOME || (home ? path.join(home, ".config") : undefined)
|
|
8
|
+
const state = process.env.XDG_STATE_HOME || (home ? path.join(home, ".local", "state") : undefined)
|
|
9
|
+
|
|
10
|
+
/** The XDG base directories that root Zyro Sec's global paths. */
|
|
11
|
+
export function roots(app: string) {
|
|
12
|
+
return {
|
|
13
|
+
data: path.join(data!, app),
|
|
14
|
+
cache: path.join(cache!, app),
|
|
15
|
+
config: path.join(config!, app),
|
|
16
|
+
state: path.join(state!, app),
|
|
17
|
+
tmp: path.join(os.tmpdir(), app),
|
|
18
|
+
}
|
|
19
|
+
}
|