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

Commit 63051db

Browse files
committed
refactor: improve fetchModule method
1 parent 3f73035 commit 63051db

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

server/app.ts

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import { clearCompilation, computeHash, createHtml, formatBytesWithColor, getAle
1717
/** A module includes the compilation details. */
1818
export type Module = {
1919
url: string
20-
hash: string
21-
sourceHash: string
2220
deps: DependencyDescriptor[]
21+
sourceHash: string
22+
hash: string
2323
jsFile: string
2424
}
2525

@@ -90,16 +90,19 @@ export class Application implements ServerApplication {
9090
try {
9191
const bm = JSON.parse(await Deno.readTextFile(buildManifestFile))
9292
shouldRebuild = bm.compiler !== buildChecksum
93-
if (shouldRebuild) {
94-
log.debug('rebuild...')
95-
}
9693
} catch (e) { }
9794
}
95+
if (shouldRebuild) {
96+
log.debug('rebuild...')
97+
ensureTextFile(buildManifestFile, JSON.stringify({
98+
aleph: VERSION,
99+
compiler: buildChecksum,
100+
deno: Deno.version.deno,
101+
}, undefined, 2))
102+
}
98103

104+
this.#reloading = reload
99105
if (reload || shouldRebuild) {
100-
if (reload && !shouldRebuild) {
101-
this.#reloading = true
102-
}
103106
if (existsDirSync(this.buildDir)) {
104107
await Deno.remove(this.buildDir, { recursive: true })
105108
}
@@ -201,12 +204,6 @@ export class Application implements ServerApplication {
201204
this.#reloading = false
202205
}
203206

204-
ensureTextFile(buildManifestFile, JSON.stringify({
205-
aleph: VERSION,
206-
compiler: buildChecksum,
207-
deno: Deno.version.deno,
208-
}))
209-
210207
log.debug(`init project in ${Math.round(performance.now() - t)}ms`)
211208

212209
if (this.isDev) {
@@ -614,9 +611,9 @@ export class Application implements ServerApplication {
614611
} else {
615612
mod = {
616613
url,
617-
hash: '',
618-
sourceHash: '',
619614
deps: [],
615+
sourceHash: '',
616+
hash: '',
620617
jsFile: '',
621618
}
622619
if (!once) {
@@ -650,39 +647,27 @@ export class Application implements ServerApplication {
650647
mod.sourceHash = sourceHash
651648
shouldCompile = true
652649
}
653-
} else if (isRemote) {
650+
} else {
654651
let shouldFetch = true
655-
if (mod.sourceHash !== '' && !url.startsWith('http://localhost:')) {
652+
if (
653+
!this.#reloading &&
654+
(isRemote && !url.startsWith('http://localhost:')) &&
655+
mod.sourceHash !== ''
656+
) {
656657
const jsFile = util.cleanPath(saveDir + '/' + name + '.js')
657658
if (existsFileSync(jsFile)) {
658659
shouldFetch = false
659660
}
660661
}
661662
if (shouldFetch) {
662-
const [content, headers] = await this.fetchModule(url)
663+
const { content, contentType: ctype } = await this.fetchModule(url)
663664
const sourceHash = computeHash(content)
664665
sourceContent = content
665-
contentType = headers.get('content-type')
666-
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
667-
mod.sourceHash = sourceHash
668-
shouldCompile = true
669-
}
670-
}
671-
} else {
672-
const filepath = path.join(this.srcDir, url)
673-
try {
674-
sourceContent = await Deno.readFile(filepath)
675-
const sourceHash = computeHash(sourceContent)
666+
contentType = ctype
676667
if (mod.sourceHash === '' || mod.sourceHash !== sourceHash) {
677668
mod.sourceHash = sourceHash
678669
shouldCompile = true
679670
}
680-
} catch (err) {
681-
if (err instanceof Deno.errors.NotFound) {
682-
log.error(`local module '${url}' not found`)
683-
return mod
684-
}
685-
throw err
686671
}
687672
}
688673

@@ -826,7 +811,7 @@ export class Application implements ServerApplication {
826811
url,
827812
sourceHash: mod.sourceHash,
828813
deps: mod.deps,
829-
}, undefined, 4)),
814+
}, undefined, 2)),
830815
])
831816
}
832817

@@ -952,8 +937,14 @@ export class Application implements ServerApplication {
952937
}
953938
}
954939

955-
/** fetch remote module content */
956-
async fetchModule(url: string): Promise<[Uint8Array, Headers]> {
940+
/** fetch module content */
941+
async fetchModule(url: string): Promise<{ content: Uint8Array, contentType: string | null }> {
942+
if (!util.isLikelyHttpURL(url)) {
943+
const filepath = path.join(this.srcDir, util.trimPrefix(url, 'file://'))
944+
const content = await Deno.readFile(filepath)
945+
return { content, contentType: null }
946+
}
947+
957948
const u = new URL(url)
958949
if (url.startsWith('https://esm.sh/')) {
959950
if (this.isDev && !u.searchParams.has('dev')) {
@@ -983,10 +974,10 @@ export class Application implements ServerApplication {
983974
])
984975
try {
985976
const { headers } = JSON.parse(meta)
986-
return [
977+
return {
987978
content,
988-
new Headers(headers)
989-
]
979+
contentType: headers['content-type'] || null
980+
}
990981
} catch (e) { }
991982
}
992983

@@ -1017,9 +1008,12 @@ export class Application implements ServerApplication {
10171008
return m
10181009
}, {} as Record<string, string>),
10191010
url
1020-
}, undefined, 4))
1011+
}, undefined, 2))
1012+
}
1013+
return {
1014+
content,
1015+
contentType: resp.headers.get('content-type')
10211016
}
1022-
return [content, resp.headers]
10231017
} catch (e) {
10241018
err = e
10251019
}

0 commit comments

Comments
 (0)