From f42f02380ad05bf617a18934ef2820f37a53b2dd Mon Sep 17 00:00:00 2001 From: Robert Long Date: Mon, 9 Oct 2023 11:29:30 -0700 Subject: [PATCH 1/3] Fix fetch scoping for cloudflare workers --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4f74985f..61bce83c 100644 --- a/index.js +++ b/index.js @@ -211,7 +211,8 @@ class Replicate { const shouldRetry = method === 'GET' ? (response) => (response.status === 429 || response.status >= 500) : (response) => (response.status === 429); - const response = await withAutomaticRetries(async () => this.fetch(url, init), { shouldRetry }); + const _fetch = this.fetch; + const response = await withAutomaticRetries(async () => _fetch(url, init), { shouldRetry }); if (!response.ok) { const request = new Request(url, init); From df68bbe6c0fb73b421cd6282ef1ad83c63adfb44 Mon Sep 17 00:00:00 2001 From: Mattt Date: Mon, 9 Oct 2023 15:48:30 -0700 Subject: [PATCH 2/3] Apply suggestions from code review --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 61bce83c..c043e839 100644 --- a/index.js +++ b/index.js @@ -211,6 +211,8 @@ class Replicate { const shouldRetry = method === 'GET' ? (response) => (response.status === 429 || response.status >= 500) : (response) => (response.status === 429); + // Workaround to fix `TypeError: Illegal invocation` error in Cloudflare Workers + // https://github.com/replicate/replicate-javascript/issues/134 const _fetch = this.fetch; const response = await withAutomaticRetries(async () => _fetch(url, init), { shouldRetry }); From 5008f566de4354886aa9b7cd20165641bdf66a8f Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Mon, 9 Oct 2023 15:58:17 -0700 Subject: [PATCH 3/3] Fix linting error --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c043e839..902ba572 100644 --- a/index.js +++ b/index.js @@ -211,9 +211,10 @@ class Replicate { const shouldRetry = method === 'GET' ? (response) => (response.status === 429 || response.status >= 500) : (response) => (response.status === 429); + // Workaround to fix `TypeError: Illegal invocation` error in Cloudflare Workers // https://github.com/replicate/replicate-javascript/issues/134 - const _fetch = this.fetch; + const _fetch = this.fetch; // eslint-disable-line no-underscore-dangle const response = await withAutomaticRetries(async () => _fetch(url, init), { shouldRetry }); if (!response.ok) {