@cyfrin/aderyn 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,192 @@
1
+ const { createWriteStream, existsSync, mkdirSync, mkdtemp } = require("fs");
2
+ const { join, sep } = require("path");
3
+ const { spawnSync } = require("child_process");
4
+ const { tmpdir } = require("os");
5
+
6
+ const axios = require("axios");
7
+ const rimraf = require("rimraf");
8
+ const tmpDir = tmpdir();
9
+
10
+ const error = (msg) => {
11
+ console.error(msg);
12
+ process.exit(1);
13
+ };
14
+
15
+ class Package {
16
+ constructor(name, url, filename, zipExt, binaries) {
17
+ let errors = [];
18
+ if (typeof url !== "string") {
19
+ errors.push("url must be a string");
20
+ } else {
21
+ try {
22
+ new URL(url);
23
+ } catch (e) {
24
+ errors.push(e);
25
+ }
26
+ }
27
+ if (name && typeof name !== "string") {
28
+ errors.push("package name must be a string");
29
+ }
30
+ if (!name) {
31
+ errors.push("You must specify the name of your package");
32
+ }
33
+ if (binaries && typeof binaries !== "object") {
34
+ errors.push("binaries must be a string => string map");
35
+ }
36
+ if (!binaries) {
37
+ errors.push("You must specify the binaries in the package");
38
+ }
39
+
40
+ if (errors.length > 0) {
41
+ let errorMsg =
42
+ "One or more of the parameters you passed to the Binary constructor are invalid:\n";
43
+ errors.forEach((error) => {
44
+ errorMsg += error;
45
+ });
46
+ errorMsg +=
47
+ '\n\nCorrect usage: new Package("my-binary", "https://example.com/binary/download.tar.gz", {"my-binary": "my-binary"})';
48
+ error(errorMsg);
49
+ }
50
+ this.url = url;
51
+ this.name = name;
52
+ this.filename = filename;
53
+ this.zipExt = zipExt;
54
+ this.installDirectory = join(__dirname, "node_modules", ".bin_real");
55
+ this.binaries = binaries;
56
+
57
+ if (!existsSync(this.installDirectory)) {
58
+ mkdirSync(this.installDirectory, { recursive: true });
59
+ }
60
+ }
61
+
62
+ exists() {
63
+ for (const binaryName in this.binaries) {
64
+ const binRelPath = this.binaries[binaryName];
65
+ const binPath = join(this.installDirectory, binRelPath);
66
+ if (!existsSync(binPath)) {
67
+ return false;
68
+ }
69
+ }
70
+ return true;
71
+ }
72
+
73
+ install(fetchOptions, suppressLogs = false) {
74
+ if (this.exists()) {
75
+ if (!suppressLogs) {
76
+ console.error(
77
+ `${this.name} is already installed, skipping installation.`,
78
+ );
79
+ }
80
+ return Promise.resolve();
81
+ }
82
+
83
+ if (existsSync(this.installDirectory)) {
84
+ rimraf.sync(this.installDirectory);
85
+ }
86
+
87
+ mkdirSync(this.installDirectory, { recursive: true });
88
+
89
+ if (!suppressLogs) {
90
+ console.error(`Downloading release from ${this.url}`);
91
+ }
92
+
93
+ return axios({ ...fetchOptions, url: this.url, responseType: "stream" })
94
+ .then((res) => {
95
+ return new Promise((resolve, reject) => {
96
+ mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
97
+ let tempFile = join(directory, this.filename);
98
+ const sink = res.data.pipe(createWriteStream(tempFile));
99
+ sink.on("error", (err) => reject(err));
100
+ sink.on("close", () => {
101
+ if (/\.tar\.*/.test(this.zipExt)) {
102
+ const result = spawnSync("tar", [
103
+ "xf",
104
+ tempFile,
105
+ // The tarballs are stored with a leading directory
106
+ // component; we strip one component in the
107
+ // shell installers too.
108
+ "--strip-components",
109
+ "1",
110
+ "-C",
111
+ this.installDirectory,
112
+ ]);
113
+ if (result.status == 0) {
114
+ resolve();
115
+ } else if (result.error) {
116
+ reject(result.error);
117
+ } else {
118
+ reject(
119
+ new Error(
120
+ `An error occurred untarring the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
121
+ ),
122
+ );
123
+ }
124
+ } else if (this.zipExt == ".zip") {
125
+ const result = spawnSync("unzip", [
126
+ "-q",
127
+ tempFile,
128
+ "-d",
129
+ this.installDirectory,
130
+ ]);
131
+ if (result.status == 0) {
132
+ resolve();
133
+ } else if (result.error) {
134
+ reject(result.error);
135
+ } else {
136
+ reject(
137
+ new Error(
138
+ `An error occurred unzipping the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
139
+ ),
140
+ );
141
+ }
142
+ } else {
143
+ reject(
144
+ new Error(`Unrecognized file extension: ${this.zipExt}`),
145
+ );
146
+ }
147
+ });
148
+ });
149
+ });
150
+ })
151
+ .then(() => {
152
+ if (!suppressLogs) {
153
+ console.error(`${this.name} has been installed!`);
154
+ }
155
+ })
156
+ .catch((e) => {
157
+ error(`Error fetching release: ${e.message}`);
158
+ });
159
+ }
160
+
161
+ run(binaryName, fetchOptions) {
162
+ const promise = !this.exists()
163
+ ? this.install(fetchOptions, true)
164
+ : Promise.resolve();
165
+
166
+ promise
167
+ .then(() => {
168
+ const [, , ...args] = process.argv;
169
+
170
+ const options = { cwd: process.cwd(), stdio: "inherit" };
171
+
172
+ const binRelPath = this.binaries[binaryName];
173
+ if (!binRelPath) {
174
+ error(`${binaryName} is not a known binary in ${this.name}`);
175
+ }
176
+ const binPath = join(this.installDirectory, binRelPath);
177
+ const result = spawnSync(binPath, args, options);
178
+
179
+ if (result.error) {
180
+ error(result.error);
181
+ }
182
+
183
+ process.exit(result.status);
184
+ })
185
+ .catch((e) => {
186
+ error(e.message);
187
+ process.exit(1);
188
+ });
189
+ }
190
+ }
191
+
192
+ module.exports.Package = Package;
package/binary.js ADDED
@@ -0,0 +1,126 @@
1
+ const { Package } = require("./binary-install");
2
+ const os = require("os");
3
+ const cTable = require("console.table");
4
+ const libc = require("detect-libc");
5
+ const { configureProxy } = require("axios-proxy-builder");
6
+
7
+ const error = (msg) => {
8
+ console.error(msg);
9
+ process.exit(1);
10
+ };
11
+
12
+ const {
13
+ name,
14
+ artifactDownloadUrl,
15
+ supportedPlatforms,
16
+ glibcMinimum,
17
+ } = require("./package.json");
18
+
19
+ const builderGlibcMajorVersion = glibcMinimum.major;
20
+ const builderGlibcMInorVersion = glibcMinimum.series;
21
+
22
+ const getPlatform = () => {
23
+ const rawOsType = os.type();
24
+ const rawArchitecture = os.arch();
25
+
26
+ // We want to use rust-style target triples as the canonical key
27
+ // for a platform, so translate the "os" library's concepts into rust ones
28
+ let osType = "";
29
+ switch (rawOsType) {
30
+ case "Windows_NT":
31
+ osType = "pc-windows-msvc";
32
+ break;
33
+ case "Darwin":
34
+ osType = "apple-darwin";
35
+ break;
36
+ case "Linux":
37
+ osType = "unknown-linux-gnu";
38
+ break;
39
+ }
40
+
41
+ let arch = "";
42
+ switch (rawArchitecture) {
43
+ case "x64":
44
+ arch = "x86_64";
45
+ break;
46
+ case "arm64":
47
+ arch = "aarch64";
48
+ break;
49
+ }
50
+
51
+ if (rawOsType === "Linux") {
52
+ if (libc.familySync() == "musl") {
53
+ osType = "unknown-linux-musl-dynamic";
54
+ } else if (libc.isNonGlibcLinuxSync()) {
55
+ console.warn(
56
+ "Your libc is neither glibc nor musl; trying static musl binary instead",
57
+ );
58
+ osType = "unknown-linux-musl-static";
59
+ } else {
60
+ let libcVersion = libc.versionSync();
61
+ let splitLibcVersion = libcVersion.split(".");
62
+ let libcMajorVersion = splitLibcVersion[0];
63
+ let libcMinorVersion = splitLibcVersion[1];
64
+ if (
65
+ libcMajorVersion != builderGlibcMajorVersion ||
66
+ libcMinorVersion < builderGlibcMInorVersion
67
+ ) {
68
+ // We can't run the glibc binaries, but we can run the static musl ones
69
+ // if they exist
70
+ console.warn(
71
+ "Your glibc isn't compatible; trying static musl binary instead",
72
+ );
73
+ osType = "unknown-linux-musl-static";
74
+ }
75
+ }
76
+ }
77
+
78
+ // Assume the above succeeded and build a target triple to look things up with.
79
+ // If any of it failed, this lookup will fail and we'll handle it like normal.
80
+ let targetTriple = `${arch}-${osType}`;
81
+ let platform = supportedPlatforms[targetTriple];
82
+
83
+ if (!platform) {
84
+ error(
85
+ `Platform with type "${rawOsType}" and architecture "${rawArchitecture}" is not supported by ${name}.\nYour system must be one of the following:\n\n${Object.keys(
86
+ supportedPlatforms,
87
+ ).join(",")}`,
88
+ );
89
+ }
90
+
91
+ return platform;
92
+ };
93
+
94
+ const getPackage = () => {
95
+ const platform = getPlatform();
96
+ const url = `${artifactDownloadUrl}/${platform.artifactName}`;
97
+ let filename = platform.artifactName;
98
+ let ext = platform.zipExt;
99
+ let binary = new Package(name, url, filename, ext, platform.bins);
100
+
101
+ return binary;
102
+ };
103
+
104
+ const install = (suppressLogs) => {
105
+ if (!artifactDownloadUrl || artifactDownloadUrl.length === 0) {
106
+ console.warn("in demo mode, not installing binaries");
107
+ return;
108
+ }
109
+ const package = getPackage();
110
+ const proxy = configureProxy(package.url);
111
+
112
+ return package.install(proxy, suppressLogs);
113
+ };
114
+
115
+ const run = (binaryName) => {
116
+ const package = getPackage();
117
+ const proxy = configureProxy(package.url);
118
+
119
+ package.run(binaryName, proxy);
120
+ };
121
+
122
+ module.exports = {
123
+ install,
124
+ run,
125
+ getPackage,
126
+ };
package/install.js ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { install } = require("./binary");
4
+ install(false);