From 9b4bb64227633c5d8aaa864714229987694cea61 Mon Sep 17 00:00:00 2001 From: Mattt Date: Mon, 10 Jun 2024 03:55:14 -0700 Subject: [PATCH 1/3] Delete explicit `Content-Type` for `FormData` input (#268) * Delete explicit Content-Type for FormData input * Update tests --- index.js | 9 ++++++--- index.test.ts | 6 +----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 70cbe86..bf53449 100644 --- a/index.js +++ b/index.js @@ -220,12 +220,13 @@ class Replicate { url.searchParams.append(key, value); } - const headers = {}; + const headers = { + "Content-Type": "application/json", + "User-Agent": userAgent, + }; if (auth) { headers["Authorization"] = `Bearer ${auth}`; } - headers["Content-Type"] = "application/json"; - headers["User-Agent"] = userAgent; if (options.headers) { for (const [key, value] of Object.entries(options.headers)) { headers[key] = value; @@ -235,6 +236,8 @@ class Replicate { let body = undefined; if (data instanceof FormData) { body = data; + // biome-ignore lint/performance/noDelete: + delete headers["Content-Type"]; // Use automatic content type header } else if (data) { body = JSON.stringify(data); } diff --git a/index.test.ts b/index.test.ts index 834b786..8ecb5ae 100644 --- a/index.test.ts +++ b/index.test.ts @@ -296,7 +296,6 @@ describe("Replicate client", () => { nock(BASE_URL) .post("/files") - .matchHeader("Content-Type", "multipart/form-data") .reply(201, { urls: { get: "https://replicate.com/api/files/123", @@ -317,7 +316,6 @@ describe("Replicate client", () => { prompt: "Tell me a story", data, }, - stream: true, }); expect(client.fetch).toHaveBeenCalledWith( @@ -325,9 +323,7 @@ describe("Replicate client", () => { { method: "POST", body: expect.any(FormData), - headers: expect.objectContaining({ - "Content-Type": "multipart/form-data", - }), + headers: expect.any(Object), } ); const form = mockedFetch.mock.calls[0][1]?.body as FormData; From a345d0d721e654e180dfe898fc95e094d324d3f5 Mon Sep 17 00:00:00 2001 From: Mattt Date: Mon, 10 Jun 2024 04:05:17 -0700 Subject: [PATCH 2/3] Update blob creation to handle Buffer inputs correctly in createFile (#269) --- lib/files.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/files.js b/lib/files.js index f6620e9..520f2aa 100644 --- a/lib/files.js +++ b/lib/files.js @@ -15,7 +15,11 @@ async function createFile(file, metadata = {}) { blob = file; } else if (Buffer.isBuffer(file)) { filename = `buffer_${Date.now()}`; - blob = new Blob(file, { type: "application/octet-stream" }); + const bytes = new Uint8Array(file); + blob = new Blob([bytes], { + type: "application/octet-stream", + name: filename, + }); } else { throw new Error("Invalid file argument, must be a Blob, File or Buffer"); } From 4689e2bed0e44b1a099cd82f0c392a355b6c2fae Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Mon, 10 Jun 2024 04:05:37 -0700 Subject: [PATCH 3/3] 0.30.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index f268ac6..710a1e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "replicate", - "version": "0.30.1", + "version": "0.30.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "replicate", - "version": "0.30.1", + "version": "0.30.2", "license": "Apache-2.0", "devDependencies": { "@biomejs/biome": "^1.4.1", diff --git a/package.json b/package.json index 502ee21..f31fb22 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "replicate", - "version": "0.30.1", + "version": "0.30.2", "description": "JavaScript client for Replicate", "repository": "github:replicate/replicate-javascript", "homepage": "https://github.com/replicate/replicate-javascript#readme",