hypercore-fetch 9.0.3 → 9.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +24 -5
- package/package.json +1 -1
- package/test.js +36 -4
package/index.js
CHANGED
|
@@ -23,6 +23,8 @@ const MIME_EVENT_STREAM = 'text/event-stream; charset=utf-8'
|
|
|
23
23
|
|
|
24
24
|
const HEADER_CONTENT_TYPE = 'Content-Type'
|
|
25
25
|
|
|
26
|
+
export const ERROR_KEY_NOT_CREATED = 'Must create key with POST before reading'
|
|
27
|
+
|
|
26
28
|
async function DEFAULT_RENDER_INDEX (url, files, fetch) {
|
|
27
29
|
return `
|
|
28
30
|
<!DOCTYPE html>
|
|
@@ -103,7 +105,7 @@ export default async function makeHyperFetch ({
|
|
|
103
105
|
}
|
|
104
106
|
const core = await getDBCoreForName(key)
|
|
105
107
|
if (!core.length && errorOnNew) {
|
|
106
|
-
|
|
108
|
+
throw new Error(ERROR_KEY_NOT_CREATED)
|
|
107
109
|
}
|
|
108
110
|
|
|
109
111
|
const corestore = sdk.namespace(key)
|
|
@@ -269,9 +271,21 @@ export default async function makeHyperFetch ({
|
|
|
269
271
|
return { status: 400, body: 'Must specify key parameter to resolve' }
|
|
270
272
|
}
|
|
271
273
|
|
|
272
|
-
|
|
274
|
+
try {
|
|
275
|
+
const drive = await getDriveFromKey(key, true)
|
|
273
276
|
|
|
274
|
-
|
|
277
|
+
return { body: drive.core.url }
|
|
278
|
+
} catch (e) {
|
|
279
|
+
if (e.message === ERROR_KEY_NOT_CREATED) {
|
|
280
|
+
return {
|
|
281
|
+
status: 400,
|
|
282
|
+
body: e.message,
|
|
283
|
+
headers: {
|
|
284
|
+
[HEADER_CONTENT_TYPE]: MIME_TEXT_PLAIN
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
} else throw e
|
|
288
|
+
}
|
|
275
289
|
})
|
|
276
290
|
router.post(`hyper://${SPECIAL_DOMAIN}/`, async function createKey (request) {
|
|
277
291
|
// TODO: Allow importing secret keys here
|
|
@@ -313,7 +327,11 @@ export default async function makeHyperFetch ({
|
|
|
313
327
|
} else {
|
|
314
328
|
await pipelinePromise(
|
|
315
329
|
Readable.from(request.body),
|
|
316
|
-
drive.createWriteStream(pathname
|
|
330
|
+
drive.createWriteStream(pathname, {
|
|
331
|
+
metadata: {
|
|
332
|
+
mtime: Date.now()
|
|
333
|
+
}
|
|
334
|
+
})
|
|
317
335
|
)
|
|
318
336
|
}
|
|
319
337
|
|
|
@@ -603,7 +621,8 @@ async function resolvePath (drive, pathname, noResolve) {
|
|
|
603
621
|
async function listEntries (drive, pathname = '/') {
|
|
604
622
|
const entries = []
|
|
605
623
|
for await (const path of drive.readdir(pathname)) {
|
|
606
|
-
const
|
|
624
|
+
const fullPath = posix.join(pathname, path)
|
|
625
|
+
const stat = await drive.entry(fullPath)
|
|
607
626
|
if (stat === null) {
|
|
608
627
|
entries.push(path + '/')
|
|
609
628
|
} else {
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -85,6 +85,32 @@ test('Quick check', async (t) => {
|
|
|
85
85
|
t.deepEqual(await dirResponse.json(), ['example.txt'], 'File got added')
|
|
86
86
|
})
|
|
87
87
|
|
|
88
|
+
test('GET full url for created keys', async (t) => {
|
|
89
|
+
const keyURL = `hyper://localhost/?key=example${next()}`
|
|
90
|
+
|
|
91
|
+
const nonExistingResponse = await fetch(keyURL)
|
|
92
|
+
|
|
93
|
+
t.notOk(nonExistingResponse.ok, 'response has error before key is created')
|
|
94
|
+
const errorMessage = await nonExistingResponse.text()
|
|
95
|
+
|
|
96
|
+
t.equal(nonExistingResponse.status, 400, 'Got 400 error code')
|
|
97
|
+
t.notOk(errorMessage.startsWith('hyper://'), 'did not return hyper URL')
|
|
98
|
+
|
|
99
|
+
const createResponse = await fetch(keyURL, { method: 'post' })
|
|
100
|
+
await checkResponse(createResponse, t, 'Able to create drive')
|
|
101
|
+
|
|
102
|
+
const createdURL = await createResponse.text()
|
|
103
|
+
|
|
104
|
+
t.ok(createdURL.startsWith('hyper://'), 'Got new hyper:// URL')
|
|
105
|
+
|
|
106
|
+
const nowExistingResponse = await fetch(keyURL)
|
|
107
|
+
await checkResponse(nowExistingResponse, t, 'GET no longer fails on create')
|
|
108
|
+
|
|
109
|
+
const existingURL = await nowExistingResponse.text()
|
|
110
|
+
|
|
111
|
+
t.equal(existingURL, createdURL, 'URL same as in initial create')
|
|
112
|
+
})
|
|
113
|
+
|
|
88
114
|
test('PUT file', async (t) => {
|
|
89
115
|
const created = await nextURL(t)
|
|
90
116
|
|
|
@@ -156,10 +182,16 @@ test('PUT into new directory', async (t) => {
|
|
|
156
182
|
t.equal(contentType, 'text/plain; charset=utf-8', 'Content got expected mime type')
|
|
157
183
|
t.equal(content, SAMPLE_CONTENT, 'Got uploaded content back out')
|
|
158
184
|
|
|
159
|
-
const
|
|
160
|
-
await checkResponse(
|
|
161
|
-
const
|
|
162
|
-
t.deepEqual(
|
|
185
|
+
const topDirResponse = await fetch(created)
|
|
186
|
+
await checkResponse(topDirResponse, t)
|
|
187
|
+
const topDirEntries = await topDirResponse.json()
|
|
188
|
+
t.deepEqual(topDirEntries, ['subfolder/'], 'subdirectory is listed')
|
|
189
|
+
|
|
190
|
+
const subDir = new URL('./subfolder/', created)
|
|
191
|
+
const subDirResponse = await fetch(subDir)
|
|
192
|
+
await checkResponse(subDirResponse, t)
|
|
193
|
+
const subDirEntries = await subDirResponse.json()
|
|
194
|
+
t.deepEqual(subDirEntries, ['example.txt'], 'new file is listed')
|
|
163
195
|
})
|
|
164
196
|
test('PUT to overwrite a file', async (t) => {
|
|
165
197
|
const created = await nextURL(t)
|