forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (169 loc) · 5 KB
/
Copy pathindex.js
File metadata and controls
196 lines (169 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import http from 'http'
import { resolve, join } from 'path'
import { parse } from 'url'
import send from 'send'
import Router from './router'
import { render, renderJSON, errorToJSON } from './render'
import HotReloader from './hot-reloader'
import { resolveFromList } from './resolve'
export default class Server {
constructor ({ dir = '.', dev = false, hotReload = false }) {
this.dir = resolve(dir)
this.dev = dev
this.hotReloader = hotReload ? new HotReloader(this.dir) : null
this.router = new Router()
this.http = http.createServer((req, res) => {
this.run(req, res)
.catch((err) => {
console.error(err)
res.status(500)
res.end('error')
})
})
this.defineRoutes()
}
async start (port) {
if (this.hotReloader) {
await this.hotReloader.start()
}
await new Promise((resolve, reject) => {
this.http.listen(port, (err) => {
if (err) return reject(err)
resolve()
})
})
}
defineRoutes () {
this.router.get('/_next/:path+', async (req, res, params) => {
const p = join(__dirname, '..', 'client', ...(params.path || []))
await this.serveStatic(req, res, p)
})
this.router.get('/static/:path+', async (req, res, params) => {
const p = join(this.dir, 'static', ...(params.path || []))
await this.serveStatic(req, res, p)
})
this.router.get('/:path+.json', async (req, res) => {
await this.renderJSON(req, res)
})
this.router.get('/:path*', async (req, res) => {
await this.render(req, res)
})
}
async run (req, res) {
const fn = this.router.match(req, res)
if (fn) {
await fn()
} else {
await this.render404(req, res)
}
}
async render (req, res) {
const { dir, dev } = this
const { pathname, query } = parse(req.url, true)
const ctx = { req, res, pathname, query }
const opts = { dir, dev }
let html
const err = this.getCompilationError(req.url)
if (err) {
res.statusCode = 500
html = await render('/_error-debug', { ...ctx, err }, opts)
} else {
try {
html = await render(req.url, ctx, opts)
} catch (err) {
const _err = this.getCompilationError('/_error')
if (_err) {
res.statusCode = 500
html = await render('/_error-debug', { ...ctx, err: _err }, opts)
} else {
if (err.code === 'ENOENT') {
res.statusCode = 404
} else {
console.error(err)
res.statusCode = 500
}
html = await render('/_error', { ...ctx, err }, opts)
}
}
}
sendHTML(res, html)
}
async renderJSON (req, res) {
const { dir } = this
const opts = { dir }
let json
const err = this.getCompilationError(req.url)
if (err) {
res.statusCode = 500
json = await renderJSON('/_error-debug.json', opts)
json = { ...json, err: errorToJSON(err) }
} else {
try {
json = await renderJSON(req.url, opts)
} catch (err) {
const _err = this.getCompilationError('/_error.json')
if (_err) {
res.statusCode = 500
json = await renderJSON('/_error-debug.json', opts)
json = { ...json, err: errorToJSON(_err) }
} else {
if (err.code === 'ENOENT') {
res.statusCode = 404
} else {
console.error(err)
res.statusCode = 500
}
json = await renderJSON('/_error.json', opts)
}
}
}
const data = JSON.stringify(json)
res.setHeader('Content-Type', 'application/json')
res.setHeader('Content-Length', Buffer.byteLength(data))
res.end(data)
}
async render404 (req, res) {
const { dir, dev } = this
const { pathname, query } = parse(req.url, true)
const ctx = { req, res, pathname, query }
const opts = { dir, dev }
let html
const err = this.getCompilationError('/_error')
if (err) {
res.statusCode = 500
html = await render('/_error-debug', { ...ctx, err }, opts)
} else {
res.statusCode = 404
html = await render('/_error', ctx, opts)
}
sendHTML(res, html)
}
serveStatic (req, res, path) {
return new Promise((resolve, reject) => {
send(req, path)
.on('error', (err) => {
if (err.code === 'ENOENT') {
this.render404(req, res).then(resolve, reject)
} else {
reject(err)
}
})
.pipe(res)
.on('finish', resolve)
})
}
getCompilationError (url) {
if (!this.hotReloader) return
const errors = this.hotReloader.getCompilationErrors()
if (!errors.size) return
const p = parse(url || '/').pathname.replace(/\.json$/, '')
const id = join(this.dir, '.next', 'bundles', 'pages', p)
const path = resolveFromList(id, errors.keys())
if (path) return errors.get(path)[0]
}
}
function sendHTML (res, html) {
res.setHeader('Content-Type', 'text/html')
res.setHeader('Content-Length', Buffer.byteLength(html))
res.end(html)
}