@radheim/util 0.0.0-stage → 2.4.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/dist/abort.d.ts +1 -0
- package/dist/abort.js +8 -0
- package/dist/activity-calendar.d.ts +21 -0
- package/dist/activity-calendar.js +42 -0
- package/dist/artifact.d.ts +4 -0
- package/dist/artifact.js +85 -0
- package/dist/base64.d.ts +4 -0
- package/dist/base64.js +24 -0
- package/dist/binary.d.ts +7 -0
- package/dist/binary.js +34 -0
- package/dist/bom.d.ts +22 -0
- package/dist/bom.js +36 -0
- package/dist/cross-spawn-spawner.d.ts +9 -0
- package/dist/cross-spawn-spawner.js +548 -0
- package/dist/duration.d.ts +2 -0
- package/dist/duration.js +22 -0
- package/dist/effect/app-node-platform.d.ts +13 -0
- package/dist/effect/app-node-platform.js +29 -0
- package/dist/effect/app-node.d.ts +16 -0
- package/dist/effect/app-node.js +8 -0
- package/dist/effect/layer-node.d.ts +178 -0
- package/dist/effect/layer-node.js +248 -0
- package/dist/effect-flock.d.ts +31 -0
- package/dist/effect-flock.js +186 -0
- package/dist/encode.d.ts +4 -0
- package/dist/encode.js +38 -0
- package/dist/execute-output.d.ts +10 -0
- package/dist/execute-output.js +22 -0
- package/dist/flock.d.ts +28 -0
- package/dist/flock.js +267 -0
- package/dist/fs-util.d.ts +54 -0
- package/dist/fs-util.js +200 -0
- package/dist/glob.d.ts +18 -0
- package/dist/glob.js +29 -0
- package/dist/global-roots.d.ts +8 -0
- package/dist/global-roots.js +17 -0
- package/dist/global-roots.workerd.d.ts +7 -0
- package/dist/global-roots.workerd.js +15 -0
- package/dist/global.d.ts +30 -0
- package/dist/global.js +54 -0
- package/dist/hash.d.ts +4 -0
- package/dist/hash.js +12 -0
- package/dist/heap-snapshot.d.ts +19 -0
- package/dist/heap-snapshot.js +38 -0
- package/dist/no-timeout-fetch.d.ts +1 -0
- package/dist/no-timeout-fetch.js +4 -0
- package/dist/npm-config.d.ts +4 -0
- package/dist/npm-config.js +34 -0
- package/dist/npm.d.ts +45 -0
- package/dist/npm.js +441 -0
- package/dist/observability/logging.d.ts +14 -0
- package/dist/observability/logging.js +151 -0
- package/dist/observability/otlp.d.ts +19 -0
- package/dist/observability/otlp.js +63 -0
- package/dist/observability/shared.d.ts +1 -0
- package/dist/observability/shared.js +7 -0
- package/dist/observability/tracing.d.ts +2 -0
- package/dist/observability/tracing.js +3 -0
- package/dist/observability.d.ts +13 -0
- package/dist/observability.js +37 -0
- package/dist/open.d.ts +2 -0
- package/dist/open.js +10 -0
- package/dist/patch.d.ts +43 -0
- package/dist/patch.js +332 -0
- package/dist/path.d.ts +20 -0
- package/dist/path.js +134 -0
- package/dist/process-signal.d.ts +5 -0
- package/dist/process-signal.js +22 -0
- package/dist/process.d.ts +54 -0
- package/dist/process.js +167 -0
- package/dist/qr.d.ts +15 -0
- package/dist/qr.js +295 -0
- package/dist/record.d.ts +2 -0
- package/dist/record.js +2 -0
- package/dist/retry.d.ts +8 -0
- package/dist/retry.js +45 -0
- package/dist/runtime/import.bun.d.ts +2 -0
- package/dist/runtime/import.bun.js +8 -0
- package/dist/runtime/import.node.d.ts +2 -0
- package/dist/runtime/import.node.js +62 -0
- package/dist/runtime/import.workerd.d.ts +2 -0
- package/dist/runtime/import.workerd.js +7 -0
- package/dist/runtime-import.d.ts +1 -0
- package/dist/runtime-import.js +1 -0
- package/dist/session-mention.d.ts +11 -0
- package/dist/session-mention.js +18 -0
- package/dist/session-title-fallback.d.ts +16 -0
- package/dist/session-title-fallback.js +23 -0
- package/dist/session-turn.d.ts +37 -0
- package/dist/session-turn.js +56 -0
- package/dist/teardown.d.ts +8 -0
- package/dist/teardown.js +16 -0
- package/dist/text-boundary.d.ts +11 -0
- package/dist/text-boundary.js +22 -0
- package/dist/uuid.d.ts +1 -0
- package/dist/uuid.js +14 -0
- package/package.json +71 -4
- package/README.md +0 -3
package/dist/path.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
export function getFilename(path) {
|
|
2
|
+
if (!path)
|
|
3
|
+
return "";
|
|
4
|
+
const trimmed = path.replace(/[/\\]+$/, "");
|
|
5
|
+
const index = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
|
|
6
|
+
return trimmed.slice(index + 1);
|
|
7
|
+
}
|
|
8
|
+
export function getDirectory(path) {
|
|
9
|
+
if (!path)
|
|
10
|
+
return "";
|
|
11
|
+
const trimmed = path.replace(/[/\\]+$/, "");
|
|
12
|
+
const parts = trimmed.split(/[/\\]/);
|
|
13
|
+
return parts.slice(0, parts.length - 1).join("/") + "/";
|
|
14
|
+
}
|
|
15
|
+
export function getFilenameTruncated(path, maxLength = 20) {
|
|
16
|
+
const filename = getFilename(path);
|
|
17
|
+
if (filename.length <= maxLength)
|
|
18
|
+
return filename;
|
|
19
|
+
const lastDot = filename.lastIndexOf(".");
|
|
20
|
+
const extension = lastDot <= 0 ? "" : filename.slice(lastDot);
|
|
21
|
+
const available = maxLength - extension.length - 1;
|
|
22
|
+
if (available <= 0)
|
|
23
|
+
return filename.slice(0, maxLength - 1) + "…";
|
|
24
|
+
return filename.slice(0, available) + "…" + extension;
|
|
25
|
+
}
|
|
26
|
+
export function truncateMiddle(text, maxLength = 20) {
|
|
27
|
+
if (maxLength <= 0)
|
|
28
|
+
return "";
|
|
29
|
+
if (text.length <= maxLength)
|
|
30
|
+
return text;
|
|
31
|
+
const available = maxLength - 1;
|
|
32
|
+
const start = Math.ceil(available / 2);
|
|
33
|
+
const end = Math.floor(available / 2);
|
|
34
|
+
if (end <= 0)
|
|
35
|
+
return text.slice(0, start) + "…";
|
|
36
|
+
return text.slice(0, start) + "…" + text.slice(-end);
|
|
37
|
+
}
|
|
38
|
+
const isDrive = (value) => {
|
|
39
|
+
if (value.length !== 2)
|
|
40
|
+
return false;
|
|
41
|
+
const code = value.charCodeAt(0);
|
|
42
|
+
return value[1] === ":" && ((code >= 65 && code <= 90) || (code >= 97 && code <= 122));
|
|
43
|
+
};
|
|
44
|
+
const trimTrailingSlashes = (value) => {
|
|
45
|
+
for (let i = value.length - 1; i >= 0; i--) {
|
|
46
|
+
if (value[i] !== "/")
|
|
47
|
+
return value.slice(0, i + 1);
|
|
48
|
+
}
|
|
49
|
+
return "";
|
|
50
|
+
};
|
|
51
|
+
const isWindowsPath = (value) => value[1] === ":" || value.startsWith("\\\\");
|
|
52
|
+
/** A comparable form of a directory path: forward slashes for Windows paths, no trailing slash except on roots. */
|
|
53
|
+
export function comparablePath(path) {
|
|
54
|
+
const value = isWindowsPath(path) ? path.replaceAll("\\", "/") : path;
|
|
55
|
+
const trimmed = trimTrailingSlashes(value);
|
|
56
|
+
if (!trimmed && value.startsWith("/"))
|
|
57
|
+
return "/";
|
|
58
|
+
if (isDrive(trimmed))
|
|
59
|
+
return `${trimmed}/`;
|
|
60
|
+
return trimmed;
|
|
61
|
+
}
|
|
62
|
+
/** Like `comparablePath`, plus case-insensitive normalization for Windows drive and UNC paths. */
|
|
63
|
+
export function comparablePathKey(path) {
|
|
64
|
+
const key = comparablePath(path);
|
|
65
|
+
return /^[a-z]:\//i.test(key) || key.startsWith("//") ? key.toLowerCase() : key;
|
|
66
|
+
}
|
|
67
|
+
/** `child` is `parent` or inside it. Windows drive and UNC paths compare case-insensitively. */
|
|
68
|
+
export function containsDirectory(parent, child) {
|
|
69
|
+
const root = comparablePathKey(parent);
|
|
70
|
+
const target = comparablePathKey(child);
|
|
71
|
+
return target === root || target.startsWith(root.endsWith("/") ? root : `${root}/`);
|
|
72
|
+
}
|
|
73
|
+
export function sameDirectory(a, b) {
|
|
74
|
+
return containsDirectory(a, b) && containsDirectory(b, a);
|
|
75
|
+
}
|
|
76
|
+
export function encodeFilePath(filepath) {
|
|
77
|
+
// Normalize Windows paths: convert backslashes to forward slashes
|
|
78
|
+
const normalized = filepath.replace(/\\/g, "/");
|
|
79
|
+
// Handle Windows absolute paths (D:/path -> /D:/path for proper file:// URLs)
|
|
80
|
+
const rooted = /^[A-Za-z]:/.test(normalized) ? "/" + normalized : normalized;
|
|
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))
|
|
88
|
+
return segment;
|
|
89
|
+
return encodeURIComponent(segment);
|
|
90
|
+
})
|
|
91
|
+
.join("/");
|
|
92
|
+
}
|
|
93
|
+
const MAX_LINE_NUMBER = 1_000_000;
|
|
94
|
+
const lineHashPattern = /^#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i;
|
|
95
|
+
const lineHashSuffixPattern = /^(.*?)#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i;
|
|
96
|
+
const lineColonSuffixPattern = /^(.*?):(\d+)(?::\d+)?(?:-(\d+)(?::\d+)?)?:?$/;
|
|
97
|
+
function toLineRange(first, second) {
|
|
98
|
+
if (!Number.isSafeInteger(first) ||
|
|
99
|
+
!Number.isSafeInteger(second) ||
|
|
100
|
+
first < 1 ||
|
|
101
|
+
second < 1 ||
|
|
102
|
+
first > MAX_LINE_NUMBER ||
|
|
103
|
+
second > MAX_LINE_NUMBER) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
start: Math.min(first, second),
|
|
108
|
+
end: Math.max(first, second),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export function isLineRangeHash(hash) {
|
|
112
|
+
return lineHashPattern.test(hash);
|
|
113
|
+
}
|
|
114
|
+
export function parsePathLineSuffix(input) {
|
|
115
|
+
const hashMatch = input.match(lineHashSuffixPattern);
|
|
116
|
+
if (hashMatch) {
|
|
117
|
+
const first = Number(hashMatch[2]);
|
|
118
|
+
const second = hashMatch[3] ? Number(hashMatch[3]) : first;
|
|
119
|
+
return {
|
|
120
|
+
path: hashMatch[1] ?? "",
|
|
121
|
+
selection: toLineRange(first, second),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const colonMatch = input.match(lineColonSuffixPattern);
|
|
125
|
+
if (colonMatch && !/^[a-z]:?$/i.test(colonMatch[1] ?? "")) {
|
|
126
|
+
const first = Number(colonMatch[2]);
|
|
127
|
+
const second = colonMatch[3] ? Number(colonMatch[3]) : first;
|
|
128
|
+
return {
|
|
129
|
+
path: colonMatch[1] ?? "",
|
|
130
|
+
selection: toLineRange(first, second),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return { path: input, selection: undefined };
|
|
134
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * as ProcessSignal from "./process-signal.js";
|
|
2
|
+
/** Install once at process startup. Removing subscribers never restores the signal's default action. */
|
|
3
|
+
export declare function install(signal: NodeJS.Signals, unavailable: () => void): void;
|
|
4
|
+
/** Subscribe only to an installed signal. The returned cleanup removes this subscriber, not the process handler. */
|
|
5
|
+
export declare function subscribe(signal: NodeJS.Signals, listener: () => void): () => void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export * as ProcessSignal from "./process-signal.js";
|
|
2
|
+
const handlers = new Map();
|
|
3
|
+
/** Install once at process startup. Removing subscribers never restores the signal's default action. */
|
|
4
|
+
export function install(signal, unavailable) {
|
|
5
|
+
if (handlers.has(signal))
|
|
6
|
+
return;
|
|
7
|
+
const listeners = new Set();
|
|
8
|
+
handlers.set(signal, listeners);
|
|
9
|
+
process.on(signal, () => {
|
|
10
|
+
if (!listeners.size)
|
|
11
|
+
return unavailable();
|
|
12
|
+
listeners.forEach((listener) => listener());
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/** Subscribe only to an installed signal. The returned cleanup removes this subscriber, not the process handler. */
|
|
16
|
+
export function subscribe(signal, listener) {
|
|
17
|
+
const listeners = handlers.get(signal);
|
|
18
|
+
listeners?.add(listener);
|
|
19
|
+
return () => {
|
|
20
|
+
listeners?.delete(listener);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Context, Duration, Effect, Schema, Stream } from "effect";
|
|
2
|
+
import type { PlatformError } from "effect/PlatformError";
|
|
3
|
+
import { ChildProcess } from "effect/process";
|
|
4
|
+
import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
|
|
5
|
+
declare const AppProcessError_base: Schema.Class<AppProcessError, Schema.TaggedStruct<"AppProcessError", {
|
|
6
|
+
readonly command: Schema.String;
|
|
7
|
+
readonly exitCode: Schema.optional<Schema.Number>;
|
|
8
|
+
readonly stderr: Schema.optional<Schema.String>;
|
|
9
|
+
readonly cause: Schema.optional<Schema.Defect>;
|
|
10
|
+
}>, import("effect/Cause").YieldableError>;
|
|
11
|
+
export declare class AppProcessError extends AppProcessError_base {
|
|
12
|
+
get message(): string;
|
|
13
|
+
}
|
|
14
|
+
export interface RunOptions {
|
|
15
|
+
readonly combineOutput?: boolean;
|
|
16
|
+
readonly maxOutputBytes?: number;
|
|
17
|
+
readonly maxErrorBytes?: number;
|
|
18
|
+
readonly signal?: AbortSignal;
|
|
19
|
+
readonly timeout?: Duration.Input;
|
|
20
|
+
readonly stdin?: string | Uint8Array | Stream.Stream<Uint8Array, PlatformError>;
|
|
21
|
+
}
|
|
22
|
+
export interface RunStreamOptions {
|
|
23
|
+
readonly signal?: AbortSignal;
|
|
24
|
+
readonly includeStderr?: boolean;
|
|
25
|
+
readonly okExitCodes?: ReadonlyArray<number>;
|
|
26
|
+
readonly maxErrorBytes?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface RunResult {
|
|
29
|
+
readonly command: string;
|
|
30
|
+
readonly exitCode: number;
|
|
31
|
+
readonly output?: Buffer;
|
|
32
|
+
readonly stdout: Buffer;
|
|
33
|
+
readonly stderr: Buffer;
|
|
34
|
+
readonly outputTruncated?: boolean;
|
|
35
|
+
readonly stdoutTruncated: boolean;
|
|
36
|
+
readonly stderrTruncated: boolean;
|
|
37
|
+
}
|
|
38
|
+
export type Interface = ChildProcessSpawner["Service"] & {
|
|
39
|
+
readonly run: (command: ChildProcess.Command, options?: RunOptions) => Effect.Effect<RunResult, AppProcessError>;
|
|
40
|
+
readonly runStream: (command: ChildProcess.Command, options?: RunStreamOptions) => Stream.Stream<string, AppProcessError>;
|
|
41
|
+
};
|
|
42
|
+
declare const Service_base: Context.ServiceClass<Service, "@radheim/AppProcess", Interface>;
|
|
43
|
+
export declare class Service extends Service_base {
|
|
44
|
+
}
|
|
45
|
+
export declare const requireSuccess: (result: RunResult) => Effect.Effect<RunResult, AppProcessError>;
|
|
46
|
+
export declare const requireExitIn: (codes: ReadonlyArray<number>) => (result: RunResult) => Effect.Effect<RunResult, AppProcessError>;
|
|
47
|
+
export declare const abortError: (signal: AbortSignal) => Error;
|
|
48
|
+
export declare const waitForAbort: (signal: AbortSignal) => Effect.Effect<never, Error, never>;
|
|
49
|
+
export declare const collectStream: (stream: Stream.Stream<Uint8Array, PlatformError>, maxOutputBytes: number | undefined) => Effect.Effect<{
|
|
50
|
+
buffer: Buffer<ArrayBuffer>;
|
|
51
|
+
truncated: boolean;
|
|
52
|
+
}, PlatformError, never>;
|
|
53
|
+
export declare const node: import("./effect/layer-node.js").Provider<Service, never, import("./effect/layer-node.js").Tag<"global">, true>;
|
|
54
|
+
export * as AppProcess from "./process.js";
|
package/dist/process.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Context, Duration, Effect, Fiber, Layer, Schema, Stream } from "effect";
|
|
2
|
+
import { ChildProcess } from "effect/process";
|
|
3
|
+
import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
|
|
4
|
+
import { CrossSpawnSpawner } from "./cross-spawn-spawner.js";
|
|
5
|
+
import { makeGlobalNode } from "./effect/app-node.js";
|
|
6
|
+
export class AppProcessError extends Schema.TaggedError()("AppProcessError", {
|
|
7
|
+
command: Schema.String,
|
|
8
|
+
exitCode: Schema.optional(Schema.Number),
|
|
9
|
+
stderr: Schema.optional(Schema.String),
|
|
10
|
+
cause: Schema.optional(Schema.Defect()),
|
|
11
|
+
}) {
|
|
12
|
+
get message() {
|
|
13
|
+
const detail = this.stderr?.trim() || (this.cause instanceof Error ? this.cause.message : this.cause && String(this.cause));
|
|
14
|
+
const status = this.exitCode === undefined ? "" : ` (exit ${this.exitCode})`;
|
|
15
|
+
return `Command failed${status}: ${this.command}${detail ? `: ${detail}` : ""}`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export class Service extends Context.Service()("@radheim/AppProcess") {
|
|
19
|
+
}
|
|
20
|
+
export const requireSuccess = (result) => result.exitCode === 0
|
|
21
|
+
? Effect.succeed(result)
|
|
22
|
+
: Effect.fail(new AppProcessError({
|
|
23
|
+
command: result.command,
|
|
24
|
+
exitCode: result.exitCode,
|
|
25
|
+
stderr: result.stderr.toString("utf8"),
|
|
26
|
+
}));
|
|
27
|
+
export const requireExitIn = (codes) => (result) => codes.includes(result.exitCode)
|
|
28
|
+
? Effect.succeed(result)
|
|
29
|
+
: Effect.fail(new AppProcessError({
|
|
30
|
+
command: result.command,
|
|
31
|
+
exitCode: result.exitCode,
|
|
32
|
+
stderr: result.stderr.toString("utf8"),
|
|
33
|
+
}));
|
|
34
|
+
const describeCommand = (command) => {
|
|
35
|
+
if (command._tag === "StandardCommand") {
|
|
36
|
+
return command.args.length ? `${command.command} ${command.args.join(" ")}` : command.command;
|
|
37
|
+
}
|
|
38
|
+
return `${describeCommand(command.left)} | ${describeCommand(command.right)}`;
|
|
39
|
+
};
|
|
40
|
+
const wrapError = (description, cause) => cause instanceof AppProcessError ? cause : new AppProcessError({ command: description, cause });
|
|
41
|
+
export const abortError = (signal) => {
|
|
42
|
+
const reason = signal.reason;
|
|
43
|
+
if (reason instanceof Error)
|
|
44
|
+
return reason;
|
|
45
|
+
const err = new Error("Aborted");
|
|
46
|
+
err.name = "AbortError";
|
|
47
|
+
return err;
|
|
48
|
+
};
|
|
49
|
+
export const waitForAbort = (signal) => Effect.callback((resume) => {
|
|
50
|
+
if (signal.aborted) {
|
|
51
|
+
resume(Effect.fail(abortError(signal)));
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const onabort = () => resume(Effect.fail(abortError(signal)));
|
|
55
|
+
signal.addEventListener("abort", onabort, { once: true });
|
|
56
|
+
return Effect.sync(() => signal.removeEventListener("abort", onabort));
|
|
57
|
+
});
|
|
58
|
+
const normalizeStdin = (input) => typeof input === "string"
|
|
59
|
+
? Stream.make(new TextEncoder().encode(input))
|
|
60
|
+
: input instanceof Uint8Array
|
|
61
|
+
? Stream.make(input)
|
|
62
|
+
: input;
|
|
63
|
+
export const collectStream = (stream, maxOutputBytes) => Stream.runFold(stream, () => ({ chunks: [], bytes: 0, truncated: false }), (acc, chunk) => {
|
|
64
|
+
if (maxOutputBytes === undefined) {
|
|
65
|
+
acc.chunks.push(chunk);
|
|
66
|
+
acc.bytes += chunk.length;
|
|
67
|
+
return acc;
|
|
68
|
+
}
|
|
69
|
+
const remaining = maxOutputBytes - acc.bytes;
|
|
70
|
+
if (remaining > 0)
|
|
71
|
+
acc.chunks.push(remaining >= chunk.length ? chunk : chunk.slice(0, remaining));
|
|
72
|
+
acc.bytes += chunk.length;
|
|
73
|
+
acc.truncated = acc.truncated || acc.bytes > maxOutputBytes;
|
|
74
|
+
return acc;
|
|
75
|
+
}).pipe(Effect.map((x) => ({ buffer: Buffer.concat(x.chunks), truncated: x.truncated })));
|
|
76
|
+
const layer = Layer.effect(Service, Effect.gen(function* () {
|
|
77
|
+
const spawner = yield* ChildProcessSpawner;
|
|
78
|
+
const runCommand = (command, options) => {
|
|
79
|
+
const description = describeCommand(command);
|
|
80
|
+
const collect = Effect.scoped(Effect.gen(function* () {
|
|
81
|
+
const handle = yield* spawner.spawn(command);
|
|
82
|
+
if (options?.combineOutput) {
|
|
83
|
+
const [output, exitCode] = yield* Effect.all([collectStream(handle.all, options.maxOutputBytes), handle.exitCode], { concurrency: "unbounded" });
|
|
84
|
+
return {
|
|
85
|
+
command: description,
|
|
86
|
+
exitCode,
|
|
87
|
+
output: output.buffer,
|
|
88
|
+
stdout: Buffer.alloc(0),
|
|
89
|
+
stderr: Buffer.alloc(0),
|
|
90
|
+
outputTruncated: output.truncated,
|
|
91
|
+
stdoutTruncated: false,
|
|
92
|
+
stderrTruncated: false,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const [stdout, stderr, exitCode] = yield* Effect.all([
|
|
96
|
+
collectStream(handle.stdout, options?.maxOutputBytes),
|
|
97
|
+
collectStream(handle.stderr, options?.maxErrorBytes),
|
|
98
|
+
handle.exitCode,
|
|
99
|
+
], { concurrency: "unbounded" });
|
|
100
|
+
return {
|
|
101
|
+
command: description,
|
|
102
|
+
exitCode,
|
|
103
|
+
stdout: stdout.buffer,
|
|
104
|
+
stderr: stderr.buffer,
|
|
105
|
+
stdoutTruncated: stdout.truncated,
|
|
106
|
+
stderrTruncated: stderr.truncated,
|
|
107
|
+
};
|
|
108
|
+
}));
|
|
109
|
+
const timed = options?.timeout
|
|
110
|
+
? Effect.timeoutOrElse(collect, {
|
|
111
|
+
duration: options.timeout,
|
|
112
|
+
orElse: () => Effect.fail(new AppProcessError({ command: description, cause: new Error("Timed out") })),
|
|
113
|
+
})
|
|
114
|
+
: collect;
|
|
115
|
+
const aborted = options?.signal
|
|
116
|
+
? timed.pipe(Effect.raceFirst(waitForAbort(options.signal).pipe(Effect.mapError((cause) => wrapError(description, cause)))))
|
|
117
|
+
: timed;
|
|
118
|
+
return aborted.pipe(Effect.mapError((cause) => wrapError(description, cause)));
|
|
119
|
+
};
|
|
120
|
+
const run = Effect.fn("AppProcess.run")(function* (command, options) {
|
|
121
|
+
if (options?.stdin === undefined)
|
|
122
|
+
return yield* runCommand(command, options);
|
|
123
|
+
if (command._tag !== "StandardCommand") {
|
|
124
|
+
return yield* new AppProcessError({
|
|
125
|
+
command: describeCommand(command),
|
|
126
|
+
cause: new Error("stdin option only supports StandardCommand; received PipedCommand"),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
const next = ChildProcess.make(command.command, command.args, {
|
|
130
|
+
...command.options,
|
|
131
|
+
stdin: normalizeStdin(options.stdin),
|
|
132
|
+
});
|
|
133
|
+
return yield* runCommand(next, options);
|
|
134
|
+
});
|
|
135
|
+
const runStream = (command, options) => {
|
|
136
|
+
const description = describeCommand(command);
|
|
137
|
+
const okExitCodes = options?.okExitCodes;
|
|
138
|
+
const built = Stream.unwrap(Effect.gen(function* () {
|
|
139
|
+
const handle = yield* spawner.spawn(command);
|
|
140
|
+
const streams = options?.includeStderr === true
|
|
141
|
+
? yield* handle.stderr.pipe(Stream.broadcastN({ n: 2, capacity: 16 }), Effect.map((copies) => ({
|
|
142
|
+
source: Stream.merge(handle.stdout, copies[0]),
|
|
143
|
+
diagnostics: copies[1],
|
|
144
|
+
})))
|
|
145
|
+
: { source: handle.stdout, diagnostics: handle.stderr };
|
|
146
|
+
const stderrFiber = yield* Effect.forkScoped(collectStream(streams.diagnostics, options?.maxErrorBytes).pipe(Effect.map((x) => x.buffer.toString("utf8"))));
|
|
147
|
+
const lines = streams.source.pipe(Stream.decodeText, Stream.splitLines, Stream.filter((line) => line.length > 0));
|
|
148
|
+
const tail = Stream.unwrap(Effect.gen(function* () {
|
|
149
|
+
const code = yield* handle.exitCode;
|
|
150
|
+
if (okExitCodes && okExitCodes.length > 0 && !okExitCodes.includes(code)) {
|
|
151
|
+
const stderr = yield* Fiber.join(stderrFiber);
|
|
152
|
+
return Stream.fail(new AppProcessError({ command: description, exitCode: code, stderr }));
|
|
153
|
+
}
|
|
154
|
+
return Stream.empty;
|
|
155
|
+
}));
|
|
156
|
+
return Stream.concat(lines, tail);
|
|
157
|
+
}));
|
|
158
|
+
const mapped = built.pipe(Stream.mapError((cause) => wrapError(description, cause)));
|
|
159
|
+
if (!options?.signal)
|
|
160
|
+
return mapped;
|
|
161
|
+
const signal = options.signal;
|
|
162
|
+
return mapped.pipe(Stream.interruptWhen(waitForAbort(signal).pipe(Effect.mapError((cause) => wrapError(description, cause)))));
|
|
163
|
+
};
|
|
164
|
+
return Service.of({ ...spawner, run, runStream });
|
|
165
|
+
}));
|
|
166
|
+
export const node = makeGlobalNode({ service: Service, layer: layer, deps: [CrossSpawnSpawner.node] });
|
|
167
|
+
export * as AppProcess from "./process.js";
|
package/dist/qr.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Encodes text as UTF-8 into the smallest QR Code that fits. Rows of modules without a quiet zone; true is dark. */
|
|
2
|
+
export declare function encode(text: string): boolean[][];
|
|
3
|
+
/** Renders text as an SVG QR Code with 10-unit modules and a quiet zone of `border` modules. */
|
|
4
|
+
export declare function renderSVG(text: string, options?: {
|
|
5
|
+
border?: number;
|
|
6
|
+
blackColor?: string;
|
|
7
|
+
whiteColor?: string;
|
|
8
|
+
}): string;
|
|
9
|
+
/**
|
|
10
|
+
* Renders text as a QR Code for a terminal, two modules per character with half blocks. Light modules draw as blocks
|
|
11
|
+
* and dark modules as the terminal background, rows are joined with "\n", and the quiet zone is `border` modules.
|
|
12
|
+
*/
|
|
13
|
+
export declare function renderUnicodeCompact(text: string, options?: {
|
|
14
|
+
border?: number;
|
|
15
|
+
}): string;
|