@hypequery/serve 0.24.4 → 0.26.0
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.
- package/dist/adapters/fetch.d.ts.map +1 -1
- package/dist/adapters/fetch.js +19 -6
- package/dist/adapters/node.d.ts +1 -1
- package/dist/adapters/node.d.ts.map +1 -1
- package/dist/adapters/node.js +110 -11
- package/dist/adapters/standalone.d.ts +1 -1
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/utils.d.ts +5 -0
- package/dist/adapters/utils.d.ts.map +1 -1
- package/dist/adapters/utils.js +8 -0
- package/dist/auth.d.ts +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +9 -1
- package/dist/client-config.d.ts +8 -7
- package/dist/client-config.d.ts.map +1 -1
- package/dist/client-config.js +16 -7
- package/dist/cors.d.ts.map +1 -1
- package/dist/cors.js +2 -1
- package/dist/dev.d.ts +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/pipeline.d.ts +2 -0
- package/dist/pipeline.d.ts.map +1 -1
- package/dist/pipeline.js +81 -10
- package/dist/query-logger.d.ts +6 -0
- package/dist/query-logger.d.ts.map +1 -1
- package/dist/query-logger.js +6 -0
- package/dist/semantic/datasets/contract-endpoint.d.ts.map +1 -1
- package/dist/semantic/datasets/contract-endpoint.js +1 -0
- package/dist/semantic/datasets/dataset-endpoint.d.ts.map +1 -1
- package/dist/semantic/datasets/dataset-endpoint.js +40 -13
- package/dist/semantic/datasets/index.d.ts +1 -1
- package/dist/semantic/datasets/index.d.ts.map +1 -1
- package/dist/semantic/datasets/metric-endpoint.d.ts.map +1 -1
- package/dist/semantic/datasets/metric-endpoint.js +2 -1
- package/dist/semantic/datasets/utils/client-endpoint-binding.d.ts +22 -0
- package/dist/semantic/datasets/utils/client-endpoint-binding.d.ts.map +1 -0
- package/dist/semantic/datasets/utils/client-endpoint-binding.js +20 -0
- package/dist/semantic/datasets/utils/client-endpoint-profile.d.ts +10 -0
- package/dist/semantic/datasets/utils/client-endpoint-profile.d.ts.map +1 -0
- package/dist/semantic/datasets/utils/client-endpoint-profile.js +15 -0
- package/dist/semantic/datasets/utils/client-response-meta.d.ts +8 -0
- package/dist/semantic/datasets/utils/client-response-meta.d.ts.map +1 -0
- package/dist/semantic/datasets/utils/client-response-meta.js +28 -0
- package/dist/semantic/datasets/utils/dataset-entry.d.ts +23 -0
- package/dist/semantic/datasets/utils/dataset-entry.d.ts.map +1 -1
- package/dist/semantic/datasets/utils/dataset-entry.js +20 -0
- package/dist/semantic/datasets/utils/mcp-restrictions.d.ts +16 -0
- package/dist/semantic/datasets/utils/mcp-restrictions.d.ts.map +1 -0
- package/dist/semantic/datasets/utils/mcp-restrictions.js +33 -0
- package/dist/semantic/datasets/utils/ndjson-frames.d.ts +18 -0
- package/dist/semantic/datasets/utils/ndjson-frames.d.ts.map +1 -0
- package/dist/semantic/datasets/utils/ndjson-frames.js +56 -0
- package/dist/semantic/datasets/utils/semantic-fixture-builder.d.ts +9 -1
- package/dist/semantic/datasets/utils/semantic-fixture-builder.d.ts.map +1 -1
- package/dist/semantic/datasets/utils/semantic-fixture-builder.js +33 -5
- package/dist/semantic/datasets/utils/stream-format.d.ts +14 -0
- package/dist/semantic/datasets/utils/stream-format.d.ts.map +1 -0
- package/dist/semantic/datasets/utils/stream-format.js +50 -0
- package/dist/server/api-builder.d.ts +2 -1
- package/dist/server/api-builder.d.ts.map +1 -1
- package/dist/server/api-builder.js +3 -1
- package/dist/server/builder.d.ts +2 -1
- package/dist/server/builder.d.ts.map +1 -1
- package/dist/server/builder.js +3 -1
- package/dist/server/client-contract.d.ts +7 -0
- package/dist/server/client-contract.d.ts.map +1 -0
- package/dist/server/client-contract.js +17 -0
- package/dist/server/context-query-builder.d.ts +25 -0
- package/dist/server/context-query-builder.d.ts.map +1 -0
- package/dist/server/context-query-builder.js +51 -0
- package/dist/server/create-api.d.ts.map +1 -1
- package/dist/server/create-api.js +88 -3
- package/dist/server/mcp-source.d.ts +28 -0
- package/dist/server/mcp-source.d.ts.map +1 -1
- package/dist/server/mcp-source.js +5 -0
- package/dist/tenant.d.ts +9 -0
- package/dist/tenant.d.ts.map +1 -1
- package/dist/tenant.js +8 -0
- package/dist/type-tests/client-contract.test-d.d.ts +13 -0
- package/dist/type-tests/client-contract.test-d.d.ts.map +1 -0
- package/dist/type-tests/client-contract.test-d.js +14 -0
- package/dist/type-tests/streaming.test-d.d.ts +45 -0
- package/dist/type-tests/streaming.test-d.d.ts.map +1 -0
- package/dist/type-tests/streaming.test-d.js +59 -0
- package/dist/types.d.ts +86 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/client-schema-precondition.d.ts +31 -0
- package/dist/utils/client-schema-precondition.d.ts.map +1 -0
- package/dist/utils/client-schema-precondition.js +57 -0
- package/dist/utils/jwt-audience.d.ts +9 -0
- package/dist/utils/jwt-audience.d.ts.map +1 -0
- package/dist/utils/jwt-audience.js +27 -0
- package/dist/utils/observe-stream-body.d.ts +20 -0
- package/dist/utils/observe-stream-body.d.ts.map +1 -0
- package/dist/utils/observe-stream-body.js +83 -0
- package/dist/utils/response-drain.d.ts +4 -0
- package/dist/utils/response-drain.d.ts.map +1 -0
- package/dist/utils/response-drain.js +10 -0
- package/dist/utils/response-headers.d.ts +13 -0
- package/dist/utils/response-headers.d.ts.map +1 -0
- package/dist/utils/response-headers.js +40 -0
- package/dist/utils/stream-body.d.ts +37 -0
- package/dist/utils/stream-body.d.ts.map +1 -0
- package/dist/utils/stream-body.js +13 -0
- package/dist/utils/stream-timeout.d.ts +15 -0
- package/dist/utils/stream-timeout.d.ts.map +1 -0
- package/dist/utils/stream-timeout.js +20 -0
- package/package.json +8 -7
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ProtocolClientContractError, validateProtocolClientQuery } from '@hypequery/protocol';
|
|
2
|
+
/** The request header a generated client or MCP bridge pins an endpoint's schema hash with. */
|
|
3
|
+
export const CLIENT_SCHEMA_HEADER = 'x-hypequery-client-schema';
|
|
4
|
+
const SCHEMA_CHANGED = {
|
|
5
|
+
type: 'VALIDATION_ERROR',
|
|
6
|
+
message: 'Client endpoint schema changed.',
|
|
7
|
+
details: { code: 'CLIENT_SCHEMA_CHANGED' },
|
|
8
|
+
};
|
|
9
|
+
/** RFC 6901 pointer segments, as Python's client_query_error reports them. */
|
|
10
|
+
function pointerPath(pointer) {
|
|
11
|
+
return pointer.split('/').slice(1)
|
|
12
|
+
.map(part => /^\d+$/.test(part) ? Number(part) : part.replace(/~1/g, '/').replace(/~0/g, '~'));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* RFC 0018's execution precondition. Runs after authentication, authorization
|
|
16
|
+
* and tenant resolution, and before input validation, so a stale client is
|
|
17
|
+
* told its schema changed rather than that its body is invalid. A matching
|
|
18
|
+
* pin also holds the body to the strict client profile, as Python's opted-in
|
|
19
|
+
* endpoints do. Exposes no replacement schema.
|
|
20
|
+
*/
|
|
21
|
+
export function checkClientSchema(binding, headers, input) {
|
|
22
|
+
const pin = headers[CLIENT_SCHEMA_HEADER];
|
|
23
|
+
if (binding === undefined || pin === undefined)
|
|
24
|
+
return { kind: 'unpinned' };
|
|
25
|
+
let endpoint;
|
|
26
|
+
let contract = undefined;
|
|
27
|
+
try {
|
|
28
|
+
contract = binding.contract();
|
|
29
|
+
endpoint = contract?.endpoints[0];
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// An endpoint the contract cannot describe matches no pin a client holds.
|
|
33
|
+
endpoint = undefined;
|
|
34
|
+
}
|
|
35
|
+
if (contract === undefined || endpoint === undefined || pin !== endpoint.schemaHash) {
|
|
36
|
+
return { kind: 'refused', status: 409, error: SCHEMA_CHANGED };
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const body = structuredClone(validateProtocolClientQuery(contract, endpoint.id, input ?? {}).body);
|
|
40
|
+
if (endpoint.time !== null && body.timezone === undefined)
|
|
41
|
+
body.timezone = endpoint.time.defaultTimezone;
|
|
42
|
+
return { kind: 'pinned', endpoint, input: body };
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (!(error instanceof ProtocolClientContractError))
|
|
46
|
+
throw error;
|
|
47
|
+
return {
|
|
48
|
+
kind: 'refused',
|
|
49
|
+
status: 400,
|
|
50
|
+
error: {
|
|
51
|
+
type: 'VALIDATION_ERROR',
|
|
52
|
+
message: 'Invalid client query.',
|
|
53
|
+
details: { issues: [{ path: pointerPath(error.path), message: 'Invalid client query.', code: error.code }] },
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface JwtStrategyAudience {
|
|
2
|
+
readonly version: 1;
|
|
3
|
+
/** The `aud` values a token may carry one of; empty when the audience is not checked. */
|
|
4
|
+
readonly audiences: readonly string[];
|
|
5
|
+
}
|
|
6
|
+
export declare function attachJwtAudience<T extends object>(strategy: T, audience: string | readonly string[] | undefined): T;
|
|
7
|
+
/** The audiences a `createJwtStrategy` strategy accepts, or undefined for any other strategy. */
|
|
8
|
+
export declare function readJwtAudience(strategy: unknown): readonly string[] | undefined;
|
|
9
|
+
//# sourceMappingURL=jwt-audience.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt-audience.d.ts","sourceRoot":"","sources":["../../src/utils/jwt-audience.ts"],"names":[],"mappings":"AAWA,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;IACpB,yFAAyF;IACzF,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CACvC;AAED,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAChD,QAAQ,EAAE,CAAC,EACX,QAAQ,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,GAC/C,CAAC,CASH;AAED,iGAAiG;AACjG,wBAAgB,eAAe,CAAC,QAAQ,EAAE,OAAO,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,CAIhF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lets a host learn which audiences a JWT strategy accepts without verifying a token.
|
|
3
|
+
*
|
|
4
|
+
* `createJwtStrategy` returns a closure, so its configuration is otherwise invisible.
|
|
5
|
+
* `hypequery mcp --http` needs it to refuse a strategy that would also accept tokens
|
|
6
|
+
* issued for another API. Attached under a registered symbol so a consumer that cannot
|
|
7
|
+
* import serve (it is an optional peer) reads the same key. Keep it in step with
|
|
8
|
+
* `@hypequery/mcp`'s `http-auth.ts`.
|
|
9
|
+
*/
|
|
10
|
+
const jwtAudienceSymbol = Symbol.for('hypequery.jwt-audience.v1');
|
|
11
|
+
export function attachJwtAudience(strategy, audience) {
|
|
12
|
+
const audiences = audience === undefined ? [] : typeof audience === 'string' ? [audience] : [...audience];
|
|
13
|
+
Object.defineProperty(strategy, jwtAudienceSymbol, {
|
|
14
|
+
value: Object.freeze({ version: 1, audiences: Object.freeze(audiences) }),
|
|
15
|
+
enumerable: false,
|
|
16
|
+
configurable: false,
|
|
17
|
+
writable: false,
|
|
18
|
+
});
|
|
19
|
+
return strategy;
|
|
20
|
+
}
|
|
21
|
+
/** The audiences a `createJwtStrategy` strategy accepts, or undefined for any other strategy. */
|
|
22
|
+
export function readJwtAudience(strategy) {
|
|
23
|
+
if (typeof strategy !== 'function')
|
|
24
|
+
return undefined;
|
|
25
|
+
const value = strategy[jwtAudienceSymbol];
|
|
26
|
+
return value?.version === 1 && Array.isArray(value.audiences) ? value.audiences : undefined;
|
|
27
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ServeStreamOutcome } from '../types.js';
|
|
2
|
+
import { type ServeStreamBody } from './stream-body.js';
|
|
3
|
+
/** How an observed stream ended. `error` is set when `outcome` is `failed`. */
|
|
4
|
+
export interface StreamSettlement {
|
|
5
|
+
outcome: ServeStreamOutcome;
|
|
6
|
+
bytes: number;
|
|
7
|
+
error?: unknown;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* *body* with a stream that calls *onSettled* exactly once, when the stream
|
|
11
|
+
* ends, fails, or is cancelled. It is called before the reader sees that end
|
|
12
|
+
* but never waited for: a slow reporter must not hold the response open (and
|
|
13
|
+
* so run it into a stream timeout). A failure the body absorbed in-band (its
|
|
14
|
+
* `failure`) counts as `failed`, even when the client cancels after reading
|
|
15
|
+
* the error frame, as does a cancellation whose reason is a stream timeout.
|
|
16
|
+
* Chunks pass through unchanged and are pulled only when the reader asks, so
|
|
17
|
+
* backpressure is preserved.
|
|
18
|
+
*/
|
|
19
|
+
export declare function observeStreamBody(body: ServeStreamBody, onSettled: (settlement: StreamSettlement) => Promise<void> | void): ServeStreamBody;
|
|
20
|
+
//# sourceMappingURL=observe-stream-body.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observe-stream-body.d.ts","sourceRoot":"","sources":["../../src/utils/observe-stream-body.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtD,OAAO,EAAc,KAAK,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEpE,+EAA+E;AAC/E,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,kBAAkB,CAAC;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,eAAe,EACrB,SAAS,EAAE,CAAC,UAAU,EAAE,gBAAgB,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,GAChE,eAAe,CA8DjB"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { isStreamTimeoutError } from './stream-timeout.js';
|
|
2
|
+
import { streamBody } from './stream-body.js';
|
|
3
|
+
/**
|
|
4
|
+
* *body* with a stream that calls *onSettled* exactly once, when the stream
|
|
5
|
+
* ends, fails, or is cancelled. It is called before the reader sees that end
|
|
6
|
+
* but never waited for: a slow reporter must not hold the response open (and
|
|
7
|
+
* so run it into a stream timeout). A failure the body absorbed in-band (its
|
|
8
|
+
* `failure`) counts as `failed`, even when the client cancels after reading
|
|
9
|
+
* the error frame, as does a cancellation whose reason is a stream timeout.
|
|
10
|
+
* Chunks pass through unchanged and are pulled only when the reader asks, so
|
|
11
|
+
* backpressure is preserved.
|
|
12
|
+
*/
|
|
13
|
+
export function observeStreamBody(body, onSettled) {
|
|
14
|
+
const reader = body.stream.getReader();
|
|
15
|
+
let bytes = 0;
|
|
16
|
+
let settled = false;
|
|
17
|
+
let cancelled = false;
|
|
18
|
+
const settle = (outcome, error) => {
|
|
19
|
+
if (settled)
|
|
20
|
+
return;
|
|
21
|
+
settled = true;
|
|
22
|
+
try {
|
|
23
|
+
const reported = onSettled(outcome === 'failed' ? { outcome, bytes, error } : { outcome, bytes });
|
|
24
|
+
// Reporting must never break the response.
|
|
25
|
+
void Promise.resolve(reported).catch(() => undefined);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// Reporting must never break the response.
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/** Settles on what the body itself recorded, if anything, else on *fallback*. */
|
|
32
|
+
const settleWithAbsorbed = (fallback) => {
|
|
33
|
+
let absorbed;
|
|
34
|
+
try {
|
|
35
|
+
absorbed = body.failure?.();
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
// A failure callback that throws reports that as the failure.
|
|
39
|
+
absorbed = error ?? new Error('The stream body failure callback threw.');
|
|
40
|
+
}
|
|
41
|
+
if (absorbed !== undefined)
|
|
42
|
+
settle('failed', absorbed);
|
|
43
|
+
else
|
|
44
|
+
settle(fallback);
|
|
45
|
+
};
|
|
46
|
+
const stream = new ReadableStream({
|
|
47
|
+
async pull(controller) {
|
|
48
|
+
let chunk;
|
|
49
|
+
try {
|
|
50
|
+
chunk = await reader.read();
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
settle('failed', error);
|
|
54
|
+
if (!cancelled)
|
|
55
|
+
controller.error(error);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (cancelled)
|
|
59
|
+
return;
|
|
60
|
+
if (chunk.done) {
|
|
61
|
+
settleWithAbsorbed('completed');
|
|
62
|
+
controller.close();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
bytes += chunk.value.byteLength;
|
|
66
|
+
controller.enqueue(chunk.value);
|
|
67
|
+
},
|
|
68
|
+
async cancel(reason) {
|
|
69
|
+
cancelled = true;
|
|
70
|
+
try {
|
|
71
|
+
if (isStreamTimeoutError(reason))
|
|
72
|
+
settle('failed', reason);
|
|
73
|
+
else
|
|
74
|
+
settleWithAbsorbed('cancelled');
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
// Reporting can never skip stopping the source (and the query behind it).
|
|
78
|
+
await reader.cancel(reason).catch(() => undefined);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
}, { highWaterMark: 0 });
|
|
82
|
+
return streamBody(stream, body.contentType);
|
|
83
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-drain.d.ts","sourceRoot":"","sources":["../../src/utils/response-drain.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAE3C,0EAA0E;AAC1E,eAAO,MAAM,OAAO,GAAI,KAAK,cAAc,kBASvC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Resolves on `drain` or `close`, removing both listeners either way. */
|
|
2
|
+
export const drained = (res) => new Promise((resolve) => {
|
|
3
|
+
const done = () => {
|
|
4
|
+
res.off('drain', done);
|
|
5
|
+
res.off('close', done);
|
|
6
|
+
resolve();
|
|
7
|
+
};
|
|
8
|
+
res.on('drain', done);
|
|
9
|
+
res.on('close', done);
|
|
10
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* *headers* with `content-type` set to *value*, replacing it in any casing, so
|
|
3
|
+
* a runtime that combines duplicate names never sends two types.
|
|
4
|
+
*/
|
|
5
|
+
export declare function withContentType(headers: Record<string, string> | undefined, value: string): Record<string, string>;
|
|
6
|
+
/** *headers* with `content-type` defaulted to *value* when absent in any casing. */
|
|
7
|
+
export declare function withDefaultContentType(headers: Record<string, string> | undefined, value: string): Record<string, string>;
|
|
8
|
+
/**
|
|
9
|
+
* *extra* applied over *base*, matching names in any casing. `vary` combines
|
|
10
|
+
* instead, so `Vary: Accept` on an endpoint survives CORS adding `Vary: Origin`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function mergeResponseHeaders(base: Record<string, string> | undefined, extra: Record<string, string>): Record<string, string>;
|
|
13
|
+
//# sourceMappingURL=response-headers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-headers.d.ts","sourceRoot":"","sources":["../../src/utils/response-headers.ts"],"names":[],"mappings":"AASA;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EAC3C,KAAK,EAAE,MAAM,GACZ,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAExB;AAED,oFAAoF;AACpF,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EAC3C,KAAK,EAAE,MAAM,GACZ,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAIxB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,EACxC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC5B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAUxB"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Header names compared case-insensitively. */
|
|
2
|
+
const sameHeader = (a, b) => a.toLowerCase() === b.toLowerCase();
|
|
3
|
+
const withoutHeader = (headers, name) => Object.fromEntries(Object.entries(headers ?? {}).filter(([key]) => !sameHeader(key, name)));
|
|
4
|
+
const findHeader = (headers, name) => Object.entries(headers ?? {}).find(([key]) => sameHeader(key, name))?.[1];
|
|
5
|
+
/**
|
|
6
|
+
* *headers* with `content-type` set to *value*, replacing it in any casing, so
|
|
7
|
+
* a runtime that combines duplicate names never sends two types.
|
|
8
|
+
*/
|
|
9
|
+
export function withContentType(headers, value) {
|
|
10
|
+
return { ...withoutHeader(headers, 'content-type'), 'content-type': value };
|
|
11
|
+
}
|
|
12
|
+
/** *headers* with `content-type` defaulted to *value* when absent in any casing. */
|
|
13
|
+
export function withDefaultContentType(headers, value) {
|
|
14
|
+
return findHeader(headers, 'content-type') === undefined
|
|
15
|
+
? { 'content-type': value, ...headers }
|
|
16
|
+
: { ...headers };
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* *extra* applied over *base*, matching names in any casing. `vary` combines
|
|
20
|
+
* instead, so `Vary: Accept` on an endpoint survives CORS adding `Vary: Origin`.
|
|
21
|
+
*/
|
|
22
|
+
export function mergeResponseHeaders(base, extra) {
|
|
23
|
+
let merged = { ...base };
|
|
24
|
+
for (const [name, value] of Object.entries(extra)) {
|
|
25
|
+
const existing = findHeader(merged, name);
|
|
26
|
+
merged = withoutHeader(merged, name);
|
|
27
|
+
merged[name] = sameHeader(name, 'vary') && existing !== undefined
|
|
28
|
+
? combineVary(existing, value)
|
|
29
|
+
: value;
|
|
30
|
+
}
|
|
31
|
+
return merged;
|
|
32
|
+
}
|
|
33
|
+
const combineVary = (a, b) => {
|
|
34
|
+
const fields = [];
|
|
35
|
+
for (const field of `${a},${b}`.split(',').map(part => part.trim()).filter(Boolean)) {
|
|
36
|
+
if (!fields.some(seen => sameHeader(seen, field)))
|
|
37
|
+
fields.push(field);
|
|
38
|
+
}
|
|
39
|
+
return fields.join(', ');
|
|
40
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
declare const STREAM_BODY: unique symbol;
|
|
2
|
+
/**
|
|
3
|
+
* A response body sent as it is produced rather than serialized whole. Return
|
|
4
|
+
* one from an endpoint handler (see {@link streamBody}); the adapters write it
|
|
5
|
+
* chunk by chunk and cancel it when the client goes away.
|
|
6
|
+
*
|
|
7
|
+
* The request's `onRequestEnd` or `onError` hook, and its query log entry,
|
|
8
|
+
* fire when the stream finishes rather than when it opens, with the stream's
|
|
9
|
+
* full duration and outcome.
|
|
10
|
+
*/
|
|
11
|
+
export interface ServeStreamBody {
|
|
12
|
+
readonly [STREAM_BODY]: true;
|
|
13
|
+
/** The response's `content-type`. */
|
|
14
|
+
readonly contentType: string;
|
|
15
|
+
readonly stream: ReadableStream<Uint8Array>;
|
|
16
|
+
/**
|
|
17
|
+
* For a stream that reports a failure in-band, such as a final error frame,
|
|
18
|
+
* and then ends normally: returns the error it absorbed, or `undefined` when
|
|
19
|
+
* it succeeded. Read once the stream has ended.
|
|
20
|
+
*/
|
|
21
|
+
readonly failure?: () => unknown;
|
|
22
|
+
}
|
|
23
|
+
export interface ServeStreamBodyOptions {
|
|
24
|
+
/**
|
|
25
|
+
* For a stream that reports a failure in-band (for example a final error
|
|
26
|
+
* frame) and then ends normally: returns the error it absorbed, or
|
|
27
|
+
* `undefined` when it succeeded. Called once the stream has ended, so the
|
|
28
|
+
* failure still reaches `onError` and the query log. A stream that errors
|
|
29
|
+
* needs no `failure`: the error is reported as it is.
|
|
30
|
+
*/
|
|
31
|
+
failure?: () => unknown;
|
|
32
|
+
}
|
|
33
|
+
/** Wraps *stream* as a response body the adapters send as it is produced. */
|
|
34
|
+
export declare function streamBody(stream: ReadableStream<Uint8Array>, contentType: string, options?: ServeStreamBodyOptions): ServeStreamBody;
|
|
35
|
+
export declare function isServeStreamBody(value: unknown): value is ServeStreamBody;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=stream-body.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-body.d.ts","sourceRoot":"","sources":["../../src/utils/stream-body.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,WAAW,eAA2C,CAAC;AAE7D;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC;IAC7B,qCAAqC;IACrC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IAC5C;;;;OAIG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,OAAO,CAAC;CAClC;AAED,MAAM,WAAW,sBAAsB;IACrC;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,MAAM,OAAO,CAAC;CACzB;AAED,6EAA6E;AAC7E,wBAAgB,UAAU,CACxB,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,WAAW,EAAE,MAAM,EACnB,OAAO,GAAE,sBAA2B,GACnC,eAAe,CAOjB;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,eAAe,CAE1E"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const STREAM_BODY = Symbol.for('hypequery.serve.streamBody');
|
|
2
|
+
/** Wraps *stream* as a response body the adapters send as it is produced. */
|
|
3
|
+
export function streamBody(stream, contentType, options = {}) {
|
|
4
|
+
return {
|
|
5
|
+
[STREAM_BODY]: true,
|
|
6
|
+
contentType,
|
|
7
|
+
stream,
|
|
8
|
+
...(options.failure ? { failure: options.failure } : {}),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export function isServeStreamBody(value) {
|
|
12
|
+
return typeof value === 'object' && value !== null && value[STREAM_BODY] === true;
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why the server stopped a streamed response early: it sent nothing for the
|
|
3
|
+
* idle timeout, ran past the total stream timeout, or opened only after the
|
|
4
|
+
* request had already timed out with a 504.
|
|
5
|
+
*/
|
|
6
|
+
export type StreamTimeoutKind = 'idle' | 'total' | 'request';
|
|
7
|
+
/** The reason a stream is cancelled with when the server times it out. */
|
|
8
|
+
export declare class StreamTimeoutError extends Error {
|
|
9
|
+
readonly kind: StreamTimeoutKind;
|
|
10
|
+
readonly timeoutMs: number;
|
|
11
|
+
readonly code = "STREAM_TIMEOUT";
|
|
12
|
+
constructor(kind: StreamTimeoutKind, timeoutMs: number);
|
|
13
|
+
}
|
|
14
|
+
export declare function isStreamTimeoutError(value: unknown): value is StreamTimeoutError;
|
|
15
|
+
//# sourceMappingURL=stream-timeout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-timeout.d.ts","sourceRoot":"","sources":["../../src/utils/stream-timeout.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC;AAQ7D,0EAA0E;AAC1E,qBAAa,kBAAmB,SAAQ,KAAK;IAG/B,QAAQ,CAAC,IAAI,EAAE,iBAAiB;IAAE,QAAQ,CAAC,SAAS,EAAE,MAAM;IAFxE,QAAQ,CAAC,IAAI,oBAAoB;gBAEZ,IAAI,EAAE,iBAAiB,EAAW,SAAS,EAAE,MAAM;CAIzE;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,kBAAkB,CAEhF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const MESSAGES = {
|
|
2
|
+
idle: ms => `The stream sent nothing for ${ms}ms`,
|
|
3
|
+
total: ms => `The stream did not finish within ${ms}ms`,
|
|
4
|
+
request: ms => `The stream opened after the request timed out (${ms}ms)`,
|
|
5
|
+
};
|
|
6
|
+
/** The reason a stream is cancelled with when the server times it out. */
|
|
7
|
+
export class StreamTimeoutError extends Error {
|
|
8
|
+
kind;
|
|
9
|
+
timeoutMs;
|
|
10
|
+
code = 'STREAM_TIMEOUT';
|
|
11
|
+
constructor(kind, timeoutMs) {
|
|
12
|
+
super(MESSAGES[kind](timeoutMs));
|
|
13
|
+
this.kind = kind;
|
|
14
|
+
this.timeoutMs = timeoutMs;
|
|
15
|
+
this.name = 'StreamTimeoutError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function isStreamTimeoutError(value) {
|
|
19
|
+
return value instanceof StreamTimeoutError;
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hypequery/serve",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"description": "Code-first TypeScript runtime for validated ClickHouse analytics APIs, OpenAPI, auth, and tenancy",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"clickhouse",
|
|
@@ -34,11 +34,12 @@
|
|
|
34
34
|
"dist"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"jose": "^6.2.
|
|
37
|
+
"jose": "^6.2.15",
|
|
38
38
|
"openapi-typescript": "^7.13.0",
|
|
39
39
|
"zod": "^4.6.6",
|
|
40
40
|
"zod-to-json-schema": "^3.25.2",
|
|
41
|
-
"@hypequery/datasets": "^0.
|
|
41
|
+
"@hypequery/datasets": "^0.31.0",
|
|
42
|
+
"@hypequery/protocol": "^0.26.0"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
|
44
45
|
"tsx": "^4.0.0",
|
|
@@ -54,13 +55,13 @@
|
|
|
54
55
|
},
|
|
55
56
|
"devDependencies": {
|
|
56
57
|
"@types/express": "^5.0.6",
|
|
57
|
-
"@types/node": "^
|
|
58
|
-
"@vitest/coverage-v8": "^5.0
|
|
58
|
+
"@types/node": "^26.6.6",
|
|
59
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
59
60
|
"express": "^5.3.0",
|
|
60
|
-
"fastify": "^5.12.
|
|
61
|
+
"fastify": "^5.12.8",
|
|
61
62
|
"tsx": "^4.23.15",
|
|
62
63
|
"typescript": "^6.0.3",
|
|
63
|
-
"vitest": "^5.0
|
|
64
|
+
"vitest": "^5.1.0"
|
|
64
65
|
},
|
|
65
66
|
"repository": {
|
|
66
67
|
"type": "git",
|