Skip to content

Support for Node 16.12 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
33 changes: 15 additions & 18 deletions lib/node-loader-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down