@arcadia-eternity/content-sdk 4.12.0 → 4.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.
@@ -1,5 +1,5 @@
1
1
  import { type PackageUpdate } from './package-updates.js';
2
- export { officialPackage, findPackageUpdate, selectPackageUpdate, selectServerBase, installServerBaseUpdate, } from './package-updates.js';
2
+ export { officialPackage, findPackageUpdate, selectPackageUpdate, startAutomaticContentUpdates, selectServerBase, installServerBaseUpdate, } from './package-updates.js';
3
3
  import type { ContentWorkspace, EnvironmentInstallation, PrepareEnvironmentInput, StaticPreparer } from './content-workspace.js';
4
4
  import type { ManagedSource } from './managed-sources.js';
5
5
  import { type UnknownRecord } from './types.js';
@@ -39,7 +39,7 @@ export declare class ManagedProjects {
39
39
  document?: PrepareEnvironmentInput['document'];
40
40
  signal?: AbortSignal;
41
41
  beforeCommit?: () => Promise<void>;
42
- }, validate: StaticPreparer): Promise<EnvironmentInstallation>;
42
+ }, validate: StaticPreparer, officialBaseRoot?: string): Promise<EnvironmentInstallation>;
43
43
  /** Refresh only shipped bytes, without resolving or downloading third-party dependencies. */
44
44
  synchronizeBundledBase(workspace: ContentWorkspace, validate: StaticPreparer): Promise<void>;
45
45
  synchronizeProject<T>(project: string, consume: (directory: string, version: string) => Promise<T>): Promise<T | undefined>;
@@ -3,7 +3,7 @@ import { execFile } from 'node:child_process';
3
3
  import { mkdir, mkdtemp, readFile, readdir, realpath, rename, rm, writeFile } from 'node:fs/promises';
4
4
  import { join, resolve } from 'node:path';
5
5
  import { withOfficialUpdateDirectory } from './package-updates.js';
6
- export { officialPackage, findPackageUpdate, selectPackageUpdate, selectServerBase, installServerBaseUpdate, } from './package-updates.js';
6
+ export { officialPackage, findPackageUpdate, selectPackageUpdate, startAutomaticContentUpdates, selectServerBase, installServerBaseUpdate, } from './package-updates.js';
7
7
  import { isDeepStrictEqual, promisify } from 'node:util';
8
8
  import { satisfies, valid, validRange } from 'semver';
9
9
  import { inspectNpmProject, resolveNpmInvocation, withShippedEnvironmentProject, withShippedCatalogArchive, } from '@arcadia-eternity/pack-loader/node';
@@ -103,6 +103,9 @@ export class ManagedProjects {
103
103
  throw contentError('INSTALL_SOURCES_INVALID');
104
104
  this.#options = {
105
105
  ...options,
106
+ get bundledPackageRoot() {
107
+ return options.bundledPackageRoot;
108
+ },
106
109
  registry: registryUrl(options.registry),
107
110
  sources: structuredClone(options.sources ?? {}),
108
111
  timeoutMs: limit(options.timeoutMs, DEFAULT_TIMEOUT, 3600_000),
@@ -224,7 +227,7 @@ export class ManagedProjects {
224
227
  await rm(staging, { recursive: true, force: true });
225
228
  }
226
229
  }
227
- prepareEnvironment(workspace, input, validate) {
230
+ prepareEnvironment(workspace, input, validate, officialBaseRoot) {
228
231
  if (typeof validate !== 'function')
229
232
  return Promise.reject(contentError('STATIC_PREPARER_REQUIRED'));
230
233
  return this.#track(this.#prepare(async () => {
@@ -238,6 +241,7 @@ export class ManagedProjects {
238
241
  return input.document ?? draft?.document;
239
242
  }, {
240
243
  ...input,
244
+ officialBaseRoot,
241
245
  projectDirectory: input.seedDirectory ?? join(workspace.root, 'environments', input.environmentId),
242
246
  }, async (directory, signal, document) => {
243
247
  return workspace.prepareEnvironment({ ...input, document, projectDirectory: directory, signal }, validate);
@@ -364,7 +368,7 @@ export class ManagedProjects {
364
368
  const includesBase = Object.hasOwn(dependencies, baseName) || document.sources.includes(baseName);
365
369
  if (Object.hasOwn(dependencies, baseName) && document.sources.includes(baseName))
366
370
  throw contentError('INSTALL_DUPLICATE_PACKAGE');
367
- let baseRoot = this.#options.bundledPackageRoot;
371
+ let baseRoot = input.officialBaseRoot ?? this.#options.bundledPackageRoot;
368
372
  if (includesBase && !baseRoot)
369
373
  throw contentError('BUNDLED_PACKAGE_UNAVAILABLE');
370
374
  if (includesBase) {
@@ -6,8 +6,11 @@ export interface PackageUpdate {
6
6
  url: string;
7
7
  integrity: string;
8
8
  }
9
+ export declare function startAutomaticContentUpdates(update: (options: {
10
+ signal: AbortSignal;
11
+ }) => Promise<unknown>, onError: (error: unknown) => void): () => Promise<void> | undefined;
9
12
  export declare function selectPackageUpdate(metadata: unknown, name: string, current: string, client: string): PackageUpdate | null;
10
- export declare function findPackageUpdate(name: string, current: string, client: string): Promise<PackageUpdate | null>;
13
+ export declare function findPackageUpdate(name: string, current: string, client: string, signal?: AbortSignal): Promise<PackageUpdate | null>;
11
14
  export declare function withOfficialUpdateDirectory<T>(candidate: PackageUpdate, signal: AbortSignal, use: (directory: string) => Promise<T>): Promise<T>;
12
15
  /** Only completed version directories are eligible. Offline startup never contacts the registry. */
13
16
  export declare function selectServerBase(store: string, shipped: string, host: string, pin?: string, validate?: (directory: string) => Promise<void>): Promise<{
@@ -15,4 +18,7 @@ export declare function selectServerBase(store: string, shipped: string, host: s
15
18
  version: string;
16
19
  cached: boolean;
17
20
  }>;
18
- export declare function installServerBaseUpdate(store: string, candidate: PackageUpdate, host: string, validate: (directory: string) => Promise<void>): Promise<void>;
21
+ export declare function installServerBaseUpdate(store: string, candidate: PackageUpdate, host: string, validate: (directory: string, signal: AbortSignal) => Promise<void>, options?: {
22
+ signal?: AbortSignal;
23
+ beforeCommit?: (directory: string) => Promise<void>;
24
+ }): Promise<void>;
@@ -6,6 +6,37 @@ import { tmpdir } from 'node:os';
6
6
  import { acquireSource } from './managed-sources.js';
7
7
  import { errorCode, isRecord } from './types.js';
8
8
  export const officialPackage = '@arcadia-eternity/data-pack-base';
9
+ export function startAutomaticContentUpdates(update, onError) {
10
+ const controller = new AbortController();
11
+ let running;
12
+ let timer;
13
+ function schedule(delay) {
14
+ timer = setTimeout(() => {
15
+ running = check();
16
+ }, delay);
17
+ timer.unref();
18
+ }
19
+ async function check() {
20
+ let delay = 6 * 60 * 60 * 1000;
21
+ try {
22
+ await update({ signal: controller.signal });
23
+ }
24
+ catch (error) {
25
+ if (controller.signal.aborted)
26
+ return;
27
+ onError(error);
28
+ delay = 15 * 60 * 1000;
29
+ }
30
+ if (!controller.signal.aborted)
31
+ schedule(delay);
32
+ }
33
+ schedule(3000);
34
+ return () => {
35
+ controller.abort();
36
+ clearTimeout(timer);
37
+ return running;
38
+ };
39
+ }
9
40
  export function selectPackageUpdate(metadata, name, current, client) {
10
41
  if (!isRecord(metadata) ||
11
42
  metadata.name !== name ||
@@ -43,11 +74,12 @@ export function selectPackageUpdate(metadata, name, current, client) {
43
74
  }
44
75
  return null;
45
76
  }
46
- export async function findPackageUpdate(name, current, client) {
77
+ export async function findPackageUpdate(name, current, client, signal) {
78
+ const timeout = AbortSignal.timeout(20000);
47
79
  const response = await fetch('https://registry.npmjs.org/' + encodeURIComponent(name), {
48
80
  headers: { Accept: 'application/vnd.npm.install-v1+json' },
49
81
  redirect: 'error',
50
- signal: AbortSignal.timeout(20000),
82
+ signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
51
83
  });
52
84
  if (!response.ok)
53
85
  throw new Error('无法检查 npm 包更新(HTTP ' + response.status + ')');
@@ -158,16 +190,21 @@ export async function selectServerBase(store, shipped, host, pin, validate) {
158
190
  cause: lastError,
159
191
  });
160
192
  }
161
- export async function installServerBaseUpdate(store, candidate, host, validate) {
193
+ export async function installServerBaseUpdate(store, candidate, host, validate, options = {}) {
162
194
  if (!semver.satisfies(host, candidate.range))
163
195
  throw new Error('INCOMPATIBLE_OFFICIAL_PACKAGE');
164
196
  await mkdir(store, { recursive: true, mode: 0o700 });
165
197
  const stage = await mkdtemp(join(store, '.install-'));
166
198
  try {
167
- const directory = await acquireOfficial(candidate, join(stage, 'source'), AbortSignal.timeout(120000));
168
- await validate(directory);
199
+ const timeout = AbortSignal.timeout(120000);
200
+ const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
201
+ const directory = await acquireOfficial(candidate, join(stage, 'source'), signal);
202
+ await validate(directory, signal);
169
203
  const digest = await catalogMaterialDigest(directory);
170
204
  await writeFile(join(directory, '.arcadia-material-digest'), digest);
205
+ signal.throwIfAborted();
206
+ await options.beforeCommit?.(directory);
207
+ options.signal?.throwIfAborted();
171
208
  try {
172
209
  await rename(directory, join(store, candidate.version));
173
210
  }
@@ -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.12.0",
24
+ "@arcadia-eternity/content-sdk": "4.13.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.12.0"
41
+ "arcadia": "^4.13.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.12.0",
3
+ "version": "4.13.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.12.0",
67
- "@arcadia-eternity/const": "4.12.0",
68
- "@arcadia-eternity/pack-loader": "4.12.0",
69
- "@arcadia-eternity/schema": "4.12.0"
66
+ "@arcadia-eternity/pack-loader": "4.13.0",
67
+ "@arcadia-eternity/schema": "4.13.0",
68
+ "@arcadia-eternity/const": "4.13.0",
69
+ "@arcadia-eternity/battle": "4.13.0"
70
70
  },
71
71
  "repository": {
72
72
  "type": "git",