@tearleads/client-sdk 0.1.257 → 0.1.259

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"principalHistoryRequest.js","sourceRoot":"","sources":["principalHistoryRequest.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAQpD,MAAM,iCAAiC,GAAG,MAAM,CAAC;AAEjD,SAAS,cAAc,CACrB,OAA0B,EAC1B,KAKC;IAED,OAAO,CACL,KAAK,CAAC,OAAO;QACb,IAAI,8BAA8B,CAChC,OAAO,EACP,KAAK,CAAC,MAAM,EACZ,KAAK,CAAC,IAAI,EACV,KAAK,CAAC,OAAO,CACd,CACF,CAAC;AACJ,CAAC;AAED,oFAAoF;AACpF,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,OAA0B,EAC1B,KAWC;IAED,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IAC/B,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC/C,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAChC,MAAM,QAAQ,GAAwB,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;IAC5E,OAAO,IAAI,EAAE,CAAC;QACZ,IAAI,OAAO,CAAC,SAAS,EAAE;YAAE,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;QAClD,MAAM,OAAO,GAAG,MAAM,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;QAC5D,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,OAAO,CAAC,uBAAuB,EAAE;YAAE,SAAS;QAC/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UAAU,EAAE,IAAI,CAAC,UAAU,EAAE;aACvD;YACH,CAAC,CAAC,OAAO,CACZ,CAAC;QACF,OAAO,MAAM,IAAI,kBAAkB,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IAC/D,CAAC;YAAS,CAAC;QACT,QAAQ,CAAC,OAAO,EAAE,CAAC;IACrB,CAAC;AACH,CAAC;AAOD,KAAK,UAAU,mBAAmB,CAChC,IAAa,EACb,QAA6B;IAM7B,MAAM,KAAK,GACT,OAAO,IAAI,KAAK,QAAQ;QACxB,IAAI,KAAK,IAAI;QACb,eAAe,IAAI,IAAI;QACvB,OAAO,IAAI,CAAC,aAAa,KAAK,QAAQ;QACpC,CAAC,CAAC,IAAI,CAAC,aAAa;QACpB,CAAC,CAAC,SAAS,CAAC;IAChB,IAAI,KAAK,KAAK,SAAS;QACrB,OAAO;YACL,IAAI,EAAE,OAAO;YACb,OAAO,EAAE,2CAA2C;SACrD,CAAC;IACJ,QAAQ,CAAC,SAAS,GAAG,KAAK,KAAK,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9E,QAAQ,CAAC,QAAQ,GAAG,KAAK,CAAC;IAC1B,IAAI,QAAQ,CAAC,SAAS,IAAI,CAAC;QACzB,OAAO;YACL,IAAI,EAAE,uCAAuC;YAC7C,IAAI,EAAE,MAAM;YACZ,OAAO,EAAE,4DAA4D;SACtE,CAAC;IACJ,4EAA4E;IAC5E,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAClC,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CACnD,CAAC;IACF,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { JsonOperation } from \"@tearleads/validators/operation\";\nimport type { ApiRequestRuntime } from \"./apiRequestRuntime\";\nimport { decodeJsonOperationResponse } from \"./operationResponse\";\nimport { PrincipalHistoryRequestContext } from \"./principalHistoryRequestContext\";\nimport { requestDeadline } from \"./requestDeadline\";\nimport type {\n HttpMethod,\n RequestFailure,\n RequestResult,\n RequestResultOptions,\n} from \"./types\";\n\nconst PRINCIPAL_HISTORY_READ_TIMEOUT_MS = 15_000;\n\nfunction requestContext(\n runtime: ApiRequestRuntime,\n input: {\n readonly path: string;\n readonly method: HttpMethod;\n readonly options?: RequestResultOptions;\n readonly context?: PrincipalHistoryRequestContext;\n },\n): PrincipalHistoryRequestContext {\n return (\n input.context ??\n new PrincipalHistoryRequestContext(\n runtime,\n input.method,\n input.path,\n input.options,\n )\n );\n}\n\n/** Retry only an explicit, validated response proving the operation rolled back. */\nexport async function principalHistoryRequest<T>(\n runtime: ApiRequestRuntime,\n input: {\n readonly path: string;\n readonly validator: (value: unknown) => value is T;\n /** Maps a schema-valid 200 body to the value the validator checks. */\n readonly decode?: (value: unknown) => unknown;\n readonly method: HttpMethod;\n readonly body?: string;\n readonly options?: RequestResultOptions;\n readonly operation: JsonOperation;\n readonly context?: PrincipalHistoryRequestContext;\n readonly requestTimeoutMs?: number;\n },\n): Promise<RequestResult<T>> {\n const { path, method } = input;\n const context = requestContext(runtime, input);\n const options = context.options;\n const progress: PreparationProgress = { previous: undefined, unchanged: 0 };\n while (true) {\n if (context.cancelled()) return context.failure();\n const decoded = await readResponse(runtime, context, input);\n if (!decoded.ok && context.restartReadAfterRenewal()) continue;\n if (!decoded.ok) return preserveCommitUncertainty(method, context, decoded);\n if (decoded.data.status === 202) {\n context.afterPreparation();\n const failure = await continuePreparation(decoded.data.data, progress);\n if (failure)\n return runtime.responseRequest.reportFailure({\n ...failure,\n method,\n options,\n path,\n status: 202,\n statusText: decoded.data.statusText,\n });\n continue;\n }\n const valid = validatedSuccess(decoded.data, input);\n return valid\n ? { ok: true, data: valid.data }\n : invalidResponseFailure(runtime, context, input, decoded.data);\n }\n}\n\nfunction invalidResponseFailure(\n runtime: ApiRequestRuntime,\n context: PrincipalHistoryRequestContext,\n input: { readonly path: string; readonly method: HttpMethod },\n response: { readonly status: number; readonly statusText: string },\n): RequestFailure {\n const { path, method } = input;\n const options = context.options;\n const failure = runtime.responseRequest.reportFailure({\n kind: \"shape\",\n message: `Invalid principal policy response for ${path}`,\n method,\n options: method === \"GET\" ? options : { ...options, reportErrors: false },\n path,\n status: response.status,\n statusText: response.statusText,\n });\n return preserveCommitUncertainty(method, context, failure);\n}\n\nfunction decodeSuccess(\n response: { readonly data: unknown; readonly status: number },\n decode: ((value: unknown) => unknown) | undefined,\n): unknown {\n return response.status === 200 && decode\n ? decode(response.data)\n : response.data;\n}\n\nfunction validatedSuccess<T>(\n response: { readonly data: unknown; readonly status: number },\n input: {\n readonly validator: (value: unknown) => value is T;\n readonly decode?: (value: unknown) => unknown;\n },\n): { readonly data: T } | null {\n const data = decodeSuccess(response, input.decode);\n return response.status === 200 && input.validator(data) ? { data } : null;\n}\n\nfunction preserveCommitUncertainty(\n method: HttpMethod,\n context: PrincipalHistoryRequestContext,\n failure: RequestFailure,\n): RequestFailure {\n if (method === \"GET\") return failure;\n // A network/decode failure or intermediary 5xx says nothing about commit.\n // Inner write errors are silent until this final classification is known.\n const ambiguous =\n failure.kind !== \"http\" ||\n ((failure.status ?? 0) >= 500 &&\n // The declared 503 schema requires the exact code and committed:false.\n // Unvalidated codes never reach RequestFailure.code.\n !(\n failure.status === 503 &&\n failure.code === \"principal_history_preparation_unavailable\"\n )) ||\n failure.status === 408 ||\n failure.status === 499;\n const result =\n ambiguous && failure.kind !== \"outcome-unknown\"\n ? context.failure()\n : failure;\n if (\n context.options.reportErrors !== false &&\n (result.kind === \"http\" || !context.cancelled())\n )\n result.report();\n return result;\n}\n\n/** A cancelled request is not a timeout, even if its deadline also passed. */\nfunction interruptedFailure(\n context: PrincipalHistoryRequestContext,\n deadline: { readonly expired: () => boolean },\n response: Response | undefined,\n): RequestFailure {\n return context.failure(response, deadline.expired() && !context.cancelled());\n}\n\nasync function readResponse(\n runtime: ApiRequestRuntime,\n context: PrincipalHistoryRequestContext,\n input: {\n readonly path: string;\n readonly method: HttpMethod;\n readonly body?: string;\n readonly operation: JsonOperation;\n readonly requestTimeoutMs?: number;\n },\n) {\n // A fresh timer per response, so preparation continuations reset it.\n const deadline = requestDeadline(\n context.options,\n // Writes await acknowledgement unless the caller supplies a deadline.\n // Aborting a committed write cannot be repaired by the continuation loop.\n input.requestTimeoutMs ??\n (input.method === \"GET\" ? PRINCIPAL_HISTORY_READ_TIMEOUT_MS : null),\n );\n const options =\n input.method === \"GET\"\n ? deadline.options\n : { ...deadline.options, reportErrors: false };\n try {\n const session = runtime.responseRequest.sessionSignal?.();\n const response = await context.withCredential(options.signal, () => {\n context.beforeRequest();\n return runtime.responseRequest(\n input.path,\n input.method,\n input.body,\n options,\n [],\n input.operation,\n );\n });\n if (!response) return interruptedFailure(context, deadline, undefined);\n if (!response.ok) {\n // Authentication rejection precedes the workflow and remains definitive.\n if (response.kind === \"http\" && response.status === 401) return response;\n return (\n (await context.withCredential(options.signal, () => response)) ??\n interruptedFailure(context, deadline, undefined)\n );\n }\n const body = response.data;\n const decoded = await context.withCredential(options.signal, () =>\n decodeJsonOperationResponse(\n runtime.responseRequest,\n input.operation,\n body,\n input.path,\n options,\n session,\n ),\n );\n if (!decoded) return interruptedFailure(context, deadline, body);\n const result = await context.withCredential(options.signal, () =>\n decoded.ok\n ? {\n ok: true as const,\n data: { ...decoded.data, statusText: body.statusText },\n }\n : decoded,\n );\n return result ?? interruptedFailure(context, deadline, body);\n } finally {\n deadline.dispose();\n }\n}\n\ninterface PreparationProgress {\n previous: string | undefined;\n unchanged: number;\n}\n\nasync function continuePreparation(\n data: unknown,\n progress: PreparationProgress,\n): Promise<{\n readonly kind: \"shape\" | \"http\";\n readonly message: string;\n readonly code?: string;\n} | null> {\n const token =\n typeof data === \"object\" &&\n data !== null &&\n \"progressToken\" in data &&\n typeof data.progressToken === \"string\"\n ? data.progressToken\n : undefined;\n if (token === undefined)\n return {\n kind: \"shape\",\n message: \"Principal preparation progress is missing\",\n };\n progress.unchanged = token === progress.previous ? progress.unchanged + 1 : 0;\n progress.previous = token;\n if (progress.unchanged >= 2)\n return {\n code: \"principal_history_preparation_stalled\",\n kind: \"http\",\n message: \"Principal history preparation did not advance; retry later\",\n };\n // The next round checks cancellation and identity again before any request.\n await new Promise<void>((resolve) =>\n setTimeout(resolve, progress.unchanged ? 250 : 25),\n );\n return null;\n}\n"]}
1
+ {"version":3,"file":"principalHistoryRequest.js","sourceRoot":"","sources":["principalHistoryRequest.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAQpE,MAAM,iCAAiC,GAAG,MAAM,CAAC;AAEjD,SAAS,cAAc,CACrB,OAA0B,EAC1B,KAKC;IAED,OAAO,CACL,KAAK,CAAC,OAAO;QACb,IAAI,8BAA8B,CAChC,OAAO,EACP,KAAK,CAAC,MAAM,EACZ,KAAK,CAAC,IAAI,EACV,KAAK,CAAC,OAAO,CACd,CACF,CAAC;AACJ,CAAC;AAED,oFAAoF;AACpF,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,OAA0B,EAC1B,KAWC;IAED,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IAC/B,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC/C,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAChC,MAAM,QAAQ,GAAwB,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;IAC5E,OAAO,IAAI,EAAE,CAAC;QACZ,IA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;IACJ,4EAA4E;IAC5E,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAClC,UAAU,CAAC,OAAO,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CACnD,CAAC;IACF,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import type { JsonOperation } from \"@tearleads/validators/operation\";\nimport type { ApiRequestRuntime } from \"./apiRequestRuntime\";\nimport { decodeJsonOperationResponse } from \"./operationResponse\";\nimport { PrincipalHistoryRequestContext } from \"./principalHistoryRequestContext\";\nimport { requestDeadline } from \"./requestDeadline\";\nimport { resourceRequestActivity } from \"./resourceRequestActivity\";\nimport type {\n HttpMethod,\n RequestFailure,\n RequestResult,\n RequestResultOptions,\n} from \"./types\";\n\nconst PRINCIPAL_HISTORY_READ_TIMEOUT_MS = 15_000;\n\nfunction requestContext(\n runtime: ApiRequestRuntime,\n input: {\n readonly path: string;\n readonly method: HttpMethod;\n readonly options?: RequestResultOptions;\n readonly context?: PrincipalHistoryRequestContext;\n },\n): PrincipalHistoryRequestContext {\n return (\n input.context ??\n new PrincipalHistoryRequestContext(\n runtime,\n input.method,\n input.path,\n input.options,\n )\n );\n}\n\n/** Retry only an explicit, validated response proving the operation rolled back. */\nexport async function principalHistoryRequest<T>(\n runtime: ApiRequestRuntime,\n input: {\n readonly path: string;\n readonly validator: (value: unknown) => value is T;\n /** Maps a schema-valid 200 body to the value the validator checks. */\n readonly decode?: (value: unknown) => unknown;\n readonly method: HttpMethod;\n readonly body?: string;\n readonly options?: RequestResultOptions;\n readonly operation: JsonOperation;\n readonly context?: PrincipalHistoryRequestContext;\n readonly requestTimeoutMs?: number;\n },\n): Promise<RequestResult<T>> {\n const { path, method } = input;\n const context = requestContext(runtime, input);\n const options = context.options;\n const progress: PreparationProgress = { previous: undefined, unchanged: 0 };\n while (true) {\n if (context.cancelled()) return context.failure();\n const decoded = await readResponse(runtime, context, input);\n if (!decoded.ok && context.restartReadAfterRenewal()) continue;\n if (!decoded.ok) return preserveCommitUncertainty(method, context, decoded);\n if (decoded.data.status === 202) {\n context.afterPreparation();\n const failure = await continuePreparation(decoded.data.data, progress);\n if (failure)\n return runtime.responseRequest.reportFailure({\n ...failure,\n method,\n options,\n path,\n status: 202,\n statusText: decoded.data.statusText,\n });\n continue;\n }\n const valid = validatedSuccess(decoded.data, input);\n return valid\n ? { ok: true, data: valid.data }\n : invalidResponseFailure(runtime, context, input, decoded.data);\n }\n}\n\nfunction invalidResponseFailure(\n runtime: ApiRequestRuntime,\n context: PrincipalHistoryRequestContext,\n input: { readonly path: string; readonly method: HttpMethod },\n response: { readonly status: number; readonly statusText: string },\n): RequestFailure {\n const { path, method } = input;\n const options = context.options;\n const failure = runtime.responseRequest.reportFailure({\n kind: \"shape\",\n message: `Invalid principal policy response for ${path}`,\n method,\n options: method === \"GET\" ? options : { ...options, reportErrors: false },\n path,\n status: response.status,\n statusText: response.statusText,\n });\n return preserveCommitUncertainty(method, context, failure);\n}\n\nfunction decodeSuccess(\n response: { readonly data: unknown; readonly status: number },\n decode: ((value: unknown) => unknown) | undefined,\n): unknown {\n return response.status === 200 && decode\n ? decode(response.data)\n : response.data;\n}\n\nfunction validatedSuccess<T>(\n response: { readonly data: unknown; readonly status: number },\n input: {\n readonly validator: (value: unknown) => value is T;\n readonly decode?: (value: unknown) => unknown;\n },\n): { readonly data: T } | null {\n const data = decodeSuccess(response, input.decode);\n return response.status === 200 && input.validator(data) ? { data } : null;\n}\n\nfunction preserveCommitUncertainty(\n method: HttpMethod,\n context: PrincipalHistoryRequestContext,\n failure: RequestFailure,\n): RequestFailure {\n if (method === \"GET\") return failure;\n // A network/decode failure or intermediary 5xx says nothing about commit.\n // Inner write errors are silent until this final classification is known.\n const ambiguous =\n failure.kind !== \"http\" ||\n ((failure.status ?? 0) >= 500 &&\n // The declared 503 schema requires the exact code and committed:false.\n // Unvalidated codes never reach RequestFailure.code.\n !(\n failure.status === 503 &&\n failure.code === \"principal_history_preparation_unavailable\"\n )) ||\n failure.status === 408 ||\n failure.status === 499;\n const result =\n ambiguous && failure.kind !== \"outcome-unknown\"\n ? context.failure()\n : failure;\n if (\n context.options.reportErrors !== false &&\n (result.kind === \"http\" || !context.cancelled())\n )\n result.report();\n return result;\n}\n\n/** A cancelled request is not a timeout, even if its deadline also passed. */\nfunction interruptedFailure(\n context: PrincipalHistoryRequestContext,\n deadline: { readonly expired: () => boolean },\n response: Response | undefined,\n): RequestFailure {\n return context.failure(response, deadline.expired() && !context.cancelled());\n}\n\nasync function readResponse(\n runtime: ApiRequestRuntime,\n context: PrincipalHistoryRequestContext,\n input: {\n readonly path: string;\n readonly method: HttpMethod;\n readonly body?: string;\n readonly operation: JsonOperation;\n readonly requestTimeoutMs?: number;\n },\n) {\n // A fresh timer per response, so preparation continuations reset it.\n const deadline = requestDeadline(\n context.options,\n // Writes await acknowledgement unless the caller supplies a deadline.\n // Aborting a committed write cannot be repaired by the continuation loop.\n input.requestTimeoutMs ??\n (input.method === \"GET\" ? PRINCIPAL_HISTORY_READ_TIMEOUT_MS : null),\n );\n const options =\n input.method === \"GET\"\n ? deadline.options\n : { ...deadline.options, reportErrors: false };\n try {\n const session = runtime.responseRequest.sessionSignal?.();\n const response = await context.withCredential(options.signal, () => {\n context.beforeRequest();\n return runtime.responseRequest(\n input.path,\n input.method,\n input.body,\n options,\n [],\n input.operation,\n );\n });\n if (!response) return interruptedFailure(context, deadline, undefined);\n if (!response.ok) {\n // Authentication rejection precedes the workflow and remains definitive.\n if (response.kind === \"http\" && response.status === 401) return response;\n return (\n (await context.withCredential(options.signal, () => response)) ??\n interruptedFailure(context, deadline, undefined)\n );\n }\n const body = response.data;\n const decoded = await context.withCredential(options.signal, () =>\n decodeJsonOperationResponse(\n runtime.responseRequest,\n input.operation,\n body,\n input.path,\n options,\n session,\n ),\n );\n if (!decoded) return interruptedFailure(context, deadline, body);\n const result = await context.withCredential(options.signal, () =>\n decoded.ok\n ? {\n ok: true as const,\n data: { ...decoded.data, statusText: body.statusText },\n }\n : decoded,\n );\n return result ?? interruptedFailure(context, deadline, body);\n } finally {\n options[resourceRequestActivity]?.finish();\n deadline.dispose();\n }\n}\n\ninterface PreparationProgress {\n previous: string | undefined;\n unchanged: number;\n}\n\nasync function continuePreparation(\n data: unknown,\n progress: PreparationProgress,\n): Promise<{\n readonly kind: \"shape\" | \"http\";\n readonly message: string;\n readonly code?: string;\n} | null> {\n const token =\n typeof data === \"object\" &&\n data !== null &&\n \"progressToken\" in data &&\n typeof data.progressToken === \"string\"\n ? data.progressToken\n : undefined;\n if (token === undefined)\n return {\n kind: \"shape\",\n message: \"Principal preparation progress is missing\",\n };\n progress.unchanged = token === progress.previous ? progress.unchanged + 1 : 0;\n progress.previous = token;\n if (progress.unchanged >= 2)\n return {\n code: \"principal_history_preparation_stalled\",\n kind: \"http\",\n message: \"Principal history preparation did not advance; retry later\",\n };\n // The next round checks cancellation and identity again before any request.\n await new Promise<void>((resolve) =>\n setTimeout(resolve, progress.unchanged ? 250 : 25),\n );\n return null;\n}\n"]}
@@ -0,0 +1,12 @@
1
+ import type { HttpOperation } from "../validators/operation/index.js";
2
+ import type { ApiRequestMetrics } from "./apiRequestMetrics.js";
3
+ export declare const resourceRequestActivity: unique symbol;
4
+ export interface ResourceRequestActivity {
5
+ start(metrics: ApiRequestMetrics, identity: string | null, operation: HttpOperation): void;
6
+ finish(): void;
7
+ }
8
+ /** Start only at fetch dispatch. Release after the response has been read,
9
+ * including on decode failure, cancellation, or an early return. */
10
+ export declare function withResourceRequestActivity<TOptions extends object, T>(resourceId: string, options: TOptions, request: (options: TOptions & {
11
+ [resourceRequestActivity]: ResourceRequestActivity;
12
+ }) => Promise<T>): Promise<T>;
@@ -0,0 +1,28 @@
1
+ export const resourceRequestActivity = Symbol("resourceRequestActivity");
2
+ /** Start only at fetch dispatch. Release after the response has been read,
3
+ * including on decode failure, cancellation, or an early return. */
4
+ export async function withResourceRequestActivity(resourceId, options, request) {
5
+ let release;
6
+ const activity = {
7
+ start(metrics, identity, operation) {
8
+ release ??= metrics.beginResource(identity, operation, resourceId);
9
+ },
10
+ finish() {
11
+ const finish = release;
12
+ release = undefined;
13
+ try {
14
+ finish?.();
15
+ }
16
+ catch {
17
+ // A host's instrumentation cannot change a request's outcome.
18
+ }
19
+ },
20
+ };
21
+ try {
22
+ return await request({ ...options, [resourceRequestActivity]: activity });
23
+ }
24
+ finally {
25
+ activity.finish();
26
+ }
27
+ }
28
+ //# sourceMappingURL=resourceRequestActivity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resourceRequestActivity.js","sourceRoot":"","sources":["resourceRequestActivity.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAUzE;oEACoE;AACpE,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC/C,UAAkB,EAClB,OAAiB,EACjB,OAEe;IAEf,IAAI,OAAiC,CAAC;IACtC,MAAM,QAAQ,GAA4B;QACxC,KAAK,CAAC,OAAO,EAAE,QAAQ,EAAE,SAAS;YAChC,OAAO,KAAK,OAAO,CAAC,aAAa,CAAC,QAAQ,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC;QACrE,CAAC;QACD,MAAM;YACJ,MAAM,MAAM,GAAG,OAAO,CAAC;YACvB,OAAO,GAAG,SAAS,CAAC;YACpB,IAAI,CAAC;gBACH,MAAM,EAAE,EAAE,CAAC;YACb,CAAC;YAAC,MAAM,CAAC;gBACP,8DAA8D;YAChE,CAAC;QACH,CAAC;KACF,CAAC;IACF,IAAI,CAAC;QACH,OAAO,MAAM,OAAO,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC,uBAAuB,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC5E,CAAC;YAAS,CAAC;QACT,QAAQ,CAAC,MAAM,EAAE,CAAC;IACpB,CAAC;AACH,CAAC","sourcesContent":["import type { HttpOperation } from \"@tearleads/validators/operation\";\nimport type { ApiRequestMetrics } from \"./apiRequestMetrics\";\n\nexport const resourceRequestActivity = Symbol(\"resourceRequestActivity\");\nexport interface ResourceRequestActivity {\n start(\n metrics: ApiRequestMetrics,\n identity: string | null,\n operation: HttpOperation,\n ): void;\n finish(): void;\n}\n\n/** Start only at fetch dispatch. Release after the response has been read,\n * including on decode failure, cancellation, or an early return. */\nexport async function withResourceRequestActivity<TOptions extends object, T>(\n resourceId: string,\n options: TOptions,\n request: (\n options: TOptions & { [resourceRequestActivity]: ResourceRequestActivity },\n ) => Promise<T>,\n): Promise<T> {\n let release: (() => void) | undefined;\n const activity: ResourceRequestActivity = {\n start(metrics, identity, operation) {\n release ??= metrics.beginResource(identity, operation, resourceId);\n },\n finish() {\n const finish = release;\n release = undefined;\n try {\n finish?.();\n } catch {\n // A host's instrumentation cannot change a request's outcome.\n }\n },\n };\n try {\n return await request({ ...options, [resourceRequestActivity]: activity });\n } finally {\n activity.finish();\n }\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import type { DocumentAttributionRangesQuery, HttpOperation, HttpOperationMethod } from "../validators/operation/index.js";
2
2
  import type { ReferencedPrincipalStateResponse } from "../validators/response/index.js";
3
3
  import type { ApiNetworkError } from "./networkFailure.js";
4
+ import type { ResourceRequestActivity, resourceRequestActivity } from "./resourceRequestActivity.js";
4
5
  import type { WriterProjectionListing } from "./verifiedManifestCitations.js";
5
6
  export type HttpMethod = HttpOperationMethod;
6
7
  export type RequestBody = BodyInit;
@@ -8,6 +9,7 @@ export type ListDocumentEditAttributionRangesOptions = Omit<DocumentAttributionR
8
9
  cursor?: DocumentAttributionRangesQuery["cursor"] | null;
9
10
  };
10
11
  export interface RequestResultOptions {
12
+ readonly [resourceRequestActivity]?: ResourceRequestActivity;
11
13
  /** Checked at dispatch, on renewal and before handling the response. */
12
14
  readonly stillCurrent?: (() => boolean) | undefined;
13
15
  readonly signal?: AbortSignal | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n DocumentAttributionRangesQuery,\n HttpOperation,\n HttpOperationMethod,\n} from \"@tearleads/validators/operation\";\nimport type { ReferencedPrincipalStateResponse } from \"@tearleads/validators/response\";\nimport type { ApiNetworkError } from \"./networkFailure\";\nimport type { WriterProjectionListing } from \"./verifiedManifestCitations\";\n\nexport type HttpMethod = HttpOperationMethod;\nexport type RequestBody = BodyInit;\n\nexport type ListDocumentEditAttributionRangesOptions = Omit<\n DocumentAttributionRangesQuery,\n \"cursor\"\n> & {\n cursor?: DocumentAttributionRangesQuery[\"cursor\"] | null;\n};\n\nexport interface RequestResultOptions {\n /** Checked at dispatch, on renewal and before handling the response. */\n readonly stillCurrent?: (() => boolean) | undefined;\n readonly signal?: AbortSignal | undefined;\n /** Expected target for a declared 402; mismatched response identities fail closed. */\n readonly expectedPaymentRequiredOrganizationId?: string | undefined;\n readonly headers?: Record<string, string> | undefined;\n readonly reportErrors?: boolean | undefined;\n /** Called after this request or a concurrent request renews the same session. */\n readonly onSessionRenewed?: (() => void) | undefined;\n readonly retryOnSessionExpired?: boolean | \"renew-only\" | undefined;\n /**\n * Writer-projection reads only: where a listing placed the container, so\n * an unplaced container can cite its listed parent's held bytes. It\n * changes which hashes the read cites, never what the read accepts.\n */\n readonly projectionListing?: WriterProjectionListing | undefined;\n /**\n * Group creates, group policy commits and shares only: the organization\n * read-model cursor the caller holds. The response then carries the\n * read-model delta from it when the commit advanced from that cursor.\n */\n readonly readModelCursor?: string | undefined;\n}\n\nexport type OperationRequestFn = <T>(\n path: string,\n validator: (value: unknown) => value is T,\n method: HttpMethod,\n body: RequestBody | undefined,\n options: RequestResultOptions | undefined,\n failureOperation: HttpOperation,\n) => Promise<T | null>;\n\nexport type OperationRequestResultFn = <T>(\n path: string,\n validator: (value: unknown) => value is T,\n method: HttpMethod,\n body: RequestBody | undefined,\n options: RequestResultOptions | undefined,\n failureOperation: HttpOperation,\n) => Promise<RequestResult<T>>;\n\nexport type RequestFailureKind =\n | \"storage\"\n | \"http\"\n | \"network\"\n | \"json\"\n | \"shape\"\n | \"cancelled\"\n | \"outcome-unknown\";\n\nexport interface RequestFailure {\n readonly code?: string | undefined;\n readonly kind: RequestFailureKind;\n readonly message: string;\n readonly method: HttpMethod;\n /** Set when the fetch itself threw: what the client could observe, without secrets. */\n readonly networkError?: ApiNetworkError | undefined;\n readonly ok: false;\n readonly path: string;\n readonly report: () => void;\n readonly status: number | null;\n readonly statusText: string;\n readonly stalePrincipalHeads?: ReferencedPrincipalStateResponse[] | undefined;\n /**\n * With `container_descendant_rekeys_required`, the full owed set, parent-first;\n * with `container_descendant_rekeys_inaccessible`, its unwritable subset.\n * A hint, never authority; verify each container before signing for it.\n */\n readonly requiredContainerIds?: readonly string[] | undefined;\n}\n\nexport interface RequestSuccess<T> {\n readonly data: T;\n readonly ok: true;\n}\n\nexport type RequestResult<T> = RequestFailure | RequestSuccess<T>;\n\nexport interface ResponseRequestValidationFailureInput {\n readonly code?: string | undefined;\n readonly kind: RequestFailureKind;\n readonly message: string;\n readonly method: HttpMethod;\n readonly options?: RequestResultOptions | undefined;\n readonly path: string;\n readonly status: number | null;\n readonly statusText: string;\n}\n\nexport interface OperationResponseRequestFn {\n (\n path: string,\n method: HttpMethod,\n body: RequestBody | undefined,\n options: RequestResultOptions,\n additionalSuccessStatuses: readonly number[],\n failureOperation: HttpOperation,\n ): Promise<RequestResult<Response>>;\n readonly sessionSignal?: () => AbortSignal;\n readonly reportFailure: (\n input: ResponseRequestValidationFailureInput,\n ) => RequestFailure;\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n DocumentAttributionRangesQuery,\n HttpOperation,\n HttpOperationMethod,\n} from \"@tearleads/validators/operation\";\nimport type { ReferencedPrincipalStateResponse } from \"@tearleads/validators/response\";\nimport type { ApiNetworkError } from \"./networkFailure\";\nimport type {\n ResourceRequestActivity,\n resourceRequestActivity,\n} from \"./resourceRequestActivity\";\nimport type { WriterProjectionListing } from \"./verifiedManifestCitations\";\n\nexport type HttpMethod = HttpOperationMethod;\nexport type RequestBody = BodyInit;\n\nexport type ListDocumentEditAttributionRangesOptions = Omit<\n DocumentAttributionRangesQuery,\n \"cursor\"\n> & {\n cursor?: DocumentAttributionRangesQuery[\"cursor\"] | null;\n};\n\nexport interface RequestResultOptions {\n readonly [resourceRequestActivity]?: ResourceRequestActivity;\n /** Checked at dispatch, on renewal and before handling the response. */\n readonly stillCurrent?: (() => boolean) | undefined;\n readonly signal?: AbortSignal | undefined;\n /** Expected target for a declared 402; mismatched response identities fail closed. */\n readonly expectedPaymentRequiredOrganizationId?: string | undefined;\n readonly headers?: Record<string, string> | undefined;\n readonly reportErrors?: boolean | undefined;\n /** Called after this request or a concurrent request renews the same session. */\n readonly onSessionRenewed?: (() => void) | undefined;\n readonly retryOnSessionExpired?: boolean | \"renew-only\" | undefined;\n /**\n * Writer-projection reads only: where a listing placed the container, so\n * an unplaced container can cite its listed parent's held bytes. It\n * changes which hashes the read cites, never what the read accepts.\n */\n readonly projectionListing?: WriterProjectionListing | undefined;\n /**\n * Group creates, group policy commits and shares only: the organization\n * read-model cursor the caller holds. The response then carries the\n * read-model delta from it when the commit advanced from that cursor.\n */\n readonly readModelCursor?: string | undefined;\n}\n\nexport type OperationRequestFn = <T>(\n path: string,\n validator: (value: unknown) => value is T,\n method: HttpMethod,\n body: RequestBody | undefined,\n options: RequestResultOptions | undefined,\n failureOperation: HttpOperation,\n) => Promise<T | null>;\n\nexport type OperationRequestResultFn = <T>(\n path: string,\n validator: (value: unknown) => value is T,\n method: HttpMethod,\n body: RequestBody | undefined,\n options: RequestResultOptions | undefined,\n failureOperation: HttpOperation,\n) => Promise<RequestResult<T>>;\n\nexport type RequestFailureKind =\n | \"storage\"\n | \"http\"\n | \"network\"\n | \"json\"\n | \"shape\"\n | \"cancelled\"\n | \"outcome-unknown\";\n\nexport interface RequestFailure {\n readonly code?: string | undefined;\n readonly kind: RequestFailureKind;\n readonly message: string;\n readonly method: HttpMethod;\n /** Set when the fetch itself threw: what the client could observe, without secrets. */\n readonly networkError?: ApiNetworkError | undefined;\n readonly ok: false;\n readonly path: string;\n readonly report: () => void;\n readonly status: number | null;\n readonly statusText: string;\n readonly stalePrincipalHeads?: ReferencedPrincipalStateResponse[] | undefined;\n /**\n * With `container_descendant_rekeys_required`, the full owed set, parent-first;\n * with `container_descendant_rekeys_inaccessible`, its unwritable subset.\n * A hint, never authority; verify each container before signing for it.\n */\n readonly requiredContainerIds?: readonly string[] | undefined;\n}\n\nexport interface RequestSuccess<T> {\n readonly data: T;\n readonly ok: true;\n}\n\nexport type RequestResult<T> = RequestFailure | RequestSuccess<T>;\n\nexport interface ResponseRequestValidationFailureInput {\n readonly code?: string | undefined;\n readonly kind: RequestFailureKind;\n readonly message: string;\n readonly method: HttpMethod;\n readonly options?: RequestResultOptions | undefined;\n readonly path: string;\n readonly status: number | null;\n readonly statusText: string;\n}\n\nexport interface OperationResponseRequestFn {\n (\n path: string,\n method: HttpMethod,\n body: RequestBody | undefined,\n options: RequestResultOptions,\n additionalSuccessStatuses: readonly number[],\n failureOperation: HttpOperation,\n ): Promise<RequestResult<Response>>;\n readonly sessionSignal?: () => AbortSignal;\n readonly reportFailure: (\n input: ResponseRequestValidationFailureInput,\n ) => RequestFailure;\n}\n"]}
@@ -3,6 +3,7 @@ import { decodeContainerWriterProjection, decodeDocumentWriterProjection, } from
3
3
  import { formatKnownManifestHints } from "../validators/util/index.js";
4
4
  import { principalHistoryRequest } from "./principalHistoryRequest.js";
5
5
  import { PrincipalHistoryRequestContext } from "./principalHistoryRequestContext.js";
6
+ import { withResourceRequestActivity } from "./resourceRequestActivity.js";
6
7
  import { heldManifestCitations, } from "./verifiedManifestCitations.js";
7
8
  const NO_CITATIONS = {
8
9
  bundles: new Map(),
@@ -87,8 +88,13 @@ async function citedProjectionRead(runtime, citations, input, options) {
87
88
  export function writerProjectionRequests(runtime, citations) {
88
89
  const requestResult = (path, validator, method, body, options, operation) => {
89
90
  const kind = method === "GET" ? writerProjectionKind(operation.id) : null;
90
- if (kind)
91
- return citedProjectionRead(runtime, citations(), { kind, path, validator }, options ?? {});
91
+ if (kind) {
92
+ const read = (requestOptions) => citedProjectionRead(runtime, citations(), { kind, path, validator }, requestOptions);
93
+ const id = kind === "document" ? projectionId(path) : null;
94
+ return id === null
95
+ ? read(options ?? {})
96
+ : withResourceRequestActivity(id, options ?? {}, read);
97
+ }
92
98
  return runtime.requestResult(path, validator, method, body, options, operation);
93
99
  };
94
100
  const request = async (...args) => {
@@ -1 +1 @@
1
- {"version":3,"file":"writerProjectionRequests.js","sourceRoot":"","sources":["writerProjectionRequests.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qCAAqC,EACrC,oCAAoC,GACrC,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,+BAA+B,EAC/B,8BAA8B,GAC/B,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAEtE,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAOlF,OAAO,EAEL,qBAAqB,GAGtB,MAAM,6BAA6B,CAAC;AAErC,MAAM,YAAY,GAA4B;IAC5C,OAAO,EAAE,IAAI,GAAG,EAAE;IAClB,SAAS,EAAE,IAAI,GAAG,EAAE;IACpB,WAAW,EAAE,IAAI,GAAG,EAAE;CACvB,CAAC;AAEF,MAAM,eAAe,GACnB,6DAA6D,CAAC;AAEhE,SAAS,YAAY,CAAC,IAAY;IAChC,MAAM,OAAO,GAAG,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAChD,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACvC,IAAI,CAAC;QACH,OAAO,kBAAkB,CAAC,OAAO,CA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AAE,KAAK,EAAE,YAAY,EAAE,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;AAC/E,CAAC;AAED,MAAM,UAAU,wBAAwB,CACtC,OAA0B,EAC1B,SAAiD;IAKjD,MAAM,aAAa,GAA6B,CAC9C,IAAI,EACJ,SAAS,EACT,MAAM,EACN,IAAI,EACJ,OAAO,EACP,SAAS,EACT,EAAE;QACF,MAAM,IAAI,GAAG,MAAM,KAAK,KAAK,CAAC,CAAC,CAAC,oBAAoB,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAC1E,IAAI,IAAI;YACN,OAAO,mBAAmB,CACxB,OAAO,EACP,SAAS,EAAE,EACX,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,EACzB,OAAO,IAAI,EAAE,CACd,CAAC;QACJ,OAAO,OAAO,CAAC,aAAa,CAC1B,IAAI,EACJ,SAAS,EACT,MAAM,EACN,IAAI,EACJ,OAAO,EACP,SAAS,CACV,CAAC;IACJ,CAAC,CAAC;IACF,MAAM,OAAO,GAAuB,KAAK,EAAE,GAAG,IAAI,EAAE,EAAE;QACpD,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,GAAG,IAAI,CAAC,CAAC;QAC5C,OAAO,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IACxC,CAAC,CAAC;IACF,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC;AACpC,CAAC","sourcesContent":["import {\n getContainerWriterProjectionOperation,\n getDocumentWriterProjectionOperation,\n} from \"@tearleads/validators/operation\";\nimport {\n decodeContainerWriterProjection,\n decodeDocumentWriterProjection,\n} from \"@tearleads/validators/response\";\nimport { formatKnownManifestHints } from \"@tearleads/validators/util\";\nimport type { ApiRequestRuntime } from \"./apiRequestRuntime\";\nimport { principalHistoryRequest } from \"./principalHistoryRequest\";\nimport { PrincipalHistoryRequestContext } from \"./principalHistoryRequestContext\";\nimport type {\n OperationRequestFn,\n OperationRequestResultFn,\n RequestResult,\n RequestResultOptions,\n} from \"./types\";\nimport {\n type HeldProjectionCitations,\n heldManifestCitations,\n type VerifiedManifestCitations,\n type WriterProjectionTarget,\n} from \"./verifiedManifestCitations\";\n\nconst NO_CITATIONS: HeldProjectionCitations = {\n bundles: new Map(),\n kekLevels: new Map(),\n directories: new Map(),\n};\n\nconst PROJECTION_PATH =\n /^\\/(?:containers|documents)\\/([^/?#]+)\\/writer-projection$/u;\n\nfunction projectionId(path: string): string | null {\n const encoded = PROJECTION_PATH.exec(path)?.[1];\n if (encoded === undefined) return null;\n try {\n return decodeURIComponent(encoded);\n } catch {\n return null;\n }\n}\n\nfunction writerProjectionKind(\n operationId: string,\n): WriterProjectionTarget[\"kind\"] | null {\n if (operationId === \"containers.writerProjection.get\") return \"container\";\n if (operationId === \"documents.writerProjection.get\") return \"document\";\n return null;\n}\n\nfunction readProjection<T>(\n runtime: ApiRequestRuntime,\n input: {\n readonly kind: WriterProjectionTarget[\"kind\"];\n readonly path: string;\n readonly validator: (value: unknown) => value is T;\n },\n held: HeldProjectionCitations,\n context: PrincipalHistoryRequestContext,\n): Promise<RequestResult<T>> {\n const container = input.kind === \"container\";\n return principalHistoryRequest(runtime, {\n path: input.path,\n validator: input.validator,\n decode: (value) =>\n container\n ? decodeContainerWriterProjection(\n value,\n held.bundles,\n held.kekLevels,\n held.directories,\n )\n : decodeDocumentWriterProjection(\n value,\n held.bundles,\n held.kekLevels,\n held.directories,\n ),\n method: \"GET\",\n context,\n operation: container\n ? getContainerWriterProjectionOperation\n : getDocumentWriterProjectionOperation,\n });\n}\n\n/**\n * Preparation and body deadlines sit inside the projection read. The wire\n * cites each manifest bundle by hash and may cite a held KEK level by digest;\n * decoding resolves every citation, from the response or from the exact bytes\n * this request declared held, before any validator, cache, or verifier sees\n * the projection. A response this device\n * cannot rebuild fails closed into one complete, hint-free refetch.\n */\nasync function citedProjectionRead<T>(\n runtime: ApiRequestRuntime,\n citations: VerifiedManifestCitations | null,\n input: {\n readonly kind: WriterProjectionTarget[\"kind\"];\n readonly path: string;\n readonly validator: (value: unknown) => value is T;\n },\n options: RequestResultOptions,\n): Promise<RequestResult<T>> {\n // Bind the identity before citations load: an account switch while they\n // load, or before the refetch, must not carry one identity's hints or\n // answer into another's session. Each request rechecks this context.\n const context = new PrincipalHistoryRequestContext(\n runtime,\n \"GET\",\n input.path,\n options,\n );\n const id = projectionId(input.path);\n // Caller-specific headers make a request its own; it cites nothing. The\n // caller's abort also ends a citation wait.\n const held =\n id === null || options.headers !== undefined\n ? NO_CITATIONS\n : await heldManifestCitations(\n citations,\n { kind: input.kind, id, listing: options.projectionListing },\n options.signal,\n );\n if (held.bundles.size + held.kekLevels.size + held.directories.size === 0)\n return readProjection(runtime, input, held, context);\n const hinted = await readProjection(\n runtime,\n input,\n held,\n context.derive({\n ...options,\n headers: {\n \"x-known-manifests\": formatKnownManifestHints({\n manifests: [...held.bundles.keys()],\n kekLevels: [...held.kekLevels.keys()],\n directories: [...held.directories.keys()],\n }),\n },\n reportErrors: false,\n }),\n );\n if (hinted.ok) return hinted;\n if (hinted.kind !== \"shape\") {\n if (options.reportErrors !== false) hinted.report();\n return hinted;\n }\n return readProjection(runtime, input, NO_CITATIONS, context.derive(options));\n}\n\nexport function writerProjectionRequests(\n runtime: ApiRequestRuntime,\n citations: () => VerifiedManifestCitations | null,\n): {\n request: OperationRequestFn;\n requestResult: OperationRequestResultFn;\n} {\n const requestResult: OperationRequestResultFn = (\n path,\n validator,\n method,\n body,\n options,\n operation,\n ) => {\n const kind = method === \"GET\" ? writerProjectionKind(operation.id) : null;\n if (kind)\n return citedProjectionRead(\n runtime,\n citations(),\n { kind, path, validator },\n options ?? {},\n );\n return runtime.requestResult(\n path,\n validator,\n method,\n body,\n options,\n operation,\n );\n };\n const request: OperationRequestFn = async (...args) => {\n const result = await requestResult(...args);\n return result.ok ? result.data : null;\n };\n return { request, requestResult };\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CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAC1E,IAAI,IAAI,EAAE,CAAC;YACT,MAAM,IAAI,GAAG,CAAC,cAAoC,EAAE,EAAE,CACpD,mBAAmB,CACjB,OAAO,EACP,SAAS,EAAE,EACX,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,EACzB,cAAc,CACf,CAAC;YACJ,MAAM,EAAE,GAAG,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;YAC3D,OAAO,EAAE,KAAK,IAAI;gBAChB,CAAC,CAAC,IAAI,CAAC,OAAO,IAAI,EAAE,CAAC;gBACrB,CAAC,CAAC,2BAA2B,CAAC,EAAE,EAAE,OAAO,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;QAC3D,CAAC;QACD,OAAO,OAAO,CAAC,aAAa,CAC1B,IAAI,EACJ,SAAS,EACT,MAAM,EACN,IAAI,EACJ,OAAO,EACP,SAAS,CACV,CAAC;IACJ,CAAC,CAAC;IACF,MAAM,OAAO,GAAuB,KAAK,EAAE,GAAG,IAAI,EAAE,EAAE;QACpD,MAAM,MAAM,GAAG,MAAM,aAAa,CAAC,GAAG,IAAI,CAAC,CAAC;QAC5C,OAAO,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IACxC,CAAC,CAAC;IACF,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,CAAC;AACpC,CAAC","sourcesContent":["import {\n getContainerWriterProjectionOperation,\n getDocumentWriterProjectionOperation,\n} from \"@tearleads/validators/operation\";\nimport {\n decodeContainerWriterProjection,\n decodeDocumentWriterProjection,\n} from \"@tearleads/validators/response\";\nimport { formatKnownManifestHints } from \"@tearleads/validators/util\";\nimport type { ApiRequestRuntime } from \"./apiRequestRuntime\";\nimport { principalHistoryRequest } from \"./principalHistoryRequest\";\nimport { PrincipalHistoryRequestContext } from \"./principalHistoryRequestContext\";\nimport { withResourceRequestActivity } from \"./resourceRequestActivity\";\nimport type {\n OperationRequestFn,\n OperationRequestResultFn,\n RequestResult,\n RequestResultOptions,\n} from \"./types\";\nimport {\n type HeldProjectionCitations,\n heldManifestCitations,\n type VerifiedManifestCitations,\n type WriterProjectionTarget,\n} from \"./verifiedManifestCitations\";\n\nconst NO_CITATIONS: HeldProjectionCitations = {\n bundles: new Map(),\n kekLevels: new Map(),\n directories: new Map(),\n};\n\nconst PROJECTION_PATH =\n /^\\/(?:containers|documents)\\/([^/?#]+)\\/writer-projection$/u;\n\nfunction projectionId(path: string): string | null {\n const encoded = PROJECTION_PATH.exec(path)?.[1];\n if (encoded === undefined) return null;\n try {\n return decodeURIComponent(encoded);\n } catch {\n return null;\n }\n}\n\nfunction writerProjectionKind(\n operationId: string,\n): WriterProjectionTarget[\"kind\"] | null {\n if (operationId === \"containers.writerProjection.get\") return \"container\";\n if (operationId === \"documents.writerProjection.get\") return \"document\";\n return null;\n}\n\nfunction readProjection<T>(\n runtime: ApiRequestRuntime,\n input: {\n readonly kind: WriterProjectionTarget[\"kind\"];\n readonly path: string;\n readonly validator: (value: unknown) => value is T;\n },\n held: HeldProjectionCitations,\n context: PrincipalHistoryRequestContext,\n): Promise<RequestResult<T>> {\n const container = input.kind === \"container\";\n return principalHistoryRequest(runtime, {\n path: input.path,\n validator: input.validator,\n decode: (value) =>\n container\n ? decodeContainerWriterProjection(\n value,\n held.bundles,\n held.kekLevels,\n held.directories,\n )\n : decodeDocumentWriterProjection(\n value,\n held.bundles,\n held.kekLevels,\n held.directories,\n ),\n method: \"GET\",\n context,\n operation: container\n ? getContainerWriterProjectionOperation\n : getDocumentWriterProjectionOperation,\n });\n}\n\n/**\n * Preparation and body deadlines sit inside the projection read. The wire\n * cites each manifest bundle by hash and may cite a held KEK level by digest;\n * decoding resolves every citation, from the response or from the exact bytes\n * this request declared held, before any validator, cache, or verifier sees\n * the projection. A response this device\n * cannot rebuild fails closed into one complete, hint-free refetch.\n */\nasync function citedProjectionRead<T>(\n runtime: ApiRequestRuntime,\n citations: VerifiedManifestCitations | null,\n input: {\n readonly kind: WriterProjectionTarget[\"kind\"];\n readonly path: string;\n readonly validator: (value: unknown) => value is T;\n },\n options: RequestResultOptions,\n): Promise<RequestResult<T>> {\n // Bind the identity before citations load: an account switch while they\n // load, or before the refetch, must not carry one identity's hints or\n // answer into another's session. Each request rechecks this context.\n const context = new PrincipalHistoryRequestContext(\n runtime,\n \"GET\",\n input.path,\n options,\n );\n const id = projectionId(input.path);\n // Caller-specific headers make a request its own; it cites nothing. The\n // caller's abort also ends a citation wait.\n const held =\n id === null || options.headers !== undefined\n ? NO_CITATIONS\n : await heldManifestCitations(\n citations,\n { kind: input.kind, id, listing: options.projectionListing },\n options.signal,\n );\n if (held.bundles.size + held.kekLevels.size + held.directories.size === 0)\n return readProjection(runtime, input, held, context);\n const hinted = await readProjection(\n runtime,\n input,\n held,\n context.derive({\n ...options,\n headers: {\n \"x-known-manifests\": formatKnownManifestHints({\n manifests: [...held.bundles.keys()],\n kekLevels: [...held.kekLevels.keys()],\n directories: [...held.directories.keys()],\n }),\n },\n reportErrors: false,\n }),\n );\n if (hinted.ok) return hinted;\n if (hinted.kind !== \"shape\") {\n if (options.reportErrors !== false) hinted.report();\n return hinted;\n }\n return readProjection(runtime, input, NO_CITATIONS, context.derive(options));\n}\n\nexport function writerProjectionRequests(\n runtime: ApiRequestRuntime,\n citations: () => VerifiedManifestCitations | null,\n): {\n request: OperationRequestFn;\n requestResult: OperationRequestResultFn;\n} {\n const requestResult: OperationRequestResultFn = (\n path,\n validator,\n method,\n body,\n options,\n operation,\n ) => {\n const kind = method === \"GET\" ? writerProjectionKind(operation.id) : null;\n if (kind) {\n const read = (requestOptions: RequestResultOptions) =>\n citedProjectionRead(\n runtime,\n citations(),\n { kind, path, validator },\n requestOptions,\n );\n const id = kind === \"document\" ? projectionId(path) : null;\n return id === null\n ? read(options ?? {})\n : withResourceRequestActivity(id, options ?? {}, read);\n }\n return runtime.requestResult(\n path,\n validator,\n method,\n body,\n options,\n operation,\n );\n };\n const request: OperationRequestFn = async (...args) => {\n const result = await requestResult(...args);\n return result.ok ? result.data : null;\n };\n return { request, requestResult };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tearleads/client-sdk",
3
- "version": "0.1.257",
3
+ "version": "0.1.259",
4
4
  "description": "React-free Tearleads client runtime: local SQLite persistence, keys, sync, encrypted blobs, and domain workflows.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {