@starko-one/sdk 0.0.0-stage → 0.1.0-beta.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/CHANGELOG.md +248 -0
- package/LICENSE +17 -0
- package/NOTICE +58 -0
- package/README.md +81 -2
- package/dist/api-BAFm4Lxj.d.ts +558 -0
- package/dist/api-DNrctwJu.d.cts +558 -0
- package/dist/browser/index.cjs +168 -0
- package/dist/browser/index.d.cts +131 -0
- package/dist/browser/index.d.ts +131 -0
- package/dist/browser/index.js +7 -0
- package/dist/build/rollup.cjs +22 -0
- package/dist/build/rollup.d.cts +34 -0
- package/dist/build/rollup.d.ts +34 -0
- package/dist/build/rollup.js +3 -0
- package/dist/build/vite.cjs +51 -0
- package/dist/build/vite.d.cts +23 -0
- package/dist/build/vite.d.ts +23 -0
- package/dist/build/vite.js +46 -0
- package/dist/build/webpack.cjs +18 -0
- package/dist/build/webpack.d.cts +39 -0
- package/dist/build/webpack.d.ts +39 -0
- package/dist/build/webpack.js +3 -0
- package/dist/chunk-2PM4FRM5.cjs +431 -0
- package/dist/chunk-335HNCOJ.js +249 -0
- package/dist/chunk-3AW5B2WW.js +115 -0
- package/dist/chunk-3R5TQAWD.js +216 -0
- package/dist/chunk-6H5MOPTQ.cjs +1230 -0
- package/dist/chunk-7TKNT3QJ.cjs +2487 -0
- package/dist/chunk-A4WLNFMU.cjs +220 -0
- package/dist/chunk-ALYFFOPR.cjs +134 -0
- package/dist/chunk-BVVFVRMO.js +1113 -0
- package/dist/chunk-CIHNKN62.cjs +15 -0
- package/dist/chunk-CMGCH6HD.js +1212 -0
- package/dist/chunk-D6QAPQ7L.cjs +56 -0
- package/dist/chunk-DUO5V6WZ.js +213 -0
- package/dist/chunk-EC2WZ6PG.js +90 -0
- package/dist/chunk-EFURLFJT.cjs +187 -0
- package/dist/chunk-EROUORMY.js +53 -0
- package/dist/chunk-LXBXFOAP.cjs +96 -0
- package/dist/chunk-MF6SA6DB.js +563 -0
- package/dist/chunk-N5R6ORIB.js +413 -0
- package/dist/chunk-N6R6BWHK.cjs +142 -0
- package/dist/chunk-QAGGN2T7.cjs +225 -0
- package/dist/chunk-QV4HRIDD.cjs +659 -0
- package/dist/chunk-TKNDYQB3.cjs +583 -0
- package/dist/chunk-UI5YC4TH.js +131 -0
- package/dist/chunk-VEHZYLK6.cjs +1151 -0
- package/dist/chunk-VNCWOGTI.js +12 -0
- package/dist/chunk-VWNIOGE6.js +180 -0
- package/dist/chunk-WDKRKGYF.js +608 -0
- package/dist/chunk-XOZIXBUK.js +2462 -0
- package/dist/chunk-ZS3K3SDR.cjs +258 -0
- package/dist/cli/index.cjs +2780 -0
- package/dist/common-DLFAqL2d.d.cts +36 -0
- package/dist/common-DbSXkaw6.d.ts +36 -0
- package/dist/dedupe-Bh5riD8Z.d.cts +13 -0
- package/dist/dedupe-DoYiSx-9.d.ts +13 -0
- package/dist/handler-BcnsnvhF.d.cts +82 -0
- package/dist/handler-qHzilC9T.d.ts +82 -0
- package/dist/index.cjs +309 -0
- package/dist/index.d.cts +187 -0
- package/dist/index.d.ts +187 -0
- package/dist/index.js +4 -0
- package/dist/next/client.cjs +113 -0
- package/dist/next/client.d.cts +26 -0
- package/dist/next/client.d.ts +26 -0
- package/dist/next/client.js +41 -0
- package/dist/next/config.cjs +277 -0
- package/dist/next/config.d.cts +193 -0
- package/dist/next/config.d.ts +193 -0
- package/dist/next/config.js +263 -0
- package/dist/next/index.cjs +148 -0
- package/dist/next/index.d.cts +32 -0
- package/dist/next/index.d.ts +32 -0
- package/dist/next/index.js +59 -0
- package/dist/node/index.cjs +257 -0
- package/dist/node/index.d.cts +145 -0
- package/dist/node/index.d.ts +145 -0
- package/dist/node/index.js +83 -0
- package/dist/options-BUhWuWJX.d.cts +79 -0
- package/dist/options-BUhWuWJX.d.ts +79 -0
- package/dist/react/index.cjs +107 -0
- package/dist/react/index.d.cts +84 -0
- package/dist/react/index.d.ts +84 -0
- package/dist/react/index.js +103 -0
- package/dist/scope-D1ouJm2h.d.cts +628 -0
- package/dist/scope-D1ouJm2h.d.ts +628 -0
- package/dist/server-edge-7624N3WF.cjs +51 -0
- package/dist/server-edge-BEEXWL7T.js +48 -0
- package/dist/server-node-ET4LIPEC.cjs +27 -0
- package/dist/server-node-TQUVMZUC.js +24 -0
- package/dist/session-C_WzbEN2.d.cts +4 -0
- package/dist/session-C_WzbEN2.d.ts +4 -0
- package/dist/session-propagation-Pr0Zb9Zn.d.cts +30 -0
- package/dist/session-propagation-RM5py1bp.d.ts +30 -0
- package/dist/starko.min.js +6 -0
- package/docs/browser.md +386 -0
- package/docs/configuration.md +310 -0
- package/docs/nextjs.md +488 -0
- package/docs/node.md +427 -0
- package/docs/other-backends.md +232 -0
- package/docs/privacy.md +112 -0
- package/docs/react.md +187 -0
- package/docs/source-maps.md +482 -0
- package/docs/troubleshooting.md +263 -0
- package/docs/widget.md +84 -0
- package/docs/wire-format.md +547 -0
- package/package.json +191 -4
|
@@ -0,0 +1,2780 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/cli/main.ts
|
|
27
|
+
var import_node_fs2 = require("fs");
|
|
28
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
29
|
+
var import_node_util3 = require("util");
|
|
30
|
+
|
|
31
|
+
// src/build/shared/debug-id.ts
|
|
32
|
+
var import_node_crypto = require("crypto");
|
|
33
|
+
|
|
34
|
+
// src/types/wire.ts
|
|
35
|
+
var LIMITS = {
|
|
36
|
+
MAX_EVENTS_PER_ENVELOPE: 20,
|
|
37
|
+
/** Bytes of the request body as sent (after gzip when compressed). */
|
|
38
|
+
MAX_BODY_BYTES: 512 * 1024,
|
|
39
|
+
/** Bytes after decompression. */
|
|
40
|
+
MAX_INFLATED_BYTES: 2 * 1024 * 1024,
|
|
41
|
+
MAX_EXCEPTIONS: 10,
|
|
42
|
+
/** Beyond it the 25 most recent and the 25 oldest frames are kept (the middle of a deep recursion is dropped). */
|
|
43
|
+
MAX_FRAMES: 50,
|
|
44
|
+
/** Distinct `chunk_id`s the server resolves per event; frames of further chunks stay unresolved. */
|
|
45
|
+
MAX_CHUNK_IDS_PER_EVENT: 8,
|
|
46
|
+
/** Nesting depth of the JSON body; deeper bodies are rejected before any processing. */
|
|
47
|
+
MAX_JSON_DEPTH: 20,
|
|
48
|
+
MAX_TYPE: 200,
|
|
49
|
+
MAX_VALUE: 2e3,
|
|
50
|
+
MAX_FILENAME: 1e3,
|
|
51
|
+
MAX_FUNCTION: 300,
|
|
52
|
+
MAX_CONTEXT_LINE: 300,
|
|
53
|
+
/** Lines in each of pre_context / post_context. */
|
|
54
|
+
MAX_CONTEXT_LINES: 7,
|
|
55
|
+
MAX_BREADCRUMBS: 50,
|
|
56
|
+
/** Serialized bytes of all breadcrumbs. */
|
|
57
|
+
MAX_BREADCRUMB_BYTES: 32 * 1024,
|
|
58
|
+
MAX_TAGS: 50,
|
|
59
|
+
MAX_TAG_KEY: 64,
|
|
60
|
+
MAX_TAG_VALUE: 256,
|
|
61
|
+
/** Serialized bytes of `extra` and of `contexts`, each. */
|
|
62
|
+
MAX_EXTRA_BYTES: 16 * 1024,
|
|
63
|
+
MAX_FINGERPRINT: 128,
|
|
64
|
+
MAX_RELEASE: 128,
|
|
65
|
+
MAX_ENVIRONMENT: 64,
|
|
66
|
+
MAX_URL: 2048
|
|
67
|
+
};
|
|
68
|
+
var PATHS = {
|
|
69
|
+
INGEST: "/api/v1/ingest/errors",
|
|
70
|
+
CONFIG: "/api/v1/ingest/config",
|
|
71
|
+
SOURCEMAPS_CHECK: "/api/v1/sourcemaps/check",
|
|
72
|
+
SOURCEMAPS_UPLOAD: "/api/v1/sourcemaps/upload",
|
|
73
|
+
SOURCEMAPS_COMPLETE: "/api/v1/sourcemaps/complete",
|
|
74
|
+
/** Bulk upload (wire format v1, upload v2): see "Source-map bulk upload" below. */
|
|
75
|
+
SOURCEMAPS_BULK_CHECK: "/api/v1/sourcemaps/bulk/check",
|
|
76
|
+
SOURCEMAPS_BULK_START: "/api/v1/sourcemaps/bulk/start",
|
|
77
|
+
SOURCEMAPS_BULK_FINISH: "/api/v1/sourcemaps/bulk/finish"
|
|
78
|
+
};
|
|
79
|
+
var DEFAULT_HOST = "https://v2.starko.one";
|
|
80
|
+
var SOURCEMAP_PART_BYTES = 3 * 1024 * 1024;
|
|
81
|
+
var SOURCEMAP_MAX_BYTES = 64 * 1024 * 1024;
|
|
82
|
+
var SOURCEMAP_MAX_PARTS = 32;
|
|
83
|
+
var SOURCEMAP_MAX_CHUNKS_PER_CHECK = 500;
|
|
84
|
+
var SOURCEMAP_MAX_FILE_NAME = 512;
|
|
85
|
+
var SOURCEMAP_ROUTE_MAX_SECONDS = {
|
|
86
|
+
CHECK: 30,
|
|
87
|
+
UPLOAD: 60,
|
|
88
|
+
COMPLETE: 60,
|
|
89
|
+
BULK_CHECK: 30,
|
|
90
|
+
BULK_START: 30,
|
|
91
|
+
BULK_FINISH: 120
|
|
92
|
+
};
|
|
93
|
+
var SOURCEMAP_BULK_MAX_CHECK = 1e3;
|
|
94
|
+
var SOURCEMAP_BULK_MAX_MAPS = 200;
|
|
95
|
+
var SOURCEMAP_BULK_MAX_OBJECT_BYTES = Math.ceil(SOURCEMAP_MAX_BYTES * 1.01);
|
|
96
|
+
var SOURCEMAP_BULK_UPLOAD_CONCURRENCY = 16;
|
|
97
|
+
var SOURCEMAP_BULK_REQUEST_CONCURRENCY = 2;
|
|
98
|
+
var DEBUG_ID_GLOBAL = "_starkoDebugIds";
|
|
99
|
+
var RELEASE_GLOBAL = "_starkoRelease";
|
|
100
|
+
var DEBUG_ID_COMMENT = "//# debugId=";
|
|
101
|
+
var DEBUG_ID_NAMESPACE = "6f2d3c1e-9b4a-4f07-8a55-7c1e0d9b2a41";
|
|
102
|
+
|
|
103
|
+
// src/build/shared/debug-id.ts
|
|
104
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
105
|
+
function isDebugId(value) {
|
|
106
|
+
return typeof value === "string" && UUID_RE.test(value);
|
|
107
|
+
}
|
|
108
|
+
function createDebugId(code) {
|
|
109
|
+
const namespace = Buffer.from(DEBUG_ID_NAMESPACE.replace(/-/g, ""), "hex");
|
|
110
|
+
const digest = (0, import_node_crypto.createHash)("sha1").update(namespace).update(code, "utf8").digest();
|
|
111
|
+
digest[6] = digest[6] & 15 | 80;
|
|
112
|
+
digest[8] = digest[8] & 63 | 128;
|
|
113
|
+
const hex = digest.subarray(0, 16).toString("hex");
|
|
114
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
115
|
+
}
|
|
116
|
+
function debugIdComment(debugId) {
|
|
117
|
+
return `${DEBUG_ID_COMMENT}${debugId}`;
|
|
118
|
+
}
|
|
119
|
+
var COMMENT_LINE_RE = /^\/\/# debugId=([0-9a-fA-F-]{36})[ \t\r]*$/;
|
|
120
|
+
var MAX_TRAILING_LINES = 16;
|
|
121
|
+
function findDebugId(code) {
|
|
122
|
+
let end = code.length;
|
|
123
|
+
for (let i = 0; i < MAX_TRAILING_LINES && end > 0; i++) {
|
|
124
|
+
const start = code.lastIndexOf("\n", end - 1) + 1;
|
|
125
|
+
const line = code.slice(start, end).trim();
|
|
126
|
+
end = start - 1;
|
|
127
|
+
if (line === "") continue;
|
|
128
|
+
if (!line.startsWith("//")) return void 0;
|
|
129
|
+
const match = COMMENT_LINE_RE.exec(line);
|
|
130
|
+
if (match && isDebugId(match[1])) return match[1].toLowerCase();
|
|
131
|
+
}
|
|
132
|
+
return void 0;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/build/shared/snippet.ts
|
|
136
|
+
function jsStringLiteral(value) {
|
|
137
|
+
return JSON.stringify(value).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029").replace(/</g, "\\u003c");
|
|
138
|
+
}
|
|
139
|
+
function createSnippet(debugId, release) {
|
|
140
|
+
const id = jsStringLiteral(debugId);
|
|
141
|
+
const setRelease = release === void 0 ? "" : `g.${RELEASE_GLOBAL}||(g.${RELEASE_GLOBAL}=${jsStringLiteral(release)});`;
|
|
142
|
+
return ';!function(){try{var g="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{};' + setRelease + `var s=(new Error).stack;s&&((g.${DEBUG_ID_GLOBAL}=g.${DEBUG_ID_GLOBAL}||{})[s]=${id})}catch(e){}}();`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/build/shared/prologue.ts
|
|
146
|
+
function findInsertionOffset(code) {
|
|
147
|
+
let pos = 0;
|
|
148
|
+
if (code.charCodeAt(0) === 65279) pos = 1;
|
|
149
|
+
if (code.startsWith("#!", pos)) {
|
|
150
|
+
const nl = code.indexOf("\n", pos);
|
|
151
|
+
if (nl === -1) return code.length;
|
|
152
|
+
pos = nl + 1;
|
|
153
|
+
}
|
|
154
|
+
let safe = pos;
|
|
155
|
+
for (; ; ) {
|
|
156
|
+
const trivia = skipTrivia(code, pos);
|
|
157
|
+
if (trivia === void 0) return safe;
|
|
158
|
+
pos = trivia.end;
|
|
159
|
+
const ch = code[pos];
|
|
160
|
+
if (ch !== '"' && ch !== "'") return pos;
|
|
161
|
+
const end = scanString(code, pos);
|
|
162
|
+
if (end === void 0) return safe;
|
|
163
|
+
const after = skipTrivia(code, end);
|
|
164
|
+
if (after === void 0) return safe;
|
|
165
|
+
if (code[after.end] === ";") {
|
|
166
|
+
pos = after.end + 1;
|
|
167
|
+
safe = pos;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (after.end >= code.length) {
|
|
171
|
+
return code.length;
|
|
172
|
+
}
|
|
173
|
+
if (after.newline && !continuesExpression(code, after.end)) {
|
|
174
|
+
pos = after.end;
|
|
175
|
+
safe = pos;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
return pos;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
var LINE_TERMINATOR = /[\n\r\u2028\u2029]/;
|
|
182
|
+
function skipTrivia(code, from) {
|
|
183
|
+
let pos = from;
|
|
184
|
+
let newline = false;
|
|
185
|
+
while (pos < code.length) {
|
|
186
|
+
const ch = code[pos];
|
|
187
|
+
if (LINE_TERMINATOR.test(ch)) {
|
|
188
|
+
newline = true;
|
|
189
|
+
pos++;
|
|
190
|
+
} else if (/\s/.test(ch)) {
|
|
191
|
+
pos++;
|
|
192
|
+
} else if (ch === "/" && code[pos + 1] === "/") {
|
|
193
|
+
const nl = code.slice(pos).search(LINE_TERMINATOR);
|
|
194
|
+
if (nl === -1) return { end: code.length, newline };
|
|
195
|
+
pos += nl;
|
|
196
|
+
} else if (ch === "/" && code[pos + 1] === "*") {
|
|
197
|
+
const close = code.indexOf("*/", pos + 2);
|
|
198
|
+
if (close === -1) return void 0;
|
|
199
|
+
if (LINE_TERMINATOR.test(code.slice(pos + 2, close))) newline = true;
|
|
200
|
+
pos = close + 2;
|
|
201
|
+
} else {
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return { end: pos, newline };
|
|
206
|
+
}
|
|
207
|
+
function scanString(code, start) {
|
|
208
|
+
const quote = code[start];
|
|
209
|
+
let pos = start + 1;
|
|
210
|
+
while (pos < code.length) {
|
|
211
|
+
const ch = code[pos];
|
|
212
|
+
if (ch === quote) return pos + 1;
|
|
213
|
+
if (ch === "\\") {
|
|
214
|
+
pos += code[pos + 1] === "\r" && code[pos + 2] === "\n" ? 3 : 2;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (ch === "\n" || ch === "\r") return void 0;
|
|
218
|
+
pos++;
|
|
219
|
+
}
|
|
220
|
+
return void 0;
|
|
221
|
+
}
|
|
222
|
+
function continuesExpression(code, pos) {
|
|
223
|
+
const rest = code.slice(pos, pos + 12);
|
|
224
|
+
if (/^(?:\+\+|--)/.test(rest)) return false;
|
|
225
|
+
return /^(?:[.,([?:<>=&|^%*/`+\-~]|!=|in(?![\w$])|instanceof(?![\w$]))/.test(rest);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/build/shared/vlq.ts
|
|
229
|
+
var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
230
|
+
var INDEX = new Int8Array(128).fill(-1);
|
|
231
|
+
for (let i = 0; i < CHARS.length; i++) INDEX[CHARS.charCodeAt(i)] = i;
|
|
232
|
+
function decodeMappings(mappings) {
|
|
233
|
+
const lines = [];
|
|
234
|
+
let line = [];
|
|
235
|
+
const state = [0, 0, 0, 0, 0];
|
|
236
|
+
const values = [];
|
|
237
|
+
let value = 0;
|
|
238
|
+
let shift = 0;
|
|
239
|
+
const endSegment = () => {
|
|
240
|
+
if (values.length === 0) return;
|
|
241
|
+
if (values.length !== 1 && values.length !== 4 && values.length !== 5) {
|
|
242
|
+
throw new Error(`Invalid source map segment with ${values.length} fields`);
|
|
243
|
+
}
|
|
244
|
+
state[0] += values[0];
|
|
245
|
+
if (values.length === 1) {
|
|
246
|
+
line.push([state[0]]);
|
|
247
|
+
} else {
|
|
248
|
+
state[1] += values[1];
|
|
249
|
+
state[2] += values[2];
|
|
250
|
+
state[3] += values[3];
|
|
251
|
+
if (values.length === 5) {
|
|
252
|
+
state[4] += values[4];
|
|
253
|
+
line.push([state[0], state[1], state[2], state[3], state[4]]);
|
|
254
|
+
} else {
|
|
255
|
+
line.push([state[0], state[1], state[2], state[3]]);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
values.length = 0;
|
|
259
|
+
};
|
|
260
|
+
for (let i = 0; i < mappings.length; i++) {
|
|
261
|
+
const code = mappings.charCodeAt(i);
|
|
262
|
+
if (code === 44) {
|
|
263
|
+
endSegment();
|
|
264
|
+
} else if (code === 59) {
|
|
265
|
+
endSegment();
|
|
266
|
+
lines.push(line);
|
|
267
|
+
line = [];
|
|
268
|
+
state[0] = 0;
|
|
269
|
+
} else {
|
|
270
|
+
const digit = code < 128 ? INDEX[code] : -1;
|
|
271
|
+
if (digit === -1) throw new Error(`Invalid base64 VLQ character ${JSON.stringify(mappings[i])}`);
|
|
272
|
+
value += (digit & 31) * 2 ** shift;
|
|
273
|
+
if (digit & 32) {
|
|
274
|
+
shift += 5;
|
|
275
|
+
} else {
|
|
276
|
+
const negative = value % 2 === 1;
|
|
277
|
+
const magnitude = Math.floor(value / 2);
|
|
278
|
+
values.push(negative ? -magnitude || 0 : magnitude);
|
|
279
|
+
value = 0;
|
|
280
|
+
shift = 0;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
endSegment();
|
|
285
|
+
lines.push(line);
|
|
286
|
+
return lines;
|
|
287
|
+
}
|
|
288
|
+
function encodeValue(n) {
|
|
289
|
+
let vlq = n < 0 ? -n * 2 + 1 : n * 2;
|
|
290
|
+
let out = "";
|
|
291
|
+
do {
|
|
292
|
+
let digit = vlq % 32;
|
|
293
|
+
vlq = Math.floor(vlq / 32);
|
|
294
|
+
if (vlq > 0) digit |= 32;
|
|
295
|
+
out += CHARS[digit];
|
|
296
|
+
} while (vlq > 0);
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
function encodeMappings(lines) {
|
|
300
|
+
const state = [0, 0, 0, 0, 0];
|
|
301
|
+
let out = "";
|
|
302
|
+
for (let l = 0; l < lines.length; l++) {
|
|
303
|
+
if (l > 0) out += ";";
|
|
304
|
+
state[0] = 0;
|
|
305
|
+
const segments = lines[l];
|
|
306
|
+
for (let s = 0; s < segments.length; s++) {
|
|
307
|
+
const seg = segments[s];
|
|
308
|
+
if (s > 0) out += ",";
|
|
309
|
+
out += encodeValue(seg[0] - state[0]);
|
|
310
|
+
state[0] = seg[0];
|
|
311
|
+
if (seg.length === 1) continue;
|
|
312
|
+
for (let f = 1; f < seg.length; f++) {
|
|
313
|
+
out += encodeValue(seg[f] - state[f]);
|
|
314
|
+
state[f] = seg[f];
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return out;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// src/build/shared/inject.ts
|
|
322
|
+
var SOURCE_MAPPING_URL_LINE = /^[ \t]*\/\/[#@][ \t]+sourceMappingURL=/;
|
|
323
|
+
function findSourceMappingUrlLine(code) {
|
|
324
|
+
let end = code.length;
|
|
325
|
+
while (end > 0) {
|
|
326
|
+
const start = code.lastIndexOf("\n", end - 1) + 1;
|
|
327
|
+
const line = code.slice(start, end);
|
|
328
|
+
if (line.trim() === "") {
|
|
329
|
+
end = start - 1;
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
return SOURCE_MAPPING_URL_LINE.test(line) ? start : void 0;
|
|
333
|
+
}
|
|
334
|
+
return void 0;
|
|
335
|
+
}
|
|
336
|
+
function planInjection(code, options = {}) {
|
|
337
|
+
const debugId = options.debugId ?? createDebugId(code);
|
|
338
|
+
const urlLine = findSourceMappingUrlLine(code);
|
|
339
|
+
const comment = urlLine !== void 0 ? { offset: urlLine, text: `${debugIdComment(debugId)}
|
|
340
|
+
` } : {
|
|
341
|
+
offset: code.length,
|
|
342
|
+
text: `${code.length === 0 || code.endsWith("\n") ? "" : "\n"}${debugIdComment(debugId)}
|
|
343
|
+
`
|
|
344
|
+
};
|
|
345
|
+
const offset = Math.min(findInsertionOffset(code), comment.offset);
|
|
346
|
+
const atLineStart = offset === 0 || code[offset - 1] === "\n";
|
|
347
|
+
const snippetText = createSnippet(debugId, options.release);
|
|
348
|
+
const snippet = { offset, text: atLineStart ? `${snippetText}
|
|
349
|
+
` : snippetText };
|
|
350
|
+
return { debugId, snippet, comment };
|
|
351
|
+
}
|
|
352
|
+
function applyToCode(code, insertions) {
|
|
353
|
+
const sorted = insertions.map((ins, index) => ({ ins, index })).sort((a, b) => a.ins.offset - b.ins.offset || a.index - b.index);
|
|
354
|
+
let out = "";
|
|
355
|
+
let at = 0;
|
|
356
|
+
for (const { ins } of sorted) {
|
|
357
|
+
out += code.slice(at, ins.offset) + ins.text;
|
|
358
|
+
at = ins.offset;
|
|
359
|
+
}
|
|
360
|
+
return out + code.slice(at);
|
|
361
|
+
}
|
|
362
|
+
function lineAndColumn(code, offset) {
|
|
363
|
+
let line = 0;
|
|
364
|
+
let lineStart = 0;
|
|
365
|
+
for (let i = code.indexOf("\n"); i !== -1 && i < offset; i = code.indexOf("\n", i + 1)) {
|
|
366
|
+
line++;
|
|
367
|
+
lineStart = i + 1;
|
|
368
|
+
}
|
|
369
|
+
return { line, column: offset - lineStart };
|
|
370
|
+
}
|
|
371
|
+
function shiftSegments(lines, line, column, text) {
|
|
372
|
+
while (lines.length <= line) lines.push([]);
|
|
373
|
+
const segments = lines[line];
|
|
374
|
+
const newlines = text.split("\n").length - 1;
|
|
375
|
+
if (newlines === 0) {
|
|
376
|
+
for (const seg of segments) if (seg[0] >= column) seg[0] += text.length;
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const lastLineLength = text.length - text.lastIndexOf("\n") - 1;
|
|
380
|
+
const before2 = [];
|
|
381
|
+
const after = [];
|
|
382
|
+
for (const seg of segments) {
|
|
383
|
+
if (seg[0] >= column) {
|
|
384
|
+
seg[0] = seg[0] - column + lastLineLength;
|
|
385
|
+
after.push(seg);
|
|
386
|
+
} else {
|
|
387
|
+
before2.push(seg);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const inserted = Array.from({ length: newlines - 1 }, () => []);
|
|
391
|
+
lines.splice(line, 1, before2, ...inserted, after);
|
|
392
|
+
}
|
|
393
|
+
var before = (a, line, column) => a.line < line || a.line === line && a.column < column;
|
|
394
|
+
function movedPosition(p, at) {
|
|
395
|
+
const newlines = at.text.split("\n").length - 1;
|
|
396
|
+
if (newlines === 0) return p.line === at.line ? { line: p.line, column: p.column + at.text.length } : p;
|
|
397
|
+
const lastLineLength = at.text.length - at.text.lastIndexOf("\n") - 1;
|
|
398
|
+
return { line: p.line + newlines, column: p.line === at.line ? p.column - at.column + lastLineLength : p.column };
|
|
399
|
+
}
|
|
400
|
+
function shiftAt(map, ordered) {
|
|
401
|
+
if (Array.isArray(map.sections)) {
|
|
402
|
+
const sections = map.sections.map((sec) => ({ ...sec, offset: { line: Number(sec.offset?.line) || 0, column: Number(sec.offset?.column) || 0 } }));
|
|
403
|
+
for (const at of ordered) {
|
|
404
|
+
let inside = -1;
|
|
405
|
+
for (let i = 0; i < sections.length; i++) if (before(sections[i].offset, at.line, at.column)) inside = i;
|
|
406
|
+
for (let i = 0; i < sections.length; i++) {
|
|
407
|
+
const sec = sections[i];
|
|
408
|
+
if (!before(sec.offset, at.line, at.column)) sec.offset = movedPosition(sec.offset, at);
|
|
409
|
+
}
|
|
410
|
+
if (inside >= 0) {
|
|
411
|
+
const sec = sections[inside];
|
|
412
|
+
if (!sec.map || typeof sec.map !== "object") throw new Error("Index source map sections with a `url` instead of a `map` are not supported");
|
|
413
|
+
const rel = { line: at.line - sec.offset.line, column: at.line === sec.offset.line ? at.column - sec.offset.column : at.column, text: at.text };
|
|
414
|
+
sec.map = shiftAt(sec.map, [rel]);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return { ...map, sections };
|
|
418
|
+
}
|
|
419
|
+
const lines = decodeMappings(map.mappings ?? "");
|
|
420
|
+
for (const at of ordered) shiftSegments(lines, at.line, at.column, at.text);
|
|
421
|
+
return { ...map, mappings: encodeMappings(lines) };
|
|
422
|
+
}
|
|
423
|
+
function shiftSourceMap(map, code, insertions) {
|
|
424
|
+
const ordered = insertions.map((ins, index) => ({ ins, index })).sort((a, b) => b.ins.offset - a.ins.offset || b.index - a.index).map(({ ins }) => ({ ...lineAndColumn(code, ins.offset), text: ins.text }));
|
|
425
|
+
return shiftAt(map, ordered);
|
|
426
|
+
}
|
|
427
|
+
function hasStarkoRegistration(code, debugId) {
|
|
428
|
+
const id = debugId.toLowerCase();
|
|
429
|
+
return code.includes(`(g.${DEBUG_ID_GLOBAL}=g.${DEBUG_ID_GLOBAL}||{})[s]=${jsStringLiteral(id)}`);
|
|
430
|
+
}
|
|
431
|
+
function hasNativeRegistration(code, debugId) {
|
|
432
|
+
const head = code.slice(0, 4096);
|
|
433
|
+
const id = debugId.toLowerCase();
|
|
434
|
+
if (!head.toLowerCase().includes(id)) return false;
|
|
435
|
+
if (!/_(?:sentryD|d)ebugIds\b/.test(head)) return false;
|
|
436
|
+
return new RegExp(`\\]\\s*=\\s*["']${id}["']`, "i").test(head);
|
|
437
|
+
}
|
|
438
|
+
function injectDebugId(code, map, options = {}) {
|
|
439
|
+
const existing = findDebugId(code);
|
|
440
|
+
if (existing) {
|
|
441
|
+
const withId = (m) => m.debugId === existing ? m : { ...m, debugId: existing };
|
|
442
|
+
if (hasStarkoRegistration(code, existing) || options.keepNativelyRegistered && hasNativeRegistration(code, existing)) {
|
|
443
|
+
return { code, map: map && withId(map), debugId: existing, injected: false, kind: "none" };
|
|
444
|
+
}
|
|
445
|
+
const plan2 = planInjection(code, { ...options, debugId: existing });
|
|
446
|
+
const newMap2 = map ? withId(shiftSourceMap(map, code, [plan2.snippet])) : void 0;
|
|
447
|
+
return { code: applyToCode(code, [plan2.snippet]), map: newMap2, debugId: existing, injected: true, kind: "snippet" };
|
|
448
|
+
}
|
|
449
|
+
const plan = planInjection(code, options);
|
|
450
|
+
const insertions = [plan.snippet, plan.comment];
|
|
451
|
+
const newMap = map ? { ...shiftSourceMap(map, code, insertions), debugId: plan.debugId } : void 0;
|
|
452
|
+
return { code: applyToCode(code, insertions), map: newMap, debugId: plan.debugId, injected: true, kind: "full" };
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// src/build/shared/release.ts
|
|
456
|
+
var import_node_child_process = require("child_process");
|
|
457
|
+
var COMMIT_ENV_VARS = [
|
|
458
|
+
"VERCEL_GIT_COMMIT_SHA",
|
|
459
|
+
"GITHUB_SHA",
|
|
460
|
+
"CI_COMMIT_SHA",
|
|
461
|
+
// GitLab
|
|
462
|
+
"BITBUCKET_COMMIT",
|
|
463
|
+
"CIRCLE_SHA1",
|
|
464
|
+
"CF_PAGES_COMMIT_SHA",
|
|
465
|
+
"RENDER_GIT_COMMIT",
|
|
466
|
+
"RAILWAY_GIT_COMMIT_SHA",
|
|
467
|
+
"COMMIT_REF",
|
|
468
|
+
// Netlify
|
|
469
|
+
"BUILDKITE_COMMIT",
|
|
470
|
+
"DRONE_COMMIT_SHA",
|
|
471
|
+
"TRAVIS_COMMIT",
|
|
472
|
+
"BUILD_SOURCEVERSION",
|
|
473
|
+
// Azure Pipelines
|
|
474
|
+
"CODEBUILD_RESOLVED_SOURCE_VERSION",
|
|
475
|
+
"HEROKU_SLUG_COMMIT",
|
|
476
|
+
"SOURCE_VERSION",
|
|
477
|
+
// Heroku
|
|
478
|
+
"GIT_COMMIT"
|
|
479
|
+
// Jenkins
|
|
480
|
+
];
|
|
481
|
+
var BRANCH_ENV_VARS = [
|
|
482
|
+
"VERCEL_GIT_COMMIT_REF",
|
|
483
|
+
"GITHUB_HEAD_REF",
|
|
484
|
+
"GITHUB_REF_NAME",
|
|
485
|
+
"CI_COMMIT_REF_NAME",
|
|
486
|
+
"BITBUCKET_BRANCH",
|
|
487
|
+
"CIRCLE_BRANCH",
|
|
488
|
+
"CF_PAGES_BRANCH",
|
|
489
|
+
"RENDER_GIT_BRANCH",
|
|
490
|
+
"RAILWAY_GIT_BRANCH",
|
|
491
|
+
"BRANCH",
|
|
492
|
+
// Netlify
|
|
493
|
+
"BUILDKITE_BRANCH",
|
|
494
|
+
"DRONE_BRANCH",
|
|
495
|
+
"TRAVIS_BRANCH",
|
|
496
|
+
"BUILD_SOURCEBRANCHNAME",
|
|
497
|
+
"GIT_BRANCH"
|
|
498
|
+
];
|
|
499
|
+
function clean(value) {
|
|
500
|
+
const trimmed = value?.trim();
|
|
501
|
+
return trimmed ? trimmed : void 0;
|
|
502
|
+
}
|
|
503
|
+
function firstEnv(env, names) {
|
|
504
|
+
for (const name of names) {
|
|
505
|
+
const value = clean(env[name]);
|
|
506
|
+
if (value) return value;
|
|
507
|
+
}
|
|
508
|
+
return void 0;
|
|
509
|
+
}
|
|
510
|
+
function git(args, cwd) {
|
|
511
|
+
try {
|
|
512
|
+
return clean(
|
|
513
|
+
(0, import_node_child_process.execFileSync)("git", args, {
|
|
514
|
+
cwd,
|
|
515
|
+
encoding: "utf8",
|
|
516
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
517
|
+
timeout: 5e3,
|
|
518
|
+
windowsHide: true
|
|
519
|
+
})
|
|
520
|
+
);
|
|
521
|
+
} catch {
|
|
522
|
+
return void 0;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
function resolveCommit(options = {}) {
|
|
526
|
+
const env = options.env ?? process.env;
|
|
527
|
+
return firstEnv(env, COMMIT_ENV_VARS) ?? (options.useGit === false ? void 0 : git(["rev-parse", "HEAD"], options.cwd));
|
|
528
|
+
}
|
|
529
|
+
function resolveRelease(explicit, options = {}) {
|
|
530
|
+
const env = options.env ?? process.env;
|
|
531
|
+
const release = clean(explicit) ?? clean(env.STARKO_RELEASE) ?? resolveCommit(options);
|
|
532
|
+
return release === void 0 ? void 0 : release.slice(0, LIMITS.MAX_RELEASE);
|
|
533
|
+
}
|
|
534
|
+
function resolveReleaseInfo(explicit, options = {}) {
|
|
535
|
+
const version2 = resolveRelease(explicit, options);
|
|
536
|
+
if (version2 === void 0) return void 0;
|
|
537
|
+
const env = options.env ?? process.env;
|
|
538
|
+
const commit = resolveCommit(options);
|
|
539
|
+
let branch = firstEnv(env, BRANCH_ENV_VARS);
|
|
540
|
+
if (!branch && options.useGit !== false) {
|
|
541
|
+
const head = git(["rev-parse", "--abbrev-ref", "HEAD"], options.cwd);
|
|
542
|
+
if (head && head !== "HEAD") branch = head;
|
|
543
|
+
}
|
|
544
|
+
const info = { version: version2 };
|
|
545
|
+
if (commit) info.commit = commit;
|
|
546
|
+
if (branch) info.branch = branch;
|
|
547
|
+
return info;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// src/build/shared/options.ts
|
|
551
|
+
var DEFAULT_UPLOAD_TIMEOUT_MS = 18e4;
|
|
552
|
+
var DEFAULT_UPLOAD_CONCURRENCY = 8;
|
|
553
|
+
var MAX_UPLOAD_CONCURRENCY = 16;
|
|
554
|
+
var DEFAULT_BULK_UPLOAD_CONCURRENCY = SOURCEMAP_BULK_UPLOAD_CONCURRENCY;
|
|
555
|
+
function resolveUploadConcurrency(value, defaultValue = DEFAULT_UPLOAD_CONCURRENCY) {
|
|
556
|
+
if (value === void 0 || !Number.isFinite(value)) return defaultValue;
|
|
557
|
+
return Math.min(MAX_UPLOAD_CONCURRENCY, Math.max(1, Math.floor(value)));
|
|
558
|
+
}
|
|
559
|
+
function errorMessage(error) {
|
|
560
|
+
if (error instanceof Error) return error.message;
|
|
561
|
+
try {
|
|
562
|
+
return String(error);
|
|
563
|
+
} catch {
|
|
564
|
+
return "unknown error";
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
function redact(message, secrets) {
|
|
568
|
+
let out = message;
|
|
569
|
+
for (const secret of secrets) {
|
|
570
|
+
if (secret && secret.length >= 4) out = out.split(secret).join("[redacted]");
|
|
571
|
+
}
|
|
572
|
+
return out;
|
|
573
|
+
}
|
|
574
|
+
function createLogger(options = {}) {
|
|
575
|
+
const prefix = options.prefix ?? "[starko]";
|
|
576
|
+
const secrets = options.secrets ?? [];
|
|
577
|
+
const sink = options.sink ?? ((level, line) => level === "info" ? console.log(line) : level === "warn" ? console.warn(line) : console.error(line));
|
|
578
|
+
const write = (level) => (message) => {
|
|
579
|
+
if (!options.silent) sink(level, prefix ? `${prefix} ${redact(message, secrets)}` : redact(message, secrets));
|
|
580
|
+
};
|
|
581
|
+
return { info: write("info"), warn: write("warn"), error: write("error") };
|
|
582
|
+
}
|
|
583
|
+
function resolveConnection(options, env = process.env) {
|
|
584
|
+
const apiKey = options.apiKey?.trim() || env.STARKO_API_KEY?.trim() || void 0;
|
|
585
|
+
const host = (options.host?.trim() || env.STARKO_HOST?.trim() || DEFAULT_HOST).replace(/\/+$/, "");
|
|
586
|
+
return { apiKey, host };
|
|
587
|
+
}
|
|
588
|
+
function escapeRegExp(text) {
|
|
589
|
+
return text.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
590
|
+
}
|
|
591
|
+
function globToRegExp(glob) {
|
|
592
|
+
const pattern = glob.includes("/") ? glob.replace(/^\.\//, "") : `**/${glob}`;
|
|
593
|
+
let re = "";
|
|
594
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
595
|
+
const ch = pattern[i];
|
|
596
|
+
if (ch === "*") {
|
|
597
|
+
if (pattern[i + 1] === "*") {
|
|
598
|
+
const slash = pattern[i + 2] === "/";
|
|
599
|
+
re += slash ? "(?:.*/)?" : ".*";
|
|
600
|
+
i += slash ? 2 : 1;
|
|
601
|
+
} else {
|
|
602
|
+
re += "[^/]*";
|
|
603
|
+
}
|
|
604
|
+
} else if (ch === "?") {
|
|
605
|
+
re += "[^/]";
|
|
606
|
+
} else {
|
|
607
|
+
re += escapeRegExp(ch);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return new RegExp(`^${re}$`);
|
|
611
|
+
}
|
|
612
|
+
var JS_FILE = /\.(?:m|c)?js$/;
|
|
613
|
+
var DEFAULT_EXCLUDE = ["**/node_modules/**"];
|
|
614
|
+
function toArray(value) {
|
|
615
|
+
if (value === void 0) return void 0;
|
|
616
|
+
return Array.isArray(value) ? value : [value];
|
|
617
|
+
}
|
|
618
|
+
function test(pattern, path3) {
|
|
619
|
+
if (typeof pattern === "string") return globToRegExp(pattern).test(path3);
|
|
620
|
+
pattern.lastIndex = 0;
|
|
621
|
+
return pattern.test(path3);
|
|
622
|
+
}
|
|
623
|
+
function createFileFilter(options) {
|
|
624
|
+
const include = toArray(options.include);
|
|
625
|
+
const exclude = toArray(options.exclude) ?? DEFAULT_EXCLUDE;
|
|
626
|
+
return (relativePath) => {
|
|
627
|
+
const path3 = relativePath.replace(/\\/g, "/");
|
|
628
|
+
if (!JS_FILE.test(path3)) return false;
|
|
629
|
+
if (include && !include.some((p) => test(p, path3))) return false;
|
|
630
|
+
return !exclude.some((p) => test(p, path3));
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// src/build/shared/upload-bulk.ts
|
|
635
|
+
var import_node_crypto3 = require("crypto");
|
|
636
|
+
var import_node_util2 = require("util");
|
|
637
|
+
var import_node_zlib2 = require("zlib");
|
|
638
|
+
|
|
639
|
+
// src/build/shared/upload-core.ts
|
|
640
|
+
var import_node_crypto2 = require("crypto");
|
|
641
|
+
|
|
642
|
+
// package.json
|
|
643
|
+
var version = "0.1.0-beta.1";
|
|
644
|
+
|
|
645
|
+
// src/build/shared/version.ts
|
|
646
|
+
var PACKAGE_VERSION = version;
|
|
647
|
+
var USER_AGENT = `starko-sdk-build/${version}`;
|
|
648
|
+
|
|
649
|
+
// src/build/shared/upload-core.ts
|
|
650
|
+
var DEFAULT_MAX_REQUESTS_PER_MINUTE = 1900;
|
|
651
|
+
var DEFAULT_MAX_BYTES_IN_FLIGHT = 256 * 1024 * 1024;
|
|
652
|
+
var MAX_RETRY_AFTER_MS = 12e4;
|
|
653
|
+
var MAX_RATE_LIMITED_ATTEMPTS = 8;
|
|
654
|
+
var ROUTE_TIMEOUT_MARGIN_MS = 15e3;
|
|
655
|
+
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
656
|
+
var ROUTE_SECONDS = {
|
|
657
|
+
[PATHS.SOURCEMAPS_CHECK]: SOURCEMAP_ROUTE_MAX_SECONDS.CHECK,
|
|
658
|
+
[PATHS.SOURCEMAPS_UPLOAD]: SOURCEMAP_ROUTE_MAX_SECONDS.UPLOAD,
|
|
659
|
+
[PATHS.SOURCEMAPS_COMPLETE]: SOURCEMAP_ROUTE_MAX_SECONDS.COMPLETE,
|
|
660
|
+
[PATHS.SOURCEMAPS_BULK_CHECK]: SOURCEMAP_ROUTE_MAX_SECONDS.BULK_CHECK,
|
|
661
|
+
[PATHS.SOURCEMAPS_BULK_START]: SOURCEMAP_ROUTE_MAX_SECONDS.BULK_START,
|
|
662
|
+
[PATHS.SOURCEMAPS_BULK_FINISH]: SOURCEMAP_ROUTE_MAX_SECONDS.BULK_FINISH
|
|
663
|
+
};
|
|
664
|
+
function routeTimeoutMs(path3) {
|
|
665
|
+
const seconds = ROUTE_SECONDS[path3.split("?")[0]];
|
|
666
|
+
return seconds === void 0 ? DEFAULT_TIMEOUT_MS : seconds * 1e3 + ROUTE_TIMEOUT_MARGIN_MS;
|
|
667
|
+
}
|
|
668
|
+
function wireFileName(fileName) {
|
|
669
|
+
if (fileName.length <= SOURCEMAP_MAX_FILE_NAME) return fileName;
|
|
670
|
+
let tail = fileName.slice(fileName.length - (SOURCEMAP_MAX_FILE_NAME - 1));
|
|
671
|
+
const first = tail.charCodeAt(0);
|
|
672
|
+
if (first >= 56320 && first <= 57343) tail = tail.slice(1);
|
|
673
|
+
return `\u2026${tail}`;
|
|
674
|
+
}
|
|
675
|
+
var UPLOAD_PROTOCOLS = ["auto", "bulk", "v1"];
|
|
676
|
+
function createUploadProgress() {
|
|
677
|
+
return { total: 0, checked: 0, uploaded: 0, skipped: 0, failed: 0, sending: 0, bytesSent: 0 };
|
|
678
|
+
}
|
|
679
|
+
var StarkoUploadError = class extends Error {
|
|
680
|
+
constructor(message, options = {}) {
|
|
681
|
+
super(message);
|
|
682
|
+
this.name = "StarkoUploadError";
|
|
683
|
+
this.status = options.status;
|
|
684
|
+
this.fatal = options.fatal ?? false;
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
function sha256Hex(data) {
|
|
688
|
+
return (0, import_node_crypto2.createHash)("sha256").update(data).digest("hex");
|
|
689
|
+
}
|
|
690
|
+
function abortedError() {
|
|
691
|
+
return new StarkoUploadError("the upload was stopped (deadline reached)", { fatal: true });
|
|
692
|
+
}
|
|
693
|
+
function sleep(ms, signal) {
|
|
694
|
+
return new Promise((resolve, reject) => {
|
|
695
|
+
if (signal?.aborted) return reject(abortedError());
|
|
696
|
+
const onAbort = () => {
|
|
697
|
+
clearTimeout(timer);
|
|
698
|
+
reject(abortedError());
|
|
699
|
+
};
|
|
700
|
+
const timer = setTimeout(() => {
|
|
701
|
+
signal?.removeEventListener("abort", onAbort);
|
|
702
|
+
resolve();
|
|
703
|
+
}, ms);
|
|
704
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
function retryAfterMs(header) {
|
|
708
|
+
if (!header) return void 0;
|
|
709
|
+
const seconds = Number(header);
|
|
710
|
+
if (Number.isFinite(seconds) && seconds >= 0) return seconds * 1e3;
|
|
711
|
+
const date = Date.parse(header);
|
|
712
|
+
return Number.isNaN(date) ? void 0 : Math.max(0, date - Date.now());
|
|
713
|
+
}
|
|
714
|
+
function formatDuration(ms) {
|
|
715
|
+
if (ms < 9950) return `${Number((Math.max(0, ms) / 1e3).toFixed(1))} s`;
|
|
716
|
+
const s = Math.round(ms / 1e3);
|
|
717
|
+
if (s < 120) return `${s} s`;
|
|
718
|
+
if (s < 2 * 3600) return `${Math.round(s / 60)} min`;
|
|
719
|
+
return `${Math.round(s / 3600)} h`;
|
|
720
|
+
}
|
|
721
|
+
function networkReason(error) {
|
|
722
|
+
const base = errorMessage(error);
|
|
723
|
+
const cause = error?.cause;
|
|
724
|
+
const code = typeof cause?.code === "string" ? cause.code : void 0;
|
|
725
|
+
if (code) return `${base} (${code})`;
|
|
726
|
+
if (typeof cause?.message === "string" && cause.message && cause.message !== base) return `${base} (${cause.message})`;
|
|
727
|
+
return base;
|
|
728
|
+
}
|
|
729
|
+
var Pacer = class {
|
|
730
|
+
constructor(perMinute, now = Date.now()) {
|
|
731
|
+
this.perMs = perMinute / 6e4;
|
|
732
|
+
this.burst = Math.max(1, Math.round(this.perMs * 2e3));
|
|
733
|
+
this.tokens = this.burst;
|
|
734
|
+
this.last = now;
|
|
735
|
+
}
|
|
736
|
+
/** Takes a token and returns 0, or returns how long to wait before asking again. */
|
|
737
|
+
reserve(now) {
|
|
738
|
+
this.tokens = Math.min(this.burst, this.tokens + (now - this.last) * this.perMs);
|
|
739
|
+
this.last = now;
|
|
740
|
+
if (this.tokens >= 1) {
|
|
741
|
+
this.tokens -= 1;
|
|
742
|
+
return 0;
|
|
743
|
+
}
|
|
744
|
+
return Math.max(1, Math.ceil((1 - this.tokens) / this.perMs));
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
var Budget = class {
|
|
748
|
+
constructor(limit) {
|
|
749
|
+
this.limit = limit;
|
|
750
|
+
this.used = 0;
|
|
751
|
+
this.waiters = [];
|
|
752
|
+
}
|
|
753
|
+
/** Weight held right now (tests). */
|
|
754
|
+
get inUse() {
|
|
755
|
+
return this.used;
|
|
756
|
+
}
|
|
757
|
+
async acquire(weight) {
|
|
758
|
+
const held = await this.hold(weight);
|
|
759
|
+
return () => held.release();
|
|
760
|
+
}
|
|
761
|
+
async hold(weight) {
|
|
762
|
+
let w = Math.min(Math.max(0, weight), this.limit);
|
|
763
|
+
if (this.waiters.length === 0 && (this.used === 0 || this.used + w <= this.limit)) {
|
|
764
|
+
this.used += w;
|
|
765
|
+
} else {
|
|
766
|
+
await new Promise((resolve) => this.waiters.push({ weight: w, resolve }));
|
|
767
|
+
}
|
|
768
|
+
let released = false;
|
|
769
|
+
return {
|
|
770
|
+
release: () => {
|
|
771
|
+
if (released) return;
|
|
772
|
+
released = true;
|
|
773
|
+
this.used -= w;
|
|
774
|
+
this.drain();
|
|
775
|
+
},
|
|
776
|
+
resize: (next) => {
|
|
777
|
+
if (released) return;
|
|
778
|
+
const n = Math.min(Math.max(0, next), this.limit);
|
|
779
|
+
this.used += n - w;
|
|
780
|
+
w = n;
|
|
781
|
+
this.drain();
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
drain() {
|
|
786
|
+
while (this.waiters.length > 0) {
|
|
787
|
+
const next = this.waiters[0];
|
|
788
|
+
if (this.used !== 0 && this.used + next.weight > this.limit) return;
|
|
789
|
+
this.waiters.shift();
|
|
790
|
+
this.used += next.weight;
|
|
791
|
+
next.resolve();
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
/** Lets every waiter through (the upload is stopping; they check the stop themselves). */
|
|
795
|
+
releaseAll() {
|
|
796
|
+
for (const w of this.waiters.splice(0)) {
|
|
797
|
+
this.used += w.weight;
|
|
798
|
+
w.resolve();
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
var Channel = class {
|
|
803
|
+
constructor() {
|
|
804
|
+
this.items = [];
|
|
805
|
+
this.waiters = [];
|
|
806
|
+
this.lengthWaiters = [];
|
|
807
|
+
this.closedFlag = false;
|
|
808
|
+
}
|
|
809
|
+
get closed() {
|
|
810
|
+
return this.closedFlag;
|
|
811
|
+
}
|
|
812
|
+
get length() {
|
|
813
|
+
return this.items.length;
|
|
814
|
+
}
|
|
815
|
+
push(value) {
|
|
816
|
+
if (this.closedFlag) return;
|
|
817
|
+
const waiter = this.waiters.shift();
|
|
818
|
+
if (waiter) waiter(value);
|
|
819
|
+
else {
|
|
820
|
+
this.items.push(value);
|
|
821
|
+
this.wakeLength();
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
next() {
|
|
825
|
+
if (this.items.length > 0) return Promise.resolve(this.items.shift());
|
|
826
|
+
if (this.closedFlag) return Promise.resolve(void 0);
|
|
827
|
+
return new Promise((resolve) => this.waiters.push(resolve));
|
|
828
|
+
}
|
|
829
|
+
/** Up to `n` queued items, without waiting. */
|
|
830
|
+
take(n) {
|
|
831
|
+
return this.items.splice(0, Math.max(0, n));
|
|
832
|
+
}
|
|
833
|
+
/** Everything queued (the queue stays open). */
|
|
834
|
+
drainAll() {
|
|
835
|
+
return this.items.splice(0);
|
|
836
|
+
}
|
|
837
|
+
/** Resolves once at least `n` items are queued, the queue is closed, or `ms` passed. */
|
|
838
|
+
waitForLength(n, ms) {
|
|
839
|
+
if (this.items.length >= n || this.closedFlag || ms <= 0) return Promise.resolve();
|
|
840
|
+
return new Promise((resolve) => {
|
|
841
|
+
const entry = {
|
|
842
|
+
n,
|
|
843
|
+
resolve: () => {
|
|
844
|
+
clearTimeout(timer);
|
|
845
|
+
resolve();
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
const timer = setTimeout(() => {
|
|
849
|
+
const at = this.lengthWaiters.indexOf(entry);
|
|
850
|
+
if (at !== -1) this.lengthWaiters.splice(at, 1);
|
|
851
|
+
resolve();
|
|
852
|
+
}, ms);
|
|
853
|
+
this.lengthWaiters.push(entry);
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
wakeLength() {
|
|
857
|
+
for (let i = this.lengthWaiters.length - 1; i >= 0; i--) {
|
|
858
|
+
const w = this.lengthWaiters[i];
|
|
859
|
+
if (this.items.length >= w.n || this.closedFlag) {
|
|
860
|
+
this.lengthWaiters.splice(i, 1);
|
|
861
|
+
w.resolve();
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
/** No more items; with `drop`, the queued ones are discarded too. */
|
|
866
|
+
close(drop = false) {
|
|
867
|
+
this.closedFlag = true;
|
|
868
|
+
if (drop) this.items = [];
|
|
869
|
+
for (const w of this.waiters.splice(0)) w(void 0);
|
|
870
|
+
this.wakeLength();
|
|
871
|
+
}
|
|
872
|
+
async *[Symbol.asyncIterator]() {
|
|
873
|
+
for (; ; ) {
|
|
874
|
+
const v = await this.next();
|
|
875
|
+
if (v === void 0) return;
|
|
876
|
+
yield v;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
async function readItem(item) {
|
|
881
|
+
const content = item.map ?? (item.load ? await item.load() : void 0);
|
|
882
|
+
if (content === void 0) throw new Error("no source map content");
|
|
883
|
+
return typeof content === "string" ? Buffer.from(content, "utf8") : content;
|
|
884
|
+
}
|
|
885
|
+
var SecretBook = class {
|
|
886
|
+
constructor(apiKey) {
|
|
887
|
+
this.literals = /* @__PURE__ */ new Set();
|
|
888
|
+
this.hosts = /* @__PURE__ */ new Set();
|
|
889
|
+
this.add(apiKey, 4);
|
|
890
|
+
}
|
|
891
|
+
add(secret, minLength = 8) {
|
|
892
|
+
if (secret && secret.length >= minLength) this.literals.add(secret);
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* A signed URL: the URL, its query, its path with the query, and its
|
|
896
|
+
* `signature` value (raw and decoded) are all redacted from then on.
|
|
897
|
+
*/
|
|
898
|
+
addSignedUrl(raw) {
|
|
899
|
+
this.add(raw);
|
|
900
|
+
let url;
|
|
901
|
+
try {
|
|
902
|
+
url = new URL(raw);
|
|
903
|
+
} catch {
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
this.hosts.add(url.host);
|
|
907
|
+
if (url.search.length > 1) this.add(url.search.slice(1));
|
|
908
|
+
this.add(`${url.pathname}${url.search}`);
|
|
909
|
+
if (url.pathname.length > 1) this.add(url.pathname.slice(1));
|
|
910
|
+
for (const [name, value] of url.searchParams) {
|
|
911
|
+
if (/signature|sig$/i.test(name)) {
|
|
912
|
+
this.add(value);
|
|
913
|
+
this.add(encodeURIComponent(value));
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
redact(message) {
|
|
918
|
+
let out = redact(message, Array.from(this.literals).sort((a, b) => b.length - a.length));
|
|
919
|
+
out = out.replace(/([?&;](?:[a-z0-9_-]*signature|sig|x-amz-signature|x-ut-[a-z-]+|expires)=)[^&\s"'<>)]*/gi, "$1[redacted]");
|
|
920
|
+
for (const host of this.hosts) {
|
|
921
|
+
const escaped = host.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
922
|
+
out = out.replace(new RegExp(`(https?://${escaped})[/?#][^\\s"'<>)]*`, "gi"), "$1/[redacted]");
|
|
923
|
+
}
|
|
924
|
+
return out;
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
var Session = class {
|
|
928
|
+
constructor(options) {
|
|
929
|
+
this.result = { uploaded: [], skipped: [], failed: [] };
|
|
930
|
+
/** Every request waits until then (a 429's Retry-After). */
|
|
931
|
+
this.pausedUntil = 0;
|
|
932
|
+
this.consecutiveNetworkErrors = 0;
|
|
933
|
+
/** The Starko server's clock minus ours (from its `Date` header): signed URLs expire by the server's clock. */
|
|
934
|
+
this.clockOffsetMs = 0;
|
|
935
|
+
this.stopListeners = /* @__PURE__ */ new Set();
|
|
936
|
+
this.options = options;
|
|
937
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
938
|
+
if (typeof doFetch !== "function") throw new StarkoUploadError("global fetch is not available (Node 18 or later is required)", { fatal: true });
|
|
939
|
+
this.doFetch = doFetch;
|
|
940
|
+
this.host = options.host.replace(/\/+$/, "");
|
|
941
|
+
this.apiKey = options.apiKey;
|
|
942
|
+
this.release = options.release;
|
|
943
|
+
this.retries = options.retries ?? 4;
|
|
944
|
+
this.retryBaseMs = options.retryBaseMs ?? 500;
|
|
945
|
+
this.maxRetryDelayMs = options.maxRetryDelayMs ?? 3e4;
|
|
946
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
947
|
+
this.maxConsecutiveNetworkErrors = Math.max(1, options.maxConsecutiveNetworkErrors ?? 3);
|
|
948
|
+
this.signal = options.signal;
|
|
949
|
+
this.deadlineAt = options.deadlineAt;
|
|
950
|
+
this.logger = options.logger;
|
|
951
|
+
this.progress = options.progress ?? createUploadProgress();
|
|
952
|
+
this.pacer = new Pacer(Math.max(1, options.maxRequestsPerMinute ?? DEFAULT_MAX_REQUESTS_PER_MINUTE));
|
|
953
|
+
this.budget = new Budget(Math.max(1, options.maxBytesInFlight ?? DEFAULT_MAX_BYTES_IN_FLIGHT));
|
|
954
|
+
this.secrets = new SecretBook(options.apiKey);
|
|
955
|
+
this.signal?.addEventListener("abort", () => this.notifyStop(), { once: true });
|
|
956
|
+
}
|
|
957
|
+
stopped() {
|
|
958
|
+
return this.stop !== void 0 || this.signal?.aborted === true;
|
|
959
|
+
}
|
|
960
|
+
/** Stops everything at once with `error` (the first one wins). */
|
|
961
|
+
stopWith(error) {
|
|
962
|
+
this.stop ??= error;
|
|
963
|
+
this.notifyStop();
|
|
964
|
+
}
|
|
965
|
+
/** Called once when the upload stops (a fatal error or the deadline); returns the unsubscribe. */
|
|
966
|
+
onStop(fn) {
|
|
967
|
+
if (this.stopped()) {
|
|
968
|
+
fn();
|
|
969
|
+
return () => void 0;
|
|
970
|
+
}
|
|
971
|
+
this.stopListeners.add(fn);
|
|
972
|
+
return () => this.stopListeners.delete(fn);
|
|
973
|
+
}
|
|
974
|
+
notifyStop() {
|
|
975
|
+
this.budget.releaseAll();
|
|
976
|
+
for (const fn of Array.from(this.stopListeners)) {
|
|
977
|
+
this.stopListeners.delete(fn);
|
|
978
|
+
try {
|
|
979
|
+
fn();
|
|
980
|
+
} catch {
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
redact(message) {
|
|
985
|
+
return this.secrets.redact(message);
|
|
986
|
+
}
|
|
987
|
+
info(message) {
|
|
988
|
+
this.logger?.info(this.redact(message));
|
|
989
|
+
}
|
|
990
|
+
warn(message) {
|
|
991
|
+
this.logger?.warn(this.redact(message));
|
|
992
|
+
}
|
|
993
|
+
fail(chunkId, fileName, error) {
|
|
994
|
+
this.result.failed.push({ chunkId, fileName, error: this.redact(error) });
|
|
995
|
+
this.progress.failed++;
|
|
996
|
+
}
|
|
997
|
+
/** The protocol this upload uses (chosen once). */
|
|
998
|
+
useProtocol(protocol) {
|
|
999
|
+
this.result.protocol = protocol;
|
|
1000
|
+
this.progress.protocol = protocol;
|
|
1001
|
+
}
|
|
1002
|
+
skip(chunkId, fileName) {
|
|
1003
|
+
this.result.skipped.push({ chunkId, fileName });
|
|
1004
|
+
this.progress.skipped++;
|
|
1005
|
+
}
|
|
1006
|
+
uploaded(chunkId, fileName, parts, sizeBytes) {
|
|
1007
|
+
this.result.uploaded.push({ chunkId, fileName, parts, sizeBytes });
|
|
1008
|
+
this.progress.uploaded++;
|
|
1009
|
+
}
|
|
1010
|
+
backoff(attempt) {
|
|
1011
|
+
const base = Math.min(this.retryBaseMs * 2 ** attempt, this.maxRetryDelayMs);
|
|
1012
|
+
return Math.round(base * (0.75 + Math.random() * 0.5));
|
|
1013
|
+
}
|
|
1014
|
+
sleep(ms) {
|
|
1015
|
+
return sleep(ms, this.signal);
|
|
1016
|
+
}
|
|
1017
|
+
/** Waits for a 429 pause to end and for a slot under the request rate. */
|
|
1018
|
+
async gate() {
|
|
1019
|
+
for (; ; ) {
|
|
1020
|
+
if (this.signal?.aborted) throw abortedError();
|
|
1021
|
+
const now = Date.now();
|
|
1022
|
+
if (this.pausedUntil > now) {
|
|
1023
|
+
await sleep(this.pausedUntil - now, this.signal);
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
1026
|
+
const wait = this.pacer.reserve(now);
|
|
1027
|
+
if (wait === 0) return;
|
|
1028
|
+
await sleep(wait, this.signal);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* POSTs to the Starko host and returns the `{ ok: true }` body. Network
|
|
1033
|
+
* errors and 5xx are retried with backoff; a 429 pauses every request
|
|
1034
|
+
* (one stopping the upload when it asks for more than two minutes, or
|
|
1035
|
+
* past the deadline); 401 and 403 stop everything; a 3xx is never
|
|
1036
|
+
* followed; anything else non-2xx throws a non-fatal StarkoUploadError
|
|
1037
|
+
* carrying the status (callers decide what it means for their maps).
|
|
1038
|
+
*/
|
|
1039
|
+
async request(what, path3, init) {
|
|
1040
|
+
const url = `${this.host}${path3}`;
|
|
1041
|
+
const signal = this.signal;
|
|
1042
|
+
const timeoutMs = this.options.timeoutMs ?? routeTimeoutMs(path3);
|
|
1043
|
+
let rateLimited = 0;
|
|
1044
|
+
for (let attempt = 0; ; ) {
|
|
1045
|
+
await this.gate();
|
|
1046
|
+
const controller = new AbortController();
|
|
1047
|
+
let timedOut = false;
|
|
1048
|
+
const timer = setTimeout(() => {
|
|
1049
|
+
timedOut = true;
|
|
1050
|
+
controller.abort();
|
|
1051
|
+
}, timeoutMs);
|
|
1052
|
+
const onAbort = () => controller.abort();
|
|
1053
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1054
|
+
const done = () => {
|
|
1055
|
+
clearTimeout(timer);
|
|
1056
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1057
|
+
};
|
|
1058
|
+
let res;
|
|
1059
|
+
let text;
|
|
1060
|
+
try {
|
|
1061
|
+
res = await this.doFetch(url, {
|
|
1062
|
+
method: "POST",
|
|
1063
|
+
headers: {
|
|
1064
|
+
authorization: `Bearer ${this.apiKey}`,
|
|
1065
|
+
"content-type": init.contentType,
|
|
1066
|
+
"user-agent": USER_AGENT
|
|
1067
|
+
},
|
|
1068
|
+
// Node's fetch accepts a Buffer (a Uint8Array); the DOM typings are narrower.
|
|
1069
|
+
body: init.body,
|
|
1070
|
+
// Never followed: a redirect would POST the client's source to another address.
|
|
1071
|
+
redirect: "manual",
|
|
1072
|
+
signal: controller.signal
|
|
1073
|
+
});
|
|
1074
|
+
text = await res.text();
|
|
1075
|
+
} catch (error) {
|
|
1076
|
+
done();
|
|
1077
|
+
if (signal?.aborted) throw abortedError();
|
|
1078
|
+
const reason = timedOut ? `no answer within ${Math.round(timeoutMs / 1e3)} s` : this.redact(networkReason(error));
|
|
1079
|
+
this.consecutiveNetworkErrors++;
|
|
1080
|
+
if (this.consecutiveNetworkErrors >= this.maxConsecutiveNetworkErrors) {
|
|
1081
|
+
throw new StarkoUploadError(`cannot reach ${this.host}: ${this.consecutiveNetworkErrors} network errors in a row (last: ${what}: ${reason})`, {
|
|
1082
|
+
fatal: true
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
if (attempt >= this.retries) throw new StarkoUploadError(`${what}: network error after ${attempt + 1} attempts: ${reason}`);
|
|
1086
|
+
this.warn(`${what}: network error (${reason}), retrying`);
|
|
1087
|
+
await sleep(this.backoff(attempt), signal);
|
|
1088
|
+
attempt++;
|
|
1089
|
+
continue;
|
|
1090
|
+
}
|
|
1091
|
+
done();
|
|
1092
|
+
this.consecutiveNetworkErrors = 0;
|
|
1093
|
+
const date = Date.parse(res.headers.get("date") ?? "");
|
|
1094
|
+
if (!Number.isNaN(date)) this.clockOffsetMs = date - Date.now();
|
|
1095
|
+
if (res.status >= 300 && res.status < 400 || res.type === "opaqueredirect") {
|
|
1096
|
+
const location = res.headers.get("location");
|
|
1097
|
+
throw new StarkoUploadError(
|
|
1098
|
+
`${what}: ${this.host} answered with a redirect (${res.status || "3xx"}${location ? ` to ${this.redact(location)}` : ""}); source maps are never sent to another address. Set host to the address Starko answers on.`,
|
|
1099
|
+
{ status: res.status || 307, fatal: true }
|
|
1100
|
+
);
|
|
1101
|
+
}
|
|
1102
|
+
let body;
|
|
1103
|
+
try {
|
|
1104
|
+
body = text ? JSON.parse(text) : void 0;
|
|
1105
|
+
} catch {
|
|
1106
|
+
body = void 0;
|
|
1107
|
+
}
|
|
1108
|
+
const serverMessage = body?.message ? `: ${this.redact(String(body.message))}` : "";
|
|
1109
|
+
if (res.status === 429) {
|
|
1110
|
+
const asked = retryAfterMs(res.headers.get("retry-after"));
|
|
1111
|
+
const now = Date.now();
|
|
1112
|
+
if (asked !== void 0 && (asked > MAX_RETRY_AFTER_MS || this.deadlineAt !== void 0 && now + asked >= this.deadlineAt)) {
|
|
1113
|
+
throw new StarkoUploadError(
|
|
1114
|
+
`${what}: the server asks to wait ${formatDuration(asked)} before the next request (429${serverMessage}); the upload stops here`,
|
|
1115
|
+
{ status: 429, fatal: true }
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1118
|
+
rateLimited++;
|
|
1119
|
+
if (rateLimited > MAX_RATE_LIMITED_ATTEMPTS) {
|
|
1120
|
+
throw new StarkoUploadError(`${what}: server answered 429 ${rateLimited} times${serverMessage}`, { status: 429 });
|
|
1121
|
+
}
|
|
1122
|
+
const wait = Math.min(asked ?? Math.max(this.backoff(rateLimited - 1), 1e3), MAX_RETRY_AFTER_MS);
|
|
1123
|
+
if (now + wait > this.pausedUntil + 1e3) {
|
|
1124
|
+
this.warn(`the server is rate limiting this API key (429${serverMessage}); pausing the upload for ${formatDuration(wait)}`);
|
|
1125
|
+
}
|
|
1126
|
+
this.pausedUntil = Math.max(this.pausedUntil, now + wait);
|
|
1127
|
+
continue;
|
|
1128
|
+
}
|
|
1129
|
+
if (!res.ok && init.onError) {
|
|
1130
|
+
const special = init.onError({ status: res.status, headers: res.headers, body }, serverMessage);
|
|
1131
|
+
if (special) throw special;
|
|
1132
|
+
}
|
|
1133
|
+
if (res.status === 423) {
|
|
1134
|
+
throw new StarkoUploadError(`${what}: Starko has disabled source-map uploads (423${serverMessage}); nothing more is sent`, { status: 423, fatal: true });
|
|
1135
|
+
}
|
|
1136
|
+
if (res.status >= 500) {
|
|
1137
|
+
if (attempt >= this.retries) {
|
|
1138
|
+
throw new StarkoUploadError(`${what}: server answered ${res.status} after ${attempt + 1} attempts${serverMessage}`, { status: res.status });
|
|
1139
|
+
}
|
|
1140
|
+
const asked = retryAfterMs(res.headers.get("retry-after"));
|
|
1141
|
+
const wait = asked !== void 0 ? Math.min(Math.max(asked, this.backoff(attempt)), MAX_RETRY_AFTER_MS) : Math.min(this.backoff(attempt), this.maxRetryDelayMs);
|
|
1142
|
+
this.warn(`${what}: server answered ${res.status}${serverMessage}, retrying in ${formatDuration(wait)}`);
|
|
1143
|
+
await sleep(wait, signal);
|
|
1144
|
+
attempt++;
|
|
1145
|
+
continue;
|
|
1146
|
+
}
|
|
1147
|
+
if (res.status === 401) {
|
|
1148
|
+
throw new StarkoUploadError(
|
|
1149
|
+
`Starko rejected the API key (401)${serverMessage}. Set apiKey or STARKO_API_KEY to a secret key with the sourcemaps:write scope (Starko > Settings).`,
|
|
1150
|
+
{ status: 401, fatal: true }
|
|
1151
|
+
);
|
|
1152
|
+
}
|
|
1153
|
+
if (res.status === 403) {
|
|
1154
|
+
throw new StarkoUploadError(`Starko refused the upload (403)${serverMessage}. The API key needs the sourcemaps:write scope.`, {
|
|
1155
|
+
status: 403,
|
|
1156
|
+
fatal: true
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
if (res.status === 409) {
|
|
1160
|
+
const wait = retryAfterMs(res.headers.get("retry-after"));
|
|
1161
|
+
if (wait !== void 0) {
|
|
1162
|
+
throw new StarkoUploadError(
|
|
1163
|
+
`${what}: Starko is receiving this map through a direct upload to its storage that may still be under way; it accepts it through this route again in ${formatDuration(wait)} (409${serverMessage}). The next build sends it.`,
|
|
1164
|
+
{ status: 409 }
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1167
|
+
throw new StarkoUploadError(
|
|
1168
|
+
`${what}: Starko already has a different source map for this debug id (409)${serverMessage}. A debug id is write-once; delete the stored map in Starko to replace it.`,
|
|
1169
|
+
{ status: 409 }
|
|
1170
|
+
);
|
|
1171
|
+
}
|
|
1172
|
+
if (res.status === 413 && /allowance/i.test(serverMessage)) {
|
|
1173
|
+
throw new StarkoUploadError(`${what}: server answered 413${serverMessage}`, { status: 413, fatal: true });
|
|
1174
|
+
}
|
|
1175
|
+
if (!res.ok) {
|
|
1176
|
+
throw new StarkoUploadError(`${what}: server answered ${res.status}${serverMessage}`, { status: res.status });
|
|
1177
|
+
}
|
|
1178
|
+
if (!body || body.ok !== true) {
|
|
1179
|
+
throw new StarkoUploadError(`${what}: unexpected response${serverMessage || ` (${res.status})`}`, { status: res.status });
|
|
1180
|
+
}
|
|
1181
|
+
return body;
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
};
|
|
1185
|
+
|
|
1186
|
+
// src/build/shared/upload-v1.ts
|
|
1187
|
+
var import_node_util = require("util");
|
|
1188
|
+
var import_node_zlib = require("zlib");
|
|
1189
|
+
var gzip = (0, import_node_util.promisify)(import_node_zlib.gzip);
|
|
1190
|
+
var CHECKS_IN_FLIGHT = 4;
|
|
1191
|
+
function splitParts(data, size) {
|
|
1192
|
+
if (data.length === 0) return [data];
|
|
1193
|
+
const parts = [];
|
|
1194
|
+
for (let at = 0; at < data.length; at += size) parts.push(data.subarray(at, at + size));
|
|
1195
|
+
return parts;
|
|
1196
|
+
}
|
|
1197
|
+
async function runV1(s, source) {
|
|
1198
|
+
const options = s.options;
|
|
1199
|
+
const partBytes = Math.min(options.partBytes ?? SOURCEMAP_PART_BYTES, SOURCEMAP_PART_BYTES);
|
|
1200
|
+
if (!(partBytes > 0)) {
|
|
1201
|
+
s.stopWith(new StarkoUploadError("partBytes must be positive", { fatal: true }));
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
const concurrency = resolveUploadConcurrency(options.concurrency);
|
|
1205
|
+
const checkBatchSize = Math.max(1, Math.min(options.checkBatchSize ?? SOURCEMAP_MAX_CHUNKS_PER_CHECK, SOURCEMAP_MAX_CHUNKS_PER_CHECK));
|
|
1206
|
+
const queue = new Channel();
|
|
1207
|
+
const unsubscribe = s.onStop(() => queue.close(true));
|
|
1208
|
+
async function uploadOne(p) {
|
|
1209
|
+
const release = await s.budget.acquire(p.size * 2);
|
|
1210
|
+
if (s.stopped()) {
|
|
1211
|
+
release();
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
s.progress.sending++;
|
|
1215
|
+
try {
|
|
1216
|
+
const raw = await readItem(p.item);
|
|
1217
|
+
if (sha256Hex(raw) !== p.hash) throw new StarkoUploadError("the source map changed while it was being uploaded; not uploaded");
|
|
1218
|
+
const gz = await gzip(raw);
|
|
1219
|
+
const parts = splitParts(gz, partBytes);
|
|
1220
|
+
if (parts.length > SOURCEMAP_MAX_PARTS) {
|
|
1221
|
+
throw new StarkoUploadError(`source map is ${gz.length} bytes gzipped, more than ${SOURCEMAP_MAX_PARTS} parts of ${partBytes} bytes; not uploaded`);
|
|
1222
|
+
}
|
|
1223
|
+
for (let part = 0; part < parts.length; part++) {
|
|
1224
|
+
if (s.stopped()) return;
|
|
1225
|
+
const query = new URLSearchParams({
|
|
1226
|
+
chunk_id: p.chunkId,
|
|
1227
|
+
part: String(part),
|
|
1228
|
+
parts: String(parts.length),
|
|
1229
|
+
content_hash: p.hash
|
|
1230
|
+
});
|
|
1231
|
+
await s.request(
|
|
1232
|
+
`upload ${p.fileName ?? p.chunkId} part ${part + 1}/${parts.length}`,
|
|
1233
|
+
`${PATHS.SOURCEMAPS_UPLOAD}?${query.toString()}`,
|
|
1234
|
+
{ body: parts[part], contentType: "application/octet-stream" }
|
|
1235
|
+
);
|
|
1236
|
+
s.progress.bytesSent += parts[part].length;
|
|
1237
|
+
}
|
|
1238
|
+
const complete = {
|
|
1239
|
+
chunk_id: p.chunkId,
|
|
1240
|
+
parts: parts.length,
|
|
1241
|
+
content_hash: p.hash,
|
|
1242
|
+
size_bytes: p.size
|
|
1243
|
+
};
|
|
1244
|
+
if (p.fileName) complete.file_name = wireFileName(p.fileName);
|
|
1245
|
+
if (s.release) complete.release = { version: s.release.version };
|
|
1246
|
+
await s.request(`complete ${p.fileName ?? p.chunkId}`, PATHS.SOURCEMAPS_COMPLETE, {
|
|
1247
|
+
body: JSON.stringify(complete),
|
|
1248
|
+
contentType: "application/json"
|
|
1249
|
+
});
|
|
1250
|
+
s.uploaded(p.chunkId, p.fileName, parts.length, p.size);
|
|
1251
|
+
} catch (error) {
|
|
1252
|
+
if (error instanceof StarkoUploadError && error.fatal) {
|
|
1253
|
+
s.stopWith(error);
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
s.fail(p.chunkId, p.fileName, errorMessage(error));
|
|
1257
|
+
} finally {
|
|
1258
|
+
s.progress.sending--;
|
|
1259
|
+
release();
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
const workers = Array.from({ length: concurrency }, async () => {
|
|
1263
|
+
for (; ; ) {
|
|
1264
|
+
const p = await queue.next();
|
|
1265
|
+
if (p === void 0 || s.stopped()) return;
|
|
1266
|
+
await uploadOne(p);
|
|
1267
|
+
}
|
|
1268
|
+
});
|
|
1269
|
+
const checkSlots = new Budget(CHECKS_IN_FLIGHT);
|
|
1270
|
+
const checks = [];
|
|
1271
|
+
const dispatchCheck = (batch) => {
|
|
1272
|
+
checks.push(
|
|
1273
|
+
(async () => {
|
|
1274
|
+
const release = await checkSlots.acquire(1);
|
|
1275
|
+
try {
|
|
1276
|
+
if (s.stopped()) return;
|
|
1277
|
+
const payload = {
|
|
1278
|
+
chunks: batch.map((p) => ({ chunk_id: p.chunkId, content_hash: p.hash }))
|
|
1279
|
+
};
|
|
1280
|
+
if (s.release) payload.release = s.release;
|
|
1281
|
+
const res = await s.request("check", PATHS.SOURCEMAPS_CHECK, {
|
|
1282
|
+
body: JSON.stringify(payload),
|
|
1283
|
+
contentType: "application/json"
|
|
1284
|
+
});
|
|
1285
|
+
if (!res.data || !Array.isArray(res.data.missing)) throw new StarkoUploadError("check: response has no `missing` list");
|
|
1286
|
+
const missing = new Set(res.data.missing.map(String));
|
|
1287
|
+
s.progress.checked += batch.length;
|
|
1288
|
+
for (const p of batch) {
|
|
1289
|
+
if (missing.has(p.chunkId)) queue.push(p);
|
|
1290
|
+
else s.skip(p.chunkId, p.fileName);
|
|
1291
|
+
}
|
|
1292
|
+
} catch (error) {
|
|
1293
|
+
s.stopWith(error);
|
|
1294
|
+
} finally {
|
|
1295
|
+
release();
|
|
1296
|
+
}
|
|
1297
|
+
})()
|
|
1298
|
+
);
|
|
1299
|
+
};
|
|
1300
|
+
try {
|
|
1301
|
+
let batch = [];
|
|
1302
|
+
for await (const p of source) {
|
|
1303
|
+
if (s.stopped()) break;
|
|
1304
|
+
batch.push(p);
|
|
1305
|
+
if (batch.length >= checkBatchSize) {
|
|
1306
|
+
dispatchCheck(batch);
|
|
1307
|
+
batch = [];
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
if (batch.length > 0 && !s.stopped()) dispatchCheck(batch);
|
|
1311
|
+
await Promise.all(checks);
|
|
1312
|
+
} catch (error) {
|
|
1313
|
+
s.stopWith(error);
|
|
1314
|
+
} finally {
|
|
1315
|
+
queue.close();
|
|
1316
|
+
}
|
|
1317
|
+
await Promise.all(workers);
|
|
1318
|
+
unsubscribe();
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
// src/build/shared/upload-bulk.ts
|
|
1322
|
+
var gzip2 = (0, import_node_util2.promisify)(import_node_zlib2.gzip);
|
|
1323
|
+
var GZIP_OPTIONS = { level: 6, memLevel: 8, windowBits: 15, strategy: import_node_zlib2.constants.Z_DEFAULT_STRATEGY };
|
|
1324
|
+
function gzipMap(raw) {
|
|
1325
|
+
return gzip2(raw, GZIP_OPTIONS);
|
|
1326
|
+
}
|
|
1327
|
+
var CHECKS_IN_FLIGHT2 = 4;
|
|
1328
|
+
var COMPRESSORS = 4;
|
|
1329
|
+
var PROBE_MAPS = 4;
|
|
1330
|
+
var EXPIRY_MARGIN_MS = 6e4;
|
|
1331
|
+
var MAX_STARTS_PER_MAP = 4;
|
|
1332
|
+
var MAX_BUSY_WAITS = 3;
|
|
1333
|
+
var MAX_BUSY_WAIT_MS = 6e4;
|
|
1334
|
+
var PUT_RETRIES = 2;
|
|
1335
|
+
var MAX_PUT_RETRY_AFTER_MS = 3e4;
|
|
1336
|
+
var MAX_FINISH_ROUNDS = 5;
|
|
1337
|
+
var FINISH_RETRY_MS = 1e3;
|
|
1338
|
+
var PUT_MS_PER_128_KIB = 1e3;
|
|
1339
|
+
var monotonic = () => performance.now();
|
|
1340
|
+
var Notifier = class {
|
|
1341
|
+
constructor() {
|
|
1342
|
+
this.waiters = [];
|
|
1343
|
+
}
|
|
1344
|
+
notify() {
|
|
1345
|
+
for (const w of this.waiters.splice(0)) w();
|
|
1346
|
+
}
|
|
1347
|
+
wait() {
|
|
1348
|
+
return new Promise((resolve) => this.waiters.push(resolve));
|
|
1349
|
+
}
|
|
1350
|
+
};
|
|
1351
|
+
var HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
1352
|
+
var SAFE_VALUE = /^[^\r\n"\\]*$/;
|
|
1353
|
+
function multipartBody(target, content) {
|
|
1354
|
+
let boundary;
|
|
1355
|
+
do
|
|
1356
|
+
boundary = `----starko-${(0, import_node_crypto3.randomBytes)(16).toString("hex")}`;
|
|
1357
|
+
while (content.includes(boundary));
|
|
1358
|
+
const head = Buffer.from(
|
|
1359
|
+
`--${boundary}\r
|
|
1360
|
+
Content-Disposition: form-data; name="${target.form_field}"; filename="${target.file_name}"\r
|
|
1361
|
+
Content-Type: ${target.content_type}\r
|
|
1362
|
+
\r
|
|
1363
|
+
`,
|
|
1364
|
+
"utf8"
|
|
1365
|
+
);
|
|
1366
|
+
const tail = Buffer.from(`\r
|
|
1367
|
+
--${boundary}--\r
|
|
1368
|
+
`, "utf8");
|
|
1369
|
+
return { body: Buffer.concat([head, content, tail]), contentType: `multipart/form-data; boundary=${boundary}` };
|
|
1370
|
+
}
|
|
1371
|
+
async function runBulk(s, source, mode) {
|
|
1372
|
+
const o = s.options;
|
|
1373
|
+
const checkBatch = Math.max(1, Math.min(o.bulkCheckBatchSize ?? SOURCEMAP_BULK_MAX_CHECK, SOURCEMAP_BULK_MAX_CHECK));
|
|
1374
|
+
const mapBatch = Math.max(1, Math.min(o.bulkBatchSize ?? SOURCEMAP_BULK_MAX_MAPS, SOURCEMAP_BULK_MAX_MAPS));
|
|
1375
|
+
const startLingerMs = Math.max(0, o.bulkLingerMs ?? 250);
|
|
1376
|
+
const finishLingerMs = Math.max(0, o.bulkLingerMs ?? 2e3);
|
|
1377
|
+
const concurrency = resolveUploadConcurrency(o.concurrency, DEFAULT_BULK_UPLOAD_CONCURRENCY);
|
|
1378
|
+
const maxStorageFailures = Math.max(1, o.maxStorageFailures ?? 3);
|
|
1379
|
+
const configured = mode === "bulk" ? " (as configured)" : "";
|
|
1380
|
+
let first;
|
|
1381
|
+
try {
|
|
1382
|
+
first = await source.next();
|
|
1383
|
+
} catch (error) {
|
|
1384
|
+
s.stopWith(error);
|
|
1385
|
+
return;
|
|
1386
|
+
}
|
|
1387
|
+
if (first.done) return;
|
|
1388
|
+
let firstMissing;
|
|
1389
|
+
try {
|
|
1390
|
+
firstMissing = await bulkCheck(s, [first.value]);
|
|
1391
|
+
} catch (error) {
|
|
1392
|
+
const status = error instanceof StarkoUploadError ? error.status : void 0;
|
|
1393
|
+
if (status === 404 || status === 405) {
|
|
1394
|
+
if (mode === "bulk") {
|
|
1395
|
+
s.stopWith(
|
|
1396
|
+
new StarkoUploadError(
|
|
1397
|
+
`${s.host} has no bulk upload (bulk/check answered ${status}). Set uploadProtocol to "auto" or "v1" (--protocol), or update the Starko server.`,
|
|
1398
|
+
{ status, fatal: true }
|
|
1399
|
+
)
|
|
1400
|
+
);
|
|
1401
|
+
return;
|
|
1402
|
+
}
|
|
1403
|
+
s.useProtocol("v1");
|
|
1404
|
+
s.info(`source map upload: using the v1 upload through ${s.host}: the server has no bulk upload (bulk/check answered ${status})`);
|
|
1405
|
+
await runV1(s, prepend(first.value, source));
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1408
|
+
s.stopWith(error);
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
s.useProtocol("bulk");
|
|
1412
|
+
s.info(`source map upload: using the bulk upload${configured}: each map goes straight to Starko's storage`);
|
|
1413
|
+
const compressQueue = new Channel();
|
|
1414
|
+
const startQueue = new Channel();
|
|
1415
|
+
const putQueue = new Channel();
|
|
1416
|
+
const finishQueue = new Channel();
|
|
1417
|
+
const v1Input = new Channel();
|
|
1418
|
+
const changed = new Notifier();
|
|
1419
|
+
const delayed = /* @__PURE__ */ new Set();
|
|
1420
|
+
let pendingMaps = 0;
|
|
1421
|
+
let checksDone = false;
|
|
1422
|
+
let startsInFlight = 0;
|
|
1423
|
+
let awaitingPut = 0;
|
|
1424
|
+
let storageProven = false;
|
|
1425
|
+
let storageFailuresInRow = 0;
|
|
1426
|
+
let storageOrigin;
|
|
1427
|
+
let fallenBack = false;
|
|
1428
|
+
let v1Done;
|
|
1429
|
+
let closing;
|
|
1430
|
+
const closeAll = (drop) => {
|
|
1431
|
+
for (const q of [compressQueue, startQueue, putQueue, finishQueue]) q.close(drop);
|
|
1432
|
+
v1Input.close(drop);
|
|
1433
|
+
changed.notify();
|
|
1434
|
+
};
|
|
1435
|
+
const unsubscribe = s.onStop(() => closeAll(true));
|
|
1436
|
+
const maybeIdle = () => {
|
|
1437
|
+
if (checksDone && pendingMaps === 0) closeAll(false);
|
|
1438
|
+
};
|
|
1439
|
+
const newMap = (p) => {
|
|
1440
|
+
pendingMaps++;
|
|
1441
|
+
return { p, expiresAt: 0, reserved: false, starts: 0, busyWaits: 0, puts: 0, putRestarts: 0, putOk: false, putExpired: false, finishRounds: 0, settled: false };
|
|
1442
|
+
};
|
|
1443
|
+
const settle = (m) => {
|
|
1444
|
+
if (m.settled) return;
|
|
1445
|
+
m.settled = true;
|
|
1446
|
+
m.held?.release();
|
|
1447
|
+
m.held = void 0;
|
|
1448
|
+
m.gz = void 0;
|
|
1449
|
+
pendingMaps--;
|
|
1450
|
+
changed.notify();
|
|
1451
|
+
maybeIdle();
|
|
1452
|
+
};
|
|
1453
|
+
const failMap = (m, error) => {
|
|
1454
|
+
if (m.settled) return;
|
|
1455
|
+
s.fail(m.p.chunkId, m.p.fileName, error);
|
|
1456
|
+
settle(m);
|
|
1457
|
+
};
|
|
1458
|
+
const notSent = (m) => settle(m);
|
|
1459
|
+
const handOver = (m) => {
|
|
1460
|
+
if (m.settled) return;
|
|
1461
|
+
v1Input.push(m.p);
|
|
1462
|
+
s.progress.checked--;
|
|
1463
|
+
settle(m);
|
|
1464
|
+
};
|
|
1465
|
+
const later = (ms, fn) => {
|
|
1466
|
+
const task = s.sleep(ms).then(fn, () => void 0);
|
|
1467
|
+
delayed.add(task);
|
|
1468
|
+
void task.finally(() => delayed.delete(task));
|
|
1469
|
+
};
|
|
1470
|
+
const routeChecked = (batch, missing) => {
|
|
1471
|
+
s.progress.checked += batch.length;
|
|
1472
|
+
for (const p of batch) {
|
|
1473
|
+
if (!missing.has(p.chunkId)) {
|
|
1474
|
+
s.skip(p.chunkId, p.fileName);
|
|
1475
|
+
continue;
|
|
1476
|
+
}
|
|
1477
|
+
const m = newMap(p);
|
|
1478
|
+
if (fallenBack) handOver(m);
|
|
1479
|
+
else if (closing) notSent(m);
|
|
1480
|
+
else compressQueue.push(m);
|
|
1481
|
+
}
|
|
1482
|
+
};
|
|
1483
|
+
const restart = (m, why) => {
|
|
1484
|
+
m.target = void 0;
|
|
1485
|
+
m.putOk = false;
|
|
1486
|
+
m.putExpired = false;
|
|
1487
|
+
if (fallenBack) {
|
|
1488
|
+
handOver(m);
|
|
1489
|
+
return;
|
|
1490
|
+
}
|
|
1491
|
+
if (m.starts >= MAX_STARTS_PER_MAP) {
|
|
1492
|
+
failMap(m, `not uploaded after ${m.starts} bulk/start requests (last: ${why}); the next build sends it`);
|
|
1493
|
+
return;
|
|
1494
|
+
}
|
|
1495
|
+
startQueue.push(m);
|
|
1496
|
+
};
|
|
1497
|
+
const storageFailure = (m, reason) => {
|
|
1498
|
+
m.putOk = false;
|
|
1499
|
+
m.putError = reason;
|
|
1500
|
+
storageFailuresInRow++;
|
|
1501
|
+
if (storageFailuresInRow >= maxStorageFailures) fallBack(reason);
|
|
1502
|
+
if (!s.stopped()) finishQueue.push(m);
|
|
1503
|
+
};
|
|
1504
|
+
const fallBack = (reason) => {
|
|
1505
|
+
if (fallenBack || closing || s.stopped()) return;
|
|
1506
|
+
const where = storageOrigin ?? "the storage host";
|
|
1507
|
+
const hostOnly = storageOrigin ? new URL(storageOrigin).host : "the storage host";
|
|
1508
|
+
if (mode === "bulk") {
|
|
1509
|
+
s.stopWith(
|
|
1510
|
+
new StarkoUploadError(
|
|
1511
|
+
`uploads to Starko's storage at ${where} fail (${storageFailuresInRow} in a row, last: ${reason}). The build machine must be able to reach ${hostOnly} (a proxy or a firewall may block it): allow it, or set uploadProtocol to "v1" (--protocol v1) to send every map through ${s.host}.`,
|
|
1512
|
+
{ fatal: true }
|
|
1513
|
+
)
|
|
1514
|
+
);
|
|
1515
|
+
return;
|
|
1516
|
+
}
|
|
1517
|
+
fallenBack = true;
|
|
1518
|
+
s.result.fallback = { storageHost: hostOnly, reason };
|
|
1519
|
+
s.warn(
|
|
1520
|
+
`source map upload: uploads to Starko's storage at ${where} fail (${storageFailuresInRow} in a row, last: ${reason}); this machine probably cannot reach that host (a proxy or a firewall?). The remaining source maps go through ${s.host} instead (v1 upload), the ones already reserved for storage included. Allow ${hostOnly} for builds, or set uploadProtocol to "v1" (--protocol v1).`
|
|
1521
|
+
);
|
|
1522
|
+
v1Done = runV1(s, v1Input);
|
|
1523
|
+
for (const m of compressQueue.drainAll()) handOver(m);
|
|
1524
|
+
for (const m of startQueue.drainAll()) handOver(m);
|
|
1525
|
+
for (const m of putQueue.drainAll()) {
|
|
1526
|
+
awaitingPut--;
|
|
1527
|
+
handOver(m);
|
|
1528
|
+
}
|
|
1529
|
+
changed.notify();
|
|
1530
|
+
};
|
|
1531
|
+
const drain = (error) => {
|
|
1532
|
+
if (closing || s.stopped()) return;
|
|
1533
|
+
closing = error;
|
|
1534
|
+
for (const m of compressQueue.drainAll()) notSent(m);
|
|
1535
|
+
for (const m of startQueue.drainAll()) {
|
|
1536
|
+
if (m.reserved) startQueue.push(m);
|
|
1537
|
+
else notSent(m);
|
|
1538
|
+
}
|
|
1539
|
+
changed.notify();
|
|
1540
|
+
};
|
|
1541
|
+
const isCleanStop = (error) => error instanceof StarkoUploadError && error.fatal && (error.status === 429 || error.status === 413);
|
|
1542
|
+
const checkSlots = new Budget(CHECKS_IN_FLIGHT2);
|
|
1543
|
+
const checks = [];
|
|
1544
|
+
const dispatchCheck = (batch) => {
|
|
1545
|
+
checks.push(
|
|
1546
|
+
(async () => {
|
|
1547
|
+
const release = await checkSlots.acquire(1);
|
|
1548
|
+
try {
|
|
1549
|
+
if (s.stopped()) return;
|
|
1550
|
+
if (fallenBack) {
|
|
1551
|
+
for (const p of batch) v1Input.push(p);
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1554
|
+
routeChecked(batch, await bulkCheck(s, batch));
|
|
1555
|
+
} catch (error) {
|
|
1556
|
+
s.stopWith(error);
|
|
1557
|
+
} finally {
|
|
1558
|
+
release();
|
|
1559
|
+
}
|
|
1560
|
+
})()
|
|
1561
|
+
);
|
|
1562
|
+
};
|
|
1563
|
+
const checkLoop = (async () => {
|
|
1564
|
+
routeChecked([first.value], firstMissing);
|
|
1565
|
+
let batch = [];
|
|
1566
|
+
try {
|
|
1567
|
+
for (; ; ) {
|
|
1568
|
+
if (s.stopped() || closing) break;
|
|
1569
|
+
if (fallenBack) {
|
|
1570
|
+
for (const p of batch) v1Input.push(p);
|
|
1571
|
+
batch = [];
|
|
1572
|
+
for (; ; ) {
|
|
1573
|
+
if (s.stopped()) break;
|
|
1574
|
+
const next2 = await source.next();
|
|
1575
|
+
if (next2.done) break;
|
|
1576
|
+
v1Input.push(next2.value);
|
|
1577
|
+
}
|
|
1578
|
+
break;
|
|
1579
|
+
}
|
|
1580
|
+
const next = await source.next();
|
|
1581
|
+
if (next.done) break;
|
|
1582
|
+
batch.push(next.value);
|
|
1583
|
+
if (batch.length >= checkBatch) {
|
|
1584
|
+
dispatchCheck(batch);
|
|
1585
|
+
batch = [];
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
if (batch.length > 0 && !s.stopped() && !closing) {
|
|
1589
|
+
if (fallenBack) for (const p of batch) v1Input.push(p);
|
|
1590
|
+
else dispatchCheck(batch);
|
|
1591
|
+
}
|
|
1592
|
+
await Promise.all(checks);
|
|
1593
|
+
} catch (error) {
|
|
1594
|
+
s.stopWith(error);
|
|
1595
|
+
} finally {
|
|
1596
|
+
checksDone = true;
|
|
1597
|
+
maybeIdle();
|
|
1598
|
+
}
|
|
1599
|
+
})();
|
|
1600
|
+
const compressors = Array.from({ length: COMPRESSORS }, async () => {
|
|
1601
|
+
for (; ; ) {
|
|
1602
|
+
const m = await compressQueue.next();
|
|
1603
|
+
if (m === void 0 || s.stopped()) return;
|
|
1604
|
+
const held = await s.budget.hold(m.p.size * 2);
|
|
1605
|
+
if (s.stopped()) {
|
|
1606
|
+
held.release();
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1609
|
+
if (fallenBack || closing) {
|
|
1610
|
+
held.release();
|
|
1611
|
+
if (fallenBack) handOver(m);
|
|
1612
|
+
else notSent(m);
|
|
1613
|
+
continue;
|
|
1614
|
+
}
|
|
1615
|
+
try {
|
|
1616
|
+
const raw = await readItem(m.p.item);
|
|
1617
|
+
if (sha256Hex(raw) !== m.p.hash) throw new StarkoUploadError("the source map changed while it was being uploaded; not uploaded");
|
|
1618
|
+
const gz = await gzipMap(raw);
|
|
1619
|
+
if (gz.length > SOURCEMAP_BULK_MAX_OBJECT_BYTES) throw new StarkoUploadError(`source map is ${gz.length} bytes gzipped, over the ${SOURCEMAP_BULK_MAX_OBJECT_BYTES}-byte limit; not uploaded`);
|
|
1620
|
+
m.gz = gz;
|
|
1621
|
+
held.resize(gz.length * 2);
|
|
1622
|
+
m.held = held;
|
|
1623
|
+
} catch (error) {
|
|
1624
|
+
held.release();
|
|
1625
|
+
failMap(m, errorMessage(error));
|
|
1626
|
+
continue;
|
|
1627
|
+
}
|
|
1628
|
+
if (fallenBack) handOver(m);
|
|
1629
|
+
else if (closing) notSent(m);
|
|
1630
|
+
else startQueue.push(m);
|
|
1631
|
+
}
|
|
1632
|
+
});
|
|
1633
|
+
const dispatcher = (queue, slots, lingerMs, size, ready, send) => (async () => {
|
|
1634
|
+
const inFlight = /* @__PURE__ */ new Set();
|
|
1635
|
+
const free = new Budget(slots);
|
|
1636
|
+
for (; ; ) {
|
|
1637
|
+
const first2 = await queue.next();
|
|
1638
|
+
if (first2 === void 0) break;
|
|
1639
|
+
while (!ready() && !s.stopped() && !queue.closed) await changed.wait();
|
|
1640
|
+
const release = await free.acquire(1);
|
|
1641
|
+
if (s.stopped()) {
|
|
1642
|
+
release();
|
|
1643
|
+
break;
|
|
1644
|
+
}
|
|
1645
|
+
const n = size();
|
|
1646
|
+
await queue.waitForLength(n - 1, lingerMs);
|
|
1647
|
+
if (s.stopped()) {
|
|
1648
|
+
release();
|
|
1649
|
+
break;
|
|
1650
|
+
}
|
|
1651
|
+
const batch = [first2, ...queue.take(n - 1)];
|
|
1652
|
+
const task = send(batch).finally(() => {
|
|
1653
|
+
release();
|
|
1654
|
+
inFlight.delete(task);
|
|
1655
|
+
});
|
|
1656
|
+
inFlight.add(task);
|
|
1657
|
+
}
|
|
1658
|
+
await Promise.all(inFlight);
|
|
1659
|
+
})();
|
|
1660
|
+
const handleStart = (m, entry) => {
|
|
1661
|
+
const message = typeof entry.message === "string" && entry.message ? `: ${entry.message}` : "";
|
|
1662
|
+
switch (entry.status) {
|
|
1663
|
+
case "upload": {
|
|
1664
|
+
const problem = targetProblem(entry.upload, m, s.host);
|
|
1665
|
+
if (problem) {
|
|
1666
|
+
failMap(m, `bulk/start gave an unusable upload target (${problem}); not uploaded`);
|
|
1667
|
+
return;
|
|
1668
|
+
}
|
|
1669
|
+
const target = entry.upload;
|
|
1670
|
+
s.secrets.addSignedUrl(target.url);
|
|
1671
|
+
storageOrigin = new URL(target.url).origin;
|
|
1672
|
+
m.target = target;
|
|
1673
|
+
m.expiresAt = expiryOf(target, monotonic(), s.clockOffsetMs);
|
|
1674
|
+
m.reserved = true;
|
|
1675
|
+
m.putExpired = false;
|
|
1676
|
+
if (fallenBack) {
|
|
1677
|
+
handOver(m);
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
awaitingPut++;
|
|
1681
|
+
putQueue.push(m);
|
|
1682
|
+
return;
|
|
1683
|
+
}
|
|
1684
|
+
case "uploaded":
|
|
1685
|
+
m.reserved = true;
|
|
1686
|
+
m.target = void 0;
|
|
1687
|
+
m.putOk = true;
|
|
1688
|
+
finishQueue.push(m);
|
|
1689
|
+
return;
|
|
1690
|
+
case "present":
|
|
1691
|
+
case "pending":
|
|
1692
|
+
s.skip(m.p.chunkId, m.p.fileName);
|
|
1693
|
+
settle(m);
|
|
1694
|
+
return;
|
|
1695
|
+
case "conflict":
|
|
1696
|
+
failMap(m, `Starko already has a different source map for this debug id (conflict${message}). A debug id is write-once; delete the stored map in Starko to replace it.`);
|
|
1697
|
+
return;
|
|
1698
|
+
case "busy": {
|
|
1699
|
+
const wait = Math.max(1, Number(entry.retry_after_s) || 1) * 1e3;
|
|
1700
|
+
const fits = s.deadlineAt === void 0 || Date.now() + wait < s.deadlineAt;
|
|
1701
|
+
if (m.busyWaits < MAX_BUSY_WAITS && wait <= MAX_BUSY_WAIT_MS && fits && !closing) {
|
|
1702
|
+
m.busyWaits++;
|
|
1703
|
+
m.starts--;
|
|
1704
|
+
later(wait, () => fallenBack ? restart(m, "busy") : startQueue.push(m));
|
|
1705
|
+
return;
|
|
1706
|
+
}
|
|
1707
|
+
failMap(m, `earlier uploads of this debug id may still be under way on the server; it accepts this map again in ${formatDuration(wait)} (busy${message}). The next build sends it.`);
|
|
1708
|
+
return;
|
|
1709
|
+
}
|
|
1710
|
+
case "over_allowance":
|
|
1711
|
+
drain(new StarkoUploadError(`bulk/start: ${entry.message || "the workspace's source-map storage allowance is reached"} (over_allowance); the upload stops here`, { status: 413, fatal: true }));
|
|
1712
|
+
notSent(m);
|
|
1713
|
+
return;
|
|
1714
|
+
default:
|
|
1715
|
+
failMap(m, `bulk/start: unexpected answer for this map (${String(entry.status)}); not uploaded`);
|
|
1716
|
+
}
|
|
1717
|
+
};
|
|
1718
|
+
const sendStart = async (batch) => {
|
|
1719
|
+
const live = batch.filter((m) => {
|
|
1720
|
+
if (m.settled) return false;
|
|
1721
|
+
if (fallenBack) {
|
|
1722
|
+
handOver(m);
|
|
1723
|
+
return false;
|
|
1724
|
+
}
|
|
1725
|
+
if (closing && !m.reserved) {
|
|
1726
|
+
notSent(m);
|
|
1727
|
+
return false;
|
|
1728
|
+
}
|
|
1729
|
+
return true;
|
|
1730
|
+
});
|
|
1731
|
+
if (live.length === 0) return;
|
|
1732
|
+
startsInFlight++;
|
|
1733
|
+
changed.notify();
|
|
1734
|
+
try {
|
|
1735
|
+
for (const m of live) m.starts++;
|
|
1736
|
+
const payload = {
|
|
1737
|
+
maps: live.map((m) => {
|
|
1738
|
+
const e = { chunk_id: m.p.chunkId, content_hash: m.p.hash, size_bytes: m.gz.length };
|
|
1739
|
+
if (m.p.fileName) e.file_name = wireFileName(m.p.fileName);
|
|
1740
|
+
return e;
|
|
1741
|
+
})
|
|
1742
|
+
};
|
|
1743
|
+
if (s.release) payload.release = s.release;
|
|
1744
|
+
const res = await s.request(`bulk/start (${live.length} map(s))`, PATHS.SOURCEMAPS_BULK_START, {
|
|
1745
|
+
body: JSON.stringify(payload),
|
|
1746
|
+
contentType: "application/json"
|
|
1747
|
+
});
|
|
1748
|
+
const entries = answerFor(res.data?.maps, live, "bulk/start");
|
|
1749
|
+
live.forEach((m, i) => handleStart(m, entries[i]));
|
|
1750
|
+
} catch (error) {
|
|
1751
|
+
if (isCleanStop(error)) {
|
|
1752
|
+
drain(error);
|
|
1753
|
+
for (const m of live) if (!m.reserved) notSent(m);
|
|
1754
|
+
for (const m of live) if (!m.settled) restart(m, errorMessage(error));
|
|
1755
|
+
} else if (error instanceof StarkoUploadError && error.fatal) {
|
|
1756
|
+
s.stopWith(error);
|
|
1757
|
+
} else {
|
|
1758
|
+
for (const m of live) failMap(m, errorMessage(error));
|
|
1759
|
+
}
|
|
1760
|
+
} finally {
|
|
1761
|
+
startsInFlight--;
|
|
1762
|
+
changed.notify();
|
|
1763
|
+
}
|
|
1764
|
+
};
|
|
1765
|
+
const startDispatcher = dispatcher(
|
|
1766
|
+
startQueue,
|
|
1767
|
+
SOURCEMAP_BULK_REQUEST_CONCURRENCY,
|
|
1768
|
+
startLingerMs,
|
|
1769
|
+
() => storageProven ? mapBatch : Math.min(mapBatch, PROBE_MAPS),
|
|
1770
|
+
// Until storage is known to be reachable: one small start at a time, and only when its maps were tried.
|
|
1771
|
+
// Then: only while the PUTs need more work, so starts carry many maps each instead of a few.
|
|
1772
|
+
() => fallenBack || (storageProven ? awaitingPut < concurrency * 2 : startsInFlight === 0 && awaitingPut === 0),
|
|
1773
|
+
sendStart
|
|
1774
|
+
);
|
|
1775
|
+
const putOne = async (m) => {
|
|
1776
|
+
if (m.settled) return;
|
|
1777
|
+
if (fallenBack) {
|
|
1778
|
+
awaitingPut--;
|
|
1779
|
+
handOver(m);
|
|
1780
|
+
return;
|
|
1781
|
+
}
|
|
1782
|
+
if (!m.target || m.expiresAt - monotonic() < EXPIRY_MARGIN_MS) {
|
|
1783
|
+
awaitingPut--;
|
|
1784
|
+
changed.notify();
|
|
1785
|
+
restart(m, "the upload URL expired");
|
|
1786
|
+
return;
|
|
1787
|
+
}
|
|
1788
|
+
m.puts++;
|
|
1789
|
+
s.progress.sending++;
|
|
1790
|
+
let outcome;
|
|
1791
|
+
try {
|
|
1792
|
+
outcome = await putToStorage(s, m.target, m.gz);
|
|
1793
|
+
} catch (error) {
|
|
1794
|
+
s.progress.sending--;
|
|
1795
|
+
awaitingPut--;
|
|
1796
|
+
changed.notify();
|
|
1797
|
+
if (s.signal?.aborted) s.stopWith(error);
|
|
1798
|
+
else storageFailure(m, errorMessage(error));
|
|
1799
|
+
return;
|
|
1800
|
+
}
|
|
1801
|
+
s.progress.sending--;
|
|
1802
|
+
awaitingPut--;
|
|
1803
|
+
changed.notify();
|
|
1804
|
+
if (outcome.kind === "ok") {
|
|
1805
|
+
m.putOk = true;
|
|
1806
|
+
storageFailuresInRow = 0;
|
|
1807
|
+
if (!storageProven) {
|
|
1808
|
+
storageProven = true;
|
|
1809
|
+
changed.notify();
|
|
1810
|
+
}
|
|
1811
|
+
s.progress.bytesSent += m.gz.length;
|
|
1812
|
+
finishQueue.push(m);
|
|
1813
|
+
return;
|
|
1814
|
+
}
|
|
1815
|
+
if (outcome.kind === "refused" && m.expiresAt - monotonic() < EXPIRY_MARGIN_MS) {
|
|
1816
|
+
m.putOk = false;
|
|
1817
|
+
m.putExpired = true;
|
|
1818
|
+
m.putError = outcome.reason;
|
|
1819
|
+
finishQueue.push(m);
|
|
1820
|
+
return;
|
|
1821
|
+
}
|
|
1822
|
+
storageFailure(m, outcome.reason);
|
|
1823
|
+
};
|
|
1824
|
+
const putWorkers = Array.from({ length: concurrency }, async () => {
|
|
1825
|
+
for (; ; ) {
|
|
1826
|
+
const m = await putQueue.next();
|
|
1827
|
+
if (m === void 0 || s.stopped()) return;
|
|
1828
|
+
await putOne(m);
|
|
1829
|
+
}
|
|
1830
|
+
});
|
|
1831
|
+
const handleFinish = (m, entry, progressed) => {
|
|
1832
|
+
const message = typeof entry.message === "string" && entry.message ? `: ${entry.message}` : "";
|
|
1833
|
+
switch (entry.status) {
|
|
1834
|
+
case "stored":
|
|
1835
|
+
case "pending":
|
|
1836
|
+
s.uploaded(m.p.chunkId, m.p.fileName, 1, m.p.size);
|
|
1837
|
+
settle(m);
|
|
1838
|
+
return;
|
|
1839
|
+
case "not_uploaded":
|
|
1840
|
+
if (fallenBack) {
|
|
1841
|
+
handOver(m);
|
|
1842
|
+
return;
|
|
1843
|
+
}
|
|
1844
|
+
if (m.putExpired) {
|
|
1845
|
+
restart(m, m.putError ?? `not_uploaded${message}`);
|
|
1846
|
+
return;
|
|
1847
|
+
}
|
|
1848
|
+
if (m.putOk) {
|
|
1849
|
+
restart(m, `not_uploaded${message}`);
|
|
1850
|
+
return;
|
|
1851
|
+
}
|
|
1852
|
+
if (m.putRestarts < 1 && !closing) {
|
|
1853
|
+
m.putRestarts++;
|
|
1854
|
+
restart(m, m.putError ?? `not_uploaded${message}`);
|
|
1855
|
+
return;
|
|
1856
|
+
}
|
|
1857
|
+
failMap(m, `could not upload to Starko's storage: ${m.putError ?? "not uploaded"}`);
|
|
1858
|
+
return;
|
|
1859
|
+
case "not_reserved":
|
|
1860
|
+
m.reserved = false;
|
|
1861
|
+
restart(m, `not_reserved${message}`);
|
|
1862
|
+
return;
|
|
1863
|
+
case "retry":
|
|
1864
|
+
if (!progressed) m.finishRounds++;
|
|
1865
|
+
if (m.finishRounds < MAX_FINISH_ROUNDS) {
|
|
1866
|
+
later(FINISH_RETRY_MS * Math.max(1, m.finishRounds), () => finishQueue.push(m));
|
|
1867
|
+
return;
|
|
1868
|
+
}
|
|
1869
|
+
failMap(m, `the server could not check the upload in ${m.finishRounds} tries (retry${message}); the next build sends it`);
|
|
1870
|
+
return;
|
|
1871
|
+
case "conflict":
|
|
1872
|
+
failMap(m, `Starko already has a different source map for this debug id (conflict${message}). A debug id is write-once; delete the stored map in Starko to replace it.`);
|
|
1873
|
+
return;
|
|
1874
|
+
case "failed":
|
|
1875
|
+
failMap(m, `the server refused the uploaded map (failed${message}); it is not sent again by this build`);
|
|
1876
|
+
return;
|
|
1877
|
+
default:
|
|
1878
|
+
failMap(m, `bulk/finish: unexpected answer for this map (${String(entry.status)})`);
|
|
1879
|
+
}
|
|
1880
|
+
};
|
|
1881
|
+
const sendFinish = async (batch) => {
|
|
1882
|
+
const live = batch.filter((m) => !m.settled);
|
|
1883
|
+
if (live.length === 0) return;
|
|
1884
|
+
try {
|
|
1885
|
+
const payload = { maps: live.map((m) => ({ chunk_id: m.p.chunkId, content_hash: m.p.hash })) };
|
|
1886
|
+
const res = await s.request(`bulk/finish (${live.length} map(s))`, PATHS.SOURCEMAPS_BULK_FINISH, {
|
|
1887
|
+
body: JSON.stringify(payload),
|
|
1888
|
+
contentType: "application/json"
|
|
1889
|
+
});
|
|
1890
|
+
const entries = answerFor(res.data?.maps, live, "bulk/finish");
|
|
1891
|
+
const progressed = entries.some((e) => e.status !== "retry");
|
|
1892
|
+
live.forEach((m, i) => handleFinish(m, entries[i], progressed));
|
|
1893
|
+
} catch (error) {
|
|
1894
|
+
if (error instanceof StarkoUploadError && error.fatal) s.stopWith(error);
|
|
1895
|
+
else for (const m of live) failMap(m, errorMessage(error));
|
|
1896
|
+
}
|
|
1897
|
+
};
|
|
1898
|
+
const finishDispatcher = dispatcher(finishQueue, SOURCEMAP_BULK_REQUEST_CONCURRENCY, finishLingerMs, () => mapBatch, () => true, sendFinish);
|
|
1899
|
+
await checkLoop;
|
|
1900
|
+
await Promise.all([...compressors, startDispatcher, ...putWorkers, finishDispatcher]);
|
|
1901
|
+
while (delayed.size > 0) await Promise.all(Array.from(delayed));
|
|
1902
|
+
if (v1Done) await v1Done;
|
|
1903
|
+
unsubscribe();
|
|
1904
|
+
if (closing && s.stop === void 0) s.stop = closing;
|
|
1905
|
+
}
|
|
1906
|
+
async function bulkCheck(s, batch) {
|
|
1907
|
+
const payload = { chunks: batch.map((p) => ({ chunk_id: p.chunkId, content_hash: p.hash })) };
|
|
1908
|
+
if (s.release) payload.release = s.release;
|
|
1909
|
+
const res = await s.request("bulk/check", PATHS.SOURCEMAPS_BULK_CHECK, {
|
|
1910
|
+
body: JSON.stringify(payload),
|
|
1911
|
+
contentType: "application/json"
|
|
1912
|
+
});
|
|
1913
|
+
if (!res.data || !Array.isArray(res.data.missing)) throw new StarkoUploadError("bulk/check: response has no `missing` list");
|
|
1914
|
+
return new Set(res.data.missing.map(String));
|
|
1915
|
+
}
|
|
1916
|
+
function answerFor(entries, batch, what) {
|
|
1917
|
+
if (!Array.isArray(entries) || entries.length !== batch.length) {
|
|
1918
|
+
throw new StarkoUploadError(`${what}: the answer has ${Array.isArray(entries) ? entries.length : "no"} entries for ${batch.length} map(s)`);
|
|
1919
|
+
}
|
|
1920
|
+
entries.forEach((e, i) => {
|
|
1921
|
+
if (!e || typeof e !== "object" || String(e.chunk_id).toLowerCase() !== batch[i].p.chunkId.toLowerCase()) {
|
|
1922
|
+
throw new StarkoUploadError(`${what}: the answer does not follow the request's order`);
|
|
1923
|
+
}
|
|
1924
|
+
});
|
|
1925
|
+
return entries;
|
|
1926
|
+
}
|
|
1927
|
+
function targetProblem(t, m, host) {
|
|
1928
|
+
if (!t || typeof t !== "object") return "no target";
|
|
1929
|
+
if (typeof t.url !== "string") return "no URL";
|
|
1930
|
+
let url;
|
|
1931
|
+
try {
|
|
1932
|
+
url = new URL(t.url);
|
|
1933
|
+
} catch {
|
|
1934
|
+
return "not a URL";
|
|
1935
|
+
}
|
|
1936
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && host.startsWith("http:"))) return `a ${url.protocol} URL`;
|
|
1937
|
+
if (url.username || url.password) return "credentials in the URL";
|
|
1938
|
+
if (t.method !== "PUT") return `method ${String(t.method)}`;
|
|
1939
|
+
if (!t.headers || typeof t.headers !== "object") return "no headers";
|
|
1940
|
+
for (const [k, v] of Object.entries(t.headers)) {
|
|
1941
|
+
if (!HEADER_NAME.test(k) || typeof v !== "string" || /[\r\n]/.test(v)) return "a malformed header";
|
|
1942
|
+
if (/^(authorization|cookie|host|content-length)$/i.test(k)) return `a ${k} header`;
|
|
1943
|
+
}
|
|
1944
|
+
for (const [field, value] of [
|
|
1945
|
+
["form_field", t.form_field],
|
|
1946
|
+
["file_name", t.file_name],
|
|
1947
|
+
["content_type", t.content_type]
|
|
1948
|
+
]) {
|
|
1949
|
+
if (typeof value !== "string" || !value || !SAFE_VALUE.test(value)) return `a malformed ${field}`;
|
|
1950
|
+
}
|
|
1951
|
+
if (t.size_bytes !== m.gz.length) return `size ${String(t.size_bytes)} instead of the ${m.gz.length} bytes declared`;
|
|
1952
|
+
if (!validSeconds(t.expires_in_s) && typeof t.expires_at !== "string") return "no expiry";
|
|
1953
|
+
return void 0;
|
|
1954
|
+
}
|
|
1955
|
+
var validSeconds = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0;
|
|
1956
|
+
function expiryOf(target, received, clockOffsetMs) {
|
|
1957
|
+
if (validSeconds(target.expires_in_s)) return received + target.expires_in_s * 1e3;
|
|
1958
|
+
const at = Date.parse(String(target.expires_at));
|
|
1959
|
+
if (!Number.isNaN(at)) return received + (at - (Date.now() + clockOffsetMs));
|
|
1960
|
+
return received + 5 * 6e4;
|
|
1961
|
+
}
|
|
1962
|
+
async function putToStorage(s, target, gz) {
|
|
1963
|
+
const { body, contentType } = multipartBody(target, gz);
|
|
1964
|
+
const headers = new Headers();
|
|
1965
|
+
for (const [k, v] of Object.entries(target.headers)) headers.set(k, v);
|
|
1966
|
+
headers.set("content-type", contentType);
|
|
1967
|
+
const timeoutMs = s.timeoutMs + Math.ceil(body.length / (128 * 1024)) * PUT_MS_PER_128_KIB;
|
|
1968
|
+
const signal = s.signal;
|
|
1969
|
+
let last = "";
|
|
1970
|
+
for (let attempt = 0; ; attempt++) {
|
|
1971
|
+
if (signal?.aborted) throw abortedError();
|
|
1972
|
+
const controller = new AbortController();
|
|
1973
|
+
let timedOut = false;
|
|
1974
|
+
const timer = setTimeout(() => {
|
|
1975
|
+
timedOut = true;
|
|
1976
|
+
controller.abort();
|
|
1977
|
+
}, timeoutMs);
|
|
1978
|
+
const onAbort = () => controller.abort();
|
|
1979
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1980
|
+
let retryAfter;
|
|
1981
|
+
try {
|
|
1982
|
+
const res = await s.doFetch(target.url, {
|
|
1983
|
+
method: "PUT",
|
|
1984
|
+
headers,
|
|
1985
|
+
body,
|
|
1986
|
+
// Never followed: the body is the client's source code.
|
|
1987
|
+
redirect: "manual",
|
|
1988
|
+
signal: controller.signal
|
|
1989
|
+
});
|
|
1990
|
+
const text = await res.text().catch(() => "");
|
|
1991
|
+
if (res.status >= 200 && res.status < 300) return { kind: "ok" };
|
|
1992
|
+
if (res.status >= 300 && res.status < 400 || res.type === "opaqueredirect") {
|
|
1993
|
+
return { kind: "redirect", reason: `storage answered with a redirect (${res.status || "3xx"}), which is never followed` };
|
|
1994
|
+
}
|
|
1995
|
+
const detail = briefBody(s, text);
|
|
1996
|
+
if (res.status === 408 || res.status === 429 || res.status >= 500) {
|
|
1997
|
+
last = `storage answered ${res.status}${detail}`;
|
|
1998
|
+
if (res.status === 429) retryAfter = retryAfterMs(res.headers.get("retry-after"));
|
|
1999
|
+
} else {
|
|
2000
|
+
return { kind: "refused", reason: `storage refused the upload (${res.status}${detail})` };
|
|
2001
|
+
}
|
|
2002
|
+
} catch (error) {
|
|
2003
|
+
if (signal?.aborted) throw abortedError();
|
|
2004
|
+
last = timedOut ? `no answer from storage within ${Math.round(timeoutMs / 1e3)} s` : s.redact(networkReason(error));
|
|
2005
|
+
} finally {
|
|
2006
|
+
clearTimeout(timer);
|
|
2007
|
+
signal?.removeEventListener("abort", onAbort);
|
|
2008
|
+
}
|
|
2009
|
+
if (attempt >= PUT_RETRIES) return { kind: "unreachable", reason: last };
|
|
2010
|
+
await s.sleep(Math.min(retryAfter ?? s.backoff(attempt), MAX_PUT_RETRY_AFTER_MS));
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
function briefBody(s, text) {
|
|
2014
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
2015
|
+
if (!flat) return "";
|
|
2016
|
+
return `: ${s.redact(flat).slice(0, 160)}`;
|
|
2017
|
+
}
|
|
2018
|
+
async function* prepend(first, rest) {
|
|
2019
|
+
yield first;
|
|
2020
|
+
for (; ; ) {
|
|
2021
|
+
const next = await rest.next();
|
|
2022
|
+
if (next.done) return;
|
|
2023
|
+
yield next.value;
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
// src/build/shared/upload.ts
|
|
2028
|
+
async function* prepare(s, items) {
|
|
2029
|
+
const prepared = /* @__PURE__ */ new Map();
|
|
2030
|
+
for (const item of items) {
|
|
2031
|
+
if (s.stopped()) return;
|
|
2032
|
+
let raw;
|
|
2033
|
+
try {
|
|
2034
|
+
raw = await readItem(item);
|
|
2035
|
+
} catch (error) {
|
|
2036
|
+
s.fail(item.chunkId, item.fileName, `could not read the source map: ${errorMessage(error)}`);
|
|
2037
|
+
continue;
|
|
2038
|
+
}
|
|
2039
|
+
if (raw.length > SOURCEMAP_MAX_BYTES) {
|
|
2040
|
+
s.fail(item.chunkId, item.fileName, `source map is ${raw.length} bytes, over the ${SOURCEMAP_MAX_BYTES}-byte limit; not uploaded`);
|
|
2041
|
+
continue;
|
|
2042
|
+
}
|
|
2043
|
+
let mapDebugId;
|
|
2044
|
+
try {
|
|
2045
|
+
mapDebugId = JSON.parse(raw.toString("utf8")).debugId;
|
|
2046
|
+
} catch {
|
|
2047
|
+
s.fail(item.chunkId, item.fileName, "source map is not valid JSON; not uploaded");
|
|
2048
|
+
continue;
|
|
2049
|
+
}
|
|
2050
|
+
if (mapDebugId !== item.chunkId) {
|
|
2051
|
+
s.fail(item.chunkId, item.fileName, `the source map's debugId (${typeof mapDebugId === "string" ? mapDebugId : "missing"}) is not the bundle's debug id; not uploaded`);
|
|
2052
|
+
continue;
|
|
2053
|
+
}
|
|
2054
|
+
const hash = sha256Hex(raw);
|
|
2055
|
+
const seen = prepared.get(item.chunkId);
|
|
2056
|
+
if (seen) {
|
|
2057
|
+
if (seen.hash !== hash) {
|
|
2058
|
+
s.fail(item.chunkId, item.fileName, `another source map (${seen.fileName ?? "unknown file"}) has the same debug id with different content; not uploaded`);
|
|
2059
|
+
} else {
|
|
2060
|
+
s.progress.total--;
|
|
2061
|
+
}
|
|
2062
|
+
continue;
|
|
2063
|
+
}
|
|
2064
|
+
const p = { chunkId: item.chunkId, fileName: item.fileName, hash, size: raw.length, item };
|
|
2065
|
+
prepared.set(item.chunkId, p);
|
|
2066
|
+
yield p;
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
async function uploadSourcemaps(items, options) {
|
|
2070
|
+
const s = new Session(options);
|
|
2071
|
+
s.progress.total = items.length;
|
|
2072
|
+
const protocol = options.protocol ?? "auto";
|
|
2073
|
+
if (protocol !== "auto" && protocol !== "bulk" && protocol !== "v1") {
|
|
2074
|
+
throw new StarkoUploadError(`unknown upload protocol "${String(protocol)}" (auto, bulk or v1)`, { fatal: true });
|
|
2075
|
+
}
|
|
2076
|
+
const source = prepare(s, items);
|
|
2077
|
+
try {
|
|
2078
|
+
if (protocol === "v1") {
|
|
2079
|
+
s.useProtocol("v1");
|
|
2080
|
+
s.info(`source map upload: using the v1 upload through ${s.host} (as configured)`);
|
|
2081
|
+
await runV1(s, source);
|
|
2082
|
+
} else {
|
|
2083
|
+
await runBulk(s, source, protocol);
|
|
2084
|
+
}
|
|
2085
|
+
} catch (error) {
|
|
2086
|
+
s.stopWith(error);
|
|
2087
|
+
} finally {
|
|
2088
|
+
await source.return(void 0);
|
|
2089
|
+
}
|
|
2090
|
+
if (s.stop !== void 0) throw sanitized(s, s.stop);
|
|
2091
|
+
if (s.signal?.aborted) throw abortedError();
|
|
2092
|
+
return s.result;
|
|
2093
|
+
}
|
|
2094
|
+
function sanitized(s, error) {
|
|
2095
|
+
if (error instanceof StarkoUploadError) {
|
|
2096
|
+
const message = s.redact(error.message);
|
|
2097
|
+
return message === error.message ? error : new StarkoUploadError(message, { status: error.status, fatal: error.fatal });
|
|
2098
|
+
}
|
|
2099
|
+
return new StarkoUploadError(s.redact(errorMessage(error)), { fatal: true });
|
|
2100
|
+
}
|
|
2101
|
+
|
|
2102
|
+
// src/build/shared/files.ts
|
|
2103
|
+
var import_node_crypto4 = require("crypto");
|
|
2104
|
+
var import_node_fs = require("fs");
|
|
2105
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
2106
|
+
var TEMP_SUFFIX = ".starko-tmp";
|
|
2107
|
+
var SHIFTED_FOR_FIELD = "x_starko_shifted_for";
|
|
2108
|
+
var FILES_IN_PARALLEL = 8;
|
|
2109
|
+
var REWRITES_IN_PARALLEL = 2;
|
|
2110
|
+
async function forEachLimit(items, limit, fn) {
|
|
2111
|
+
let next = 0;
|
|
2112
|
+
const workers = Array.from({ length: Math.max(1, Math.min(limit, items.length)) }, async () => {
|
|
2113
|
+
while (next < items.length) {
|
|
2114
|
+
const index = next++;
|
|
2115
|
+
await fn(items[index], index);
|
|
2116
|
+
}
|
|
2117
|
+
});
|
|
2118
|
+
await Promise.all(workers);
|
|
2119
|
+
}
|
|
2120
|
+
function createLimiter(limit) {
|
|
2121
|
+
let active = 0;
|
|
2122
|
+
const waiting = [];
|
|
2123
|
+
return async (fn) => {
|
|
2124
|
+
if (active >= limit) await new Promise((resolve) => waiting.push(resolve));
|
|
2125
|
+
active++;
|
|
2126
|
+
try {
|
|
2127
|
+
return await fn();
|
|
2128
|
+
} finally {
|
|
2129
|
+
active--;
|
|
2130
|
+
waiting.shift()?.();
|
|
2131
|
+
}
|
|
2132
|
+
};
|
|
2133
|
+
}
|
|
2134
|
+
var SOURCE_MAPPING_URL = /^[ \t]*\/\/[#@][ \t]+sourceMappingURL=[ \t]*(\S*)[ \t]*$/;
|
|
2135
|
+
var REMOTE_URL = /^(?:[a-z][a-z0-9+.-]*:)?\/\//i;
|
|
2136
|
+
function findSourceMappingUrl(code) {
|
|
2137
|
+
let end = code.length;
|
|
2138
|
+
for (let i = 0; i < 16 && end > 0; i++) {
|
|
2139
|
+
const start = code.lastIndexOf("\n", end - 1) + 1;
|
|
2140
|
+
const line = code.slice(start, end).replace(/\r$/, "");
|
|
2141
|
+
const lineEnd = end;
|
|
2142
|
+
end = start - 1;
|
|
2143
|
+
if (line.trim() === "") continue;
|
|
2144
|
+
if (!line.trimStart().startsWith("//")) return void 0;
|
|
2145
|
+
const match = SOURCE_MAPPING_URL.exec(line);
|
|
2146
|
+
if (match) return { url: match[1], start, end: lineEnd };
|
|
2147
|
+
}
|
|
2148
|
+
return void 0;
|
|
2149
|
+
}
|
|
2150
|
+
function localMapReference(url) {
|
|
2151
|
+
if (!url || url.startsWith("data:")) return void 0;
|
|
2152
|
+
const clean2 = url.replace(/[?#].*$/, "");
|
|
2153
|
+
let decoded = clean2;
|
|
2154
|
+
try {
|
|
2155
|
+
decoded = decodeURI(clean2);
|
|
2156
|
+
} catch {
|
|
2157
|
+
}
|
|
2158
|
+
if (!decoded || REMOTE_URL.test(decoded) || /^[a-z][a-z0-9+.-]*:/i.test(decoded)) return void 0;
|
|
2159
|
+
if (import_node_path.default.isAbsolute(decoded) || import_node_path.default.posix.isAbsolute(decoded) || import_node_path.default.win32.isAbsolute(decoded)) return void 0;
|
|
2160
|
+
return decoded;
|
|
2161
|
+
}
|
|
2162
|
+
function isInside(root, target) {
|
|
2163
|
+
const rel = import_node_path.default.relative(root, target);
|
|
2164
|
+
return rel !== "" && rel !== ".." && !rel.startsWith(`..${import_node_path.default.sep}`) && !import_node_path.default.isAbsolute(rel);
|
|
2165
|
+
}
|
|
2166
|
+
async function containedFile(realRoot2, candidate) {
|
|
2167
|
+
try {
|
|
2168
|
+
const real = await import_node_fs.promises.realpath(candidate);
|
|
2169
|
+
if (!isInside(realRoot2, real)) return void 0;
|
|
2170
|
+
return (await import_node_fs.promises.stat(real)).isFile() ? real : void 0;
|
|
2171
|
+
} catch {
|
|
2172
|
+
return void 0;
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
async function realRoot(dir) {
|
|
2176
|
+
return import_node_fs.promises.realpath(import_node_path.default.resolve(dir));
|
|
2177
|
+
}
|
|
2178
|
+
async function resolveMapPath(jsPath, code, root) {
|
|
2179
|
+
const rootReal = await realRoot(root);
|
|
2180
|
+
const jsReal = await containedFile(rootReal, jsPath);
|
|
2181
|
+
if (!jsReal) return void 0;
|
|
2182
|
+
const candidates = [];
|
|
2183
|
+
const ref = findSourceMappingUrl(code)?.url;
|
|
2184
|
+
const local = ref === void 0 ? void 0 : localMapReference(ref);
|
|
2185
|
+
if (local !== void 0) candidates.push(import_node_path.default.resolve(import_node_path.default.dirname(jsReal), local));
|
|
2186
|
+
candidates.push(`${jsReal}.map`);
|
|
2187
|
+
for (const candidate of candidates) {
|
|
2188
|
+
const contained = await containedFile(rootReal, candidate);
|
|
2189
|
+
if (contained) return contained;
|
|
2190
|
+
}
|
|
2191
|
+
return void 0;
|
|
2192
|
+
}
|
|
2193
|
+
async function listFiles(dir, onError) {
|
|
2194
|
+
const out = [];
|
|
2195
|
+
const walk = async (current, isRoot) => {
|
|
2196
|
+
let entries;
|
|
2197
|
+
try {
|
|
2198
|
+
entries = await import_node_fs.promises.readdir(current, { withFileTypes: true });
|
|
2199
|
+
} catch (error) {
|
|
2200
|
+
if (isRoot || !onError) throw error;
|
|
2201
|
+
onError(current, error);
|
|
2202
|
+
return;
|
|
2203
|
+
}
|
|
2204
|
+
for (const entry of entries) {
|
|
2205
|
+
const full = import_node_path.default.join(current, entry.name);
|
|
2206
|
+
if (entry.isDirectory()) await walk(full, false);
|
|
2207
|
+
else if (entry.isFile()) out.push(full);
|
|
2208
|
+
}
|
|
2209
|
+
};
|
|
2210
|
+
await walk(dir, true);
|
|
2211
|
+
return out.sort();
|
|
2212
|
+
}
|
|
2213
|
+
async function findBundles(dir, filter) {
|
|
2214
|
+
const root = await realRoot(dir);
|
|
2215
|
+
const bundles = [];
|
|
2216
|
+
const withoutMap = [];
|
|
2217
|
+
const unreadable = [];
|
|
2218
|
+
const files = await listFiles(root, (d, error) => unreadable.push({ file: toRelative(root, d), reason: errorMessage(error) }));
|
|
2219
|
+
const found = [];
|
|
2220
|
+
await forEachLimit(files, FILES_IN_PARALLEL, async (file, index) => {
|
|
2221
|
+
const relativePath = toRelative(root, file);
|
|
2222
|
+
if (!filter(relativePath)) return;
|
|
2223
|
+
try {
|
|
2224
|
+
const code = await import_node_fs.promises.readFile(file, "utf8");
|
|
2225
|
+
const mapPath = await resolveMapPath(file, code, root);
|
|
2226
|
+
found[index] = mapPath ? { bundle: { jsPath: file, relativePath, mapPath, debugId: findDebugId(code) } } : { withoutMap: relativePath };
|
|
2227
|
+
} catch (error) {
|
|
2228
|
+
found[index] = { unreadable: { file: relativePath, reason: errorMessage(error) } };
|
|
2229
|
+
}
|
|
2230
|
+
});
|
|
2231
|
+
for (const f of found) {
|
|
2232
|
+
if (!f) continue;
|
|
2233
|
+
if ("bundle" in f) bundles.push(f.bundle);
|
|
2234
|
+
else if ("withoutMap" in f) withoutMap.push(f.withoutMap);
|
|
2235
|
+
else unreadable.push(f.unreadable);
|
|
2236
|
+
}
|
|
2237
|
+
return { bundles, withoutMap, unreadable };
|
|
2238
|
+
}
|
|
2239
|
+
function toRelative(root, file) {
|
|
2240
|
+
return import_node_path.default.relative(root, file).split(import_node_path.default.sep).join("/");
|
|
2241
|
+
}
|
|
2242
|
+
async function readMap(mapPath) {
|
|
2243
|
+
const map = JSON.parse(await import_node_fs.promises.readFile(mapPath, "utf8"));
|
|
2244
|
+
if (!map || typeof map !== "object" || typeof map.version !== "number") throw new Error("not a source map");
|
|
2245
|
+
return map;
|
|
2246
|
+
}
|
|
2247
|
+
function mapNamesDebugId(raw, id) {
|
|
2248
|
+
const needle = `"debugId":"${id}"`;
|
|
2249
|
+
const first = raw.indexOf("{");
|
|
2250
|
+
if (first === -1 || raw.subarray(0, first).toString("utf8").trim() !== "") return false;
|
|
2251
|
+
const at = raw.indexOf(needle);
|
|
2252
|
+
if (at === -1) return false;
|
|
2253
|
+
const nested = raw.indexOf("{", first + 1);
|
|
2254
|
+
if (nested === -1 || nested > at) return true;
|
|
2255
|
+
const tail = raw.subarray(raw.lastIndexOf(needle) + needle.length);
|
|
2256
|
+
return tail.length <= 64 && tail.toString("utf8").trim() === "}";
|
|
2257
|
+
}
|
|
2258
|
+
var NOFOLLOW = import_node_fs.constants.O_NOFOLLOW ?? 0;
|
|
2259
|
+
async function writeTemp(file, content) {
|
|
2260
|
+
const temp = `${file}.${(0, import_node_crypto4.randomBytes)(8).toString("hex")}${TEMP_SUFFIX}`;
|
|
2261
|
+
const handle = await import_node_fs.promises.open(temp, import_node_fs.constants.O_WRONLY | import_node_fs.constants.O_CREAT | import_node_fs.constants.O_EXCL | NOFOLLOW, 420);
|
|
2262
|
+
try {
|
|
2263
|
+
await handle.writeFile(content);
|
|
2264
|
+
} finally {
|
|
2265
|
+
await handle.close();
|
|
2266
|
+
}
|
|
2267
|
+
return temp;
|
|
2268
|
+
}
|
|
2269
|
+
async function checkReplaced(file, root) {
|
|
2270
|
+
const st = await import_node_fs.promises.lstat(file);
|
|
2271
|
+
if (!st.isFile()) throw new Error(`${file} is not a regular file after it was replaced`);
|
|
2272
|
+
if (root && !isInside(await import_node_fs.promises.realpath(root), await import_node_fs.promises.realpath(file))) throw new Error(`${file} is not inside ${root} after it was replaced`);
|
|
2273
|
+
}
|
|
2274
|
+
async function replaceFile(file, content, root) {
|
|
2275
|
+
let temp;
|
|
2276
|
+
try {
|
|
2277
|
+
temp = await writeTemp(file, content);
|
|
2278
|
+
await import_node_fs.promises.rename(temp, file);
|
|
2279
|
+
temp = void 0;
|
|
2280
|
+
await checkReplaced(file, root);
|
|
2281
|
+
} catch (error) {
|
|
2282
|
+
if (temp) await import_node_fs.promises.unlink(temp).catch(() => void 0);
|
|
2283
|
+
throw error;
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
async function replacePair(mapPath, mapText, jsPath, jsText, root) {
|
|
2287
|
+
let mapTemp;
|
|
2288
|
+
let jsTemp;
|
|
2289
|
+
try {
|
|
2290
|
+
mapTemp = await writeTemp(mapPath, mapText);
|
|
2291
|
+
jsTemp = await writeTemp(jsPath, jsText);
|
|
2292
|
+
await import_node_fs.promises.rename(mapTemp, mapPath);
|
|
2293
|
+
mapTemp = void 0;
|
|
2294
|
+
await import_node_fs.promises.rename(jsTemp, jsPath);
|
|
2295
|
+
jsTemp = void 0;
|
|
2296
|
+
await checkReplaced(mapPath, root);
|
|
2297
|
+
await checkReplaced(jsPath, root);
|
|
2298
|
+
} catch (error) {
|
|
2299
|
+
if (mapTemp) await import_node_fs.promises.unlink(mapTemp).catch(() => void 0);
|
|
2300
|
+
if (jsTemp) await import_node_fs.promises.unlink(jsTemp).catch(() => void 0);
|
|
2301
|
+
throw error;
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
async function removeStaleTemps(root) {
|
|
2305
|
+
const walk = async (dir) => {
|
|
2306
|
+
let entries;
|
|
2307
|
+
try {
|
|
2308
|
+
entries = await import_node_fs.promises.readdir(dir, { withFileTypes: true });
|
|
2309
|
+
} catch {
|
|
2310
|
+
return;
|
|
2311
|
+
}
|
|
2312
|
+
for (const entry of entries) {
|
|
2313
|
+
const full = import_node_path.default.join(dir, entry.name);
|
|
2314
|
+
if (entry.name.endsWith(TEMP_SUFFIX) && (entry.isFile() || entry.isSymbolicLink())) await import_node_fs.promises.unlink(full).catch(() => void 0);
|
|
2315
|
+
else if (entry.isDirectory()) await walk(full);
|
|
2316
|
+
}
|
|
2317
|
+
};
|
|
2318
|
+
await walk(root);
|
|
2319
|
+
}
|
|
2320
|
+
async function injectDirectory(dir, options) {
|
|
2321
|
+
const root = await realRoot(dir);
|
|
2322
|
+
if (!options.dryRun) await removeStaleTemps(root);
|
|
2323
|
+
const { bundles, unreadable } = await findBundles(root, options.filter);
|
|
2324
|
+
const result = { injected: [], alreadyInjected: [], skipped: [...unreadable] };
|
|
2325
|
+
const byMap = /* @__PURE__ */ new Map();
|
|
2326
|
+
for (const b of bundles) byMap.set(b.mapPath, [...byMap.get(b.mapPath) ?? [], b]);
|
|
2327
|
+
const outcomes = [];
|
|
2328
|
+
const rewrite = createLimiter(REWRITES_IN_PARALLEL);
|
|
2329
|
+
await forEachLimit(bundles, FILES_IN_PARALLEL, async (bundle, index) => {
|
|
2330
|
+
const file = bundle.relativePath;
|
|
2331
|
+
try {
|
|
2332
|
+
const code = await import_node_fs.promises.readFile(bundle.jsPath, "utf8");
|
|
2333
|
+
const shared = byMap.get(bundle.mapPath).length > 1;
|
|
2334
|
+
const existing = findDebugId(code);
|
|
2335
|
+
if (existing && (hasStarkoRegistration(code, existing) || options.keepNativelyRegistered && hasNativeRegistration(code, existing)) && mapNamesDebugId(await import_node_fs.promises.readFile(bundle.mapPath), existing)) {
|
|
2336
|
+
outcomes[index] = { list: "alreadyInjected", entry: { file, debugId: existing } };
|
|
2337
|
+
return;
|
|
2338
|
+
}
|
|
2339
|
+
outcomes[index] = await rewrite(async () => {
|
|
2340
|
+
const map = await readMap(bundle.mapPath);
|
|
2341
|
+
const fingerprint = createDebugId(code);
|
|
2342
|
+
const alreadyShifted = map[SHIFTED_FOR_FIELD] === fingerprint;
|
|
2343
|
+
const out = injectDebugId(code, alreadyShifted ? void 0 : map, { release: options.release, keepNativelyRegistered: options.keepNativelyRegistered });
|
|
2344
|
+
if (out.kind === "none") {
|
|
2345
|
+
if (map.debugId !== out.debugId && !options.dryRun && !shared) {
|
|
2346
|
+
await replaceFile(bundle.mapPath, JSON.stringify({ ...map, debugId: out.debugId }), root);
|
|
2347
|
+
}
|
|
2348
|
+
return { list: "alreadyInjected", entry: { file, debugId: out.debugId } };
|
|
2349
|
+
}
|
|
2350
|
+
if (shared) {
|
|
2351
|
+
return { list: "skipped", entry: { file, reason: `its source map ${import_node_path.default.basename(bundle.mapPath)} is shared with another bundle` } };
|
|
2352
|
+
}
|
|
2353
|
+
if (!options.dryRun) {
|
|
2354
|
+
const newMap = alreadyShifted ? { ...map, debugId: out.debugId } : { ...out.map, [SHIFTED_FOR_FIELD]: fingerprint };
|
|
2355
|
+
await replacePair(bundle.mapPath, JSON.stringify(newMap), bundle.jsPath, out.code, root);
|
|
2356
|
+
}
|
|
2357
|
+
return { list: "injected", entry: { file, debugId: out.debugId, existingId: out.kind === "snippet" } };
|
|
2358
|
+
});
|
|
2359
|
+
} catch (error) {
|
|
2360
|
+
outcomes[index] = { list: "skipped", entry: { file, reason: errorMessage(error) } };
|
|
2361
|
+
}
|
|
2362
|
+
});
|
|
2363
|
+
for (const o of outcomes) {
|
|
2364
|
+
if (!o) continue;
|
|
2365
|
+
if (o.list === "injected") result.injected.push(o.entry);
|
|
2366
|
+
else if (o.list === "alreadyInjected") result.alreadyInjected.push(o.entry);
|
|
2367
|
+
else result.skipped.push(o.entry);
|
|
2368
|
+
}
|
|
2369
|
+
return result;
|
|
2370
|
+
}
|
|
2371
|
+
async function collectUploads(dir, filter) {
|
|
2372
|
+
const { bundles, unreadable } = await findBundles(dir, filter);
|
|
2373
|
+
const collected = await collectUploadsFor(bundles);
|
|
2374
|
+
return { ...collected, unreadable: [...unreadable, ...collected.unreadable] };
|
|
2375
|
+
}
|
|
2376
|
+
async function loadMapFor(mapPath, chunkId) {
|
|
2377
|
+
const raw = await import_node_fs.promises.readFile(mapPath);
|
|
2378
|
+
if (raw.includes(`"debugId":"${chunkId}"`)) return raw;
|
|
2379
|
+
const map = JSON.parse(raw.toString("utf8"));
|
|
2380
|
+
return Buffer.from(JSON.stringify({ ...map, debugId: chunkId }), "utf8");
|
|
2381
|
+
}
|
|
2382
|
+
async function collectUploadsFor(bundles) {
|
|
2383
|
+
const result = { entries: [], missingDebugId: [], unreadable: [] };
|
|
2384
|
+
for (const bundle of bundles) {
|
|
2385
|
+
try {
|
|
2386
|
+
let chunkId = bundle.debugId ?? findDebugId(await import_node_fs.promises.readFile(bundle.jsPath, "utf8"));
|
|
2387
|
+
if (!chunkId) {
|
|
2388
|
+
const map = await readMap(bundle.mapPath);
|
|
2389
|
+
chunkId = isDebugId(map.debugId) ? map.debugId.toLowerCase() : void 0;
|
|
2390
|
+
}
|
|
2391
|
+
if (!chunkId) {
|
|
2392
|
+
result.missingDebugId.push(bundle.relativePath);
|
|
2393
|
+
continue;
|
|
2394
|
+
}
|
|
2395
|
+
const id = chunkId;
|
|
2396
|
+
const mapPath = bundle.mapPath;
|
|
2397
|
+
result.entries.push({ chunkId: id, load: () => loadMapFor(mapPath, id), fileName: bundle.relativePath, jsPath: bundle.jsPath, mapPath });
|
|
2398
|
+
} catch (error) {
|
|
2399
|
+
result.unreadable.push({ file: bundle.relativePath, reason: errorMessage(error) });
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
return result;
|
|
2403
|
+
}
|
|
2404
|
+
function stripSourceMappingUrl(code, ref) {
|
|
2405
|
+
let cut = ref.end;
|
|
2406
|
+
if (code[cut] === "\n") cut++;
|
|
2407
|
+
return code.slice(0, ref.start) + code.slice(cut);
|
|
2408
|
+
}
|
|
2409
|
+
async function deleteSourcemaps(entries, root, logger) {
|
|
2410
|
+
let deleted = 0;
|
|
2411
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2412
|
+
let rootReal;
|
|
2413
|
+
try {
|
|
2414
|
+
rootReal = await realRoot(root);
|
|
2415
|
+
} catch (error) {
|
|
2416
|
+
logger?.warn(`could not delete source maps under ${root}: ${errorMessage(error)}`);
|
|
2417
|
+
return 0;
|
|
2418
|
+
}
|
|
2419
|
+
for (const { jsPath, mapPath } of entries) {
|
|
2420
|
+
if (!mapPath) continue;
|
|
2421
|
+
const map = await containedFile(rootReal, mapPath);
|
|
2422
|
+
if (!map) {
|
|
2423
|
+
if (!seen.has(mapPath)) logger?.warn(`not deleting ${mapPath}: it is not a file inside ${root}`);
|
|
2424
|
+
seen.add(mapPath);
|
|
2425
|
+
continue;
|
|
2426
|
+
}
|
|
2427
|
+
if (seen.has(map)) continue;
|
|
2428
|
+
seen.add(map);
|
|
2429
|
+
try {
|
|
2430
|
+
await import_node_fs.promises.rm(map, { force: true });
|
|
2431
|
+
deleted++;
|
|
2432
|
+
} catch (error) {
|
|
2433
|
+
logger?.warn(`could not delete ${map}: ${errorMessage(error)}`);
|
|
2434
|
+
continue;
|
|
2435
|
+
}
|
|
2436
|
+
if (!jsPath) continue;
|
|
2437
|
+
try {
|
|
2438
|
+
const js = await containedFile(rootReal, jsPath);
|
|
2439
|
+
if (!js) continue;
|
|
2440
|
+
const code = await import_node_fs.promises.readFile(js, "utf8");
|
|
2441
|
+
const ref = findSourceMappingUrl(code);
|
|
2442
|
+
const local = ref && localMapReference(ref.url);
|
|
2443
|
+
if (!ref || local === void 0) continue;
|
|
2444
|
+
const target = import_node_path.default.resolve(import_node_path.default.dirname(js), local);
|
|
2445
|
+
if (target !== map && import_node_path.default.basename(target) !== import_node_path.default.basename(map)) continue;
|
|
2446
|
+
await replaceFile(js, stripSourceMappingUrl(code, ref), rootReal);
|
|
2447
|
+
} catch (error) {
|
|
2448
|
+
logger?.warn(`could not remove the sourceMappingURL comment from ${jsPath}: ${errorMessage(error)}`);
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
return deleted;
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
// src/build/shared/pipeline.ts
|
|
2455
|
+
var PROGRESS_INTERVAL_MS = 3e4;
|
|
2456
|
+
var MAX_FAILURE_LINES = 20;
|
|
2457
|
+
async function runUpload(entries, options) {
|
|
2458
|
+
try {
|
|
2459
|
+
return await runUploadUnsafe(entries, options);
|
|
2460
|
+
} catch (error) {
|
|
2461
|
+
const message = errorMessage(error);
|
|
2462
|
+
try {
|
|
2463
|
+
options.logger.error(`source map upload failed: ${message}`);
|
|
2464
|
+
} catch {
|
|
2465
|
+
}
|
|
2466
|
+
return { ok: false, error: message };
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
var count = (n) => n.toLocaleString("en-US");
|
|
2470
|
+
function progressLine(p) {
|
|
2471
|
+
return `${count(p.checked)} of ${count(p.total)} checked, ${count(p.uploaded)} uploaded, ${count(p.skipped)} already on the server, ${count(p.failed)} failed`;
|
|
2472
|
+
}
|
|
2473
|
+
function stoppedSummary(p) {
|
|
2474
|
+
const notSent = Math.max(0, p.total - p.uploaded - p.skipped - p.failed);
|
|
2475
|
+
const inFlight = p.sending > 0 ? ` (${count(p.sending)} of them were being sent and were stopped)` : "";
|
|
2476
|
+
return `${count(p.uploaded)} of ${count(p.total)} source map(s) uploaded, ${count(p.skipped)} already on the server, ${count(p.failed)} failed, ${count(notSent)} not sent${inFlight}`;
|
|
2477
|
+
}
|
|
2478
|
+
var CONTINUES = "The next build continues from there: a map the server has completed is never sent again.";
|
|
2479
|
+
async function runUploadUnsafe(entries, options) {
|
|
2480
|
+
const { logger } = options;
|
|
2481
|
+
const releaseNote = options.release ? ` for release ${options.release.version}` : "";
|
|
2482
|
+
if (entries.length === 0) {
|
|
2483
|
+
logger.info("no source maps to upload");
|
|
2484
|
+
return { ok: true };
|
|
2485
|
+
}
|
|
2486
|
+
if (options.dryRun) {
|
|
2487
|
+
logger.info(`dry run: would upload ${entries.length} source map(s)${releaseNote} to ${options.host}`);
|
|
2488
|
+
for (const e of entries) logger.info(` ${e.fileName ?? e.mapPath ?? "?"} \u2192 ${e.chunkId}`);
|
|
2489
|
+
return { ok: true };
|
|
2490
|
+
}
|
|
2491
|
+
if (!options.apiKey) {
|
|
2492
|
+
const error = `no API key (set apiKey or STARKO_API_KEY): ${entries.length} source map(s) were not uploaded`;
|
|
2493
|
+
logger.warn(error);
|
|
2494
|
+
return { ok: false, error };
|
|
2495
|
+
}
|
|
2496
|
+
const deadlineMs = options.uploadTimeoutMs !== void 0 && options.uploadTimeoutMs > 0 ? options.uploadTimeoutMs : DEFAULT_UPLOAD_TIMEOUT_MS;
|
|
2497
|
+
const started = Date.now();
|
|
2498
|
+
const progress = createUploadProgress();
|
|
2499
|
+
const controller = new AbortController();
|
|
2500
|
+
let timer;
|
|
2501
|
+
const deadline = new Promise((resolve) => {
|
|
2502
|
+
timer = setTimeout(() => {
|
|
2503
|
+
controller.abort();
|
|
2504
|
+
resolve("deadline");
|
|
2505
|
+
}, deadlineMs);
|
|
2506
|
+
});
|
|
2507
|
+
const interval = options.progressIntervalMs !== void 0 && options.progressIntervalMs > 0 ? options.progressIntervalMs : PROGRESS_INTERVAL_MS;
|
|
2508
|
+
const ticker = setInterval(() => {
|
|
2509
|
+
try {
|
|
2510
|
+
logger.info(`source map upload: ${progressLine(progress)} (${formatDuration(Date.now() - started)} so far)`);
|
|
2511
|
+
} catch {
|
|
2512
|
+
}
|
|
2513
|
+
}, interval);
|
|
2514
|
+
ticker.unref?.();
|
|
2515
|
+
const upload = uploadSourcemaps(entries, {
|
|
2516
|
+
concurrency: options.uploadConcurrency,
|
|
2517
|
+
...options.client,
|
|
2518
|
+
protocol: options.uploadProtocol ?? "auto",
|
|
2519
|
+
apiKey: options.apiKey,
|
|
2520
|
+
host: options.host,
|
|
2521
|
+
release: options.release,
|
|
2522
|
+
logger,
|
|
2523
|
+
signal: controller.signal,
|
|
2524
|
+
deadlineAt: started + deadlineMs,
|
|
2525
|
+
progress
|
|
2526
|
+
});
|
|
2527
|
+
upload.catch(() => void 0);
|
|
2528
|
+
let result;
|
|
2529
|
+
try {
|
|
2530
|
+
const raced = await Promise.race([upload, deadline]);
|
|
2531
|
+
if (raced === "deadline") {
|
|
2532
|
+
const error = `the upload did not finish within ${formatDuration(deadlineMs)}: ${stoppedSummary(progress)}`;
|
|
2533
|
+
logger.warn(
|
|
2534
|
+
`source map upload: ${error}. ${CONTINUES} To upload more per build, raise uploadTimeoutMs${progress.protocol === "bulk" ? "" : " (or uploadConcurrency)"}; see "Large builds" in the source maps guide.`
|
|
2535
|
+
);
|
|
2536
|
+
return { ok: false, error };
|
|
2537
|
+
}
|
|
2538
|
+
result = raced;
|
|
2539
|
+
} catch (error) {
|
|
2540
|
+
const message = controller.signal.aborted ? `the upload did not finish within ${formatDuration(deadlineMs)}: ${stoppedSummary(progress)}` : errorMessage(error);
|
|
2541
|
+
const partial = progress.uploaded + progress.skipped > 0 ? ` (before it stopped: ${stoppedSummary(progress)}. ${CONTINUES})` : "";
|
|
2542
|
+
logger.error(`source map upload failed: ${message}${controller.signal.aborted ? ` ${CONTINUES}` : partial}`);
|
|
2543
|
+
return { ok: false, error: message };
|
|
2544
|
+
} finally {
|
|
2545
|
+
clearTimeout(timer);
|
|
2546
|
+
clearInterval(ticker);
|
|
2547
|
+
}
|
|
2548
|
+
for (const f of result.failed.slice(0, MAX_FAILURE_LINES)) logger.warn(`could not upload ${f.fileName ?? f.chunkId}: ${f.error}`);
|
|
2549
|
+
if (result.failed.length > MAX_FAILURE_LINES) logger.warn(`\u2026 and ${count(result.failed.length - MAX_FAILURE_LINES)} more source map(s) could not be uploaded`);
|
|
2550
|
+
logger.info(
|
|
2551
|
+
`source maps${releaseNote}: ${count(result.uploaded.length)} uploaded, ${count(result.skipped.length)} already on the server, ${count(result.failed.length)} failed (${formatDuration(Date.now() - started)})`
|
|
2552
|
+
);
|
|
2553
|
+
return result.failed.length === 0 ? { ok: true, result } : { ok: false, result, error: `${result.failed.length} source map(s) could not be uploaded` };
|
|
2554
|
+
}
|
|
2555
|
+
|
|
2556
|
+
// src/cli/main.ts
|
|
2557
|
+
var HELP = `starko ${PACKAGE_VERSION}: Starko error tracking source maps
|
|
2558
|
+
|
|
2559
|
+
Usage
|
|
2560
|
+
starko sourcemaps inject <dir> [options]
|
|
2561
|
+
Give every bundle under <dir> that has a source map a debug id: a
|
|
2562
|
+
runtime snippet, a trailing //# debugId= comment and the map's debugId
|
|
2563
|
+
field. A bundle that already has a debug id from another tool keeps it
|
|
2564
|
+
and only gets the snippet registering it. Rewrites the files in place
|
|
2565
|
+
(each bundle and its map as a pair); bundles already injected are left
|
|
2566
|
+
as they are, so running it twice is safe. Only files inside <dir> are
|
|
2567
|
+
read or written: a sourceMappingURL pointing outside it (../, an
|
|
2568
|
+
absolute path, a symbolic link, a URL) is ignored.
|
|
2569
|
+
|
|
2570
|
+
starko sourcemaps upload <dir> [options]
|
|
2571
|
+
Upload the source maps of the injected bundles under <dir> to Starko.
|
|
2572
|
+
Maps the server already has are skipped.
|
|
2573
|
+
|
|
2574
|
+
Options
|
|
2575
|
+
--release <version> Release of this build. Default: STARKO_RELEASE, the CI
|
|
2576
|
+
commit (GITHUB_SHA, VERCEL_GIT_COMMIT_SHA, \u2026), git HEAD.
|
|
2577
|
+
--api-key <key> Secret key with the sourcemaps:write scope.
|
|
2578
|
+
Default: STARKO_API_KEY (prefer the variable: flags
|
|
2579
|
+
show up in process lists and shell history).
|
|
2580
|
+
--host <url> Starko host. Default: STARKO_HOST, https://v2.starko.one
|
|
2581
|
+
--include <glob> Only bundles matching the glob (repeatable).
|
|
2582
|
+
--exclude <glob> Skip bundles matching the glob (repeatable).
|
|
2583
|
+
Default: **/node_modules/**
|
|
2584
|
+
--delete-after-upload upload: delete the maps (and the sourceMappingURL
|
|
2585
|
+
comments pointing at them) after the upload attempt,
|
|
2586
|
+
EVEN WHEN IT FAILED, so a deploy of <dir> never
|
|
2587
|
+
ships them. Not with --dry-run.
|
|
2588
|
+
--timeout <seconds> upload: deadline for the whole upload. Default 180.
|
|
2589
|
+
When it passes, what was uploaded stays uploaded and
|
|
2590
|
+
the next run sends only the rest.
|
|
2591
|
+
--concurrency <n> upload: source maps sent at the same time, 1 to 16.
|
|
2592
|
+
Default 16 with the bulk upload, 8 with v1.
|
|
2593
|
+
--protocol <name> upload: auto (default), bulk or v1. auto sends each
|
|
2594
|
+
map straight to Starko's storage when the server
|
|
2595
|
+
offers it (bulk), else through the Starko host (v1),
|
|
2596
|
+
and falls back to v1 when the storage host cannot
|
|
2597
|
+
be reached. bulk never falls back; v1 never
|
|
2598
|
+
contacts the storage host.
|
|
2599
|
+
--dry-run Show what would be done; change and send nothing.
|
|
2600
|
+
--silent Print errors only.
|
|
2601
|
+
-h, --help Show this help.
|
|
2602
|
+
-v, --version Print the version.
|
|
2603
|
+
|
|
2604
|
+
Exit status: 0 on success, 1 when the command failed, 2 on a usage error.
|
|
2605
|
+
`;
|
|
2606
|
+
function defaultIo() {
|
|
2607
|
+
return {
|
|
2608
|
+
stdout: (line) => process.stdout.write(`${line}
|
|
2609
|
+
`),
|
|
2610
|
+
stderr: (line) => process.stderr.write(`${line}
|
|
2611
|
+
`),
|
|
2612
|
+
env: process.env,
|
|
2613
|
+
cwd: process.cwd()
|
|
2614
|
+
};
|
|
2615
|
+
}
|
|
2616
|
+
var UsageError = class extends Error {
|
|
2617
|
+
};
|
|
2618
|
+
async function main(argv, io = defaultIo()) {
|
|
2619
|
+
let parsed;
|
|
2620
|
+
try {
|
|
2621
|
+
parsed = parse(argv);
|
|
2622
|
+
} catch (error) {
|
|
2623
|
+
io.stderr(`starko: ${error instanceof Error ? error.message : String(error)}`);
|
|
2624
|
+
io.stderr("Run `starko --help` for usage.");
|
|
2625
|
+
return 2;
|
|
2626
|
+
}
|
|
2627
|
+
const { values, positionals } = parsed;
|
|
2628
|
+
if (values.help) {
|
|
2629
|
+
io.stdout(HELP);
|
|
2630
|
+
return 0;
|
|
2631
|
+
}
|
|
2632
|
+
if (values.version) {
|
|
2633
|
+
io.stdout(PACKAGE_VERSION);
|
|
2634
|
+
return 0;
|
|
2635
|
+
}
|
|
2636
|
+
try {
|
|
2637
|
+
const [group, command, dir, ...extra] = positionals;
|
|
2638
|
+
if (group === void 0) {
|
|
2639
|
+
io.stdout(HELP);
|
|
2640
|
+
return 2;
|
|
2641
|
+
}
|
|
2642
|
+
if (group !== "sourcemaps") throw new UsageError(`unknown command "${group}"`);
|
|
2643
|
+
if (command !== "inject" && command !== "upload") {
|
|
2644
|
+
throw new UsageError(command ? `unknown command "sourcemaps ${command}"` : "missing command: sourcemaps inject | upload");
|
|
2645
|
+
}
|
|
2646
|
+
if (!dir) throw new UsageError(`missing directory: starko sourcemaps ${command} <dir>`);
|
|
2647
|
+
if (extra.length > 0) throw new UsageError(`unexpected argument "${extra[0]}"`);
|
|
2648
|
+
if (command === "inject" && values["delete-after-upload"]) throw new UsageError("--delete-after-upload only applies to upload");
|
|
2649
|
+
if (command === "inject" && values.timeout !== void 0) throw new UsageError("--timeout only applies to upload");
|
|
2650
|
+
if (command === "inject" && values.concurrency !== void 0) throw new UsageError("--concurrency only applies to upload");
|
|
2651
|
+
if (command === "inject" && values.protocol !== void 0) throw new UsageError("--protocol only applies to upload");
|
|
2652
|
+
let uploadProtocol;
|
|
2653
|
+
if (values.protocol !== void 0) {
|
|
2654
|
+
if (!UPLOAD_PROTOCOLS.includes(values.protocol)) {
|
|
2655
|
+
throw new UsageError(`--protocol must be ${UPLOAD_PROTOCOLS.join(", ").replace(/, (?=[^,]*$)/, " or ")}, not "${values.protocol}"`);
|
|
2656
|
+
}
|
|
2657
|
+
uploadProtocol = values.protocol;
|
|
2658
|
+
}
|
|
2659
|
+
let uploadTimeoutMs;
|
|
2660
|
+
if (values.timeout !== void 0) {
|
|
2661
|
+
const seconds = Number(values.timeout);
|
|
2662
|
+
if (!Number.isFinite(seconds) || seconds <= 0) throw new UsageError(`--timeout must be a positive number of seconds, not "${values.timeout}"`);
|
|
2663
|
+
uploadTimeoutMs = seconds * 1e3;
|
|
2664
|
+
}
|
|
2665
|
+
let uploadConcurrency;
|
|
2666
|
+
if (values.concurrency !== void 0) {
|
|
2667
|
+
const n = Number(values.concurrency);
|
|
2668
|
+
if (!Number.isInteger(n) || n < 1 || n > MAX_UPLOAD_CONCURRENCY) {
|
|
2669
|
+
throw new UsageError(`--concurrency must be a whole number from 1 to ${MAX_UPLOAD_CONCURRENCY}, not "${values.concurrency}"`);
|
|
2670
|
+
}
|
|
2671
|
+
uploadConcurrency = n;
|
|
2672
|
+
}
|
|
2673
|
+
const root = import_node_path2.default.resolve(io.cwd, dir);
|
|
2674
|
+
if (!(0, import_node_fs2.existsSync)(root) || !(0, import_node_fs2.statSync)(root).isDirectory()) {
|
|
2675
|
+
io.stderr(`starko: ${dir} is not a directory`);
|
|
2676
|
+
return 1;
|
|
2677
|
+
}
|
|
2678
|
+
const connection = resolveConnection({ apiKey: values["api-key"], host: values.host }, io.env);
|
|
2679
|
+
const sink = (level, line) => level === "info" ? io.stdout(line) : io.stderr(line);
|
|
2680
|
+
const logger = createLogger({ silent: values.silent, prefix: "", secrets: [connection.apiKey], sink });
|
|
2681
|
+
const errorLogger = createLogger({ prefix: "", secrets: [connection.apiKey], sink });
|
|
2682
|
+
const filter = createFileFilter({ include: values.include, exclude: values.exclude });
|
|
2683
|
+
const releaseEnv = { env: io.env, cwd: root };
|
|
2684
|
+
if (command === "inject") {
|
|
2685
|
+
const release = resolveRelease(values.release, releaseEnv);
|
|
2686
|
+
const result = await injectDirectory(root, { filter, release, dryRun: values["dry-run"] });
|
|
2687
|
+
const verb = values["dry-run"] ? "would inject" : "injected";
|
|
2688
|
+
for (const r of result.injected) {
|
|
2689
|
+
logger.info(r.existingId ? `${verb} the snippet for the existing debug id of ${r.file} (${r.debugId})` : `${verb} ${r.file} (${r.debugId})`);
|
|
2690
|
+
}
|
|
2691
|
+
for (const r of result.alreadyInjected) logger.info(`already injected ${r.file} (${r.debugId})`);
|
|
2692
|
+
for (const r of result.skipped) errorLogger.warn(`skipped ${r.file}: ${r.reason}`);
|
|
2693
|
+
logger.info(
|
|
2694
|
+
`${verb} ${result.injected.length} bundle(s), ${result.alreadyInjected.length} already had a debug id, ${result.skipped.length} skipped${release ? ` (release ${release})` : ""}`
|
|
2695
|
+
);
|
|
2696
|
+
if (result.injected.length + result.alreadyInjected.length === 0) {
|
|
2697
|
+
errorLogger.error(`no bundle with a source map found under ${dir}`);
|
|
2698
|
+
return 1;
|
|
2699
|
+
}
|
|
2700
|
+
return result.skipped.length > 0 ? 1 : 0;
|
|
2701
|
+
}
|
|
2702
|
+
const collected = await collectUploads(root, filter);
|
|
2703
|
+
for (const u of collected.unreadable) errorLogger.warn(`could not read ${u.file}: ${u.reason}`);
|
|
2704
|
+
if (collected.missingDebugId.length > 0) {
|
|
2705
|
+
errorLogger.warn(
|
|
2706
|
+
`${collected.missingDebugId.length} bundle(s) have a source map but no debug id and are not uploaded (run \`starko sourcemaps inject ${dir}\` first): ${collected.missingDebugId.slice(0, 5).join(", ")}${collected.missingDebugId.length > 5 ? ", \u2026" : ""}`
|
|
2707
|
+
);
|
|
2708
|
+
}
|
|
2709
|
+
if (collected.entries.length === 0) {
|
|
2710
|
+
errorLogger.error(`no injected bundle with a source map found under ${dir}`);
|
|
2711
|
+
return 1;
|
|
2712
|
+
}
|
|
2713
|
+
let outcome;
|
|
2714
|
+
try {
|
|
2715
|
+
outcome = await runUpload(collected.entries, {
|
|
2716
|
+
apiKey: connection.apiKey,
|
|
2717
|
+
host: connection.host,
|
|
2718
|
+
release: resolveReleaseInfo(values.release, releaseEnv),
|
|
2719
|
+
dryRun: values["dry-run"],
|
|
2720
|
+
uploadTimeoutMs,
|
|
2721
|
+
uploadConcurrency,
|
|
2722
|
+
uploadProtocol,
|
|
2723
|
+
logger: values.silent ? errorLogger : logger,
|
|
2724
|
+
client: io.client
|
|
2725
|
+
});
|
|
2726
|
+
} finally {
|
|
2727
|
+
if (values["delete-after-upload"] && !values["dry-run"]) {
|
|
2728
|
+
const deleted = await deleteSourcemaps(collected.entries, root, errorLogger);
|
|
2729
|
+
if (deleted > 0) logger.info(`deleted ${deleted} source map(s) from ${dir}`);
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
if (!outcome.ok) {
|
|
2733
|
+
if (values.silent && outcome.error) errorLogger.error(outcome.error);
|
|
2734
|
+
return 1;
|
|
2735
|
+
}
|
|
2736
|
+
return collected.unreadable.length > 0 ? 1 : 0;
|
|
2737
|
+
} catch (error) {
|
|
2738
|
+
if (error instanceof UsageError) {
|
|
2739
|
+
io.stderr(`starko: ${error.message}`);
|
|
2740
|
+
io.stderr("Run `starko --help` for usage.");
|
|
2741
|
+
return 2;
|
|
2742
|
+
}
|
|
2743
|
+
io.stderr(`starko: ${error instanceof Error ? error.message : String(error)}`);
|
|
2744
|
+
return 1;
|
|
2745
|
+
}
|
|
2746
|
+
}
|
|
2747
|
+
function parse(argv) {
|
|
2748
|
+
return (0, import_node_util3.parseArgs)({
|
|
2749
|
+
args: argv,
|
|
2750
|
+
allowPositionals: true,
|
|
2751
|
+
strict: true,
|
|
2752
|
+
options: {
|
|
2753
|
+
release: { type: "string" },
|
|
2754
|
+
"api-key": { type: "string" },
|
|
2755
|
+
host: { type: "string" },
|
|
2756
|
+
include: { type: "string", multiple: true },
|
|
2757
|
+
exclude: { type: "string", multiple: true },
|
|
2758
|
+
"delete-after-upload": { type: "boolean" },
|
|
2759
|
+
timeout: { type: "string" },
|
|
2760
|
+
concurrency: { type: "string" },
|
|
2761
|
+
protocol: { type: "string" },
|
|
2762
|
+
"dry-run": { type: "boolean" },
|
|
2763
|
+
silent: { type: "boolean" },
|
|
2764
|
+
help: { type: "boolean", short: "h" },
|
|
2765
|
+
version: { type: "boolean", short: "v" }
|
|
2766
|
+
}
|
|
2767
|
+
});
|
|
2768
|
+
}
|
|
2769
|
+
|
|
2770
|
+
// src/cli/index.ts
|
|
2771
|
+
main(process.argv.slice(2)).then(
|
|
2772
|
+
(code) => {
|
|
2773
|
+
process.exitCode = code;
|
|
2774
|
+
},
|
|
2775
|
+
(error) => {
|
|
2776
|
+
process.stderr.write(`starko: ${error instanceof Error ? error.message : String(error)}
|
|
2777
|
+
`);
|
|
2778
|
+
process.exitCode = 1;
|
|
2779
|
+
}
|
|
2780
|
+
);
|