rustywind 0.24.3 → 0.25.0-alpha.1

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.
package/lib/download.js DELETED
@@ -1,318 +0,0 @@
1
- // @ts-check
2
- "use strict";
3
-
4
- const path = require("path");
5
- const fs = require("fs");
6
- const os = require("os");
7
- const https = require("https");
8
- const util = require("util");
9
- const url = require("url");
10
- const URL = url.URL;
11
- const child_process = require("child_process");
12
- const proxy_from_env = require("proxy-from-env");
13
-
14
- const packageVersion = require("../package.json").version;
15
- const tmpDir = path.join(os.tmpdir(), `rustywind-cache-${packageVersion}`);
16
-
17
- const fsUnlink = util.promisify(fs.unlink);
18
- const fsExists = util.promisify(fs.exists);
19
- const fsMkdir = util.promisify(fs.mkdir);
20
-
21
- const isWindows = os.platform() === "win32";
22
-
23
- /**
24
- * @param {string} _url
25
- * @returns boolean
26
- */
27
- function isGithubUrl(_url) {
28
- return url.parse(_url).hostname === "api.github.com";
29
- }
30
-
31
- /**
32
- * @param {string} url
33
- * @param {string} dest
34
- * @param {{ headers: Record<string, string>; proxy?: string; }} opts
35
- * @returns boolean
36
- */
37
- function downloadWin(url, dest, opts) {
38
- return new Promise((resolve, reject) => {
39
- let userAgent;
40
- if (opts.headers["user-agent"]) {
41
- userAgent = opts.headers["user-agent"];
42
- delete opts.headers["user-agent"];
43
- }
44
- const headerValues = Object.keys(opts.headers)
45
- .map((key) => `\\"${key}\\"=\\"${opts.headers[key]}\\"`)
46
- .join("; ");
47
- const headers = `@{${headerValues}}`;
48
- console.log("Downloading with Invoke-WebRequest");
49
- dest = sanitizePathForPowershell(dest);
50
- let iwrCmd = `[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -URI ${url} -UseBasicParsing -OutFile ${dest} -Headers ${headers}`;
51
- if (userAgent) {
52
- iwrCmd += " -UserAgent " + userAgent;
53
- }
54
- if (opts.proxy) {
55
- iwrCmd += " -Proxy " + opts.proxy;
56
-
57
- try {
58
- const { username, password } = new URL(opts.proxy);
59
- if (username && password) {
60
- const decodedPassword = decodeURIComponent(password);
61
- iwrCmd += ` -ProxyCredential (New-Object PSCredential ('${username}', (ConvertTo-SecureString '${decodedPassword}' -AsPlainText -Force)))`;
62
- }
63
- } catch (err) {
64
- reject(err);
65
- }
66
- }
67
-
68
- iwrCmd = `powershell "${iwrCmd}"`;
69
-
70
- child_process.exec(iwrCmd, (err) => {
71
- if (err) {
72
- reject(err);
73
- return;
74
- }
75
- resolve();
76
- });
77
- });
78
- }
79
-
80
- async function download(_url, dest, opts) {
81
- // Handle proxy setup
82
- const proxy = proxy_from_env.getProxyForUrl(url.parse(_url));
83
- if (proxy) {
84
- const HttpsProxyAgent = require("https-proxy-agent");
85
- opts = {
86
- ...opts,
87
- agent: new HttpsProxyAgent(proxy),
88
- proxy,
89
- };
90
- }
91
-
92
- // Windows-specific handling
93
- if (isWindows) {
94
- return downloadWin(_url, dest, opts);
95
- }
96
-
97
- // Remove auth headers for non-GitHub URLs
98
- if (opts.headers.authorization && !isGithubUrl(_url)) {
99
- delete opts.headers.authorization;
100
- }
101
-
102
- return new Promise((resolve, reject) => {
103
- const outFile = fs.createWriteStream(dest);
104
- const mergedOpts = { ...url.parse(_url), ...opts };
105
-
106
- const request = https.get(mergedOpts, (response) => {
107
- // Handle redirects
108
- if (response.statusCode === 302) {
109
- outFile.destroy();
110
- return download(response.headers.location, dest, opts).then(resolve, reject);
111
- }
112
-
113
- // Handle error status codes
114
- if (response.statusCode !== 200) {
115
- outFile.destroy();
116
- reject(new Error(`Download failed with ${response.statusCode}`));
117
- return;
118
- }
119
-
120
- // Pipe the response to file
121
- response.pipe(outFile);
122
- });
123
-
124
- // Handle write stream errors
125
- outFile.on('error', async (err) => {
126
- outFile.destroy();
127
- await fsUnlink(dest).catch(() => { });
128
- reject(err);
129
- });
130
-
131
- // Handle successful completion
132
- outFile.on('finish', () => {
133
- outFile.close(() => resolve(null));
134
- });
135
-
136
- // Handle request errors
137
- request.on('error', async (err) => {
138
- outFile.destroy();
139
- await fsUnlink(dest).catch(() => { });
140
- reject(err);
141
- });
142
- });
143
- }
144
-
145
- /**
146
- * @param {{ force: boolean; token: string; version: string; }} opts
147
- * @param {string} assetName
148
- * @param {string} downloadFolder
149
- * @return {Promise<void>}
150
- */
151
- async function getAssetFromGitHub(opts, assetName, downloadFolder) {
152
- const assetDownloadPath = path.join(downloadFolder, assetName);
153
-
154
- // We can just use the cached binary
155
- if (!opts.force && (await fsExists(assetDownloadPath))) {
156
- console.log("Using cached download: " + assetDownloadPath);
157
- return;
158
- }
159
-
160
- const downloadOpts = {
161
- headers: {
162
- "user-agent": "rustywind",
163
- },
164
- };
165
-
166
- downloadOpts.headers.accept = "application/octet-stream";
167
- if (opts.token) {
168
- downloadOpts.headers.authorization = `token ${opts.token}`;
169
- }
170
-
171
- const jsonRelease = require("../release.json");
172
- const asset = jsonRelease.assets.find((a) => a.name === assetName);
173
- if (!asset) {
174
- throw new Error("Asset not found with name: " + assetName);
175
- }
176
-
177
- console.log(`Downloading from ${asset.url}`);
178
- console.log(`Downloading to ${assetDownloadPath}`);
179
-
180
- try {
181
- await download(asset.url, assetDownloadPath, downloadOpts);
182
- } catch (e) {
183
- console.error("Download failed:", e);
184
- console.error(
185
- `Attempting to download from 'browser download url' ${asset.browser_download_url} instead`
186
- );
187
-
188
- delete downloadOpts.headers.authorization;
189
- await download(asset.browser_download_url, assetDownloadPath, downloadOpts);
190
- }
191
- }
192
-
193
- function unzipWindows(zipPath, destinationDir) {
194
- return new Promise((resolve, reject) => {
195
- zipPath = sanitizePathForPowershell(zipPath);
196
- destinationDir = sanitizePathForPowershell(destinationDir);
197
- const expandCmd =
198
- "powershell -ExecutionPolicy Bypass -Command Expand-Archive " +
199
- ["-Path", zipPath, "-DestinationPath", destinationDir, "-Force"].join(
200
- " "
201
- );
202
- child_process.exec(expandCmd, (err, _stdout, stderr) => {
203
- if (err) {
204
- reject(err);
205
- return;
206
- }
207
-
208
- if (stderr) {
209
- console.log(stderr);
210
- reject(new Error(stderr));
211
- return;
212
- }
213
-
214
- console.log("Expand-Archive completed");
215
- resolve();
216
- });
217
- });
218
- }
219
-
220
- // Handle whitespace in filepath as powershell split's path with whitespaces
221
- function sanitizePathForPowershell(path) {
222
- path = path.replace(/ /g, "` "); // replace whitespace with "` " as solution provided here https://stackoverflow.com/a/18537344/7374562
223
- return path;
224
- }
225
-
226
- function untar(zipPath, destinationDir) {
227
- return new Promise((resolve, reject) => {
228
- const unzipProc = child_process.spawn(
229
- "tar",
230
- ["xvf", zipPath, "-C", destinationDir],
231
- { stdio: "inherit" }
232
- );
233
- unzipProc.on("error", (err) => {
234
- reject(err);
235
- });
236
- unzipProc.on("close", (code) => {
237
- console.log(`tar xvf exited with ${code}`);
238
- if (code !== 0) {
239
- reject(new Error(`tar xvf exited with ${code}`));
240
- return;
241
- } else {
242
- process.exit(0);
243
- }
244
-
245
- resolve();
246
- });
247
- });
248
- }
249
-
250
- async function unzipRustywind(zipPath, destinationDir) {
251
- if (isWindows) {
252
- await unzipWindows(zipPath, destinationDir);
253
- } else {
254
- await untar(zipPath, destinationDir);
255
- }
256
-
257
- const expectedName = path.join(destinationDir, "rustywind");
258
- if (await fsExists(expectedName)) {
259
- return expectedName;
260
- }
261
-
262
- if (await fsExists(expectedName + ".exe")) {
263
- return expectedName + ".exe";
264
- }
265
-
266
- throw new Error(
267
- `Expecting rustywind or rustywind.exe unzipped into ${destinationDir}, didn't find one.`
268
- );
269
- }
270
-
271
- module.exports = async (opts) => {
272
- if (!opts.version) {
273
- return Promise.reject(new Error("Missing version"));
274
- }
275
-
276
- if (!opts.target) {
277
- return Promise.reject(new Error("Missing target"));
278
- }
279
-
280
- const extension = isWindows ? ".zip" : ".tar.gz";
281
- const assetName =
282
- ["rustywind", opts.version, opts.target].join("-") + extension;
283
-
284
- if (!(await fsExists(tmpDir))) {
285
- await fsMkdir(tmpDir);
286
- }
287
-
288
- const assetDownloadPath = path.join(tmpDir, assetName);
289
- try {
290
- await getAssetFromGitHub(opts, assetName, tmpDir);
291
- } catch (e) {
292
- console.log("Deleting invalid download cache");
293
- try {
294
- await fsUnlink(assetDownloadPath);
295
- } catch (e) { }
296
-
297
- throw e;
298
- }
299
-
300
- console.log(`Unzipping to ${opts.destDir}`);
301
- try {
302
- const destinationPath = await unzipRustywind(
303
- assetDownloadPath,
304
- opts.destDir
305
- );
306
- if (!isWindows) {
307
- await util.promisify(fs.chmod)(destinationPath, "755");
308
- }
309
- } catch (e) {
310
- console.log("Deleting invalid download");
311
-
312
- try {
313
- await fsUnlink(assetDownloadPath);
314
- } catch (e) { }
315
-
316
- throw e;
317
- }
318
- };
package/lib/get.js DELETED
@@ -1,51 +0,0 @@
1
- // @ts-check
2
- "use strict";
3
-
4
- const https = require("https");
5
- const url = require("url");
6
- const proxy_from_env = require("proxy-from-env");
7
-
8
- module.exports = get;
9
-
10
- /**
11
- * @param {string} _url
12
- * @param {https.RequestOptions} opts
13
- * @returns
14
- */
15
- function get(_url, opts) {
16
- console.log(`GET ${_url}`);
17
-
18
- const proxy = proxy_from_env.getProxyForUrl(url.parse(_url));
19
- if (proxy !== "") {
20
- var HttpsProxyAgent = require("https-proxy-agent");
21
- opts = {
22
- ...opts,
23
- agent: new HttpsProxyAgent(proxy),
24
- };
25
- }
26
-
27
- return new Promise((resolve, reject) => {
28
- let result = "";
29
- opts = {
30
- ...url.parse(_url),
31
- ...opts,
32
- };
33
- https.get(opts, (response) => {
34
- if (response.statusCode !== 200) {
35
- reject(new Error("Request failed: " + response.statusCode));
36
- }
37
-
38
- response.on("data", (d) => {
39
- result += d.toString();
40
- });
41
-
42
- response.on("end", () => {
43
- resolve(result);
44
- });
45
-
46
- response.on("error", (e) => {
47
- reject(e);
48
- });
49
- });
50
- });
51
- }
package/lib/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const rustyWindPath: string;
package/lib/index.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
-
3
- const path = require("path");
4
-
5
- module.exports.rustyWindPath = path.join(
6
- __dirname,
7
- `../bin/rustywind${process.platform === "win32" ? ".exe" : ""}`
8
- );
@@ -1,84 +0,0 @@
1
- // @ts-check
2
- "use strict";
3
-
4
- // Imports
5
- const os = require("os");
6
- const fs = require("fs");
7
- const path = require("path");
8
- const util = require("util");
9
-
10
- const download = require("./download");
11
-
12
- const fsExists = util.promisify(fs.exists);
13
- const mkdir = util.promisify(fs.mkdir);
14
-
15
- const forceInstall = process.argv.includes("--force");
16
- if (forceInstall) {
17
- console.log("--force, ignoring caches");
18
- }
19
-
20
- const { VERSION } = require("./constants");
21
- const BIN_PATH = path.join(__dirname, "../bin");
22
-
23
-
24
- ////////////////////////////////////////////////////////////////////////////////
25
- const APP_NAME = "rustywind";
26
- const REPO = "avencera/rustywind";
27
- const GITHUB_REPO = `https://github.com/${REPO}`;
28
- ////////////////////////////////////////////////////////////////////////////////
29
-
30
- process.on("unhandledRejection", (reason, promise) => {
31
- console.log("Unhandled rejection: ", promise, "reason:", reason);
32
- });
33
-
34
- function getTarget() {
35
- const arch = os.arch();
36
- const platform = os.platform();
37
-
38
- console.log(`Downloading: ${APP_NAME}`);
39
- console.log(` from: ${GITHUB_REPO}`);
40
- console.log(` for platform: ${arch}-${platform}\n`);
41
-
42
- switch (platform) {
43
- case "darwin":
44
- return arch == "x64" ? "x86_64-apple-darwin" : "aarch64-apple-darwin";
45
- case "win32":
46
- return arch === "x64" ? "x86_64-pc-windows-msvc" : "i686-pc-windows-msvc";
47
- case "linux":
48
- return arch === "x64"
49
- ? "x86_64-unknown-linux-musl"
50
- : arch === "arm"
51
- ? "arm-unknown-linux-gnueabihf"
52
- : arch === "arm64"
53
- ? "aarch64-unknown-linux-gnu"
54
- : arch === "ppc64"
55
- ? "powerpc64le-unknown-linux-gnu"
56
- : "i686-unknown-linux-musl";
57
- default:
58
- throw new Error("Unknown platform: " + os.platform());
59
- }
60
- }
61
-
62
- async function main() {
63
- const binExists = await fsExists(BIN_PATH);
64
- if (!binExists) {
65
- await mkdir(BIN_PATH);
66
- }
67
-
68
- const opts = {
69
- version: VERSION,
70
- token: process.env["GITHUB_TOKEN"],
71
- target: getTarget(),
72
- destDir: BIN_PATH,
73
- force: forceInstall,
74
- };
75
-
76
- try {
77
- await download(opts);
78
- } catch (err) {
79
- console.error(`Downloading rustywind failed: ${err.stack}`);
80
- process.exit(1);
81
- }
82
- }
83
-
84
- main();
package/lib/release.json DELETED
File without changes
@@ -1,33 +0,0 @@
1
- // @ts-check
2
- "use strict";
3
-
4
- const fs = require("fs");
5
-
6
- const { VERSION } = require("./constants");
7
- const downloadRelease = require("./download-release");
8
-
9
- process.on("unhandledRejection", (reason, promise) => {
10
- console.log("Unhandled rejection: ", promise, "reason:", reason);
11
- });
12
-
13
- async function main() {
14
- const opts = {
15
- version: VERSION,
16
- token: process.env["GITHUB_TOKEN"],
17
- };
18
- try {
19
- const release = await downloadRelease(opts);
20
- release.assets = release.assets.map((asset) => ({
21
- ...asset,
22
- download_count: undefined,
23
- }));
24
- fs.writeFileSync("release.json", JSON.stringify(release, undefined, 2), {
25
- encoding: "utf8",
26
- });
27
- } catch (err) {
28
- console.error(`Downloading rustywind metadata failed: ${err.stack}`);
29
- process.exit(1);
30
- }
31
- }
32
-
33
- main();