diff options
author | 2023-08-06 20:39:40 -0700 | |
---|---|---|
committer | 2023-08-06 20:39:40 -0700 | |
commit | 0665733b0302ac7e743e4c131c7697741fa923a5 (patch) | |
tree | 2609e7055acbb308ddd52b45393a9b61fdc397bf | |
parent | 70c3371b14293e18a5e0470e8b8cd4c04259a606 (diff) | |
download | bun-0665733b0302ac7e743e4c131c7697741fa923a5.tar.gz bun-0665733b0302ac7e743e4c131c7697741fa923a5.tar.zst bun-0665733b0302ac7e743e4c131c7697741fa923a5.zip |
Fixes #4029 and fixes #4022 (#4032)
* Fixes #4029 and Fixes #4022
Fixes #4029
Fixes #4022
* Add test for all the fetches
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/js/out/InternalModuleRegistryConstants.h | 12 | ||||
-rw-r--r-- | src/js/thirdparty/isomorphic-fetch.js | 6 | ||||
-rw-r--r-- | src/js/thirdparty/node-fetch.js | 1 | ||||
-rw-r--r-- | test/js/node/fs/node-fetch.test.js | 44 |
4 files changed, 34 insertions, 29 deletions
diff --git a/src/js/out/InternalModuleRegistryConstants.h b/src/js/out/InternalModuleRegistryConstants.h index 5cd010c12..0ad5c4c97 100644 --- a/src/js/out/InternalModuleRegistryConstants.h +++ b/src/js/out/InternalModuleRegistryConstants.h @@ -209,11 +209,11 @@ static constexpr ASCIILiteral ThirdpartyDetectLibcLinuxCode = "(function (){\"us // // -static constexpr ASCIILiteral ThirdpartyIsomorphicFetchCode = "(function (){\"use strict\";return globalThis.fetch})\n"_s; +static constexpr ASCIILiteral ThirdpartyIsomorphicFetchCode = "(function (){\"use strict\";const bunFetch=Bun.fetch,fetch=(...args)=>bunFetch(...args);return fetch.default=fetch,fetch.fetch=fetch,fetch})\n"_s; // // -static constexpr ASCIILiteral ThirdpartyNodeFetchCode = "(function (){\"use strict\";const{Headers,Request,Response,Blob,File=Blob,FormData}=globalThis,realFetch=Bun.fetch;function fetch(...args){return realFetch(...args)}class AbortError extends DOMException{constructor(message){super(message,\"AbortError\")}}class FetchBaseError extends Error{constructor(message,type){super(message);this.type=type}}class FetchError extends FetchBaseError{constructor(message,type,systemError){super(message,type);this.code=systemError\?.code}}function blobFrom(path,options){return Promise.resolve(Bun.file(data))}function blobFromSync(path,options){return Bun.file(data)}var fileFrom=blobFrom,fileFromSync=blobFromSync;function isRedirect(code){return code===301||code===302||code===303||code===307||code===308}return Object.assign(fetch,{AbortError,Blob,FetchBaseError,FetchError,File,FormData,Headers,Request,Response,blobFrom,blobFromSync,fileFrom,fileFromSync,isRedirect,fetch})})\n"_s; +static constexpr ASCIILiteral ThirdpartyNodeFetchCode = "(function (){\"use strict\";const{Headers,Request,Response,Blob,File=Blob,FormData}=globalThis,realFetch=Bun.fetch;function fetch(...args){return realFetch(...args)}class AbortError extends DOMException{constructor(message){super(message,\"AbortError\")}}class FetchBaseError extends Error{constructor(message,type){super(message);this.type=type}}class FetchError extends FetchBaseError{constructor(message,type,systemError){super(message,type);this.code=systemError\?.code}}function blobFrom(path,options){return Promise.resolve(Bun.file(data))}function blobFromSync(path,options){return Bun.file(data)}var fileFrom=blobFrom,fileFromSync=blobFromSync;function isRedirect(code){return code===301||code===302||code===303||code===307||code===308}return Object.assign(fetch,{AbortError,Blob,FetchBaseError,FetchError,File,FormData,Headers,Request,Response,blobFrom,blobFromSync,fileFrom,fileFromSync,isRedirect,fetch,default:fetch})})\n"_s; // // @@ -434,11 +434,11 @@ static constexpr ASCIILiteral ThirdpartyDetectLibcLinuxCode = "(function (){\"us // // -static constexpr ASCIILiteral ThirdpartyIsomorphicFetchCode = "(function (){\"use strict\";return globalThis.fetch})\n"_s; +static constexpr ASCIILiteral ThirdpartyIsomorphicFetchCode = "(function (){\"use strict\";const bunFetch=Bun.fetch,fetch=(...args)=>bunFetch(...args);return fetch.default=fetch,fetch.fetch=fetch,fetch})\n"_s; // // -static constexpr ASCIILiteral ThirdpartyNodeFetchCode = "(function (){\"use strict\";const{Headers,Request,Response,Blob,File=Blob,FormData}=globalThis,realFetch=Bun.fetch;function fetch(...args){return realFetch(...args)}class AbortError extends DOMException{constructor(message){super(message,\"AbortError\")}}class FetchBaseError extends Error{constructor(message,type){super(message);this.type=type}}class FetchError extends FetchBaseError{constructor(message,type,systemError){super(message,type);this.code=systemError\?.code}}function blobFrom(path,options){return Promise.resolve(Bun.file(data))}function blobFromSync(path,options){return Bun.file(data)}var fileFrom=blobFrom,fileFromSync=blobFromSync;function isRedirect(code){return code===301||code===302||code===303||code===307||code===308}return Object.assign(fetch,{AbortError,Blob,FetchBaseError,FetchError,File,FormData,Headers,Request,Response,blobFrom,blobFromSync,fileFrom,fileFromSync,isRedirect,fetch})})\n"_s; +static constexpr ASCIILiteral ThirdpartyNodeFetchCode = "(function (){\"use strict\";const{Headers,Request,Response,Blob,File=Blob,FormData}=globalThis,realFetch=Bun.fetch;function fetch(...args){return realFetch(...args)}class AbortError extends DOMException{constructor(message){super(message,\"AbortError\")}}class FetchBaseError extends Error{constructor(message,type){super(message);this.type=type}}class FetchError extends FetchBaseError{constructor(message,type,systemError){super(message,type);this.code=systemError\?.code}}function blobFrom(path,options){return Promise.resolve(Bun.file(data))}function blobFromSync(path,options){return Bun.file(data)}var fileFrom=blobFrom,fileFromSync=blobFromSync;function isRedirect(code){return code===301||code===302||code===303||code===307||code===308}return Object.assign(fetch,{AbortError,Blob,FetchBaseError,FetchError,File,FormData,Headers,Request,Response,blobFrom,blobFromSync,fileFrom,fileFromSync,isRedirect,fetch,default:fetch})})\n"_s; // // @@ -660,11 +660,11 @@ static constexpr ASCIILiteral ThirdpartyDetectLibcLinuxCode = "(function (){\"us // // -static constexpr ASCIILiteral ThirdpartyIsomorphicFetchCode = "(function (){\"use strict\";return globalThis.fetch})\n"_s; +static constexpr ASCIILiteral ThirdpartyIsomorphicFetchCode = "(function (){\"use strict\";const bunFetch=Bun.fetch,fetch=(...args)=>bunFetch(...args);return fetch.default=fetch,fetch.fetch=fetch,fetch})\n"_s; // // -static constexpr ASCIILiteral ThirdpartyNodeFetchCode = "(function (){\"use strict\";const{Headers,Request,Response,Blob,File=Blob,FormData}=globalThis,realFetch=Bun.fetch;function fetch(...args){return realFetch(...args)}class AbortError extends DOMException{constructor(message){super(message,\"AbortError\")}}class FetchBaseError extends Error{constructor(message,type){super(message);this.type=type}}class FetchError extends FetchBaseError{constructor(message,type,systemError){super(message,type);this.code=systemError\?.code}}function blobFrom(path,options){return Promise.resolve(Bun.file(data))}function blobFromSync(path,options){return Bun.file(data)}var fileFrom=blobFrom,fileFromSync=blobFromSync;function isRedirect(code){return code===301||code===302||code===303||code===307||code===308}return Object.assign(fetch,{AbortError,Blob,FetchBaseError,FetchError,File,FormData,Headers,Request,Response,blobFrom,blobFromSync,fileFrom,fileFromSync,isRedirect,fetch})})\n"_s; +static constexpr ASCIILiteral ThirdpartyNodeFetchCode = "(function (){\"use strict\";const{Headers,Request,Response,Blob,File=Blob,FormData}=globalThis,realFetch=Bun.fetch;function fetch(...args){return realFetch(...args)}class AbortError extends DOMException{constructor(message){super(message,\"AbortError\")}}class FetchBaseError extends Error{constructor(message,type){super(message);this.type=type}}class FetchError extends FetchBaseError{constructor(message,type,systemError){super(message,type);this.code=systemError\?.code}}function blobFrom(path,options){return Promise.resolve(Bun.file(data))}function blobFromSync(path,options){return Bun.file(data)}var fileFrom=blobFrom,fileFromSync=blobFromSync;function isRedirect(code){return code===301||code===302||code===303||code===307||code===308}return Object.assign(fetch,{AbortError,Blob,FetchBaseError,FetchError,File,FormData,Headers,Request,Response,blobFrom,blobFromSync,fileFrom,fileFromSync,isRedirect,fetch,default:fetch})})\n"_s; // // diff --git a/src/js/thirdparty/isomorphic-fetch.js b/src/js/thirdparty/isomorphic-fetch.js index 7b36684e2..59c8a6d06 100644 --- a/src/js/thirdparty/isomorphic-fetch.js +++ b/src/js/thirdparty/isomorphic-fetch.js @@ -1 +1,5 @@ -export default globalThis.fetch; +const bunFetch = Bun.fetch; +const fetch = (...args) => bunFetch(...args); +fetch.default = fetch; +fetch.fetch = fetch; +export default fetch; diff --git a/src/js/thirdparty/node-fetch.js b/src/js/thirdparty/node-fetch.js index 899536d9b..f8bc9e3cd 100644 --- a/src/js/thirdparty/node-fetch.js +++ b/src/js/thirdparty/node-fetch.js @@ -60,4 +60,5 @@ export default Object.assign(fetch, { fileFromSync, isRedirect, fetch, + default: fetch, }); diff --git a/test/js/node/fs/node-fetch.test.js b/test/js/node/fs/node-fetch.test.js index 33af3252d..c174917a7 100644 --- a/test/js/node/fs/node-fetch.test.js +++ b/test/js/node/fs/node-fetch.test.js @@ -1,4 +1,6 @@ import fetch2, { fetch, Response, Request, Headers } from "node-fetch"; +import * as iso from "isomorphic-fetch"; +import * as vercelFetch from "@vercel/fetch"; import { test, expect } from "bun:test"; @@ -8,26 +10,24 @@ test("node-fetch", () => { expect(Headers).toBe(globalThis.Headers); }); -test("node-fetch fetches", async () => { - const server = Bun.serve({ - port: 0, - fetch(req, server) { - server.stop(); - return new Response(); - }, +for (const [impl, name] of [ + [fetch, "node-fetch.fetch"], + [fetch2, "node-fetch.default"], + [fetch2.default, "node-fetch.default.default"], + [iso.fetch, "isomorphic-fetch.fetch"], + [iso.default.fetch, "isomorphic-fetch.default.fetch"], + [iso.default, "isomorphic-fetch.default"], + [vercelFetch.default(fetch), "@vercel/fetch.default"], +]) { + test(name + " fetches", async () => { + const server = Bun.serve({ + port: 0, + fetch(req, server) { + server.stop(); + return new Response(); + }, + }); + expect(await impl("http://" + server.hostname + ":" + server.port)).toBeInstanceOf(Response); + server.stop(true); }); - expect(await fetch("http://" + server.hostname + ":" + server.port)).toBeInstanceOf(Response); - server.stop(true); -}); - -test("node-fetch.default fetches", async () => { - const server = Bun.serve({ - port: 0, - fetch(req, server) { - server.stop(); - return new Response(); - }, - }); - expect(await fetch2("http://" + server.hostname + ":" + server.port)).toBeInstanceOf(Response); - server.stop(true); -}); +} |