split HttpUrlPlugin into two plugins for http and https

This commit is contained in:
Tobias Koppers 2020-07-06 17:45:09 +02:00
parent 5f4c4662dd
commit 7bbc2aa3ce
5 changed files with 108 additions and 60 deletions

View File

@ -432,6 +432,9 @@ module.exports = mergeExports(fn, {
schemes: {
get HttpUriPlugin() {
return require("./schemes/HttpUriPlugin");
},
get HttpsUriPlugin() {
return require("./schemes/HttpsUriPlugin");
}
}
}

View File

@ -7,7 +7,6 @@
const { URL } = require("url");
const NormalModule = require("../NormalModule");
const memorize = require("../util/memorize");
/** @typedef {import("../Compiler")} Compiler */
@ -21,66 +20,41 @@ class HttpUriPlugin {
compiler.hooks.compilation.tap(
"HttpUriPlugin",
(compilation, { normalModuleFactory }) => {
const resolveHandler = resourceData => {
const url = new URL(resourceData.resource);
resourceData.path = url.origin + url.pathname;
resourceData.query = url.search;
resourceData.fragment = url.hash;
return /** @type {true} */ (true);
};
const readHandler = (scheme, getBuiltin) => (
resource,
module,
callback
) => {
return getBuiltin().get(new URL(resource), res => {
if (res.statusCode !== 200) {
res.destroy();
return callback(
new Error(`${scheme} request status code = ${res.statusCode}`)
);
}
const bufferArr = [];
res.on("data", chunk => {
bufferArr.push(chunk);
});
res.on("end", () => {
if (!res.complete) {
return callback(new Error(`${scheme} request was terminated`));
}
callback(null, Buffer.concat(bufferArr));
});
});
};
normalModuleFactory.hooks.resolveForScheme
.for("http")
.tap("HttpUriPlugin", resolveHandler);
normalModuleFactory.hooks.resolveForScheme
.for("https")
.tap("HttpUriPlugin", resolveHandler);
.tap("HttpUriPlugin", resourceData => {
const url = new URL(resourceData.resource);
resourceData.path = url.origin + url.pathname;
resourceData.query = url.search;
resourceData.fragment = url.hash;
return /** @type {true} */ (true);
});
NormalModule.getCompilationHooks(compilation)
.readResourceForScheme.for("http")
.tapAsync(
"HttpUriPlugin",
readHandler(
"http",
memorize(() => require("http"))
)
);
NormalModule.getCompilationHooks(compilation)
.readResourceForScheme.for("https")
.tapAsync(
"HttpUriPlugin",
readHandler(
"https",
memorize(() => require("https"))
)
);
.tapAsync("HttpUriPlugin", (resource, module, callback) => {
return require("http").get(new URL(resource), res => {
if (res.statusCode !== 200) {
res.destroy();
return callback(
new Error(`http request status code = ${res.statusCode}`)
);
}
const bufferArr = [];
res.on("data", chunk => {
bufferArr.push(chunk);
});
res.on("end", () => {
if (!res.complete) {
return callback(new Error("http request was terminated"));
}
callback(null, Buffer.concat(bufferArr));
});
});
});
}
);
}

View File

@ -0,0 +1,63 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { URL } = require("url");
const NormalModule = require("../NormalModule");
/** @typedef {import("../Compiler")} Compiler */
class HttpsUriPlugin {
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(
"HttpsUriPlugin",
(compilation, { normalModuleFactory }) => {
normalModuleFactory.hooks.resolveForScheme
.for("https")
.tap("HttpsUriPlugin", resourceData => {
const url = new URL(resourceData.resource);
resourceData.path = url.origin + url.pathname;
resourceData.query = url.search;
resourceData.fragment = url.hash;
return /** @type {true} */ (true);
});
NormalModule.getCompilationHooks(compilation)
.readResourceForScheme.for("https")
.tapAsync("HttpsUriPlugin", (resource, module, callback) => {
return require("https").get(new URL(resource), res => {
if (res.statusCode !== 200) {
res.destroy();
return callback(
new Error(`https request status code = ${res.statusCode}`)
);
}
const bufferArr = [];
res.on("data", chunk => {
bufferArr.push(chunk);
});
res.on("end", () => {
if (!res.complete) {
return callback(new Error("https request was terminated"));
}
callback(null, Buffer.concat(bufferArr));
});
});
});
}
);
}
}
module.exports = HttpsUriPlugin;

View File

@ -1,4 +1,4 @@
const { HttpUriPlugin } = require("../../../../").experiments.schemes;
const { HttpsUriPlugin } = require("../../../../").experiments.schemes;
/** @type {import("../../../../").Configuration} */
module.exports = {
@ -11,5 +11,5 @@ module.exports = {
}
]
},
plugins: [new HttpUriPlugin()]
plugins: [new HttpsUriPlugin()]
};

10
types.d.ts vendored
View File

@ -2867,6 +2867,14 @@ declare class HttpUriPlugin {
*/
apply(compiler: Compiler): void;
}
declare class HttpsUriPlugin {
constructor();
/**
* Apply the plugin
*/
apply(compiler: Compiler): void;
}
declare class IgnorePlugin {
constructor(options: IgnorePluginOptions);
options: IgnorePluginOptions;
@ -8646,7 +8654,7 @@ declare namespace exports {
}
export namespace experiments {
export namespace schemes {
export { HttpUriPlugin };
export { HttpUriPlugin, HttpsUriPlugin };
}
}
export type WebpackPluginFunction = (