@git.zone/cli 12.1.1 → 12.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/templates/npm/.package.json +1 -1
- package/assets/templates/service/.package.json +1 -1
- package/assets/templates/wcc/.package.json +1 -1
- package/assets/templates/website/.package.json +1 -1
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/helpers.tssea.d.ts +2 -7
- package/dist_ts/helpers.tssea.js +20 -10
- package/dist_ts/helpers.versionsource.d.ts +23 -5
- package/dist_ts/helpers.versionsource.js +115 -37
- package/dist_ts/helpers.workflow.d.ts +60 -17
- package/dist_ts/helpers.workflow.js +156 -51
- package/dist_ts/mod_config/index.js +98 -31
- package/dist_ts/mod_release/classes.releasejournal.d.ts +63 -8
- package/dist_ts/mod_release/classes.releasejournal.js +283 -124
- package/dist_ts/mod_release/helpers.cargorelease.d.ts +4 -3
- package/dist_ts/mod_release/helpers.cargorelease.js +5 -4
- package/dist_ts/mod_release/helpers.fossglobalassets.d.ts +61 -0
- package/dist_ts/mod_release/helpers.fossglobalassets.js +580 -0
- package/dist_ts/mod_release/helpers.giteaassets.d.ts +15 -51
- package/dist_ts/mod_release/helpers.giteaassets.js +40 -507
- package/dist_ts/mod_release/helpers.releaseassets.d.ts +40 -0
- package/dist_ts/mod_release/helpers.releaseassets.js +349 -0
- package/dist_ts/mod_release/helpers.releaseassettransfer.d.ts +67 -0
- package/dist_ts/mod_release/helpers.releaseassettransfer.js +239 -0
- package/dist_ts/mod_release/helpers.releasecheck.js +3 -2
- package/dist_ts/mod_release/helpers.releasepreparation.d.ts +7 -6
- package/dist_ts/mod_release/helpers.releasepreparation.js +34 -7
- package/dist_ts/mod_release/helpers.releasepublication.d.ts +8 -2
- package/dist_ts/mod_release/helpers.releasepublication.js +218 -54
- package/dist_ts/mod_release/helpers.releaseversionsource.d.ts +7 -5
- package/dist_ts/mod_release/helpers.releaseversionsource.js +9 -7
- package/dist_ts/mod_release/index.d.ts +1 -1
- package/dist_ts/mod_release/index.js +109 -68
- package/dist_ts/mod_release/interfaces.fossglobalassets.d.ts +108 -0
- package/dist_ts/mod_release/interfaces.fossglobalassets.js +2 -0
- package/dist_ts/mod_release/interfaces.giteaassets.d.ts +9 -43
- package/dist_ts/mod_release/interfaces.releaseassets.d.ts +68 -0
- package/dist_ts/mod_release/interfaces.releaseassets.js +2 -0
- package/dist_ts/plugins.d.ts +1 -0
- package/dist_ts/plugins.js +1 -1
- package/package.json +3 -2
- package/readme.md +339 -41
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/helpers.tssea.ts +31 -10
- package/ts/helpers.versionsource.ts +176 -45
- package/ts/helpers.workflow.ts +288 -78
- package/ts/mod_config/index.ts +130 -38
- package/ts/mod_release/classes.releasejournal.ts +499 -180
- package/ts/mod_release/helpers.cargorelease.ts +8 -3
- package/ts/mod_release/helpers.fossglobalassets.ts +886 -0
- package/ts/mod_release/helpers.giteaassets.ts +127 -694
- package/ts/mod_release/helpers.releaseassets.ts +516 -0
- package/ts/mod_release/helpers.releaseassettransfer.ts +339 -0
- package/ts/mod_release/helpers.releasecheck.ts +3 -1
- package/ts/mod_release/helpers.releasepreparation.ts +55 -12
- package/ts/mod_release/helpers.releasepublication.ts +351 -69
- package/ts/mod_release/helpers.releaseversionsource.ts +11 -7
- package/ts/mod_release/index.ts +151 -90
- package/ts/mod_release/interfaces.fossglobalassets.ts +127 -0
- package/ts/mod_release/interfaces.giteaassets.ts +14 -47
- package/ts/mod_release/interfaces.releaseassets.ts +96 -0
- package/ts/plugins.ts +3 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import * as plugins from "./mod.plugins.js";
|
|
2
|
+
import type {
|
|
3
|
+
IPrepareReleaseAssetsOptions,
|
|
4
|
+
IReleaseAssetDestination,
|
|
5
|
+
IReleaseAssetTarget,
|
|
6
|
+
IReleaseFrozenAsset,
|
|
7
|
+
TReleaseAssetTargetName,
|
|
8
|
+
} from "./interfaces.releaseassets.js";
|
|
9
|
+
export type * from "./interfaces.releaseassets.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Every release asset target with what it fixes for the shared machinery: the
|
|
13
|
+
* code host its messages name and the bounds of one release's asset set.
|
|
14
|
+
*/
|
|
15
|
+
export const releaseAssetTargets: Readonly<
|
|
16
|
+
Record<TReleaseAssetTargetName, Readonly<IReleaseAssetTarget>>
|
|
17
|
+
> = Object.freeze({
|
|
18
|
+
giteaAssets: Object.freeze({
|
|
19
|
+
label: "Gitea",
|
|
20
|
+
hostAssetNoun: "attachment",
|
|
21
|
+
maximumAssets: 1024,
|
|
22
|
+
maximumAssetBytes: 32 * 1024 * 1024 * 1024,
|
|
23
|
+
}),
|
|
24
|
+
// The code host's bounds: 100 assets per release, each of at most
|
|
25
|
+
// GIT_STORAGE_MAX_RELEASE_ASSET_BYTES (2 GiB by default).
|
|
26
|
+
fossglobalAssets: Object.freeze({
|
|
27
|
+
label: "foss.global",
|
|
28
|
+
hostAssetNoun: "asset",
|
|
29
|
+
maximumAssets: 100,
|
|
30
|
+
maximumAssetBytes: 2 * 1024 * 1024 * 1024,
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const maximumManifestBytes = 4 * 1024 * 1024;
|
|
35
|
+
const canonicalName = /^[A-Za-z0-9][A-Za-z0-9._-]{0,199}$/;
|
|
36
|
+
const sha256Pattern = /^[a-f0-9]{64}$/;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The destination holds something other than the frozen release or asset.
|
|
40
|
+
* Terminal: the claim/inspect executor records it as a destination conflict.
|
|
41
|
+
*/
|
|
42
|
+
export class ReleaseAssetConflictError extends Error {
|
|
43
|
+
constructor(message: string) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = "ReleaseAssetConflictError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const labelOf = (target: TReleaseAssetTargetName): string =>
|
|
50
|
+
releaseAssetTargets[target].label;
|
|
51
|
+
|
|
52
|
+
export const assertReleaseAssetRecord = (
|
|
53
|
+
target: TReleaseAssetTargetName,
|
|
54
|
+
value: unknown,
|
|
55
|
+
): Record<string, unknown> => {
|
|
56
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
57
|
+
throw new Error(`Invalid ${labelOf(target)} asset object.`);
|
|
58
|
+
}
|
|
59
|
+
return value as Record<string, unknown>;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const exactKeys = (
|
|
63
|
+
target: TReleaseAssetTargetName,
|
|
64
|
+
value: Record<string, unknown>,
|
|
65
|
+
keys: string[],
|
|
66
|
+
): void => {
|
|
67
|
+
if (Object.keys(value).sort().join("\0") !== [...keys].sort().join("\0")) {
|
|
68
|
+
throw new Error(`Unexpected ${labelOf(target)} asset manifest fields.`);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const assertName = (target: TReleaseAssetTargetName, value: unknown): string => {
|
|
73
|
+
if (typeof value !== "string" || !canonicalName.test(value)) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`${labelOf(target)} asset names must be canonical single path components.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return value;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const assertReleaseAssetTag = (
|
|
82
|
+
target: TReleaseAssetTargetName,
|
|
83
|
+
value: string,
|
|
84
|
+
): void => {
|
|
85
|
+
if (
|
|
86
|
+
!canonicalName.test(value) ||
|
|
87
|
+
value.endsWith(".") ||
|
|
88
|
+
value.endsWith(".lock") ||
|
|
89
|
+
value.includes("..")
|
|
90
|
+
) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`${labelOf(target)} asset publication requires a canonical release tag.`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const assertSizeAndHash = (
|
|
98
|
+
target: TReleaseAssetTargetName,
|
|
99
|
+
value: Record<string, unknown>,
|
|
100
|
+
): void => {
|
|
101
|
+
if (
|
|
102
|
+
!Number.isSafeInteger(value.size) ||
|
|
103
|
+
(value.size as number) < 0 ||
|
|
104
|
+
(value.size as number) > releaseAssetTargets[target].maximumAssetBytes ||
|
|
105
|
+
typeof value.sha256 !== "string" ||
|
|
106
|
+
!sha256Pattern.test(value.sha256)
|
|
107
|
+
) {
|
|
108
|
+
throw new Error(`Invalid ${labelOf(target)} asset size or SHA256.`);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const normalizeReleaseAssetDestination = (
|
|
113
|
+
target: TReleaseAssetTargetName,
|
|
114
|
+
value: IReleaseAssetDestination,
|
|
115
|
+
): IReleaseAssetDestination => {
|
|
116
|
+
const candidate = assertReleaseAssetRecord(target, value);
|
|
117
|
+
exactKeys(target, candidate, ["apiOrigin", "owner", "repository"]);
|
|
118
|
+
let url: URL;
|
|
119
|
+
try {
|
|
120
|
+
url = new URL(candidate.apiOrigin as string);
|
|
121
|
+
} catch {
|
|
122
|
+
throw new Error(`Invalid ${labelOf(target)} API origin.`);
|
|
123
|
+
}
|
|
124
|
+
if (
|
|
125
|
+
url.protocol !== "https:" ||
|
|
126
|
+
url.username ||
|
|
127
|
+
url.password ||
|
|
128
|
+
url.search ||
|
|
129
|
+
url.hash ||
|
|
130
|
+
url.pathname !== "/" ||
|
|
131
|
+
![url.origin, `${url.origin}/`].includes(candidate.apiOrigin as string)
|
|
132
|
+
) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`${labelOf(target)} API origin must be credential-free canonical HTTPS.`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
apiOrigin: url.origin,
|
|
139
|
+
owner: assertName(target, candidate.owner),
|
|
140
|
+
repository: assertName(target, candidate.repository),
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The credential of a release asset target from its configured environment
|
|
146
|
+
* variable. Only the variable's name ever appears in a message.
|
|
147
|
+
*/
|
|
148
|
+
export const readReleaseAssetCredential = (
|
|
149
|
+
target: TReleaseAssetTargetName,
|
|
150
|
+
tokenEnv: string,
|
|
151
|
+
): string => {
|
|
152
|
+
const token = process.env[tokenEnv];
|
|
153
|
+
if (!token)
|
|
154
|
+
throw new Error(
|
|
155
|
+
`${labelOf(target)} assets require the configured credential in ${tokenEnv}.`,
|
|
156
|
+
);
|
|
157
|
+
return token;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** The frozen copy's name in the release record directory, covered by its retention. */
|
|
161
|
+
export const releaseAssetFileName = (
|
|
162
|
+
target: TReleaseAssetTargetName,
|
|
163
|
+
name: string,
|
|
164
|
+
): string =>
|
|
165
|
+
`asset.${plugins.crypto.createHash("sha256").update(assertName(target, name)).digest("hex")}.bin`;
|
|
166
|
+
|
|
167
|
+
/** The anonymous download URL by tag and asset name. */
|
|
168
|
+
export const releaseAssetPublicUrl = (
|
|
169
|
+
target: TReleaseAssetTargetName,
|
|
170
|
+
destination: IReleaseAssetDestination,
|
|
171
|
+
tag: string,
|
|
172
|
+
name: string,
|
|
173
|
+
): string => {
|
|
174
|
+
const normalized = normalizeReleaseAssetDestination(target, destination);
|
|
175
|
+
assertReleaseAssetTag(target, tag);
|
|
176
|
+
return `${normalized.apiOrigin}/${normalized.owner}/${normalized.repository}/releases/download/${encodeURIComponent(tag)}/${assertName(target, name)}`;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const assertRelativeAssetPath = (value: unknown): string => {
|
|
180
|
+
if (
|
|
181
|
+
typeof value !== "string" ||
|
|
182
|
+
value.length > 4096 ||
|
|
183
|
+
value
|
|
184
|
+
.split("/")
|
|
185
|
+
.some(
|
|
186
|
+
(part) =>
|
|
187
|
+
!canonicalName.test(part) ||
|
|
188
|
+
/^(?:node_modules|coverage|secrets?|credentials?|runtime|data|tmp|temp|id_rsa|id_ed25519)(?:\..*)?$/i.test(
|
|
189
|
+
part,
|
|
190
|
+
) ||
|
|
191
|
+
/\.(?:pem|key|p12|pfx)$/i.test(part),
|
|
192
|
+
)
|
|
193
|
+
) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
"Asset inputs require canonical repository paths outside credential/runtime directories.",
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
return value;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/** Reject symlinks in every component, including the caller-provided root. */
|
|
202
|
+
const assertRegularPath = async (
|
|
203
|
+
target: TReleaseAssetTargetName,
|
|
204
|
+
absolutePath: string,
|
|
205
|
+
directory = false,
|
|
206
|
+
): Promise<void> => {
|
|
207
|
+
const resolved = plugins.path.resolve(absolutePath);
|
|
208
|
+
const parsed = plugins.path.parse(resolved);
|
|
209
|
+
let current = parsed.root;
|
|
210
|
+
const parts = resolved
|
|
211
|
+
.slice(parsed.root.length)
|
|
212
|
+
.split(plugins.path.sep)
|
|
213
|
+
.filter(Boolean);
|
|
214
|
+
for (let index = 0; index < parts.length; index++) {
|
|
215
|
+
current = plugins.path.join(current, parts[index]);
|
|
216
|
+
const stat = await plugins.fs.lstat(current);
|
|
217
|
+
const expectDirectory = index < parts.length - 1 || directory;
|
|
218
|
+
if (
|
|
219
|
+
stat.isSymbolicLink() ||
|
|
220
|
+
(expectDirectory ? !stat.isDirectory() : !stat.isFile())
|
|
221
|
+
) {
|
|
222
|
+
throw new Error(
|
|
223
|
+
`${labelOf(target)} asset paths must use regular files and directories without symlinks.`,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/** Opens a regular file reached without any symlink, read-only. */
|
|
230
|
+
export const openReleaseAssetFile = async (
|
|
231
|
+
target: TReleaseAssetTargetName,
|
|
232
|
+
path: string,
|
|
233
|
+
) => {
|
|
234
|
+
await assertRegularPath(target, path);
|
|
235
|
+
const handle = await plugins.fs.open(
|
|
236
|
+
path,
|
|
237
|
+
plugins.fsConstants.O_RDONLY | plugins.fsConstants.O_NOFOLLOW,
|
|
238
|
+
);
|
|
239
|
+
try {
|
|
240
|
+
if (!(await handle.stat()).isFile())
|
|
241
|
+
throw new Error(`${labelOf(target)} asset input is not a regular file.`);
|
|
242
|
+
return handle;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
await handle.close();
|
|
245
|
+
throw error;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
export type TReleaseAssetFileHandle = Awaited<
|
|
250
|
+
ReturnType<typeof openReleaseAssetFile>
|
|
251
|
+
>;
|
|
252
|
+
|
|
253
|
+
const readBoundedFile = async (
|
|
254
|
+
target: TReleaseAssetTargetName,
|
|
255
|
+
path: string,
|
|
256
|
+
maximum: number,
|
|
257
|
+
): Promise<Buffer> => {
|
|
258
|
+
const handle = await openReleaseAssetFile(target, path);
|
|
259
|
+
try {
|
|
260
|
+
if ((await handle.stat()).size > maximum)
|
|
261
|
+
throw new Error(
|
|
262
|
+
`${labelOf(target)} asset metadata exceeds its size limit.`,
|
|
263
|
+
);
|
|
264
|
+
const chunks: Buffer[] = [];
|
|
265
|
+
let size = 0;
|
|
266
|
+
for await (const chunk of handle.createReadStream({
|
|
267
|
+
autoClose: false,
|
|
268
|
+
highWaterMark: 64 * 1024,
|
|
269
|
+
})) {
|
|
270
|
+
size += chunk.length;
|
|
271
|
+
if (size > maximum)
|
|
272
|
+
throw new Error(
|
|
273
|
+
`${labelOf(target)} asset metadata exceeds its size limit.`,
|
|
274
|
+
);
|
|
275
|
+
chunks.push(chunk);
|
|
276
|
+
}
|
|
277
|
+
return Buffer.concat(chunks, size);
|
|
278
|
+
} finally {
|
|
279
|
+
await handle.close();
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
/** Hashes the whole open file; its size, hash and timestamps must match the frozen asset. */
|
|
284
|
+
export const verifyReleaseAssetFile = async (
|
|
285
|
+
target: TReleaseAssetTargetName,
|
|
286
|
+
handle: TReleaseAssetFileHandle,
|
|
287
|
+
asset: IReleaseFrozenAsset,
|
|
288
|
+
): Promise<void> => {
|
|
289
|
+
const label = labelOf(target);
|
|
290
|
+
const before = await handle.stat();
|
|
291
|
+
if (before.size !== asset.size)
|
|
292
|
+
throw new Error(
|
|
293
|
+
`Stored ${label} asset size conflicts with its frozen identity.`,
|
|
294
|
+
);
|
|
295
|
+
const hash = plugins.crypto.createHash("sha256");
|
|
296
|
+
let size = 0;
|
|
297
|
+
for await (const chunk of handle.createReadStream({
|
|
298
|
+
autoClose: false,
|
|
299
|
+
start: 0,
|
|
300
|
+
highWaterMark: 64 * 1024,
|
|
301
|
+
})) {
|
|
302
|
+
size += chunk.length;
|
|
303
|
+
if (size > asset.size)
|
|
304
|
+
throw new Error(`Stored ${label} asset exceeds its frozen size.`);
|
|
305
|
+
hash.update(chunk);
|
|
306
|
+
}
|
|
307
|
+
const after = await handle.stat();
|
|
308
|
+
if (
|
|
309
|
+
size !== asset.size ||
|
|
310
|
+
hash.digest("hex") !== asset.sha256 ||
|
|
311
|
+
before.mtimeMs !== after.mtimeMs ||
|
|
312
|
+
before.ctimeMs !== after.ctimeMs
|
|
313
|
+
) {
|
|
314
|
+
throw new Error(
|
|
315
|
+
`Stored ${label} asset bytes conflict with their frozen identity.`,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
export function assertReleaseFrozenAssets(
|
|
321
|
+
target: TReleaseAssetTargetName,
|
|
322
|
+
assets: unknown,
|
|
323
|
+
destination?: IReleaseAssetDestination,
|
|
324
|
+
tag?: string,
|
|
325
|
+
): asserts assets is IReleaseFrozenAsset[] {
|
|
326
|
+
const label = labelOf(target);
|
|
327
|
+
if (
|
|
328
|
+
!Array.isArray(assets) ||
|
|
329
|
+
assets.length === 0 ||
|
|
330
|
+
assets.length > releaseAssetTargets[target].maximumAssets
|
|
331
|
+
)
|
|
332
|
+
throw new Error(`Invalid ${label} asset set.`);
|
|
333
|
+
const names = new Set<string>();
|
|
334
|
+
for (const entry of assets) {
|
|
335
|
+
const asset = assertReleaseAssetRecord(target, entry);
|
|
336
|
+
exactKeys(target, asset, [
|
|
337
|
+
"name",
|
|
338
|
+
"size",
|
|
339
|
+
"sha256",
|
|
340
|
+
"fileName",
|
|
341
|
+
"publicUrl",
|
|
342
|
+
]);
|
|
343
|
+
const name = assertName(target, asset.name);
|
|
344
|
+
assertSizeAndHash(target, asset);
|
|
345
|
+
if (names.has(name) || asset.fileName !== releaseAssetFileName(target, name))
|
|
346
|
+
throw new Error(`Duplicate or invalid stored ${label} asset identity.`);
|
|
347
|
+
names.add(name);
|
|
348
|
+
let url: URL;
|
|
349
|
+
try {
|
|
350
|
+
url = new URL(asset.publicUrl as string);
|
|
351
|
+
} catch {
|
|
352
|
+
throw new Error(`Invalid frozen ${label} asset URL.`);
|
|
353
|
+
}
|
|
354
|
+
if (
|
|
355
|
+
url.protocol !== "https:" ||
|
|
356
|
+
url.username ||
|
|
357
|
+
url.password ||
|
|
358
|
+
url.search ||
|
|
359
|
+
url.hash ||
|
|
360
|
+
url.href !== asset.publicUrl ||
|
|
361
|
+
(destination &&
|
|
362
|
+
tag &&
|
|
363
|
+
asset.publicUrl !==
|
|
364
|
+
releaseAssetPublicUrl(target, destination, tag, name))
|
|
365
|
+
) {
|
|
366
|
+
throw new Error(
|
|
367
|
+
`Frozen ${label} asset URL conflicts with its destination.`,
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export const validateReleaseAssets = async (
|
|
374
|
+
target: TReleaseAssetTargetName,
|
|
375
|
+
artifactDirectory: string,
|
|
376
|
+
assets: IReleaseFrozenAsset[],
|
|
377
|
+
): Promise<void> => {
|
|
378
|
+
assertReleaseFrozenAssets(target, assets);
|
|
379
|
+
await assertRegularPath(target, artifactDirectory, true);
|
|
380
|
+
for (const asset of assets) {
|
|
381
|
+
const handle = await openReleaseAssetFile(
|
|
382
|
+
target,
|
|
383
|
+
plugins.path.join(artifactDirectory, asset.fileName),
|
|
384
|
+
);
|
|
385
|
+
try {
|
|
386
|
+
await verifyReleaseAssetFile(target, handle, asset);
|
|
387
|
+
} finally {
|
|
388
|
+
await handle.close();
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Reads the manifest `{schemaVersion: 1, version, assets: [{name, path, size,
|
|
395
|
+
* sha256}]}` and freezes each listed file into the release record directory,
|
|
396
|
+
* proving its bytes before and after the copy.
|
|
397
|
+
*/
|
|
398
|
+
export const prepareReleaseAssets = async (
|
|
399
|
+
target: TReleaseAssetTargetName,
|
|
400
|
+
options: IPrepareReleaseAssetsOptions,
|
|
401
|
+
): Promise<IReleaseFrozenAsset[]> => {
|
|
402
|
+
const label = labelOf(target);
|
|
403
|
+
const destination = normalizeReleaseAssetDestination(
|
|
404
|
+
target,
|
|
405
|
+
options.destination,
|
|
406
|
+
);
|
|
407
|
+
assertReleaseAssetTag(target, options.tag);
|
|
408
|
+
if (
|
|
409
|
+
!/^\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?(?:\+[A-Za-z0-9.-]+)?$/.test(
|
|
410
|
+
options.version,
|
|
411
|
+
)
|
|
412
|
+
)
|
|
413
|
+
throw new Error(`Invalid ${label} asset manifest release version.`);
|
|
414
|
+
await assertRegularPath(target, options.repositoryRoot, true);
|
|
415
|
+
const manifestPath = plugins.path.join(
|
|
416
|
+
options.repositoryRoot,
|
|
417
|
+
assertRelativeAssetPath(options.manifestPath),
|
|
418
|
+
);
|
|
419
|
+
let manifest: Record<string, unknown>;
|
|
420
|
+
try {
|
|
421
|
+
manifest = assertReleaseAssetRecord(
|
|
422
|
+
target,
|
|
423
|
+
JSON.parse(
|
|
424
|
+
(
|
|
425
|
+
await readBoundedFile(target, manifestPath, maximumManifestBytes)
|
|
426
|
+
).toString("utf8"),
|
|
427
|
+
),
|
|
428
|
+
);
|
|
429
|
+
} catch {
|
|
430
|
+
throw new Error(
|
|
431
|
+
`Unable to read a bounded regular ${label} asset manifest.`,
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
exactKeys(target, manifest, ["schemaVersion", "version", "assets"]);
|
|
435
|
+
if (
|
|
436
|
+
manifest.schemaVersion !== 1 ||
|
|
437
|
+
manifest.version !== options.version ||
|
|
438
|
+
!Array.isArray(manifest.assets) ||
|
|
439
|
+
manifest.assets.length === 0 ||
|
|
440
|
+
manifest.assets.length > releaseAssetTargets[target].maximumAssets
|
|
441
|
+
)
|
|
442
|
+
throw new Error(
|
|
443
|
+
`${label} asset manifest must match the exact release version and schema.`,
|
|
444
|
+
);
|
|
445
|
+
const paths = new Set<string>();
|
|
446
|
+
const assets = manifest.assets.map((entry): IReleaseFrozenAsset => {
|
|
447
|
+
const input = assertReleaseAssetRecord(target, entry);
|
|
448
|
+
exactKeys(target, input, ["name", "path", "size", "sha256"]);
|
|
449
|
+
const path = assertRelativeAssetPath(input.path);
|
|
450
|
+
if (paths.has(path))
|
|
451
|
+
throw new Error(`${label} asset input paths must be unique.`);
|
|
452
|
+
paths.add(path);
|
|
453
|
+
const name = assertName(target, input.name);
|
|
454
|
+
assertSizeAndHash(target, input);
|
|
455
|
+
return {
|
|
456
|
+
name,
|
|
457
|
+
size: input.size as number,
|
|
458
|
+
sha256: input.sha256 as string,
|
|
459
|
+
fileName: releaseAssetFileName(target, name),
|
|
460
|
+
publicUrl: releaseAssetPublicUrl(target, destination, options.tag, name),
|
|
461
|
+
};
|
|
462
|
+
});
|
|
463
|
+
assertReleaseFrozenAssets(target, assets, destination, options.tag);
|
|
464
|
+
// The journal owner creates this directory; never follow/create an unexpected root here.
|
|
465
|
+
await assertRegularPath(target, options.artifactDirectory, true);
|
|
466
|
+
for (let index = 0; index < assets.length; index++) {
|
|
467
|
+
const asset = assets[index];
|
|
468
|
+
const inputPath = plugins.path.join(
|
|
469
|
+
options.repositoryRoot,
|
|
470
|
+
[...paths][index],
|
|
471
|
+
);
|
|
472
|
+
const input = await openReleaseAssetFile(target, inputPath);
|
|
473
|
+
try {
|
|
474
|
+
await verifyReleaseAssetFile(target, input, asset);
|
|
475
|
+
const outputPath = plugins.path.join(
|
|
476
|
+
options.artifactDirectory,
|
|
477
|
+
asset.fileName,
|
|
478
|
+
);
|
|
479
|
+
const output = await plugins.fs.open(outputPath, "wx", 0o600);
|
|
480
|
+
try {
|
|
481
|
+
let position = 0;
|
|
482
|
+
for await (const chunk of input.createReadStream({
|
|
483
|
+
autoClose: false,
|
|
484
|
+
start: 0,
|
|
485
|
+
highWaterMark: 64 * 1024,
|
|
486
|
+
})) {
|
|
487
|
+
if (position + chunk.length > asset.size)
|
|
488
|
+
throw new Error(`${label} asset changed during freezing.`);
|
|
489
|
+
let written = 0;
|
|
490
|
+
while (written < chunk.length) {
|
|
491
|
+
const result = await output.write(
|
|
492
|
+
chunk,
|
|
493
|
+
written,
|
|
494
|
+
chunk.length - written,
|
|
495
|
+
position + written,
|
|
496
|
+
);
|
|
497
|
+
if (!result.bytesWritten)
|
|
498
|
+
throw new Error(
|
|
499
|
+
`Unable to freeze complete ${label} asset bytes.`,
|
|
500
|
+
);
|
|
501
|
+
written += result.bytesWritten;
|
|
502
|
+
}
|
|
503
|
+
position += chunk.length;
|
|
504
|
+
}
|
|
505
|
+
await output.sync();
|
|
506
|
+
} finally {
|
|
507
|
+
await output.close();
|
|
508
|
+
}
|
|
509
|
+
// Includes detecting same-size mutation between the validation and copy passes.
|
|
510
|
+
await validateReleaseAssets(target, options.artifactDirectory, [asset]);
|
|
511
|
+
} finally {
|
|
512
|
+
await input.close();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return assets;
|
|
516
|
+
};
|