@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
package/src/source.js
DELETED
|
@@ -1,468 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
-
|
|
3
|
-
import {createHash} from 'node:crypto';
|
|
4
|
-
import {lstat, readdir, readFile} from 'node:fs/promises';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
|
|
7
|
-
import {strToU8, zipSync} from 'fflate';
|
|
8
|
-
|
|
9
|
-
import {validateArchiveEntryName} from './archive.js';
|
|
10
|
-
import {validateManagedExtensionApiManifest} from './extension-api-manifest.js';
|
|
11
|
-
import {buildExtensionBundles, validateExtensionBundleConfigurations} from './extension-bundle.js';
|
|
12
|
-
import {
|
|
13
|
-
validateExtensionSourceMetadata,
|
|
14
|
-
validateManagedExtensionContents,
|
|
15
|
-
} from './extension-dependencies.js';
|
|
16
|
-
import {applyProjectAssetAdditions} from './project-asset-additions.js';
|
|
17
|
-
import {cleanUpTurboWarpBlocks} from './turbowarp-clean-up.js';
|
|
18
|
-
|
|
19
|
-
export const sourceFormatVersion = 1;
|
|
20
|
-
export const fixedZipTimestamp = new Date(1980, 0, 1, 0, 0, 0, 0);
|
|
21
|
-
|
|
22
|
-
function assert(condition, message) {
|
|
23
|
-
if (!condition) {
|
|
24
|
-
throw new Error(message);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async function readJson(filePath, description) {
|
|
29
|
-
try {
|
|
30
|
-
return JSON.parse(await readFile(filePath, 'utf8'));
|
|
31
|
-
} catch (error) {
|
|
32
|
-
throw new Error(`${description} is not valid JSON: ${filePath} (${error.message})`, {
|
|
33
|
-
cause: error,
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async function assertDirectory(directoryPath, description) {
|
|
39
|
-
const stats = await lstat(directoryPath);
|
|
40
|
-
assert(
|
|
41
|
-
stats.isDirectory() && !stats.isSymbolicLink(),
|
|
42
|
-
`${description} must be a directory, not a file or symbolic link: ${directoryPath}`,
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function listRegularFiles(rootDirectory, description) {
|
|
47
|
-
await assertDirectory(rootDirectory, description);
|
|
48
|
-
const files = [];
|
|
49
|
-
|
|
50
|
-
async function visit(directory, relativeDirectory) {
|
|
51
|
-
const entries = await readdir(directory, {withFileTypes: true});
|
|
52
|
-
entries.sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
|
|
53
|
-
for (const entry of entries) {
|
|
54
|
-
const relativePath = relativeDirectory
|
|
55
|
-
? path.posix.join(relativeDirectory, entry.name)
|
|
56
|
-
: entry.name;
|
|
57
|
-
const absolutePath = path.join(directory, entry.name);
|
|
58
|
-
if (entry.isDirectory()) {
|
|
59
|
-
await visit(absolutePath, relativePath);
|
|
60
|
-
} else {
|
|
61
|
-
assert(
|
|
62
|
-
entry.isFile(),
|
|
63
|
-
`${description} contains a symbolic link or unsupported entry: ${relativePath}`,
|
|
64
|
-
);
|
|
65
|
-
files.push(relativePath);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
await visit(rootDirectory, '');
|
|
71
|
-
return files;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function assertSameFileSet(actualFiles, expectedFiles, description) {
|
|
75
|
-
const actual = new Set(actualFiles);
|
|
76
|
-
const expected = new Set(expectedFiles);
|
|
77
|
-
const missing = [...expected].filter((filePath) => !actual.has(filePath)).sort();
|
|
78
|
-
const extra = [...actual].filter((filePath) => !expected.has(filePath)).sort();
|
|
79
|
-
assert(
|
|
80
|
-
missing.length === 0 && extra.length === 0,
|
|
81
|
-
`${description} does not match its manifest. Missing: ${missing.join(', ') || '(none)'}. ` +
|
|
82
|
-
`Extra: ${extra.join(', ') || '(none)'}.`,
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function md5(contents) {
|
|
87
|
-
return createHash('md5').update(contents).digest('hex');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function validateSourceManifest(manifest) {
|
|
91
|
-
assert(
|
|
92
|
-
manifest && typeof manifest === 'object' && !Array.isArray(manifest),
|
|
93
|
-
'sb3-source.json must contain an object.',
|
|
94
|
-
);
|
|
95
|
-
assert(
|
|
96
|
-
manifest.formatVersion === sourceFormatVersion,
|
|
97
|
-
`Unsupported SB3 source format version: ${manifest.formatVersion}`,
|
|
98
|
-
);
|
|
99
|
-
assert(
|
|
100
|
-
manifest.project === 'project.source.json',
|
|
101
|
-
'SB3 source format 1 requires project.source.json.',
|
|
102
|
-
);
|
|
103
|
-
assert(
|
|
104
|
-
manifest.embeddedExtensions === 'embedded-extensions.json',
|
|
105
|
-
'SB3 source format 1 requires embedded-extensions.json.',
|
|
106
|
-
);
|
|
107
|
-
assert(
|
|
108
|
-
manifest.assetsDirectory === 'assets',
|
|
109
|
-
'SB3 source format 1 requires the assets directory.',
|
|
110
|
-
);
|
|
111
|
-
assert(
|
|
112
|
-
Array.isArray(manifest.archiveEntries) && manifest.archiveEntries.length > 0,
|
|
113
|
-
'sb3-source.json archiveEntries must be a non-empty array.',
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
const seenEntries = new Set();
|
|
117
|
-
let projectEntryCount = 0;
|
|
118
|
-
for (const entryName of manifest.archiveEntries) {
|
|
119
|
-
validateArchiveEntryName(entryName);
|
|
120
|
-
assert(!seenEntries.has(entryName), `Duplicate archive entry in manifest: ${entryName}`);
|
|
121
|
-
assert(
|
|
122
|
-
!entryName.endsWith('/'),
|
|
123
|
-
`Directory ZIP entries are not supported in SB3 source format 1: ${entryName}`,
|
|
124
|
-
);
|
|
125
|
-
assert(
|
|
126
|
-
!/^(?:0|[1-9][0-9]*)$/u.test(entryName),
|
|
127
|
-
`Integer-like ZIP entry names cannot preserve JavaScript object order: ${entryName}`,
|
|
128
|
-
);
|
|
129
|
-
seenEntries.add(entryName);
|
|
130
|
-
if (entryName === 'project.json') {
|
|
131
|
-
projectEntryCount += 1;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
assert(projectEntryCount === 1, 'archiveEntries must contain project.json exactly once.');
|
|
135
|
-
return manifest.archiveEntries.filter((entryName) => entryName !== 'project.json');
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function validateExtensionManifest(extensionManifest, project, extensionFiles) {
|
|
139
|
-
assert(
|
|
140
|
-
extensionManifest && typeof extensionManifest === 'object' && !Array.isArray(extensionManifest),
|
|
141
|
-
'embedded-extensions.json must contain an object.',
|
|
142
|
-
);
|
|
143
|
-
assert(
|
|
144
|
-
extensionManifest.formatVersion === sourceFormatVersion,
|
|
145
|
-
`Unsupported embedded extension manifest version: ${extensionManifest.formatVersion}`,
|
|
146
|
-
);
|
|
147
|
-
assert(
|
|
148
|
-
Array.isArray(extensionManifest.extensions),
|
|
149
|
-
'embedded-extensions.json extensions must be an array.',
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
const extensionUrls = project.extensionURLs ?? {};
|
|
153
|
-
assert(
|
|
154
|
-
extensionUrls && typeof extensionUrls === 'object' && !Array.isArray(extensionUrls),
|
|
155
|
-
'project.source.json extensionURLs must be an object when present.',
|
|
156
|
-
);
|
|
157
|
-
const extensionsById = new Map();
|
|
158
|
-
const expectedFiles = [];
|
|
159
|
-
|
|
160
|
-
for (const extension of extensionManifest.extensions) {
|
|
161
|
-
assert(
|
|
162
|
-
extension && typeof extension === 'object' && !Array.isArray(extension),
|
|
163
|
-
'Each embedded extension manifest entry must be an object.',
|
|
164
|
-
);
|
|
165
|
-
assert(
|
|
166
|
-
typeof extension.id === 'string' && /^[A-Za-z0-9._-]+$/u.test(extension.id),
|
|
167
|
-
`Invalid embedded extension ID: ${JSON.stringify(extension.id)}`,
|
|
168
|
-
);
|
|
169
|
-
assert(!extensionsById.has(extension.id), `Duplicate embedded extension ID: ${extension.id}`);
|
|
170
|
-
const expectedPath = `extensions/${extension.id}.js`;
|
|
171
|
-
assert(
|
|
172
|
-
extension.path === expectedPath,
|
|
173
|
-
`Embedded extension path must match its ID: expected ${expectedPath}, got ${extension.path}`,
|
|
174
|
-
);
|
|
175
|
-
assert(
|
|
176
|
-
typeof extension.mediaType === 'string' &&
|
|
177
|
-
extension.mediaType.length > 0 &&
|
|
178
|
-
!/[;,\r\n]/u.test(extension.mediaType),
|
|
179
|
-
`Invalid media type for embedded extension ${extension.id}: ${extension.mediaType}`,
|
|
180
|
-
);
|
|
181
|
-
assert(
|
|
182
|
-
Array.isArray(extension.parameters) &&
|
|
183
|
-
extension.parameters.every(
|
|
184
|
-
(parameter) =>
|
|
185
|
-
typeof parameter === 'string' && parameter.length > 0 && !/[;,\r\n]/u.test(parameter),
|
|
186
|
-
),
|
|
187
|
-
`Invalid data URL parameters for embedded extension ${extension.id}.`,
|
|
188
|
-
);
|
|
189
|
-
assert(
|
|
190
|
-
extension.encoding === 'base64' || extension.encoding === 'percent',
|
|
191
|
-
`Unsupported encoding for embedded extension ${extension.id}: ${extension.encoding}`,
|
|
192
|
-
);
|
|
193
|
-
assert(
|
|
194
|
-
extensionUrls[extension.id] === `embedded-extension:${extension.path}`,
|
|
195
|
-
`project.source.json mapping does not match embedded extension ${extension.id}.`,
|
|
196
|
-
);
|
|
197
|
-
validateExtensionSourceMetadata(extension);
|
|
198
|
-
extensionsById.set(extension.id, extension);
|
|
199
|
-
expectedFiles.push(`${extension.id}.js`);
|
|
200
|
-
if (extension.source?.apiManifest) {
|
|
201
|
-
expectedFiles.push(path.posix.relative('extensions', extension.source.apiManifest.path));
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
for (const [extensionId, extensionUrl] of Object.entries(extensionUrls)) {
|
|
206
|
-
assert(
|
|
207
|
-
typeof extensionUrl !== 'string' || !extensionUrl.startsWith('data:'),
|
|
208
|
-
`project.source.json still contains an embedded data URL: ${extensionId}`,
|
|
209
|
-
);
|
|
210
|
-
if (typeof extensionUrl === 'string' && extensionUrl.startsWith('embedded-extension:')) {
|
|
211
|
-
assert(
|
|
212
|
-
extensionsById.has(extensionId),
|
|
213
|
-
`project.source.json has no metadata for embedded extension ${extensionId}.`,
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
assertSameFileSet(extensionFiles, expectedFiles, 'Embedded extension files');
|
|
219
|
-
return {
|
|
220
|
-
extensionBundles: validateExtensionBundleConfigurations(
|
|
221
|
-
extensionManifest.extensionBundles,
|
|
222
|
-
extensionManifest.extensions,
|
|
223
|
-
),
|
|
224
|
-
extensions: extensionManifest.extensions,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function collectAssetReferences(project) {
|
|
229
|
-
assert(Array.isArray(project.targets), 'project.source.json targets must be an array.');
|
|
230
|
-
const references = [];
|
|
231
|
-
for (const [targetIndex, target] of project.targets.entries()) {
|
|
232
|
-
assert(
|
|
233
|
-
target && typeof target === 'object' && !Array.isArray(target),
|
|
234
|
-
`Project target ${targetIndex} must be an object.`,
|
|
235
|
-
);
|
|
236
|
-
for (const [collectionName, kind] of [
|
|
237
|
-
['costumes', 'costume'],
|
|
238
|
-
['sounds', 'sound'],
|
|
239
|
-
]) {
|
|
240
|
-
const assets = target[collectionName] ?? [];
|
|
241
|
-
assert(
|
|
242
|
-
Array.isArray(assets),
|
|
243
|
-
`Project target ${targetIndex} ${collectionName} must be an array.`,
|
|
244
|
-
);
|
|
245
|
-
for (const [assetIndex, asset] of assets.entries()) {
|
|
246
|
-
assert(
|
|
247
|
-
asset && typeof asset === 'object' && !Array.isArray(asset),
|
|
248
|
-
`Project ${kind} ${targetIndex}:${assetIndex} must be an object.`,
|
|
249
|
-
);
|
|
250
|
-
references.push({asset, description: `${kind} ${targetIndex}:${assetIndex}`});
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return references;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function percentEncode(contents) {
|
|
258
|
-
return [...contents]
|
|
259
|
-
.map((byte) => `%${byte.toString(16).padStart(2, '0').toUpperCase()}`)
|
|
260
|
-
.join('');
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function encodeExtensionDataUrl(extension, contents) {
|
|
264
|
-
const metadata = [extension.mediaType, ...extension.parameters].join(';');
|
|
265
|
-
if (extension.encoding === 'base64') {
|
|
266
|
-
return `data:${metadata};base64,${Buffer.from(contents).toString('base64')}`;
|
|
267
|
-
}
|
|
268
|
-
return `data:${metadata},${percentEncode(contents)}`;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
async function inspectSb3Source(sourceDirectory, validateManagedExtensions) {
|
|
272
|
-
const resolvedSourceDirectory = path.resolve(sourceDirectory);
|
|
273
|
-
await assertDirectory(resolvedSourceDirectory, 'SB3 source');
|
|
274
|
-
const sourceManifest = await readJson(
|
|
275
|
-
path.join(resolvedSourceDirectory, 'sb3-source.json'),
|
|
276
|
-
'SB3 source manifest',
|
|
277
|
-
);
|
|
278
|
-
const assetEntries = validateSourceManifest(sourceManifest);
|
|
279
|
-
const project = await readJson(
|
|
280
|
-
path.join(resolvedSourceDirectory, sourceManifest.project),
|
|
281
|
-
'SB3 project source',
|
|
282
|
-
);
|
|
283
|
-
assert(
|
|
284
|
-
project && typeof project === 'object' && !Array.isArray(project),
|
|
285
|
-
'project.source.json must contain an object.',
|
|
286
|
-
);
|
|
287
|
-
const extensionManifest = await readJson(
|
|
288
|
-
path.join(resolvedSourceDirectory, sourceManifest.embeddedExtensions),
|
|
289
|
-
'Embedded extension manifest',
|
|
290
|
-
);
|
|
291
|
-
|
|
292
|
-
const assetsDirectory = path.join(resolvedSourceDirectory, sourceManifest.assetsDirectory);
|
|
293
|
-
const extensionsDirectory = path.join(resolvedSourceDirectory, 'extensions');
|
|
294
|
-
const [assetFiles, extensionFiles] = await Promise.all([
|
|
295
|
-
listRegularFiles(assetsDirectory, 'Assets directory'),
|
|
296
|
-
listRegularFiles(extensionsDirectory, 'Extensions directory'),
|
|
297
|
-
]);
|
|
298
|
-
assertSameFileSet(assetFiles, assetEntries, 'Asset files');
|
|
299
|
-
const {extensionBundles, extensions} = validateExtensionManifest(
|
|
300
|
-
extensionManifest,
|
|
301
|
-
project,
|
|
302
|
-
extensionFiles,
|
|
303
|
-
);
|
|
304
|
-
|
|
305
|
-
const assetContents = new Map();
|
|
306
|
-
await Promise.all(
|
|
307
|
-
assetFiles.map(async (assetPath) => {
|
|
308
|
-
assetContents.set(assetPath, await readFile(path.join(assetsDirectory, assetPath)));
|
|
309
|
-
}),
|
|
310
|
-
);
|
|
311
|
-
const referencedAssets = new Set();
|
|
312
|
-
const references = collectAssetReferences(project);
|
|
313
|
-
for (const {asset, description} of references) {
|
|
314
|
-
assert(
|
|
315
|
-
typeof asset.assetId === 'string' && /^[a-f0-9]{32}$/u.test(asset.assetId),
|
|
316
|
-
`Invalid assetId for ${description}: ${asset.assetId}`,
|
|
317
|
-
);
|
|
318
|
-
assert(
|
|
319
|
-
typeof asset.dataFormat === 'string' && /^[A-Za-z0-9]+$/u.test(asset.dataFormat),
|
|
320
|
-
`Invalid dataFormat for ${description}: ${asset.dataFormat}`,
|
|
321
|
-
);
|
|
322
|
-
const expectedFilename = `${asset.assetId}.${asset.dataFormat}`;
|
|
323
|
-
assert(
|
|
324
|
-
asset.md5ext === expectedFilename,
|
|
325
|
-
`Asset filename mismatch for ${description}: expected ${expectedFilename}, got ${asset.md5ext}`,
|
|
326
|
-
);
|
|
327
|
-
const contents = assetContents.get(expectedFilename);
|
|
328
|
-
assert(contents, `Referenced asset is missing for ${description}: ${expectedFilename}`);
|
|
329
|
-
const actualAssetId = md5(contents);
|
|
330
|
-
assert(
|
|
331
|
-
actualAssetId === asset.assetId,
|
|
332
|
-
`Asset content hash mismatch for ${description}: expected ${asset.assetId}, got ${actualAssetId}`,
|
|
333
|
-
);
|
|
334
|
-
referencedAssets.add(expectedFilename);
|
|
335
|
-
}
|
|
336
|
-
const unreferencedAssets = assetFiles.filter((assetPath) => !referencedAssets.has(assetPath));
|
|
337
|
-
assert(
|
|
338
|
-
unreferencedAssets.length === 0,
|
|
339
|
-
`Asset manifest contains unreferenced files: ${unreferencedAssets.join(', ')}`,
|
|
340
|
-
);
|
|
341
|
-
|
|
342
|
-
const extensionContents = new Map();
|
|
343
|
-
const extensionApiManifestContents = new Map();
|
|
344
|
-
await Promise.all(
|
|
345
|
-
extensions.map(async (extension) => {
|
|
346
|
-
const contents = await readFile(path.join(resolvedSourceDirectory, extension.path));
|
|
347
|
-
if (validateManagedExtensions) {
|
|
348
|
-
validateManagedExtensionContents(extension, contents);
|
|
349
|
-
}
|
|
350
|
-
extensionContents.set(extension.id, contents);
|
|
351
|
-
if (extension.source?.apiManifest) {
|
|
352
|
-
const apiManifestContents = await readFile(
|
|
353
|
-
path.join(resolvedSourceDirectory, extension.source.apiManifest.path),
|
|
354
|
-
);
|
|
355
|
-
if (validateManagedExtensions) {
|
|
356
|
-
validateManagedExtensionApiManifest(extension, apiManifestContents);
|
|
357
|
-
}
|
|
358
|
-
extensionApiManifestContents.set(extension.id, apiManifestContents);
|
|
359
|
-
}
|
|
360
|
-
}),
|
|
361
|
-
);
|
|
362
|
-
|
|
363
|
-
return {
|
|
364
|
-
assetContents,
|
|
365
|
-
assetReferenceCount: references.length,
|
|
366
|
-
extensions,
|
|
367
|
-
extensionApiManifestContents,
|
|
368
|
-
extensionBundles,
|
|
369
|
-
extensionContents,
|
|
370
|
-
project,
|
|
371
|
-
resolvedSourceDirectory,
|
|
372
|
-
sourceManifest,
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
export async function inspectSb3SourceForExtensionSync(sourceDirectory) {
|
|
377
|
-
return inspectSb3Source(sourceDirectory, false);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
export async function validateSb3Source(sourceDirectory) {
|
|
381
|
-
const source = await inspectSb3Source(sourceDirectory, true);
|
|
382
|
-
buildExtensionBundles({
|
|
383
|
-
extensionBundles: source.extensionBundles,
|
|
384
|
-
extensionContents: source.extensionContents,
|
|
385
|
-
extensions: source.extensions,
|
|
386
|
-
project: source.project,
|
|
387
|
-
});
|
|
388
|
-
return source;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* @param {string} sourceDirectory
|
|
393
|
-
* @param {{allowedAssetRoots?: string[], cleanUpBlocks?: boolean, projectAssetsPath?: string}} [options]
|
|
394
|
-
*/
|
|
395
|
-
export async function createDeterministicSb3(sourceDirectory, options = {}) {
|
|
396
|
-
const {allowedAssetRoots = [], cleanUpBlocks = false, projectAssetsPath} = options;
|
|
397
|
-
assert(typeof cleanUpBlocks === 'boolean', 'cleanUpBlocks must be a boolean.');
|
|
398
|
-
assert(Array.isArray(allowedAssetRoots), 'allowedAssetRoots must be an array.');
|
|
399
|
-
assert(
|
|
400
|
-
projectAssetsPath === undefined || typeof projectAssetsPath === 'string',
|
|
401
|
-
'projectAssetsPath must be a string when provided.',
|
|
402
|
-
);
|
|
403
|
-
const source = await validateSb3Source(sourceDirectory);
|
|
404
|
-
const projectAssets = projectAssetsPath
|
|
405
|
-
? await applyProjectAssetAdditions({
|
|
406
|
-
allowedAssetRoots,
|
|
407
|
-
assetContents: source.assetContents,
|
|
408
|
-
archiveEntries: source.sourceManifest.archiveEntries,
|
|
409
|
-
manifestPath: projectAssetsPath,
|
|
410
|
-
project: source.project,
|
|
411
|
-
})
|
|
412
|
-
: {
|
|
413
|
-
archiveEntries: source.sourceManifest.archiveEntries,
|
|
414
|
-
assetContents: source.assetContents,
|
|
415
|
-
assetReferenceCount: 0,
|
|
416
|
-
project: source.project,
|
|
417
|
-
summary: null,
|
|
418
|
-
};
|
|
419
|
-
const bundled = buildExtensionBundles({
|
|
420
|
-
extensionBundles: source.extensionBundles,
|
|
421
|
-
extensionContents: source.extensionContents,
|
|
422
|
-
extensions: source.extensions,
|
|
423
|
-
project: projectAssets.project,
|
|
424
|
-
});
|
|
425
|
-
let project = structuredClone(bundled.project);
|
|
426
|
-
let blockCleanUp = null;
|
|
427
|
-
if (cleanUpBlocks) {
|
|
428
|
-
const cleaned = cleanUpTurboWarpBlocks(project);
|
|
429
|
-
project = cleaned.project;
|
|
430
|
-
blockCleanUp = {
|
|
431
|
-
movedCommentCount: cleaned.movedCommentCount,
|
|
432
|
-
movedScriptCount: cleaned.movedScriptCount,
|
|
433
|
-
scriptCount: cleaned.scriptCount,
|
|
434
|
-
targetCount: cleaned.targetCount,
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
for (const extension of bundled.extensions) {
|
|
438
|
-
project.extensionURLs[extension.id] = encodeExtensionDataUrl(
|
|
439
|
-
extension,
|
|
440
|
-
bundled.extensionContents.get(extension.id),
|
|
441
|
-
);
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
/** @type {Record<string, Uint8Array>} */
|
|
445
|
-
const archiveEntries = {};
|
|
446
|
-
for (const entryName of projectAssets.archiveEntries) {
|
|
447
|
-
archiveEntries[entryName] =
|
|
448
|
-
entryName === 'project.json'
|
|
449
|
-
? strToU8(`${JSON.stringify(project)}\n`)
|
|
450
|
-
: projectAssets.assetContents.get(entryName);
|
|
451
|
-
}
|
|
452
|
-
const archive = zipSync(archiveEntries, {
|
|
453
|
-
level: 6,
|
|
454
|
-
mtime: fixedZipTimestamp,
|
|
455
|
-
});
|
|
456
|
-
|
|
457
|
-
return {
|
|
458
|
-
archive,
|
|
459
|
-
assetCount: projectAssets.assetContents.size,
|
|
460
|
-
assetReferenceCount: source.assetReferenceCount + projectAssets.assetReferenceCount,
|
|
461
|
-
blockCleanUp,
|
|
462
|
-
bundlePlans: bundled.bundlePlans,
|
|
463
|
-
embeddedExtensionCount: bundled.extensions.length,
|
|
464
|
-
entryCount: projectAssets.archiveEntries.length,
|
|
465
|
-
projectAssetAdditions: projectAssets.summary,
|
|
466
|
-
source,
|
|
467
|
-
};
|
|
468
|
-
}
|