83 lines
4.2 KiB
JavaScript
83 lines
4.2 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.process = void 0;
|
|
const url_1 = __importDefault(require("url"));
|
|
const page_1 = __importDefault(require("./page"));
|
|
const manifest_1 = __importDefault(require("./manifest"));
|
|
const script_1 = __importDefault(require("./script"));
|
|
const stylesheet_1 = __importDefault(require("./stylesheet"));
|
|
const urlUtil = __importStar(require("../../utils/url"));
|
|
const encoding_1 = require("../encoding");
|
|
const os_1 = require("os");
|
|
const IS_WIN = (0, os_1.platform)() === 'win32';
|
|
const DISK_RE = /^[A-Za-z]:/;
|
|
const RESOURCE_PROCESSORS = [page_1.default, manifest_1.default, script_1.default, stylesheet_1.default];
|
|
function getResourceUrlReplacer(ctx) {
|
|
return function urlReplacer(resourceUrl, resourceType, charsetAttrValue, baseUrl, isCrossDomain = false, isUrlsSet = false) {
|
|
if (isUrlsSet)
|
|
return urlUtil.handleUrlsSet(urlReplacer, resourceUrl, resourceType, charsetAttrValue, baseUrl, isCrossDomain);
|
|
if (!urlUtil.isSupportedProtocol(resourceUrl) && !urlUtil.isSpecialPage(resourceUrl))
|
|
return resourceUrl;
|
|
if (IS_WIN && ctx.dest.protocol === 'file:' && DISK_RE.test(resourceUrl))
|
|
resourceUrl = '/' + resourceUrl;
|
|
// NOTE: Resolves base URLs without a protocol ('//google.com/path' for example).
|
|
baseUrl = baseUrl ? url_1.default.resolve(ctx.dest.url, baseUrl) : '';
|
|
resourceUrl = urlUtil.processSpecialChars(resourceUrl);
|
|
let resolvedUrl = url_1.default.resolve(baseUrl || ctx.dest.url, resourceUrl);
|
|
if (!urlUtil.isValidUrl(resolvedUrl))
|
|
return resourceUrl;
|
|
// NOTE: Script or <link rel='preload' as='script'>
|
|
const isScriptLike = urlUtil.parseResourceType(resourceType).isScript;
|
|
const charsetStr = charsetAttrValue || isScriptLike && ctx.contentInfo.charset.get();
|
|
resolvedUrl = urlUtil.ensureTrailingSlash(resourceUrl, resolvedUrl);
|
|
if (!urlUtil.isValidUrl(resolvedUrl))
|
|
return resolvedUrl;
|
|
return ctx.toProxyUrl(resolvedUrl, isCrossDomain, resourceType, charsetStr);
|
|
};
|
|
}
|
|
async function process(ctx) {
|
|
const { destResBody, contentInfo } = ctx;
|
|
const { encoding, charset } = contentInfo;
|
|
const decoded = await (0, encoding_1.decodeContent)(destResBody, encoding, charset);
|
|
for (const processor of RESOURCE_PROCESSORS) {
|
|
if (!processor.shouldProcessResource(ctx))
|
|
continue;
|
|
const urlReplacer = getResourceUrlReplacer(ctx);
|
|
if (page_1.default === processor)
|
|
await ctx.prepareInjectableUserScripts(ctx.eventFactory, ctx.session.injectable.userScripts);
|
|
// @ts-ignore: Cannot invoke an expression whose type lacks a call signature
|
|
const processed = processor.processResource(decoded, ctx, charset, urlReplacer);
|
|
if (processed === page_1.default.RESTART_PROCESSING)
|
|
return await process(ctx);
|
|
return await (0, encoding_1.encodeContent)(processed, encoding, charset);
|
|
}
|
|
return destResBody;
|
|
}
|
|
exports.process = process;
|