diff --git a/README.md b/README.md index 9d0389a..e548f9f 100755 --- a/README.md +++ b/README.md @@ -120,13 +120,18 @@ Note that this is only available with the `writable: true` flag. ### `fetch('hyper://NAME/example.txt', {method: 'PUT', body: 'Hello World'})` -You can add files to archives using a `PUT` method along with a `body`. +You can add files to archives using a `PUT` method along with a +`body`. Note that this is only available with the `writable: true` +flag. The `body` can be any of the options supported by the Fetch API such as a `String`, `Blob`, `FormData`, or `ReadableStream`. `NAME` can either be the 52 character [z32 encoded](https://github.com/mafintosh/z32) key for a Hyperdrive or Hypercore , or a domain to parse with the [DNSLink](https://www.dnslink.io/) standard. -Note that this is only available with the `writable: true` flag. +The mtime metadata is automatically set to the current time when +uploading. To override this value, pass a `Date` header with a value +set to a date string according to [RFC +7231](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.1.1). An attempt to `PUT` a file to a hyperdrive which is not writable will fail with status `403`. diff --git a/index.js b/index.js index 72fb0db..6911e8a 100755 --- a/index.js +++ b/index.js @@ -397,6 +397,7 @@ export default async function makeHyperFetch ({ const { hostname, pathname: rawPathname } = new URL(request.url) const pathname = decodeURI(ensureLeadingSlash(rawPathname)) const contentType = request.headers.get('Content-Type') || '' + const mtime = Date.parse(request.headers.get('Date')) || Date.now() const isFormData = contentType.includes('multipart/form-data') const drive = await getDrive(`hyper://${hostname}/`, true) @@ -405,8 +406,6 @@ export default async function makeHyperFetch ({ return { status: 403, body: `Cannot PUT file to read-only drive: ${drive.url}`, headers: { Location: request.url } } } - const mtime = Date.now() - if (isFormData) { // It's a form! Get the files out and process them const formData = await request.formData() diff --git a/test.js b/test.js index f9a046f..27f1aae 100755 --- a/test.js +++ b/test.js @@ -142,9 +142,13 @@ test('PUT file', async (t) => { const uploadLocation = new URL('./example.txt', created) + const fakeDate = new Date(Date.parse(0)).toUTCString() const uploadResponse = await fetch(uploadLocation, { method: 'put', - body: SAMPLE_CONTENT + body: SAMPLE_CONTENT, + headers: { + Date: fakeDate + } }) await checkResponse(uploadResponse, t, 'upload successful') @@ -159,7 +163,7 @@ test('PUT file', async (t) => { t.equal(contentType, 'text/plain; charset=utf-8', 'Content got expected mime type') t.equal(content, SAMPLE_CONTENT, 'Got uploaded content back out') - t.ok(lastModified, 'Last-Modified header got set') + t.equal(lastModified, fakeDate, 'Last-Modified header was set to value of Date header') }) test('PUT FormData', async (t) => { const created = await nextURL(t)