Skip to content

Commit 953e1d2

Browse files
committed
chore: adding some types
to slightly clarify the API between this library and its users, add a bit of types where easy. many of the types I've added rely on some "any"s because I'm trying to provide some initial value to make future work easier. a single place uses the non-null assertion operator: apparently there's an open bug in typescript where Promise.all() wrongly adds " | undefined" microsoft/TypeScript#33752 we'll try to keep an eye open here to see when it can be removed.
1 parent 72c36eb commit 953e1d2

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

lib/index.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Docker, DockerOptions } from "./docker";
55
import * as dockerFile from "./docker-file";
66
import { getRuntime } from "./inputs/runtime/docker";
77
import { buildResponse } from "./response-builder";
8-
import { StaticAnalysisOptions } from "./types";
8+
import { ManifestFile, PluginResponse, StaticAnalysisOptions } from "./types";
99

1010
export { inspect, dockerFile };
1111

@@ -35,7 +35,7 @@ async function analyzeDynamically(
3535
targetImage: string,
3636
dockerfileAnalysis: dockerFile.DockerFileAnalysis | undefined,
3737
analysisOptions: any,
38-
) {
38+
): Promise<PluginResponse> {
3939
const [runtime, dependencies, manifestFiles] = await Promise.all([
4040
getRuntime(analysisOptions),
4141
getDependencies(targetImage, dockerfileAnalysis, analysisOptions),
@@ -46,12 +46,15 @@ async function analyzeDynamically(
4646
runtime,
4747
dependencies,
4848
dockerfileAnalysis,
49-
manifestFiles,
49+
manifestFiles!, // bug in typescript wrongly adds `undefined`
5050
analysisOptions,
5151
);
5252
}
5353

54-
async function analyzeStatically(targetImage: string, options: any) {
54+
async function analyzeStatically(
55+
targetImage: string,
56+
options: any,
57+
): Promise<PluginResponse> {
5558
const staticAnalysisOptions = getStaticAnalysisOptions(options);
5659

5760
// Relevant only if using a Docker runtime. Optional, but we may consider what to put here
@@ -218,7 +221,10 @@ function getDependencies(
218221
});
219222
}
220223

221-
async function getManifestFiles(targetImage: string, options?: any) {
224+
async function getManifestFiles(
225+
targetImage: string,
226+
options?: any,
227+
): Promise<ManifestFile[]> {
222228
if (!options.manifestGlobs) {
223229
return [];
224230
}

lib/response-builder.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
// analyses' are done.
33

44
import { DockerFilePackages, instructionDigest } from "./instruction-parser";
5+
import * as types from "./types";
56

67
export { buildResponse };
78

89
function buildResponse(
9-
runtime,
10+
runtime: string | undefined,
1011
depsAnalysis,
1112
dockerfileAnalysis,
12-
manifestFiles,
13+
manifestFiles: types.ManifestFile[],
1314
options,
14-
) {
15+
): types.PluginResponse {
1516
const deps = depsAnalysis.package.dependencies;
1617
const dockerfilePkgs = collectDockerfilePkgs(dockerfileAnalysis, deps);
1718
const finalDeps = excludeBaseImageDeps(deps, dockerfilePkgs, options);
@@ -31,7 +32,10 @@ function buildResponse(
3132
};
3233
}
3334

34-
function pluginMetadataRes(runtime, depsAnalysis) {
35+
function pluginMetadataRes(
36+
runtime: string | undefined,
37+
depsAnalysis,
38+
): types.PluginMetadata {
3539
return {
3640
name: "snyk-docker-plugin",
3741
runtime,

lib/types.ts

+20
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,23 @@ export enum OsReleaseFilePath {
2121
RedHat = "/etc/redhat-release",
2222
Oracle = "/etc/oracle-release",
2323
}
24+
25+
export interface ManifestFile {
26+
name: string;
27+
path: string;
28+
contents: string;
29+
}
30+
31+
export interface PluginMetadata {
32+
name: string;
33+
runtime: string | undefined;
34+
packageManager: any;
35+
dockerImageId: any;
36+
imageLayers: any;
37+
}
38+
39+
export interface PluginResponse {
40+
plugin: PluginMetadata;
41+
package: any;
42+
manifestFiles: ManifestFile[];
43+
}

0 commit comments

Comments
 (0)