Skip to content

Commit

Permalink
refactor: implement API exports & minimal docs (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken authored May 15, 2024
1 parent c24938b commit ed6ae3e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 7 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ require(Buffer.from("6673", "hex").toString());

Then use `js-x-ray` to run an analysis of the JavaScript code:
```js
import { runASTAnalysis } from "@nodesecure/js-x-ray";
import { AstAnalyser } from "@nodesecure/js-x-ray";
import { readFileSync } from "node:fs";

const { warnings, dependencies } = runASTAnalysis(
readFileSync("./file.js", "utf-8")
const scanner = new AstAnalyser();

const { warnings, dependencies } = await scanner.analyseFile(
"./file.js"
);

console.log(dependencies);
Expand Down Expand Up @@ -174,11 +176,16 @@ You can pass an array of probes to the `runASTAnalysis/runASTAnalysisOnFile` fun
Here using the example probe upper:

```ts
import { runASTAnalysis } from "@nodesecure/js-x-ray";
import { AstAnalyser } from "@nodesecure/js-x-ray";

// add your customProbes here (see example above)

const result = runASTAnalysis("const danger = 'danger';", { customProbes, skipDefaultProbes: true });
const scanner = new AstAnalyser({
customProbes,
skipDefaultProbes: true
});

const result = scanner.analyse("const danger = 'danger';");

console.log(result);
```
Expand All @@ -202,6 +209,12 @@ Congrats, you have created your first custom probe! 🎉
> Check the types in [index.d.ts](index.d.ts) and [types/api.d.ts](types/api.d.ts) for more details about the `options`
## API
- [AstAnalyser](./docs/api/AstAnalyser.md)
- [EntryFilesAnalyser](./docs/api/EntryFilesAnalyser.md)
Legacy APIs waiting to be deprecated;
<details>
<summary>runASTAnalysis(str: string, options?: RuntimeOptions & AstAnalyserOptions): Report</summary>
Expand Down
66 changes: 66 additions & 0 deletions docs/api/AstAnalyser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# AstAnalyser

```js
import { AstAnalyser } from "@nodesecure/js-x-ray";
import { TsSourceParser } from "@nodesecure/ts-source-parser";

const scanner = new AstAnalyser({
customParser: new TsSourceParser()
});

const result = scanner.analyse("const x: number = 5;");
console.log(result);
```

AstAnalyser options is described by the following TS interface:

```ts
interface AstAnalyserOptions {
/**
* @default JsSourceParser
*/
customParser?: SourceParser;
/**
* @default []
*/
customProbes?: Probe[];
/**
* @default false
*/
skipDefaultProbes?: boolean;
}
```

By default the AstAnalyser class is capable of parsing JavaScript source code using Meriyah.

## API

```ts
declare class AstAnalyser {
constructor(options?: AstAnalyserOptions);
analyse: (str: string, options?: RuntimeOptions) => Report;
analyseFile(pathToFile: string, options?: RuntimeFileOptions): Promise<ReportOnFile>;
}
```

The `analyseFile` method is a superset of `analyse` with the ability to read the file on the local filesystem with additional features like detecting if the file is ESM or CJS.

```ts
interface Report {
dependencies: Map<string, Dependency>;
warnings: Warning[];
idsLengthAvg: number;
stringScore: number;
isOneLineRequire: boolean;
}

type ReportOnFile = {
ok: true,
warnings: Warning[];
dependencies: Map<string, Dependency>;
isMinified: boolean;
} | {
ok: false,
warnings: Warning[];
}
```
38 changes: 38 additions & 0 deletions docs/api/EntryFilesAnalyser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# EntryFilesAnalyser

```js
import { EntryFilesAnalyser } from "@nodesecure/js-x-ray";

const efa = new EntryFilesAnalyser();

// Either a string path or a WHAWG URL
const entryFiles = [
"./path/to/file.js"
];

for await (const report of efa.analyse(entryFiles)) {
console.log(report);
}
```

The constructor options is described by the following TS interface

```ts
interface EntryFilesAnalyserOptions {
astAnalyzer?: AstAnalyser;
loadExtensions?: (defaults: string[]) => string[];
}
```

Default files extensions are `.js`, `.cjs`, `.mjs` and `.node`

## API

```ts
declare class EntryFilesAnalyser {
constructor(options?: EntryFilesAnalyserOptions);
analyse(entryFiles: (string | URL)[]): AsyncGenerator<ReportOnFile & { url: string }>;
}
```

For more informations about `Report` and `ReportOnFile` interfaces please see [AstAnalyser documentation](./AstAnalyser.md)
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
EntryFilesAnalyserOptions,

SourceParser,
JsSourceParser,
runASTAnalysis,
runASTAnalysisOnFile,
Report,
Expand All @@ -31,6 +32,7 @@ export {
AstAnalyserOptions,
EntryFilesAnalyser,
EntryFilesAnalyserOptions,
JsSourceParser,
SourceParser,
runASTAnalysis,
runASTAnalysisOnFile,
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { warnings } from "./src/warnings.js";
import { JsSourceParser } from "./src/JsSourceParser.js";
import { AstAnalyser } from "./src/AstAnalyser.js";
import { EntryFilesAnalyser } from "./src/EntryFilesAnalyser.js";

function runASTAnalysis(
str,
Expand All @@ -28,8 +29,8 @@ async function runASTAnalysisOnFile(
options = {}
) {
const {
customParser = new JsSourceParser(),
customProbes = [],
customParser = new JsSourceParser(),
skipDefaultProbes = false,
...opts
} = options;
Expand All @@ -46,6 +47,8 @@ async function runASTAnalysisOnFile(
export {
warnings,
AstAnalyser,
EntryFilesAnalyser,
JsSourceParser,
runASTAnalysis,
runASTAnalysisOnFile
};
7 changes: 6 additions & 1 deletion types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { Statement } from "meriyah/dist/src/estree.js";
export {
AstAnalyser,
AstAnalyserOptions,

EntryFilesAnalyser,
EntryFilesAnalyserOptions,

JsSourceParser,
SourceParser,
runASTAnalysis,
runASTAnalysisOnFile,
Expand Down Expand Up @@ -101,7 +104,7 @@ interface SourceParser {
declare class AstAnalyser {
constructor(options?: AstAnalyserOptions);
analyse: (str: string, options?: RuntimeOptions) => Report;
analyzeFile(pathToFile: string, options?: RuntimeFileOptions): Promise<ReportOnFile>;
analyseFile(pathToFile: string, options?: RuntimeFileOptions): Promise<ReportOnFile>;
}

interface EntryFilesAnalyserOptions {
Expand All @@ -118,5 +121,7 @@ declare class EntryFilesAnalyser {
analyse(entryFiles: (string | URL)[]): AsyncGenerator<ReportOnFile & { url: string }>;
}

declare class JsSourceParser implements SourceParser {}

declare function runASTAnalysis(str: string, options?: RuntimeOptions & AstAnalyserOptions): Report;
declare function runASTAnalysisOnFile(pathToFile: string, options?: RuntimeFileOptions & AstAnalyserOptions): Promise<ReportOnFile>;

0 comments on commit ed6ae3e

Please sign in to comment.