@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
97 lines • 3.67 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 { z } from 'zod';
import { yieldBytes } from "../io/stream.js";
import { StreamRefusal } from "../rs/errors.js";
import { closeStream, openStream, streamOutputChunks } from "../rs/stream.js";
import { materialize } from "../io/types.js";
export class GzipDataError extends Error {
exitCode;
keepsOutput;
firstHeader;
reasons;
fatal;
constructor(reasons, fatal, exitCode = 1, keepsOutput = false, firstHeader = false) {
super(reasons.join('\n'));
this.exitCode = exitCode;
this.keepsOutput = keepsOutput;
this.firstHeader = firstHeader;
this.name = 'GzipDataError';
this.reasons = reasons;
this.fatal = fatal;
}
/** gzip's lines for the failure, the input named `label`. */
render(label) {
return this.reasons.map((reason) => `${reason.split('{}').join(label)}\n`).join('');
}
}
const gzipFailure = z
.object({
reasons: z.array(z.string()),
fatal: z.boolean(),
exit_code: z.number().int(),
keeps_output: z.boolean(),
first_header: z.boolean(),
})
.strict();
function failure(stream) {
if (stream.status() === 0)
return null;
const record = gzipFailure.parse(JSON.parse(new TextDecoder().decode(stream.takeStderr())));
return new GzipDataError(record.reasons, record.fatal, record.exit_code, record.keeps_output, record.first_header);
}
function* output(stream, data) {
const error = failure(stream);
if (data.byteLength > 0)
yield data;
if (error !== null)
throw error;
}
async function* codecStream(source, config) {
const stream = await openStream('compress', config);
try {
for await (const data of streamOutputChunks(stream, source, undefined, false)) {
yield* output(stream, data);
}
const error = failure(stream);
if (error !== null)
throw error;
}
catch (error) {
if (!(error instanceof StreamRefusal))
throw error;
if (error.output.byteLength > 0)
yield error.output;
throw new Error(error.message);
}
finally {
await closeStream(stream);
}
}
/** Encode one member through the shared codec. */
export async function gzip(bytes, name = '', level = null) {
return materialize(gzipCompressStream(yieldBytes(bytes), level, name));
}
export async function gunzip(bytes) {
return materialize(gunzipStream(yieldBytes(bytes)));
}
/** Encode bounded chunks and close an abandoned source. */
export function gzipCompressStream(source, level = null, name = '') {
return codecStream(source, { operation: 'encode', format: 'gzip', level: level ?? -1, name });
}
/** Decode members and retain bytes produced before a checked failure. */
export function gunzipStream(source, test = false, passthrough = false) {
return codecStream(source, { operation: 'decode', format: 'gzip', test, passthrough });
}
//# sourceMappingURL=compress.js.map