hypercore-fetch 9.0.4 → 9.0.6
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 +18 -9
- package/package.json +1 -1
- package/test.js +13 -5
package/index.js
CHANGED
|
@@ -22,6 +22,7 @@ const MIME_TEXT_HTML = 'text/html; charset=utf-8'
|
|
|
22
22
|
const MIME_EVENT_STREAM = 'text/event-stream; charset=utf-8'
|
|
23
23
|
|
|
24
24
|
const HEADER_CONTENT_TYPE = 'Content-Type'
|
|
25
|
+
const HEADER_LAST_MODIFIED = 'Last-Modified'
|
|
25
26
|
|
|
26
27
|
export const ERROR_KEY_NOT_CREATED = 'Must create key with POST before reading'
|
|
27
28
|
|
|
@@ -327,7 +328,11 @@ export default async function makeHyperFetch ({
|
|
|
327
328
|
} else {
|
|
328
329
|
await pipelinePromise(
|
|
329
330
|
Readable.from(request.body),
|
|
330
|
-
drive.createWriteStream(pathname
|
|
331
|
+
drive.createWriteStream(pathname, {
|
|
332
|
+
metadata: {
|
|
333
|
+
mtime: Date.now()
|
|
334
|
+
}
|
|
335
|
+
})
|
|
331
336
|
)
|
|
332
337
|
}
|
|
333
338
|
|
|
@@ -435,11 +440,12 @@ export default async function makeHyperFetch ({
|
|
|
435
440
|
resHeaders.ETag = `${entry.seq}`
|
|
436
441
|
|
|
437
442
|
const contentType = getMimeType(path)
|
|
438
|
-
resHeaders[
|
|
443
|
+
resHeaders[HEADER_CONTENT_TYPE] = contentType
|
|
444
|
+
|
|
439
445
|
|
|
440
|
-
if (entry
|
|
441
|
-
const date = new Date(entry.metadata.mtime)
|
|
442
|
-
resHeaders[
|
|
446
|
+
if (entry?.value?.metadata?.mtime) {
|
|
447
|
+
const date = new Date(entry.value.metadata.mtime)
|
|
448
|
+
resHeaders[HEADER_LAST_MODIFIED] = date.toUTCString()
|
|
443
449
|
}
|
|
444
450
|
|
|
445
451
|
const size = entry.value.byteLength
|
|
@@ -545,6 +551,7 @@ async function serveFile (headers, drive, pathname) {
|
|
|
545
551
|
|
|
546
552
|
const entry = await drive.entry(pathname)
|
|
547
553
|
|
|
554
|
+
|
|
548
555
|
const resHeaders = {
|
|
549
556
|
ETag: `${entry.seq}`,
|
|
550
557
|
[HEADER_CONTENT_TYPE]: contentType,
|
|
@@ -552,9 +559,10 @@ async function serveFile (headers, drive, pathname) {
|
|
|
552
559
|
Link: `<${fullURL}>; rel="canonical"`
|
|
553
560
|
}
|
|
554
561
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
562
|
+
|
|
563
|
+
if (entry?.value?.metadata?.mtime) {
|
|
564
|
+
const date = new Date(entry.value.metadata.mtime)
|
|
565
|
+
resHeaders[HEADER_LAST_MODIFIED] = date.toUTCString()
|
|
558
566
|
}
|
|
559
567
|
|
|
560
568
|
const size = entry.value.blob.byteLength
|
|
@@ -617,7 +625,8 @@ async function resolvePath (drive, pathname, noResolve) {
|
|
|
617
625
|
async function listEntries (drive, pathname = '/') {
|
|
618
626
|
const entries = []
|
|
619
627
|
for await (const path of drive.readdir(pathname)) {
|
|
620
|
-
const
|
|
628
|
+
const fullPath = posix.join(pathname, path)
|
|
629
|
+
const stat = await drive.entry(fullPath)
|
|
621
630
|
if (stat === null) {
|
|
622
631
|
entries.push(path + '/')
|
|
623
632
|
} else {
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -104,7 +104,7 @@ test('GET full url for created keys', async (t) => {
|
|
|
104
104
|
t.ok(createdURL.startsWith('hyper://'), 'Got new hyper:// URL')
|
|
105
105
|
|
|
106
106
|
const nowExistingResponse = await fetch(keyURL)
|
|
107
|
-
await checkResponse(nowExistingResponse, t,'GET no longer fails on create')
|
|
107
|
+
await checkResponse(nowExistingResponse, t, 'GET no longer fails on create')
|
|
108
108
|
|
|
109
109
|
const existingURL = await nowExistingResponse.text()
|
|
110
110
|
|
|
@@ -129,9 +129,11 @@ test('PUT file', async (t) => {
|
|
|
129
129
|
|
|
130
130
|
const content = await uploadedContentResponse.text()
|
|
131
131
|
const contentType = uploadedContentResponse.headers.get('Content-Type')
|
|
132
|
+
const lastModified = uploadedContentResponse.headers.get('Last-Modified')
|
|
132
133
|
|
|
133
134
|
t.equal(contentType, 'text/plain; charset=utf-8', 'Content got expected mime type')
|
|
134
135
|
t.equal(content, SAMPLE_CONTENT, 'Got uploaded content back out')
|
|
136
|
+
t.ok(lastModified, 'Last-Modified header got set')
|
|
135
137
|
})
|
|
136
138
|
test('PUT FormData', async (t) => {
|
|
137
139
|
const created = await nextURL(t)
|
|
@@ -182,10 +184,16 @@ test('PUT into new directory', async (t) => {
|
|
|
182
184
|
t.equal(contentType, 'text/plain; charset=utf-8', 'Content got expected mime type')
|
|
183
185
|
t.equal(content, SAMPLE_CONTENT, 'Got uploaded content back out')
|
|
184
186
|
|
|
185
|
-
const
|
|
186
|
-
await checkResponse(
|
|
187
|
-
const
|
|
188
|
-
t.deepEqual(
|
|
187
|
+
const topDirResponse = await fetch(created)
|
|
188
|
+
await checkResponse(topDirResponse, t)
|
|
189
|
+
const topDirEntries = await topDirResponse.json()
|
|
190
|
+
t.deepEqual(topDirEntries, ['subfolder/'], 'subdirectory is listed')
|
|
191
|
+
|
|
192
|
+
const subDir = new URL('./subfolder/', created)
|
|
193
|
+
const subDirResponse = await fetch(subDir)
|
|
194
|
+
await checkResponse(subDirResponse, t)
|
|
195
|
+
const subDirEntries = await subDirResponse.json()
|
|
196
|
+
t.deepEqual(subDirEntries, ['example.txt'], 'new file is listed')
|
|
189
197
|
})
|
|
190
198
|
test('PUT to overwrite a file', async (t) => {
|
|
191
199
|
const created = await nextURL(t)
|