Registry / http-networking / get-source

get-source

JSON →
library2.0.12jsnpmunverified

`get-source` is a JavaScript utility designed to fetch and resolve source-mapped source files in both Node.js and browser environments. Currently stable at version 2.0.12, it provides both synchronous and asynchronous APIs to read file contents and traverse sourcemap chains. Its core differentiating features include comprehensive sourcemap support—handling external, embedded, inline links, and long chains—and a built-in cache for performance. It's particularly useful for enhancing call stacks, advanced logging, and creating error display components in front-end development, as demonstrated by its use in libraries like `StackTracey` and `ololog`. The library ships with TypeScript types, making it suitable for modern JavaScript and TypeScript projects. While the synchronous API is designed not to throw errors, the asynchronous API utilizes standard Promise-based error handling.

npm install get-source
INSTALL
IMPORT
SIG · GET-SOURCE
G
get-source
http-networkingjavascriptv2.0.12
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getSource
import getSource from 'get-source'
import { getSource } from 'get-source'
This is the default export, primarily used for the synchronous API. Avoid named import or CommonJS require().
getSource.async
import getSource from 'get-source'; const asyncSource = getSource.async;
import { async } from 'get-source'
The asynchronous API is a property of the default export, not a separate named export. It returns awaitable Promises.
getSource.resetCache
import getSource from 'get-source'; getSource.resetCache(); getSource.async.resetCache();
import { resetCache } from 'get-source'
Cache reset functions are properties of the respective sync/async APIs. There are separate caches for sync and async operations.

Demonstrates fetching a source-mapped file asynchronously and resolving a specific line/column to its original source location, including basic error handling.

import getSource from 'get-source'; import fs from 'fs'; import path from 'path'; // Simulate a minified file and its sourcemap for demonstration const minifiedCodePath = path.resolve('./dist/index.min.js'); const minifiedMapPath = path.resolve('./dist/index.min.js.map'); const originalCodePath = path.resolve('./src/index.js'); // In a real scenario, these files would exist or be fetched from a URL fs.mkdirSync(path.dirname(minifiedCodePath), { recursive: true }); fs.writeFileSync(originalCodePath, 'console.log("Hello original source!");\nconst sum = (a, b) => a + b;'); fs.writeFileSync(minifiedCodePath, 'console.log("Hello minified!"); var a = 1, b = 2; console.log(a+b); //# sourceMappingURL=index.min.js.map'); fs.writeFileSync(minifiedMapPath, JSON.stringify({ version: 3, file: 'index.min.js', sources: ['../src/index.js'], sourcesContent: ['console.log("Hello original source!");\nconst sum = (a, b) => a + b;'], names: [], mappings: 'AAAA,aAAa;AAAb;AAAA,IAAM,KAAK,GAAG,IAAM,IAAI,EAAE,CAAC,GAAE,CAAC,MAAMA,EAAE,CAAC;AAAA,IAAM,MAAM,GAAG,CAAC,GAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAAA,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC;AAAA,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAE,MAAM,CAAC;' })); async function run() { try { // Fetch the minified file and its sourcemap asynchronously const file = await getSource.async(minifiedCodePath); console.log(`Fetched file: ${file.path}`); console.log(`Minified text: ${file.text.substring(0, 50)}...`); // Resolve a location in the minified file to its original source // Example: The 'console.log(a+b)' statement is around line 1, column 50 in minified. const location = await file.resolve({ line: 1, column: 50 }); if (location && location.sourceFile) { console.log('\nResolved original location:'); console.log(` Original File: ${location.sourceFile.path}`); console.log(` Original Line: ${location.line}`); console.log(` Original Column: ${location.column}`); console.log(` Original Code: ${location.sourceLine}`); } else { console.log('Could not resolve location.'); if (file.error) console.error('Error during fetch:', file.error); } } catch (e) { console.error('An error occurred:', e); } finally { // Clean up created files fs.unlinkSync(minifiedCodePath); fs.unlinkSync(minifiedMapPath); fs.unlinkSync(originalCodePath); fs.rmdirSync(path.dirname(minifiedCodePath)); fs.rmdirSync(path.dirname(originalCodePath)); } } run();
Debug
Known issues
gotchaThe synchronous API (`getSource`) does not throw errors for operations like file not found or sourcemap parsing issues. Instead, it returns a file object where the `text` field will be an empty string and an `error` property will contain the `Error` object if an issue occurred.
fix
Always check the `.error` property of the returned `file` object when using the synchronous API: `const file = getSource('./non-existent.js'); if (file.error) { console.error(file.error); }`
affects: >=1.0.0
gotchaThe asynchronous API (`getSource.async`) throws errors for failed operations (e.g., file not found, network issues). This contrasts with the synchronous API's non-throwing behavior.
fix
Wrap asynchronous calls in `try...catch` blocks or handle Promise rejections using `.catch()`: `try { await getSource.async('./non-existent.js'); } catch (e) { console.error(e); }`
affects: >=1.0.0
gotchaLine and column numbers passed to the `file.resolve()` method are 1-indexed (e.g., `line: 1`, `column: 8`). Providing 0-indexed values will result in invalid lookups or incorrect resolutions.
fix
Ensure that any `line` or `column` values originating from 0-indexed sources (like array indices) are incremented by 1 before being passed to `file.resolve()`.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to use CommonJS `require()` syntax to import `get-source` in a Node.js environment where it is treated as an ES Module.
fix
Use ES Module `import` syntax: `import getSource from 'get-source';` in your JavaScript files. Ensure your `package.json` has `"type": "module"` or your file is named `.mjs`.
TypeError: Cannot read properties of undefined (reading 'sourceLine')
Attempting to access properties of the `location` object returned by `file.resolve()` without checking if `location` itself is valid or if `location.sourceFile` exists, often because the original location could not be resolved (e.g., due to an invalid input line/column or malformed sourcemap).
fix
Always verify the returned `location` and its `sourceFile` property before accessing sub-properties: `const location = await file.resolve({ line: 1, column: 8 }); if (location && location.sourceFile) { console.log(location.sourceLine); } else { console.error('Could not resolve location.'); }`
Upgrade
Version history
2.0.12latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
16
OpenAI (training)
2
Resources
get-source — npm install get-source · libregistry