@argos-ci/core 6.7.2 → 6.7.4-alpha.1.sha-4c8577c
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/index.d.mts +91 -3
- package/dist/index.mjs +388 -9
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { ArgosAPISchema } from "@argos-ci/api-client";
|
|
2
2
|
import { ScreenshotMetadata } from "@argos-ci/util";
|
|
3
3
|
//#region src/config.d.ts
|
|
4
|
+
/** Argos API used unless one is named explicitly or by `ARGOS_API_BASE_URL`. */
|
|
5
|
+
declare const DEFAULT_API_BASE_URL = "https://api.argos-ci.com/v2/";
|
|
4
6
|
interface Config {
|
|
5
7
|
/**
|
|
6
8
|
* Base URL of the Argos API.
|
|
@@ -133,7 +135,22 @@ interface Config {
|
|
|
133
135
|
*/
|
|
134
136
|
subset?: boolean;
|
|
135
137
|
}
|
|
136
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Options that change how the configuration is read, as opposed to what is in it.
|
|
140
|
+
*/
|
|
141
|
+
type ReadConfigOptions = {
|
|
142
|
+
/**
|
|
143
|
+
* Require a branch and a commit, which every build-shaped command needs to
|
|
144
|
+
* describe what it is reporting on.
|
|
145
|
+
*
|
|
146
|
+
* Set to `false` for a command that has nothing to do with a commit — uploading
|
|
147
|
+
* a standalone media, say. Without it those commands cannot run outside a git
|
|
148
|
+
* repository at all, and the error they fail with talks about branches.
|
|
149
|
+
* @default true
|
|
150
|
+
*/
|
|
151
|
+
requireGitContext?: boolean;
|
|
152
|
+
};
|
|
153
|
+
declare function readConfig(options?: Partial<Config>, { requireGitContext }?: ReadConfigOptions): Promise<Config>;
|
|
137
154
|
declare function getConfigFromOptions({ parallel, ...options }: Omit<Partial<Config>, "parallel"> & {
|
|
138
155
|
parallel?: {
|
|
139
156
|
/** Unique build ID for this parallel build */
|
|
@@ -143,7 +160,7 @@ declare function getConfigFromOptions({ parallel, ...options }: Omit<Partial<Con
|
|
|
143
160
|
/** The index of the parallel node */
|
|
144
161
|
index?: number;
|
|
145
162
|
} | false | undefined;
|
|
146
|
-
}): Promise<Config>;
|
|
163
|
+
}, readOptions?: ReadConfigOptions): Promise<Config>;
|
|
147
164
|
//#endregion
|
|
148
165
|
//#region src/deploy.d.ts
|
|
149
166
|
interface DeployParameters {
|
|
@@ -233,6 +250,77 @@ type FinalizeResult = {
|
|
|
233
250
|
*/
|
|
234
251
|
declare function finalize(params: FinalizeParameters): Promise<FinalizeResult>;
|
|
235
252
|
//#endregion
|
|
253
|
+
//#region src/media.d.ts
|
|
254
|
+
type Media = ArgosAPISchema.components["schemas"]["Media"];
|
|
255
|
+
/** Which half of a before/after pair a media is. */
|
|
256
|
+
type MediaState = NonNullable<Media["state"]>;
|
|
257
|
+
/** Who can open a media's share page. */
|
|
258
|
+
type MediaVisibility = Media["visibility"];
|
|
259
|
+
interface UploadMediaParameters {
|
|
260
|
+
/**
|
|
261
|
+
* Paths of the files to upload.
|
|
262
|
+
*/
|
|
263
|
+
files: string[];
|
|
264
|
+
/**
|
|
265
|
+
* Argos token. Falls back to `ARGOS_TOKEN` and to CI tokenless
|
|
266
|
+
* authentication, like every other Argos command.
|
|
267
|
+
*/
|
|
268
|
+
token?: string;
|
|
269
|
+
/**
|
|
270
|
+
* Base URL of the Argos API.
|
|
271
|
+
* @default "https://api.argos-ci.com/v2/"
|
|
272
|
+
*/
|
|
273
|
+
apiBaseUrl?: string;
|
|
274
|
+
/**
|
|
275
|
+
* Project to upload to, as `owner/project`. Required when authenticating with
|
|
276
|
+
* a personal access token; ignored with a project token, which already
|
|
277
|
+
* identifies its project.
|
|
278
|
+
*/
|
|
279
|
+
project?: string;
|
|
280
|
+
/**
|
|
281
|
+
* Branch the media belongs to. Use this when the pull request does not exist
|
|
282
|
+
* yet: the media is staged, and Argos publishes it — and posts the pull
|
|
283
|
+
* request comment — on its own once a pull request opens for that branch.
|
|
284
|
+
*/
|
|
285
|
+
branch?: string;
|
|
286
|
+
/**
|
|
287
|
+
* Pull request to publish the media to. Argos keeps a single comment on it
|
|
288
|
+
* listing every media uploaded, edited in place.
|
|
289
|
+
*/
|
|
290
|
+
prNumber?: number;
|
|
291
|
+
/**
|
|
292
|
+
* Which half of a before/after pair these files are. Inferred from a file name
|
|
293
|
+
* ending in `-before` or `-after`; pass this to override it.
|
|
294
|
+
*/
|
|
295
|
+
state?: MediaState;
|
|
296
|
+
/**
|
|
297
|
+
* Prose shown under the media in the pull request comment.
|
|
298
|
+
*/
|
|
299
|
+
description?: string;
|
|
300
|
+
/**
|
|
301
|
+
* Who can open the share page. `team` requires an Argos session; `public` only
|
|
302
|
+
* requires the URL. Defaults to the most private option the plan allows.
|
|
303
|
+
*/
|
|
304
|
+
visibility?: MediaVisibility;
|
|
305
|
+
/**
|
|
306
|
+
* Convert images to WebP before uploading, which is what makes a screenshot
|
|
307
|
+
* upload fast. Set to `false` to upload the file exactly as it is.
|
|
308
|
+
* @default true
|
|
309
|
+
*/
|
|
310
|
+
compress?: boolean;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Upload standalone images or videos to Argos and get back shareable URLs with
|
|
314
|
+
* ready-to-paste Markdown.
|
|
315
|
+
*
|
|
316
|
+
* Not tied to a build or a test run: this is for the screenshot or screen
|
|
317
|
+
* recording you want to put in a pull request, a changelog or a chat message.
|
|
318
|
+
*
|
|
319
|
+
* Argos stores the bytes as given and serves them from its image CDN, so a media
|
|
320
|
+
* is ready the moment the upload finishes — there is no processing to wait on.
|
|
321
|
+
*/
|
|
322
|
+
declare function uploadMedia(params: UploadMediaParameters): Promise<Media[]>;
|
|
323
|
+
//#endregion
|
|
236
324
|
//#region src/mime-type.d.ts
|
|
237
325
|
/**
|
|
238
326
|
* Get the mime type of a snapshot file based on its extension.
|
|
@@ -375,4 +463,4 @@ declare function skip(params: SkipParameters): Promise<{
|
|
|
375
463
|
build: ArgosAPISchema.components["schemas"]["Build"];
|
|
376
464
|
}>;
|
|
377
465
|
//#endregion
|
|
378
|
-
export { Config, DeployParameters, FinalizeParameters, FinalizeResult, UploadParameters, deploy, finalize, getConfigFromOptions, getSnapshotMimeType, readConfig, skip, upload };
|
|
466
|
+
export { Config, DEFAULT_API_BASE_URL, DeployParameters, FinalizeParameters, FinalizeResult, Media, MediaState, MediaVisibility, ReadConfigOptions, UploadMediaParameters, UploadParameters, deploy, finalize, getConfigFromOptions, getSnapshotMimeType, readConfig, skip, upload, uploadMedia };
|
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { promisify } from "node:util";
|
|
|
5
5
|
import pRetry from "p-retry";
|
|
6
6
|
import createDebug from "debug";
|
|
7
7
|
import { createReadStream, existsSync, readFileSync } from "node:fs";
|
|
8
|
-
import { readFile, stat } from "node:fs/promises";
|
|
8
|
+
import { open, readFile, stat } from "node:fs/promises";
|
|
9
9
|
import { basename, extname, join, resolve } from "node:path";
|
|
10
10
|
import glob from "fast-glob";
|
|
11
11
|
import mime from "mime-types";
|
|
@@ -860,7 +860,10 @@ const mustBeCommit = (value) => {
|
|
|
860
860
|
throw new Error("Invalid commit");
|
|
861
861
|
}
|
|
862
862
|
};
|
|
863
|
+
/** Prefix of the OAuth access token `argos login` stores. */
|
|
864
|
+
const OAUTH_ACCESS_TOKEN_PREFIX = "argos_oat_";
|
|
863
865
|
const mustBeArgosToken = (value) => {
|
|
866
|
+
if (typeof value === "string" && value.startsWith(OAUTH_ACCESS_TOKEN_PREFIX)) return;
|
|
864
867
|
if (value && value.length !== 40) throw new Error("Invalid Argos repository token (must be 40 characters)");
|
|
865
868
|
};
|
|
866
869
|
const minInteger = (min) => (value) => {
|
|
@@ -905,21 +908,25 @@ convict.addFormat({
|
|
|
905
908
|
},
|
|
906
909
|
coerce: toIntArray
|
|
907
910
|
});
|
|
911
|
+
/** Argos API used unless one is named explicitly or by `ARGOS_API_BASE_URL`. */
|
|
912
|
+
const DEFAULT_API_BASE_URL = "https://api.argos-ci.com/v2/";
|
|
908
913
|
const schema = {
|
|
909
914
|
apiBaseUrl: {
|
|
910
915
|
env: "ARGOS_API_BASE_URL",
|
|
911
|
-
default:
|
|
916
|
+
default: DEFAULT_API_BASE_URL,
|
|
912
917
|
format: mustBeApiBaseUrl
|
|
913
918
|
},
|
|
914
919
|
commit: {
|
|
915
920
|
env: "ARGOS_COMMIT",
|
|
916
921
|
default: null,
|
|
917
|
-
format: mustBeCommit
|
|
922
|
+
format: mustBeCommit,
|
|
923
|
+
nullable: true
|
|
918
924
|
},
|
|
919
925
|
branch: {
|
|
920
926
|
env: "ARGOS_BRANCH",
|
|
921
927
|
default: null,
|
|
922
|
-
format: String
|
|
928
|
+
format: String,
|
|
929
|
+
nullable: true
|
|
923
930
|
},
|
|
924
931
|
token: {
|
|
925
932
|
env: "ARGOS_TOKEN",
|
|
@@ -1072,7 +1079,7 @@ function getDefaultConfig() {
|
|
|
1072
1079
|
return cfg;
|
|
1073
1080
|
}, {});
|
|
1074
1081
|
}
|
|
1075
|
-
async function readConfig(options = {}) {
|
|
1082
|
+
async function readConfig(options = {}, { requireGitContext = true } = {}) {
|
|
1076
1083
|
const config = createConfig();
|
|
1077
1084
|
const ciEnv = await getCiEnvironment();
|
|
1078
1085
|
const defaultConfig = getDefaultConfig();
|
|
@@ -1105,18 +1112,18 @@ async function readConfig(options = {}) {
|
|
|
1105
1112
|
subset: options.subset ?? defaultConfig.subset ?? false,
|
|
1106
1113
|
mergeQueuePrNumbers: options.mergeQueuePrNumbers ?? defaultConfig.mergeQueuePrNumbers ?? ciEnv?.mergeQueuePrNumbers ?? null
|
|
1107
1114
|
});
|
|
1108
|
-
if (!config.get("branch") || !config.get("commit")) throw new Error("Argos requires a branch and a commit to be set. If you are running in a non-git environment consider setting ARGOS_BRANCH and ARGOS_COMMIT environment variables.");
|
|
1115
|
+
if (requireGitContext && (!config.get("branch") || !config.get("commit"))) throw new Error("Argos requires a branch and a commit to be set. If you are running in a non-git environment consider setting ARGOS_BRANCH and ARGOS_COMMIT environment variables.");
|
|
1109
1116
|
config.validate();
|
|
1110
1117
|
return config.get();
|
|
1111
1118
|
}
|
|
1112
|
-
async function getConfigFromOptions({ parallel, ...options }) {
|
|
1119
|
+
async function getConfigFromOptions({ parallel, ...options }, readOptions) {
|
|
1113
1120
|
return readConfig({
|
|
1114
1121
|
...options,
|
|
1115
1122
|
parallel: parallel !== void 0 ? Boolean(parallel) : void 0,
|
|
1116
1123
|
parallelNonce: parallel ? parallel.nonce : void 0,
|
|
1117
1124
|
parallelTotal: parallel ? parallel.total : void 0,
|
|
1118
1125
|
parallelIndex: parallel ? parallel.index : void 0
|
|
1119
|
-
});
|
|
1126
|
+
}, readOptions);
|
|
1120
1127
|
}
|
|
1121
1128
|
//#endregion
|
|
1122
1129
|
//#region src/hashing.ts
|
|
@@ -1512,6 +1519,378 @@ async function finalize(params) {
|
|
|
1512
1519
|
};
|
|
1513
1520
|
}
|
|
1514
1521
|
//#endregion
|
|
1522
|
+
//#region src/media-compress.ts
|
|
1523
|
+
/**
|
|
1524
|
+
* Content types converted to WebP before upload.
|
|
1525
|
+
*
|
|
1526
|
+
* PNG and JPEG only. `image/webp` is already there; `image/avif` compresses
|
|
1527
|
+
* better than WebP, so converting it would make the file bigger; `image/gif` may
|
|
1528
|
+
* be animated, and an animated image is the one case where re-encoding risks
|
|
1529
|
+
* dropping frames rather than saving bytes.
|
|
1530
|
+
*/
|
|
1531
|
+
const COMPRESSIBLE_CONTENT_TYPES = /* @__PURE__ */ new Set(["image/png", "image/jpeg"]);
|
|
1532
|
+
/**
|
|
1533
|
+
* Longest side WebP can encode. A full-page screenshot goes past it more often
|
|
1534
|
+
* than you would think — 1440 wide by 20000 tall is an ordinary "capture the
|
|
1535
|
+
* whole scroll" result — and sharp throws rather than clamping.
|
|
1536
|
+
*/
|
|
1537
|
+
const WEBP_MAX_DIMENSION = 16383;
|
|
1538
|
+
/**
|
|
1539
|
+
* Most pixels worth decoding, matching the screenshot pipeline's own budget.
|
|
1540
|
+
*
|
|
1541
|
+
* The per-side limit above is not a bound on work: 16383x16383 clears it and is
|
|
1542
|
+
* 268 million pixels, over a gigabyte of raw RGBA before anything is encoded,
|
|
1543
|
+
* which is enough to take a CI container down over one upload.
|
|
1544
|
+
*/
|
|
1545
|
+
const MAX_PIXELS$1 = 8e7;
|
|
1546
|
+
/**
|
|
1547
|
+
* Quality passed to the WebP encoder.
|
|
1548
|
+
*
|
|
1549
|
+
* 85 rather than sharp's default 80: these files are looked at by a human
|
|
1550
|
+
* judging a UI, so text and 1px borders have to survive. It still lands an order
|
|
1551
|
+
* of magnitude under the source PNG.
|
|
1552
|
+
*/
|
|
1553
|
+
const WEBP_QUALITY = 85;
|
|
1554
|
+
/**
|
|
1555
|
+
* How much of a PNG's head to read looking for an animation control chunk. `acTL`
|
|
1556
|
+
* has to precede the first `IDAT`, and the chunks before it are a fixed handful,
|
|
1557
|
+
* so it is always inside the first few hundred bytes.
|
|
1558
|
+
*/
|
|
1559
|
+
const PNG_HEAD_BYTES = 4096;
|
|
1560
|
+
/**
|
|
1561
|
+
* Convert an image to WebP in a temporary file, and hand back what to upload.
|
|
1562
|
+
*
|
|
1563
|
+
* Screenshots are the bulk of what gets uploaded here and PNG is the worst
|
|
1564
|
+
* possible container for them: a 1440x900 UI capture is routinely 1–2 MB as PNG
|
|
1565
|
+
* and under 100 KB as WebP. That is the difference between an upload an agent
|
|
1566
|
+
* waits on and one it does not, and it is what keeps a long screen recording's
|
|
1567
|
+
* companion screenshots inside the plan's file size limit.
|
|
1568
|
+
*
|
|
1569
|
+
* Returns the original file untouched — same path, same content type — whenever
|
|
1570
|
+
* converting would not help or cannot be done: a video, an already-efficient
|
|
1571
|
+
* format, an animated image, an image too large for the encoder, bytes that came
|
|
1572
|
+
* out no smaller, or any failure along the way. Compression is an optimization,
|
|
1573
|
+
* so a file Argos could have uploaded as-is must not be turned into a failed
|
|
1574
|
+
* upload by it. The caller uploads whatever comes back.
|
|
1575
|
+
*/
|
|
1576
|
+
async function compressMediaToWebp(source) {
|
|
1577
|
+
const keepOriginal = {
|
|
1578
|
+
source,
|
|
1579
|
+
cleanup: () => {}
|
|
1580
|
+
};
|
|
1581
|
+
if (!COMPRESSIBLE_CONTENT_TYPES.has(source.contentType)) {
|
|
1582
|
+
debug(`Not compressing ${source.path}: ${source.contentType} is left as-is`);
|
|
1583
|
+
return keepOriginal;
|
|
1584
|
+
}
|
|
1585
|
+
let temporary = null;
|
|
1586
|
+
try {
|
|
1587
|
+
if (await checkIsAnimatedPng(source)) {
|
|
1588
|
+
debug(`Not compressing ${source.path}: animated PNG`);
|
|
1589
|
+
return keepOriginal;
|
|
1590
|
+
}
|
|
1591
|
+
const [metadata, originalStats] = await Promise.all([sharp(source.path).metadata(), stat(source.path)]);
|
|
1592
|
+
const { width, height } = metadata;
|
|
1593
|
+
if (width && width > WEBP_MAX_DIMENSION || height && height > WEBP_MAX_DIMENSION) {
|
|
1594
|
+
debug(`Not compressing ${source.path}: ${width}x${height} exceeds WebP's ${WEBP_MAX_DIMENSION}px limit`);
|
|
1595
|
+
return keepOriginal;
|
|
1596
|
+
}
|
|
1597
|
+
if (width && height && width * height > MAX_PIXELS$1) {
|
|
1598
|
+
debug(`Not compressing ${source.path}: ${width * height} pixels exceeds the ${MAX_PIXELS$1} budget`);
|
|
1599
|
+
return keepOriginal;
|
|
1600
|
+
}
|
|
1601
|
+
temporary = await createTemporaryFile();
|
|
1602
|
+
const { size } = await sharp(source.path).rotate().webp({ quality: WEBP_QUALITY }).toFile(temporary.path);
|
|
1603
|
+
if (size >= originalStats.size) {
|
|
1604
|
+
debug(`Not compressing ${source.path}: WebP is larger (${size} >= ${originalStats.size} bytes)`);
|
|
1605
|
+
temporary.remove();
|
|
1606
|
+
return keepOriginal;
|
|
1607
|
+
}
|
|
1608
|
+
debug(`Compressed ${source.path} to WebP: ${originalStats.size} → ${size} bytes`);
|
|
1609
|
+
const converted = temporary;
|
|
1610
|
+
return {
|
|
1611
|
+
source: {
|
|
1612
|
+
path: converted.path,
|
|
1613
|
+
contentType: "image/webp"
|
|
1614
|
+
},
|
|
1615
|
+
cleanup: converted.remove
|
|
1616
|
+
};
|
|
1617
|
+
} catch (error) {
|
|
1618
|
+
const message = error instanceof Error ? error.message : "Unknown Error";
|
|
1619
|
+
debug(`Not compressing ${source.path}: ${message}`);
|
|
1620
|
+
temporary?.remove();
|
|
1621
|
+
return keepOriginal;
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Create a temporary file and keep the callback that deletes it.
|
|
1626
|
+
*
|
|
1627
|
+
* Not `promisify(tmp.file)`: that resolves with the path alone and drops both the
|
|
1628
|
+
* open descriptor and the remove callback, which leaks one of each per file —
|
|
1629
|
+
* `tmp` only cleans up at exit when `setGracefulCleanup()` was called, and even
|
|
1630
|
+
* then it would hold every file for the whole run.
|
|
1631
|
+
*/
|
|
1632
|
+
function createTemporaryFile() {
|
|
1633
|
+
return new Promise((resolve, reject) => {
|
|
1634
|
+
tmp.file({
|
|
1635
|
+
prefix: "argos-media-",
|
|
1636
|
+
postfix: ".webp",
|
|
1637
|
+
discardDescriptor: true
|
|
1638
|
+
}, (error, path, _fd, removeCallback) => {
|
|
1639
|
+
if (error) {
|
|
1640
|
+
reject(error);
|
|
1641
|
+
return;
|
|
1642
|
+
}
|
|
1643
|
+
resolve({
|
|
1644
|
+
path,
|
|
1645
|
+
remove: () => {
|
|
1646
|
+
try {
|
|
1647
|
+
removeCallback();
|
|
1648
|
+
} catch {}
|
|
1649
|
+
}
|
|
1650
|
+
});
|
|
1651
|
+
});
|
|
1652
|
+
});
|
|
1653
|
+
}
|
|
1654
|
+
/**
|
|
1655
|
+
* Whether a PNG carries an `acTL` chunk, which makes it an APNG.
|
|
1656
|
+
*
|
|
1657
|
+
* Read off the bytes rather than from sharp: libvips reports no page count for an
|
|
1658
|
+
* APNG, so `metadata.pages` cannot tell an animation from a still image here.
|
|
1659
|
+
*/
|
|
1660
|
+
async function checkIsAnimatedPng(source) {
|
|
1661
|
+
if (source.contentType !== "image/png") return false;
|
|
1662
|
+
const file = await open(source.path);
|
|
1663
|
+
try {
|
|
1664
|
+
const { buffer, bytesRead } = await file.read({ buffer: Buffer.alloc(PNG_HEAD_BYTES) });
|
|
1665
|
+
return buffer.subarray(0, bytesRead).includes("acTL", 0, "ascii");
|
|
1666
|
+
} finally {
|
|
1667
|
+
await file.close();
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
//#endregion
|
|
1671
|
+
//#region src/media.ts
|
|
1672
|
+
/**
|
|
1673
|
+
* Content types Argos accepts for a standalone media upload. Kept here so a
|
|
1674
|
+
* caller learns the file is unsupported before anything is uploaded.
|
|
1675
|
+
*/
|
|
1676
|
+
const SUPPORTED_CONTENT_TYPES = /* @__PURE__ */ new Set([
|
|
1677
|
+
"image/png",
|
|
1678
|
+
"image/jpeg",
|
|
1679
|
+
"image/webp",
|
|
1680
|
+
"image/avif",
|
|
1681
|
+
"image/gif",
|
|
1682
|
+
"video/mp4",
|
|
1683
|
+
"video/webm",
|
|
1684
|
+
"video/quicktime"
|
|
1685
|
+
]);
|
|
1686
|
+
/**
|
|
1687
|
+
* Extensions `mime-types` gets wrong or does not know for the formats we accept.
|
|
1688
|
+
* `.mov` in particular resolves to `video/quicktime` on most systems but not all.
|
|
1689
|
+
*/
|
|
1690
|
+
const CONTENT_TYPE_BY_EXTENSION = {
|
|
1691
|
+
".mov": "video/quicktime",
|
|
1692
|
+
".avif": "image/avif"
|
|
1693
|
+
};
|
|
1694
|
+
/**
|
|
1695
|
+
* A file name ending in `-before` or `-after`, which is a caller labelling half
|
|
1696
|
+
* of a pair rather than naming a file. Mirrors the server's own parsing, so the
|
|
1697
|
+
* two halves end up sharing one name and get compared side by side.
|
|
1698
|
+
*/
|
|
1699
|
+
const STATE_SUFFIX_REGEX = /^(.*)-(before|after)(\.[^.]+)?$/i;
|
|
1700
|
+
/**
|
|
1701
|
+
* Upload standalone images or videos to Argos and get back shareable URLs with
|
|
1702
|
+
* ready-to-paste Markdown.
|
|
1703
|
+
*
|
|
1704
|
+
* Not tied to a build or a test run: this is for the screenshot or screen
|
|
1705
|
+
* recording you want to put in a pull request, a changelog or a chat message.
|
|
1706
|
+
*
|
|
1707
|
+
* Argos stores the bytes as given and serves them from its image CDN, so a media
|
|
1708
|
+
* is ready the moment the upload finishes — there is no processing to wait on.
|
|
1709
|
+
*/
|
|
1710
|
+
async function uploadMedia(params) {
|
|
1711
|
+
const { token: _token, ...debugParams } = params;
|
|
1712
|
+
debug("Starting media upload with params", debugParams);
|
|
1713
|
+
if (params.files.length === 0) throw new Error("No files to upload");
|
|
1714
|
+
const identities = params.files.map((filepath) => resolveMediaIdentity(filepath, params.state));
|
|
1715
|
+
const contentTypes = params.files.map((filepath) => getMediaContentType(filepath));
|
|
1716
|
+
assertDistinctIdentities(params.files, identities);
|
|
1717
|
+
await assertFilesAreReadable(params.files);
|
|
1718
|
+
const { authToken, apiBaseUrl, project } = await resolveAuth(params);
|
|
1719
|
+
const apiClient = createClient({
|
|
1720
|
+
baseUrl: apiBaseUrl,
|
|
1721
|
+
authToken
|
|
1722
|
+
});
|
|
1723
|
+
const results = [];
|
|
1724
|
+
for (const [index, filepath] of params.files.entries()) results.push(await uploadOne({
|
|
1725
|
+
apiClient,
|
|
1726
|
+
params,
|
|
1727
|
+
project,
|
|
1728
|
+
source: {
|
|
1729
|
+
path: filepath,
|
|
1730
|
+
contentType: contentTypes[index]
|
|
1731
|
+
},
|
|
1732
|
+
identity: identities[index]
|
|
1733
|
+
}));
|
|
1734
|
+
return results;
|
|
1735
|
+
}
|
|
1736
|
+
async function uploadOne(args) {
|
|
1737
|
+
const { apiClient, params, project, identity } = args;
|
|
1738
|
+
const compressed = params.compress === false ? null : await compressMediaToWebp(args.source);
|
|
1739
|
+
try {
|
|
1740
|
+
return await registerAndUpload({
|
|
1741
|
+
...args,
|
|
1742
|
+
apiClient,
|
|
1743
|
+
params,
|
|
1744
|
+
project,
|
|
1745
|
+
identity,
|
|
1746
|
+
source: compressed?.source ?? args.source
|
|
1747
|
+
});
|
|
1748
|
+
} finally {
|
|
1749
|
+
compressed?.cleanup();
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
async function registerAndUpload(args) {
|
|
1753
|
+
const { apiClient, params, project, identity, source } = args;
|
|
1754
|
+
const [hash, stats] = await Promise.all([hashFile(source.path), stat(source.path)]);
|
|
1755
|
+
debug(`Registering media ${identity.name} (${stats.size} bytes)`);
|
|
1756
|
+
const createResponse = await apiClient.POST("/media", { body: {
|
|
1757
|
+
name: identity.name,
|
|
1758
|
+
state: identity.state,
|
|
1759
|
+
description: params.description || null,
|
|
1760
|
+
contentType: source.contentType,
|
|
1761
|
+
size: stats.size,
|
|
1762
|
+
hash,
|
|
1763
|
+
visibility: params.visibility ?? null,
|
|
1764
|
+
project,
|
|
1765
|
+
prNumber: params.prNumber ?? null,
|
|
1766
|
+
branch: params.branch ?? null
|
|
1767
|
+
} });
|
|
1768
|
+
if (createResponse.error) throwAPIError(createResponse.error, createResponse.response);
|
|
1769
|
+
const { media, upload } = createResponse.data;
|
|
1770
|
+
if (!upload) {
|
|
1771
|
+
debug(`Media already uploaded: ${media.url}`);
|
|
1772
|
+
return media;
|
|
1773
|
+
}
|
|
1774
|
+
debug(`Uploading ${source.path}`);
|
|
1775
|
+
await uploadFileWithPresignedPost({
|
|
1776
|
+
url: upload.url,
|
|
1777
|
+
fields: upload.fields,
|
|
1778
|
+
path: source.path,
|
|
1779
|
+
contentType: source.contentType
|
|
1780
|
+
});
|
|
1781
|
+
const finalizeResponse = await apiClient.POST("/media/{mediaId}/finalize", { params: { path: { mediaId: media.id } } });
|
|
1782
|
+
if (finalizeResponse.error) throwAPIError(finalizeResponse.error, finalizeResponse.response);
|
|
1783
|
+
debug(`Media uploaded: ${finalizeResponse.data.url}`);
|
|
1784
|
+
return finalizeResponse.data;
|
|
1785
|
+
}
|
|
1786
|
+
/**
|
|
1787
|
+
* Resolve what it takes to talk to the API: the token, the API to send to, and
|
|
1788
|
+
* the project to upload into.
|
|
1789
|
+
*
|
|
1790
|
+
* Only the three fields the configuration layer is being asked about are handed
|
|
1791
|
+
* to it — never the caller's whole parameter bag. `branch` and `prNumber` mean
|
|
1792
|
+
* something else there (the branch and pull request of a *build*), and a media's
|
|
1793
|
+
* are sent to the CI tokenless exchange as the ref being authorized, so passing
|
|
1794
|
+
* them through would authorize an upload against whatever branch it named rather
|
|
1795
|
+
* than the one the job is running on.
|
|
1796
|
+
*
|
|
1797
|
+
* `requireGitContext: false` because uploading a screenshot has nothing to do
|
|
1798
|
+
* with a commit: without it this cannot run outside a git repository at all, which
|
|
1799
|
+
* is exactly where an agent holding a screenshot in a scratch directory is.
|
|
1800
|
+
*/
|
|
1801
|
+
async function resolveAuth(params) {
|
|
1802
|
+
const config = await getConfigFromOptions({
|
|
1803
|
+
token: params.token,
|
|
1804
|
+
apiBaseUrl: params.apiBaseUrl,
|
|
1805
|
+
project: params.project
|
|
1806
|
+
}, { requireGitContext: false });
|
|
1807
|
+
return {
|
|
1808
|
+
authToken: await resolveArgosToken(config),
|
|
1809
|
+
apiBaseUrl: config.apiBaseUrl,
|
|
1810
|
+
project: config.project ?? null
|
|
1811
|
+
};
|
|
1812
|
+
}
|
|
1813
|
+
/**
|
|
1814
|
+
* Determine a file's content type, and refuse anything Argos does not accept
|
|
1815
|
+
* before spending a hash or a request on it.
|
|
1816
|
+
*/
|
|
1817
|
+
function getMediaContentType(filepath) {
|
|
1818
|
+
const extension = filepath.slice(filepath.lastIndexOf(".")).toLowerCase();
|
|
1819
|
+
const contentType = CONTENT_TYPE_BY_EXTENSION[extension] || mime.lookup(filepath);
|
|
1820
|
+
if (!contentType) throw new Error(`Unable to determine the file type of: ${filepath}`);
|
|
1821
|
+
if (!SUPPORTED_CONTENT_TYPES.has(contentType)) throw new Error(`Unsupported file type "${contentType}" for ${filepath}. Argos accepts ${[...SUPPORTED_CONTENT_TYPES].join(", ")}.`);
|
|
1822
|
+
return contentType;
|
|
1823
|
+
}
|
|
1824
|
+
/**
|
|
1825
|
+
* Read a media's identity off its path.
|
|
1826
|
+
*
|
|
1827
|
+
* `checkout-before.png` is a caller saying "this is the before", not a file
|
|
1828
|
+
* literally called that, so the suffix is lifted off the name — which is what
|
|
1829
|
+
* lets the two halves of a pair share one name and be shown side by side. An
|
|
1830
|
+
* explicit `state` replaces the one read from the name, but the name is stripped
|
|
1831
|
+
* either way: `checkout-before.png` uploaded as the after is still `checkout.png`.
|
|
1832
|
+
*/
|
|
1833
|
+
function resolveMediaIdentity(filepath, state) {
|
|
1834
|
+
const fileName = basename(filepath);
|
|
1835
|
+
const match = STATE_SUFFIX_REGEX.exec(fileName);
|
|
1836
|
+
if (!match) return {
|
|
1837
|
+
name: fileName,
|
|
1838
|
+
state: state ?? null
|
|
1839
|
+
};
|
|
1840
|
+
const [, stem, suffix, extension] = match;
|
|
1841
|
+
if (stem === void 0 || suffix === void 0) return {
|
|
1842
|
+
name: fileName,
|
|
1843
|
+
state: state ?? null
|
|
1844
|
+
};
|
|
1845
|
+
return {
|
|
1846
|
+
name: `${stem}${extension ?? ""}`,
|
|
1847
|
+
state: state ?? (suffix.toLowerCase() === "before" ? "before" : "after")
|
|
1848
|
+
};
|
|
1849
|
+
}
|
|
1850
|
+
/**
|
|
1851
|
+
* Refuse a batch in which two files resolve to the same identity.
|
|
1852
|
+
*
|
|
1853
|
+
* Name and state are what a media is: uploading two files that agree on both
|
|
1854
|
+
* would silently make the second a new version of the first, and only one of them
|
|
1855
|
+
* would be visible afterwards.
|
|
1856
|
+
*
|
|
1857
|
+
* The everyday way in takes no flags at all — `before/shot.png after/shot.png`,
|
|
1858
|
+
* two captures of the same screen kept in separate directories, both of which
|
|
1859
|
+
* upload as `shot.png` because the directory is not part of the name. An explicit
|
|
1860
|
+
* `state` over a hyphenated pair (`nav-before.png nav-after.png --state after`)
|
|
1861
|
+
* gets here too, by overwriting the label that told the two halves apart.
|
|
1862
|
+
*/
|
|
1863
|
+
/**
|
|
1864
|
+
* Refuse a batch containing a file that cannot be read.
|
|
1865
|
+
*
|
|
1866
|
+
* A typo or a glob that matched nothing has to fail here rather than when the
|
|
1867
|
+
* loop reaches it: by then the files before it have been created, billed and
|
|
1868
|
+
* finalized, there is nothing to resume, and the error a caller sees blames
|
|
1869
|
+
* whatever step happened to touch the path first.
|
|
1870
|
+
*/
|
|
1871
|
+
async function assertFilesAreReadable(files) {
|
|
1872
|
+
await Promise.all(files.map(async (filepath) => {
|
|
1873
|
+
try {
|
|
1874
|
+
await stat(filepath);
|
|
1875
|
+
} catch {
|
|
1876
|
+
throw new Error(`Cannot read file: ${filepath}`);
|
|
1877
|
+
}
|
|
1878
|
+
}));
|
|
1879
|
+
}
|
|
1880
|
+
function assertDistinctIdentities(files, identities) {
|
|
1881
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1882
|
+
for (const [index, identity] of identities.entries()) {
|
|
1883
|
+
const key = `${identity.name}\u0000${identity.state ?? ""}`;
|
|
1884
|
+
const previous = seen.get(key);
|
|
1885
|
+
const filepath = files[index];
|
|
1886
|
+
if (previous !== void 0) {
|
|
1887
|
+
const state = identity.state ? ` (${identity.state})` : "";
|
|
1888
|
+
throw new Error(`${previous} and ${filepath} would both upload as "${identity.name}"${state}, so the second would replace the first. Rename one of them, or upload them separately.`);
|
|
1889
|
+
}
|
|
1890
|
+
seen.set(key, filepath);
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
//#endregion
|
|
1515
1894
|
//#region src/mime-type.ts
|
|
1516
1895
|
/**
|
|
1517
1896
|
* Get the mime type of a snapshot file based on its extension.
|
|
@@ -1919,4 +2298,4 @@ function formatPreviewUrl(url, formatter) {
|
|
|
1919
2298
|
return new URL(urlObj.pathname + urlObj.search + urlObj.hash, formatter.baseUrl).href;
|
|
1920
2299
|
}
|
|
1921
2300
|
//#endregion
|
|
1922
|
-
export { deploy, finalize, getConfigFromOptions, getSnapshotMimeType, readConfig, skip, upload };
|
|
2301
|
+
export { DEFAULT_API_BASE_URL, deploy, finalize, getConfigFromOptions, getSnapshotMimeType, readConfig, skip, upload, uploadMedia };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@argos-ci/core",
|
|
3
3
|
"description": "Node.js SDK for visual testing with Argos.",
|
|
4
|
-
"version": "6.7.
|
|
4
|
+
"version": "6.7.4-alpha.1.sha-4c8577c",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"access": "public"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@argos-ci/api-client": "0.
|
|
43
|
-
"@argos-ci/util": "4.1.
|
|
42
|
+
"@argos-ci/api-client": "0.29.1-alpha.1.sha-4c8577c",
|
|
43
|
+
"@argos-ci/util": "4.1.1-alpha.11.sha-4c8577c",
|
|
44
44
|
"convict": "^6.2.5",
|
|
45
45
|
"debug": "^4.4.3",
|
|
46
46
|
"fast-glob": "^3.3.3",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"lint": "eslint .",
|
|
69
69
|
"test": "vitest"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "4c8577c08ff1d9681c5b951dc73665cc8b7955c8"
|
|
72
72
|
}
|