@flipstream/sdk 0.0.0-stage → 0.5.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 (93) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/LICENSE +202 -0
  3. package/README.md +491 -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 +23 -0
  8. package/dist/auth/discovery.js +71 -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 +106 -0
  17. package/dist/auth/signIn.js +125 -0
  18. package/dist/auth/signIn.js.map +1 -0
  19. package/dist/auth/tokenEndpoint.d.ts +18 -0
  20. package/dist/auth/tokenEndpoint.js +36 -0
  21. package/dist/auth/tokenEndpoint.js.map +1 -0
  22. package/dist/auth/transport.d.ts +18 -0
  23. package/dist/auth/transport.js +26 -0
  24. package/dist/auth/transport.js.map +1 -0
  25. package/dist/core/body.d.ts +14 -0
  26. package/dist/core/body.js +69 -0
  27. package/dist/core/body.js.map +1 -0
  28. package/dist/core/call.d.ts +17 -0
  29. package/dist/core/call.js +116 -0
  30. package/dist/core/call.js.map +1 -0
  31. package/dist/core/fetch.d.ts +43 -0
  32. package/dist/core/fetch.js +130 -0
  33. package/dist/core/fetch.js.map +1 -0
  34. package/dist/core/read.d.ts +17 -0
  35. package/dist/core/read.js +92 -0
  36. package/dist/core/read.js.map +1 -0
  37. package/dist/core/result.d.ts +96 -0
  38. package/dist/core/result.js +42 -0
  39. package/dist/core/result.js.map +1 -0
  40. package/dist/core/retry.d.ts +40 -0
  41. package/dist/core/retry.js +41 -0
  42. package/dist/core/retry.js.map +1 -0
  43. package/dist/core/transport.d.ts +62 -0
  44. package/dist/core/transport.js +9 -0
  45. package/dist/core/transport.js.map +1 -0
  46. package/dist/flipstream.d.ts +52 -0
  47. package/dist/flipstream.js +36 -0
  48. package/dist/flipstream.js.map +1 -0
  49. package/dist/index.d.ts +12 -0
  50. package/dist/index.js +8 -0
  51. package/dist/index.js.map +1 -0
  52. package/dist/node/clientSecret.d.ts +41 -0
  53. package/dist/node/clientSecret.js +124 -0
  54. package/dist/node/clientSecret.js.map +1 -0
  55. package/dist/node/fileLock.d.ts +27 -0
  56. package/dist/node/fileLock.js +248 -0
  57. package/dist/node/fileLock.js.map +1 -0
  58. package/dist/node/headless.d.ts +25 -0
  59. package/dist/node/headless.js +41 -0
  60. package/dist/node/headless.js.map +1 -0
  61. package/dist/node/index.d.ts +9 -0
  62. package/dist/node/index.js +11 -0
  63. package/dist/node/index.js.map +1 -0
  64. package/dist/node/loopback.d.ts +77 -0
  65. package/dist/node/loopback.js +183 -0
  66. package/dist/node/loopback.js.map +1 -0
  67. package/dist/node/session.d.ts +192 -0
  68. package/dist/node/session.js +480 -0
  69. package/dist/node/session.js.map +1 -0
  70. package/dist/query/catalog.d.ts +15 -0
  71. package/dist/query/catalog.js +41 -0
  72. package/dist/query/catalog.js.map +1 -0
  73. package/dist/query/errors.d.ts +8 -0
  74. package/dist/query/errors.js +16 -0
  75. package/dist/query/errors.js.map +1 -0
  76. package/dist/query/health.d.ts +16 -0
  77. package/dist/query/health.js +18 -0
  78. package/dist/query/health.js.map +1 -0
  79. package/dist/query/query.d.ts +35 -0
  80. package/dist/query/query.js +47 -0
  81. package/dist/query/query.js.map +1 -0
  82. package/dist/recorded/query-planner/catalog-google_search_console.json +140 -0
  83. package/dist/recorded/query-planner/catalog.json +1234 -0
  84. package/dist/spec/query-planner/index.d.ts +1 -0
  85. package/dist/spec/query-planner/index.js +3 -0
  86. package/dist/spec/query-planner/index.js.map +1 -0
  87. package/dist/spec/query-planner/types.gen.d.ts +300 -0
  88. package/dist/spec/query-planner/types.gen.js +3 -0
  89. package/dist/spec/query-planner/types.gen.js.map +1 -0
  90. package/dist/spec/query-planner/zod.gen.d.ts +325 -0
  91. package/dist/spec/query-planner/zod.gen.js +137 -0
  92. package/dist/spec/query-planner/zod.gen.js.map +1 -0
  93. package/package.json +99 -4
@@ -0,0 +1,130 @@
1
+ const SERVICES = ['query', 'api', 'auth'];
2
+ const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']);
3
+ const ABORTED = { kind: 'aborted' };
4
+ const REFUSED = { kind: 'refused', reason: 'destination' };
5
+ export function fetchTransport(options) {
6
+ const send = options.fetch ?? globalThis.fetch;
7
+ const allowed = destinations(options);
8
+ /**
9
+ * One send. The destination comes first, so a call that may not go asks for no token and spends
10
+ * no refresh. The Request is built once, and that same object is checked and sent, so a
11
+ * factory can't hand the check one URL and the network another.
12
+ */
13
+ async function attempt(call, ask) {
14
+ const built = call.makeRequest();
15
+ if (!allowed(call.service, built.url))
16
+ return REFUSED;
17
+ const token = await tokenFor(options.token, ask, call.signal);
18
+ if (token.kind !== 'token')
19
+ return token;
20
+ if (call.signal.aborted)
21
+ return ABORTED;
22
+ const headers = new Headers(built.headers);
23
+ try {
24
+ headers.set('authorization', `Bearer ${token.token}`);
25
+ }
26
+ catch (cause) {
27
+ // A token no header can carry (a line break, a NUL, a character past U+00FF) is no token.
28
+ return { kind: 'no_token', cause };
29
+ }
30
+ const sent = await sendRequest(send, built, headers, call.signal);
31
+ return sent.kind === 'sent' ? { ...sent, token: token.token } : sent;
32
+ }
33
+ return async (call) => {
34
+ const first = await attempt(call, {});
35
+ if (first.kind !== 'sent')
36
+ return first;
37
+ if (first.response.status !== 401)
38
+ return { kind: 'response', response: first.response };
39
+ // A 401 means nothing ran, so the one resend is safe for any method (D414). A second 401 is
40
+ // the answer.
41
+ void first.response.body?.cancel().catch(() => undefined);
42
+ const again = await attempt(call, { rejected: first.token });
43
+ return again.kind === 'sent' ? { kind: 'response', response: again.response } : again;
44
+ };
45
+ }
46
+ /**
47
+ * The default transport without a token, for `GET /health` (#111): the same destinations and the
48
+ * same send, but it asks no token function and adds no `authorization` header, so it works
49
+ * signed out. It never resends after a 401: there is no token to renew.
50
+ */
51
+ export function tokenlessTransport(options) {
52
+ const send = options.fetch ?? globalThis.fetch;
53
+ const allowed = destinations(options);
54
+ return async (call) => {
55
+ const built = call.makeRequest();
56
+ if (!allowed(call.service, built.url))
57
+ return REFUSED;
58
+ const sent = await sendRequest(send, built, new Headers(built.headers), call.signal);
59
+ return sent.kind === 'sent' ? { kind: 'response', response: sent.response } : sent;
60
+ };
61
+ }
62
+ /**
63
+ * Where a call may go: its own service's origin, over https, or over plain http to a loopback
64
+ * host when that is allowed. The hosts are parsed once: one that is not a URL throws here, at
65
+ * once, not on the first call.
66
+ */
67
+ function destinations({ hosts, allowLocalhostHttp }) {
68
+ const origins = new Map();
69
+ for (const service of SERVICES) {
70
+ const host = hosts[service];
71
+ if (host !== undefined)
72
+ origins.set(service, new URL(host).origin);
73
+ }
74
+ return (service, href) => {
75
+ const url = new URL(href);
76
+ const secure = url.protocol === 'https:' || (allowLocalhostHttp === true && url.protocol === 'http:' && LOOPBACK.has(url.hostname));
77
+ return secure && origins.get(service) === url.origin;
78
+ };
79
+ }
80
+ /**
81
+ * One request out, as every transport of the SDK's own sends it (sign-in's too, #63), with
82
+ * `headers` in place of `built`'s. Nothing is sent once `signal` has fired. No cookies, and no
83
+ * redirect followed: a redirect would take what the request carries (a token, a code) to a URL
84
+ * nobody checked. A 3xx comes back as it is, outside the contract (`readJson`).
85
+ */
86
+ export async function sendRequest(send, built, headers, signal) {
87
+ if (signal.aborted)
88
+ return ABORTED;
89
+ const request = new Request(built, { headers, signal, credentials: 'omit', redirect: 'manual' });
90
+ // A Request follows its signal only weakly. Held from the call's signal while it is sent, so
91
+ // a fetch that keeps it only through its own abort listener is still told when the call ends
92
+ // (#57: without this, under forced garbage collection, it was told 0 times in 20).
93
+ const holdRequest = () => void request;
94
+ signal.addEventListener('abort', holdRequest, { once: true });
95
+ try {
96
+ return { kind: 'sent', response: await send(request) };
97
+ }
98
+ catch {
99
+ return signal.aborted ? ABORTED : { kind: 'unavailable' };
100
+ }
101
+ finally {
102
+ signal.removeEventListener('abort', holdRequest);
103
+ }
104
+ }
105
+ /**
106
+ * The token function's answer, raced against the call's signal: an abort while it is pending
107
+ * ends the call at once (#30). The function itself runs on; its answer is dropped.
108
+ */
109
+ async function tokenFor(token, ask, signal) {
110
+ if (signal.aborted)
111
+ return ABORTED;
112
+ let stop = () => undefined;
113
+ const aborted = new Promise((resolve) => {
114
+ stop = () => resolve(ABORTED);
115
+ signal.addEventListener('abort', stop, { once: true });
116
+ });
117
+ // Through then(), so a function that throws at once is caught like one that rejects.
118
+ const asked = Promise.resolve()
119
+ .then(() => token(ask))
120
+ .then((value) => typeof value === 'string' && value !== ''
121
+ ? { kind: 'token', token: value }
122
+ : { kind: 'no_token', cause: new TypeError('the token function gave no token') }, (cause) => ({ kind: 'no_token', cause }));
123
+ try {
124
+ return await Promise.race([asked, aborted]);
125
+ }
126
+ finally {
127
+ signal.removeEventListener('abort', stop);
128
+ }
129
+ }
130
+ //# sourceMappingURL=fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/core/fetch.ts"],"names":[],"mappings":"AA2BA,MAAM,QAAQ,GAA2B,CAAC,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAA;AACjE,MAAM,QAAQ,GAAwB,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC,CAAA;AAClF,MAAM,OAAO,GAAG,EAAE,IAAI,EAAE,SAAS,EAAW,CAAA;AAC5C,MAAM,OAAO,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,aAAa,EAAW,CAAA;AAOnE,MAAM,UAAU,cAAc,CAAC,OAA8B;IAC3D,MAAM,IAAI,GAAG,OAAO,CAAC,KA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ACvC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE;QACjC,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,IAAI,SAAS,CAAC,kCAAkC,CAAC,EAAE,EACpF,CAAC,KAAc,EAAS,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CACzD,CAAA;IACH,IAAI,CAAC;QACH,OAAO,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAA;IAC7C,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;IAC3C,CAAC;AACH,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { z } from 'zod';
2
+ import { type ApiError, type ApiResult } from './result.js';
3
+ import type { ServiceName, TransportResult } from './transport.js';
4
+ type NoAnswer = Exclude<TransportResult, {
5
+ readonly kind: 'response';
6
+ }>;
7
+ /** Why a transport has no answer to read. */
8
+ export declare function noAnswer(sent: NoAnswer): ApiError;
9
+ /**
10
+ * A transport's answer, read under the call's `signal` and checked against `schema`: the data, or
11
+ * the one failure it is. A failure status keeps what the host said (`Said`), read by `service`'s
12
+ * own error envelope (D442). A 2xx whose body is
13
+ * not JSON, or fails its schema, is `invalid`: reported, never shown as empty. Zod's issues name
14
+ * paths and expected types, never the body's values.
15
+ */
16
+ export declare function readJson<T>(sent: TransportResult, schema: z.ZodType<T>, signal: AbortSignal, service: ServiceName): Promise<ApiResult<T>>;
17
+ export {};
@@ -0,0 +1,92 @@
1
+ import { z } from 'zod';
2
+ import { parseJson, said } from './body.js';
3
+ import { classifyStatus } from './result.js';
4
+ /** Why a transport has no answer to read. */
5
+ export function noAnswer(sent) {
6
+ switch (sent.kind) {
7
+ case 'refused':
8
+ return { kind: 'refused' };
9
+ case 'unavailable':
10
+ return { kind: 'unavailable' };
11
+ case 'cancelled':
12
+ return { kind: 'cancelled' };
13
+ case 'aborted':
14
+ return { kind: 'aborted' };
15
+ case 'no_token':
16
+ return { kind: 'no_token', cause: sent.cause };
17
+ default: {
18
+ const unhandled = sent;
19
+ throw new Error(`unhandled transport result ${JSON.stringify(unhandled)}`);
20
+ }
21
+ }
22
+ }
23
+ /**
24
+ * A transport's answer, read under the call's `signal` and checked against `schema`: the data, or
25
+ * the one failure it is. A failure status keeps what the host said (`Said`), read by `service`'s
26
+ * own error envelope (D442). A 2xx whose body is
27
+ * not JSON, or fails its schema, is `invalid`: reported, never shown as empty. Zod's issues name
28
+ * paths and expected types, never the body's values.
29
+ */
30
+ export async function readJson(sent, schema, signal, service) {
31
+ if (sent.kind !== 'response')
32
+ return failed(noAnswer(sent));
33
+ const { response } = sent;
34
+ const { status } = response;
35
+ const meaning = classifyStatus(status);
36
+ if (meaning === 'outside-contract') {
37
+ void response.body?.cancel().catch(() => undefined);
38
+ return failed({ kind: 'invalid', issues: `HTTP status ${String(status)} is outside the contract`, status });
39
+ }
40
+ const body = await readText(response, signal);
41
+ if (!body.ok)
42
+ return failed(body.error);
43
+ if (meaning !== 'success')
44
+ return failed({ kind: meaning, status, ...said(body.text, service) });
45
+ const json = parseJson(body.text);
46
+ if (!json.ok)
47
+ return failed({ kind: 'invalid', issues: 'the body is not JSON', status, text: body.text });
48
+ const parsed = schema.safeParse(json.value);
49
+ if (!parsed.success)
50
+ return failed({ kind: 'invalid', issues: z.prettifyError(parsed.error), status });
51
+ return { ok: true, data: parsed.data };
52
+ }
53
+ /**
54
+ * The whole body as text, or why there is none: the caller aborted, or it broke off. A
55
+ * transport's body need not observe the signal (one read from memory doesn't, and a transport
56
+ * may hand over any stream), so the read holds its own reader and cancels it when the signal
57
+ * fires: the call ends at once, the body is let go, and late data is never handed over.
58
+ */
59
+ async function readText(response, signal) {
60
+ const aborted = { ok: false, error: { kind: 'aborted' } };
61
+ const { body } = response;
62
+ if (signal.aborted) {
63
+ void body?.cancel().catch(() => undefined);
64
+ return aborted;
65
+ }
66
+ if (body === null)
67
+ return { ok: true, text: '' };
68
+ const reader = body.getReader();
69
+ const stop = () => void reader.cancel().catch(() => undefined);
70
+ signal.addEventListener('abort', stop, { once: true });
71
+ // As Response.text() decodes: UTF-8, a leading BOM dropped, bad bytes replaced.
72
+ const decoder = new TextDecoder();
73
+ let text = '';
74
+ try {
75
+ for (;;) {
76
+ const chunk = await reader.read();
77
+ if (chunk.done)
78
+ break;
79
+ text += decoder.decode(chunk.value, { stream: true });
80
+ }
81
+ text += decoder.decode();
82
+ return signal.aborted ? aborted : { ok: true, text };
83
+ }
84
+ catch {
85
+ return signal.aborted ? aborted : { ok: false, error: { kind: 'unavailable' } };
86
+ }
87
+ finally {
88
+ signal.removeEventListener('abort', stop);
89
+ }
90
+ }
91
+ const failed = (error) => ({ ok: false, error });
92
+ //# sourceMappingURL=read.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.js","sourceRoot":"","sources":["../../src/core/read.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAC3C,OAAO,EAAE,cAAc,EAAiC,MAAM,aAAa,CAAA;AAK3E,6CAA6C;AAC7C,MAAM,UAAU,QAAQ,CAAC,IAAc;IACrC,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QAClB,KAAK,SAAS;YACZ,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAA;QAC5B,KAAK,aAAa;YAChB,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,CAAA;QAChC,KAAK,WAAW;YACd,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,CAAA;QAC9B,KAAK,SAAS;YACZ,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAA;QAC5B,KAAK,UAAU;YACb,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,IAAI,CA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,CAAC,SAAS,CAAC,CAAA;IACpE,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;IACtD,gFAAgF;IAChF,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;IACjC,IAAI,IAAI,GAAG,EAAE,CAAA;IACb,IAAI,CAAC;QACH,SAAS,CAAC;YACR,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;YACjC,IAAI,KAAK,CAAC,IAAI;gBAAE,MAAK;YACrB,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAA;QACvD,CAAC;QACD,IAAI,IAAI,OAAO,CAAC,MAAM,EAAE,CAAA;QACxB,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAA;IACtD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,CAAA;IACjF,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;IAC3C,CAAC;AACH,CAAC;AAED,MAAM,MAAM,GAAG,CAAC,KAAe,EAAoD,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAA"}
@@ -0,0 +1,96 @@
1
+ import type { PlannerErrorCode } from '../spec/query-planner/types.gen.js';
2
+ /** What every call returns: its data, or one failure with one meaning (D412). */
3
+ export type ApiResult<T> = {
4
+ readonly ok: true;
5
+ readonly data: T;
6
+ } | {
7
+ readonly ok: false;
8
+ readonly error: ApiError;
9
+ };
10
+ /** The failures a status gives: the host answered, and said no. */
11
+ export type StatusKind = 'unauthorized' | 'forbidden' | 'not_found' | 'conflict' | 'rejected' | 'unavailable';
12
+ /**
13
+ * A host's error code: one of the codes the planner documents (#61), or one it added since, kept
14
+ * as it came, never dropped. `isPlannerCode` narrows it to the known ones, so a switch over them
15
+ * can be exhaustive. Whether a code means a retry is safe is the server's to say (DW37), never
16
+ * inferred here: until then, each endpoint's rule decides.
17
+ */
18
+ export type ErrorCode = PlannerErrorCode | (string & Record<never, never>);
19
+ /**
20
+ * What came with a failure status: the host's error, read from its body (today the planner's
21
+ * `{ code, message }`, D429), or the body as it came, when it is not one (a proxy's `Bad gateway`).
22
+ */
23
+ export type Said = {
24
+ readonly code: ErrorCode;
25
+ readonly message: string;
26
+ /** The request's id, once a host sends one. None does yet (D429). */
27
+ readonly requestId?: string;
28
+ readonly text?: never;
29
+ } | {
30
+ readonly text: string;
31
+ readonly code?: never;
32
+ readonly message?: never;
33
+ readonly requestId?: never;
34
+ };
35
+ /** A failure the host answered with a status, and what came with it. */
36
+ export type Answered = {
37
+ readonly kind: StatusKind;
38
+ readonly status: number;
39
+ /** How long the host asked the caller to wait (`Retry-After`), when it said so. */
40
+ readonly retryAfterMs?: number;
41
+ } & Said;
42
+ export type ApiError = Answered
43
+ /** No answer at all: offline, a body that broke off, or no token right now. */
44
+ | {
45
+ readonly kind: 'unavailable';
46
+ readonly status?: never;
47
+ }
48
+ /**
49
+ * Outside the contract: reported, never shown as empty. `status` is the response's, when one
50
+ * arrived; `text` is its body, when a 2xx body is not JSON.
51
+ */
52
+ | {
53
+ readonly kind: 'invalid';
54
+ readonly issues: string;
55
+ readonly status?: number;
56
+ readonly text?: string;
57
+ }
58
+ /** The app's session moved while the call ran: nobody may see the answer. */
59
+ | {
60
+ readonly kind: 'cancelled';
61
+ }
62
+ /** The caller's own abort (D409). */
63
+ | {
64
+ readonly kind: 'aborted';
65
+ }
66
+ /** The call's own deadline passed first (#15). A write's outcome is then unknown: it may have run. */
67
+ | {
68
+ readonly kind: 'timeout';
69
+ }
70
+ /**
71
+ * Nothing was sent, by rule: the destination isn't one of the configured hosts over https, a
72
+ * path value can't stay one segment ('', '.', '..'), or the request has keys the call doesn't
73
+ * send, named in `fields`. No token was asked for, and none went anywhere.
74
+ */
75
+ | {
76
+ readonly kind: 'refused';
77
+ readonly fields?: readonly [string, ...string[]];
78
+ }
79
+ /** The token function failed, or gave no token, so nothing was sent; `cause` is what it threw. */
80
+ | {
81
+ readonly kind: 'no_token';
82
+ readonly cause: unknown;
83
+ };
84
+ /**
85
+ * What an HTTP status means (the console's table, D412). Only 2xx is a success. A status no host
86
+ * should send (1xx, 3xx, 0 from an opaque redirect, or not a whole number) is outside the
87
+ * contract, so it is reported, never quietly passed.
88
+ */
89
+ export declare function classifyStatus(status: number): 'success' | StatusKind | 'outside-contract';
90
+ /** A failed call, thrown by `orThrow`, carrying its `ApiError`. */
91
+ export declare class ApiFailure extends Error {
92
+ readonly error: ApiError;
93
+ constructor(error: ApiError);
94
+ }
95
+ /** The data, or the failure thrown as `ApiFailure`, for callers who want exceptions (D412). */
96
+ export declare function orThrow<T>(result: ApiResult<T>): T;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * What an HTTP status means (the console's table, D412). Only 2xx is a success. A status no host
3
+ * should send (1xx, 3xx, 0 from an opaque redirect, or not a whole number) is outside the
4
+ * contract, so it is reported, never quietly passed.
5
+ */
6
+ export function classifyStatus(status) {
7
+ if (!Number.isInteger(status))
8
+ return 'outside-contract';
9
+ if (status >= 200 && status <= 299)
10
+ return 'success';
11
+ if (status === 401)
12
+ return 'unauthorized';
13
+ if (status === 403)
14
+ return 'forbidden';
15
+ if (status === 404)
16
+ return 'not_found';
17
+ if (status === 409 || status === 412)
18
+ return 'conflict';
19
+ if (status === 429 || (status >= 500 && status <= 599))
20
+ return 'unavailable';
21
+ if (status >= 400 && status <= 499)
22
+ return 'rejected';
23
+ return 'outside-contract';
24
+ }
25
+ /** A failed call, thrown by `orThrow`, carrying its `ApiError`. */
26
+ export class ApiFailure extends Error {
27
+ error;
28
+ constructor(error) {
29
+ super(error.kind === 'refused' && error.fields !== undefined
30
+ ? `the call failed: refused, for fields it doesn't send: ${error.fields.join(', ')}`
31
+ : `the call failed: ${error.kind}`);
32
+ this.name = 'ApiFailure';
33
+ this.error = error;
34
+ }
35
+ }
36
+ /** The data, or the failure thrown as `ApiFailure`, for callers who want exceptions (D412). */
37
+ export function orThrow(result) {
38
+ if (!result.ok)
39
+ throw new ApiFailure(result.error);
40
+ return result.data;
41
+ }
42
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/core/result.ts"],"names":[],"mappings":"AAgFA;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,MAAc;IAC3C,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC;QAAE,OAAO,kBAAkB,CAAA;IACxD,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,IAAI,GAAG;QAAE,OAAO,SAAS,CAAA;IACpD,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,cAAc,CAAA;IACzC,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,WAAW,CAAA;IACtC,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,WAAW,CAAA;IACtC,IAAI,MAAM,KAAK,GAAG,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,UAAU,CAAA;IACvD,IAAI,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,IAAI,GAAG,IAAI,MAAM,IAAI,GAAG,CAAC;QAAE,OAAO,aAAa,CAAA;IAC5E,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,IAAI,GAAG;QAAE,OAAO,UAAU,CAAA;IACrD,OAAO,kBAAkB,CAAA;AAC3B,CAAC;AAED,mEAAmE;AACnE,MAAM,OAAO,UAAW,SAAQ,KAAK;IAC1B,KAAK,CAAU;IAExB,YAAY,KAAe;QACzB,KAAK,CACH,KAAK,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS;YACpD,CAAC,CAAC,yDAAyD,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YACpF,CAAC,CAAC,oBAAoB,KAAK,CAAC,IAAI,EAAE,CACrC,CAAA;QACD,IAAI,CAAC,IAAI,GAAG,YAAY,CAAA;QACxB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;CACF;AAED,+FAA+F;AAC/F,MAAM,UAAU,OAAO,CAAI,MAAoB;IAC7C,IAAI,CAAC,MAAM,CAAC,EAAE;QAAE,MAAM,IAAI,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAClD,OAAO,MAAM,CAAC,IAAI,CAAA;AACpB,CAAC"}
@@ -0,0 +1,40 @@
1
+ /** An endpoint's rule, declared next to its schema: `never` for `POST /query` (D414), `read` for GETs. */
2
+ export type RetryRule = 'never' | 'read';
3
+ /** What one attempt came to, as far as the rule cares. */
4
+ export type Outcome = {
5
+ readonly kind: 'answered';
6
+ readonly status: number;
7
+ readonly retryAfter: string | null;
8
+ }
9
+ /** No answer: offline, or a connection that failed. */
10
+ | {
11
+ readonly kind: 'unavailable';
12
+ }
13
+ /** Refused, no token, cancelled or aborted: never tried again (D433). */
14
+ | {
15
+ readonly kind: 'final';
16
+ };
17
+ export type Decision = {
18
+ readonly kind: 'stop';
19
+ } | {
20
+ readonly kind: 'wait';
21
+ readonly ms: number;
22
+ };
23
+ /**
24
+ * Whether to try again after `attempt` attempts, and after how long. A wait that would not end
25
+ * before `deadline` is not started: the outcome is the answer, and the caller decides.
26
+ */
27
+ export declare function decide(rule: RetryRule, attempt: number, outcome: Outcome, now: number, deadline: number): Decision;
28
+ /**
29
+ * The ceiling for a host's stated wait: a hostile or broken header must never park a caller for
30
+ * hours (`Retry-After: 999999999` is about 31 years). From the CLI's `api/http.ts`.
31
+ */
32
+ export declare const MAX_RETRY_AFTER_MS = 3600000;
33
+ /**
34
+ * `Retry-After` as milliseconds, in its delay-seconds form only (D434): digits, so no sign,
35
+ * fraction, hex or exponent, clamped to MAX_RETRY_AFTER_MS. An HTTP date in any of its three forms
36
+ * reads as no hint, and the backoff applies: no Flipstream host sends one, and the asks to the
37
+ * planner and #91 say seconds. `undefined` when absent or not seconds, never a guess. Moved from
38
+ * the CLI's `parseRetryAfterMs`; `retry-after-ms` is not read (#14).
39
+ */
40
+ export declare function parseRetryAfterMs(header: string | null): number | undefined;
@@ -0,0 +1,41 @@
1
+ /*
2
+ * When a call is tried again (D414, D433): a pure decision, with no IO and no clock of its own,
3
+ * so every rule is tested without a server or a timer.
4
+ */
5
+ /** The statuses a read is tried again after (D414). A 500 is not one: it says the server failed. */
6
+ const READ_STATUSES = new Set([408, 429, 502, 503, 504]);
7
+ const MAX_RETRIES = 2;
8
+ const STOP = { kind: 'stop' };
9
+ /**
10
+ * Whether to try again after `attempt` attempts, and after how long. A wait that would not end
11
+ * before `deadline` is not started: the outcome is the answer, and the caller decides.
12
+ */
13
+ export function decide(rule, attempt, outcome, now, deadline) {
14
+ if (rule === 'never' || outcome.kind === 'final' || attempt > MAX_RETRIES)
15
+ return STOP;
16
+ if (outcome.kind === 'answered' && !READ_STATUSES.has(outcome.status))
17
+ return STOP;
18
+ const asked = outcome.kind === 'answered' ? parseRetryAfterMs(outcome.retryAfter) : undefined;
19
+ // When the host names no wait: 500 ms, then 1 s. No jitter, so the decision stays pure.
20
+ const ms = asked ?? (attempt === 1 ? 500 : 1000);
21
+ return now + ms < deadline ? { kind: 'wait', ms } : STOP;
22
+ }
23
+ /**
24
+ * The ceiling for a host's stated wait: a hostile or broken header must never park a caller for
25
+ * hours (`Retry-After: 999999999` is about 31 years). From the CLI's `api/http.ts`.
26
+ */
27
+ export const MAX_RETRY_AFTER_MS = 3_600_000;
28
+ /**
29
+ * `Retry-After` as milliseconds, in its delay-seconds form only (D434): digits, so no sign,
30
+ * fraction, hex or exponent, clamped to MAX_RETRY_AFTER_MS. An HTTP date in any of its three forms
31
+ * reads as no hint, and the backoff applies: no Flipstream host sends one, and the asks to the
32
+ * planner and #91 say seconds. `undefined` when absent or not seconds, never a guess. Moved from
33
+ * the CLI's `parseRetryAfterMs`; `retry-after-ms` is not read (#14).
34
+ */
35
+ export function parseRetryAfterMs(header) {
36
+ if (header === null)
37
+ return undefined;
38
+ const trimmed = header.trim();
39
+ return /^\d{1,10}$/.test(trimmed) ? Math.min(MAX_RETRY_AFTER_MS, Number(trimmed) * 1000) : undefined;
40
+ }
41
+ //# sourceMappingURL=retry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/core/retry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAeH,oGAAoG;AACpG,MAAM,aAAa,GAAwB,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;AAC7E,MAAM,WAAW,GAAG,CAAC,CAAA;AACrB,MAAM,IAAI,GAAa,EAAE,IAAI,EAAE,MAAM,EAAE,CAAA;AAEvC;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,IAAe,EAAE,OAAe,EAAE,OAAgB,EAAE,GAAW,EAAE,QAAgB;IACtG,IAAI,IAAI,KAAK,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,IAAI,OAAO,GAAG,WAAW;QAAE,OAAO,IAAI,CAAA;IACtF,IAAI,OAAO,CAAC,IAAI,KAAK,UAAU,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAA;IAClF,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,iBAAiB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;IAC7F,wFAAwF;IACxF,MAAM,EAAE,GAAG,KAAK,IAAI,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;IAChD,OAAO,GAAG,GAAG,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;AAC1D,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,SAAS,CAAA;AAE3C;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAqB;IACrD,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO,SAAS,CAAA;IACrC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,EAAE,CAAA;IAC7B,OAAO,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,kBAAkB,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AACtG,CAAC"}
@@ -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,52 @@
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 Health } from './query/health.js';
5
+ import { type QueryRequest } from './query/query.js';
6
+ import type { Catalog, CatalogSource, QueryResponse } from './spec/query-planner/types.gen.js';
7
+ /** Each service's base URL. Only public hosts (D410); a call goes only to its own service's. */
8
+ export type Hosts = Readonly<Partial<Record<ServiceName, string>>>;
9
+ /** Asked before every attempt, and once more after a 401 with the token the host refused. */
10
+ export type TokenFunction = (ask: TokenAsk) => string | Promise<string>;
11
+ export type FlipstreamOptions =
12
+ /** The console: its own transport, which attaches the token and decides what may go. */
13
+ {
14
+ readonly transport: Transport;
15
+ readonly hosts?: Hosts;
16
+ readonly token?: never;
17
+ readonly fetch?: never;
18
+ readonly allowLocalhostHttp?: never;
19
+ }
20
+ /** The CLI and customers: a token function, and the caller's own fetch if it likes. */
21
+ | {
22
+ readonly token: TokenFunction;
23
+ readonly fetch?: typeof fetch;
24
+ readonly hosts?: Hosts;
25
+ /** Plain http to a loopback host, for development and tests only. Off unless set. */
26
+ readonly allowLocalhostHttp?: boolean;
27
+ readonly transport?: never;
28
+ };
29
+ export interface CallOptions {
30
+ /** Ends the call at once: its answer is `aborted`. */
31
+ readonly signal?: AbortSignal;
32
+ /** The whole call's deadline, every attempt and wait included; the endpoint's own when left out. */
33
+ readonly deadlineMs?: number;
34
+ }
35
+ /** What `createFlipstream` gives: each call settles into an `ApiResult`, never a thrown error (D412). */
36
+ export interface Flipstream {
37
+ readonly catalog: {
38
+ list(options?: CallOptions): Promise<ApiResult<Catalog>>;
39
+ get(request: {
40
+ readonly source: string;
41
+ }, options?: CallOptions): Promise<ApiResult<CatalogSource>>;
42
+ };
43
+ query(request: QueryRequest, options?: CallOptions): Promise<ApiResult<QueryResponse>>;
44
+ /**
45
+ * Whether the planner answers, signed out: `GET /health` with no token. It goes through neither
46
+ * the app's transport nor the token function, since they hold the token, but straight to the
47
+ * query host, under the same destination rule. A 200 with an object of strings is up.
48
+ */
49
+ health(options?: CallOptions): Promise<ApiResult<Health>>;
50
+ }
51
+ /** The SDK, for one caller (D421): the console with its transport, or anyone with a token function. */
52
+ export declare function createFlipstream(options: FlipstreamOptions): Flipstream;