@nextclaw/app-runtime 0.10.0 → 0.12.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.
Files changed (56) hide show
  1. package/README.md +19 -2
  2. package/dist/index.d.ts +7 -2
  3. package/dist/index.js +6 -1
  4. package/dist/package.js +1 -1
  5. package/dist/services/app-home.service.d.ts +4 -0
  6. package/dist/services/app-home.service.d.ts.map +1 -1
  7. package/dist/services/app-home.service.js +15 -1
  8. package/dist/services/app-home.service.js.map +1 -1
  9. package/dist/services/app-install-source.service.js +14 -1
  10. package/dist/services/app-install-source.service.js.map +1 -1
  11. package/dist/services/app-installation-filesystem.service.js +68 -0
  12. package/dist/services/app-installation-filesystem.service.js.map +1 -0
  13. package/dist/services/app-installation-integrity.service.d.ts +20 -0
  14. package/dist/services/app-installation-integrity.service.d.ts.map +1 -0
  15. package/dist/services/app-installation-integrity.service.js +84 -0
  16. package/dist/services/app-installation-integrity.service.js.map +1 -0
  17. package/dist/services/app-installation-lifecycle.service.js +110 -0
  18. package/dist/services/app-installation-lifecycle.service.js.map +1 -0
  19. package/dist/services/app-installation.service.d.ts +21 -11
  20. package/dist/services/app-installation.service.d.ts.map +1 -1
  21. package/dist/services/app-installation.service.js +262 -280
  22. package/dist/services/app-installation.service.js.map +1 -1
  23. package/dist/services/app-instance-inventory.service.d.ts +31 -0
  24. package/dist/services/app-instance-inventory.service.d.ts.map +1 -0
  25. package/dist/services/app-instance-inventory.service.js +136 -0
  26. package/dist/services/app-instance-inventory.service.js.map +1 -0
  27. package/dist/services/app-instance-storage.service.d.ts +55 -0
  28. package/dist/services/app-instance-storage.service.d.ts.map +1 -0
  29. package/dist/services/app-instance-storage.service.js +269 -0
  30. package/dist/services/app-instance-storage.service.js.map +1 -0
  31. package/dist/services/app-manifest.service.d.ts +5 -1
  32. package/dist/services/app-manifest.service.d.ts.map +1 -1
  33. package/dist/services/app-manifest.service.js +59 -3
  34. package/dist/services/app-manifest.service.js.map +1 -1
  35. package/dist/services/app-publish.service.js +1 -1
  36. package/dist/services/app-publish.service.js.map +1 -1
  37. package/dist/services/app-registry.service.d.ts +13 -3
  38. package/dist/services/app-registry.service.d.ts.map +1 -1
  39. package/dist/services/app-registry.service.js +143 -13
  40. package/dist/services/app-registry.service.js.map +1 -1
  41. package/dist/services/file-lock.service.d.ts +20 -0
  42. package/dist/services/file-lock.service.d.ts.map +1 -0
  43. package/dist/services/file-lock.service.js +156 -0
  44. package/dist/services/file-lock.service.js.map +1 -0
  45. package/dist/types/app-installation.types.d.ts +7 -0
  46. package/dist/types/app-installation.types.d.ts.map +1 -1
  47. package/dist/types/app-manifest.types.d.ts +22 -1
  48. package/dist/types/app-manifest.types.d.ts.map +1 -1
  49. package/dist/types/app-manifest.types.js.map +1 -1
  50. package/dist/types/app-registry.types.d.ts +7 -1
  51. package/dist/types/app-registry.types.d.ts.map +1 -1
  52. package/dist/types/app-storage.types.d.ts +64 -0
  53. package/dist/types/app-storage.types.d.ts.map +1 -0
  54. package/dist/types/app-storage.types.js +7 -0
  55. package/dist/types/app-storage.types.js.map +1 -0
  56. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"app-installation.service.js","names":[],"sources":["../../src/services/app-installation.service.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { access, cp, mkdir, readdir, rename, rm } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { AppBundleService } from \"#app-runtime/services/app-bundle.service.js\";\nimport { AppManifestService } from \"#app-runtime/services/app-manifest.service.js\";\nimport {\n isAppComponentManifestBundle,\n isAppStandaloneManifestBundle,\n} from \"#app-runtime/types/app-manifest.types.js\";\nimport { AppHomeService } from \"#app-runtime/services/app-home.service.js\";\nimport { AppBuildService } from \"#app-runtime/services/app-build.service.js\";\nimport type { AppDistributionMode } from \"#app-runtime/types/app-bundle.types.js\";\nimport type { AppDocumentGrantMap } from \"#app-runtime/types/app-permissions.types.js\";\nimport { AppRegistryConfigService } from \"#app-runtime/services/app-registry-config.service.js\";\nimport { AppRemoteRegistryClientService } from \"#app-runtime/services/app-remote-registry-client.service.js\";\nimport { AppRegistryService } from \"#app-runtime/services/app-registry.service.js\";\nimport { AppInstallSourceService } from \"#app-runtime/services/app-install-source.service.js\";\nimport type {\n AppInfoResult,\n AppInstallProgressHandler,\n AppActivationResult,\n AppInstallResult,\n AppLaunchResolution,\n AppUpdateResult,\n AppRollbackResult,\n AppUninstallResult,\n InstalledAppListItem,\n} from \"#app-runtime/types/app-installation.types.js\";\n\nexport class AppInstallationService {\n private readonly installSourceService: AppInstallSourceService;\n\n constructor(\n private readonly appHomeService: AppHomeService = new AppHomeService(),\n private readonly bundleService: AppBundleService = new AppBundleService(),\n private readonly manifestService: AppManifestService = new AppManifestService(),\n private readonly buildService: AppBuildService = new AppBuildService(),\n private readonly registryService: AppRegistryService = new AppRegistryService(appHomeService),\n private readonly registryConfigService: AppRegistryConfigService = new AppRegistryConfigService(\n appHomeService,\n ),\n private readonly remoteRegistryClient: AppRemoteRegistryClientService = new AppRemoteRegistryClientService(\n new AppRegistryConfigService(appHomeService),\n ),\n ) {\n this.installSourceService = new AppInstallSourceService(\n this.manifestService,\n this.remoteRegistryClient,\n );\n }\n\n install = async (\n appSource: string,\n options?: {\n registryUrl?: string;\n onProgress?: AppInstallProgressHandler;\n },\n ): Promise<AppInstallResult> => {\n const { onProgress, registryUrl } = options ?? {};\n await onProgress?.(\"resolving\");\n const source = await this.installSourceService.resolve(appSource, registryUrl);\n const tempDirectory = await this.appHomeService.createTemporaryDirectory(\"napp-install-\");\n try {\n if (source.kind === \"registry\") {\n await onProgress?.(\"downloading\");\n }\n const bundlePath =\n source.kind === \"directory\"\n ? (await this.bundleService.packAppDirectory({\n appDirectory: source.appDirectory,\n outputPath: path.join(tempDirectory, \"app.napp\"),\n })).bundlePath\n : source.kind === \"bundle\"\n ? source.bundlePath\n : (\n await this.remoteRegistryClient.downloadBundle({\n resolution: source.registryResolution,\n targetDirectory: tempDirectory,\n })\n ).bundlePath;\n await onProgress?.(\"verifying\");\n const extractedDirectory = path.join(tempDirectory, \"bundle\");\n const extractedMetadata = await this.bundleService.extractBundle({\n bundlePath,\n targetDirectory: extractedDirectory,\n });\n await this.materializeDistribution({\n appDirectory: extractedDirectory,\n distributionMode: extractedMetadata.metadata.distributionMode,\n });\n const manifestBundle = await this.manifestService.load(extractedDirectory);\n const installDirectory = this.appHomeService.getInstallDirectory(\n manifestBundle.manifest.id,\n manifestBundle.manifest.version,\n );\n if (\n source.kind === \"registry\" &&\n manifestBundle.manifest.id !== source.registryResolution.appId\n ) {\n throw new Error(\n `bundle manifest.appId 与 registry 请求不一致:期望 ${source.registryResolution.appId},实际 ${manifestBundle.manifest.id}`,\n );\n }\n await onProgress?.(\"installing\");\n const dataDirectory = this.appHomeService.getAppDataDirectory(manifestBundle.manifest.id);\n await this.copyToImmutableInstallDirectory({\n appId: manifestBundle.manifest.id,\n appVersion: manifestBundle.manifest.version,\n extractedDirectory,\n installDirectory,\n });\n await mkdir(dataDirectory, { recursive: true });\n await onProgress?.(\"finalizing\");\n let registryRecord;\n try {\n registryRecord = await this.registryService.upsertInstallation({\n appId: manifestBundle.manifest.id,\n name: manifestBundle.manifest.name,\n description: manifestBundle.manifest.description,\n version: manifestBundle.manifest.version,\n installDirectory,\n dataDirectory,\n sourceKind: source.kind,\n distributionMode:\n source.kind === \"registry\"\n ? source.registryResolution.distributionMode\n : extractedMetadata.metadata.distributionMode,\n sourceRef: source.sourceRef,\n installedAt: new Date().toISOString(),\n permissions: manifestBundle.manifest.schemaVersion === 1\n ? manifestBundle.manifest.permissions ?? {}\n : {},\n registryUrl:\n source.kind === \"registry\" ? source.registryResolution.registryUrl : undefined,\n bundleUrl:\n source.kind === \"registry\" ? source.registryResolution.bundleUrl : undefined,\n sha256: source.kind === \"registry\" ? source.registryResolution.sha256 : undefined,\n publisher:\n source.kind === \"registry\" ? source.registryResolution.publisher : undefined,\n manifestSchemaVersion: manifestBundle.manifest.schemaVersion,\n components: isAppComponentManifestBundle(manifestBundle)\n ? manifestBundle.components.map((component) => ({\n ...component,\n componentDirectory: path.join(installDirectory, component.path),\n manifestPath: path.join(\n installDirectory,\n component.path,\n component.kind === \"panel\" ? \"panel-app.json\" : \"service-app.json\",\n ),\n }))\n : undefined,\n primaryPanelId: isAppComponentManifestBundle(manifestBundle)\n ? manifestBundle.primaryPanelId\n : undefined,\n });\n } catch (error) {\n await rm(installDirectory, { recursive: true, force: true });\n throw error;\n }\n const activeVersionRecord = registryRecord.installedVersions[registryRecord.activeVersion];\n return {\n appId: registryRecord.appId,\n name: registryRecord.name,\n version: registryRecord.activeVersion,\n installDirectory,\n dataDirectory,\n sourceKind: source.kind,\n sourceRef: source.sourceRef,\n distributionMode:\n registryRecord.installedVersions[registryRecord.activeVersion]?.distributionMode,\n permissions:\n registryRecord.installedVersions[registryRecord.activeVersion]?.permissions ?? {},\n registryUrl:\n registryRecord.installedVersions[registryRecord.activeVersion]?.registryUrl,\n bundleUrl:\n registryRecord.installedVersions[registryRecord.activeVersion]?.bundleUrl,\n sha256: registryRecord.installedVersions[registryRecord.activeVersion]?.sha256,\n publisher:\n registryRecord.installedVersions[registryRecord.activeVersion]?.publisher,\n enabled: registryRecord.enabled,\n manifestSchemaVersion: activeVersionRecord?.manifestSchemaVersion ?? 1,\n components: activeVersionRecord?.components,\n primaryPanelId: activeVersionRecord?.primaryPanelId,\n };\n } finally {\n await rm(tempDirectory, { recursive: true, force: true });\n }\n };\n\n update = async (\n appId: string,\n options?: {\n version?: string;\n registryUrl?: string;\n onProgress?: AppInstallProgressHandler;\n },\n ): Promise<AppUpdateResult> => {\n const { onProgress, registryUrl: requestedRegistryUrl, version } = options ?? {};\n await onProgress?.(\"resolving\");\n const appRecord = await this.registryService.getApp(appId);\n if (!appRecord) {\n throw new Error(`未找到已安装应用:${appId}`);\n }\n const activeVersionRecord = appRecord.installedVersions[appRecord.activeVersion];\n const registryUrl =\n requestedRegistryUrl ??\n activeVersionRecord?.registryUrl ??\n (await this.registryConfigService.getSnapshot()).currentUrl;\n const resolution = await this.remoteRegistryClient.resolve({\n appId,\n version,\n registryUrl,\n });\n if (resolution.version === appRecord.activeVersion) {\n if (!activeVersionRecord) {\n throw new Error(`已安装应用缺少激活版本:${appId}`);\n }\n return {\n appId: appRecord.appId,\n name: appRecord.name,\n version: appRecord.activeVersion,\n previousVersion: appRecord.activeVersion,\n installDirectory: activeVersionRecord.installDirectory,\n dataDirectory: appRecord.dataDirectory,\n sourceKind: activeVersionRecord.sourceKind,\n sourceRef: activeVersionRecord.sourceRef,\n permissions: activeVersionRecord.permissions,\n registryUrl: activeVersionRecord.registryUrl,\n bundleUrl: activeVersionRecord.bundleUrl,\n sha256: activeVersionRecord.sha256,\n publisher: activeVersionRecord.publisher,\n enabled: appRecord.enabled,\n manifestSchemaVersion: activeVersionRecord.manifestSchemaVersion,\n components: activeVersionRecord.components,\n primaryPanelId: activeVersionRecord.primaryPanelId,\n updated: false,\n };\n }\n const installedTarget = appRecord.installedVersions[resolution.version];\n if (installedTarget) {\n await onProgress?.(\"verifying\");\n await onProgress?.(\"installing\");\n await this.rollback(appId, resolution.version);\n await onProgress?.(\"finalizing\");\n return {\n appId: appRecord.appId,\n name: appRecord.name,\n version: resolution.version,\n previousVersion: appRecord.activeVersion,\n installDirectory: installedTarget.installDirectory,\n dataDirectory: appRecord.dataDirectory,\n sourceKind: installedTarget.sourceKind,\n distributionMode: installedTarget.distributionMode,\n sourceRef: installedTarget.sourceRef,\n permissions: installedTarget.permissions,\n registryUrl: installedTarget.registryUrl,\n bundleUrl: installedTarget.bundleUrl,\n sha256: installedTarget.sha256,\n publisher: installedTarget.publisher,\n enabled: appRecord.enabled,\n manifestSchemaVersion: installedTarget.manifestSchemaVersion,\n components: installedTarget.components,\n primaryPanelId: installedTarget.primaryPanelId,\n updated: true,\n };\n }\n const installResult = await this.install(`${appId}@${resolution.version}`, {\n registryUrl,\n onProgress,\n });\n return {\n ...installResult,\n previousVersion: appRecord.activeVersion,\n updated: true,\n };\n };\n\n uninstall = async (\n appId: string,\n purgeData: boolean,\n ): Promise<AppUninstallResult> => {\n const appRecord = await this.registryService.getApp(appId);\n if (!appRecord) {\n throw new Error(`未找到已安装应用:${appId}`);\n }\n const removedVersions = Object.keys(appRecord.installedVersions).sort((left, right) =>\n left.localeCompare(right),\n );\n const stagedPaths: Array<{ originalPath: string; stagedPath: string }> = [];\n const stagePath = async (originalPath: string): Promise<void> => {\n if (!await this.pathExists(originalPath)) {\n return;\n }\n const stagedPath = `${originalPath}.uninstalling-${randomUUID()}`;\n await rename(originalPath, stagedPath);\n stagedPaths.push({ originalPath, stagedPath });\n };\n const restoreStagedPaths = async (): Promise<void> => {\n for (const entry of [...stagedPaths].reverse()) {\n if (await this.pathExists(entry.stagedPath)) {\n await rename(entry.stagedPath, entry.originalPath);\n }\n }\n };\n try {\n for (const versionRecord of Object.values(appRecord.installedVersions)) {\n await stagePath(versionRecord.installDirectory);\n }\n if (purgeData) {\n await stagePath(appRecord.dataDirectory);\n }\n const removedRecord = await this.registryService.removeApp(appId);\n if (!removedRecord) {\n throw new Error(`卸载过程中应用记录已发生变化:${appId}`);\n }\n } catch (error) {\n try {\n await restoreStagedPaths();\n } catch (restoreError) {\n throw new AggregateError(\n [error, restoreError],\n `卸载 ${appId} 失败,且无法完全恢复已暂存文件。`,\n );\n }\n throw error;\n }\n await Promise.all(stagedPaths.map((entry) =>\n rm(entry.stagedPath, { recursive: true, force: true }),\n ));\n return {\n appId,\n removedVersions,\n dataRemoved: purgeData,\n };\n };\n\n list = async (): Promise<InstalledAppListItem[]> => {\n const appRecords = await this.registryService.listApps();\n return appRecords.map((appRecord) => ({\n appId: appRecord.appId,\n name: appRecord.name,\n activeVersion: appRecord.activeVersion,\n sourceKind:\n appRecord.installedVersions[appRecord.activeVersion]?.sourceKind ?? \"directory\",\n distributionMode:\n appRecord.installedVersions[appRecord.activeVersion]?.distributionMode,\n enabled: appRecord.enabled,\n manifestSchemaVersion:\n appRecord.installedVersions[appRecord.activeVersion]?.manifestSchemaVersion ?? 1,\n primaryPanelId:\n appRecord.installedVersions[appRecord.activeVersion]?.primaryPanelId,\n }));\n };\n\n reconcileFilesystem = async (): Promise<void> => {\n await this.appHomeService.ensureBaseDirectories();\n const appRecords = await this.registryService.listApps();\n const referencedInstallPaths = new Set(appRecords.flatMap((record) =>\n Object.values(record.installedVersions).map((version) => path.resolve(version.installDirectory))));\n const referencedDataPaths = new Set(appRecords.map((record) => path.resolve(record.dataDirectory)));\n\n const packageDirectories = await this.readDirectories(this.appHomeService.getPackagesDirectory());\n for (const appDirectory of packageDirectories) {\n await this.reconcileGeneratedSiblings(appDirectory, referencedInstallPaths);\n }\n await this.reconcileGeneratedSiblings(\n this.appHomeService.getDataDirectory(),\n referencedDataPaths,\n );\n };\n\n info = async (appId: string): Promise<AppInfoResult> => {\n const appRecord = await this.registryService.getApp(appId);\n if (!appRecord) {\n throw new Error(`未找到已安装应用:${appId}`);\n }\n const installedVersions = Object.values(appRecord.installedVersions).sort((left, right) =>\n left.version.localeCompare(right.version),\n );\n return {\n appId: appRecord.appId,\n name: appRecord.name,\n description: appRecord.description,\n activeVersion: appRecord.activeVersion,\n enabled: appRecord.enabled,\n dataDirectory: appRecord.dataDirectory,\n installedVersions: installedVersions.map((versionRecord) => ({\n version: versionRecord.version,\n installDirectory: versionRecord.installDirectory,\n sourceKind: versionRecord.sourceKind,\n distributionMode: versionRecord.distributionMode,\n sourceRef: versionRecord.sourceRef,\n installedAt: versionRecord.installedAt,\n permissions: versionRecord.permissions,\n registryUrl: versionRecord.registryUrl,\n bundleUrl: versionRecord.bundleUrl,\n sha256: versionRecord.sha256,\n publisher: versionRecord.publisher,\n manifestSchemaVersion: versionRecord.manifestSchemaVersion,\n components: versionRecord.components,\n primaryPanelId: versionRecord.primaryPanelId,\n })),\n grants: appRecord.grants,\n };\n };\n\n setEnabled = async (appId: string, enabled: boolean): Promise<AppActivationResult> => {\n const appRecord = await this.registryService.setEnabled(appId, enabled);\n return {\n appId: appRecord.appId,\n activeVersion: appRecord.activeVersion,\n enabled: appRecord.enabled,\n };\n };\n\n rollback = async (appId: string, version: string): Promise<AppRollbackResult> => {\n const currentRecord = await this.registryService.getApp(appId);\n if (!currentRecord) {\n throw new Error(`未找到已安装应用:${appId}`);\n }\n if (currentRecord.activeVersion === version) {\n return {\n appId,\n activeVersion: version,\n previousVersion: version,\n enabled: currentRecord.enabled,\n rolledBack: false,\n };\n }\n const targetVersion = currentRecord.installedVersions[version];\n if (!targetVersion) {\n throw new Error(`应用 ${appId} 未安装版本 ${version}。`);\n }\n const targetManifest = await this.manifestService.load(targetVersion.installDirectory);\n if (targetManifest.manifest.id !== appId || targetManifest.manifest.version !== version) {\n throw new Error(`回滚目标 ${appId}@${version} 校验失败。`);\n }\n const appRecord = await this.registryService.activateVersion(appId, version);\n return {\n appId,\n activeVersion: appRecord.activeVersion,\n previousVersion: currentRecord.activeVersion,\n enabled: appRecord.enabled,\n rolledBack: true,\n };\n };\n\n resolveLaunch = async (\n appReference: string,\n explicitDocumentGrantMap: AppDocumentGrantMap,\n ): Promise<AppLaunchResolution> => {\n const sourceType = await this.installSourceService.detectLocal(appReference, false);\n if (sourceType.kind === \"directory\") {\n return {\n appDirectory: sourceType.appDirectory,\n documentGrantMap: explicitDocumentGrantMap,\n };\n }\n const appRecord = await this.registryService.getApp(appReference);\n if (!appRecord) {\n throw new Error(`未找到应用目录,也未找到已安装应用:${appReference}`);\n }\n const activeVersion = appRecord.installedVersions[appRecord.activeVersion];\n if (!activeVersion) {\n throw new Error(`已安装应用缺少激活版本:${appReference}`);\n }\n return {\n appDirectory: activeVersion.installDirectory,\n appId: appRecord.appId,\n dataDirectory: appRecord.dataDirectory,\n documentGrantMap: {\n ...appRecord.grants,\n ...explicitDocumentGrantMap,\n },\n };\n };\n\n persistGrants = async (\n appId: string | undefined,\n documentGrantMap: AppDocumentGrantMap,\n ): Promise<void> => {\n if (!appId || Object.keys(documentGrantMap).length === 0) {\n return;\n }\n await this.registryService.updateGrants(appId, documentGrantMap);\n };\n\n private copyToImmutableInstallDirectory = async (params: {\n appId: string;\n appVersion: string;\n extractedDirectory: string;\n installDirectory: string;\n }): Promise<void> => {\n const { appId, appVersion, extractedDirectory, installDirectory } = params;\n if (await this.pathExists(installDirectory)) {\n throw new Error(`应用版本目录已存在,不能覆盖不可变版本:${appId}@${appVersion}`);\n }\n await mkdir(path.dirname(installDirectory), { recursive: true });\n const stagedInstallDirectory = `${installDirectory}.staging-${randomUUID()}`;\n try {\n await cp(extractedDirectory, stagedInstallDirectory, { recursive: true });\n const stagedManifest = await this.manifestService.load(stagedInstallDirectory);\n if (\n stagedManifest.manifest.id !== appId ||\n stagedManifest.manifest.version !== appVersion\n ) {\n throw new Error(\"staging manifest 与已验证 bundle 身份不一致。\");\n }\n await rename(stagedInstallDirectory, installDirectory);\n } catch (error) {\n await rm(stagedInstallDirectory, { recursive: true, force: true });\n throw error;\n }\n };\n\n private materializeDistribution = async (params: {\n appDirectory: string;\n distributionMode: AppDistributionMode;\n }): Promise<void> => {\n if (params.distributionMode !== \"source\") {\n return;\n }\n const manifestBundle = await this.manifestService.load(params.appDirectory);\n if (!isAppStandaloneManifestBundle(manifestBundle)) {\n throw new Error(\"schema v2 组合包不允许运行安装期 build。\");\n }\n if (manifestBundle.manifest.main.kind !== \"wasi-http-component\") {\n return;\n }\n await this.buildService.build({\n appDirectory: params.appDirectory,\n install: true,\n });\n };\n\n private reconcileGeneratedSiblings = async (\n parentDirectory: string,\n referencedPaths: Set<string>,\n ): Promise<void> => {\n for (const entry of await this.readDirectories(parentDirectory)) {\n const entryName = path.basename(entry);\n const stagingMarker = \".staging-\";\n const uninstallingMarker = \".uninstalling-\";\n if (entryName.includes(stagingMarker)) {\n await rm(entry, { recursive: true, force: true });\n continue;\n }\n const markerIndex = entryName.indexOf(uninstallingMarker);\n if (markerIndex < 0) {\n continue;\n }\n const originalPath = path.join(parentDirectory, entryName.slice(0, markerIndex));\n if (referencedPaths.has(path.resolve(originalPath)) && !await this.pathExists(originalPath)) {\n await rename(entry, originalPath);\n } else {\n await rm(entry, { recursive: true, force: true });\n }\n }\n };\n\n private readDirectories = async (directory: string): Promise<string[]> => {\n try {\n return (await readdir(directory, { withFileTypes: true }))\n .filter((entry) => entry.isDirectory())\n .map((entry) => path.join(directory, entry.name));\n } catch (error) {\n if (this.isMissingFileError(error)) {\n return [];\n }\n throw error;\n }\n };\n\n private pathExists = async (targetPath: string): Promise<boolean> => {\n try {\n await access(targetPath);\n return true;\n } catch {\n return false;\n }\n };\n\n private isMissingFileError = (error: unknown): boolean =>\n typeof error === \"object\" && error !== null &&\n \"code\" in error && (error as { code?: unknown }).code === \"ENOENT\";\n}\n"],"mappings":";;;;;;;;;;;;;AA6BA,IAAa,yBAAb,MAAoC;CAClC;CAEA,YACE,iBAAkD,IAAI,gBAAgB,EACtE,gBAAmD,IAAI,kBAAkB,EACzE,kBAAuD,IAAI,oBAAoB,EAC/E,eAAiD,IAAI,iBAAiB,EACtE,kBAAuD,IAAI,mBAAmB,eAAe,EAC7F,wBAAmE,IAAI,yBACrE,eACD,EACD,uBAAwE,IAAI,+BAC1E,IAAI,yBAAyB,eAAe,CAC7C,EACD;AAXiB,OAAA,iBAAA;AACA,OAAA,gBAAA;AACA,OAAA,kBAAA;AACA,OAAA,eAAA;AACA,OAAA,kBAAA;AACA,OAAA,wBAAA;AAGA,OAAA,uBAAA;AAIjB,OAAK,uBAAuB,IAAI,wBAC9B,KAAK,iBACL,KAAK,qBACN;;CAGH,UAAU,OACR,WACA,YAI8B;EAC9B,MAAM,EAAE,YAAY,gBAAgB,WAAW,EAAE;AACjD,QAAM,aAAa,YAAY;EAC/B,MAAM,SAAS,MAAM,KAAK,qBAAqB,QAAQ,WAAW,YAAY;EAC9E,MAAM,gBAAgB,MAAM,KAAK,eAAe,yBAAyB,gBAAgB;AACzF,MAAI;AACF,OAAI,OAAO,SAAS,WAClB,OAAM,aAAa,cAAc;GAEnC,MAAM,aACJ,OAA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,OAAO,cAAyC;AACxE,MAAI;AACF,WAAQ,MAAM,QAAQ,WAAW,EAAE,eAAe,MAAM,CAAC,EACtD,QAAQ,UAAU,MAAM,aAAa,CAAC,CACtC,KAAK,UAAU,KAAK,KAAK,WAAW,MAAM,KAAK,CAAC;WAC5C,OAAO;AACd,OAAI,KAAK,mBAAmB,MAAM,CAChC,QAAO,EAAE;AAEX,SAAM;;;CAIV,aAAqB,OAAO,eAAyC;AACnE,MAAI;AACF,SAAM,OAAO,WAAW;AACxB,UAAO;UACD;AACN,UAAO;;;CAIX,sBAA8B,UAC5B,OAAO,UAAU,YAAY,UAAU,QACvC,UAAU,SAAU,MAA6B,SAAS"}
1
+ {"version":3,"file":"app-installation.service.js","names":[],"sources":["../../src/services/app-installation.service.ts"],"sourcesContent":["import { AsyncLocalStorage } from \"node:async_hooks\";\nimport { rm } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { AppBundleService } from \"#app-runtime/services/app-bundle.service.js\";\nimport { AppManifestService } from \"#app-runtime/services/app-manifest.service.js\";\nimport { isAppComponentManifestBundle } from \"#app-runtime/types/app-manifest.types.js\";\nimport type { AppManifestBundle } from \"#app-runtime/types/app-manifest.types.js\";\nimport { AppHomeService } from \"#app-runtime/services/app-home.service.js\";\nimport { AppInstanceStorageService } from \"#app-runtime/services/app-instance-storage.service.js\";\nimport { AppInstallationIntegrityService } from \"#app-runtime/services/app-installation-integrity.service.js\";\nimport { AppInstallationFilesystemService } from \"#app-runtime/services/app-installation-filesystem.service.js\";\nimport { AppInstallationLifecycleService } from \"#app-runtime/services/app-installation-lifecycle.service.js\";\nimport { FileLockService } from \"#app-runtime/services/file-lock.service.js\";\nimport { AppBuildService } from \"#app-runtime/services/app-build.service.js\";\nimport type { AppBundleExtractResult } from \"#app-runtime/types/app-bundle.types.js\";\nimport type { AppDocumentGrantMap } from \"#app-runtime/types/app-permissions.types.js\";\nimport type { AppPublisher } from \"#app-runtime/types/app-remote-registry.types.js\";\nimport { AppRegistryConfigService } from \"#app-runtime/services/app-registry-config.service.js\";\nimport { AppRemoteRegistryClientService } from \"#app-runtime/services/app-remote-registry-client.service.js\";\nimport { AppRegistryService } from \"#app-runtime/services/app-registry.service.js\";\nimport {\n AppInstallSourceService,\n type ResolvedAppInstallSource,\n} from \"#app-runtime/services/app-install-source.service.js\";\nimport type {\n AppInfoResult,\n AppInstallProgressHandler,\n AppActivationResult,\n AppInstallResult,\n AppLaunchResolution,\n AppUpdateResult,\n AppRollbackResult,\n AppUninstallResult,\n InstalledAppListItem,\n} from \"#app-runtime/types/app-installation.types.js\";\n\nconst SAFE_APP_ID_PATTERN = /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/;\n\ntype AppInstallOptions = {\n registryUrl?: string;\n onProgress?: AppInstallProgressHandler;\n activate?: boolean;\n trustedPublisher?: AppPublisher;\n};\n\ntype PreparedAppInstall = {\n source: ResolvedAppInstallSource;\n extractedDirectory: string;\n extractedMetadata: AppBundleExtractResult;\n manifestBundle: AppManifestBundle;\n options: AppInstallOptions;\n};\n\nexport class AppInstallationService {\n private readonly installSourceService: AppInstallSourceService;\n private readonly instanceStorageService: AppInstanceStorageService;\n private readonly fileLockService = new FileLockService();\n private readonly integrityService: AppInstallationIntegrityService;\n private readonly filesystemService: AppInstallationFilesystemService;\n private readonly lifecycleService: AppInstallationLifecycleService;\n private readonly appOperationContext = new AsyncLocalStorage<ReadonlySet<string>>();\n\n constructor(\n private readonly appHomeService: AppHomeService = new AppHomeService(),\n private readonly bundleService: AppBundleService = new AppBundleService(),\n private readonly manifestService: AppManifestService = new AppManifestService(),\n private readonly buildService: AppBuildService = new AppBuildService(),\n private readonly registryService: AppRegistryService = new AppRegistryService(appHomeService),\n private readonly registryConfigService: AppRegistryConfigService = new AppRegistryConfigService(\n appHomeService,\n ),\n private readonly remoteRegistryClient: AppRemoteRegistryClientService = new AppRemoteRegistryClientService(\n new AppRegistryConfigService(appHomeService),\n ),\n instanceStorageService?: AppInstanceStorageService,\n ) {\n this.installSourceService = new AppInstallSourceService(\n this.manifestService,\n this.remoteRegistryClient,\n this.bundleService,\n );\n this.instanceStorageService = instanceStorageService ?? new AppInstanceStorageService(appHomeService);\n this.integrityService = new AppInstallationIntegrityService(manifestService);\n this.filesystemService = new AppInstallationFilesystemService({\n buildService,\n integrityService: this.integrityService,\n manifestService,\n });\n this.lifecycleService = new AppInstallationLifecycleService({\n registryService,\n registryConfigService,\n remoteRegistryClient,\n instanceStorageService: this.instanceStorageService,\n integrityService: this.integrityService,\n install: this.install,\n rollback: this.rollback,\n });\n }\n\n install = async (\n appSource: string,\n options: AppInstallOptions = {},\n ): Promise<AppInstallResult> => {\n const { onProgress, registryUrl } = options;\n await onProgress?.(\"resolving\");\n const source = await this.installSourceService.resolve(appSource, registryUrl);\n const tempDirectory = await this.appHomeService.createTemporaryDirectory(\"napp-install-\");\n try {\n const bundlePath = await this.installSourceService.materializeBundle(\n source,\n tempDirectory,\n onProgress,\n );\n await onProgress?.(\"verifying\");\n const extractedDirectory = path.join(tempDirectory, \"bundle\");\n const extractedMetadata = await this.bundleService.extractBundle({\n bundlePath,\n targetDirectory: extractedDirectory,\n });\n await this.filesystemService.materializeDistribution({\n appDirectory: extractedDirectory,\n distributionMode: extractedMetadata.metadata.distributionMode,\n });\n const manifestBundle = await this.manifestService.load(extractedDirectory);\n return await this.installPreparedBundle({\n source,\n extractedDirectory,\n extractedMetadata,\n manifestBundle,\n options,\n });\n } finally {\n await rm(tempDirectory, { recursive: true, force: true });\n }\n };\n\n private installPreparedBundle = async (\n prepared: PreparedAppInstall,\n ): Promise<AppInstallResult> => {\n const appId = prepared.manifestBundle.manifest.id;\n return await this.withAppOperation(\n appId,\n async () => await this.commitPreparedInstall(prepared),\n );\n };\n\n private commitPreparedInstall = async (\n prepared: PreparedAppInstall,\n ): Promise<AppInstallResult> => {\n const { extractedDirectory, extractedMetadata, manifestBundle, options, source } = prepared;\n const { onProgress } = options;\n const { id: appId, version } = manifestBundle.manifest;\n const installDirectory = this.appHomeService.getInstallDirectory(appId, version);\n if (source.kind === \"registry\" && appId !== source.registryResolution.appId) {\n throw new Error(\n `bundle manifest.appId 与 registry 请求不一致:期望 ${source.registryResolution.appId},实际 ${appId}`,\n );\n }\n await onProgress?.(\"installing\");\n const existingRecord = await this.registryService.getApp(appId);\n const publisher = source.kind === \"registry\"\n ? source.registryResolution.publisher\n : options.trustedPublisher;\n const currentPublisher = existingRecord?.publisher ??\n existingRecord?.installedVersions[existingRecord.activeVersion]?.publisher;\n if (currentPublisher && currentPublisher.id !== publisher?.id) {\n throw new Error(\n `应用 ${appId} 已绑定发布者 ${currentPublisher.id},拒绝由 ${publisher?.id ?? \"未验证本地来源\"} 覆盖。`,\n );\n }\n const dataSchemaVersion = manifestBundle.manifest.schemaVersion === 2\n ? manifestBundle.manifest.storage?.schemaVersion ?? 1\n : 1;\n if (existingRecord?.defaultInstance.dataSchemaVersion !== undefined &&\n existingRecord.defaultInstance.dataSchemaVersion !== dataSchemaVersion) {\n throw new Error(\n `应用 ${appId} 数据 schema 从 ${existingRecord.defaultInstance.dataSchemaVersion} 升级到 ${dataSchemaVersion},但包内没有受支持的迁移合同。更新已停止,现有数据和版本保持不变。`,\n );\n }\n const instanceDirectory = this.appHomeService.getAppInstanceDirectory(appId, \"default\");\n const instanceExisted = await this.integrityService.pathExists(instanceDirectory);\n const contentSha256 = await this.filesystemService.copyToImmutableInstallDirectory({\n appId,\n appVersion: version,\n extractedDirectory,\n installDirectory,\n });\n const defaultInstance = await this.instanceStorageService.materializeDefaultInstance({\n appId,\n publisherId: (currentPublisher ?? publisher)?.id,\n legacyDataDirectory: existingRecord?.defaultInstance.storage.layout === \"legacy\"\n ? existingRecord.dataDirectory\n : undefined,\n dataSchemaVersion,\n });\n await onProgress?.(\"finalizing\");\n let registryRecord;\n try {\n registryRecord = await this.registryService.upsertInstallation({\n appId,\n name: manifestBundle.manifest.name,\n description: manifestBundle.manifest.description,\n version,\n installDirectory,\n defaultInstance,\n sourceKind: source.kind,\n distributionMode: source.kind === \"registry\"\n ? source.registryResolution.distributionMode\n : extractedMetadata.metadata.distributionMode,\n sourceRef: source.sourceRef,\n installedAt: new Date().toISOString(),\n permissions: manifestBundle.manifest.schemaVersion === 1\n ? manifestBundle.manifest.permissions ?? {}\n : this.manifestService.resolvePlatformSecurity(manifestBundle.manifest).permissions,\n registryUrl: source.kind === \"registry\"\n ? source.registryResolution.registryUrl\n : undefined,\n bundleUrl: source.kind === \"registry\" ? source.registryResolution.bundleUrl : undefined,\n sha256: source.kind === \"registry\" ? source.registryResolution.sha256 : undefined,\n publisher,\n manifestSchemaVersion: manifestBundle.manifest.schemaVersion,\n components: isAppComponentManifestBundle(manifestBundle)\n ? manifestBundle.components.map((component) => ({\n ...component,\n componentDirectory: path.join(installDirectory, component.path),\n manifestPath: path.join(\n installDirectory,\n component.path,\n component.kind === \"panel\" ? \"panel-app.json\" : \"service-app.json\",\n ),\n }))\n : undefined,\n primaryPanelId: isAppComponentManifestBundle(manifestBundle)\n ? manifestBundle.primaryPanelId\n : undefined,\n security: isAppComponentManifestBundle(manifestBundle)\n ? this.manifestService.resolvePlatformSecurity(manifestBundle.manifest)\n : undefined,\n dataSchemaVersion,\n contentSha256,\n activate: options.activate,\n });\n } catch (error) {\n await this.integrityService.removeDirectory(installDirectory);\n if (!instanceExisted) {\n await this.instanceStorageService.rollbackNewInstance({\n instance: defaultInstance,\n legacyDataDirectory: existingRecord?.defaultInstance.storage.layout === \"legacy\"\n ? existingRecord.dataDirectory\n : undefined,\n });\n }\n throw error;\n }\n const installedVersion = registryRecord.installedVersions[version];\n return {\n appId: registryRecord.appId,\n name: registryRecord.name,\n version,\n installDirectory,\n dataDirectory: defaultInstance.storage.dataDirectory,\n instance: registryRecord.defaultInstance,\n sourceKind: source.kind,\n sourceRef: source.sourceRef,\n distributionMode: installedVersion?.distributionMode,\n permissions: installedVersion?.permissions ?? {},\n registryUrl: installedVersion?.registryUrl,\n bundleUrl: installedVersion?.bundleUrl,\n sha256: installedVersion?.sha256,\n publisher: installedVersion?.publisher,\n enabled: registryRecord.enabled,\n manifestSchemaVersion: installedVersion?.manifestSchemaVersion ?? 1,\n components: installedVersion?.components,\n primaryPanelId: installedVersion?.primaryPanelId,\n };\n };\n\n update = async (\n appId: string,\n options?: {\n version?: string;\n registryUrl?: string;\n onProgress?: AppInstallProgressHandler;\n activate?: boolean;\n },\n ): Promise<AppUpdateResult> => {\n return await this.withAppOperation(\n appId,\n async () => await this.lifecycleService.update(appId, options),\n );\n };\n\n uninstall = async (\n appId: string,\n purgeData: boolean,\n ): Promise<AppUninstallResult> => {\n return await this.withAppOperation(\n appId,\n async () => await this.lifecycleService.uninstall(appId, purgeData),\n );\n };\n\n list = async (): Promise<InstalledAppListItem[]> => {\n const appRecords = await this.registryService.listApps();\n return appRecords.map((appRecord) => ({\n appId: appRecord.appId,\n name: appRecord.name,\n activeVersion: appRecord.activeVersion,\n sourceKind:\n appRecord.installedVersions[appRecord.activeVersion]?.sourceKind ?? \"directory\",\n distributionMode:\n appRecord.installedVersions[appRecord.activeVersion]?.distributionMode,\n enabled: appRecord.enabled,\n manifestSchemaVersion:\n appRecord.installedVersions[appRecord.activeVersion]?.manifestSchemaVersion ?? 1,\n primaryPanelId:\n appRecord.installedVersions[appRecord.activeVersion]?.primaryPanelId,\n }));\n };\n\n reconcileFilesystem = async (): Promise<void> => {\n await this.appHomeService.ensureBaseDirectories();\n let appRecords = await this.registryService.listApps();\n for (const appRecord of appRecords) {\n await this.withAppOperation(appRecord.appId, async () => {\n for (const versionRecord of Object.values(appRecord.installedVersions)) {\n if (!await this.integrityService.pathExists(versionRecord.installDirectory)) {\n continue;\n }\n if (!versionRecord.contentSha256) {\n const contentSha256 = await this.integrityService.calculateDigest(\n versionRecord.installDirectory,\n );\n await this.registryService.setVersionContentDigest(\n appRecord.appId,\n versionRecord.version,\n contentSha256,\n );\n }\n await this.integrityService.protectDirectory(versionRecord.installDirectory);\n }\n });\n }\n appRecords = await this.registryService.listApps();\n const referencedInstallPaths = new Set(appRecords.flatMap((record) =>\n Object.values(record.installedVersions).map((version) => path.resolve(version.installDirectory))));\n const referencedDataPaths = new Set(appRecords.map((record) => path.resolve(\n record.defaultInstance.storage.layout === \"instance-v1\"\n ? record.defaultInstance.storage.instanceDirectory\n : record.dataDirectory,\n )));\n\n const packageDirectories = await this.filesystemService.readDirectories(\n this.appHomeService.getPackagesDirectory(),\n );\n for (const appDirectory of packageDirectories) {\n await this.filesystemService.reconcileGeneratedSiblings(appDirectory, referencedInstallPaths);\n }\n await this.filesystemService.reconcileGeneratedSiblings(\n this.appHomeService.getDataDirectory(),\n referencedDataPaths,\n );\n for (const appRecord of appRecords) {\n if (appRecord.defaultInstance.storage.layout !== \"instance-v1\") {\n continue;\n }\n await this.filesystemService.reconcileGeneratedSiblings(\n appRecord.defaultInstance.storage.instanceDirectory,\n new Set([path.resolve(appRecord.defaultInstance.storage.dataDirectory)]),\n );\n }\n for (const appRecord of appRecords) {\n if (appRecord.defaultInstance.storage.layout !== \"legacy\") {\n continue;\n }\n await this.withAppOperation(appRecord.appId, async () => {\n const currentRecord = await this.registryService.getApp(appRecord.appId);\n if (!currentRecord || currentRecord.defaultInstance.storage.layout !== \"legacy\") {\n return;\n }\n const activeVersion = currentRecord.installedVersions[currentRecord.activeVersion];\n if (!activeVersion) {\n throw new Error(`已安装应用缺少激活版本:${currentRecord.appId}`);\n }\n const instance = await this.instanceStorageService.materializeDefaultInstance({\n appId: currentRecord.appId,\n publisherId: currentRecord.publisher?.id,\n legacyDataDirectory: currentRecord.dataDirectory,\n dataSchemaVersion: currentRecord.defaultInstance.dataSchemaVersion,\n });\n try {\n await this.registryService.upsertInstallation({\n appId: currentRecord.appId,\n name: currentRecord.name,\n description: currentRecord.description,\n version: currentRecord.activeVersion,\n installDirectory: activeVersion.installDirectory,\n defaultInstance: instance,\n sourceKind: activeVersion.sourceKind,\n sourceRef: activeVersion.sourceRef,\n installedAt: activeVersion.installedAt,\n distributionMode: activeVersion.distributionMode,\n permissions: activeVersion.permissions,\n registryUrl: activeVersion.registryUrl,\n bundleUrl: activeVersion.bundleUrl,\n sha256: activeVersion.sha256,\n publisher: currentRecord.publisher ?? activeVersion.publisher,\n manifestSchemaVersion: activeVersion.manifestSchemaVersion,\n components: activeVersion.components,\n primaryPanelId: activeVersion.primaryPanelId,\n security: activeVersion.security,\n dataSchemaVersion: activeVersion.dataSchemaVersion,\n contentSha256: activeVersion.contentSha256,\n enabled: currentRecord.enabled,\n });\n } catch (error) {\n await this.instanceStorageService.rollbackNewInstance({\n instance,\n legacyDataDirectory: currentRecord.dataDirectory,\n });\n throw error;\n }\n });\n }\n };\n\n info = async (appId: string): Promise<AppInfoResult> => {\n const appRecord = await this.registryService.getApp(appId);\n if (!appRecord) {\n throw new Error(`未找到已安装应用:${appId}`);\n }\n const installedVersions = Object.values(appRecord.installedVersions).sort((left, right) =>\n left.version.localeCompare(right.version),\n );\n return {\n appId: appRecord.appId,\n name: appRecord.name,\n description: appRecord.description,\n activeVersion: appRecord.activeVersion,\n enabled: appRecord.enabled,\n dataDirectory: appRecord.dataDirectory,\n instance: appRecord.defaultInstance,\n storage: appRecord.defaultInstance.storage,\n storageUsage: await this.instanceStorageService.measureUsage(\n appRecord.defaultInstance.storage,\n ),\n installedVersions: installedVersions.map((versionRecord) => ({\n version: versionRecord.version,\n installDirectory: versionRecord.installDirectory,\n sourceKind: versionRecord.sourceKind,\n distributionMode: versionRecord.distributionMode,\n sourceRef: versionRecord.sourceRef,\n installedAt: versionRecord.installedAt,\n permissions: versionRecord.permissions,\n registryUrl: versionRecord.registryUrl,\n bundleUrl: versionRecord.bundleUrl,\n sha256: versionRecord.sha256,\n publisher: versionRecord.publisher,\n manifestSchemaVersion: versionRecord.manifestSchemaVersion,\n components: versionRecord.components,\n primaryPanelId: versionRecord.primaryPanelId,\n contentSha256: versionRecord.contentSha256,\n })),\n grants: appRecord.grants,\n };\n };\n\n setEnabled = async (appId: string, enabled: boolean): Promise<AppActivationResult> => {\n return await this.withAppOperation(appId, async () => {\n const appRecord = await this.registryService.setEnabled(appId, enabled);\n return {\n appId: appRecord.appId,\n activeVersion: appRecord.activeVersion,\n enabled: appRecord.enabled,\n };\n });\n };\n\n rollback = async (appId: string, version: string): Promise<AppRollbackResult> => {\n return await this.withAppOperation(appId, async () => {\n const currentRecord = await this.registryService.getApp(appId);\n if (!currentRecord) {\n throw new Error(`未找到已安装应用:${appId}`);\n }\n if (currentRecord.activeVersion === version) {\n return {\n appId,\n activeVersion: version,\n previousVersion: version,\n enabled: currentRecord.enabled,\n rolledBack: false,\n };\n }\n const targetVersion = currentRecord.installedVersions[version];\n if (!targetVersion) {\n throw new Error(`应用 ${appId} 未安装版本 ${version}。`);\n }\n if (targetVersion.dataSchemaVersion !== currentRecord.defaultInstance.dataSchemaVersion) {\n throw new Error(\n `应用 ${appId}@${version} 需要数据 schema ${targetVersion.dataSchemaVersion},当前实例为 ${currentRecord.defaultInstance.dataSchemaVersion}。没有可用 checkpoint,已阻止回滚。`,\n );\n }\n await this.assertVersionIntegrity(appId, version);\n const appRecord = await this.registryService.activateVersion(appId, version);\n return {\n appId,\n activeVersion: appRecord.activeVersion,\n previousVersion: currentRecord.activeVersion,\n enabled: appRecord.enabled,\n rolledBack: true,\n };\n });\n };\n\n assertVersionIntegrity = async (appId: string, version?: string): Promise<void> => {\n const appRecord = await this.registryService.getApp(appId);\n if (!appRecord) {\n throw new Error(`未找到已安装应用:${appId}`);\n }\n const targetVersion = version ?? appRecord.activeVersion;\n const versionRecord = appRecord.installedVersions[targetVersion];\n if (!versionRecord) {\n throw new Error(`应用 ${appId} 未安装版本 ${targetVersion}。`);\n }\n const contentSha256 = await this.integrityService.assertVersion({\n appId,\n version: targetVersion,\n versionRecord,\n });\n if (!versionRecord.contentSha256) {\n await this.registryService.setVersionContentDigest(appId, targetVersion, contentSha256);\n await this.integrityService.protectDirectory(versionRecord.installDirectory);\n }\n };\n\n withAppOperation = async <T>(appId: string, operation: () => Promise<T>): Promise<T> => {\n if (!SAFE_APP_ID_PATTERN.test(appId)) {\n throw new Error(`appId 不是安全的 App 操作标识:${appId}`);\n }\n const lockPath = path.resolve(this.appHomeService.getAppOperationLockPath(appId));\n const activeLocks = this.appOperationContext.getStore();\n if (activeLocks?.has(lockPath)) {\n return await operation();\n }\n return await this.fileLockService.withLock(lockPath, async () => {\n const nextLocks = new Set(activeLocks ?? []);\n nextLocks.add(lockPath);\n return await this.appOperationContext.run(nextLocks, operation);\n });\n };\n\n resolveLaunch = async (\n appReference: string,\n explicitDocumentGrantMap: AppDocumentGrantMap,\n ): Promise<AppLaunchResolution> => {\n const sourceType = await this.installSourceService.detectLocal(appReference, false);\n if (sourceType.kind === \"directory\") {\n return {\n appDirectory: sourceType.appDirectory,\n documentGrantMap: explicitDocumentGrantMap,\n };\n }\n const appRecord = await this.registryService.getApp(appReference);\n if (!appRecord) {\n throw new Error(`未找到应用目录,也未找到已安装应用:${appReference}`);\n }\n const activeVersion = appRecord.installedVersions[appRecord.activeVersion];\n if (!activeVersion) {\n throw new Error(`已安装应用缺少激活版本:${appReference}`);\n }\n await this.assertVersionIntegrity(appRecord.appId, appRecord.activeVersion);\n return {\n appDirectory: activeVersion.installDirectory,\n appId: appRecord.appId,\n dataDirectory: appRecord.dataDirectory,\n storage: appRecord.defaultInstance.storage,\n documentGrantMap: {\n ...appRecord.grants,\n ...explicitDocumentGrantMap,\n },\n };\n };\n\n persistGrants = async (\n appId: string | undefined,\n documentGrantMap: AppDocumentGrantMap,\n ): Promise<void> => {\n if (!appId || Object.keys(documentGrantMap).length === 0) {\n return;\n }\n await this.registryService.updateGrants(appId, documentGrantMap);\n };\n\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoCA,MAAM,sBAAsB;AAiB5B,IAAa,yBAAb,MAAoC;CAClC;CACA;CACA,kBAAmC,IAAI,iBAAiB;CACxD;CACA;CACA;CACA,sBAAuC,IAAI,mBAAwC;CAEnF,YACE,iBAAkD,IAAI,gBAAgB,EACtE,gBAAmD,IAAI,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,cAAc,KAAK,oBAAoB,UAAU;AACvD,MAAI,aAAa,IAAI,SAAS,CAC5B,QAAO,MAAM,WAAW;AAE1B,SAAO,MAAM,KAAK,gBAAgB,SAAS,UAAU,YAAY;GAC/D,MAAM,YAAY,IAAI,IAAI,eAAe,EAAE,CAAC;AAC5C,aAAU,IAAI,SAAS;AACvB,UAAO,MAAM,KAAK,oBAAoB,IAAI,WAAW,UAAU;IAC/D;;CAGJ,gBAAgB,OACd,cACA,6BACiC;EACjC,MAAM,aAAa,MAAM,KAAK,qBAAqB,YAAY,cAAc,MAAM;AACnF,MAAI,WAAW,SAAS,YACtB,QAAO;GACL,cAAc,WAAW;GACzB,kBAAkB;GACnB;EAEH,MAAM,YAAY,MAAM,KAAK,gBAAgB,OAAO,aAAa;AACjE,MAAI,CAAC,UACH,OAAM,IAAI,MAAM,qBAAqB,eAAe;EAEtD,MAAM,gBAAgB,UAAU,kBAAkB,UAAU;AAC5D,MAAI,CAAC,cACH,OAAM,IAAI,MAAM,eAAe,eAAe;AAEhD,QAAM,KAAK,uBAAuB,UAAU,OAAO,UAAU,cAAc;AAC3E,SAAO;GACL,cAAc,cAAc;GAC5B,OAAO,UAAU;GACjB,eAAe,UAAU;GACzB,SAAS,UAAU,gBAAgB;GACnC,kBAAkB;IAChB,GAAG,UAAU;IACb,GAAG;IACJ;GACF;;CAGH,gBAAgB,OACd,OACA,qBACkB;AAClB,MAAI,CAAC,SAAS,OAAO,KAAK,iBAAiB,CAAC,WAAW,EACrD;AAEF,QAAM,KAAK,gBAAgB,aAAa,OAAO,iBAAiB"}
@@ -0,0 +1,31 @@
1
+ import { AppInstanceInventory } from "../types/app-storage.types.js";
2
+ import { AppInstanceStorageService } from "./app-instance-storage.service.js";
3
+
4
+ //#region src/services/app-instance-inventory.service.d.ts
5
+ declare class AppInstanceInventoryService {
6
+ private readonly storageService;
7
+ private readonly fileLockService;
8
+ constructor(storageService?: AppInstanceStorageService);
9
+ list: (instancesRoot: string) => Promise<AppInstanceInventory>;
10
+ purge: (params: {
11
+ instancesRoot: string;
12
+ appId: string;
13
+ instanceId: string;
14
+ assertCanPurge?: () => Promise<void>;
15
+ }) => Promise<void>;
16
+ purgeNested: (params: {
17
+ instancesRoot: string;
18
+ pathSegments: string[];
19
+ appId: string;
20
+ instanceId: string;
21
+ assertCanPurge?: () => Promise<void>;
22
+ }) => Promise<void>;
23
+ private listDirectories;
24
+ private assertSafeId;
25
+ private assertWithinRoot;
26
+ private pathExists;
27
+ private isMissingFileError;
28
+ }
29
+ //#endregion
30
+ export { AppInstanceInventoryService };
31
+ //# sourceMappingURL=app-instance-inventory.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-instance-inventory.service.d.ts","names":[],"sources":["../../src/services/app-instance-inventory.service.ts"],"mappings":";;;;cAYa,2BAAA;EAAA,iBAIQ,cAAA;EAAA,iBAHF,eAAA;cAGE,cAAA,GAAc,yBAAA;EAGjC,IAAA,GAAc,aAAA,aAAwB,OAAA,CAAQ,oBAAA;EAuD9C,KAAA,GAAe,MAAA;IACb,aAAA;IACA,KAAA;IACA,UAAA;IACA,cAAA,SAAuB,OAAA;EAAA,MACrB,OAAA;EAWJ,WAAA,GAAqB,MAAA;IACnB,aAAA;IACA,YAAA;IACA,KAAA;IACA,UAAA;IACA,cAAA,SAAuB,OAAA;EAAA,MACrB,OAAA;EAAA,QAuCI,eAAA;EAAA,QAYA,YAAA;EAAA,QAMA,gBAAA;EAAA,QAOA,UAAA;EAAA,QASA,kBAAA;AAAA"}
@@ -0,0 +1,136 @@
1
+ import { FileLockService } from "./file-lock.service.js";
2
+ import { AppInstanceStorageService } from "./app-instance-storage.service.js";
3
+ import { randomUUID } from "node:crypto";
4
+ import { access, readdir, rename, rm } from "node:fs/promises";
5
+ import path from "node:path";
6
+ //#region src/services/app-instance-inventory.service.ts
7
+ const SAFE_ID_PATTERN = /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/;
8
+ var AppInstanceInventoryService = class {
9
+ fileLockService = new FileLockService();
10
+ constructor(storageService = new AppInstanceStorageService()) {
11
+ this.storageService = storageService;
12
+ }
13
+ list = async (instancesRoot) => {
14
+ const root = path.resolve(instancesRoot);
15
+ const entries = [];
16
+ const diagnostics = [];
17
+ for (const appDirectory of await this.listDirectories(root)) {
18
+ const appId = appDirectory.name;
19
+ const appPath = path.join(root, appId);
20
+ if (!SAFE_ID_PATTERN.test(appId)) {
21
+ diagnostics.push({
22
+ instanceDirectory: appPath,
23
+ message: `不安全的 App Instance appId:${appId}`
24
+ });
25
+ continue;
26
+ }
27
+ for (const instanceDirectory of await this.listDirectories(appPath)) {
28
+ const instanceId = instanceDirectory.name;
29
+ const instancePath = path.join(appPath, instanceId);
30
+ if (!SAFE_ID_PATTERN.test(instanceId)) {
31
+ diagnostics.push({
32
+ instanceDirectory: instancePath,
33
+ message: `不安全的 App Instance instanceId:${instanceId}`
34
+ });
35
+ continue;
36
+ }
37
+ try {
38
+ const instance = await this.storageService.inspect({
39
+ appId,
40
+ instanceId,
41
+ instanceDirectory: instancePath
42
+ });
43
+ const usage = await this.storageService.measureUsage(instance.storage);
44
+ entries.push({
45
+ appId,
46
+ instanceId,
47
+ publisherId: instance.publisherId,
48
+ storage: instance.storage,
49
+ usage,
50
+ createdAt: instance.createdAt,
51
+ migratedAt: instance.migratedAt
52
+ });
53
+ } catch (error) {
54
+ diagnostics.push({
55
+ instanceDirectory: instancePath,
56
+ message: error instanceof Error ? error.message : String(error)
57
+ });
58
+ }
59
+ }
60
+ }
61
+ entries.sort((left, right) => left.appId.localeCompare(right.appId) || left.instanceId.localeCompare(right.instanceId));
62
+ diagnostics.sort((left, right) => left.instanceDirectory.localeCompare(right.instanceDirectory));
63
+ return {
64
+ entries,
65
+ diagnostics
66
+ };
67
+ };
68
+ purge = async (params) => {
69
+ const { appId, assertCanPurge, instanceId, instancesRoot } = params;
70
+ return await this.purgeNested({
71
+ instancesRoot,
72
+ pathSegments: [appId, instanceId],
73
+ appId,
74
+ instanceId,
75
+ assertCanPurge
76
+ });
77
+ };
78
+ purgeNested = async (params) => {
79
+ const { appId, assertCanPurge, instanceId, instancesRoot, pathSegments } = params;
80
+ this.assertSafeId(appId, "appId");
81
+ this.assertSafeId(instanceId, "instanceId");
82
+ if (pathSegments.length === 0) throw new Error("App Instance 相对路径不能为空。");
83
+ pathSegments.forEach((segment, index) => this.assertSafeId(segment, `pathSegments[${index}]`));
84
+ const root = path.resolve(instancesRoot);
85
+ const instanceDirectory = path.resolve(root, ...pathSegments);
86
+ this.assertWithinRoot(root, instanceDirectory);
87
+ await this.fileLockService.withLock(`${instanceDirectory}.lock`, async () => {
88
+ await assertCanPurge?.();
89
+ await this.storageService.inspect({
90
+ appId,
91
+ instanceId,
92
+ instanceDirectory
93
+ });
94
+ const stagingDirectory = `${instanceDirectory}.deleting-${randomUUID()}`;
95
+ await rename(instanceDirectory, stagingDirectory);
96
+ try {
97
+ await rm(stagingDirectory, { recursive: true });
98
+ } catch (error) {
99
+ try {
100
+ if (!await this.pathExists(instanceDirectory)) await rename(stagingDirectory, instanceDirectory);
101
+ } catch (restoreError) {
102
+ throw new AggregateError([error, restoreError], `App Instance 删除失败且恢复失败:${appId}/${instanceId}`);
103
+ }
104
+ throw error;
105
+ }
106
+ });
107
+ };
108
+ listDirectories = async (directory) => {
109
+ try {
110
+ return (await readdir(directory, { withFileTypes: true })).filter((entry) => entry.isDirectory() && !entry.name.startsWith("."));
111
+ } catch (error) {
112
+ if (this.isMissingFileError(error)) return [];
113
+ throw error;
114
+ }
115
+ };
116
+ assertSafeId = (value, field) => {
117
+ if (!SAFE_ID_PATTERN.test(value)) throw new Error(`${field} 不是安全的 App Instance 标识:${value}`);
118
+ };
119
+ assertWithinRoot = (root, target) => {
120
+ const relative = path.relative(root, target);
121
+ if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) throw new Error(`App Instance 路径不在受管根目录内:${target}`);
122
+ };
123
+ pathExists = async (targetPath) => {
124
+ try {
125
+ await access(targetPath);
126
+ return true;
127
+ } catch {
128
+ return false;
129
+ }
130
+ };
131
+ isMissingFileError = (error) => typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
132
+ };
133
+ //#endregion
134
+ export { AppInstanceInventoryService };
135
+
136
+ //# sourceMappingURL=app-instance-inventory.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-instance-inventory.service.js","names":[],"sources":["../../src/services/app-instance-inventory.service.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { access, readdir, rename, rm } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { AppInstanceStorageService } from \"#app-runtime/services/app-instance-storage.service.js\";\nimport { FileLockService } from \"#app-runtime/services/file-lock.service.js\";\nimport type {\n AppInstanceInventory,\n AppInstanceInventoryEntry,\n} from \"#app-runtime/types/app-storage.types.js\";\n\nconst SAFE_ID_PATTERN = /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/;\n\nexport class AppInstanceInventoryService {\n private readonly fileLockService = new FileLockService();\n\n constructor(\n private readonly storageService = new AppInstanceStorageService(),\n ) {}\n\n list = async (instancesRoot: string): Promise<AppInstanceInventory> => {\n const root = path.resolve(instancesRoot);\n const entries: AppInstanceInventoryEntry[] = [];\n const diagnostics: AppInstanceInventory[\"diagnostics\"] = [];\n for (const appDirectory of await this.listDirectories(root)) {\n const appId = appDirectory.name;\n const appPath = path.join(root, appId);\n if (!SAFE_ID_PATTERN.test(appId)) {\n diagnostics.push({\n instanceDirectory: appPath,\n message: `不安全的 App Instance appId:${appId}`,\n });\n continue;\n }\n for (const instanceDirectory of await this.listDirectories(appPath)) {\n const instanceId = instanceDirectory.name;\n const instancePath = path.join(appPath, instanceId);\n if (!SAFE_ID_PATTERN.test(instanceId)) {\n diagnostics.push({\n instanceDirectory: instancePath,\n message: `不安全的 App Instance instanceId:${instanceId}`,\n });\n continue;\n }\n try {\n const instance = await this.storageService.inspect({\n appId,\n instanceId,\n instanceDirectory: instancePath,\n });\n const usage = await this.storageService.measureUsage(instance.storage);\n entries.push({\n appId,\n instanceId,\n publisherId: instance.publisherId,\n storage: instance.storage,\n usage,\n createdAt: instance.createdAt,\n migratedAt: instance.migratedAt,\n });\n } catch (error) {\n diagnostics.push({\n instanceDirectory: instancePath,\n message: error instanceof Error ? error.message : String(error),\n });\n }\n }\n }\n entries.sort((left, right) =>\n left.appId.localeCompare(right.appId) || left.instanceId.localeCompare(right.instanceId));\n diagnostics.sort((left, right) =>\n left.instanceDirectory.localeCompare(right.instanceDirectory));\n return { entries, diagnostics };\n };\n\n purge = async (params: {\n instancesRoot: string;\n appId: string;\n instanceId: string;\n assertCanPurge?: () => Promise<void>;\n }): Promise<void> => {\n const { appId, assertCanPurge, instanceId, instancesRoot } = params;\n return await this.purgeNested({\n instancesRoot,\n pathSegments: [appId, instanceId],\n appId,\n instanceId,\n assertCanPurge,\n });\n };\n\n purgeNested = async (params: {\n instancesRoot: string;\n pathSegments: string[];\n appId: string;\n instanceId: string;\n assertCanPurge?: () => Promise<void>;\n }): Promise<void> => {\n const { appId, assertCanPurge, instanceId, instancesRoot, pathSegments } = params;\n this.assertSafeId(appId, \"appId\");\n this.assertSafeId(instanceId, \"instanceId\");\n if (pathSegments.length === 0) {\n throw new Error(\"App Instance 相对路径不能为空。\");\n }\n pathSegments.forEach((segment, index) =>\n this.assertSafeId(segment, `pathSegments[${index}]`));\n const root = path.resolve(instancesRoot);\n const instanceDirectory = path.resolve(root, ...pathSegments);\n this.assertWithinRoot(root, instanceDirectory);\n await this.fileLockService.withLock(`${instanceDirectory}.lock`, async () => {\n await assertCanPurge?.();\n await this.storageService.inspect({\n appId,\n instanceId,\n instanceDirectory,\n });\n const stagingDirectory = `${instanceDirectory}.deleting-${randomUUID()}`;\n await rename(instanceDirectory, stagingDirectory);\n try {\n await rm(stagingDirectory, { recursive: true });\n } catch (error) {\n try {\n if (!await this.pathExists(instanceDirectory)) {\n await rename(stagingDirectory, instanceDirectory);\n }\n } catch (restoreError) {\n throw new AggregateError(\n [error, restoreError],\n `App Instance 删除失败且恢复失败:${appId}/${instanceId}`,\n );\n }\n throw error;\n }\n });\n };\n\n private listDirectories = async (directory: string) => {\n try {\n return (await readdir(directory, { withFileTypes: true }))\n .filter((entry) => entry.isDirectory() && !entry.name.startsWith(\".\"));\n } catch (error) {\n if (this.isMissingFileError(error)) {\n return [];\n }\n throw error;\n }\n };\n\n private assertSafeId = (value: string, field: string): void => {\n if (!SAFE_ID_PATTERN.test(value)) {\n throw new Error(`${field} 不是安全的 App Instance 标识:${value}`);\n }\n };\n\n private assertWithinRoot = (root: string, target: string): void => {\n const relative = path.relative(root, target);\n if (!relative || relative.startsWith(\"..\") || path.isAbsolute(relative)) {\n throw new Error(`App Instance 路径不在受管根目录内:${target}`);\n }\n };\n\n private pathExists = async (targetPath: string): Promise<boolean> => {\n try {\n await access(targetPath);\n return true;\n } catch {\n return false;\n }\n };\n\n private isMissingFileError = (error: unknown): boolean =>\n typeof error === \"object\" && error !== null &&\n \"code\" in error && (error as { code?: unknown }).code === \"ENOENT\";\n}\n"],"mappings":";;;;;;AAUA,MAAM,kBAAkB;AAExB,IAAa,8BAAb,MAAyC;CACvC,kBAAmC,IAAI,iBAAiB;CAExD,YACE,iBAAkC,IAAI,2BAA2B,EACjE;AADiB,OAAA,iBAAA;;CAGnB,OAAO,OAAO,kBAAyD;EACrE,MAAM,OAAO,KAAK,QAAQ,cAAc;EACxC,MAAM,UAAuC,EAAE;EAC/C,MAAM,cAAmD,EAAE;AAC3D,OAAK,MAAM,gBAAgB,MAAM,KAAK,gBAAgB,KAAK,EAAE;GAC3D,MAAM,QAAQ,aAAa;GAC3B,MAAM,UAAU,KAAK,KAAK,MAAM,MAAM;AACtC,OAAI,CAAC,gBAAgB,KAAK,MAAM,EAAE;AAChC,gBAAY,KAAK;KACf,mBAAmB;KACnB,SAAS,2BAA2B;KACrC,CAAC;AACF;;AAEF,QAAK,MAAM,qBAAqB,MAAM,KAAK,gBAAgB,QAAQ,EAAE;IACnE,MAAM,aAAa,kBAAkB;IACrC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U,YAAY,UAAU,QACvC,UAAU,SAAU,MAA6B,SAAS"}
@@ -0,0 +1,55 @@
1
+ import { AppHomeService } from "./app-home.service.js";
2
+ import { AppInstanceRecord, AppStorageContext, AppStorageUsage } from "../types/app-storage.types.js";
3
+
4
+ //#region src/services/app-instance-storage.service.d.ts
5
+ declare class AppInstanceStorageService {
6
+ private readonly appHomeService;
7
+ private readonly fileLockService;
8
+ constructor(appHomeService?: AppHomeService);
9
+ materializeDefaultInstance: (params: {
10
+ appId: string;
11
+ publisherId?: string;
12
+ legacyDataDirectory?: string;
13
+ dataSchemaVersion?: number;
14
+ }) => Promise<AppInstanceRecord>;
15
+ materialize: (params: {
16
+ appId: string;
17
+ instanceId: string;
18
+ instanceDirectory: string;
19
+ publisherId?: string;
20
+ legacyDataDirectory?: string;
21
+ dataSchemaVersion?: number;
22
+ }) => Promise<AppInstanceRecord>;
23
+ private materializeUnlocked;
24
+ private materializeNewInstance;
25
+ buildLegacyDefaultInstance: (params: {
26
+ appId: string;
27
+ dataDirectory: string;
28
+ createdAt: string;
29
+ }) => AppInstanceRecord;
30
+ measureUsage: (storage: AppStorageContext) => Promise<AppStorageUsage>;
31
+ inspect: (params: {
32
+ appId: string;
33
+ instanceId: string;
34
+ instanceDirectory: string;
35
+ }) => Promise<AppInstanceRecord>;
36
+ rollbackNewInstance: (params: {
37
+ instance: AppInstanceRecord;
38
+ legacyDataDirectory?: string;
39
+ }) => Promise<void>;
40
+ private resolveExistingInstance;
41
+ private buildContext;
42
+ private ensureStorageDirectories;
43
+ private readMetadata;
44
+ private toInstanceRecord;
45
+ private assertMetadataIdentity;
46
+ private assertMetadataCoordinates;
47
+ private bindPublisher;
48
+ private assertSafeId;
49
+ private measureDirectory;
50
+ private pathExists;
51
+ private isMissingFileError;
52
+ }
53
+ //#endregion
54
+ export { AppInstanceStorageService };
55
+ //# sourceMappingURL=app-instance-storage.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app-instance-storage.service.d.ts","names":[],"sources":["../../src/services/app-instance-storage.service.ts"],"mappings":";;;;cAiBa,yBAAA;EAAA,iBAGkB,cAAA;EAAA,iBAFZ,eAAA;cAEY,cAAA,GAAgB,cAAA;EAE7C,0BAAA,GAAoC,MAAA;IAClC,KAAA;IACA,WAAA;IACA,mBAAA;IACA,iBAAA;EAAA,MACE,OAAA,CAAQ,iBAAA;EAWZ,WAAA,GAAqB,MAAA;IACnB,KAAA;IACA,UAAA;IACA,iBAAA;IACA,WAAA;IACA,mBAAA;IACA,iBAAA;EAAA,MACE,OAAA,CAAQ,iBAAA;EAAA,QAUJ,mBAAA;EAAA,QA6BA,sBAAA;EA+ER,0BAAA,GAA8B,MAAA;IAC5B,KAAA;IACA,aAAA;IACA,SAAA;EAAA,MACE,iBAAA;EAoBJ,YAAA,GAAsB,OAAA,EAAS,iBAAA,KAAoB,OAAA,CAAQ,eAAA;EAqB3D,OAAA,GAAiB,MAAA;IACf,KAAA;IACA,UAAA;IACA,iBAAA;EAAA,MACE,OAAA,CAAQ,iBAAA;EAgBZ,mBAAA,GAA6B,MAAA;IAC3B,QAAA,EAAU,iBAAA;IACV,mBAAA;EAAA,MACE,OAAA;EAAA,QAqBI,uBAAA;EAAA,QAqBA,YAAA;EAAA,QAgBA,wBAAA;EAAA,QAWA,YAAA;EAAA,QA0BA,gBAAA;EAAA,QAcA,sBAAA;EAAA,QAcA,yBAAA;EAAA,QAYA,aAAA;EAAA,QAqBA,YAAA;EAAA,QAMA,gBAAA;EAAA,QA0BA,UAAA;EAAA,QASA,kBAAA;AAAA"}