Skip to content

Commit

Permalink
fixup! add test
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamingr committed Feb 16, 2022
1 parent c44d9ae commit 975be02
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/internal/modules/esm/fetch_module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const {
ObjectHasOwnProperty,
ObjectPrototypeHasOwnProperty,
PromisePrototypeThen,
SafeMap,
StringPrototypeEndsWith,
Expand Down Expand Up @@ -30,7 +30,7 @@ const { compose } = require('stream');
/**
* Only for GET requests, other requests would need new Map
* HTTP cache semantics keep diff caches
*
*
* It caches either the promise or the cahce entry since import.meta.url needs
* the value synchronously for the response location after all redirects.
*
Expand Down Expand Up @@ -104,12 +104,12 @@ function fetchWithRedirects(parsed) {
const req = handler(parsed, {
headers: { Accept: '*/*' },
});
// Note that `once` is used here to handle `error` and that it hits the
// Note that `once` is used here to handle `error` and that it hits the
// finally on network error/timeout.
const { 0: res } = await once(req, 'response');
try {
const isRedirect = res.statusCode >= 300 && res.statusCode <= 303;
const hasLocation = ObjectHasOwnProperty(res.headers, 'location');
const hasLocation = ObjectPrototypeHasOwnProperty(res.headers, 'location');
if (isRedirect && hasLocation) {
const location = new URL(res.headers.location, parsed);
if (location.protocol !== 'http:' && location.protocol !== 'https:') {
Expand All @@ -124,7 +124,7 @@ function fetchWithRedirects(parsed) {
return entry;
}
if (res.statusCode === 404) {
const err = new ERR_MODULE_NOT_FOUND(parsed.href);
const err = new ERR_MODULE_NOT_FOUND(parsed.href, null);
err.message = `Cannot find module '${parsed.href}', HTTP 404`;
throw err;
}
Expand Down
11 changes: 11 additions & 0 deletions test/es-module/test-http-imports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ for (const { protocol, createServer } of [
const server = createServer(function(_req, res) {
const url = new URL(_req.url, host);
const redirect = url.searchParams.get('redirect');
if (url.pathname === '/not-found') {
res.writeHead(404);
res.end();
return;
}
if (redirect) {
const { status, location } = JSON.parse(redirect);
res.writeHead(status, {
Expand Down Expand Up @@ -164,6 +169,12 @@ for (const { protocol, createServer } of [
import(unsupportedMIME.href),
'should not be able to load unsupported MIMEs from http:'
);
const notFound = new URL(url.href);
notFound.pathname = '/not-found';
await assert.rejects(
import(notFound.href),
{ code: 'ERR_MODULE_NOT_FOUND' },
);

server.close();
}
Expand Down

0 comments on commit 975be02

Please sign in to comment.