techne-sdk 0.9.0 → 0.11.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 +33 -0
- package/README.md +40 -4
- package/dist/catalog/catalog.gen.js +192 -9
- package/dist/generated/index.d.ts +6 -0
- package/dist/generated/index.js +18 -0
- package/dist/generated/lexicons.d.ts +296 -14
- package/dist/generated/lexicons.js +167 -7
- package/dist/generated/nsid.d.ts +2 -0
- package/dist/generated/nsid.js +2 -0
- package/dist/generated/nsid.json +2 -0
- package/dist/generated/types/pub/techne/draft/identity/attachLinkGrant.d.ts +54 -0
- package/dist/generated/types/pub/techne/draft/identity/attachLinkGrant.js +76 -0
- package/dist/generated/types/pub/techne/draft/identity/claimLinkGrant.d.ts +49 -0
- package/dist/generated/types/pub/techne/draft/identity/claimLinkGrant.js +55 -0
- package/dist/session/own-session.d.ts +6 -6
- package/dist/session/own-session.js +45 -44
- package/dist/session/phone.d.ts +27 -8
- package/dist/session/phone.js +73 -14
- package/dist/session/relay.d.ts +23 -0
- package/dist/session/relay.js +44 -0
- package/dist/wasm/techne-wasm.gen.js +1 -1
- package/dist/wasm/techne-wasm2js.gen.js +15866 -15866
- package/package.json +1 -1
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
import { METHOD } from '../generated/nsid.js';
|
|
2
2
|
import { dpopKeyFromJwk, dpopProof } from './dpop.js';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
export const PLATFORM_ANSWER_WINDOW_MS = 60_000;
|
|
6
|
-
/** How long after the first send a refresh that got no answer may be sent again. */
|
|
7
|
-
export const LOST_ANSWER_WINDOW_MS = 50_000;
|
|
8
|
-
/** Every send is given up this long before {@link PLATFORM_ANSWER_WINDOW_MS} ends (clock skew, transit). */
|
|
9
|
-
export const SEND_MARGIN_MS = 5_000;
|
|
10
|
-
/** The waits between those sends, and how long one send may take at most. */
|
|
11
|
-
const LOST_ANSWER_DELAYS_MS = [1_000, 2_000, 4_000, 8_000, 15_000];
|
|
12
|
-
const REFRESH_TIMEOUT_MS = 15_000;
|
|
3
|
+
import { OwnSessionError, SESSION_ENDED, fetchOr, nonceIn, postPlatform, resendUntilAnswered, str, trimSlash, withNonceRetry, withTimeout, } from './relay.js';
|
|
4
|
+
export { LOST_ANSWER_WINDOW_MS, PLATFORM_ANSWER_WINDOW_MS, SEND_MARGIN_MS } from './relay.js';
|
|
13
5
|
/** How long sign-out waits for each of its calls (revoke, signOutDevice). */
|
|
14
6
|
export const SIGN_OUT_TIMEOUT_MS = 10_000;
|
|
15
7
|
/** An early refresh holds the call only when the access token has less than this to live. */
|
|
@@ -36,7 +28,7 @@ export function deviceProofHeaders(key, deviceSession, method, url) {
|
|
|
36
28
|
return { authorization: `Bearer ${deviceSession}`, dpop: dpopProof(key, { htm: method, htu: url, token: deviceSession }) };
|
|
37
29
|
}
|
|
38
30
|
/** The same token set (access and refresh token). */
|
|
39
|
-
function sameSet(a, b) {
|
|
31
|
+
export function sameSet(a, b) {
|
|
40
32
|
return a.accessToken === b.accessToken && a.refreshToken === b.refreshToken;
|
|
41
33
|
}
|
|
42
34
|
/** The device's own session (module doc). Make one with {@link OwnSession.restore} or {@link OwnSession.signedIn}.
|
|
@@ -242,40 +234,30 @@ export class OwnSession {
|
|
|
242
234
|
}
|
|
243
235
|
}
|
|
244
236
|
/** One refresh request (`body()` makes it, with a fresh proof each time), sent again while no answer comes
|
|
245
|
-
* (module doc
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
try {
|
|
256
|
-
got = await postPlatform(this.#fetch, this.#platformUrl, METHOD.REFRESH_OWN_SESSION, body(), { timeoutMs: Math.min(REFRESH_TIMEOUT_MS, left) });
|
|
237
|
+
* (module doc, {@link resendUntilAnswered}). */
|
|
238
|
+
#resendUntilAnswered(started, body) {
|
|
239
|
+
return resendUntilAnswered({
|
|
240
|
+
nsid: METHOD.REFRESH_OWN_SESSION,
|
|
241
|
+
started,
|
|
242
|
+
now: () => this.#now(),
|
|
243
|
+
sleep: (ms) => this.#sleep(ms),
|
|
244
|
+
noAnswerYet: (status) => NO_ANSWER_YET.has(status),
|
|
245
|
+
send: async (timeoutMs) => {
|
|
246
|
+
const got = await postPlatform(this.#fetch, this.#platformUrl, METHOD.REFRESH_OWN_SESSION, body(), { timeoutMs });
|
|
257
247
|
this.#noteNonce(this.#data.tokenEndpoint, nonceIn(got));
|
|
258
|
-
}
|
|
259
|
-
catch (e) {
|
|
260
|
-
failure = e;
|
|
261
|
-
}
|
|
262
|
-
if (got && !NO_ANSWER_YET.has(got.status))
|
|
263
248
|
return got;
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (got)
|
|
267
|
-
return got;
|
|
268
|
-
throw new OwnSessionError(NETWORK_ERROR, 'techne-sdk/session: refreshOwnSession got no answer', { cause: failure });
|
|
269
|
-
}
|
|
270
|
-
await this.#sleep(wait);
|
|
271
|
-
}
|
|
249
|
+
},
|
|
250
|
+
});
|
|
272
251
|
}
|
|
273
|
-
/** Ends the session here:
|
|
252
|
+
/** Ends the session here: calls `onEnded`, and unless `clear` is false (the store holds no set of this
|
|
253
|
+
* session's) clears the store and signs the device credential out (best effort, not awaited): the grant
|
|
254
|
+
* is gone, and the device session would otherwise keep the phone's push token on the platform until it
|
|
255
|
+
* expires. */
|
|
274
256
|
#end(error, clear = true) {
|
|
275
257
|
if (!this.#endedWith) {
|
|
276
258
|
this.#endedWith = error;
|
|
277
259
|
if (clear)
|
|
278
|
-
void this.#
|
|
260
|
+
void this.#clearIfStillOurs();
|
|
279
261
|
try {
|
|
280
262
|
this.#opts.onEnded?.(error);
|
|
281
263
|
}
|
|
@@ -283,6 +265,22 @@ export class OwnSession {
|
|
|
283
265
|
}
|
|
284
266
|
return this.#endedWith;
|
|
285
267
|
}
|
|
268
|
+
/** The store and the device credential, cleared only while the store holds this session's set (or none):
|
|
269
|
+
* a set another process rotated meanwhile (no shared `lock`) is a live session, not this ended one. */
|
|
270
|
+
async #clearIfStillOurs() {
|
|
271
|
+
try {
|
|
272
|
+
const stored = await this.#opts.store.get();
|
|
273
|
+
const from = this.#unsavedFrom;
|
|
274
|
+
if (stored && !(this.#ours(stored) && (sameSet(stored, this.#data) || (from && sameSet(stored, from)))))
|
|
275
|
+
return;
|
|
276
|
+
if (stored)
|
|
277
|
+
await this.#opts.store.delete().catch(() => { });
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
await this.#signOutDevice(this.#data);
|
|
283
|
+
}
|
|
286
284
|
/**
|
|
287
285
|
* Sign out: wait for a refresh in flight (so the newest refresh token is the one revoked), revoke the grant
|
|
288
286
|
* at the PDS (`revokeOwnSession` with the refresh token, so the whole grant goes), sign the device
|
|
@@ -312,12 +310,7 @@ export class OwnSession {
|
|
|
312
310
|
const d = stored === undefined || (from && sameSet(stored, from)) ? this.#data : stored;
|
|
313
311
|
try {
|
|
314
312
|
const revoked = await this.#revoke(d).catch(() => false);
|
|
315
|
-
|
|
316
|
-
if (d.deviceSession) {
|
|
317
|
-
const init = { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' };
|
|
318
|
-
deviceSignedOut = await withTimeout(SIGN_OUT_TIMEOUT_MS, (signal) => this.#deviceFetch(d, `/xrpc/${METHOD.SIGN_OUT_DEVICE}`, { ...init, signal })).then((r) => r.ok, () => false);
|
|
319
|
-
}
|
|
320
|
-
return { revoked, deviceSignedOut };
|
|
313
|
+
return { revoked, deviceSignedOut: await this.#signOutDevice(d) };
|
|
321
314
|
}
|
|
322
315
|
finally {
|
|
323
316
|
if (stored !== undefined)
|
|
@@ -329,6 +322,14 @@ export class OwnSession {
|
|
|
329
322
|
this.#end(new OwnSessionError(SESSION_ENDED, 'techne-sdk/session: signed out'), false);
|
|
330
323
|
}
|
|
331
324
|
}
|
|
325
|
+
/** `signOutDevice` with `d`'s device credential, given up after {@link SIGN_OUT_TIMEOUT_MS}: whether the
|
|
326
|
+
* platform took it. Never throws; false when there is no device credential. */
|
|
327
|
+
async #signOutDevice(d) {
|
|
328
|
+
if (!d.deviceSession)
|
|
329
|
+
return false;
|
|
330
|
+
const init = { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' };
|
|
331
|
+
return withTimeout(SIGN_OUT_TIMEOUT_MS, (signal) => this.#deviceFetch(d, `/xrpc/${METHOD.SIGN_OUT_DEVICE}`, { ...init, signal })).then((r) => r.ok, () => false);
|
|
332
|
+
}
|
|
332
333
|
async #revoke(d) {
|
|
333
334
|
const { answer } = await withNonceRetry(METHOD.REVOKE_OWN_SESSION, this.#nonces.get(nonceKey(d.revocationEndpoint)), async (nonce) => {
|
|
334
335
|
const got = await postPlatform(this.#fetch, this.#platformUrl, METHOD.REVOKE_OWN_SESSION, { token: d.refreshToken ?? d.accessToken, dpop: dpopProof(this.#key, { htm: 'POST', htu: d.revocationEndpoint, nonce }) }, { timeoutMs: SIGN_OUT_TIMEOUT_MS });
|
package/dist/session/phone.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type DpopKey } from './dpop.js';
|
|
2
|
-
import type
|
|
2
|
+
import { type OwnSessionData, type TokenStore } from './own-session.js';
|
|
3
3
|
import { type Fetch } from './relay.js';
|
|
4
4
|
/** The endpoints the door's proofs name (`startPhoneSignup` with `ownSession:{}`). */
|
|
5
5
|
export interface OwnSessionEndpoints {
|
|
@@ -64,16 +64,35 @@ export declare function finishPhoneSignIn(pending: PendingPhoneSignIn, code: str
|
|
|
64
64
|
* holding sessions. The device, with its full `rsds_` device session, takes over the OAuth session the
|
|
65
65
|
* platform holds for its account (`social.scenius.adoptHeldSession`): it gets the tokens and their DPoP key,
|
|
66
66
|
* and its `rsds_` narrows to that key (so from then on it goes only with a proof:
|
|
67
|
-
* {@link OwnSession.deviceFetchHandler}). The account's other devices on older builds sign in again.
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
67
|
+
* {@link OwnSession.deviceFetchHandler}). The account's other devices on older builds sign in again.
|
|
68
|
+
*
|
|
69
|
+
* The platform deletes its copy when it answers, so the answer must not be lost:
|
|
70
|
+
* - **On the wire**: a request that got no answer (the network failed, a send took longer than 15 s, a
|
|
71
|
+
* gateway answered 502/504) is sent again, the same request, starting up to
|
|
72
|
+
* {@link LOST_ANSWER_WINDOW_MS} after the first, each send given up before the platform's
|
|
73
|
+
* {@link PLATFORM_ANSWER_WINDOW_MS}: a platform that keeps the answer for the same device credential
|
|
74
|
+
* gives it again (one that does not refuses the retry, and the person signs in once). Past that window it
|
|
75
|
+
* rejects `NetworkError`.
|
|
76
|
+
* - **On the phone**: with `store`, the session is saved there as soon as it is checked, before the issuer's
|
|
77
|
+
* metadata is fetched (with the atproto endpoints, `/oauth/token` and `/oauth/revoke`), so an app killed
|
|
78
|
+
* meanwhile finds it in the store on its next launch ({@link OwnSession.restore}). It is saved only when
|
|
79
|
+
* the store holds no session when it looks, and its endpoints are corrected there afterwards only while the
|
|
80
|
+
* store still holds that set.
|
|
81
|
+
*
|
|
82
|
+
* The token and revocation endpoints come from the issuer's metadata (tried three times; failing that, the
|
|
83
|
+
* atproto defaults, so the tokens are never thrown away). Without `store`, save the answer at once
|
|
84
|
+
* ({@link OwnSession.signedIn}). With `store`, open the session with {@link OwnSession.restore}, not
|
|
85
|
+
* `signedIn`: the store holds this set, or a newer one that landed during the metadata fetch (a refresh, a
|
|
86
|
+
* sign-in), which `signedIn` would write over with spent tokens; only when `restore` finds nothing (the store
|
|
87
|
+
* could not be written) `signedIn` the answer. Rejects with `NoHeldSession` (nothing to
|
|
88
|
+
* adopt: already adopted, or never held), `Unavailable` (try again), `NetworkError` (no answer: try again),
|
|
89
|
+
* `WrongAccount` (a session for another DID than `did`), or `InvalidResponse` (no session, no issuer, or an
|
|
90
|
+
* issuer that is not an https origin; checked before anything is kept).
|
|
74
91
|
*/
|
|
75
92
|
export declare function adoptHeldSession(o: PhoneSignInOptions & {
|
|
76
93
|
deviceSession: string;
|
|
77
94
|
/** The DID the device is signed in as: a session for another account is refused (`WrongAccount`). */
|
|
78
95
|
did: string;
|
|
96
|
+
/** The app's session store: the session is saved there before the metadata fetch (above). */
|
|
97
|
+
store?: TokenStore;
|
|
79
98
|
}): Promise<OwnSessionData>;
|
package/dist/session/phone.js
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import { METHOD } from '../generated/nsid.js';
|
|
16
16
|
import { dpopKeyFromJwk, dpopProof, generateDpopKey, pkcePair } from './dpop.js';
|
|
17
|
-
import {
|
|
17
|
+
import { sameSet } from './own-session.js';
|
|
18
|
+
import { OwnSessionError, fetchOr, nonceIn, obj, postPlatform, refusal, resendUntilAnswered, str, trimSlash, withNonceRetry, withTimeout, } from './relay.js';
|
|
18
19
|
/** The fields every own-session answer must carry (beyond the lexicon's `required`, the SDK needs `pds`, where
|
|
19
20
|
* the tokens go, and `sub`). */
|
|
20
21
|
function ownAnswer(nsid, own) {
|
|
@@ -130,8 +131,13 @@ async function authorizationServerEndpoints(issuer, fetch) {
|
|
|
130
131
|
}
|
|
131
132
|
return { tokenEndpoint, revocationEndpoint };
|
|
132
133
|
}
|
|
133
|
-
/**
|
|
134
|
-
*
|
|
134
|
+
/** The token and revocation endpoints every atproto authorization server (`@atproto/oauth-provider`, the
|
|
135
|
+
* ePDS) serves: `/oauth/token` and `/oauth/revoke` on the issuer. */
|
|
136
|
+
function atprotoEndpoints(issuer) {
|
|
137
|
+
const base = trimSlash(issuer);
|
|
138
|
+
return { tokenEndpoint: `${base}/oauth/token`, revocationEndpoint: `${base}/oauth/revoke` };
|
|
139
|
+
}
|
|
140
|
+
/** {@link authorizationServerEndpoints}, tried three times; when no try answers, {@link atprotoEndpoints}.
|
|
135
141
|
* Used after the platform handed its session over and deleted its copy, so the tokens are kept either way
|
|
136
142
|
* (if those endpoints were wrong, the next refresh is refused and the person signs in again, which is what
|
|
137
143
|
* throwing the tokens away would have meant at once). */
|
|
@@ -146,26 +152,65 @@ async function endpointsKeepingTokens(issuer, fetch) {
|
|
|
146
152
|
await sleep(METADATA_RETRY_DELAYS_MS[i]);
|
|
147
153
|
}
|
|
148
154
|
}
|
|
149
|
-
|
|
150
|
-
return { tokenEndpoint: `${base}/oauth/token`, revocationEndpoint: `${base}/oauth/revoke` };
|
|
155
|
+
return atprotoEndpoints(issuer);
|
|
151
156
|
}
|
|
157
|
+
/** Save `data` when `store` holds no session when it looks (a session already there is not written over):
|
|
158
|
+
* whether it did. Never throws. */
|
|
159
|
+
async function saveIfEmpty(store, data) {
|
|
160
|
+
try {
|
|
161
|
+
if ((await store.get()) !== null)
|
|
162
|
+
return false;
|
|
163
|
+
await store.set(data);
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** An adopt answer lost on the way (a gateway that got no answer from the platform): send it again. A 503 or
|
|
171
|
+
* a 429 is the platform's answer (nothing was handed over: try later), not a lost one. */
|
|
172
|
+
const ADOPT_NO_ANSWER = new Set([502, 504]);
|
|
152
173
|
/**
|
|
153
174
|
* @deprecated A one-time move for a device signed in on an older build, removed when the platform stops
|
|
154
175
|
* holding sessions. The device, with its full `rsds_` device session, takes over the OAuth session the
|
|
155
176
|
* platform holds for its account (`social.scenius.adoptHeldSession`): it gets the tokens and their DPoP key,
|
|
156
177
|
* and its `rsds_` narrows to that key (so from then on it goes only with a proof:
|
|
157
|
-
* {@link OwnSession.deviceFetchHandler}). The account's other devices on older builds sign in again.
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
178
|
+
* {@link OwnSession.deviceFetchHandler}). The account's other devices on older builds sign in again.
|
|
179
|
+
*
|
|
180
|
+
* The platform deletes its copy when it answers, so the answer must not be lost:
|
|
181
|
+
* - **On the wire**: a request that got no answer (the network failed, a send took longer than 15 s, a
|
|
182
|
+
* gateway answered 502/504) is sent again, the same request, starting up to
|
|
183
|
+
* {@link LOST_ANSWER_WINDOW_MS} after the first, each send given up before the platform's
|
|
184
|
+
* {@link PLATFORM_ANSWER_WINDOW_MS}: a platform that keeps the answer for the same device credential
|
|
185
|
+
* gives it again (one that does not refuses the retry, and the person signs in once). Past that window it
|
|
186
|
+
* rejects `NetworkError`.
|
|
187
|
+
* - **On the phone**: with `store`, the session is saved there as soon as it is checked, before the issuer's
|
|
188
|
+
* metadata is fetched (with the atproto endpoints, `/oauth/token` and `/oauth/revoke`), so an app killed
|
|
189
|
+
* meanwhile finds it in the store on its next launch ({@link OwnSession.restore}). It is saved only when
|
|
190
|
+
* the store holds no session when it looks, and its endpoints are corrected there afterwards only while the
|
|
191
|
+
* store still holds that set.
|
|
192
|
+
*
|
|
193
|
+
* The token and revocation endpoints come from the issuer's metadata (tried three times; failing that, the
|
|
194
|
+
* atproto defaults, so the tokens are never thrown away). Without `store`, save the answer at once
|
|
195
|
+
* ({@link OwnSession.signedIn}). With `store`, open the session with {@link OwnSession.restore}, not
|
|
196
|
+
* `signedIn`: the store holds this set, or a newer one that landed during the metadata fetch (a refresh, a
|
|
197
|
+
* sign-in), which `signedIn` would write over with spent tokens; only when `restore` finds nothing (the store
|
|
198
|
+
* could not be written) `signedIn` the answer. Rejects with `NoHeldSession` (nothing to
|
|
199
|
+
* adopt: already adopted, or never held), `Unavailable` (try again), `NetworkError` (no answer: try again),
|
|
200
|
+
* `WrongAccount` (a session for another DID than `did`), or `InvalidResponse` (no session, no issuer, or an
|
|
201
|
+
* issuer that is not an https origin; checked before anything is kept).
|
|
164
202
|
*/
|
|
165
203
|
export async function adoptHeldSession(o) {
|
|
166
204
|
const fetch = fetchOr(o.fetch);
|
|
167
205
|
const nsid = METHOD.ADOPT_HELD_SESSION;
|
|
168
|
-
const answer = await
|
|
206
|
+
const answer = await resendUntilAnswered({
|
|
207
|
+
nsid,
|
|
208
|
+
started: Date.now(),
|
|
209
|
+
now: () => Date.now(),
|
|
210
|
+
sleep,
|
|
211
|
+
noAnswerYet: (status) => ADOPT_NO_ANSWER.has(status),
|
|
212
|
+
send: (timeoutMs) => postPlatform(fetch, o.platformUrl, nsid, {}, { headers: { authorization: `Bearer ${o.deviceSession}` }, timeoutMs }),
|
|
213
|
+
});
|
|
169
214
|
if (answer.status !== 200)
|
|
170
215
|
throw refusal(nsid, answer);
|
|
171
216
|
const own = ownAnswer(nsid, obj(answer.json, 'ownSession'));
|
|
@@ -175,5 +220,19 @@ export async function adoptHeldSession(o) {
|
|
|
175
220
|
const issuer = str(own, 'issuer');
|
|
176
221
|
if (!issuer || !HTTPS_ORIGIN.test(issuer))
|
|
177
222
|
throw new OwnSessionError('InvalidResponse', `techne-sdk/session: ${nsid} named no https issuer origin (${issuer})`);
|
|
178
|
-
|
|
223
|
+
const first = sessionData(own, key, atprotoEndpoints(issuer), undefined, { deviceSession: o.deviceSession });
|
|
224
|
+
const saved = o.store ? await saveIfEmpty(o.store, first) : false;
|
|
225
|
+
const endpoints = await endpointsKeepingTokens(issuer, fetch);
|
|
226
|
+
if (endpoints.tokenEndpoint === first.tokenEndpoint && endpoints.revocationEndpoint === first.revocationEndpoint)
|
|
227
|
+
return first;
|
|
228
|
+
const data = { ...first, ...endpoints };
|
|
229
|
+
if (saved && o.store) {
|
|
230
|
+
try {
|
|
231
|
+
const now = await o.store.get();
|
|
232
|
+
if (now && sameSet(now, first))
|
|
233
|
+
await o.store.set(data);
|
|
234
|
+
}
|
|
235
|
+
catch { }
|
|
236
|
+
}
|
|
237
|
+
return data;
|
|
179
238
|
}
|
package/dist/session/relay.d.ts
CHANGED
|
@@ -67,3 +67,26 @@ export declare function withNonceRetry(nsid: string, nonce: string | undefined,
|
|
|
67
67
|
answer: PlatformAnswer;
|
|
68
68
|
nonce?: string;
|
|
69
69
|
}>;
|
|
70
|
+
/** How long the platform keeps an answer for the same request (a refresh's for the same token and key, an
|
|
71
|
+
* adopt's for the same device credential). */
|
|
72
|
+
export declare const PLATFORM_ANSWER_WINDOW_MS = 60000;
|
|
73
|
+
/** How long after the first send a request that got no answer may be sent again. */
|
|
74
|
+
export declare const LOST_ANSWER_WINDOW_MS = 50000;
|
|
75
|
+
/** Every send is given up this long before {@link PLATFORM_ANSWER_WINDOW_MS} ends (clock skew, transit). */
|
|
76
|
+
export declare const SEND_MARGIN_MS = 5000;
|
|
77
|
+
/**
|
|
78
|
+
* One platform request whose answer the platform keeps for {@link PLATFORM_ANSWER_WINDOW_MS}, sent again
|
|
79
|
+
* while no answer comes: `send(timeoutMs)` makes it (afresh each time, e.g. with a new proof). A send that
|
|
80
|
+
* throws (no answer, or no answer in its time) or an answer `noAnswerYet` names counts as none. A send starts
|
|
81
|
+
* up to {@link LOST_ANSWER_WINDOW_MS} after `started`, and each is given up {@link SEND_MARGIN_MS} before
|
|
82
|
+
* the platform's window ends, so no resend reaches the platform after its kept answer is gone. Past that,
|
|
83
|
+
* the last answer, or `NetworkError`.
|
|
84
|
+
*/
|
|
85
|
+
export declare function resendUntilAnswered(o: {
|
|
86
|
+
nsid: string;
|
|
87
|
+
started: number;
|
|
88
|
+
now: () => number;
|
|
89
|
+
sleep: (ms: number) => Promise<void>;
|
|
90
|
+
noAnswerYet: (status: number) => boolean;
|
|
91
|
+
send: (timeoutMs: number) => Promise<PlatformAnswer>;
|
|
92
|
+
}): Promise<PlatformAnswer>;
|
package/dist/session/relay.js
CHANGED
|
@@ -118,3 +118,47 @@ export async function withNonceRetry(nsid, nonce, send, retries = 1) {
|
|
|
118
118
|
nonce = err.nonce;
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
|
+
/** How long the platform keeps an answer for the same request (a refresh's for the same token and key, an
|
|
122
|
+
* adopt's for the same device credential). */
|
|
123
|
+
export const PLATFORM_ANSWER_WINDOW_MS = 60_000;
|
|
124
|
+
/** How long after the first send a request that got no answer may be sent again. */
|
|
125
|
+
export const LOST_ANSWER_WINDOW_MS = 50_000;
|
|
126
|
+
/** Every send is given up this long before {@link PLATFORM_ANSWER_WINDOW_MS} ends (clock skew, transit). */
|
|
127
|
+
export const SEND_MARGIN_MS = 5_000;
|
|
128
|
+
/** The waits between those sends, and how long one send may take at most. */
|
|
129
|
+
const LOST_ANSWER_DELAYS_MS = [1_000, 2_000, 4_000, 8_000, 15_000];
|
|
130
|
+
const SEND_TIMEOUT_MS = 15_000;
|
|
131
|
+
/**
|
|
132
|
+
* One platform request whose answer the platform keeps for {@link PLATFORM_ANSWER_WINDOW_MS}, sent again
|
|
133
|
+
* while no answer comes: `send(timeoutMs)` makes it (afresh each time, e.g. with a new proof). A send that
|
|
134
|
+
* throws (no answer, or no answer in its time) or an answer `noAnswerYet` names counts as none. A send starts
|
|
135
|
+
* up to {@link LOST_ANSWER_WINDOW_MS} after `started`, and each is given up {@link SEND_MARGIN_MS} before
|
|
136
|
+
* the platform's window ends, so no resend reaches the platform after its kept answer is gone. Past that,
|
|
137
|
+
* the last answer, or `NetworkError`.
|
|
138
|
+
*/
|
|
139
|
+
export async function resendUntilAnswered(o) {
|
|
140
|
+
const deadline = o.started + PLATFORM_ANSWER_WINDOW_MS - SEND_MARGIN_MS;
|
|
141
|
+
let failure;
|
|
142
|
+
for (let lost = 0;; lost++) {
|
|
143
|
+
let got;
|
|
144
|
+
const left = deadline - o.now();
|
|
145
|
+
if (left <= 0)
|
|
146
|
+
throw new OwnSessionError(NETWORK_ERROR, `techne-sdk/session: ${o.nsid} got no answer in the window`, { cause: failure });
|
|
147
|
+
try {
|
|
148
|
+
got = await o.send(Math.min(SEND_TIMEOUT_MS, left));
|
|
149
|
+
failure = undefined;
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
failure = e;
|
|
153
|
+
}
|
|
154
|
+
if (got && !o.noAnswerYet(got.status))
|
|
155
|
+
return got;
|
|
156
|
+
const wait = Math.max(LOST_ANSWER_DELAYS_MS[Math.min(lost, LOST_ANSWER_DELAYS_MS.length - 1)], ((got ? retryAfterOf(got) : undefined) ?? 0) * 1000);
|
|
157
|
+
if (o.now() + wait - o.started > LOST_ANSWER_WINDOW_MS) {
|
|
158
|
+
if (got)
|
|
159
|
+
return got;
|
|
160
|
+
throw new OwnSessionError(NETWORK_ERROR, `techne-sdk/session: ${o.nsid} got no answer`, { cause: failure });
|
|
161
|
+
}
|
|
162
|
+
await o.sleep(wait);
|
|
163
|
+
}
|
|
164
|
+
}
|