@hypequery/serve 0.24.4 → 0.25.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.
Files changed (101) hide show
  1. package/dist/adapters/fetch.d.ts.map +1 -1
  2. package/dist/adapters/fetch.js +19 -6
  3. package/dist/adapters/node.d.ts +1 -1
  4. package/dist/adapters/node.d.ts.map +1 -1
  5. package/dist/adapters/node.js +110 -11
  6. package/dist/adapters/standalone.d.ts +1 -1
  7. package/dist/adapters/standalone.d.ts.map +1 -1
  8. package/dist/adapters/utils.d.ts +5 -0
  9. package/dist/adapters/utils.d.ts.map +1 -1
  10. package/dist/adapters/utils.js +8 -0
  11. package/dist/auth.d.ts +1 -1
  12. package/dist/auth.d.ts.map +1 -1
  13. package/dist/auth.js +6 -0
  14. package/dist/client-config.d.ts +8 -7
  15. package/dist/client-config.d.ts.map +1 -1
  16. package/dist/client-config.js +16 -7
  17. package/dist/cors.d.ts.map +1 -1
  18. package/dist/cors.js +2 -1
  19. package/dist/dev.d.ts +1 -1
  20. package/dist/index.d.ts +3 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +2 -0
  23. package/dist/pipeline.d.ts +2 -0
  24. package/dist/pipeline.d.ts.map +1 -1
  25. package/dist/pipeline.js +81 -10
  26. package/dist/query-logger.d.ts +6 -0
  27. package/dist/query-logger.d.ts.map +1 -1
  28. package/dist/query-logger.js +6 -0
  29. package/dist/semantic/datasets/contract-endpoint.d.ts.map +1 -1
  30. package/dist/semantic/datasets/contract-endpoint.js +1 -0
  31. package/dist/semantic/datasets/dataset-endpoint.d.ts.map +1 -1
  32. package/dist/semantic/datasets/dataset-endpoint.js +40 -13
  33. package/dist/semantic/datasets/index.d.ts +1 -1
  34. package/dist/semantic/datasets/index.d.ts.map +1 -1
  35. package/dist/semantic/datasets/metric-endpoint.d.ts.map +1 -1
  36. package/dist/semantic/datasets/metric-endpoint.js +2 -1
  37. package/dist/semantic/datasets/utils/client-endpoint-binding.d.ts +22 -0
  38. package/dist/semantic/datasets/utils/client-endpoint-binding.d.ts.map +1 -0
  39. package/dist/semantic/datasets/utils/client-endpoint-binding.js +20 -0
  40. package/dist/semantic/datasets/utils/client-endpoint-profile.d.ts +10 -0
  41. package/dist/semantic/datasets/utils/client-endpoint-profile.d.ts.map +1 -0
  42. package/dist/semantic/datasets/utils/client-endpoint-profile.js +15 -0
  43. package/dist/semantic/datasets/utils/client-response-meta.d.ts +8 -0
  44. package/dist/semantic/datasets/utils/client-response-meta.d.ts.map +1 -0
  45. package/dist/semantic/datasets/utils/client-response-meta.js +28 -0
  46. package/dist/semantic/datasets/utils/dataset-entry.d.ts +23 -0
  47. package/dist/semantic/datasets/utils/dataset-entry.d.ts.map +1 -1
  48. package/dist/semantic/datasets/utils/dataset-entry.js +20 -0
  49. package/dist/semantic/datasets/utils/ndjson-frames.d.ts +18 -0
  50. package/dist/semantic/datasets/utils/ndjson-frames.d.ts.map +1 -0
  51. package/dist/semantic/datasets/utils/ndjson-frames.js +56 -0
  52. package/dist/semantic/datasets/utils/semantic-fixture-builder.d.ts +9 -1
  53. package/dist/semantic/datasets/utils/semantic-fixture-builder.d.ts.map +1 -1
  54. package/dist/semantic/datasets/utils/semantic-fixture-builder.js +33 -5
  55. package/dist/semantic/datasets/utils/stream-format.d.ts +14 -0
  56. package/dist/semantic/datasets/utils/stream-format.d.ts.map +1 -0
  57. package/dist/semantic/datasets/utils/stream-format.js +50 -0
  58. package/dist/server/api-builder.d.ts +2 -1
  59. package/dist/server/api-builder.d.ts.map +1 -1
  60. package/dist/server/api-builder.js +3 -1
  61. package/dist/server/builder.d.ts +2 -1
  62. package/dist/server/builder.d.ts.map +1 -1
  63. package/dist/server/builder.js +3 -1
  64. package/dist/server/client-contract.d.ts +7 -0
  65. package/dist/server/client-contract.d.ts.map +1 -0
  66. package/dist/server/client-contract.js +17 -0
  67. package/dist/server/context-query-builder.d.ts +25 -0
  68. package/dist/server/context-query-builder.d.ts.map +1 -0
  69. package/dist/server/context-query-builder.js +51 -0
  70. package/dist/server/create-api.d.ts.map +1 -1
  71. package/dist/server/create-api.js +73 -3
  72. package/dist/tenant.d.ts +9 -0
  73. package/dist/tenant.d.ts.map +1 -1
  74. package/dist/tenant.js +8 -0
  75. package/dist/type-tests/client-contract.test-d.d.ts +13 -0
  76. package/dist/type-tests/client-contract.test-d.d.ts.map +1 -0
  77. package/dist/type-tests/client-contract.test-d.js +14 -0
  78. package/dist/type-tests/streaming.test-d.d.ts +45 -0
  79. package/dist/type-tests/streaming.test-d.d.ts.map +1 -0
  80. package/dist/type-tests/streaming.test-d.js +59 -0
  81. package/dist/types.d.ts +86 -2
  82. package/dist/types.d.ts.map +1 -1
  83. package/dist/utils/client-schema-precondition.d.ts +31 -0
  84. package/dist/utils/client-schema-precondition.d.ts.map +1 -0
  85. package/dist/utils/client-schema-precondition.js +57 -0
  86. package/dist/utils/observe-stream-body.d.ts +20 -0
  87. package/dist/utils/observe-stream-body.d.ts.map +1 -0
  88. package/dist/utils/observe-stream-body.js +83 -0
  89. package/dist/utils/response-drain.d.ts +4 -0
  90. package/dist/utils/response-drain.d.ts.map +1 -0
  91. package/dist/utils/response-drain.js +10 -0
  92. package/dist/utils/response-headers.d.ts +13 -0
  93. package/dist/utils/response-headers.d.ts.map +1 -0
  94. package/dist/utils/response-headers.js +40 -0
  95. package/dist/utils/stream-body.d.ts +37 -0
  96. package/dist/utils/stream-body.d.ts.map +1 -0
  97. package/dist/utils/stream-body.js +13 -0
  98. package/dist/utils/stream-timeout.d.ts +15 -0
  99. package/dist/utils/stream-timeout.d.ts.map +1 -0
  100. package/dist/utils/stream-timeout.js +20 -0
  101. package/package.json +8 -7
@@ -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,4 @@
1
+ import type { ServerResponse } from 'http';
2
+ /** Resolves on `drain` or `close`, removing both listeners either way. */
3
+ export declare const drained: (res: ServerResponse) => Promise<void>;
4
+ //# sourceMappingURL=response-drain.d.ts.map
@@ -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.24.4",
3
+ "version": "0.25.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.14",
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.29.0"
41
+ "@hypequery/datasets": "^0.30.0",
42
+ "@hypequery/protocol": "^0.25.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": "^20.11.30",
58
- "@vitest/coverage-v8": "^5.0.5",
58
+ "@types/node": "^26.6.6",
59
+ "@vitest/coverage-v8": "^5.1.0",
59
60
  "express": "^5.3.0",
60
- "fastify": "^5.12.7",
61
+ "fastify": "^5.12.8",
61
62
  "tsx": "^4.23.15",
62
63
  "typescript": "^6.0.3",
63
- "vitest": "^5.0.5"
64
+ "vitest": "^5.1.0"
64
65
  },
65
66
  "repository": {
66
67
  "type": "git",