@flipstream/sdk 0.4.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 +42 -0
- package/README.md +325 -20
- package/dist/auth/discovery.d.ts +2 -0
- package/dist/auth/discovery.js +4 -1
- package/dist/auth/discovery.js.map +1 -1
- package/dist/auth/signIn.d.ts +11 -2
- package/dist/auth/signIn.js +19 -27
- package/dist/auth/signIn.js.map +1 -1
- package/dist/auth/tokenEndpoint.d.ts +18 -0
- package/dist/auth/tokenEndpoint.js +36 -0
- package/dist/auth/tokenEndpoint.js.map +1 -0
- package/dist/core/fetch.d.ts +8 -0
- package/dist/core/fetch.js +36 -11
- package/dist/core/fetch.js.map +1 -1
- package/dist/core/result.d.ts +4 -2
- package/dist/core/result.js +3 -1
- package/dist/core/result.js.map +1 -1
- package/dist/flipstream.d.ts +7 -0
- package/dist/flipstream.js +14 -4
- package/dist/flipstream.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/node/clientSecret.d.ts +41 -0
- package/dist/node/clientSecret.js +124 -0
- package/dist/node/clientSecret.js.map +1 -0
- package/dist/node/fileLock.d.ts +27 -0
- package/dist/node/fileLock.js +267 -0
- package/dist/node/fileLock.js.map +1 -0
- 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/headless.d.ts +7 -2
- package/dist/node/headless.js +15 -2
- package/dist/node/headless.js.map +1 -1
- package/dist/node/index.d.ts +9 -0
- package/dist/node/index.js +9 -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/loopback.d.ts +24 -5
- package/dist/node/loopback.js +80 -23
- package/dist/node/loopback.js.map +1 -1
- package/dist/node/session.d.ts +224 -0
- package/dist/node/session.js +622 -0
- package/dist/node/session.js.map +1 -0
- 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/query/health.d.ts +16 -0
- package/dist/query/health.js +18 -0
- package/dist/query/health.js.map +1 -0
- package/dist/query/query.d.ts +8 -0
- package/dist/query/query.js +12 -0
- package/dist/query/query.js.map +1 -1
- 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 +18 -3
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdir, open, readFile, stat, unlink } from 'node:fs/promises';
|
|
3
|
+
import { dirname, isAbsolute } from 'node:path';
|
|
4
|
+
import { clearInterval, setInterval } from 'node:timers';
|
|
5
|
+
import { setTimeout as pause } from 'node:timers/promises';
|
|
6
|
+
/*
|
|
7
|
+
* The session's `lock` (D444), ready-made for Node processes that share one token store, such as
|
|
8
|
+
* the CLI run by several agents at once (#114): a lock file, created exclusively, held while `work`
|
|
9
|
+
* runs, and removed afterwards.
|
|
10
|
+
*
|
|
11
|
+
* A holder that died must not hold every other process off for ever, and a slow one must never be
|
|
12
|
+
* taken for dead: either way, two refreshes would spend one refresh token. So a holder touches its
|
|
13
|
+
* file every second, and a file nobody touches any more is taken over.
|
|
14
|
+
* - A timeout alone can't tell them apart: a hold lasts as long as its save, which the session
|
|
15
|
+
* waits out and never cuts short.
|
|
16
|
+
* - Nor can the holder's process ID: IDs are reused, and two containers sharing a folder each see
|
|
17
|
+
* the other's processes as gone.
|
|
18
|
+
* - A file is stale once a waiter has seen it unchanged over 10 s of its own tries, or over 3 s
|
|
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
|
+
* - 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.
|
|
26
|
+
*
|
|
27
|
+
* Waiters that judge a lock stale together take turns through a second file, so none removes a
|
|
28
|
+
* lock another has just taken (honest-typescript §5): of those, only the first finds it unchanged.
|
|
29
|
+
*/
|
|
30
|
+
/** How often a waiter tries again. */
|
|
31
|
+
const RETRY_MS = 100;
|
|
32
|
+
/** How often a holder touches its lock file. */
|
|
33
|
+
const TOUCH_MS = 1_000;
|
|
34
|
+
/** A file untouched this long has no live holder. */
|
|
35
|
+
const STALE_MS = 10_000;
|
|
36
|
+
/** How long a waiter watches a file whose last touch is already STALE_MS old by the clock. */
|
|
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;
|
|
42
|
+
/**
|
|
43
|
+
* A lock across processes, for `createSession`'s `lock` option: for a token store that several
|
|
44
|
+
* Node processes share, such as a CLI run several times at once. Give every process the same
|
|
45
|
+
* absolute path, on a local disk, beside the store.
|
|
46
|
+
*
|
|
47
|
+
* - **It is taken by creating the file exclusively,** and removed once `work` has settled, whether
|
|
48
|
+
* it succeeded or failed. Only then does the lock settle, with `work`'s outcome.
|
|
49
|
+
* - **While another process holds it,** it tries again every 100 ms. When `signal` fires it gives
|
|
50
|
+
* up, rejecting with `signal.reason`, and `work` never runs after that. With a signal that has
|
|
51
|
+
* already fired, it creates nothing.
|
|
52
|
+
* - **A process that dies holding it** leaves a file nobody touches any more: a holder touches it
|
|
53
|
+
* every second. The next process takes it over once the file is 10 s old by this machine's clock
|
|
54
|
+
* and it has seen it unchanged for 3 s, or, whatever the clock says, once it has seen it unchanged
|
|
55
|
+
* for 10 s.
|
|
56
|
+
* - **A holder that can't touch its file for 10 s loses it,** and its `work` goes on beside the next
|
|
57
|
+
* holder's: a thread blocked by a synchronous store, or a process stopped by Ctrl-Z or a
|
|
58
|
+
* debugger. Keep the store asynchronous.
|
|
59
|
+
* - **It isn't reentrant:** `work` that takes the same lock again waits for itself until its
|
|
60
|
+
* signal.
|
|
61
|
+
* - **When the file can't be removed,** `work`'s outcome still stands, and the next process takes
|
|
62
|
+
* the file over as stale.
|
|
63
|
+
*
|
|
64
|
+
* @param path - The lock file. Its folder is made when it isn't there (on POSIX, for this user
|
|
65
|
+
* only, as the file is).
|
|
66
|
+
*/
|
|
67
|
+
export function fileLock(path) {
|
|
68
|
+
// A relative path names another file in each working directory, and locks nothing.
|
|
69
|
+
if (!isAbsolute(path))
|
|
70
|
+
throw new TypeError(`fileLock needs an absolute path, the same in every process: ${path}`);
|
|
71
|
+
return async (work, signal) => {
|
|
72
|
+
const held = await take(path, signal);
|
|
73
|
+
try {
|
|
74
|
+
return await work();
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
await held.release();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
async function take(path, signal) {
|
|
82
|
+
const id = randomUUID();
|
|
83
|
+
const lock = lookout(path);
|
|
84
|
+
const turns = lookout(turnsOf(path));
|
|
85
|
+
for (;;) {
|
|
86
|
+
if (signal.aborted)
|
|
87
|
+
return aborted(signal);
|
|
88
|
+
const handle = await createWith(path, id);
|
|
89
|
+
if (handle !== null) {
|
|
90
|
+
const held = hold(path, id, handle);
|
|
91
|
+
// Taken in the same moment the signal fired: let go at once, unused.
|
|
92
|
+
if (!signal.aborted)
|
|
93
|
+
return held;
|
|
94
|
+
await held.release();
|
|
95
|
+
return aborted(signal);
|
|
96
|
+
}
|
|
97
|
+
const stale = await lock();
|
|
98
|
+
if (stale !== null)
|
|
99
|
+
await takeOver(path, stale, id, turns);
|
|
100
|
+
// An abort ends the pause early; the loop's first line then gives up.
|
|
101
|
+
await pause(RETRY_MS, undefined, { signal }).catch(noop);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function hold(path, id, handle) {
|
|
105
|
+
let touching = false;
|
|
106
|
+
const touched = () => {
|
|
107
|
+
touching = false;
|
|
108
|
+
};
|
|
109
|
+
const touch = setInterval(() => {
|
|
110
|
+
// One at a time: on a hung disk, touches would pile up behind each other. A touch that fails
|
|
111
|
+
// is tried again a second later; ten seconds of them and the lock looks stale.
|
|
112
|
+
if (touching)
|
|
113
|
+
return;
|
|
114
|
+
touching = true;
|
|
115
|
+
const now = new Date();
|
|
116
|
+
handle.utimes(now, now).then(touched, touched);
|
|
117
|
+
}, TOUCH_MS);
|
|
118
|
+
// A process with nothing left to do but hold the lock exits, and its lock goes stale, rather
|
|
119
|
+
// than holding every other process off for ever.
|
|
120
|
+
touch.unref();
|
|
121
|
+
return {
|
|
122
|
+
release: async () => {
|
|
123
|
+
// Removed while it is still being touched, so no waiter can take it over as stale meanwhile.
|
|
124
|
+
await removeOwn(path, id);
|
|
125
|
+
clearInterval(touch);
|
|
126
|
+
await handle.close().catch(noop);
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Removes a stale lock file, unless it changed since it was judged stale, while holding the turns
|
|
132
|
+
* file. A turns file left by a waiter that died is removed in its turn, once it too is stale.
|
|
133
|
+
*/
|
|
134
|
+
async function takeOver(path, stale, id, turns) {
|
|
135
|
+
const file = turnsOf(path);
|
|
136
|
+
const handle = await createWith(file, id);
|
|
137
|
+
if (handle === null) {
|
|
138
|
+
const dead = await turns();
|
|
139
|
+
if (dead !== null)
|
|
140
|
+
await removeIf(file, dead);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
await removeIf(path, stale);
|
|
145
|
+
}
|
|
146
|
+
finally {
|
|
147
|
+
await removeOwn(file, id);
|
|
148
|
+
await handle.close().catch(noop);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const turnsOf = (path) => `${path}.stale`;
|
|
152
|
+
/**
|
|
153
|
+
* The file, created exclusively for this user with `id` in it; null while another holds it.
|
|
154
|
+
* Windows removes an open file's name only once its last handle closes, so every removal here
|
|
155
|
+
* comes before its handle's close, and a name still pending removal reads as held (`createNew`).
|
|
156
|
+
*/
|
|
157
|
+
async function createWith(file, id) {
|
|
158
|
+
const handle = await createNew(file).catch(async (error) => {
|
|
159
|
+
if (codeOf(error) !== 'ENOENT')
|
|
160
|
+
throw error;
|
|
161
|
+
// The folder isn't there yet: a keychain store has none of its own.
|
|
162
|
+
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
|
|
163
|
+
return createNew(file);
|
|
164
|
+
});
|
|
165
|
+
if (handle === null)
|
|
166
|
+
return null;
|
|
167
|
+
try {
|
|
168
|
+
await handle.writeFile(id);
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
// Nothing this attempt made is left behind (honest-typescript §7).
|
|
172
|
+
if (await isStill(file, handle).catch(() => false))
|
|
173
|
+
await unlink(file).catch(noop);
|
|
174
|
+
await handle.close().catch(noop);
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
// A write that hung for 10 s can let a waiter take the file over while it is still empty (rev143
|
|
178
|
+
// P3-2): the name is then another's, and this handle holds nothing.
|
|
179
|
+
if (await isStill(file, handle))
|
|
180
|
+
return handle;
|
|
181
|
+
await handle.close().catch(noop);
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
async function createNew(file) {
|
|
185
|
+
try {
|
|
186
|
+
return await open(file, 'wx', 0o600);
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
if (codeOf(error) === 'EEXIST' || deletePending(error))
|
|
190
|
+
return null;
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/** Whether `file` names the file `handle` has open. */
|
|
195
|
+
async function isStill(file, handle) {
|
|
196
|
+
const [mine, now] = await Promise.all([handle.stat({ bigint: true }), stat(file, { bigint: true }).catch(() => null)]);
|
|
197
|
+
return now !== null && now.dev === mine.dev && now.ino === mine.ino;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* What a waiter sees of a file at each try: its holder and when it was last touched. It comes back
|
|
201
|
+
* as stale once it has been seen unchanged over STALE_MS of tries, or over OLD_WATCH_MS when its
|
|
202
|
+
* last touch is also STALE_MS old by the clock. Time seen is counted in tries, so a slow waiter
|
|
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.
|
|
213
|
+
*/
|
|
214
|
+
function lookout(file) {
|
|
215
|
+
let seen = null;
|
|
216
|
+
let tries = 0;
|
|
217
|
+
let triedAt = { clock: Date.now(), time: performance.now() };
|
|
218
|
+
return async () => {
|
|
219
|
+
const now = await look(file);
|
|
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;
|
|
225
|
+
seen = now?.key ?? null;
|
|
226
|
+
if (now === null)
|
|
227
|
+
return null;
|
|
228
|
+
const watched = tries * RETRY_MS;
|
|
229
|
+
const old = Date.now() - now.touchedAt >= STALE_MS;
|
|
230
|
+
return watched >= STALE_MS || (old && watched >= OLD_WATCH_MS) ? now.key : null;
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
async function look(file) {
|
|
234
|
+
try {
|
|
235
|
+
// Read apart: a file replaced between the two only starts the count again.
|
|
236
|
+
const [holder, { mtimeMs }] = await Promise.all([readFile(file, 'utf8'), stat(file)]);
|
|
237
|
+
return { key: `${holder} ${String(mtimeMs)}`, touchedAt: mtimeMs };
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
if (codeOf(error) === 'ENOENT' || deletePending(error))
|
|
241
|
+
return null;
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/** Removes `file` only if it is still as it was seen. */
|
|
246
|
+
async function removeIf(file, seen) {
|
|
247
|
+
if ((await look(file))?.key !== seen)
|
|
248
|
+
return;
|
|
249
|
+
await unlink(file).catch((error) => {
|
|
250
|
+
if (codeOf(error) !== 'ENOENT')
|
|
251
|
+
throw error;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
/** Removes `file` only while it holds `id`: one taken over meanwhile is its new holder's. */
|
|
255
|
+
async function removeOwn(file, id) {
|
|
256
|
+
if ((await readFile(file, 'utf8').catch(() => null)) === id)
|
|
257
|
+
await unlink(file).catch(noop);
|
|
258
|
+
}
|
|
259
|
+
function aborted(signal) {
|
|
260
|
+
const reason = signal.reason;
|
|
261
|
+
throw reason;
|
|
262
|
+
}
|
|
263
|
+
/** Windows refuses to open a removed file's name while another process still has it open. */
|
|
264
|
+
const deletePending = (error) => process.platform === 'win32' && codeOf(error) === 'EPERM';
|
|
265
|
+
const codeOf = (error) => (error instanceof Error && 'code' in error ? error.code : undefined);
|
|
266
|
+
const noop = () => undefined;
|
|
267
|
+
//# sourceMappingURL=fileLock.js.map
|
|
@@ -0,0 +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;;;;;;;;;;;;;;;;;;;;;;;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;AAErB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAY;IACnC,mFAAmF;IACnF,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,+DAA+D,IAAI,EAAE,CAAC,CAAA;IACjH,OAAO,KAAK,EAAK,IAAsB,EAAE,MAAmB,EAAc,EAAE;QAC1E,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;QACrC,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,EAAE,CAAA;QACrB,CAAC;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AC,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,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CACrE,CAAA;AACH,CAAC;AAED,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAA;AAC1C,mGAAmG;AACnG,oCAAoC;AACpC,MAAM,IAAI,GAAG,SAAS,CAAC,QAAQ,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,UAAU,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAE3F,KAAK,UAAU,IAAI,CAAC,IAAY;IAC9B,IAAI,CAAC;Q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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/headless.d.ts
CHANGED
|
@@ -11,10 +11,15 @@ export type PastedRedirect =
|
|
|
11
11
|
readonly ok: false;
|
|
12
12
|
readonly error: CallbackError | {
|
|
13
13
|
readonly kind: 'not_a_url';
|
|
14
|
+
}
|
|
15
|
+
/** Not the address the sign-in sends the browser back to (`pending.redirectUri`). */
|
|
16
|
+
| {
|
|
17
|
+
readonly kind: 'wrong_redirect';
|
|
14
18
|
};
|
|
15
19
|
};
|
|
16
20
|
/**
|
|
17
|
-
* The redirect the person pasted, checked as the loopback checks one
|
|
18
|
-
*
|
|
21
|
+
* The redirect the person pasted, checked as the loopback checks one, before anything is sent: its
|
|
22
|
+
* address first (the loopback answers only its own `/callback`), then its state, its issuer, the
|
|
23
|
+
* server's error, the code.
|
|
19
24
|
*/
|
|
20
25
|
export declare function parsePastedRedirect(text: string, pending: PendingSignIn): PastedRedirect;
|
package/dist/node/headless.js
CHANGED
|
@@ -14,15 +14,28 @@ export function isHeadless(env, platform = process.platform) {
|
|
|
14
14
|
}
|
|
15
15
|
const nonEmpty = (value) => value !== undefined && value !== '';
|
|
16
16
|
/**
|
|
17
|
-
* The redirect the person pasted, checked as the loopback checks one
|
|
18
|
-
*
|
|
17
|
+
* The redirect the person pasted, checked as the loopback checks one, before anything is sent: its
|
|
18
|
+
* address first (the loopback answers only its own `/callback`), then its state, its issuer, the
|
|
19
|
+
* server's error, the code.
|
|
19
20
|
*/
|
|
20
21
|
export function parsePastedRedirect(text, pending) {
|
|
21
22
|
const trimmed = text.trim();
|
|
22
23
|
if (!URL.canParse(trimmed))
|
|
23
24
|
return { ok: false, error: { kind: 'not_a_url' } };
|
|
24
25
|
const callbackUrl = new URL(trimmed);
|
|
26
|
+
if (!isRedirectTo(callbackUrl, pending.redirectUri))
|
|
27
|
+
return { ok: false, error: { kind: 'wrong_redirect' } };
|
|
25
28
|
const read = readCallback(callbackUrl, pending);
|
|
26
29
|
return read.kind === 'code' ? { ok: true, callbackUrl, code: read.code } : { ok: false, error: read };
|
|
27
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* The same scheme, host, port and path; the query is the redirect's own. Not `origin`, which is
|
|
33
|
+
* "null" for every private-use scheme (RFC 8252 §7.1), so any two of them would match.
|
|
34
|
+
*/
|
|
35
|
+
function isRedirectTo(url, redirectUri) {
|
|
36
|
+
if (!URL.canParse(redirectUri))
|
|
37
|
+
return false;
|
|
38
|
+
const expected = new URL(redirectUri);
|
|
39
|
+
return url.protocol === expected.protocol && url.host === expected.host && url.pathname === expected.pathname;
|
|
40
|
+
}
|
|
28
41
|
//# sourceMappingURL=headless.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"headless.js","sourceRoot":"","sources":["../../src/node/headless.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAA0C,MAAM,mBAAmB,CAAA;AAExF;;;;GAIG;AAEH,4FAA4F;AAC5F,MAAM,UAAU,UAAU,CAAC,GAAiD,EAAE,WAAmB,OAAO,CAAC,QAAQ;IAC/G,IAAI,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,IAAI,CAAA;IACpC,IAAI,QAAQ,CAAC,GAAG,CAAC,gBAA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OAAO,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,EAAE,CAAA;IAC9E,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAA;IACpC,IAAI,CAAC,YAAY,CAAC,WAAW,EAAE,OAAO,CAAC,WAAW,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,gBAAgB,EAAE,EAAE,CAAA;IAC5G,MAAM,IAAI,GAAG,YAAY,CAAC,WAAW,EAAE,OAAO,CAAC,CAAA;IAC/C,OAAO,IAAI,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;AACvG,CAAC;AAED;;;GAGG;AACH,SAAS,YAAY,CAAC,GAAQ,EAAE,WAAmB;IACjD,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC;QAAE,OAAO,KAAK,CAAA;IAC5C,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,WAAW,CAAC,CAAA;IACrC,OAAO,GAAG,CAAC,QAAQ,KAAK,QAAQ,CAAC,QAAQ,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ,CAAC,IAAI,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ,CAAC,QAAQ,CAAA;AAC/G,CAAC"}
|
package/dist/node/index.d.ts
CHANGED
|
@@ -2,3 +2,12 @@ export { signInWithLoopback } from './loopback.js';
|
|
|
2
2
|
export type { LoopbackError, LoopbackResult, LoopbackSignInOptions } from './loopback.js';
|
|
3
3
|
export { isHeadless, parsePastedRedirect } from './headless.js';
|
|
4
4
|
export type { PastedRedirect } from './headless.js';
|
|
5
|
+
export { createSession, SessionError } from './session.js';
|
|
6
|
+
export { fileLock } from './fileLock.js';
|
|
7
|
+
export type { RevokeResult, Session, SessionFailure, SessionOptions, SessionTokens, TokenUpdate } from './session.js';
|
|
8
|
+
export { finishSignIn } from './clientSecret.js';
|
|
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,5 +1,13 @@
|
|
|
1
1
|
// The `@flipstream/sdk/node` entry: sign-in for Node apps, which the main entry never pulls in,
|
|
2
|
-
// so a browser bundle has no node:http (#65)
|
|
2
|
+
// so a browser bundle has no node:http (#65), and the session, which holds a refresh token and so
|
|
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), and ready-made stores for it (#137).
|
|
3
5
|
export { signInWithLoopback } from './loopback.js';
|
|
4
6
|
export { isHeadless, parsePastedRedirect } from './headless.js';
|
|
7
|
+
export { createSession, SessionError } from './session.js';
|
|
8
|
+
export { fileLock } from './fileLock.js';
|
|
9
|
+
// An app with a server signs in as a confidential client, with its client secret (#123).
|
|
10
|
+
export { finishSignIn } from './clientSecret.js';
|
|
11
|
+
export { fileStore } from './fileStore.js';
|
|
12
|
+
export { keychainStore } from './keychainStore.js';
|
|
5
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,
|
|
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 {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* One keychain step in a process of its own, which keychainStore spawns and kills at its deadline
|
|
3
|
+
* (D485, after the CLI's keychain-child.ts). A keychain call can park forever: on macOS, when the
|
|
4
|
+
* keychain wants a prompt that no screen can show, the call never returns (cli#119). Nothing inside
|
|
5
|
+
* the process can end it then, not an AbortSignal, a worker's terminate, or process.exit, which
|
|
6
|
+
* joins the stuck thread. A process can be killed.
|
|
7
|
+
*
|
|
8
|
+
* - **The request is one line on standard input:** the tokens never become an argument or a
|
|
9
|
+
* variable of this process, which other users can read.
|
|
10
|
+
* - **Standard input stays open as the parent's heartbeat.** When it closes, the parent is gone, and
|
|
11
|
+
* this process kills itself, rather than being left behind on a call that never returns.
|
|
12
|
+
* - **The answer is one line on file descriptor 3,** a pipe of its own, in one synchronous write:
|
|
13
|
+
* whatever else prints to standard output (a preload in NODE_OPTIONS, say) can't garble it.
|
|
14
|
+
*/
|
|
15
|
+
import { writeSync } from 'node:fs';
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
/** The tokens to save are the `set` step's `secret`. */
|
|
18
|
+
const zRequest = z.discriminatedUnion('op', [
|
|
19
|
+
z.strictObject({ op: z.literal('get'), service: z.string(), account: z.string() }),
|
|
20
|
+
z.strictObject({ op: z.literal('delete'), service: z.string(), account: z.string() }),
|
|
21
|
+
z.strictObject({ op: z.literal('set'), service: z.string(), account: z.string(), secret: z.string() }),
|
|
22
|
+
]);
|
|
23
|
+
// SIGKILL, never process.exit: a call stuck in the kernel would hold up an exit too.
|
|
24
|
+
const dieWithParent = () => {
|
|
25
|
+
process.kill(process.pid, 'SIGKILL');
|
|
26
|
+
};
|
|
27
|
+
// Installed first, for the whole life of the process: a parent that dies right after writing the
|
|
28
|
+
// request must still take this process with it.
|
|
29
|
+
process.stdin.on('end', dieWithParent);
|
|
30
|
+
process.stdin.on('close', dieWithParent);
|
|
31
|
+
const ANSWER_FD = 3;
|
|
32
|
+
const emit = (answer) => {
|
|
33
|
+
writeSync(ANSWER_FD, `${JSON.stringify(answer)}\n`);
|
|
34
|
+
};
|
|
35
|
+
function firstLine() {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
let buffered = '';
|
|
38
|
+
process.stdin.setEncoding('utf8');
|
|
39
|
+
process.stdin.on('data', (chunk) => {
|
|
40
|
+
buffered += chunk;
|
|
41
|
+
const end = buffered.indexOf('\n');
|
|
42
|
+
if (end !== -1)
|
|
43
|
+
resolve(buffered.slice(0, end));
|
|
44
|
+
});
|
|
45
|
+
process.stdin.on('error', reject);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function requestOf(line) {
|
|
49
|
+
let parsed;
|
|
50
|
+
try {
|
|
51
|
+
parsed = JSON.parse(line);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const request = zRequest.safeParse(parsed);
|
|
57
|
+
return request.success ? request.data : undefined;
|
|
58
|
+
}
|
|
59
|
+
async function step(request) {
|
|
60
|
+
let keyring;
|
|
61
|
+
try {
|
|
62
|
+
keyring = await import('@napi-rs/keyring');
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
const missing = error instanceof Error && 'code' in error && error.code === 'ERR_MODULE_NOT_FOUND';
|
|
66
|
+
return missing ? { ok: false, error: 'not installed', missing: true } : { ok: false, error: `the keyring couldn't load: ${messageOf(error)}` };
|
|
67
|
+
}
|
|
68
|
+
// Linux: the Secret Service only. Without one, the library falls back to the kernel's keyring,
|
|
69
|
+
// which forgets everything at a reboot; a step that fails is better than a sign-in that vanishes.
|
|
70
|
+
try {
|
|
71
|
+
const entry = new keyring.AsyncEntry(request.service, request.account, { linux: { store: 'secret-service' } });
|
|
72
|
+
switch (request.op) {
|
|
73
|
+
case 'get': {
|
|
74
|
+
// No item is nothing stored. 2.1.0's typings say `undefined` for one, but it gives `null`
|
|
75
|
+
// (Michel's live check on #142, 17 Oct: a read after a clear failed on `Buffer.from(null)`).
|
|
76
|
+
// An empty secret is nothing stored too: no save writes one.
|
|
77
|
+
const secret = await entry.getSecret();
|
|
78
|
+
return { ok: true, value: secret === undefined || secret === null || secret.length === 0 ? null : Buffer.from(secret).toString('utf8') };
|
|
79
|
+
}
|
|
80
|
+
case 'set':
|
|
81
|
+
await entry.setSecret(Buffer.from(request.secret, 'utf8'));
|
|
82
|
+
return { ok: true, value: null };
|
|
83
|
+
// Deleted, or nothing there to delete: either way nothing is saved now.
|
|
84
|
+
case 'delete':
|
|
85
|
+
await entry.deletePassword();
|
|
86
|
+
return { ok: true, value: null };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
// The keyring's own words: they never hold the secret, which is the value it didn't give.
|
|
91
|
+
return { ok: false, error: messageOf(error) };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const messageOf = (error) => (error instanceof Error ? error.message : String(error));
|
|
95
|
+
const request = requestOf(await firstLine());
|
|
96
|
+
emit(request === undefined ? { ok: false, error: 'the keychain step got no readable request' } : await step(request));
|
|
97
|
+
// Standard input, still open as the heartbeat, would keep this process alive. The answer has left in
|
|
98
|
+
// a synchronous write, and the keychain call has returned, so no stuck thread holds up the exit.
|
|
99
|
+
process.exit(0);
|
|
100
|
+
//# sourceMappingURL=keychainChild.js.map
|