Skip to content

Commit 88c8929

Browse files
author
X
authored
Merge pull request #39 from masataka/fix-#31
Fix: Lack of URL encoding processing #31
2 parents fd6627c + 0a74001 commit 88c8929

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
Thumbs.db
33
.aleph/
44
dist/
5+
6+
.vscode/launch.json

project.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export class Project {
221221
}
222222

223223
async callAPI(req: ServerRequest, loc: { pathname: string, search?: string }): Promise<APIHandler | null> {
224-
const [url] = this.#apiRouting.createRouter(loc)
224+
const [url] = this.#apiRouting.createRouter({ ...loc, pathname: decodeURI(loc.pathname) })
225225
if (url.pagePath != '') {
226226
const moduleID = url.pagePath + '.js'
227227
if (this.#modules.has(moduleID)) {
@@ -1555,7 +1555,8 @@ function fixImportUrl(importUrl: string): string {
15551555
if (isRemote) {
15561556
return '/-/' + url.hostname + (url.port ? '/' + url.port : '') + pathname + ext
15571557
}
1558-
return pathname + ext
1558+
const result = pathname + ext
1559+
return !isRemote && importUrl.startsWith('/api/') ? decodeURI(result) : result;
15591560
}
15601561

15611562
/** get hash(sha1) of the content, mix current aleph.js version when the second parameter is `true` */

routing.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ export class Routing {
176176
}
177177

178178
export function getPagePath(moduleId: string): string {
179-
return util.trimSuffix(moduleId, '.js').replace(reMDExt, '').toLowerCase().replace(/\s+/g, '-').replace(/^\/pages\//, '/').replace(/\/?index$/, '/')
179+
const id = util.trimSuffix(moduleId, '.js').replace(reMDExt, '').toLowerCase().replace(/^\/pages\//, '/').replace(/\/?index$/, '/')
180+
return id.startsWith('/api/') ? id : id.replace(/\s+/g, '-')
180181
}
181182

182183
function matchPath(routePath: string, locPath: string): [Record<string, string>, boolean] {

server.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ export async function start(appDir: string, port: number, isDev = false, reload
6363
continue
6464
}
6565

66+
// serve public files
67+
const filePath = path.join(project.appRoot, 'public', decodeURI(pathname))
68+
if (existsFileSync(filePath)) {
69+
const info = Deno.lstatSync(filePath)
70+
const lastModified = info.mtime?.toUTCString() ?? new Date().toUTCString()
71+
if (lastModified === req.headers.get('If-Modified-Since')) {
72+
resp.status(304).send('')
73+
continue
74+
}
75+
76+
const body = Deno.readFileSync(filePath)
77+
resp.setHeader('Last-Modified', lastModified)
78+
resp.send(body, getContentType(filePath))
79+
continue
80+
}
81+
6682
// serve APIs
6783
if (pathname.startsWith('/api/')) {
6884
project.callAPI(req, { pathname, search: url.search })
@@ -113,21 +129,6 @@ export async function start(appDir: string, port: number, isDev = false, reload
113129
}
114130
}
115131

116-
// serve public files
117-
const filePath = path.join(project.appRoot, 'public', pathname)
118-
if (existsFileSync(filePath)) {
119-
const info = await Deno.lstat(filePath)
120-
if (info.mtime?.toUTCString() === req.headers.get('If-Modified-Since')) {
121-
resp.status(304).send('')
122-
continue
123-
}
124-
125-
const body = await Deno.readFile(filePath)
126-
resp.setHeader('Last-Modified', info.mtime!.toUTCString())
127-
resp.send(body, getContentType(filePath))
128-
continue
129-
}
130-
131132
// ssr
132133
const [status, html] = await project.getPageHtml({ pathname, search: url.search })
133134
resp.status(status).send(html, 'text/html; charset=utf-8')

0 commit comments

Comments
 (0)