mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
fix(javascript): preserve selected inputs and completed source evidence
This commit is contained in:
1 parent
3864c7d667
commit
721dd3d1aa
11 files changed
+453
-177
No files matched your search
@@ -9,8 +9,15 @@ import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
|
||||
import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import { OwnedDirectoryHandle } from "../../filesystem/OwnedDirectoryHandle.js";
|
||||
import { createHash } from "node:crypto";
|
||||
import { lstat, mkdir, opendir, writeFile } from "node:fs/promises";
|
||||
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
||||
import {
|
||||
lstat,
|
||||
mkdir,
|
||||
opendir,
|
||||
realpath,
|
||||
stat,
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import { basename, dirname, isAbsolute, join, relative, sep } from "node:path";
|
||||
import { TextDecoder } from "node:util";
|
||||
import { normalizeArtifactPath } from "../../artifacts/ArtifactPaths.js";
|
||||
import {
|
||||
@@ -175,11 +182,6 @@ const snapshotRecoveryInput = async (
|
||||
signal?: AbortSignal,
|
||||
resources?: ArtifactResourceScope,
|
||||
): Promise<RecoveryInputSnapshot> => {
|
||||
if (!isAbsolute(path))
|
||||
throw recoveryInputFailure(
|
||||
"path",
|
||||
"Select an absolute JavaScript file path",
|
||||
);
|
||||
let bytes: Buffer;
|
||||
try {
|
||||
bytes = await readRecoveryFile(path, maximum, signal, resources);
|
||||
@@ -254,17 +256,69 @@ const snapshotRecoveryInput = async (
|
||||
};
|
||||
};
|
||||
|
||||
/** Preserve each selected file and its relative input layout without concatenation. */
|
||||
/** Resolve native parent traversal while keeping the selected final filename. */
|
||||
const recoveryInputLayoutPath = async (
|
||||
path: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<string> => {
|
||||
if (!isAbsolute(path))
|
||||
throw recoveryInputFailure(
|
||||
"path",
|
||||
"Select an absolute JavaScript file path",
|
||||
);
|
||||
try {
|
||||
checkRecoveryCancellation(signal);
|
||||
const parent = dirname(path);
|
||||
// Native lookup must accept the parent before realpath can collapse it:
|
||||
// Darwin realpath(file/..) otherwise succeeds despite ENOTDIR on reads.
|
||||
if (!(await stat(parent)).isDirectory())
|
||||
throw new NonRegularFileReadError(path, false);
|
||||
const canonicalParent = await realpath(parent);
|
||||
checkRecoveryCancellation(signal);
|
||||
return join(canonicalParent, basename(path));
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof AnalysisCancelledError) throw cause;
|
||||
if (
|
||||
cause instanceof Error &&
|
||||
"code" in cause &&
|
||||
(cause.code === "EACCES" || cause.code === "EPERM")
|
||||
)
|
||||
throw new AnalysisAccessDeniedError(OPERATION, path, cause.code, {
|
||||
cause,
|
||||
});
|
||||
throw recoveryInputFailure(
|
||||
"path",
|
||||
`Cannot resolve selected JavaScript input parent ${path}: ${recoveryFailureMessage(cause)}`,
|
||||
cause,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/** Own selected path interpretation, native collisions and sequential snapshots. */
|
||||
export const snapshotRecoveryInputs = async (
|
||||
paths: readonly [string, ...string[]],
|
||||
directory: string,
|
||||
signal?: AbortSignal,
|
||||
resources?: ArtifactResourceScope,
|
||||
): Promise<readonly [RecoveryInputSnapshot, ...RecoveryInputSnapshot[]]> => {
|
||||
let commonDirectory = dirname(resolve(paths[0]));
|
||||
for (const path of paths) {
|
||||
const selections: [
|
||||
{ readonly path: string; readonly layoutPath: string },
|
||||
...{ readonly path: string; readonly layoutPath: string }[],
|
||||
] = [
|
||||
{
|
||||
path: paths[0],
|
||||
layoutPath: await recoveryInputLayoutPath(paths[0], signal),
|
||||
},
|
||||
];
|
||||
for (const path of paths.slice(1))
|
||||
selections.push({
|
||||
path,
|
||||
layoutPath: await recoveryInputLayoutPath(path, signal),
|
||||
});
|
||||
let commonDirectory = dirname(selections[0].layoutPath);
|
||||
for (const { layoutPath } of selections) {
|
||||
while (true) {
|
||||
const fromCommon = relative(commonDirectory, resolve(path));
|
||||
const fromCommon = relative(commonDirectory, layoutPath);
|
||||
if (
|
||||
fromCommon !== ".." &&
|
||||
!fromCommon.startsWith(`..${sep}`) &&
|
||||
@@ -282,11 +336,11 @@ export const snapshotRecoveryInputs = async (
|
||||
}
|
||||
const snapshots: RecoveryInputSnapshot[] = [];
|
||||
let remaining = RECOVERY_LIMITS.inputBytes;
|
||||
for (const path of paths) {
|
||||
for (const { path, layoutPath } of selections) {
|
||||
const relativePath =
|
||||
paths.length === 1
|
||||
selections.length === 1
|
||||
? "bundle.js"
|
||||
: relative(commonDirectory, resolve(path)).split(sep).join("/");
|
||||
: relative(commonDirectory, layoutPath).split(sep).join("/");
|
||||
try {
|
||||
// Wakaru's multi-input public paths interpret backslashes as separators.
|
||||
if (relativePath.includes("\\"))
|
||||
|
||||
Reference in new issue
Block a user