@terreno/comms 57.2.0 → 57.3.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/README.md +58 -0
- package/dist/adapters/expoPush.d.ts +54 -0
- package/dist/adapters/expoPush.js +400 -0
- package/dist/adapters/sendgrid.d.ts +3 -0
- package/dist/adapters/sendgrid.js +7 -1
- package/dist/adapters/twilioSms.d.ts +36 -0
- package/dist/adapters/twilioSms.js +232 -0
- package/dist/adapters/twilioVerify.d.ts +44 -0
- package/dist/adapters/twilioVerify.js +212 -0
- package/dist/commsApp.d.ts +4 -1
- package/dist/commsApp.js +28 -2
- package/dist/commsQuery.d.ts +30 -0
- package/dist/commsQuery.js +134 -0
- package/dist/commsRetry.d.ts +16 -0
- package/dist/commsRetry.js +79 -0
- package/dist/commsService.d.ts +11 -4
- package/dist/commsService.js +217 -47
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -1
- package/dist/routes/commsDashboard.d.ts +11 -0
- package/dist/routes/commsDashboard.js +414 -0
- package/dist/templates.d.ts +9 -0
- package/dist/templates.js +34 -1
- package/dist/types.d.ts +11 -0
- package/dist/webhooks/sendgridWebhooks.d.ts +16 -0
- package/dist/webhooks/sendgridWebhooks.js +251 -0
- package/dist/webhooks/twilioWebhooks.d.ts +18 -0
- package/dist/webhooks/twilioWebhooks.js +175 -0
- package/package.json +26 -5
- package/src/adapters/expoPush.test.ts +585 -0
- package/src/adapters/expoPush.ts +304 -0
- package/src/adapters/sendgrid.ts +8 -0
- package/src/adapters/twilioSms.test.ts +374 -0
- package/src/adapters/twilioSms.ts +237 -0
- package/src/adapters/twilioVerify.test.ts +207 -0
- package/src/adapters/twilioVerify.ts +227 -0
- package/src/commsApp.test.ts +13 -0
- package/src/commsApp.ts +38 -3
- package/src/commsQuery.test.ts +165 -0
- package/src/commsQuery.ts +162 -0
- package/src/commsRetry.ts +87 -0
- package/src/commsService.test.ts +68 -0
- package/src/commsService.ts +177 -20
- package/src/index.ts +9 -2
- package/src/routes/commsDashboard.test.ts +310 -0
- package/src/routes/commsDashboard.ts +327 -0
- package/src/templates.test.ts +63 -1
- package/src/templates.ts +47 -0
- package/src/tests/bunSetup.ts +8 -0
- package/src/types.ts +13 -0
- package/src/webhooks/sendgridWebhooks.test.ts +520 -0
- package/src/webhooks/sendgridWebhooks.ts +166 -0
- package/src/webhooks/twilioWebhooks.test.ts +352 -0
- package/src/webhooks/twilioWebhooks.ts +154 -0
- package/dist/routes/commsExplorer.d.ts +0 -7
- package/dist/routes/commsExplorer.js +0 -147
- package/src/routes/commsExplorer.ts +0 -114
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# @terreno/comms
|
|
2
|
+
|
|
3
|
+
Pluggable communications providers for Terreno backends.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @terreno/comms
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer dependency: `mongoose ^8.0.0 || ^9.0.0`. `@terreno/api` is required at runtime.
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import {
|
|
17
|
+
CommsApp,
|
|
18
|
+
ConsoleMailProvider,
|
|
19
|
+
ConsolePushProvider,
|
|
20
|
+
ConsoleSmsProvider,
|
|
21
|
+
ConsoleVerificationProvider,
|
|
22
|
+
} from "@terreno/comms";
|
|
23
|
+
import {TerrenoApp} from "@terreno/api";
|
|
24
|
+
import {User} from "./models/user";
|
|
25
|
+
|
|
26
|
+
new TerrenoApp({userModel: User})
|
|
27
|
+
.register(
|
|
28
|
+
new CommsApp({
|
|
29
|
+
defaultFrom: "notifications@example.com",
|
|
30
|
+
mail: new ConsoleMailProvider(),
|
|
31
|
+
push: new ConsolePushProvider(),
|
|
32
|
+
sms: new ConsoleSmsProvider(),
|
|
33
|
+
verification: new ConsoleVerificationProvider(),
|
|
34
|
+
})
|
|
35
|
+
)
|
|
36
|
+
.start();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Send from routes or jobs with `getCommsService()`. Console providers log counts and lengths only — never content, addresses, tokens, or codes.
|
|
40
|
+
|
|
41
|
+
## What's included
|
|
42
|
+
|
|
43
|
+
- `CommsApp` — TerrenoPlugin for mail, SMS, push, and verification
|
|
44
|
+
- Console providers for local development
|
|
45
|
+
- `CommsMessage` / `PushToken` models and admin explorer routes
|
|
46
|
+
- Adapter subpath `@terreno/comms/adapters/sendgrid` (optional peer `@sendgrid/mail`)
|
|
47
|
+
- Adapter subpath `@terreno/comms/adapters/twilioSms` (optional peer `twilio`)
|
|
48
|
+
- Adapter subpath `@terreno/comms/adapters/twilioVerify` (optional peer `twilio`)
|
|
49
|
+
- Adapter subpath `@terreno/comms/adapters/expoPush` (optional peer `expo-server-sdk`)
|
|
50
|
+
- `beforeSend`, delivery events, opt-out, and payload retention controls
|
|
51
|
+
|
|
52
|
+
## Documentation
|
|
53
|
+
|
|
54
|
+
Full API reference: [docs/reference/comms.md](https://github.com/flourishhealth/terreno/blob/master/docs/reference/comms.md)
|
|
55
|
+
|
|
56
|
+
## License and Contributing
|
|
57
|
+
|
|
58
|
+
Licensed under the [MIT License](https://github.com/flourishhealth/terreno/blob/master/LICENSE). See [CONTRIBUTING.md](https://github.com/flourishhealth/terreno/blob/master/CONTRIBUTING.md) for contribution guidelines.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { DeliveryEvent, PushMessage, PushProvider, SendResult } from "../types";
|
|
2
|
+
export interface ExpoPushMessage {
|
|
3
|
+
badge?: number;
|
|
4
|
+
body: string;
|
|
5
|
+
data?: Record<string, unknown>;
|
|
6
|
+
sound?: string | null;
|
|
7
|
+
title: string;
|
|
8
|
+
to: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ExpoPushTicket {
|
|
11
|
+
details?: {
|
|
12
|
+
error?: string;
|
|
13
|
+
};
|
|
14
|
+
id?: string;
|
|
15
|
+
message?: string;
|
|
16
|
+
status: "error" | "ok";
|
|
17
|
+
}
|
|
18
|
+
export interface ExpoPushReceipt {
|
|
19
|
+
details?: {
|
|
20
|
+
error?: string;
|
|
21
|
+
};
|
|
22
|
+
message?: string;
|
|
23
|
+
status: "error" | "ok";
|
|
24
|
+
}
|
|
25
|
+
export interface ExpoPushClient {
|
|
26
|
+
chunkPushNotificationReceiptIds: (receiptIds: string[]) => string[][];
|
|
27
|
+
chunkPushNotifications: (messages: ExpoPushMessage[]) => ExpoPushMessage[][];
|
|
28
|
+
getPushNotificationReceiptsAsync: (receiptIds: string[]) => Promise<Record<string, ExpoPushReceipt>>;
|
|
29
|
+
sendPushNotificationsAsync: (messages: ExpoPushMessage[]) => Promise<ExpoPushTicket[]>;
|
|
30
|
+
}
|
|
31
|
+
export interface ExpoPushProviderOptions {
|
|
32
|
+
accessToken?: string;
|
|
33
|
+
/** Injected client for tests. Production wiring loads `expo-server-sdk`. */
|
|
34
|
+
client?: ExpoPushClient;
|
|
35
|
+
isExpoPushToken?: (token: string) => boolean;
|
|
36
|
+
onDeadToken?: (token: string) => Promise<void>;
|
|
37
|
+
onDeliveryEvent?: (event: DeliveryEvent) => Promise<void>;
|
|
38
|
+
receiptPollDelayMs?: number;
|
|
39
|
+
scheduleReceiptPoll?: (delayMs: number, run: () => Promise<void>) => void;
|
|
40
|
+
}
|
|
41
|
+
export declare class ExpoPushProvider implements PushProvider {
|
|
42
|
+
readonly id = "expo";
|
|
43
|
+
private readonly client;
|
|
44
|
+
private readonly isExpoPushToken;
|
|
45
|
+
private readonly onDeadToken?;
|
|
46
|
+
private readonly onDeliveryEvent?;
|
|
47
|
+
private readonly receiptPollDelayMs;
|
|
48
|
+
private readonly scheduleReceiptPoll;
|
|
49
|
+
private pendingReceipts;
|
|
50
|
+
constructor(options?: ExpoPushProviderOptions);
|
|
51
|
+
sendPush(message: PushMessage): Promise<SendResult[]>;
|
|
52
|
+
private pollReceipts;
|
|
53
|
+
private applyReceipt;
|
|
54
|
+
}
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __values = (this && this.__values) || function(o) {
|
|
39
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
+
if (m) return m.call(o);
|
|
41
|
+
if (o && typeof o.length === "number") return {
|
|
42
|
+
next: function () {
|
|
43
|
+
if (o && i >= o.length) o = void 0;
|
|
44
|
+
return { value: o && o[i++], done: !o };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
+
};
|
|
49
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
+
if (!m) return o;
|
|
52
|
+
var i = m.call(o), r, ar = [], e;
|
|
53
|
+
try {
|
|
54
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
+
}
|
|
56
|
+
catch (error) { e = { error: error }; }
|
|
57
|
+
finally {
|
|
58
|
+
try {
|
|
59
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
+
}
|
|
61
|
+
finally { if (e) throw e.error; }
|
|
62
|
+
}
|
|
63
|
+
return ar;
|
|
64
|
+
};
|
|
65
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
|
+
exports.ExpoPushProvider = void 0;
|
|
67
|
+
var node_module_1 = require("node:module");
|
|
68
|
+
var api_1 = require("@terreno/api");
|
|
69
|
+
var nodeRequire = (0, node_module_1.createRequire)(__filename);
|
|
70
|
+
var DEFAULT_RECEIPT_POLL_DELAY_MS = 15 * 60 * 1000;
|
|
71
|
+
var resolveAccessToken = function (options) { var _a; return (_a = options === null || options === void 0 ? void 0 : options.accessToken) !== null && _a !== void 0 ? _a : process.env.EXPO_ACCESS_TOKEN; };
|
|
72
|
+
var loadExpoSdk = function () {
|
|
73
|
+
try {
|
|
74
|
+
// Optional peer — keep `expo-server-sdk` out of core dependencies.
|
|
75
|
+
return nodeRequire("expo-server-sdk");
|
|
76
|
+
}
|
|
77
|
+
catch (_a) {
|
|
78
|
+
throw new Error("ExpoPushProvider requires optional peer dependency expo-server-sdk. " +
|
|
79
|
+
"Install it with: bun add expo-server-sdk");
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var defaultIsExpoPushToken = function (token) {
|
|
83
|
+
return (token.startsWith("ExponentPushToken[") || token.startsWith("ExpoPushToken[")) &&
|
|
84
|
+
token.endsWith("]");
|
|
85
|
+
};
|
|
86
|
+
var classifyExpoError = function (code) {
|
|
87
|
+
if (code === "InvalidCredentials" || code === "MessageTooBig") {
|
|
88
|
+
return { errorClass: "config", errorCode: code };
|
|
89
|
+
}
|
|
90
|
+
if (code === "DeviceNotRegistered") {
|
|
91
|
+
return { errorClass: "permanent", errorCode: code };
|
|
92
|
+
}
|
|
93
|
+
if (code === "MessageRateExceeded") {
|
|
94
|
+
return { errorClass: "transient", errorCode: code };
|
|
95
|
+
}
|
|
96
|
+
return { errorClass: "transient", errorCode: code !== null && code !== void 0 ? code : "expo-unknown" };
|
|
97
|
+
};
|
|
98
|
+
var failedResult = function (_a) {
|
|
99
|
+
var error = _a.error, errorClass = _a.errorClass, errorCode = _a.errorCode;
|
|
100
|
+
if (errorClass === "config") {
|
|
101
|
+
api_1.logger.error("[comms:expo] ".concat(errorCode, ": ").concat(error));
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
accepted: false,
|
|
105
|
+
error: error,
|
|
106
|
+
errorClass: errorClass,
|
|
107
|
+
errorCode: errorCode,
|
|
108
|
+
isPermanentFailure: errorClass === "permanent",
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
var invalidTokenResult = function () {
|
|
112
|
+
return failedResult({
|
|
113
|
+
error: "Not an Expo push token",
|
|
114
|
+
errorClass: "permanent",
|
|
115
|
+
errorCode: "expo-invalid-token",
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
var ticketToResult = function (ticket) {
|
|
119
|
+
var _a, _b;
|
|
120
|
+
if (!ticket) {
|
|
121
|
+
return failedResult({
|
|
122
|
+
error: "Expo returned no ticket for this token",
|
|
123
|
+
errorClass: "transient",
|
|
124
|
+
errorCode: "expo-missing-ticket",
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (ticket.status === "ok") {
|
|
128
|
+
return { accepted: true, providerMessageId: ticket.id };
|
|
129
|
+
}
|
|
130
|
+
var _c = classifyExpoError((_a = ticket.details) === null || _a === void 0 ? void 0 : _a.error), errorClass = _c.errorClass, errorCode = _c.errorCode;
|
|
131
|
+
return failedResult({
|
|
132
|
+
error: (_b = ticket.message) !== null && _b !== void 0 ? _b : errorCode,
|
|
133
|
+
errorClass: errorClass,
|
|
134
|
+
errorCode: errorCode,
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
var defaultScheduleReceiptPoll = function (delayMs, run) {
|
|
138
|
+
setTimeout(function () {
|
|
139
|
+
void run();
|
|
140
|
+
}, delayMs);
|
|
141
|
+
};
|
|
142
|
+
var ExpoPushProvider = /** @class */ (function () {
|
|
143
|
+
function ExpoPushProvider(options) {
|
|
144
|
+
var _a, _b, _c, _d;
|
|
145
|
+
this.id = "expo";
|
|
146
|
+
this.pendingReceipts = new Map();
|
|
147
|
+
this.onDeadToken = options === null || options === void 0 ? void 0 : options.onDeadToken;
|
|
148
|
+
this.onDeliveryEvent = options === null || options === void 0 ? void 0 : options.onDeliveryEvent;
|
|
149
|
+
this.receiptPollDelayMs = (_a = options === null || options === void 0 ? void 0 : options.receiptPollDelayMs) !== null && _a !== void 0 ? _a : DEFAULT_RECEIPT_POLL_DELAY_MS;
|
|
150
|
+
this.scheduleReceiptPoll = (_b = options === null || options === void 0 ? void 0 : options.scheduleReceiptPoll) !== null && _b !== void 0 ? _b : defaultScheduleReceiptPoll;
|
|
151
|
+
if (options === null || options === void 0 ? void 0 : options.client) {
|
|
152
|
+
this.client = options.client;
|
|
153
|
+
this.isExpoPushToken = (_c = options.isExpoPushToken) !== null && _c !== void 0 ? _c : defaultIsExpoPushToken;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
var ExpoSdk = loadExpoSdk();
|
|
157
|
+
var accessToken = resolveAccessToken(options);
|
|
158
|
+
this.client = new ExpoSdk.Expo(accessToken ? { accessToken: accessToken } : {});
|
|
159
|
+
this.isExpoPushToken = (_d = options === null || options === void 0 ? void 0 : options.isExpoPushToken) !== null && _d !== void 0 ? _d : ExpoSdk.Expo.isExpoPushToken;
|
|
160
|
+
}
|
|
161
|
+
ExpoPushProvider.prototype.sendPush = function (message) {
|
|
162
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
163
|
+
var results, validIndexes, expoMessages, chunks, cursor, newReceipts, _loop_1, this_1, chunks_1, chunks_1_1, chunk, e_1_1;
|
|
164
|
+
var e_1, _a;
|
|
165
|
+
var _this = this;
|
|
166
|
+
return __generator(this, function (_b) {
|
|
167
|
+
switch (_b.label) {
|
|
168
|
+
case 0:
|
|
169
|
+
results = message.tokens.map(function (token) {
|
|
170
|
+
return _this.isExpoPushToken(token) ? { accepted: false, errorCode: "pending" } : invalidTokenResult();
|
|
171
|
+
});
|
|
172
|
+
validIndexes = [];
|
|
173
|
+
expoMessages = [];
|
|
174
|
+
message.tokens.forEach(function (token, index) {
|
|
175
|
+
if (!_this.isExpoPushToken(token)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
validIndexes.push(index);
|
|
179
|
+
expoMessages.push({
|
|
180
|
+
badge: message.badge,
|
|
181
|
+
body: message.body,
|
|
182
|
+
data: message.data,
|
|
183
|
+
sound: message.sound,
|
|
184
|
+
title: message.title,
|
|
185
|
+
to: token,
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
if (expoMessages.length === 0) {
|
|
189
|
+
return [2 /*return*/, results.map(function (result) {
|
|
190
|
+
return result.errorCode === "pending" ? invalidTokenResult() : result;
|
|
191
|
+
})];
|
|
192
|
+
}
|
|
193
|
+
chunks = this.client.chunkPushNotifications(expoMessages);
|
|
194
|
+
cursor = 0;
|
|
195
|
+
newReceipts = [];
|
|
196
|
+
_loop_1 = function (chunk) {
|
|
197
|
+
var tickets, error_1, errorMessage, offset, tokenIndex;
|
|
198
|
+
return __generator(this, function (_c) {
|
|
199
|
+
switch (_c.label) {
|
|
200
|
+
case 0:
|
|
201
|
+
_c.trys.push([0, 2, , 3]);
|
|
202
|
+
return [4 /*yield*/, this_1.client.sendPushNotificationsAsync(chunk)];
|
|
203
|
+
case 1:
|
|
204
|
+
tickets = _c.sent();
|
|
205
|
+
return [3 /*break*/, 3];
|
|
206
|
+
case 2:
|
|
207
|
+
error_1 = _c.sent();
|
|
208
|
+
errorMessage = error_1 instanceof Error ? error_1.message : "Expo send failed";
|
|
209
|
+
for (offset = 0; offset < chunk.length; offset += 1) {
|
|
210
|
+
tokenIndex = validIndexes[cursor + offset];
|
|
211
|
+
if (tokenIndex === undefined) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
results[tokenIndex] = failedResult({
|
|
215
|
+
error: errorMessage,
|
|
216
|
+
errorClass: "transient",
|
|
217
|
+
errorCode: "expo-send-throw",
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
cursor += chunk.length;
|
|
221
|
+
return [2 /*return*/, "continue"];
|
|
222
|
+
case 3:
|
|
223
|
+
chunk.forEach(function (expoMessage, offset) {
|
|
224
|
+
var tokenIndex = validIndexes[cursor + offset];
|
|
225
|
+
if (tokenIndex === undefined) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
var result = ticketToResult(tickets[offset]);
|
|
229
|
+
results[tokenIndex] = result;
|
|
230
|
+
if (result.accepted && result.providerMessageId) {
|
|
231
|
+
_this.pendingReceipts.set(result.providerMessageId, String(expoMessage.to));
|
|
232
|
+
newReceipts.push(result.providerMessageId);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
cursor += chunk.length;
|
|
236
|
+
return [2 /*return*/];
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
this_1 = this;
|
|
241
|
+
_b.label = 1;
|
|
242
|
+
case 1:
|
|
243
|
+
_b.trys.push([1, 6, 7, 8]);
|
|
244
|
+
chunks_1 = __values(chunks), chunks_1_1 = chunks_1.next();
|
|
245
|
+
_b.label = 2;
|
|
246
|
+
case 2:
|
|
247
|
+
if (!!chunks_1_1.done) return [3 /*break*/, 5];
|
|
248
|
+
chunk = chunks_1_1.value;
|
|
249
|
+
return [5 /*yield**/, _loop_1(chunk)];
|
|
250
|
+
case 3:
|
|
251
|
+
_b.sent();
|
|
252
|
+
_b.label = 4;
|
|
253
|
+
case 4:
|
|
254
|
+
chunks_1_1 = chunks_1.next();
|
|
255
|
+
return [3 /*break*/, 2];
|
|
256
|
+
case 5: return [3 /*break*/, 8];
|
|
257
|
+
case 6:
|
|
258
|
+
e_1_1 = _b.sent();
|
|
259
|
+
e_1 = { error: e_1_1 };
|
|
260
|
+
return [3 /*break*/, 8];
|
|
261
|
+
case 7:
|
|
262
|
+
try {
|
|
263
|
+
if (chunks_1_1 && !chunks_1_1.done && (_a = chunks_1.return)) _a.call(chunks_1);
|
|
264
|
+
}
|
|
265
|
+
finally { if (e_1) throw e_1.error; }
|
|
266
|
+
return [7 /*endfinally*/];
|
|
267
|
+
case 8:
|
|
268
|
+
if (newReceipts.length > 0) {
|
|
269
|
+
this.scheduleReceiptPoll(this.receiptPollDelayMs, function () { return _this.pollReceipts(newReceipts); });
|
|
270
|
+
}
|
|
271
|
+
return [2 /*return*/, results.map(function (result) {
|
|
272
|
+
return result.errorCode === "pending"
|
|
273
|
+
? failedResult({
|
|
274
|
+
error: "Expo returned no ticket for this token",
|
|
275
|
+
errorClass: "transient",
|
|
276
|
+
errorCode: "expo-missing-ticket",
|
|
277
|
+
})
|
|
278
|
+
: result;
|
|
279
|
+
})];
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
ExpoPushProvider.prototype.pollReceipts = function (receiptIds) {
|
|
285
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
286
|
+
var chunks, chunks_2, chunks_2_1, chunk, receipts, error_2, chunk_1, chunk_1_1, receiptId, e_2_1;
|
|
287
|
+
var e_2, _a, e_3, _b;
|
|
288
|
+
var _this = this;
|
|
289
|
+
return __generator(this, function (_c) {
|
|
290
|
+
switch (_c.label) {
|
|
291
|
+
case 0:
|
|
292
|
+
chunks = this.client.chunkPushNotificationReceiptIds(receiptIds);
|
|
293
|
+
_c.label = 1;
|
|
294
|
+
case 1:
|
|
295
|
+
_c.trys.push([1, 10, 11, 12]);
|
|
296
|
+
chunks_2 = __values(chunks), chunks_2_1 = chunks_2.next();
|
|
297
|
+
_c.label = 2;
|
|
298
|
+
case 2:
|
|
299
|
+
if (!!chunks_2_1.done) return [3 /*break*/, 9];
|
|
300
|
+
chunk = chunks_2_1.value;
|
|
301
|
+
receipts = void 0;
|
|
302
|
+
_c.label = 3;
|
|
303
|
+
case 3:
|
|
304
|
+
_c.trys.push([3, 5, , 6]);
|
|
305
|
+
return [4 /*yield*/, this.client.getPushNotificationReceiptsAsync(chunk)];
|
|
306
|
+
case 4:
|
|
307
|
+
receipts = _c.sent();
|
|
308
|
+
return [3 /*break*/, 6];
|
|
309
|
+
case 5:
|
|
310
|
+
error_2 = _c.sent();
|
|
311
|
+
api_1.logger.warn("[comms:expo] Receipt poll failed: ".concat(String(error_2)));
|
|
312
|
+
try {
|
|
313
|
+
for (chunk_1 = (e_3 = void 0, __values(chunk)), chunk_1_1 = chunk_1.next(); !chunk_1_1.done; chunk_1_1 = chunk_1.next()) {
|
|
314
|
+
receiptId = chunk_1_1.value;
|
|
315
|
+
this.pendingReceipts.delete(receiptId);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
319
|
+
finally {
|
|
320
|
+
try {
|
|
321
|
+
if (chunk_1_1 && !chunk_1_1.done && (_b = chunk_1.return)) _b.call(chunk_1);
|
|
322
|
+
}
|
|
323
|
+
finally { if (e_3) throw e_3.error; }
|
|
324
|
+
}
|
|
325
|
+
return [3 /*break*/, 8];
|
|
326
|
+
case 6: return [4 /*yield*/, Promise.all(Object.entries(receipts).map(function (_a) {
|
|
327
|
+
var _b = __read(_a, 2), receiptId = _b[0], receipt = _b[1];
|
|
328
|
+
return _this.applyReceipt(receiptId, receipt);
|
|
329
|
+
}))];
|
|
330
|
+
case 7:
|
|
331
|
+
_c.sent();
|
|
332
|
+
_c.label = 8;
|
|
333
|
+
case 8:
|
|
334
|
+
chunks_2_1 = chunks_2.next();
|
|
335
|
+
return [3 /*break*/, 2];
|
|
336
|
+
case 9: return [3 /*break*/, 12];
|
|
337
|
+
case 10:
|
|
338
|
+
e_2_1 = _c.sent();
|
|
339
|
+
e_2 = { error: e_2_1 };
|
|
340
|
+
return [3 /*break*/, 12];
|
|
341
|
+
case 11:
|
|
342
|
+
try {
|
|
343
|
+
if (chunks_2_1 && !chunks_2_1.done && (_a = chunks_2.return)) _a.call(chunks_2);
|
|
344
|
+
}
|
|
345
|
+
finally { if (e_2) throw e_2.error; }
|
|
346
|
+
return [7 /*endfinally*/];
|
|
347
|
+
case 12: return [2 /*return*/];
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
};
|
|
352
|
+
ExpoPushProvider.prototype.applyReceipt = function (receiptId, receipt) {
|
|
353
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
354
|
+
var token, _a, errorClass, errorCode;
|
|
355
|
+
var _b;
|
|
356
|
+
return __generator(this, function (_c) {
|
|
357
|
+
switch (_c.label) {
|
|
358
|
+
case 0:
|
|
359
|
+
token = this.pendingReceipts.get(receiptId);
|
|
360
|
+
this.pendingReceipts.delete(receiptId);
|
|
361
|
+
if (!(receipt.status === "ok")) return [3 /*break*/, 3];
|
|
362
|
+
if (!this.onDeliveryEvent) return [3 /*break*/, 2];
|
|
363
|
+
return [4 /*yield*/, this.onDeliveryEvent({
|
|
364
|
+
channel: "push",
|
|
365
|
+
providerMessageId: receiptId,
|
|
366
|
+
raw: receipt,
|
|
367
|
+
status: "delivered",
|
|
368
|
+
})];
|
|
369
|
+
case 1:
|
|
370
|
+
_c.sent();
|
|
371
|
+
_c.label = 2;
|
|
372
|
+
case 2: return [2 /*return*/];
|
|
373
|
+
case 3:
|
|
374
|
+
_a = classifyExpoError((_b = receipt.details) === null || _b === void 0 ? void 0 : _b.error), errorClass = _a.errorClass, errorCode = _a.errorCode;
|
|
375
|
+
if (!this.onDeliveryEvent) return [3 /*break*/, 5];
|
|
376
|
+
return [4 /*yield*/, this.onDeliveryEvent({
|
|
377
|
+
channel: "push",
|
|
378
|
+
errorClass: errorClass,
|
|
379
|
+
errorCode: errorCode,
|
|
380
|
+
providerMessageId: receiptId,
|
|
381
|
+
raw: receipt,
|
|
382
|
+
status: "failed",
|
|
383
|
+
})];
|
|
384
|
+
case 4:
|
|
385
|
+
_c.sent();
|
|
386
|
+
_c.label = 5;
|
|
387
|
+
case 5:
|
|
388
|
+
if (!(token && errorClass === "permanent" && this.onDeadToken)) return [3 /*break*/, 7];
|
|
389
|
+
return [4 /*yield*/, this.onDeadToken(token)];
|
|
390
|
+
case 6:
|
|
391
|
+
_c.sent();
|
|
392
|
+
_c.label = 7;
|
|
393
|
+
case 7: return [2 /*return*/];
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
};
|
|
398
|
+
return ExpoPushProvider;
|
|
399
|
+
}());
|
|
400
|
+
exports.ExpoPushProvider = ExpoPushProvider;
|
|
@@ -14,6 +14,7 @@ export interface SendGridMailProviderOptions {
|
|
|
14
14
|
fromEmail?: string;
|
|
15
15
|
fromName?: string;
|
|
16
16
|
sandboxMode?: boolean;
|
|
17
|
+
webhookVerificationKey?: string;
|
|
17
18
|
}
|
|
18
19
|
export declare class SendGridMailProvider implements MailProvider {
|
|
19
20
|
readonly id = "sendgrid";
|
|
@@ -21,7 +22,9 @@ export declare class SendGridMailProvider implements MailProvider {
|
|
|
21
22
|
private readonly fromEmail?;
|
|
22
23
|
private readonly fromName?;
|
|
23
24
|
private readonly sandboxMode;
|
|
25
|
+
private readonly webhookVerificationKey?;
|
|
24
26
|
constructor(options?: SendGridMailProviderOptions);
|
|
27
|
+
getWebhookVerificationKey: () => string | undefined;
|
|
25
28
|
sendMail(message: MailMessage): Promise<SendResult>;
|
|
26
29
|
}
|
|
27
30
|
export {};
|
|
@@ -165,12 +165,18 @@ var bodyFromError = function (error) {
|
|
|
165
165
|
};
|
|
166
166
|
var SendGridMailProvider = /** @class */ (function () {
|
|
167
167
|
function SendGridMailProvider(options) {
|
|
168
|
-
var
|
|
168
|
+
var _this = this;
|
|
169
|
+
var _a, _b;
|
|
169
170
|
this.id = "sendgrid";
|
|
171
|
+
this.getWebhookVerificationKey = function () {
|
|
172
|
+
return _this.webhookVerificationKey;
|
|
173
|
+
};
|
|
170
174
|
var apiKey = resolveApiKey(options);
|
|
171
175
|
this.fromEmail = options === null || options === void 0 ? void 0 : options.fromEmail;
|
|
172
176
|
this.fromName = options === null || options === void 0 ? void 0 : options.fromName;
|
|
173
177
|
this.sandboxMode = (_a = options === null || options === void 0 ? void 0 : options.sandboxMode) !== null && _a !== void 0 ? _a : process.env.NODE_ENV === "test";
|
|
178
|
+
this.webhookVerificationKey =
|
|
179
|
+
(_b = options === null || options === void 0 ? void 0 : options.webhookVerificationKey) !== null && _b !== void 0 ? _b : process.env.SENDGRID_WEBHOOK_VERIFICATION_KEY;
|
|
174
180
|
if (options === null || options === void 0 ? void 0 : options.client) {
|
|
175
181
|
this.client = options.client;
|
|
176
182
|
return;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { SendResult, SmsMessage, SmsProvider } from "../types";
|
|
2
|
+
export interface TwilioMessageCreateParams {
|
|
3
|
+
body: string;
|
|
4
|
+
from?: string;
|
|
5
|
+
messagingServiceSid?: string;
|
|
6
|
+
statusCallback?: string;
|
|
7
|
+
to: string;
|
|
8
|
+
}
|
|
9
|
+
export interface TwilioSmsClient {
|
|
10
|
+
messages: {
|
|
11
|
+
create: (params: TwilioMessageCreateParams) => Promise<{
|
|
12
|
+
sid: string;
|
|
13
|
+
}>;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface TwilioSmsProviderOptions {
|
|
17
|
+
accountSid?: string;
|
|
18
|
+
authToken?: string;
|
|
19
|
+
/** Injected client for tests. Production wiring loads `twilio`. */
|
|
20
|
+
client?: TwilioSmsClient;
|
|
21
|
+
fromNumber?: string;
|
|
22
|
+
messagingServiceSid?: string;
|
|
23
|
+
statusCallbackUrl?: string;
|
|
24
|
+
}
|
|
25
|
+
export declare class TwilioSmsProvider implements SmsProvider {
|
|
26
|
+
readonly id = "twilio";
|
|
27
|
+
private readonly authToken;
|
|
28
|
+
private readonly client;
|
|
29
|
+
private readonly fromNumber?;
|
|
30
|
+
private readonly messagingServiceSid?;
|
|
31
|
+
private statusCallbackUrl?;
|
|
32
|
+
constructor(options?: TwilioSmsProviderOptions);
|
|
33
|
+
getAuthToken: () => string;
|
|
34
|
+
applyDefaultStatusCallbackUrl: (url: string) => void;
|
|
35
|
+
sendSms(message: SmsMessage): Promise<SendResult>;
|
|
36
|
+
}
|