@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.
- 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 +6 -0
- 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 +3 -0
- 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/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 +73 -3
- 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/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
package/dist/pipeline.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import {
|
|
2
|
+
import { isServeStreamBody } from './utils/stream-body.js';
|
|
3
|
+
import { observeStreamBody } from './utils/observe-stream-body.js';
|
|
4
|
+
import { isStreamTimeoutError } from './utils/stream-timeout.js';
|
|
5
|
+
import { mergeResponseHeaders } from './utils/response-headers.js';
|
|
6
|
+
import { resolveTenantMode, warnTenantMisconfiguration } from './tenant.js';
|
|
3
7
|
import { applySemanticTenantRuntime } from './semantic/utils/tenant-runtime.js';
|
|
4
8
|
import { generateRequestId, validateCorrelationId } from './utils.js';
|
|
5
9
|
import { withNoStoreErrorHeaders } from './utils/error-headers.js';
|
|
6
10
|
import { buildOpenApiDocument } from './openapi.js';
|
|
7
11
|
import { buildDocsHtml } from './docs-ui.js';
|
|
8
12
|
import { ServeHttpError } from './errors.js';
|
|
13
|
+
import { checkClientSchema } from './utils/client-schema-precondition.js';
|
|
14
|
+
import { clientResponse } from './semantic/datasets/utils/client-response-meta.js';
|
|
9
15
|
import { checkRoleAuthorization, checkScopeAuthorization, AuthError, } from './auth.js';
|
|
10
16
|
import { handleCorsRequest } from './cors.js';
|
|
11
17
|
import { resolveLocalAuthRequirement } from './auth-requirement.js';
|
|
@@ -295,7 +301,9 @@ export const executeEndpoint = async (options) => {
|
|
|
295
301
|
// The pipeline owns the authenticated principal: neither the context factory
|
|
296
302
|
// nor caller-supplied context may replace it after authorization ran.
|
|
297
303
|
context.auth = authContext;
|
|
298
|
-
const activeTenantConfig =
|
|
304
|
+
const activeTenantConfig = endpoint.tenantIndependent
|
|
305
|
+
? undefined
|
|
306
|
+
: resolveTenantConfig(tenantConfig, endpoint.tenant);
|
|
299
307
|
if (activeTenantConfig) {
|
|
300
308
|
const tenantRequired = activeTenantConfig.required !== false;
|
|
301
309
|
const tenantId = authContext ? activeTenantConfig.extract(authContext) : null;
|
|
@@ -318,7 +326,7 @@ export const executeEndpoint = async (options) => {
|
|
|
318
326
|
}
|
|
319
327
|
if (tenantId) {
|
|
320
328
|
context.tenantId = tenantId;
|
|
321
|
-
const mode = activeTenantConfig
|
|
329
|
+
const mode = resolveTenantMode(activeTenantConfig);
|
|
322
330
|
const column = activeTenantConfig.column;
|
|
323
331
|
applySemanticTenantRuntime(context, {
|
|
324
332
|
queryKey: endpoint.key,
|
|
@@ -333,11 +341,20 @@ export const executeEndpoint = async (options) => {
|
|
|
333
341
|
queryKey: endpoint.key,
|
|
334
342
|
hasTenantConfig: true,
|
|
335
343
|
hasTenantId: false,
|
|
336
|
-
mode: activeTenantConfig
|
|
344
|
+
mode: resolveTenantMode(activeTenantConfig),
|
|
337
345
|
});
|
|
338
346
|
}
|
|
339
347
|
}
|
|
340
|
-
|
|
348
|
+
// Before input validation: a stale client must learn its schema changed,
|
|
349
|
+
// not that a field it was generated with no longer validates.
|
|
350
|
+
const clientSchema = checkClientSchema(endpoint.clientEndpoint, request.headers, context.input);
|
|
351
|
+
if (clientSchema.kind === 'refused') {
|
|
352
|
+
return createErrorResponse(clientSchema.status, clientSchema.error.type, clientSchema.error.message, clientSchema.error.details, { 'x-request-id': requestId, ...traceHeaders, 'cache-control': 'no-store' });
|
|
353
|
+
}
|
|
354
|
+
if (clientSchema.kind === 'pinned') {
|
|
355
|
+
context.input = clientSchema.input;
|
|
356
|
+
}
|
|
357
|
+
else if (inputCameFromQuery(request)) {
|
|
341
358
|
context.input = coerceQueryInput(endpoint.inputSchema, context.input);
|
|
342
359
|
}
|
|
343
360
|
const validationResult = validateInput(endpoint.inputSchema, context.input);
|
|
@@ -360,17 +377,69 @@ export const executeEndpoint = async (options) => {
|
|
|
360
377
|
...(globalMiddlewares ?? []),
|
|
361
378
|
...endpoint.middlewares,
|
|
362
379
|
];
|
|
363
|
-
const
|
|
380
|
+
const handled = await runMiddlewares(pipeline, context, () => endpoint.handler(context));
|
|
381
|
+
const streamed = isServeStreamBody(handled);
|
|
382
|
+
// A streamed body goes out as produced; the client-contract envelope is for JSON pages.
|
|
383
|
+
const result = clientSchema.kind === 'pinned' && !streamed
|
|
384
|
+
? clientResponse(handled, requestId, clientSchema.input)
|
|
385
|
+
: handled;
|
|
364
386
|
const headers = withManagedTraceHeaders(endpoint.defaultHeaders, requestId, correlationId);
|
|
365
387
|
// Authenticated and tenant-aware responses must never be shared by an
|
|
366
388
|
// intermediary cache. Endpoint TTLs only opt public, tenant-independent
|
|
367
|
-
// responses into shared caching.
|
|
368
|
-
if (requiresAuth || authContext || activeTenantConfig) {
|
|
389
|
+
// responses into shared caching. A stream is one query's live result.
|
|
390
|
+
if (streamed || requiresAuth || authContext || activeTenantConfig) {
|
|
369
391
|
headers['cache-control'] = 'no-store';
|
|
370
392
|
}
|
|
371
393
|
else if (typeof cacheTtlMs === 'number') {
|
|
372
394
|
headers['cache-control'] = cacheTtlMs > 0 ? `public, max-age=${Math.floor(cacheTtlMs / 1000)}` : 'no-store';
|
|
373
395
|
}
|
|
396
|
+
if (isServeStreamBody(result)) {
|
|
397
|
+
// The status goes out now, but the outcome is known only once the
|
|
398
|
+
// stream ends: report the end (or the failure) then, with its duration.
|
|
399
|
+
const openDurationMs = Date.now() - startedAt;
|
|
400
|
+
const body = observeStreamBody(result, async ({ outcome, bytes, error }) => {
|
|
401
|
+
const durationMs = Date.now() - startedAt;
|
|
402
|
+
const stream = { outcome, bytes, openDurationMs };
|
|
403
|
+
const event = {
|
|
404
|
+
requestId,
|
|
405
|
+
queryKey: endpoint.key,
|
|
406
|
+
metadata: metadataWithAuth,
|
|
407
|
+
request,
|
|
408
|
+
auth: context.auth,
|
|
409
|
+
durationMs,
|
|
410
|
+
stream,
|
|
411
|
+
};
|
|
412
|
+
const failed = outcome === 'failed';
|
|
413
|
+
// Logged first, synchronously: the stream does not wait for the hooks.
|
|
414
|
+
if (queryLogger?.listenerCount ?? 0 > 0) {
|
|
415
|
+
queryLogger?.emit({
|
|
416
|
+
requestId,
|
|
417
|
+
endpointKey: endpoint.key,
|
|
418
|
+
path: endpoint.metadata.path ?? `/${endpoint.key}`,
|
|
419
|
+
method: request.method,
|
|
420
|
+
status: failed ? 'error' : 'completed',
|
|
421
|
+
startTime: startedAt,
|
|
422
|
+
endTime: startedAt + durationMs,
|
|
423
|
+
durationMs,
|
|
424
|
+
input: context.input,
|
|
425
|
+
// The status the client received: the stream's 200, or the 504 the
|
|
426
|
+
// adapter sent before a stream that opened too late was cancelled.
|
|
427
|
+
responseStatus: isStreamTimeoutError(error) && error.kind === 'request' ? 504 : 200,
|
|
428
|
+
...(failed
|
|
429
|
+
? { error: error instanceof Error ? error : new Error(String(error)) }
|
|
430
|
+
: { result: body }),
|
|
431
|
+
stream,
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
if (failed) {
|
|
435
|
+
await safeInvokeHook('onError', hooks.onError, { ...event, error });
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
await safeInvokeHook('onRequestEnd', hooks.onRequestEnd, { ...event, result: body });
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
return { status: 200, headers, body };
|
|
442
|
+
}
|
|
374
443
|
const durationMs = Date.now() - startedAt;
|
|
375
444
|
await safeInvokeHook('onRequestEnd', hooks.onRequestEnd, {
|
|
376
445
|
requestId,
|
|
@@ -463,7 +532,7 @@ export const createServeHandler = ({ router, globalMiddlewares, authStrategies,
|
|
|
463
532
|
const endpoint = router.match(request.method, request.path);
|
|
464
533
|
if (!endpoint) {
|
|
465
534
|
const response = createErrorResponse(404, 'NOT_FOUND', `No endpoint registered for ${request.method} ${request.path}`, undefined, { 'x-request-id': requestId, ...traceHeaders });
|
|
466
|
-
response.headers =
|
|
535
|
+
response.headers = mergeResponseHeaders(response.headers, corsHeaders);
|
|
467
536
|
return response;
|
|
468
537
|
}
|
|
469
538
|
const response = await executeEndpoint({
|
|
@@ -480,7 +549,7 @@ export const createServeHandler = ({ router, globalMiddlewares, authStrategies,
|
|
|
480
549
|
});
|
|
481
550
|
// Inject CORS headers into every response
|
|
482
551
|
if (Object.keys(corsHeaders).length > 0) {
|
|
483
|
-
response.headers =
|
|
552
|
+
response.headers = mergeResponseHeaders(response.headers, corsHeaders);
|
|
484
553
|
}
|
|
485
554
|
return response;
|
|
486
555
|
};
|
|
@@ -489,6 +558,7 @@ export const createOpenApiEndpoint = (path, getEndpoints, options) => {
|
|
|
489
558
|
let cachedDocument = null;
|
|
490
559
|
return {
|
|
491
560
|
key: '__hypequery_openapi__',
|
|
561
|
+
tenantIndependent: true,
|
|
492
562
|
method: 'GET',
|
|
493
563
|
inputSchema: undefined,
|
|
494
564
|
outputSchema: z.any(),
|
|
@@ -517,6 +587,7 @@ export const createOpenApiEndpoint = (path, getEndpoints, options) => {
|
|
|
517
587
|
};
|
|
518
588
|
export const createDocsEndpoint = (path, openapiPath, options) => ({
|
|
519
589
|
key: '__hypequery_docs__',
|
|
590
|
+
tenantIndependent: true,
|
|
520
591
|
method: 'GET',
|
|
521
592
|
inputSchema: undefined,
|
|
522
593
|
outputSchema: z.string(),
|
package/dist/query-logger.d.ts
CHANGED
|
@@ -4,8 +4,12 @@
|
|
|
4
4
|
* Fires for every endpoint execution regardless of the underlying
|
|
5
5
|
* query backend (ClickHouse, BigQuery, mock data, etc.).
|
|
6
6
|
*/
|
|
7
|
+
import type { ServeStreamInfo } from './types.js';
|
|
7
8
|
/**
|
|
8
9
|
* Event emitted by the serve-layer query logger.
|
|
10
|
+
*
|
|
11
|
+
* For a streamed response, the `completed` or `error` event is emitted once the
|
|
12
|
+
* stream ends, with its full duration, and carries {@link ServeQueryEvent.stream}.
|
|
9
13
|
*/
|
|
10
14
|
export interface ServeQueryEvent {
|
|
11
15
|
requestId: string;
|
|
@@ -20,6 +24,8 @@ export interface ServeQueryEvent {
|
|
|
20
24
|
responseStatus?: number;
|
|
21
25
|
error?: Error;
|
|
22
26
|
result?: unknown;
|
|
27
|
+
/** Set for a streamed response: how it ended and how much it sent. */
|
|
28
|
+
stream?: ServeStreamInfo;
|
|
23
29
|
}
|
|
24
30
|
/**
|
|
25
31
|
* Callback for serve query events.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query-logger.d.ts","sourceRoot":"","sources":["../src/query-logger.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH
|
|
1
|
+
{"version":3,"file":"query-logger.d.ts","sourceRoot":"","sources":["../src/query-logger.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,SAAS,GAAG,WAAW,GAAG,OAAO,CAAC;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,sEAAsE;IACtE,MAAM,CAAC,EAAE,eAAe,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;AAEvE;;;;;;GAMG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,SAAS,CAAsC;IAEvD;;;OAGG;IACH,EAAE,CAAC,QAAQ,EAAE,uBAAuB,GAAG,MAAM,IAAI;IAOjD;;OAEG;IACH,IAAI,CAAC,KAAK,EAAE,eAAe,GAAG,IAAI;IAUlC;;OAEG;IACH,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED;;OAEG;IACH,SAAS,IAAI,IAAI;CAGlB;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,GAAG,IAAI,CActE;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,GAAG,IAAI,CAoB1E"}
|
package/dist/query-logger.js
CHANGED
|
@@ -63,6 +63,9 @@ export function formatQueryEvent(event) {
|
|
|
63
63
|
if (event.status === 'error' && event.error) {
|
|
64
64
|
line += ` — ${event.error.message}`;
|
|
65
65
|
}
|
|
66
|
+
else if (event.stream?.outcome === 'cancelled') {
|
|
67
|
+
line += ' — client disconnected';
|
|
68
|
+
}
|
|
66
69
|
return line;
|
|
67
70
|
}
|
|
68
71
|
/**
|
|
@@ -84,6 +87,9 @@ export function formatQueryEventJSON(event) {
|
|
|
84
87
|
...(event.status === 'error' && event.error
|
|
85
88
|
? { error: event.error.message }
|
|
86
89
|
: {}),
|
|
90
|
+
...(event.stream
|
|
91
|
+
? { stream: { outcome: event.stream.outcome, bytes: event.stream.bytes } }
|
|
92
|
+
: {}),
|
|
87
93
|
timestamp: new Date(event.endTime ?? event.startTime).toISOString(),
|
|
88
94
|
});
|
|
89
95
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contract-endpoint.d.ts","sourceRoot":"","sources":["../../../src/semantic/datasets/contract-endpoint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAClF,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAKhG;;;GAGG;AACH,wBAAgB,2BAA2B,CACzC,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC,EAC7B,OAAO,CAAC,EAAE,aAAa,CAAC,GAAG,CAAC,GAC3B,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAetC;AAED,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,gBAAgB,GAClC,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,CAAC,
|
|
1
|
+
{"version":3,"file":"contract-endpoint.d.ts","sourceRoot":"","sources":["../../../src/semantic/datasets/contract-endpoint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAClF,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAKhG;;;GAGG;AACH,wBAAgB,2BAA2B,CACzC,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC,EAC7B,OAAO,CAAC,EAAE,aAAa,CAAC,GAAG,CAAC,GAC3B,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAetC;AAED,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,gBAAgB,GAClC,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,CAAC,CAgC/D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataset-endpoint.d.ts","sourceRoot":"","sources":["../../../src/semantic/datasets/dataset-endpoint.ts"],"names":[],"mappings":"AACA;;;;;;;;;GASG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EACV,WAAW,EAGX,aAAa,EAEd,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EACV,aAAa,EACb,uBAAuB,EACxB,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"dataset-endpoint.d.ts","sourceRoot":"","sources":["../../../src/semantic/datasets/dataset-endpoint.ts"],"names":[],"mappings":"AACA;;;;;;;;;GASG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EACV,WAAW,EAGX,aAAa,EAEd,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EACV,aAAa,EACb,uBAAuB,EACxB,MAAM,qBAAqB,CAAC;AAU7B,OAAO,EAA6C,KAAK,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAUxG,OAAO,EAA8B,KAAK,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAE5G,YAAY,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAuB7D,QAAA,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;iBAGvB,CAAC;AAMH,wBAAgB,qBAAqB,CAAC,KAAK,SAAS,WAAW,EAC7D,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,YAAY,CAAC,KAAK,CAAC,EAC1B,SAAS,EAAE,aAAa,EACxB,cAAc,EAAE,uBAAuB,EACvC,eAAe,CAAC,EAAE,sBAAsB,GACvC,aAAa,CAAC,CAAC,CAAC,UAAU,EAAE,OAAO,mBAAmB,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,CAAC,CAsJ1E"}
|
|
@@ -11,11 +11,16 @@ import { semanticResponseMeta } from './utils/semantic-response-meta.js';
|
|
|
11
11
|
*/
|
|
12
12
|
import { z } from 'zod';
|
|
13
13
|
import { buildDatasetInputSchema } from '@hypequery/datasets/schemas';
|
|
14
|
+
import { datasetForStream } from '@hypequery/datasets/internal';
|
|
14
15
|
import { ServeHttpError } from '../../errors.js';
|
|
15
16
|
import { resolveSemanticExecutionRuntime, resolveSemanticQueryBuilder, } from '../query-builder-context.js';
|
|
16
17
|
import { buildDatasetQueryDescription } from './utils/dataset-query-metadata.js';
|
|
17
|
-
import { resolveDatasetEntry } from './utils/dataset-entry.js';
|
|
18
|
+
import { resolveDatasetEntry, resolveStreamMaxRows } from './utils/dataset-entry.js';
|
|
19
|
+
import { ARROW_STREAM_MEDIA_TYPE, NDJSON_MEDIA_TYPE, requestedStreamFormat, } from './utils/stream-format.js';
|
|
20
|
+
import { datasetNdjsonStream } from './utils/ndjson-frames.js';
|
|
21
|
+
import { streamBody } from '../../utils/stream-body.js';
|
|
18
22
|
import { resolveLocalAuthRequirement } from '../../auth-requirement.js';
|
|
23
|
+
import { endpointMaxLimit } from './utils/client-endpoint-profile.js';
|
|
19
24
|
import { assertRelatedDatasetAccess } from './utils/related-dataset-policy.js';
|
|
20
25
|
// ---------------------------------------------------------------------------
|
|
21
26
|
// Zod schemas for dataset query input / output
|
|
@@ -46,7 +51,8 @@ const datasetResultSchema = z.object({
|
|
|
46
51
|
export function createDatasetEndpoint(name, entry, analytics, builderFactory, relatedPolicies) {
|
|
47
52
|
const resolved = resolveDatasetEntry(entry);
|
|
48
53
|
const ds = resolved.dataset;
|
|
49
|
-
const effectiveMaxLimit = resolved.maxLimit
|
|
54
|
+
const effectiveMaxLimit = endpointMaxLimit(ds, resolved.maxLimit);
|
|
55
|
+
const streamMaxRows = resolveStreamMaxRows(name, ds, resolved.stream, effectiveMaxLimit);
|
|
50
56
|
// Build a schema whose dimension/measure/filter fields are enumerated from
|
|
51
57
|
// this dataset's contract, so OpenAPI/docs and clients see the valid fields.
|
|
52
58
|
const datasetQueryInputSchema = buildDatasetInputSchema(ds, {
|
|
@@ -84,6 +90,11 @@ export function createDatasetEndpoint(name, entry, analytics, builderFactory, re
|
|
|
84
90
|
const input = ctx.input ?? {};
|
|
85
91
|
const start = Date.now();
|
|
86
92
|
const runtime = resolveSemanticExecutionRuntime(semanticContext);
|
|
93
|
+
// A stream is asked for with Accept; an endpoint that does not stream answers with a page.
|
|
94
|
+
const streamFormat = streamMaxRows === undefined
|
|
95
|
+
? undefined
|
|
96
|
+
: requestedStreamFormat(ctx.request?.headers?.accept);
|
|
97
|
+
const rowCap = streamFormat ? streamMaxRows : effectiveMaxLimit;
|
|
87
98
|
// Validate
|
|
88
99
|
const query = {
|
|
89
100
|
dimensions: input.dimensions,
|
|
@@ -92,7 +103,7 @@ export function createDatasetEndpoint(name, entry, analytics, builderFactory, re
|
|
|
92
103
|
having: input.having,
|
|
93
104
|
segments: input.segments,
|
|
94
105
|
orderBy: input.orderBy,
|
|
95
|
-
limit: Math.min(input.limit ??
|
|
106
|
+
limit: Math.min(input.limit ?? rowCap, rowCap),
|
|
96
107
|
offset: input.offset,
|
|
97
108
|
by: input.by,
|
|
98
109
|
timeKey: input.timeKey,
|
|
@@ -100,7 +111,8 @@ export function createDatasetEndpoint(name, entry, analytics, builderFactory, re
|
|
|
100
111
|
};
|
|
101
112
|
assertRelatedDatasetAccess(ds, query, ctx.auth ?? null, relatedPolicies);
|
|
102
113
|
const validationContext = runtime ? { runtime } : undefined;
|
|
103
|
-
|
|
114
|
+
// A stream is validated against the dataset's stream ceiling (maxStreamRows).
|
|
115
|
+
const validation = analytics.validate(streamFormat ? datasetForStream(ds) : ds, query, validationContext);
|
|
104
116
|
if (!validation.valid) {
|
|
105
117
|
throw new ServeHttpError(400, 'VALIDATION_ERROR', validation.errors.join('; '));
|
|
106
118
|
}
|
|
@@ -111,20 +123,33 @@ export function createDatasetEndpoint(name, entry, analytics, builderFactory, re
|
|
|
111
123
|
throw new ServeHttpError(500, 'INTERNAL_SERVER_ERROR', `Dataset endpoint "${name}" requires dataset tenantKey and serve tenant runtime when tenant isolation is enabled.`);
|
|
112
124
|
}
|
|
113
125
|
}
|
|
126
|
+
const executionRuntime = {
|
|
127
|
+
...runtime,
|
|
128
|
+
builderFactory: runtimeBuilderFactory,
|
|
129
|
+
tenant: runtime?.tenant,
|
|
130
|
+
};
|
|
131
|
+
// Meta — opt in via the `includeMeta` input field or the x-include-meta header.
|
|
132
|
+
const includeMeta = input.includeMeta === true
|
|
133
|
+
|| ctx.request?.headers?.['x-include-meta'] === 'true';
|
|
134
|
+
if (streamFormat === 'arrow') {
|
|
135
|
+
const { data } = await analytics.streamArrow(ds, query, { runtime: executionRuntime });
|
|
136
|
+
return streamBody(data, ARROW_STREAM_MEDIA_TYPE);
|
|
137
|
+
}
|
|
138
|
+
if (streamFormat === 'ndjson') {
|
|
139
|
+
const { data, meta } = await analytics.stream(ds, query, { runtime: executionRuntime });
|
|
140
|
+
const startMeta = includeMeta
|
|
141
|
+
? semanticResponseMeta({ ...meta, timingMs: Date.now() - start }, resolved.trustedDiagnostics)
|
|
142
|
+
: undefined;
|
|
143
|
+
const frames = datasetNdjsonStream(data, startMeta);
|
|
144
|
+
return streamBody(frames.stream, NDJSON_MEDIA_TYPE, { failure: frames.failure });
|
|
145
|
+
}
|
|
114
146
|
const result = await analytics.execute(ds, query, {
|
|
115
|
-
runtime:
|
|
116
|
-
...runtime,
|
|
117
|
-
builderFactory: runtimeBuilderFactory,
|
|
118
|
-
tenant: runtime?.tenant,
|
|
119
|
-
},
|
|
147
|
+
runtime: executionRuntime,
|
|
120
148
|
cache: typeof resolved.cache === 'number' && resolved.cache > 0
|
|
121
149
|
? { ttlMs: resolved.cache }
|
|
122
150
|
: undefined,
|
|
123
151
|
});
|
|
124
152
|
const timingMs = Date.now() - start;
|
|
125
|
-
// Meta — opt in via the `includeMeta` input field or the x-include-meta header.
|
|
126
|
-
const includeMeta = input.includeMeta === true
|
|
127
|
-
|| ctx.request?.headers?.['x-include-meta'] === 'true';
|
|
128
153
|
return {
|
|
129
154
|
data: result.data,
|
|
130
155
|
meta: includeMeta
|
|
@@ -144,7 +169,9 @@ export function createDatasetEndpoint(name, entry, analytics, builderFactory, re
|
|
|
144
169
|
tenant: resolved.tenant,
|
|
145
170
|
metadata,
|
|
146
171
|
cacheTtlMs: resolved.cache ?? null,
|
|
147
|
-
|
|
172
|
+
// The same request answers with a JSON page or a stream depending on
|
|
173
|
+
// Accept, so a cache must key on it.
|
|
174
|
+
defaultHeaders: streamMaxRows === undefined ? undefined : { vary: 'Accept' },
|
|
148
175
|
requiredRoles: resolved.requiredRoles,
|
|
149
176
|
requiredScopes: resolved.requiredScopes,
|
|
150
177
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { dataset, dimension, measure, belongsTo, hasMany, hasOne, sum, count, countDistinct, avg, min, max, divide, multiply, subtract, add, nullIfZero, coalesce, round, floor, ceil, eq, neq, gt, gte, lt, lte, inList, notInList, between, like, isNull, isNotNull, asc, desc, filter, order, createDatasetRegistry, publishDatasets, } from '@hypequery/datasets';
|
|
2
|
-
export type { FieldType, DimensionType, DimensionOptions, DimensionDefinition, MeasureOptions, MeasureDefinition, InferDimensionType, RelationshipKind, RelationshipDefinition, AggregationType, MeasureAggregation, AggregationSpec, FormulaExpr, DerivedMetricSpec, TimeGrain, MetricRef, BaseMetricRef, DerivedMetricRef, GrainedMetricRef, MetricContract, MetricFilter, MetricOrderBy, MetricQuery, DatasetQuery, MetricResultMeta, MetricResult, DatasetQueryResult, MetricHandle, ExecutionContext, SemanticExecutionRuntime, SemanticTenantRuntime, SemanticFilterDefinition, SemanticFiltersDefinition, DatasetConfig, DatasetLimits, DatasetInstance, BaseMetricConfig, DerivedMetricConfig, DatasetRegistryInstance, DatasetFieldNames, DatasetPublisher, PublishableMetric, PublishedMetricHandle, PublishedMetricMap, PublishedMetrics, PublishedDataset, PublishedDatasetRegistry, PublishDatasetOptions, } from '@hypequery/datasets';
|
|
2
|
+
export type { FieldType, DimensionType, DimensionOptions, DimensionDefinition, MeasureOptions, MeasureDefinition, InferDimensionType, DimensionResultValue, RelationshipKind, RelationshipDefinition, AggregationType, MeasureAggregation, AggregationSpec, FormulaExpr, DerivedMetricSpec, TimeGrain, MetricRef, BaseMetricRef, DerivedMetricRef, GrainedMetricRef, MetricContract, MetricFilter, MetricOrderBy, MetricQuery, DatasetQuery, MetricResultMeta, MetricResult, DatasetQueryResult, MetricHandle, ExecutionContext, SemanticExecutionRuntime, SemanticTenantRuntime, SemanticFilterDefinition, SemanticFiltersDefinition, DatasetConfig, DatasetLimits, DatasetInstance, BaseMetricConfig, DerivedMetricConfig, DatasetRegistryInstance, DatasetFieldNames, DatasetPublisher, PublishableMetric, PublishedMetricHandle, PublishedMetricMap, PublishedMetrics, PublishedDataset, PublishedDatasetRegistry, PublishDatasetOptions, } from '@hypequery/datasets';
|
|
3
3
|
export { createMetricEndpoint } from './metric-endpoint.js';
|
|
4
4
|
export { createDatasetEndpoint } from './dataset-endpoint.js';
|
|
5
5
|
export { createSemanticContractEndpoint, buildSemanticContractSource, } from './contract-endpoint.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/semantic/datasets/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,OAAO,EACP,SAAS,EACT,OAAO,EACP,SAAS,EACT,OAAO,EACP,MAAM,EACN,GAAG,EACH,KAAK,EACL,aAAa,EACb,GAAG,EACH,GAAG,EACH,GAAG,EACH,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,GAAG,EACH,UAAU,EACV,QAAQ,EACR,KAAK,EACL,KAAK,EACL,IAAI,EACJ,EAAE,EACF,GAAG,EACH,EAAE,EACF,GAAG,EACH,EAAE,EACF,GAAG,EACH,MAAM,EACN,SAAS,EACT,OAAO,EACP,IAAI,EACJ,MAAM,EACN,SAAS,EACT,GAAG,EACH,IAAI,EACJ,MAAM,EACN,KAAK,EACL,qBAAqB,EACrB,eAAe,GAChB,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EACV,SAAS,EACT,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,eAAe,EACf,kBAAkB,EAClB,eAAe,EACf,WAAW,EACX,iBAAiB,EACjB,SAAS,EACT,SAAS,EACT,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,wBAAwB,EACxB,qBAAqB,EACrB,wBAAwB,EACxB,yBAAyB,EACzB,aAAa,EACb,aAAa,EACb,eAAe,EACf,gBAAgB,EAChB,mBAAmB,EACnB,uBAAuB,EACvB,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EACjB,qBAAqB,EACrB,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,GAC5B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/semantic/datasets/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,OAAO,EACP,SAAS,EACT,OAAO,EACP,SAAS,EACT,OAAO,EACP,MAAM,EACN,GAAG,EACH,KAAK,EACL,aAAa,EACb,GAAG,EACH,GAAG,EACH,GAAG,EACH,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,GAAG,EACH,UAAU,EACV,QAAQ,EACR,KAAK,EACL,KAAK,EACL,IAAI,EACJ,EAAE,EACF,GAAG,EACH,EAAE,EACF,GAAG,EACH,EAAE,EACF,GAAG,EACH,MAAM,EACN,SAAS,EACT,OAAO,EACP,IAAI,EACJ,MAAM,EACN,SAAS,EACT,GAAG,EACH,IAAI,EACJ,MAAM,EACN,KAAK,EACL,qBAAqB,EACrB,eAAe,GAChB,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EACV,SAAS,EACT,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,gBAAgB,EAChB,sBAAsB,EACtB,eAAe,EACf,kBAAkB,EAClB,eAAe,EACf,WAAW,EACX,iBAAiB,EACjB,SAAS,EACT,SAAS,EACT,aAAa,EACb,gBAAgB,EAChB,gBAAgB,EAChB,cAAc,EACd,YAAY,EACZ,aAAa,EACb,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,wBAAwB,EACxB,qBAAqB,EACrB,wBAAwB,EACxB,yBAAyB,EACzB,aAAa,EACb,aAAa,EACb,eAAe,EACf,gBAAgB,EAChB,mBAAmB,EACnB,uBAAuB,EACvB,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EACjB,qBAAqB,EACrB,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,GAC5B,MAAM,wBAAwB,CAAC;AAChC,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metric-endpoint.d.ts","sourceRoot":"","sources":["../../../src/semantic/datasets/metric-endpoint.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EAGZ,WAAW,EACX,aAAa,EACb,eAAe,EACf,oBAAoB,EACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAEL,KAAK,aAAa,EAElB,KAAK,YAAY,EACjB,KAAK,uBAAuB,EAC7B,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"metric-endpoint.d.ts","sourceRoot":"","sources":["../../../src/semantic/datasets/metric-endpoint.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EAGZ,WAAW,EACX,aAAa,EACb,eAAe,EACf,oBAAoB,EACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAEL,KAAK,aAAa,EAElB,KAAK,YAAY,EACjB,KAAK,uBAAuB,EAC7B,MAAM,qBAAqB,CAAC;AAQ7B,OAAO,EAA8B,KAAK,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAuB5G,QAAA,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;iBAGtB,CAAC;AAyBH,wBAAgB,kBAAkB,CAAC,KAAK,SAAS,WAAW,EAC1D,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC,GACxB;IACD,MAAM,EAAE,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC/B,IAAI,CAAC,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAClC,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,MAAM,CAAC,EAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;IACrC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,WAAW,CAAC,EAAE,eAAe,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAUA;AAMD,wBAAgB,oBAAoB,CAAC,KAAK,SAAS,WAAW,EAC5D,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC,EACzB,SAAS,EAAE,aAAa,EACxB,qBAAqB,EAAE,uBAAuB,EAC9C,eAAe,CAAC,EAAE,sBAAsB,GACvC,aAAa,CAAC,CAAC,CAAC,UAAU,EAAE,OAAO,kBAAkB,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,CAAC,CA8HzE"}
|
|
@@ -12,6 +12,7 @@ import { buildMetricInputSchema } from '@hypequery/datasets/schemas';
|
|
|
12
12
|
import { ServeHttpError } from '../../errors.js';
|
|
13
13
|
import { resolveSemanticExecutionRuntime, resolveSemanticQueryBuilder, } from '../query-builder-context.js';
|
|
14
14
|
import { resolveLocalAuthRequirement } from '../../auth-requirement.js';
|
|
15
|
+
import { endpointMaxLimit } from './utils/client-endpoint-profile.js';
|
|
15
16
|
import { assertRelatedDatasetAccess } from './utils/related-dataset-policy.js';
|
|
16
17
|
// ---------------------------------------------------------------------------
|
|
17
18
|
// Zod schemas for metric query input / output
|
|
@@ -66,7 +67,7 @@ export function createMetricEndpoint(name, entry, analytics, defaultBuilderFacto
|
|
|
66
67
|
const ds = (metricRef.__type === 'metric_ref' ? metricRef.dataset : metricRef.metric.dataset);
|
|
67
68
|
// Page-size cap, mirroring datasets: clamp (don't reject) and apply a default
|
|
68
69
|
// so a metric query is never unbounded.
|
|
69
|
-
const effectiveMaxLimit = resolved.maxLimit
|
|
70
|
+
const effectiveMaxLimit = endpointMaxLimit(ds, resolved.maxLimit);
|
|
70
71
|
const metricQueryInputSchema = buildMetricInputSchema(ds, contract.name, {
|
|
71
72
|
includeMeta: true,
|
|
72
73
|
timeKey: true,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type DatasetClientEndpoint } from '@hypequery/datasets';
|
|
2
|
+
import type { ProtocolClientContract } from '@hypequery/protocol';
|
|
3
|
+
/**
|
|
4
|
+
* A dataset or metric endpoint's place in the API's client contract. It is
|
|
5
|
+
* resolved lazily, so an API that never exports or pins a contract never
|
|
6
|
+
* builds one, and at the path `api.manifest()` names, which `api.route()`
|
|
7
|
+
* may change after the API is created.
|
|
8
|
+
*/
|
|
9
|
+
export interface ClientEndpointBinding {
|
|
10
|
+
/**
|
|
11
|
+
* The endpoint as the client contract describes it, or `undefined` when
|
|
12
|
+
* the contract cannot advertise it (a metric with a fixed grain). Throws
|
|
13
|
+
* when the endpoint is misconfigured for export.
|
|
14
|
+
*/
|
|
15
|
+
definition(): DatasetClientEndpoint | undefined;
|
|
16
|
+
/** A one-endpoint contract at the binding's current path, under its mount path. */
|
|
17
|
+
contract(): ProtocolClientContract | undefined;
|
|
18
|
+
/** The trusted mount path every contract path starts with. */
|
|
19
|
+
readonly mountPath: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function createClientEndpointBinding(mountPath: string, resolve: () => DatasetClientEndpoint | undefined): ClientEndpointBinding;
|
|
22
|
+
//# sourceMappingURL=client-endpoint-binding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-endpoint-binding.d.ts","sourceRoot":"","sources":["../../../../src/semantic/datasets/utils/client-endpoint-binding.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAC7F,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAElE;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,UAAU,IAAI,qBAAqB,GAAG,SAAS,CAAC;IAChD,mFAAmF;IACnF,QAAQ,IAAI,sBAAsB,GAAG,SAAS,CAAC;IAC/C,8DAA8D;IAC9D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,qBAAqB,GAAG,SAAS,GAC/C,qBAAqB,CAiBvB"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { buildDatasetClientContract } from '@hypequery/datasets';
|
|
2
|
+
export function createClientEndpointBinding(mountPath, resolve) {
|
|
3
|
+
// One entry per exported path: it changes only when `api.route()` moves the endpoint.
|
|
4
|
+
const contracts = new Map();
|
|
5
|
+
return {
|
|
6
|
+
mountPath,
|
|
7
|
+
definition: resolve,
|
|
8
|
+
contract() {
|
|
9
|
+
const definition = resolve();
|
|
10
|
+
if (definition === undefined)
|
|
11
|
+
return undefined;
|
|
12
|
+
let contract = contracts.get(definition.path);
|
|
13
|
+
if (contract === undefined) {
|
|
14
|
+
contract = buildDatasetClientContract([definition], { mountPath }).contract;
|
|
15
|
+
contracts.set(definition.path, contract);
|
|
16
|
+
}
|
|
17
|
+
return contract;
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AnyDatasetInstance, ClientContractProfile } from '@hypequery/datasets';
|
|
2
|
+
/** The page cap a semantic endpoint clamps to: the entry's, else the dataset's, else 1000. */
|
|
3
|
+
export declare function endpointMaxLimit(dataset: AnyDatasetInstance, maxLimit: number | undefined): number;
|
|
4
|
+
/**
|
|
5
|
+
* The client profile a semantic endpoint enforces on a pinned request. An
|
|
6
|
+
* omitted `limit` reads the endpoint's whole page cap, so that is both its
|
|
7
|
+
* default and its maximum; other ceilings keep the profile defaults.
|
|
8
|
+
*/
|
|
9
|
+
export declare function endpointClientProfile(dataset: AnyDatasetInstance, maxLimit: number | undefined): ClientContractProfile;
|
|
10
|
+
//# sourceMappingURL=client-endpoint-profile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-endpoint-profile.d.ts","sourceRoot":"","sources":["../../../../src/semantic/datasets/utils/client-endpoint-profile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAKrF,8FAA8F;AAC9F,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAElG;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,qBAAqB,CAGtH"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** RFC 0018's ceiling on one page; a larger endpoint cap is advertised as this. */
|
|
2
|
+
const CLIENT_MAX_LIMIT = 100_000;
|
|
3
|
+
/** The page cap a semantic endpoint clamps to: the entry's, else the dataset's, else 1000. */
|
|
4
|
+
export function endpointMaxLimit(dataset, maxLimit) {
|
|
5
|
+
return maxLimit ?? dataset.limits?.maxResultSize ?? 1000;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The client profile a semantic endpoint enforces on a pinned request. An
|
|
9
|
+
* omitted `limit` reads the endpoint's whole page cap, so that is both its
|
|
10
|
+
* default and its maximum; other ceilings keep the profile defaults.
|
|
11
|
+
*/
|
|
12
|
+
export function endpointClientProfile(dataset, maxLimit) {
|
|
13
|
+
const cap = Math.min(endpointMaxLimit(dataset, maxLimit), CLIENT_MAX_LIMIT);
|
|
14
|
+
return { maxLimit: cap, defaultLimit: cap };
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Narrow a pinned request's response to RFC 0018's public metadata:
|
|
3
|
+
* requestId, timingMs, rowCount, pagination and cache hit. A contract client
|
|
4
|
+
* refuses metadata members it does not know, so diagnostics, cache ages and
|
|
5
|
+
* result-limit notes are left out.
|
|
6
|
+
*/
|
|
7
|
+
export declare function clientResponse(result: unknown, requestId: string, input: Readonly<Record<string, unknown>>): unknown;
|
|
8
|
+
//# sourceMappingURL=client-response-meta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-response-meta.d.ts","sourceRoot":"","sources":["../../../../src/semantic/datasets/utils/client-response-meta.ts"],"names":[],"mappings":"AAcA;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GACvC,OAAO,CAgBT"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
function isSemanticResult(value) {
|
|
2
|
+
return typeof value === 'object' && value !== null && Array.isArray(value.data);
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Narrow a pinned request's response to RFC 0018's public metadata:
|
|
6
|
+
* requestId, timingMs, rowCount, pagination and cache hit. A contract client
|
|
7
|
+
* refuses metadata members it does not know, so diagnostics, cache ages and
|
|
8
|
+
* result-limit notes are left out.
|
|
9
|
+
*/
|
|
10
|
+
export function clientResponse(result, requestId, input) {
|
|
11
|
+
if (!isSemanticResult(result))
|
|
12
|
+
return result;
|
|
13
|
+
const { data, meta } = result;
|
|
14
|
+
if (meta === undefined || meta === null)
|
|
15
|
+
return { data };
|
|
16
|
+
return {
|
|
17
|
+
data,
|
|
18
|
+
meta: {
|
|
19
|
+
requestId,
|
|
20
|
+
timingMs: meta.timingMs ?? 0,
|
|
21
|
+
rowCount: data.length,
|
|
22
|
+
pagination: meta.pagination === undefined
|
|
23
|
+
? { limit: input.limit, offset: input.offset, hasMore: false }
|
|
24
|
+
: { limit: meta.pagination.limit, offset: meta.pagination.offset, hasMore: meta.pagination.hasMore },
|
|
25
|
+
...(meta.cache === undefined ? {} : { cache: { hit: meta.cache.hit } }),
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -15,6 +15,18 @@ export type DatasetEntry<TAuth extends AuthContext = AuthContext> = AnyDatasetIn
|
|
|
15
15
|
maxLimit?: number;
|
|
16
16
|
/** Enable SQL and internal tenant IDs in opt-in metadata only on trusted endpoints. */
|
|
17
17
|
trustedDiagnostics?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Lets this endpoint stream its result, for exports and other large reads.
|
|
20
|
+
* A request asks with `Accept: application/x-ndjson` (row batches as NDJSON
|
|
21
|
+
* frames) or `Accept: application/vnd.apache.arrow.stream` (an Arrow IPC
|
|
22
|
+
* stream); without this option those requests get a JSON page. `maxRows`
|
|
23
|
+
* caps one streamed response and defaults to `maxLimit`; it may not exceed
|
|
24
|
+
* the dataset's stream ceiling, `limits.maxStreamRows` (else
|
|
25
|
+
* `limits.maxResultSize`).
|
|
26
|
+
*/
|
|
27
|
+
stream?: boolean | {
|
|
28
|
+
maxRows?: number;
|
|
29
|
+
};
|
|
18
30
|
};
|
|
19
31
|
export declare function resolveDatasetEntry<TAuth extends AuthContext>(entry: DatasetEntry<TAuth>): {
|
|
20
32
|
dataset: AnyDatasetInstance;
|
|
@@ -28,5 +40,16 @@ export declare function resolveDatasetEntry<TAuth extends AuthContext>(entry: Da
|
|
|
28
40
|
maxLimit?: number;
|
|
29
41
|
/** Enable SQL and internal tenant IDs in opt-in metadata only on trusted endpoints. */
|
|
30
42
|
trustedDiagnostics?: boolean;
|
|
43
|
+
stream?: boolean | {
|
|
44
|
+
maxRows?: number;
|
|
45
|
+
};
|
|
31
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* The row cap of a streaming endpoint, or `undefined` when it does not stream.
|
|
49
|
+
* A cap above the dataset's stream ceiling is refused here rather than failing
|
|
50
|
+
* every request's validation.
|
|
51
|
+
*/
|
|
52
|
+
export declare function resolveStreamMaxRows(name: string, dataset: AnyDatasetInstance, stream: boolean | {
|
|
53
|
+
maxRows?: number;
|
|
54
|
+
} | undefined, pageCap: number): number | undefined;
|
|
32
55
|
//# sourceMappingURL=dataset-entry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataset-entry.d.ts","sourceRoot":"","sources":["../../../../src/semantic/datasets/utils/dataset-entry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,eAAe,EACf,oBAAoB,EACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,MAAM,MAAM,YAAY,CAAC,KAAK,SAAS,WAAW,GAAG,WAAW,IAC5D,kBAAkB,GAClB;IACE,OAAO,EAAE,kBAAkB,CAAC;IAC5B,+EAA+E;IAC/E,IAAI,CAAC,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAClC,kFAAkF;IAClF,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,MAAM,CAAC,EAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;IACrC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,mDAAmD;IACnD,WAAW,CAAC,EAAE,eAAe,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,kBAAkB,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"dataset-entry.d.ts","sourceRoot":"","sources":["../../../../src/semantic/datasets/utils/dataset-entry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,YAAY,EACZ,eAAe,EACf,oBAAoB,EACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,MAAM,MAAM,YAAY,CAAC,KAAK,SAAS,WAAW,GAAG,WAAW,IAC5D,kBAAkB,GAClB;IACE,OAAO,EAAE,kBAAkB,CAAC;IAC5B,+EAA+E;IAC/E,IAAI,CAAC,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAClC,kFAAkF;IAClF,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,MAAM,CAAC,EAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;IACrC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,mDAAmD;IACnD,WAAW,CAAC,EAAE,eAAe,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;;;;;;OAQG;IACH,MAAM,CAAC,EAAE,OAAO,GAAG;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACzC,CAAC;AAgBN,wBAAgB,mBAAmB,CAAC,KAAK,SAAS,WAAW,EAC3D,KAAK,EAAE,YAAY,CAAC,KAAK,CAAC,GACzB;IACD,OAAO,EAAE,kBAAkB,CAAC;IAC5B,IAAI,CAAC,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAClC,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,MAAM,CAAC,EAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;IACrC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,WAAW,CAAC,EAAE,eAAe,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uFAAuF;IACvF,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,MAAM,CAAC,EAAE,OAAO,GAAG;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACzC,CAUA;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,kBAAkB,EAC3B,MAAM,EAAE,OAAO,GAAG;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,EAClD,OAAO,EAAE,MAAM,GACd,MAAM,GAAG,SAAS,CAepB"}
|
|
@@ -13,3 +13,23 @@ export function resolveDatasetEntry(entry) {
|
|
|
13
13
|
}
|
|
14
14
|
throw new Error('Invalid dataset entry.');
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* The row cap of a streaming endpoint, or `undefined` when it does not stream.
|
|
18
|
+
* A cap above the dataset's stream ceiling is refused here rather than failing
|
|
19
|
+
* every request's validation.
|
|
20
|
+
*/
|
|
21
|
+
export function resolveStreamMaxRows(name, dataset, stream, pageCap) {
|
|
22
|
+
if (!stream)
|
|
23
|
+
return undefined;
|
|
24
|
+
const maxRows = stream === true ? pageCap : stream.maxRows ?? pageCap;
|
|
25
|
+
if (!Number.isInteger(maxRows) || maxRows < 1) {
|
|
26
|
+
throw new Error(`Dataset endpoint "${name}": stream.maxRows must be a positive integer.`);
|
|
27
|
+
}
|
|
28
|
+
const limits = dataset.limits;
|
|
29
|
+
const ceiling = limits?.maxStreamRows ?? limits?.maxResultSize;
|
|
30
|
+
if (ceiling !== undefined && maxRows > ceiling) {
|
|
31
|
+
const source = limits?.maxStreamRows !== undefined ? 'maxStreamRows' : 'maxResultSize';
|
|
32
|
+
throw new Error(`Dataset endpoint "${name}": stream.maxRows (${maxRows}) exceeds the dataset's limits.${source} (${ceiling}).`);
|
|
33
|
+
}
|
|
34
|
+
return maxRows;
|
|
35
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A streamed dataset response as NDJSON frames, one JSON object per line:
|
|
3
|
+
*
|
|
4
|
+
* {"type":"start","meta":{...}} meta only when requested
|
|
5
|
+
* {"type":"rows","data":[...]} one per batch, as `execute` returns rows
|
|
6
|
+
* {"type":"end","rowCount":N} the result is complete
|
|
7
|
+
* {"type":"error","error":{...}} the query failed after the response began
|
|
8
|
+
*
|
|
9
|
+
* The HTTP status is sent before the first row, so a failure partway is a
|
|
10
|
+
* final `error` frame; a response without an `end` frame is incomplete. The
|
|
11
|
+
* client only sees a sanitized message: `failure()` returns the error itself,
|
|
12
|
+
* once the stream has ended, for hooks and logs.
|
|
13
|
+
*/
|
|
14
|
+
export declare function datasetNdjsonStream(rows: ReadableStream<Record<string, unknown>[]>, meta?: unknown): {
|
|
15
|
+
stream: ReadableStream<Uint8Array>;
|
|
16
|
+
failure: () => unknown;
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=ndjson-frames.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ndjson-frames.d.ts","sourceRoot":"","sources":["../../../../src/semantic/datasets/utils/ndjson-frames.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,cAAc,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,EAC/C,IAAI,CAAC,EAAE,OAAO,GACb;IAAE,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IAAC,OAAO,EAAE,MAAM,OAAO,CAAA;CAAE,CAyChE"}
|