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

Commit db14980

Browse files
committed
refactor(cli): move localproxy to a module
1 parent 947b416 commit db14980

File tree

2 files changed

+57
-44
lines changed

2 files changed

+57
-44
lines changed

cli.ts

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { listenAndServe, path, walk } from './deps.ts'
2-
import { Request } from './server/api.ts'
3-
import { getContentType } from './server/mime.ts'
4-
import { createHtml } from './server/util.ts'
1+
import { path, walk } from './deps.ts'
2+
import { localProxy } from './server/localproxy.ts'
53
import { existsDirSync } from './shared/fs.ts'
64
import type { LevelNames } from './shared/log.ts'
75
import log from './shared/log.ts'
86
import util from './shared/util.ts'
9-
import type { ServerRequest } from './types.ts'
107
import { VERSION } from './version.ts'
118

129
const commands = {
@@ -131,45 +128,8 @@ async function main() {
131128
log.debug('load env from', path.basename(p))
132129
}
133130

134-
// proxy https://deno.land/x/aleph for aleph.js dev
135-
const p = Deno.env.get('ALEPH_DEV_PORT')
136-
if (p && !/^\d+$/.test(p)) {
137-
log.fatal('invalid ALEPH_DEV_PORT:', p)
138-
}
139-
if (p) {
140-
const cwd = Deno.cwd()
141-
const port = parseInt(p)
142-
listenAndServe({ port }, async (req: ServerRequest) => {
143-
const url = new URL('http://localhost' + req.url)
144-
const resp = new Request(req, util.cleanPath(url.pathname), {}, url.searchParams)
145-
const filepath = path.join(cwd, url.pathname)
146-
try {
147-
const info = await Deno.lstat(filepath)
148-
if (info.isDirectory) {
149-
const r = Deno.readDir(filepath)
150-
const items: string[] = []
151-
for await (const item of r) {
152-
if (!item.name.startsWith('.')) {
153-
items.push(`<li><a href='${path.join(url.pathname, encodeURI(item.name))}'>${item.name}${item.isDirectory ? '/' : ''}<a></li>`)
154-
}
155-
}
156-
resp.send(createHtml({
157-
head: [`<title>aleph.js/</title>`],
158-
body: `<h1>&nbsp;aleph.js/</h1><ul>${Array.from(items).join('')}</ul>`
159-
}), 'text/html')
160-
return
161-
}
162-
resp.send(await Deno.readFile(filepath), getContentType(filepath))
163-
} catch (err) {
164-
if (err instanceof Deno.errors.NotFound) {
165-
resp.status(404).send('file not found')
166-
return
167-
}
168-
resp.status(500).send(err.message)
169-
}
170-
})
171-
log.info(`Proxy https://deno.land/x/aleph on http://localhost:${port}`)
172-
}
131+
// proxy https://deno.land/x/aleph on localhost
132+
localProxy()
173133

174134
const { default: cmd } = await import(`./cli/${command}.ts`)
175135
switch (command) {

server/localproxy.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { path, serve } from '../deps.ts'
2+
import log from '../shared/log.ts'
3+
import util from '../shared/util.ts'
4+
import { Request } from './api.ts'
5+
import { getContentType } from './mime.ts'
6+
import { createHtml } from './util.ts'
7+
8+
/** proxy https://deno.land/x/aleph on localhost */
9+
export async function localProxy() {
10+
const p = Deno.env.get('ALEPH_DEV_PORT')
11+
if (!p) {
12+
return
13+
}
14+
15+
if (!/^\d+$/.test(p)) {
16+
log.fatal('invalid ALEPH_DEV_PORT:', p)
17+
}
18+
19+
const cwd = Deno.cwd()
20+
const port = parseInt(p)
21+
const s = serve({ port })
22+
23+
log.info(`Proxy https://deno.land/x/aleph on http://localhost:${port}`)
24+
for await (const r of s) {
25+
const url = new URL('http://localhost' + r.url)
26+
const resp = new Request(r, util.cleanPath(url.pathname), {}, url.searchParams)
27+
const filepath = path.join(cwd, url.pathname)
28+
try {
29+
const info = await Deno.lstat(filepath)
30+
if (info.isDirectory) {
31+
const r = Deno.readDir(filepath)
32+
const items: string[] = []
33+
for await (const item of r) {
34+
if (!item.name.startsWith('.')) {
35+
items.push(`<li><a href='${path.join(url.pathname, encodeURI(item.name))}'>${item.name}${item.isDirectory ? '/' : ''}<a></li>`)
36+
}
37+
}
38+
resp.send(createHtml({
39+
head: [`<title>aleph.js/</title>`],
40+
body: `<h1>&nbsp;aleph.js/</h1><ul>${Array.from(items).join('')}</ul>`
41+
}), 'text/html')
42+
return
43+
}
44+
resp.send(await Deno.readFile(filepath), getContentType(filepath))
45+
} catch (err) {
46+
if (err instanceof Deno.errors.NotFound) {
47+
resp.status(404).send('file not found')
48+
return
49+
}
50+
resp.status(500).send(err.message)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)