vue-go-tsc 0.2.2 → 0.2.4

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/vue-go-tsc CHANGED
@@ -263,6 +263,14 @@ async function main() {
263
263
 
264
264
  child.on('error', (err) => {
265
265
  console.error(`vue-go-tsc: failed to start ${BIN_PATH}: ${err.message}`);
266
+ if (['ENOENT', 'EACCES', 'ENOEXEC'].includes(err.code)) {
267
+ // Missing, non-executable or truncated binary (e.g. an interrupted
268
+ // install, or a package manager that skipped the postinstall script).
269
+ console.error(
270
+ `vue-go-tsc: the tsgo binary is missing or broken; reinstall it with:\n` +
271
+ ` node ${join(__dirname, '..', 'install.js')}`
272
+ );
273
+ }
266
274
  process.exit(1);
267
275
  });
268
276
 
package/install.js CHANGED
@@ -1,10 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const { existsSync, mkdirSync, chmodSync, copyFileSync } = require('fs');
3
+ const { existsSync, mkdirSync, chmodSync, copyFileSync, renameSync, rmSync, readdirSync, createWriteStream } = require('fs');
4
4
  const { join } = require('path');
5
5
  const os = require('os');
6
+ const http = require('http');
6
7
  const https = require('https');
7
- const { createWriteStream } = require('fs');
8
+ const { spawnSync } = require('child_process');
8
9
 
9
10
  const PACKAGE_VERSION = require('./package.json').version;
10
11
  const BINARY_NAME = process.platform === 'win32' ? 'tsgo.exe' : 'tsgo';
@@ -17,6 +18,8 @@ const BIN_PATH = join(BIN_DIR, BINARY_NAME);
17
18
  const CACHE_ROOT = process.env.VUE_GO_TSC_CACHE_DIR || join(os.homedir(), '.cache', 'vue-go-tsc');
18
19
  const CACHE_DIR = join(CACHE_ROOT, `v${PACKAGE_VERSION}`);
19
20
 
21
+ const MAX_REDIRECTS = 10;
22
+
20
23
  // Platform mapping for GitHub releases
21
24
  const PLATFORM_MAP = {
22
25
  'darwin-x64': 'tsgo-darwin-amd64',
@@ -41,46 +44,151 @@ function getPlatformBinary() {
41
44
  return PLATFORM_MAP[key];
42
45
  }
43
46
 
44
- function download(url, dest) {
47
+ // A file next to `dest` that is unique to this process, so concurrent
48
+ // installs (parallel CI jobs, pnpm workspaces) never write the same file.
49
+ function tempPathFor(dest) {
50
+ return `${dest}.${process.pid}.tmp`;
51
+ }
52
+
53
+ // Temporary files being written by this process, removed if it is killed.
54
+ const pendingTempFiles = new Set();
55
+
56
+ function cleanupPendingTempFiles() {
57
+ for (const tmp of pendingTempFiles) {
58
+ rmSync(tmp, { force: true });
59
+ }
60
+ }
61
+
62
+ // Remove temporary binaries left in `dir` by an install that was killed
63
+ // before it could clean up. Only for directories no other install writes to.
64
+ function removeStaleTempFiles(dir, name) {
65
+ try {
66
+ for (const entry of readdirSync(dir)) {
67
+ if (entry.startsWith(`${name}.`) && entry.endsWith('.tmp')) {
68
+ rmSync(join(dir, entry), { force: true });
69
+ }
70
+ }
71
+ } catch {}
72
+ }
73
+
74
+ // Download `url` to `dest`.
75
+ //
76
+ // The body is streamed into a temporary file and only renamed onto `dest` once
77
+ // it is complete: an interrupted install (Ctrl+C, a sibling postinstall
78
+ // failing, a network cut) must never leave a truncated binary behind, since a
79
+ // present binary is what later installs trust. A body shorter than the
80
+ // announced Content-Length is rejected for the same reason.
81
+ function download(url, dest, redirects = 0) {
45
82
  return new Promise((resolve, reject) => {
46
- const file = createWriteStream(dest);
47
- https.get(url, (response) => {
48
- if (response.statusCode === 302 || response.statusCode === 301) {
49
- // Follow redirect
50
- download(response.headers.location, dest).then(resolve).catch(reject);
83
+ const client = url.startsWith('http:') ? http : https;
84
+ const request = client.get(url, (response) => {
85
+ const { statusCode } = response;
86
+ if ([301, 302, 303, 307, 308].includes(statusCode) && response.headers.location) {
87
+ response.resume();
88
+ if (redirects >= MAX_REDIRECTS) {
89
+ reject(new Error(`Download failed: too many redirects (${url})`));
90
+ return;
91
+ }
92
+ const next = new URL(response.headers.location, url).toString();
93
+ download(next, dest, redirects + 1).then(resolve, reject);
51
94
  return;
52
95
  }
53
96
 
54
- if (response.statusCode !== 200) {
55
- reject(new Error(`Download failed: ${response.statusCode} ${response.statusMessage}`));
97
+ if (statusCode !== 200) {
98
+ response.resume();
99
+ reject(new Error(`Download failed: ${statusCode} ${response.statusMessage}`));
56
100
  return;
57
101
  }
58
102
 
59
- response.pipe(file);
103
+ const expected = Number(response.headers['content-length']);
104
+ const tmp = tempPathFor(dest);
105
+ pendingTempFiles.add(tmp);
106
+ const file = createWriteStream(tmp);
107
+ let received = 0;
108
+ let failed = false;
109
+
110
+ const fail = (err) => {
111
+ if (failed) return;
112
+ failed = true;
113
+ response.destroy();
114
+ file.destroy();
115
+ rmSync(tmp, { force: true });
116
+ pendingTempFiles.delete(tmp);
117
+ reject(err);
118
+ };
119
+
120
+ response.on('data', (chunk) => {
121
+ received += chunk.length;
122
+ });
123
+ response.on('aborted', () => fail(new Error('Download failed: connection closed early')));
124
+ response.on('error', fail);
125
+ file.on('error', fail);
60
126
  file.on('finish', () => {
61
- file.close();
62
- resolve();
127
+ if (failed) return;
128
+ if (Number.isFinite(expected) && expected > 0 && received !== expected) {
129
+ fail(new Error(`Download failed: received ${received} of ${expected} bytes`));
130
+ return;
131
+ }
132
+ file.close((err) => {
133
+ if (err) {
134
+ fail(err);
135
+ return;
136
+ }
137
+ try {
138
+ renameSync(tmp, dest);
139
+ pendingTempFiles.delete(tmp);
140
+ resolve();
141
+ } catch (renameErr) {
142
+ fail(renameErr);
143
+ }
144
+ });
63
145
  });
64
- }).on('error', (err) => {
65
- reject(err);
146
+
147
+ response.pipe(file);
66
148
  });
149
+ request.on('error', reject);
67
150
  });
68
151
  }
69
152
 
70
- function finalizeBinary() {
153
+ // Copy `src` onto `dest` through a temporary file, so a reader never sees a
154
+ // partially written binary (the shared cache is read by concurrent installs).
155
+ function copyAtomic(src, dest) {
156
+ const tmp = tempPathFor(dest);
157
+ try {
158
+ copyFileSync(src, tmp);
159
+ renameSync(tmp, dest);
160
+ } finally {
161
+ rmSync(tmp, { force: true });
162
+ }
163
+ }
164
+
165
+ function makeExecutable(path) {
71
166
  // Make executable (Unix-like systems)
72
167
  if (process.platform !== 'win32') {
73
- chmodSync(BIN_PATH, 0o755);
168
+ chmodSync(path, 0o755);
74
169
  }
75
170
  }
76
171
 
172
+ // A binary is usable when it exists and actually runs: this rejects a
173
+ // truncated or corrupted file (e.g. left by an install from an older version
174
+ // of this script) instead of trusting it forever.
175
+ function isUsableBinary(path) {
176
+ if (!existsSync(path)) {
177
+ return false;
178
+ }
179
+ const result = spawnSync(path, ['--version'], { stdio: 'ignore', timeout: 30000 });
180
+ return !result.error && result.status === 0;
181
+ }
182
+
77
183
  async function install() {
78
184
  try {
79
- // Check if binary already exists (useful for development)
80
- if (existsSync(BIN_PATH)) {
185
+ // Keep a working binary (useful for development and reinstalls).
186
+ if (isUsableBinary(BIN_PATH)) {
81
187
  console.log('✓ vue-go-tsc binary already exists');
82
188
  return;
83
189
  }
190
+ rmSync(BIN_PATH, { force: true });
191
+ removeStaleTempFiles(BIN_DIR, BINARY_NAME);
84
192
 
85
193
  if (!existsSync(BIN_DIR)) {
86
194
  mkdirSync(BIN_DIR, { recursive: true });
@@ -89,14 +197,18 @@ async function install() {
89
197
  const platformBinary = getPlatformBinary();
90
198
  const cachedBinary = join(CACHE_DIR, platformBinary);
91
199
 
92
- // 1) Serve from the shared cache when present (best-effort — a cache miss or
93
- // error just falls through to the download below).
200
+ // 1) Serve from the shared cache when present (best-effort — a cache miss,
201
+ // an error or a corrupted entry just falls through to the download).
94
202
  try {
95
203
  if (existsSync(cachedBinary)) {
96
- copyFileSync(cachedBinary, BIN_PATH);
97
- finalizeBinary();
98
- console.log(`✓ vue-go-tsc restored from cache (${cachedBinary})`);
99
- return;
204
+ copyAtomic(cachedBinary, BIN_PATH);
205
+ makeExecutable(BIN_PATH);
206
+ if (isUsableBinary(BIN_PATH)) {
207
+ console.log(`✓ vue-go-tsc restored from cache (${cachedBinary})`);
208
+ return;
209
+ }
210
+ rmSync(BIN_PATH, { force: true });
211
+ rmSync(cachedBinary, { force: true });
100
212
  }
101
213
  } catch {
102
214
  // ignore — download is the source of truth
@@ -107,14 +219,14 @@ async function install() {
107
219
  console.log('Installing vue-go-tsc...');
108
220
  console.log(`Downloading from: ${downloadUrl}`);
109
221
  await download(downloadUrl, BIN_PATH);
110
- finalizeBinary();
222
+ makeExecutable(BIN_PATH);
111
223
 
112
224
  // 3) Populate the cache for next time (never fail the install on this).
113
225
  try {
114
226
  if (!existsSync(CACHE_DIR)) {
115
227
  mkdirSync(CACHE_DIR, { recursive: true });
116
228
  }
117
- copyFileSync(BIN_PATH, cachedBinary);
229
+ copyAtomic(BIN_PATH, cachedBinary);
118
230
  } catch {
119
231
  // cache is an optimization only
120
232
  }
@@ -131,4 +243,15 @@ async function install() {
131
243
  }
132
244
  }
133
245
 
134
- install();
246
+ if (require.main === module) {
247
+ // An interrupted install must not leave a partial download behind.
248
+ for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) {
249
+ process.on(signal, () => {
250
+ cleanupPendingTempFiles();
251
+ process.exit(128 + os.constants.signals[signal]);
252
+ });
253
+ }
254
+ install();
255
+ }
256
+
257
+ module.exports = { download, copyAtomic, isUsableBinary };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-go-tsc",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Blazingly fast Vue 3 type checker powered by typescript-go — a Go-native alternative to vue-tsc",
5
5
  "keywords": [
6
6
  "vue",