forked from RangerMauve/hypercore-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
312 lines (263 loc) · 9.33 KB
/
index.js
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
const resolveDatPath = require('resolve-dat-path')
const Headers = require('fetch-headers')
const mime = require('mime/lite')
const concat = require('concat-stream')
const SDK = require('dat-sdk')
const { Readable } = require('stream')
const parseRange = require('range-parser')
const bodyToStream = require('fetch-request-body-to-stream')
const pump = require('pump-promise')
const makeDir = require('make-dir')
const DAT_REGEX = /\w+:\/\/([^/]+)\/?([^#?]*)?/
const NOT_WRITABLE_ERROR = 'Archive not writable'
const READABLE_ALLOW = ['GET', 'HEAD']
const WRITABLE_ALLOW = ['PUT', 'DELETE']
const ALL_ALLOW = READABLE_ALLOW.concat(WRITABLE_ALLOW)
module.exports = function makeFetch (opts = {}) {
let { Hyperdrive, resolveName, base, session, writable = false } = opts
let sdk = null
let gettingSDK = null
let onClose = async () => undefined
const isSourceDat = base && (base.startsWith('dat://') || base.startsWith('hyper://'))
datFetch.close = () => onClose()
return datFetch
function getResolve () {
if (resolveName) return resolveName
return getSDK().then(({ resolveName }) => resolveName)
}
function getHyperdrive () {
if (Hyperdrive) return Hyperdrive
return getSDK().then(({ Hyperdrive }) => Hyperdrive)
}
function getSDK () {
if (sdk) return Promise.resolve(sdk)
if (gettingSDK) return gettingSDK
return SDK(opts).then((gotSDK) => {
sdk = gotSDK
gettingSDK = null
onClose = async () => sdk.close()
Hyperdrive = sdk.Hyperdrive
resolveName = sdk.resolveName
return sdk
})
}
function resolveDatPathAwait (archive, path) {
return new Promise((resolve, reject) => {
resolveDatPath(archive, path, (err, resolved) => {
if (err) reject(err)
else resolve(resolved)
})
})
}
function checkWritable (archive) {
if (!writable) throw new Error(NOT_WRITABLE_ERROR)
if (!archive.writable) {
throw new Error(NOT_WRITABLE_ERROR)
}
}
async function datFetch (url, opts = {}) {
if (typeof url !== 'string') {
opts = url
url = opts.url
}
const isDatURL = url.startsWith('dat://') || url.startsWith('hyper://')
const urlHasProtocol = url.match(/^\w+:\/\//)
const shouldIntercept = isDatURL || (!urlHasProtocol && isSourceDat)
if (!shouldIntercept) throw new Error('Invalid protocol, must be dat:// or hyper://')
const { headers: rawHeaders, method: rawMethod } = opts
const headers = new Headers(rawHeaders || {})
const method = rawMethod ? rawMethod.toUpperCase() : 'GET'
const responseHeaders = new Headers()
try {
let { path, key } = parseDatURL(url)
if (!path) path = '/'
const resolve = await getResolve()
try {
key = await resolve(`dat://${key}`)
} catch (e) {
// Probably a domain that couldn't resolve
if (key.includes('.')) throw e
}
const Hyperdrive = await getHyperdrive()
const archive = Hyperdrive(key)
await archive.ready()
const canonical = `hyper://${archive.key.toString('hex')}/${path || ''}`
responseHeaders.append('Link', `<${canonical}>; rel="canonical"`)
const isWritable = writable && archive.writable
const allowHeaders = isWritable ? ALL_ALLOW : READABLE_ALLOW
responseHeaders.set('Allow', allowHeaders.join(', '))
// We can say the file hasn't changed if the drive version hasn't changed
responseHeaders.set('ETag', `"${archive.version}"`)
if (method === 'PUT') {
checkWritable(archive)
if (path.endsWith('/')) {
await makeDir(path, { fs: archive })
} else {
const parentDir = path.split('/').slice(0, -1).join('/')
if (parentDir) {
await makeDir(parentDir, { fs: archive })
}
// Create a new file from the request body
const { body } = opts
const source = bodyToStream(body, session)
const destination = archive.createWriteStream(path)
await pump(source, destination)
}
return new FakeResponse(200, 'OK', responseHeaders, intoStream(''), url)
} else if (method === 'DELETE') {
checkWritable(archive)
const stats = await archive.stat(path)
// Weird stuff happening up in here...
const stat = Array.isArray(stats) ? stats[0] : stats
if (stat.isDirectory()) {
await archive.rmdir(path)
} else {
await archive.unlink(path)
}
return new FakeResponse(200, 'OK', responseHeaders, intoStream(''), url)
} else if ((method === 'GET') || (method === 'HEAD')) {
let stat = null
let finalPath = path
if (finalPath === '.well-known/dat') {
const { key } = archive
const entry = `dat://${key.toString('hex')}\nttl=3600`
return new FakeResponse(200, 'OK', responseHeaders, intoStream(entry), url)
}
try {
if (headers.get('X-Resolve') === 'none') {
stat = await archive.stat(path)
} else {
const resolved = await resolveDatPathAwait(archive, path)
finalPath = resolved.path
stat = resolved.stat
}
} catch (e) {
return new FakeResponse(
404,
'Not Found',
new Headers([
'content-type', 'text/plain'
]),
intoStream(e.stack),
url)
}
responseHeaders.set('Content-Type', mime.getType(finalPath) || 'text/plain')
let stream = null
const isRanged = headers.get('Range') || headers.get('range')
let statusCode = 200
if (stat.isDirectory()) {
const stats = await archive.readdir(finalPath, { includeStats: true })
const files = stats.map(({ stat, name }) => (stat.isDirectory() ? `${name}/` : name))
if (headers.get('Accept') === 'application/json') {
const json = JSON.stringify(files, null, '\t')
stream = intoStream(Buffer.from(json))
} else {
const page = `
<!DOCTYPE html>
<title>${url}</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<h1>Index of ${path}</h1>
<ul>
<li><a href="../">../</a></li>${files.map((file) => `
<li><a href="${file}">./${file}</a></li>
`).join('')}
</ul>
`
responseHeaders.set('Content-Type', 'text/html')
const buffer = Buffer.from(page)
stream = intoStream(buffer)
}
} else {
responseHeaders.set('Accept-Ranges', 'bytes')
const { blocks, downloadedBlocks } = await archive.stats(finalPath)
responseHeaders.set('X-Blocks', `${blocks}`)
responseHeaders.set('X-Blocks-Downloaded', `${downloadedBlocks}`)
if (isRanged) {
const { size } = stat
const range = parseRange(size, isRanged)[0]
if (range && range.type === 'bytes') {
statusCode = 206
const { start, end } = range
const length = (end - start + 1)
responseHeaders.set('Content-Length', `${length}`)
responseHeaders.set('Content-Range', `bytes${start}-${end}/${size}`)
stream = archive.createReadStream(finalPath, {
start,
end
})
} else {
responseHeaders.set('Content-Length', `${size}`)
stream = archive.createReadStream(finalPath)
}
} else {
stream = archive.createReadStream(finalPath)
}
}
if (method === 'HEAD') {
stream.destroy()
return new FakeResponse(204, 'ok', responseHeaders, intoStream(''), url)
} else {
return new FakeResponse(statusCode, 'ok', responseHeaders, stream, url)
}
} else {
return new FakeResponse(405, 'Method Not Allowed', responseHeaders, intoStream('Method Not Allowed'), url)
}
} catch (e) {
const isUnauthorized = (e.message === NOT_WRITABLE_ERROR)
const status = isUnauthorized ? 403 : 500
const message = isUnauthorized ? 'Not Authorized' : 'Server Error'
return new FakeResponse(status, message, responseHeaders, intoStream(e.stack), url)
}
}
}
function parseDatURL (url) {
let [, key, path] = url.toString().match(DAT_REGEX)
let version = null
if (key.includes('+')) [key, version] = key.split('+')
return {
key,
path,
version
}
}
class FakeResponse {
constructor (status, statusText, headers, stream, url) {
this.body = stream
this.headers = headers
this.url = url
this.status = status
this.statusText = statusText
}
get ok () {
return this.status && this.status < 400
}
get useFinalURL () {
return true
}
async arrayBuffer () {
const buffer = await concatPromise(this.body)
return buffer.buffer
}
async text () {
const buffer = await concatPromise(this.body)
return buffer.toString('utf-8')
}
async json () {
return JSON.parse(await this.text())
}
}
function concatPromise (stream) {
return new Promise((resolve, reject) => {
var concatStream = concat(resolve)
concatStream.once('error', reject)
stream.pipe(concatStream)
})
}
function intoStream (data) {
return new Readable({
read () {
this.push(data)
this.push(null)
}
})
}