diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index c206b7e..27404fa 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -1,17 +1,26 @@ +name: Build and Test + on: - - push - - pull_request + push: + branches: + - main + pull_request: + branches: + - "*" jobs: - build-test: + build: runs-on: ubuntu-latest + steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: "16.13" - uses: pnpm/action-setup@v2.0.1 with: - version: 6.11.5 - run_install: | - - recursive: true - args: [--frozen-lockfile, --strict-peer-dependencies] + version: 6.20.3 + - run: pnpm install --frozen-lockfile + - run: pnpm test + - run: pnpm run check-format - run: pnpm run lint - - run: pnpm run test diff --git a/README.md b/README.md index 10140a4..198ff84 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ npm install --save @node-loader/http yarn add --save @node-loader/http ``` +NodeJS 16.12 changed the Node Loader API. If using NodeJS@<16.12, please use `@node-loader/http@1`. Otherwise, use `@node-loader/http@latest`. + ## Usage Create a file that imports a module over http: diff --git a/lib/node-loader-http.js b/lib/node-loader-http.js index d5d13d8..d98716e 100644 --- a/lib/node-loader-http.js +++ b/lib/node-loader-http.js @@ -25,7 +25,7 @@ export function resolve(specifier, context, defaultResolve) { return defaultResolve(specifier, context, defaultResolve); } -export function getFormat(url, context, defaultGetFormat) { +export async function load(url, context, defaultLoad) { if (useLoader(url)) { let format; // TODO: maybe change to content-type / mime type check rather than file extensions @@ -44,31 +44,28 @@ export function getFormat(url, context, defaultGetFormat) { format = "module"; } + let source; + + const httpResponse = await fetch(url); + + if (httpResponse.ok) { + source = await httpResponse.text(); + } else { + throw Error( + `Request to download javascript code from ${url} failed with HTTP status ${httpResponse.status} ${httpResponse.statusText}` + ); + } + return { + source, format, }; } - return defaultGetFormat(url, context, defaultGetFormat); + return defaultLoad(url, context, defaultLoad); } export function getSource(url, context, defaultGetSource) { - if (useLoader(url)) { - return fetch(url).then((r) => { - if (r.ok) { - return r.text().then((source) => { - return { - source, - }; - }); - } else { - throw Error( - `Request to download javascript code from ${url} failed with HTTP status ${r.status} ${r.statusText}` - ); - } - }); - } - return defaultGetSource(url, context, defaultGetSource); }