-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
runtime-info.ts
104 lines (91 loc) · 3.33 KB
/
runtime-info.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import { major as nodeMajorVersion } from './node-version';
// list of NPM scopes included in version reporting e.g. @aws-cdk and @aws-solutions-konstruk
const WHITELIST_SCOPES = ['@aws-cdk', '@aws-solutions-konstruk'];
/**
* Returns a list of loaded modules and their versions.
*/
export function collectRuntimeInformation(): cxschema.RuntimeInfo {
const libraries: { [name: string]: string } = {};
for (const fileName of Object.keys(require.cache)) {
const pkg = findNpmPackage(fileName);
if (pkg && !pkg.private) {
libraries[pkg.name] = pkg.version;
}
}
// include only libraries that are in the whitelistLibraries list
for (const name of Object.keys(libraries)) {
let foundMatch = false;
for (const scope of WHITELIST_SCOPES) {
if (name.startsWith(`${scope}/`)) {
foundMatch = true;
}
}
if (!foundMatch) {
delete libraries[name];
}
}
// add jsii runtime version
libraries['jsii-runtime'] = getJsiiAgentVersion();
return { libraries };
}
/**
* Determines which NPM module a given loaded javascript file is from.
*
* The only infromation that is available locally is a list of Javascript files,
* and every source file is associated with a search path to resolve the further
* ``require`` calls made from there, which includes its own directory on disk,
* and parent directories - for example:
*
* [ '...repo/packages/aws-cdk-resources/lib/cfn/node_modules',
* '...repo/packages/aws-cdk-resources/lib/node_modules',
* '...repo/packages/aws-cdk-resources/node_modules',
* '...repo/packages/node_modules',
* // etc...
* ]
*
* We are looking for ``package.json`` that is anywhere in the tree, except it's
* in the parent directory, not in the ``node_modules`` directory. For this
* reason, we strip the ``/node_modules`` suffix off each path and use regular
* module resolution to obtain a reference to ``package.json``.
*
* @param fileName a javascript file name.
* @returns the NPM module infos (aka ``package.json`` contents), or
* ``undefined`` if the lookup was unsuccessful.
*/
function findNpmPackage(fileName: string): { name: string, version: string, private?: boolean } | undefined {
const mod = require.cache[fileName];
const paths = mod.paths.map(stripNodeModules);
try {
const packagePath = require.resolve(
// Resolution behavior changed in node 12.0.0 - https://github.com/nodejs/node/issues/27583
nodeMajorVersion >= 12 ? './package.json' : 'package.json',
{ paths },
);
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(packagePath);
} catch (e) {
return undefined;
}
/**
* @param s a path.
* @returns ``s`` with any terminating ``/node_modules``
* (or ``\\node_modules``) stripped off.)
*/
function stripNodeModules(s: string): string {
if (s.endsWith('/node_modules') || s.endsWith('\\node_modules')) {
// /node_modules is 13 characters
return s.substr(0, s.length - 13);
}
return s;
}
}
function getJsiiAgentVersion() {
let jsiiAgent = process.env.JSII_AGENT;
// if JSII_AGENT is not specified, we will assume this is a node.js runtime
// and plug in our node.js version
if (!jsiiAgent) {
jsiiAgent = `node.js/${process.version}`;
}
return jsiiAgent;
}