Skip to content

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

Merged
merged 7 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.16
52 changes: 46 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
"version": "1.4.0",
"description": "A JavaScript client for the Netlify Blob Store",
"type": "module",
"main": "./dist/main.js",
"exports": "./dist/main.js",
"main": "./dist/src/main.js",
"exports": "./dist/src/main.js",
"files": [
"dist/**/*.js",
"dist/**/*.d.ts"
"dist/src/**/*.js",
"dist/src/**/*.d.ts",
"!dist/src/**/*.test.d.ts"
],
"scripts": {
"build": "run-s build:*",
"build:check": "tsc",
"build:transpile": "esbuild src/main.ts --bundle --outdir=dist --platform=node --format=esm",
"build:transpile": "esbuild src/main.ts --bundle --outdir=dist/src --platform=node --format=esm",
"dev": "esbuild src/main.ts --bundle --outdir=dist/src --platform=node --format=esm --watch",
"prepare": "husky install node_modules/@netlify/eslint-config-node/.husky/",
"prepublishOnly": "npm ci && npm test",
"prepack": "npm run build",
Expand All @@ -29,8 +31,7 @@
"test:ci": "run-s build test:ci:*",
"test:dev:vitest": "vitest run",
"test:dev:vitest:watch": "vitest watch",
"test:ci:vitest": "vitest run",
"watch": "tsc --watch"
"test:ci:vitest": "vitest run"
},
"config": {
"eslint": "--ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{src,scripts,.github}/**/*.{js,ts,md,html}\" \"*.{js,ts,md,html}\"",
Expand All @@ -54,6 +55,7 @@
"husky": "^8.0.0",
"node-fetch": "^3.3.1",
"semver": "^7.5.3",
"tmp-promise": "^3.0.3",
"typescript": "^5.0.0",
"vitest": "^0.33.0"
},
Expand Down
51 changes: 50 additions & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { version as nodeVersion } from 'process'
import { writeFile } from 'node:fs/promises'
import { version as nodeVersion } from 'node:process'

import semver from 'semver'
import tmp from 'tmp-promise'
import { describe, test, expect, beforeAll } from 'vitest'

import { streamToString } from '../test/util.js'
Expand Down Expand Up @@ -310,6 +312,53 @@ describe('set', () => {
await blobs.set(key, value, { ttl })
})

// We need `Readable.toWeb` to be available, which needs Node 16+.
if (semver.gte(nodeVersion, '16.0.0')) {
test('Accepts a file', async () => {
expect.assertions(5)

const fileContents = 'Hello from a file'
const fetcher = async (...args: Parameters<typeof globalThis.fetch>) => {
const [url, options] = args
const headers = options?.headers as Record<string, string>

expect(options?.method).toBe('put')

if (url === `https://api.netlify.com/api/v1/sites/${siteID}/blobs/${key}?context=production`) {
const data = JSON.stringify({ url: signedURL })

expect(headers.authorization).toBe(`Bearer ${apiToken}`)

return new Response(data)
}

if (url === signedURL) {
expect(await streamToString(options?.body as unknown as NodeJS.ReadableStream)).toBe(fileContents)
expect(headers['cache-control']).toBe('max-age=0, stale-while-revalidate=60')

return new Response(value)
}

throw new Error(`Unexpected fetch call: ${url}`)
}

const { cleanup, path } = await tmp.file()

await writeFile(path, fileContents)

const blobs = new Blobs({
authentication: {
token: apiToken,
},
fetcher,
siteID,
})

await blobs.setFile(key, path)
await cleanup()
})
}

test('Throws when the API returns a non-200 status code', async () => {
const fetcher = async (...args: Parameters<typeof globalThis.fetch>) => {
const [url, options] = args
Expand Down
29 changes: 28 additions & 1 deletion src/main.ts
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'
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 😅


import { fetchAndRetry } from './retry.ts'

interface APICredentials {
Expand Down Expand Up @@ -129,7 +133,19 @@ export class Blobs {
headers['cache-control'] = 'max-age=0, stale-while-revalidate=60'
}

const res = await fetchAndRetry(this.fetcher, url, { body, method, headers })
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 fetchAndRetry(this.fetcher, url, options)

if (res.status === 404 && method === HTTPMethod.Get) {
return null
Expand Down Expand Up @@ -202,6 +218,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 = {
Expand Down