@kubohiroya/sb3-toolchain 0.11.0 → 0.13.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/CHANGELOG.md +30 -4
- package/README.ja.md +17 -1
- package/README.md +17 -1
- package/dist/archive.d.ts +1 -0
- package/dist/archive.js +16 -0
- package/dist/archive.js.map +1 -0
- package/dist/assert.d.ts +2 -0
- package/dist/assert.js +11 -0
- package/dist/assert.js.map +1 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +12 -0
- package/dist/bin.js.map +1 -0
- package/dist/build.d.ts +23 -0
- package/dist/build.js +111 -0
- package/dist/build.js.map +1 -0
- package/dist/cli.d.ts +87 -0
- package/dist/cli.js +544 -0
- package/dist/cli.js.map +1 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +7 -0
- package/dist/constants.js.map +1 -0
- package/dist/extension-api-manifest.d.ts +65 -0
- package/dist/extension-api-manifest.js +231 -0
- package/dist/extension-api-manifest.js.map +1 -0
- package/dist/extension-bundle-archive.d.ts +36 -0
- package/dist/extension-bundle-archive.js +302 -0
- package/dist/extension-bundle-archive.js.map +1 -0
- package/dist/extension-bundle-configuration.d.ts +55 -0
- package/dist/extension-bundle-configuration.js +200 -0
- package/dist/extension-bundle-configuration.js.map +1 -0
- package/dist/extension-bundle.d.ts +39 -0
- package/dist/extension-bundle.js +580 -0
- package/dist/extension-bundle.js.map +1 -0
- package/dist/extension-dependencies.d.ts +18 -0
- package/dist/extension-dependencies.js +59 -0
- package/dist/extension-dependencies.js.map +1 -0
- package/dist/extension-id-migration.d.ts +83 -0
- package/dist/extension-id-migration.js +292 -0
- package/dist/extension-id-migration.js.map +1 -0
- package/dist/extension-sync.d.ts +111 -0
- package/dist/extension-sync.js +463 -0
- package/dist/extension-sync.js.map +1 -0
- package/dist/import.d.ts +39 -0
- package/dist/import.js +244 -0
- package/dist/import.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +18 -0
- package/dist/npm-extension-source.d.ts +13 -0
- package/dist/npm-extension-source.js +63 -0
- package/dist/npm-extension-source.js.map +1 -0
- package/dist/output-safety.d.ts +29 -0
- package/dist/output-safety.js +213 -0
- package/dist/output-safety.js.map +1 -0
- package/dist/project-asset-additions.d.ts +29 -0
- package/dist/project-asset-additions.js +412 -0
- package/dist/project-asset-additions.js.map +1 -0
- package/dist/release-snapshot.d.ts +75 -0
- package/dist/release-snapshot.js +177 -0
- package/dist/release-snapshot.js.map +1 -0
- package/dist/source.d.ts +41 -0
- package/dist/source.js +282 -0
- package/dist/source.js.map +1 -0
- package/dist/turbowarp-clean-up.d.ts +18 -0
- package/dist/turbowarp-clean-up.js +266 -0
- package/dist/turbowarp-clean-up.js.map +1 -0
- package/dist/types.d.ts +80 -0
- package/package.json +31 -20
- package/bin/sb3-toolchain.mjs +0 -9
- package/src/archive.js +0 -35
- package/src/build.js +0 -188
- package/src/cli.js +0 -748
- package/src/constants.js +0 -4
- package/src/extension-api-manifest.js +0 -336
- package/src/extension-bundle-archive.js +0 -422
- package/src/extension-bundle-configuration.js +0 -265
- package/src/extension-bundle.js +0 -695
- package/src/extension-dependencies.js +0 -114
- package/src/extension-id-migration.js +0 -496
- package/src/extension-sync.js +0 -754
- package/src/import.js +0 -397
- package/src/index.js +0 -75
- package/src/npm-extension-source.js +0 -111
- package/src/output-safety.js +0 -322
- package/src/project-asset-additions.js +0 -560
- package/src/release-snapshot.js +0 -247
- package/src/source.js +0 -468
- package/src/turbowarp-clean-up.js +0 -295
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { ExtensionApiCompatibilityChange } from './extension-api-manifest';
|
|
2
|
+
import { DirectoryComparison } from './output-safety';
|
|
3
|
+
export declare const defaultExtensionArtifactSizeLimit: number;
|
|
4
|
+
export type FetchImplementation = typeof globalThis.fetch;
|
|
5
|
+
export type ExtensionSyncMode = 'sync' | 'update';
|
|
6
|
+
export interface ExtensionSyncConfirmContext {
|
|
7
|
+
comparison: DirectoryComparison;
|
|
8
|
+
sourceDirectory: string;
|
|
9
|
+
}
|
|
10
|
+
export interface SyncExtensionsOptions {
|
|
11
|
+
confirmReplace?: (context: ExtensionSyncConfirmContext) => boolean | Promise<boolean>;
|
|
12
|
+
fetch?: FetchImplementation;
|
|
13
|
+
maximumArtifactBytes?: number;
|
|
14
|
+
maximumManifestBytes?: number;
|
|
15
|
+
sourceDirectory: string;
|
|
16
|
+
yes?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface UpdateExtensionsOptions extends SyncExtensionsOptions {
|
|
19
|
+
allowBreakingApi?: boolean;
|
|
20
|
+
apiManifestArtifact?: string;
|
|
21
|
+
extensionId?: string;
|
|
22
|
+
migrateToId?: string;
|
|
23
|
+
sourceArtifact?: string;
|
|
24
|
+
}
|
|
25
|
+
export declare function extensionStatus(sourceDirectory: string, { fetch: fetchImplementation }?: {
|
|
26
|
+
fetch?: FetchImplementation;
|
|
27
|
+
}): Promise<({
|
|
28
|
+
id: string;
|
|
29
|
+
installedVersion: string;
|
|
30
|
+
local: string;
|
|
31
|
+
package: string;
|
|
32
|
+
state: string;
|
|
33
|
+
version: string;
|
|
34
|
+
ref?: undefined;
|
|
35
|
+
remoteCommit?: undefined;
|
|
36
|
+
resolvedCommit?: undefined;
|
|
37
|
+
} | {
|
|
38
|
+
id: string;
|
|
39
|
+
local: string;
|
|
40
|
+
ref: string;
|
|
41
|
+
remoteCommit: string;
|
|
42
|
+
resolvedCommit: string;
|
|
43
|
+
state: string;
|
|
44
|
+
installedVersion?: undefined;
|
|
45
|
+
package?: undefined;
|
|
46
|
+
version?: undefined;
|
|
47
|
+
})[]>;
|
|
48
|
+
export declare function syncExtensions({ confirmReplace, fetch: fetchImplementation, maximumArtifactBytes, maximumManifestBytes, sourceDirectory, yes, }: SyncExtensionsOptions): Promise<{
|
|
49
|
+
apiCompatibility: {
|
|
50
|
+
changes: ExtensionApiCompatibilityChange[];
|
|
51
|
+
id: string;
|
|
52
|
+
previousId: string | undefined;
|
|
53
|
+
}[];
|
|
54
|
+
extensions: ({
|
|
55
|
+
id: string;
|
|
56
|
+
package: string;
|
|
57
|
+
version: string | undefined;
|
|
58
|
+
previousId?: undefined;
|
|
59
|
+
resolvedCommit?: undefined;
|
|
60
|
+
} | {
|
|
61
|
+
id: string;
|
|
62
|
+
previousId: string | undefined;
|
|
63
|
+
resolvedCommit: string | null;
|
|
64
|
+
package?: undefined;
|
|
65
|
+
version?: undefined;
|
|
66
|
+
})[];
|
|
67
|
+
migration: {
|
|
68
|
+
counts: import('./extension-id-migration').ExtensionIdMigrationCounts;
|
|
69
|
+
fromId: string | undefined;
|
|
70
|
+
toId: string | undefined;
|
|
71
|
+
totalChanges: number;
|
|
72
|
+
unclassifiedReferences: import('./extension-id-migration').ExtensionIdReference[];
|
|
73
|
+
} | null;
|
|
74
|
+
mode: ExtensionSyncMode;
|
|
75
|
+
sourceDirectory: string;
|
|
76
|
+
changed: boolean;
|
|
77
|
+
comparison: DirectoryComparison;
|
|
78
|
+
rollbackCleanupWarning: string | null;
|
|
79
|
+
}>;
|
|
80
|
+
export declare function updateExtensions({ allowBreakingApi, apiManifestArtifact, confirmReplace, extensionId, fetch: fetchImplementation, maximumArtifactBytes, maximumManifestBytes, migrateToId, sourceArtifact, sourceDirectory, yes, }: UpdateExtensionsOptions): Promise<{
|
|
81
|
+
apiCompatibility: {
|
|
82
|
+
changes: ExtensionApiCompatibilityChange[];
|
|
83
|
+
id: string;
|
|
84
|
+
previousId: string | undefined;
|
|
85
|
+
}[];
|
|
86
|
+
extensions: ({
|
|
87
|
+
id: string;
|
|
88
|
+
package: string;
|
|
89
|
+
version: string | undefined;
|
|
90
|
+
previousId?: undefined;
|
|
91
|
+
resolvedCommit?: undefined;
|
|
92
|
+
} | {
|
|
93
|
+
id: string;
|
|
94
|
+
previousId: string | undefined;
|
|
95
|
+
resolvedCommit: string | null;
|
|
96
|
+
package?: undefined;
|
|
97
|
+
version?: undefined;
|
|
98
|
+
})[];
|
|
99
|
+
migration: {
|
|
100
|
+
counts: import('./extension-id-migration').ExtensionIdMigrationCounts;
|
|
101
|
+
fromId: string | undefined;
|
|
102
|
+
toId: string | undefined;
|
|
103
|
+
totalChanges: number;
|
|
104
|
+
unclassifiedReferences: import('./extension-id-migration').ExtensionIdReference[];
|
|
105
|
+
} | null;
|
|
106
|
+
mode: ExtensionSyncMode;
|
|
107
|
+
sourceDirectory: string;
|
|
108
|
+
changed: boolean;
|
|
109
|
+
comparison: DirectoryComparison;
|
|
110
|
+
rollbackCleanupWarning: string | null;
|
|
111
|
+
}>;
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { assert, errorMessage } from "./assert.js";
|
|
2
|
+
import { assertNoInterruptedRollback, compareDirectories, pathExists, replaceDirectoryTransactionally } from "./output-safety.js";
|
|
3
|
+
import { compareExtensionApiManifests, defaultExtensionApiManifestSizeLimit, extensionApiManifestIntegrity, formatExtensionApiCompatibilityChanges, parseExtensionApiManifest, validateExtensionApiManifestSourceMetadata, validateManagedExtensionApiManifest } from "./extension-api-manifest.js";
|
|
4
|
+
import { extensionHeaderId, extensionIntegrity, validateExtensionSourceMetadata, validateManagedExtensionContents } from "./extension-dependencies.js";
|
|
5
|
+
import { createDeterministicSb3, inspectSb3SourceForExtensionSync, validateSb3Source } from "./source.js";
|
|
6
|
+
import { rewriteExtensionIdDocuments, validateNewExtensionId } from "./extension-id-migration.js";
|
|
7
|
+
import { readNpmExtensionSource } from "./npm-extension-source.js";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { cp, lstat, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
10
|
+
//#region src/extension-sync.ts
|
|
11
|
+
var defaultExtensionArtifactSizeLimit = 5242880;
|
|
12
|
+
var githubApiResponseSizeLimit = 1048576;
|
|
13
|
+
function assertFetch(fetchImplementation) {
|
|
14
|
+
assert(typeof fetchImplementation === "function", "A Fetch API implementation is required for GitHub extension operations.");
|
|
15
|
+
return fetchImplementation;
|
|
16
|
+
}
|
|
17
|
+
function githubHeaders(accept) {
|
|
18
|
+
return {
|
|
19
|
+
Accept: accept,
|
|
20
|
+
"User-Agent": "sb3-toolchain",
|
|
21
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function rawArtifactUrl(source, commit, artifactPath = source.artifact) {
|
|
25
|
+
const artifact = artifactPath.split("/").map((segment) => encodeURIComponent(segment)).join("/");
|
|
26
|
+
return `https://raw.githubusercontent.com/${source.repository}/${commit}/${artifact}`;
|
|
27
|
+
}
|
|
28
|
+
async function downloadExtensionApiManifest(extension, commit, fetchImplementation, maximumManifestBytes, expectedId = extension.id) {
|
|
29
|
+
const metadata = validateExtensionApiManifestSourceMetadata(extension);
|
|
30
|
+
if (!metadata) return null;
|
|
31
|
+
const contents = await fetchBytes(fetchImplementation, rawArtifactUrl(extension.source, commit, metadata.artifact), maximumManifestBytes, `GitHub extension API manifest download for ${extension.id}`, "application/json");
|
|
32
|
+
return {
|
|
33
|
+
contents,
|
|
34
|
+
manifest: parseExtensionApiManifest(contents, { expectedId })
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function githubCommitUrl(source) {
|
|
38
|
+
return `https://api.github.com/repos/${source.repository}/commits/` + encodeURIComponent(source.ref);
|
|
39
|
+
}
|
|
40
|
+
async function readLimitedResponse(response, maximumBytes, description) {
|
|
41
|
+
const contentLength = response.headers?.get?.("content-length");
|
|
42
|
+
if (contentLength !== null && contentLength !== void 0) {
|
|
43
|
+
const parsedLength = Number(contentLength);
|
|
44
|
+
assert(Number.isSafeInteger(parsedLength) && parsedLength >= 0, `${description} returned an invalid Content-Length.`);
|
|
45
|
+
assert(parsedLength <= maximumBytes, `${description} exceeds the ${maximumBytes}-byte limit.`);
|
|
46
|
+
}
|
|
47
|
+
if (!response.body?.getReader) {
|
|
48
|
+
const contents = Buffer.from(await response.arrayBuffer());
|
|
49
|
+
assert(contents.length <= maximumBytes, `${description} exceeds the ${maximumBytes}-byte limit.`);
|
|
50
|
+
return contents;
|
|
51
|
+
}
|
|
52
|
+
const reader = response.body.getReader();
|
|
53
|
+
const chunks = [];
|
|
54
|
+
let totalLength = 0;
|
|
55
|
+
try {
|
|
56
|
+
while (true) {
|
|
57
|
+
const { done, value } = await reader.read();
|
|
58
|
+
if (done) break;
|
|
59
|
+
totalLength += value.byteLength;
|
|
60
|
+
assert(totalLength <= maximumBytes, `${description} exceeds the ${maximumBytes}-byte limit.`);
|
|
61
|
+
chunks.push(Buffer.from(value));
|
|
62
|
+
}
|
|
63
|
+
} catch (error) {
|
|
64
|
+
await reader.cancel(error).catch(() => {});
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
return Buffer.concat(chunks, totalLength);
|
|
68
|
+
}
|
|
69
|
+
async function fetchBytes(fetchImplementation, url, maximumBytes, description, accept) {
|
|
70
|
+
const parsedUrl = new URL(url);
|
|
71
|
+
assert(parsedUrl.protocol === "https:", `${description} requires HTTPS.`);
|
|
72
|
+
let response;
|
|
73
|
+
try {
|
|
74
|
+
response = await fetchImplementation(url, {
|
|
75
|
+
headers: githubHeaders(accept),
|
|
76
|
+
redirect: "error"
|
|
77
|
+
});
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw new Error(`${description} request failed: ${errorMessage(error)}`, { cause: error });
|
|
80
|
+
}
|
|
81
|
+
assert(response && typeof response === "object", `${description} request returned an invalid response.`);
|
|
82
|
+
assert(response.redirected !== true && (!response.url || new URL(response.url).href === parsedUrl.href), `${description} refused a redirected response.`);
|
|
83
|
+
assert(response.ok, `${description} request failed with HTTP ${response.status ?? "(unknown)"}.`);
|
|
84
|
+
return readLimitedResponse(response, maximumBytes, description);
|
|
85
|
+
}
|
|
86
|
+
async function resolveGithubCommit(extension, fetchImplementation) {
|
|
87
|
+
const source = extension.source;
|
|
88
|
+
if (/^[a-f0-9]{40}$/u.test(source.ref)) return source.ref;
|
|
89
|
+
const contents = await fetchBytes(fetchImplementation, githubCommitUrl(source), githubApiResponseSizeLimit, `GitHub ref lookup for ${extension.id}`, "application/vnd.github+json");
|
|
90
|
+
let response;
|
|
91
|
+
try {
|
|
92
|
+
response = JSON.parse(contents.toString("utf8"));
|
|
93
|
+
} catch (error) {
|
|
94
|
+
throw new Error(`GitHub ref lookup for ${extension.id} returned invalid JSON.`, { cause: error });
|
|
95
|
+
}
|
|
96
|
+
assert(typeof response?.sha === "string" && /^[a-f0-9]{40}$/u.test(response.sha), `GitHub ref lookup for ${extension.id} returned an invalid commit SHA.`);
|
|
97
|
+
return response.sha;
|
|
98
|
+
}
|
|
99
|
+
async function downloadExtension(extension, commit, fetchImplementation, maximumArtifactBytes, expectedId = extension.id) {
|
|
100
|
+
const contents = await fetchBytes(fetchImplementation, rawArtifactUrl(extension.source, commit), maximumArtifactBytes, `GitHub artifact download for ${extension.id}`, "application/javascript, text/javascript;q=0.9, */*;q=0.1");
|
|
101
|
+
const actualId = extensionHeaderId(contents);
|
|
102
|
+
assert(actualId === expectedId, `Downloaded extension header ID mismatch for ${extension.id}: expected ${expectedId}, got ${actualId ?? "(missing)"}`);
|
|
103
|
+
return contents;
|
|
104
|
+
}
|
|
105
|
+
function managedExtensions(source) {
|
|
106
|
+
return source.extensions.filter((extension) => extension.source !== void 0);
|
|
107
|
+
}
|
|
108
|
+
async function inspectExtensionSource(sourceDirectory, { willReplace = false } = {}) {
|
|
109
|
+
const resolvedSourceDirectory = path.resolve(sourceDirectory);
|
|
110
|
+
if (willReplace) {
|
|
111
|
+
assert(resolvedSourceDirectory !== path.parse(resolvedSourceDirectory).root, "Refusing to replace a filesystem root during extension synchronization.");
|
|
112
|
+
assert(!await pathExists(path.join(resolvedSourceDirectory, ".git")), "Refusing to replace a Git repository root during extension synchronization.");
|
|
113
|
+
}
|
|
114
|
+
const stats = await lstat(resolvedSourceDirectory);
|
|
115
|
+
assert(stats.isDirectory() && !stats.isSymbolicLink(), `SB3 source must be a directory, not a file or symbolic link: ${resolvedSourceDirectory}`);
|
|
116
|
+
if (willReplace) await assertNoInterruptedRollback(resolvedSourceDirectory);
|
|
117
|
+
return inspectSb3SourceForExtensionSync(resolvedSourceDirectory);
|
|
118
|
+
}
|
|
119
|
+
async function installCandidate({ candidateDirectory, confirmReplace, initialSourceFingerprint, sourceDirectory, yes }) {
|
|
120
|
+
const comparison = await compareDirectories(sourceDirectory, candidateDirectory);
|
|
121
|
+
assert(comparison.existingFingerprint === initialSourceFingerprint, "SB3 source changed while extension artifacts were being fetched; refusing to replace it.");
|
|
122
|
+
if (comparison.identical) {
|
|
123
|
+
await rm(candidateDirectory, {
|
|
124
|
+
recursive: true,
|
|
125
|
+
force: true
|
|
126
|
+
});
|
|
127
|
+
return {
|
|
128
|
+
changed: false,
|
|
129
|
+
comparison,
|
|
130
|
+
rollbackCleanupWarning: null
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
const context = {
|
|
134
|
+
comparison,
|
|
135
|
+
sourceDirectory
|
|
136
|
+
};
|
|
137
|
+
if (!yes) {
|
|
138
|
+
assert(typeof confirmReplace === "function", `Managed extension files differ in ${sourceDirectory}. Non-interactive replacement requires --yes.`);
|
|
139
|
+
assert(await confirmReplace(context), "Extension synchronization cancelled; the existing source was not changed.");
|
|
140
|
+
}
|
|
141
|
+
const latestComparison = await compareDirectories(sourceDirectory, candidateDirectory);
|
|
142
|
+
assert(latestComparison.existingFingerprint === comparison.existingFingerprint && latestComparison.candidateFingerprint === comparison.candidateFingerprint, "SB3 source or extension candidate changed during synchronization; refusing to replace it.");
|
|
143
|
+
return {
|
|
144
|
+
changed: true,
|
|
145
|
+
comparison,
|
|
146
|
+
...await replaceDirectoryTransactionally(candidateDirectory, sourceDirectory)
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async function updateCandidate({ allowBreakingApi, apiManifestArtifact, confirmReplace, fetchImplementation, maximumArtifactBytes, maximumManifestBytes, migrateToId, mode, selectedExtensionId, sourceArtifact, sourceDirectory, yes }) {
|
|
150
|
+
assert(Number.isSafeInteger(maximumArtifactBytes) && maximumArtifactBytes > 0, "maximumArtifactBytes must be a positive integer.");
|
|
151
|
+
assert(Number.isSafeInteger(maximumManifestBytes) && maximumManifestBytes > 0, "maximumManifestBytes must be a positive integer.");
|
|
152
|
+
assert(!allowBreakingApi || yes, "--allow-breaking-api requires --yes.");
|
|
153
|
+
const source = await inspectExtensionSource(sourceDirectory, { willReplace: true });
|
|
154
|
+
const initialComparison = await compareDirectories(source.resolvedSourceDirectory, source.resolvedSourceDirectory);
|
|
155
|
+
assert(initialComparison.identical && initialComparison.existingFingerprint === initialComparison.candidateFingerprint, "SB3 source changed while its initial state was being inspected.");
|
|
156
|
+
const managed = managedExtensions(source);
|
|
157
|
+
assert(managed.length > 0, "No managed embedded extensions were found.");
|
|
158
|
+
if (selectedExtensionId !== void 0) assert(managed.some((extension) => extension.id === selectedExtensionId), `Managed extension was not found: ${selectedExtensionId}`);
|
|
159
|
+
const selected = managed.filter((extension) => selectedExtensionId === void 0 || extension.id === selectedExtensionId);
|
|
160
|
+
if (migrateToId !== void 0) {
|
|
161
|
+
assert(mode === "update", "Extension ID migration is only available during update.");
|
|
162
|
+
assert(selectedExtensionId !== void 0, "Extension ID migration requires an explicit existing extension ID.");
|
|
163
|
+
validateNewExtensionId(migrateToId);
|
|
164
|
+
assert(selected.every((extension) => extension.source?.provider === "github"), "Extension ID migration is not supported for npm-managed extensions.");
|
|
165
|
+
}
|
|
166
|
+
assert(sourceArtifact === void 0 || migrateToId !== void 0, "A replacement artifact path requires an extension ID migration.");
|
|
167
|
+
assert(apiManifestArtifact === void 0 || migrateToId !== void 0, "A replacement API manifest artifact path requires an extension ID migration.");
|
|
168
|
+
if (migrateToId !== void 0) {
|
|
169
|
+
const extensionManifest = JSON.parse(await readFile(path.join(source.resolvedSourceDirectory, source.sourceManifest.embeddedExtensions), "utf8"));
|
|
170
|
+
assert(selectedExtensionId !== void 0, "Extension ID migration requires an extension ID.");
|
|
171
|
+
rewriteExtensionIdDocuments({
|
|
172
|
+
apiManifestArtifact,
|
|
173
|
+
extensionManifest,
|
|
174
|
+
newId: migrateToId,
|
|
175
|
+
oldId: selectedExtensionId,
|
|
176
|
+
project: source.project,
|
|
177
|
+
sourceArtifact
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
const downloads = await Promise.all(selected.map(async (extension) => {
|
|
181
|
+
let effectiveExtension = extension;
|
|
182
|
+
if (sourceArtifact !== void 0 || apiManifestArtifact !== void 0) {
|
|
183
|
+
const effectiveSource = { ...extension.source };
|
|
184
|
+
if (sourceArtifact !== void 0) effectiveSource.artifact = sourceArtifact;
|
|
185
|
+
if (apiManifestArtifact !== void 0) {
|
|
186
|
+
assert(effectiveSource.apiManifest, `Extension ${extension.id} has no managed API manifest metadata.`);
|
|
187
|
+
effectiveSource.apiManifest = {
|
|
188
|
+
...effectiveSource.apiManifest,
|
|
189
|
+
artifact: apiManifestArtifact
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
effectiveExtension = {
|
|
193
|
+
...extension,
|
|
194
|
+
source: effectiveSource
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
validateExtensionSourceMetadata(effectiveExtension);
|
|
198
|
+
if (effectiveExtension.source?.provider === "npm") {
|
|
199
|
+
const npmSource = await readNpmExtensionSource(effectiveExtension, source.resolvedSourceDirectory, {
|
|
200
|
+
allowVersionMismatch: mode === "update",
|
|
201
|
+
maximumArtifactBytes,
|
|
202
|
+
maximumManifestBytes
|
|
203
|
+
});
|
|
204
|
+
const apiManifestDownload = npmSource.apiManifestContents ? {
|
|
205
|
+
contents: npmSource.apiManifestContents,
|
|
206
|
+
manifest: parseExtensionApiManifest(npmSource.apiManifestContents, { expectedId: extension.id })
|
|
207
|
+
} : null;
|
|
208
|
+
let compatibilityChanges = [];
|
|
209
|
+
if (mode === "sync") {
|
|
210
|
+
validateManagedExtensionContents(extension, npmSource.contents);
|
|
211
|
+
if (apiManifestDownload) validateManagedExtensionApiManifest(extension, apiManifestDownload.contents);
|
|
212
|
+
} else if (apiManifestDownload) {
|
|
213
|
+
const installedApiManifestContents = source.extensionApiManifestContents.get(extension.id);
|
|
214
|
+
assert(installedApiManifestContents, `Managed extension API manifest is missing for ${extension.id}.`);
|
|
215
|
+
const installedApiManifest = validateManagedExtensionApiManifest(extension, installedApiManifestContents)?.manifest;
|
|
216
|
+
assert(installedApiManifest, `Managed extension API manifest is invalid: ${extension.id}`);
|
|
217
|
+
compatibilityChanges = compareExtensionApiManifests(installedApiManifest, apiManifestDownload.manifest);
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
apiManifestDownload,
|
|
221
|
+
commit: null,
|
|
222
|
+
compatibilityChanges,
|
|
223
|
+
contents: npmSource.contents,
|
|
224
|
+
effectiveExtension,
|
|
225
|
+
extension,
|
|
226
|
+
npmVersion: npmSource.version
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
const githubFetch = assertFetch(fetchImplementation);
|
|
230
|
+
const commit = mode === "sync" ? extension.source.resolvedCommit : await resolveGithubCommit(extension, githubFetch);
|
|
231
|
+
const expectedId = migrateToId ?? extension.id;
|
|
232
|
+
const [contents, apiManifestDownload] = await Promise.all([downloadExtension(effectiveExtension, commit, githubFetch, maximumArtifactBytes, expectedId), downloadExtensionApiManifest(effectiveExtension, commit, githubFetch, maximumManifestBytes, expectedId)]);
|
|
233
|
+
let compatibilityChanges = [];
|
|
234
|
+
if (mode === "sync") {
|
|
235
|
+
validateManagedExtensionContents(extension, contents);
|
|
236
|
+
if (apiManifestDownload) validateManagedExtensionApiManifest(extension, apiManifestDownload.contents);
|
|
237
|
+
} else if (apiManifestDownload) {
|
|
238
|
+
const installedApiManifestContents = source.extensionApiManifestContents.get(extension.id);
|
|
239
|
+
assert(installedApiManifestContents, `Managed extension API manifest is missing for ${extension.id}.`);
|
|
240
|
+
const installedApiManifest = validateManagedExtensionApiManifest(extension, installedApiManifestContents)?.manifest;
|
|
241
|
+
assert(installedApiManifest, `Managed extension API manifest is invalid: ${extension.id}`);
|
|
242
|
+
compatibilityChanges = compareExtensionApiManifests(installedApiManifest, apiManifestDownload.manifest);
|
|
243
|
+
}
|
|
244
|
+
return {
|
|
245
|
+
apiManifestDownload,
|
|
246
|
+
commit,
|
|
247
|
+
compatibilityChanges,
|
|
248
|
+
contents,
|
|
249
|
+
effectiveExtension,
|
|
250
|
+
extension
|
|
251
|
+
};
|
|
252
|
+
}));
|
|
253
|
+
const breakingApiChanges = downloads.flatMap((download) => download.compatibilityChanges.filter((change) => change.breaking).map((change) => ({
|
|
254
|
+
...change,
|
|
255
|
+
extensionId: download.extension.id
|
|
256
|
+
})));
|
|
257
|
+
if (breakingApiChanges.length > 0 && !allowBreakingApi) {
|
|
258
|
+
const details = downloads.filter((download) => download.compatibilityChanges.some((change) => change.breaking)).map((download) => formatExtensionApiCompatibilityChanges(download.extension.id, download.compatibilityChanges.filter((change) => change.breaking))).join("\n");
|
|
259
|
+
throw new Error(`Extension API update contains ${breakingApiChanges.length} breaking change(s). Review the reported paths, then use --allow-breaking-api with --yes to apply.
|
|
260
|
+
` + details);
|
|
261
|
+
}
|
|
262
|
+
const parentDirectory = path.dirname(source.resolvedSourceDirectory);
|
|
263
|
+
const candidateDirectory = await mkdtemp(path.join(parentDirectory, `.${path.basename(source.resolvedSourceDirectory)}.extensions-`));
|
|
264
|
+
let installed = false;
|
|
265
|
+
try {
|
|
266
|
+
await cp(source.resolvedSourceDirectory, candidateDirectory, {
|
|
267
|
+
preserveTimestamps: true,
|
|
268
|
+
recursive: true,
|
|
269
|
+
verbatimSymlinks: true
|
|
270
|
+
});
|
|
271
|
+
const manifestPath = path.join(candidateDirectory, source.sourceManifest.embeddedExtensions);
|
|
272
|
+
const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
|
|
273
|
+
const projectPath = path.join(candidateDirectory, source.sourceManifest.project);
|
|
274
|
+
const entriesById = new Map(manifest.extensions.map((entry) => [entry.id, entry]));
|
|
275
|
+
let manifestChanged = false;
|
|
276
|
+
let migration;
|
|
277
|
+
for (const { apiManifestDownload, commit, contents, effectiveExtension, extension, npmVersion } of downloads) {
|
|
278
|
+
if (migrateToId !== void 0) {
|
|
279
|
+
const project = JSON.parse(await readFile(projectPath, "utf8"));
|
|
280
|
+
migration = rewriteExtensionIdDocuments({
|
|
281
|
+
apiManifestArtifact,
|
|
282
|
+
extensionManifest: manifest,
|
|
283
|
+
newId: migrateToId,
|
|
284
|
+
oldId: extension.id,
|
|
285
|
+
project,
|
|
286
|
+
sourceArtifact: effectiveExtension.source?.artifact
|
|
287
|
+
});
|
|
288
|
+
const migratedSource = migration.extensionManifest.extensions[migration.extensionIndex].source;
|
|
289
|
+
assert(migratedSource, `Migrated extension has no source metadata: ${extension.id}`);
|
|
290
|
+
migratedSource.resolvedCommit = commit ?? void 0;
|
|
291
|
+
migratedSource.integrity = extensionIntegrity(contents);
|
|
292
|
+
if (apiManifestDownload && migratedSource.apiManifest) migratedSource.apiManifest.integrity = extensionApiManifestIntegrity(apiManifestDownload.contents);
|
|
293
|
+
const writes = [
|
|
294
|
+
writeFile(projectPath, `${JSON.stringify(migration.project, null, 2)}\n`),
|
|
295
|
+
writeFile(manifestPath, `${JSON.stringify(migration.extensionManifest, null, 2)}\n`),
|
|
296
|
+
writeFile(path.join(candidateDirectory, migration.newPath), contents)
|
|
297
|
+
];
|
|
298
|
+
if (apiManifestDownload && migration.newApiManifestPath) writes.push(writeFile(path.join(candidateDirectory, migration.newApiManifestPath), apiManifestDownload.contents));
|
|
299
|
+
await Promise.all(writes);
|
|
300
|
+
await rm(path.join(candidateDirectory, migration.oldPath));
|
|
301
|
+
if (migration.oldApiManifestPath) await rm(path.join(candidateDirectory, migration.oldApiManifestPath));
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
await writeFile(path.join(candidateDirectory, extension.path), contents);
|
|
305
|
+
if (apiManifestDownload) {
|
|
306
|
+
const apiManifestPath = extension.source?.apiManifest?.path;
|
|
307
|
+
assert(apiManifestPath, `Managed extension API manifest path is missing: ${extension.id}`);
|
|
308
|
+
await writeFile(path.join(candidateDirectory, apiManifestPath), apiManifestDownload.contents);
|
|
309
|
+
}
|
|
310
|
+
if (mode === "update") {
|
|
311
|
+
const candidateExtension = entriesById.get(extension.id);
|
|
312
|
+
assert(candidateExtension, `Extension manifest entry disappeared: ${extension.id}`);
|
|
313
|
+
const candidateSource = candidateExtension.source;
|
|
314
|
+
assert(candidateSource, `Extension manifest entry has no source: ${extension.id}`);
|
|
315
|
+
const integrity = extensionIntegrity(contents);
|
|
316
|
+
manifestChanged = manifestChanged || candidateSource.integrity !== integrity;
|
|
317
|
+
if (candidateSource.provider === "npm") {
|
|
318
|
+
manifestChanged = manifestChanged || candidateSource.version !== npmVersion;
|
|
319
|
+
candidateSource.version = npmVersion;
|
|
320
|
+
} else {
|
|
321
|
+
manifestChanged = manifestChanged || candidateSource.resolvedCommit !== commit;
|
|
322
|
+
candidateSource.resolvedCommit = commit ?? void 0;
|
|
323
|
+
}
|
|
324
|
+
candidateSource.integrity = integrity;
|
|
325
|
+
if (apiManifestDownload && candidateSource.apiManifest) {
|
|
326
|
+
const apiManifestIntegrity = extensionApiManifestIntegrity(apiManifestDownload.contents);
|
|
327
|
+
manifestChanged = manifestChanged || candidateSource.apiManifest.integrity !== apiManifestIntegrity;
|
|
328
|
+
candidateSource.apiManifest.integrity = apiManifestIntegrity;
|
|
329
|
+
candidateSource.apiManifest.formatVersion = apiManifestDownload.manifest.formatVersion;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (manifestChanged && migrateToId === void 0) await writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
334
|
+
if (migration) await createDeterministicSb3(candidateDirectory);
|
|
335
|
+
else await validateSb3Source(candidateDirectory);
|
|
336
|
+
const result = await installCandidate({
|
|
337
|
+
candidateDirectory,
|
|
338
|
+
confirmReplace,
|
|
339
|
+
initialSourceFingerprint: initialComparison.existingFingerprint,
|
|
340
|
+
sourceDirectory: source.resolvedSourceDirectory,
|
|
341
|
+
yes
|
|
342
|
+
});
|
|
343
|
+
installed = true;
|
|
344
|
+
return {
|
|
345
|
+
...result,
|
|
346
|
+
apiCompatibility: downloads.filter(({ apiManifestDownload }) => apiManifestDownload !== null).map(({ compatibilityChanges, extension }) => ({
|
|
347
|
+
changes: compatibilityChanges,
|
|
348
|
+
id: migrateToId ?? extension.id,
|
|
349
|
+
previousId: migrateToId === void 0 ? void 0 : extension.id
|
|
350
|
+
})),
|
|
351
|
+
extensions: downloads.map(({ commit, extension, npmVersion }) => {
|
|
352
|
+
const extensionSource = extension.source;
|
|
353
|
+
return extensionSource?.provider === "npm" ? {
|
|
354
|
+
id: extension.id,
|
|
355
|
+
package: extensionSource.package,
|
|
356
|
+
version: npmVersion
|
|
357
|
+
} : {
|
|
358
|
+
id: migrateToId ?? extension.id,
|
|
359
|
+
previousId: migrateToId === void 0 ? void 0 : extension.id,
|
|
360
|
+
resolvedCommit: commit
|
|
361
|
+
};
|
|
362
|
+
}),
|
|
363
|
+
migration: migration === void 0 ? null : {
|
|
364
|
+
counts: migration.counts,
|
|
365
|
+
fromId: selectedExtensionId,
|
|
366
|
+
toId: migrateToId,
|
|
367
|
+
totalChanges: migration.totalChanges,
|
|
368
|
+
unclassifiedReferences: migration.unclassifiedReferences
|
|
369
|
+
},
|
|
370
|
+
mode,
|
|
371
|
+
sourceDirectory: source.resolvedSourceDirectory
|
|
372
|
+
};
|
|
373
|
+
} finally {
|
|
374
|
+
if (!installed) await rm(candidateDirectory, {
|
|
375
|
+
recursive: true,
|
|
376
|
+
force: true
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async function extensionStatus(sourceDirectory, { fetch: fetchImplementation = globalThis.fetch } = {}) {
|
|
381
|
+
const source = await inspectExtensionSource(sourceDirectory);
|
|
382
|
+
return Promise.all(managedExtensions(source).map(async (extension) => {
|
|
383
|
+
const extensionSource = extension.source;
|
|
384
|
+
assert(extensionSource, `Managed extension has no source metadata: ${extension.id}`);
|
|
385
|
+
let local = "valid";
|
|
386
|
+
try {
|
|
387
|
+
const contents = source.extensionContents.get(extension.id);
|
|
388
|
+
assert(contents, `Managed extension contents are missing: ${extension.id}`);
|
|
389
|
+
validateManagedExtensionContents(extension, contents);
|
|
390
|
+
if (extensionSource.apiManifest) {
|
|
391
|
+
const apiManifestContents = source.extensionApiManifestContents.get(extension.id);
|
|
392
|
+
assert(apiManifestContents, `Managed extension API manifest is missing: ${extension.id}`);
|
|
393
|
+
validateManagedExtensionApiManifest(extension, apiManifestContents);
|
|
394
|
+
}
|
|
395
|
+
} catch {
|
|
396
|
+
local = "modified";
|
|
397
|
+
}
|
|
398
|
+
if (extensionSource.provider === "npm") {
|
|
399
|
+
const npmSource = await readNpmExtensionSource(extension, source.resolvedSourceDirectory, {
|
|
400
|
+
allowVersionMismatch: true,
|
|
401
|
+
maximumArtifactBytes: defaultExtensionArtifactSizeLimit,
|
|
402
|
+
maximumManifestBytes: defaultExtensionApiManifestSizeLimit
|
|
403
|
+
});
|
|
404
|
+
if (npmSource.version === extensionSource.version) {
|
|
405
|
+
validateManagedExtensionContents(extension, npmSource.contents);
|
|
406
|
+
if (npmSource.apiManifestContents) validateManagedExtensionApiManifest(extension, npmSource.apiManifestContents);
|
|
407
|
+
}
|
|
408
|
+
return {
|
|
409
|
+
id: extension.id,
|
|
410
|
+
installedVersion: npmSource.version,
|
|
411
|
+
local,
|
|
412
|
+
package: extensionSource.package,
|
|
413
|
+
state: npmSource.version === extensionSource.version ? "current" : "update-available",
|
|
414
|
+
version: extensionSource.version
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
const remoteCommit = await resolveGithubCommit(extension, assertFetch(fetchImplementation));
|
|
418
|
+
return {
|
|
419
|
+
id: extension.id,
|
|
420
|
+
local,
|
|
421
|
+
ref: extensionSource.ref,
|
|
422
|
+
remoteCommit,
|
|
423
|
+
resolvedCommit: extensionSource.resolvedCommit,
|
|
424
|
+
state: remoteCommit === extensionSource.resolvedCommit ? "current" : "update-available"
|
|
425
|
+
};
|
|
426
|
+
}));
|
|
427
|
+
}
|
|
428
|
+
async function syncExtensions({ confirmReplace, fetch: fetchImplementation = globalThis.fetch, maximumArtifactBytes = defaultExtensionArtifactSizeLimit, maximumManifestBytes = defaultExtensionApiManifestSizeLimit, sourceDirectory, yes = false }) {
|
|
429
|
+
return updateCandidate({
|
|
430
|
+
allowBreakingApi: false,
|
|
431
|
+
apiManifestArtifact: void 0,
|
|
432
|
+
confirmReplace,
|
|
433
|
+
fetchImplementation,
|
|
434
|
+
maximumArtifactBytes,
|
|
435
|
+
maximumManifestBytes,
|
|
436
|
+
migrateToId: void 0,
|
|
437
|
+
mode: "sync",
|
|
438
|
+
selectedExtensionId: void 0,
|
|
439
|
+
sourceArtifact: void 0,
|
|
440
|
+
sourceDirectory,
|
|
441
|
+
yes
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
async function updateExtensions({ allowBreakingApi = false, apiManifestArtifact, confirmReplace, extensionId, fetch: fetchImplementation = globalThis.fetch, maximumArtifactBytes = defaultExtensionArtifactSizeLimit, maximumManifestBytes = defaultExtensionApiManifestSizeLimit, migrateToId, sourceArtifact, sourceDirectory, yes = false }) {
|
|
445
|
+
return updateCandidate({
|
|
446
|
+
allowBreakingApi,
|
|
447
|
+
apiManifestArtifact,
|
|
448
|
+
confirmReplace,
|
|
449
|
+
fetchImplementation,
|
|
450
|
+
maximumArtifactBytes,
|
|
451
|
+
maximumManifestBytes,
|
|
452
|
+
migrateToId,
|
|
453
|
+
mode: "update",
|
|
454
|
+
selectedExtensionId: extensionId,
|
|
455
|
+
sourceArtifact,
|
|
456
|
+
sourceDirectory,
|
|
457
|
+
yes
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
//#endregion
|
|
461
|
+
export { defaultExtensionArtifactSizeLimit, extensionStatus, syncExtensions, updateExtensions };
|
|
462
|
+
|
|
463
|
+
//# sourceMappingURL=extension-sync.js.map
|