agl 43.7.0 → 43.9.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/dist_serve/bundle.js +871 -858
- package/dist_serve/bundle.js.map +2 -2
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/aglapi/aglapisurface.document.js +824 -6
- package/dist_ts/aglapi/interfaces.d.ts +43 -0
- package/dist_ts/aglapi/interfaces.js +11 -1
- package/dist_ts/classes.apnsclient.d.ts +97 -0
- package/dist_ts/classes.apnsclient.js +363 -0
- package/dist_ts/classes.authmodels.js +19 -6
- package/dist_ts/classes.authstore.js +15 -2
- package/dist_ts/classes.cli.js +8 -1
- package/dist_ts/classes.controller.d.ts +30 -3
- package/dist_ts/classes.controller.js +250 -9
- package/dist_ts/classes.controllerdevices.d.ts +35 -7
- package/dist_ts/classes.controllerdevices.js +86 -13
- package/dist_ts/classes.controllerpush.d.ts +111 -0
- package/dist_ts/classes.controllerpush.js +299 -0
- package/dist_ts/classes.fcmclient.d.ts +78 -0
- package/dist_ts/classes.fcmclient.js +278 -0
- package/dist_ts/classes.inbox.d.ts +10 -2
- package/dist_ts/classes.inbox.js +9 -2
- package/dist_ts/classes.mcpclient.d.ts +5 -0
- package/dist_ts/classes.mcpclient.js +20 -8
- package/dist_ts/classes.mcpcontrollerhost.d.ts +3 -0
- package/dist_ts/classes.mcpcontrollerhost.js +21 -2
- package/dist_ts/classes.mcptools.d.ts +1 -1
- package/dist_ts/classes.mcptools.js +2 -1
- package/dist_ts/classes.mcpvalidation.js +4 -1
- package/dist_ts/classes.mcpworkercaller.d.ts +6 -0
- package/dist_ts/classes.mcpworkercaller.js +26 -4
- package/dist_ts/classes.pushdelivery.d.ts +115 -0
- package/dist_ts/classes.pushdelivery.js +311 -0
- package/dist_ts/classes.pushhandover.d.ts +46 -0
- package/dist_ts/classes.pushhandover.js +131 -0
- package/dist_ts/classes.pushregistrations.d.ts +96 -0
- package/dist_ts/classes.pushregistrations.js +263 -0
- package/dist_ts/classes.pushsettings.d.ts +125 -0
- package/dist_ts/classes.pushsettings.js +377 -0
- package/dist_ts/classes.ticketstore.d.ts +3 -3
- package/dist_ts/classes.ticketstore.js +23 -4
- package/dist_ts/classes.workercallers.d.ts +73 -2
- package/dist_ts/classes.workercallers.js +354 -43
- package/dist_ts/driver/classes.directreports.d.ts +5 -0
- package/dist_ts/driver/classes.directreports.js +3 -2
- package/dist_ts/fleet/classes.workerlaunches.d.ts +6 -4
- package/dist_ts/fleet/classes.workerlaunches.js +10 -9
- package/dist_ts/fleet/classes.workers.d.ts +12 -2
- package/dist_ts/fleet/classes.workers.js +82 -2
- package/dist_ts/functions.deviceapi.d.ts +23 -3
- package/dist_ts/functions.deviceapi.js +43 -3
- package/dist_ts/functions.mcptickettools.d.ts +60 -0
- package/dist_ts/functions.mcptickettools.js +4 -3
- package/dist_ts/functions.pushcli.d.ts +43 -0
- package/dist_ts/functions.pushcli.js +201 -0
- package/dist_ts/functions.pushcredentials.d.ts +53 -0
- package/dist_ts/functions.pushcredentials.js +111 -0
- package/dist_ts/functions.pushwire.d.ts +8 -0
- package/dist_ts/functions.pushwire.js +83 -0
- package/dist_ts/functions.remotecli.d.ts +2 -2
- package/dist_ts/functions.remotecli.js +4 -4
- package/dist_ts/functions.ticketcli.d.ts +4 -2
- package/dist_ts/functions.ticketcli.js +70 -4
- package/dist_ts/functions.ticketlisting.d.ts +38 -0
- package/dist_ts/functions.ticketlisting.js +211 -0
- package/dist_ts/functions.ticketvalidation.d.ts +62 -0
- package/dist_ts/functions.ticketvalidation.js +41 -3
- package/dist_ts/functions.workercallervalidation.js +3 -2
- package/dist_ts/functions.workerprocessproof.d.ts +12 -0
- package/dist_ts/functions.workerprocessproof.js +32 -1
- package/dist_ts/interfaces.auth.d.ts +27 -3
- package/dist_ts/interfaces.auth.js +9 -1
- package/dist_ts/interfaces.lifecycle.d.ts +7 -1
- package/dist_ts/interfaces.push.d.ts +69 -0
- package/dist_ts/interfaces.push.js +25 -0
- package/dist_ts/plugins.d.ts +3 -1
- package/dist_ts/plugins.js +4 -2
- package/dist_ts_interfaces/devices.d.ts +178 -2
- package/dist_ts_interfaces/devices.js +24 -2
- package/dist_ts_interfaces/index.d.ts +1 -0
- package/dist_ts_interfaces/index.js +2 -1
- package/dist_ts_interfaces/mcprequests.d.ts +3 -2
- package/dist_ts_interfaces/mcprequests.js +3 -1
- package/dist_ts_interfaces/push.d.ts +166 -0
- package/dist_ts_interfaces/push.js +24 -0
- package/dist_ts_interfaces/requests.tickets.d.ts +31 -3
- package/dist_ts_interfaces/requests.tickets.js +2 -2
- package/dist_ts_interfaces/tickets.d.ts +174 -2
- package/dist_ts_interfaces/tickets.js +20 -2
- package/dist_ts_interfaces/workercallers.d.ts +43 -3
- package/package.json +2 -2
- package/readme.md +95 -14
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/aglapi/aglapisurface.document.ts +823 -5
- package/ts/aglapi/interfaces.ts +10 -0
- package/ts/classes.apnsclient.ts +389 -0
- package/ts/classes.authmodels.ts +21 -5
- package/ts/classes.authstore.ts +14 -1
- package/ts/classes.cli.ts +7 -0
- package/ts/classes.controller.ts +273 -7
- package/ts/classes.controllerdevices.ts +94 -12
- package/ts/classes.controllerpush.ts +351 -0
- package/ts/classes.fcmclient.ts +292 -0
- package/ts/classes.inbox.ts +13 -2
- package/ts/classes.mcpclient.ts +22 -7
- package/ts/classes.mcpcontrollerhost.ts +22 -1
- package/ts/classes.mcptools.ts +1 -0
- package/ts/classes.mcpvalidation.ts +2 -0
- package/ts/classes.mcpworkercaller.ts +24 -2
- package/ts/classes.pushdelivery.ts +378 -0
- package/ts/classes.pushhandover.ts +98 -0
- package/ts/classes.pushregistrations.ts +244 -0
- package/ts/classes.pushsettings.ts +428 -0
- package/ts/classes.ticketstore.ts +21 -3
- package/ts/classes.workercallers.ts +329 -37
- package/ts/driver/classes.directreports.ts +7 -1
- package/ts/fleet/classes.workerlaunches.ts +9 -7
- package/ts/fleet/classes.workers.ts +65 -2
- package/ts/functions.deviceapi.ts +54 -2
- package/ts/functions.mcptickettools.ts +3 -2
- package/ts/functions.pushcli.ts +212 -0
- package/ts/functions.pushcredentials.ts +129 -0
- package/ts/functions.pushwire.ts +106 -0
- package/ts/functions.remotecli.ts +3 -2
- package/ts/functions.ticketcli.ts +70 -5
- package/ts/functions.ticketlisting.ts +214 -0
- package/ts/functions.ticketvalidation.ts +41 -2
- package/ts/functions.workercallervalidation.ts +2 -1
- package/ts/functions.workerprocessproof.ts +34 -0
- package/ts/interfaces.auth.ts +26 -3
- package/ts/interfaces.lifecycle.ts +7 -1
- package/ts/interfaces.push.ts +78 -0
- package/ts/plugins.ts +3 -1
- package/ts_interfaces/devices.schema.json +327 -0
- package/ts_interfaces/devices.ts +206 -1
- package/ts_interfaces/index.ts +1 -0
- package/ts_interfaces/mcprequests.ts +3 -0
- package/ts_interfaces/push.ts +162 -0
- package/ts_interfaces/requests.tickets.ts +32 -2
- package/ts_interfaces/tickets.ts +136 -3
- package/ts_interfaces/workercallers.ts +34 -2
- package/ts_web/00_commitinfo_data.ts +1 -1
- package/ts_web/elements.inbox.ts +50 -9
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The push trigger's clean-stop handover (AGL-1639): the Inbox items it announced that still waited when the controller
|
|
5
|
+
* stopped cleanly, and the pairs the handover it started from named that it had not met yet, by id and `since`, so the
|
|
6
|
+
* next start does not alert again for the same waits. One document per controller in `agl_controller_push_handover`,
|
|
7
|
+
* `{ id, announced }`, its top level fixed for good. `ControllerPush.stop` writes both in it, whole, once (an announced
|
|
8
|
+
* pair wins over a handed-over one with the same id); nothing is written while the controller runs. The next start reads
|
|
9
|
+
* it once and deletes it, so a controller killed later leaves none and its next start announces what waits once more
|
|
10
|
+
* under the same keys. An older controller has no model for the collection and never reads it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** One announced item: its Inbox id and the `since` it was announced with. */
|
|
14
|
+
export interface IPushHandoverEntry {
|
|
15
|
+
id: string;
|
|
16
|
+
since: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface IControllerPushHandoverDocument {
|
|
20
|
+
/** `controller:<port>`. */
|
|
21
|
+
id: string;
|
|
22
|
+
/** Newest `since` first. */
|
|
23
|
+
announced: IPushHandoverEntry[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* At most `entries` items are handed over (the Inbox lists at most 200), the newest by `since` when there are more; an
|
|
28
|
+
* item whose id is longer than `idBytes` UTF-8 bytes is left out and announced again. A document is read with up to
|
|
29
|
+
* `readEntries` entries, so one a later release wrote with more is still read and deleted.
|
|
30
|
+
*/
|
|
31
|
+
export const pushHandoverLimits = { entries: 200, idBytes: 1024, readEntries: 1000, documentBytes: 2 * 1024 * 1024 } as const;
|
|
32
|
+
|
|
33
|
+
const isPlainObject = (valueArg: unknown): valueArg is Record<string, unknown> => (
|
|
34
|
+
!!valueArg && typeof valueArg === 'object' && !Array.isArray(valueArg)
|
|
35
|
+
&& (Object.getPrototypeOf(valueArg) === Object.prototype || Object.getPrototypeOf(valueArg) === null)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const assertDocument: (valueArg: unknown) => asserts valueArg is IControllerPushHandoverDocument = (valueArg) => {
|
|
39
|
+
const value = valueArg as Partial<IControllerPushHandoverDocument> | null;
|
|
40
|
+
let size = Number.POSITIVE_INFINITY;
|
|
41
|
+
try { size = Buffer.byteLength(JSON.stringify(value?.announced), 'utf8'); } catch { /* refused below */ }
|
|
42
|
+
if (!isPlainObject(value)
|
|
43
|
+
|| Object.keys(value).length !== 2
|
|
44
|
+
|| typeof value.id !== 'string' || !/^controller:[1-9][0-9]{0,4}$/u.test(value.id)
|
|
45
|
+
|| !Array.isArray(value.announced) || value.announced.length > pushHandoverLimits.readEntries
|
|
46
|
+
|| !value.announced.every(isPlainObject)
|
|
47
|
+
|| size > pushHandoverLimits.documentBytes) {
|
|
48
|
+
throw new Error('Invalid push handover record.');
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
@plugins.nosqldb.managed({ collectionName: 'agl_controller_push_handover' })
|
|
53
|
+
@plugins.nosqldb.exactPersistence({ assertDocument })
|
|
54
|
+
export class ControllerPushHandoverModel extends plugins.nosqldb.SmartDataDbDoc<ControllerPushHandoverModel, IControllerPushHandoverDocument> {
|
|
55
|
+
declare static exact: plugins.nosqldb.TExact<ControllerPushHandoverModel>;
|
|
56
|
+
@plugins.nosqldb.unI() public id!: string;
|
|
57
|
+
@plugins.nosqldb.svDb() public announced!: IPushHandoverEntry[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const isEntry = (valueArg: unknown): valueArg is IPushHandoverEntry => (
|
|
61
|
+
isPlainObject(valueArg) && typeof valueArg.id === 'string' && valueArg.id !== ''
|
|
62
|
+
&& Buffer.byteLength(valueArg.id, 'utf8') <= pushHandoverLimits.idBytes
|
|
63
|
+
&& typeof valueArg.since === 'number' && Number.isFinite(valueArg.since)
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
/** What a stop hands over of `announcedArg` (item id to `since`): at most 200, newest `since` first, then by id. */
|
|
67
|
+
export const pushHandoverEntries = (announcedArg: ReadonlyMap<string, number>): IPushHandoverEntry[] => (
|
|
68
|
+
[...announcedArg].map(([id, since]) => ({ id, since })).filter(isEntry)
|
|
69
|
+
.sort((leftArg, rightArg) => rightArg.since - leftArg.since || (leftArg.id < rightArg.id ? -1 : leftArg.id > rightArg.id ? 1 : 0))
|
|
70
|
+
.slice(0, pushHandoverLimits.entries)
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Reads the controller's handover and deletes it: what the last clean stop announced, item id to `since`, or none when
|
|
75
|
+
* no clean stop left one. Throws when the document does not read or could not be deleted; it is then not used.
|
|
76
|
+
*/
|
|
77
|
+
export const takePushHandover = async (controllerIdArg: string): Promise<ReadonlyMap<string, number> | undefined> => {
|
|
78
|
+
const current = await ControllerPushHandoverModel.exact.findStoredOne({ id: controllerIdArg });
|
|
79
|
+
if (!current) return undefined;
|
|
80
|
+
const { announced } = ControllerPushHandoverModel.exact.toPersisted(current);
|
|
81
|
+
const deleted = await ControllerPushHandoverModel.exact.delete({ current });
|
|
82
|
+
if (deleted.status !== 'deleted') throw new Error('The push handover changed while it was read and was not used.');
|
|
83
|
+
// An entry a later release wrote in another shape is not one of these, and is announced again.
|
|
84
|
+
return new Map(announced.filter(isEntry).map((entryArg) => [entryArg.id, entryArg.since]));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** Writes the controller's handover whole: what `announcedArg` names (`pushHandoverEntries`), replacing any there. */
|
|
88
|
+
export const keepPushHandover = async (controllerIdArg: string, announcedArg: ReadonlyMap<string, number>): Promise<void> => {
|
|
89
|
+
const announced = pushHandoverEntries(announcedArg);
|
|
90
|
+
const current = await ControllerPushHandoverModel.exact.findStoredOne({ id: controllerIdArg });
|
|
91
|
+
if (!current) {
|
|
92
|
+
const inserted = await ControllerPushHandoverModel.exact.insert({ id: controllerIdArg, announced });
|
|
93
|
+
if (inserted.status !== 'inserted') throw new Error('The push handover changed concurrently and was not stored.');
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const result = await ControllerPushHandoverModel.exact.transition({ current, change: (modelArg) => { modelArg.announced = announced; } });
|
|
97
|
+
if (result.status !== 'transitioned') throw new Error('The push handover changed concurrently and was not stored.');
|
|
98
|
+
};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import type { TDevicePushEnvironment, TDevicePushPlatform } from '../ts_interfaces/devices.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A paired device's registration for notifications (AGL-1639): one document per device, its id the device's. It is
|
|
6
|
+
* its own collection, never a field of the device record, which is exact: an earlier release would refuse a device
|
|
7
|
+
* record that carried one. The token is secret-like: it leaves the controller only to its push service, never in an
|
|
8
|
+
* answer, event, audit entry or log.
|
|
9
|
+
*/
|
|
10
|
+
export interface IControllerPushRegistrationDocument {
|
|
11
|
+
/** The device's id. */
|
|
12
|
+
id: string;
|
|
13
|
+
platform: TDevicePushPlatform;
|
|
14
|
+
/** APNs only: the environment the token belongs to. */
|
|
15
|
+
environment?: TDevicePushEnvironment;
|
|
16
|
+
/** The push service's token for the device. */
|
|
17
|
+
token: string;
|
|
18
|
+
/** Whether the device asked for direct report results too. */
|
|
19
|
+
reports: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* 32 random bytes, base64url: the key a notification's collapse key is derived under (HMAC over the item's id). It is
|
|
22
|
+
* made with the registration and kept while it stands, also when the device registers again with another token, so
|
|
23
|
+
* a notification's key stays the same across the host's restarts and the app's registrations.
|
|
24
|
+
*/
|
|
25
|
+
salt: string;
|
|
26
|
+
/** Unix milliseconds the registration's platform, token or choices were last set. */
|
|
27
|
+
registeredAt: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** What a registration says without its token or salt. */
|
|
31
|
+
export type TControllerPushRegistrationState = Pick<IControllerPushRegistrationDocument, 'platform' | 'reports'>;
|
|
32
|
+
|
|
33
|
+
/** What a device registers: the registration without its id, salt and time. */
|
|
34
|
+
export type TControllerPushRegistrationRequest = Pick<IControllerPushRegistrationDocument, 'platform' | 'environment' | 'token' | 'reports'>;
|
|
35
|
+
|
|
36
|
+
const deviceIdPattern = /^[A-Za-z0-9_-]{24}$/u;
|
|
37
|
+
const saltPattern = /^[A-Za-z0-9_-]{43}$/u;
|
|
38
|
+
const pushEnvironments: readonly unknown[] = ['sandbox', 'production'] satisfies readonly TDevicePushEnvironment[];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A push token as its platform hands it to the app: an APNs device token as lowercase hex of 32 to 100 bytes (Apple
|
|
42
|
+
* states no fixed size), an FCM registration token as 1 to 1024 characters of letters, digits, `_`, `-` and `:`.
|
|
43
|
+
*/
|
|
44
|
+
export const isDevicePushToken = (platformArg: TDevicePushPlatform, valueArg: unknown): valueArg is string => (
|
|
45
|
+
typeof valueArg === 'string' && (platformArg === 'apns'
|
|
46
|
+
? /^(?:[0-9a-f]{2}){32,100}$/u.test(valueArg)
|
|
47
|
+
: /^[A-Za-z0-9_:-]{1,1024}$/u.test(valueArg))
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A stored token as the record holds it: 1 to 4096 characters without a control character. Never the request's token
|
|
52
|
+
* pattern (`isDevicePushToken`), which guesses at the push services' alphabets: a token a later release took outside
|
|
53
|
+
* it must never make this one refuse the record, which would stop the start's sweep.
|
|
54
|
+
*/
|
|
55
|
+
const isStoredPushToken = (valueArg: unknown): valueArg is string => (
|
|
56
|
+
typeof valueArg === 'string' && valueArg.length > 0 && valueArg.length <= 4096 && !/\p{Cc}/u.test(valueArg)
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const isTimestamp = (valueArg: unknown): valueArg is number => Number.isSafeInteger(valueArg) && (valueArg as number) > 0;
|
|
60
|
+
|
|
61
|
+
const assertDocument: (valueArg: unknown) => asserts valueArg is IControllerPushRegistrationDocument = (valueArg) => {
|
|
62
|
+
const value = valueArg as Partial<IControllerPushRegistrationDocument> | null;
|
|
63
|
+
const keys = value && typeof value === 'object' && !Array.isArray(value) ? Object.keys(value) : [];
|
|
64
|
+
const expected = ['id', 'platform', 'token', 'reports', 'salt', 'registeredAt', ...(value?.platform === 'apns' ? ['environment'] : [])];
|
|
65
|
+
if (
|
|
66
|
+
!value
|
|
67
|
+
|| keys.length !== expected.length
|
|
68
|
+
|| !expected.every((keyArg) => Object.hasOwn(value, keyArg))
|
|
69
|
+
|| typeof value.id !== 'string' || !deviceIdPattern.test(value.id)
|
|
70
|
+
|| (value.platform !== 'apns' && value.platform !== 'fcm')
|
|
71
|
+
|| (value.platform === 'apns' && !pushEnvironments.includes(value.environment))
|
|
72
|
+
|| !isStoredPushToken(value.token)
|
|
73
|
+
|| typeof value.reports !== 'boolean'
|
|
74
|
+
|| typeof value.salt !== 'string' || !saltPattern.test(value.salt)
|
|
75
|
+
|| !isTimestamp(value.registeredAt)
|
|
76
|
+
) {
|
|
77
|
+
// Never the value: the document holds a token.
|
|
78
|
+
throw new Error('Invalid push registration record.');
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
@plugins.nosqldb.managed({ collectionName: 'agl_controller_push_registrations' })
|
|
83
|
+
@plugins.nosqldb.exactPersistence({ assertDocument })
|
|
84
|
+
export class ControllerPushRegistrationModel extends plugins.nosqldb.SmartDataDbDoc<
|
|
85
|
+
ControllerPushRegistrationModel,
|
|
86
|
+
IControllerPushRegistrationDocument
|
|
87
|
+
> {
|
|
88
|
+
declare static exact: plugins.nosqldb.TExact<ControllerPushRegistrationModel>;
|
|
89
|
+
@plugins.nosqldb.unI() public id!: string;
|
|
90
|
+
@plugins.nosqldb.svDb() public platform!: TDevicePushPlatform;
|
|
91
|
+
@plugins.nosqldb.svDb() public environment?: TDevicePushEnvironment;
|
|
92
|
+
@plugins.nosqldb.svDb() public token!: string;
|
|
93
|
+
@plugins.nosqldb.svDb() public reports!: boolean;
|
|
94
|
+
@plugins.nosqldb.svDb() public salt!: string;
|
|
95
|
+
@plugins.nosqldb.svDb() public registeredAt!: number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const stateOf = (documentArg: IControllerPushRegistrationDocument): TControllerPushRegistrationState => ({
|
|
99
|
+
platform: documentArg.platform,
|
|
100
|
+
reports: documentArg.reports,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
/** A device's registration, without its token or salt; none while it holds none. */
|
|
104
|
+
export const readPushRegistration = async (deviceIdArg: string): Promise<TControllerPushRegistrationState | undefined> => {
|
|
105
|
+
const stored = await ControllerPushRegistrationModel.exact.findStoredOne({ id: deviceIdArg });
|
|
106
|
+
return stored === null ? undefined : stateOf(ControllerPushRegistrationModel.exact.toPersisted(stored));
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Sets a device's registration: a new one gets a fresh salt, an existing one takes the request's platform, token and
|
|
111
|
+
* choices and keeps its salt; the same request again writes nothing. The caller runs it after the device's record and
|
|
112
|
+
* in the one order of the device's writes, so a revoked device is never registered again.
|
|
113
|
+
*/
|
|
114
|
+
export const writePushRegistration = async (
|
|
115
|
+
deviceIdArg: string,
|
|
116
|
+
requestArg: TControllerPushRegistrationRequest,
|
|
117
|
+
nowArg: number,
|
|
118
|
+
): Promise<TControllerPushRegistrationState> => {
|
|
119
|
+
const content = {
|
|
120
|
+
platform: requestArg.platform,
|
|
121
|
+
...(requestArg.platform === 'apns' ? { environment: requestArg.environment } : {}),
|
|
122
|
+
token: requestArg.token,
|
|
123
|
+
reports: requestArg.reports,
|
|
124
|
+
};
|
|
125
|
+
const current = await ControllerPushRegistrationModel.exact.findStoredOne({ id: deviceIdArg });
|
|
126
|
+
if (current === null) {
|
|
127
|
+
const inserted = await ControllerPushRegistrationModel.exact.insert({
|
|
128
|
+
id: deviceIdArg, ...content, salt: plugins.crypto.randomBytes(32).toString('base64url'), registeredAt: nowArg,
|
|
129
|
+
});
|
|
130
|
+
if (inserted.status !== 'inserted') throw new Error('The push registration changed concurrently and was not stored.');
|
|
131
|
+
return stateOf(ControllerPushRegistrationModel.exact.toPersisted(inserted.document));
|
|
132
|
+
}
|
|
133
|
+
const stored = ControllerPushRegistrationModel.exact.toPersisted(current);
|
|
134
|
+
if (stored.platform === content.platform && stored.environment === content.environment && stored.token === content.token
|
|
135
|
+
&& stored.reports === content.reports) {
|
|
136
|
+
return stateOf(stored);
|
|
137
|
+
}
|
|
138
|
+
const result = await ControllerPushRegistrationModel.exact.transition({ current, change: (modelArg) => {
|
|
139
|
+
modelArg.platform = content.platform;
|
|
140
|
+
if (content.environment === undefined) delete modelArg.environment;
|
|
141
|
+
else modelArg.environment = content.environment;
|
|
142
|
+
modelArg.token = content.token;
|
|
143
|
+
modelArg.reports = content.reports;
|
|
144
|
+
modelArg.registeredAt = nowArg;
|
|
145
|
+
} });
|
|
146
|
+
if (result.status !== 'transitioned') throw new Error('The push registration changed concurrently and was not stored.');
|
|
147
|
+
return stateOf(ControllerPushRegistrationModel.exact.toPersisted(result.document));
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Deletes a device's registration; answers whether there was one. `optionsArg.session` runs it inside the caller's
|
|
152
|
+
* transaction (a revocation, or an origin move that removes the device).
|
|
153
|
+
*/
|
|
154
|
+
export const deletePushRegistration = async (
|
|
155
|
+
deviceIdArg: string,
|
|
156
|
+
optionsArg: plugins.nosqldb.IExactSessionOptions = {},
|
|
157
|
+
): Promise<boolean> => {
|
|
158
|
+
const current = await ControllerPushRegistrationModel.exact.findStoredOne({ id: deviceIdArg }, optionsArg);
|
|
159
|
+
if (current === null) return false;
|
|
160
|
+
const result = await ControllerPushRegistrationModel.exact.delete({ current }, optionsArg);
|
|
161
|
+
if (result.status !== 'deleted' && await ControllerPushRegistrationModel.exact.findStoredOne({ id: deviceIdArg }, optionsArg)) {
|
|
162
|
+
throw new Error('A push registration could not be removed.');
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Deletes every registration whose device is gone: one left behind by a write that failed half-way or by an earlier
|
|
169
|
+
* release that revoked a device without knowing registrations. Answers how many it deleted.
|
|
170
|
+
*/
|
|
171
|
+
export const sweepPushRegistrations = async (pairedArg: (deviceIdArg: string) => Promise<boolean>): Promise<number> => {
|
|
172
|
+
let deleted = 0;
|
|
173
|
+
for (const current of await ControllerPushRegistrationModel.exact.findStored()) {
|
|
174
|
+
const { id } = ControllerPushRegistrationModel.exact.toPersisted(current);
|
|
175
|
+
if (await pairedArg(id)) continue;
|
|
176
|
+
const result = await ControllerPushRegistrationModel.exact.delete({ current });
|
|
177
|
+
if (result.status === 'deleted') deleted += 1;
|
|
178
|
+
else if (await ControllerPushRegistrationModel.exact.findStoredOne({ id })) {
|
|
179
|
+
throw new Error('A push registration of a removed device could not be removed.');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return deleted;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/** How many of `deviceIdsArg` hold a registration, per platform (`agl push status`). */
|
|
186
|
+
export const countPushRegistrations = async (deviceIdsArg: ReadonlySet<string>): Promise<Record<TDevicePushPlatform, number>> => {
|
|
187
|
+
const counts: Record<TDevicePushPlatform, number> = { apns: 0, fcm: 0 };
|
|
188
|
+
for (const current of await ControllerPushRegistrationModel.exact.findStored()) {
|
|
189
|
+
const { id, platform } = ControllerPushRegistrationModel.exact.toPersisted(current);
|
|
190
|
+
if (deviceIdsArg.has(id)) counts[platform] += 1;
|
|
191
|
+
}
|
|
192
|
+
return counts;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/** A registration as the delivery sends to it, with its token: it never leaves the host but to its push service. */
|
|
196
|
+
export interface IControllerPushTarget {
|
|
197
|
+
deviceId: string;
|
|
198
|
+
platform: TDevicePushPlatform;
|
|
199
|
+
environment?: TDevicePushEnvironment;
|
|
200
|
+
token: string;
|
|
201
|
+
/** Whether the device asked for direct report results too. */
|
|
202
|
+
reports: boolean;
|
|
203
|
+
/** The key a notification's key is made under (`pushNotificationKey`). */
|
|
204
|
+
salt: string;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The registrations of `deviceIdsArg`, with their tokens, in the order of the ids. */
|
|
208
|
+
export const readPushTargets = async (deviceIdsArg: readonly string[]): Promise<IControllerPushTarget[]> => {
|
|
209
|
+
const targets: IControllerPushTarget[] = [];
|
|
210
|
+
for (const deviceId of deviceIdsArg) {
|
|
211
|
+
const stored = await ControllerPushRegistrationModel.exact.findStoredOne({ id: deviceId });
|
|
212
|
+
if (stored === null) continue;
|
|
213
|
+
const { platform, environment, token, reports, salt } = ControllerPushRegistrationModel.exact.toPersisted(stored);
|
|
214
|
+
targets.push({ deviceId, platform, ...(environment === undefined ? {} : { environment }), token, reports, salt });
|
|
215
|
+
}
|
|
216
|
+
return targets;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Deletes a device's registration if it still holds `tokenArg`: its push service called that token gone. A registration
|
|
221
|
+
* the device renewed with another token meanwhile stays. Answers whether it deleted one.
|
|
222
|
+
*/
|
|
223
|
+
export const deletePushRegistrationHolding = async (deviceIdArg: string, tokenArg: string): Promise<boolean> => {
|
|
224
|
+
const current = await ControllerPushRegistrationModel.exact.findStoredOne({ id: deviceIdArg });
|
|
225
|
+
if (current === null || ControllerPushRegistrationModel.exact.toPersisted(current).token !== tokenArg) return false;
|
|
226
|
+
const result = await ControllerPushRegistrationModel.exact.delete({ current });
|
|
227
|
+
return result.status === 'deleted';
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* What a notification's key is made over besides an Inbox item's id: never an item's id, which holds no control
|
|
232
|
+
* character, so the key of the owner's test notification, of the summary and of a direct report's result never equals
|
|
233
|
+
* an item's. A report's key is over its name: a newer result of the same report replaces the older one.
|
|
234
|
+
*/
|
|
235
|
+
export const pushKeySubjects = {
|
|
236
|
+
test: '\u0000test',
|
|
237
|
+
summary: '\u0000summary',
|
|
238
|
+
report: (nameArg: string): string => `\u0000report\u0000${nameArg}`,
|
|
239
|
+
} as const;
|
|
240
|
+
|
|
241
|
+
/** A notification's key: base64url of HMAC-SHA256 under the registration's salt over `subjectArg` (an item's id). */
|
|
242
|
+
export const pushNotificationKey = (saltArg: string, subjectArg: string): string => (
|
|
243
|
+
plugins.crypto.createHmac('sha256', Buffer.from(saltArg, 'base64url')).update(subjectArg, 'utf8').digest('base64url')
|
|
244
|
+
);
|