@flipstream/sdk 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +127 -33
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/node/fileLock.js +26 -7
- package/dist/node/fileLock.js.map +1 -1
- package/dist/node/fileStore.d.ts +19 -0
- package/dist/node/fileStore.js +135 -0
- package/dist/node/fileStore.js.map +1 -0
- package/dist/node/index.d.ts +4 -0
- package/dist/node/index.js +3 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/keychainChild.d.ts +1 -0
- package/dist/node/keychainChild.js +100 -0
- package/dist/node/keychainChild.js.map +1 -0
- package/dist/node/keychainStore.d.ts +30 -0
- package/dist/node/keychainStore.js +150 -0
- package/dist/node/keychainStore.js.map +1 -0
- package/dist/node/session.d.ts +44 -12
- package/dist/node/session.js +166 -24
- package/dist/node/session.js.map +1 -1
- package/dist/node/store.d.ts +44 -0
- package/dist/node/store.js +91 -0
- package/dist/node/store.js.map +1 -0
- package/dist/version.gen.d.ts +2 -0
- package/dist/version.gen.js +4 -0
- package/dist/version.gen.js.map +1 -0
- package/package.json +9 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.6.0](https://github.com/flipstream-io/sdk/compare/typescript-v0.5.0...typescript-v0.6.0) (2026-10-17)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **typescript:** a sign-out clears the store, once, however it ends ([#140](https://github.com/flipstream-io/sdk/issues/140)) ([e594331](https://github.com/flipstream-io/sdk/commit/e594331fc796bebdc125cc58b6ed9eaedb2d9549))
|
|
9
|
+
* **typescript:** createSession({ issuer }) discovers on first need ([#152](https://github.com/flipstream-io/sdk/issues/152)) ([3120bf7](https://github.com/flipstream-io/sdk/commit/3120bf7bc20f8651b85fd91a7806710e4d132850))
|
|
10
|
+
* **typescript:** keychain and file token stores in /node ([#142](https://github.com/flipstream-io/sdk/issues/142)) ([bc81870](https://github.com/flipstream-io/sdk/commit/bc8187045dcf05a30952fba1971ba52eb6db86cf))
|
|
11
|
+
* **typescript:** version, the SDK's own version, readable at run time ([#148](https://github.com/flipstream-io/sdk/issues/148)) ([ac287e0](https://github.com/flipstream-io/sdk/commit/ac287e06d790971fee316335e73e1401790054bb))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
* **typescript:** fileLock restarts its watch after a clock jump ([#145](https://github.com/flipstream-io/sdk/issues/145)) ([4990d88](https://github.com/flipstream-io/sdk/commit/4990d886c81e7faa43473c6d0f90176307ecda0a))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Tests
|
|
20
|
+
|
|
21
|
+
* **typescript:** an opt-in check of a sign-out behind a real fileLock ([#155](https://github.com/flipstream-io/sdk/issues/155)) ([5a1956c](https://github.com/flipstream-io/sdk/commit/5a1956cd0af293d656b60c81025076193ba547da))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### CI
|
|
25
|
+
|
|
26
|
+
* close three blind spots in the root check scripts ([#150](https://github.com/flipstream-io/sdk/issues/150)) ([00e6d41](https://github.com/flipstream-io/sdk/commit/00e6d415da1dfcd6aa1dc434547a3661d0a89ec4))
|
|
27
|
+
|
|
3
28
|
## [0.5.0](https://github.com/flipstream-io/sdk/compare/typescript-v0.4.0...typescript-v0.5.0) (2026-10-17)
|
|
4
29
|
|
|
5
30
|
|
package/README.md
CHANGED
|
@@ -50,8 +50,9 @@ one source.
|
|
|
50
50
|
|
|
51
51
|
For an app that runs on the person's own machine, or in their terminal: a CLI, a script, a local
|
|
52
52
|
tool. `@flipstream/sdk/node` signs them in through their browser, with OAuth 2.0's authorisation
|
|
53
|
-
code and PKCE.
|
|
54
|
-
|
|
53
|
+
code and PKCE. Signing in stores nothing: the tokens are the app's to keep, and the SDK has stores
|
|
54
|
+
ready for them (see "Keeping the person signed in"). A web app signs in on its server instead (see
|
|
55
|
+
"In a browser").
|
|
55
56
|
|
|
56
57
|
In a TypeScript project, add `@types/node` (`npm install --save-dev @types/node`): this entry's
|
|
57
58
|
types use Node's own.
|
|
@@ -176,16 +177,125 @@ if (isHeadless(process.env)) {
|
|
|
176
177
|
### Keeping the person signed in
|
|
177
178
|
|
|
178
179
|
`createSession` keeps an access token fresh from the tokens your store holds, and signs the person
|
|
179
|
-
out. It holds a refresh token, so it belongs in a CLI or on a server, never in browser code.
|
|
180
|
+
out. It holds a refresh token, so it belongs in a CLI or on a server, never in browser code. The
|
|
181
|
+
SDK has two stores ready for it, which keep the store's rules for you.
|
|
180
182
|
|
|
181
183
|
```ts
|
|
182
184
|
import { createFlipstream, discover } from '@flipstream/sdk'
|
|
185
|
+
import { createSession, fileStore, isHeadless, keychainStore, signInWithLoopback } from '@flipstream/sdk/node'
|
|
186
|
+
import { homedir } from 'node:os'
|
|
187
|
+
import { join } from 'node:path'
|
|
188
|
+
|
|
189
|
+
const issuer = 'https://auth.flipstream.io'
|
|
190
|
+
|
|
191
|
+
// The system's keychain on the person's own machine; a file only this user can read on a server,
|
|
192
|
+
// over SSH or in CI.
|
|
193
|
+
const store = isHeadless(process.env)
|
|
194
|
+
? fileStore(join(homedir(), '.config', 'example-cli', 'tokens.json'))
|
|
195
|
+
: keychainStore({ service: 'io.example.cli', account: issuer })
|
|
196
|
+
|
|
197
|
+
// Sign in once, and save the tokens. Only signing in needs the server first.
|
|
198
|
+
if ((await store.tokens()) === null) {
|
|
199
|
+
const server = await discover(issuer)
|
|
200
|
+
if (!server.ok) throw new Error(`sign-in is unavailable: ${server.error.kind}`)
|
|
201
|
+
const signedIn = await signInWithLoopback({
|
|
202
|
+
server: server.data,
|
|
203
|
+
clientId: '<your client id>',
|
|
204
|
+
scopes: ['<scope>'],
|
|
205
|
+
open: (url) => {
|
|
206
|
+
console.log(`Sign in at ${url}`)
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
if (!signedIn.ok) throw new Error(`sign-in failed: ${signedIn.error.kind}`)
|
|
210
|
+
await store.save(signedIn.data)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// The session discovers the server itself, only when a refresh or a sign-out needs it.
|
|
214
|
+
const session = createSession({ issuer, clientId: '<your client id>', ...store })
|
|
215
|
+
const fs = createFlipstream({ token: session.token })
|
|
216
|
+
|
|
217
|
+
// Signing out ends the session at the server, and clears the store through onTokens.
|
|
218
|
+
const signedOut = await session.revoke()
|
|
219
|
+
console.log(signedOut.kind)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
- **`issuer`,** as above, discovers the server itself, only when a refresh or a sign-out needs it,
|
|
223
|
+
and keeps it. A command holding a token with time left then sends nothing to the auth host, and
|
|
224
|
+
an outage there doesn't stop it. Give exactly one of `issuer` and `server`, from your own
|
|
225
|
+
`discover` (as in "Writing your own store" below).
|
|
226
|
+
- **Write the issuer exactly as its metadata names it,** with no slash at the end for
|
|
227
|
+
`https://auth.flipstream.io`. Any other spelling fails every discovery.
|
|
228
|
+
- **A discovery that gets no answer** (offline, a 5xx, a timeout) is `unavailable` from
|
|
229
|
+
`token()`. **One answered outside the contract** (a 404, metadata naming another issuer) is
|
|
230
|
+
`refresh_failed`, with the error: waiting won't mend it. From `revoke()`, either is
|
|
231
|
+
`unconfirmed`. A failed discovery is tried again at the next need.
|
|
232
|
+
- **What it guarantees:** with a store that shows only what it has saved and compares and sets on
|
|
233
|
+
`replaces`, and one lock shared by every process, the session never sends a refresh token another
|
|
234
|
+
refresh is spending, never hands out a token that wasn't saved, never overwrites a newer sign-in,
|
|
235
|
+
and says `revoked` only when the server took the access token just before and refuses it just
|
|
236
|
+
after. A lost answer or a failed save can still leave a spent token stored, and a server fault
|
|
237
|
+
between the two checks reads as `revoked`.
|
|
238
|
+
- **The ready-made stores keep both rules:** each shows only what it has saved, and saves a
|
|
239
|
+
refresh's tokens only while it still holds the ones they replace, so a newer sign-in is kept.
|
|
240
|
+
`save` is for a sign-in's tokens, and `clear` for an app that signs out without a session:
|
|
241
|
+
`session.revoke()` clears the store itself.
|
|
242
|
+
- **`fileStore(path)`** writes the whole file under a temporary name in its folder, flushes it to
|
|
243
|
+
disk, renames it over the old one and flushes the folder: a reader, or a power cut, sees the old
|
|
244
|
+
tokens or the new ones, never half. The file's mode is 0600, and a folder it makes is 0700. It
|
|
245
|
+
refuses a file that is a link, isn't this user's or is open to others, and it won't read or
|
|
246
|
+
save in a folder others can write to. On Windows the folder isn't flushed, and nothing checks
|
|
247
|
+
who owns what.
|
|
248
|
+
- **`keychainStore({ service, account })`** keeps the tokens in one item of the system's
|
|
249
|
+
credential store: the macOS keychain, Windows' Credential Manager, or the Secret Service on
|
|
250
|
+
Linux (GNOME Keyring, KWallet). It needs the optional package `@napi-rs/keyring`
|
|
251
|
+
(`npm install @napi-rs/keyring`); without it, every step fails as `store_failed`, naming the
|
|
252
|
+
package.
|
|
253
|
+
- Each step runs in a process of its own, ended after 30 s (`timeoutMs`), so a keychain
|
|
254
|
+
prompt that nobody answers can't hold up your app. That process is `process.execPath`, so
|
|
255
|
+
it needs a Node that runs a script: not an Electron app (without `ELECTRON_RUN_AS_NODE`), a
|
|
256
|
+
Node single-executable application, or a compiled Bun binary. A bundle must keep
|
|
257
|
+
`keychainChild.js` beside the module, with `zod` and `@napi-rs/keyring` resolvable from
|
|
258
|
+
there, and `import.meta.url` set. Where it doesn't (an esbuild CommonJS bundle, say), only the
|
|
259
|
+
keychain's steps fail, as `store_failed`.
|
|
260
|
+
- Through `session.token()`, a keychain read that takes longer than the caller's 15 s wait is
|
|
261
|
+
`unavailable`. A longer `timeoutMs` helps your own `tokens`, `save` and `clear`, and a
|
|
262
|
+
refresh, which goes on after the caller's wait.
|
|
263
|
+
- Tokens over 2,560 bytes, Windows' limit, kept on every platform, fail the save with nothing
|
|
264
|
+
written. A Flipstream sign-in's tokens take about 1.6 KB.
|
|
265
|
+
- On Linux without a Secret Service, as on most servers, use `fileStore`.
|
|
266
|
+
- On macOS, a script run by the same Node can read the item without a prompt, much as with a
|
|
267
|
+
file only the person can read.
|
|
268
|
+
- It's tested on macOS. Windows and Linux aren't tested yet.
|
|
269
|
+
- **One session per store,** shared by everything that uses it, or pass `lock`, a lock every
|
|
270
|
+
process takes, which settles only once its work has settled. Two sessions on one store refresh as
|
|
271
|
+
two processes would, and the server can end the session.
|
|
272
|
+
- **In one process,** the ready-made stores take each save, update and clear of the same file or
|
|
273
|
+
item in turn, so a sign-in saved while a refresh is saving is kept.
|
|
274
|
+
- **Across processes,** take the session's lock around `save` and `clear` too: a store's compare
|
|
275
|
+
and its write are one step only among processes that take turns.
|
|
276
|
+
- **`session.token()`** rejects with a `SessionError` when there's no token to give: `signed-out`,
|
|
277
|
+
`unavailable` (after 15 s at most), `store_failed` or `refresh_failed`.
|
|
278
|
+
- **`session.revoke()`** gives `revoked`, or `unconfirmed` with the reason. It ends the session the
|
|
279
|
+
store holds when its turn comes, and, whatever the result, clears it through `onTokens`
|
|
280
|
+
(`cleared`), once, in the same hold of the lock, so a refresh can't bring it back. A sign-in saved
|
|
281
|
+
after that is kept. It waits for the clear at most 10 s. After `timeout` or `aborted`, the clear
|
|
282
|
+
may still be on its way: once it reaches the lock, it waits at most 60 s for the lock and for each
|
|
283
|
+
of its three store calls, so up to about four minutes. A lock held longer than its 60 s by another
|
|
284
|
+
process, or a store call that doesn't answer within its 60 s, leaves the store as it was.
|
|
285
|
+
|
|
286
|
+
**Writing your own store:** give `tokens` and `onTokens` instead, and keep the same two rules.
|
|
287
|
+
`tokens()` returns only what `onTokens` has durably saved: write first, show second. `onTokens`
|
|
288
|
+
writes only while the store's key is still `update.replaces`, and throws when it refuses. The key is
|
|
289
|
+
the stored refresh token, or the access token when it has none or an empty one
|
|
290
|
+
(`refreshToken || accessToken`). Here in memory:
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
import { discover } from '@flipstream/sdk'
|
|
183
294
|
import { createSession, type SessionTokens, type TokenUpdate } from '@flipstream/sdk/node'
|
|
184
295
|
|
|
185
296
|
const server = await discover('https://auth.flipstream.io')
|
|
186
297
|
if (!server.ok) throw new Error(`sign-in is unavailable: ${server.error.kind}`)
|
|
187
298
|
|
|
188
|
-
// Your store, here in memory. A real one saves first and shows second (see below).
|
|
189
299
|
let stored: SessionTokens | null = null
|
|
190
300
|
|
|
191
301
|
const session = createSession({
|
|
@@ -204,37 +314,8 @@ const session = createSession({
|
|
|
204
314
|
stored = { accessToken, ...(refreshToken === undefined ? {} : { refreshToken }), ...(expiresAt === undefined ? {} : { expiresAt }) }
|
|
205
315
|
},
|
|
206
316
|
})
|
|
207
|
-
|
|
208
|
-
const fs = createFlipstream({ token: session.token })
|
|
209
|
-
|
|
210
|
-
// Signing out ends the session at the server. Then clear your own store, whatever the result.
|
|
211
|
-
const signedOut = await session.revoke()
|
|
212
|
-
stored = null
|
|
213
|
-
console.log(signedOut.kind)
|
|
214
317
|
```
|
|
215
318
|
|
|
216
|
-
- **What it guarantees:** with a store that shows only what it has saved and compares and sets on
|
|
217
|
-
`replaces`, and one lock shared by every process, the session never sends a refresh token another
|
|
218
|
-
refresh is spending, never hands out a token that wasn't saved, never overwrites a newer sign-in,
|
|
219
|
-
and says `revoked` only when the server took the access token just before and refuses it just
|
|
220
|
-
after. A lost answer or a failed save can still leave a spent token stored, and a server fault
|
|
221
|
-
between the two checks reads as `revoked`.
|
|
222
|
-
- **Your store saves first and shows second.** `tokens()` returns only what `onTokens` has durably
|
|
223
|
-
saved: the system keychain, or a file written under a temporary name, flushed to disk (fsync),
|
|
224
|
-
renamed over the old one, and its folder flushed too. `onTokens` writes only while the store's key
|
|
225
|
-
is still `update.replaces`, and throws when it refuses. The key is the stored refresh token, or the
|
|
226
|
-
access token when it has none or an empty one (`refreshToken || accessToken`).
|
|
227
|
-
- **One session per store,** shared by everything that uses it, or pass `lock`, a lock every
|
|
228
|
-
process takes, which settles only once its work has settled. Two sessions on one store refresh as
|
|
229
|
-
two processes would, and the server can end the session.
|
|
230
|
-
- **`session.token()`** rejects with a `SessionError` when there's no token to give: `signed-out`,
|
|
231
|
-
`unavailable` (after 15 s at most), `store_failed` or `refresh_failed`.
|
|
232
|
-
- **`session.revoke()`** gives `revoked`, or `unconfirmed` with the reason. It doesn't touch your
|
|
233
|
-
store: clear it yourself afterwards, whatever the result.
|
|
234
|
-
- **A known risk:** on a store that doesn't compare and set, a refresh whose save is slow can
|
|
235
|
-
land after your clear and bring a live session back. A later release moves the clear into the
|
|
236
|
-
session, inside its queue.
|
|
237
|
-
|
|
238
319
|
### Several processes on one store
|
|
239
320
|
|
|
240
321
|
`fileLock(path)` is a ready-made `lock` for Node processes that share one store, such as a CLI
|
|
@@ -478,6 +559,17 @@ await fs.catalog.list({ signal: controller.signal, deadlineMs: 10_000 })
|
|
|
478
559
|
it never goes through your transport: it goes to the query host, over https, with the global
|
|
479
560
|
`fetch`.
|
|
480
561
|
|
|
562
|
+
## Which version is running
|
|
563
|
+
|
|
564
|
+
```ts
|
|
565
|
+
import { version } from '@flipstream/sdk'
|
|
566
|
+
|
|
567
|
+
console.log(`@flipstream/sdk ${version}`) // "@flipstream/sdk 0.5.0", say
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
`version` is the installed package's own, from its `package.json`. It's a string in the code, so it
|
|
571
|
+
reads the same in Node and in a browser bundle, with no file to load.
|
|
572
|
+
|
|
481
573
|
## Requirements
|
|
482
574
|
|
|
483
575
|
- **Node 20.19+ or 22.12+,** or a current browser. Sign-in needs WebCrypto, so a page that signs
|
|
@@ -486,6 +578,8 @@ await fs.catalog.list({ signal: controller.signal, deadlineMs: 10_000 })
|
|
|
486
578
|
CommonJS too, both entries; Node 22.12 prints an experimental warning when it does.
|
|
487
579
|
- **The types ship in the package,** for both entries. The Node entry's types use Node's own,
|
|
488
580
|
from `@types/node`.
|
|
581
|
+
- **`keychainStore` needs `@napi-rs/keyring` 2.1 or later,** an optional peer dependency. Nothing
|
|
582
|
+
else needs it, and npm doesn't install it for you.
|
|
489
583
|
|
|
490
584
|
## Licence
|
|
491
585
|
|
package/dist/index.d.ts
CHANGED
|
@@ -10,3 +10,4 @@ export type { TokenAsk } from './core/fetch.js';
|
|
|
10
10
|
export type { Catalog, CatalogSource, CustomDimensions, Dimension, ErrorBody, Format, Metric, PlannerErrorCode, QueryResponse, Row, SortSpec, Source, } from './spec/query-planner/types.gen.js';
|
|
11
11
|
export * from './auth/index.js';
|
|
12
12
|
export * from './api/index.js';
|
|
13
|
+
export { version } from './version.gen.js';
|
package/dist/index.js
CHANGED
|
@@ -5,4 +5,6 @@ export { isPlannerCode } from './query/errors.js';
|
|
|
5
5
|
export { ApiFailure, orThrow } from './core/result.js';
|
|
6
6
|
export * from './auth/index.js';
|
|
7
7
|
export * from './api/index.js';
|
|
8
|
+
// The SDK's own version, written from package.json at build time (tools/write-version.mjs, #132).
|
|
9
|
+
export { version } from './version.gen.js';
|
|
8
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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;AAIlD,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"}
|
|
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;AAIlD,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;AAC9B,kGAAkG;AAClG,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA"}
|
package/dist/node/fileLock.js
CHANGED
|
@@ -17,11 +17,12 @@ import { setTimeout as pause } from 'node:timers/promises';
|
|
|
17
17
|
* the other's processes as gone.
|
|
18
18
|
* - A file is stale once a waiter has seen it unchanged over 10 s of its own tries, or over 3 s
|
|
19
19
|
* when its last touch is also 10 s old by this machine's clock. The second lets a sign-out, whose
|
|
20
|
-
* whole budget is 10 s (D445), take over a crashed holder's leftover (rev143 P1-1).
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* this one, in a virtual machine sharing the folder say, has only
|
|
20
|
+
* whole budget is 10 s (D445), take over a crashed holder's leftover (rev143 P1-1).
|
|
21
|
+
* - Neither a clock that jumps forward nor a laptop waking from sleep makes a live lock stale on
|
|
22
|
+
* its own: a jump starts the waiter's watch again (`lookout`), so the holder, whose file may only
|
|
23
|
+
* now look old, has 3 s from then to touch it (#144, R2-1). A holder on a clock 10 s or more
|
|
24
|
+
* behind this one, in a virtual machine sharing the folder say, has only 2 to 3 s without a
|
|
25
|
+
* touch, all the time.
|
|
25
26
|
*
|
|
26
27
|
* Waiters that judge a lock stale together take turns through a second file, so none removes a
|
|
27
28
|
* lock another has just taken (honest-typescript §5): of those, only the first finds it unchanged.
|
|
@@ -34,6 +35,10 @@ const TOUCH_MS = 1_000;
|
|
|
34
35
|
const STALE_MS = 10_000;
|
|
35
36
|
/** How long a waiter watches a file whose last touch is already STALE_MS old by the clock. */
|
|
36
37
|
const OLD_WATCH_MS = 3_000;
|
|
38
|
+
/** The clock moving this much further than time did between two tries is a jump. */
|
|
39
|
+
const JUMP_MS = 1_000;
|
|
40
|
+
/** Two tries this far apart by the clock alone are a jump too: a wake, whichever clock counts sleep. */
|
|
41
|
+
const WAKE_MS = 5_000;
|
|
37
42
|
/**
|
|
38
43
|
* A lock across processes, for `createSession`'s `lock` option: for a token store that several
|
|
39
44
|
* Node processes share, such as a CLI run several times at once. Give every process the same
|
|
@@ -195,14 +200,28 @@ async function isStill(file, handle) {
|
|
|
195
200
|
* What a waiter sees of a file at each try: its holder and when it was last touched. It comes back
|
|
196
201
|
* as stale once it has been seen unchanged over STALE_MS of tries, or over OLD_WATCH_MS when its
|
|
197
202
|
* last touch is also STALE_MS old by the clock. Time seen is counted in tries, so a slow waiter
|
|
198
|
-
* only takes longer. Any change,
|
|
203
|
+
* only takes longer. Any change, a try that found nothing, or a jump starts the count again.
|
|
204
|
+
*
|
|
205
|
+
* A jump is either of two things since the last try:
|
|
206
|
+
* - the clock moving JUMP_MS further than `performance.now()` did: the clock stepped forward. Load,
|
|
207
|
+
* or a thread blocked between tries, moves both alike, so it never counts;
|
|
208
|
+
* - the clock moving over WAKE_MS at all: the machine slept. macOS's monotonic clock counts sleep
|
|
209
|
+
* (libuv's `mach_continuous_time`), and Windows' may, so the first can't see a wake there. Only a
|
|
210
|
+
* step or a sleep of 9 s or more can make a live holder's file, touched every second, look 10 s
|
|
211
|
+
* old, and those all show as gaps of over 5 s. A waiter that stalls over 5 s at every try
|
|
212
|
+
* restarts each time.
|
|
199
213
|
*/
|
|
200
214
|
function lookout(file) {
|
|
201
215
|
let seen = null;
|
|
202
216
|
let tries = 0;
|
|
217
|
+
let triedAt = { clock: Date.now(), time: performance.now() };
|
|
203
218
|
return async () => {
|
|
204
219
|
const now = await look(file);
|
|
205
|
-
|
|
220
|
+
const at = { clock: Date.now(), time: performance.now() };
|
|
221
|
+
const moved = at.clock - triedAt.clock;
|
|
222
|
+
const jumped = moved - (at.time - triedAt.time) > JUMP_MS || moved > WAKE_MS;
|
|
223
|
+
triedAt = at;
|
|
224
|
+
tries = now !== null && now.key === seen && !jumped ? tries + 1 : 0;
|
|
206
225
|
seen = now?.key ?? null;
|
|
207
226
|
if (now === null)
|
|
208
227
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fileLock.js","sourceRoot":"","sources":["../../src/node/fileLock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAmB,MAAM,kBAAkB,CAAA;AACvF,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAA;AAG1D
|
|
1
|
+
{"version":3,"file":"fileLock.js","sourceRoot":"","sources":["../../src/node/fileLock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAmB,MAAM,kBAAkB,CAAA;AACvF,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAA;AAG1D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,sCAAsC;AACtC,MAAM,QAAQ,GAAG,GAAG,CAAA;AACpB,gDAAgD;AAChD,MAAM,QAAQ,GAAG,KAAK,CAAA;AACtB,qDAAqD;AACrD,MAAM,QAAQ,GAAG,MAAM,CAAA;AACvB,8FAA8F;AAC9F,MAAM,YAAY,GAAG,KAAK,CAAA;AAC1B,oFAAoF;AACpF,MAAM,OAAO,GAAG,KAAK,CAAA;AACrB,wGAAwG;AACxG,MAAM,OAAO,GAAG,KAAK,CAAA;AA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7F,CAAC;AAED,SAAS,OAAO,CAAC,MAAmB;IAClC,MAAM,MAAM,GAAY,MAAM,CAAC,MAAM,CAAA;IACrC,MAAM,MAAM,CAAA;AACd,CAAC;AAED,6FAA6F;AAC7F,MAAM,aAAa,GAAG,CAAC,KAAc,EAAW,EAAE,CAAC,OAAO,CAAC,QAAQ,KAAK,OAAO,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,OAAO,CAAA;AAE5G,MAAM,MAAM,GAAG,CAAC,KAAc,EAAW,EAAE,CAAC,CAAC,KAAK,YAAY,KAAK,IAAI,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;AAEhH,MAAM,IAAI,GAAG,GAAc,EAAE,CAAC,SAAS,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type TokenStore } from './store.js';
|
|
2
|
+
/**
|
|
3
|
+
* The tokens in one file, readable by its owner only (mode 0600), for a CLI or a backend on any
|
|
4
|
+
* platform Node runs on. A save writes the whole file under a temporary name in the same folder,
|
|
5
|
+
* flushes it to disk (fsync), renames it over the old one, and flushes the folder: a reader, in this
|
|
6
|
+
* process or another, sees the old tokens or the new ones, never half of either, and a power cut
|
|
7
|
+
* can't bring back the old file with its spent refresh token.
|
|
8
|
+
*
|
|
9
|
+
* - **A missing file** is nobody signed in. A file that holds anything else, or that this user
|
|
10
|
+
* doesn't own alone, fails the read: a symbolic link, a pipe, another user's file, or one that
|
|
11
|
+
* others may read or write.
|
|
12
|
+
* - **The folder** is made (mode 0700) when it doesn't exist. A read or a save in a folder that
|
|
13
|
+
* another user owns or that others may write to fails: they could swap the file. A clear still
|
|
14
|
+
* deletes there, since a deleted file is what a sign-out wants. Only the mode bits are read, so
|
|
15
|
+
* an access list that lets others write (macOS `chmod +a`) goes unseen.
|
|
16
|
+
* - **On Windows** the folder isn't flushed, since it can't be opened to be, the file's mode is what
|
|
17
|
+
* Windows makes of 0600, and nothing checks who owns what.
|
|
18
|
+
*/
|
|
19
|
+
export declare function fileStore(path: string): TokenStore;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { mkdir, open, readdir, rename, stat, unlink } from 'node:fs/promises';
|
|
4
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { parse, storeOf } from './store.js';
|
|
6
|
+
/**
|
|
7
|
+
* The tokens in one file, readable by its owner only (mode 0600), for a CLI or a backend on any
|
|
8
|
+
* platform Node runs on. A save writes the whole file under a temporary name in the same folder,
|
|
9
|
+
* flushes it to disk (fsync), renames it over the old one, and flushes the folder: a reader, in this
|
|
10
|
+
* process or another, sees the old tokens or the new ones, never half of either, and a power cut
|
|
11
|
+
* can't bring back the old file with its spent refresh token.
|
|
12
|
+
*
|
|
13
|
+
* - **A missing file** is nobody signed in. A file that holds anything else, or that this user
|
|
14
|
+
* doesn't own alone, fails the read: a symbolic link, a pipe, another user's file, or one that
|
|
15
|
+
* others may read or write.
|
|
16
|
+
* - **The folder** is made (mode 0700) when it doesn't exist. A read or a save in a folder that
|
|
17
|
+
* another user owns or that others may write to fails: they could swap the file. A clear still
|
|
18
|
+
* deletes there, since a deleted file is what a sign-out wants. Only the mode bits are read, so
|
|
19
|
+
* an access list that lets others write (macOS `chmod +a`) goes unseen.
|
|
20
|
+
* - **On Windows** the folder isn't flushed, since it can't be opened to be, the file's mode is what
|
|
21
|
+
* Windows makes of 0600, and nothing checks who owns what.
|
|
22
|
+
*/
|
|
23
|
+
export function fileStore(path) {
|
|
24
|
+
const file = resolve(path);
|
|
25
|
+
return storeOf(`file:${file}`, () => read(file), (tokens) => (tokens === null ? remove(file) : replace(file, tokens)));
|
|
26
|
+
}
|
|
27
|
+
const POSIX = process.platform !== 'win32';
|
|
28
|
+
// Not through a link, and not waiting on a pipe that nobody writes to (O_NONBLOCK): what is opened
|
|
29
|
+
// is checked before a byte is read.
|
|
30
|
+
const READ = constants.O_RDONLY | (POSIX ? constants.O_NOFOLLOW | constants.O_NONBLOCK : 0);
|
|
31
|
+
async function read(file) {
|
|
32
|
+
try {
|
|
33
|
+
await mustBeMine(dirname(file));
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (codeOf(error) === 'ENOENT')
|
|
37
|
+
return null;
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
let handle;
|
|
41
|
+
try {
|
|
42
|
+
handle = await open(file, READ);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (codeOf(error) === 'ENOENT')
|
|
46
|
+
return null;
|
|
47
|
+
if (codeOf(error) === 'ELOOP')
|
|
48
|
+
throw new Error(`the token file is a symbolic link, which the store doesn't follow: ${file}`, { cause: error });
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const found = await handle.stat();
|
|
53
|
+
if (!found.isFile())
|
|
54
|
+
throw new Error(`the token file isn't a regular file: ${file}`);
|
|
55
|
+
if (POSIX && (!ownedByMe(found) || (found.mode & 0o077) !== 0))
|
|
56
|
+
throw new Error(`the token file must be this user's, readable by them only (mode 0600): ${file}`);
|
|
57
|
+
return parse(await handle.readFile('utf8'));
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
await handle.close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function replace(file, tokens) {
|
|
64
|
+
const folder = dirname(file);
|
|
65
|
+
await mkdir(folder, { recursive: true, mode: 0o700 });
|
|
66
|
+
await mustBeMine(folder);
|
|
67
|
+
// Created, never opened over an existing file ('wx'), so a name planted beforehand fails the save.
|
|
68
|
+
const temporary = join(folder, `.${basename(file)}.${randomUUID()}.tmp`);
|
|
69
|
+
const handle = await open(temporary, 'wx', 0o600);
|
|
70
|
+
try {
|
|
71
|
+
try {
|
|
72
|
+
await handle.writeFile(JSON.stringify(tokens), 'utf8');
|
|
73
|
+
await handle.sync();
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
await handle.close();
|
|
77
|
+
}
|
|
78
|
+
await rename(temporary, file);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
// What this save created goes with it; a rename that failed left the old file as it was.
|
|
82
|
+
await unlink(temporary).catch(() => undefined);
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
await flush(folder);
|
|
86
|
+
}
|
|
87
|
+
/** The file, and any temporary file a save left behind when its process died: it may hold a live refresh token. */
|
|
88
|
+
async function remove(file) {
|
|
89
|
+
const folder = dirname(file);
|
|
90
|
+
let entries;
|
|
91
|
+
try {
|
|
92
|
+
entries = await readdir(folder);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
if (codeOf(error) === 'ENOENT')
|
|
96
|
+
return;
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
const prefix = `.${basename(file)}.`;
|
|
100
|
+
const left = (entry) => entry.startsWith(prefix) && entry.endsWith('.tmp') && UUID.test(entry.slice(prefix.length, -'.tmp'.length));
|
|
101
|
+
for (const name of [basename(file), ...entries.filter(left)]) {
|
|
102
|
+
try {
|
|
103
|
+
await unlink(join(folder, name));
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (codeOf(error) !== 'ENOENT')
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
await flush(folder);
|
|
111
|
+
}
|
|
112
|
+
/** The folder's entries to disk: without this, a power cut can undo a rename or a delete that had already shown. */
|
|
113
|
+
async function flush(folder) {
|
|
114
|
+
if (!POSIX)
|
|
115
|
+
return;
|
|
116
|
+
const handle = await open(folder, 'r');
|
|
117
|
+
try {
|
|
118
|
+
await handle.sync();
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
await handle.close();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** A folder this user owns, which others can't write to: no one else can swap, plant or delete the file in it. */
|
|
125
|
+
async function mustBeMine(folder) {
|
|
126
|
+
if (!POSIX)
|
|
127
|
+
return;
|
|
128
|
+
const found = await stat(folder);
|
|
129
|
+
if (!ownedByMe(found) || (found.mode & 0o022) !== 0)
|
|
130
|
+
throw new Error(`the token file's folder must be this user's, and writable by them only: ${folder}`);
|
|
131
|
+
}
|
|
132
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
133
|
+
const ownedByMe = (found) => found.uid === process.getuid?.();
|
|
134
|
+
const codeOf = (error) => (error instanceof Error && 'code' in error ? error.code : undefined);
|
|
135
|
+
//# sourceMappingURL=fileStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fileStore.js","sourceRoot":"","sources":["../../src/node/fileStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,SAAS,EAAc,MAAM,SAAS,CAAA;AAC/C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAmB,MAAM,kBAAkB,CAAA;AAC9F,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAC5D,OAAO,EAAE,KAAK,EAAE,OAAO,EAAmB,MAAM,YAAY,CAAA;AAG5D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,SAAS,CAAC,IAAY;IACpC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1B,OAAO,OAAO,CACZ,QAAQ,IAAI,EAAE,EACd,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAChB,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,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IAClB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACtC,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;IACrB,CAAC;YAAS,CAAC;QACT,MAAM,MAAM,CAAC,KAAK,EAAE,CAAA;IACtB,CAAC;AACH,CAAC;AAED,kHAAkH;AAClH,KAAK,UAAU,UAAU,CAAC,MAAc;IACtC,IAAI,CAAC,KAAK;QAAE,OAAM;IAClB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,CAAA;IAChC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,2EAA2E,MAAM,EAAE,CAAC,CAAA;AAC3J,CAAC;AAED,MAAM,IAAI,GAAG,gEAAgE,CAAA;AAE7E,MAAM,SAAS,GAAG,CAAC,KAAY,EAAW,EAAE,CAAC,KAAK,CAAC,GAAG,KAAK,OAAO,CAAC,MAAM,EAAE,EAAE,CAAA;AAE7E,MAAM,MAAM,GAAG,CAAC,KAAc,EAAW,EAAE,CAAC,CAAC,KAAK,YAAY,KAAK,IAAI,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA"}
|
package/dist/node/index.d.ts
CHANGED
|
@@ -7,3 +7,7 @@ export { fileLock } from './fileLock.js';
|
|
|
7
7
|
export type { RevokeResult, Session, SessionFailure, SessionOptions, SessionTokens, TokenUpdate } from './session.js';
|
|
8
8
|
export { finishSignIn } from './clientSecret.js';
|
|
9
9
|
export type { ServerSignInOptions } from './clientSecret.js';
|
|
10
|
+
export { fileStore } from './fileStore.js';
|
|
11
|
+
export { keychainStore } from './keychainStore.js';
|
|
12
|
+
export type { KeychainStoreOptions } from './keychainStore.js';
|
|
13
|
+
export type { TokenStore } from './store.js';
|
package/dist/node/index.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// The `@flipstream/sdk/node` entry: sign-in for Node apps, which the main entry never pulls in,
|
|
2
2
|
// so a browser bundle has no node:http (#65), and the session, which holds a refresh token and so
|
|
3
3
|
// belongs on a backend or in a CLI, never in browser code (D452), with a lock for a store several
|
|
4
|
-
// processes share (#114).
|
|
4
|
+
// processes share (#114), and ready-made stores for it (#137).
|
|
5
5
|
export { signInWithLoopback } from './loopback.js';
|
|
6
6
|
export { isHeadless, parsePastedRedirect } from './headless.js';
|
|
7
7
|
export { createSession, SessionError } from './session.js';
|
|
8
8
|
export { fileLock } from './fileLock.js';
|
|
9
9
|
// An app with a server signs in as a confidential client, with its client secret (#123).
|
|
10
10
|
export { finishSignIn } from './clientSecret.js';
|
|
11
|
+
export { fileStore } from './fileStore.js';
|
|
12
|
+
export { keychainStore } from './keychainStore.js';
|
|
11
13
|
//# sourceMappingURL=index.js.map
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,kGAAkG;AAClG,kGAAkG;AAClG
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,kGAAkG;AAClG,kGAAkG;AAClG,+DAA+D;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAElD,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AAE/D,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,yFAAyF;AACzF,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|