@jsenv/test 3.6.13 → 3.7.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/dist/exception.js +959 -0
- package/dist/execute_using_dynamic_import.js +1 -1
- package/dist/js/node_child_process_controlled.mjs +5 -1
- package/dist/js/node_worker_thread_controlled.mjs +5 -1
- package/dist/jsenv_test.js +1546 -683
- package/dist/jsenv_test_node_modules.js +198 -1
- package/package.json +22 -20
- package/src/runtime_node/child_exec_options.js +1 -2
package/dist/jsenv_test.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { readdir, chmod, stat, lstat, chmodSync, statSync, lstatSync, promises, readFile as readFile$1, readdirSync, openSync, closeSync, unlinkSync, rmdirSync, mkdirSync, readFileSync, writeFileSync as writeFileSync$1, unlink, rmdir, existsSync } from "node:fs";
|
|
1
|
+
import { createSupportsColor, isUnicodeSupported, stripAnsi, emojiRegex, eastAsianWidth, clearTerminal, eraseLines } from "./jsenv_test_node_modules.js";
|
|
2
|
+
import { URL_META, createException } from "./exception.js";
|
|
3
|
+
import { readdir, chmod, stat, lstat, chmodSync, statSync, lstatSync, promises, readFile as readFile$1, readdirSync, openSync, closeSync, unlinkSync, rmdirSync, mkdirSync, readFileSync, writeFileSync as writeFileSync$1, unlink, rmdir, existsSync, realpathSync } from "node:fs";
|
|
4
4
|
import { takeCoverage } from "node:v8";
|
|
5
|
-
import stripAnsi from "strip-ansi";
|
|
6
5
|
import { pathToFileURL, fileURLToPath } from "node:url";
|
|
7
6
|
import { createRequire } from "node:module";
|
|
8
7
|
import { applyBabelPlugins } from "@jsenv/ast";
|
|
9
8
|
import { cpuUsage, memoryUsage } from "node:process";
|
|
10
|
-
import { createSupportsColor, isUnicodeSupported, emojiRegex, eastAsianWidth, clearTerminal, eraseLines } from "./jsenv_test_node_modules.js";
|
|
11
9
|
import { stripVTControlCharacters } from "node:util";
|
|
12
|
-
import { createException } from "@jsenv/exception";
|
|
13
10
|
import crypto from "node:crypto";
|
|
14
11
|
import { dirname } from "node:path";
|
|
15
12
|
import { createServer } from "node:net";
|
|
@@ -17,7 +14,6 @@ import { spawn, spawnSync, fork } from "node:child_process";
|
|
|
17
14
|
import { availableParallelism, cpus, totalmem, release, freemem } from "node:os";
|
|
18
15
|
import { SOURCEMAP, generateSourcemapDataUrl } from "@jsenv/sourcemap";
|
|
19
16
|
import { injectSupervisorIntoHTML, supervisorFileUrl } from "@jsenv/plugin-supervisor";
|
|
20
|
-
import { findFreePort } from "@jsenv/server";
|
|
21
17
|
import { Worker } from "node:worker_threads";
|
|
22
18
|
import he from "he";
|
|
23
19
|
import "node:tty";
|
|
@@ -2163,609 +2159,6 @@ const findSelfOrAncestorDirectoryUrl = (url, callback) => {
|
|
|
2163
2159
|
return null;
|
|
2164
2160
|
};
|
|
2165
2161
|
|
|
2166
|
-
/*
|
|
2167
|
-
* Link to things doing pattern matching:
|
|
2168
|
-
* https://git-scm.com/docs/gitignore
|
|
2169
|
-
* https://github.com/kaelzhang/node-ignore
|
|
2170
|
-
*/
|
|
2171
|
-
|
|
2172
|
-
/** @module jsenv_url_meta **/
|
|
2173
|
-
/**
|
|
2174
|
-
* An object representing the result of applying a pattern to an url
|
|
2175
|
-
* @typedef {Object} MatchResult
|
|
2176
|
-
* @property {boolean} matched Indicates if url matched pattern
|
|
2177
|
-
* @property {number} patternIndex Index where pattern stopped matching url, otherwise pattern.length
|
|
2178
|
-
* @property {number} urlIndex Index where url stopped matching pattern, otherwise url.length
|
|
2179
|
-
* @property {Array} matchGroups Array of strings captured during pattern matching
|
|
2180
|
-
*/
|
|
2181
|
-
|
|
2182
|
-
/**
|
|
2183
|
-
* Apply a pattern to an url
|
|
2184
|
-
* @param {Object} applyPatternMatchingParams
|
|
2185
|
-
* @param {string} applyPatternMatchingParams.pattern "*", "**" and trailing slash have special meaning
|
|
2186
|
-
* @param {string} applyPatternMatchingParams.url a string representing an url
|
|
2187
|
-
* @return {MatchResult}
|
|
2188
|
-
*/
|
|
2189
|
-
const applyPattern = ({ url, pattern }) => {
|
|
2190
|
-
const { matched, patternIndex, index, groups } = applyMatching(pattern, url);
|
|
2191
|
-
const matchGroups = [];
|
|
2192
|
-
let groupIndex = 0;
|
|
2193
|
-
for (const group of groups) {
|
|
2194
|
-
if (group.name) {
|
|
2195
|
-
matchGroups[group.name] = group.string;
|
|
2196
|
-
} else {
|
|
2197
|
-
matchGroups[groupIndex] = group.string;
|
|
2198
|
-
groupIndex++;
|
|
2199
|
-
}
|
|
2200
|
-
}
|
|
2201
|
-
return {
|
|
2202
|
-
matched,
|
|
2203
|
-
patternIndex,
|
|
2204
|
-
urlIndex: index,
|
|
2205
|
-
matchGroups,
|
|
2206
|
-
};
|
|
2207
|
-
};
|
|
2208
|
-
|
|
2209
|
-
/**
|
|
2210
|
-
* Core pattern matching function that processes patterns against strings
|
|
2211
|
-
* @param {string} pattern - The pattern with special syntax (*,**,/) to match against
|
|
2212
|
-
* @param {string} string - The string to test against the pattern
|
|
2213
|
-
* @returns {Object} Result containing match status and capture groups
|
|
2214
|
-
* @private
|
|
2215
|
-
*/
|
|
2216
|
-
const applyMatching = (pattern, string) => {
|
|
2217
|
-
const groups = [];
|
|
2218
|
-
let patternIndex = 0;
|
|
2219
|
-
let index = 0;
|
|
2220
|
-
let remainingPattern = pattern;
|
|
2221
|
-
let remainingString = string;
|
|
2222
|
-
let restoreIndexes = true;
|
|
2223
|
-
|
|
2224
|
-
const consumePattern = (count) => {
|
|
2225
|
-
const subpattern = remainingPattern.slice(0, count);
|
|
2226
|
-
remainingPattern = remainingPattern.slice(count);
|
|
2227
|
-
patternIndex += count;
|
|
2228
|
-
return subpattern;
|
|
2229
|
-
};
|
|
2230
|
-
const consumeString = (count) => {
|
|
2231
|
-
const substring = remainingString.slice(0, count);
|
|
2232
|
-
remainingString = remainingString.slice(count);
|
|
2233
|
-
index += count;
|
|
2234
|
-
return substring;
|
|
2235
|
-
};
|
|
2236
|
-
const consumeRemainingString = () => {
|
|
2237
|
-
return consumeString(remainingString.length);
|
|
2238
|
-
};
|
|
2239
|
-
|
|
2240
|
-
const matchOne = () => {
|
|
2241
|
-
// pattern consumed
|
|
2242
|
-
if (remainingPattern === "") {
|
|
2243
|
-
if (remainingString === "") {
|
|
2244
|
-
return true; // string fully matched pattern
|
|
2245
|
-
}
|
|
2246
|
-
if (remainingString[0] === "?") {
|
|
2247
|
-
// match search params
|
|
2248
|
-
consumeRemainingString();
|
|
2249
|
-
return true;
|
|
2250
|
-
}
|
|
2251
|
-
return false; // fails because string longer than expect
|
|
2252
|
-
}
|
|
2253
|
-
|
|
2254
|
-
// -- from this point pattern is not consumed --
|
|
2255
|
-
// string consumed, pattern not consumed
|
|
2256
|
-
if (remainingString === "") {
|
|
2257
|
-
if (remainingPattern === "**") {
|
|
2258
|
-
// trailing "**" is optional
|
|
2259
|
-
consumePattern(2);
|
|
2260
|
-
return true;
|
|
2261
|
-
}
|
|
2262
|
-
if (remainingPattern === "*") {
|
|
2263
|
-
groups.push({ string: "" });
|
|
2264
|
-
}
|
|
2265
|
-
return false; // fail because string shorter than expect
|
|
2266
|
-
}
|
|
2267
|
-
|
|
2268
|
-
// -- from this point pattern and string are not consumed --
|
|
2269
|
-
// fast path trailing slash
|
|
2270
|
-
if (remainingPattern === "/") {
|
|
2271
|
-
if (remainingString[0] === "/") {
|
|
2272
|
-
// trailing slash match remaining
|
|
2273
|
-
consumePattern(1);
|
|
2274
|
-
groups.push({ string: consumeRemainingString() });
|
|
2275
|
-
return true;
|
|
2276
|
-
}
|
|
2277
|
-
return false;
|
|
2278
|
-
}
|
|
2279
|
-
|
|
2280
|
-
// fast path trailing '**'
|
|
2281
|
-
if (remainingPattern === "**") {
|
|
2282
|
-
consumePattern(2);
|
|
2283
|
-
consumeRemainingString();
|
|
2284
|
-
return true;
|
|
2285
|
-
}
|
|
2286
|
-
|
|
2287
|
-
if (remainingPattern.slice(0, 4) === "/**/") {
|
|
2288
|
-
consumePattern(3); // consumes "/**/"
|
|
2289
|
-
const skipResult = skipUntilMatchIterative({
|
|
2290
|
-
pattern: remainingPattern,
|
|
2291
|
-
string: remainingString,
|
|
2292
|
-
canSkipSlash: true,
|
|
2293
|
-
});
|
|
2294
|
-
for (let i = 0; i < skipResult.groups.length; i++) {
|
|
2295
|
-
groups.push(skipResult.groups[i]);
|
|
2296
|
-
}
|
|
2297
|
-
consumePattern(skipResult.patternIndex);
|
|
2298
|
-
consumeRemainingString();
|
|
2299
|
-
restoreIndexes = false;
|
|
2300
|
-
return skipResult.matched;
|
|
2301
|
-
}
|
|
2302
|
-
|
|
2303
|
-
// pattern leading **
|
|
2304
|
-
if (remainingPattern.slice(0, 2) === "**") {
|
|
2305
|
-
consumePattern(2); // consumes "**"
|
|
2306
|
-
let skipAllowed = true;
|
|
2307
|
-
if (remainingPattern[0] === "/") {
|
|
2308
|
-
consumePattern(1); // consumes "/"
|
|
2309
|
-
// when remainingPattern was preceeded by "**/"
|
|
2310
|
-
// and remainingString have no "/"
|
|
2311
|
-
// then skip is not allowed, a regular match will be performed
|
|
2312
|
-
if (remainingString.includes("/")) {
|
|
2313
|
-
skipAllowed = false;
|
|
2314
|
-
}
|
|
2315
|
-
}
|
|
2316
|
-
// pattern ending with "**" or "**/" match remaining string
|
|
2317
|
-
if (remainingPattern === "") {
|
|
2318
|
-
consumeRemainingString();
|
|
2319
|
-
return true;
|
|
2320
|
-
}
|
|
2321
|
-
if (skipAllowed) {
|
|
2322
|
-
const skipResult = skipUntilMatchIterative({
|
|
2323
|
-
pattern: remainingPattern,
|
|
2324
|
-
string: remainingString,
|
|
2325
|
-
canSkipSlash: true,
|
|
2326
|
-
});
|
|
2327
|
-
for (let i = 0; i < skipResult.groups.length; i++) {
|
|
2328
|
-
groups.push(skipResult.groups[i]);
|
|
2329
|
-
}
|
|
2330
|
-
consumePattern(skipResult.patternIndex);
|
|
2331
|
-
consumeRemainingString();
|
|
2332
|
-
restoreIndexes = false;
|
|
2333
|
-
return skipResult.matched;
|
|
2334
|
-
}
|
|
2335
|
-
}
|
|
2336
|
-
|
|
2337
|
-
if (remainingPattern[0] === "*") {
|
|
2338
|
-
consumePattern(1); // consumes "*"
|
|
2339
|
-
if (remainingPattern === "") {
|
|
2340
|
-
// matches everything except "/"
|
|
2341
|
-
const slashIndex = remainingString.indexOf("/");
|
|
2342
|
-
if (slashIndex === -1) {
|
|
2343
|
-
groups.push({ string: consumeRemainingString() });
|
|
2344
|
-
return true;
|
|
2345
|
-
}
|
|
2346
|
-
groups.push({ string: consumeString(slashIndex) });
|
|
2347
|
-
return false;
|
|
2348
|
-
}
|
|
2349
|
-
// the next char must not the one expect by remainingPattern[0]
|
|
2350
|
-
// because * is greedy and expect to skip at least one char
|
|
2351
|
-
if (remainingPattern[0] === remainingString[0]) {
|
|
2352
|
-
groups.push({ string: "" });
|
|
2353
|
-
patternIndex = patternIndex - 1;
|
|
2354
|
-
return false;
|
|
2355
|
-
}
|
|
2356
|
-
const skipResult = skipUntilMatchIterative({
|
|
2357
|
-
pattern: remainingPattern,
|
|
2358
|
-
string: remainingString,
|
|
2359
|
-
canSkipSlash: false,
|
|
2360
|
-
});
|
|
2361
|
-
groups.push(skipResult.group);
|
|
2362
|
-
for (let i = 0; i < skipResult.groups.length; i++) {
|
|
2363
|
-
groups.push(skipResult.groups[i]);
|
|
2364
|
-
}
|
|
2365
|
-
consumePattern(skipResult.patternIndex);
|
|
2366
|
-
consumeString(skipResult.index);
|
|
2367
|
-
restoreIndexes = false;
|
|
2368
|
-
return skipResult.matched;
|
|
2369
|
-
}
|
|
2370
|
-
|
|
2371
|
-
if (remainingPattern[0] !== remainingString[0]) {
|
|
2372
|
-
return false;
|
|
2373
|
-
}
|
|
2374
|
-
|
|
2375
|
-
return undefined;
|
|
2376
|
-
};
|
|
2377
|
-
|
|
2378
|
-
// Replace recursive iterate() with iterative approach
|
|
2379
|
-
let matched;
|
|
2380
|
-
let continueIteration = true;
|
|
2381
|
-
|
|
2382
|
-
while (continueIteration) {
|
|
2383
|
-
const patternIndexBefore = patternIndex;
|
|
2384
|
-
const indexBefore = index;
|
|
2385
|
-
|
|
2386
|
-
matched = matchOne();
|
|
2387
|
-
|
|
2388
|
-
if (matched === undefined) {
|
|
2389
|
-
consumePattern(1);
|
|
2390
|
-
consumeString(1);
|
|
2391
|
-
// Continue the loop instead of recursion
|
|
2392
|
-
continue;
|
|
2393
|
-
}
|
|
2394
|
-
|
|
2395
|
-
if (matched === false && restoreIndexes) {
|
|
2396
|
-
patternIndex = patternIndexBefore;
|
|
2397
|
-
index = indexBefore;
|
|
2398
|
-
}
|
|
2399
|
-
|
|
2400
|
-
// End the loop
|
|
2401
|
-
continueIteration = false;
|
|
2402
|
-
}
|
|
2403
|
-
|
|
2404
|
-
return {
|
|
2405
|
-
matched,
|
|
2406
|
-
patternIndex,
|
|
2407
|
-
index,
|
|
2408
|
-
groups,
|
|
2409
|
-
};
|
|
2410
|
-
};
|
|
2411
|
-
|
|
2412
|
-
/**
|
|
2413
|
-
* Iterative version of skipUntilMatch that avoids recursion
|
|
2414
|
-
* @param {Object} params
|
|
2415
|
-
* @param {string} params.pattern - The pattern to match
|
|
2416
|
-
* @param {string} params.string - The string to test against
|
|
2417
|
-
* @param {boolean} params.canSkipSlash - Whether slash characters can be skipped
|
|
2418
|
-
* @returns {Object} Result of the matching attempt
|
|
2419
|
-
*/
|
|
2420
|
-
const skipUntilMatchIterative = ({ pattern, string, canSkipSlash }) => {
|
|
2421
|
-
let index = 0;
|
|
2422
|
-
let remainingString = string;
|
|
2423
|
-
let longestAttemptRange = null;
|
|
2424
|
-
let isLastAttempt = false;
|
|
2425
|
-
|
|
2426
|
-
const failure = () => {
|
|
2427
|
-
return {
|
|
2428
|
-
matched: false,
|
|
2429
|
-
patternIndex: longestAttemptRange.patternIndex,
|
|
2430
|
-
index: longestAttemptRange.index + longestAttemptRange.length,
|
|
2431
|
-
groups: longestAttemptRange.groups,
|
|
2432
|
-
group: {
|
|
2433
|
-
string: string.slice(0, longestAttemptRange.index),
|
|
2434
|
-
},
|
|
2435
|
-
};
|
|
2436
|
-
};
|
|
2437
|
-
|
|
2438
|
-
// Loop until a match is found or all attempts fail
|
|
2439
|
-
while (true) {
|
|
2440
|
-
const matchAttempt = applyMatching(pattern, remainingString);
|
|
2441
|
-
|
|
2442
|
-
if (matchAttempt.matched) {
|
|
2443
|
-
return {
|
|
2444
|
-
matched: true,
|
|
2445
|
-
patternIndex: matchAttempt.patternIndex,
|
|
2446
|
-
index: index + matchAttempt.index,
|
|
2447
|
-
groups: matchAttempt.groups,
|
|
2448
|
-
group: {
|
|
2449
|
-
string:
|
|
2450
|
-
remainingString === ""
|
|
2451
|
-
? string
|
|
2452
|
-
: string.slice(0, -remainingString.length),
|
|
2453
|
-
},
|
|
2454
|
-
};
|
|
2455
|
-
}
|
|
2456
|
-
|
|
2457
|
-
const attemptIndex = matchAttempt.index;
|
|
2458
|
-
const attemptRange = {
|
|
2459
|
-
patternIndex: matchAttempt.patternIndex,
|
|
2460
|
-
index,
|
|
2461
|
-
length: attemptIndex,
|
|
2462
|
-
groups: matchAttempt.groups,
|
|
2463
|
-
};
|
|
2464
|
-
|
|
2465
|
-
if (
|
|
2466
|
-
!longestAttemptRange ||
|
|
2467
|
-
longestAttemptRange.length < attemptRange.length
|
|
2468
|
-
) {
|
|
2469
|
-
longestAttemptRange = attemptRange;
|
|
2470
|
-
}
|
|
2471
|
-
|
|
2472
|
-
if (isLastAttempt) {
|
|
2473
|
-
return failure();
|
|
2474
|
-
}
|
|
2475
|
-
|
|
2476
|
-
const nextIndex = attemptIndex + 1;
|
|
2477
|
-
if (nextIndex >= remainingString.length) {
|
|
2478
|
-
return failure();
|
|
2479
|
-
}
|
|
2480
|
-
|
|
2481
|
-
if (remainingString[0] === "/") {
|
|
2482
|
-
if (!canSkipSlash) {
|
|
2483
|
-
return failure();
|
|
2484
|
-
}
|
|
2485
|
-
// when it's the last slash, the next attempt is the last
|
|
2486
|
-
if (remainingString.indexOf("/", 1) === -1) {
|
|
2487
|
-
isLastAttempt = true;
|
|
2488
|
-
}
|
|
2489
|
-
}
|
|
2490
|
-
|
|
2491
|
-
// search against the next unattempted string
|
|
2492
|
-
index += nextIndex;
|
|
2493
|
-
remainingString = remainingString.slice(nextIndex);
|
|
2494
|
-
}
|
|
2495
|
-
};
|
|
2496
|
-
|
|
2497
|
-
const applyPatternMatching = ({ url, pattern }) => {
|
|
2498
|
-
assertUrlLike(pattern, "pattern");
|
|
2499
|
-
if (url && typeof url.href === "string") url = url.href;
|
|
2500
|
-
assertUrlLike(url, "url");
|
|
2501
|
-
return applyPattern({ url, pattern });
|
|
2502
|
-
};
|
|
2503
|
-
|
|
2504
|
-
const resolveAssociations = (associations, resolver) => {
|
|
2505
|
-
let resolve = () => {};
|
|
2506
|
-
if (typeof resolver === "function") {
|
|
2507
|
-
resolve = resolver;
|
|
2508
|
-
} else if (typeof resolver === "string") {
|
|
2509
|
-
const baseUrl = resolver;
|
|
2510
|
-
assertUrlLike(baseUrl, "baseUrl");
|
|
2511
|
-
resolve = (pattern) => new URL(pattern, baseUrl).href;
|
|
2512
|
-
} else if (resolver && typeof resolver.href === "string") {
|
|
2513
|
-
const baseUrl = resolver.href;
|
|
2514
|
-
assertUrlLike(baseUrl, "baseUrl");
|
|
2515
|
-
resolve = (pattern) => new URL(pattern, baseUrl).href;
|
|
2516
|
-
}
|
|
2517
|
-
|
|
2518
|
-
const associationsResolved = {};
|
|
2519
|
-
for (const key of Object.keys(associations)) {
|
|
2520
|
-
const value = associations[key];
|
|
2521
|
-
if (typeof value === "object" && value !== null) {
|
|
2522
|
-
const valueMapResolved = {};
|
|
2523
|
-
for (const pattern of Object.keys(value)) {
|
|
2524
|
-
const valueAssociated = value[pattern];
|
|
2525
|
-
let patternResolved;
|
|
2526
|
-
try {
|
|
2527
|
-
patternResolved = resolve(pattern);
|
|
2528
|
-
} catch {
|
|
2529
|
-
// it's not really an url, no need to perform url resolution nor encoding
|
|
2530
|
-
patternResolved = pattern;
|
|
2531
|
-
}
|
|
2532
|
-
valueMapResolved[patternResolved] = valueAssociated;
|
|
2533
|
-
}
|
|
2534
|
-
associationsResolved[key] = valueMapResolved;
|
|
2535
|
-
} else {
|
|
2536
|
-
associationsResolved[key] = value;
|
|
2537
|
-
}
|
|
2538
|
-
}
|
|
2539
|
-
return associationsResolved;
|
|
2540
|
-
};
|
|
2541
|
-
|
|
2542
|
-
const asFlatAssociations = (associations) => {
|
|
2543
|
-
if (!isPlainObject(associations)) {
|
|
2544
|
-
throw new TypeError(
|
|
2545
|
-
`associations must be a plain object, got ${associations}`,
|
|
2546
|
-
);
|
|
2547
|
-
}
|
|
2548
|
-
const flatAssociations = {};
|
|
2549
|
-
for (const associationName of Object.keys(associations)) {
|
|
2550
|
-
const associationValue = associations[associationName];
|
|
2551
|
-
if (!isPlainObject(associationValue)) {
|
|
2552
|
-
continue;
|
|
2553
|
-
}
|
|
2554
|
-
for (const pattern of Object.keys(associationValue)) {
|
|
2555
|
-
const patternValue = associationValue[pattern];
|
|
2556
|
-
const previousValue = flatAssociations[pattern];
|
|
2557
|
-
if (isPlainObject(previousValue)) {
|
|
2558
|
-
flatAssociations[pattern] = {
|
|
2559
|
-
...previousValue,
|
|
2560
|
-
[associationName]: patternValue,
|
|
2561
|
-
};
|
|
2562
|
-
} else {
|
|
2563
|
-
flatAssociations[pattern] = {
|
|
2564
|
-
[associationName]: patternValue,
|
|
2565
|
-
};
|
|
2566
|
-
}
|
|
2567
|
-
}
|
|
2568
|
-
}
|
|
2569
|
-
return flatAssociations;
|
|
2570
|
-
};
|
|
2571
|
-
|
|
2572
|
-
const applyAssociations = ({ url, associations }) => {
|
|
2573
|
-
if (url && typeof url.href === "string") url = url.href;
|
|
2574
|
-
assertUrlLike(url);
|
|
2575
|
-
const flatAssociations = asFlatAssociations(associations);
|
|
2576
|
-
let associatedValue = {};
|
|
2577
|
-
for (const pattern of Object.keys(flatAssociations)) {
|
|
2578
|
-
const { matched } = applyPatternMatching({
|
|
2579
|
-
pattern,
|
|
2580
|
-
url,
|
|
2581
|
-
});
|
|
2582
|
-
if (matched) {
|
|
2583
|
-
const value = flatAssociations[pattern];
|
|
2584
|
-
associatedValue = deepAssign(associatedValue, value);
|
|
2585
|
-
}
|
|
2586
|
-
}
|
|
2587
|
-
return associatedValue;
|
|
2588
|
-
};
|
|
2589
|
-
|
|
2590
|
-
const deepAssign = (firstValue, secondValue) => {
|
|
2591
|
-
if (!isPlainObject(firstValue)) {
|
|
2592
|
-
if (isPlainObject(secondValue)) {
|
|
2593
|
-
return deepAssign({}, secondValue);
|
|
2594
|
-
}
|
|
2595
|
-
return secondValue;
|
|
2596
|
-
}
|
|
2597
|
-
if (!isPlainObject(secondValue)) {
|
|
2598
|
-
return secondValue;
|
|
2599
|
-
}
|
|
2600
|
-
for (const key of Object.keys(secondValue)) {
|
|
2601
|
-
const leftPopertyValue = firstValue[key];
|
|
2602
|
-
const rightPropertyValue = secondValue[key];
|
|
2603
|
-
firstValue[key] = deepAssign(leftPopertyValue, rightPropertyValue);
|
|
2604
|
-
}
|
|
2605
|
-
return firstValue;
|
|
2606
|
-
};
|
|
2607
|
-
|
|
2608
|
-
const urlChildMayMatch = ({ url, associations, predicate }) => {
|
|
2609
|
-
if (url && typeof url.href === "string") url = url.href;
|
|
2610
|
-
assertUrlLike(url, "url");
|
|
2611
|
-
// the function was meants to be used on url ending with '/'
|
|
2612
|
-
if (!url.endsWith("/")) {
|
|
2613
|
-
throw new Error(`url should end with /, got ${url}`);
|
|
2614
|
-
}
|
|
2615
|
-
if (typeof predicate !== "function") {
|
|
2616
|
-
throw new TypeError(`predicate must be a function, got ${predicate}`);
|
|
2617
|
-
}
|
|
2618
|
-
const flatAssociations = asFlatAssociations(associations);
|
|
2619
|
-
// for full match we must create an object to allow pattern to override previous ones
|
|
2620
|
-
let fullMatchMeta = {};
|
|
2621
|
-
let someFullMatch = false;
|
|
2622
|
-
// for partial match, any meta satisfying predicate will be valid because
|
|
2623
|
-
// we don't know for sure if pattern will still match for a file inside pathname
|
|
2624
|
-
const partialMatchMetaArray = [];
|
|
2625
|
-
for (const pattern of Object.keys(flatAssociations)) {
|
|
2626
|
-
const value = flatAssociations[pattern];
|
|
2627
|
-
const matchResult = applyPatternMatching({
|
|
2628
|
-
pattern,
|
|
2629
|
-
url,
|
|
2630
|
-
});
|
|
2631
|
-
if (matchResult.matched) {
|
|
2632
|
-
someFullMatch = true;
|
|
2633
|
-
if (isPlainObject(fullMatchMeta) && isPlainObject(value)) {
|
|
2634
|
-
fullMatchMeta = {
|
|
2635
|
-
...fullMatchMeta,
|
|
2636
|
-
...value,
|
|
2637
|
-
};
|
|
2638
|
-
} else {
|
|
2639
|
-
fullMatchMeta = value;
|
|
2640
|
-
}
|
|
2641
|
-
} else if (someFullMatch === false && matchResult.urlIndex >= url.length) {
|
|
2642
|
-
partialMatchMetaArray.push(value);
|
|
2643
|
-
}
|
|
2644
|
-
}
|
|
2645
|
-
if (someFullMatch) {
|
|
2646
|
-
return Boolean(predicate(fullMatchMeta));
|
|
2647
|
-
}
|
|
2648
|
-
return partialMatchMetaArray.some((partialMatchMeta) =>
|
|
2649
|
-
predicate(partialMatchMeta),
|
|
2650
|
-
);
|
|
2651
|
-
};
|
|
2652
|
-
|
|
2653
|
-
const applyAliases = ({ url, aliases }) => {
|
|
2654
|
-
for (const key of Object.keys(aliases)) {
|
|
2655
|
-
const matchResult = applyPatternMatching({
|
|
2656
|
-
pattern: key,
|
|
2657
|
-
url,
|
|
2658
|
-
});
|
|
2659
|
-
if (!matchResult.matched) {
|
|
2660
|
-
continue;
|
|
2661
|
-
}
|
|
2662
|
-
const { matchGroups } = matchResult;
|
|
2663
|
-
const alias = aliases[key];
|
|
2664
|
-
const parts = alias.split("*");
|
|
2665
|
-
let newUrl = "";
|
|
2666
|
-
let index = 0;
|
|
2667
|
-
for (const part of parts) {
|
|
2668
|
-
newUrl += `${part}`;
|
|
2669
|
-
if (index < parts.length - 1) {
|
|
2670
|
-
newUrl += matchGroups[index];
|
|
2671
|
-
}
|
|
2672
|
-
index++;
|
|
2673
|
-
}
|
|
2674
|
-
return newUrl;
|
|
2675
|
-
}
|
|
2676
|
-
return url;
|
|
2677
|
-
};
|
|
2678
|
-
|
|
2679
|
-
const matches = (url, patterns) => {
|
|
2680
|
-
return Boolean(
|
|
2681
|
-
applyAssociations({
|
|
2682
|
-
url,
|
|
2683
|
-
associations: {
|
|
2684
|
-
yes: patterns,
|
|
2685
|
-
},
|
|
2686
|
-
}).yes,
|
|
2687
|
-
);
|
|
2688
|
-
};
|
|
2689
|
-
|
|
2690
|
-
// const assertSpecifierMetaMap = (value, checkComposition = true) => {
|
|
2691
|
-
// if (!isPlainObject(value)) {
|
|
2692
|
-
// throw new TypeError(
|
|
2693
|
-
// `specifierMetaMap must be a plain object, got ${value}`,
|
|
2694
|
-
// );
|
|
2695
|
-
// }
|
|
2696
|
-
// if (checkComposition) {
|
|
2697
|
-
// const plainObject = value;
|
|
2698
|
-
// Object.keys(plainObject).forEach((key) => {
|
|
2699
|
-
// assertUrlLike(key, "specifierMetaMap key");
|
|
2700
|
-
// const value = plainObject[key];
|
|
2701
|
-
// if (value !== null && !isPlainObject(value)) {
|
|
2702
|
-
// throw new TypeError(
|
|
2703
|
-
// `specifierMetaMap value must be a plain object or null, got ${value} under key ${key}`,
|
|
2704
|
-
// );
|
|
2705
|
-
// }
|
|
2706
|
-
// });
|
|
2707
|
-
// }
|
|
2708
|
-
// };
|
|
2709
|
-
const assertUrlLike = (value, name = "url") => {
|
|
2710
|
-
if (typeof value !== "string") {
|
|
2711
|
-
throw new TypeError(`${name} must be a url string, got ${value}`);
|
|
2712
|
-
}
|
|
2713
|
-
if (isWindowsPathnameSpecifier(value)) {
|
|
2714
|
-
throw new TypeError(
|
|
2715
|
-
`${name} must be a url but looks like a windows pathname, got ${value}`,
|
|
2716
|
-
);
|
|
2717
|
-
}
|
|
2718
|
-
if (!hasScheme(value)) {
|
|
2719
|
-
throw new TypeError(
|
|
2720
|
-
`${name} must be a url and no scheme found, got ${value}`,
|
|
2721
|
-
);
|
|
2722
|
-
}
|
|
2723
|
-
};
|
|
2724
|
-
const isPlainObject = (value) => {
|
|
2725
|
-
if (value === null) {
|
|
2726
|
-
return false;
|
|
2727
|
-
}
|
|
2728
|
-
if (typeof value === "object") {
|
|
2729
|
-
if (Array.isArray(value)) {
|
|
2730
|
-
return false;
|
|
2731
|
-
}
|
|
2732
|
-
return true;
|
|
2733
|
-
}
|
|
2734
|
-
return false;
|
|
2735
|
-
};
|
|
2736
|
-
const isWindowsPathnameSpecifier = (specifier) => {
|
|
2737
|
-
const firstChar = specifier[0];
|
|
2738
|
-
if (!/[a-zA-Z]/.test(firstChar)) return false;
|
|
2739
|
-
const secondChar = specifier[1];
|
|
2740
|
-
if (secondChar !== ":") return false;
|
|
2741
|
-
const thirdChar = specifier[2];
|
|
2742
|
-
return thirdChar === "/" || thirdChar === "\\";
|
|
2743
|
-
};
|
|
2744
|
-
const hasScheme = (specifier) => /^[a-zA-Z]+:/.test(specifier);
|
|
2745
|
-
|
|
2746
|
-
const createFilter = (patterns, url, map = (v) => v) => {
|
|
2747
|
-
const associations = resolveAssociations(
|
|
2748
|
-
{
|
|
2749
|
-
yes: patterns,
|
|
2750
|
-
},
|
|
2751
|
-
url,
|
|
2752
|
-
);
|
|
2753
|
-
return (url) => {
|
|
2754
|
-
const meta = applyAssociations({ url, associations });
|
|
2755
|
-
return Boolean(map(meta.yes));
|
|
2756
|
-
};
|
|
2757
|
-
};
|
|
2758
|
-
|
|
2759
|
-
const URL_META = {
|
|
2760
|
-
resolveAssociations,
|
|
2761
|
-
applyAssociations,
|
|
2762
|
-
applyAliases,
|
|
2763
|
-
applyPatternMatching,
|
|
2764
|
-
urlChildMayMatch,
|
|
2765
|
-
matches,
|
|
2766
|
-
createFilter,
|
|
2767
|
-
};
|
|
2768
|
-
|
|
2769
2162
|
const readDirectory = async (url, { emfileMaxWait = 1000 } = {}) => {
|
|
2770
2163
|
const directoryUrl = assertAndNormalizeDirectoryUrl(url);
|
|
2771
2164
|
const directoryUrlObject = new URL(directoryUrl);
|
|
@@ -3737,98 +3130,1488 @@ const removeDirectory = async (
|
|
|
3737
3130
|
await removeNonDirectory(symbolicLinkUrl, { maxRetries, retryDelay });
|
|
3738
3131
|
};
|
|
3739
3132
|
|
|
3740
|
-
try {
|
|
3741
|
-
if (onlyContent) {
|
|
3742
|
-
await removeDirectoryContent(rootDirectoryUrl);
|
|
3743
|
-
} else {
|
|
3744
|
-
await visitDirectory(rootDirectoryUrl);
|
|
3133
|
+
try {
|
|
3134
|
+
if (onlyContent) {
|
|
3135
|
+
await removeDirectoryContent(rootDirectoryUrl);
|
|
3136
|
+
} else {
|
|
3137
|
+
await visitDirectory(rootDirectoryUrl);
|
|
3138
|
+
}
|
|
3139
|
+
} finally {
|
|
3140
|
+
await removeDirectoryOperation.end();
|
|
3141
|
+
}
|
|
3142
|
+
};
|
|
3143
|
+
|
|
3144
|
+
const removeDirectoryNaive = (
|
|
3145
|
+
directoryPath,
|
|
3146
|
+
{ handleNotEmptyError = null, handlePermissionError = null } = {},
|
|
3147
|
+
) => {
|
|
3148
|
+
return new Promise((resolve, reject) => {
|
|
3149
|
+
rmdir(directoryPath, (error, lstatObject) => {
|
|
3150
|
+
if (error) {
|
|
3151
|
+
if (handlePermissionError && error.code === "EPERM") {
|
|
3152
|
+
resolve(handlePermissionError(error));
|
|
3153
|
+
} else if (error.code === "ENOENT") {
|
|
3154
|
+
resolve();
|
|
3155
|
+
} else if (
|
|
3156
|
+
handleNotEmptyError &&
|
|
3157
|
+
// linux os
|
|
3158
|
+
(error.code === "ENOTEMPTY" ||
|
|
3159
|
+
// SunOS
|
|
3160
|
+
error.code === "EEXIST")
|
|
3161
|
+
) {
|
|
3162
|
+
resolve(handleNotEmptyError(error));
|
|
3163
|
+
} else {
|
|
3164
|
+
reject(error);
|
|
3165
|
+
}
|
|
3166
|
+
} else {
|
|
3167
|
+
resolve(lstatObject);
|
|
3168
|
+
}
|
|
3169
|
+
});
|
|
3170
|
+
});
|
|
3171
|
+
};
|
|
3172
|
+
|
|
3173
|
+
const ensureEmptyDirectorySync = (source) => {
|
|
3174
|
+
const stat = readEntryStatSync(source, {
|
|
3175
|
+
nullIfNotFound: true,
|
|
3176
|
+
followLink: false,
|
|
3177
|
+
});
|
|
3178
|
+
|
|
3179
|
+
if (stat === null) {
|
|
3180
|
+
// if there is nothing, create a directory
|
|
3181
|
+
writeDirectorySync(source, { allowUseless: true });
|
|
3182
|
+
return;
|
|
3183
|
+
}
|
|
3184
|
+
if (stat.isDirectory()) {
|
|
3185
|
+
removeEntrySync(source, {
|
|
3186
|
+
recursive: true,
|
|
3187
|
+
onlyContent: true,
|
|
3188
|
+
allowUseless: true,
|
|
3189
|
+
});
|
|
3190
|
+
return;
|
|
3191
|
+
}
|
|
3192
|
+
|
|
3193
|
+
const sourceType = statsToType(stat);
|
|
3194
|
+
const sourcePath = urlToFileSystemPath(assertAndNormalizeFileUrl(source));
|
|
3195
|
+
throw new Error(
|
|
3196
|
+
`ensureEmptyDirectorySync expect directory at ${sourcePath}, found ${sourceType} instead`,
|
|
3197
|
+
);
|
|
3198
|
+
};
|
|
3199
|
+
|
|
3200
|
+
const ensureEmptyDirectory = async (source) => {
|
|
3201
|
+
const stats = await readEntryStat(source, {
|
|
3202
|
+
nullIfNotFound: true,
|
|
3203
|
+
followLink: false,
|
|
3204
|
+
});
|
|
3205
|
+
if (stats === null) {
|
|
3206
|
+
// if there is nothing, create a directory
|
|
3207
|
+
await writeDirectory(source, { allowUseless: true });
|
|
3208
|
+
return;
|
|
3209
|
+
}
|
|
3210
|
+
if (stats.isDirectory()) {
|
|
3211
|
+
// if there is a directory remove its content and done
|
|
3212
|
+
await removeEntry(source, {
|
|
3213
|
+
allowUseless: true,
|
|
3214
|
+
recursive: true,
|
|
3215
|
+
onlyContent: true,
|
|
3216
|
+
});
|
|
3217
|
+
return;
|
|
3218
|
+
}
|
|
3219
|
+
|
|
3220
|
+
const sourceType = statsToType(stats);
|
|
3221
|
+
const sourcePath = urlToFileSystemPath(assertAndNormalizeFileUrl(source));
|
|
3222
|
+
throw new Error(
|
|
3223
|
+
`ensureEmptyDirectory expect directory at ${sourcePath}, found ${sourceType} instead`,
|
|
3224
|
+
);
|
|
3225
|
+
};
|
|
3226
|
+
|
|
3227
|
+
const POST = ({ url, body, githubToken, headers }) => {
|
|
3228
|
+
return sendHttpRequest({
|
|
3229
|
+
url,
|
|
3230
|
+
method: "POST",
|
|
3231
|
+
headers: {
|
|
3232
|
+
...tokenToHeaders(githubToken),
|
|
3233
|
+
"accept": "application/vnd.github+json",
|
|
3234
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
3235
|
+
...headers,
|
|
3236
|
+
},
|
|
3237
|
+
body: JSON.stringify(body),
|
|
3238
|
+
responseStatusHandlers: {
|
|
3239
|
+
201: async (response) => {
|
|
3240
|
+
const json = await response.json();
|
|
3241
|
+
return json;
|
|
3242
|
+
},
|
|
3243
|
+
},
|
|
3244
|
+
});
|
|
3245
|
+
};
|
|
3246
|
+
|
|
3247
|
+
const PATCH = ({ signal, url, body, githubToken, headers }) => {
|
|
3248
|
+
return sendHttpRequest({
|
|
3249
|
+
signal,
|
|
3250
|
+
url,
|
|
3251
|
+
method: "PATCH",
|
|
3252
|
+
headers: {
|
|
3253
|
+
...tokenToHeaders(githubToken),
|
|
3254
|
+
"accept": "application/vnd.github+json",
|
|
3255
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
3256
|
+
...headers,
|
|
3257
|
+
},
|
|
3258
|
+
body: JSON.stringify(body),
|
|
3259
|
+
responseStatusHandlers: {
|
|
3260
|
+
200: async (response) => {
|
|
3261
|
+
const json = await response.json();
|
|
3262
|
+
return json;
|
|
3263
|
+
},
|
|
3264
|
+
},
|
|
3265
|
+
});
|
|
3266
|
+
};
|
|
3267
|
+
|
|
3268
|
+
const tokenToHeaders = (token) => {
|
|
3269
|
+
if (!token) {
|
|
3270
|
+
throw new Error(`missing token, request will not be authorized.`);
|
|
3271
|
+
}
|
|
3272
|
+
return {
|
|
3273
|
+
authorization: `token ${token}`,
|
|
3274
|
+
};
|
|
3275
|
+
};
|
|
3276
|
+
|
|
3277
|
+
const sendHttpRequest = async ({
|
|
3278
|
+
signal,
|
|
3279
|
+
url,
|
|
3280
|
+
method,
|
|
3281
|
+
headers,
|
|
3282
|
+
body,
|
|
3283
|
+
responseStatusHandlers = {},
|
|
3284
|
+
}) => {
|
|
3285
|
+
let response;
|
|
3286
|
+
try {
|
|
3287
|
+
response = await fetch(url, {
|
|
3288
|
+
signal,
|
|
3289
|
+
method,
|
|
3290
|
+
headers: {
|
|
3291
|
+
...(typeof body === "undefined"
|
|
3292
|
+
? {}
|
|
3293
|
+
: { "content-length": Buffer.byteLength(body) }),
|
|
3294
|
+
...headers,
|
|
3295
|
+
},
|
|
3296
|
+
body,
|
|
3297
|
+
});
|
|
3298
|
+
} catch (error) {
|
|
3299
|
+
throw new Error(`network error during request.
|
|
3300
|
+
--- request method ---
|
|
3301
|
+
${method}
|
|
3302
|
+
--- request url ---
|
|
3303
|
+
${url}
|
|
3304
|
+
--- error stack ---
|
|
3305
|
+
${error.stack}`);
|
|
3306
|
+
}
|
|
3307
|
+
|
|
3308
|
+
const { status } = response;
|
|
3309
|
+
const responseStatusHandler = responseStatusHandlers[status];
|
|
3310
|
+
if (responseStatusHandler) {
|
|
3311
|
+
return responseStatusHandler(response);
|
|
3312
|
+
}
|
|
3313
|
+
const responseBodyAsJson = await response.json();
|
|
3314
|
+
const error = new Error(`unexpected response status.
|
|
3315
|
+
--- response status ---
|
|
3316
|
+
${response.status}
|
|
3317
|
+
--- response statusText ---
|
|
3318
|
+
${response.statusText}
|
|
3319
|
+
--- expect response status ---
|
|
3320
|
+
${Object.keys(responseStatusHandlers).join(", ")}
|
|
3321
|
+
--- request method ---
|
|
3322
|
+
${method}
|
|
3323
|
+
--- request url ---
|
|
3324
|
+
${url}
|
|
3325
|
+
--- response json ---
|
|
3326
|
+
${(JSON.stringify(responseBodyAsJson), " ")}`);
|
|
3327
|
+
error.responseStatus = status;
|
|
3328
|
+
throw error;
|
|
3329
|
+
};
|
|
3330
|
+
|
|
3331
|
+
/**
|
|
3332
|
+
*
|
|
3333
|
+
* https://github.com/IgnusG/jest-report-action/blob/c006b890ba3c3b650e6c55916a643ca82b64133b/tasks/github-api.js#L12
|
|
3334
|
+
* https://docs.github.com/fr/rest/checks/runs?apiVersion=2022-11-28#create-a-check-run
|
|
3335
|
+
*/
|
|
3336
|
+
|
|
3337
|
+
|
|
3338
|
+
const startGithubCheckRun = async ({
|
|
3339
|
+
logLevel,
|
|
3340
|
+
githubToken,
|
|
3341
|
+
repositoryOwner,
|
|
3342
|
+
repositoryName,
|
|
3343
|
+
commitSha,
|
|
3344
|
+
checkName,
|
|
3345
|
+
checkTitle,
|
|
3346
|
+
checkSummary,
|
|
3347
|
+
checkStatus = "in_progress",
|
|
3348
|
+
}) => {
|
|
3349
|
+
if (typeof githubToken !== "string") {
|
|
3350
|
+
throw new TypeError(
|
|
3351
|
+
`githubToken must be a string but received ${githubToken}`,
|
|
3352
|
+
);
|
|
3353
|
+
}
|
|
3354
|
+
if (typeof repositoryOwner !== "string") {
|
|
3355
|
+
throw new TypeError(
|
|
3356
|
+
`repositoryOwner must be a string but received ${repositoryOwner}`,
|
|
3357
|
+
);
|
|
3358
|
+
}
|
|
3359
|
+
if (typeof repositoryName !== "string") {
|
|
3360
|
+
throw new TypeError(
|
|
3361
|
+
`repositoryName must be a string but received ${repositoryName}`,
|
|
3362
|
+
);
|
|
3363
|
+
}
|
|
3364
|
+
if (typeof commitSha !== "string") {
|
|
3365
|
+
throw new TypeError(`commitSha must be a string but received ${commitSha}`);
|
|
3366
|
+
}
|
|
3367
|
+
if (typeof checkName !== "string") {
|
|
3368
|
+
throw new TypeError(`checkName must be a string but received ${checkName}`);
|
|
3369
|
+
}
|
|
3370
|
+
|
|
3371
|
+
const logger = createLogger({ logLevel });
|
|
3372
|
+
|
|
3373
|
+
const checkApiURL = `https://api.github.com/repos/${repositoryOwner}/${repositoryName}/check-runs`;
|
|
3374
|
+
logger.debug(`POST ${checkApiURL} (for commit ${commitSha})`);
|
|
3375
|
+
let check;
|
|
3376
|
+
try {
|
|
3377
|
+
check = await POST({
|
|
3378
|
+
url: checkApiURL,
|
|
3379
|
+
githubToken,
|
|
3380
|
+
headers: {
|
|
3381
|
+
"accept": "application/vnd.github+json",
|
|
3382
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
3383
|
+
},
|
|
3384
|
+
body: {
|
|
3385
|
+
head_sha: commitSha,
|
|
3386
|
+
status: checkStatus,
|
|
3387
|
+
name: checkName,
|
|
3388
|
+
output: {
|
|
3389
|
+
title: checkTitle,
|
|
3390
|
+
summary: checkSummary,
|
|
3391
|
+
},
|
|
3392
|
+
},
|
|
3393
|
+
});
|
|
3394
|
+
logger.debug(`${UNICODE.OK} check created ${check.html_url}`);
|
|
3395
|
+
} catch (e) {
|
|
3396
|
+
logger.error(
|
|
3397
|
+
createDetailedMessage(`${UNICODE.FAILURE} failed to create check`, {
|
|
3398
|
+
"error stack": e.stack,
|
|
3399
|
+
}),
|
|
3400
|
+
);
|
|
3401
|
+
return { progress: () => {}, fail: () => {}, pass: () => {} };
|
|
3402
|
+
}
|
|
3403
|
+
|
|
3404
|
+
const updateState = async ({
|
|
3405
|
+
status,
|
|
3406
|
+
conclusion,
|
|
3407
|
+
title = check.title,
|
|
3408
|
+
summary = check.summary,
|
|
3409
|
+
annotations = [],
|
|
3410
|
+
}) => {
|
|
3411
|
+
let annotationsSent = 0;
|
|
3412
|
+
const annotationsBatch = annotations.slice(annotationsSent, 50);
|
|
3413
|
+
const body = {
|
|
3414
|
+
...(status ? { status } : {}),
|
|
3415
|
+
...(conclusion ? { conclusion } : {}),
|
|
3416
|
+
output: {
|
|
3417
|
+
title: title === undefined ? check.title : title,
|
|
3418
|
+
summary: summary === undefined ? check.summary : summary,
|
|
3419
|
+
...(annotationsBatch.length ? { annotations: annotationsBatch } : {}),
|
|
3420
|
+
},
|
|
3421
|
+
};
|
|
3422
|
+
|
|
3423
|
+
try {
|
|
3424
|
+
logger.debug(`PATCH check ${check.html_url}
|
|
3425
|
+
--- body ---
|
|
3426
|
+
${JSON.stringify(body, null, " ")}`);
|
|
3427
|
+
check = await PATCH({
|
|
3428
|
+
url: check.url,
|
|
3429
|
+
githubToken,
|
|
3430
|
+
body,
|
|
3431
|
+
});
|
|
3432
|
+
logger.debug(`${UNICODE.OK} check updated`);
|
|
3433
|
+
} catch (e) {
|
|
3434
|
+
logger.error(
|
|
3435
|
+
createDetailedMessage(`${UNICODE.FAILURE} failed to update check`, {
|
|
3436
|
+
"error stack": e.stack,
|
|
3437
|
+
}),
|
|
3438
|
+
);
|
|
3439
|
+
return;
|
|
3440
|
+
}
|
|
3441
|
+
|
|
3442
|
+
annotationsSent += annotationsBatch.length;
|
|
3443
|
+
while (annotationsSent < annotations.length) {
|
|
3444
|
+
const annotationsBatch = annotations.slice(annotationsSent, 50);
|
|
3445
|
+
check = await PATCH({
|
|
3446
|
+
url: check.url,
|
|
3447
|
+
githubToken,
|
|
3448
|
+
body: {
|
|
3449
|
+
head_sha: commitSha,
|
|
3450
|
+
output: {
|
|
3451
|
+
annotations: annotationsBatch,
|
|
3452
|
+
},
|
|
3453
|
+
},
|
|
3454
|
+
});
|
|
3455
|
+
annotationsSent += annotationsBatch.length;
|
|
3456
|
+
}
|
|
3457
|
+
};
|
|
3458
|
+
|
|
3459
|
+
let lastProgressCall;
|
|
3460
|
+
let pendingAnnotations = [];
|
|
3461
|
+
let pendingAbortController;
|
|
3462
|
+
let pendingProgressPromise = Promise.resolve();
|
|
3463
|
+
let msBetweenProgressCalls = 500;
|
|
3464
|
+
|
|
3465
|
+
return {
|
|
3466
|
+
progress: async ({ title, summary, annotations = [] }) => {
|
|
3467
|
+
if (check.conclusion === "failure") {
|
|
3468
|
+
throw new Error(`cannot progress() after fail()`);
|
|
3469
|
+
}
|
|
3470
|
+
if (check.conclusion === "success") {
|
|
3471
|
+
throw new Error(`cannot progress() after pass()`);
|
|
3472
|
+
}
|
|
3473
|
+
pendingProgressPromise = (async () => {
|
|
3474
|
+
const nowMs = Date.now();
|
|
3475
|
+
const isFirstCall = !lastProgressCall;
|
|
3476
|
+
lastProgressCall = nowMs;
|
|
3477
|
+
if (isFirstCall) {
|
|
3478
|
+
await updateState({
|
|
3479
|
+
title,
|
|
3480
|
+
summary,
|
|
3481
|
+
annotations,
|
|
3482
|
+
});
|
|
3483
|
+
return;
|
|
3484
|
+
}
|
|
3485
|
+
if (pendingAbortController) {
|
|
3486
|
+
pendingAbortController.abort();
|
|
3487
|
+
}
|
|
3488
|
+
const msEllapsedSinceLastProgressCall = nowMs - lastProgressCall;
|
|
3489
|
+
const msEllapsedIsBigEnough =
|
|
3490
|
+
msEllapsedSinceLastProgressCall > msBetweenProgressCalls;
|
|
3491
|
+
if (msEllapsedIsBigEnough) {
|
|
3492
|
+
annotations = [...pendingAnnotations, ...annotations];
|
|
3493
|
+
pendingAnnotations.length = 0;
|
|
3494
|
+
await updateState({
|
|
3495
|
+
title,
|
|
3496
|
+
summary,
|
|
3497
|
+
annotations,
|
|
3498
|
+
});
|
|
3499
|
+
return;
|
|
3500
|
+
}
|
|
3501
|
+
pendingAnnotations.push(...annotations);
|
|
3502
|
+
pendingAbortController = new AbortController();
|
|
3503
|
+
await new Promise((resolve) => {
|
|
3504
|
+
pendingAbortController.signal.onabort = resolve;
|
|
3505
|
+
setTimeout(resolve, msBetweenProgressCalls);
|
|
3506
|
+
});
|
|
3507
|
+
if (pendingAbortController && pendingAbortController.signal.aborted) {
|
|
3508
|
+
return;
|
|
3509
|
+
}
|
|
3510
|
+
pendingAbortController = null;
|
|
3511
|
+
await updateState({
|
|
3512
|
+
title,
|
|
3513
|
+
summary,
|
|
3514
|
+
annotations,
|
|
3515
|
+
});
|
|
3516
|
+
})();
|
|
3517
|
+
await pendingProgressPromise;
|
|
3518
|
+
pendingProgressPromise = Promise.resolve();
|
|
3519
|
+
},
|
|
3520
|
+
fail: async ({ title, summary, annotations } = {}) => {
|
|
3521
|
+
await pendingProgressPromise;
|
|
3522
|
+
if (pendingAbortController) {
|
|
3523
|
+
pendingAbortController.abort();
|
|
3524
|
+
}
|
|
3525
|
+
if (check.conclusion === "failure") {
|
|
3526
|
+
throw new Error(`already failed`);
|
|
3527
|
+
}
|
|
3528
|
+
if (check.conclusion === "success") {
|
|
3529
|
+
throw new Error(`cannot fail() after pass()`);
|
|
3530
|
+
}
|
|
3531
|
+
return updateState({
|
|
3532
|
+
status: "completed",
|
|
3533
|
+
conclusion: "failure",
|
|
3534
|
+
title,
|
|
3535
|
+
summary,
|
|
3536
|
+
annotations,
|
|
3537
|
+
});
|
|
3538
|
+
},
|
|
3539
|
+
pass: async ({ title, summary, annotations } = {}) => {
|
|
3540
|
+
await pendingProgressPromise;
|
|
3541
|
+
if (pendingAbortController) {
|
|
3542
|
+
pendingAbortController.abort();
|
|
3543
|
+
}
|
|
3544
|
+
if (check.conclusion === "failure") {
|
|
3545
|
+
throw new Error(`cannot pass() after fail()`);
|
|
3546
|
+
}
|
|
3547
|
+
if (check.conclusion === "success") {
|
|
3548
|
+
throw new Error(`already passed`);
|
|
3549
|
+
}
|
|
3550
|
+
return updateState({
|
|
3551
|
+
status: "completed",
|
|
3552
|
+
conclusion: "success",
|
|
3553
|
+
title,
|
|
3554
|
+
summary,
|
|
3555
|
+
annotations,
|
|
3556
|
+
});
|
|
3557
|
+
},
|
|
3558
|
+
};
|
|
3559
|
+
};
|
|
3560
|
+
|
|
3561
|
+
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
|
|
3562
|
+
const readGitHubWorkflowEnv = () => {
|
|
3563
|
+
const eventName = process.env.GITHUB_EVENT_NAME;
|
|
3564
|
+
if (!eventName) {
|
|
3565
|
+
throw new Error(
|
|
3566
|
+
`missing process.env.GITHUB_EVENT_NAME, we are not in a github workflow`,
|
|
3567
|
+
);
|
|
3568
|
+
}
|
|
3569
|
+
const githubToken = process.env.GITHUB_TOKEN;
|
|
3570
|
+
if (!githubToken) {
|
|
3571
|
+
throw new Error(`missing githubToken`);
|
|
3572
|
+
}
|
|
3573
|
+
const githubRepository = process.env.GITHUB_REPOSITORY;
|
|
3574
|
+
if (!githubRepository) {
|
|
3575
|
+
throw new Error(`missing process.env.GITHUB_REPOSITORY`);
|
|
3576
|
+
}
|
|
3577
|
+
const [repositoryOwner, repositoryName] = githubRepository.split("/");
|
|
3578
|
+
return {
|
|
3579
|
+
rootDirectoryUrl: process.env.GITHUB_WORKSPACE,
|
|
3580
|
+
githubToken,
|
|
3581
|
+
repositoryOwner,
|
|
3582
|
+
repositoryName,
|
|
3583
|
+
commitSha: process.env.GITHUB_SHA,
|
|
3584
|
+
};
|
|
3585
|
+
};
|
|
3586
|
+
|
|
3587
|
+
// https://nodejs.org/api/packages.html#resolving-user-conditions
|
|
3588
|
+
const readCustomConditionsFromProcessArgs = () => {
|
|
3589
|
+
if (process.env.IGNORE_PACKAGE_CONDITIONS) {
|
|
3590
|
+
return [];
|
|
3591
|
+
}
|
|
3592
|
+
const packageConditions = [];
|
|
3593
|
+
for (const arg of process.execArgv) {
|
|
3594
|
+
if (arg.includes("-C=")) {
|
|
3595
|
+
const packageCondition = arg.slice(0, "-C=".length);
|
|
3596
|
+
packageConditions.push(packageCondition);
|
|
3597
|
+
}
|
|
3598
|
+
if (arg.includes("--conditions=")) {
|
|
3599
|
+
const packageCondition = arg.slice("--conditions=".length);
|
|
3600
|
+
packageConditions.push(packageCondition);
|
|
3601
|
+
}
|
|
3602
|
+
}
|
|
3603
|
+
return packageConditions;
|
|
3604
|
+
};
|
|
3605
|
+
|
|
3606
|
+
const asDirectoryUrl = (url) => {
|
|
3607
|
+
const { pathname } = new URL(url);
|
|
3608
|
+
if (pathname.endsWith("/")) {
|
|
3609
|
+
return url;
|
|
3610
|
+
}
|
|
3611
|
+
return new URL("./", url).href;
|
|
3612
|
+
};
|
|
3613
|
+
|
|
3614
|
+
const getParentUrl = (url) => {
|
|
3615
|
+
if (url.startsWith("file://")) {
|
|
3616
|
+
// With node.js new URL('../', 'file:///C:/').href
|
|
3617
|
+
// returns "file:///C:/" instead of "file:///"
|
|
3618
|
+
const resource = url.slice("file://".length);
|
|
3619
|
+
const slashLastIndex = resource.lastIndexOf("/");
|
|
3620
|
+
if (slashLastIndex === -1) {
|
|
3621
|
+
return url;
|
|
3622
|
+
}
|
|
3623
|
+
const lastCharIndex = resource.length - 1;
|
|
3624
|
+
if (slashLastIndex === lastCharIndex) {
|
|
3625
|
+
const slashBeforeLastIndex = resource.lastIndexOf(
|
|
3626
|
+
"/",
|
|
3627
|
+
slashLastIndex - 1,
|
|
3628
|
+
);
|
|
3629
|
+
if (slashBeforeLastIndex === -1) {
|
|
3630
|
+
return url;
|
|
3631
|
+
}
|
|
3632
|
+
return `file://${resource.slice(0, slashBeforeLastIndex + 1)}`;
|
|
3633
|
+
}
|
|
3634
|
+
|
|
3635
|
+
return `file://${resource.slice(0, slashLastIndex + 1)}`;
|
|
3636
|
+
}
|
|
3637
|
+
return new URL(url.endsWith("/") ? "../" : "./", url).href;
|
|
3638
|
+
};
|
|
3639
|
+
|
|
3640
|
+
const isValidUrl = (url) => {
|
|
3641
|
+
try {
|
|
3642
|
+
// eslint-disable-next-line no-new
|
|
3643
|
+
new URL(url);
|
|
3644
|
+
return true;
|
|
3645
|
+
} catch {
|
|
3646
|
+
return false;
|
|
3647
|
+
}
|
|
3648
|
+
};
|
|
3649
|
+
|
|
3650
|
+
const defaultLookupPackageScope = (url) => {
|
|
3651
|
+
let scopeUrl = asDirectoryUrl(url);
|
|
3652
|
+
while (scopeUrl !== "file:///") {
|
|
3653
|
+
if (scopeUrl.endsWith("node_modules/")) {
|
|
3654
|
+
return null;
|
|
3655
|
+
}
|
|
3656
|
+
const packageJsonUrlObject = new URL("package.json", scopeUrl);
|
|
3657
|
+
if (existsSync(packageJsonUrlObject)) {
|
|
3658
|
+
return scopeUrl;
|
|
3659
|
+
}
|
|
3660
|
+
scopeUrl = getParentUrl(scopeUrl);
|
|
3661
|
+
}
|
|
3662
|
+
return null;
|
|
3663
|
+
};
|
|
3664
|
+
|
|
3665
|
+
const defaultReadPackageJson = (packageUrl) => {
|
|
3666
|
+
const packageJsonUrl = new URL("package.json", packageUrl);
|
|
3667
|
+
const buffer = readFileSync(packageJsonUrl);
|
|
3668
|
+
const string = String(buffer);
|
|
3669
|
+
try {
|
|
3670
|
+
return JSON.parse(string);
|
|
3671
|
+
} catch {
|
|
3672
|
+
throw new Error(`Invalid package configuration`);
|
|
3673
|
+
}
|
|
3674
|
+
};
|
|
3675
|
+
|
|
3676
|
+
// https://github.com/nodejs/node/blob/0367b5c35ea0f98b323175a4aaa8e651af7a91e7/tools/node_modules/eslint/node_modules/%40babel/core/lib/vendor/import-meta-resolve.js#L2473
|
|
3677
|
+
|
|
3678
|
+
const createInvalidModuleSpecifierError = (
|
|
3679
|
+
reason,
|
|
3680
|
+
specifier,
|
|
3681
|
+
{ parentUrl },
|
|
3682
|
+
) => {
|
|
3683
|
+
const error = new Error(
|
|
3684
|
+
`Invalid module "${specifier}" ${reason} imported from ${fileURLToPath(
|
|
3685
|
+
parentUrl,
|
|
3686
|
+
)}`,
|
|
3687
|
+
);
|
|
3688
|
+
error.code = "INVALID_MODULE_SPECIFIER";
|
|
3689
|
+
return error;
|
|
3690
|
+
};
|
|
3691
|
+
|
|
3692
|
+
const createInvalidPackageTargetError = (
|
|
3693
|
+
reason,
|
|
3694
|
+
target,
|
|
3695
|
+
{ parentUrl, packageDirectoryUrl, key, isImport },
|
|
3696
|
+
) => {
|
|
3697
|
+
let message;
|
|
3698
|
+
if (key === ".") {
|
|
3699
|
+
message = `Invalid "exports" main target defined in ${fileURLToPath(
|
|
3700
|
+
packageDirectoryUrl,
|
|
3701
|
+
)}package.json imported from ${fileURLToPath(parentUrl)}; ${reason}`;
|
|
3702
|
+
} else {
|
|
3703
|
+
message = `Invalid "${
|
|
3704
|
+
isImport ? "imports" : "exports"
|
|
3705
|
+
}" target ${JSON.stringify(target)} defined for "${key}" in ${fileURLToPath(
|
|
3706
|
+
packageDirectoryUrl,
|
|
3707
|
+
)}package.json imported from ${fileURLToPath(parentUrl)}; ${reason}`;
|
|
3708
|
+
}
|
|
3709
|
+
const error = new Error(message);
|
|
3710
|
+
error.code = "INVALID_PACKAGE_TARGET";
|
|
3711
|
+
return error;
|
|
3712
|
+
};
|
|
3713
|
+
|
|
3714
|
+
const createPackagePathNotExportedError = (
|
|
3715
|
+
subpath,
|
|
3716
|
+
{ parentUrl, packageDirectoryUrl },
|
|
3717
|
+
) => {
|
|
3718
|
+
let message;
|
|
3719
|
+
if (subpath === ".") {
|
|
3720
|
+
message = `No "exports" main defined in ${fileURLToPath(
|
|
3721
|
+
packageDirectoryUrl,
|
|
3722
|
+
)}package.json imported from ${fileURLToPath(parentUrl)}`;
|
|
3723
|
+
} else {
|
|
3724
|
+
message = `Package subpath "${subpath}" is not defined by "exports" in ${fileURLToPath(
|
|
3725
|
+
packageDirectoryUrl,
|
|
3726
|
+
)}package.json imported from ${fileURLToPath(parentUrl)}`;
|
|
3727
|
+
}
|
|
3728
|
+
const error = new Error(message);
|
|
3729
|
+
error.code = "PACKAGE_PATH_NOT_EXPORTED";
|
|
3730
|
+
return error;
|
|
3731
|
+
};
|
|
3732
|
+
|
|
3733
|
+
const createModuleNotFoundError = (specifier, { parentUrl }) => {
|
|
3734
|
+
const error = new Error(
|
|
3735
|
+
`Cannot find "${specifier}" imported from ${fileURLToPath(parentUrl)}`,
|
|
3736
|
+
);
|
|
3737
|
+
error.code = "MODULE_NOT_FOUND";
|
|
3738
|
+
return error;
|
|
3739
|
+
};
|
|
3740
|
+
|
|
3741
|
+
const createPackageImportNotDefinedError = (
|
|
3742
|
+
specifier,
|
|
3743
|
+
{ parentUrl, packageDirectoryUrl },
|
|
3744
|
+
) => {
|
|
3745
|
+
const error = new Error(
|
|
3746
|
+
`Package import specifier "${specifier}" is not defined in ${fileURLToPath(
|
|
3747
|
+
packageDirectoryUrl,
|
|
3748
|
+
)}package.json imported from ${fileURLToPath(parentUrl)}`,
|
|
3749
|
+
);
|
|
3750
|
+
error.code = "PACKAGE_IMPORT_NOT_DEFINED";
|
|
3751
|
+
return error;
|
|
3752
|
+
};
|
|
3753
|
+
|
|
3754
|
+
const isSpecifierForNodeBuiltin = (specifier) => {
|
|
3755
|
+
return (
|
|
3756
|
+
specifier.startsWith("node:") ||
|
|
3757
|
+
NODE_BUILTIN_MODULE_SPECIFIERS.includes(specifier)
|
|
3758
|
+
);
|
|
3759
|
+
};
|
|
3760
|
+
|
|
3761
|
+
const NODE_BUILTIN_MODULE_SPECIFIERS = [
|
|
3762
|
+
"assert",
|
|
3763
|
+
"assert/strict",
|
|
3764
|
+
"async_hooks",
|
|
3765
|
+
"buffer_ieee754",
|
|
3766
|
+
"buffer",
|
|
3767
|
+
"child_process",
|
|
3768
|
+
"cluster",
|
|
3769
|
+
"console",
|
|
3770
|
+
"constants",
|
|
3771
|
+
"crypto",
|
|
3772
|
+
"_debugger",
|
|
3773
|
+
"diagnostics_channel",
|
|
3774
|
+
"dgram",
|
|
3775
|
+
"dns",
|
|
3776
|
+
"domain",
|
|
3777
|
+
"events",
|
|
3778
|
+
"freelist",
|
|
3779
|
+
"fs",
|
|
3780
|
+
"fsevents",
|
|
3781
|
+
"fs/promises",
|
|
3782
|
+
"_http_agent",
|
|
3783
|
+
"_http_client",
|
|
3784
|
+
"_http_common",
|
|
3785
|
+
"_http_incoming",
|
|
3786
|
+
"_http_outgoing",
|
|
3787
|
+
"_http_server",
|
|
3788
|
+
"http",
|
|
3789
|
+
"http2",
|
|
3790
|
+
"https",
|
|
3791
|
+
"inspector",
|
|
3792
|
+
"_linklist",
|
|
3793
|
+
"module",
|
|
3794
|
+
"net",
|
|
3795
|
+
"node-inspect/lib/_inspect",
|
|
3796
|
+
"node-inspect/lib/internal/inspect_client",
|
|
3797
|
+
"node-inspect/lib/internal/inspect_repl",
|
|
3798
|
+
"os",
|
|
3799
|
+
"path",
|
|
3800
|
+
"perf_hooks",
|
|
3801
|
+
"process",
|
|
3802
|
+
"punycode",
|
|
3803
|
+
"querystring",
|
|
3804
|
+
"readline",
|
|
3805
|
+
"repl",
|
|
3806
|
+
"smalloc",
|
|
3807
|
+
"sqlite",
|
|
3808
|
+
"_stream_duplex",
|
|
3809
|
+
"_stream_transform",
|
|
3810
|
+
"_stream_wrap",
|
|
3811
|
+
"_stream_passthrough",
|
|
3812
|
+
"_stream_readable",
|
|
3813
|
+
"_stream_writable",
|
|
3814
|
+
"stream",
|
|
3815
|
+
"stream/promises",
|
|
3816
|
+
"string_decoder",
|
|
3817
|
+
"sys",
|
|
3818
|
+
"timers",
|
|
3819
|
+
"_tls_common",
|
|
3820
|
+
"_tls_legacy",
|
|
3821
|
+
"_tls_wrap",
|
|
3822
|
+
"tls",
|
|
3823
|
+
"trace_events",
|
|
3824
|
+
"tty",
|
|
3825
|
+
"url",
|
|
3826
|
+
"util",
|
|
3827
|
+
"v8/tools/arguments",
|
|
3828
|
+
"v8/tools/codemap",
|
|
3829
|
+
"v8/tools/consarray",
|
|
3830
|
+
"v8/tools/csvparser",
|
|
3831
|
+
"v8/tools/logreader",
|
|
3832
|
+
"v8/tools/profile_view",
|
|
3833
|
+
"v8/tools/splaytree",
|
|
3834
|
+
"v8",
|
|
3835
|
+
"vm",
|
|
3836
|
+
"worker_threads",
|
|
3837
|
+
"zlib",
|
|
3838
|
+
// global is special
|
|
3839
|
+
"global",
|
|
3840
|
+
];
|
|
3841
|
+
|
|
3842
|
+
/*
|
|
3843
|
+
* https://nodejs.org/api/esm.html#resolver-algorithm-specification
|
|
3844
|
+
* https://github.com/nodejs/node/blob/0367b5c35ea0f98b323175a4aaa8e651af7a91e7/lib/internal/modules/esm/resolve.js#L1
|
|
3845
|
+
* deviations from the spec:
|
|
3846
|
+
* - take into account "browser", "module" and "jsnext"
|
|
3847
|
+
* - the check for isDirectory -> throw is delayed is descoped to the caller
|
|
3848
|
+
* - the call to real path ->
|
|
3849
|
+
* delayed to the caller so that we can decide to
|
|
3850
|
+
* maintain symlink as facade url when it's outside project directory
|
|
3851
|
+
* or use the real path when inside
|
|
3852
|
+
*/
|
|
3853
|
+
|
|
3854
|
+
const applyNodeEsmResolution = ({
|
|
3855
|
+
specifier,
|
|
3856
|
+
parentUrl,
|
|
3857
|
+
conditions = [...readCustomConditionsFromProcessArgs(), "node", "import"],
|
|
3858
|
+
lookupPackageScope = defaultLookupPackageScope,
|
|
3859
|
+
readPackageJson = defaultReadPackageJson,
|
|
3860
|
+
preservesSymlink = false,
|
|
3861
|
+
}) => {
|
|
3862
|
+
const resolution = applyPackageSpecifierResolution(specifier, {
|
|
3863
|
+
parentUrl: String(parentUrl),
|
|
3864
|
+
conditions,
|
|
3865
|
+
lookupPackageScope,
|
|
3866
|
+
readPackageJson,
|
|
3867
|
+
preservesSymlink,
|
|
3868
|
+
});
|
|
3869
|
+
const { url } = resolution;
|
|
3870
|
+
if (url.startsWith("file:")) {
|
|
3871
|
+
if (url.includes("%2F") || url.includes("%5C")) {
|
|
3872
|
+
throw createInvalidModuleSpecifierError(
|
|
3873
|
+
`must not include encoded "/" or "\\" characters`,
|
|
3874
|
+
specifier,
|
|
3875
|
+
{
|
|
3876
|
+
parentUrl,
|
|
3877
|
+
},
|
|
3878
|
+
);
|
|
3879
|
+
}
|
|
3880
|
+
return resolution;
|
|
3881
|
+
}
|
|
3882
|
+
return resolution;
|
|
3883
|
+
};
|
|
3884
|
+
|
|
3885
|
+
const applyPackageSpecifierResolution = (specifier, resolutionContext) => {
|
|
3886
|
+
const { parentUrl } = resolutionContext;
|
|
3887
|
+
// relative specifier
|
|
3888
|
+
if (
|
|
3889
|
+
specifier[0] === "/" ||
|
|
3890
|
+
specifier.startsWith("./") ||
|
|
3891
|
+
specifier.startsWith("../")
|
|
3892
|
+
) {
|
|
3893
|
+
if (specifier[0] !== "/") {
|
|
3894
|
+
const browserFieldResolution = applyBrowserFieldResolution(
|
|
3895
|
+
specifier,
|
|
3896
|
+
resolutionContext,
|
|
3897
|
+
);
|
|
3898
|
+
if (browserFieldResolution) {
|
|
3899
|
+
return browserFieldResolution;
|
|
3900
|
+
}
|
|
3901
|
+
}
|
|
3902
|
+
return {
|
|
3903
|
+
type: "relative_specifier",
|
|
3904
|
+
url: new URL(specifier, parentUrl).href,
|
|
3905
|
+
};
|
|
3906
|
+
}
|
|
3907
|
+
if (specifier[0] === "#") {
|
|
3908
|
+
return applyPackageImportsResolution(specifier, resolutionContext);
|
|
3909
|
+
}
|
|
3910
|
+
try {
|
|
3911
|
+
const urlObject = new URL(specifier);
|
|
3912
|
+
if (specifier.startsWith("node:")) {
|
|
3913
|
+
return {
|
|
3914
|
+
type: "node_builtin_specifier",
|
|
3915
|
+
url: specifier,
|
|
3916
|
+
};
|
|
3917
|
+
}
|
|
3918
|
+
return {
|
|
3919
|
+
type: "absolute_specifier",
|
|
3920
|
+
url: urlObject.href,
|
|
3921
|
+
};
|
|
3922
|
+
} catch {
|
|
3923
|
+
// bare specifier
|
|
3924
|
+
const browserFieldResolution = applyBrowserFieldResolution(
|
|
3925
|
+
specifier,
|
|
3926
|
+
resolutionContext,
|
|
3927
|
+
);
|
|
3928
|
+
if (browserFieldResolution) {
|
|
3929
|
+
return browserFieldResolution;
|
|
3930
|
+
}
|
|
3931
|
+
const packageResolution = applyPackageResolve(specifier, resolutionContext);
|
|
3932
|
+
const search = new URL(specifier, "file:///").search;
|
|
3933
|
+
if (search && !new URL(packageResolution.url).search) {
|
|
3934
|
+
packageResolution.url = `${packageResolution.url}${search}`;
|
|
3935
|
+
}
|
|
3936
|
+
return packageResolution;
|
|
3937
|
+
}
|
|
3938
|
+
};
|
|
3939
|
+
|
|
3940
|
+
const applyBrowserFieldResolution = (specifier, resolutionContext) => {
|
|
3941
|
+
const { parentUrl, conditions, lookupPackageScope, readPackageJson } =
|
|
3942
|
+
resolutionContext;
|
|
3943
|
+
const browserCondition = conditions.includes("browser");
|
|
3944
|
+
if (!browserCondition) {
|
|
3945
|
+
return null;
|
|
3946
|
+
}
|
|
3947
|
+
const packageDirectoryUrl = lookupPackageScope(parentUrl);
|
|
3948
|
+
if (!packageDirectoryUrl) {
|
|
3949
|
+
return null;
|
|
3950
|
+
}
|
|
3951
|
+
const packageJson = readPackageJson(packageDirectoryUrl);
|
|
3952
|
+
if (!packageJson) {
|
|
3953
|
+
return null;
|
|
3954
|
+
}
|
|
3955
|
+
const { browser } = packageJson;
|
|
3956
|
+
if (!browser) {
|
|
3957
|
+
return null;
|
|
3958
|
+
}
|
|
3959
|
+
if (typeof browser !== "object") {
|
|
3960
|
+
return null;
|
|
3961
|
+
}
|
|
3962
|
+
let url;
|
|
3963
|
+
if (specifier.startsWith(".")) {
|
|
3964
|
+
const specifierUrl = new URL(specifier, parentUrl).href;
|
|
3965
|
+
const specifierRelativeUrl = specifierUrl.slice(packageDirectoryUrl.length);
|
|
3966
|
+
const secifierRelativeNotation = `./${specifierRelativeUrl}`;
|
|
3967
|
+
const browserMapping = browser[secifierRelativeNotation];
|
|
3968
|
+
if (typeof browserMapping === "string") {
|
|
3969
|
+
url = new URL(browserMapping, packageDirectoryUrl).href;
|
|
3970
|
+
} else if (browserMapping === false) {
|
|
3971
|
+
url = `file:///@ignore/${specifierUrl.slice("file:///")}`;
|
|
3972
|
+
}
|
|
3973
|
+
} else {
|
|
3974
|
+
const browserMapping = browser[specifier];
|
|
3975
|
+
if (typeof browserMapping === "string") {
|
|
3976
|
+
url = new URL(browserMapping, packageDirectoryUrl).href;
|
|
3977
|
+
} else if (browserMapping === false) {
|
|
3978
|
+
url = `file:///@ignore/${specifier}`;
|
|
3979
|
+
}
|
|
3980
|
+
}
|
|
3981
|
+
if (url) {
|
|
3982
|
+
return {
|
|
3983
|
+
type: "field:browser",
|
|
3984
|
+
isMain: true,
|
|
3985
|
+
packageDirectoryUrl,
|
|
3986
|
+
packageJson,
|
|
3987
|
+
url,
|
|
3988
|
+
};
|
|
3989
|
+
}
|
|
3990
|
+
return null;
|
|
3991
|
+
};
|
|
3992
|
+
|
|
3993
|
+
const applyPackageImportsResolution = (
|
|
3994
|
+
internalSpecifier,
|
|
3995
|
+
resolutionContext,
|
|
3996
|
+
) => {
|
|
3997
|
+
const { parentUrl, lookupPackageScope, readPackageJson } = resolutionContext;
|
|
3998
|
+
if (internalSpecifier === "#" || internalSpecifier.startsWith("#/")) {
|
|
3999
|
+
throw createInvalidModuleSpecifierError(
|
|
4000
|
+
"not a valid internal imports specifier name",
|
|
4001
|
+
internalSpecifier,
|
|
4002
|
+
resolutionContext,
|
|
4003
|
+
);
|
|
4004
|
+
}
|
|
4005
|
+
const packageDirectoryUrl = lookupPackageScope(parentUrl);
|
|
4006
|
+
if (packageDirectoryUrl !== null) {
|
|
4007
|
+
const packageJson = readPackageJson(packageDirectoryUrl);
|
|
4008
|
+
const { imports } = packageJson;
|
|
4009
|
+
if (imports !== null && typeof imports === "object") {
|
|
4010
|
+
const resolved = applyPackageImportsExportsResolution(internalSpecifier, {
|
|
4011
|
+
...resolutionContext,
|
|
4012
|
+
packageDirectoryUrl,
|
|
4013
|
+
packageJson,
|
|
4014
|
+
isImport: true,
|
|
4015
|
+
});
|
|
4016
|
+
if (resolved) {
|
|
4017
|
+
return resolved;
|
|
4018
|
+
}
|
|
4019
|
+
}
|
|
4020
|
+
}
|
|
4021
|
+
throw createPackageImportNotDefinedError(internalSpecifier, {
|
|
4022
|
+
...resolutionContext,
|
|
4023
|
+
packageDirectoryUrl,
|
|
4024
|
+
});
|
|
4025
|
+
};
|
|
4026
|
+
|
|
4027
|
+
const applyPackageResolve = (packageSpecifier, resolutionContext) => {
|
|
4028
|
+
const { parentUrl, conditions, readPackageJson, preservesSymlink, isImport } =
|
|
4029
|
+
resolutionContext;
|
|
4030
|
+
if (packageSpecifier === "") {
|
|
4031
|
+
throw new Error("invalid module specifier");
|
|
4032
|
+
}
|
|
4033
|
+
if (
|
|
4034
|
+
conditions.includes("node") &&
|
|
4035
|
+
isSpecifierForNodeBuiltin(packageSpecifier)
|
|
4036
|
+
) {
|
|
4037
|
+
return {
|
|
4038
|
+
type: "node_builtin_specifier",
|
|
4039
|
+
url: `node:${packageSpecifier}`,
|
|
4040
|
+
};
|
|
4041
|
+
}
|
|
4042
|
+
let { packageName, packageSubpath } = parsePackageSpecifier(packageSpecifier);
|
|
4043
|
+
if (
|
|
4044
|
+
packageName[0] === "." ||
|
|
4045
|
+
packageName.includes("\\") ||
|
|
4046
|
+
packageName.includes("%")
|
|
4047
|
+
) {
|
|
4048
|
+
throw createInvalidModuleSpecifierError(
|
|
4049
|
+
`is not a valid package name`,
|
|
4050
|
+
packageName,
|
|
4051
|
+
resolutionContext,
|
|
4052
|
+
);
|
|
4053
|
+
}
|
|
4054
|
+
if (isImport && packageSubpath.endsWith("/")) {
|
|
4055
|
+
throw new Error("invalid module specifier");
|
|
4056
|
+
}
|
|
4057
|
+
const questionCharIndex = packageName.indexOf("?");
|
|
4058
|
+
if (questionCharIndex > -1) {
|
|
4059
|
+
packageName = packageName.slice(0, questionCharIndex);
|
|
4060
|
+
}
|
|
4061
|
+
const selfResolution = applyPackageSelfResolution(packageSubpath, {
|
|
4062
|
+
...resolutionContext,
|
|
4063
|
+
packageName,
|
|
4064
|
+
});
|
|
4065
|
+
if (selfResolution) {
|
|
4066
|
+
return selfResolution;
|
|
4067
|
+
}
|
|
4068
|
+
let currentUrl = parentUrl;
|
|
4069
|
+
while (currentUrl !== "file:///") {
|
|
4070
|
+
const packageDirectoryFacadeUrl = new URL(
|
|
4071
|
+
`node_modules/${packageName}/`,
|
|
4072
|
+
currentUrl,
|
|
4073
|
+
).href;
|
|
4074
|
+
if (!existsSync(new URL(packageDirectoryFacadeUrl))) {
|
|
4075
|
+
currentUrl = getParentUrl(currentUrl);
|
|
4076
|
+
continue;
|
|
4077
|
+
}
|
|
4078
|
+
const packageDirectoryUrl = preservesSymlink
|
|
4079
|
+
? packageDirectoryFacadeUrl
|
|
4080
|
+
: resolvePackageSymlink(packageDirectoryFacadeUrl);
|
|
4081
|
+
const packageJson = readPackageJson(packageDirectoryUrl);
|
|
4082
|
+
if (packageJson !== null) {
|
|
4083
|
+
const { exports } = packageJson;
|
|
4084
|
+
if (exports !== null && exports !== undefined) {
|
|
4085
|
+
return applyPackageExportsResolution(packageSubpath, {
|
|
4086
|
+
...resolutionContext,
|
|
4087
|
+
packageDirectoryUrl,
|
|
4088
|
+
packageJson,
|
|
4089
|
+
exports,
|
|
4090
|
+
});
|
|
4091
|
+
}
|
|
4092
|
+
}
|
|
4093
|
+
return applyLegacySubpathResolution(packageSubpath, {
|
|
4094
|
+
...resolutionContext,
|
|
4095
|
+
packageDirectoryUrl,
|
|
4096
|
+
packageJson,
|
|
4097
|
+
});
|
|
4098
|
+
}
|
|
4099
|
+
throw createModuleNotFoundError(packageName, resolutionContext);
|
|
4100
|
+
};
|
|
4101
|
+
|
|
4102
|
+
const applyPackageSelfResolution = (packageSubpath, resolutionContext) => {
|
|
4103
|
+
const { parentUrl, packageName, lookupPackageScope, readPackageJson } =
|
|
4104
|
+
resolutionContext;
|
|
4105
|
+
const packageDirectoryUrl = lookupPackageScope(parentUrl);
|
|
4106
|
+
if (!packageDirectoryUrl) {
|
|
4107
|
+
return undefined;
|
|
4108
|
+
}
|
|
4109
|
+
const packageJson = readPackageJson(packageDirectoryUrl);
|
|
4110
|
+
if (!packageJson) {
|
|
4111
|
+
return undefined;
|
|
4112
|
+
}
|
|
4113
|
+
if (packageJson.name !== packageName) {
|
|
4114
|
+
return undefined;
|
|
4115
|
+
}
|
|
4116
|
+
const { exports } = packageJson;
|
|
4117
|
+
if (!exports) {
|
|
4118
|
+
const subpathResolution = applyLegacySubpathResolution(packageSubpath, {
|
|
4119
|
+
...resolutionContext,
|
|
4120
|
+
packageDirectoryUrl,
|
|
4121
|
+
packageJson,
|
|
4122
|
+
});
|
|
4123
|
+
if (subpathResolution && subpathResolution.type !== "subpath") {
|
|
4124
|
+
return subpathResolution;
|
|
4125
|
+
}
|
|
4126
|
+
return undefined;
|
|
4127
|
+
}
|
|
4128
|
+
return applyPackageExportsResolution(packageSubpath, {
|
|
4129
|
+
...resolutionContext,
|
|
4130
|
+
packageDirectoryUrl,
|
|
4131
|
+
packageJson,
|
|
4132
|
+
});
|
|
4133
|
+
};
|
|
4134
|
+
|
|
4135
|
+
// https://github.com/nodejs/node/blob/0367b5c35ea0f98b323175a4aaa8e651af7a91e7/lib/internal/modules/esm/resolve.js#L642
|
|
4136
|
+
const applyPackageExportsResolution = (packageSubpath, resolutionContext) => {
|
|
4137
|
+
if (packageSubpath === ".") {
|
|
4138
|
+
const mainExport = applyMainExportResolution(resolutionContext);
|
|
4139
|
+
if (!mainExport) {
|
|
4140
|
+
throw createPackagePathNotExportedError(
|
|
4141
|
+
packageSubpath,
|
|
4142
|
+
resolutionContext,
|
|
4143
|
+
);
|
|
4144
|
+
}
|
|
4145
|
+
const resolved = applyPackageTargetResolution(mainExport, {
|
|
4146
|
+
...resolutionContext,
|
|
4147
|
+
key: ".",
|
|
4148
|
+
});
|
|
4149
|
+
if (resolved) {
|
|
4150
|
+
return resolved;
|
|
4151
|
+
}
|
|
4152
|
+
throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
|
|
4153
|
+
}
|
|
4154
|
+
const packageExportsInfo = readExports(resolutionContext);
|
|
4155
|
+
if (
|
|
4156
|
+
packageExportsInfo.type === "object" &&
|
|
4157
|
+
packageExportsInfo.allKeysAreRelative
|
|
4158
|
+
) {
|
|
4159
|
+
const resolved = applyPackageImportsExportsResolution(packageSubpath, {
|
|
4160
|
+
...resolutionContext,
|
|
4161
|
+
isImport: false,
|
|
4162
|
+
});
|
|
4163
|
+
if (resolved) {
|
|
4164
|
+
return resolved;
|
|
4165
|
+
}
|
|
4166
|
+
}
|
|
4167
|
+
throw createPackagePathNotExportedError(packageSubpath, resolutionContext);
|
|
4168
|
+
};
|
|
4169
|
+
|
|
4170
|
+
const applyPackageImportsExportsResolution = (matchKey, resolutionContext) => {
|
|
4171
|
+
const { packageJson, isImport } = resolutionContext;
|
|
4172
|
+
const matchObject = isImport ? packageJson.imports : packageJson.exports;
|
|
4173
|
+
|
|
4174
|
+
if (!matchKey.includes("*") && matchObject.hasOwnProperty(matchKey)) {
|
|
4175
|
+
const target = matchObject[matchKey];
|
|
4176
|
+
return applyPackageTargetResolution(target, {
|
|
4177
|
+
...resolutionContext,
|
|
4178
|
+
key: matchKey,
|
|
4179
|
+
isImport,
|
|
4180
|
+
});
|
|
4181
|
+
}
|
|
4182
|
+
const expansionKeys = Object.keys(matchObject)
|
|
4183
|
+
.filter((key) => key.split("*").length === 2)
|
|
4184
|
+
.sort(comparePatternKeys);
|
|
4185
|
+
for (const expansionKey of expansionKeys) {
|
|
4186
|
+
const [patternBase, patternTrailer] = expansionKey.split("*");
|
|
4187
|
+
if (matchKey === patternBase) continue;
|
|
4188
|
+
if (!matchKey.startsWith(patternBase)) continue;
|
|
4189
|
+
if (patternTrailer.length > 0) {
|
|
4190
|
+
if (!matchKey.endsWith(patternTrailer)) continue;
|
|
4191
|
+
if (matchKey.length < expansionKey.length) continue;
|
|
4192
|
+
}
|
|
4193
|
+
const target = matchObject[expansionKey];
|
|
4194
|
+
const subpath = matchKey.slice(
|
|
4195
|
+
patternBase.length,
|
|
4196
|
+
matchKey.length - patternTrailer.length,
|
|
4197
|
+
);
|
|
4198
|
+
return applyPackageTargetResolution(target, {
|
|
4199
|
+
...resolutionContext,
|
|
4200
|
+
key: matchKey,
|
|
4201
|
+
subpath,
|
|
4202
|
+
pattern: true,
|
|
4203
|
+
isImport,
|
|
4204
|
+
});
|
|
4205
|
+
}
|
|
4206
|
+
return null;
|
|
4207
|
+
};
|
|
4208
|
+
|
|
4209
|
+
const applyPackageTargetResolution = (target, resolutionContext) => {
|
|
4210
|
+
const {
|
|
4211
|
+
conditions,
|
|
4212
|
+
packageDirectoryUrl,
|
|
4213
|
+
packageJson,
|
|
4214
|
+
key,
|
|
4215
|
+
subpath = "",
|
|
4216
|
+
pattern = false,
|
|
4217
|
+
isImport = false,
|
|
4218
|
+
} = resolutionContext;
|
|
4219
|
+
|
|
4220
|
+
if (typeof target === "string") {
|
|
4221
|
+
if (pattern === false && subpath !== "" && !target.endsWith("/")) {
|
|
4222
|
+
throw new Error("invalid module specifier");
|
|
4223
|
+
}
|
|
4224
|
+
if (target.startsWith("./")) {
|
|
4225
|
+
const targetUrl = new URL(target, packageDirectoryUrl).href;
|
|
4226
|
+
if (!targetUrl.startsWith(packageDirectoryUrl)) {
|
|
4227
|
+
throw createInvalidPackageTargetError(
|
|
4228
|
+
`target must be inside package`,
|
|
4229
|
+
target,
|
|
4230
|
+
resolutionContext,
|
|
4231
|
+
);
|
|
4232
|
+
}
|
|
4233
|
+
return {
|
|
4234
|
+
type: isImport ? "field:imports" : "field:exports",
|
|
4235
|
+
isMain: subpath === "" || subpath === ".",
|
|
4236
|
+
packageDirectoryUrl,
|
|
4237
|
+
packageJson,
|
|
4238
|
+
url: pattern
|
|
4239
|
+
? targetUrl.replaceAll("*", subpath)
|
|
4240
|
+
: new URL(subpath, targetUrl).href,
|
|
4241
|
+
};
|
|
3745
4242
|
}
|
|
3746
|
-
|
|
3747
|
-
|
|
4243
|
+
if (!isImport || target.startsWith("../") || isValidUrl(target)) {
|
|
4244
|
+
throw createInvalidPackageTargetError(
|
|
4245
|
+
`target must starst with "./"`,
|
|
4246
|
+
target,
|
|
4247
|
+
resolutionContext,
|
|
4248
|
+
);
|
|
4249
|
+
}
|
|
4250
|
+
return applyPackageResolve(
|
|
4251
|
+
pattern ? target.replaceAll("*", subpath) : `${target}${subpath}`,
|
|
4252
|
+
{
|
|
4253
|
+
...resolutionContext,
|
|
4254
|
+
parentUrl: packageDirectoryUrl,
|
|
4255
|
+
},
|
|
4256
|
+
);
|
|
4257
|
+
}
|
|
4258
|
+
if (Array.isArray(target)) {
|
|
4259
|
+
if (target.length === 0) {
|
|
4260
|
+
return null;
|
|
4261
|
+
}
|
|
4262
|
+
let lastResult;
|
|
4263
|
+
let i = 0;
|
|
4264
|
+
while (i < target.length) {
|
|
4265
|
+
const targetValue = target[i];
|
|
4266
|
+
i++;
|
|
4267
|
+
try {
|
|
4268
|
+
const resolved = applyPackageTargetResolution(targetValue, {
|
|
4269
|
+
...resolutionContext,
|
|
4270
|
+
key: `${key}[${i}]`,
|
|
4271
|
+
subpath,
|
|
4272
|
+
pattern,
|
|
4273
|
+
isImport,
|
|
4274
|
+
});
|
|
4275
|
+
if (resolved) {
|
|
4276
|
+
return resolved;
|
|
4277
|
+
}
|
|
4278
|
+
lastResult = resolved;
|
|
4279
|
+
} catch (e) {
|
|
4280
|
+
if (e.code === "INVALID_PACKAGE_TARGET") {
|
|
4281
|
+
continue;
|
|
4282
|
+
}
|
|
4283
|
+
lastResult = e;
|
|
4284
|
+
}
|
|
4285
|
+
}
|
|
4286
|
+
if (lastResult) {
|
|
4287
|
+
throw lastResult;
|
|
4288
|
+
}
|
|
4289
|
+
return null;
|
|
4290
|
+
}
|
|
4291
|
+
if (target === null) {
|
|
4292
|
+
return null;
|
|
4293
|
+
}
|
|
4294
|
+
if (typeof target === "object") {
|
|
4295
|
+
const keys = Object.keys(target);
|
|
4296
|
+
for (const key of keys) {
|
|
4297
|
+
if (Number.isInteger(key)) {
|
|
4298
|
+
throw new Error("Invalid package configuration");
|
|
4299
|
+
}
|
|
4300
|
+
let matched;
|
|
4301
|
+
if (key === "default") {
|
|
4302
|
+
matched = true;
|
|
4303
|
+
} else {
|
|
4304
|
+
for (const conditionCandidate of conditions) {
|
|
4305
|
+
if (conditionCandidate === key) {
|
|
4306
|
+
matched = true;
|
|
4307
|
+
break;
|
|
4308
|
+
}
|
|
4309
|
+
if (conditionCandidate.includes("*")) {
|
|
4310
|
+
const conditionCandidateRegex = new RegExp(
|
|
4311
|
+
`^${conditionCandidate.replace(/\*/g, "(.*)")}$`,
|
|
4312
|
+
);
|
|
4313
|
+
if (conditionCandidateRegex.test(key)) {
|
|
4314
|
+
matched = true;
|
|
4315
|
+
break;
|
|
4316
|
+
}
|
|
4317
|
+
}
|
|
4318
|
+
}
|
|
4319
|
+
}
|
|
4320
|
+
if (matched) {
|
|
4321
|
+
const targetValue = target[key];
|
|
4322
|
+
const resolved = applyPackageTargetResolution(targetValue, {
|
|
4323
|
+
...resolutionContext,
|
|
4324
|
+
key,
|
|
4325
|
+
subpath,
|
|
4326
|
+
pattern,
|
|
4327
|
+
isImport,
|
|
4328
|
+
});
|
|
4329
|
+
if (resolved) {
|
|
4330
|
+
return resolved;
|
|
4331
|
+
}
|
|
4332
|
+
}
|
|
4333
|
+
}
|
|
4334
|
+
return null;
|
|
3748
4335
|
}
|
|
4336
|
+
throw createInvalidPackageTargetError(
|
|
4337
|
+
`target must be a string, array, object or null`,
|
|
4338
|
+
target,
|
|
4339
|
+
resolutionContext,
|
|
4340
|
+
);
|
|
3749
4341
|
};
|
|
3750
4342
|
|
|
3751
|
-
const
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
|
|
3762
|
-
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
) {
|
|
3769
|
-
resolve(handleNotEmptyError(error));
|
|
3770
|
-
} else {
|
|
3771
|
-
reject(error);
|
|
3772
|
-
}
|
|
4343
|
+
const readExports = ({ packageDirectoryUrl, packageJson }) => {
|
|
4344
|
+
const packageExports = packageJson.exports;
|
|
4345
|
+
if (Array.isArray(packageExports)) {
|
|
4346
|
+
return {
|
|
4347
|
+
type: "array",
|
|
4348
|
+
};
|
|
4349
|
+
}
|
|
4350
|
+
if (packageExports === null) {
|
|
4351
|
+
return {};
|
|
4352
|
+
}
|
|
4353
|
+
if (typeof packageExports === "object") {
|
|
4354
|
+
const keys = Object.keys(packageExports);
|
|
4355
|
+
const relativeKeys = [];
|
|
4356
|
+
const conditionalKeys = [];
|
|
4357
|
+
keys.forEach((availableKey) => {
|
|
4358
|
+
if (availableKey.startsWith(".")) {
|
|
4359
|
+
relativeKeys.push(availableKey);
|
|
3773
4360
|
} else {
|
|
3774
|
-
|
|
4361
|
+
conditionalKeys.push(availableKey);
|
|
3775
4362
|
}
|
|
3776
4363
|
});
|
|
3777
|
-
|
|
4364
|
+
const hasRelativeKey = relativeKeys.length > 0;
|
|
4365
|
+
if (hasRelativeKey && conditionalKeys.length > 0) {
|
|
4366
|
+
throw new Error(
|
|
4367
|
+
`Invalid package configuration: cannot mix relative and conditional keys in package.exports
|
|
4368
|
+
--- unexpected keys ---
|
|
4369
|
+
${conditionalKeys.map((key) => `"${key}"`).join("\n")}
|
|
4370
|
+
--- package directory url ---
|
|
4371
|
+
${packageDirectoryUrl}`,
|
|
4372
|
+
);
|
|
4373
|
+
}
|
|
4374
|
+
return {
|
|
4375
|
+
type: "object",
|
|
4376
|
+
hasRelativeKey,
|
|
4377
|
+
allKeysAreRelative: relativeKeys.length === keys.length,
|
|
4378
|
+
};
|
|
4379
|
+
}
|
|
4380
|
+
if (typeof packageExports === "string") {
|
|
4381
|
+
return { type: "string" };
|
|
4382
|
+
}
|
|
4383
|
+
return {};
|
|
3778
4384
|
};
|
|
3779
4385
|
|
|
3780
|
-
const
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
4386
|
+
const parsePackageSpecifier = (packageSpecifier) => {
|
|
4387
|
+
if (packageSpecifier[0] === "@") {
|
|
4388
|
+
const firstSlashIndex = packageSpecifier.indexOf("/");
|
|
4389
|
+
if (firstSlashIndex === -1) {
|
|
4390
|
+
throw new Error("invalid module specifier");
|
|
4391
|
+
}
|
|
4392
|
+
const secondSlashIndex = packageSpecifier.indexOf("/", firstSlashIndex + 1);
|
|
4393
|
+
if (secondSlashIndex === -1) {
|
|
4394
|
+
return {
|
|
4395
|
+
packageName: packageSpecifier,
|
|
4396
|
+
packageSubpath: ".",
|
|
4397
|
+
isScoped: true,
|
|
4398
|
+
};
|
|
4399
|
+
}
|
|
4400
|
+
const packageName = packageSpecifier.slice(0, secondSlashIndex);
|
|
4401
|
+
const afterSecondSlash = packageSpecifier.slice(secondSlashIndex + 1);
|
|
4402
|
+
const packageSubpath = `./${afterSecondSlash}`;
|
|
4403
|
+
return {
|
|
4404
|
+
packageName,
|
|
4405
|
+
packageSubpath,
|
|
4406
|
+
isScoped: true,
|
|
4407
|
+
};
|
|
4408
|
+
}
|
|
4409
|
+
const firstSlashIndex = packageSpecifier.indexOf("/");
|
|
4410
|
+
if (firstSlashIndex === -1) {
|
|
4411
|
+
return {
|
|
4412
|
+
packageName: packageSpecifier,
|
|
4413
|
+
packageSubpath: ".",
|
|
4414
|
+
};
|
|
4415
|
+
}
|
|
4416
|
+
const packageName = packageSpecifier.slice(0, firstSlashIndex);
|
|
4417
|
+
const afterFirstSlash = packageSpecifier.slice(firstSlashIndex + 1);
|
|
4418
|
+
if (afterFirstSlash === "") {
|
|
4419
|
+
return {
|
|
4420
|
+
packageName,
|
|
4421
|
+
packageSubpath: "/",
|
|
4422
|
+
};
|
|
4423
|
+
}
|
|
4424
|
+
const packageSubpath = `./${afterFirstSlash}`;
|
|
4425
|
+
return {
|
|
4426
|
+
packageName,
|
|
4427
|
+
packageSubpath,
|
|
4428
|
+
};
|
|
4429
|
+
};
|
|
3785
4430
|
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
|
|
4431
|
+
const applyMainExportResolution = (resolutionContext) => {
|
|
4432
|
+
const { packageJson } = resolutionContext;
|
|
4433
|
+
const packageExportsInfo = readExports(resolutionContext);
|
|
4434
|
+
if (
|
|
4435
|
+
packageExportsInfo.type === "array" ||
|
|
4436
|
+
packageExportsInfo.type === "string"
|
|
4437
|
+
) {
|
|
4438
|
+
return packageJson.exports;
|
|
3790
4439
|
}
|
|
3791
|
-
if (
|
|
3792
|
-
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
|
|
3796
|
-
});
|
|
3797
|
-
return;
|
|
4440
|
+
if (packageExportsInfo.type === "object") {
|
|
4441
|
+
if (packageExportsInfo.hasRelativeKey) {
|
|
4442
|
+
return packageJson.exports["."];
|
|
4443
|
+
}
|
|
4444
|
+
return packageJson.exports;
|
|
3798
4445
|
}
|
|
4446
|
+
return undefined;
|
|
4447
|
+
};
|
|
3799
4448
|
|
|
3800
|
-
|
|
3801
|
-
const
|
|
3802
|
-
|
|
3803
|
-
|
|
4449
|
+
const applyLegacySubpathResolution = (packageSubpath, resolutionContext) => {
|
|
4450
|
+
const { packageDirectoryUrl, packageJson } = resolutionContext;
|
|
4451
|
+
|
|
4452
|
+
if (packageSubpath === ".") {
|
|
4453
|
+
return applyLegacyMainResolution(packageSubpath, resolutionContext);
|
|
4454
|
+
}
|
|
4455
|
+
const browserFieldResolution = applyBrowserFieldResolution(
|
|
4456
|
+
packageSubpath,
|
|
4457
|
+
resolutionContext,
|
|
3804
4458
|
);
|
|
4459
|
+
if (browserFieldResolution) {
|
|
4460
|
+
return browserFieldResolution;
|
|
4461
|
+
}
|
|
4462
|
+
return {
|
|
4463
|
+
type: "subpath",
|
|
4464
|
+
isMain: packageSubpath === ".",
|
|
4465
|
+
packageDirectoryUrl,
|
|
4466
|
+
packageJson,
|
|
4467
|
+
url: new URL(packageSubpath, packageDirectoryUrl).href,
|
|
4468
|
+
};
|
|
3805
4469
|
};
|
|
3806
4470
|
|
|
3807
|
-
const
|
|
3808
|
-
const
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
4471
|
+
const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
|
|
4472
|
+
const { conditions, packageDirectoryUrl, packageJson } = resolutionContext;
|
|
4473
|
+
for (const condition of conditions) {
|
|
4474
|
+
const conditionResolver = mainLegacyResolvers[condition];
|
|
4475
|
+
if (!conditionResolver) {
|
|
4476
|
+
continue;
|
|
4477
|
+
}
|
|
4478
|
+
const resolved = conditionResolver(resolutionContext);
|
|
4479
|
+
if (resolved) {
|
|
4480
|
+
return {
|
|
4481
|
+
type: resolved.type,
|
|
4482
|
+
isMain: resolved.isMain,
|
|
4483
|
+
packageDirectoryUrl,
|
|
4484
|
+
packageJson,
|
|
4485
|
+
url: new URL(resolved.path, packageDirectoryUrl).href,
|
|
4486
|
+
};
|
|
4487
|
+
}
|
|
3816
4488
|
}
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
4489
|
+
return {
|
|
4490
|
+
type: "field:main", // the absence of "main" field
|
|
4491
|
+
isMain: true,
|
|
4492
|
+
packageDirectoryUrl,
|
|
4493
|
+
packageJson,
|
|
4494
|
+
url: new URL("index.js", packageDirectoryUrl).href,
|
|
4495
|
+
};
|
|
4496
|
+
};
|
|
4497
|
+
const mainLegacyResolvers = {
|
|
4498
|
+
import: ({ packageJson }) => {
|
|
4499
|
+
if (typeof packageJson.module === "string") {
|
|
4500
|
+
return { type: "field:module", isMain: true, path: packageJson.module };
|
|
4501
|
+
}
|
|
4502
|
+
if (typeof packageJson.jsnext === "string") {
|
|
4503
|
+
return { type: "field:jsnext", isMain: true, path: packageJson.jsnext };
|
|
4504
|
+
}
|
|
4505
|
+
if (typeof packageJson.main === "string") {
|
|
4506
|
+
return { type: "field:main", isMain: true, path: packageJson.main };
|
|
4507
|
+
}
|
|
4508
|
+
return null;
|
|
4509
|
+
},
|
|
4510
|
+
browser: ({ packageDirectoryUrl, packageJson }) => {
|
|
4511
|
+
const browserMain = (() => {
|
|
4512
|
+
if (typeof packageJson.browser === "string") {
|
|
4513
|
+
return packageJson.browser;
|
|
4514
|
+
}
|
|
4515
|
+
if (
|
|
4516
|
+
typeof packageJson.browser === "object" &&
|
|
4517
|
+
packageJson.browser !== null
|
|
4518
|
+
) {
|
|
4519
|
+
return packageJson.browser["."];
|
|
4520
|
+
}
|
|
4521
|
+
return "";
|
|
4522
|
+
})();
|
|
4523
|
+
|
|
4524
|
+
if (!browserMain) {
|
|
4525
|
+
if (typeof packageJson.module === "string") {
|
|
4526
|
+
return {
|
|
4527
|
+
type: "field:module",
|
|
4528
|
+
isMain: true,
|
|
4529
|
+
path: packageJson.module,
|
|
4530
|
+
};
|
|
4531
|
+
}
|
|
4532
|
+
return null;
|
|
4533
|
+
}
|
|
4534
|
+
if (
|
|
4535
|
+
typeof packageJson.module !== "string" ||
|
|
4536
|
+
packageJson.module === browserMain
|
|
4537
|
+
) {
|
|
4538
|
+
return {
|
|
4539
|
+
type: "field:browser",
|
|
4540
|
+
isMain: true,
|
|
4541
|
+
path: browserMain,
|
|
4542
|
+
};
|
|
4543
|
+
}
|
|
4544
|
+
const browserMainUrlObject = new URL(browserMain, packageDirectoryUrl);
|
|
4545
|
+
const content = readFileSync(browserMainUrlObject, "utf-8");
|
|
4546
|
+
if (
|
|
4547
|
+
(/typeof exports\s*==/.test(content) &&
|
|
4548
|
+
/typeof module\s*==/.test(content)) ||
|
|
4549
|
+
/module\.exports\s*=/.test(content)
|
|
4550
|
+
) {
|
|
4551
|
+
return {
|
|
4552
|
+
type: "field:module",
|
|
4553
|
+
isMain: true,
|
|
4554
|
+
path: packageJson.module,
|
|
4555
|
+
};
|
|
4556
|
+
}
|
|
4557
|
+
return {
|
|
4558
|
+
type: "field:browser",
|
|
4559
|
+
isMain: true,
|
|
4560
|
+
path: browserMain,
|
|
4561
|
+
};
|
|
4562
|
+
},
|
|
4563
|
+
node: ({ packageJson, conditions }) => {
|
|
4564
|
+
if (conditions.includes("import")) {
|
|
4565
|
+
if (typeof packageJson.module === "string") {
|
|
4566
|
+
return { type: "field:module", isMain: true, path: packageJson.module };
|
|
4567
|
+
}
|
|
4568
|
+
if (typeof packageJson.jsnext === "string") {
|
|
4569
|
+
return { type: "field:jsnext", isMain: true, path: packageJson.jsnext };
|
|
4570
|
+
}
|
|
4571
|
+
}
|
|
4572
|
+
if (typeof packageJson.main === "string") {
|
|
4573
|
+
return { type: "field:main", isMain: true, path: packageJson.main };
|
|
4574
|
+
}
|
|
4575
|
+
return null;
|
|
4576
|
+
},
|
|
4577
|
+
};
|
|
4578
|
+
|
|
4579
|
+
const comparePatternKeys = (keyA, keyB) => {
|
|
4580
|
+
if (!keyA.endsWith("/") && !keyA.includes("*")) {
|
|
4581
|
+
throw new Error("Invalid package configuration");
|
|
4582
|
+
}
|
|
4583
|
+
if (!keyB.endsWith("/") && !keyB.includes("*")) {
|
|
4584
|
+
throw new Error("Invalid package configuration");
|
|
4585
|
+
}
|
|
4586
|
+
const aStarIndex = keyA.indexOf("*");
|
|
4587
|
+
const baseLengthA = aStarIndex > -1 ? aStarIndex + 1 : keyA.length;
|
|
4588
|
+
const bStarIndex = keyB.indexOf("*");
|
|
4589
|
+
const baseLengthB = bStarIndex > -1 ? bStarIndex + 1 : keyB.length;
|
|
4590
|
+
if (baseLengthA > baseLengthB) {
|
|
4591
|
+
return -1;
|
|
4592
|
+
}
|
|
4593
|
+
if (baseLengthB > baseLengthA) {
|
|
4594
|
+
return 1;
|
|
4595
|
+
}
|
|
4596
|
+
if (aStarIndex === -1) {
|
|
4597
|
+
return 1;
|
|
4598
|
+
}
|
|
4599
|
+
if (bStarIndex === -1) {
|
|
4600
|
+
return -1;
|
|
4601
|
+
}
|
|
4602
|
+
if (keyA.length > keyB.length) {
|
|
4603
|
+
return -1;
|
|
4604
|
+
}
|
|
4605
|
+
if (keyB.length > keyA.length) {
|
|
4606
|
+
return 1;
|
|
3825
4607
|
}
|
|
4608
|
+
return 0;
|
|
4609
|
+
};
|
|
3826
4610
|
|
|
3827
|
-
|
|
3828
|
-
const
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
);
|
|
4611
|
+
const resolvePackageSymlink = (packageDirectoryUrl) => {
|
|
4612
|
+
const packageDirectoryPath = realpathSync(new URL(packageDirectoryUrl));
|
|
4613
|
+
const packageDirectoryResolvedUrl = pathToFileURL(packageDirectoryPath).href;
|
|
4614
|
+
return `${packageDirectoryResolvedUrl}/`;
|
|
3832
4615
|
};
|
|
3833
4616
|
|
|
3834
4617
|
const countAvailableCpus = () => {
|
|
@@ -8646,6 +9429,86 @@ const createWekbitRuntime = (params) => {
|
|
|
8646
9429
|
});
|
|
8647
9430
|
};
|
|
8648
9431
|
|
|
9432
|
+
const findFreePort = async (
|
|
9433
|
+
initialPort = 1,
|
|
9434
|
+
{
|
|
9435
|
+
signal = new AbortController().signal,
|
|
9436
|
+
hostname = "127.0.0.1",
|
|
9437
|
+
min = 1,
|
|
9438
|
+
max = 65534,
|
|
9439
|
+
next = (port) => port + 1,
|
|
9440
|
+
} = {},
|
|
9441
|
+
) => {
|
|
9442
|
+
const findFreePortOperation = Abort.startOperation();
|
|
9443
|
+
try {
|
|
9444
|
+
findFreePortOperation.addAbortSignal(signal);
|
|
9445
|
+
findFreePortOperation.throwIfAborted();
|
|
9446
|
+
|
|
9447
|
+
const testUntil = async (port, host) => {
|
|
9448
|
+
findFreePortOperation.throwIfAborted();
|
|
9449
|
+
const free = await portIsFree(port, host);
|
|
9450
|
+
if (free) {
|
|
9451
|
+
return port;
|
|
9452
|
+
}
|
|
9453
|
+
|
|
9454
|
+
const nextPort = next(port);
|
|
9455
|
+
if (nextPort > max) {
|
|
9456
|
+
throw new Error(
|
|
9457
|
+
`${hostname} has no available port between ${min} and ${max}`,
|
|
9458
|
+
);
|
|
9459
|
+
}
|
|
9460
|
+
return testUntil(nextPort, hostname);
|
|
9461
|
+
};
|
|
9462
|
+
const freePort = await testUntil(initialPort, hostname);
|
|
9463
|
+
return freePort;
|
|
9464
|
+
} finally {
|
|
9465
|
+
await findFreePortOperation.end();
|
|
9466
|
+
}
|
|
9467
|
+
};
|
|
9468
|
+
|
|
9469
|
+
const portIsFree = async (port, hostname) => {
|
|
9470
|
+
const server = createServer();
|
|
9471
|
+
|
|
9472
|
+
try {
|
|
9473
|
+
await startListening({
|
|
9474
|
+
server,
|
|
9475
|
+
port,
|
|
9476
|
+
hostname,
|
|
9477
|
+
});
|
|
9478
|
+
} catch (error) {
|
|
9479
|
+
if (error && error.code === "EADDRINUSE") {
|
|
9480
|
+
return false;
|
|
9481
|
+
}
|
|
9482
|
+
if (error && error.code === "EACCES") {
|
|
9483
|
+
return false;
|
|
9484
|
+
}
|
|
9485
|
+
throw error;
|
|
9486
|
+
}
|
|
9487
|
+
|
|
9488
|
+
await stopListening(server);
|
|
9489
|
+
return true;
|
|
9490
|
+
};
|
|
9491
|
+
|
|
9492
|
+
const startListening = ({ server, port, hostname }) => {
|
|
9493
|
+
return new Promise((resolve, reject) => {
|
|
9494
|
+
server.on("error", reject);
|
|
9495
|
+
server.on("listening", () => {
|
|
9496
|
+
// in case port is 0 (randomly assign an available port)
|
|
9497
|
+
// https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback
|
|
9498
|
+
resolve(server.address().port);
|
|
9499
|
+
});
|
|
9500
|
+
server.listen(port, hostname);
|
|
9501
|
+
});
|
|
9502
|
+
};
|
|
9503
|
+
|
|
9504
|
+
const stopListening = (server) => {
|
|
9505
|
+
return new Promise((resolve, reject) => {
|
|
9506
|
+
server.on("error", reject);
|
|
9507
|
+
server.on("close", resolve);
|
|
9508
|
+
server.close();
|
|
9509
|
+
});
|
|
9510
|
+
};
|
|
9511
|
+
|
|
8649
9512
|
const ExecOptions = {
|
|
8650
9513
|
fromExecArgv: (execArgv) => {
|
|
8651
9514
|
const execOptions = {};
|