-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.js
More file actions
335 lines (303 loc) · 10.3 KB
/
serve.js
File metadata and controls
335 lines (303 loc) · 10.3 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Dependency-free http server for serving static files
*/
import { exec } from 'child_process'
import { createReadStream } from 'fs'
import fs from 'fs/promises'
import http from 'http'
import path from 'path'
import url from 'url'
import zlib from 'zlib'
import { pipe, readStreamToReadableStream } from './streamConverters.js'
/** @type {Object<string, string>} */
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.csv': 'text/csv',
'.json': 'application/json',
'.map': 'application/json',
'.md': 'text/markdown',
'.ico': 'image/x-icon',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg',
'.parquet': 'application/x-parquet',
'.pdf': 'application/pdf',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.txt': 'text/plain',
'.ttf': 'font/ttf',
'.woff2': 'font/woff2',
}
/**
* @template T
* @typedef {T | Promise<T>} Awaitable<T>
*/
/**
* Start http server with optional path
* @param {string | undefined} serveDirectory serve files from this directory
* @param {string | undefined} key file to serve by default
*/
export async function serve(serveDirectory, key) {
// Search for open port
let port = 2048
while (port < 2048 + 10) {
try {
await startServer(port, serveDirectory)
break
} catch (/** @type {any} */ err) {
if (err.code !== 'EADDRINUSE') throw err
console.error(`port ${port} in use, trying next port`)
if (port === 2048) port++ // skip unsafe nfs port 2049
port++
}
}
console.log(`hyperparam server running on http://localhost:${port}`)
if (!key) openUrl(`http://localhost:${port}`)
else {
key = encodeURIComponent(key)
openUrl(`http://localhost:${port}/files?key=${key}`)
}
}
/**
* Route an http request
* @typedef {Object} ReadableStream
* @typedef {{ status: number, content: string | Buffer | ReadableStream, contentLength?: number, contentType?: string }} ServeResult
* @param {http.IncomingMessage} req
* @param {string | undefined} serveDirectory
* @returns {Awaitable<ServeResult>}
*/
function handleRequest(req, serveDirectory) {
if (!req.url) return { status: 400, content: 'bad request' }
const parsedUrl = url.parse(req.url, true)
const pathname = parsedUrl.pathname || ''
// get location of hyperparam assets
const hyperparamPath = decodeURIComponent(import.meta.url)
.replace('file://', '')
.replace('/bin/serve.js', '')
if (pathname === '/' || pathname === '/files/') {
// redirect to /files
return { status: 301, content: '/files' }
} else if (pathname.startsWith('/files')) {
// serve index.html
return handleStatic(`${hyperparamPath}/dist/index.html`)
} else if (pathname.startsWith('/assets/') || pathname.startsWith('/favicon') ) {
// serve static files
return handleStatic(`${hyperparamPath}/dist${pathname}`)
// else if (pathname.startsWith('/public/')) {
// // serve static files
// return handleStatic(`${hyperparamPath}${pathname.replace(/^(\/public).*/, '/dist')}`)
} else if (serveDirectory && pathname === '/api/store/list') {
// serve file list
const prefix = parsedUrl.query.prefix || ''
if (Array.isArray(prefix)) return { status: 400, content: 'bad request' }
const perfixPath = `${serveDirectory}/${decodeURIComponent(prefix)}`
return handleListing(perfixPath)
} else if (serveDirectory && pathname === '/api/store/get') {
// serve file content
const key = parsedUrl.query.key || ''
if (Array.isArray(key)) return { status: 400, content: 'bad request' }
const filePath = `${serveDirectory}/${decodeURIComponent(key)}`
if (req.method === 'HEAD') {
return handleHead(filePath)
}
const range = req.method === 'HEAD' ? '0-0' : req.headers.range
return handleStatic(filePath, range)
} else {
return { status: 404, content: 'not found' }
}
}
/**
* Serve static file from the serve directory
* @param {string} filePath
* @param {string} [range]
* @returns {Promise<ServeResult>}
*/
async function handleStatic(filePath, range) {
const stats = await fs.stat(filePath).catch(() => undefined)
if (!stats?.isFile()) {
return { status: 404, content: 'not found' }
}
const contentLength = stats.size
// detect content type
const extname = path.extname(filePath)
if (!mimeTypes[extname]) console.error(`serving unknown mimetype ${extname}`)
const contentType = mimeTypes[extname] || 'application/octet-stream'
// ranged requests
if (range) {
const [unit, ranges] = range.split('=')
if (unit === 'bytes') {
const [start, end] = ranges.split('-').map(Number)
// convert fs.ReadStream to web stream
const fsStream = createReadStream(filePath, { start, end })
const content = readStreamToReadableStream(fsStream)
const contentLength = end - start + 1
return {
status: 206,
content,
contentLength,
contentType,
}
}
}
const content = await fs.readFile(filePath)
return { status: 200, content, contentLength, contentType }
}
/**
* Serve head request
* @param {string} filePath
* @returns {Promise<ServeResult>}
*/
async function handleHead(filePath) {
const stats = await fs.stat(filePath).catch(() => undefined)
if (!stats?.isFile()) {
console.error(`file not found ${filePath}`)
return { status: 404, content: 'not found' }
}
const contentLength = stats.size
// detect content type
const extname = path.extname(filePath)
if (!mimeTypes[extname]) console.error(`serving unknown mimetype ${extname}`)
const contentType = mimeTypes[extname] || 'application/octet-stream'
return { status: 200, content: '', contentLength, contentType }
}
/**
* List files from local storage
*
* @param {string} prefix file path prefix
* @returns {Promise<ServeResult>}
*/
export async function handleListing(prefix) {
try {
const stat = await fs.stat(prefix)
if (!stat.isDirectory()) return { status: 400, content: 'not a directory' }
} catch {
return { status: 404, content: 'not found' }
}
const files = []
for (const filename of await fs.readdir(prefix, { recursive: false })) {
// get stats for each file
const filePath = `${prefix}/${filename}`
const stat = await fs.stat(filePath)
.catch(() => undefined) // handle bad symlinks
if (stat?.isFile()) {
files.push({
key: filename,
fileSize: stat.size,
lastModified: stat.mtime.toISOString(),
})
} else if (stat?.isDirectory()) {
files.push({
key: filename + '/',
lastModified: stat.mtime.toISOString(),
})
}
}
files.sort((a, b) => {
const isDirA = a.key.endsWith('/')
const isDirB = b.key.endsWith('/')
// Prioritize directories over files
if (isDirA && !isDirB) return -1
if (!isDirA && isDirB) return 1
// Check for dot folders/files and special char folders/files
const isDotFolderA = a.key.startsWith('.')
const isDotFolderB = b.key.startsWith('.')
const hasSpecialCharsA = /[^a-zA-Z0-9]/.test(a.key)
const hasSpecialCharsB = /[^a-zA-Z0-9]/.test(b.key)
// Handle dot folders/files first
if (isDotFolderA && !isDotFolderB) return -1
if (!isDotFolderA && isDotFolderB) return 1
// Handle special character folders/files second
if (hasSpecialCharsA && !hasSpecialCharsB) return 1
if (!hasSpecialCharsA && hasSpecialCharsB) return -1
return a.key.localeCompare(b.key)
})
return { status: 200, content: JSON.stringify(files), contentType: 'application/json' }
}
/**
* @param {number} port
* @param {string | undefined} path serve files from this directory
* @returns {Promise<void>}
*/
function startServer(port, path) {
return new Promise((resolve, reject) => {
// create http server
const server = http.createServer(async (req, res) => {
const startTime = new Date()
// handle request
/** @type {ServeResult} */
let result = { status: 500, content: 'internal server error' }
try {
result = await handleRequest(req, path)
} catch (err) {
console.error('error handling request', err)
}
const { status } = result
let { content } = result
// write http header
/** @type {http.OutgoingHttpHeaders} */
const headers = { 'Connection': 'keep-alive' }
if (result.contentLength !== undefined) {
headers['Content-Length'] = result.contentLength
}
if (result.contentType) headers['Content-Type'] = result.contentType
if (status === 301 && typeof content === 'string') {
// handle redirect
headers.Location = content
content = ''
}
// compress content
const gzipped = gzip(req, content)
if (gzipped) {
headers['Content-Encoding'] = 'gzip'
content = gzipped
}
res.writeHead(status, headers)
// write http response
if (content instanceof Buffer || typeof content === 'string') {
res.end(content)
} else if (content instanceof ReadableStream) {
pipe(content, res)
}
// log request
const endTime = new Date()
const ms = endTime.getTime() - startTime.getTime()
// @ts-expect-error contentLength will exist if content is ReadableStream
const length = result.contentLength || content.length || 0
const line = `${endTime.toISOString()} ${status} ${req.method} ${req.url} ${length} ${ms}ms`
if (status < 400) {
console.log(line)
} else {
// highlight errors red
console.log(`\x1b[31m${line}\x1b[0m`)
}
})
server.on('error', reject)
server.listen(port, resolve)
})
}
/**
* If the request accepts gzip, compress the content, else undefined
* @param {http.IncomingMessage} req
* @param {string | Buffer | ReadableStream} content
* @returns {Buffer | undefined}
*/
function gzip(req, content) {
if (!(content instanceof Buffer) || !(typeof content === 'string')) return undefined
const acceptEncoding = req.headers['accept-encoding']
if (acceptEncoding?.includes('gzip')) {
return zlib.gzipSync(content)
}
}
/**
* @param {string} url
* @returns {void}
*/
function openUrl(url) {
switch (process.platform) {
case 'darwin': exec(`open ${url}`); return
case 'win32': exec(`start ${url}`); return
case 'linux': exec(`xdg-open ${url}`); return
default: throw new Error(`unsupported platform ${process.platform}`)
}
}