From 182b1e778cbfdcbc73f95f2de8345e5c0613fb00 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 8 Dec 2022 11:38:26 +0100 Subject: [PATCH] Do not use experimental fetch --- .../tendermint-rpc/src/rpcclients/http.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/tendermint-rpc/src/rpcclients/http.ts b/packages/tendermint-rpc/src/rpcclients/http.ts index f7ec0b41..1d389504 100644 --- a/packages/tendermint-rpc/src/rpcclients/http.ts +++ b/packages/tendermint-rpc/src/rpcclients/http.ts @@ -11,6 +11,23 @@ function filterBadStatus(res: any): any { return res; } +/** + * Node.js 18 comes with exprimental fetch support (https://nodejs.org/de/blog/announcements/v18-release-announce/). + * This is nice, but the implementation does not yet work wekk for us. We + * can just stick with axios on those systems for now. + */ +// eslint-disable-next-line @typescript-eslint/ban-types +function isExperimental(nodeJsFunc: Function): boolean { + // This works because we get this info in node 18: + // + // > fetch.toString() + // 'async function fetch(input, init = undefined) {\n' + + // " emitExperimentalWarning('The Fetch API');\n" + + // ' return lazyUndici().fetch(input, init);\n' + + // ' }' + return nodeJsFunc.toString().includes("emitExperimentalWarning"); +} + /** * Helper to work around missing CORS support in Tendermint (https://github.com/tendermint/tendermint/pull/2800) * @@ -23,7 +40,7 @@ export async function http( headers: Record | undefined, request?: any, ): Promise { - if (typeof fetch !== "undefined") { + if (typeof fetch === "function" && !isExperimental(fetch)) { const settings = { method: method, body: request ? JSON.stringify(request) : undefined,