@basementstudio/mitt-next-plugin 0.0.0-stage → 0.2.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/LICENSE +15 -0
- package/dist/config.d.ts +44 -0
- package/dist/config.js +63 -0
- package/dist/config.js.map +1 -0
- package/dist/git.d.ts +33 -0
- package/dist/git.js +52 -0
- package/dist/git.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +21 -0
- package/dist/loader.js +71 -0
- package/dist/loader.js.map +1 -0
- package/dist/manifest.d.ts +15 -0
- package/dist/manifest.js +88 -0
- package/dist/manifest.js.map +1 -0
- package/dist/node.d.ts +2 -0
- package/dist/node.js +5 -0
- package/dist/node.js.map +1 -0
- package/dist/setup.d.ts +27 -0
- package/dist/setup.js +441 -0
- package/dist/setup.js.map +1 -0
- package/dist/transform.d.ts +40 -0
- package/dist/transform.js +365 -0
- package/dist/transform.js.map +1 -0
- package/dist/with-mitt.d.ts +29 -0
- package/dist/with-mitt.js +145 -0
- package/dist/with-mitt.js.map +1 -0
- package/package.json +72 -4
- package/README.md +0 -3
package/dist/setup.js
ADDED
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { PROJECT_ID_PATTERN } from '@basementstudio/mitt-shared/formats';
|
|
4
|
+
import { originAllowed } from '@basementstudio/mitt-shared/origins';
|
|
5
|
+
import { CONFIG_FILE, readMittConfig } from './config.js';
|
|
6
|
+
// The dev-only setup page (ADR 0013): `next dev` with withMitt() answers `/__mitt` with a checklist
|
|
7
|
+
// of what Mitt needs, and buttons that register the project or allow this origin through the API
|
|
8
|
+
// with the server's MITT_CLI_TOKEN. It runs in the dev server's own Node process, on a loopback
|
|
9
|
+
// port Next proxies to with a rewrite, so the app's routes and its production build never change.
|
|
10
|
+
// It shows whether a secret or token is set, never their values.
|
|
11
|
+
export const SETUP_PATH = '/__mitt';
|
|
12
|
+
const PAGE_TIMEOUT_MS = 20_000;
|
|
13
|
+
const API_TIMEOUT_MS = 10_000;
|
|
14
|
+
const ENTITIES = {
|
|
15
|
+
'&': '&',
|
|
16
|
+
'"': '"',
|
|
17
|
+
''': "'",
|
|
18
|
+
''': "'",
|
|
19
|
+
'<': '<',
|
|
20
|
+
'>': '>',
|
|
21
|
+
};
|
|
22
|
+
function decode(value) {
|
|
23
|
+
return value.replace(/&(amp|quot|#39|#x27|lt|gt);/g, (entity) => ENTITIES[entity] ?? entity);
|
|
24
|
+
}
|
|
25
|
+
/** The first `<script>` that loads Mitt's loader (or the widget directly), from server HTML. */
|
|
26
|
+
export function findEmbed(html, pageUrl) {
|
|
27
|
+
for (const tag of html.matchAll(/<script\b([^>]*)>/gi)) {
|
|
28
|
+
const attributes = new Map();
|
|
29
|
+
for (const attr of (tag[1] ?? '').matchAll(/([^\s"'=<>/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g)) {
|
|
30
|
+
const name = attr[1]?.toLowerCase() ?? '';
|
|
31
|
+
attributes.set(name, decode(attr[2] ?? attr[3] ?? attr[4] ?? ''));
|
|
32
|
+
}
|
|
33
|
+
const src = attributes.get('src');
|
|
34
|
+
if (!src)
|
|
35
|
+
continue;
|
|
36
|
+
let pathname;
|
|
37
|
+
try {
|
|
38
|
+
pathname = new URL(src, pageUrl).pathname;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (!/\/(loader|mitt)\.js$/.test(pathname) || !attributes.has('data-project'))
|
|
44
|
+
continue;
|
|
45
|
+
return { src, project: attributes.get('data-project'), api: attributes.get('data-api') };
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
function httpOrigin(value) {
|
|
50
|
+
if (!value)
|
|
51
|
+
return null;
|
|
52
|
+
try {
|
|
53
|
+
const url = new URL(value);
|
|
54
|
+
return url.protocol === 'http:' || url.protocol === 'https:' ? url.origin : null;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async function timed(fetchImpl, url, init, ms) {
|
|
61
|
+
return fetchImpl(url, { ...init, signal: AbortSignal.timeout(ms) });
|
|
62
|
+
}
|
|
63
|
+
async function apiMessage(response) {
|
|
64
|
+
try {
|
|
65
|
+
const body = (await response.json());
|
|
66
|
+
if (typeof body.error?.message === 'string')
|
|
67
|
+
return body.error.message;
|
|
68
|
+
}
|
|
69
|
+
catch { }
|
|
70
|
+
return `HTTP ${response.status}`;
|
|
71
|
+
}
|
|
72
|
+
function isProject(value) {
|
|
73
|
+
if (typeof value !== 'object' || value === null)
|
|
74
|
+
return false;
|
|
75
|
+
const { id, name, origins } = value;
|
|
76
|
+
return (typeof id === 'string' &&
|
|
77
|
+
typeof name === 'string' &&
|
|
78
|
+
Array.isArray(origins) &&
|
|
79
|
+
origins.every((origin) => typeof origin === 'string'));
|
|
80
|
+
}
|
|
81
|
+
function cliHeaders(token, json = false) {
|
|
82
|
+
// No Origin: the API treats requests without one as the CLI (ADR 0011).
|
|
83
|
+
return {
|
|
84
|
+
accept: 'application/json',
|
|
85
|
+
authorization: `Bearer ${token}`,
|
|
86
|
+
...(json ? { 'content-type': 'application/json' } : {}),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async function inspect(options, appOrigin, page) {
|
|
90
|
+
const { root, env, fetch: fetchImpl } = options;
|
|
91
|
+
const checks = [];
|
|
92
|
+
let project = null;
|
|
93
|
+
let secretEnv = 'MITT_SECRET';
|
|
94
|
+
try {
|
|
95
|
+
const config = readMittConfig(root);
|
|
96
|
+
secretEnv = config.secretEnv;
|
|
97
|
+
if (typeof config.project === 'string' && PROJECT_ID_PATTERN.test(config.project)) {
|
|
98
|
+
project = config.project;
|
|
99
|
+
checks.push({ status: 'ok', title: 'Project id', detail: `${CONFIG_FILE}: ${project}` });
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
checks.push({
|
|
103
|
+
status: 'todo',
|
|
104
|
+
title: 'Project id',
|
|
105
|
+
detail: config.project === undefined
|
|
106
|
+
? `Add "project" to ${CONFIG_FILE} (or run \`mitt init\`).`
|
|
107
|
+
: `"project" in ${CONFIG_FILE} must be lowercase letters, digits and dashes.`,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
checks.push({
|
|
113
|
+
status: 'fail',
|
|
114
|
+
title: 'Project id',
|
|
115
|
+
detail: error instanceof Error ? error.message : String(error),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
checks.push(env[secretEnv]
|
|
119
|
+
? { status: 'ok', title: 'Secret', detail: `${secretEnv} is set.` }
|
|
120
|
+
: {
|
|
121
|
+
status: 'todo',
|
|
122
|
+
title: 'Secret',
|
|
123
|
+
detail: `${secretEnv} is not set: dev uses the public dev secret, and production builds fail. Run \`mitt init\` or add it to .env.local and every deployment.`,
|
|
124
|
+
});
|
|
125
|
+
const pageUrl = `${appOrigin}${page}`;
|
|
126
|
+
let embed = null;
|
|
127
|
+
try {
|
|
128
|
+
const response = await timed(fetchImpl, pageUrl, { headers: { accept: 'text/html' } }, PAGE_TIMEOUT_MS);
|
|
129
|
+
embed = findEmbed(await response.text(), pageUrl);
|
|
130
|
+
if (!embed) {
|
|
131
|
+
checks.push({
|
|
132
|
+
status: 'todo',
|
|
133
|
+
title: 'Loader script',
|
|
134
|
+
detail: `${page} has no Mitt loader. Render <Mitt /> (import { Mitt } from '@basementstudio/mitt-next-plugin') in the root layout's <head>, with MITT_API_URL set. Not on React: <script src="https://mitt-web.vercel.app/w/loader.js" async data-project="${project ?? '<project>'}" data-api="https://mitt-web.vercel.app"></script> (a plain <script>, so it's in the server HTML).`,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else if (project && embed.project !== project) {
|
|
138
|
+
checks.push({
|
|
139
|
+
status: 'fail',
|
|
140
|
+
title: 'Loader script',
|
|
141
|
+
detail: `The loader on ${page} says data-project="${embed.project ?? ''}", but ${CONFIG_FILE} says ${project}.`,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
else if (!httpOrigin(embed.api)) {
|
|
145
|
+
checks.push({
|
|
146
|
+
status: 'fail',
|
|
147
|
+
title: 'Loader script',
|
|
148
|
+
detail: `The loader on ${page} needs data-api, the Mitt API's origin.`,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
checks.push({
|
|
153
|
+
status: 'ok',
|
|
154
|
+
title: 'Loader script',
|
|
155
|
+
detail: `${page} loads ${embed.src} (API ${embed.api}).`,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
161
|
+
checks.push({
|
|
162
|
+
status: 'fail',
|
|
163
|
+
title: 'Loader script',
|
|
164
|
+
detail: `Couldn't load ${page}: ${reason}`,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const apiUrl = httpOrigin(env.MITT_API_URL) ?? httpOrigin(embed?.api);
|
|
168
|
+
const token = env.MITT_CLI_TOKEN?.trim() || null;
|
|
169
|
+
checks.push(token
|
|
170
|
+
? { status: 'ok', title: 'CLI token', detail: 'MITT_CLI_TOKEN is set.' }
|
|
171
|
+
: {
|
|
172
|
+
status: 'todo',
|
|
173
|
+
title: 'CLI token',
|
|
174
|
+
detail: 'Add MITT_CLI_TOKEN to .env.local (the token the Mitt API was started with) to check registration here and to use the CLI.',
|
|
175
|
+
});
|
|
176
|
+
let registered = null;
|
|
177
|
+
const title = 'Registered, and this origin allowed';
|
|
178
|
+
if (!project) {
|
|
179
|
+
checks.push({ status: 'todo', title, detail: 'Needs a project id first.' });
|
|
180
|
+
}
|
|
181
|
+
else if (!apiUrl) {
|
|
182
|
+
checks.push({
|
|
183
|
+
status: 'todo',
|
|
184
|
+
title,
|
|
185
|
+
detail: 'Set MITT_API_URL, or give the loader a data-api, so this page knows the API.',
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
else if (!token) {
|
|
189
|
+
checks.push({ status: 'todo', title, detail: 'Needs MITT_CLI_TOKEN to ask the API.' });
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
try {
|
|
193
|
+
const response = await timed(fetchImpl, `${apiUrl}/api/projects/${project}`, { headers: cliHeaders(token) }, API_TIMEOUT_MS);
|
|
194
|
+
const body = response.ok ? await response.json() : null;
|
|
195
|
+
if (response.status === 404) {
|
|
196
|
+
checks.push({
|
|
197
|
+
status: 'todo',
|
|
198
|
+
title,
|
|
199
|
+
detail: `${project} isn't registered on ${apiUrl}.`,
|
|
200
|
+
action: { name: 'register', label: `Register ${project} with ${appOrigin}` },
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
else if (!response.ok) {
|
|
204
|
+
checks.push({ status: 'fail', title, detail: `${apiUrl}: ${await apiMessage(response)}` });
|
|
205
|
+
}
|
|
206
|
+
else if (!isProject(body)) {
|
|
207
|
+
checks.push({ status: 'fail', title, detail: `${apiUrl} answered something unexpected.` });
|
|
208
|
+
}
|
|
209
|
+
else if (originAllowed(body.origins, appOrigin)) {
|
|
210
|
+
registered = body;
|
|
211
|
+
checks.push({
|
|
212
|
+
status: 'ok',
|
|
213
|
+
title,
|
|
214
|
+
detail: `${body.name} on ${apiUrl} allows ${appOrigin}.`,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
registered = body;
|
|
219
|
+
checks.push({
|
|
220
|
+
status: 'todo',
|
|
221
|
+
title,
|
|
222
|
+
detail: `${body.name} on ${apiUrl} doesn't allow ${appOrigin} (allows ${body.origins.join(', ')}).`,
|
|
223
|
+
action: { name: 'allow', label: `Allow ${appOrigin}` },
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
229
|
+
checks.push({ status: 'fail', title, detail: `Couldn't reach ${apiUrl}: ${reason}` });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return { appOrigin, page, checks, project, apiUrl, token, registered };
|
|
233
|
+
}
|
|
234
|
+
function html(value) {
|
|
235
|
+
return value
|
|
236
|
+
.replaceAll('&', '&')
|
|
237
|
+
.replaceAll('<', '<')
|
|
238
|
+
.replaceAll('>', '>')
|
|
239
|
+
.replaceAll('"', '"')
|
|
240
|
+
.replaceAll("'", ''');
|
|
241
|
+
}
|
|
242
|
+
const MARKS = { ok: '✓', todo: '•', fail: '✕' };
|
|
243
|
+
function render(inspection, notice) {
|
|
244
|
+
const items = inspection.checks
|
|
245
|
+
.map((check) => {
|
|
246
|
+
const form = check.action
|
|
247
|
+
? `<form method="post" action="__mitt/${check.action.name}"><input type="hidden" name="page" value="${html(inspection.page)}"><button>${html(check.action.label)}</button></form>`
|
|
248
|
+
: '';
|
|
249
|
+
return `<li class="${check.status}"><span class="mark" aria-hidden="true">${MARKS[check.status]}</span><div><strong>${html(check.title)}</strong><p>${html(check.detail)}</p>${form}</div></li>`;
|
|
250
|
+
})
|
|
251
|
+
.join('');
|
|
252
|
+
const done = inspection.checks.every((check) => check.status === 'ok');
|
|
253
|
+
return `<!doctype html>
|
|
254
|
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
|
|
255
|
+
<meta name="robots" content="noindex"><title>Mitt setup</title>
|
|
256
|
+
<style>
|
|
257
|
+
body{font:14px/1.5 ui-sans-serif,system-ui,sans-serif;margin:0;padding:48px 20px;background:#f7f7f5;color:#1c1c1a}
|
|
258
|
+
main{max-width:640px;margin:0 auto}h1{font-size:20px;margin:0 0 4px}.sub{color:#6b6b66;margin:0 0 24px}
|
|
259
|
+
ul{list-style:none;padding:0;margin:0;display:grid;gap:8px}li{display:flex;gap:12px;padding:12px 14px;border-radius:8px;background:#fff;border:1px solid #e6e6e1}
|
|
260
|
+
.mark{width:20px;flex:none;text-align:center;font-weight:700}.ok .mark{color:#1a7f37}.todo .mark{color:#9a6700}.fail .mark{color:#cf222e}
|
|
261
|
+
p{margin:2px 0 0;color:#4a4a45;overflow-wrap:anywhere}form{margin-top:8px}button{font:inherit;padding:4px 12px;border-radius:6px;border:1px solid #1c1c1a;background:#1c1c1a;color:#fff;cursor:pointer}
|
|
262
|
+
.notice{padding:10px 14px;border-radius:8px;background:#fff8c5;margin:0 0 16px}code{font-family:ui-monospace,monospace}
|
|
263
|
+
</style></head><body><main>
|
|
264
|
+
<h1>Mitt setup</h1>
|
|
265
|
+
<p class="sub">Dev only: <code>next dev</code> with <code>withMitt()</code> serves this page; production builds don't have it. Checking <code>${html(inspection.appOrigin + inspection.page)}</code> (another page: <code>?page=/pricing</code>).</p>
|
|
266
|
+
${notice ? `<p class="notice" role="status">${html(notice)}</p>` : ''}
|
|
267
|
+
<ul>${items}</ul>
|
|
268
|
+
<p class="sub" style="margin-top:20px">${done ? 'All set: open any page with <code>?mitt</code> to review it.' : 'Fix the items above, then reload.'}</p>
|
|
269
|
+
</main></body></html>`;
|
|
270
|
+
}
|
|
271
|
+
function send(res, status, body, type = 'text/html') {
|
|
272
|
+
res.writeHead(status, {
|
|
273
|
+
'content-type': `${type}; charset=utf-8`,
|
|
274
|
+
'cache-control': 'no-store',
|
|
275
|
+
'x-robots-tag': 'noindex',
|
|
276
|
+
'x-frame-options': 'DENY',
|
|
277
|
+
// Not `no-referrer`: that sends `Origin: null` on the page's own form posts.
|
|
278
|
+
'referrer-policy': 'same-origin',
|
|
279
|
+
});
|
|
280
|
+
res.end(body);
|
|
281
|
+
}
|
|
282
|
+
function readBody(req) {
|
|
283
|
+
return new Promise((resolve, reject) => {
|
|
284
|
+
let body = '';
|
|
285
|
+
req.on('data', (chunk) => {
|
|
286
|
+
body += chunk.toString('utf8');
|
|
287
|
+
if (body.length > 4096)
|
|
288
|
+
req.destroy();
|
|
289
|
+
});
|
|
290
|
+
req.on('end', () => resolve(body));
|
|
291
|
+
req.on('error', reject);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
function pageParam(value) {
|
|
295
|
+
return value?.startsWith('/') && !value.startsWith('//') ? value : '/';
|
|
296
|
+
}
|
|
297
|
+
/** The app's origin as the browser sees it: Next sets `x-forwarded-host` when it proxies. */
|
|
298
|
+
function appOriginOf(req) {
|
|
299
|
+
const host = String(req.headers['x-forwarded-host'] ?? req.headers.host ?? 'localhost');
|
|
300
|
+
const proto = String(req.headers['x-forwarded-proto'] ?? 'http')
|
|
301
|
+
.split(',')[0]
|
|
302
|
+
?.trim();
|
|
303
|
+
return `${proto === 'https' ? 'https' : 'http'}://${host}`;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* A form post only counts from the page itself: its `Origin` must be the app's, so another site
|
|
307
|
+
* can't make a reviewer's dev server change an allowlist.
|
|
308
|
+
*/
|
|
309
|
+
function sameOrigin(req, appOrigin) {
|
|
310
|
+
const site = req.headers['sec-fetch-site'];
|
|
311
|
+
if (site !== undefined && site !== 'same-origin')
|
|
312
|
+
return false;
|
|
313
|
+
const origin = req.headers.origin;
|
|
314
|
+
if (typeof origin !== 'string')
|
|
315
|
+
return false;
|
|
316
|
+
try {
|
|
317
|
+
return new URL(origin).host === new URL(appOrigin).host;
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
async function act(options, name, appOrigin, page) {
|
|
324
|
+
const inspection = await inspect(options, appOrigin, page);
|
|
325
|
+
const { project, apiUrl, token, registered } = inspection;
|
|
326
|
+
if (!project || !apiUrl || !token)
|
|
327
|
+
return 'Project id, API and token are needed first.';
|
|
328
|
+
const fetchImpl = options.fetch;
|
|
329
|
+
if (name === 'register') {
|
|
330
|
+
if (registered)
|
|
331
|
+
return `${project} is already registered.`;
|
|
332
|
+
const response = await timed(fetchImpl, `${apiUrl}/api/projects`, {
|
|
333
|
+
method: 'POST',
|
|
334
|
+
headers: cliHeaders(token, true),
|
|
335
|
+
body: JSON.stringify({ id: project, name: project, origins: [appOrigin] }),
|
|
336
|
+
}, API_TIMEOUT_MS);
|
|
337
|
+
return response.ok
|
|
338
|
+
? `Registered ${project}, allowing ${appOrigin}.`
|
|
339
|
+
: `Couldn't register: ${await apiMessage(response)}`;
|
|
340
|
+
}
|
|
341
|
+
if (!registered)
|
|
342
|
+
return `${project} isn't registered yet.`;
|
|
343
|
+
if (originAllowed(registered.origins, appOrigin))
|
|
344
|
+
return `${appOrigin} is already allowed.`;
|
|
345
|
+
const response = await timed(fetchImpl, `${apiUrl}/api/projects/${project}/origins`, {
|
|
346
|
+
method: 'PUT',
|
|
347
|
+
headers: cliHeaders(token, true),
|
|
348
|
+
body: JSON.stringify({ origins: [...registered.origins, appOrigin] }),
|
|
349
|
+
}, API_TIMEOUT_MS);
|
|
350
|
+
return response.ok
|
|
351
|
+
? `Allowed ${appOrigin}.`
|
|
352
|
+
: `Couldn't allow ${appOrigin}: ${await apiMessage(response)}`;
|
|
353
|
+
}
|
|
354
|
+
function handler(options, prefix) {
|
|
355
|
+
return async (req, res) => {
|
|
356
|
+
const url = new URL(req.url ?? '/', 'http://setup.invalid');
|
|
357
|
+
if (!url.pathname.startsWith(prefix)) {
|
|
358
|
+
send(res, 404, 'Not found', 'text/plain');
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const rest = url.pathname.slice(prefix.length);
|
|
362
|
+
const appOrigin = appOriginOf(req);
|
|
363
|
+
try {
|
|
364
|
+
if (req.method === 'GET' && (rest === '' || rest === '/')) {
|
|
365
|
+
const inspection = await inspect(options, appOrigin, pageParam(url.searchParams.get('page')));
|
|
366
|
+
send(res, 200, render(inspection, url.searchParams.get('notice')));
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const action = /^\/(register|allow)$/.exec(rest)?.[1];
|
|
370
|
+
if (req.method === 'POST' && action) {
|
|
371
|
+
if (!sameOrigin(req, appOrigin)) {
|
|
372
|
+
send(res, 403, 'Only the setup page itself can do this.', 'text/plain');
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const page = pageParam(new URLSearchParams(await readBody(req)).get('page'));
|
|
376
|
+
const notice = await act(options, action, appOrigin, page);
|
|
377
|
+
const query = new URLSearchParams({ page, notice });
|
|
378
|
+
// Relative, so it also works under a basePath: …/__mitt/register → …/__mitt.
|
|
379
|
+
res.writeHead(303, { location: `../__mitt?${query}`, 'cache-control': 'no-store' });
|
|
380
|
+
res.end();
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
send(res, 404, 'Not found', 'text/plain');
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
387
|
+
send(res, 500, `Mitt setup failed: ${reason}`, 'text/plain');
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
export async function startSetupServer(options) {
|
|
392
|
+
const resolved = {
|
|
393
|
+
root: options.root,
|
|
394
|
+
env: options.env ?? process.env,
|
|
395
|
+
fetch: options.fetch ?? fetch,
|
|
396
|
+
};
|
|
397
|
+
// Only Next knows the path: anything else on this machine that finds the port gets a 404.
|
|
398
|
+
const prefix = `/${randomBytes(16).toString('hex')}${SETUP_PATH}`;
|
|
399
|
+
const server = createServer((req, res) => {
|
|
400
|
+
void handler(resolved, prefix)(req, res);
|
|
401
|
+
});
|
|
402
|
+
await new Promise((resolve, reject) => {
|
|
403
|
+
server.once('error', reject);
|
|
404
|
+
server.listen(0, '127.0.0.1', () => resolve());
|
|
405
|
+
});
|
|
406
|
+
// The dev server's lifetime decides: this never keeps the process alive on its own.
|
|
407
|
+
server.unref();
|
|
408
|
+
const { port } = server.address();
|
|
409
|
+
return {
|
|
410
|
+
target: `http://127.0.0.1:${port}${prefix}`,
|
|
411
|
+
close: () => new Promise((resolve) => server.close(() => resolve())),
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
/** Puts `/__mitt` first, before the app's own rewrites and routes. */
|
|
415
|
+
export function withSetupRewrites(config, target) {
|
|
416
|
+
const userRewrites = config.rewrites;
|
|
417
|
+
const ours = [
|
|
418
|
+
{ source: SETUP_PATH, destination: target },
|
|
419
|
+
{ source: `${SETUP_PATH}/:path*`, destination: `${target}/:path*` },
|
|
420
|
+
];
|
|
421
|
+
return {
|
|
422
|
+
...config,
|
|
423
|
+
async rewrites() {
|
|
424
|
+
const theirs = userRewrites ? await userRewrites() : [];
|
|
425
|
+
if (Array.isArray(theirs))
|
|
426
|
+
return { beforeFiles: ours, afterFiles: theirs, fallback: [] };
|
|
427
|
+
return { ...theirs, beforeFiles: [...ours, ...(theirs.beforeFiles ?? [])] };
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
const servers = new Map();
|
|
432
|
+
/** One setup server per app root and process, however often Next loads the config. */
|
|
433
|
+
export function setupServerFor(root) {
|
|
434
|
+
let server = servers.get(root);
|
|
435
|
+
if (!server) {
|
|
436
|
+
server = startSetupServer({ root });
|
|
437
|
+
servers.set(root, server);
|
|
438
|
+
}
|
|
439
|
+
return server;
|
|
440
|
+
}
|
|
441
|
+
//# sourceMappingURL=setup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup.js","sourceRoot":"","sources":["../src/setup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,YAAY,EAA0D,MAAM,WAAW,CAAA;AAEhG,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAA;AAEnE,OAAO,EAAE,WAAW,EAAY,cAAc,EAAE,MAAM,aAAa,CAAA;AAEnE,oGAAoG;AACpG,iGAAiG;AACjG,gGAAgG;AAChG,kGAAkG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,EAAE,GAAG,MAAM,EAAE,WAAW,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC,EAAE,CAAA;QAC7E,CAAC;KACF,CAAA;AACH,CAAC;AAED,MAAM,OAAO,GAAG,IAAI,GAAG,EAAgC,CAAA;AAEvD,sFAAsF;AACtF,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,IAAI,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IAC9B,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,GAAG,gBAAgB,CAAC,EAAE,IAAI,EAAE,CAAC,CAAA;QACnC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IAC3B,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type SourceMap } from 'magic-string';
|
|
2
|
+
export interface TransformOptions {
|
|
3
|
+
/** POSIX path relative to the Next app root. Part of every ID, so it must match the manifest's. */
|
|
4
|
+
relPath: string;
|
|
5
|
+
secret: string;
|
|
6
|
+
/** Written to `<html data-fb-commit>`; omitted when absent. */
|
|
7
|
+
commit?: string | null;
|
|
8
|
+
/**
|
|
9
|
+
* `sources[0]` of the sourcemap. Defaults to `relPath`; bundlers resolve it against the module's
|
|
10
|
+
* own directory, so loaders pass the file's basename.
|
|
11
|
+
*/
|
|
12
|
+
mapSource?: string;
|
|
13
|
+
/** Set to false to skip the sourcemap when the bundler won't use it. Defaults to true. */
|
|
14
|
+
sourceMap?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface Entry {
|
|
17
|
+
id: string;
|
|
18
|
+
file: string;
|
|
19
|
+
/** 1-based line of the opening `<`. */
|
|
20
|
+
line: number;
|
|
21
|
+
/** 0-based column (UTF-16 code units) of the opening `<`. */
|
|
22
|
+
column: number;
|
|
23
|
+
tag: string;
|
|
24
|
+
component: string | null;
|
|
25
|
+
}
|
|
26
|
+
export interface TransformResult {
|
|
27
|
+
code: string;
|
|
28
|
+
/** Null when nothing was injected (the source is returned untouched) or maps were disabled. */
|
|
29
|
+
map: SourceMap | null;
|
|
30
|
+
entries: Entry[];
|
|
31
|
+
warning?: string;
|
|
32
|
+
}
|
|
33
|
+
export declare function createId(secret: string, relPath: string, line: number, column: number): string;
|
|
34
|
+
/**
|
|
35
|
+
* Injects an opaque `data-fb` ID on every lowercase host JSX element, and on the library components
|
|
36
|
+
* of FORWARDING_COMPONENTS (recorded with the tag they render). Pure: the same source and
|
|
37
|
+
* options always give the same output, which is what lets the manifest be rebuilt from git
|
|
38
|
+
* instead of collected from a (cached) build.
|
|
39
|
+
*/
|
|
40
|
+
export declare function transform(source: string, options: TransformOptions): TransformResult;
|