@adea-ai/app-framework 0.27.0 → 0.28.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -15
- package/dist/app/create-application.d.ts +2 -0
- package/dist/app/create-application.js +6 -0
- package/dist/app/service-registry.js +4 -0
- package/dist/appearance/custom-theme-import.js +43 -7
- package/dist/appearance/custom-theme-library.d.ts +2 -0
- package/dist/appearance/custom-theme-library.js +5 -1
- package/dist/appearance/theme-record-adapter.js +4 -2
- package/dist/appearance/theme-registry.js +7 -1
- package/dist/cli/cli-runner.d.ts +4 -0
- package/dist/cli/cli-runner.js +14 -1
- package/dist/cli/create-application-cli.js +4 -2
- package/dist/cli/release-cut-cli.js +2 -2
- package/dist/cli/update-helper-cli.js +28 -6
- package/dist/desktop/client-asset-manifest.d.ts +47 -0
- package/dist/desktop/client-asset-manifest.js +114 -0
- package/dist/desktop/client-asset-stream.d.ts +10 -0
- package/dist/desktop/client-asset-stream.js +138 -33
- package/dist/desktop/client-assets.d.ts +20 -0
- package/dist/desktop/client-assets.js +132 -22
- package/dist/desktop/client-response-lifecycle.d.ts +22 -0
- package/dist/desktop/client-response-lifecycle.js +233 -0
- package/dist/desktop/desktop-host.d.ts +40 -2
- package/dist/desktop/desktop-host.js +159 -9
- package/dist/framework-source.json +1 -1
- package/dist/installer/command-artifact.d.ts +4 -2
- package/dist/installer/command-artifact.js +119 -19
- package/dist/installer/linux-process-group.d.ts +13 -0
- package/dist/installer/linux-process-group.js +58 -0
- package/dist/native/darwin-sync.d.ts +7 -0
- package/dist/native/darwin-sync.js +43 -0
- package/dist/native/desktop-build-hook.js +7 -0
- package/dist/native/directory-exchange.d.ts +10 -0
- package/dist/native/directory-exchange.js +90 -0
- package/dist/native/electrobun-source-patch.d.ts +5 -0
- package/dist/native/electrobun-source-patch.js +19 -1
- package/dist/native/posix-launcher-build.d.ts +24 -0
- package/dist/native/posix-launcher-build.js +232 -0
- package/dist/native/posix-tree-removal.d.ts +36 -0
- package/dist/native/posix-tree-removal.js +425 -0
- package/dist/release/feed-release-cut.js +30 -10
- package/dist/release/github-release-cut.js +16 -0
- package/dist/release/release-cut.d.ts +8 -1
- package/dist/release/release-cut.js +9 -1
- package/dist/update-helper.bundle.js +1483 -105
- package/dist/updates/channel-update-manager.d.ts +5 -0
- package/dist/updates/channel-update-manager.js +13 -1
- package/dist/updates/desktop-updater.d.ts +78 -2
- package/dist/updates/desktop-updater.js +320 -37
- package/dist/updates/launch-environment.d.ts +2 -0
- package/dist/updates/launch-environment.js +4 -0
- package/dist/updates/native-file-lock.d.ts +15 -0
- package/dist/updates/native-file-lock.js +189 -0
- package/dist/updates/packaged-launch-admission.d.ts +13 -0
- package/dist/updates/packaged-launch-admission.js +45 -0
- package/dist/updates/packaged-release-config.d.ts +18 -0
- package/dist/updates/packaged-release-config.js +60 -0
- package/dist/updates/packaged-update-startup.d.ts +44 -0
- package/dist/updates/packaged-update-startup.js +81 -0
- package/dist/updates/update-arm.d.ts +9 -8
- package/dist/updates/update-arm.js +227 -62
- package/dist/updates/update-cleanup-error.d.ts +23 -0
- package/dist/updates/update-cleanup-error.js +65 -0
- package/dist/updates/update-download-cleanup.d.ts +11 -0
- package/dist/updates/update-download-cleanup.js +112 -2
- package/dist/updates/update-download-lock.d.ts +20 -0
- package/dist/updates/update-download-lock.js +73 -0
- package/dist/updates/update-download.d.ts +2 -0
- package/dist/updates/update-download.js +7 -3
- package/dist/updates/update-generation-lease.d.ts +6 -0
- package/dist/updates/update-generation-lease.js +14 -0
- package/dist/updates/update-helper.d.ts +11 -2
- package/dist/updates/update-helper.js +322 -13
- package/dist/updates/update-inherited-launch-lease.d.ts +22 -0
- package/dist/updates/update-inherited-launch-lease.js +58 -0
- package/dist/updates/update-instance-lease.d.ts +15 -0
- package/dist/updates/update-instance-lease.js +25 -0
- package/dist/updates/update-launch-gate.d.ts +137 -0
- package/dist/updates/update-launch-gate.js +617 -0
- package/dist/updates/update-launch.js +3 -1
- package/dist/updates/update-lock.d.ts +12 -0
- package/dist/updates/update-lock.js +18 -0
- package/dist/updates/update-manager.d.ts +2 -0
- package/dist/updates/update-manager.js +9 -2
- package/dist/updates/update-marker.d.ts +7 -0
- package/dist/updates/update-marker.js +62 -0
- package/dist/updates/update-preflight-error.d.ts +7 -0
- package/dist/updates/update-preflight-error.js +11 -0
- package/dist/updates/update-process-fence.d.ts +4 -0
- package/dist/updates/update-process-fence.js +85 -73
- package/dist/updates/update-process-liveness.d.ts +2 -0
- package/dist/updates/update-process-liveness.js +11 -0
- package/dist/updates/update-resume.d.ts +37 -3
- package/dist/updates/update-resume.js +839 -23
- package/dist/updates/update-retention-marker.d.ts +67 -0
- package/dist/updates/update-retention-marker.js +165 -0
- package/dist/updates/update-retention-plan.d.ts +25 -0
- package/dist/updates/update-retention-plan.js +54 -0
- package/dist/updates/update-retention-sync.d.ts +34 -0
- package/dist/updates/update-retention-sync.js +83 -0
- package/dist/updates/update-retirement.d.ts +205 -0
- package/dist/updates/update-retirement.js +1485 -0
- package/dist/updates/update-shutdown.d.ts +16 -4
- package/dist/updates/update-shutdown.js +133 -8
- package/dist/updates/update-state-store.d.ts +58 -0
- package/dist/updates/update-state-store.js +139 -0
- package/dist/updates/update-swap-stage.d.ts +1 -1
- package/dist/updates/update-swap-stage.js +85 -14
- package/dist/updates/update-swap.d.ts +45 -2
- package/dist/updates/update-swap.js +207 -17
- package/package.json +31 -1
- package/src/appearance/custom-theme-import.ts +96 -14
- package/src/appearance/custom-theme-library.ts +5 -0
- package/src/appearance/theme-record-adapter.ts +15 -6
- package/src/appearance/theme-registry.ts +8 -1
- package/src/desktop/client-asset-manifest.ts +207 -0
- package/src/desktop/client-asset-stream.ts +193 -32
- package/src/desktop/client-assets.ts +210 -19
- package/src/desktop/client-response-lifecycle.ts +305 -0
- package/src/native/electrobun-posix-launch-lease.patch +398 -0
- package/templates/application/README.md +50 -0
- package/templates/application/electrobun.config.ts +5 -1
- package/templates/application/scripts/build-desktop.ts +4 -0
- package/templates/application/src/bun/index.ts +173 -16
- package/templates/application/vite.config.ts +112 -6
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { posix } from 'node:path'
|
|
3
|
+
|
|
4
|
+
export const CLIENT_ASSET_MANIFEST_FILE = 'client-assets-manifest.json'
|
|
5
|
+
// Rollup filename templates are placeholders, not CSS arbitrary values.
|
|
6
|
+
// oxlint-disable-next-line shadcn/no-arbitrary-values
|
|
7
|
+
export const CLIENT_ASSET_ASSET_PATTERN = 'assets/[name]-[hash][extname]'
|
|
8
|
+
// oxlint-disable-next-line shadcn/no-arbitrary-values
|
|
9
|
+
export const CLIENT_ASSET_CHUNK_PATTERN = 'assets/[name]-[hash].js'
|
|
10
|
+
const FORMAT = 'adea-client-assets-v1'
|
|
11
|
+
|
|
12
|
+
export type ClientAssetManifest = {
|
|
13
|
+
format: typeof FORMAT
|
|
14
|
+
assets: readonly { path: string; sha256: string }[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type ViteOutput =
|
|
18
|
+
| {
|
|
19
|
+
type: 'chunk'
|
|
20
|
+
fileName: string
|
|
21
|
+
name: string
|
|
22
|
+
isEntry: boolean
|
|
23
|
+
facadeModuleId?: string | null
|
|
24
|
+
moduleIds?: readonly string[]
|
|
25
|
+
code: string
|
|
26
|
+
}
|
|
27
|
+
| { type: 'asset'; fileName: string; name?: string; source: string | Uint8Array }
|
|
28
|
+
|
|
29
|
+
type ViteOutputBundle = Readonly<Record<string, ViteOutput>>
|
|
30
|
+
|
|
31
|
+
/** Build a cache allowlist from Vite's actual output bundle and explicit Rollup names. */
|
|
32
|
+
export function createClientAssetManifest(
|
|
33
|
+
bundle: ViteOutputBundle,
|
|
34
|
+
output: {
|
|
35
|
+
assetFileNames?: unknown
|
|
36
|
+
chunkFileNames?: unknown
|
|
37
|
+
entryFileNames?: unknown
|
|
38
|
+
},
|
|
39
|
+
namingEvidence: ReadonlyMap<string, number>
|
|
40
|
+
): ClientAssetManifest {
|
|
41
|
+
const patterns = [output.assetFileNames, output.chunkFileNames, output.entryFileNames]
|
|
42
|
+
if (patterns.some((pattern) => typeof pattern !== 'string' || !pattern.includes('[hash]')))
|
|
43
|
+
throw new Error('Client asset caching requires explicit [hash] output names')
|
|
44
|
+
|
|
45
|
+
const assets: { path: string; sha256: string }[] = []
|
|
46
|
+
const paths = new Set<string>()
|
|
47
|
+
const outputEvidenceCounts = new Map<string, number>()
|
|
48
|
+
for (const item of Object.values(bundle)) {
|
|
49
|
+
const evidence =
|
|
50
|
+
item.type === 'asset' ? clientAssetAssetEvidenceKey(item) : clientAssetChunkEvidenceKey(item)
|
|
51
|
+
outputEvidenceCounts.set(evidence, (outputEvidenceCounts.get(evidence) ?? 0) + 1)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
for (const item of Object.values(bundle)) {
|
|
55
|
+
if (
|
|
56
|
+
item.fileName.toLowerCase().endsWith('.html') ||
|
|
57
|
+
item.fileName.toLowerCase().endsWith('.map')
|
|
58
|
+
)
|
|
59
|
+
continue
|
|
60
|
+
if (!item.fileName.startsWith('assets/')) continue
|
|
61
|
+
|
|
62
|
+
const pattern =
|
|
63
|
+
item.type === 'asset'
|
|
64
|
+
? (output.assetFileNames as string)
|
|
65
|
+
: ((item.isEntry ? output.entryFileNames : output.chunkFileNames) as string)
|
|
66
|
+
const evidence =
|
|
67
|
+
item.type === 'asset' ? clientAssetAssetEvidenceKey(item) : clientAssetChunkEvidenceKey(item)
|
|
68
|
+
// Ambiguous evidence can match an explicit-fileName plugin output. Exclude every match.
|
|
69
|
+
if (
|
|
70
|
+
namingEvidence.get(evidence) !== 1 ||
|
|
71
|
+
outputEvidenceCounts.get(evidence) !== 1 ||
|
|
72
|
+
!matchesConfiguredHashedName(item, pattern)
|
|
73
|
+
)
|
|
74
|
+
continue
|
|
75
|
+
|
|
76
|
+
const path = `/${item.fileName}`
|
|
77
|
+
if (paths.has(path)) throw new Error('Duplicate client asset manifest path')
|
|
78
|
+
paths.add(path)
|
|
79
|
+
assets.push({
|
|
80
|
+
path,
|
|
81
|
+
sha256: createHash('sha256')
|
|
82
|
+
.update(item.type === 'chunk' ? item.code : item.source)
|
|
83
|
+
.digest('hex'),
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
const sortedAssets = [...assets]
|
|
87
|
+
// Sort a fresh local copy; the consumer template targets ES2022.
|
|
88
|
+
// oxlint-disable-next-line unicorn/no-array-sort
|
|
89
|
+
sortedAssets.sort((left, right) => left.path.localeCompare(right.path))
|
|
90
|
+
return { format: FORMAT, assets: sortedAssets }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Evidence keys for Rollup naming callbacks; explicit `fileName` outputs never call them. */
|
|
94
|
+
export function clientAssetChunkEvidenceKey(chunk: {
|
|
95
|
+
name: string
|
|
96
|
+
isEntry: boolean
|
|
97
|
+
facadeModuleId?: string | null
|
|
98
|
+
moduleIds?: readonly string[]
|
|
99
|
+
}): string {
|
|
100
|
+
const moduleIds = [...(chunk.moduleIds ?? [])]
|
|
101
|
+
// Sort a fresh local copy; the consumer template targets ES2022.
|
|
102
|
+
// oxlint-disable-next-line unicorn/no-array-sort
|
|
103
|
+
moduleIds.sort()
|
|
104
|
+
return `chunk:${JSON.stringify([
|
|
105
|
+
chunk.name,
|
|
106
|
+
chunk.isEntry,
|
|
107
|
+
chunk.facadeModuleId ?? null,
|
|
108
|
+
moduleIds,
|
|
109
|
+
])}`
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function clientAssetAssetEvidenceKey(asset: { name?: string | null }): string {
|
|
113
|
+
return `asset:${JSON.stringify([asset.name ?? null])}`
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Parse a complete manifest. Any malformed or duplicate entry disables the whole allowlist. */
|
|
117
|
+
export function parseClientAssetManifest(input: unknown): ReadonlyMap<string, string> {
|
|
118
|
+
let value = input
|
|
119
|
+
if (typeof value === 'string') {
|
|
120
|
+
try {
|
|
121
|
+
value = JSON.parse(value)
|
|
122
|
+
} catch {
|
|
123
|
+
return new Map()
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (
|
|
127
|
+
!value ||
|
|
128
|
+
typeof value !== 'object' ||
|
|
129
|
+
(value as { format?: unknown }).format !== FORMAT ||
|
|
130
|
+
!Array.isArray((value as { assets?: unknown }).assets) ||
|
|
131
|
+
(value as { assets: unknown[] }).assets.length > 10000
|
|
132
|
+
)
|
|
133
|
+
return new Map()
|
|
134
|
+
|
|
135
|
+
const entries = new Map<string, string>()
|
|
136
|
+
for (const asset of (value as { assets: unknown[] }).assets) {
|
|
137
|
+
if (!asset || typeof asset !== 'object') return new Map()
|
|
138
|
+
const { path, sha256 } = asset as { path?: unknown; sha256?: unknown }
|
|
139
|
+
if (
|
|
140
|
+
typeof path !== 'string' ||
|
|
141
|
+
typeof sha256 !== 'string' ||
|
|
142
|
+
!isImmutableClientAssetPath(path) ||
|
|
143
|
+
!/^[a-f0-9]{64}$/.test(sha256) ||
|
|
144
|
+
entries.has(path)
|
|
145
|
+
)
|
|
146
|
+
return new Map()
|
|
147
|
+
entries.set(path, sha256)
|
|
148
|
+
}
|
|
149
|
+
return entries
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Snapshot a caller-provided allowlist and reject it as a whole if any entry is invalid. */
|
|
153
|
+
export function snapshotClientAssetAllowlist(
|
|
154
|
+
value: ReadonlyMap<string, string> | undefined
|
|
155
|
+
): ReadonlyMap<string, string> {
|
|
156
|
+
if (value === undefined) return new Map()
|
|
157
|
+
try {
|
|
158
|
+
const assets: { path: string; sha256: string }[] = []
|
|
159
|
+
for (const [path, sha256] of value) {
|
|
160
|
+
if (assets.length === 10000) return new Map()
|
|
161
|
+
assets.push({ path, sha256 })
|
|
162
|
+
}
|
|
163
|
+
return parseClientAssetManifest({ format: FORMAT, assets })
|
|
164
|
+
} catch {
|
|
165
|
+
return new Map()
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function isImmutableClientAssetPath(path: string): boolean {
|
|
170
|
+
if (
|
|
171
|
+
!path.startsWith('/assets/') ||
|
|
172
|
+
path.includes('\\') ||
|
|
173
|
+
path.includes('\0') ||
|
|
174
|
+
path.includes('%') ||
|
|
175
|
+
path.includes('?') ||
|
|
176
|
+
path.includes('#') ||
|
|
177
|
+
path.endsWith('/') ||
|
|
178
|
+
path.toLowerCase().endsWith('.map') ||
|
|
179
|
+
path.toLowerCase().endsWith('.html') ||
|
|
180
|
+
posix.normalize(path) !== path
|
|
181
|
+
)
|
|
182
|
+
return false
|
|
183
|
+
return !path
|
|
184
|
+
.slice(1)
|
|
185
|
+
.split('/')
|
|
186
|
+
.some((part) => part === '.' || part === '..' || part === '')
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Match only filenames produced by the supported explicit [name]-[hash] patterns. */
|
|
190
|
+
function matchesConfiguredHashedName(item: ViteOutput, pattern: string): boolean {
|
|
191
|
+
const name = item.name
|
|
192
|
+
if (!name || name.includes('/') || name.includes('\\')) return false
|
|
193
|
+
const baseName = item.type === 'chunk' ? name : name.replace(/\.[^.]+$/, '')
|
|
194
|
+
const prefix = `assets/${baseName}-`
|
|
195
|
+
const suffix = item.type === 'chunk' ? '.js' : extname(name)
|
|
196
|
+
const supportedPattern =
|
|
197
|
+
item.type === 'chunk' ? CLIENT_ASSET_CHUNK_PATTERN : CLIENT_ASSET_ASSET_PATTERN
|
|
198
|
+
if (pattern !== supportedPattern || !suffix) return false
|
|
199
|
+
if (!item.fileName.startsWith(prefix) || !item.fileName.endsWith(suffix)) return false
|
|
200
|
+
const hash = item.fileName.slice(prefix.length, -suffix.length)
|
|
201
|
+
return hash.length > 0 && !hash.includes('/') && !hash.includes('.')
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function extname(value: string): string {
|
|
205
|
+
const index = value.lastIndexOf('.')
|
|
206
|
+
return index <= 0 ? '' : value.slice(index)
|
|
207
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer'
|
|
2
|
+
import { createHash } from 'node:crypto'
|
|
2
3
|
import type { Stats } from 'node:fs'
|
|
3
4
|
import { open, type FileHandle } from 'node:fs/promises'
|
|
4
5
|
|
|
@@ -15,6 +16,109 @@ function sameFile(expected: Stats, actual: Stats): boolean {
|
|
|
15
16
|
)
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
export type ClientAssetVerificationCache = {
|
|
20
|
+
has(path: string, metadata: Stats, sha256: string): boolean
|
|
21
|
+
remember(path: string, metadata: Stats, sha256: string): void
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const pendingVerifications = new WeakMap<
|
|
25
|
+
ClientAssetVerificationCache,
|
|
26
|
+
Map<string, Promise<boolean>>
|
|
27
|
+
>()
|
|
28
|
+
|
|
29
|
+
/** Share concurrent checks only for the same cache, file identity, and expected digest. */
|
|
30
|
+
export async function verifyClientAssetCached(
|
|
31
|
+
cache: ClientAssetVerificationCache,
|
|
32
|
+
path: string,
|
|
33
|
+
metadata: Stats,
|
|
34
|
+
sha256: string,
|
|
35
|
+
verify: () => Promise<boolean>
|
|
36
|
+
): Promise<boolean> {
|
|
37
|
+
if (cache.has(path, metadata, sha256)) return true
|
|
38
|
+
let pending = pendingVerifications.get(cache)
|
|
39
|
+
if (!pending) {
|
|
40
|
+
pending = new Map()
|
|
41
|
+
pendingVerifications.set(cache, pending)
|
|
42
|
+
}
|
|
43
|
+
const key = JSON.stringify([
|
|
44
|
+
path,
|
|
45
|
+
metadata.dev,
|
|
46
|
+
metadata.ino,
|
|
47
|
+
metadata.size,
|
|
48
|
+
metadata.mtimeMs,
|
|
49
|
+
metadata.ctimeMs,
|
|
50
|
+
sha256,
|
|
51
|
+
])
|
|
52
|
+
const existing = pending.get(key)
|
|
53
|
+
if (existing) return existing
|
|
54
|
+
const tasks = pending
|
|
55
|
+
const task = Promise.resolve()
|
|
56
|
+
.then(verify)
|
|
57
|
+
.then((matched) => {
|
|
58
|
+
if (matched) cache.remember(path, metadata, sha256)
|
|
59
|
+
return matched
|
|
60
|
+
})
|
|
61
|
+
.finally(() => {
|
|
62
|
+
tasks.delete(key)
|
|
63
|
+
if (tasks.size === 0) pendingVerifications.delete(cache)
|
|
64
|
+
})
|
|
65
|
+
tasks.set(key, task)
|
|
66
|
+
return task
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type VerifiedAssetIdentity = {
|
|
70
|
+
dev: number
|
|
71
|
+
ino: number
|
|
72
|
+
size: number
|
|
73
|
+
mtimeMs: number
|
|
74
|
+
ctimeMs: number
|
|
75
|
+
sha256: string
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Keep successful content checks only while the file identity remains unchanged. */
|
|
79
|
+
export function createClientAssetVerificationCache(maxEntries = 256): ClientAssetVerificationCache {
|
|
80
|
+
if (!Number.isInteger(maxEntries) || maxEntries < 1 || maxEntries > 10_000)
|
|
81
|
+
throw new Error('Invalid client asset verification cache size')
|
|
82
|
+
|
|
83
|
+
const entries = new Map<string, VerifiedAssetIdentity>()
|
|
84
|
+
const sameIdentity = (expected: VerifiedAssetIdentity, actual: Stats, sha256: string) =>
|
|
85
|
+
actual.isFile() &&
|
|
86
|
+
expected.dev === actual.dev &&
|
|
87
|
+
expected.ino === actual.ino &&
|
|
88
|
+
expected.size === actual.size &&
|
|
89
|
+
expected.mtimeMs === actual.mtimeMs &&
|
|
90
|
+
expected.ctimeMs === actual.ctimeMs &&
|
|
91
|
+
expected.sha256 === sha256
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
has(path, metadata, sha256) {
|
|
95
|
+
const entry = entries.get(path)
|
|
96
|
+
if (!entry) return false
|
|
97
|
+
if (!sameIdentity(entry, metadata, sha256)) {
|
|
98
|
+
entries.delete(path)
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
// Refresh this successful identity's position in the bounded LRU.
|
|
102
|
+
entries.delete(path)
|
|
103
|
+
entries.set(path, entry)
|
|
104
|
+
return true
|
|
105
|
+
},
|
|
106
|
+
remember(path, metadata, sha256) {
|
|
107
|
+
if (!metadata.isFile() || !/^[a-f0-9]{64}$/.test(sha256)) return
|
|
108
|
+
entries.delete(path)
|
|
109
|
+
entries.set(path, {
|
|
110
|
+
dev: metadata.dev,
|
|
111
|
+
ino: metadata.ino,
|
|
112
|
+
size: metadata.size,
|
|
113
|
+
mtimeMs: metadata.mtimeMs,
|
|
114
|
+
ctimeMs: metadata.ctimeMs,
|
|
115
|
+
sha256,
|
|
116
|
+
})
|
|
117
|
+
while (entries.size > maxEntries) entries.delete(entries.keys().next().value!)
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
18
122
|
type AssetFile = Pick<FileHandle, 'close' | 'stat' | 'read'>
|
|
19
123
|
type OpenAsset = (path: string, flags: 'r') => Promise<AssetFile>
|
|
20
124
|
|
|
@@ -30,6 +134,9 @@ export function createClientAssetStream(
|
|
|
30
134
|
let file: AssetFile | undefined
|
|
31
135
|
let opening: Promise<void> | undefined
|
|
32
136
|
let closing: Promise<void> | undefined
|
|
137
|
+
let pulling: Promise<void> | undefined
|
|
138
|
+
let closeFailed = false
|
|
139
|
+
let closeFailure: unknown
|
|
33
140
|
let cancelled = false
|
|
34
141
|
let offset = 0
|
|
35
142
|
const close = (): Promise<void> => {
|
|
@@ -37,7 +144,11 @@ export function createClientAssetStream(
|
|
|
37
144
|
const current = file
|
|
38
145
|
file = undefined
|
|
39
146
|
if (!current) return Promise.resolve()
|
|
40
|
-
const pending = current.close()
|
|
147
|
+
const pending = current.close().catch((error: unknown) => {
|
|
148
|
+
closeFailed = true
|
|
149
|
+
closeFailure = error
|
|
150
|
+
throw error
|
|
151
|
+
})
|
|
41
152
|
closing = pending
|
|
42
153
|
const clear = () => {
|
|
43
154
|
if (closing === pending) closing = undefined
|
|
@@ -51,7 +162,13 @@ export function createClientAssetStream(
|
|
|
51
162
|
const task = (async () => {
|
|
52
163
|
const opened = await openAsset(path, 'r')
|
|
53
164
|
if (cancelled) {
|
|
54
|
-
|
|
165
|
+
try {
|
|
166
|
+
await opened.close()
|
|
167
|
+
} catch (error) {
|
|
168
|
+
closeFailed = true
|
|
169
|
+
closeFailure = error
|
|
170
|
+
throw error
|
|
171
|
+
}
|
|
55
172
|
return
|
|
56
173
|
}
|
|
57
174
|
file = opened
|
|
@@ -70,49 +187,93 @@ export function createClientAssetStream(
|
|
|
70
187
|
}
|
|
71
188
|
return new ReadableStream<Uint8Array>(
|
|
72
189
|
{
|
|
73
|
-
|
|
190
|
+
pull(controller) {
|
|
74
191
|
if (cancelled) return
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const current = file
|
|
79
|
-
if (!current) return
|
|
80
|
-
if (offset === expected.size) {
|
|
81
|
-
const actual = await current.stat()
|
|
192
|
+
const task = (async () => {
|
|
193
|
+
try {
|
|
194
|
+
await ensureOpen()
|
|
82
195
|
if (cancelled) return
|
|
83
|
-
|
|
84
|
-
|
|
196
|
+
const current = file
|
|
197
|
+
if (!current) return
|
|
198
|
+
if (offset === expected.size) {
|
|
199
|
+
const actual = await current.stat()
|
|
200
|
+
if (cancelled) return
|
|
201
|
+
if (!sameFile(expected, actual))
|
|
202
|
+
throw new Error('Client asset changed during request')
|
|
203
|
+
await close()
|
|
204
|
+
if (cancelled) return
|
|
205
|
+
controller.close()
|
|
206
|
+
return
|
|
207
|
+
}
|
|
208
|
+
const buffer = Buffer.allocUnsafe(Math.min(FILE_CHUNK_BYTES, expected.size - offset))
|
|
209
|
+
const { bytesRead } = await current.read(buffer, 0, buffer.length, offset)
|
|
85
210
|
if (cancelled) return
|
|
86
|
-
|
|
87
|
-
|
|
211
|
+
if (!bytesRead) throw new Error('Client asset changed during request')
|
|
212
|
+
offset += bytesRead
|
|
213
|
+
if (offset === expected.size) {
|
|
214
|
+
const actual = await current.stat()
|
|
215
|
+
if (cancelled) return
|
|
216
|
+
if (!sameFile(expected, actual))
|
|
217
|
+
throw new Error('Client asset changed during request')
|
|
218
|
+
await close()
|
|
219
|
+
if (cancelled) return
|
|
220
|
+
controller.enqueue(buffer.subarray(0, bytesRead))
|
|
221
|
+
controller.close()
|
|
222
|
+
} else controller.enqueue(buffer.subarray(0, bytesRead))
|
|
223
|
+
} catch (error) {
|
|
224
|
+
await close().catch(() => {})
|
|
225
|
+
if (!cancelled) controller.error(error)
|
|
88
226
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const actual = await current.stat()
|
|
96
|
-
if (cancelled) return
|
|
97
|
-
if (!sameFile(expected, actual)) throw new Error('Client asset changed during request')
|
|
98
|
-
await close()
|
|
99
|
-
if (cancelled) return
|
|
100
|
-
controller.enqueue(buffer.subarray(0, bytesRead))
|
|
101
|
-
controller.close()
|
|
102
|
-
} else controller.enqueue(buffer.subarray(0, bytesRead))
|
|
103
|
-
} catch (error) {
|
|
104
|
-
await close().catch(() => {})
|
|
105
|
-
if (!cancelled) controller.error(error)
|
|
106
|
-
}
|
|
227
|
+
})()
|
|
228
|
+
const pending = task.finally(() => {
|
|
229
|
+
if (pulling === pending) pulling = undefined
|
|
230
|
+
})
|
|
231
|
+
pulling = pending
|
|
232
|
+
return pending
|
|
107
233
|
},
|
|
108
234
|
async cancel() {
|
|
109
235
|
cancelled = true
|
|
110
236
|
const pending = opening
|
|
111
237
|
if (pending) await pending.catch(() => {})
|
|
238
|
+
// A read or final stat can still be using the file after it opened.
|
|
239
|
+
// Do not report cancellation complete until that operation settles.
|
|
240
|
+
const activePull = pulling
|
|
241
|
+
if (activePull) await activePull.catch(() => {})
|
|
112
242
|
await close()
|
|
243
|
+
if (closeFailed) throw closeFailure
|
|
113
244
|
},
|
|
114
245
|
},
|
|
115
246
|
// No prefetch: an unconsumed response neither reads the asset nor holds an fd.
|
|
116
247
|
{ highWaterMark: 0 }
|
|
117
248
|
)
|
|
118
249
|
}
|
|
250
|
+
|
|
251
|
+
/** Verify an opted-in asset before sending immutable cache headers without buffering it. */
|
|
252
|
+
export async function verifyClientAssetDigest(
|
|
253
|
+
path: string,
|
|
254
|
+
expected: Stats,
|
|
255
|
+
expectedSha256: string,
|
|
256
|
+
openAsset: OpenAsset = open
|
|
257
|
+
): Promise<boolean> {
|
|
258
|
+
const file = await openAsset(path, 'r')
|
|
259
|
+
try {
|
|
260
|
+
if (!sameFile(expected, await file.stat())) return false
|
|
261
|
+
const hash = createHash('sha256')
|
|
262
|
+
const buffer = Buffer.allocUnsafe(FILE_CHUNK_BYTES)
|
|
263
|
+
let offset = 0
|
|
264
|
+
while (offset < expected.size) {
|
|
265
|
+
const { bytesRead } = await file.read(
|
|
266
|
+
buffer,
|
|
267
|
+
0,
|
|
268
|
+
Math.min(buffer.length, expected.size - offset),
|
|
269
|
+
offset
|
|
270
|
+
)
|
|
271
|
+
if (!bytesRead) return false
|
|
272
|
+
hash.update(buffer.subarray(0, bytesRead))
|
|
273
|
+
offset += bytesRead
|
|
274
|
+
}
|
|
275
|
+
return sameFile(expected, await file.stat()) && hash.digest('hex') === expectedSha256
|
|
276
|
+
} finally {
|
|
277
|
+
await file.close()
|
|
278
|
+
}
|
|
279
|
+
}
|