arrange-act-assert
Version:
The lightweight "Act-Arrange-Assert" oriented testing framework
153 lines (152 loc) • 5.46 kB
JavaScript
;
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getSourceMap;
const Url = __importStar(require("url"));
const Path = __importStar(require("path"));
const Fs = __importStar(require("fs"));
const getHttpFile_1 = require("./getHttpFile");
const sourcemapMatch = /\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/g;
const protocolMatch = /^(?<protocol>[a-zA-Z]+)\:(?<data>[\s\S]+)/;
function extractSourceMapURLMagicComment(content) {
const matches = content.match(sourcemapMatch);
if (!matches) {
return null;
}
const match = matches[matches.length - 1];
if (match == null) {
return null;
}
return match.substring(match.indexOf("=") + 1);
}
function isSourceMap(sourceMap) {
return typeof sourceMap.file === "string" &&
Array.isArray(sourceMap.sources) &&
(sourceMap.sourceRoot == null || typeof sourceMap.sourceRoot === "string") &&
typeof sourceMap.version === "number" &&
typeof sourceMap.mappings === "string";
}
function urlToFile(file) {
try {
return Url.fileURLToPath(file);
}
catch (e) {
return file;
}
}
function processSourceMap(folder, sourceMapString) {
const sourceMap = JSON.parse(sourceMapString);
if (isSourceMap(sourceMap)) {
for (let i = 0; i < sourceMap.sources.length; i++) {
let source = urlToFile(sourceMap.sources[i]);
const sourceRoot = urlToFile(sourceMap.sourceRoot || "");
if (sourceRoot) {
source = Path.join(sourceRoot, source);
}
sourceMap.sources[i] = Path.resolve(folder, source);
}
sourceMap.sourceRoot = "";
sourceMap.file = Path.resolve(folder, sourceMap.file);
return sourceMap;
}
return null;
}
function getSourceMapFromData(folder, data) {
const formatIndex = data.indexOf(",");
if (formatIndex > -1) {
const format = data.substring(0, formatIndex).split(";");
let sourceMapString = data.substring(formatIndex + 1);
switch (format[0]) {
case "application/json": {
if (format[format.length - 1] === "base64") {
sourceMapString = Buffer.from(sourceMapString, "base64").toString("utf8");
}
return processSourceMap(folder, sourceMapString);
}
}
}
}
async function getSourceMapFromFile(folder, file, readFile) {
try {
return processSourceMap(folder, await readFile(file));
}
catch (e) { }
return null;
}
async function getSourceMapFromHttp(folder, url, getHttpFile) {
try {
return processSourceMap(folder, await getHttpFile(url));
}
catch (e) { }
return null;
}
async function processSourceMapUrl(folder, sourceMapURL, context) {
const protocol = sourceMapURL.match(protocolMatch);
if (protocol && protocol.groups) {
switch (protocol.groups.protocol) {
case "data": {
const sourceMap = getSourceMapFromData(folder, protocol.groups.data);
if (sourceMap) {
return sourceMap;
}
}
case "https":
case "http": {
const sourceMap = await getSourceMapFromHttp(folder, sourceMapURL, context.getHttpFile);
if (sourceMap) {
return sourceMap;
}
}
}
}
return await getSourceMapFromFile(folder, Path.resolve(folder, sourceMapURL), context.readFile);
}
async function getSourceMap(folder, code, context) {
try {
const sourceMapURL = extractSourceMapURLMagicComment(code);
if (sourceMapURL) {
const data = await processSourceMapUrl(folder, sourceMapURL, {
readFile: file => Fs.promises.readFile(file, "utf8"),
getHttpFile: getHttpFile_1.getHttpFile,
...context
});
if (data) {
return data;
}
}
}
catch (e) { }
return null;
}