Skip to content

Commit d1cb2aa

Browse files
committed
Initial commit
0 parents  commit d1cb2aa

17 files changed

+1903
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.idea/
2+
/node_modules/

CONTRIBUTING.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Contributing
2+
3+
When contributing to this repository, please first discuss the change you wish to make via issue,
4+
email, or any other method with the owners of this repository before making a change.
5+
6+
Please note we have a code of conduct, please follow it in all your interactions with the project.
7+
8+
## Pull Request Process
9+
10+
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
11+
build.
12+
2. Update the README.md with details of changes to the interface.
13+
3. Increase the version numbers in any examples files and the README.md to the new version that this
14+
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
15+
4. You may merge the Pull Request in once you have the sign-off of two other developers (once there are two), or if you
16+
do not have permission to do that, you may request the second reviewer to merge it for you.
17+
18+
## Code of Conduct
19+
20+
### Our Pledge
21+
22+
In the interest of fostering an open and welcoming environment, we as
23+
contributors and maintainers pledge to making participation in our project and
24+
our community a harassment-free experience for everyone, regardless of age, body
25+
size, disability, ethnicity, gender identity and expression, level of experience,
26+
nationality, personal appearance, race, religion, or sexual identity and
27+
orientation.
28+
29+
### Our Standards
30+
31+
Examples of behavior that contributes to creating a positive environment
32+
include:
33+
34+
* Using welcoming and inclusive language
35+
* Being respectful of differing viewpoints and experiences
36+
* Gracefully accepting constructive criticism
37+
* Focusing on what is best for the community
38+
* Showing empathy towards other community members
39+
40+
Examples of unacceptable behavior by participants include:
41+
42+
* The use of sexualized language or imagery and unwelcome sexual attention or
43+
advances
44+
* Trolling, insulting/derogatory comments, and personal or political attacks
45+
* Public or private harassment
46+
* Publishing others' private information, such as a physical or electronic
47+
address, without explicit permission
48+
* Other conduct which could reasonably be considered inappropriate in a
49+
professional setting
50+
51+
### Our Responsibilities
52+
53+
Project maintainers are responsible for clarifying the standards of acceptable
54+
behavior and are expected to take appropriate and fair corrective action in
55+
response to any instances of unacceptable behavior.
56+
57+
Project maintainers have the right and responsibility to remove, edit, or
58+
reject comments, commits, code, wiki edits, issues, and other contributions
59+
that are not aligned to this Code of Conduct, or to ban temporarily or
60+
permanently any contributor for other behaviors that they deem inappropriate,
61+
threatening, offensive, or harmful.
62+
63+
### Scope
64+
65+
This Code of Conduct applies both within project spaces and in public spaces
66+
when an individual is representing the project or its community. Examples of
67+
representing a project or community include using an official project e-mail
68+
address, posting via an official social media account, or acting as an appointed
69+
representative at an online or offline event. Representation of a project may be
70+
further defined and clarified by project maintainers.
71+
72+
### Enforcement
73+
74+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
75+
reported by contacting a member of the project team with his or her respective
76+
email address. All complaints will be reviewed and investigated and will result
77+
in a response that is deemed necessary and appropriate to the circumstances.
78+
The project team is obligated to maintain confidentiality with regard to the
79+
reporter of an incident.
80+
Further details of specific enforcement policies may be posted separately.
81+
82+
Project maintainers who do not follow or enforce the Code of Conduct in good
83+
faith may face temporary or permanent repercussions as determined by other
84+
members of the project's leadership.
85+
86+
### Attribution
87+
88+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
89+
available at [http://contributor-covenant.org/version/1/4][version]
90+
91+
[homepage]: http://contributor-covenant.org
92+
[version]: http://contributor-covenant.org/version/1/4/

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2018, Kay Huber, unblu inc.
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# JS Source Extractor
2+
3+
An extractor of source code from Javascript libraries containing source maps with embedded or referenced source.
4+
5+
## Getting Started
6+
7+
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
8+
9+
### Install for Node
10+
11+
12+
```
13+
$ npm install --save-dev js-source-extractor
14+
```
15+
16+
To extract source code from a sourcemap embedded in a javascript and call a callback per source file, use:
17+
18+
```js
19+
import { extractSrc } from './js-source-extractor'
20+
21+
const baseDir = __dirname + "/../resources";
22+
extractSrc(new URL(util.format('file://%s/embedded-sourcemap-test.js', baseDir)), null, (path: string, sourceName: string, source: string | null) => {
23+
console.log("Path: " + path); // '/src'
24+
console.log("SourceName: " + sourceName); // '/embedded-sourcemap-test.ts'
25+
console.log("Source: " + source); // 'console.log(\'Hello World!\');'
26+
});
27+
```
28+
29+
To extract code from a sourcemap embedded in a javascript and write the source files to the disk, use:
30+
```js
31+
const baseDir = __dirname + "/../resources";
32+
const outDir = tmpdir() + path.sep + 'js-source-extractor-test'; // directory to store source files in
33+
await extractSrcToDir(new URL(util.format('file://%s/embedded-sourcemap-test.js', baseDir)), outDir);
34+
```
35+
36+
## Running the tests
37+
38+
JS Source Extractor features a number of mocha tests to verify the various API functionalities.
39+
40+
To start the tests, use:
41+
```
42+
$ npm run test
43+
44+
```
45+
46+
## Contributing
47+
48+
Please read [CONTRIBUTING.md](https://gist.github.com/PurpleBooth/b24679402957c63ec426) for details on our code of conduct, and the process for submitting pull requests to us.
49+
50+
## Authors
51+
52+
* **Kay Huber** - *Initial work* - [unblu inc.](https://github.com/unblu)
53+
54+
See also the list of [contributors](https://github.com/your/project/contributors) who participated in this project.
55+
56+
## License
57+
58+
This project is licensed under the ISC License - see the [LICENSE.md](LICENSE) file for details
59+
60+
## Acknowledgments
61+
62+
* Thanks Dave for your encouragement to create this tiny tool

dist/js-source-extractor.d.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// <reference types="node" />
2+
import { URL } from "url";
3+
/**
4+
* Type of resource. Required if not automatically determinable.
5+
*/
6+
export declare enum ResourceType {
7+
JAVASCRIPT = "Javascript",
8+
SOURCE_MAP = "Source Map"
9+
}
10+
/**
11+
* Extract source code from either a provided Javascript file containing a source map information, a Source Map file or a source map in string form.
12+
* Source code files must either be referenced and accessible via information in the source map or be contained in the source map (embedded).
13+
*
14+
* The extracted source code is stored in the file system. The directory structure that is provided with the source file path information in the source map will be created automatically.
15+
*
16+
* @param {module:url.URL | null} resourceUrl An url (http(s):// or file:// for local file access) to retrieve either a Javascript file or a Source Map file. If null, sourceMap must be specified.
17+
* @param {string | null} sourceMap Source Map JSON in string form to use directly. Must contain source code or refer with absolute urls to source files. If null, resourceUrl must be specified.
18+
* @param {string} outDir Directory where the source files should be stored into.
19+
* @param {ResourceType} resourceType Optional type describing whether the resourceUrl references a Javascript or a Source Map file. Automatic detection (according to the file name extensions) is attempted if not specified.
20+
* @param {RegExp} srcInclude Optional pattern to test a source code path against before sending it to the receiver. Defaults to .*
21+
* @param {RegExp} srcExclude Optional pattern to test a source code path against to exclude from sending it to the receiver. Exclude pattern is tested after the include pattern.
22+
* @returns {Promise<void>} Async function without a direct result (use receiver to receive information)
23+
*/
24+
export declare function extractSrcToDir(resourceUrl: URL | null, sourceMap: string | null, outDir?: string, resourceType?: ResourceType, srcInclude?: RegExp, srcExclude?: RegExp): Promise<void>;
25+
/**
26+
* Extract source code from either a provided Javascript file containing a source map information, a Source Map file or a source map in string form.
27+
* Source code files must either be referenced and accessible via information in the source map or be contained in the source map (embedded).
28+
*
29+
* Example:
30+
* ```js
31+
* const baseDir = __dirname + "/../resources";
32+
* extractSrc(new URL(util.format('file://%s/embedded-sourcemap-test.js', baseDir)), null, (path: string, sourceName: string, source: string | null) => {
33+
* console.log("Path: " + path); // '/src'
34+
* console.log("SourceName: " + sourceName); // '/embedded-sourcemap-test.ts'
35+
* console.log("Source: " + source); // 'console.log(\'Hello World!\');'
36+
* });
37+
* ```
38+
*
39+
* Note: Either resourceUrl or sourceMap must be specified.
40+
*
41+
* @param {module:url.URL | null} resourceUrl An url (http(s):// or file:// for local file access) to retrieve either a Javascript file or a Source Map file. If null, sourceMap must be specified.
42+
* @param {string | null} sourceMap Source Map JSON in string form to use directly. Must contain source code or refer with absolute urls to source files. If null, resourceUrl must be specified.
43+
* @param {(path: string, sourceName: string, source: (string | null)) => void} receiver A callback function called for each source file referenced in the source map.
44+
* Path is the base path of the source file, sourceName the file name, source the source code, if the file was found
45+
* @param {ResourceType} resourceType Optional type describing whether the resourceUrl references a Javascript or a Source Map file. Automatic detection (according to the file name extensions) is attempted if not specified.
46+
* @param {RegExp} srcInclude Optional pattern to test a source code path against before sending it to the receiver. Defaults to .*
47+
* @param {RegExp} srcExclude Optional pattern to test a source code path against to exclude from sending it to the receiver. Exclude pattern is tested after the include pattern.
48+
* @returns {Promise<void>} Async function without a direct result (use receiver to receive information)
49+
*/
50+
export declare function extractSrc(resourceUrl: URL | null, sourceMap: string | null, receiver: (path: string, sourceName: string, source: string | null) => void, resourceType?: ResourceType, srcInclude?: RegExp, srcExclude?: RegExp): Promise<void>;

0 commit comments

Comments
 (0)