From 0b007715a622b0a71e7830a739b9cea57cddf934 Mon Sep 17 00:00:00 2001 From: Joel Denning Date: Mon, 13 Apr 2020 10:56:14 -0600 Subject: [PATCH] Switching to node-fetch to avoid caching bugs in make-fetch-happen (#2171) * Workaround from caching problem within make-fetch-happen for 304s. * Switching to node-fetch * Docs * Fixing race conditino in build --- docs/nodejs.md | 18 ++---------------- package.json | 5 +++-- src/features/node-fetch.js | 22 +--------------------- src/system-node.js | 2 -- 4 files changed, 6 insertions(+), 41 deletions(-) diff --git a/docs/nodejs.md b/docs/nodejs.md index a3c21afe..bd786c02 100644 --- a/docs/nodejs.md +++ b/docs/nodejs.md @@ -12,7 +12,7 @@ yarn add systemjs ``` ```js -const { System, applyImportMap, clearFetchCache, setBaseUrl } = require('systemjs'); +const { System, applyImportMap, setBaseUrl } = require('systemjs'); System.import('file:///Users/name/some-module.js').then(module => { console.log("The loaded module", module); @@ -29,7 +29,7 @@ Separate instances of SystemJS, each with their own import map and module regist Additionally, [global loading](/README.md#extras), [module types](/docs/module-types.md), and the [SystemJS Registry API](/docs/api.md#registry) are supported. Other extras, such as the AMD extra, have not been thoroughly tested but are presumed to work. -Modules loaded over HTTP will be cached on disk, according to the [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) header. This caching is performed by [make-fetch-happen](https://github.com/npm/make-fetch-happen). +Modules loaded over HTTP will be loaded via [node-fetch](https://github.com/node-fetch/node-fetch). ## API @@ -106,18 +106,4 @@ setBaseUrl(System, 'https://example.com/base/'); // Use a file URL as the base setBaseUrl(System, 'file:///Users/name/some-dir/'); setBaseUrl(System, pathToFileURL(path.join(process.cwd(), 'some-dir')) + path.sep); -``` - -### clearFetchCache() -> undefined - -SystemJS uses [make-fetch-happen](https://github.com/npm/make-fetch-happen) to manage a disk cache that respects the [`Cache-Control`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) header. The `clearFetchCache` API clears the disk cache of modules loaded over the network. - -Calling `clearFetchCache` ensures that subsequent modules will be loaded from the network instead of disk cache. This does not impact any existing SystemJS modules in the registry. - -Note that the disk cache is reused by all NodeJS programs that use SystemJS on any particular machine. This includes reusing the cache after shutting down the program and restarting it. - -```js -const { clearFetchCache } = require('systemjs'); - -clearFetchCache(); ``` \ No newline at end of file diff --git a/package.json b/package.json index 9df9ca6a..3e054d7c 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,9 @@ "concurrently": "^5.1.0", "construct-style-sheets-polyfill": "^2.3.5", "cross-env": "^7.0.2", - "make-fetch-happen": "^8.0.4", + "mkdirp": "^1.0.4", "mocha": "^7.1.1", + "node-fetch": "^2.6.0", "open": "^7.0.3", "rimraf": "^3.0.2", "rollup": "^2.2.0", @@ -42,7 +43,7 @@ "whatwg-fetch": "^3.0.0" }, "scripts": { - "build": "rimraf dist && concurrently -n w: 'npm:build:*'", + "build": "rimraf dist && mkdirp dist && concurrently -n w: 'npm:build:*'", "build:node": "node --experimental-modules build-node.js", "build:browser": "rollup -c", "build-browser-dev": "rollup -c --environment dev", diff --git a/src/features/node-fetch.js b/src/features/node-fetch.js index 2d2f3887..b194093f 100644 --- a/src/features/node-fetch.js +++ b/src/features/node-fetch.js @@ -1,30 +1,10 @@ import sourceMapSupport from 'source-map-support'; -import makeFetchHappen from 'make-fetch-happen'; -import path from 'path'; +import fetch from 'node-fetch'; import { promises as fs } from 'fs'; import { fileURLToPath } from 'url'; -import rimraf from 'rimraf'; -import os from 'os'; - -export let clearFetchCache; sourceMapSupport.install(); -const home = os.homedir(); -let cacheDir; -if (process.platform === 'darwin') - cacheDir = path.join(home, 'Library', 'Caches', 'systemjs'); -else if (process.platform === 'win32') - cacheDir = path.join(process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'), 'systemjs-cache'); -else - cacheDir = path.join(process.env.XDG_CACHE_HOME || path.join(home, '.cache'), 'systemjs'); - -clearFetchCache = function () { - rimraf.sync(path.join(cacheDir, 'fetch-cache')); -}; - -const fetch = makeFetchHappen.defaults({ cacheManager: path.join(cacheDir, 'fetch-cache')}); - global.System.constructor.prototype.shouldFetch = () => true; global.System.constructor.prototype.fetch = async url => { if (url.startsWith('file:')) { diff --git a/src/system-node.js b/src/system-node.js index af9a6f0d..a403b303 100644 --- a/src/system-node.js +++ b/src/system-node.js @@ -23,8 +23,6 @@ export function applyImportMap(loader, newMap, mapBase) { loader[IMPORT_MAP_PROMISE] = Promise.resolve(); } -export { clearFetchCache } from './features/node-fetch.js'; - export function setBaseUrl(loader, url) { ensureValidSystemLoader(loader); loader[BASE_URL] = new URL(url).href;