@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
124 lines • 5.26 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
import { byteView, decodeText } from "../../../shell/bytes.js";
import { isStdin } from "../utils/stream.js";
import { specOf } from "../../spec/builtins.js";
import { FlagView } from "../../spec/flag_view.js";
import { IOResult, materialize } from "../../../io/types.js";
import { AWK_USAGE } from "../constants.js";
import {} from "./awk_types.js";
import { openStream, serveRequests } from "../../../rs/stream.js";
import { StreamHost } from "../utils/stream_host.js";
import { fsStrerror, isEnotdir, isMissingPath } from "../../../errors/fs.js";
import { posixPhrase } from "../../../errors/posix.js";
import { mountsOf, mountRoot } from "../utils/links.js";
const ENC = new TextEncoder();
const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
function parseFlags(ctx) {
const fl = new FlagView(ctx.flags, specOf('awk'));
const assignments = fl.asList('v');
const programFiles = fl.asPaths('f');
return {
fieldSeparator: fl.asStr('F') ?? null,
assignments,
programFiles,
};
}
/** Whether an operand is a `var=value` assignment, not a file: POSIX makes
* it one when a name comes before its `=`. Mirrors Python's `is_assignment`. */
export function isAssignment(operand) {
return ASSIGNMENT.test(operand);
}
/** The operand whose mount runs the program: its first file. Mirrors
* Python's `home_operand`. */
export function homeOperand(scopes) {
for (const scope of scopes) {
if (scope.rawPath !== '' && !isStdin(scope) && !isAssignment(scope.rawPath)) {
return scope;
}
}
return scopes[0] ?? null;
}
/**
* Whether the mount awk runs on serves an operand. A line whose operands span
* mounts runs awk once, on its first file's mount; an operand another mount
* serves is read through the dispatcher. Outside a workspace (no name plane)
* every operand is the mount's own.
*/
export function servedHere(ctx, path) {
const mounts = mountsOf(ctx);
if (mounts === undefined)
return true;
const home = mountRoot(ctx);
return mounts.rootOf(path.virtual).replace(/\/+$/, '') === home;
}
/** Run the program, yielding standard output as it settles. */
async function* awkStream(stream, host, io, signal) {
try {
yield* serveRequests(stream, host, signal);
io.exitCode = stream.status();
}
finally {
io.stderr = await host.errors.output();
await host.close();
stream.close();
}
}
export async function awkGeneric(paths, texts, ctx, stream) {
const f = parseFlags(ctx);
const host = new StreamHost(paths, stream, ctx, true, '', (path) => servedHere(ctx, path));
let program;
if (f.programFiles.length > 0) {
const pieces = [];
for (const programFile of f.programFiles) {
try {
const source = isStdin(programFile) ? host.stdinView() : stream(programFile);
pieces.push(decodeText(await materialize(source)));
}
catch (err) {
// GNU awk exits 2 when a -f program file cannot be opened;
// anything that is not absence keeps propagating.
if (!isMissingPath(err) && !isEnotdir(err))
throw err;
const msg = `awk: ${programFile.rawPath}: ${fsStrerror(err) ?? posixPhrase('ENOENT')}`;
return [null, new IOResult({ exitCode: 2, stderr: ENC.encode(`${msg}\n`) })];
}
}
program = pieces.join('\n');
}
else if (texts.length > 0 && texts[0] !== undefined) {
program = texts[0];
}
else {
return [null, new IOResult({ exitCode: 2, stderr: ENC.encode(`${AWK_USAGE}\n`) })];
}
// An empty operand names no file and mawk skips it, as it does an
// operand ARGV no longer holds; a `var=value` operand is assigned when
// the input reaches it. FILENAME reports the operand as typed.
const engine = await openStream('awk', {
program: byteView(program),
operands: paths.map((p) => byteView(p.rawPath)),
assignments: f.assignments.map((a) => byteView(a)),
fs: f.fieldSeparator === null ? null : byteView(f.fieldSeparator),
environ: Object.entries(ctx.env ?? {}).map(([name, value]) => byteView(`${name}=${value}`)),
});
if (!engine.busy()) {
const stderr = engine.takeStderr();
engine.close();
return [null, new IOResult({ exitCode: 2, stderr })];
}
const io = new IOResult();
return [awkStream(engine, host, io, ctx.signal), io];
}
//# sourceMappingURL=awk.js.map