dprint 0.37.1 → 0.38.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/bin.js CHANGED
@@ -9,21 +9,23 @@ const fs = require("fs");
9
9
  const exePath = path.join(__dirname, os.platform() === "win32" ? "dprint.exe" : "dprint");
10
10
 
11
11
  if (!fs.existsSync(exePath)) {
12
- require("./install_api").runInstall().then(() => {
13
- // I'm not sure why (I think due zip extraction), but the executable
14
- // doesn't seem fully ready unless waiting for the next tick
15
- setTimeout(() => {
16
- runDprintExe();
17
- }, 0);
18
- }).catch(err => {
19
- console.error(err);
12
+ try {
13
+ const resolvedExePath = require("./install_api").runInstall();
14
+ runDprintExe(resolvedExePath);
15
+ } catch (err) {
16
+ if (err !== undefined && typeof err.message === "string") {
17
+ console.error(err.message);
18
+ } else {
19
+ console.error(err);
20
+ }
20
21
  process.exit(1);
21
- });
22
+ }
22
23
  } else {
23
- runDprintExe();
24
+ runDprintExe(exePath);
24
25
  }
25
26
 
26
- function runDprintExe() {
27
+ /** @param exePath {string} */
28
+ function runDprintExe(exePath) {
27
29
  const result = child_process.spawnSync(
28
30
  exePath,
29
31
  process.argv.slice(2),
@@ -36,10 +38,10 @@ function runDprintExe() {
36
38
  throwIfNoExePath();
37
39
 
38
40
  process.exitCode = result.status;
39
- }
40
41
 
41
- function throwIfNoExePath() {
42
- if (!fs.existsSync(exePath)) {
43
- throw new Error("Could not find exe at path '" + exePath + "'. Maybe try running dprint again.");
42
+ function throwIfNoExePath() {
43
+ if (!fs.existsSync(exePath)) {
44
+ throw new Error("Could not find exe at path '" + exePath + "'. Maybe try running dprint again.");
45
+ }
44
46
  }
45
47
  }
package/install_api.js CHANGED
@@ -1,303 +1,140 @@
1
1
  // @ts-check
2
2
  "use strict";
3
3
 
4
- const crypto = require("crypto");
5
4
  const fs = require("fs");
6
- const https = require("https");
7
5
  const os = require("os");
8
6
  const path = require("path");
9
- const url = require("url");
10
- const HttpsProxyAgent = require("https-proxy-agent");
11
- const yauzl = require("yauzl");
12
7
  /** @type {string | undefined} */
13
8
  let cachedIsMusl = undefined;
14
9
 
15
- function install() {
16
- const executableFilePath = path.join(
17
- __dirname,
18
- os.platform() === "win32" ? "dprint.exe" : "dprint",
19
- );
20
-
21
- if (fs.existsSync(executableFilePath)) {
22
- return Promise.resolve();
23
- }
24
-
25
- const info = JSON.parse(fs.readFileSync(path.join(__dirname, "info.json"), "utf8"));
26
- const zipFilePath = path.join(__dirname, "dprint.zip");
27
-
28
- const target = getTarget();
29
- const downloadUrl = "https://github.com/dprint/dprint/releases/download/"
30
- + info.version
31
- + "/dprint-" + target + ".zip";
32
-
33
- // remove the old zip file if it exists
34
- try {
35
- fs.unlinkSync(zipFilePath);
36
- } catch (err) {
37
- // ignore
38
- }
39
-
40
- // now try to download it
41
- return downloadZipFileWithRetries(downloadUrl).then(() => {
42
- verifyZipChecksum();
43
- return extractZipFile().then(() => {
44
- // todo: how to just +x? does it matter?
45
- fs.chmodSync(executableFilePath, 0o755);
46
-
47
- // delete the zip file
48
- try {
49
- fs.unlinkSync(zipFilePath);
50
- } catch (err) {
51
- // ignore
52
- }
53
- }).catch(err => {
54
- throw new Error("Error extracting dprint zip file.\n\n" + err);
55
- });
56
- }).catch(err => {
57
- throw new Error("Error downloading dprint zip file.\n\n" + err);
58
- });
59
-
60
- function getTarget() {
61
- if (os.platform() === "win32") {
62
- return "x86_64-pc-windows-msvc";
63
- } else if (os.platform() === "darwin") {
64
- return `${getArch()}-apple-darwin`;
65
- } else {
66
- return `${getArch()}-unknown-linux-${getLinuxFamily()}`;
10
+ module.exports = {
11
+ runInstall() {
12
+ const dprintFileName = os.platform() === "win32" ? "dprint.exe" : "dprint";
13
+ const targetExecutablePath = path.join(
14
+ __dirname,
15
+ dprintFileName,
16
+ );
17
+
18
+ if (fs.existsSync(targetExecutablePath)) {
19
+ return targetExecutablePath;
67
20
  }
68
- }
69
21
 
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
- });
83
- }
22
+ const target = getTarget();
23
+ const sourcePackagePath = path.dirname(require.resolve("@dprint/" + target + "/package.json"));
24
+ const sourceExecutablePath = path.join(sourcePackagePath, dprintFileName);
84
25
 
85
- return download(3);
86
- }
87
-
88
- function downloadZipFile(url) {
89
- return new Promise((resolve, reject) => {
90
- const options = {};
91
- const proxyUrl = getProxyUrl(url);
92
- if (proxyUrl != null) {
93
- options.agent = new HttpsProxyAgent(proxyUrl);
94
- } else {
95
- // Node 19+ defaults keepAlive to true for `https.get`, but contains a bug
96
- // that prevents the process from exiting. Work around this by explicitly
97
- // disabling keepAlive.
98
- //
99
- // See: https://github.com/nodejs/node/issues/47228
100
- options.agent = new https.Agent({ keepAlive: false });
101
- }
102
-
103
- https.get(url, options, function(response) {
104
- if (response.statusCode != null && response.statusCode >= 200 && response.statusCode <= 299) {
105
- downloadResponse(response).then(resolve).catch(reject);
106
- } else if (response.headers.location) {
107
- downloadZipFile(response.headers.location).then(resolve).catch(reject);
108
- } else {
109
- reject(new Error("Unknown status code " + response.statusCode + " : " + response.statusMessage));
110
- }
111
- }).on("error", function(err) {
112
- try {
113
- fs.unlinkSync(zipFilePath);
114
- } catch (err) {
115
- // ignore
116
- }
117
- reject(err);
118
- });
119
- });
120
-
121
- /** @param response {import("http").IncomingMessage} */
122
- function downloadResponse(response) {
123
- return new Promise((resolve, reject) => {
124
- const file = fs.createWriteStream(zipFilePath);
125
- response.pipe(file);
126
- file.on("finish", function() {
127
- file.close((err) => {
128
- if (err) {
129
- reject(err);
130
- } else {
131
- resolve(undefined);
132
- }
133
- });
134
- });
135
- });
26
+ if (!fs.existsSync(sourceExecutablePath)) {
27
+ throw new Error("Could not find executable for @dprint/" + target + " at " + sourceExecutablePath);
136
28
  }
137
- }
138
29
 
139
- function getProxyUrl(requestUrl) {
140
30
  try {
141
- const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
142
- if (typeof proxyUrl !== "string" || proxyUrl.length === 0) {
143
- return undefined;
31
+ if (process.env.DPRINT_SIMULATED_READONLY_FILE_SYSTEM === "1") {
32
+ console.warn("Simulating readonly file system for testing.");
33
+ throw new Error("Throwing for testing purposes.");
144
34
  }
145
- if (typeof process.env.NO_PROXY === "string") {
146
- const noProxyAddresses = process.env.NO_PROXY.split(",");
147
- const host = url.parse(requestUrl).host;
148
- if (host == null || noProxyAddresses.indexOf(host) >= 0) {
149
- return undefined;
150
- }
35
+
36
+ // in order to make things faster the next time we run, copy the
37
+ // executable into the dprint package folder
38
+ fs.copyFileSync(sourceExecutablePath, targetExecutablePath);
39
+ if (os.platform() !== "win32") {
40
+ // chomd +x
41
+ chmodX(targetExecutablePath);
151
42
  }
152
- return proxyUrl;
43
+ return targetExecutablePath;
153
44
  } catch (err) {
154
- console.error("[dprint]: Error getting proxy url.", err);
155
- return undefined;
156
- }
157
- }
158
-
159
- function verifyZipChecksum() {
160
- const fileData = fs.readFileSync(zipFilePath);
161
- const actualZipChecksum = crypto.createHash("sha256").update(fileData).digest("hex").toLowerCase();
162
- const expectedZipChecksum = getExpectedZipChecksum().toLowerCase();
163
-
164
- if (actualZipChecksum !== expectedZipChecksum) {
165
- throw new Error(
166
- "Downloaded dprint zip checksum did not match the expected checksum (Actual: "
167
- + actualZipChecksum
168
- + ", Expected: "
169
- + expectedZipChecksum
170
- + ").",
171
- );
172
- }
173
-
174
- function getExpectedZipChecksum() {
175
- const checksum = info.checksums[getTarget()];
176
- if (checksum == null) {
177
- throw new Error("Could not find checksum for target: " + checksum);
45
+ // this may fail on readonly file systems... in this case, fall
46
+ // back to using the resolved package path
47
+ if (process.env.DPRINT_DEBUG === "1") {
48
+ console.warn(
49
+ "Failed to copy executable from "
50
+ + sourceExecutablePath + " to " + targetExecutablePath
51
+ + ". Using resolved package path instead.",
52
+ err,
53
+ );
178
54
  }
179
- return checksum;
55
+ // use the path found in the specific package
56
+ try {
57
+ chmodX(sourceExecutablePath);
58
+ } catch (_err) {
59
+ // ignore
60
+ }
61
+ return sourceExecutablePath;
180
62
  }
181
- }
182
-
183
- function extractZipFile() {
184
- return new Promise((resolve, reject) => {
185
- // code adapted from: https://github.com/thejoshwolfe/yauzl#usage
186
- yauzl.open(zipFilePath, { autoClose: true }, (err, zipFile) => {
187
- if (err) {
188
- reject(err);
189
- return;
190
- }
191
-
192
- const pendingWrites = [];
193
-
194
- zipFile.on("entry", (entry) => {
195
- if (!/\/$/.test(entry.fileName)) {
196
- // file entry
197
-
198
- // note: reject at the top level, but resolve this promise when finished
199
- pendingWrites.push(
200
- new Promise((resolve) => {
201
- zipFile.openReadStream(entry, (err, readStream) => {
202
- if (err) {
203
- reject(err);
204
- return;
205
- }
206
- const destination = path.join(__dirname, entry.fileName);
207
- const writeStream = fs.createWriteStream(destination);
208
- readStream.pipe(writeStream);
63
+ },
64
+ };
209
65
 
210
- writeStream.on("error", (err) => {
211
- reject(err);
212
- });
213
- writeStream.on("finish", () => {
214
- resolve(undefined);
215
- });
216
- });
217
- }),
218
- );
219
- }
220
- });
66
+ /** @filePath {string} */
67
+ function chmodX(filePath) {
68
+ const perms = fs.statSync(filePath).mode;
69
+ fs.chmodSync(filePath, perms | 0o111);
70
+ }
221
71
 
222
- zipFile.once("close", function() {
223
- Promise.all(pendingWrites).then(resolve).catch(reject);
224
- });
225
- });
226
- });
72
+ function getTarget() {
73
+ const platform = os.platform();
74
+ if (platform === "linux") {
75
+ return platform + "-" + getArch() + "-" + getLinuxFamily();
76
+ } else {
77
+ return platform + "-" + getArch();
227
78
  }
79
+ }
228
80
 
229
- function getArch() {
230
- if (os.arch() === "arm64") {
231
- return "aarch64";
232
- } else if (os.arch() === "x64") {
233
- return "x86_64";
234
- } else {
235
- throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and aarch64 binaries are available.");
236
- }
81
+ function getArch() {
82
+ const arch = os.arch();
83
+ if (arch !== "arm64" && arch !== "x64") {
84
+ throw new Error("Unsupported architecture " + os.arch() + ". Only x64 and aarch64 binaries are available.");
237
85
  }
86
+ return arch;
87
+ }
238
88
 
239
- function getLinuxFamily() {
240
- return getIsMusl() ? "musl" : "gnu";
89
+ function getLinuxFamily() {
90
+ return getIsMusl() ? "musl" : "glibc";
241
91
 
242
- function getIsMusl() {
243
- // code adapted from https://github.com/lovell/detect-libc
244
- // Copyright Apache 2.0 license, the detect-libc maintainers
245
- if (cachedIsMusl == null) {
246
- cachedIsMusl = innerGet();
247
- }
248
- return cachedIsMusl;
92
+ function getIsMusl() {
93
+ // code adapted from https://github.com/lovell/detect-libc
94
+ // Copyright Apache 2.0 license, the detect-libc maintainers
95
+ if (cachedIsMusl == null) {
96
+ cachedIsMusl = innerGet();
97
+ }
98
+ return cachedIsMusl;
249
99
 
250
- function innerGet() {
251
- try {
252
- if (os.platform() !== "linux") {
253
- return false;
254
- }
255
- return isProcessReportMusl() || isConfMusl();
256
- } catch (err) {
257
- // just in case
258
- console.warn("Error checking if musl.", err);
100
+ function innerGet() {
101
+ try {
102
+ if (os.platform() !== "linux") {
259
103
  return false;
260
104
  }
105
+ return isProcessReportMusl() || isConfMusl();
106
+ } catch (err) {
107
+ // just in case
108
+ console.warn("Error checking if musl.", err);
109
+ return false;
261
110
  }
111
+ }
262
112
 
263
- function isProcessReportMusl() {
264
- if (!process.report) {
265
- return false;
266
- }
267
- const report = process.report.getReport();
268
- if (!report || !(report.sharedObjects instanceof Array)) {
269
- return false;
270
- }
271
- return report.sharedObjects.some(o => o.includes("libc.musl-") || o.includes("ld-musl-"));
113
+ function isProcessReportMusl() {
114
+ if (!process.report) {
115
+ return false;
272
116
  }
273
-
274
- function isConfMusl() {
275
- const output = getCommandOutput();
276
- const [_, ldd1] = output.split(/[\r\n]+/);
277
- return ldd1 && ldd1.includes("musl");
117
+ const rawReport = process.report.getReport();
118
+ const report = typeof rawReport === "string" ? JSON.parse(rawReport) : rawReport;
119
+ if (!report || !(report.sharedObjects instanceof Array)) {
120
+ return false;
278
121
  }
122
+ return report.sharedObjects.some(o => o.includes("libc.musl-") || o.includes("ld-musl-"));
123
+ }
279
124
 
280
- function getCommandOutput() {
281
- try {
282
- const command = "getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true";
283
- return require("child_process").execSync(command, { encoding: "utf8" });
284
- } catch (_err) {
285
- return "";
286
- }
125
+ function isConfMusl() {
126
+ const output = getCommandOutput();
127
+ const [_, ldd1] = output.split(/[\r\n]+/);
128
+ return ldd1 && ldd1.includes("musl");
129
+ }
130
+
131
+ function getCommandOutput() {
132
+ try {
133
+ const command = "getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true";
134
+ return require("child_process").execSync(command, { encoding: "utf8" });
135
+ } catch (_err) {
136
+ return "";
287
137
  }
288
138
  }
289
139
  }
290
140
  }
291
-
292
- module.exports = {
293
- runInstall() {
294
- return install().catch(err => {
295
- if (err !== undefined && typeof err.message === "string") {
296
- console.error(err.message);
297
- } else {
298
- console.error(err);
299
- }
300
- process.exit(1);
301
- });
302
- },
303
- };
package/package.json CHANGED
@@ -1,11 +1,8 @@
1
1
  {
2
2
  "name": "dprint",
3
- "version": "0.37.1",
3
+ "version": "0.38.1",
4
4
  "description": "Pluggable and configurable code formatting platform written in Rust.",
5
5
  "bin": "bin.js",
6
- "scripts": {
7
- "postinstall": "node ./install.js"
8
- },
9
6
  "repository": {
10
7
  "type": "git",
11
8
  "url": "git+https://github.com/dprint/dprint.git"
@@ -20,8 +17,13 @@
20
17
  "url": "https://github.com/dprint/dprint/issues"
21
18
  },
22
19
  "homepage": "https://github.com/dprint/dprint#readme",
23
- "dependencies": {
24
- "https-proxy-agent": "=5.0.1",
25
- "yauzl": "=2.10.0"
20
+ "preferUnplugged": true,
21
+ "optionalDependencies": {
22
+ "@dprint/win32-x64": "0.38.1",
23
+ "@dprint/darwin-x64": "0.38.1",
24
+ "@dprint/darwin-arm64": "0.38.1",
25
+ "@dprint/linux-x64-glibc": "0.38.1",
26
+ "@dprint/linux-x64-musl": "0.38.1",
27
+ "@dprint/linux-arm64-glibc": "0.38.1"
26
28
  }
27
- }
29
+ }
package/info.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "version": "0.37.1",
3
- "checksums": {
4
- "x86_64-pc-windows-msvc": "7f25e3cd03aeee23341174f50ceff7d09266d991298b44353883a98cb1b8f67e",
5
- "x86_64-apple-darwin": "1de7aea5ead649d97c19814d856fab616965177cd77cccc998f4953aa3d8c7c4",
6
- "aarch64-apple-darwin": "a2c5c7a61ae4ca51140779b1bcc2766d8fe0fad0589828752b29b172f20c083c",
7
- "x86_64-unknown-linux-gnu": "a65e2dc853cf466ec1c682366cc1655029b7d180414ec53e0e0ecc37e503a28d",
8
- "x86_64-unknown-linux-musl": "a8a824eed75f1030c721a7e91ae79cbd5f48f8c19470d92ec5fd0dff2715add6",
9
- "aarch64-unknown-linux-gnu": "17d108c5b27df6233fa63c4f78b8b67c040cb3325a7372c9428da62f09ed8f61"
10
- }
11
- }
package/install.js DELETED
@@ -1,4 +0,0 @@
1
- // @ts-check
2
- "use strict";
3
-
4
- require("./install_api").runInstall();