{"id":20473544,"url":"https://github.com/xpl/get-source","last_synced_at":"2025-05-08T21:20:58.552Z","repository":{"id":57250439,"uuid":"66383889","full_name":"xpl/get-source","owner":"xpl","description":"Fetch source-mapped sources. Peek by file, line, column. Node \u0026 browsers. Sync \u0026 async.","archived":false,"fork":false,"pushed_at":"2021-04-14T11:57:15.000Z","size":739,"stargazers_count":28,"open_issues_count":3,"forks_count":10,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-11T17:45:48.975Z","etag":null,"topics":["javascript","read-source-code","reflection","source","source-code","source-code-analysis","sourcecode","sourcemap","sourcemap-support","sourcemaps","typescript"],"latest_commit_sha":null,"homepage":"http://npmjs.com/package/get-source","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xpl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-23T16:25:42.000Z","updated_at":"2024-09-06T17:13:32.000Z","dependencies_parsed_at":"2022-08-24T16:51:45.493Z","dependency_job_id":null,"html_url":"https://github.com/xpl/get-source","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fget-source","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fget-source/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fget-source/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xpl%2Fget-source/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xpl","download_url":"https://codeload.github.com/xpl/get-source/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253150075,"owners_count":21861829,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","read-source-code","reflection","source","source-code","source-code-analysis","sourcecode","sourcemap","sourcemap-support","sourcemaps","typescript"],"created_at":"2024-11-15T14:25:48.713Z","updated_at":"2025-05-08T21:20:58.526Z","avatar_url":"https://github.com/xpl.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# get-source\n\n[![Build Status](https://travis-ci.org/xpl/get-source.svg?branch=master)](https://travis-ci.org/xpl/get-source) [![Coverage Status](https://coveralls.io/repos/github/xpl/get-source/badge.svg)](https://coveralls.io/github/xpl/get-source) [![npm](https://img.shields.io/npm/v/get-source.svg)](https://npmjs.com/package/get-source)\n\nFetch source-mapped sources. Peek by file, line, column. Node \u0026 browsers. Sync \u0026 async.\n\n```bash\nnpm install get-source\n```\n\n## Features\n\n- [x] Allows to read source code files in Node and browsers\n- [x] Full sourcemap support (path resolving, external/embedded/inline linking, and long chains)\n- [x] **Synchronous** API — good for CLI tools (e.g. [logging](https://github.com/xpl/ololog)). Works in browsers!\n- [x] **Asynchronous** API — good for everything web!\n- [x] Built-in cache\n\n## What For\n\n- [x] Call stacks enhanced with source code information (see the [StackTracey](https://github.com/xpl/stacktracey) library)\n- [x] [Advanced logging](https://github.com/xpl/ololog) / assertion printing\n- [x] [Error displaying components](https://github.com/xpl/panic-overlay) for the front-end web development\n\n## Usage (Synchronous)\n\n```javascript\nimport getSource from 'get-source'\n```\n```javascript\nfile = getSource ('./scripts/index.min.js')\n```\n\nWill 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:\n\n```javascript\nfile.path  // normalized file path\nfile.text  // text contents\nfile.lines // array of lines\n```\n\nAnd the `resolve` method:\n\n```javascript\nfile.resolve ({ line: 1, column: 8 }) // indexes here start from 1 (by widely accepted convention). Zero indexes are invalid.\n```\n\nIt will look through the sourcemap chain, returning following:\n\n```javascript\n{\n   line:       \u003coriginal line number\u003e,\n   column:     \u003coriginal column number\u003e,\n   sourceFile: \u003coriginal source file object\u003e,\n   sourceLine: \u003coriginal source line text\u003e\n}\n```\n\nIn 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.\n\n## Usage (Asynchronous)\n\nPretty much the same as synchronous, except it's `getSource.async`. It returns awaitable promises:\n\n```javascript\nfile     = await getSource.async ('./scripts/index.min.js')\nlocation = await file.resolve ({ line: 1, column: 8 })\n```\n\n## Error Handling\n\nIn synchronous mode, it never throws (due to backward compatibility reasons with existing code):\n\n```javascript\nnonsense = getSource ('/some/nonexistent/file')\n\nnonsense.text  // should be '' (so it's safe to access without checking)\nnonsense.error // should be an Error object, representing an actual error thrown during reading/parsing\n```\n```javascript\nresolved = nonsense.resolve ({ line: 5, column: 0 })\n\nresolved.sourceLine // empty string (so it's safe to access without checking)\nresolved.error      // should be an Error object, representing an actual error thrown during reading/parsing\n```\n\nIn asychronous mode, it throws an error:\n\n```javascript\ntry { \n   file     = await getSource.async ('/some/file')\n   location = await file.resolve ({ line: 5, column: 0 })\n} catch (e) {\n   ...\n}\n```\n\n## Resetting Cache\n\nE.g. when you need to force-reload files:\n\n```javascript\ngetSource.resetCache ()        // sync cache\ngetSource.async.resetCache ()  // async cache\n```\n\nAlso, viewing cached files:\n\n```javascript\ngetSource.getCache ()        // sync cache\ngetSource.async.getCache ()  // async cache\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpl%2Fget-source","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxpl%2Fget-source","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxpl%2Fget-source/lists"}