hypercore-fetch 9.0.3 → 9.0.4
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 +17 -3
- package/package.json +1 -1
- package/test.js +26 -0
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
|
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
|
|