Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curvy-dragons-spend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/scanner": minor
---

Implement AstAnalyserOptions for main scanner API (from, workingDir and verify)
28 changes: 27 additions & 1 deletion workspaces/scanner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function from(
logger?: Scanner.Logger
): Promise<Scanner.Payload>;
function verify(
spec?: string
spec?: string,
options?: VerifyOptions
): Promise<tarball.ScannedPackageResult>;
```

Expand Down Expand Up @@ -89,6 +90,10 @@ type FromOptions = Omit<Options, "includeDevDeps"> & {
) => Promise<Payload | null>;
};

interface VerifyOptions {
astAnalyserOptions?: AstAnalyserOptions;
}

interface Options {
/**
* Specifies the maximum depth to traverse for each root dependency.
Expand Down Expand Up @@ -157,6 +162,27 @@ interface Options {
* @default true for cwd() API
*/
readonly scanRootNode?: boolean;

/**
* Enable verbose mode
*
* @default false
*/
isVerbose?: boolean;

/**
* Enable worker threads for parallel tarball scanning.
* - `true` uses the default worker count (4)
* - `number` sets an explicit worker count
*
* @default false
*/
readonly workers?: boolean | number;

/**
* Custom options for JS-X-Ray
*/
readonly astAnalyserOptions?: AstAnalyserOptions;
}
```

Expand Down
9 changes: 9 additions & 0 deletions workspaces/scanner/docs/from.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ export interface Options {
* @default false
*/
readonly workers?: boolean | number;

/**
* Custom options for the underhood JS-X-Ray instance
* when scanning tarballs or local directories
*
* @note
* Do not support `collectable` option
*/
readonly astAnalyserOptions?: AstAnalyserOptionsNoCollectable;
}
```

Expand Down
7 changes: 6 additions & 1 deletion workspaces/scanner/docs/verify.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ console.log(result);
## Signature

```ts
export interface VerifyOptions {
astAnalyserOptions?: AstAnalyserOptions;
}

function verify(
spec?: string
spec?: string,
options?: VerifyOptions
): Promise<tarball.ScannedPackageResult>
```

Expand Down
9 changes: 9 additions & 0 deletions workspaces/scanner/docs/workingDir.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ export interface Options {
* @default false
*/
readonly workers?: boolean | number;

/**
* Custom options for the underhood JS-X-Ray instance
* when scanning tarballs or local directories
*
* @note
* Do not support `collectable` option
*/
readonly astAnalyserOptions?: AstAnalyserOptionsNoCollectable;
}
```

Expand Down
15 changes: 12 additions & 3 deletions workspaces/scanner/src/class/TarballScanner.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { ManifestManager } from "@nodesecure/mama";
import { StatsCollector } from "./StatsCollector.class.ts";
import { TempDirectory } from "./TempDirectory.class.ts";
import { Logger, ScannerLoggerEvents } from "./logger.class.ts";
import type {
AstAnalyserOptionsNoCollectable
} from "../types.ts";

type CollectableMetadata = { spec?: string; };

Expand All @@ -37,6 +40,7 @@ export interface TarballScannerOptions {
maxConcurrency: number;
logger: Logger;
workers?: boolean | number;
astAnalyserOptions?: AstAnalyserOptionsNoCollectable;
}

export class TarballScanner {
Expand All @@ -48,6 +52,7 @@ export class TarballScanner {
#collectableTypes: string[];
#workerPool: NpmTarballWorkerPool | null;
#logger: Logger;
#astAnalyserOptions: AstAnalyserOptionsNoCollectable;

constructor(
options: TarballScannerOptions
Expand All @@ -59,7 +64,8 @@ export class TarballScanner {
collectables,
maxConcurrency,
logger,
workers
workers,
astAnalyserOptions
} = options;

this.#tempDir = tempDir;
Expand All @@ -68,6 +74,7 @@ export class TarballScanner {
this.#collectables = collectables;
this.#collectableTypes = collectables.map((collectable) => collectable.type);
this.#logger = logger;
this.#astAnalyserOptions = astAnalyserOptions ?? {};

this.#locker = new Mutex({ concurrency: maxConcurrency });

Expand Down Expand Up @@ -113,7 +120,8 @@ export class TarballScanner {
fn: () => this.#workerPool!.scan({
location: mama.location!,
astAnalyserOptions: {
optionalWarnings: hasLocation
optionalWarnings: hasLocation,
...this.#astAnalyserOptions
},
collectableTypes: this.#collectableTypes
}),
Expand Down Expand Up @@ -161,7 +169,8 @@ export class TarballScanner {
fn: () => scanDirOrArchive(mama, ref, {
astAnalyserOptions: {
optionalWarnings: hasLocation,
collectables: this.#collectables
collectables: this.#collectables,
...this.#astAnalyserOptions
}
}),
onSuccess: (_, stat) => {
Expand Down
6 changes: 4 additions & 2 deletions workspaces/scanner/src/depWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export async function depWalker(
npmRcEntries = {},
maxConcurrency = 8,
workers,
integrity: manifestIntegrity = null
integrity: manifestIntegrity = null,
astAnalyserOptions
} = options;

const statsCollector = new StatsCollector({ logger }, { isVerbose });
Expand Down Expand Up @@ -236,7 +237,8 @@ export async function depWalker(
collectables,
maxConcurrency,
logger,
workers
workers,
astAnalyserOptions
});

const rootDepsOptions: npm.WalkOptions = {
Expand Down
20 changes: 17 additions & 3 deletions workspaces/scanner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import os from "node:os";

// Import Third-party Dependencies
import pacote from "pacote";
import type {
AstAnalyserOptions
} from "@nodesecure/js-x-ray";
import { getLocalRegistryURL } from "@nodesecure/npm-registry-sdk";
import * as tarball from "@nodesecure/tarball";
import { ManifestManager } from "@nodesecure/mama";
Expand Down Expand Up @@ -135,11 +138,20 @@ export async function from(
);
}

export interface VerifyOptions {
astAnalyserOptions?: AstAnalyserOptions;
}

export async function verify(
spec?: string
spec?: string,
options: VerifyOptions = {}
): Promise<tarball.ScannedPackageResult> {
const { astAnalyserOptions } = options;

if (typeof spec === "undefined") {
return tarball.scanPackage(process.cwd());
return tarball.scanPackage(process.cwd(), {
astAnalyserOptions
});
}

await using tempDir = await TempDirectory.create();
Expand All @@ -149,7 +161,9 @@ export async function verify(
registry: getLocalRegistryURL()
});

const scanResult = await tarball.scanPackage(mama);
const scanResult = await tarball.scanPackage(mama, {
astAnalyserOptions
});

return scanResult;
}
Expand Down
16 changes: 15 additions & 1 deletion workspaces/scanner/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Import Third-party Dependencies
import type { Warning } from "@nodesecure/js-x-ray";
import type {
Warning,
AstAnalyserOptions
} from "@nodesecure/js-x-ray";
import type { StandardVulnerability, Kind } from "@nodesecure/vulnera";
import type { PackageModuleType } from "@nodesecure/mama";

Expand Down Expand Up @@ -282,6 +285,8 @@ export type SemverRange = string | "*";

export type HighlightPackages = string[] | Record<string, string[] | SemverRange>;

export type AstAnalyserOptionsNoCollectable = Omit<AstAnalyserOptions, "collectable">;

export interface Options {
/**
* Maximum tree depth
Expand Down Expand Up @@ -364,6 +369,15 @@ export interface Options {
* @default false
*/
readonly workers?: boolean | number;

/**
* Custom options for the underhood JS-X-Ray instance
* when scanning tarballs or local directories
*
* @note
* Do not support `collectable` option
*/
readonly astAnalyserOptions?: AstAnalyserOptionsNoCollectable;
}

export interface TokenStore {
Expand Down