-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add setFile()
method
#26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
b30eaee
aea1c14
6b15fd5
cd1cf8d
732c499
ecfd834
3c201aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
18.16 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import { createReadStream } from 'node:fs' | ||
import { stat } from 'node:fs/promises' | ||
import { Readable } from 'node:stream' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is also available as a Deno package, do you think it'd make sense to somehow use Deno-native FS APIs for this under Deno? e.g. by dependency-injecting an FS implementation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deno now supports these native Node APIs, so these should work on both runtimes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but presumably the native Deno APIs would be more performant? Probably doesn't make a difference, but could be interesting to at least do a measurement on it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OTOH, we probably won't upload a file from FS in Edge Functions in the foreseeable future, so scratch that 😅 |
||
|
||
interface APICredentials { | ||
apiURL?: string | ||
token: string | ||
|
@@ -127,7 +131,19 @@ export class Blobs { | |
headers['cache-control'] = 'max-age=0, stale-while-revalidate=60' | ||
} | ||
|
||
const res = await this.fetcher(url, { body, headers, method }) | ||
const options: RequestInit = { | ||
body, | ||
headers, | ||
method, | ||
} | ||
|
||
if (body instanceof ReadableStream) { | ||
// @ts-expect-error Part of the spec, but not typed: | ||
// https://fetch.spec.whatwg.org/#enumdef-requestduplex | ||
options.duplex = 'half' | ||
} | ||
|
||
const res = await this.fetcher(url, options) | ||
|
||
if (res.status === 404 && method === HTTPMethod.Get) { | ||
return null | ||
|
@@ -200,6 +216,17 @@ export class Blobs { | |
await this.makeStoreRequest(key, HTTPMethod.Put, headers, data) | ||
} | ||
|
||
async setFile(key: string, path: string, { ttl }: SetOptions = {}) { | ||
const { size } = await stat(path) | ||
const file = Readable.toWeb(createReadStream(path)) | ||
const headers = { | ||
...Blobs.getTTLHeaders(ttl), | ||
'content-length': size.toString(), | ||
} | ||
|
||
await this.makeStoreRequest(key, HTTPMethod.Put, headers, file as ReadableStream) | ||
} | ||
|
||
async setJSON(key: string, data: unknown, { ttl }: SetOptions = {}) { | ||
const payload = JSON.stringify(data) | ||
const headers = { | ||
|
Uh oh!
There was an error while loading. Please reload this page.