From 05754bf3897be5064bccb958568a15fcc2fbb1cf Mon Sep 17 00:00:00 2001 From: omrilotan Date: Thu, 30 May 2024 16:33:13 +0100 Subject: [PATCH] Support SourceMap response header --- CHANGELOG.md | 4 ++++ bin.js | 7 +++++-- index.js | 5 ----- lib/getSourceCodeMapUrl/index.js | 5 +---- package.json | 2 +- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99ef5cd..05f3b8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [2.2.0](https://github.com/omrilotan/isbot/compare/v2.1.0...v2.2.0) + +- Support reading sourcemap location from [SourceMap header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/SourceMap) + ## [2.1.0](https://github.com/omrilotan/isbot/compare/v2.0.0...v2.1.0) - Support reading and creating a config file (`colombo --config`) diff --git a/bin.js b/bin.js index 9a914ec..490f25f 100755 --- a/bin.js +++ b/bin.js @@ -4,6 +4,7 @@ import { readFile } from "node:fs/promises"; import inquirer from "inquirer"; import chalk from "chalk"; import semver from "semver"; +import sourceMappingURL from "source-map-url"; import { config } from "./lib/config/index.js"; import { getSourceCodeMapUrl } from "./lib/getSourceCodeMapUrl/index.js"; import { loader } from "./lib/loader/index.js"; @@ -90,9 +91,11 @@ async function start() { `Failed to load file ${clean}: ${response.status} ${response.statusText}`, ); } - const code = await response.text(); + const file = + response.headers.get("SourceMap") || + sourceMappingURL.getFrom(await response.text()); loader.end(); - const mapUrl = getSourceCodeMapUrl(code, clean); + const mapUrl = getSourceCodeMapUrl(file, clean); if (mapUrl) { ({ url } = await prompt([ diff --git a/index.js b/index.js index a165678..8d0911d 100644 --- a/index.js +++ b/index.js @@ -67,11 +67,6 @@ async function getData(url, { headers } = {}) { `Failed to load file ${url}: ${response.status} ${response.statusText}`, ); } - if (!response.ok) { - throw new Error( - `${url} returned status ${response.status} ${response.statusText}`, - ); - } const data = await response.text(); if ( diff --git a/lib/getSourceCodeMapUrl/index.js b/lib/getSourceCodeMapUrl/index.js index 83552a3..ff833c9 100644 --- a/lib/getSourceCodeMapUrl/index.js +++ b/lib/getSourceCodeMapUrl/index.js @@ -1,10 +1,7 @@ import { dirname, join } from "node:path"; import { URL } from "node:url"; -import sourceMappingURL from "source-map-url"; - -export function getSourceCodeMapUrl(source, url) { - const file = sourceMappingURL.getFrom(source); +export function getSourceCodeMapUrl(file, url) { if (!file) { return; } diff --git a/package.json b/package.json index 05fcd28..f919506 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "colombo", - "version": "2.1.0", + "version": "2.2.0", "description": "🕵️‍♂️ View Javascript source code using source-map from CLI", "keywords": [ "sourcemap",