@kubohiroya/sb3-toolchain 0.4.0 → 0.6.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.
@@ -19,6 +19,7 @@ import {
19
19
  validateManagedExtensionContents,
20
20
  } from './extension-dependencies.js';
21
21
  import {rewriteExtensionIdDocuments, validateNewExtensionId} from './extension-id-migration.js';
22
+ import {readNpmExtensionSource} from './npm-extension-source.js';
22
23
  import {
23
24
  assertNoInterruptedRollback,
24
25
  compareDirectories,
@@ -309,6 +310,9 @@ async function updateCandidate({
309
310
  `Managed extension was not found: ${selectedExtensionId}`,
310
311
  );
311
312
  }
313
+ const selected = managed.filter(
314
+ (extension) => selectedExtensionId === undefined || extension.id === selectedExtensionId,
315
+ );
312
316
  if (migrateToId !== undefined) {
313
317
  assert(mode === 'update', 'Extension ID migration is only available during update.');
314
318
  assert(
@@ -316,6 +320,10 @@ async function updateCandidate({
316
320
  'Extension ID migration requires an explicit existing extension ID.',
317
321
  );
318
322
  validateNewExtensionId(migrateToId);
323
+ assert(
324
+ selected.every((extension) => extension.source.provider === 'github'),
325
+ 'Extension ID migration is not supported for npm-managed extensions.',
326
+ );
319
327
  }
320
328
  assert(
321
329
  sourceArtifact === undefined || migrateToId !== undefined,
@@ -325,9 +333,6 @@ async function updateCandidate({
325
333
  apiManifestArtifact === undefined || migrateToId !== undefined,
326
334
  'A replacement API manifest artifact path requires an extension ID migration.',
327
335
  );
328
- const selected = managed.filter(
329
- (extension) => selectedExtensionId === undefined || extension.id === selectedExtensionId,
330
- );
331
336
  if (migrateToId !== undefined) {
332
337
  const extensionManifest = JSON.parse(
333
338
  await readFile(
@@ -364,23 +369,75 @@ async function updateCandidate({
364
369
  effectiveExtension = {...extension, source: effectiveSource};
365
370
  }
366
371
  validateExtensionSourceMetadata(effectiveExtension);
372
+ if (effectiveExtension.source.provider === 'npm') {
373
+ const npmSource = await readNpmExtensionSource(
374
+ effectiveExtension,
375
+ source.resolvedSourceDirectory,
376
+ {
377
+ allowVersionMismatch: mode === 'update',
378
+ maximumArtifactBytes,
379
+ maximumManifestBytes,
380
+ },
381
+ );
382
+ const apiManifestDownload = npmSource.apiManifestContents
383
+ ? {
384
+ contents: npmSource.apiManifestContents,
385
+ manifest: parseExtensionApiManifest(npmSource.apiManifestContents, {
386
+ expectedId: extension.id,
387
+ }),
388
+ }
389
+ : null;
390
+ let compatibilityChanges = [];
391
+ if (mode === 'sync') {
392
+ validateManagedExtensionContents(extension, npmSource.contents);
393
+ if (apiManifestDownload) {
394
+ validateManagedExtensionApiManifest(extension, apiManifestDownload.contents);
395
+ }
396
+ } else if (apiManifestDownload) {
397
+ const installedApiManifestContents = source.extensionApiManifestContents.get(
398
+ extension.id,
399
+ );
400
+ assert(
401
+ installedApiManifestContents,
402
+ `Managed extension API manifest is missing for ${extension.id}.`,
403
+ );
404
+ const installedApiManifest = validateManagedExtensionApiManifest(
405
+ extension,
406
+ installedApiManifestContents,
407
+ ).manifest;
408
+ compatibilityChanges = compareExtensionApiManifests(
409
+ installedApiManifest,
410
+ apiManifestDownload.manifest,
411
+ );
412
+ }
413
+ return {
414
+ apiManifestDownload,
415
+ commit: null,
416
+ compatibilityChanges,
417
+ contents: npmSource.contents,
418
+ effectiveExtension,
419
+ extension,
420
+ npmVersion: npmSource.version,
421
+ };
422
+ }
423
+ const githubFetch = assertFetch(fetchImplementation);
367
424
  const commit =
368
425
  mode === 'sync'
369
426
  ? extension.source.resolvedCommit
370
- : await resolveGithubCommit(extension, fetchImplementation);
427
+ : await resolveGithubCommit(extension, githubFetch);
371
428
  const expectedId = migrateToId ?? extension.id;
372
429
  const [contents, apiManifestDownload] = await Promise.all([
373
430
  downloadExtension(
374
431
  effectiveExtension,
375
432
  commit,
376
- fetchImplementation,
433
+ githubFetch,
377
434
  maximumArtifactBytes,
378
435
  expectedId,
379
436
  ),
380
437
  downloadExtensionApiManifest(
381
438
  effectiveExtension,
382
439
  commit,
383
- fetchImplementation,
440
+ githubFetch,
384
441
  maximumManifestBytes,
385
442
  expectedId,
386
443
  ),
@@ -461,6 +518,7 @@ async function updateCandidate({
461
518
  contents,
462
519
  effectiveExtension,
463
520
  extension,
521
+ npmVersion,
464
522
  } of downloads) {
465
523
  if (migrateToId !== undefined) {
466
524
  const project = JSON.parse(await readFile(projectPath, 'utf8'));
@@ -511,11 +569,14 @@ async function updateCandidate({
511
569
  const candidateExtension = entriesById.get(extension.id);
512
570
  assert(candidateExtension, `Extension manifest entry disappeared: ${extension.id}`);
513
571
  const integrity = extensionIntegrity(contents);
514
- manifestChanged =
515
- manifestChanged ||
516
- candidateExtension.source.resolvedCommit !== commit ||
517
- candidateExtension.source.integrity !== integrity;
518
- candidateExtension.source.resolvedCommit = commit;
572
+ manifestChanged = manifestChanged || candidateExtension.source.integrity !== integrity;
573
+ if (candidateExtension.source.provider === 'npm') {
574
+ manifestChanged = manifestChanged || candidateExtension.source.version !== npmVersion;
575
+ candidateExtension.source.version = npmVersion;
576
+ } else {
577
+ manifestChanged = manifestChanged || candidateExtension.source.resolvedCommit !== commit;
578
+ candidateExtension.source.resolvedCommit = commit;
579
+ }
519
580
  candidateExtension.source.integrity = integrity;
520
581
  if (apiManifestDownload) {
521
582
  const apiManifestIntegrity = extensionApiManifestIntegrity(apiManifestDownload.contents);
@@ -553,11 +614,19 @@ async function updateCandidate({
553
614
  id: migrateToId ?? extension.id,
554
615
  previousId: migrateToId === undefined ? undefined : extension.id,
555
616
  })),
556
- extensions: downloads.map(({commit, extension}) => ({
557
- id: migrateToId ?? extension.id,
558
- previousId: migrateToId === undefined ? undefined : extension.id,
559
- resolvedCommit: commit,
560
- })),
617
+ extensions: downloads.map(({commit, extension, npmVersion}) =>
618
+ extension.source.provider === 'npm'
619
+ ? {
620
+ id: extension.id,
621
+ package: extension.source.package,
622
+ version: npmVersion,
623
+ }
624
+ : {
625
+ id: migrateToId ?? extension.id,
626
+ previousId: migrateToId === undefined ? undefined : extension.id,
627
+ resolvedCommit: commit,
628
+ },
629
+ ),
561
630
  migration:
562
631
  migration === undefined
563
632
  ? null
@@ -583,10 +652,8 @@ export async function extensionStatus(
583
652
  {fetch: fetchImplementation = globalThis.fetch} = {},
584
653
  ) {
585
654
  const source = await inspectExtensionSource(sourceDirectory);
586
- const fetchFunction = assertFetch(fetchImplementation);
587
655
  return Promise.all(
588
656
  managedExtensions(source).map(async (extension) => {
589
- const remoteCommit = await resolveGithubCommit(extension, fetchFunction);
590
657
  let local = 'valid';
591
658
  try {
592
659
  validateManagedExtensionContents(extension, source.extensionContents.get(extension.id));
@@ -599,6 +666,28 @@ export async function extensionStatus(
599
666
  } catch {
600
667
  local = 'modified';
601
668
  }
669
+ if (extension.source.provider === 'npm') {
670
+ const npmSource = await readNpmExtensionSource(extension, source.resolvedSourceDirectory, {
671
+ allowVersionMismatch: true,
672
+ maximumArtifactBytes: defaultExtensionArtifactSizeLimit,
673
+ maximumManifestBytes: defaultExtensionApiManifestSizeLimit,
674
+ });
675
+ if (npmSource.version === extension.source.version) {
676
+ validateManagedExtensionContents(extension, npmSource.contents);
677
+ if (npmSource.apiManifestContents) {
678
+ validateManagedExtensionApiManifest(extension, npmSource.apiManifestContents);
679
+ }
680
+ }
681
+ return {
682
+ id: extension.id,
683
+ installedVersion: npmSource.version,
684
+ local,
685
+ package: extension.source.package,
686
+ state: npmSource.version === extension.source.version ? 'current' : 'update-available',
687
+ version: extension.source.version,
688
+ };
689
+ }
690
+ const remoteCommit = await resolveGithubCommit(extension, assertFetch(fetchImplementation));
602
691
  return {
603
692
  id: extension.id,
604
693
  local,
@@ -623,7 +712,7 @@ export async function syncExtensions({
623
712
  allowBreakingApi: false,
624
713
  apiManifestArtifact: undefined,
625
714
  confirmReplace,
626
- fetchImplementation: assertFetch(fetchImplementation),
715
+ fetchImplementation,
627
716
  maximumArtifactBytes,
628
717
  maximumManifestBytes,
629
718
  migrateToId: undefined,
@@ -652,7 +741,7 @@ export async function updateExtensions({
652
741
  allowBreakingApi,
653
742
  apiManifestArtifact,
654
743
  confirmReplace,
655
- fetchImplementation: assertFetch(fetchImplementation),
744
+ fetchImplementation,
656
745
  maximumArtifactBytes,
657
746
  maximumManifestBytes,
658
747
  migrateToId,
package/src/index.js CHANGED
@@ -57,3 +57,8 @@ export {
57
57
  sourceFormatVersion,
58
58
  validateSb3Source,
59
59
  } from './source.js';
60
+ export {
61
+ applyProjectAssetAdditions,
62
+ projectAssetAdditionsFormatVersion,
63
+ } from './project-asset-additions.js';
64
+ export {cleanUpTurboWarpBlocks, turboWarpCleanUpLayout} from './turbowarp-clean-up.js';
@@ -0,0 +1,111 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+
3
+ import {lstat, readFile, realpath} from 'node:fs/promises';
4
+ import path from 'node:path';
5
+
6
+ import {validateExtensionApiManifestSourceMetadata} from './extension-api-manifest.js';
7
+ import {extensionHeaderId, validateExtensionSourceMetadata} from './extension-dependencies.js';
8
+
9
+ function assert(condition, message) {
10
+ if (!condition) throw new Error(message);
11
+ }
12
+
13
+ function packageSegments(packageName) {
14
+ return packageName.startsWith('@') ? packageName.split('/') : [packageName];
15
+ }
16
+
17
+ async function findPackageDirectory(sourceDirectory, packageName) {
18
+ let current = path.resolve(sourceDirectory);
19
+ while (true) {
20
+ const candidate = path.join(current, 'node_modules', ...packageSegments(packageName));
21
+ try {
22
+ await lstat(path.join(candidate, 'package.json'));
23
+ return candidate;
24
+ } catch (error) {
25
+ if (error?.code !== 'ENOENT' && error?.code !== 'ENOTDIR') throw error;
26
+ }
27
+ const parent = path.dirname(current);
28
+ if (parent === current) break;
29
+ current = parent;
30
+ }
31
+ throw new Error(
32
+ `Installed npm package was not found for managed extension: ${packageName}. Run the project's package manager install command.`,
33
+ );
34
+ }
35
+
36
+ async function readPackageArtifact(packageDirectory, artifact, maximumBytes, description) {
37
+ const [resolvedPackageDirectory, resolvedArtifact] = await Promise.all([
38
+ realpath(packageDirectory),
39
+ realpath(path.join(packageDirectory, ...artifact.split('/'))),
40
+ ]);
41
+ const relativeArtifact = path.relative(resolvedPackageDirectory, resolvedArtifact);
42
+ assert(
43
+ relativeArtifact !== '' &&
44
+ relativeArtifact !== '..' &&
45
+ !relativeArtifact.startsWith(`..${path.sep}`) &&
46
+ !path.isAbsolute(relativeArtifact),
47
+ `${description} resolves outside its npm package: ${artifact}`,
48
+ );
49
+ const stats = await lstat(resolvedArtifact);
50
+ assert(stats.isFile(), `${description} must be a regular file: ${artifact}`);
51
+ assert(stats.size <= maximumBytes, `${description} exceeds the ${maximumBytes}-byte limit.`);
52
+ return readFile(resolvedArtifact);
53
+ }
54
+
55
+ export async function readNpmExtensionSource(
56
+ extension,
57
+ sourceDirectory,
58
+ {allowVersionMismatch = false, maximumArtifactBytes, maximumManifestBytes},
59
+ ) {
60
+ const source = validateExtensionSourceMetadata(extension);
61
+ assert(source?.provider === 'npm', `Extension is not managed by npm: ${extension.id}`);
62
+ const packageDirectory = await findPackageDirectory(sourceDirectory, source.package);
63
+ let packageManifest;
64
+ try {
65
+ packageManifest = JSON.parse(
66
+ await readFile(path.join(packageDirectory, 'package.json'), 'utf8'),
67
+ );
68
+ } catch (error) {
69
+ throw new Error(`Invalid package.json for npm extension ${extension.id}: ${error.message}`, {
70
+ cause: error,
71
+ });
72
+ }
73
+ assert(
74
+ packageManifest?.name === source.package,
75
+ `npm package name mismatch for extension ${extension.id}: expected ${source.package}, got ${packageManifest?.name ?? '(missing)'}`,
76
+ );
77
+ assert(
78
+ typeof packageManifest.version === 'string' &&
79
+ /^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/u.test(
80
+ packageManifest.version,
81
+ ),
82
+ `npm package has an invalid semantic version for extension ${extension.id}: ${packageManifest?.version ?? '(missing)'}`,
83
+ );
84
+ if (!allowVersionMismatch) {
85
+ assert(
86
+ packageManifest.version === source.version,
87
+ `npm package version mismatch for extension ${extension.id}: expected ${source.version}, got ${packageManifest?.version ?? '(missing)'}`,
88
+ );
89
+ }
90
+ const contents = await readPackageArtifact(
91
+ packageDirectory,
92
+ source.artifact,
93
+ maximumArtifactBytes,
94
+ `npm extension artifact for ${extension.id}`,
95
+ );
96
+ const actualId = extensionHeaderId(contents);
97
+ assert(
98
+ actualId === extension.id,
99
+ `npm extension header ID mismatch for ${extension.id}: expected ${extension.id}, got ${actualId ?? '(missing)'}`,
100
+ );
101
+ const apiMetadata = validateExtensionApiManifestSourceMetadata(extension);
102
+ const apiManifestContents = apiMetadata
103
+ ? await readPackageArtifact(
104
+ packageDirectory,
105
+ apiMetadata.artifact,
106
+ maximumManifestBytes,
107
+ `npm extension API manifest for ${extension.id}`,
108
+ )
109
+ : null;
110
+ return {apiManifestContents, contents, packageDirectory, version: packageManifest.version};
111
+ }