Skip to content

Commit d183c4c

Browse files
committed
Add support for CloudFlare workers
1 parent 7a1c0da commit d183c4c

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class Replicate {
4747
* @param {Function} [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
4848
*/
4949
constructor(options = {}) {
50-
this.auth = options.auth || process.env.REPLICATE_API_TOKEN;
50+
this.auth =
51+
options.auth ||
52+
(typeof process !== "undefined" ? process.env.REPLICATE_API_TOKEN : null);
5153
this.userAgent =
5254
options.userAgent || `replicate-javascript/${packageJSON.version}`;
5355
this.baseUrl = options.baseUrl || "https://api.replicate.com/v1";

lib/util.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,12 @@ async function validateWebhook(requestData, secret) {
4242

4343
if (body instanceof ReadableStream || body.readable) {
4444
try {
45-
const chunks = [];
46-
for await (const chunk of body) {
47-
chunks.push(Buffer.from(chunk));
48-
}
49-
body = Buffer.concat(chunks).toString("utf8");
45+
body = await new Response(body).text();
5046
} catch (err) {
5147
throw new Error(`Error reading body: ${err.message}`);
5248
}
53-
} else if (body instanceof Buffer) {
54-
body = body.toString("utf8");
49+
} else if (isTypedArray(body)) {
50+
body = await new Blob([body]).text();
5551
} else if (typeof body !== "string") {
5652
throw new Error("Invalid body type");
5753
}
@@ -231,9 +227,9 @@ async function transformFileInputs(inputs) {
231227
// a JavaScript implenentation like base64-js.
232228
// See: https://developer.mozilla.org/en-US/docs/Glossary/Base64
233229
// See: https://github.com/beatgammit/base64-js
234-
buffer = Buffer.from(await value.arrayBuffer());
230+
buffer = await value.arrayBuffer();
235231
mime = value.type;
236-
} else if (Buffer.isBuffer(value)) {
232+
} else if (isTypedArray(value)) {
237233
buffer = value;
238234
} else {
239235
return value;
@@ -246,7 +242,7 @@ async function transformFileInputs(inputs) {
246242
);
247243
}
248244

249-
const data = buffer.toString("base64");
245+
const data = bytesToBase64(buffer);
250246
mime = mime ?? "application/octet-stream";
251247

252248
return `data:${mime};base64,${data}`;
@@ -276,6 +272,20 @@ async function transform(value, mapper) {
276272
return await mapper(value);
277273
}
278274

275+
function isTypedArray(arr) {
276+
return (
277+
arr instanceof Int8Array ||
278+
arr instanceof Int16Array ||
279+
arr instanceof Int32Array ||
280+
arr instanceof Uint8Array ||
281+
arr instanceof Uint8ClampedArray ||
282+
arr instanceof Uint16Array ||
283+
arr instanceof Uint32Array ||
284+
arr instanceof Float32Array ||
285+
arr instanceof Float64Array
286+
);
287+
}
288+
279289
// Test for a plain JS object.
280290
// Source: lodash.isPlainObject
281291
function isPlainObject(value) {

0 commit comments

Comments
 (0)