@flipstream/sdk 0.12.0 → 0.13.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 (48) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +84 -14
  3. package/dist/api/backend.d.ts +11 -3
  4. package/dist/api/backend.js +26 -6
  5. package/dist/api/backend.js.map +1 -1
  6. package/dist/api/me.js +2 -1
  7. package/dist/api/me.js.map +1 -1
  8. package/dist/api/wallet.js +4 -1
  9. package/dist/api/wallet.js.map +1 -1
  10. package/dist/api/workspaces.d.ts +7 -7
  11. package/dist/api/workspaces.js +7 -7
  12. package/dist/auth/discovery.js +5 -2
  13. package/dist/auth/discovery.js.map +1 -1
  14. package/dist/core/call.d.ts +10 -1
  15. package/dist/core/call.js +9 -4
  16. package/dist/core/call.js.map +1 -1
  17. package/dist/core/identify.d.ts +59 -14
  18. package/dist/core/identify.js +54 -20
  19. package/dist/core/identify.js.map +1 -1
  20. package/dist/core/invalid.d.ts +5 -2
  21. package/dist/core/invalid.js +10 -4
  22. package/dist/core/invalid.js.map +1 -1
  23. package/dist/core/read.d.ts +9 -0
  24. package/dist/core/read.js +14 -5
  25. package/dist/core/read.js.map +1 -1
  26. package/dist/core/result.d.ts +9 -5
  27. package/dist/core/result.js.map +1 -1
  28. package/dist/core/shared.d.ts +39 -0
  29. package/dist/core/shared.js +177 -0
  30. package/dist/core/shared.js.map +1 -0
  31. package/dist/core/signal.d.ts +12 -0
  32. package/dist/core/signal.js +25 -0
  33. package/dist/core/signal.js.map +1 -0
  34. package/dist/flipstream.d.ts +87 -3
  35. package/dist/flipstream.js +197 -82
  36. package/dist/flipstream.js.map +1 -1
  37. package/dist/index.d.ts +1 -1
  38. package/dist/node/clientSecret.js +2 -1
  39. package/dist/node/clientSecret.js.map +1 -1
  40. package/dist/node/loopback.d.ts +5 -2
  41. package/dist/node/loopback.js +28 -3
  42. package/dist/node/loopback.js.map +1 -1
  43. package/dist/node/session.js +13 -6
  44. package/dist/node/session.js.map +1 -1
  45. package/dist/version.gen.js +1 -1
  46. package/docs/admin.md +56 -1
  47. package/docs/node-sign-in.md +3 -1
  48. package/package.json +1 -1
@@ -0,0 +1,177 @@
1
+ import { z } from 'zod';
2
+ import { NOT_JSON, readParsed } from './read.js';
3
+ import { classifyStatus } from './result.js';
4
+ import { stopListening } from './signal.js';
5
+ // Built fresh each time, so no two calls hold one object.
6
+ const timeout = () => ({ ok: false, error: { kind: 'timeout' } });
7
+ const aborted = () => ({ ok: false, error: { kind: 'aborted' } });
8
+ const unwanted = () => new DOMException('no call waits on this read any more', 'AbortError');
9
+ const closed = () => new DOMException('the scope has closed', 'AbortError');
10
+ /**
11
+ * Whether a shared read's failure is definitive, and sticks for the scope, or transient, and is read
12
+ * again (#376's shape, item 4). A 401 and `cancelled` are neither: each drops every shared read
13
+ * (item 6). A 408 or 425 is a moment's, and a 2xx whose body isn't JSON, or a status outside the
14
+ * contract (a proxy's or a sign-in gate's redirect), is a proxy's or a portal's answer, never the
15
+ * service's (#377's code review, 2; rounds 3 and 4, O3.T3 and O4.F1).
16
+ */
17
+ function sticks(error) {
18
+ switch (error.kind) {
19
+ case 'forbidden':
20
+ case 'not_found':
21
+ case 'conflict':
22
+ case 'refused':
23
+ return true;
24
+ case 'rejected':
25
+ return error.status !== 408 && error.status !== 425;
26
+ case 'invalid':
27
+ return error.issues !== NOT_JSON && (error.status === undefined || classifyStatus(error.status) !== 'outside-contract');
28
+ case 'unauthorized':
29
+ case 'unavailable':
30
+ case 'timeout':
31
+ case 'aborted':
32
+ case 'cancelled':
33
+ case 'no_token':
34
+ case 'needs_person':
35
+ return false;
36
+ default: {
37
+ const unhandled = error;
38
+ return unhandled;
39
+ }
40
+ }
41
+ }
42
+ /**
43
+ * `error` as an object of its own, for each call that gets it: a shallow copy, so a no_token's
44
+ * `cause` is still what the token function threw, its class and fields included, never a clone
45
+ * (#377 round 2, O2.B1). A refusal's `fields` are copied too; every other field is a primitive.
46
+ */
47
+ function copyOf(error) {
48
+ if (error.kind === 'refused' && error.fields !== undefined) {
49
+ const [first, ...rest] = error.fields;
50
+ return { ok: false, error: { ...error, fields: [first, ...rest] } };
51
+ }
52
+ return { ok: false, error: { ...error } };
53
+ }
54
+ /** The reads of one scope, empty and open. */
55
+ export function sharedReads() {
56
+ // The reads a call may join, by key; and every read still in flight, joined or not, for close().
57
+ const entries = new Map();
58
+ const live = new Set();
59
+ // Raised by every drop, so a read begun before one keeps nothing when it answers (O2.F3).
60
+ let generation = 0;
61
+ let open = true;
62
+ /** No call joins `entry` any more: a later call reads again. */
63
+ const forget = (entry) => {
64
+ if (entries.get(entry.key) === entry)
65
+ entries.delete(entry.key);
66
+ };
67
+ /** Every shared read dropped (item 6): the next call reads them all again. */
68
+ const dropAll = () => {
69
+ entries.clear();
70
+ generation += 1;
71
+ };
72
+ /**
73
+ * What the scope keeps of a read's outcome, as it settles, before any waiter hears it: an answer and
74
+ * a definitive failure, kept even when the read's last waiter left in the same tick it answered
75
+ * (#377's code review, 6), unless a newer read took its key. A read begun before a drop keeps nothing.
76
+ */
77
+ const settle = (entry, outcome) => {
78
+ entry.settled = true;
79
+ live.delete(entry);
80
+ if (entry.born !== generation)
81
+ return;
82
+ // The identity behind the scope moved (O4.F2): the service no longer takes it, or the app's own
83
+ // transport says its person or scope did.
84
+ if (outcome?.ok === false && (outcome.error.kind === 'unauthorized' || outcome.error.kind === 'cancelled'))
85
+ dropAll();
86
+ else if (outcome !== undefined && (outcome.ok || sticks(outcome.error))) {
87
+ if (!entries.has(entry.key))
88
+ entries.set(entry.key, entry);
89
+ }
90
+ else
91
+ forget(entry);
92
+ };
93
+ const begin = (key, spec, first, start) => {
94
+ const end = new AbortController();
95
+ let status;
96
+ const sent = start({ ...spec, schema: z.unknown() }, first.deadline.left(), end.signal, (answered) => (status = answered));
97
+ // Settled in the read's own resolution, so every waiter hears an outcome the scope has kept or dropped.
98
+ const answer = sent.answer.then((outcome) => {
99
+ entry.status = status;
100
+ settle(entry, outcome);
101
+ return outcome;
102
+ }, (error) => {
103
+ settle(entry, undefined);
104
+ throw error;
105
+ });
106
+ // Seen at once, so a rejection no call waits on is never unhandled.
107
+ answer.catch(() => undefined);
108
+ const entry = { key, answer, origin: sent.origin, waiters: new Set([first]), end, born: generation, status: undefined, settled: false };
109
+ entries.set(key, entry);
110
+ live.add(entry);
111
+ return entry;
112
+ };
113
+ /** The read's outcome, or `waiter`'s own end first: its deadline, its caller's abort, or its call's end. */
114
+ const waitFor = (entry, waiter) => new Promise((resolve) => {
115
+ let done = false;
116
+ const finish = (waited) => {
117
+ if (done)
118
+ return;
119
+ done = true;
120
+ clearTimeout(timer);
121
+ // A minimal polyfill signal may have no removeEventListener; it keeps its one listener.
122
+ stopListening(waiter.signal, onAbort);
123
+ waiter.end.removeEventListener('abort', onAbort);
124
+ entry.waiters.delete(waiter);
125
+ // Forgotten as it's ended, so a call that asks next starts a read of its own, never one ending.
126
+ if (!entry.settled && entry.waiters.size === 0) {
127
+ forget(entry);
128
+ entry.end.abort(unwanted());
129
+ }
130
+ resolve(waited);
131
+ };
132
+ const onAbort = () => finish({ own: aborted() });
133
+ const timer = setTimeout(() => finish({ own: timeout() }), waiter.deadline.left());
134
+ waiter.signal?.addEventListener('abort', onAbort, { once: true });
135
+ waiter.end.addEventListener('abort', onAbort, { once: true });
136
+ entry.answer.then((read) => finish({ read }), (rejected) => finish({ rejected }));
137
+ });
138
+ return {
139
+ isOpen: () => open,
140
+ async read(spec, rule, waiter, start) {
141
+ const key = `${spec.endpoint} ${rule.key}`;
142
+ for (;;) {
143
+ // A call that can no longer wait sends and joins nothing, as call() answers before it sends.
144
+ if (waiter.signal?.aborted === true || waiter.end.aborted)
145
+ return { result: aborted(), origin: undefined };
146
+ if (waiter.deadline.passed())
147
+ return { result: timeout(), origin: undefined };
148
+ const kept = entries.get(key);
149
+ if (kept !== undefined)
150
+ kept.waiters.add(waiter);
151
+ const entry = kept ?? begin(key, spec, waiter, start);
152
+ const waited = await waitFor(entry, waiter);
153
+ // A rejection is an exception, never a result, as for a call's own read (#349).
154
+ if ('rejected' in waited)
155
+ throw waited.rejected;
156
+ if ('own' in waited)
157
+ return { result: waited.own, origin: undefined };
158
+ const outcome = waited.read;
159
+ // The read timed out at its starter's deadline: a call with time left reads again (item 1).
160
+ if (!outcome.ok && outcome.error.kind === 'timeout' && open)
161
+ continue;
162
+ if (!outcome.ok)
163
+ return { result: copyOf(outcome.error), origin: entry.origin };
164
+ return { result: readParsed(structuredClone(outcome.data), spec.schema, entry.status ?? 200), origin: entry.origin };
165
+ }
166
+ },
167
+ async close() {
168
+ open = false;
169
+ dropAll();
170
+ const ending = [...live];
171
+ for (const entry of ending)
172
+ entry.end.abort(closed());
173
+ await Promise.allSettled(ending.map((entry) => entry.answer));
174
+ },
175
+ };
176
+ }
177
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../../src/core/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAGvB,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAChD,OAAO,EAAE,cAAc,EAAiC,MAAM,aAAa,CAAA;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAwF3C,0DAA0D;AAC1D,MAAM,OAAO,GAAG,GAAqB,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,CAAC,CAAA;AACnF,MAAM,OAAO,GAAG,GAAqB,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,CAAC,CAAA;AACnF,MAAM,QAAQ,GAAG,GAAiB,EAAE,CAAC,IAAI,YAAY,CAAC,qCAAqC,EAAE,YAAY,CAAC,CAAA;AAC1G,M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K,CAAC,MAAM,CAAC,CAAC,CAAA;QAC/D,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Whether `value` has what the SDK uses of a caller's signal: `aborted`, looked at first, and an
3
+ * `addEventListener` method, to hear its abort. `removeEventListener` isn't needed: a signal with
4
+ * none (a minimal polyfill's) keeps the listeners it was given (`stopListening`).
5
+ */
6
+ export declare function hasSignalShape(value: unknown): boolean;
7
+ /**
8
+ * Stops `listener` hearing `signal`'s abort, where the signal can. One with no removeEventListener
9
+ * (a minimal polyfill's) keeps it: never a throw, which would come only once the work it listened
10
+ * for was done, and lose that work's result (#347's code review, 1 and 2).
11
+ */
12
+ export declare function stopListening(signal: AbortSignal | undefined, listener: () => void): void;
@@ -0,0 +1,25 @@
1
+ /*
2
+ * A caller's signal, as the SDK takes one: by what it uses of it, never by its class, so one from
3
+ * another realm or a polyfill works as a native AbortSignal does (#351, #347). Not exported from the
4
+ * package.
5
+ */
6
+ /**
7
+ * Whether `value` has what the SDK uses of a caller's signal: `aborted`, looked at first, and an
8
+ * `addEventListener` method, to hear its abort. `removeEventListener` isn't needed: a signal with
9
+ * none (a minimal polyfill's) keeps the listeners it was given (`stopListening`).
10
+ */
11
+ export function hasSignalShape(value) {
12
+ if (typeof value !== 'object' || value === null)
13
+ return false;
14
+ return 'aborted' in value && typeof Reflect.get(value, 'addEventListener') === 'function';
15
+ }
16
+ /**
17
+ * Stops `listener` hearing `signal`'s abort, where the signal can. One with no removeEventListener
18
+ * (a minimal polyfill's) keeps it: never a throw, which would come only once the work it listened
19
+ * for was done, and lose that work's result (#347's code review, 1 and 2).
20
+ */
21
+ export function stopListening(signal, listener) {
22
+ if (typeof signal?.removeEventListener === 'function')
23
+ signal.removeEventListener('abort', listener);
24
+ }
25
+ //# sourceMappingURL=signal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signal.js","sourceRoot":"","sources":["../../src/core/signal.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,KAAK,CAAA;IAC7D,OAAO,SAAS,IAAI,KAAK,IAAI,OAAO,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,kBAAkB,CAAC,KAAK,UAAU,CAAA;AAC3F,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,MAA+B,EAAE,QAAoB;IACjF,IAAI,OAAO,MAAM,EAAE,mBAAmB,KAAK,UAAU;QAAE,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;AACtG,CAAC"}
@@ -28,13 +28,15 @@ export type TokenFunction = (ask: TokenAsk) => string | Promise<string>;
28
28
  /**
29
29
  * Who a client's requests say sent them (#192, D527), whichever way the client is made. In Node,
30
30
  * each signed-in request to an API host carries them; the signed-out `health` and the auth server
31
- * never get them. In a browser, none go yet: the hosts' CORS doesn't allow the headers until
32
- * query-planner#48.
31
+ * never get them. In a browser (a page with a `document`), only the deployed query API's origin
32
+ * does: not a custom query host, and not pulse-admin or pulse-wallet yet (#196). Another runtime
33
+ * that isn't Node sends none.
33
34
  */
34
35
  export interface Identification {
35
36
  /**
36
37
  * The app making the calls, required: every signed-in request says which app sent it, as
37
- * `User-Agent: <name>/<version> flipstream-sdk/<version> node/<version>`. Each is 1 to 64 of
38
+ * `User-Agent: <name>/<version> flipstream-sdk/<version> node/<version>` in Node, and from a
39
+ * browser as `Flipstream-Client: <name>/<version> flipstream-sdk/<version>`. Each is 1 to 64 of
38
40
  * RFC 9110's token characters (letters, digits and !#$%&'*+-.^_`|~), or a TypeError at creation.
39
41
  */
40
42
  readonly app: {
@@ -108,6 +110,40 @@ export interface CallOptions {
108
110
  * starts its own trace.
109
111
  */
110
112
  readonly traceparent?: string;
113
+ /**
114
+ * The scope this call shares reads in: one `flipstream.scope()` made (#376). One this client
115
+ * didn't make is a TypeError naming the call, with nothing sent. A closed one shares nothing: the
116
+ * call reads its own, as outside a scope.
117
+ */
118
+ readonly scope?: Scope;
119
+ }
120
+ declare global {
121
+ interface SymbolConstructor {
122
+ /**
123
+ * Declared with the SDK's types, as Node's own types declare it, so a project with only the
124
+ * ES2022 and DOM libs reads `Scope` too (#377 round 2, O2.F1). `await using` itself still needs
125
+ * TypeScript's `ESNext.Disposable` lib, and a runtime that has the symbol.
126
+ */
127
+ readonly asyncDispose: unique symbol;
128
+ }
129
+ }
130
+ /**
131
+ * One piece of work's shared reads (#376, D573): a route's load in the console, a command in the
132
+ * CLI. Made by `flipstream.scope()`, taken by each call as `{ scope }`, and ended by `close()`, or
133
+ * by `await using`, which calls it when the block ends.
134
+ */
135
+ export interface Scope {
136
+ /**
137
+ * Ends the scope: every shared read still on its way is ended, and its waiting calls answer
138
+ * `aborted`. Answers once each has settled. A call that takes the scope after this reads its own,
139
+ * as outside a scope. Closing it again does nothing more.
140
+ */
141
+ close(): Promise<void>;
142
+ /**
143
+ * `close()`, so `await using` closes the scope when its block ends. Only where the runtime has
144
+ * `Symbol.asyncDispose`; `close()` itself works everywhere (#377's code review, 5).
145
+ */
146
+ [Symbol.asyncDispose](): Promise<void>;
111
147
  }
112
148
  /** What `createFlipstream` gives: each call settles into an `ApiResult`, never a thrown error (D412). */
113
149
  export interface Flipstream {
@@ -472,6 +508,54 @@ export interface Flipstream {
472
508
  readonly member_id: string;
473
509
  }, options?: CallOptions): Promise<ApiResult<Member>>;
474
510
  };
511
+ /**
512
+ * A scope for one piece of work, such as a page's load or a command (#376, D573): every call that
513
+ * takes it as `{ scope }` shares your organisation read (`GET /organizations/me`), your person read
514
+ * (userinfo, then `/users/{sub}`) and a workspace's own read (`GET /clients/{id}`), where outside
515
+ * a scope each call reads them for itself, every time.
516
+ * - They're read from the backend at the first call that needs them, and every later call reuses
517
+ * that read, still on its way or answered. Nothing is taken on your word.
518
+ * - A shared read is the starting call's own read, its deadline and retries, under a trace of its
519
+ * own, and every call it answers names that trace. Each call keeps its own `deadlineMs` and
520
+ * signal: they end that call's wait, never the read, and a call still waiting when the read times
521
+ * out at its starter's deadline reads again. A read no call waits on any more is ended.
522
+ * - A definitive failure (`forbidden`, `not_found`, `conflict`, `refused`, `rejected` but a 408 or
523
+ * 425, and `invalid` but a 2xx whose body isn't JSON or a status outside the contract) is every
524
+ * later call's answer too, as its own copy, with no new read. A transient one is the answer of
525
+ * the calls that waited on it, and the next call reads again.
526
+ * - A 401 on a shared read, or an app's transport answering one `cancelled`, drops every shared
527
+ * read, so the next call reads them all again. A token that switches to another person or
528
+ * organisation isn't seen otherwise: close the scope on a sign-out or a switch.
529
+ * - Close it when the work is done (`close()`, or `await using`). It keeps each answer until then:
530
+ * it's one piece of work, never a session. Nothing is kept past it.
531
+ */
532
+ scope(): Scope;
533
+ /**
534
+ * `scope()` for the length of `run`: `run` gets the client with every call in the scope, and the
535
+ * scope is closed when `run` settles, so it can't be left open. `run`'s answer is `withScope`'s,
536
+ * and so is its rejection.
537
+ */
538
+ withScope<T>(run: (flipstream: ScopedFlipstream) => Promise<T>): Promise<T>;
475
539
  }
540
+ /** The client inside `withScope`: every call in its scope, and no scope of its own (#376). */
541
+ export type ScopedFlipstream = Omit<Flipstream, 'scope' | 'withScope'>;
542
+ /** Flipstream's deployed hosts: a client's defaults, and what `createFlipstreamFor`'s `deployed` stands in for. */
543
+ export declare const DEFAULT_HOSTS: {
544
+ readonly query: "https://query.flipstream.io";
545
+ readonly auth: "https://auth.flipstream.io";
546
+ readonly admin: "https://pulse.admin.api.flipstream.io";
547
+ readonly wallet: "https://pulse.wallet.api.flipstream.io";
548
+ };
549
+ /** The hosts a client takes as Flipstream's own deployment, whatever it was configured with. */
550
+ export type DeployedHosts = {
551
+ readonly [Service in keyof typeof DEFAULT_HOSTS]: string;
552
+ };
476
553
  /** The SDK, for one caller (D421): the console with its transport, or anyone with a token function. */
477
554
  export declare function createFlipstream(options: FlipstreamOptions): Flipstream;
555
+ /**
556
+ * `createFlipstream`, with `deployed` as Flipstream's own hosts: what decides whether a configured
557
+ * admin host is Flipstream's, for the console it names. The package doesn't export it: the contract
558
+ * cases (scripts/surface.mjs) use it so a local server can stand in for a deployed host, as
559
+ * `createFileLock` takes a test's timings (#378's code review, 1).
560
+ */
561
+ export declare function createFlipstreamFor(options: FlipstreamOptions, deployed: DeployedHosts): Flipstream;