UNPKG

@splunk/rum-cli

Version:

Tools for handling symbol and mapping files for symbolication

136 lines (135 loc) 6.52 kB
"use strict"; /* * Copyright Splunk Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.discoverJsMapFilePath = discoverJsMapFilePath; const filesystem_1 = require("../utils/filesystem"); const node_path_1 = __importDefault(require("node:path")); const utils_1 = require("./utils"); /** * Determine the corresponding ".map" file for the given jsFilePath. * * Strategy: * * 1) Append ".map" to the jsFilePath. If we already know this file exists, return it as the match. * This is a common naming convention for source map files. * * 2) Fallback to the "//# sourceMappingURL=..." comment in the JS file. * If this comment is present, and we detect it is a relative file path, return this value as the match. */ function discoverJsMapFilePath(jsFilePath, allJsMapFilePaths, options, logger) { return __awaiter(this, void 0, void 0, function* () { var _a, e_1, _b, _c; /* * Check if we already know about the map file by adding ".map" extension. This is a common convention. */ if (allJsMapFilePaths.includes(`${jsFilePath}.map`)) { const result = `${jsFilePath}.map`; logger.debug(`found source map pair (using standard naming convention):`); logger.debug(` - ${jsFilePath}`); logger.debug(` - ${result}`); return result; } /* * Fallback to reading the JS file and parsing its "//# sourceMappingURL=..." comment */ let sourceMappingUrlLine = null; try { const fileStream = (0, filesystem_1.makeReadStream)(jsFilePath); try { for (var _d = true, _e = __asyncValues((0, filesystem_1.readlines)(fileStream)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const line = _c; if (line.startsWith(utils_1.SOURCE_MAPPING_URL_COMMENT_PREFIX)) { sourceMappingUrlLine = line; break; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } } catch (e) { (0, utils_1.throwJsFileReadError)(e, jsFilePath, options); } let result = null; if (sourceMappingUrlLine) { result = resolveSourceMappingUrlToFilePath(sourceMappingUrlLine, jsFilePath, allJsMapFilePaths, logger); } if (result === null) { logger.debug(`no source map found for ${jsFilePath}`); } return result; }); } /** * Parse the sourceMappingURL comment to a file path, or return null if the value is unsupported by our inject tool. * * Given the jsFilePath "path/file.js": * - "//# sourceMappingURL=file.map.js" is a relative path, and "path/file.map.js" will be returned * - "//# sourceMappingURL=http://..." is not a relative path, and null will be returned */ function resolveSourceMappingUrlToFilePath(line, jsFilePath, allJsMapFilePaths, logger) { const url = line.slice(utils_1.SOURCE_MAPPING_URL_COMMENT_PREFIX.length).trim(); if (node_path_1.default.isAbsolute(url) || url.startsWith('http://') || url.startsWith('https://') || url.startsWith('data:')) { logger.debug(`skipping source map pair (unsupported sourceMappingURL comment):`); logger.debug(` - ${jsFilePath}`); logger.debug(` - ${url}`); return null; } const matchingJsMapFilePath = node_path_1.default.join(node_path_1.default.dirname(jsFilePath), url); if (!allJsMapFilePaths.includes(matchingJsMapFilePath)) { logger.debug(`skipping source map pair (file not in provided directory):`); logger.debug(` - ${jsFilePath}`); logger.debug(` - ${url}`); logger.warn(`skipping ${jsFilePath}, which is requesting a source map file outside of the provided --path`); return null; } else { logger.debug(`found source map pair (using sourceMappingURL comment):`); logger.debug(` - ${jsFilePath}`); logger.debug(` - ${matchingJsMapFilePath}`); return matchingJsMapFilePath; } }