@flipstream/sdk 0.0.0-stage → 0.4.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 (78) hide show
  1. package/CHANGELOG.md +85 -0
  2. package/LICENSE +202 -0
  3. package/README.md +280 -2
  4. package/dist/api/index.d.ts +1 -0
  5. package/dist/api/index.js +2 -0
  6. package/dist/api/index.js.map +1 -0
  7. package/dist/auth/discovery.d.ts +21 -0
  8. package/dist/auth/discovery.js +68 -0
  9. package/dist/auth/discovery.js.map +1 -0
  10. package/dist/auth/index.d.ts +5 -0
  11. package/dist/auth/index.js +5 -0
  12. package/dist/auth/index.js.map +1 -0
  13. package/dist/auth/pkce.d.ts +10 -0
  14. package/dist/auth/pkce.js +25 -0
  15. package/dist/auth/pkce.js.map +1 -0
  16. package/dist/auth/signIn.d.ts +97 -0
  17. package/dist/auth/signIn.js +133 -0
  18. package/dist/auth/signIn.js.map +1 -0
  19. package/dist/auth/transport.d.ts +18 -0
  20. package/dist/auth/transport.js +26 -0
  21. package/dist/auth/transport.js.map +1 -0
  22. package/dist/core/body.d.ts +14 -0
  23. package/dist/core/body.js +69 -0
  24. package/dist/core/body.js.map +1 -0
  25. package/dist/core/call.d.ts +17 -0
  26. package/dist/core/call.js +116 -0
  27. package/dist/core/call.js.map +1 -0
  28. package/dist/core/fetch.d.ts +35 -0
  29. package/dist/core/fetch.js +105 -0
  30. package/dist/core/fetch.js.map +1 -0
  31. package/dist/core/read.d.ts +17 -0
  32. package/dist/core/read.js +92 -0
  33. package/dist/core/read.js.map +1 -0
  34. package/dist/core/result.d.ts +94 -0
  35. package/dist/core/result.js +40 -0
  36. package/dist/core/result.js.map +1 -0
  37. package/dist/core/retry.d.ts +40 -0
  38. package/dist/core/retry.js +41 -0
  39. package/dist/core/retry.js.map +1 -0
  40. package/dist/core/transport.d.ts +62 -0
  41. package/dist/core/transport.js +9 -0
  42. package/dist/core/transport.js.map +1 -0
  43. package/dist/flipstream.d.ts +45 -0
  44. package/dist/flipstream.js +26 -0
  45. package/dist/flipstream.js.map +1 -0
  46. package/dist/index.d.ts +11 -0
  47. package/dist/index.js +8 -0
  48. package/dist/index.js.map +1 -0
  49. package/dist/node/headless.d.ts +20 -0
  50. package/dist/node/headless.js +28 -0
  51. package/dist/node/headless.js.map +1 -0
  52. package/dist/node/index.d.ts +4 -0
  53. package/dist/node/index.js +5 -0
  54. package/dist/node/index.js.map +1 -0
  55. package/dist/node/loopback.d.ts +58 -0
  56. package/dist/node/loopback.js +126 -0
  57. package/dist/node/loopback.js.map +1 -0
  58. package/dist/query/catalog.d.ts +15 -0
  59. package/dist/query/catalog.js +41 -0
  60. package/dist/query/catalog.js.map +1 -0
  61. package/dist/query/errors.d.ts +8 -0
  62. package/dist/query/errors.js +16 -0
  63. package/dist/query/errors.js.map +1 -0
  64. package/dist/query/query.d.ts +27 -0
  65. package/dist/query/query.js +35 -0
  66. package/dist/query/query.js.map +1 -0
  67. package/dist/recorded/query-planner/catalog-google_search_console.json +140 -0
  68. package/dist/recorded/query-planner/catalog.json +1234 -0
  69. package/dist/spec/query-planner/index.d.ts +1 -0
  70. package/dist/spec/query-planner/index.js +3 -0
  71. package/dist/spec/query-planner/index.js.map +1 -0
  72. package/dist/spec/query-planner/types.gen.d.ts +300 -0
  73. package/dist/spec/query-planner/types.gen.js +3 -0
  74. package/dist/spec/query-planner/types.gen.js.map +1 -0
  75. package/dist/spec/query-planner/zod.gen.d.ts +325 -0
  76. package/dist/spec/query-planner/zod.gen.js +137 -0
  77. package/dist/spec/query-planner/zod.gen.js.map +1 -0
  78. package/package.json +90 -4
@@ -0,0 +1,62 @@
1
+ /**
2
+ * The seam between the calls and whatever sends them: the os's `Transport` (its
3
+ * packages/platform/api/src/core/transport.ts), made public so the console can hand the SDK its
4
+ * own (SD46). A call says what it wants to send; a transport decides whether it may go, attaches
5
+ * the token and answers. Nothing above it ever sees a token, and it never reads a body: reading
6
+ * happens in `readJson`, under the call's signal (D409).
7
+ */
8
+ /** The public hosts the SDK calls, by what they are, never by URL (D410). */
9
+ export type ServiceName = 'query' | 'api' | 'auth';
10
+ /** Every call says what it does. Required fields, so no endpoint can leave one out. */
11
+ export interface ApiRequest {
12
+ /**
13
+ * Builds a fresh Request for every send. A body is a stream one fetch consumes, so a send after
14
+ * a 401 calls this again instead of resending a used Request.
15
+ */
16
+ readonly makeRequest: () => Request;
17
+ /** Declared per endpoint, next to its schema: a POST that only reads is `read`. */
18
+ readonly effect: 'read' | 'write';
19
+ readonly service: ServiceName;
20
+ /** A stable name for logs (`query.run`), never the URL. */
21
+ readonly endpoint: string;
22
+ /** The caller's abort, for this call alone (D409). A transport sends nothing more once it fires. */
23
+ readonly signal: AbortSignal;
24
+ }
25
+ export type Transport = (call: ApiRequest) => Promise<TransportResult>;
26
+ /**
27
+ * Why a transport sent nothing, or dropped what came back, as the os has it. The os declares
28
+ * `aborted` here but sends it nowhere; it stays so the console's results still fit. The caller's
29
+ * own abort is the `aborted` result below.
30
+ */
31
+ export type CancelReason = 'principal-changed' | 'scope-changed' | 'aborted';
32
+ export type TransportResult =
33
+ /** A destination outside the allowlist: no token went anywhere. */
34
+ {
35
+ readonly kind: 'refused';
36
+ readonly reason: 'destination';
37
+ } | {
38
+ readonly kind: 'response';
39
+ readonly response: Response;
40
+ }
41
+ /** Offline, or no token could be had right now. */
42
+ | {
43
+ readonly kind: 'unavailable';
44
+ }
45
+ /** Sent as someone who is no longer signed in here, or never sent: nobody may see it. */
46
+ | {
47
+ readonly kind: 'cancelled';
48
+ readonly reason: CancelReason;
49
+ }
50
+ /** The caller's `signal` fired: nothing more was sent, and nothing is read. */
51
+ | {
52
+ readonly kind: 'aborted';
53
+ }
54
+ /**
55
+ * A caller's token function failed, or gave no token, so nothing was sent. Why is the
56
+ * caller's to say (`cause`, what it threw): a session that ended and a refresh that couldn't
57
+ * reach its host look the same from here, so this is never read as a 401 or as an outage.
58
+ */
59
+ | {
60
+ readonly kind: 'no_token';
61
+ readonly cause: unknown;
62
+ };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The seam between the calls and whatever sends them: the os's `Transport` (its
3
+ * packages/platform/api/src/core/transport.ts), made public so the console can hand the SDK its
4
+ * own (SD46). A call says what it wants to send; a transport decides whether it may go, attaches
5
+ * the token and answers. Nothing above it ever sees a token, and it never reads a body: reading
6
+ * happens in `readJson`, under the call's signal (D409).
7
+ */
8
+ export {};
9
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../../src/core/transport.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
@@ -0,0 +1,45 @@
1
+ import { type TokenAsk } from './core/fetch.js';
2
+ import type { ApiResult } from './core/result.js';
3
+ import type { ServiceName, Transport } from './core/transport.js';
4
+ import { type QueryRequest } from './query/query.js';
5
+ import type { Catalog, CatalogSource, QueryResponse } from './spec/query-planner/types.gen.js';
6
+ /** Each service's base URL. Only public hosts (D410); a call goes only to its own service's. */
7
+ export type Hosts = Readonly<Partial<Record<ServiceName, string>>>;
8
+ /** Asked before every attempt, and once more after a 401 with the token the host refused. */
9
+ export type TokenFunction = (ask: TokenAsk) => string | Promise<string>;
10
+ export type FlipstreamOptions =
11
+ /** The console: its own transport, which attaches the token and decides what may go. */
12
+ {
13
+ readonly transport: Transport;
14
+ readonly hosts?: Hosts;
15
+ readonly token?: never;
16
+ readonly fetch?: never;
17
+ readonly allowLocalhostHttp?: never;
18
+ }
19
+ /** The CLI and customers: a token function, and the caller's own fetch if it likes. */
20
+ | {
21
+ readonly token: TokenFunction;
22
+ readonly fetch?: typeof fetch;
23
+ readonly hosts?: Hosts;
24
+ /** Plain http to a loopback host, for development and tests only. Off unless set. */
25
+ readonly allowLocalhostHttp?: boolean;
26
+ readonly transport?: never;
27
+ };
28
+ export interface CallOptions {
29
+ /** Ends the call at once: its answer is `aborted`. */
30
+ readonly signal?: AbortSignal;
31
+ /** The whole call's deadline, every attempt and wait included; the endpoint's own when left out. */
32
+ readonly deadlineMs?: number;
33
+ }
34
+ /** What `createFlipstream` gives: each call settles into an `ApiResult`, never a thrown error (D412). */
35
+ export interface Flipstream {
36
+ readonly catalog: {
37
+ list(options?: CallOptions): Promise<ApiResult<Catalog>>;
38
+ get(request: {
39
+ readonly source: string;
40
+ }, options?: CallOptions): Promise<ApiResult<CatalogSource>>;
41
+ };
42
+ query(request: QueryRequest, options?: CallOptions): Promise<ApiResult<QueryResponse>>;
43
+ }
44
+ /** The SDK, for one caller (D421): the console with its transport, or anyone with a token function. */
45
+ export declare function createFlipstream(options: FlipstreamOptions): Flipstream;
@@ -0,0 +1,26 @@
1
+ import { call } from './core/call.js';
2
+ import { fetchTransport } from './core/fetch.js';
3
+ import { catalogGet, catalogList } from './query/catalog.js';
4
+ import { queryRun } from './query/query.js';
5
+ const DEFAULT_HOSTS = { query: 'https://query.flipstream.io' };
6
+ /** The SDK, for one caller (D421): the console with its transport, or anyone with a token function. */
7
+ export function createFlipstream(options) {
8
+ const hosts = { ...DEFAULT_HOSTS, ...options.hosts };
9
+ const transport = options.transport !== undefined
10
+ ? options.transport
11
+ : fetchTransport({ token: options.token, fetch: options.fetch, hosts, allowLocalhostHttp: options.allowLocalhostHttp });
12
+ const base = (hosts.query ?? DEFAULT_HOSTS.query).replace(/\/+$/, '');
13
+ const send = (spec, { signal, deadlineMs } = {}) => call(transport, deadlineMs === undefined ? spec : { ...spec, deadlineMs }, signal);
14
+ return {
15
+ catalog: {
16
+ list: (callOptions) => send(catalogList(base), callOptions),
17
+ get(request, callOptions) {
18
+ const spec = catalogGet(base, request.source);
19
+ // Refused before anything is sent: the token function isn't asked, and nothing is spent.
20
+ return spec === null ? Promise.resolve({ ok: false, error: { kind: 'refused' } }) : send(spec, callOptions);
21
+ },
22
+ },
23
+ query: (request, callOptions) => send(queryRun(base, request), callOptions),
24
+ };
25
+ }
26
+ //# sourceMappingURL=flipstream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flipstream.js","sourceR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AC3D,GAAG,CAAC,OAAO,EAAE,WAAW;gBACtB,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;gBAC7C,yFAAyF;gBACzF,OAAO,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,WAAW,CAAC,CAAA;YAC7G,CAAC;SACF;QACD,KAAK,EAAE,CAAC,OAAO,EAAE,WAAW,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,WAAW,CAAC;KAC5E,CAAA;AACH,CAAC"}
@@ -0,0 +1,11 @@
1
+ export { createFlipstream } from './flipstream.js';
2
+ export type { CallOptions, Flipstream, FlipstreamOptions, Hosts, TokenFunction } from './flipstream.js';
3
+ export type { QueryRequest } from './query/query.js';
4
+ export { isPlannerCode } from './query/errors.js';
5
+ export { ApiFailure, orThrow } from './core/result.js';
6
+ export type { Answered, ApiError, ApiResult, ErrorCode, Said, StatusKind } from './core/result.js';
7
+ export type { ApiRequest, CancelReason, ServiceName, Transport, TransportResult } from './core/transport.js';
8
+ export type { TokenAsk } from './core/fetch.js';
9
+ export type { Catalog, CatalogSource, CustomDimensions, Dimension, ErrorBody, Format, Metric, PlannerErrorCode, QueryResponse, Row, SortSpec, Source, } from './spec/query-planner/types.gen.js';
10
+ export * from './auth/index.js';
11
+ export * from './api/index.js';
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ // The package entry point (D421): createFlipstream and its calls, the results they give (D412),
2
+ // and the core types a console needs to pass its own transport.
3
+ export { createFlipstream } from './flipstream.js';
4
+ export { isPlannerCode } from './query/errors.js';
5
+ export { ApiFailure, orThrow } from './core/result.js';
6
+ export * from './auth/index.js';
7
+ export * from './api/index.js';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,gEAAgE;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAGlD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAkBtD,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,20 @@
1
+ import { type CallbackError, type PendingSignIn } from '../auth/signIn.js';
2
+ /** No browser here to land the redirect: CI, an SSH session, or Linux without a display. */
3
+ export declare function isHeadless(env: Readonly<Record<string, string | undefined>>, platform?: string): boolean;
4
+ export type PastedRedirect =
5
+ /** Pass `callbackUrl` to `finishSignIn`, which exchanges its `code`. */
6
+ {
7
+ readonly ok: true;
8
+ readonly callbackUrl: URL;
9
+ readonly code: string;
10
+ } | {
11
+ readonly ok: false;
12
+ readonly error: CallbackError | {
13
+ readonly kind: 'not_a_url';
14
+ };
15
+ };
16
+ /**
17
+ * The redirect the person pasted, checked as the loopback checks one (its state first, then its
18
+ * issuer, the server's error, the code) before anything is sent.
19
+ */
20
+ export declare function parsePastedRedirect(text: string, pending: PendingSignIn): PastedRedirect;
@@ -0,0 +1,28 @@
1
+ import { readCallback } from '../auth/signIn.js';
2
+ /*
3
+ * Signing in where no browser can come back to this machine (CI, SSH, a Linux box without a
4
+ * display): the person opens the URL elsewhere, and pastes back the address the browser then
5
+ * fails to load. Prompting for it is the app's. Ported from the CLI's headless.ts.
6
+ */
7
+ /** No browser here to land the redirect: CI, an SSH session, or Linux without a display. */
8
+ export function isHeadless(env, platform = process.platform) {
9
+ if (nonEmpty(env['CI']))
10
+ return true;
11
+ if (nonEmpty(env['SSH_CONNECTION']) || nonEmpty(env['SSH_TTY']))
12
+ return true;
13
+ return platform === 'linux' && !nonEmpty(env['DISPLAY']) && !nonEmpty(env['WAYLAND_DISPLAY']);
14
+ }
15
+ const nonEmpty = (value) => value !== undefined && value !== '';
16
+ /**
17
+ * The redirect the person pasted, checked as the loopback checks one (its state first, then its
18
+ * issuer, the server's error, the code) before anything is sent.
19
+ */
20
+ export function parsePastedRedirect(text, pending) {
21
+ const trimmed = text.trim();
22
+ if (!URL.canParse(trimmed))
23
+ return { ok: false, error: { kind: 'not_a_url' } };
24
+ const callbackUrl = new URL(trimmed);
25
+ const read = readCallback(callbackUrl, pending);
26
+ return read.kind === 'code' ? { ok: true, callbackUrl, code: read.code } : { ok: false, error: read };
27
+ }
28
+ //# sourceMappingURL=headless.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"headless.js","sourceRoot":"","sources":["../../src/node/headless.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA0C,MAAM,mBAAmB,CAAA;AAExF;;;;GAIG;AAEH,4FAA4F;AAC5F,MAAM,UAAU,UAAU,CAAC,GAAiD,EAAE,WAAmB,OAAO,CAAC,QAAQ;IAC/G,IAAI,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,IAAI,CAAA;IACpC,IAAI,QAAQ,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,IAAI,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAAE,OAAO,IAAI,CAAA;IAC5E,OAAO,QAAQ,KAAK,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAA;AAC/F,CAAC;AAED,MAAM,QAAQ,GAAG,CAAC,KAAyB,EAAW,EAAE,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE,CAAA;AAO5F;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAAY,EAAE,OAAsB;IACtE,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAA;IAC3B,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,EAAE,CAAA;IAC9E,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAA;IACpC,MAAM,IAAI,GAAG,YAAY,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;IAC/C,OAAO,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;AACvG,CAAC"}
@@ -0,0 +1,4 @@
1
+ export { signInWithLoopback } from './loopback.js';
2
+ export type { LoopbackError, LoopbackResult, LoopbackSignInOptions } from './loopback.js';
3
+ export { isHeadless, parsePastedRedirect } from './headless.js';
4
+ export type { PastedRedirect } from './headless.js';
@@ -0,0 +1,5 @@
1
+ // The `@flipstream/sdk/node` entry: sign-in for Node apps, which the main entry never pulls in,
2
+ // so a browser bundle has no node:http (#65).
3
+ export { signInWithLoopback } from './loopback.js';
4
+ export { isHeadless, parsePastedRedirect } from './headless.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,8CAA8C;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAElD,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA"}
@@ -0,0 +1,58 @@
1
+ import { type Server } from 'node:http';
2
+ import { type SignInError, type SignInRequest, type Tokens } from '../auth/signIn.js';
3
+ import type { AuthCallOptions } from '../auth/transport.js';
4
+ export interface LoopbackSignInOptions extends Omit<SignInRequest, 'redirectUri'>, AuthCallOptions {
5
+ /**
6
+ * Shows the person where to sign in: opens their browser, or prints the URL. Called once, when
7
+ * the port is open. The SDK opens nothing itself and writes nothing to the terminal.
8
+ */
9
+ readonly open: (url: string) => void | Promise<void>;
10
+ /** The pages the browser shows after the redirect; neutral ones when left out (HTML). */
11
+ readonly page?: {
12
+ readonly signedIn: string;
13
+ readonly failed: string;
14
+ };
15
+ /** How long to wait for the redirect: 5 minutes when left out. */
16
+ readonly timeoutMs?: number;
17
+ }
18
+ export type LoopbackError = SignInError
19
+ /** The port couldn't be opened. */
20
+ | {
21
+ readonly kind: 'listen_failed';
22
+ readonly cause: unknown;
23
+ }
24
+ /** `open` threw, so nobody could have signed in. */
25
+ | {
26
+ readonly kind: 'open_failed';
27
+ readonly cause: unknown;
28
+ };
29
+ export type LoopbackResult = {
30
+ readonly ok: true;
31
+ readonly data: Tokens;
32
+ } | {
33
+ readonly ok: false;
34
+ readonly error: LoopbackError;
35
+ };
36
+ /**
37
+ * Signs the person in through their browser, with the redirect `http://127.0.0.1:<port>/callback`
38
+ * on a port of the system's choosing, bound to 127.0.0.1 only. The redirect is checked as
39
+ * `finishSignIn` checks one (its state first), and its code exchanged. The first redirect decides,
40
+ * if it comes within `timeoutMs`; the port is closed on every path: signed in, refused,
41
+ * `timeout`, `aborted`, or `open_failed`.
42
+ */
43
+ export declare function signInWithLoopback(options: LoopbackSignInOptions): Promise<LoopbackResult>;
44
+ type Listening = {
45
+ readonly ok: true;
46
+ readonly server: Server;
47
+ readonly port: number;
48
+ readonly close: () => Promise<void>;
49
+ } | {
50
+ readonly ok: false;
51
+ readonly cause: unknown;
52
+ };
53
+ /**
54
+ * A server on 127.0.0.1 only (never every address: the code it receives would be reachable from
55
+ * the network), on a port of the system's choosing. `close` ends every connection with it.
56
+ */
57
+ export declare function listenOnLoopback(): Promise<Listening>;
58
+ export {};
@@ -0,0 +1,126 @@
1
+ import { createServer } from 'node:http';
2
+ import { finishSignIn, startSignIn } from '../auth/signIn.js';
3
+ const WAIT_MS = 300_000;
4
+ const PAGE = (heading, text) => `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>${heading}</title></head>` +
5
+ `<body style="font-family:system-ui,sans-serif;max-width:32rem;margin:4rem auto;padding:0 1rem">` +
6
+ `<h1>${heading}</h1><p>${text}</p>` +
7
+ // The code is good once, and is then exchanged; still, it leaves the address bar.
8
+ `<script>try{history.replaceState(null,'',location.pathname)}catch(e){}</script></body></html>`;
9
+ const PAGES = { signedIn: PAGE('Signed in', 'You can close this tab.'), failed: PAGE('Sign-in failed', 'Return to the app for details.') };
10
+ /**
11
+ * Signs the person in through their browser, with the redirect `http://127.0.0.1:<port>/callback`
12
+ * on a port of the system's choosing, bound to 127.0.0.1 only. The redirect is checked as
13
+ * `finishSignIn` checks one (its state first), and its code exchanged. The first redirect decides,
14
+ * if it comes within `timeoutMs`; the port is closed on every path: signed in, refused,
15
+ * `timeout`, `aborted`, or `open_failed`.
16
+ */
17
+ export async function signInWithLoopback(options) {
18
+ const { signal } = options;
19
+ // A function, so each look reads the signal as it is now, past the awaits between them.
20
+ const aborted = () => signal?.aborted === true;
21
+ if (aborted())
22
+ return { ok: false, error: { kind: 'aborted' } };
23
+ const pages = options.page ?? PAGES;
24
+ const listening = await listenOnLoopback();
25
+ if (!listening.ok)
26
+ return { ok: false, error: { kind: 'listen_failed', cause: listening.cause } };
27
+ const { server, close } = listening;
28
+ const redirectUri = `http://127.0.0.1:${String(listening.port)}/callback`;
29
+ let timer;
30
+ let onAbort;
31
+ const redirect = { taken: false };
32
+ try {
33
+ const { url, pending } = await startSignIn({ server: options.server, clientId: options.clientId, scopes: options.scopes, redirectUri });
34
+ // An abort while the port opened, or the URL was made, fired no event this could still hear.
35
+ if (aborted())
36
+ return { ok: false, error: { kind: 'aborted' } };
37
+ const settled = new Promise((resolve, reject) => {
38
+ timer = setTimeout(() => {
39
+ resolve({ ok: false, error: { kind: 'timeout' } });
40
+ }, options.timeoutMs ?? WAIT_MS);
41
+ onAbort = () => {
42
+ resolve({ ok: false, error: { kind: 'aborted' } });
43
+ };
44
+ signal?.addEventListener('abort', onAbort, { once: true });
45
+ server.on('request', (request, response) => {
46
+ // A request target no URL can be made of (`//[`) is no redirect, and must not throw here,
47
+ // where nothing would catch it.
48
+ const target = request.url ?? '/';
49
+ const callbackUrl = URL.canParse(target, redirectUri) ? new URL(target, redirectUri) : null;
50
+ if (callbackUrl?.pathname !== '/callback' || redirect.taken) {
51
+ void send(response, callbackUrl === null ? 400 : 404, pages.failed);
52
+ return;
53
+ }
54
+ redirect.taken = true;
55
+ // The redirect came in time: the exchange has its own deadline (`finishSignIn`).
56
+ clearTimeout(timer);
57
+ finishSignIn(callbackUrl, pending, { signal, fetch: options.fetch }).then(async (result) => {
58
+ await send(response, result.ok ? 200 : 400, result.ok ? pages.signedIn : pages.failed);
59
+ resolve(result);
60
+ }, async (error) => {
61
+ await send(response, 500, pages.failed);
62
+ reject(error instanceof Error ? error : new Error(String(error)));
63
+ });
64
+ });
65
+ });
66
+ // Not awaited on its own: an `open` that never settles (one waiting on a browser process)
67
+ // must not hold back the redirect, the timeout or an abort.
68
+ // Once a redirect is taken, its exchange decides: an opener failing later loses nothing.
69
+ const openFailed = Promise.resolve()
70
+ .then(() => options.open(url))
71
+ .then(() => new Promise(() => undefined), (cause) => redirect.taken ? new Promise(() => undefined) : Promise.resolve({ ok: false, error: { kind: 'open_failed', cause } }));
72
+ return await Promise.race([settled, openFailed]);
73
+ }
74
+ finally {
75
+ clearTimeout(timer);
76
+ if (onAbort !== undefined)
77
+ signal?.removeEventListener('abort', onAbort);
78
+ await close();
79
+ }
80
+ }
81
+ /**
82
+ * A server on 127.0.0.1 only (never every address: the code it receives would be reachable from
83
+ * the network), on a port of the system's choosing. `close` ends every connection with it.
84
+ */
85
+ export async function listenOnLoopback() {
86
+ const sockets = new Set();
87
+ const server = createServer();
88
+ server.on('connection', (socket) => {
89
+ sockets.add(socket);
90
+ socket.on('close', () => sockets.delete(socket));
91
+ });
92
+ const close = () => new Promise((resolve) => {
93
+ for (const socket of sockets)
94
+ socket.destroy();
95
+ server.close(() => {
96
+ resolve();
97
+ });
98
+ });
99
+ const port = await new Promise((resolve) => {
100
+ server.once('error', (cause) => {
101
+ resolve({ cause });
102
+ });
103
+ server.listen(0, '127.0.0.1', () => {
104
+ const address = server.address();
105
+ resolve(typeof address === 'object' && address !== null ? address.port : { cause: new Error('the server has no port') });
106
+ });
107
+ });
108
+ if (typeof port === 'number')
109
+ return { ok: true, server, port, close };
110
+ await close();
111
+ return { ok: false, cause: port.cause };
112
+ }
113
+ /**
114
+ * The page, sent whole: settles once it has left, so closing the port can't cut it short, or at
115
+ * once when the browser has already gone, which no event would then say.
116
+ */
117
+ function send(response, status, html) {
118
+ if (response.destroyed || response.writableEnded)
119
+ return Promise.resolve();
120
+ response.writeHead(status, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', connection: 'close' });
121
+ return new Promise((resolve) => {
122
+ response.once('close', resolve);
123
+ response.end(html, resolve);
124
+ });
125
+ }
126
+ //# sourceMappingURL=loopback.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loopback.js","sourceRoot":"","sources":["../../src/node/loopback.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA0D,MAAM,WAAW,CAAA;AAEhG,OAAO,EAAE,YAAY,EAAE,WAAW,EAAqD,MAAM,mBAAmB,CAAA;AA6BhH,MAAM,OAAO,GAAG,OAAO,CAAA;AAEvB,MAAM,IAAI,GAAG,CAAC,OAAe,EAAE,IAAY,EAAU,EAAE,CACrD,qEAAqE,OAAO,iBAAiB;IAC7F,iGAAiG;IACjG,OAAO,OAAO,WAAW,IAAI,MAAM;IACnC,kFAAkF;IAClF,+FAA+F,CAAA;AACjG,MAAM,KAAK,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,WAAW,EAAE,yBAA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type { Catalog, CatalogSource } from '../spec/query-planner/types.gen.js';
3
+ /** `GET /catalog`: every source the caller may query, with its dimensions and metrics. */
4
+ export declare function catalogList(base: string): CallSpec<Catalog>;
5
+ /**
6
+ * `GET /catalog/{source}`: one source. Null when `source` can't stay one path segment, so the
7
+ * call is refused before anything is sent.
8
+ */
9
+ export declare function catalogGet(base: string, source: string): CallSpec<CatalogSource> | null;
10
+ /**
11
+ * A value bound for one path segment, encoded, so `/`, `?` and `#` stay inside it; or null for
12
+ * '', '.' and '..', which a URL reads as steps, not names (`/catalog/..` is `/`), and for a value
13
+ * with a lone surrogate, which has no UTF-8 to encode.
14
+ */
15
+ export declare function segment(value: string): string | null;
@@ -0,0 +1,41 @@
1
+ import { zCatalogCatalogGetResponse, zCatalogSourceCatalogSourceGetResponse } from '../spec/query-planner/zod.gen.js';
2
+ /*
3
+ * The planner's catalogue: what each call sends, and the rules it is sent under, next to its
4
+ * schema. Reads, so a failed connection or an outage is tried again (D414).
5
+ */
6
+ const READ = { effect: 'read', service: 'query', deadlineMs: 30_000, retry: 'read' };
7
+ /** `GET /catalog`: every source the caller may query, with its dimensions and metrics. */
8
+ export function catalogList(base) {
9
+ return { ...READ, endpoint: 'catalog.list', makeRequest: () => new Request(`${base}/catalog`), schema: zCatalogCatalogGetResponse };
10
+ }
11
+ /**
12
+ * `GET /catalog/{source}`: one source. Null when `source` can't stay one path segment, so the
13
+ * call is refused before anything is sent.
14
+ */
15
+ export function catalogGet(base, source) {
16
+ const name = segment(source);
17
+ if (name === null)
18
+ return null;
19
+ return {
20
+ ...READ,
21
+ endpoint: 'catalog.get',
22
+ makeRequest: () => new Request(`${base}/catalog/${name}`),
23
+ schema: zCatalogSourceCatalogSourceGetResponse,
24
+ };
25
+ }
26
+ /**
27
+ * A value bound for one path segment, encoded, so `/`, `?` and `#` stay inside it; or null for
28
+ * '', '.' and '..', which a URL reads as steps, not names (`/catalog/..` is `/`), and for a value
29
+ * with a lone surrogate, which has no UTF-8 to encode.
30
+ */
31
+ export function segment(value) {
32
+ if (value === '' || value === '.' || value === '..')
33
+ return null;
34
+ try {
35
+ return encodeURIComponent(value);
36
+ }
37
+ catch {
38
+ return null;
39
+ }
40
+ }
41
+ //# sourceMappingURL=catalog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.js","sourceRoot":"","sources":["../../src/query/catalog.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,0BAA0B,EAAE,sCAAsC,EAAE,MAAM,kCAAkC,CAAA;AAErH;;;GAGG;AAEH,MAAM,IAAI,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAW,CAAA;AAE7F,0FAA0F;AAC1F,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,OAAO,EAAE,GAAG,IAAI,EAAE,QAAQ,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,IAAI,OAAO,CAAC,GAAG,IAAI,UAAU,CAAC,EAAE,MAAM,EAAE,0BAA0B,EAAE,CAAA;AACrI,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY,EAAE,MAAc;IACrD,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAC5B,IAAI,IAAI,KAAK,IAAI;QAAE,OAAO,IAAI,CAAA;IAC9B,OAAO;QACL,GAAG,IAAI;QACP,QAAQ,EAAE,aAAa;QACvB,WAAW,EAAE,GAAG,EAAE,CAAC,IAAI,OAAO,CAAC,GAAG,IAAI,YAAY,IAAI,EAAE,CAAC;QACzD,MAAM,EAAE,sCAAsC;KAC/C,CAAA;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,KAAa;IACnC,IAAI,KAAK,KAAK,EAAE,IAAI,KAAK,KAAK,GAAG,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,IAAI,CAAA;IAChE,IAAI,CAAC;QACH,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAA;IAClC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACb,CAAC;AACH,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { PlannerErrorCode } from '../spec/query-planner/types.gen.js';
2
+ /**
3
+ * Whether `code` is one the planner documents, narrowing it so a switch over the known codes can
4
+ * be exhaustive. A code it added since is kept as it came, and reads false here. KEYS_UNAVAILABLE
5
+ * is the one known to mean nothing ran (the planner couldn't check the token); whether a retry is
6
+ * safe stays the server's to say (DW37).
7
+ */
8
+ export declare function isPlannerCode(code: string | undefined): code is PlannerErrorCode;
@@ -0,0 +1,16 @@
1
+ import { zPlannerErrorCode } from '../spec/query-planner/zod.gen.js';
2
+ /*
3
+ * The planner's error codes (#61), from its spec's overlay until query-planner#27 declares them.
4
+ * The CLI's exit codes and hints stay the CLI's; there's no "did you mean" here (D417): the
5
+ * planner's own suggestions are typed once DW42 declares them.
6
+ */
7
+ /**
8
+ * Whether `code` is one the planner documents, narrowing it so a switch over the known codes can
9
+ * be exhaustive. A code it added since is kept as it came, and reads false here. KEYS_UNAVAILABLE
10
+ * is the one known to mean nothing ran (the planner couldn't check the token); whether a retry is
11
+ * safe stays the server's to say (DW37).
12
+ */
13
+ export function isPlannerCode(code) {
14
+ return zPlannerErrorCode.safeParse(code).success;
15
+ }
16
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/query/errors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAA;AAEpE;;;;GAIG;AAEH;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,IAAwB;IACpD,OAAO,iBAAiB,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,OAAO,CAAA;AAClD,CAAC"}
@@ -0,0 +1,27 @@
1
+ import type { CallSpec } from '../core/call.js';
2
+ import type { QueryResponse, SortSpec } from '../spec/query-planner/types.gen.js';
3
+ /**
4
+ * A query, as the planner reads it: its spec's LogicalRequest, without `table` (fail closed,
5
+ * D407). The planner honours `table` only for an unmodelled source, and whether public callers may
6
+ * name a table at all is still open (DW50). If it stays public, a later change adds it, typed as
7
+ * the spec says. Written out rather than derived, so the public type names nothing the package
8
+ * doesn't export (#99); the check below keeps it the spec's.
9
+ */
10
+ export interface QueryRequest {
11
+ source: string;
12
+ connection_id: string;
13
+ dimensions?: Array<string>;
14
+ metrics?: Array<string>;
15
+ filters?: Array<{
16
+ [key: string]: unknown;
17
+ }>;
18
+ sort?: Array<SortSpec>;
19
+ offset?: number;
20
+ rows?: number;
21
+ }
22
+ /**
23
+ * `POST /query`. It only reads, but it is never tried again (D414): a timeout at the gateway may
24
+ * have run on the warehouse. Its one resend, after a 401, is the transport's. 60 s, until the
25
+ * planner has a deadline of its own (DW38).
26
+ */
27
+ export declare function queryRun(base: string, request: QueryRequest): CallSpec<QueryResponse>;
@@ -0,0 +1,35 @@
1
+ import { zQueryQueryPostResponse } from '../spec/query-planner/zod.gen.js';
2
+ const matchesTheSpec = true;
3
+ void matchesTheSpec; // read once, so unused-locals checks keep it
4
+ /**
5
+ * `POST /query`. It only reads, but it is never tried again (D414): a timeout at the gateway may
6
+ * have run on the warehouse. Its one resend, after a 401, is the transport's. 60 s, until the
7
+ * planner has a deadline of its own (DW38).
8
+ */
9
+ export function queryRun(base, request) {
10
+ return {
11
+ effect: 'read',
12
+ service: 'query',
13
+ endpoint: 'query.run',
14
+ deadlineMs: 60_000,
15
+ retry: 'never',
16
+ // Built when the request is, inside the transport's attempt, so a value JSON can't carry (a
17
+ // BigInt, a cycle) rejects the call's promise rather than throwing from fs.query itself (D436).
18
+ makeRequest: () => new Request(`${base}/query`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: bodyOf(request) }),
19
+ schema: zQueryQueryPostResponse,
20
+ };
21
+ }
22
+ /** The fields a query sends, each picked from the request (D435). `table` is not one (DW50). */
23
+ const FIELDS = ['source', 'connection_id', 'dimensions', 'metrics', 'filters', 'sort', 'offset', 'rows'];
24
+ const everyFieldPicked = true;
25
+ void everyFieldPicked; // read once, so unused-locals checks keep it
26
+ /** The request as JSON: the declared fields, each left out when it's undefined. */
27
+ function bodyOf(request) {
28
+ const picked = {};
29
+ for (const field of FIELDS) {
30
+ if (request[field] !== undefined)
31
+ picked[field] = request[field];
32
+ }
33
+ return JSON.stringify(picked);
34
+ }
35
+ //# sourceMappingURL=query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/query/query.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,kCAAkC,CAAA;AAwB1E,MAAM,cAAc,GAAsD,IAAI,CAAA;AAC9E,KAAK,cAAc,CAAA,CAAC,6CAA6C;AAEjE;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAY,EAAE,OAAqB;IAC1D,OAAO;QACL,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,OAAO;QAChB,QAAQ,EAAE,WAAW;QACrB,UAAU,EAAE,MAAM;QAClB,KAAK,EAAE,OAAO;QACd,4FAA4F;QAC5F,gGAAgG;QAChG,WAAW,EAAE,GAAG,EAAE,CAAC,IAAI,OAAO,CAAC,GAAG,IAAI,QAAQ,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3I,MAAM,EAAE,uBAAuB;KAChC,CAAA;AACH,CAAC;AAED,gGAAgG;AAChG,MAAM,MAAM,GAAG,CAAC,QAAQ,EAAE,eAAe,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAoD,CAAA;AAK3J,MAAM,gBAAgB,GAAiD,IAAI,CAAA;AAC3E,KAAK,gBAAgB,CAAA,CAAC,6CA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