mirror of
https://github.com/morluto/rea.git
synced 2026-11-15 19:23:24 +00:00
* refactor(domain): one owner for the non-array record predicate Thirty-four source files each declared isRecord as a non-null object that is not an array, and six more spelled the same predicate as isObject, isSchemaRecord or isStorageRecord. They now import isRecord from src/domain/isRecord.ts, whose overloads keep JsonValue narrowing for the desktop and environment readers. DshCordisPatch's predicate also excludes Cordis expressions, so it is renamed isPatchRecord. architecture-guards refuses a new local copy under any name. The seven files that still hold one are listed and must leave the list when their copy goes. * fix(guards): count record predicate copies precisely * test(hopper): retry after confirmed application exit * fix: reject stale record predicate exceptions
106 lines
3.7 KiB
TypeScript
106 lines
3.7 KiB
TypeScript
import { MAX_NODE_TIMER_DELAY_MS } from "../domain/analysisResourceLimits.js";
|
|
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
|
import type { BrowserObservationOperation } from "../domain/browserObservationErrors.js";
|
|
import { sanitizeBrowserUrl } from "../domain/browserObservation.js";
|
|
import { createWebTextArtifact } from "../domain/webContentArtifact.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import { isRecord } from "../domain/isRecord.js";
|
|
|
|
export type UnknownRecord = Readonly<Record<string, unknown>>;
|
|
|
|
export const recordValue = (value: unknown): UnknownRecord | undefined =>
|
|
isRecord(value) ? value : undefined;
|
|
|
|
/** Preserve an omitted producer inventory as unknown rather than empty. */
|
|
export const admitRecords = (value: unknown): readonly unknown[] | undefined =>
|
|
Array.isArray(value) ? value : undefined;
|
|
|
|
export const recordsValue = (value: unknown): readonly UnknownRecord[] =>
|
|
Array.isArray(value)
|
|
? value.flatMap((item) => {
|
|
const record = recordValue(item);
|
|
return record === undefined ? [] : [record];
|
|
})
|
|
: [];
|
|
|
|
export const cdpStringValue = (value: unknown): string | undefined =>
|
|
typeof value === "string" ? value : undefined;
|
|
|
|
export const numberValue = (value: unknown): number | undefined =>
|
|
typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
|
|
/** An omitted resource size is unknown; a supplied invalid size is malformed. */
|
|
export const resourceContentSize = (
|
|
value: unknown,
|
|
): Result<number | null, { readonly _tag: "InvalidCdpResourceSize" }> => {
|
|
if (value === undefined) return ok(null);
|
|
const size = numberValue(value);
|
|
return size !== undefined && size >= 0
|
|
? ok(size)
|
|
: err({ _tag: "InvalidCdpResourceSize" });
|
|
};
|
|
|
|
export const isHttpUrl = (value: string | undefined): boolean => {
|
|
if (value === undefined) return false;
|
|
try {
|
|
const protocol = new URL(value).protocol;
|
|
return protocol === "http:" || protocol === "https:";
|
|
} catch (cause: unknown) {
|
|
// Non-URL input is not an HTTP URL.
|
|
void cause;
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export const allowedSanitizedUrl = (
|
|
value: unknown,
|
|
allowedOrigins: ReadonlySet<string>,
|
|
): ReturnType<typeof sanitizeBrowserUrl> | undefined => {
|
|
const text = cdpStringValue(value);
|
|
if (text === undefined) return undefined;
|
|
const sanitized = sanitizeBrowserUrl(text);
|
|
return sanitized.origin !== null && allowedOrigins.has(sanitized.origin)
|
|
? sanitized
|
|
: undefined;
|
|
};
|
|
|
|
export const sourceResult = (
|
|
source: string,
|
|
): { included: true; artifact: ReturnType<typeof createWebTextArtifact> } => ({
|
|
included: true,
|
|
artifact: createWebTextArtifact(source, "text/javascript"),
|
|
});
|
|
|
|
export const sourceExcluded = (
|
|
reason: string,
|
|
): { included: false; reason: string } => ({ included: false, reason });
|
|
|
|
export const delayWithCancellation = async (
|
|
durationMs: number,
|
|
operation: BrowserObservationOperation,
|
|
signal?: AbortSignal,
|
|
): Promise<void> => {
|
|
let remaining = durationMs;
|
|
while (remaining > 0) {
|
|
if (signal?.aborted === true)
|
|
throw new AnalysisCancelledError(operation, { cause: signal.reason });
|
|
const delay = Math.min(remaining, MAX_NODE_TIMER_DELAY_MS);
|
|
await new Promise<void>((resolve, reject) => {
|
|
const timer = setTimeout(() => {
|
|
signal?.removeEventListener("abort", onAbort);
|
|
resolve();
|
|
}, delay);
|
|
const onAbort = (): void => {
|
|
clearTimeout(timer);
|
|
signal?.removeEventListener("abort", onAbort);
|
|
reject(
|
|
new AnalysisCancelledError(operation, { cause: signal?.reason }),
|
|
);
|
|
};
|
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
if (signal?.aborted === true) onAbort();
|
|
});
|
|
remaining -= delay;
|
|
}
|
|
};
|