npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

get-source

v2.0.12

Published

Fetch source-mapped sources. Peek by file, line, column. Node & browsers. Sync & async.

Downloads

3,853,543

Readme

get-source

Build Status Coverage Status npm

Fetch source-mapped sources. Peek by file, line, column. Node & browsers. Sync & async.

npm install get-source

Features

  • [x] Allows to read source code files in Node and browsers
  • [x] Full sourcemap support (path resolving, external/embedded/inline linking, and long chains)
  • [x] Synchronous API — good for CLI tools (e.g. logging). Works in browsers!
  • [x] Asynchronous API — good for everything web!
  • [x] Built-in cache

What for

Usage (Synchronous)

import getSource from 'get-source'
file = getSource ('./scripts/index.min.js')

Will read the file synchronously (either via XHR or by filesystem API, depending on the environment) and return it's cached representation. Result will contain the following fields:

file.path  // normalized file path
file.text  // text contents
file.lines // array of lines

And the resolve method:

file.resolve ({ line: 1, column: 8 }) // indexes here start from 1 (by widely accepted convention). Zero indexes are invalid.

It will look through the sourcemap chain, returning following:

{
   line:       <original line number>,
   column:     <original column number>,
   sourceFile: <original source file object>,
   sourceLine: <original source line text>
}

In that returned object, sourceFile is the same kind of object that getSource returns. So you can access its text, lines and path fields to obtain the full information. And the sourceLine is returned just for the convenience, as a shortcut.

Usage (Asynchronous)

Pretty much the same as synchronous, except it's getSource.async. It returns awaitable promises:

file     = await getSource.async ('./scripts/index.min.js')
location = await file.resolve ({ line: 1, column: 8 })

Error handling

In synchronous mode, it never throws (due to backward compatibility reasons with existing code):

nonsense = getSource ('/some/nonexistent/file')

nonsense.text  // should be '' (so it's safe to access without checking)
nonsense.error // should be an Error object, representing an actual error thrown during reading/parsing
resolved = nonsense.resolve ({ line: 5, column: 0 })

resolved.sourceLine // empty string (so it's safe to access without checking)
resolved.error      // should be an Error object, representing an actual error thrown during reading/parsing

In asychronous mode, it throws an error:

try { 
   file     = await getSource.async ('/some/file')
   location = await file.resolve ({ line: 5, column: 0 })
} catch (e) {
   ...
}

Resetting Cache

E.g. when you need to force-reload files:

getSource.resetCache ()        // sync cache
getSource.async.resetCache ()  // async cache

Also, viewing cached files:

getSource.getCache ()        // sync cache
getSource.async.getCache ()  // async cache