@terreno/comms 57.4.0 → 57.6.1
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 +2 -2
- package/dist/adapters/console.js +48 -109
- package/dist/adapters/expoPush.js +159 -345
- package/dist/adapters/sendgrid.js +94 -164
- package/dist/adapters/twilioSms.js +96 -151
- package/dist/adapters/twilioVerify.js +70 -132
- package/dist/commsApp.js +27 -27
- package/dist/commsQuery.js +33 -34
- package/dist/commsRetry.js +17 -25
- package/dist/commsService.js +674 -1098
- package/dist/models/commsMessage.js +64 -172
- package/dist/models/pushToken.js +5 -5
- package/dist/routes/commsDashboard.js +174 -325
- package/dist/routes/pushTokens.js +122 -217
- package/dist/templates.js +27 -41
- package/dist/webhooks/sendgridWebhooks.js +109 -226
- package/dist/webhooks/twilioWebhooks.js +59 -117
- package/package.json +6 -6
- package/tsconfig.json +6 -20
|
@@ -1,250 +1,133 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
3
|
exports.maybeRegisterSendGridCommsWebhooks = void 0;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const sendgrid_1 = require("../adapters/sendgrid");
|
|
6
|
+
const commsMessage_1 = require("../models/commsMessage");
|
|
7
|
+
const eventString = (value) => {
|
|
55
8
|
return typeof value === "string" ? value : "";
|
|
56
9
|
};
|
|
57
|
-
|
|
10
|
+
const escapeRegex = (value) => {
|
|
58
11
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
59
12
|
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
case 1:
|
|
72
|
-
exact = _c.sent();
|
|
73
|
-
if (exact === null || exact === void 0 ? void 0 : exact.providerMessageId) {
|
|
74
|
-
return [2 /*return*/, exact.providerMessageId];
|
|
75
|
-
}
|
|
76
|
-
return [4 /*yield*/, commsMessage_1.CommsMessage.findOneOrNone({
|
|
77
|
-
providerMessageId: new RegExp("^".concat(escapeRegex(prefix))),
|
|
78
|
-
})];
|
|
79
|
-
case 2:
|
|
80
|
-
prefixed = _c.sent();
|
|
81
|
-
return [2 /*return*/, (_b = prefixed === null || prefixed === void 0 ? void 0 : prefixed.providerMessageId) !== null && _b !== void 0 ? _b : prefix];
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}); };
|
|
85
|
-
var applySendGridEvent = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
86
|
-
var type, providerMessageId, reason, email, isSoft, optOut;
|
|
87
|
-
var event = _b.event, service = _b.service;
|
|
88
|
-
return __generator(this, function (_c) {
|
|
89
|
-
switch (_c.label) {
|
|
90
|
-
case 0:
|
|
91
|
-
type = eventString(event.event);
|
|
92
|
-
return [4 /*yield*/, storedProviderMessageId(eventString(event.sg_message_id))];
|
|
93
|
-
case 1:
|
|
94
|
-
providerMessageId = _c.sent();
|
|
95
|
-
reason = eventString(event.reason);
|
|
96
|
-
email = eventString(event.email);
|
|
97
|
-
if (!(type === "delivered")) return [3 /*break*/, 3];
|
|
98
|
-
return [4 /*yield*/, service.recordDeliveryEvent({
|
|
99
|
-
channel: "mail",
|
|
100
|
-
providerMessageId: providerMessageId,
|
|
101
|
-
raw: event,
|
|
102
|
-
status: "delivered",
|
|
103
|
-
})];
|
|
104
|
-
case 2:
|
|
105
|
-
_c.sent();
|
|
106
|
-
return [2 /*return*/];
|
|
107
|
-
case 3:
|
|
108
|
-
if (!(type === "open")) return [3 /*break*/, 5];
|
|
109
|
-
return [4 /*yield*/, service.recordDeliveryEvent({
|
|
110
|
-
channel: "mail",
|
|
111
|
-
providerMessageId: providerMessageId,
|
|
112
|
-
raw: event,
|
|
113
|
-
status: "opened",
|
|
114
|
-
})];
|
|
115
|
-
case 4:
|
|
116
|
-
_c.sent();
|
|
117
|
-
return [2 /*return*/];
|
|
118
|
-
case 5:
|
|
119
|
-
if (!(type === "bounce")) return [3 /*break*/, 7];
|
|
120
|
-
isSoft = eventString(event.type) === "blocked";
|
|
121
|
-
return [4 /*yield*/, service.recordDeliveryEvent({
|
|
122
|
-
channel: "mail",
|
|
123
|
-
errorClass: isSoft ? "transient" : "permanent",
|
|
124
|
-
errorCode: reason || (isSoft ? "deferred" : "bounce"),
|
|
125
|
-
providerMessageId: providerMessageId,
|
|
126
|
-
raw: event,
|
|
127
|
-
status: "bounced",
|
|
128
|
-
})];
|
|
129
|
-
case 6:
|
|
130
|
-
_c.sent();
|
|
131
|
-
return [2 /*return*/];
|
|
132
|
-
case 7:
|
|
133
|
-
if (!(type === "dropped")) return [3 /*break*/, 9];
|
|
134
|
-
return [4 /*yield*/, service.recordDeliveryEvent({
|
|
135
|
-
channel: "mail",
|
|
136
|
-
errorClass: "permanent",
|
|
137
|
-
errorCode: reason || "dropped",
|
|
138
|
-
providerMessageId: providerMessageId,
|
|
139
|
-
raw: event,
|
|
140
|
-
status: "failed",
|
|
141
|
-
})];
|
|
142
|
-
case 8:
|
|
143
|
-
_c.sent();
|
|
144
|
-
return [2 /*return*/];
|
|
145
|
-
case 9:
|
|
146
|
-
if (!(type === "spamreport" || type === "unsubscribe" || type === "group_unsubscribe")) return [3 /*break*/, 11];
|
|
147
|
-
optOut = {
|
|
148
|
-
channel: "mail",
|
|
149
|
-
provider: "sendgrid",
|
|
150
|
-
raw: event,
|
|
151
|
-
reason: type,
|
|
152
|
-
to: email,
|
|
153
|
-
};
|
|
154
|
-
return [4 /*yield*/, service.recordOptOut(optOut)];
|
|
155
|
-
case 10:
|
|
156
|
-
_c.sent();
|
|
157
|
-
_c.label = 11;
|
|
158
|
-
case 11: return [2 /*return*/];
|
|
159
|
-
}
|
|
13
|
+
const storedProviderMessageId = async (sgMessageId) => {
|
|
14
|
+
const prefix = sgMessageId.split(".")[0] ?? sgMessageId;
|
|
15
|
+
if (!prefix) {
|
|
16
|
+
return sgMessageId;
|
|
17
|
+
}
|
|
18
|
+
const exact = await commsMessage_1.CommsMessage.findOneOrNone({ providerMessageId: prefix });
|
|
19
|
+
if (exact?.providerMessageId) {
|
|
20
|
+
return exact.providerMessageId;
|
|
21
|
+
}
|
|
22
|
+
const prefixed = await commsMessage_1.CommsMessage.findOneOrNone({
|
|
23
|
+
providerMessageId: new RegExp(`^${escapeRegex(prefix)}`),
|
|
160
24
|
});
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
25
|
+
return prefixed?.providerMessageId ?? prefix;
|
|
26
|
+
};
|
|
27
|
+
const applySendGridEvent = async ({ event, service, }) => {
|
|
28
|
+
const type = eventString(event.event);
|
|
29
|
+
const providerMessageId = await storedProviderMessageId(eventString(event.sg_message_id));
|
|
30
|
+
const reason = eventString(event.reason);
|
|
31
|
+
const email = eventString(event.email);
|
|
32
|
+
if (type === "delivered") {
|
|
33
|
+
await service.recordDeliveryEvent({
|
|
34
|
+
channel: "mail",
|
|
35
|
+
providerMessageId,
|
|
36
|
+
raw: event,
|
|
37
|
+
status: "delivered",
|
|
38
|
+
});
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (type === "open") {
|
|
42
|
+
await service.recordDeliveryEvent({
|
|
43
|
+
channel: "mail",
|
|
44
|
+
providerMessageId,
|
|
45
|
+
raw: event,
|
|
46
|
+
status: "opened",
|
|
47
|
+
});
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (type === "bounce") {
|
|
51
|
+
const isSoft = eventString(event.type) === "blocked";
|
|
52
|
+
await service.recordDeliveryEvent({
|
|
53
|
+
channel: "mail",
|
|
54
|
+
errorClass: isSoft ? "transient" : "permanent",
|
|
55
|
+
errorCode: reason || (isSoft ? "deferred" : "bounce"),
|
|
56
|
+
providerMessageId,
|
|
57
|
+
raw: event,
|
|
58
|
+
status: "bounced",
|
|
59
|
+
});
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (type === "dropped") {
|
|
63
|
+
await service.recordDeliveryEvent({
|
|
64
|
+
channel: "mail",
|
|
65
|
+
errorClass: "permanent",
|
|
66
|
+
errorCode: reason || "dropped",
|
|
67
|
+
providerMessageId,
|
|
68
|
+
raw: event,
|
|
69
|
+
status: "failed",
|
|
70
|
+
});
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (type === "spamreport" || type === "unsubscribe" || type === "group_unsubscribe") {
|
|
74
|
+
const optOut = {
|
|
75
|
+
channel: "mail",
|
|
76
|
+
provider: "sendgrid",
|
|
77
|
+
raw: event,
|
|
78
|
+
reason: type,
|
|
79
|
+
to: email,
|
|
80
|
+
};
|
|
81
|
+
await service.recordOptOut(optOut);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const registerSendGridCommsWebhooks = ({ basePath, publicKey, service, webhooks, }) => {
|
|
164
85
|
webhooks.route({
|
|
165
|
-
handler:
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
if (!!body_1_1.done) return [3 /*break*/, 9];
|
|
182
|
-
item = body_1_1.value;
|
|
183
|
-
if (!item || typeof item !== "object" || Array.isArray(item)) {
|
|
184
|
-
return [3 /*break*/, 8];
|
|
185
|
-
}
|
|
186
|
-
event_1 = item;
|
|
187
|
-
eventId = eventString(event_1.sg_event_id);
|
|
188
|
-
if (!eventId) {
|
|
189
|
-
return [3 /*break*/, 8];
|
|
190
|
-
}
|
|
191
|
-
return [4 /*yield*/, webhooks.claim({ eventId: eventId, source: "sendgrid" })];
|
|
192
|
-
case 3:
|
|
193
|
-
claimResult = _d.sent();
|
|
194
|
-
if (claimResult === "duplicate") {
|
|
195
|
-
return [3 /*break*/, 8];
|
|
196
|
-
}
|
|
197
|
-
_d.label = 4;
|
|
198
|
-
case 4:
|
|
199
|
-
_d.trys.push([4, 6, , 8]);
|
|
200
|
-
return [4 /*yield*/, applySendGridEvent({ event: event_1, service: service })];
|
|
201
|
-
case 5:
|
|
202
|
-
_d.sent();
|
|
203
|
-
return [3 /*break*/, 8];
|
|
204
|
-
case 6:
|
|
205
|
-
error_1 = _d.sent();
|
|
206
|
-
return [4 /*yield*/, webhooks.release({ eventId: eventId, source: "sendgrid" })];
|
|
207
|
-
case 7:
|
|
208
|
-
_d.sent();
|
|
209
|
-
throw error_1;
|
|
210
|
-
case 8:
|
|
211
|
-
body_1_1 = body_1.next();
|
|
212
|
-
return [3 /*break*/, 2];
|
|
213
|
-
case 9: return [3 /*break*/, 12];
|
|
214
|
-
case 10:
|
|
215
|
-
e_1_1 = _d.sent();
|
|
216
|
-
e_1 = { error: e_1_1 };
|
|
217
|
-
return [3 /*break*/, 12];
|
|
218
|
-
case 11:
|
|
219
|
-
try {
|
|
220
|
-
if (body_1_1 && !body_1_1.done && (_c = body_1.return)) _c.call(body_1);
|
|
221
|
-
}
|
|
222
|
-
finally { if (e_1) throw e_1.error; }
|
|
223
|
-
return [7 /*endfinally*/];
|
|
224
|
-
case 12: return [2 /*return*/];
|
|
86
|
+
handler: async ({ body }) => {
|
|
87
|
+
if (!Array.isArray(body)) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
for (const item of body) {
|
|
91
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const event = item;
|
|
95
|
+
const eventId = eventString(event.sg_event_id);
|
|
96
|
+
if (!eventId) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const claimResult = await webhooks.claim({ eventId, source: "sendgrid" });
|
|
100
|
+
if (claimResult === "duplicate") {
|
|
101
|
+
continue;
|
|
225
102
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
103
|
+
try {
|
|
104
|
+
await applySendGridEvent({ event, service });
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
await webhooks.release({ eventId, source: "sendgrid" });
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
path: `${basePath}/webhooks/sendgrid`,
|
|
229
113
|
source: "sendgrid",
|
|
230
|
-
verify: (0, api_1.sendgridEventSignature)({ publicKey
|
|
114
|
+
verify: (0, api_1.sendgridEventSignature)({ publicKey }),
|
|
231
115
|
});
|
|
232
116
|
};
|
|
233
|
-
|
|
234
|
-
var basePath = _a.basePath, mail = _a.mail, service = _a.service, webhooks = _a.webhooks;
|
|
117
|
+
const maybeRegisterSendGridCommsWebhooks = ({ basePath, mail, service, webhooks, }) => {
|
|
235
118
|
if (!webhooks || !(mail instanceof sendgrid_1.SendGridMailProvider)) {
|
|
236
119
|
return;
|
|
237
120
|
}
|
|
238
|
-
|
|
121
|
+
const publicKey = mail.getWebhookVerificationKey();
|
|
239
122
|
if (!publicKey) {
|
|
240
123
|
api_1.logger.error("[comms] Skipping SendGrid webhook routes; set webhookVerificationKey or SENDGRID_WEBHOOK_VERIFICATION_KEY");
|
|
241
124
|
return;
|
|
242
125
|
}
|
|
243
126
|
registerSendGridCommsWebhooks({
|
|
244
|
-
basePath
|
|
245
|
-
publicKey
|
|
246
|
-
service
|
|
247
|
-
webhooks
|
|
127
|
+
basePath,
|
|
128
|
+
publicKey,
|
|
129
|
+
service,
|
|
130
|
+
webhooks,
|
|
248
131
|
});
|
|
249
132
|
};
|
|
250
133
|
exports.maybeRegisterSendGridCommsWebhooks = maybeRegisterSendGridCommsWebhooks;
|
|
@@ -1,54 +1,17 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.maybeRegisterTwilioCommsWebhooks = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const twilioSms_1 = require("../adapters/twilioSms");
|
|
6
|
+
const formString = (body, key) => {
|
|
43
7
|
if (!body || typeof body !== "object") {
|
|
44
8
|
return "";
|
|
45
9
|
}
|
|
46
|
-
|
|
10
|
+
const value = body[key];
|
|
47
11
|
return typeof value === "string" ? value : "";
|
|
48
12
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
var keyword = (_a = body.trim().split(/\s+/)[0]) === null || _a === void 0 ? void 0 : _a.toUpperCase();
|
|
13
|
+
const inboundKeyword = (body) => {
|
|
14
|
+
const keyword = body.trim().split(/\s+/)[0]?.toUpperCase();
|
|
52
15
|
if (keyword === "STOP") {
|
|
53
16
|
return "sms-stop";
|
|
54
17
|
}
|
|
@@ -57,7 +20,7 @@ var inboundKeyword = function (body) {
|
|
|
57
20
|
}
|
|
58
21
|
return undefined;
|
|
59
22
|
};
|
|
60
|
-
|
|
23
|
+
const mapStatus = (messageStatus, errorCode) => {
|
|
61
24
|
if (messageStatus === "delivered") {
|
|
62
25
|
return { status: "delivered" };
|
|
63
26
|
}
|
|
@@ -70,105 +33,84 @@ var mapStatus = function (messageStatus, errorCode) {
|
|
|
70
33
|
}
|
|
71
34
|
return undefined;
|
|
72
35
|
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
var inboundUrl = "".concat(publicUrl).concat(inboundPath);
|
|
36
|
+
const registerTwilioCommsWebhooks = ({ authToken, basePath, publicUrl, service, webhooks, }) => {
|
|
37
|
+
const statusPath = `${basePath}/webhooks/twilio/status`;
|
|
38
|
+
const inboundPath = `${basePath}/webhooks/twilio/inbound`;
|
|
39
|
+
const statusUrl = `${publicUrl}${statusPath}`;
|
|
40
|
+
const inboundUrl = `${publicUrl}${inboundPath}`;
|
|
79
41
|
webhooks.route({
|
|
80
|
-
eventId:
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return status ?
|
|
42
|
+
eventId: (req) => {
|
|
43
|
+
const sid = formString(req.body, "MessageSid");
|
|
44
|
+
const status = formString(req.body, "MessageStatus");
|
|
45
|
+
return status ? `${sid}:${status}` : sid;
|
|
84
46
|
},
|
|
85
|
-
handler:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
errorClass: mapped.errorClass,
|
|
99
|
-
errorCode: mapped.errorCode,
|
|
100
|
-
providerMessageId: formString(body, "MessageSid"),
|
|
101
|
-
raw: body,
|
|
102
|
-
status: mapped.status,
|
|
103
|
-
})];
|
|
104
|
-
case 1:
|
|
105
|
-
_c.sent();
|
|
106
|
-
return [2 /*return*/];
|
|
107
|
-
}
|
|
47
|
+
handler: async ({ body }) => {
|
|
48
|
+
const messageStatus = formString(body, "MessageStatus");
|
|
49
|
+
const mapped = mapStatus(messageStatus, formString(body, "ErrorCode"));
|
|
50
|
+
if (!mapped) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await service.recordDeliveryEvent({
|
|
54
|
+
channel: "sms",
|
|
55
|
+
errorClass: mapped.errorClass,
|
|
56
|
+
errorCode: mapped.errorCode,
|
|
57
|
+
providerMessageId: formString(body, "MessageSid"),
|
|
58
|
+
raw: body,
|
|
59
|
+
status: mapped.status,
|
|
108
60
|
});
|
|
109
|
-
}
|
|
61
|
+
},
|
|
110
62
|
path: statusPath,
|
|
111
63
|
source: "twilio-status",
|
|
112
|
-
verify: (0, api_1.twilioSignature)({ authToken
|
|
64
|
+
verify: (0, api_1.twilioSignature)({ authToken, url: statusUrl }),
|
|
113
65
|
});
|
|
114
66
|
webhooks.route({
|
|
115
|
-
eventId:
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return "".concat(sid, ":").concat(keyword);
|
|
67
|
+
eventId: (req) => {
|
|
68
|
+
const sid = formString(req.body, "SmsSid") || formString(req.body, "MessageSid");
|
|
69
|
+
const keyword = inboundKeyword(formString(req.body, "Body")) ?? "inbound";
|
|
70
|
+
return `${sid}:${keyword}`;
|
|
120
71
|
},
|
|
121
|
-
handler:
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
channel: "sms",
|
|
133
|
-
provider: "twilio",
|
|
134
|
-
raw: body,
|
|
135
|
-
reason: reason,
|
|
136
|
-
to: formString(body, "From"),
|
|
137
|
-
})];
|
|
138
|
-
case 1:
|
|
139
|
-
_c.sent();
|
|
140
|
-
return [2 /*return*/];
|
|
141
|
-
}
|
|
72
|
+
handler: async ({ body }) => {
|
|
73
|
+
const reason = inboundKeyword(formString(body, "Body"));
|
|
74
|
+
if (!reason) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
await service.recordOptOut({
|
|
78
|
+
channel: "sms",
|
|
79
|
+
provider: "twilio",
|
|
80
|
+
raw: body,
|
|
81
|
+
reason,
|
|
82
|
+
to: formString(body, "From"),
|
|
142
83
|
});
|
|
143
|
-
}
|
|
84
|
+
},
|
|
144
85
|
path: inboundPath,
|
|
145
86
|
source: "twilio-inbound",
|
|
146
|
-
verify: (0, api_1.twilioSignature)({ authToken
|
|
87
|
+
verify: (0, api_1.twilioSignature)({ authToken, url: inboundUrl }),
|
|
147
88
|
});
|
|
148
89
|
};
|
|
149
|
-
|
|
150
|
-
var _b, _c;
|
|
151
|
-
var basePath = _a.basePath, publicUrl = _a.publicUrl, service = _a.service, sms = _a.sms, webhooks = _a.webhooks;
|
|
90
|
+
const maybeRegisterTwilioCommsWebhooks = ({ basePath, publicUrl, service, sms, webhooks, }) => {
|
|
152
91
|
if (!webhooks || !(sms instanceof twilioSms_1.TwilioSmsProvider)) {
|
|
153
92
|
return;
|
|
154
93
|
}
|
|
155
|
-
|
|
94
|
+
const authToken = sms.getAuthToken();
|
|
156
95
|
if (!authToken) {
|
|
157
96
|
api_1.logger.error("[comms] Skipping Twilio webhook routes; auth token is missing");
|
|
158
97
|
return;
|
|
159
98
|
}
|
|
160
|
-
|
|
99
|
+
const resolvedPublicUrl = (publicUrl ??
|
|
100
|
+
process.env.PUBLIC_API_URL ??
|
|
101
|
+
process.env.COMMS_WEBHOOK_PUBLIC_URL ??
|
|
102
|
+
"").replace(/\/$/, "");
|
|
161
103
|
if (!resolvedPublicUrl) {
|
|
162
104
|
api_1.logger.error("[comms] Skipping Twilio webhook routes; set webhookPublicUrl, PUBLIC_API_URL, or COMMS_WEBHOOK_PUBLIC_URL");
|
|
163
105
|
return;
|
|
164
106
|
}
|
|
165
|
-
sms.applyDefaultStatusCallbackUrl(
|
|
107
|
+
sms.applyDefaultStatusCallbackUrl(`${resolvedPublicUrl}${basePath}/webhooks/twilio/status`);
|
|
166
108
|
registerTwilioCommsWebhooks({
|
|
167
|
-
authToken
|
|
168
|
-
basePath
|
|
109
|
+
authToken,
|
|
110
|
+
basePath,
|
|
169
111
|
publicUrl: resolvedPublicUrl,
|
|
170
|
-
service
|
|
171
|
-
webhooks
|
|
112
|
+
service,
|
|
113
|
+
webhooks,
|
|
172
114
|
});
|
|
173
115
|
};
|
|
174
116
|
exports.maybeRegisterTwilioCommsWebhooks = maybeRegisterTwilioCommsWebhooks;
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Josh Gachnang",
|
|
3
3
|
"bugs": {
|
|
4
|
-
"url": "https://github.com/
|
|
4
|
+
"url": "https://github.com/TerrenoLabs/terreno/issues"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@terreno/api": "57.
|
|
7
|
+
"@terreno/api": "57.6.1",
|
|
8
8
|
"libphonenumber-js": "^1.13.8",
|
|
9
9
|
"luxon": "^3.7.2"
|
|
10
10
|
},
|
|
11
11
|
"description": "Pluggable mail, SMS, push, and verification providers — part of Terreno, the batteries-included TypeScript framework for universal apps.",
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@biomejs/biome": "^2.3.6",
|
|
14
|
-
"@terreno/test": "57.
|
|
14
|
+
"@terreno/test": "57.6.1",
|
|
15
15
|
"@types/bun": "^1.4.0",
|
|
16
16
|
"@types/express": "^5.0.6",
|
|
17
17
|
"@types/luxon": "^3.7.1",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"types": "./dist/adapters/twilioVerify.d.ts"
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
|
-
"homepage": "https://github.com/
|
|
45
|
+
"homepage": "https://github.com/TerrenoLabs/terreno#readme",
|
|
46
46
|
"keywords": [
|
|
47
47
|
"communications",
|
|
48
48
|
"email",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
},
|
|
76
76
|
"repository": {
|
|
77
77
|
"type": "git",
|
|
78
|
-
"url": "git+ssh://git@github.com/
|
|
78
|
+
"url": "git+ssh://git@github.com/TerrenoLabs/terreno.git"
|
|
79
79
|
},
|
|
80
80
|
"scripts": {
|
|
81
81
|
"compile": "bun tsc",
|
|
@@ -89,5 +89,5 @@
|
|
|
89
89
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
90
90
|
},
|
|
91
91
|
"types": "dist/index.d.ts",
|
|
92
|
-
"version": "57.
|
|
92
|
+
"version": "57.6.1"
|
|
93
93
|
}
|