dprint 0.32.2 → 0.34.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.
Files changed (4) hide show
  1. package/bin.js +11 -15
  2. package/info.json +7 -6
  3. package/install_api.js +113 -28
  4. package/package.json +2 -1
package/bin.js CHANGED
@@ -13,29 +13,25 @@ if (!fs.existsSync(exePath)) {
13
13
  runDprintExe();
14
14
  }).catch(err => {
15
15
  console.error(err);
16
- process.exit(child_process.exitCode || 1);
16
+ process.exit(1);
17
17
  });
18
18
  } else {
19
19
  runDprintExe();
20
20
  }
21
21
 
22
22
  function runDprintExe() {
23
- try {
24
- const result = child_process.spawnSync(
25
- exePath,
26
- process.argv.slice(2),
27
- { stdio: "inherit" },
28
- );
29
- if (result.error) {
30
- throw result.error;
31
- }
23
+ const result = child_process.spawnSync(
24
+ exePath,
25
+ process.argv.slice(2),
26
+ { stdio: "inherit" },
27
+ );
28
+ if (result.error) {
29
+ throw result.error;
30
+ }
32
31
 
33
- throwIfNoExePath();
32
+ throwIfNoExePath();
34
33
 
35
- process.exitCode = result.status;
36
- } catch (err) {
37
- throw err;
38
- }
34
+ process.exitCode = result.status;
39
35
  }
40
36
 
41
37
  function throwIfNoExePath() {
package/info.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
- "version": "0.32.2",
2
+ "version": "0.34.0",
3
3
  "checksums": {
4
- "windows-x86_64": "e4c0916438a11b18b1d6a3ce7c4e23f18998648d39247fa0b617c9863dea6d11",
5
- "darwin-x86_64": "5abcc7e2f99b98a629b73ded47ca84bc851b4f6ec35ff6df496c2eefd569c55c",
6
- "darwin-aarch64": "ec909acfc179b064e7d19e130c1aa1bdf0350d929b9a04dc6f6b5ce84432eb69",
7
- "linux-x86_64": "8e8d0f32e5522a61f6251fb3b9c6be106584ccdfdb13008b276c1ee8bfdc6ffa",
8
- "linux-aarch64": "ddff520568958b6dfeac83105a913db7247188035a0e1af12d4100d0c0d631e4"
4
+ "windows-x86_64": "29da4f68d07cab10d0d90fdcf671c3ccdda15503ac5aa5c23c72534064b0e0b4",
5
+ "darwin-x86_64": "e9907275ab476897a72f99e279f137df0cea81b63a78da9367d3027ff072b18e",
6
+ "darwin-aarch64": "21a43b89adc42fc563371b6807642c8eb8669e75471a57bef67de99bd1bb7b75",
7
+ "linux-x86_64-gnu": "9bf233635ed8f0871fa7285ab40ea78c9216650073ad25ffec097b57738da268",
8
+ "linux-x86_64-musl": "227572fbcb761310dd21c2286fdc56a033f408919706895f7bf3913f241cf63a",
9
+ "linux-aarch64": "ef75ba1e02064a1f22184aab1888d0040e795e996c27c5339694e45cbad0500e"
9
10
  }
10
11
  }
package/install_api.js CHANGED
@@ -1,12 +1,16 @@
1
1
  // @ts-check
2
2
  "use strict";
3
3
 
4
- const https = require("https");
5
- const fs = require("fs");
6
4
  const crypto = require("crypto");
5
+ const fs = require("fs");
6
+ const https = require("https");
7
7
  const os = require("os");
8
8
  const path = require("path");
9
+ const url = require("url");
10
+ const HttpsProxyAgent = require("https-proxy-agent");
9
11
  const yauzl = require("yauzl");
12
+ /** @type {string | undefined} */
13
+ let cachedIsMusl = undefined;
10
14
 
11
15
  function install() {
12
16
  const executableFilePath = path.join(
@@ -34,7 +38,7 @@ function install() {
34
38
  }
35
39
 
36
40
  // now try to download it
37
- return downloadZipFile(url).then(() => {
41
+ return downloadZipFileWithRetries(url).then(() => {
38
42
  verifyZipChecksum();
39
43
  return extractZipFile().then(() => {
40
44
  // todo: how to just +x? does it matter?
@@ -57,27 +61,39 @@ function install() {
57
61
  if (os.platform() === "win32") {
58
62
  return "x86_64-pc-windows-msvc";
59
63
  } else if (os.platform() === "darwin") {
60
- if (os.arch() === "arm64") {
61
- return "aarch64-apple-darwin";
62
- } else if (os.arch() === "x64") {
63
- return "x86_64-apple-darwin";
64
- } else {
65
- throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and M1 binaries are available.");
66
- }
64
+ return `${getArch()}-apple-darwin`;
67
65
  } else {
68
- if (os.arch() === "arm64") {
69
- return "aarch64-unknown-linux-gnu";
70
- } else if (os.arch() === "x64") {
71
- return "x86_64-unknown-linux-gnu";
72
- } else {
73
- throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and aarch64 binaries are available.");
74
- }
66
+ return `${getArch()}-unknown-linux-${getLinuxFamily()}`;
67
+ }
68
+ }
69
+
70
+ function downloadZipFileWithRetries(url) {
71
+ /** @param remaining {number} */
72
+ function download(remaining) {
73
+ return downloadZipFile(url)
74
+ .catch(err => {
75
+ if (remaining === 0) {
76
+ return Promise.reject(err);
77
+ } else {
78
+ console.error("Error downloading dprint zip file.", err);
79
+ console.error("Retrying download (remaining: " + remaining + ")");
80
+ return download(remaining - 1);
81
+ }
82
+ });
75
83
  }
84
+
85
+ return download(3);
76
86
  }
77
87
 
78
88
  function downloadZipFile(url) {
79
89
  return new Promise((resolve, reject) => {
80
- https.get(url, function(response) {
90
+ const options = {};
91
+ const proxyUrl = getProxyUrl(url);
92
+ if (proxyUrl != null) {
93
+ options.agent = new HttpsProxyAgent(proxyUrl);
94
+ }
95
+
96
+ https.get(url, options, function(response) {
81
97
  if (response.statusCode >= 200 && response.statusCode <= 299) {
82
98
  downloadResponse(response).then(resolve).catch(reject);
83
99
  } else if (response.headers.location) {
@@ -113,6 +129,21 @@ function install() {
113
129
  }
114
130
  }
115
131
 
132
+ function getProxyUrl(requestUrl) {
133
+ const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
134
+ if (typeof proxyUrl !== "string" || proxyUrl.length === 0) {
135
+ return undefined;
136
+ }
137
+ if (typeof process.env.NO_PROXY === "string") {
138
+ const noProxyAddresses = process.env.NO_PROXY.split(",");
139
+ const host = url.parse(requestUrl).host;
140
+ if (noProxyAddresses.indexOf(host) >= 0) {
141
+ return undefined;
142
+ }
143
+ }
144
+ return proxyUrl;
145
+ }
146
+
116
147
  function verifyZipChecksum() {
117
148
  const fileData = fs.readFileSync(zipFilePath);
118
149
  const actualZipChecksum = crypto.createHash("sha256").update(fileData).digest("hex").toLowerCase();
@@ -133,17 +164,9 @@ function install() {
133
164
  case "win32":
134
165
  return info.checksums["windows-x86_64"];
135
166
  case "darwin":
136
- if (os.arch() === "arm64") {
137
- return info.checksums["darwin-aarch64"];
138
- } else {
139
- return info.checksums["darwin-x86_64"];
140
- }
167
+ return info.checksums[`darwin-${getArch()}`];
141
168
  default:
142
- if (os.arch() === "arm64") {
143
- return info.checksums["linux-aarch64"];
144
- } else {
145
- return info.checksums["linux-x86_64"];
146
- }
169
+ return info.checksums[`linux-${getArch()}-${getLinuxFamily()}`];
147
170
  }
148
171
  }
149
172
  }
@@ -193,6 +216,68 @@ function install() {
193
216
  });
194
217
  });
195
218
  }
219
+
220
+ function getArch() {
221
+ if (os.arch() === "arm64") {
222
+ return "aarch64";
223
+ } else if (os.arch() === "x64") {
224
+ return "x86_64";
225
+ } else {
226
+ throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and aarch64 binaries are available.");
227
+ }
228
+ }
229
+
230
+ function getLinuxFamily() {
231
+ return getIsMusl() ? "musl" : "gnu";
232
+
233
+ function getIsMusl() {
234
+ // code adapted from https://github.com/lovell/detect-libc
235
+ // Copyright Apache 2.0 license, the detect-libc maintainers
236
+ if (cachedIsMusl == null) {
237
+ cachedIsMusl = innerGet();
238
+ }
239
+ return cachedIsMusl;
240
+
241
+ function innerGet() {
242
+ try {
243
+ if (os.platform() !== "linux") {
244
+ return false;
245
+ }
246
+ return isProcessReportMusl() || isConfMusl();
247
+ } catch (err) {
248
+ // just in case
249
+ console.warn("Error checking if musl.", err);
250
+ return false;
251
+ }
252
+ }
253
+
254
+ function isProcessReportMusl() {
255
+ if (!process.report) {
256
+ return false;
257
+ }
258
+ const report = process.report.getReport();
259
+ if (!report || !(report.sharedObjects instanceof Array)) {
260
+ return false;
261
+ }
262
+ return report.sharedObjects.some(o => o.includes("libc.musl-") || o.includes("ld-musl-"));
263
+ }
264
+
265
+ function isConfMusl() {
266
+ const output = getCommandOutput();
267
+ const [_, ldd1] = output.split(/[\r\n]+/);
268
+ return ldd1 && ldd1.includes("musl");
269
+ }
270
+
271
+ function getCommandOutput() {
272
+ try {
273
+ const command = "getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true";
274
+ return require("child_process").execSync(command, { encoding: "utf8" });
275
+ } catch (_err) {
276
+ return "";
277
+ }
278
+ }
279
+ }
280
+ }
196
281
  }
197
282
 
198
283
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dprint",
3
- "version": "0.32.2",
3
+ "version": "0.34.0",
4
4
  "description": "Pluggable and configurable code formatting platform written in Rust.",
5
5
  "bin": "bin.js",
6
6
  "scripts": {
@@ -21,6 +21,7 @@
21
21
  },
22
22
  "homepage": "https://github.com/dprint/dprint#readme",
23
23
  "dependencies": {
24
+ "https-proxy-agent": "=5.0.1",
24
25
  "yauzl": "=2.10.0"
25
26
  }
26
27
  }