@vllnt/convex-push 0.0.0-stage → 0.1.0-canary.4a1ca91
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 +41 -0
- package/LICENSE +21 -0
- package/README.md +415 -2
- package/dist/client/index.d.ts +180 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +152 -0
- package/dist/client/index.js.map +1 -0
- package/dist/component/_generated/api.d.ts +80 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +274 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +142 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/actions.d.ts +29 -0
- package/dist/component/actions.d.ts.map +1 -0
- package/dist/component/actions.js +29 -0
- package/dist/component/actions.js.map +1 -0
- package/dist/component/checks.d.ts +27 -0
- package/dist/component/checks.d.ts.map +1 -0
- package/dist/component/checks.js +107 -0
- package/dist/component/checks.js.map +1 -0
- package/dist/component/convex.config.d.ts +18 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +30 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +8 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +15 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/crypto/codec.d.ts +64 -0
- package/dist/component/crypto/codec.d.ts.map +1 -0
- package/dist/component/crypto/codec.js +102 -0
- package/dist/component/crypto/codec.js.map +1 -0
- package/dist/component/crypto/jwt.d.ts +46 -0
- package/dist/component/crypto/jwt.d.ts.map +1 -0
- package/dist/component/crypto/jwt.js +82 -0
- package/dist/component/crypto/jwt.js.map +1 -0
- package/dist/component/crypto/token_seal.d.ts +31 -0
- package/dist/component/crypto/token_seal.d.ts.map +1 -0
- package/dist/component/crypto/token_seal.js +44 -0
- package/dist/component/crypto/token_seal.js.map +1 -0
- package/dist/component/crypto/web_push_crypto.d.ts +42 -0
- package/dist/component/crypto/web_push_crypto.d.ts.map +1 -0
- package/dist/component/crypto/web_push_crypto.js +49 -0
- package/dist/component/crypto/web_push_crypto.js.map +1 -0
- package/dist/component/internal_mutations.d.ts +48 -0
- package/dist/component/internal_mutations.d.ts.map +1 -0
- package/dist/component/internal_mutations.js +82 -0
- package/dist/component/internal_mutations.js.map +1 -0
- package/dist/component/mutations.d.ts +223 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +244 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/providers/apns.d.ts +46 -0
- package/dist/component/providers/apns.d.ts.map +1 -0
- package/dist/component/providers/apns.js +105 -0
- package/dist/component/providers/apns.js.map +1 -0
- package/dist/component/providers/fcm.d.ts +36 -0
- package/dist/component/providers/fcm.d.ts.map +1 -0
- package/dist/component/providers/fcm.js +116 -0
- package/dist/component/providers/fcm.js.map +1 -0
- package/dist/component/providers/http.d.ts +45 -0
- package/dist/component/providers/http.d.ts.map +1 -0
- package/dist/component/providers/http.js +74 -0
- package/dist/component/providers/http.js.map +1 -0
- package/dist/component/providers/keys.d.ts +70 -0
- package/dist/component/providers/keys.d.ts.map +1 -0
- package/dist/component/providers/keys.js +89 -0
- package/dist/component/providers/keys.js.map +1 -0
- package/dist/component/providers/payload.d.ts +83 -0
- package/dist/component/providers/payload.d.ts.map +1 -0
- package/dist/component/providers/payload.js +162 -0
- package/dist/component/providers/payload.js.map +1 -0
- package/dist/component/providers/token_cache.d.ts +56 -0
- package/dist/component/providers/token_cache.d.ts.map +1 -0
- package/dist/component/providers/token_cache.js +42 -0
- package/dist/component/providers/token_cache.js.map +1 -0
- package/dist/component/providers/web_push.d.ts +40 -0
- package/dist/component/providers/web_push.d.ts.map +1 -0
- package/dist/component/providers/web_push.js +94 -0
- package/dist/component/providers/web_push.js.map +1 -0
- package/dist/component/queries.d.ts +132 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +156 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/queue.d.ts +66 -0
- package/dist/component/queue.d.ts.map +1 -0
- package/dist/component/queue.js +356 -0
- package/dist/component/queue.js.map +1 -0
- package/dist/component/registry.d.ts +46 -0
- package/dist/component/registry.d.ts.map +1 -0
- package/dist/component/registry.js +128 -0
- package/dist/component/registry.js.map +1 -0
- package/dist/component/schema.d.ts +286 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +70 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/senders.d.ts +67 -0
- package/dist/component/senders.d.ts.map +1 -0
- package/dist/component/senders.js +123 -0
- package/dist/component/senders.js.map +1 -0
- package/dist/component/validators.d.ts +784 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +193 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/shared.d.ts +109 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +131 -0
- package/dist/shared.js.map +1 -0
- package/docs/API.md +258 -0
- package/llms.txt +33 -0
- package/package.json +125 -4
- package/src/client/index.ts +276 -0
- package/src/component/_generated/api.ts +96 -0
- package/src/component/_generated/component.ts +263 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +178 -0
- package/src/component/actions.ts +31 -0
- package/src/component/checks.ts +117 -0
- package/src/component/convex.config.ts +32 -0
- package/src/component/crons.ts +17 -0
- package/src/component/crypto/codec.ts +113 -0
- package/src/component/crypto/jwt.ts +109 -0
- package/src/component/crypto/token_seal.ts +56 -0
- package/src/component/crypto/web_push_crypto.ts +75 -0
- package/src/component/internal_mutations.ts +88 -0
- package/src/component/mutations.ts +257 -0
- package/src/component/providers/apns.ts +116 -0
- package/src/component/providers/fcm.ts +132 -0
- package/src/component/providers/http.ts +78 -0
- package/src/component/providers/keys.ts +133 -0
- package/src/component/providers/payload.ts +200 -0
- package/src/component/providers/token_cache.ts +75 -0
- package/src/component/providers/web_push.ts +101 -0
- package/src/component/queries.ts +182 -0
- package/src/component/queue.ts +425 -0
- package/src/component/registry.ts +144 -0
- package/src/component/schema.ts +82 -0
- package/src/component/senders.ts +152 -0
- package/src/component/validators.ts +285 -0
- package/src/shared.ts +155 -0
- package/src/test.ts +25 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Register or refresh a device. A known address is updated in place; an address
|
|
3
|
+
* that moves to another subject is reassigned to it; a rotated token with a
|
|
4
|
+
* known `installRef` (same app) replaces its row. An invalid device
|
|
5
|
+
* becomes active again. `lastSeenAt` is refreshed on every call, so Apple's 410
|
|
6
|
+
* timestamp can tell a later registration apart.
|
|
7
|
+
* A disabled device stays disabled unless it moves to another subject.
|
|
8
|
+
*/
|
|
9
|
+
export declare const register: import("convex/server").RegisteredMutation<"public", {
|
|
10
|
+
address: {
|
|
11
|
+
platform: "apns";
|
|
12
|
+
token: string;
|
|
13
|
+
} | {
|
|
14
|
+
platform: "fcm";
|
|
15
|
+
token: string;
|
|
16
|
+
} | {
|
|
17
|
+
auth: string;
|
|
18
|
+
endpoint: string;
|
|
19
|
+
p256dh: string;
|
|
20
|
+
platform: "webpush";
|
|
21
|
+
} | {
|
|
22
|
+
address: string;
|
|
23
|
+
platform: "custom";
|
|
24
|
+
provider: string;
|
|
25
|
+
};
|
|
26
|
+
appRef: string;
|
|
27
|
+
installRef?: string | undefined;
|
|
28
|
+
locale?: string | undefined;
|
|
29
|
+
subjectRef: string;
|
|
30
|
+
}, Promise<{
|
|
31
|
+
deviceId: import("convex/values").GenericId<"devices">;
|
|
32
|
+
created: boolean;
|
|
33
|
+
}>>;
|
|
34
|
+
/**
|
|
35
|
+
* Remove a device, for example on logout. Pass the device id or its address.
|
|
36
|
+
* When `subjectRef` is given, only a device owned by that subject is removed.
|
|
37
|
+
*/
|
|
38
|
+
export declare const unregister: import("convex/server").RegisteredMutation<"public", {
|
|
39
|
+
address?: {
|
|
40
|
+
platform: "apns";
|
|
41
|
+
token: string;
|
|
42
|
+
} | {
|
|
43
|
+
platform: "fcm";
|
|
44
|
+
token: string;
|
|
45
|
+
} | {
|
|
46
|
+
auth: string;
|
|
47
|
+
endpoint: string;
|
|
48
|
+
p256dh: string;
|
|
49
|
+
platform: "webpush";
|
|
50
|
+
} | {
|
|
51
|
+
address: string;
|
|
52
|
+
platform: "custom";
|
|
53
|
+
provider: string;
|
|
54
|
+
} | undefined;
|
|
55
|
+
deviceId?: string | undefined;
|
|
56
|
+
subjectRef?: string | undefined;
|
|
57
|
+
}, Promise<{
|
|
58
|
+
removed: boolean;
|
|
59
|
+
}>>;
|
|
60
|
+
/** Pause (`disabled`) or resume (`active`) one device. */
|
|
61
|
+
export declare const setDeviceStatus: import("convex/server").RegisteredMutation<"public", {
|
|
62
|
+
deviceId: string;
|
|
63
|
+
status: "active" | "disabled";
|
|
64
|
+
subjectRef?: string | undefined;
|
|
65
|
+
}, Promise<{
|
|
66
|
+
updated: boolean;
|
|
67
|
+
}>>;
|
|
68
|
+
/** Enqueue one message and fan it out to the recipient's active devices. */
|
|
69
|
+
export declare const send: import("convex/server").RegisteredMutation<"public", {
|
|
70
|
+
config: {
|
|
71
|
+
apps: {
|
|
72
|
+
app: {
|
|
73
|
+
environment: "production" | "sandbox";
|
|
74
|
+
platform: "apns";
|
|
75
|
+
topic: string;
|
|
76
|
+
} | {
|
|
77
|
+
platform: "fcm";
|
|
78
|
+
} | {
|
|
79
|
+
platform: "webpush";
|
|
80
|
+
} | {
|
|
81
|
+
platform: "custom";
|
|
82
|
+
provider: string;
|
|
83
|
+
};
|
|
84
|
+
appRef: string;
|
|
85
|
+
}[];
|
|
86
|
+
maxAttempts: number;
|
|
87
|
+
maxParallelism: number;
|
|
88
|
+
onDeliveryHandle?: string | undefined;
|
|
89
|
+
providers: {
|
|
90
|
+
handle: string;
|
|
91
|
+
provider: string;
|
|
92
|
+
}[];
|
|
93
|
+
scrub: boolean;
|
|
94
|
+
};
|
|
95
|
+
message: {
|
|
96
|
+
badge?: number | undefined;
|
|
97
|
+
body?: string | undefined;
|
|
98
|
+
category?: string | undefined;
|
|
99
|
+
collapseKey?: string | undefined;
|
|
100
|
+
data?: Record<string, string | number | boolean> | undefined;
|
|
101
|
+
idempotencyKey?: string | undefined;
|
|
102
|
+
overrides?: {
|
|
103
|
+
apns?: {
|
|
104
|
+
interruptionLevel?: "active" | "critical" | "passive" | "time-sensitive" | undefined;
|
|
105
|
+
mutableContent?: boolean | undefined;
|
|
106
|
+
rawJson?: string | undefined;
|
|
107
|
+
threadId?: string | undefined;
|
|
108
|
+
} | undefined;
|
|
109
|
+
fcm?: {
|
|
110
|
+
channelId?: string | undefined;
|
|
111
|
+
rawJson?: string | undefined;
|
|
112
|
+
} | undefined;
|
|
113
|
+
webpush?: {
|
|
114
|
+
icon?: string | undefined;
|
|
115
|
+
rawJson?: string | undefined;
|
|
116
|
+
url?: string | undefined;
|
|
117
|
+
} | undefined;
|
|
118
|
+
} | undefined;
|
|
119
|
+
priority?: "high" | "normal" | undefined;
|
|
120
|
+
sound?: string | undefined;
|
|
121
|
+
title?: string | undefined;
|
|
122
|
+
to: {
|
|
123
|
+
subjectRef: string;
|
|
124
|
+
} | {
|
|
125
|
+
deviceIds: string[];
|
|
126
|
+
};
|
|
127
|
+
ttlSeconds?: number | undefined;
|
|
128
|
+
};
|
|
129
|
+
}, Promise<{
|
|
130
|
+
messageId: import("./_generated/dataModel.js").Id<"messages">;
|
|
131
|
+
deliveries: number;
|
|
132
|
+
deduplicated: boolean;
|
|
133
|
+
}>>;
|
|
134
|
+
/**
|
|
135
|
+
* Enqueue up to 100 messages and 1,000 deliveries in one transaction. If one
|
|
136
|
+
* is refused, or the batch fans out to more deliveries, none is queued.
|
|
137
|
+
*/
|
|
138
|
+
export declare const sendBatch: import("convex/server").RegisteredMutation<"public", {
|
|
139
|
+
config: {
|
|
140
|
+
apps: {
|
|
141
|
+
app: {
|
|
142
|
+
environment: "production" | "sandbox";
|
|
143
|
+
platform: "apns";
|
|
144
|
+
topic: string;
|
|
145
|
+
} | {
|
|
146
|
+
platform: "fcm";
|
|
147
|
+
} | {
|
|
148
|
+
platform: "webpush";
|
|
149
|
+
} | {
|
|
150
|
+
platform: "custom";
|
|
151
|
+
provider: string;
|
|
152
|
+
};
|
|
153
|
+
appRef: string;
|
|
154
|
+
}[];
|
|
155
|
+
maxAttempts: number;
|
|
156
|
+
maxParallelism: number;
|
|
157
|
+
onDeliveryHandle?: string | undefined;
|
|
158
|
+
providers: {
|
|
159
|
+
handle: string;
|
|
160
|
+
provider: string;
|
|
161
|
+
}[];
|
|
162
|
+
scrub: boolean;
|
|
163
|
+
};
|
|
164
|
+
messages: {
|
|
165
|
+
badge?: number | undefined;
|
|
166
|
+
body?: string | undefined;
|
|
167
|
+
category?: string | undefined;
|
|
168
|
+
collapseKey?: string | undefined;
|
|
169
|
+
data?: Record<string, string | number | boolean> | undefined;
|
|
170
|
+
idempotencyKey?: string | undefined;
|
|
171
|
+
overrides?: {
|
|
172
|
+
apns?: {
|
|
173
|
+
interruptionLevel?: "active" | "critical" | "passive" | "time-sensitive" | undefined;
|
|
174
|
+
mutableContent?: boolean | undefined;
|
|
175
|
+
rawJson?: string | undefined;
|
|
176
|
+
threadId?: string | undefined;
|
|
177
|
+
} | undefined;
|
|
178
|
+
fcm?: {
|
|
179
|
+
channelId?: string | undefined;
|
|
180
|
+
rawJson?: string | undefined;
|
|
181
|
+
} | undefined;
|
|
182
|
+
webpush?: {
|
|
183
|
+
icon?: string | undefined;
|
|
184
|
+
rawJson?: string | undefined;
|
|
185
|
+
url?: string | undefined;
|
|
186
|
+
} | undefined;
|
|
187
|
+
} | undefined;
|
|
188
|
+
priority?: "high" | "normal" | undefined;
|
|
189
|
+
sound?: string | undefined;
|
|
190
|
+
title?: string | undefined;
|
|
191
|
+
to: {
|
|
192
|
+
subjectRef: string;
|
|
193
|
+
} | {
|
|
194
|
+
deviceIds: string[];
|
|
195
|
+
};
|
|
196
|
+
ttlSeconds?: number | undefined;
|
|
197
|
+
}[];
|
|
198
|
+
}, Promise<{
|
|
199
|
+
messageId: import("./_generated/dataModel.js").Id<"messages">;
|
|
200
|
+
deliveries: number;
|
|
201
|
+
deduplicated: boolean;
|
|
202
|
+
}[]>>;
|
|
203
|
+
/** Cancel a message: every queued delivery ends as failed with `CANCELLED`. */
|
|
204
|
+
export declare const cancel: import("convex/server").RegisteredMutation<"public", {
|
|
205
|
+
messageId: string;
|
|
206
|
+
}, Promise<{
|
|
207
|
+
cancelled: number;
|
|
208
|
+
}>>;
|
|
209
|
+
/**
|
|
210
|
+
* Delete messages older than the retention window (and expired more than a day
|
|
211
|
+
* ago, so no send job is still on its way) with their deliveries, devices
|
|
212
|
+
* invalid and unseen for that long, and expired cached tokens. One pass deletes
|
|
213
|
+
* about `batch` rows (default and maximum 1,000; at most 100 devices) and
|
|
214
|
+
* reschedules itself while there is more. Idempotent.
|
|
215
|
+
*/
|
|
216
|
+
export declare const prune: import("convex/server").RegisteredMutation<"public", {
|
|
217
|
+
batch?: number | undefined;
|
|
218
|
+
retentionMs?: number | undefined;
|
|
219
|
+
}, Promise<{
|
|
220
|
+
deleted: number;
|
|
221
|
+
more: boolean;
|
|
222
|
+
}>>;
|
|
223
|
+
//# sourceMappingURL=mutations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAmBA;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;GA0DnB,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;;;;;;GA4BrB,CAAC;AAEH,0DAA0D;AAC1D,eAAO,MAAM,eAAe;;;;;;GAc1B,CAAC;AAEH,4EAA4E;AAC5E,eAAO,MAAM,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAIf,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAiBpB,CAAC;AAEH,+EAA+E;AAC/E,eAAO,MAAM,MAAM;;;;GAsBjB,CAAC;AAEH;;;;;;GAMG;AACH,eAAO,MAAM,KAAK;;;;;;GAwDhB,CAAC"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { DEFAULT_PRUNE_BATCH, DEFAULT_RETENTION_MS, MAX_BATCH_DELIVERIES, MAX_BATCH_MESSAGES, MAX_DEVICES_PER_MESSAGE, MAX_PRUNE_BATCH, PRUNE_DEVICES_PER_PASS, PRUNE_GRACE_MS, PRUNE_MESSAGES_PER_PASS, } from "../shared.js";
|
|
3
|
+
import { api } from "./_generated/api.js";
|
|
4
|
+
import { mutation } from "./_generated/server.js";
|
|
5
|
+
import { enqueueMessage, finishDelivery, forgetDevice, logger, scrubIfDone } from "./queue.js";
|
|
6
|
+
import { addressHash, checkAddress, checkRef, fail } from "./registry.js";
|
|
7
|
+
import { deviceAddress, sendConfig, sendInput, sendResultView } from "./validators.js";
|
|
8
|
+
/**
|
|
9
|
+
* Register or refresh a device. A known address is updated in place; an address
|
|
10
|
+
* that moves to another subject is reassigned to it; a rotated token with a
|
|
11
|
+
* known `installRef` (same app) replaces its row. An invalid device
|
|
12
|
+
* becomes active again. `lastSeenAt` is refreshed on every call, so Apple's 410
|
|
13
|
+
* timestamp can tell a later registration apart.
|
|
14
|
+
* A disabled device stays disabled unless it moves to another subject.
|
|
15
|
+
*/
|
|
16
|
+
export const register = mutation({
|
|
17
|
+
args: {
|
|
18
|
+
subjectRef: v.string(),
|
|
19
|
+
appRef: v.string(),
|
|
20
|
+
address: deviceAddress,
|
|
21
|
+
installRef: v.optional(v.string()),
|
|
22
|
+
locale: v.optional(v.string()),
|
|
23
|
+
},
|
|
24
|
+
returns: v.object({ deviceId: v.string(), created: v.boolean() }),
|
|
25
|
+
handler: async (ctx, args) => {
|
|
26
|
+
checkRef("subjectRef", args.subjectRef);
|
|
27
|
+
checkRef("appRef", args.appRef);
|
|
28
|
+
if (args.installRef !== undefined)
|
|
29
|
+
checkRef("installRef", args.installRef);
|
|
30
|
+
if (args.locale !== undefined && args.locale.length > 64)
|
|
31
|
+
fail("INVALID_ARGUMENT", "locale must be at most 64 characters");
|
|
32
|
+
checkAddress(args.address);
|
|
33
|
+
const hash = await addressHash(args.address);
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
const byAddress = await ctx.db
|
|
36
|
+
.query("devices")
|
|
37
|
+
.withIndex("by_address_hash", (q) => q.eq("addressHash", hash))
|
|
38
|
+
.first();
|
|
39
|
+
const byInstall = byAddress || args.installRef === undefined
|
|
40
|
+
? null
|
|
41
|
+
: await ctx.db
|
|
42
|
+
.query("devices")
|
|
43
|
+
.withIndex("by_install", (q) => q.eq("installRef", args.installRef).eq("appRef", args.appRef))
|
|
44
|
+
.first();
|
|
45
|
+
const existing = byAddress ?? byInstall;
|
|
46
|
+
if (!existing) {
|
|
47
|
+
const deviceId = await ctx.db.insert("devices", {
|
|
48
|
+
subjectRef: args.subjectRef,
|
|
49
|
+
appRef: args.appRef,
|
|
50
|
+
address: args.address,
|
|
51
|
+
addressHash: hash,
|
|
52
|
+
installRef: args.installRef,
|
|
53
|
+
locale: args.locale,
|
|
54
|
+
status: "active",
|
|
55
|
+
lastSeenAt: now,
|
|
56
|
+
});
|
|
57
|
+
logger.info("device.registered", { deviceId });
|
|
58
|
+
return { deviceId, created: true };
|
|
59
|
+
}
|
|
60
|
+
const reassigned = existing.subjectRef !== args.subjectRef;
|
|
61
|
+
const status = existing.status === "disabled" && !reassigned ? "disabled" : "active";
|
|
62
|
+
await ctx.db.patch("devices", existing._id, {
|
|
63
|
+
subjectRef: args.subjectRef,
|
|
64
|
+
appRef: args.appRef,
|
|
65
|
+
address: args.address,
|
|
66
|
+
addressHash: hash,
|
|
67
|
+
installRef: args.installRef ?? existing.installRef,
|
|
68
|
+
locale: args.locale ?? existing.locale,
|
|
69
|
+
status,
|
|
70
|
+
invalidCode: undefined,
|
|
71
|
+
lastSeenAt: now,
|
|
72
|
+
});
|
|
73
|
+
return { deviceId: existing._id, created: false };
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* Remove a device, for example on logout. Pass the device id or its address.
|
|
78
|
+
* When `subjectRef` is given, only a device owned by that subject is removed.
|
|
79
|
+
*/
|
|
80
|
+
export const unregister = mutation({
|
|
81
|
+
args: {
|
|
82
|
+
deviceId: v.optional(v.string()),
|
|
83
|
+
address: v.optional(deviceAddress),
|
|
84
|
+
subjectRef: v.optional(v.string()),
|
|
85
|
+
},
|
|
86
|
+
returns: v.object({ removed: v.boolean() }),
|
|
87
|
+
handler: async (ctx, args) => {
|
|
88
|
+
if ((args.deviceId === undefined) === (args.address === undefined)) {
|
|
89
|
+
fail("INVALID_ARGUMENT", "pass exactly one of deviceId or address");
|
|
90
|
+
}
|
|
91
|
+
const id = args.deviceId === undefined ? null : ctx.db.normalizeId("devices", args.deviceId);
|
|
92
|
+
const hash = args.address === undefined ? null : await addressHash(args.address);
|
|
93
|
+
const device = id
|
|
94
|
+
? await ctx.db.get("devices", id)
|
|
95
|
+
: hash === null
|
|
96
|
+
? null
|
|
97
|
+
: await ctx.db
|
|
98
|
+
.query("devices")
|
|
99
|
+
.withIndex("by_address_hash", (q) => q.eq("addressHash", hash))
|
|
100
|
+
.first();
|
|
101
|
+
const owned = device !== null && (args.subjectRef === undefined || device.subjectRef === args.subjectRef);
|
|
102
|
+
if (owned) {
|
|
103
|
+
await ctx.db.delete("devices", device._id);
|
|
104
|
+
await forgetDevice(ctx, device._id);
|
|
105
|
+
}
|
|
106
|
+
return { removed: owned };
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
/** Pause (`disabled`) or resume (`active`) one device. */
|
|
110
|
+
export const setDeviceStatus = mutation({
|
|
111
|
+
args: {
|
|
112
|
+
deviceId: v.string(),
|
|
113
|
+
status: v.union(v.literal("active"), v.literal("disabled")),
|
|
114
|
+
subjectRef: v.optional(v.string()),
|
|
115
|
+
},
|
|
116
|
+
returns: v.object({ updated: v.boolean() }),
|
|
117
|
+
handler: async (ctx, args) => {
|
|
118
|
+
const id = ctx.db.normalizeId("devices", args.deviceId);
|
|
119
|
+
const device = id ? await ctx.db.get("devices", id) : null;
|
|
120
|
+
if (!device || (args.subjectRef !== undefined && device.subjectRef !== args.subjectRef))
|
|
121
|
+
return { updated: false };
|
|
122
|
+
await ctx.db.patch("devices", device._id, { status: args.status, invalidCode: undefined });
|
|
123
|
+
return { updated: true };
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
/** Enqueue one message and fan it out to the recipient's active devices. */
|
|
127
|
+
export const send = mutation({
|
|
128
|
+
args: { message: sendInput, config: sendConfig },
|
|
129
|
+
returns: sendResultView,
|
|
130
|
+
handler: async (ctx, { message, config }) => enqueueMessage(ctx, message, config),
|
|
131
|
+
});
|
|
132
|
+
/**
|
|
133
|
+
* Enqueue up to 100 messages and 1,000 deliveries in one transaction. If one
|
|
134
|
+
* is refused, or the batch fans out to more deliveries, none is queued.
|
|
135
|
+
*/
|
|
136
|
+
export const sendBatch = mutation({
|
|
137
|
+
args: { messages: v.array(sendInput), config: sendConfig },
|
|
138
|
+
returns: v.array(sendResultView),
|
|
139
|
+
handler: async (ctx, { messages, config }) => {
|
|
140
|
+
if (messages.length > MAX_BATCH_MESSAGES)
|
|
141
|
+
fail("INVALID_ARGUMENT", `sendBatch accepts at most ${String(MAX_BATCH_MESSAGES)} messages`);
|
|
142
|
+
const results = [];
|
|
143
|
+
let deliveries = 0;
|
|
144
|
+
for (const message of messages) {
|
|
145
|
+
const result = await enqueueMessage(ctx, message, config);
|
|
146
|
+
deliveries += result.deduplicated ? 0 : result.deliveries;
|
|
147
|
+
if (deliveries > MAX_BATCH_DELIVERIES) {
|
|
148
|
+
fail("INVALID_ARGUMENT", `sendBatch fans out to more than ${String(MAX_BATCH_DELIVERIES)} deliveries; split it`);
|
|
149
|
+
}
|
|
150
|
+
results.push(result);
|
|
151
|
+
}
|
|
152
|
+
return results;
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
/** Cancel a message: every queued delivery ends as failed with `CANCELLED`. */
|
|
156
|
+
export const cancel = mutation({
|
|
157
|
+
args: { messageId: v.string() },
|
|
158
|
+
returns: v.object({ cancelled: v.number() }),
|
|
159
|
+
handler: async (ctx, args) => {
|
|
160
|
+
const id = ctx.db.normalizeId("messages", args.messageId);
|
|
161
|
+
const message = id ? await ctx.db.get("messages", id) : null;
|
|
162
|
+
if (!message)
|
|
163
|
+
return { cancelled: 0 };
|
|
164
|
+
const queued = await ctx.db
|
|
165
|
+
.query("deliveries")
|
|
166
|
+
.withIndex("by_message", (q) => q.eq("messageId", message._id))
|
|
167
|
+
.take(MAX_DEVICES_PER_MESSAGE);
|
|
168
|
+
const now = Date.now();
|
|
169
|
+
let cancelled = 0;
|
|
170
|
+
for (const delivery of queued.filter((d) => d.status === "queued")) {
|
|
171
|
+
const patch = { status: "failed", errorCode: "CANCELLED", nextAttemptAt: undefined, updatedAt: now };
|
|
172
|
+
await ctx.db.patch("deliveries", delivery._id, patch);
|
|
173
|
+
await finishDelivery(ctx, { ...delivery, ...patch }, message);
|
|
174
|
+
cancelled += 1;
|
|
175
|
+
}
|
|
176
|
+
await scrubIfDone(ctx, message._id);
|
|
177
|
+
return { cancelled };
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
/**
|
|
181
|
+
* Delete messages older than the retention window (and expired more than a day
|
|
182
|
+
* ago, so no send job is still on its way) with their deliveries, devices
|
|
183
|
+
* invalid and unseen for that long, and expired cached tokens. One pass deletes
|
|
184
|
+
* about `batch` rows (default and maximum 1,000; at most 100 devices) and
|
|
185
|
+
* reschedules itself while there is more. Idempotent.
|
|
186
|
+
*/
|
|
187
|
+
export const prune = mutation({
|
|
188
|
+
args: { retentionMs: v.optional(v.number()), batch: v.optional(v.number()) },
|
|
189
|
+
returns: v.object({ deleted: v.number(), more: v.boolean() }),
|
|
190
|
+
handler: async (ctx, args) => {
|
|
191
|
+
const retentionMs = args.retentionMs ?? DEFAULT_RETENTION_MS;
|
|
192
|
+
const batch = args.batch ?? DEFAULT_PRUNE_BATCH;
|
|
193
|
+
if (!Number.isInteger(batch) || batch < 1 || batch > MAX_PRUNE_BATCH) {
|
|
194
|
+
fail("INVALID_ARGUMENT", `batch must be an integer from 1 to ${String(MAX_PRUNE_BATCH)}`);
|
|
195
|
+
}
|
|
196
|
+
if (!Number.isFinite(retentionMs) || retentionMs < 0)
|
|
197
|
+
fail("INVALID_ARGUMENT", "retentionMs must be a non-negative number");
|
|
198
|
+
const now = Date.now();
|
|
199
|
+
const cutoff = now - retentionMs;
|
|
200
|
+
const pageSize = Math.min(batch, PRUNE_MESSAGES_PER_PASS);
|
|
201
|
+
const old = await ctx.db
|
|
202
|
+
.query("messages")
|
|
203
|
+
.withIndex("by_creation_time", (q) => q.lt("_creationTime", cutoff))
|
|
204
|
+
.take(pageSize);
|
|
205
|
+
const doomed = old.filter((m) => m.expiresAt + PRUNE_GRACE_MS < now);
|
|
206
|
+
const pages = await Promise.all(doomed.map(async (message) => ({
|
|
207
|
+
message,
|
|
208
|
+
deliveries: await ctx.db
|
|
209
|
+
.query("deliveries")
|
|
210
|
+
.withIndex("by_message", (q) => q.eq("messageId", message._id))
|
|
211
|
+
.take(MAX_DEVICES_PER_MESSAGE + 1),
|
|
212
|
+
})));
|
|
213
|
+
let deleted = 0;
|
|
214
|
+
let stopped = false;
|
|
215
|
+
const deletions = [];
|
|
216
|
+
for (const { message, deliveries } of pages) {
|
|
217
|
+
if (deleted > 0 && deleted + deliveries.length + 1 > batch) {
|
|
218
|
+
stopped = true;
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
deletions.push(...deliveries.map((d) => ctx.db.delete("deliveries", d._id)), ctx.db.delete("messages", message._id));
|
|
222
|
+
deleted += deliveries.length + 1;
|
|
223
|
+
}
|
|
224
|
+
await Promise.all(deletions);
|
|
225
|
+
const room = Math.max(batch - deleted, 0);
|
|
226
|
+
const devices = await ctx.db
|
|
227
|
+
.query("devices")
|
|
228
|
+
.withIndex("by_status_seen", (q) => q.eq("status", "invalid").lt("lastSeenAt", cutoff))
|
|
229
|
+
.take(Math.min(room, PRUNE_DEVICES_PER_PASS));
|
|
230
|
+
for (const device of devices) {
|
|
231
|
+
await ctx.db.delete("devices", device._id);
|
|
232
|
+
await forgetDevice(ctx, device._id);
|
|
233
|
+
}
|
|
234
|
+
const tokens = await ctx.db.query("providerTokens").take(PRUNE_MESSAGES_PER_PASS);
|
|
235
|
+
const expiredTokens = tokens.filter((t) => t.expiresAt < now);
|
|
236
|
+
await Promise.all(expiredTokens.map((t) => ctx.db.delete("providerTokens", t._id)));
|
|
237
|
+
deleted += devices.length + expiredTokens.length;
|
|
238
|
+
const more = stopped || devices.length === Math.min(room, PRUNE_DEVICES_PER_PASS) || (old.length === pageSize && doomed.length > 0);
|
|
239
|
+
if (more)
|
|
240
|
+
await ctx.scheduler.runAfter(0, api.mutations.prune, { retentionMs, batch });
|
|
241
|
+
return { deleted, more };
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
//# sourceMappingURL=mutations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,E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AC,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { SendResult } from "../validators.js";
|
|
2
|
+
import type { PushEnv } from "./keys.js";
|
|
3
|
+
import { type PushContent } from "./payload.js";
|
|
4
|
+
import type { TokenCache } from "./token_cache.js";
|
|
5
|
+
/** One APNs delivery. */
|
|
6
|
+
export interface ApnsJob {
|
|
7
|
+
messageId: string;
|
|
8
|
+
token: string;
|
|
9
|
+
topic: string;
|
|
10
|
+
environment: "production" | "sandbox";
|
|
11
|
+
content: PushContent;
|
|
12
|
+
expiresAt: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The Apple host for an environment.
|
|
16
|
+
* @param environment - production or sandbox.
|
|
17
|
+
* @returns The host name.
|
|
18
|
+
*/
|
|
19
|
+
export declare function apnsHost(environment: "production" | "sandbox"): string;
|
|
20
|
+
/**
|
|
21
|
+
* Build the request headers. Silent pushes use push type `background` at priority 5, as Apple requires.
|
|
22
|
+
* The default collapse id is the message id, so a duplicate send replaces the original.
|
|
23
|
+
* @param job - The delivery.
|
|
24
|
+
* @param jwt - The provider token.
|
|
25
|
+
* @returns Header map.
|
|
26
|
+
*/
|
|
27
|
+
export declare function apnsHeaders(job: ApnsJob, jwt: string): Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* Map an APNs response to a sender result.
|
|
30
|
+
* @param status - HTTP status.
|
|
31
|
+
* @param headers - Response headers.
|
|
32
|
+
* @param body - Parsed JSON body (`{ reason, timestamp? }` on errors).
|
|
33
|
+
* @param now - Clock.
|
|
34
|
+
* @returns The result. `ExpiredProviderToken` is a retry; the caller drops the cached token.
|
|
35
|
+
*/
|
|
36
|
+
export declare function classifyApns(status: number, headers: Headers, body: unknown, now: number): SendResult;
|
|
37
|
+
/**
|
|
38
|
+
* Send one APNs notification over HTTP/2 with plain fetch.
|
|
39
|
+
* @param job - The delivery.
|
|
40
|
+
* @param env - Component env with the Apple key.
|
|
41
|
+
* @param cache - Provider-token cache.
|
|
42
|
+
* @param now - Clock.
|
|
43
|
+
* @returns The sender result.
|
|
44
|
+
*/
|
|
45
|
+
export declare function sendApns(job: ApnsJob, env: PushEnv, cache: TokenCache, now: () => number): Promise<SendResult>;
|
|
46
|
+
//# sourceMappingURL=apns.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apns.d.ts","sourceRoot":"","sources":["../../../src/component/providers/apns.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEzC,OAAO,EAA4C,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAC1F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AASnD,yBAAyB;AACzB,MAAM,WAAW,OAAO;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,YAAY,GAAG,SAAS,CAAC;IACtC,OAAO,EAAE,WAAW,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,WAAW,EAAE,YAAY,GAAG,SAAS,GAAG,MAAM,CAEtE;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAY7E;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,CAoBrG;AAED;;;;;;;GAOG;AACH,wBAAsB,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAqBpH"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { APNS_SERVER_ERROR_DELAY_MS } from "../../shared.js";
|
|
2
|
+
import { importSigningKey, signJwt } from "../crypto/jwt.js";
|
|
3
|
+
import { parseRetryAfter, providerFetch, readJson } from "./http.js";
|
|
4
|
+
import { readApns } from "./keys.js";
|
|
5
|
+
import { buildApnsPayload, isJsonObject, isSilent } from "./payload.js";
|
|
6
|
+
/** Apple's documented token lifetime is 60 minutes, with no replacement within 20. Reuse for 45. */
|
|
7
|
+
const APNS_TOKEN_REUSE_MS = 2_700_000;
|
|
8
|
+
const APNS_TOKEN_LIFETIME_MS = 3_600_000;
|
|
9
|
+
/** Apple reasons that mean the device address is dead. */
|
|
10
|
+
const DEAD_REASONS = new Set(["BadDeviceToken", "DeviceTokenNotForTopic", "ExpiredToken", "Unregistered"]);
|
|
11
|
+
/**
|
|
12
|
+
* The Apple host for an environment.
|
|
13
|
+
* @param environment - production or sandbox.
|
|
14
|
+
* @returns The host name.
|
|
15
|
+
*/
|
|
16
|
+
export function apnsHost(environment) {
|
|
17
|
+
return environment === "production" ? "api.push.apple.com" : "api.sandbox.push.apple.com";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Build the request headers. Silent pushes use push type `background` at priority 5, as Apple requires.
|
|
21
|
+
* The default collapse id is the message id, so a duplicate send replaces the original.
|
|
22
|
+
* @param job - The delivery.
|
|
23
|
+
* @param jwt - The provider token.
|
|
24
|
+
* @returns Header map.
|
|
25
|
+
*/
|
|
26
|
+
export function apnsHeaders(job, jwt) {
|
|
27
|
+
const silent = isSilent(job.content);
|
|
28
|
+
const ttl = job.content.options.ttlSeconds;
|
|
29
|
+
return {
|
|
30
|
+
authorization: `bearer ${jwt}`,
|
|
31
|
+
"apns-topic": job.topic,
|
|
32
|
+
"apns-push-type": silent ? "background" : "alert",
|
|
33
|
+
"apns-priority": !silent && job.content.options.priority === "high" ? "10" : "5",
|
|
34
|
+
"apns-expiration": ttl === 0 ? "0" : String(Math.floor(job.expiresAt / 1000)),
|
|
35
|
+
"apns-collapse-id": job.content.options.collapseKey ?? job.messageId,
|
|
36
|
+
"content-type": "application/json",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Map an APNs response to a sender result.
|
|
41
|
+
* @param status - HTTP status.
|
|
42
|
+
* @param headers - Response headers.
|
|
43
|
+
* @param body - Parsed JSON body (`{ reason, timestamp? }` on errors).
|
|
44
|
+
* @param now - Clock.
|
|
45
|
+
* @returns The result. `ExpiredProviderToken` is a retry; the caller drops the cached token.
|
|
46
|
+
*/
|
|
47
|
+
export function classifyApns(status, headers, body, now) {
|
|
48
|
+
if (status === 200)
|
|
49
|
+
return { status: "sent", providerMessageId: headers.get("apns-id") ?? undefined, httpStatus: 200 };
|
|
50
|
+
const reason = isJsonObject(body) && typeof body.reason === "string" ? body.reason : `HTTP_${String(status)}`;
|
|
51
|
+
const timestamp = isJsonObject(body) && typeof body.timestamp === "number" ? body.timestamp : undefined;
|
|
52
|
+
if ((status === 400 || status === 410) && DEAD_REASONS.has(reason)) {
|
|
53
|
+
return { status: "invalid", code: reason, httpStatus: status, invalidSince: timestamp };
|
|
54
|
+
}
|
|
55
|
+
if (status === 403 && reason === "ExpiredProviderToken")
|
|
56
|
+
return { status: "retry", code: reason, httpStatus: status };
|
|
57
|
+
if (status === 429) {
|
|
58
|
+
return { status: "retry", code: reason, httpStatus: status, delayMs: parseRetryAfter(headers.get("retry-after"), now) };
|
|
59
|
+
}
|
|
60
|
+
if (status >= 500) {
|
|
61
|
+
return {
|
|
62
|
+
status: "retry",
|
|
63
|
+
code: reason,
|
|
64
|
+
httpStatus: status,
|
|
65
|
+
delayMs: parseRetryAfter(headers.get("retry-after"), now) ?? APNS_SERVER_ERROR_DELAY_MS,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return { status: "failed", code: reason, httpStatus: status };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Send one APNs notification over HTTP/2 with plain fetch.
|
|
72
|
+
* @param job - The delivery.
|
|
73
|
+
* @param env - Component env with the Apple key.
|
|
74
|
+
* @param cache - Provider-token cache.
|
|
75
|
+
* @param now - Clock.
|
|
76
|
+
* @returns The sender result.
|
|
77
|
+
*/
|
|
78
|
+
export async function sendApns(job, env, cache, now) {
|
|
79
|
+
const key = await readApns(env, job.environment);
|
|
80
|
+
if (!key.ok)
|
|
81
|
+
return { status: "failed", code: key.code };
|
|
82
|
+
const slot = { credentialRef: key.value.credentialRef };
|
|
83
|
+
const jwt = await cache.get(slot, key.value.pemText, async () => {
|
|
84
|
+
const signingKey = await importSigningKey(key.value.pemText, "ES256");
|
|
85
|
+
if (!signingKey)
|
|
86
|
+
return { ok: false, result: { status: "failed", code: "CREDENTIALS_INVALID" } };
|
|
87
|
+
const issuedAt = now();
|
|
88
|
+
const token = await signJwt({ kid: key.value.keyId }, { iss: key.value.teamId, iat: Math.floor(issuedAt / 1000) }, signingKey, "ES256");
|
|
89
|
+
return { ok: true, token, refreshAfter: issuedAt + APNS_TOKEN_REUSE_MS, expiresAt: issuedAt + APNS_TOKEN_LIFETIME_MS };
|
|
90
|
+
});
|
|
91
|
+
if (!jwt.ok)
|
|
92
|
+
return jwt.result;
|
|
93
|
+
const http = await providerFetch(`https://${apnsHost(job.environment)}/3/device/${encodeURIComponent(job.token)}`, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers: apnsHeaders(job, jwt.token),
|
|
96
|
+
body: JSON.stringify(buildApnsPayload(job.content)),
|
|
97
|
+
});
|
|
98
|
+
if (!http.ok)
|
|
99
|
+
return http.result;
|
|
100
|
+
const result = classifyApns(http.response.status, http.response.headers, await readJson(http.response), now());
|
|
101
|
+
if (result.status === "retry" && result.code === "ExpiredProviderToken")
|
|
102
|
+
await cache.drop(slot);
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|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