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

Commit 487c2c0

Browse files
author
Je
committed
fix: fix server Broken pipe error
1 parent 9f6d6a3 commit 487c2c0

File tree

1 file changed

+20
-53
lines changed

1 file changed

+20
-53
lines changed

server.ts

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import log from './log.ts'
33
import { getContentType } from './mime.ts'
44
import { injectHmr, Project } from './project.ts'
55
import { path, serve, ws } from './std.ts'
6-
import util, { hashShort } from './util.ts'
6+
import util, { existsFileSync, hashShort } from './util.ts'
77

88
export async function start(appDir: string, port: number, isDev = false) {
99
const project = new Project(appDir, isDev ? 'development' : 'production')
@@ -69,22 +69,15 @@ export async function start(appDir: string, port: number, isDev = false) {
6969
// serve dist files
7070
if (pathname.startsWith('/_aleph/')) {
7171
if (pathname.endsWith('.css')) {
72-
try {
73-
const filePath = path.join(project.buildDir, util.trimPrefix(pathname, '/_aleph/'))
74-
const info = await Deno.lstat(filePath)
75-
if (!info.isDirectory) {
76-
const body = await Deno.readFile(filePath)
77-
req.respond({
78-
status: 200,
79-
headers: new Headers({ 'Content-Type': 'text/css; charset=utf-8' }),
80-
body
81-
})
82-
continue
83-
}
84-
} catch (err) {
85-
if (!(err instanceof Deno.errors.NotFound)) {
86-
throw err
87-
}
72+
const filePath = path.join(project.buildDir, util.trimPrefix(pathname, '/_aleph/'))
73+
if (existsFileSync(filePath)) {
74+
const body = await Deno.readFile(filePath)
75+
req.respond({
76+
status: 200,
77+
headers: new Headers({ 'Content-Type': 'text/css; charset=utf-8' }),
78+
body
79+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
80+
continue
8881
}
8982
} else {
9083
const reqSourceMap = pathname.endsWith('.js.map')
@@ -125,47 +118,21 @@ export async function start(appDir: string, port: number, isDev = false) {
125118
'ETag': mod.hash
126119
}),
127120
body
128-
})
121+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
129122
continue
130123
}
131124
}
132-
req.respond({
133-
status: 404,
134-
headers: new Headers({ 'Content-Type': 'text/html' }),
135-
body: createHtml({
136-
lang: 'en',
137-
head: ['<title>404 - not found</title>'],
138-
body: '<p><strong><code>404</code></strong><small> - </small><span>not found</span></p>'
139-
})
140-
})
141-
continue
142125
}
143126

144127
// serve public files
145-
try {
146-
const filePath = path.join(project.appRoot, 'public', pathname)
147-
const info = await Deno.lstat(filePath)
148-
if (!info.isDirectory) {
149-
const body = await Deno.readFile(filePath)
150-
req.respond({
151-
status: 200,
152-
headers: new Headers({ 'Content-Type': getContentType(filePath) }),
153-
body
154-
})
155-
continue
156-
}
157-
} catch (err) {
158-
if (!(err instanceof Deno.errors.NotFound)) {
159-
throw err
160-
}
161-
}
162-
163-
if (pathname === '/favicon.ico') {
128+
const filePath = path.join(project.appRoot, 'public', pathname)
129+
if (existsFileSync(filePath)) {
130+
const body = await Deno.readFile(filePath)
164131
req.respond({
165-
status: 404,
166-
headers: new Headers({ 'Content-Type': 'text/plain' }),
167-
body: 'icon not found'
168-
})
132+
status: 200,
133+
headers: new Headers({ 'Content-Type': getContentType(filePath) }),
134+
body
135+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
169136
continue
170137
}
171138

@@ -175,7 +142,7 @@ export async function start(appDir: string, port: number, isDev = false) {
175142
status,
176143
headers: new Headers({ 'Content-Type': 'text/html' }),
177144
body: html
178-
})
145+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
179146
} catch (err) {
180147
req.respond({
181148
status: 500,
@@ -185,7 +152,7 @@ export async function start(appDir: string, port: number, isDev = false) {
185152
head: ['<title>500 - internal server error</title>'],
186153
body: `<p><strong><code>500</code></strong><small> - </small><span>${err.message}</span></p>`
187154
})
188-
})
155+
}).catch(err => log.warn('ServerRequest.respond:', err.message))
189156
}
190157
}
191158
} catch (err) {

0 commit comments

Comments
 (0)