@serfab/cadre-client 0.0.0-stage → 2.0.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/ADOPTING.md +220 -0
- package/README.md +129 -3
- package/dist/address.d.ts +45 -0
- package/dist/address.js +109 -0
- package/dist/address.js.map +1 -0
- package/dist/app-link/index.d.ts +16 -0
- package/dist/app-link/index.js +17 -0
- package/dist/app-link/index.js.map +1 -0
- package/dist/app-link/join-response.d.ts +31 -0
- package/dist/app-link/join-response.js +87 -0
- package/dist/app-link/join-response.js.map +1 -0
- package/dist/app-link/known-apps.d.ts +32 -0
- package/dist/app-link/known-apps.js +51 -0
- package/dist/app-link/known-apps.js.map +1 -0
- package/dist/app-link/link-request-store.d.ts +50 -0
- package/dist/app-link/link-request-store.js +101 -0
- package/dist/app-link/link-request-store.js.map +1 -0
- package/dist/app-link/links.d.ts +47 -0
- package/dist/app-link/links.js +76 -0
- package/dist/app-link/links.js.map +1 -0
- package/dist/app-link/recommend-links.d.ts +30 -0
- package/dist/app-link/recommend-links.js +33 -0
- package/dist/app-link/recommend-links.js.map +1 -0
- package/dist/app-link/recommendation-store.d.ts +54 -0
- package/dist/app-link/recommendation-store.js +99 -0
- package/dist/app-link/recommendation-store.js.map +1 -0
- package/dist/bootstrap.d.ts +106 -0
- package/dist/bootstrap.js +106 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/inbox.d.ts +10 -0
- package/dist/inbox.js +28 -0
- package/dist/inbox.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/invitation-text.d.ts +81 -0
- package/dist/invitation-text.js +80 -0
- package/dist/invitation-text.js.map +1 -0
- package/dist/lifecycle/background-runner.d.ts +94 -0
- package/dist/lifecycle/background-runner.js +278 -0
- package/dist/lifecycle/background-runner.js.map +1 -0
- package/dist/lifecycle/index.d.ts +9 -0
- package/dist/lifecycle/index.js +10 -0
- package/dist/lifecycle/index.js.map +1 -0
- package/dist/loader.d.ts +47 -0
- package/dist/loader.js +93 -0
- package/dist/loader.js.map +1 -0
- package/dist/model.d.ts +284 -0
- package/dist/model.js +22 -0
- package/dist/model.js.map +1 -0
- package/dist/node-code-text.d.ts +10 -0
- package/dist/node-code-text.js +30 -0
- package/dist/node-code-text.js.map +1 -0
- package/dist/result.d.ts +79 -0
- package/dist/result.js +84 -0
- package/dist/result.js.map +1 -0
- package/dist/sereus/client.d.ts +49 -0
- package/dist/sereus/client.js +420 -0
- package/dist/sereus/client.js.map +1 -0
- package/dist/sereus/edge-host.d.ts +22 -0
- package/dist/sereus/edge-host.js +61 -0
- package/dist/sereus/edge-host.js.map +1 -0
- package/dist/sereus/host.d.ts +57 -0
- package/dist/sereus/host.js +2 -0
- package/dist/sereus/host.js.map +1 -0
- package/dist/sereus/index.d.ts +8 -0
- package/dist/sereus/index.js +9 -0
- package/dist/sereus/index.js.map +1 -0
- package/dist/sereus/invitations.d.ts +16 -0
- package/dist/sereus/invitations.js +196 -0
- package/dist/sereus/invitations.js.map +1 -0
- package/dist/sereus/node-code.d.ts +52 -0
- package/dist/sereus/node-code.js +51 -0
- package/dist/sereus/node-code.js.map +1 -0
- package/dist/store.d.ts +27 -0
- package/dist/store.js +51 -0
- package/dist/store.js.map +1 -0
- package/dist/stores.d.ts +37 -0
- package/dist/stores.js +45 -0
- package/dist/stores.js.map +1 -0
- package/examples/README.md +20 -0
- package/examples/nativescript/cadrons-page.xml +24 -0
- package/examples/nativescript/cadrons-view-model.ts +68 -0
- package/examples/react-native/CadronList.tsx +204 -0
- package/examples/react-native/InvitationPanel.tsx +162 -0
- package/examples/react-native/useStore.ts +11 -0
- package/examples/svelte/CadronList.svelte +86 -0
- package/examples/svelte/InvitationPanel.svelte +79 -0
- package/package.json +65 -3
- package/src/address.ts +125 -0
- package/src/app-link/index.ts +45 -0
- package/src/app-link/join-response.ts +109 -0
- package/src/app-link/known-apps.ts +70 -0
- package/src/app-link/link-request-store.ts +129 -0
- package/src/app-link/links.ts +90 -0
- package/src/app-link/recommend-links.ts +49 -0
- package/src/app-link/recommendation-store.ts +132 -0
- package/src/bootstrap.ts +184 -0
- package/src/globals.d.ts +26 -0
- package/src/inbox.ts +36 -0
- package/src/index.ts +15 -0
- package/src/invitation-text.ts +156 -0
- package/src/lifecycle/background-runner.ts +363 -0
- package/src/lifecycle/index.ts +17 -0
- package/src/loader.ts +103 -0
- package/src/model.ts +299 -0
- package/src/node-code-text.ts +25 -0
- package/src/result.ts +131 -0
- package/src/sereus/client.ts +454 -0
- package/src/sereus/edge-host.ts +81 -0
- package/src/sereus/host.ts +56 -0
- package/src/sereus/index.ts +8 -0
- package/src/sereus/invitations.ts +206 -0
- package/src/sereus/node-code.ts +80 -0
- package/src/store.ts +61 -0
- package/src/stores.ts +59 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The join response: the owner app's answer to a join request that arrived as a link, carried in
|
|
3
|
+
* the link it opens back to the asking app. Either an invitation targeted at the request's peer
|
|
4
|
+
* id or the reason there is none.
|
|
5
|
+
*
|
|
6
|
+
* `sereus-join-response:1.<base64url of canonical JSON>`, the join request's own encoding. The
|
|
7
|
+
* nonce is the request's `reply.nonce`, so the asking app takes only an answer to a request it
|
|
8
|
+
* has open (`LinkRequestStore.take`). Nothing in it is signed: `appName` is what the answering
|
|
9
|
+
* app calls itself, for the asking app's messages only.
|
|
10
|
+
*/
|
|
11
|
+
import { CADRE_INVITATION_PREFIX, CADRE_JOIN_REQUEST_MAX_APP_NAME, CADRE_JOIN_REQUEST_PREFIX, canonicalJson, } from '@serfab/cadre-core';
|
|
12
|
+
import { fromString as uint8ArrayFromString, toString as uint8ArrayToString } from 'uint8arrays';
|
|
13
|
+
/** Version 1 of the join response; the only one either side reads. */
|
|
14
|
+
export const CADRE_JOIN_RESPONSE_PREFIX = 'sereus-join-response:1.';
|
|
15
|
+
const JOIN_REFUSALS = ['denied', 'no-cadre', 'not-owner', 'already-member'];
|
|
16
|
+
/** Encode a response as the text the owner app's link carries. Refuses what {@link decodeCadreJoinResponse} would. */
|
|
17
|
+
export function encodeCadreJoinResponse(response) {
|
|
18
|
+
const json = canonicalJson(requireCadreJoinResponse(response));
|
|
19
|
+
return `${CADRE_JOIN_RESPONSE_PREFIX}${uint8ArrayToString(new TextEncoder().encode(json), 'base64url')}`;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Decode the text an answering link carried. Refuses, naming the problem: a join request,
|
|
23
|
+
* any prefix but version 1's, a body that is not base64url JSON of the response's shape, an
|
|
24
|
+
* empty nonce, an `appName` that is empty or longer than 64 characters once trimmed, an
|
|
25
|
+
* `invitation` that is not an invitation's text, an unknown refusal, and both or neither of
|
|
26
|
+
* `invitation` and `refused`.
|
|
27
|
+
*/
|
|
28
|
+
export function decodeCadreJoinResponse(text) {
|
|
29
|
+
const trimmed = text.trim();
|
|
30
|
+
if (trimmed.startsWith(CADRE_JOIN_REQUEST_PREFIX)) {
|
|
31
|
+
throw new Error('This is a join request, not the answer to one');
|
|
32
|
+
}
|
|
33
|
+
if (!trimmed.startsWith(CADRE_JOIN_RESPONSE_PREFIX)) {
|
|
34
|
+
throw new Error(`A join response must start with "${CADRE_JOIN_RESPONSE_PREFIX}" (this one starts with "${trimmed.slice(0, CADRE_JOIN_RESPONSE_PREFIX.length)}")`);
|
|
35
|
+
}
|
|
36
|
+
let parsed;
|
|
37
|
+
try {
|
|
38
|
+
parsed = JSON.parse(new TextDecoder().decode(uint8ArrayFromString(trimmed.slice(CADRE_JOIN_RESPONSE_PREFIX.length), 'base64url')));
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw new Error('A join response must carry base64url-encoded JSON after its prefix', { cause: error });
|
|
42
|
+
}
|
|
43
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
44
|
+
throw new Error('A join response must decode to a JSON object');
|
|
45
|
+
}
|
|
46
|
+
const { v, nonce, appName, invitation, refused } = parsed;
|
|
47
|
+
if (v !== 1)
|
|
48
|
+
throw new Error(`A join response must be version 1 (this one says ${JSON.stringify(v)})`);
|
|
49
|
+
if (typeof nonce !== 'string')
|
|
50
|
+
throw new Error('A join response must carry its request\'s nonce');
|
|
51
|
+
if (typeof appName !== 'string')
|
|
52
|
+
throw new Error('A join response must carry the answering app\'s name');
|
|
53
|
+
if (invitation !== undefined && typeof invitation !== 'string')
|
|
54
|
+
throw new Error('A join response\'s invitation must be text');
|
|
55
|
+
if (refused !== undefined && typeof refused !== 'string')
|
|
56
|
+
throw new Error('A join response\'s refusal must be text');
|
|
57
|
+
return requireCadreJoinResponse({ nonce, appName, invitation, refused });
|
|
58
|
+
}
|
|
59
|
+
/** The shape rules both directions apply. Returns the response with its name trimmed. */
|
|
60
|
+
function requireCadreJoinResponse(fields) {
|
|
61
|
+
const { nonce, invitation, refused } = fields;
|
|
62
|
+
if (nonce.length === 0)
|
|
63
|
+
throw new Error('A join response must carry its request\'s nonce');
|
|
64
|
+
const appName = fields.appName.trim();
|
|
65
|
+
if (appName.length === 0)
|
|
66
|
+
throw new Error('A join response must name the answering app');
|
|
67
|
+
if (appName.length > CADRE_JOIN_REQUEST_MAX_APP_NAME) {
|
|
68
|
+
throw new Error(`A join response's app name must be at most ${CADRE_JOIN_REQUEST_MAX_APP_NAME} characters (this one has ${appName.length})`);
|
|
69
|
+
}
|
|
70
|
+
if ((invitation === undefined) === (refused === undefined)) {
|
|
71
|
+
throw new Error('A join response must carry exactly one of an invitation and a refusal');
|
|
72
|
+
}
|
|
73
|
+
if (invitation !== undefined) {
|
|
74
|
+
if (!invitation.startsWith(CADRE_INVITATION_PREFIX)) {
|
|
75
|
+
throw new Error(`A join response's invitation must start with "${CADRE_INVITATION_PREFIX}"`);
|
|
76
|
+
}
|
|
77
|
+
return { v: 1, nonce, appName, invitation };
|
|
78
|
+
}
|
|
79
|
+
if (!isJoinRefusal(refused)) {
|
|
80
|
+
throw new Error(`A join response's refusal must be one of ${JOIN_REFUSALS.join(', ')} (this one is ${JSON.stringify(refused)})`);
|
|
81
|
+
}
|
|
82
|
+
return { v: 1, nonce, appName, refused };
|
|
83
|
+
}
|
|
84
|
+
function isJoinRefusal(value) {
|
|
85
|
+
return value !== undefined && JOIN_REFUSALS.includes(value);
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=join-response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"join-response.js","sourceRoot":"","sources":["../../src/app-link/join-response.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EACN,uBAAuB,EACvB,+BAA+B,EAC/B,yBAAyB,EACzB,aAAa,GACb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjG,sEAAsE;AACtE,MAAM,CAAC,MAAM,0BAA0B,GAAG,yBAAyB,CAAC;AASpE,MAAM,aAAa,GAAsB,CAAC,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,gBAAgB,CAAyB,CAAC;AAOvH,sHAAsH;AACtH,MAAM,UAAU,uBAAuB,CAAC,QAA2B;IAClE,MAAM,IAAI,GAAG,aAAa,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC/D,OAAO,GAAG,0BAA0B,GAAG,kBAAkB,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC,EAAE,CAAC;AAC1G,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAAY;IACnD,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;IAC5B,IAAI,OAAO,CAAC,UAAU,CAAC,yBAAyB,CAAC,EAAE,CAAC;QACnD,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;IAClE,CAAC;IACD,IAAI,CAAC,OAAO,CAAC,UA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EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,4CAA4C,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,iBAAiB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAClI,CAAC;IACD,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC1C,CAAC;AAED,SAAS,aAAa,CAAC,KAAyB;IAC/C,OAAO,KAAK,KAAK,SAAS,IAAI,aAAa,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AAC7D,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Sereus apps one app can open at their own URL scheme, and the lookup of which of them are
|
|
3
|
+
* installed. iOS has no app chooser for a scheme several apps register, so an app asking to join
|
|
4
|
+
* opens one owner app by that app's own scheme, chosen from this list.
|
|
5
|
+
*/
|
|
6
|
+
/** A Sereus app another can open at its own URL scheme. */
|
|
7
|
+
export interface SereusApp {
|
|
8
|
+
/** The name the asking app shows in its list of owner apps. */
|
|
9
|
+
name: string;
|
|
10
|
+
/** The app's own custom URL scheme (`expo.scheme`), lowercase. */
|
|
11
|
+
scheme: string;
|
|
12
|
+
/** The app's Play application id, for its store page; absent while it is not on Play. */
|
|
13
|
+
androidPackage?: string;
|
|
14
|
+
/** The app's numeric App Store id, for its store page; absent while it is not on the App Store. */
|
|
15
|
+
iosAppStoreId?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The Sereus apps the kit knows; one entry per published app. A new app is added here by a pull
|
|
19
|
+
* request and a kit release; until then an app passes it as an extra (`appLinks.extraApps` in
|
|
20
|
+
* the config plugin, and the `apps` argument of {@link installedSereusApps}).
|
|
21
|
+
*
|
|
22
|
+
* `app.plugin.cjs` keeps a copy (`withCadre.KNOWN_SEREUS_APPS`), because the CommonJS plugin
|
|
23
|
+
* cannot load this ESM build; `test/app-link/app-link.spec.ts` keeps the two equal.
|
|
24
|
+
*/
|
|
25
|
+
export declare const KNOWN_SEREUS_APPS: readonly SereusApp[];
|
|
26
|
+
/**
|
|
27
|
+
* The apps in `apps` that `canOpenURL('<scheme>://')` reports installed, minus `ownScheme`, in
|
|
28
|
+
* the order given and one per scheme. For iOS, where each queried scheme must be listed in
|
|
29
|
+
* `LSApplicationQueriesSchemes`: React Native's `canOpenURL` rejects for a scheme that is not, and
|
|
30
|
+
* such an app is counted as not installed, with one warning naming the schemes to add.
|
|
31
|
+
*/
|
|
32
|
+
export declare function installedSereusApps(apps: readonly SereusApp[], ownScheme: string, canOpenURL: (url: string) => Promise<boolean>): Promise<SereusApp[]>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Sereus apps one app can open at their own URL scheme, and the lookup of which of them are
|
|
3
|
+
* installed. iOS has no app chooser for a scheme several apps register, so an app asking to join
|
|
4
|
+
* opens one owner app by that app's own scheme, chosen from this list.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The Sereus apps the kit knows; one entry per published app. A new app is added here by a pull
|
|
8
|
+
* request and a kit release; until then an app passes it as an extra (`appLinks.extraApps` in
|
|
9
|
+
* the config plugin, and the `apps` argument of {@link installedSereusApps}).
|
|
10
|
+
*
|
|
11
|
+
* `app.plugin.cjs` keeps a copy (`withCadre.KNOWN_SEREUS_APPS`), because the CommonJS plugin
|
|
12
|
+
* cannot load this ESM build; `test/app-link/app-link.spec.ts` keeps the two equal.
|
|
13
|
+
*/
|
|
14
|
+
export const KNOWN_SEREUS_APPS = [
|
|
15
|
+
{ name: 'Sereus P2P Chat', scheme: 'sereus-chat' },
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* The apps in `apps` that `canOpenURL('<scheme>://')` reports installed, minus `ownScheme`, in
|
|
19
|
+
* the order given and one per scheme. For iOS, where each queried scheme must be listed in
|
|
20
|
+
* `LSApplicationQueriesSchemes`: React Native's `canOpenURL` rejects for a scheme that is not, and
|
|
21
|
+
* such an app is counted as not installed, with one warning naming the schemes to add.
|
|
22
|
+
*/
|
|
23
|
+
export async function installedSereusApps(apps, ownScheme, canOpenURL) {
|
|
24
|
+
const candidates = oneAppPerScheme(apps).filter((app) => app.scheme !== ownScheme);
|
|
25
|
+
const unqueryable = [];
|
|
26
|
+
const installed = await Promise.all(candidates.map(async (app) => {
|
|
27
|
+
try {
|
|
28
|
+
return await canOpenURL(`${app.scheme}://`);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
unqueryable.push({ scheme: app.scheme, error });
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}));
|
|
35
|
+
if (unqueryable.length > 0) {
|
|
36
|
+
console.warn(`[cadre-client/app-link] canOpenURL refused ${unqueryable.map((entry) => entry.scheme).join(', ')}; ` +
|
|
37
|
+
'add each to LSApplicationQueriesSchemes (the config plugin\'s appLinks.extraApps does)', unqueryable);
|
|
38
|
+
}
|
|
39
|
+
return candidates.filter((_app, index) => installed[index]);
|
|
40
|
+
}
|
|
41
|
+
/** The first app listed for each scheme: the known list and an app's extras may name one twice. */
|
|
42
|
+
function oneAppPerScheme(apps) {
|
|
43
|
+
const seen = new Set();
|
|
44
|
+
return apps.filter((app) => {
|
|
45
|
+
if (seen.has(app.scheme))
|
|
46
|
+
return false;
|
|
47
|
+
seen.add(app.scheme);
|
|
48
|
+
return true;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=known-apps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"known-apps.js","sourceRoot":"","sources":["../../src/app-link/known-apps.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAcH;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAyB;IACtD,EAAE,IAAI,EAAE,iBAAiB,EAAE,MAAM,EAAE,aAAa,EAAE;CAClD,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UA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AE,OAAO,KAAK,CAAC;QACvC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACrB,OAAO,IAAI,CAAC;IACb,CAAC,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `LinkRequestStore`: the asking app's record of the join request it opened as a link, so it
|
|
3
|
+
* takes only the answer to that request, once. The record is saved before the owner app opens,
|
|
4
|
+
* in the app-private node-local database (`PhoneNode.linkRequestSlot()`), so an asking app the
|
|
5
|
+
* OS killed while the owner app was in front still takes the answer when the link starts it.
|
|
6
|
+
*
|
|
7
|
+
* The nonce is the security contract: an answer carrying any other nonce, a second answer to the
|
|
8
|
+
* same request, and an answer after {@link LINK_REQUEST_TTL_MS} are refused with
|
|
9
|
+
* {@link UnsolicitedJoinResponseError}, so a link no request asked for cannot hand the app an
|
|
10
|
+
* invitation.
|
|
11
|
+
*/
|
|
12
|
+
import type { DurableSlot } from '@serfab/cadre-core';
|
|
13
|
+
import { type CadreJoinResponse } from './join-response.js';
|
|
14
|
+
/** How long an open request takes an answer: long enough to unlock the owner app and confirm. */
|
|
15
|
+
export declare const LINK_REQUEST_TTL_MS: number;
|
|
16
|
+
/** The join request the app opened as a link and has not taken an answer to. */
|
|
17
|
+
export interface OpenLinkRequest {
|
|
18
|
+
/** The request's `reply.nonce`; the answer must carry it. */
|
|
19
|
+
nonce: string;
|
|
20
|
+
/** The owner app opened (iOS), for the app's messages; null when the Android chooser picked it. */
|
|
21
|
+
ownerAppName: string | null;
|
|
22
|
+
startedAtMs: number;
|
|
23
|
+
}
|
|
24
|
+
/** An answer that is not to the request this app has open: none is open, it expired, it was answered already, or the nonce differs. */
|
|
25
|
+
export declare class UnsolicitedJoinResponseError extends Error {
|
|
26
|
+
constructor(message: string);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create one per app, over `PhoneNode.linkRequestSlot()`: its calls run one at a time, so two
|
|
30
|
+
* deliveries of the same answer (the launch URL and a `url` event) cannot both take it.
|
|
31
|
+
*/
|
|
32
|
+
export declare class LinkRequestStore {
|
|
33
|
+
private readonly slot;
|
|
34
|
+
private readonly now;
|
|
35
|
+
private turn;
|
|
36
|
+
constructor(slot: DurableSlot, now?: () => number);
|
|
37
|
+
/** Open a request, or return the open one unchanged (same nonce) so an answer to an earlier tap still lands. */
|
|
38
|
+
begin(ownerAppName: string | null): Promise<OpenLinkRequest>;
|
|
39
|
+
/** The open request, or null when none is open or it is older than {@link LINK_REQUEST_TTL_MS}. A slot read that fails throws. */
|
|
40
|
+
current(): Promise<OpenLinkRequest | null>;
|
|
41
|
+
cancel(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* The decoded response when its nonce is the open request's, clearing the request; else throws
|
|
44
|
+
* {@link UnsolicitedJoinResponseError}. A malformed response throws the codec's error and leaves
|
|
45
|
+
* the request open; a slot read that fails throws, rather than reading as no request.
|
|
46
|
+
*/
|
|
47
|
+
take(responseText: string): Promise<CadreJoinResponse>;
|
|
48
|
+
private inTurn;
|
|
49
|
+
private readOpen;
|
|
50
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { toString as uint8ArrayToString } from 'uint8arrays';
|
|
2
|
+
import { decodeCadreJoinResponse } from './join-response.js';
|
|
3
|
+
/** How long an open request takes an answer: long enough to unlock the owner app and confirm. */
|
|
4
|
+
export const LINK_REQUEST_TTL_MS = 30 * 60_000;
|
|
5
|
+
/** The slot's text while no request is open: the slot cannot be deleted, only written. */
|
|
6
|
+
const NO_OPEN_REQUEST = '';
|
|
7
|
+
/** Random bytes in a nonce: 128 bits, 22 base64url characters. */
|
|
8
|
+
const NONCE_BYTES = 16;
|
|
9
|
+
/** An answer that is not to the request this app has open: none is open, it expired, it was answered already, or the nonce differs. */
|
|
10
|
+
export class UnsolicitedJoinResponseError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'UnsolicitedJoinResponseError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create one per app, over `PhoneNode.linkRequestSlot()`: its calls run one at a time, so two
|
|
18
|
+
* deliveries of the same answer (the launch URL and a `url` event) cannot both take it.
|
|
19
|
+
*/
|
|
20
|
+
export class LinkRequestStore {
|
|
21
|
+
slot;
|
|
22
|
+
now;
|
|
23
|
+
turn = Promise.resolve();
|
|
24
|
+
constructor(slot, now = Date.now) {
|
|
25
|
+
this.slot = slot;
|
|
26
|
+
this.now = now;
|
|
27
|
+
}
|
|
28
|
+
/** Open a request, or return the open one unchanged (same nonce) so an answer to an earlier tap still lands. */
|
|
29
|
+
begin(ownerAppName) {
|
|
30
|
+
return this.inTurn(async () => {
|
|
31
|
+
const open = await this.readOpen();
|
|
32
|
+
if (open)
|
|
33
|
+
return open;
|
|
34
|
+
const request = { nonce: newLinkNonce(), ownerAppName, startedAtMs: this.now() };
|
|
35
|
+
await this.slot.save(JSON.stringify(request));
|
|
36
|
+
return request;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** The open request, or null when none is open or it is older than {@link LINK_REQUEST_TTL_MS}. A slot read that fails throws. */
|
|
40
|
+
current() {
|
|
41
|
+
return this.inTurn(() => this.readOpen());
|
|
42
|
+
}
|
|
43
|
+
cancel() {
|
|
44
|
+
return this.inTurn(() => this.slot.save(NO_OPEN_REQUEST));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The decoded response when its nonce is the open request's, clearing the request; else throws
|
|
48
|
+
* {@link UnsolicitedJoinResponseError}. A malformed response throws the codec's error and leaves
|
|
49
|
+
* the request open; a slot read that fails throws, rather than reading as no request.
|
|
50
|
+
*/
|
|
51
|
+
take(responseText) {
|
|
52
|
+
return this.inTurn(async () => {
|
|
53
|
+
const response = decodeCadreJoinResponse(responseText);
|
|
54
|
+
const open = await this.readOpen();
|
|
55
|
+
if (!open) {
|
|
56
|
+
throw new UnsolicitedJoinResponseError('This answer is not to a join request this app has open: none is open, it expired, or it was answered already');
|
|
57
|
+
}
|
|
58
|
+
if (open.nonce !== response.nonce) {
|
|
59
|
+
throw new UnsolicitedJoinResponseError('This answer is to a different join request than the one this app has open');
|
|
60
|
+
}
|
|
61
|
+
await this.slot.save(NO_OPEN_REQUEST);
|
|
62
|
+
return response;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
inTurn(op) {
|
|
66
|
+
const result = this.turn.then(op);
|
|
67
|
+
this.turn = result.catch(() => undefined);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
async readOpen() {
|
|
71
|
+
const text = await this.slot.load();
|
|
72
|
+
if (text === undefined || text === NO_OPEN_REQUEST)
|
|
73
|
+
return null;
|
|
74
|
+
const request = parseOpenLinkRequest(text);
|
|
75
|
+
if (!request)
|
|
76
|
+
return null;
|
|
77
|
+
return this.now() - request.startedAtMs > LINK_REQUEST_TTL_MS ? null : request;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** The saved record, or null, logged, when it is not one: a record no answer can match is no open request. */
|
|
81
|
+
function parseOpenLinkRequest(text) {
|
|
82
|
+
let parsed;
|
|
83
|
+
try {
|
|
84
|
+
parsed = JSON.parse(text);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
console.warn('[cadre-client/app-link] the open link request record is not JSON; reading it as none:', err);
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const { nonce, ownerAppName, startedAtMs } = (typeof parsed === 'object' && parsed !== null ? parsed : {});
|
|
91
|
+
if (typeof nonce !== 'string' || (ownerAppName !== null && typeof ownerAppName !== 'string') || typeof startedAtMs !== 'number' || !Number.isFinite(startedAtMs)) {
|
|
92
|
+
console.warn('[cadre-client/app-link] the open link request record has the wrong shape; reading it as none:', text);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
return { nonce, ownerAppName, startedAtMs };
|
|
96
|
+
}
|
|
97
|
+
/** 16 random bytes, base64url: the form a join request's `reply.nonce` takes. */
|
|
98
|
+
function newLinkNonce() {
|
|
99
|
+
return uint8ArrayToString(crypto.getRandomValues(new Uint8Array(NONCE_BYTES)), 'base64url');
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=link-request-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"link-request-store.js","sourceRoot":"","sources":["../../src/app-link/link-request-store.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAC7D,OAAO,EAAE,uBAAuB,EAA0B,MAAM,oBAAoB,CAAC;AAErF,iGAAiG;AACjG,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,GAAG,MAAM,CAAC;AAE/C,0FAA0F;AAC1F,MAAM,eAAe,GAAG,EAAE,CAAC;AAE3B,kEAAkE;AAClE,MAAM,WAAW,GAAG,EAAE,CAAC;AAWvB,uIAAuI;AACvI,MAAM,OAAO,4BAA6B,SAAQ,KAAK;IACtD,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,8BAA8B,CAAC;IAC5C,CAAC;CACD;AAED;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IAIV;IACA;IAJV,IAAI,GAAqB,OAAO,CAAC,OAAO,EAAE,CAAC;IAEnD,YACkB,IAAiB,EACjB,MAAoB,IAAI,CAAC,GAAG;QAD5B,SAAI,GAAJ,IAAI,CAAa;QACjB,QAAG,GAAH,GAAG,CAAyB;IAC3C,CAAC;IAEJ,gHAAgH;IAChH,KAAK,CAAC,YAA2B;QAChC,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,IAAI,EAAE;YAC7B,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnC,IAAI,IAAI;gBAAE,OAAO,IAAI,CAAC;YACtB,MAAM,OAAO,GAAoB,EAAE,KAAK,EAAE,YAAY,EAAE,EAAE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C;IACb,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,CAAC;AAC7C,CAAC;AAED,iFAAiF;AACjF,SAAS,YAAY;IACpB,OAAO,kBAAkB,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type CadreJoinResponse } from './join-response.js';
|
|
2
|
+
import type { SereusApp } from './known-apps.js';
|
|
3
|
+
/**
|
|
4
|
+
* Longest answering link {@link joinResponseLink} builds. An invitation names at most six machine
|
|
5
|
+
* addresses, which puts a response link at about 4 KB by its shape (not measured); the limit
|
|
6
|
+
* guards against invitations growing past what an intent or a URL reliably carries.
|
|
7
|
+
*/
|
|
8
|
+
export declare const CADRE_LINK_MAX_LENGTH = 32768;
|
|
9
|
+
/**
|
|
10
|
+
* What an incoming URL carries: a join request to answer, the answer to this app's own, or the
|
|
11
|
+
* scheme of the app that recommended this one. That scheme is unverified: the app trusts it only
|
|
12
|
+
* when it is on its own known-apps list, and names that app from the list.
|
|
13
|
+
*/
|
|
14
|
+
export type CadreLink = {
|
|
15
|
+
kind: 'join-request';
|
|
16
|
+
text: string;
|
|
17
|
+
} | {
|
|
18
|
+
kind: 'join-response';
|
|
19
|
+
text: string;
|
|
20
|
+
} | {
|
|
21
|
+
kind: 'recommended-by';
|
|
22
|
+
text: string;
|
|
23
|
+
};
|
|
24
|
+
/** {@link joinResponseLink} refused to build a link longer than {@link CADRE_LINK_MAX_LENGTH}; show the invitation for copying instead. */
|
|
25
|
+
export declare class CadreLinkTooLongError extends Error {
|
|
26
|
+
readonly length: number;
|
|
27
|
+
constructor(length: number);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* What an incoming URL is, or null for anything else, a node code (`sereus-join:1.…`) and an
|
|
31
|
+
* invitation included, so an app's other link routing is untouched. Recognizes only the forms
|
|
32
|
+
* this module builds, under any scheme (a second install of an app has a scheme of its own).
|
|
33
|
+
* Returns the text undecoded: decoding, and refusing, is the caller's step.
|
|
34
|
+
*/
|
|
35
|
+
export declare function parseCadreLink(url: string): CadreLink | null;
|
|
36
|
+
/** Android (`ownerApp` null): the request text itself. iOS: `<ownerApp.scheme>://cadre-join-request/<text>`. */
|
|
37
|
+
export declare function joinRequestLink(requestText: string, ownerApp: SereusApp | null): string;
|
|
38
|
+
/**
|
|
39
|
+
* `<reply.scheme>://cadre-join-response/<response text>`. Checks the scheme and the nonce again
|
|
40
|
+
* (a web or system scheme throws), and throws {@link CadreLinkTooLongError} above
|
|
41
|
+
* {@link CADRE_LINK_MAX_LENGTH}.
|
|
42
|
+
*/
|
|
43
|
+
export declare function joinResponseLink(reply: {
|
|
44
|
+
scheme: string;
|
|
45
|
+
}, response: CadreJoinResponse): string;
|
|
46
|
+
/** `<app.scheme>://cadre-recommended-by/<ownScheme>`: opened at a recommended app already installed, it names the recommending app as the install referrer would. */
|
|
47
|
+
export declare function recommendedByLink(app: SereusApp, ownScheme: string): string;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The links two Sereus apps on one phone open each other with: the asking app's join request,
|
|
3
|
+
* and the owner app's answer opened back at the asking app's own scheme.
|
|
4
|
+
*
|
|
5
|
+
* - Android: the request text itself (`sereus-join-request:1.…`), which every opted-in Sereus app
|
|
6
|
+
* registers, so the system app chooser lists every installed owner app.
|
|
7
|
+
* - iOS: `<owner scheme>://cadre-join-request/<request text>`, one owner app chosen by its scheme.
|
|
8
|
+
* - The answer, both platforms: `<reply scheme>://cadre-join-response/<response text>`. The owner
|
|
9
|
+
* app builds it from the request's `reply.scheme`; it never opens a URL a request supplied.
|
|
10
|
+
* - A recommendation, both platforms: `<recommended app's scheme>://cadre-recommended-by/<own scheme>`,
|
|
11
|
+
* opened by an owner app at a recommended app that is already installed.
|
|
12
|
+
*/
|
|
13
|
+
import { CADRE_JOIN_REQUEST_PREFIX, requireCadreJoinReply } from '@serfab/cadre-core';
|
|
14
|
+
import { encodeCadreJoinResponse } from './join-response.js';
|
|
15
|
+
/**
|
|
16
|
+
* Longest answering link {@link joinResponseLink} builds. An invitation names at most six machine
|
|
17
|
+
* addresses, which puts a response link at about 4 KB by its shape (not measured); the limit
|
|
18
|
+
* guards against invitations growing past what an intent or a URL reliably carries.
|
|
19
|
+
*/
|
|
20
|
+
export const CADRE_LINK_MAX_LENGTH = 32_768;
|
|
21
|
+
const REQUEST_PATH = 'cadre-join-request';
|
|
22
|
+
const RESPONSE_PATH = 'cadre-join-response';
|
|
23
|
+
const RECOMMENDED_BY_PATH = 'cadre-recommended-by';
|
|
24
|
+
/** `<scheme>://<path>/<text>` for any scheme, where the path names the link's kind. */
|
|
25
|
+
const SCHEME_LINK = new RegExp(`^[a-z][a-z0-9+.-]*://(${REQUEST_PATH}|${RESPONSE_PATH}|${RECOMMENDED_BY_PATH})/(.+)$`, 'i');
|
|
26
|
+
const KIND_BY_PATH = {
|
|
27
|
+
[REQUEST_PATH]: 'join-request',
|
|
28
|
+
[RESPONSE_PATH]: 'join-response',
|
|
29
|
+
[RECOMMENDED_BY_PATH]: 'recommended-by',
|
|
30
|
+
};
|
|
31
|
+
/** {@link joinResponseLink} refused to build a link longer than {@link CADRE_LINK_MAX_LENGTH}; show the invitation for copying instead. */
|
|
32
|
+
export class CadreLinkTooLongError extends Error {
|
|
33
|
+
length;
|
|
34
|
+
constructor(length) {
|
|
35
|
+
super(`A join response link of ${length} characters is longer than ${CADRE_LINK_MAX_LENGTH}`);
|
|
36
|
+
this.length = length;
|
|
37
|
+
this.name = 'CadreLinkTooLongError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* What an incoming URL is, or null for anything else, a node code (`sereus-join:1.…`) and an
|
|
42
|
+
* invitation included, so an app's other link routing is untouched. Recognizes only the forms
|
|
43
|
+
* this module builds, under any scheme (a second install of an app has a scheme of its own).
|
|
44
|
+
* Returns the text undecoded: decoding, and refusing, is the caller's step.
|
|
45
|
+
*/
|
|
46
|
+
export function parseCadreLink(url) {
|
|
47
|
+
const trimmed = url.trim();
|
|
48
|
+
if (trimmed.startsWith(CADRE_JOIN_REQUEST_PREFIX))
|
|
49
|
+
return { kind: 'join-request', text: trimmed };
|
|
50
|
+
const match = SCHEME_LINK.exec(trimmed);
|
|
51
|
+
if (!match)
|
|
52
|
+
return null;
|
|
53
|
+
const [, path, text] = match;
|
|
54
|
+
return { kind: KIND_BY_PATH[path.toLowerCase()], text };
|
|
55
|
+
}
|
|
56
|
+
/** Android (`ownerApp` null): the request text itself. iOS: `<ownerApp.scheme>://cadre-join-request/<text>`. */
|
|
57
|
+
export function joinRequestLink(requestText, ownerApp) {
|
|
58
|
+
return ownerApp ? `${ownerApp.scheme}://${REQUEST_PATH}/${requestText}` : requestText;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* `<reply.scheme>://cadre-join-response/<response text>`. Checks the scheme and the nonce again
|
|
62
|
+
* (a web or system scheme throws), and throws {@link CadreLinkTooLongError} above
|
|
63
|
+
* {@link CADRE_LINK_MAX_LENGTH}.
|
|
64
|
+
*/
|
|
65
|
+
export function joinResponseLink(reply, response) {
|
|
66
|
+
const { scheme } = requireCadreJoinReply({ scheme: reply.scheme, nonce: response.nonce });
|
|
67
|
+
const link = `${scheme}://${RESPONSE_PATH}/${encodeCadreJoinResponse(response)}`;
|
|
68
|
+
if (link.length > CADRE_LINK_MAX_LENGTH)
|
|
69
|
+
throw new CadreLinkTooLongError(link.length);
|
|
70
|
+
return link;
|
|
71
|
+
}
|
|
72
|
+
/** `<app.scheme>://cadre-recommended-by/<ownScheme>`: opened at a recommended app already installed, it names the recommending app as the install referrer would. */
|
|
73
|
+
export function recommendedByLink(app, ownScheme) {
|
|
74
|
+
return `${app.scheme}://${RECOMMENDED_BY_PATH}/${ownScheme}`;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=links.js.map
|
|
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,CAAC,CAAC;IACxC,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IACxB,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,KAAK,CAAC;IAC7B,OAAO,EAAE,IAAI,EAAE,YAAY,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC;AACzD,CAAC;AAED,gHAAgH;AAChH,MAAM,UAAU,eAAe,CAAC,WAAmB,EAAE,QAA0B;IAC9E,OAAO,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,MAAM,MAAM,YAAY,IAAI,WAAW,EAAE,CAAC,CAAC,CAAC,WAAW,CAAC;AACvF,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAyB,EAAE,QAA2B;IACtF,MAAM,EAAE,MAAM,EAAE,GAAG,qBAAqB,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAC1F,MAAM,IAAI,GAAG,GAAG,MAAM,MAAM,aAAa,IAAI,uBAAuB,CAAC,QAAQ,CAAC,EAAE,CAAC;IACjF,IAAI,IAAI,CAAC,MAAM,GAAG,qBAAqB;QAAE,MAAM,IAAI,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACtF,OAAO,IAAI,CAAC;AACb,CAAC;AAED,qKAAqK;AACrK,MAAM,UAAU,iBAAiB,CAAC,GAAc,EAAE,SAAiB;IAClE,OAAO,GAAG,GAAG,CAAC,MAAM,MAAM,mBAAmB,IAAI,SAAS,EAAE,CAAC;AAC9D,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an owner app opens to recommend a Sereus app the user does not have yet: the app's store
|
|
3
|
+
* page, which on Android names the recommending app in the install referrer. The recommended
|
|
4
|
+
* app reads the referrer at first launch (`recommendedBySchemeFromReferrer`) to name the app that
|
|
5
|
+
* recommended it and open it directly. For an app already installed the owner app opens
|
|
6
|
+
* `recommendedByLink` (links.ts) instead.
|
|
7
|
+
*
|
|
8
|
+
* The scheme a referrer or a link carries is a hint, never trusted: anyone can build a Play link
|
|
9
|
+
* with any referrer. The recommended app drops a scheme that is not on its own known-apps list,
|
|
10
|
+
* and names the app from that list.
|
|
11
|
+
*/
|
|
12
|
+
import type { SereusApp } from './known-apps.js';
|
|
13
|
+
/** The install referrer an owner app's Play link carries, followed by the owner app's own scheme. */
|
|
14
|
+
export declare const RECOMMENDED_BY_REFERRER_PREFIX = "sereus-recommended-by:1.";
|
|
15
|
+
/** The platforms with an app store a link can open; the app passes `Platform.OS`. */
|
|
16
|
+
export type StorePlatform = 'android' | 'ios';
|
|
17
|
+
/** `sereus-recommended-by:1.<ownScheme>`: the install referrer naming the recommending app. */
|
|
18
|
+
export declare function recommendedByReferrer(ownScheme: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* The recommending app's scheme when the install referrer is exactly the token an owner app's
|
|
21
|
+
* store link carries, and null otherwise: an install no Sereus link started reads
|
|
22
|
+
* `utm_source=google-play&utm_medium=organic`. Unverified: see the module comment.
|
|
23
|
+
*/
|
|
24
|
+
export declare function recommendedBySchemeFromReferrer(referrer: string): string | null;
|
|
25
|
+
/**
|
|
26
|
+
* The app's store page, or null when the app has no store id for `platform`. Android: its Play
|
|
27
|
+
* page, with the install referrer naming `ownScheme`. iOS: its App Store page, with nothing about
|
|
28
|
+
* the recommending app, since iOS has no install referrer.
|
|
29
|
+
*/
|
|
30
|
+
export declare function storePageLink(app: SereusApp, platform: StorePlatform, ownScheme: string): string | null;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** The install referrer an owner app's Play link carries, followed by the owner app's own scheme. */
|
|
2
|
+
export const RECOMMENDED_BY_REFERRER_PREFIX = 'sereus-recommended-by:1.';
|
|
3
|
+
/** `sereus-recommended-by:1.<ownScheme>`: the install referrer naming the recommending app. */
|
|
4
|
+
export function recommendedByReferrer(ownScheme) {
|
|
5
|
+
return `${RECOMMENDED_BY_REFERRER_PREFIX}${ownScheme}`;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The recommending app's scheme when the install referrer is exactly the token an owner app's
|
|
9
|
+
* store link carries, and null otherwise: an install no Sereus link started reads
|
|
10
|
+
* `utm_source=google-play&utm_medium=organic`. Unverified: see the module comment.
|
|
11
|
+
*/
|
|
12
|
+
export function recommendedBySchemeFromReferrer(referrer) {
|
|
13
|
+
const trimmed = referrer.trim();
|
|
14
|
+
if (!trimmed.startsWith(RECOMMENDED_BY_REFERRER_PREFIX))
|
|
15
|
+
return null;
|
|
16
|
+
const scheme = trimmed.slice(RECOMMENDED_BY_REFERRER_PREFIX.length);
|
|
17
|
+
return scheme.length > 0 ? scheme : null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The app's store page, or null when the app has no store id for `platform`. Android: its Play
|
|
21
|
+
* page, with the install referrer naming `ownScheme`. iOS: its App Store page, with nothing about
|
|
22
|
+
* the recommending app, since iOS has no install referrer.
|
|
23
|
+
*/
|
|
24
|
+
export function storePageLink(app, platform, ownScheme) {
|
|
25
|
+
if (platform === 'android') {
|
|
26
|
+
if (!app.androidPackage)
|
|
27
|
+
return null;
|
|
28
|
+
const referrer = encodeURIComponent(recommendedByReferrer(ownScheme));
|
|
29
|
+
return `https://play.google.com/store/apps/details?id=${encodeURIComponent(app.androidPackage)}&referrer=${referrer}`;
|
|
30
|
+
}
|
|
31
|
+
return app.iosAppStoreId ? `https://apps.apple.com/app/id${app.iosAppStoreId}` : null;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=recommend-links.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recommend-links.js","sourceRoot":"","sources":["../../src/app-link/recommend-links.ts"],"names":[],"mappings":"AAaA,qGAAqG;AACrG,MAAM,CAAC,MAAM,8BAA8B,GAAG,0BAA0B,CAAC;AAKzE,+FAA+F;AAC/F,MAAM,UAAU,qBAAqB,CAAC,SAAiB;IACtD,OAAO,GAAG,8BAA8B,GAAG,SAAS,EAAE,CAAC;AACxD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,+BAA+B,CAAC,QAAgB;IAC/D,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC;IAChC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,8BAA8B,CAAC;QAAE,OAAO,IAAI,CAAC;IACrE,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,8BAA8B,CAAC,MAAM,CAAC,CAAC;IACpE,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC;AAC1C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,GAAc,EAAE,QAAuB,EAAE,SAAiB;IACvF,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC5B,IAAI,CAAC,GAAG,CAAC,cAAc;YAAE,OAAO,IAAI,CAAC;QACrC,MAAM,QAAQ,GAAG,kBAAkB,CAAC,qBAAqB,CAAC,SAAS,CAAC,CAAC,CAAC;QACtE,OAAO,iDAAiD,kBAAkB,CAAC,GAAG,CAAC,cAAc,CAAC,aAAa,QAAQ,EAAE,CAAC;IACvH,CAAC;IACD,OAAO,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,gCAAgC,GAAG,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACvF,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `AppRecommendationStore`: the owner app's record that the user allowed another Sereus app to
|
|
3
|
+
* join before that app asked. When the recommended app's join request opened as a link arrives
|
|
4
|
+
* with that app's scheme as its `reply.scheme`, the owner app takes the pre-approval and answers
|
|
5
|
+
* with a targeted invitation without asking again (docs/cadre/apps.md → "Recommending another
|
|
6
|
+
* app").
|
|
7
|
+
*
|
|
8
|
+
* A pre-approval is the security contract: it is used up by the one answer it allows, lasts
|
|
9
|
+
* {@link RECOMMENDATION_TTL_MS}, and is never returned for a party other than the one the user
|
|
10
|
+
* was in when they allowed it. It admits a machine only: the invitation it answers with grants no
|
|
11
|
+
* ownership, and an app with strands to bring joins with a separately redeemed owner grant
|
|
12
|
+
* (whether a link join may also carry ownership is
|
|
13
|
+
* `tickets/blocked/decide-app-link-join-carries-ownership.md`).
|
|
14
|
+
*/
|
|
15
|
+
import type { DurableSlot } from '@serfab/cadre-core';
|
|
16
|
+
/** How long a pre-approval waits for the recommended app's request: enough to install it from the store and open it. */
|
|
17
|
+
export declare const RECOMMENDATION_TTL_MS: number;
|
|
18
|
+
/** The user allowed the app at `scheme` to join `partyId` before it asked. */
|
|
19
|
+
export interface AppRecommendation {
|
|
20
|
+
/** The recommended app's own URL scheme: what its request's `reply.scheme` must be. */
|
|
21
|
+
scheme: string;
|
|
22
|
+
/** The name the owner app showed the user, from its known-apps list, never from a request. */
|
|
23
|
+
appName: string;
|
|
24
|
+
/** The party the owner app was in when the user allowed it. */
|
|
25
|
+
partyId: string;
|
|
26
|
+
approvedAtMs: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create one per app, over `PhoneNode.recommendationSlot()`: its calls run one at a time, so two
|
|
30
|
+
* deliveries of the same request (the launch URL and a `url` event) cannot both take the
|
|
31
|
+
* pre-approval.
|
|
32
|
+
*/
|
|
33
|
+
export declare class AppRecommendationStore {
|
|
34
|
+
private readonly slot;
|
|
35
|
+
private readonly now;
|
|
36
|
+
private turn;
|
|
37
|
+
constructor(slot: DurableSlot, now?: () => number);
|
|
38
|
+
/** Record a pre-approval, replacing any for the same scheme. */
|
|
39
|
+
approve(entry: Omit<AppRecommendation, 'approvedAtMs'>): Promise<AppRecommendation>;
|
|
40
|
+
/** The live pre-approvals for `partyId`, most recently approved first, for the owner app's list. A slot read that fails throws. */
|
|
41
|
+
live(partyId: string): Promise<AppRecommendation[]>;
|
|
42
|
+
/** Drop the pre-approval for `scheme`, if any. */
|
|
43
|
+
withdraw(scheme: string): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* The live pre-approval for `scheme` in `partyId`, removed as it is returned; null when there is
|
|
46
|
+
* none. Removed even when the caller then fails to mint, so it allows one answer at most.
|
|
47
|
+
*/
|
|
48
|
+
take(scheme: string, partyId: string): Promise<AppRecommendation | null>;
|
|
49
|
+
private inTurn;
|
|
50
|
+
/** The unexpired entries for `partyId`: what every write keeps, so expired and other parties' entries are pruned by it. */
|
|
51
|
+
private readLive;
|
|
52
|
+
private readUnexpired;
|
|
53
|
+
private write;
|
|
54
|
+
}
|