From ed9593e09d6aac22b8418029e9967e023bc65163 Mon Sep 17 00:00:00 2001 From: GrygrFlzr Date: Thu, 15 Apr 2021 13:55:18 +0700 Subject: [PATCH 1/3] Prefer native fetch over node-fetch --- .changeset/chatty-clouds-listen.md | 5 +++++ .../kit/src/runtime/server/page/load_node.js | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 .changeset/chatty-clouds-listen.md diff --git a/.changeset/chatty-clouds-listen.md b/.changeset/chatty-clouds-listen.md new file mode 100644 index 000000000000..1e80d4391126 --- /dev/null +++ b/.changeset/chatty-clouds-listen.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Prefer native fetch over node-fetch when available diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index 3b24d6e0c93d..f6facc059387 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -1,8 +1,20 @@ -import fetch, { Response } from 'node-fetch'; +import nodeFetch from 'node-fetch'; import { parse, resolve } from 'url'; import { normalize } from '../../load.js'; import { ssr } from '../index.js'; +// only use node-fetch when native unavailable +if (typeof fetch !== 'function') { + // @ts-ignore mismatch between native fetch and node-fetch + globalThis.fetch = nodeFetch.bind({}); + // @ts-ignore + globalThis.Response = nodeFetch.Response; + // @ts-ignore + globalThis.Request = nodeFetch.Request; + // @ts-ignore + globalThis.Headers = nodeFetch.Headers; +} + const s = JSON.stringify; /** @@ -98,6 +110,7 @@ export async function load_node({ // external fetch response = await fetch( parsed.href, + // @ts-ignore mismatch between native fetch and node-fetch /** @type {import('node-fetch').RequestInit} */ (opts) ); } else { @@ -125,6 +138,7 @@ export async function load_node({ // TODO we need to know what protocol to use response = await fetch( `http://${page.host}/${asset.file}`, + // @ts-ignore mismatch between native fetch and node-fetch /** @type {import('node-fetch').RequestInit} */ (opts) ); } From 2274e889ee96334d3d1aabcad44d559270f81ce7 Mon Sep 17 00:00:00 2001 From: GrygrFlzr Date: Thu, 15 Apr 2021 14:01:14 +0700 Subject: [PATCH 2/3] Remove excess ts-ignore --- packages/kit/src/runtime/server/page/load_node.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index f6facc059387..dcb1e78cbcb4 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -71,7 +71,6 @@ export async function load_node({ * @param {RequestInfo} resource * @param {RequestInit} opts */ - // @ts-ignore mismatch between client fetch and node-fetch fetch: async (resource, opts = {}) => { /** @type {string} */ let url; @@ -108,11 +107,7 @@ export async function load_node({ if (parsed.protocol) { // external fetch - response = await fetch( - parsed.href, - // @ts-ignore mismatch between native fetch and node-fetch - /** @type {import('node-fetch').RequestInit} */ (opts) - ); + response = await fetch(parsed.href, /** @type {RequestInit} */ (opts)); } else { // otherwise we're dealing with an internal fetch const resolved = resolve(request.path, parsed.pathname); @@ -138,8 +133,7 @@ export async function load_node({ // TODO we need to know what protocol to use response = await fetch( `http://${page.host}/${asset.file}`, - // @ts-ignore mismatch between native fetch and node-fetch - /** @type {import('node-fetch').RequestInit} */ (opts) + /** @type {RequestInit} */ (opts) ); } } From 13033d8efd3ae8c14038be56b6a2ef26a053ff7e Mon Sep 17 00:00:00 2001 From: GrygrFlzr Date: Thu, 15 Apr 2021 14:41:09 +0700 Subject: [PATCH 3/3] Deconstruct instead of field access --- packages/kit/src/runtime/server/page/load_node.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index dcb1e78cbcb4..c8064f4d3b26 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -1,4 +1,8 @@ -import nodeFetch from 'node-fetch'; +import nodeFetch, { + Response as NodeResponse, + Request as NodeRequest, + Headers as NodeHeaders +} from 'node-fetch'; import { parse, resolve } from 'url'; import { normalize } from '../../load.js'; import { ssr } from '../index.js'; @@ -6,13 +10,13 @@ import { ssr } from '../index.js'; // only use node-fetch when native unavailable if (typeof fetch !== 'function') { // @ts-ignore mismatch between native fetch and node-fetch - globalThis.fetch = nodeFetch.bind({}); + globalThis.fetch = nodeFetch; // @ts-ignore - globalThis.Response = nodeFetch.Response; + globalThis.Response = NodeResponse; // @ts-ignore - globalThis.Request = nodeFetch.Request; + globalThis.Request = NodeRequest; // @ts-ignore - globalThis.Headers = nodeFetch.Headers; + globalThis.Headers = NodeHeaders; } const s = JSON.stringify;