@teleporthq/teleport-plugin-next-workflows 0.45.6 → 0.45.9
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/__tests__/analytics-track-outcome.test.ts +56 -0
- package/__tests__/newsletter-plugin.test.ts +165 -0
- package/__tests__/server-job-self-calls-absolute.test.ts +430 -0
- package/dist/cjs/newsletter/helper-code.js +2 -2
- package/dist/cjs/newsletter/routes-code.d.ts.map +1 -1
- package/dist/cjs/newsletter/routes-code.js +64 -31
- package/dist/cjs/newsletter/routes-code.js.map +1 -1
- package/dist/cjs/nodes/analytics/analytics-track-outcome.d.ts.map +1 -1
- package/dist/cjs/nodes/analytics/analytics-track-outcome.js +12 -0
- package/dist/cjs/nodes/analytics/analytics-track-outcome.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/newsletter/helper-code.js +2 -2
- package/dist/esm/newsletter/routes-code.d.ts.map +1 -1
- package/dist/esm/newsletter/routes-code.js +64 -31
- package/dist/esm/newsletter/routes-code.js.map +1 -1
- package/dist/esm/nodes/analytics/analytics-track-outcome.d.ts.map +1 -1
- package/dist/esm/nodes/analytics/analytics-track-outcome.js +12 -0
- package/dist/esm/nodes/analytics/analytics-track-outcome.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/newsletter/helper-code.ts +2 -2
- package/src/newsletter/routes-code.ts +67 -31
- package/src/nodes/analytics/analytics-track-outcome.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teleporthq/teleport-plugin-next-workflows",
|
|
3
|
-
"version": "0.45.
|
|
3
|
+
"version": "0.45.9",
|
|
4
4
|
"description": "A plugin for workflow code generation in Next.js projects.",
|
|
5
5
|
"author": "teleportHQ",
|
|
6
6
|
"license": "MIT",
|
|
@@ -69,5 +69,5 @@
|
|
|
69
69
|
"@teleporthq/teleport-shared": "^0.45.0",
|
|
70
70
|
"@teleporthq/teleport-types": "^0.45.0"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "a9b0a45792908ae9e06ac7dc819da99a56febda0"
|
|
73
73
|
}
|
|
@@ -530,8 +530,8 @@ function canSendNewsletterEmail() {
|
|
|
530
530
|
|
|
531
531
|
// A link to one of the site's pages carries the token in its fragment: a
|
|
532
532
|
// fragment never reaches a server, a CDN log or a Referer (the page's first
|
|
533
|
-
// head script keeps it). The site's own unsubscribe
|
|
534
|
-
// server, so a link to
|
|
533
|
+
// head script keeps it). The site's own confirm and unsubscribe routes read it
|
|
534
|
+
// on the server, so a link to one of them carries it in the query.
|
|
535
535
|
function pageLink(path, token) {
|
|
536
536
|
return path + (path.indexOf('/api/') === 0 ? '?t=' : '#t=') + encodeURIComponent(token);
|
|
537
537
|
}
|
|
@@ -8,13 +8,15 @@
|
|
|
8
8
|
* `footer`, `popup`, `form`), else `block`; errors are logged by code and
|
|
9
9
|
* message only (never a database row); every answer is `{ ok: true, pending }`, after at least
|
|
10
10
|
* `NEWSLETTER_MIN_RESPONSE_MS`, whatever happened to the address.
|
|
11
|
-
* - `confirm` — POST `{ token }` → `{ ok, email? }
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* - `confirm` and `unsubscribe` — POST `{ token }` → `{ ok, email? }`; a GET
|
|
12
|
+
* with `?t=` (a click in a mail client) answers a small page whose button
|
|
13
|
+
* POSTs it, so a link scanner that opens every link confirms and
|
|
14
|
+
* unsubscribes nobody.
|
|
15
15
|
*/
|
|
16
16
|
import { securityRuntimeSpecifier } from '../security-runtime/security-runtime-code'
|
|
17
|
+
import { newsletterTokenSpec } from './helper-code'
|
|
17
18
|
import {
|
|
19
|
+
NEWSLETTER_CONFIRM_URL,
|
|
18
20
|
NEWSLETTER_UNSUBSCRIBE_URL,
|
|
19
21
|
NEWSLETTER_VISITOR_LIMIT,
|
|
20
22
|
NEWSLETTER_VISITOR_SCOPE,
|
|
@@ -86,32 +88,7 @@ ${methodGuard(['POST'])}
|
|
|
86
88
|
};
|
|
87
89
|
`
|
|
88
90
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
module.exports = async function handler(req, res) {
|
|
92
|
-
${methodGuard(['POST'])}
|
|
93
|
-
if (!(await __security.rateLimitRequest(req, res, { scope: 'newsletter-confirm', limit: ${NEWSLETTER_LINK_VISITOR_LIMIT}, windowSeconds: 3600 }))) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
var body = readBody(req);
|
|
97
|
-
var token = typeof body.token === 'string' ? body.token : '';
|
|
98
|
-
if (!token) {
|
|
99
|
-
return res.status(200).json({ ok: false });
|
|
100
|
-
}
|
|
101
|
-
try {
|
|
102
|
-
var result = await __newsletter.withNewsletterDb(function (db) {
|
|
103
|
-
return __newsletter.confirmNewsletter({ db: db, token: token, req: req });
|
|
104
|
-
});
|
|
105
|
-
return res.status(200).json(result.ok ? { ok: true, email: result.email } : { ok: false });
|
|
106
|
-
} catch (err) {
|
|
107
|
-
return __security.sendClientError(res, 503, 'We could not confirm the subscription. Please try again in a moment.', __newsletter.safeError(err));
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
`
|
|
111
|
-
|
|
112
|
-
export const generateNewsletterUnsubscribeRoute = (): string => `${ROUTE_PREAMBLE}
|
|
113
|
-
|
|
114
|
-
function escapeHtml(value) {
|
|
91
|
+
const LINK_PAGE_CODE = `function escapeHtml(value) {
|
|
115
92
|
return String(value == null ? '' : value)
|
|
116
93
|
.replace(/&/g, '&')
|
|
117
94
|
.replace(/</g, '<')
|
|
@@ -135,7 +112,66 @@ function sendPage(res, status, title, message, form) {
|
|
|
135
112
|
'button{font:inherit;padding:10px 18px;border:0;border-radius:6px;background:#111;color:#fff;cursor:pointer}</style>' +
|
|
136
113
|
'</head><body><h1>' + escapeHtml(title) + '</h1><p>' + escapeHtml(message) + '</p>' + (form || '') + '</body></html>'
|
|
137
114
|
);
|
|
138
|
-
}
|
|
115
|
+
}`
|
|
116
|
+
|
|
117
|
+
const CONFIRM_LINK_DAYS = newsletterTokenSpec.confirmMaxAgeSeconds / 86400
|
|
118
|
+
|
|
119
|
+
export const generateNewsletterConfirmRoute = (): string => `${ROUTE_PREAMBLE}
|
|
120
|
+
|
|
121
|
+
${LINK_PAGE_CODE}
|
|
122
|
+
|
|
123
|
+
var NOT_A_LINK = 'This confirmation link does not work. A link works for ${CONFIRM_LINK_DAYS} days: sign up again to get a new one.';
|
|
124
|
+
|
|
125
|
+
module.exports = async function handler(req, res) {
|
|
126
|
+
${methodGuard(['GET', 'POST'])}
|
|
127
|
+
if (!(await __security.rateLimitRequest(req, res, { scope: 'newsletter-confirm', limit: ${NEWSLETTER_LINK_VISITOR_LIMIT}, windowSeconds: 3600 }))) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (req.method === 'GET') {
|
|
131
|
+
// Opening the link changes nothing (a mail client's link scanner opens it
|
|
132
|
+
// too, and double opt-in means the person's own action): the button does.
|
|
133
|
+
var linkToken = req.query && typeof req.query.t === 'string' ? req.query.t : '';
|
|
134
|
+
if (!__newsletter.verifyNewsletterToken(linkToken, 'confirm')) {
|
|
135
|
+
return sendPage(res, 200, 'Link not valid', NOT_A_LINK, '');
|
|
136
|
+
}
|
|
137
|
+
return sendPage(
|
|
138
|
+
res,
|
|
139
|
+
200,
|
|
140
|
+
'Confirm your subscription',
|
|
141
|
+
'Receive the newsletter at this address?',
|
|
142
|
+
'<form method="post" action="${NEWSLETTER_CONFIRM_URL}"><input type="hidden" name="t" value="' +
|
|
143
|
+
escapeHtml(linkToken) + '"><button type="submit">Confirm subscription</button></form>'
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
var body = readBody(req);
|
|
147
|
+
var fromForm = String((req.headers && req.headers['content-type']) || '').indexOf('application/x-www-form-urlencoded') !== -1;
|
|
148
|
+
var token = typeof body.token === 'string' ? body.token : typeof body.t === 'string' ? body.t : '';
|
|
149
|
+
var result = { ok: false };
|
|
150
|
+
try {
|
|
151
|
+
if (token) {
|
|
152
|
+
result = await __newsletter.withNewsletterDb(function (db) {
|
|
153
|
+
return __newsletter.confirmNewsletter({ db: db, token: token, req: req });
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
} catch (err) {
|
|
157
|
+
if (fromForm) {
|
|
158
|
+
console.error('[newsletter] confirm failed:', __newsletter.safeError(err));
|
|
159
|
+
return sendPage(res, 503, 'Please try again', 'We could not confirm your subscription just now. Please try again in a moment.', '');
|
|
160
|
+
}
|
|
161
|
+
return __security.sendClientError(res, 503, 'We could not confirm the subscription. Please try again in a moment.', __newsletter.safeError(err));
|
|
162
|
+
}
|
|
163
|
+
if (fromForm) {
|
|
164
|
+
return result.ok
|
|
165
|
+
? sendPage(res, 200, "You're subscribed", 'Thank you for confirming. You will receive the newsletter at this address.', '')
|
|
166
|
+
: sendPage(res, 200, 'Link not valid', NOT_A_LINK, '');
|
|
167
|
+
}
|
|
168
|
+
return res.status(200).json(result.ok ? { ok: true, email: result.email } : { ok: false });
|
|
169
|
+
};
|
|
170
|
+
`
|
|
171
|
+
|
|
172
|
+
export const generateNewsletterUnsubscribeRoute = (): string => `${ROUTE_PREAMBLE}
|
|
173
|
+
|
|
174
|
+
${LINK_PAGE_CODE}
|
|
139
175
|
|
|
140
176
|
var NOT_A_LINK = 'This unsubscribe link does not work. Use the link in a newer email from us.';
|
|
141
177
|
|
|
@@ -20,6 +20,13 @@ import { NodeHandlerGenerator } from '../types'
|
|
|
20
20
|
* the visitor id is the tracker's alone: a browser may still hold a persistent
|
|
21
21
|
* id from before its consent was withdrawn.
|
|
22
22
|
*
|
|
23
|
+
* The service takes exactly the money outcomes from a server, each with the
|
|
24
|
+
* order or booking id it dedupes a retried report on, and every other outcome
|
|
25
|
+
* from a browser only. So on the server any other name is refused as
|
|
26
|
+
* `browser-only` (an automation, a webhook or a cron has no visitor's page to
|
|
27
|
+
* report it from), and a money outcome without an id as `no-outcome-id`: an id
|
|
28
|
+
* made up here would count a retried report twice. Both say why in the logs.
|
|
29
|
+
*
|
|
23
30
|
* Never throws: an outcome that cannot be reported answers `{ sent: false }`
|
|
24
31
|
* and the workflow continues — analytics must never cost a sale.
|
|
25
32
|
*/
|
|
@@ -57,6 +64,10 @@ const SERVER_BODY = `async function analytics_track_outcome(config, context) {
|
|
|
57
64
|
if (!name) {
|
|
58
65
|
return { sent: false, reason: 'no-name' };
|
|
59
66
|
}
|
|
67
|
+
if (${JSON.stringify(SERVER_ONLY_OUTCOMES)}.indexOf(name) === -1) {
|
|
68
|
+
console.warn('[analytics] the "' + name + '" outcome was not reported: only a visitor\\'s browser can report it, never a server step');
|
|
69
|
+
return { sent: false, reason: 'browser-only' };
|
|
70
|
+
}
|
|
60
71
|
// \`globalThis.process\` (member access): webpack rewrites a bare \`process\`.
|
|
61
72
|
var env = globalThis.process && globalThis.process.env;
|
|
62
73
|
var baseUrl = (env && env.NEXT_PUBLIC_TELEPORT_ANALYTICS_URL) || '';
|
|
@@ -67,6 +78,7 @@ const SERVER_BODY = `async function analytics_track_outcome(config, context) {
|
|
|
67
78
|
}
|
|
68
79
|
// The service dedupes on this id and drops an outcome without one.
|
|
69
80
|
if (config.outcomeId == null || config.outcomeId === '') {
|
|
81
|
+
console.warn('[analytics] the "' + name + '" outcome was not reported: it has no order or booking id');
|
|
70
82
|
return { sent: false, reason: 'no-outcome-id' };
|
|
71
83
|
}
|
|
72
84
|
var ids = context && context.__tpa && typeof context.__tpa === 'object' ? context.__tpa : {};
|