@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
90 lines (89 loc) • 4.59 kB
TypeScript
/**
* Attachment policy — one answer for every SDK loader that turns a
* caller-supplied string into bytes.
*
* Two primitives, both policy-aware:
*
* - {@link readAttachmentFile}: realpath containment in `allowedRoots`
* (when configured), `O_NOFOLLOW` open on the real path, regular files
* only, size-capped read that notices a file growing under it.
* - {@link downloadAttachment}: `safeDownloadWithMeta` with the policy's
* URL switches — HTTPS-only and public hosts by default, redirects
* re-validated per hop, caller headers never replayed across one.
*
* The policy itself is resolved by {@link resolveAttachmentPolicy} from the
* constructor's `attachments` config and the `NEUROLINK_ATTACHMENT_*`
* variables, and travels on the per-call options (`attachmentPolicy`). A
* loader that is reached without one — a host calling `WebLoader` directly, a
* processor used on its own — falls back to the environment-derived default,
* so the operator switches apply everywhere.
*
* @module utils/attachmentPolicy
*/
import type { AttachmentDownloadOptions, AttachmentPolicy, AttachmentReadOptions, AttachmentReadResult, AttachmentRefusalCode, AttachmentsConfig, SafeDownloadResult } from "../types/index.js";
/**
* An error the loaders throw when the policy refuses an attachment. Carries a
* stable `code`; the message names the option that would allow the read.
*/
export declare class AttachmentRefusedError extends Error {
readonly code: AttachmentRefusalCode;
constructor(code: AttachmentRefusalCode, message: string, cause?: unknown);
}
/**
* Resolve the policy an instance runs under. An explicit config field wins
* over its environment variable; the defaults are the safe ones.
*/
export declare function resolveAttachmentPolicy(config?: AttachmentsConfig): AttachmentPolicy;
/** Runtime guard for a policy that travelled on an untyped options bag. */
export declare function isAttachmentPolicy(value: unknown): value is AttachmentPolicy;
/**
* The policy stamped on a call's options, or the environment-derived default.
* Accepts any options bag so loaders deep in the file pipeline can read it off
* whatever reached them.
*/
export declare function getAttachmentPolicy(carrier: {
attachmentPolicy?: unknown;
} | undefined | null): AttachmentPolicy;
/** The lower of a loader's own cap and the policy's global cap. */
export declare function effectiveAttachmentCap(policy: AttachmentPolicy, loaderCap: number): number;
/**
* Check `filePath` against the policy's roots and return the real path to
* open. Without roots the real path is still resolved (so the later
* `O_NOFOLLOW` open behaves identically for a symlinked input), and the
* one-time warning fires.
*
* @throws {AttachmentRefusedError} `ATTACHMENT_OUTSIDE_ROOTS`
*/
export declare function resolveAttachmentPath(filePath: string, policy: AttachmentPolicy, label: string): Promise<string>;
/**
* Read a local attachment under the policy.
*
* Containment is judged on the real path (see {@link resolveAttachmentPath});
* the open is `O_NOFOLLOW | O_NONBLOCK` on that real path so a symlink swapped
* in after the check is refused by the kernel and a FIFO cannot park a worker;
* the descriptor is then stat'd (regular files only, size under the cap) and
* drained under the same cap.
*
* @throws {AttachmentRefusedError} with `ATTACHMENT_OUTSIDE_ROOTS`,
* `ATTACHMENT_NOT_A_FILE` or `ATTACHMENT_TOO_LARGE`; other file-system
* errors (ENOENT, EACCES, ELOOP, …) propagate unchanged.
*/
export declare function readAttachmentFile(filePath: string, options: AttachmentReadOptions): Promise<AttachmentReadResult>;
/**
* Quick scheme check that names the switch, so a refused `http://` URL tells
* the caller what to set rather than only that HTTPS is required.
*
* @throws {AttachmentRefusedError} `ATTACHMENT_URL_REFUSED`
*/
export declare function assertAttachmentUrlScheme(url: string, policy: AttachmentPolicy, label: string): void;
/**
* Download a caller-supplied URL under the policy. Builds on
* `safeDownloadWithMeta`: DNS-pinned connection, bounded body, redirects only
* when the policy allows and then re-validated per hop with the caller's
* headers dropped.
*
* @throws {AttachmentRefusedError} `ATTACHMENT_URL_REFUSED` when the URL is
* refused by the policy or the SSRF guard; transport and HTTP errors
* propagate as plain errors from `safeDownloadWithMeta`.
*/
export declare function downloadAttachment(url: string, options: AttachmentDownloadOptions): Promise<SafeDownloadResult>;