/** * Internal helpers used by `./protocol.ts` to enforce the RemoteWorker * protocol contract at the trust boundary. * * Nothing in this module is part of the public `@lostgradient/weft/worker-protocol` surface * (other than `isRemoteWorkerJsonValue`, which `protocol.ts` re-exports). * Splitting these helpers out of `protocol.ts` keeps the canonical parser * module focused on the schema-to-guard mapping a reviewer audits. * * @module worker/protocol-internals */ import type { ProtocolErrorMessage, RegisterErrorMessage, RemoteWorkerJsonValue } from './protocol-messages.ts'; /** * Protocol parse failure with a machine-readable code. * * @example * ```ts * import type { RemoteWorkerProtocolFailure } from '@lostgradient/weft/worker-protocol'; * * const failure: RemoteWorkerProtocolFailure = { * code: 'invalid_message', * message: 'Protocol message must be an object', * }; * ``` */ export type RemoteWorkerProtocolFailure = { readonly code: ProtocolErrorMessage['code'] | RegisterErrorMessage['code']; readonly message: string; readonly requestedProtocolVersion?: number; }; /** * Result returned by protocol parser helpers. * * @example * ```ts * import type { RemoteWorkerProtocolParseResult, RegisterMessage } from '@lostgradient/weft/worker-protocol'; * * const result: RemoteWorkerProtocolParseResult = { * ok: false, * error: { code: 'invalid_registration', message: 'workerId is required' }, * }; * ``` */ export type RemoteWorkerProtocolParseResult = { readonly ok: true; readonly message: T; } | { readonly ok: false; readonly error: RemoteWorkerProtocolFailure; }; export declare function isRecord(value: unknown): value is Record; /** * Return true when a value can be represented by JSON on the worker protocol. * * @example * ```ts * import { isRemoteWorkerJsonValue } from '@lostgradient/weft/worker-protocol'; * * const canSend = isRemoteWorkerJsonValue({ nested: ['ok'] }); * ``` */ export declare function isRemoteWorkerJsonValue(value: unknown): value is RemoteWorkerJsonValue; export declare function isNonEmptyString(value: unknown): value is string; export declare function isStringArray(value: unknown): value is string[]; export declare function isStringRecord(value: unknown): value is Record; export declare function isFiniteNumber(value: unknown): value is number; export declare function protocolFailure(code: RemoteWorkerProtocolFailure['code'], message: string, requestedProtocolVersion?: number): RemoteWorkerProtocolParseResult; export type FieldSpec = readonly [ sourceKey: string, isRequired: boolean, predicate: (v: unknown) => boolean, errorMessage: string ]; export type CollectFieldsResult = { readonly ok: true; readonly values: Record; } | { readonly ok: false; readonly error: RemoteWorkerProtocolParseResult; }; export declare function collectFields(code: RemoteWorkerProtocolFailure['code'], record: Record, specs: readonly FieldSpec[]): CollectFieldsResult;