@autter/runtime-cli 0.0.0-stage → 1.0.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 +91 -2
- package/dist/build-BXFh_jbP.d.cts +187 -0
- package/dist/build-BXFh_jbP.d.ts +187 -0
- package/dist/chunk-GGHHEUPQ.js +41 -0
- package/dist/chunk-SIRFO2QU.js +524 -0
- package/dist/cli.js +622 -0
- package/dist/index.cjs +581 -0
- package/dist/index.d.cts +50 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +64 -0
- package/dist/next.cjs +628 -0
- package/dist/next.d.cts +23 -0
- package/dist/next.d.ts +23 -0
- package/dist/next.js +84 -0
- package/dist/vite.cjs +576 -0
- package/dist/vite.d.cts +54 -0
- package/dist/vite.d.ts +54 -0
- package/dist/vite.js +57 -0
- package/dist/webpack.cjs +559 -0
- package/dist/webpack.d.cts +46 -0
- package/dist/webpack.d.ts +46 -0
- package/dist/webpack.js +11 -0
- package/package.json +97 -5
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
// src/debug-id.ts
|
|
2
|
+
import { createHash } from "crypto";
|
|
3
|
+
var DEBUG_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
4
|
+
function normalizeDebugId(value) {
|
|
5
|
+
if (typeof value !== "string") return null;
|
|
6
|
+
const id = value.trim().toLowerCase();
|
|
7
|
+
return DEBUG_ID_RE.test(id) ? id : null;
|
|
8
|
+
}
|
|
9
|
+
function debugIdFor(bytes) {
|
|
10
|
+
const hash = createHash("sha256").update(bytes).digest();
|
|
11
|
+
hash[6] = hash[6] & 15 | 64;
|
|
12
|
+
hash[8] = hash[8] & 63 | 128;
|
|
13
|
+
const hex = hash.subarray(0, 16).toString("hex");
|
|
14
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
15
|
+
}
|
|
16
|
+
var DEBUG_ID_COMMENT_RE = /^\/\/# debugId=([0-9a-fA-F-]{36})[ \t]*\r?$/gm;
|
|
17
|
+
function readDebugIdComment(code) {
|
|
18
|
+
let found = null;
|
|
19
|
+
for (const match of code.matchAll(DEBUG_ID_COMMENT_RE)) found = normalizeDebugId(match[1]) ?? found;
|
|
20
|
+
return found;
|
|
21
|
+
}
|
|
22
|
+
var DEBUG_ID_REGISTRY = "_autterDebugIds";
|
|
23
|
+
function debugIdSnippet(id) {
|
|
24
|
+
return `;!function(){try{var g="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},s=(new g.Error).stack;s&&((g.${DEBUG_ID_REGISTRY}=g.${DEBUG_ID_REGISTRY}||{})[s]="${id}")}catch(e){}}();`;
|
|
25
|
+
}
|
|
26
|
+
var SNIPPET_RE = /\(g\._autterDebugIds=g\._autterDebugIds\|\|\{\}\)\[s\]="[0-9a-f]{8}-/;
|
|
27
|
+
function hasDebugIdSnippet(code) {
|
|
28
|
+
return SNIPPET_RE.test(code);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/files.ts
|
|
32
|
+
import { existsSync } from "fs";
|
|
33
|
+
import { readdir, readFile } from "fs/promises";
|
|
34
|
+
import { dirname, join, relative, resolve, sep } from "path";
|
|
35
|
+
var BUNDLE_RE = /\.[cm]?js$/;
|
|
36
|
+
var SOURCE_MAPPING_URL_RE = /^[ \t]*\/\/[#@] sourceMappingURL=([^\s'"]+)[ \t]*\r?$/gm;
|
|
37
|
+
function readSourceMappingUrl(code) {
|
|
38
|
+
let found = null;
|
|
39
|
+
for (const match of code.matchAll(SOURCE_MAPPING_URL_RE)) found = match[1];
|
|
40
|
+
return found;
|
|
41
|
+
}
|
|
42
|
+
function stripSourceMappingUrl(code) {
|
|
43
|
+
const lines = code.split("\n");
|
|
44
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
45
|
+
const line = lines[i].trim();
|
|
46
|
+
if (line === "") continue;
|
|
47
|
+
if (/^\/\/[#@] sourceMappingURL=/.test(line)) {
|
|
48
|
+
lines.splice(i, 1);
|
|
49
|
+
return lines.join("\n");
|
|
50
|
+
}
|
|
51
|
+
if (!line.startsWith("//#")) break;
|
|
52
|
+
}
|
|
53
|
+
return code;
|
|
54
|
+
}
|
|
55
|
+
function mapPathFor(js, code) {
|
|
56
|
+
const url = readSourceMappingUrl(code);
|
|
57
|
+
if (url && !/^[a-z][\w+.-]*:/i.test(url) && !url.startsWith("/")) {
|
|
58
|
+
let decoded = url.split(/[?#]/)[0];
|
|
59
|
+
try {
|
|
60
|
+
decoded = decodeURIComponent(decoded);
|
|
61
|
+
} catch {
|
|
62
|
+
}
|
|
63
|
+
const candidate = resolve(dirname(js), decoded);
|
|
64
|
+
if (existsSync(candidate)) return candidate;
|
|
65
|
+
}
|
|
66
|
+
const sibling = `${js}.map`;
|
|
67
|
+
return existsSync(sibling) ? sibling : null;
|
|
68
|
+
}
|
|
69
|
+
async function walk(dir, out) {
|
|
70
|
+
let entries;
|
|
71
|
+
try {
|
|
72
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
73
|
+
} catch {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
for (const entry of entries) {
|
|
77
|
+
if (entry.name === "node_modules") continue;
|
|
78
|
+
const full = join(dir, entry.name);
|
|
79
|
+
if (entry.isDirectory()) await walk(full, out);
|
|
80
|
+
else if (entry.isFile() && BUNDLE_RE.test(entry.name)) out.push(full);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function findBundles(dir, files) {
|
|
84
|
+
const root = resolve(dir);
|
|
85
|
+
const candidates = [];
|
|
86
|
+
if (files) {
|
|
87
|
+
for (const file of files) if (BUNDLE_RE.test(file)) candidates.push(resolve(root, file));
|
|
88
|
+
} else {
|
|
89
|
+
await walk(root, candidates);
|
|
90
|
+
}
|
|
91
|
+
const bundles = [];
|
|
92
|
+
for (const js of [...new Set(candidates)].sort()) {
|
|
93
|
+
let code;
|
|
94
|
+
try {
|
|
95
|
+
code = await readFile(js, "utf8");
|
|
96
|
+
} catch {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const map = mapPathFor(js, code);
|
|
100
|
+
if (map) bundles.push({ js, map, path: relative(root, js).split(sep).join("/") });
|
|
101
|
+
}
|
|
102
|
+
return bundles;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/inject.ts
|
|
106
|
+
import { readFile as readFile2, writeFile } from "fs/promises";
|
|
107
|
+
var SNIPPET_LINE_KEY = "x_autter_snippet_line";
|
|
108
|
+
function snippetLine(code) {
|
|
109
|
+
let line = 0;
|
|
110
|
+
let i = 0;
|
|
111
|
+
if (code.startsWith("#!")) {
|
|
112
|
+
const nl = code.indexOf("\n");
|
|
113
|
+
if (nl < 0) return -1;
|
|
114
|
+
line = 1;
|
|
115
|
+
i = nl + 1;
|
|
116
|
+
}
|
|
117
|
+
for (; ; ) {
|
|
118
|
+
while (i < code.length && /\s/.test(code[i])) i++;
|
|
119
|
+
if (code.startsWith("//", i)) {
|
|
120
|
+
const nl = code.indexOf("\n", i);
|
|
121
|
+
if (nl < 0) return -1;
|
|
122
|
+
i = nl + 1;
|
|
123
|
+
} else if (code.startsWith("/*", i)) {
|
|
124
|
+
const end = code.indexOf("*/", i + 2);
|
|
125
|
+
if (end < 0) return -1;
|
|
126
|
+
i = end + 2;
|
|
127
|
+
} else {
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return code[i] === '"' || code[i] === "'" ? -1 : line;
|
|
132
|
+
}
|
|
133
|
+
function shiftMappings(mappings, at, count = 1) {
|
|
134
|
+
const lines = mappings.split(";");
|
|
135
|
+
while (lines.length < at) lines.push("");
|
|
136
|
+
lines.splice(at, 0, ...Array(count).fill(""));
|
|
137
|
+
return lines.join(";");
|
|
138
|
+
}
|
|
139
|
+
function insertBeforeTrailingComments(code, text) {
|
|
140
|
+
const lines = code.split("\n");
|
|
141
|
+
let at = lines.length;
|
|
142
|
+
while (at > 0) {
|
|
143
|
+
const line = lines[at - 1].trim();
|
|
144
|
+
if (line === "" || line.startsWith("//#") || line.startsWith("//@")) at--;
|
|
145
|
+
else break;
|
|
146
|
+
}
|
|
147
|
+
const head = lines.slice(0, at).join("\n");
|
|
148
|
+
const tail = lines.slice(at).join("\n");
|
|
149
|
+
return `${head}${head && !head.endsWith("\n") ? "\n" : ""}${text}${tail ? `
|
|
150
|
+
${tail.replace(/^\n+/, "")}` : "\n"}`;
|
|
151
|
+
}
|
|
152
|
+
async function injectBundle(bundle, options = {}) {
|
|
153
|
+
const bytes = await readFile2(bundle.js);
|
|
154
|
+
const code = bytes.toString("utf8");
|
|
155
|
+
const base = { path: bundle.path, js: bundle.js, map: bundle.map };
|
|
156
|
+
let map;
|
|
157
|
+
try {
|
|
158
|
+
map = JSON.parse(await readFile2(bundle.map, "utf8"));
|
|
159
|
+
if (!map || typeof map !== "object") throw new Error("not an object");
|
|
160
|
+
} catch {
|
|
161
|
+
return { ...base, debugId: "", status: "invalid-map" };
|
|
162
|
+
}
|
|
163
|
+
const existing = readDebugIdComment(code);
|
|
164
|
+
const hasSnippet = hasDebugIdSnippet(code);
|
|
165
|
+
const debugId = existing ?? debugIdFor(bytes);
|
|
166
|
+
let out = code;
|
|
167
|
+
let mapDirty = false;
|
|
168
|
+
if (!hasSnippet) {
|
|
169
|
+
const shifted = typeof map[SNIPPET_LINE_KEY] === "number" ? map[SNIPPET_LINE_KEY] : null;
|
|
170
|
+
const line = shifted ?? snippetLine(code);
|
|
171
|
+
if (line >= 0 && (shifted !== null || typeof map.mappings === "string" && !("sections" in map))) {
|
|
172
|
+
let offset = 0;
|
|
173
|
+
for (let n = 0; n < line; n++) offset = out.indexOf("\n", offset) + 1;
|
|
174
|
+
out = `${out.slice(0, offset)}${debugIdSnippet(debugId)}
|
|
175
|
+
${out.slice(offset)}`;
|
|
176
|
+
if (shifted === null) {
|
|
177
|
+
map.mappings = shiftMappings(map.mappings, line);
|
|
178
|
+
map[SNIPPET_LINE_KEY] = line;
|
|
179
|
+
mapDirty = true;
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
out = insertBeforeTrailingComments(out, debugIdSnippet(debugId));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (!existing) out = `${out}${out.endsWith("\n") ? "" : "\n"}//# debugId=${debugId}
|
|
186
|
+
`;
|
|
187
|
+
if (options.stripSourceMappingUrl) out = stripSourceMappingUrl(out);
|
|
188
|
+
if (map.debugId !== debugId) {
|
|
189
|
+
map.debugId = debugId;
|
|
190
|
+
mapDirty = true;
|
|
191
|
+
}
|
|
192
|
+
const changed = mapDirty || out !== code;
|
|
193
|
+
if (changed && !options.dryRun) {
|
|
194
|
+
if (mapDirty) await writeFile(bundle.map, JSON.stringify(map));
|
|
195
|
+
if (out !== code) await writeFile(bundle.js, out);
|
|
196
|
+
}
|
|
197
|
+
return { ...base, debugId, status: changed ? "injected" : "unchanged" };
|
|
198
|
+
}
|
|
199
|
+
async function injectDir(dir, options = {}) {
|
|
200
|
+
const results = [];
|
|
201
|
+
for (const bundle of await findBundles(dir, options.files)) results.push(await injectBundle(bundle, options));
|
|
202
|
+
return results;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/release.ts
|
|
206
|
+
import { execFileSync } from "child_process";
|
|
207
|
+
var RELEASE_RE = /^[a-zA-Z0-9._-]{1,200}$/;
|
|
208
|
+
var RELEASE_ENV_VARS = [
|
|
209
|
+
"GITHUB_SHA",
|
|
210
|
+
"VERCEL_GIT_COMMIT_SHA",
|
|
211
|
+
"CF_PAGES_COMMIT_SHA",
|
|
212
|
+
"GIT_SHA"
|
|
213
|
+
];
|
|
214
|
+
function gitHead(cwd = process.cwd()) {
|
|
215
|
+
try {
|
|
216
|
+
const sha = execFileSync("git", ["rev-parse", "HEAD"], {
|
|
217
|
+
cwd,
|
|
218
|
+
encoding: "utf8",
|
|
219
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
220
|
+
timeout: 1e4
|
|
221
|
+
}).trim();
|
|
222
|
+
return /^[0-9a-f]{7,64}$/.test(sha) ? sha : null;
|
|
223
|
+
} catch {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function resolveRelease(explicit, env = process.env, cwd = process.cwd()) {
|
|
228
|
+
const candidates = [
|
|
229
|
+
["--release", explicit],
|
|
230
|
+
...RELEASE_ENV_VARS.map((name) => [name, env[name]])
|
|
231
|
+
];
|
|
232
|
+
for (const [source, value] of candidates) {
|
|
233
|
+
const release = value?.trim();
|
|
234
|
+
if (!release) continue;
|
|
235
|
+
if (!RELEASE_RE.test(release)) {
|
|
236
|
+
throw new Error(
|
|
237
|
+
`release "${release}" (from ${source}) is not valid: use 1-200 letters, digits, ".", "_" or "-" (a commit SHA works)`
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
return { release, source };
|
|
241
|
+
}
|
|
242
|
+
const head = gitHead(cwd);
|
|
243
|
+
if (head) return { release: head, source: "git" };
|
|
244
|
+
throw new Error(
|
|
245
|
+
`no release: pass --release <release>, or set one of ${RELEASE_ENV_VARS.join(", ")}, or run inside a git checkout`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/upload.ts
|
|
250
|
+
import { createHash as createHash2 } from "crypto";
|
|
251
|
+
import { readFile as readFile3, rm, writeFile as writeFile2 } from "fs/promises";
|
|
252
|
+
var DEFAULT_ENDPOINT = "https://otlp.autter.dev";
|
|
253
|
+
var MAX_UPLOAD_BYTES = 5 * 1024 * 1024;
|
|
254
|
+
var CHECK_BATCH = 500;
|
|
255
|
+
var MAX_ATTEMPTS = 3;
|
|
256
|
+
function resolveEndpoint(flag, env = process.env) {
|
|
257
|
+
const raw = flag || env.AUTTER_ENDPOINT || env.AUTTER_RUNTIME_ENDPOINT || env.OTEL_EXPORTER_OTLP_ENDPOINT || DEFAULT_ENDPOINT;
|
|
258
|
+
return raw.trim().replace(/\/+$/, "").replace(/\/v1\/[a-z-]+$/, "");
|
|
259
|
+
}
|
|
260
|
+
function servedFilename(urlPrefix, path) {
|
|
261
|
+
let prefix = (urlPrefix ?? "/").trim() || "/";
|
|
262
|
+
if (prefix.startsWith("~/")) prefix = prefix.slice(1);
|
|
263
|
+
if (!/^[a-z][\w+.-]*:\/\//i.test(prefix) && !prefix.startsWith("/")) prefix = `/${prefix}`;
|
|
264
|
+
return `${prefix.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
|
|
265
|
+
}
|
|
266
|
+
function storedMapJson(map) {
|
|
267
|
+
const { sourcesContent: _omitted, ...safe } = map;
|
|
268
|
+
return JSON.stringify(safe);
|
|
269
|
+
}
|
|
270
|
+
async function prepare(bundle, urlPrefix, release) {
|
|
271
|
+
let map;
|
|
272
|
+
try {
|
|
273
|
+
map = JSON.parse(await readFile3(bundle.map, "utf8"));
|
|
274
|
+
} catch {
|
|
275
|
+
return { error: "map is not valid JSON" };
|
|
276
|
+
}
|
|
277
|
+
if (!map || map.version !== 3 || typeof map.mappings !== "string" || !Array.isArray(map.sources)) {
|
|
278
|
+
return { error: "not a version 3 source map with mappings and sources" };
|
|
279
|
+
}
|
|
280
|
+
const code = await readFile3(bundle.js, "utf8");
|
|
281
|
+
const debugId = readDebugIdComment(code) ?? normalizeDebugId(map.debugId);
|
|
282
|
+
const json = storedMapJson(map);
|
|
283
|
+
const filename = servedFilename(urlPrefix, bundle.path);
|
|
284
|
+
const body = `{"release":${JSON.stringify(release)},"filename":${JSON.stringify(filename)},${debugId ? `"debugId":${JSON.stringify(debugId)},` : ""}"map":${json}}`;
|
|
285
|
+
return {
|
|
286
|
+
bundle,
|
|
287
|
+
body,
|
|
288
|
+
path: bundle.path,
|
|
289
|
+
filename,
|
|
290
|
+
debugId,
|
|
291
|
+
hash: createHash2("sha256").update(json).digest("hex"),
|
|
292
|
+
bytes: Buffer.byteLength(body)
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
var FatalUploadError = class extends Error {
|
|
296
|
+
};
|
|
297
|
+
function sleep(ms) {
|
|
298
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
299
|
+
}
|
|
300
|
+
async function uploadDir(dir, options = {}) {
|
|
301
|
+
const env = options.env ?? process.env;
|
|
302
|
+
const log = options.log ?? (() => {
|
|
303
|
+
});
|
|
304
|
+
const doFetch = options.fetch ?? fetch;
|
|
305
|
+
const { release, source: releaseSource } = resolveRelease(options.release, env, options.cwd);
|
|
306
|
+
const endpoint = resolveEndpoint(options.endpoint, env);
|
|
307
|
+
const summary = {
|
|
308
|
+
release,
|
|
309
|
+
releaseSource,
|
|
310
|
+
endpoint,
|
|
311
|
+
dryRun: Boolean(options.dryRun),
|
|
312
|
+
uploaded: [],
|
|
313
|
+
skipped: [],
|
|
314
|
+
failed: [],
|
|
315
|
+
legacyIngester: false,
|
|
316
|
+
deletedMaps: []
|
|
317
|
+
};
|
|
318
|
+
const items = [];
|
|
319
|
+
for (const bundle of await findBundles(dir, options.files)) {
|
|
320
|
+
const prepared = await prepare(bundle, options.urlPrefix, release);
|
|
321
|
+
if ("error" in prepared) {
|
|
322
|
+
summary.failed.push({ path: bundle.path, filename: servedFilename(options.urlPrefix, bundle.path), error: prepared.error });
|
|
323
|
+
} else if (prepared.bytes > MAX_UPLOAD_BYTES) {
|
|
324
|
+
summary.failed.push({
|
|
325
|
+
path: prepared.path,
|
|
326
|
+
filename: prepared.filename,
|
|
327
|
+
error: `map is ${(prepared.bytes / 1048576).toFixed(1)} MB without sourcesContent; the ingester accepts up to 5 MB`
|
|
328
|
+
});
|
|
329
|
+
} else {
|
|
330
|
+
items.push(prepared);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (options.dryRun) {
|
|
334
|
+
summary.uploaded = items.map(strip);
|
|
335
|
+
return summary;
|
|
336
|
+
}
|
|
337
|
+
const key = (options.key ?? env.AUTTER_RUNTIME_KEY ?? "").trim();
|
|
338
|
+
if (!key) throw new FatalUploadError("no ingest key: set AUTTER_RUNTIME_KEY to a server key (autter_rt_\u2026)");
|
|
339
|
+
if (key.startsWith("autter_rtc_")) {
|
|
340
|
+
throw new FatalUploadError("AUTTER_RUNTIME_KEY is a publishable client key (autter_rtc_\u2026); uploads need a server key (autter_rt_\u2026)");
|
|
341
|
+
}
|
|
342
|
+
const headers = { "content-type": "application/json", authorization: `Bearer ${key}` };
|
|
343
|
+
const skipped = /* @__PURE__ */ new Set();
|
|
344
|
+
for (let start = 0; start < items.length && !summary.legacyIngester; start += CHECK_BATCH) {
|
|
345
|
+
const batch = items.slice(start, start + CHECK_BATCH);
|
|
346
|
+
try {
|
|
347
|
+
const res = await doFetch(`${endpoint}/v1/sourcemaps/check`, {
|
|
348
|
+
method: "POST",
|
|
349
|
+
headers,
|
|
350
|
+
body: JSON.stringify({
|
|
351
|
+
release,
|
|
352
|
+
files: batch.map((item) => ({ filename: item.filename, ...item.debugId ? { debugId: item.debugId } : {}, hash: item.hash }))
|
|
353
|
+
})
|
|
354
|
+
});
|
|
355
|
+
if (res.status === 404) {
|
|
356
|
+
summary.legacyIngester = true;
|
|
357
|
+
log(
|
|
358
|
+
"warning: this ingester predates 1.6.0, so it stores no debug ids and rejects .mjs/.cjs maps. Upgrade it: docker pull ghcr.io/autter-dev/otlp-ingester:latest"
|
|
359
|
+
);
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
if (res.status === 401 || res.status === 403) {
|
|
363
|
+
throw new FatalUploadError(`the ingester rejected the key (${res.status}): ${(await res.text()).slice(0, 300)}`);
|
|
364
|
+
}
|
|
365
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
366
|
+
const body = await res.json();
|
|
367
|
+
const present = new Set(Array.isArray(body.present) ? body.present : []);
|
|
368
|
+
batch.forEach((item, index) => {
|
|
369
|
+
if (present.has(index)) skipped.add(item);
|
|
370
|
+
});
|
|
371
|
+
} catch (err) {
|
|
372
|
+
if (err instanceof FatalUploadError) throw err;
|
|
373
|
+
log(`warning: could not check which maps the ingester already has (${err.message}); uploading all`);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
const uploaded = [];
|
|
377
|
+
let fatal = null;
|
|
378
|
+
const uploadOne = async (item) => {
|
|
379
|
+
for (let attempt = 1; ; attempt++) {
|
|
380
|
+
if (fatal) return;
|
|
381
|
+
let status = 0;
|
|
382
|
+
let detail = "";
|
|
383
|
+
try {
|
|
384
|
+
const res = await doFetch(`${endpoint}/v1/sourcemaps`, { method: "POST", headers, body: item.body });
|
|
385
|
+
status = res.status;
|
|
386
|
+
if (res.ok) {
|
|
387
|
+
uploaded.push(item);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
detail = (await res.text()).slice(0, 300);
|
|
391
|
+
} catch (err) {
|
|
392
|
+
detail = err.message;
|
|
393
|
+
}
|
|
394
|
+
if (status === 401 || status === 403) {
|
|
395
|
+
fatal = new FatalUploadError(`the ingester rejected the key (${status}): ${detail}`);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const retryable = status === 0 || status === 429 || status >= 500;
|
|
399
|
+
if (!retryable || attempt >= MAX_ATTEMPTS) {
|
|
400
|
+
summary.failed.push({ path: item.path, filename: item.filename, error: status ? `HTTP ${status}: ${detail}` : detail });
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
await sleep(500 * 2 ** (attempt - 1));
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
const queue = items.filter((item) => !skipped.has(item));
|
|
407
|
+
const workers = Array.from({ length: Math.max(1, Math.min(options.concurrency ?? 8, queue.length)) }, async () => {
|
|
408
|
+
for (let item = queue.shift(); item; item = queue.shift()) await uploadOne(item);
|
|
409
|
+
});
|
|
410
|
+
await Promise.all(workers);
|
|
411
|
+
if (fatal) throw fatal;
|
|
412
|
+
const order = (a, b) => items.indexOf(a) - items.indexOf(b);
|
|
413
|
+
uploaded.sort(order);
|
|
414
|
+
if (options.deleteAfterUpload) {
|
|
415
|
+
for (const item of [...uploaded, ...skipped]) {
|
|
416
|
+
await rm(item.bundle.map, { force: true });
|
|
417
|
+
const code = await readFile3(item.bundle.js, "utf8");
|
|
418
|
+
const stripped = stripSourceMappingUrl(code);
|
|
419
|
+
if (stripped !== code) await writeFile2(item.bundle.js, stripped);
|
|
420
|
+
summary.deletedMaps.push(item.bundle.map);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
summary.uploaded = uploaded.map(strip);
|
|
424
|
+
summary.skipped = [...skipped].sort(order).map(strip);
|
|
425
|
+
return summary;
|
|
426
|
+
}
|
|
427
|
+
function strip(item) {
|
|
428
|
+
const { path, filename, debugId, hash, bytes } = item;
|
|
429
|
+
return { path, filename, debugId, hash, bytes };
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// src/build.ts
|
|
433
|
+
function formatUploadSummary(summary) {
|
|
434
|
+
const lines = [];
|
|
435
|
+
const verb = summary.dryRun ? "would upload" : "uploaded";
|
|
436
|
+
for (const item of summary.uploaded) {
|
|
437
|
+
lines.push(`${verb} ${item.filename}${item.debugId ? ` (debug id ${item.debugId})` : ""}`);
|
|
438
|
+
}
|
|
439
|
+
for (const item of summary.skipped) lines.push(`skipped ${item.filename} (already uploaded)`);
|
|
440
|
+
for (const item of summary.failed) lines.push(`failed ${item.filename}: ${item.error}`);
|
|
441
|
+
lines.push(
|
|
442
|
+
`${summary.uploaded.length} ${verb}, ${summary.skipped.length} already uploaded, ${summary.failed.length} failed (release ${summary.release} from ${summary.releaseSource}, ${summary.endpoint})`
|
|
443
|
+
);
|
|
444
|
+
return lines;
|
|
445
|
+
}
|
|
446
|
+
async function processBuildOutput(input) {
|
|
447
|
+
const { options } = input;
|
|
448
|
+
const log = (line) => {
|
|
449
|
+
if (!options.silent) console.log(`[autter-runtime] ${line}`);
|
|
450
|
+
};
|
|
451
|
+
const result = { injected: [], upload: null };
|
|
452
|
+
try {
|
|
453
|
+
result.injected = await injectDir(input.dir, {
|
|
454
|
+
files: input.files,
|
|
455
|
+
dryRun: options.dryRun,
|
|
456
|
+
stripSourceMappingUrl: input.stripSourceMappingUrl
|
|
457
|
+
});
|
|
458
|
+
const changed = result.injected.filter((r) => r.status === "injected").length;
|
|
459
|
+
if (result.injected.length) {
|
|
460
|
+
log(`debug ids: ${changed} bundle(s) injected, ${result.injected.length - changed} already had one`);
|
|
461
|
+
}
|
|
462
|
+
for (const bad of result.injected.filter((r) => r.status === "invalid-map")) log(`skipped ${bad.path}: its map is not valid JSON`);
|
|
463
|
+
if (options.upload === false) return result;
|
|
464
|
+
const key = options.key ?? process.env.AUTTER_RUNTIME_KEY;
|
|
465
|
+
if (!key && !options.dryRun) {
|
|
466
|
+
if (result.injected.length) log("AUTTER_RUNTIME_KEY is not set: source maps were not uploaded");
|
|
467
|
+
return result;
|
|
468
|
+
}
|
|
469
|
+
if (!result.injected.some((r) => r.status !== "invalid-map")) return result;
|
|
470
|
+
result.upload = await uploadDir(input.dir, {
|
|
471
|
+
files: input.files,
|
|
472
|
+
release: options.release,
|
|
473
|
+
urlPrefix: options.urlPrefix ?? input.urlPrefix,
|
|
474
|
+
endpoint: options.endpoint,
|
|
475
|
+
key,
|
|
476
|
+
dryRun: options.dryRun,
|
|
477
|
+
concurrency: options.concurrency,
|
|
478
|
+
deleteAfterUpload: options.deleteAfterUpload ?? input.deleteAfterUpload,
|
|
479
|
+
log
|
|
480
|
+
});
|
|
481
|
+
for (const line of formatUploadSummary(result.upload)) log(line);
|
|
482
|
+
if (result.upload.failed.length && options.failOnError) {
|
|
483
|
+
throw new Error(`${result.upload.failed.length} source map upload(s) failed`);
|
|
484
|
+
}
|
|
485
|
+
} catch (err) {
|
|
486
|
+
const message = `source maps: ${err instanceof Error ? err.message : String(err)}`;
|
|
487
|
+
if (options.failOnError) throw new Error(`[autter-runtime] ${message}`);
|
|
488
|
+
if (!options.silent) console.warn(`[autter-runtime] ${message}`);
|
|
489
|
+
}
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export {
|
|
494
|
+
DEBUG_ID_RE,
|
|
495
|
+
normalizeDebugId,
|
|
496
|
+
debugIdFor,
|
|
497
|
+
readDebugIdComment,
|
|
498
|
+
DEBUG_ID_REGISTRY,
|
|
499
|
+
debugIdSnippet,
|
|
500
|
+
hasDebugIdSnippet,
|
|
501
|
+
BUNDLE_RE,
|
|
502
|
+
readSourceMappingUrl,
|
|
503
|
+
stripSourceMappingUrl,
|
|
504
|
+
mapPathFor,
|
|
505
|
+
findBundles,
|
|
506
|
+
SNIPPET_LINE_KEY,
|
|
507
|
+
snippetLine,
|
|
508
|
+
shiftMappings,
|
|
509
|
+
injectBundle,
|
|
510
|
+
injectDir,
|
|
511
|
+
RELEASE_RE,
|
|
512
|
+
RELEASE_ENV_VARS,
|
|
513
|
+
gitHead,
|
|
514
|
+
resolveRelease,
|
|
515
|
+
DEFAULT_ENDPOINT,
|
|
516
|
+
MAX_UPLOAD_BYTES,
|
|
517
|
+
resolveEndpoint,
|
|
518
|
+
servedFilename,
|
|
519
|
+
storedMapJson,
|
|
520
|
+
FatalUploadError,
|
|
521
|
+
uploadDir,
|
|
522
|
+
formatUploadSummary,
|
|
523
|
+
processBuildOutput
|
|
524
|
+
};
|