@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
68 lines • 2.91 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 { commandFiles, dirAwareStream, resolveOrEmpty } from "../files.js";
import { specOf } from "../../../spec/builtins.js";
import { FlagView } from "../../../spec/flag_view.js";
import { IOResult } from "../../../../io/types.js";
import { StreamRefusal } from "../../../../rs/errors.js";
import { closeStream, openStream, runStream, streamOutputChunks, } from "../../../../rs/stream.js";
import { byteView, fromByteView } from "../../../../shell/bytes.js";
import { stdinStream } from "../../utils/stream.js";
import { perOperand } from "../../utils/operands.js";
import { OutputStream } from "../../../../io/stream.js";
const strings = async (_accessor, paths, _texts, ctx) => {
const minLen = new FlagView(ctx.flags, specOf('strings')).asText('n');
const config = { min_len: minLen === undefined ? null : byteView(minLen) };
// The engine refuses -n before any operand is touched, as binutils does.
let engine;
try {
engine = await openStream('strings', config);
}
catch (err) {
if (!(err instanceof StreamRefusal))
throw err;
return [null, new IOResult({ exitCode: 1, stderr: fromByteView(`${err.message}\n`) })];
}
try {
const files = commandFiles(ctx, paths);
const resolved = await resolveOrEmpty(files, paths);
const stream = stdinStream(dirAwareStream(files, ctx), ctx.stdin, false, false);
const io = new IOResult();
let used = false;
const each = (source) => {
if (used)
return runStream('strings', config, source, ctx.signal);
used = true;
return streamOutputChunks(engine, source, ctx.signal);
};
const produced = perOperand(resolved, stream, ctx.stdin, io, 'strings', each)[Symbol.asyncIterator]();
return [
new OutputStream(produced, async () => {
await produced.return?.();
await closeStream(engine);
}),
io,
];
}
catch (error) {
await closeStream(engine);
throw error;
}
};
export const BUILDER = {
name: 'strings',
read: true,
fn: strings,
};
//# sourceMappingURL=strings.js.map