Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 7b32a00

Browse files
committed
refactor(server): download dep when deno cache failed
1 parent 1d74ba3 commit 7b32a00

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

server/app.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,12 @@ export class Appliaction {
516516
}
517517

518518
const p = Deno.run({
519-
cmd: [Deno.execPath(), 'info'],
519+
cmd: [Deno.execPath(), 'info', '--unstable', '--json'],
520520
stdout: 'piped',
521521
stderr: 'null'
522522
})
523-
this.#denoCacheDir = (new TextDecoder).decode(await p.output()).split('"')[1]
523+
const output = (new TextDecoder).decode(await p.output())
524+
this.#denoCacheDir = JSON.parse(output).denoDir
524525
p.close()
525526
if (!existsDirSync(this.#denoCacheDir)) {
526527
log.fatal('invalid deno cache dir')
@@ -937,7 +938,7 @@ export class Appliaction {
937938
const isLocalhost = /^https?:\/\/localhost(:\d+)?\//.test(url)
938939
if (['js', 'ts', 'jsx', 'tsx'].includes(mod.loader) && !isLocalhost) {
939940
try {
940-
sourceContent = await this.loadDependency(url)
941+
sourceContent = await this.fetchDependency(url)
941942
const sourceHash = (new Sha1).update(sourceContent).hex()
942943
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
943944
mod.sourceHash = sourceHash
@@ -949,7 +950,7 @@ export class Appliaction {
949950
return mod
950951
}
951952
} else {
952-
// cache non-localhost file to local drive
953+
// todo: cache non-localhost file to local drive
953954
try {
954955
if (!isLocalhost) {
955956
log.info('Download', url)
@@ -1246,44 +1247,46 @@ export class Appliaction {
12461247
})
12471248
}
12481249

1249-
/** load dependency content, use deno builtin cache system */
1250-
private async loadDependency(url: string): Promise<Uint8Array> {
1250+
/** fetch dependency content, use deno builtin cache system */
1251+
private async fetchDependency(url: string): Promise<Uint8Array> {
12511252
const u = new URL(url)
12521253
if (url.startsWith('https://esm.sh/')) {
12531254
if (this.isDev && !u.searchParams.has('dev')) {
12541255
u.searchParams.set('dev', '')
1256+
u.search = u.search.replace('dev=', 'dev')
12551257
}
1256-
u.search = u.search.replace(/\=(&|$)/, '$1')
12571258
}
12581259

12591260
const { protocol, hostname, port, pathname, search } = u
12601261
const versioned = reFullVersion.test(pathname)
1261-
const dir = path.join(this.#denoCacheDir, 'deps', util.trimSuffix(protocol, ':'), hostname + (port ? '_PORT' + port : ''))
1262-
const filename = path.join(dir, (new Sha256()).update(pathname + search).hex())
1262+
const reload = this.#reloading || !versioned
1263+
const cacheDir = path.join(this.#denoCacheDir, 'deps', util.trimSuffix(protocol, ':'), hostname + (port ? '_PORT' + port : ''))
1264+
const cacheFilename = path.join(cacheDir, (new Sha256()).update(pathname + search).hex())
12631265

1264-
if (versioned && !this.#reloading && existsFileSync(filename)) {
1265-
return await Deno.readFile(filename)
1266+
if (!reload && existsFileSync(cacheFilename)) {
1267+
return await Deno.readFile(cacheFilename)
12661268
}
12671269

1268-
const p = Deno.run({
1269-
cmd: [
1270-
Deno.execPath(),
1271-
'cache',
1272-
this.#reloading || !versioned ? '--reload' : '',
1273-
u.toString()
1274-
].filter(Boolean),
1275-
stdout: 'piped',
1276-
stderr: 'piped'
1277-
})
1278-
await Deno.stderr.write(await p.output())
1279-
await Deno.stderr.write(await p.stderrOutput())
1280-
p.close()
1281-
1282-
if (existsFileSync(filename)) {
1283-
return await Deno.readFile(filename)
1284-
} else {
1285-
throw new Error(`not found`)
1270+
try {
1271+
log.info('Download', url)
1272+
const p = Deno.run({
1273+
cmd: [Deno.execPath(), 'cache', reload ? '--reload' : '', u.toString()],
1274+
stdout: 'null',
1275+
stderr: 'null'
1276+
})
1277+
await p.status()
1278+
p.close()
1279+
if (existsFileSync(cacheFilename)) {
1280+
return await Deno.readFile(cacheFilename)
1281+
}
1282+
} catch (e) {
1283+
log.warn(e)
12861284
}
1285+
1286+
// download dep when deno cache failed
1287+
log.info('Redownload', url)
1288+
const buffer = await fetch(u.toString()).then(resp => resp.arrayBuffer())
1289+
return await Deno.readAll(new Deno.Buffer(buffer))
12871290
}
12881291

12891292
/** bundle modules for production. */
@@ -1388,7 +1391,7 @@ export class Appliaction {
13881391
}).flat().join('\n')
13891392
const mod = this.newModule(`/${name}.js`)
13901393
const hash = (new Sha1).update(buildChecksum).update(bundlingCode).hex()
1391-
const bundlingFile = path.join(this.buildDir, mod.url)
1394+
const bundlingFile = path.join(this.buildDir, `${name}.bundling.js`)
13921395
const bundleFile = path.join(this.buildDir, `${name}.bundle.${util.shortHash(hash)}.js`)
13931396

13941397
if (!existsFileSync(bundleFile)) {
@@ -1445,7 +1448,7 @@ export class Appliaction {
14451448
})
14461449

14471450
// workaround for https://github.com/denoland/deno/issues/9212
1448-
if (Deno.version.deno === '1.7.0' && bundlingFile.endsWith('/deps.bundling.js')) {
1451+
if (Deno.version.deno === '1.7.0' && bundlingFile.endsWith('deps.bundling.js')) {
14491452
code = code.replace(' _ = l.baseState, ', ' var _ = l.baseState, ')
14501453
}
14511454

0 commit comments

Comments
 (0)