Skip to content
Open
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

A [nodejs loader](https://nodejs.org/dist/latest-v13.x/docs/api/esm.html#esm_experimental_loaders) for loading modules over the network with http/https.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

**Table of Contents** _generated with [DocToc](https://github.com/thlorenz/doctoc) (`npx doctoc README.md`)_

- [Installation](#installation)
- [Usage](#usage)
- [Configure cache behaviour](#configure-cache-behaviour)
- [Semantics](#semantics)
- [Composition](#composition)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Installation

```sh
Expand All @@ -27,11 +40,20 @@ Now run node with the `--experimental-loader` flag:
node --experimental-loader @node-loader/http file.js
```

### Configure cache behaviour

Override default `make-fetch-happen` [options](https://github.com/npm/make-fetch-happen#--make-fetch-happen-options) via specifying the `HTTP_IMPORT_CACHING` (`opts.cache`) environment variable, and/or the `HTTP_IMPORT_CACHDIR` (`opts.cachePath`) environment variable.

```sh
HTTP_IMPORT_CACHING=default \
HTTP_IMPORT_CACHDIR=.local-cache \
node --experimental-loader @node-loader/http file.js
```

## Semantics

This project uses [node-fetch](https://github.com/node-fetch/node-fetch) to implement familiar HTTP semantics,
including http redirects, https redirects, HTTP status checks, etc. Customizing the behavior of node-fetch is
planned, but not yet implemented.
This project uses [make-fetch-happen](https://github.com/npm/make-fetch-happen) to implement familiar HTTP semantics,
including http redirects, https redirects, HTTP status checks, etc.

## Composition

Expand Down
43 changes: 41 additions & 2 deletions lib/node-loader-http.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
import fetch from "node-fetch";
import fetch from "make-fetch-happen";
import path from "path";
import { homedir } from "os";

const HTTP_IMPORT_CACHING = process.env.HTTP_IMPORT_CACHING || "default";

const allowedStrategies = [
"default",
"no-store",
"reload",
"no-cache",
"force-cache",
"only-if-cached",
];

if (!allowedStrategies.includes(HTTP_IMPORT_CACHING)) {
throw new Error(
`Incorrect HTTP_IMPORT_CACHING value "${HTTP_IMPORT_CACHING}". Please refer to make-fetch-happen documentation https://github.com/npm/make-fetch-happen#--optscache for allowed values`
);
}

const HTTP_IMPORT_CACHDIR =
process.env.HTTP_IMPORT_CACHDIR ||
(process.platform === "darwin"
? path.join(homedir(), "Library", "Caches", "node-loader-http")
: process.platform === "win32"
? path.join(
process.env.LOCALAPPDATA || path.join(homedir(), "AppData", "Local"),
"node-loader-http-cache"
)
: path.join(
process.env.XDG_CACHE_HOME || path.join(homedir(), ".cache"),
"node-loader-http"
));

console.info("@node-loader/http", "cache =", HTTP_IMPORT_CACHING);
console.info("@node-loader/http", "cachePath =", HTTP_IMPORT_CACHDIR);

export function resolve(specifier, context, defaultResolve) {
const { parentURL = null } = context;
Expand Down Expand Up @@ -46,7 +82,10 @@ export async function load(url, context, defaultLoad) {

let source;

const httpResponse = await fetch(url);
const httpResponse = await fetch(url, {
cachePath: HTTP_IMPORT_CACHDIR,
cache: HTTP_IMPORT_CACHING,
});

if (httpResponse.ok) {
source = await httpResponse.text();
Expand Down
Loading