@arcadia-eternity/content-sdk 4.9.0 → 4.10.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.
@@ -493,6 +493,7 @@ export interface EnvironmentExport {
493
493
  }
494
494
  export interface WorkspaceSnapshot {
495
495
  format: 'arcadia.workspace-snapshot/2';
496
+ builtinEnvironmentAliases?: Readonly<Record<string, string>>;
496
497
  builtinEnvironmentIds?: Readonly<Record<string, string>>;
497
498
  workspaceId: string;
498
499
  drafts: (EnvironmentDraft & {
@@ -522,6 +523,7 @@ export interface ContentWorkspace {
522
523
  readonly root: string;
523
524
  readonly workspaceId: string;
524
525
  readonly bootId: string;
526
+ readonly builtinEnvironmentAliases?: Readonly<Record<string, string>>;
525
527
  readonly builtinEnvironmentIds?: Readonly<Record<string, string>>;
526
528
  listDrafts(): Promise<EnvironmentDraft[]>;
527
529
  getDraft(environmentId: string): Promise<EnvironmentDraft>;
@@ -12,11 +12,18 @@ import { withSourceLock } from './source-lock.js';
12
12
  import { contentError, readJson, materialFiles, writeMaterials } from './workspace-materials.js';
13
13
  import { errorCode, isRecord } from './types.js';
14
14
  import { compactMaterials, expandSharedMaterials, exportDependencies, } from './environment-sharing.js';
15
- import { environmentShareMaterials } from './environment-host-base.js';
15
+ import { environmentShareMaterials, hostBaseName } from './environment-host-base.js';
16
16
  export { copyEnvironmentMember } from './environment-member-transfer.js';
17
17
  import { parseManifestDraft, preparedDocument } from './prepared-document.js';
18
18
  export { readInstalledEnvironmentDocument } from './prepared-document.js';
19
19
  export { normalizeEnvironmentDocument } from './debug-environments.js';
20
+ function hasHostBase(document) {
21
+ return Object.hasOwn(document.packages, hostBaseName) || document.sources.includes(hostBaseName);
22
+ }
23
+ function assertBaseRetained(before, after) {
24
+ if (before && hasHostBase(before) && !hasHostBase(after))
25
+ throw contentError('ENVIRONMENT_BASE_REQUIRED', '环境必须保留官方基础包。');
26
+ }
20
27
  const compositionSchema = z
21
28
  .object({
22
29
  rules: EnvironmentRulesSchema,
@@ -69,6 +76,13 @@ function readBuiltinEnvironmentIds(value) {
69
76
  ? { 'training-120': checkId(value.builtin120EnvironmentId, new RegExp('^builtin-120-' + uuid.source.slice(1))) }
70
77
  : {};
71
78
  }
79
+ function readBuiltinEnvironmentAliases(value) {
80
+ if (!('builtinEnvironmentAliases' in value))
81
+ return {};
82
+ if (!isRecord(value.builtinEnvironmentAliases))
83
+ throw contentError('WORKSPACE_FORMAT_INVALID');
84
+ return Object.fromEntries(Object.entries(value.builtinEnvironmentAliases).map(([oldId, id]) => [checkId(oldId, envId), checkId(id, envId)]));
85
+ }
72
86
  async function regularDirectory(path) {
73
87
  await mkdir(path, { recursive: true, mode: 0o700 });
74
88
  const info = await lstat(path);
@@ -131,6 +145,7 @@ class ContentWorkspaceImplementation {
131
145
  root;
132
146
  bootId = randomUUID();
133
147
  workspaceId;
148
+ builtinEnvironmentAliases = {};
134
149
  builtinEnvironmentIds = {};
135
150
  #queue = Promise.resolve();
136
151
  #closed = false;
@@ -175,6 +190,7 @@ class ContentWorkspaceImplementation {
175
190
  else {
176
191
  workspace.workspaceId = randomUUID();
177
192
  }
193
+ workspace.builtinEnvironmentAliases = readBuiltinEnvironmentAliases(metadata ?? {});
178
194
  workspace.builtinEnvironmentIds = readBuiltinEnvironmentIds(metadata ?? {});
179
195
  if (!metadata || 'builtin120EnvironmentId' in metadata)
180
196
  await workspace.#saveMetadata();
@@ -203,26 +219,55 @@ class ContentWorkspaceImplementation {
203
219
  }
204
220
  registerBuiltinEnvironments(keys, adopted) {
205
221
  return this.#exclusive(async () => {
206
- const entries = keys.map(key => {
207
- checkId(key, envId);
208
- const previous = Object.hasOwn(this.builtinEnvironmentIds, key) ? this.builtinEnvironmentIds[key] : undefined;
209
- const legacy = Object.hasOwn(adopted, key) ? checkId(adopted[key], envId) : undefined;
210
- const id = previous ?? legacy ?? 'builtin-' + randomUUID();
211
- return [key, id];
212
- });
213
- const ids = readBuiltinEnvironmentIds({ builtinEnvironmentIds: Object.fromEntries(entries) });
214
- if (!isDeepStrictEqual(ids, this.builtinEnvironmentIds)) {
215
- await this.#saveMetadata(ids);
222
+ const ids = readBuiltinEnvironmentIds({ builtinEnvironmentIds: Object.fromEntries(keys.map(key => [key, key])) });
223
+ const aliases = { ...this.builtinEnvironmentAliases };
224
+ const retiring = new Map();
225
+ for (const key of keys) {
226
+ const previous = Object.hasOwn(this.builtinEnvironmentIds, key)
227
+ ? this.builtinEnvironmentIds[key]
228
+ : Object.hasOwn(adopted, key)
229
+ ? adopted[key]
230
+ : undefined;
231
+ if (previous === key)
232
+ continue;
233
+ // An unregistered custom draft may already own the requested ID.
234
+ if (await lstat(this.#directory(key)).catch(error => {
235
+ if (errorCode(error) !== 'ENOENT')
236
+ throw error;
237
+ return undefined;
238
+ }))
239
+ throw contentError('BUILTIN_ENVIRONMENT_ID_CONFLICT', key);
240
+ if (previous !== undefined) {
241
+ checkId(previous, envId);
242
+ if (Object.hasOwn(ids, previous))
243
+ throw contentError('BUILTIN_ENVIRONMENT_ID_CONFLICT', previous);
244
+ retiring.set(previous, key);
245
+ }
246
+ }
247
+ for (const [oldId, id] of retiring) {
248
+ // Preserve the previous installation intact. A restart can repeat this step before the metadata commits.
249
+ const archive = join(this.root, 'migration-backups');
250
+ await regularDirectory(archive);
251
+ await rename(this.#directory(oldId), join(archive, oldId)).catch(error => {
252
+ if (errorCode(error) !== 'ENOENT')
253
+ throw error;
254
+ });
255
+ aliases[oldId] = id;
256
+ }
257
+ if (!isDeepStrictEqual(ids, this.builtinEnvironmentIds) || retiring.size) {
258
+ await this.#saveMetadata(ids, aliases);
216
259
  this.builtinEnvironmentIds = ids;
260
+ this.builtinEnvironmentAliases = aliases;
217
261
  }
218
262
  return ids;
219
263
  }, true);
220
264
  }
221
- async #saveMetadata(ids = this.builtinEnvironmentIds) {
265
+ async #saveMetadata(ids = this.builtinEnvironmentIds, aliases = this.builtinEnvironmentAliases) {
222
266
  await writeFileAtomic(join(this.root, 'workspace.json'), json({
223
267
  format: 'arcadia.content-workspace/2',
224
268
  workspaceId: this.workspaceId,
225
269
  builtinEnvironmentIds: ids,
270
+ ...(Object.keys(aliases).length && { builtinEnvironmentAliases: aliases }),
226
271
  }));
227
272
  }
228
273
  async #assertOwner() {
@@ -312,6 +357,8 @@ class ContentWorkspaceImplementation {
312
357
  });
313
358
  if ((previous === null ? null : sha256Hex(previous)) !== input.expectedRevision)
314
359
  throw contentError('REVISION_CONFLICT');
360
+ if (previous !== null)
361
+ assertBaseRetained(parseManifestDraft(previous).document, document);
315
362
  await regularDirectory(directory);
316
363
  await this.#archiveRemovedDeclarations(document.id);
317
364
  if (previous !== null) {
@@ -413,6 +460,7 @@ class ContentWorkspaceImplementation {
413
460
  const document = normalizeEnvironmentDocument(input.document ?? draft?.document);
414
461
  if (document.id !== input.environmentId)
415
462
  throw contentError('ENVIRONMENT_ID_MISMATCH');
463
+ assertBaseRetained(draft?.document, document);
416
464
  const source = input.projectDirectory ??
417
465
  input.directory ??
418
466
  input.seedDirectory ??
@@ -441,15 +489,14 @@ class ContentWorkspaceImplementation {
441
489
  }
442
490
  await writeMaterials(staging, materials, limits);
443
491
  const manifest = await readJson(join(staging, 'package.json'));
444
- const base = '@arcadia-eternity/data-pack-base';
445
- if (Object.hasOwn(document.packages, base) || document.sources.includes(base)) {
446
- const installed = await readJson(join(staging, 'node_modules', base, 'package.json'));
492
+ if (hasHostBase(document)) {
493
+ const installed = await readJson(join(staging, 'node_modules', hostBaseName, 'package.json'));
447
494
  if (typeof installed.version !== 'string')
448
495
  throw contentError('BUNDLED_PACKAGE_INVALID');
449
496
  document = {
450
497
  ...document,
451
- packages: { ...document.packages, [base]: installed.version },
452
- sources: document.sources.filter(name => name !== base),
498
+ packages: { ...document.packages, [hostBaseName]: installed.version },
499
+ sources: document.sources.filter(name => name !== hostBaseName),
453
500
  };
454
501
  }
455
502
  await writeFile(join(staging, 'package.json'), json({ ...this.#manifest(document, manifest), dependencies: manifest.dependencies ?? {} }));
@@ -664,6 +711,9 @@ class ContentWorkspaceImplementation {
664
711
  format: 'arcadia.workspace-snapshot/2',
665
712
  workspaceId: this.workspaceId,
666
713
  builtinEnvironmentIds: this.builtinEnvironmentIds,
714
+ ...(Object.keys(this.builtinEnvironmentAliases).length && {
715
+ builtinEnvironmentAliases: this.builtinEnvironmentAliases,
716
+ }),
667
717
  drafts,
668
718
  installations,
669
719
  };
@@ -699,6 +749,7 @@ class ContentWorkspaceImplementation {
699
749
  !Array.isArray(snapshot.installations))
700
750
  throw contentError('WORKSPACE_SNAPSHOT_INVALID');
701
751
  const builtinEnvironmentIds = readBuiltinEnvironmentIds(snapshot);
752
+ const builtinEnvironmentAliases = readBuiltinEnvironmentAliases(snapshot);
702
753
  if ((await this.#listDrafts()).length)
703
754
  throw contentError('WORKSPACE_NOT_EMPTY');
704
755
  const drafts = new Map(snapshot.drafts.map(draft => {
@@ -756,6 +807,7 @@ class ContentWorkspaceImplementation {
756
807
  throw contentError('WORKSPACE_SNAPSHOT_INVALID');
757
808
  }
758
809
  this.workspaceId = mode === 'restore' ? checkId(snapshot.workspaceId, uuid) : this.workspaceId;
810
+ this.builtinEnvironmentAliases = builtinEnvironmentAliases;
759
811
  this.builtinEnvironmentIds = builtinEnvironmentIds;
760
812
  await this.#saveMetadata();
761
813
  return { workspaceId: this.workspaceId, installationIds: Object.fromEntries([...ids].map(id => [id, id])) };
@@ -776,6 +828,7 @@ class ContentWorkspaceImplementation {
776
828
  });
777
829
  if ((previous?.revision ?? null) !== (input.expectedRevision ?? null))
778
830
  throw contentError('REVISION_CONFLICT');
831
+ assertBaseRetained(previous?.document, document);
779
832
  const staging = join(this.root, 'staging', `import-${randomUUID()}`);
780
833
  await mkdir(staging);
781
834
  try {
@@ -990,6 +1043,8 @@ async function changeEnvironmentPackage(workspace, input) {
990
1043
  changed = true;
991
1044
  }
992
1045
  }
1046
+ if (affected.has(hostBaseName))
1047
+ throw contentError('ENVIRONMENT_BASE_REQUIRED', '环境必须保留官方基础包。');
993
1048
  const roots = [...active].filter(key => affected.has(key));
994
1049
  if (!roots.length)
995
1050
  throw contentError('ENVIRONMENT_PACKAGE_NOT_FOUND');
@@ -1024,11 +1079,10 @@ export async function readShippedEnvironments(file, bundledVersions) {
1024
1079
  return catalog;
1025
1080
  }
1026
1081
  export async function synchronizeShippedEnvironments(workspace, catalog, adopted, prepare) {
1027
- const ids = await workspace.registerBuiltinEnvironments(catalog.environments.map(doc => doc.id), adopted);
1082
+ await workspace.registerBuiltinEnvironments(catalog.environments.map(doc => doc.id), adopted);
1028
1083
  const drafts = await workspace.listDrafts();
1029
- for (const definition of catalog.environments) {
1030
- const id = ids[definition.id];
1031
- const document = { ...definition, id };
1084
+ for (const document of catalog.environments) {
1085
+ const id = document.id;
1032
1086
  const current = drafts.find(item => item.document.id === id);
1033
1087
  if (current && isDeepStrictEqual(current.document, document)) {
1034
1088
  const [installed] = await workspace.listInstallations(id);
@@ -1037,5 +1091,5 @@ export async function synchronizeShippedEnvironments(workspace, catalog, adopted
1037
1091
  }
1038
1092
  await prepare(document, current?.revision ?? null);
1039
1093
  }
1040
- return ids[catalog.defaultEnvironment];
1094
+ return catalog.defaultEnvironment;
1041
1095
  }
@@ -1,4 +1,5 @@
1
1
  import type { EnvironmentDocumentV2 } from './debug-environments.js';
2
+ export declare const hostBaseName = "@arcadia-eternity/data-pack-base";
2
3
  type Material = {
3
4
  path: string;
4
5
  bytes: Buffer;
@@ -3,7 +3,7 @@ import { createHash } from 'node:crypto';
3
3
  import { readFile } from 'node:fs/promises';
4
4
  import { withShippedEnvironmentProject, withShippedCatalogArchive } from '@arcadia-eternity/pack-loader/node';
5
5
  import { contentError, materialFiles } from './workspace-materials.js';
6
- const hostBaseName = '@arcadia-eternity/data-pack-base';
6
+ export const hostBaseName = '@arcadia-eternity/data-pack-base';
7
7
  const baseLocation = 'node_modules/' + hostBaseName;
8
8
  export function isBaseMaterial(path) {
9
9
  return ('/' + path.toLowerCase() + '/').includes('/' + baseLocation + '/');
@@ -21,7 +21,7 @@
21
21
  "devDependencies": {
22
22
  "@types/node": "26.6.6",
23
23
  "typescript": "6.0.3",
24
- "@arcadia-eternity/content-sdk": "4.9.0",
24
+ "@arcadia-eternity/content-sdk": "4.10.0",
25
25
  "fflate": "0.8.3"
26
26
  },
27
27
  "arcadia": {
@@ -38,6 +38,6 @@
38
38
  "license": "MIT",
39
39
  "engines": {
40
40
  "node": ">=24 <25",
41
- "arcadia": "^4.9.0"
41
+ "arcadia": "^4.10.0"
42
42
  }
43
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcadia-eternity/content-sdk",
3
- "version": "4.9.0",
3
+ "version": "4.10.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "engines": {
@@ -63,10 +63,10 @@
63
63
  "zod": "4.6.7",
64
64
  "fflate": "0.8.3",
65
65
  "simple-git": "4.1.0",
66
- "@arcadia-eternity/battle": "4.9.0",
67
- "@arcadia-eternity/schema": "4.9.0",
68
- "@arcadia-eternity/const": "4.9.0",
69
- "@arcadia-eternity/pack-loader": "4.9.0"
66
+ "@arcadia-eternity/battle": "4.10.0",
67
+ "@arcadia-eternity/const": "4.10.0",
68
+ "@arcadia-eternity/pack-loader": "4.10.0",
69
+ "@arcadia-eternity/schema": "4.10.0"
70
70
  },
71
71
  "repository": {
72
72
  "type": "git",