-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
168 lines (143 loc) · 3.99 KB
/
index.js
File metadata and controls
168 lines (143 loc) · 3.99 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
import http from 'bare-http1'
import { pipeline } from 'bare-stream'
import * as SDK from 'hyper-sdk'
import { Mime } from 'mime/lite'
import standardTypes from 'mime/types/standard.js'
import otherTypes from 'mime/types/other.js'
const mime = new Mime(standardTypes, otherTypes)
// Support gemini files
mime.define({
'text/gemini': ['gmi', 'gemini'],
'text/org': ['org']
}, true)
const INDEX_FILES = [
'index.html',
'index.md',
'index.gmi',
'index.gemini',
'index.org',
'README.md',
'README.org'
]
const SEP = '/'
export const PORT = 3748 // DRIV on phone dial pad
export async function create ({ port = PORT, ...sdkOptions } = {}) {
let sdk = null
let loading = null
async function getSDK () {
if (sdk) return sdk
if (loading) return loading
loading = SDK.create({
storage: './storage',
...sdkOptions
})
sdk = await loading
loading = null
return sdk
}
const server = http.createServer(async (req, res) => {
const sdk = await getSDK()
const { method, url } = req
res.statusCode = 200
if (!url.startsWith('/hyper/')) {
res.statusCode = 404
res.setHeader('Content-Type', 'text/plain')
res.end('Not Found')
return
}
if (method !== 'GET') {
res.statusCode = 405
res.setHeader('Content-Type', 'text/plain')
res.end('Method Not Allowed')
return
}
try {
const [hostname, ...pathSegments] = url.split(SEP).slice(2)
let filePath = pathSegments.join(SEP) || SEP
const fullURL = new URL(filePath, `hyper://${hostname}`)
// TODO: Parse search params
const noResolve = fullURL.search.includes('noResolve')
filePath = fullURL.pathname
const key = await sdk.resolveDNSToKey(hostname)
const drive = await sdk.getDrive(key)
if (filePath.endsWith(SEP)) {
// TODO: Read index.*
// TODO: Account for HTML Accept
const entries = []
for await (const path of drive.readdir(filePath)) {
const fullPath = filePath + SEP + path
const stat = await drive.entry(fullPath)
if (stat === null) {
entries.push(path + '/')
} else {
entries.push(path)
}
}
let hadIndex = false
if (!noResolve) {
for (const indexFile of INDEX_FILES) {
if (entries.includes(indexFile)) {
hadIndex = true
filePath = filePath + indexFile
break
}
}
}
if (!hadIndex) {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(entries))
return
}
}
let entry = null
for (const entryPath of makeToTry(filePath)) {
entry = await drive.entry(entryPath)
filePath = entryPath
if (entry) break
}
if (!entry) {
res.statusCode = 404
res.setHeader('Content-Type', 'text/plain')
res.end('Not Found')
return
}
res.statusCode = 200
const contentType = mime.getType(filePath)
res.setHeader('Content-Type', contentType)
res.setHeader('Content-Length', `${entry.value.blob.byteLength}`)
const rs = drive.createReadStream(filePath)
pipeline([rs, res])
} catch (e) {
res.statusCode = 500
res.setHeader('Content-Type', 'text/plain')
// TODO: Hide stack trace in console
res.end(e.stack)
}
})
const boundPort = await new Promise((resolve, reject) => {
server.listen(port, (e) => {
if (e) return reject(e)
const { port } = server.address()
resolve(port)
})
})
async function close () {
server.close()
if (loading) sdk = await loading
if (sdk) {
await sdk.close()
}
}
return { close, server, sdk, port: boundPort }
}
function makeToTry (pathname) {
return [
pathname,
pathname + '.html',
pathname + '.md',
pathname + '.gmi',
pathname + '.gemini',
pathname + '.org'
]
}