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,428 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import type { ITpmSecretCodec } from './classes.tpmsecretcodec.js';
|
|
3
|
+
import { isPushVendorReason } from './interfaces.push.js';
|
|
4
|
+
import type { TDevicePushPlatform } from '../ts_interfaces/devices.js';
|
|
5
|
+
import type { IPushApnsStatus, IPushFcmStatus, TPushCredentialState } from '../ts_interfaces/push.js';
|
|
6
|
+
import {
|
|
7
|
+
isApnsSigningKey,
|
|
8
|
+
isAppleId,
|
|
9
|
+
isBundleId,
|
|
10
|
+
isFirebaseProjectId,
|
|
11
|
+
isServiceAccountEmail,
|
|
12
|
+
isServiceAccountKeyId,
|
|
13
|
+
isServiceAccountSigningKey,
|
|
14
|
+
privateKeyBytes,
|
|
15
|
+
privateKeyFromBytes,
|
|
16
|
+
type IApnsCredentialInput,
|
|
17
|
+
type IFcmCredentialInput,
|
|
18
|
+
} from './functions.pushcredentials.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The host's push settings (AGL-1639): one document per controller in `agl_controller_push_settings`, `{ id, content }`.
|
|
22
|
+
* The top level is fixed for good (the exact client refuses a stored top-level field its model does not declare);
|
|
23
|
+
* `content` is a plain object this release reads key by key and writes back with every key it does not know, so a
|
|
24
|
+
* document a later release wrote keeps what that release added. This release reads `previews` (off unless true), and
|
|
25
|
+
* `apns` and `fcm`: what the credential is for, its key TPM2-sealed (`TpmSecretCodec`) under a name bound to the
|
|
26
|
+
* controller, the platform and the credential's generation, when it was set, and `refused` once Apple or Google
|
|
27
|
+
* refused it. The running controller is the only writer: `agl push` reaches it over its private channel.
|
|
28
|
+
*/
|
|
29
|
+
export interface IControllerPushSettingsDocument {
|
|
30
|
+
/** `controller:<port>`. */
|
|
31
|
+
id: string;
|
|
32
|
+
content: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A key as the store holds it: sealed, with the generation its seal name binds. */
|
|
36
|
+
export interface IStoredPushKey {
|
|
37
|
+
generation: number;
|
|
38
|
+
ciphertext: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Apple or Google refused the credential: nothing is sent on the platform until the owner sets it again. */
|
|
42
|
+
export interface IStoredPushRefusal {
|
|
43
|
+
at: number;
|
|
44
|
+
/** The push service's code for the refusal, when it named one. */
|
|
45
|
+
vendorReason?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface IStoredApnsCredential {
|
|
49
|
+
keyId: string;
|
|
50
|
+
teamId: string;
|
|
51
|
+
topic: string;
|
|
52
|
+
key: IStoredPushKey;
|
|
53
|
+
setAt: number;
|
|
54
|
+
refused?: IStoredPushRefusal;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface IStoredFcmCredential {
|
|
58
|
+
projectId: string;
|
|
59
|
+
clientEmail: string;
|
|
60
|
+
privateKeyId?: string;
|
|
61
|
+
key: IStoredPushKey;
|
|
62
|
+
setAt: number;
|
|
63
|
+
refused?: IStoredPushRefusal;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The stored document's `content` is bounded plain JSON. */
|
|
67
|
+
const maximumContentBytes = 64 * 1024;
|
|
68
|
+
|
|
69
|
+
const isPlainObject = (valueArg: unknown): valueArg is Record<string, unknown> => (
|
|
70
|
+
!!valueArg && typeof valueArg === 'object' && !Array.isArray(valueArg)
|
|
71
|
+
&& (Object.getPrototypeOf(valueArg) === Object.prototype || Object.getPrototypeOf(valueArg) === null)
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const assertDocument: (valueArg: unknown) => asserts valueArg is IControllerPushSettingsDocument = (valueArg) => {
|
|
75
|
+
const value = valueArg as Partial<IControllerPushSettingsDocument> | null;
|
|
76
|
+
let size = Number.POSITIVE_INFINITY;
|
|
77
|
+
try { size = Buffer.byteLength(JSON.stringify(value?.content), 'utf8'); } catch { /* refused below */ }
|
|
78
|
+
if (!isPlainObject(value)
|
|
79
|
+
|| Object.keys(value).length !== 2
|
|
80
|
+
|| typeof value.id !== 'string' || !/^controller:[1-9][0-9]{0,4}$/u.test(value.id)
|
|
81
|
+
|| !isPlainObject(value.content)
|
|
82
|
+
|| size > maximumContentBytes) {
|
|
83
|
+
throw new Error('Invalid push settings record.');
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
@plugins.nosqldb.managed({ collectionName: 'agl_controller_push_settings' })
|
|
88
|
+
@plugins.nosqldb.exactPersistence({ assertDocument })
|
|
89
|
+
export class ControllerPushSettingsModel extends plugins.nosqldb.SmartDataDbDoc<ControllerPushSettingsModel, IControllerPushSettingsDocument> {
|
|
90
|
+
declare static exact: plugins.nosqldb.TExact<ControllerPushSettingsModel>;
|
|
91
|
+
@plugins.nosqldb.unI() public id!: string;
|
|
92
|
+
@plugins.nosqldb.svDb() public content!: Record<string, unknown>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const isTime = (valueArg: unknown): valueArg is number => Number.isSafeInteger(valueArg) && (valueArg as number) > 0;
|
|
96
|
+
|
|
97
|
+
const isStoredKey = (valueArg: unknown): valueArg is IStoredPushKey => (
|
|
98
|
+
isPlainObject(valueArg)
|
|
99
|
+
&& Number.isSafeInteger(valueArg.generation) && (valueArg.generation as number) >= 1
|
|
100
|
+
&& typeof valueArg.ciphertext === 'string' && /^[A-Za-z0-9_-]{1,131072}$/u.test(valueArg.ciphertext)
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const isRefusal = (valueArg: unknown): valueArg is IStoredPushRefusal => (
|
|
104
|
+
isPlainObject(valueArg) && isTime(valueArg.at) && (valueArg.vendorReason === undefined || isPushVendorReason(valueArg.vendorReason))
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
/** A stored entry this release reads: its own keys checked, any other key a later release's. */
|
|
108
|
+
const readApnsEntry = (valueArg: unknown): IStoredApnsCredential | undefined => (
|
|
109
|
+
isPlainObject(valueArg) && isAppleId(valueArg.keyId) && isAppleId(valueArg.teamId) && isBundleId(valueArg.topic)
|
|
110
|
+
&& isStoredKey(valueArg.key) && isTime(valueArg.setAt) && (valueArg.refused === undefined || isRefusal(valueArg.refused))
|
|
111
|
+
? valueArg as unknown as IStoredApnsCredential : undefined
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const readFcmEntry = (valueArg: unknown): IStoredFcmCredential | undefined => (
|
|
115
|
+
isPlainObject(valueArg) && isFirebaseProjectId(valueArg.projectId) && isServiceAccountEmail(valueArg.clientEmail)
|
|
116
|
+
&& (valueArg.privateKeyId === undefined || isServiceAccountKeyId(valueArg.privateKeyId))
|
|
117
|
+
&& isStoredKey(valueArg.key) && isTime(valueArg.setAt) && (valueArg.refused === undefined || isRefusal(valueArg.refused))
|
|
118
|
+
? valueArg as unknown as IStoredFcmCredential : undefined
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
/** The APNs credential a sender signs with. */
|
|
122
|
+
export interface IApnsCredential {
|
|
123
|
+
keyId: string;
|
|
124
|
+
teamId: string;
|
|
125
|
+
topic: string;
|
|
126
|
+
key: plugins.crypto.KeyObject;
|
|
127
|
+
/** The stored generation: a refusal names it, so a credential set meanwhile is not the one refused. */
|
|
128
|
+
generation: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The FCM credential a sender signs with. */
|
|
132
|
+
export interface IFcmCredential {
|
|
133
|
+
projectId: string;
|
|
134
|
+
clientEmail: string;
|
|
135
|
+
privateKeyId?: string;
|
|
136
|
+
key: plugins.crypto.KeyObject;
|
|
137
|
+
generation: number;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface IHeldPlatform<TStored, TCredential> {
|
|
141
|
+
state: TPushCredentialState;
|
|
142
|
+
stored?: TStored;
|
|
143
|
+
/** Present exactly while `state` is `ready`. */
|
|
144
|
+
credential?: TCredential;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface IControllerPushSettingsOptions {
|
|
148
|
+
/** `controller:<port>`. */
|
|
149
|
+
controllerId: string;
|
|
150
|
+
/** The controller database's issuer id: a key is sealed under a name bound to it. */
|
|
151
|
+
issuerId: string;
|
|
152
|
+
codec: ITpmSecretCodec;
|
|
153
|
+
now?: () => number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const readRecord = (controllerIdArg: string) => ControllerPushSettingsModel.exact.findStoredOne({ id: controllerIdArg });
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The push settings of one running controller: what it read at start and every change the owner makes, in one order.
|
|
160
|
+
* The keys exist in plaintext only in this process, as key objects; the store holds them sealed. A change of a
|
|
161
|
+
* platform's credential reaches the listeners (the delivery service drops its sender for the platform).
|
|
162
|
+
*/
|
|
163
|
+
export class ControllerPushSettings {
|
|
164
|
+
private previewsOn = false;
|
|
165
|
+
private apnsHeld: IHeldPlatform<IStoredApnsCredential, IApnsCredential> = { state: 'unset' };
|
|
166
|
+
private fcmHeld: IHeldPlatform<IStoredFcmCredential, IFcmCredential> = { state: 'unset' };
|
|
167
|
+
/**
|
|
168
|
+
* The newest generation each platform's credential had in this process, also after `unset`: the next one is always
|
|
169
|
+
* higher, so a refusal of an earlier credential that arrives late never names the one set since.
|
|
170
|
+
*/
|
|
171
|
+
private readonly generations: Record<TDevicePushPlatform, number> = { apns: 0, fcm: 0 };
|
|
172
|
+
private readonly listeners = new Set<(platformArg: TDevicePushPlatform) => void>();
|
|
173
|
+
private mutationTail: Promise<unknown> = Promise.resolve();
|
|
174
|
+
private closed = false;
|
|
175
|
+
|
|
176
|
+
private constructor(private readonly options: IControllerPushSettingsOptions) {}
|
|
177
|
+
|
|
178
|
+
/** Reads the controller's settings and unseals each credential that was not refused. */
|
|
179
|
+
public static async open(optionsArg: IControllerPushSettingsOptions): Promise<ControllerPushSettings> {
|
|
180
|
+
const settings = new ControllerPushSettings(optionsArg);
|
|
181
|
+
const stored = await readRecord(optionsArg.controllerId);
|
|
182
|
+
const content = stored ? ControllerPushSettingsModel.exact.toPersisted(stored).content : {};
|
|
183
|
+
settings.previewsOn = content.previews === true;
|
|
184
|
+
settings.apnsHeld = await settings.holdApns(content.apns);
|
|
185
|
+
settings.fcmHeld = await settings.holdFcm(content.fcm);
|
|
186
|
+
// An entry this release cannot read still names its generation: the next credential's seal name is not that one's.
|
|
187
|
+
for (const platform of ['apns', 'fcm'] as const) {
|
|
188
|
+
const entry = content[platform];
|
|
189
|
+
const generation = isPlainObject(entry) && isPlainObject(entry.key) ? entry.key.generation : undefined;
|
|
190
|
+
if (Number.isSafeInteger(generation) && (generation as number) > 0) settings.generations[platform] = generation as number;
|
|
191
|
+
}
|
|
192
|
+
return settings;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private now(): number {
|
|
196
|
+
return this.options.now?.() ?? Date.now();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Whether notifications carry previews. */
|
|
200
|
+
public previews(): boolean {
|
|
201
|
+
return this.previewsOn;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** What `agl push status` shows of the APNs credential, without the key. */
|
|
205
|
+
public apnsStatus(): Omit<IPushApnsStatus, 'registrations'> {
|
|
206
|
+
const { state, stored } = this.apnsHeld;
|
|
207
|
+
return {
|
|
208
|
+
state,
|
|
209
|
+
...(stored === undefined ? {} : { keyId: stored.keyId, teamId: stored.teamId, topic: stored.topic, setAt: stored.setAt }),
|
|
210
|
+
...(state === 'refused' && stored?.refused ? { refused: { at: stored.refused.at, reason: 'credential_refused' as const,
|
|
211
|
+
...(stored.refused.vendorReason === undefined ? {} : { vendorReason: stored.refused.vendorReason }) } } : {}),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** What `agl push status` shows of the FCM credential, without the key. */
|
|
216
|
+
public fcmStatus(): Omit<IPushFcmStatus, 'registrations'> {
|
|
217
|
+
const { state, stored } = this.fcmHeld;
|
|
218
|
+
return {
|
|
219
|
+
state,
|
|
220
|
+
...(stored === undefined ? {} : { projectId: stored.projectId, clientEmail: stored.clientEmail, setAt: stored.setAt }),
|
|
221
|
+
...(state === 'refused' && stored?.refused ? { refused: { at: stored.refused.at, reason: 'credential_refused' as const,
|
|
222
|
+
...(stored.refused.vendorReason === undefined ? {} : { vendorReason: stored.refused.vendorReason }) } } : {}),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** The APNs credential to send with, while it is ready. */
|
|
227
|
+
public apnsCredential(): IApnsCredential | undefined {
|
|
228
|
+
return this.closed ? undefined : this.apnsHeld.credential;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The FCM credential to send with, while it is ready. */
|
|
232
|
+
public fcmCredential(): IFcmCredential | undefined {
|
|
233
|
+
return this.closed ? undefined : this.fcmHeld.credential;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Whether the host holds a credential for the platform that Apple or Google did not refuse. */
|
|
237
|
+
public ready(platformArg: TDevicePushPlatform): boolean {
|
|
238
|
+
return (platformArg === 'apns' ? this.apnsCredential() : this.fcmCredential()) !== undefined;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Calls `listenerArg` with the platform whenever its credential changes; answers the unsubscription. */
|
|
242
|
+
public onChange(listenerArg: (platformArg: TDevicePushPlatform) => void): () => void {
|
|
243
|
+
this.listeners.add(listenerArg);
|
|
244
|
+
return () => { this.listeners.delete(listenerArg); };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Seals and stores a new APNs credential, which replaces the one held (a refused one too). */
|
|
248
|
+
public setApns(inputArg: IApnsCredentialInput): Promise<boolean> {
|
|
249
|
+
return this.mutate(async () => {
|
|
250
|
+
const generation = this.nextGeneration('apns');
|
|
251
|
+
const key = await this.seal('apns', generation, inputArg.key);
|
|
252
|
+
const stored: IStoredApnsCredential = { keyId: inputArg.keyId, teamId: inputArg.teamId, topic: inputArg.topic, key, setAt: this.now() };
|
|
253
|
+
await this.write((contentArg) => { contentArg.apns = stored; });
|
|
254
|
+
this.apnsHeld = { state: 'ready', stored, credential: {
|
|
255
|
+
keyId: stored.keyId, teamId: stored.teamId, topic: stored.topic, key: inputArg.key, generation,
|
|
256
|
+
} };
|
|
257
|
+
this.changed('apns');
|
|
258
|
+
return true;
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Seals and stores a new FCM credential, which replaces the one held (a refused one too). */
|
|
263
|
+
public setFcm(inputArg: IFcmCredentialInput): Promise<boolean> {
|
|
264
|
+
return this.mutate(async () => {
|
|
265
|
+
const generation = this.nextGeneration('fcm');
|
|
266
|
+
const key = await this.seal('fcm', generation, inputArg.key);
|
|
267
|
+
const stored: IStoredFcmCredential = {
|
|
268
|
+
projectId: inputArg.projectId, clientEmail: inputArg.clientEmail,
|
|
269
|
+
...(inputArg.privateKeyId === undefined ? {} : { privateKeyId: inputArg.privateKeyId }), key, setAt: this.now(),
|
|
270
|
+
};
|
|
271
|
+
await this.write((contentArg) => { contentArg.fcm = stored; });
|
|
272
|
+
this.fcmHeld = { state: 'ready', stored, credential: {
|
|
273
|
+
projectId: stored.projectId, clientEmail: stored.clientEmail,
|
|
274
|
+
...(stored.privateKeyId === undefined ? {} : { privateKeyId: stored.privateKeyId }), key: inputArg.key, generation,
|
|
275
|
+
} };
|
|
276
|
+
this.changed('fcm');
|
|
277
|
+
return true;
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** Removes a platform's credential; answers whether the host held one (also an unreadable one). */
|
|
282
|
+
public unset(platformArg: TDevicePushPlatform): Promise<boolean> {
|
|
283
|
+
return this.mutate(async () => {
|
|
284
|
+
const current = await readRecord(this.options.controllerId);
|
|
285
|
+
const held = platformArg === 'apns' ? this.apnsHeld : this.fcmHeld;
|
|
286
|
+
if (!current || (!Object.hasOwn(ControllerPushSettingsModel.exact.toPersisted(current).content, platformArg) && held.state === 'unset')) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
await this.write((contentArg) => { delete contentArg[platformArg]; });
|
|
290
|
+
if (platformArg === 'apns') this.apnsHeld = { state: 'unset' };
|
|
291
|
+
else this.fcmHeld = { state: 'unset' };
|
|
292
|
+
this.changed(platformArg);
|
|
293
|
+
return true;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Turns previews on or off; answers whether that changed them. */
|
|
298
|
+
public setPreviews(previewsArg: boolean): Promise<boolean> {
|
|
299
|
+
return this.mutate(async () => {
|
|
300
|
+
if (this.previewsOn === previewsArg) return false;
|
|
301
|
+
await this.write((contentArg) => { contentArg.previews = previewsArg; });
|
|
302
|
+
this.previewsOn = previewsArg;
|
|
303
|
+
return true;
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Apple or Google refused generation `generationArg` of a platform's credential: it is kept as `refused`, and nothing
|
|
309
|
+
* is sent on the platform until the owner sets it again. A credential set since is not the one refused.
|
|
310
|
+
*/
|
|
311
|
+
public refuse(platformArg: TDevicePushPlatform, generationArg: number, vendorReasonArg?: string): Promise<boolean> {
|
|
312
|
+
return this.mutate(async () => {
|
|
313
|
+
const held = platformArg === 'apns' ? this.apnsHeld : this.fcmHeld;
|
|
314
|
+
if (held.state !== 'ready' || held.credential?.generation !== generationArg || !held.stored) return false;
|
|
315
|
+
const refused: IStoredPushRefusal = { at: this.now(), ...(isPushVendorReason(vendorReasonArg) ? { vendorReason: vendorReasonArg } : {}) };
|
|
316
|
+
const stored = { ...held.stored, refused };
|
|
317
|
+
await this.write((contentArg) => {
|
|
318
|
+
// What a later release added to the entry stays.
|
|
319
|
+
contentArg[platformArg] = { ...(isPlainObject(contentArg[platformArg]) ? contentArg[platformArg] : {}), ...stored };
|
|
320
|
+
});
|
|
321
|
+
if (platformArg === 'apns') this.apnsHeld = { state: 'refused', stored: stored as IStoredApnsCredential };
|
|
322
|
+
else this.fcmHeld = { state: 'refused', stored: stored as IStoredFcmCredential };
|
|
323
|
+
this.changed(platformArg);
|
|
324
|
+
return true;
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Forgets the keys; the record stays. Changes still running fail. */
|
|
329
|
+
public close(): void {
|
|
330
|
+
this.closed = true;
|
|
331
|
+
this.apnsHeld = { state: this.apnsHeld.state, ...(this.apnsHeld.stored ? { stored: this.apnsHeld.stored } : {}) };
|
|
332
|
+
this.fcmHeld = { state: this.fcmHeld.state, ...(this.fcmHeld.stored ? { stored: this.fcmHeld.stored } : {}) };
|
|
333
|
+
this.listeners.clear();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** A generation the platform's credential never had in this process, nor the stored one. */
|
|
337
|
+
private nextGeneration(platformArg: TDevicePushPlatform): number {
|
|
338
|
+
this.generations[platformArg] += 1;
|
|
339
|
+
return this.generations[platformArg];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private changed(platformArg: TDevicePushPlatform): void {
|
|
343
|
+
for (const listener of [...this.listeners]) listener(platformArg);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private sealName(platformArg: TDevicePushPlatform, generationArg: number): string {
|
|
347
|
+
return `agl-controller.${this.options.issuerId.slice(0, 22)}.${this.options.controllerId.replace(':', '-')}.push-${platformArg}.${generationArg}`;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private async seal(platformArg: TDevicePushPlatform, generationArg: number, keyArg: plugins.crypto.KeyObject): Promise<IStoredPushKey> {
|
|
351
|
+
const bytes = privateKeyBytes(keyArg);
|
|
352
|
+
try {
|
|
353
|
+
return { generation: generationArg, ciphertext: await this.options.codec.seal(this.sealName(platformArg, generationArg), bytes) };
|
|
354
|
+
} finally {
|
|
355
|
+
bytes.fill(0);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** The key of a stored credential, unsealed; none (`unreadable`) when this machine cannot unseal it. */
|
|
360
|
+
private async unseal(platformArg: TDevicePushPlatform, keyArg: IStoredPushKey): Promise<plugins.crypto.KeyObject | undefined> {
|
|
361
|
+
let bytes: Uint8Array | undefined;
|
|
362
|
+
try {
|
|
363
|
+
bytes = await this.options.codec.unseal(this.sealName(platformArg, keyArg.generation), keyArg.ciphertext);
|
|
364
|
+
return privateKeyFromBytes(bytes);
|
|
365
|
+
} catch {
|
|
366
|
+
return undefined;
|
|
367
|
+
} finally {
|
|
368
|
+
bytes?.fill(0);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
private async holdApns(valueArg: unknown): Promise<IHeldPlatform<IStoredApnsCredential, IApnsCredential>> {
|
|
373
|
+
if (valueArg === undefined) return { state: 'unset' };
|
|
374
|
+
const stored = readApnsEntry(valueArg);
|
|
375
|
+
if (!stored) return this.unreadable('APNs', 'apns');
|
|
376
|
+
if (stored.refused) return { state: 'refused', stored };
|
|
377
|
+
const key = await this.unseal('apns', stored.key);
|
|
378
|
+
if (!key || !isApnsSigningKey(key)) return { ...this.unreadable('APNs', 'apns'), stored };
|
|
379
|
+
return { state: 'ready', stored, credential: {
|
|
380
|
+
keyId: stored.keyId, teamId: stored.teamId, topic: stored.topic, key, generation: stored.key.generation,
|
|
381
|
+
} };
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private async holdFcm(valueArg: unknown): Promise<IHeldPlatform<IStoredFcmCredential, IFcmCredential>> {
|
|
385
|
+
if (valueArg === undefined) return { state: 'unset' };
|
|
386
|
+
const stored = readFcmEntry(valueArg);
|
|
387
|
+
if (!stored) return this.unreadable('FCM', 'fcm');
|
|
388
|
+
if (stored.refused) return { state: 'refused', stored };
|
|
389
|
+
const key = await this.unseal('fcm', stored.key);
|
|
390
|
+
if (!key || !isServiceAccountSigningKey(key)) return { ...this.unreadable('FCM', 'fcm'), stored };
|
|
391
|
+
return { state: 'ready', stored, credential: {
|
|
392
|
+
projectId: stored.projectId, clientEmail: stored.clientEmail,
|
|
393
|
+
...(stored.privateKeyId === undefined ? {} : { privateKeyId: stored.privateKeyId }), key, generation: stored.key.generation,
|
|
394
|
+
} };
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private unreadable(nameArg: string, platformArg: TDevicePushPlatform): { state: 'unreadable' } {
|
|
398
|
+
console.warn(`The ${nameArg} push credential of ${this.options.controllerId} cannot be read or unsealed on this machine, so `
|
|
399
|
+
+ `nothing is sent on it: set it again with \`agl push ${platformArg} set\`.`);
|
|
400
|
+
return { state: 'unreadable' };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Changes the stored `content` with `changeArg`, keeping every key this release does not know. */
|
|
404
|
+
private async write(changeArg: (contentArg: Record<string, unknown>) => void): Promise<void> {
|
|
405
|
+
const current = await readRecord(this.options.controllerId);
|
|
406
|
+
if (!current) {
|
|
407
|
+
const content: Record<string, unknown> = {};
|
|
408
|
+
changeArg(content);
|
|
409
|
+
const inserted = await ControllerPushSettingsModel.exact.insert({ id: this.options.controllerId, content });
|
|
410
|
+
if (inserted.status !== 'inserted') throw new Error('The push settings changed concurrently and were not stored.');
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const content = structuredClone(ControllerPushSettingsModel.exact.toPersisted(current).content);
|
|
414
|
+
changeArg(content);
|
|
415
|
+
const result = await ControllerPushSettingsModel.exact.transition({ current, change: (modelArg) => { modelArg.content = content; } });
|
|
416
|
+
if (result.status !== 'transitioned') throw new Error('The push settings changed concurrently and were not stored.');
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
private mutate<T>(operationArg: () => Promise<T>): Promise<T> {
|
|
420
|
+
if (this.closed) return Promise.reject(new Error('The push settings are closed.'));
|
|
421
|
+
const task = this.mutationTail.then(() => {
|
|
422
|
+
if (this.closed) throw new Error('The push settings are closed.');
|
|
423
|
+
return operationArg();
|
|
424
|
+
});
|
|
425
|
+
this.mutationTail = task.then(() => undefined, () => undefined);
|
|
426
|
+
return task;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
@@ -5,7 +5,9 @@ import {
|
|
|
5
5
|
type TTicketLink, type TTicketOperation, type TTicketRequest, type TTicketResponse,
|
|
6
6
|
type TTicketFleetEvent, type ITicketFleetEventResult, type ITicketComment, type TTicketData, type ITicketOpenQuestion, type TTicketAutoLabel,
|
|
7
7
|
type ITicketDetail, type ITicketGet, type ITicketAsk, type ITicketMaintenanceWindow, ticketHistoryPage,
|
|
8
|
+
type ITicketOverviewRequest, type ITicketPageRequest, ticketPageErrorMessages,
|
|
8
9
|
} from '../ts_interfaces/tickets.js';
|
|
10
|
+
import { TicketCursorError, ticketOverview, ticketPage } from './functions.ticketlisting.js';
|
|
9
11
|
import {
|
|
10
12
|
TicketModel, TicketBoardModel, TicketCommentModel, TicketActivityModel, TicketQuestionModel, TicketFleetEventModel,
|
|
11
13
|
} from './classes.ticketmodels.js';
|
|
@@ -62,7 +64,7 @@ interface ITicketTrack {
|
|
|
62
64
|
* lost (`concurrent_change`, nothing written), a transaction whose commit outcome is unknown (`outcome_unknown`), a
|
|
63
65
|
* fleet event key that names another request, and an owner capability the controller could not verify.
|
|
64
66
|
*/
|
|
65
|
-
export type TTicketErrorCode = keyof typeof ticketErrorMessages | 'concurrent_change' | 'outcome_unknown'
|
|
67
|
+
export type TTicketErrorCode = keyof typeof ticketErrorMessages | keyof typeof ticketPageErrorMessages | 'concurrent_change' | 'outcome_unknown'
|
|
66
68
|
| 'ticket_fleet_event_conflict' | 'unauthorized';
|
|
67
69
|
|
|
68
70
|
export class TicketError extends Error {
|
|
@@ -449,7 +451,7 @@ export class TicketStore {
|
|
|
449
451
|
public async execute<TMethod extends TTicketOperation>(
|
|
450
452
|
methodArg: TMethod, requestArg: TTicketRequest<TMethod>, actorArg: ITicketActor | undefined, signalArg?: AbortSignal,
|
|
451
453
|
): Promise<TTicketResponse<TMethod>> {
|
|
452
|
-
if (actorArg === undefined && methodArg !== 'list' && methodArg !== 'get') {
|
|
454
|
+
if (actorArg === undefined && methodArg !== 'list' && methodArg !== 'get' && methodArg !== 'page' && methodArg !== 'overview') {
|
|
453
455
|
throw new TicketError('ticket_identity_required', ticketErrorMessages.ticket_identity_required);
|
|
454
456
|
}
|
|
455
457
|
const actor = actorArg === undefined ? undefined : ticketActorSchema.parse(actorArg);
|
|
@@ -792,7 +794,7 @@ export class TicketStore {
|
|
|
792
794
|
}
|
|
793
795
|
|
|
794
796
|
/**
|
|
795
|
-
* Each operation reads what it decides on and nothing more: the listing operations read the whole board and every
|
|
797
|
+
* Each operation reads what it decides on and nothing more: the listing operations (list, board, search, page, overview) read the whole board and every
|
|
796
798
|
* open question; create the parent and the destination column's count; move the source and destination columns;
|
|
797
799
|
* update the ticket, its parent and, when the project changes, its children; every other operation one ticket, and
|
|
798
800
|
* get one page of its history or, with `commentId`, that one comment. The board document's revision transition in the caller orders every mutation.
|
|
@@ -800,6 +802,22 @@ export class TicketStore {
|
|
|
800
802
|
private async perform(methodArg: TTicketOperation, requestArg: TTicketRequest<TTicketOperation>, actorArg: ITicketActor | undefined,
|
|
801
803
|
sessionArg: TSession, nowArg: string, sequenceArg: number,
|
|
802
804
|
revisionArg: number, trackArg: ITicketTrack): Promise<unknown> {
|
|
805
|
+
if (methodArg === 'page' || methodArg === 'overview') {
|
|
806
|
+
// Reads what `list` reads; `functions.ticketlisting.ts` pages or summarizes it without another read.
|
|
807
|
+
const request = requestArg as ITicketPageRequest & ITicketOverviewRequest;
|
|
808
|
+
const all = await this.allTickets(sessionArg);
|
|
809
|
+
await this.countOpenQuestions(trackArg, sessionArg);
|
|
810
|
+
const matches = ticketFilter(methodArg === 'page' ? request : { mine: request.mine, project: request.project }, actorArg);
|
|
811
|
+
const tickets = all.map((recordArg) => this.visible(recordArg.ticket, actorArg, trackArg)).filter(matches);
|
|
812
|
+
const openQuestions = (ticketIdArg: string): number => trackArg.openQuestions.get(ticketIdArg) ?? 0;
|
|
813
|
+
try {
|
|
814
|
+
return methodArg === 'page' ? ticketPage(tickets, request, revisionArg, openQuestions, actorArg?.sessionId ?? actorArg?.id)
|
|
815
|
+
: ticketOverview(tickets, request, revisionArg, nowArg, openQuestions);
|
|
816
|
+
} catch (errorArg) {
|
|
817
|
+
if (errorArg instanceof TicketCursorError) throw new TicketError(errorArg.code, ticketPageErrorMessages[errorArg.code]);
|
|
818
|
+
throw errorArg;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
803
821
|
if (methodArg === 'list' || methodArg === 'board' || methodArg === 'search') {
|
|
804
822
|
const filters: ITicketFilters = methodArg === 'search'
|
|
805
823
|
? { text: (requestArg as TTicketRequest<'search'>).query, label: (requestArg as TTicketRequest<'search'>).label }
|