alurkerja-cli 1.0.3 → 1.0.5

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/index.js CHANGED
@@ -56,8 +56,10 @@ function getVersion() {
56
56
  */
57
57
  async function downloadBinary(version, platform) {
58
58
  const binaryName = `alurkerja-${platform.os}-${platform.arch}${platform.os === 'windows' ? '.exe' : ''}`;
59
- // Use GitLab raw file download with correct parameters
60
- const downloadUrl = `${GITLAB_BASE}/-/raw/master/dist/${binaryName}?ref_type=heads&inline=false`;
59
+ // Use GitLab API v4 for raw file download to get actual binary
60
+ const encodedPath = encodeURIComponent(`dist/${binaryName}`);
61
+ const encodedProject = encodeURIComponent('alurkerja/on-premises/toolkits/alurkerja-cli');
62
+ const downloadUrl = `https://gitlab.javan.co.id/api/v4/projects/${encodedProject}/repository/files/${encodedPath}/raw?ref=master`;
61
63
 
62
64
  console.log(`📥 Downloading Alurkerja CLI ${version}...`);
63
65
 
@@ -69,54 +71,59 @@ async function downloadBinary(version, platform) {
69
71
 
70
72
  return new Promise((resolve, reject) => {
71
73
  const file = fs.createWriteStream(tempPath);
74
+ let downloadComplete = false;
72
75
 
73
- const url = new URL(downloadUrl);
74
- const options = {
75
- hostname: url.hostname,
76
- path: url.pathname + url.search,
77
- headers: {
78
- 'PRIVATE-TOKEN': GITLAB_TOKEN
79
- }
80
- };
81
-
82
- const req = https.get(options, (res) => {
83
- if (res.statusCode === 302 || res.statusCode === 301) {
84
- // Follow redirect with token
85
- const redirectUrl = new URL(res.headers.location);
86
- const redirectOptions = {
87
- hostname: redirectUrl.hostname,
88
- path: redirectUrl.pathname + redirectUrl.search,
89
- headers: {
90
- 'PRIVATE-TOKEN': GITLAB_TOKEN
91
- }
92
- };
93
- return https.get(redirectOptions, (redirectRes) => {
94
- redirectRes.pipe(file);
76
+ const downloadFile = (targetUrl) => {
77
+ const url = new URL(targetUrl);
78
+ const options = {
79
+ hostname: url.hostname,
80
+ path: url.pathname + url.search,
81
+ headers: {
82
+ 'PRIVATE-TOKEN': GITLAB_TOKEN
83
+ }
84
+ };
85
+
86
+ const req = https.get(options, (res) => {
87
+ if (res.statusCode === 302 || res.statusCode === 301) {
88
+ // Follow redirect
89
+ const redirectUrl = res.headers.location.startsWith('http')
90
+ ? res.headers.location
91
+ : `https://${url.hostname}${res.headers.location}`;
92
+ return downloadFile(redirectUrl);
93
+ }
94
+
95
+ if (res.statusCode !== 200) {
96
+ file.close();
97
+ try { fs.unlinkSync(tempPath); } catch {}
98
+ reject(new Error(`Download failed with status ${res.statusCode}`));
99
+ return;
100
+ }
101
+
102
+ res.pipe(file);
103
+ res.on('end', () => {
104
+ downloadComplete = true;
95
105
  });
96
- }
106
+ });
97
107
 
98
- if (res.statusCode !== 200) {
99
- file.close();
100
- fs.unlinkSync(tempPath);
101
- reject(new Error(`Download failed with status ${res.statusCode}`));
102
- return;
103
- }
108
+ req.on('error', (err) => {
109
+ if (!downloadComplete) {
110
+ file.close();
111
+ try { fs.unlinkSync(tempPath); } catch {}
112
+ reject(err);
113
+ }
114
+ });
104
115
 
105
- res.pipe(file);
106
- });
107
-
108
- req.on('error', (err) => {
109
- file.close();
110
- try { fs.unlinkSync(tempPath); } catch {}
111
- reject(err);
112
- });
116
+ req.setTimeout(30000, () => {
117
+ if (!downloadComplete) {
118
+ req.abort();
119
+ file.close();
120
+ try { fs.unlinkSync(tempPath); } catch {}
121
+ reject(new Error('Download timeout'));
122
+ }
123
+ });
124
+ };
113
125
 
114
- req.setTimeout(30000, () => {
115
- req.abort();
116
- file.close();
117
- try { fs.unlinkSync(tempPath); } catch {}
118
- reject(new Error('Download timeout'));
119
- });
126
+ downloadFile(downloadUrl);
120
127
 
121
128
  file.on('error', (err) => {
122
129
  try { fs.unlinkSync(tempPath); } catch {}
@@ -131,7 +138,15 @@ async function downloadBinary(version, platform) {
131
138
  return;
132
139
  }
133
140
 
134
- // Make executable on Unix systems before renaming
141
+ // Validate the downloaded file
142
+ const stats = fs.statSync(tempPath);
143
+ if (stats.size < 1000) {
144
+ try { fs.unlinkSync(tempPath); } catch {}
145
+ reject(new Error(`Download failed: received invalid file (${stats.size} bytes)`));
146
+ return;
147
+ }
148
+
149
+ // Make executable on Unix systems
135
150
  if (platform.os !== 'windows') {
136
151
  try {
137
152
  fs.chmodSync(tempPath, '755');
@@ -142,19 +157,8 @@ async function downloadBinary(version, platform) {
142
157
  }
143
158
  }
144
159
 
145
- // Validate the downloaded file
146
- const stats = fs.statSync(tempPath);
147
- if (stats.size < 1000) {
148
- // File too small, likely an error page
149
- const content = fs.readFileSync(tempPath, 'utf8');
150
- try { fs.unlinkSync(tempPath); } catch {}
151
- reject(new Error(`Download failed: received invalid file (${stats.size} bytes)`));
152
- return;
153
- }
154
-
155
160
  // Rename temp file to final path
156
161
  try {
157
- // Remove old binary if exists
158
162
  if (fs.existsSync(BINARY_PATH)) {
159
163
  fs.unlinkSync(BINARY_PATH);
160
164
  }
package/index.js.backup CHANGED
@@ -56,8 +56,10 @@ function getVersion() {
56
56
  */
57
57
  async function downloadBinary(version, platform) {
58
58
  const binaryName = `alurkerja-${platform.os}-${platform.arch}${platform.os === 'windows' ? '.exe' : ''}`;
59
- // Use GitLab raw file download with correct parameters
60
- const downloadUrl = `${GITLAB_BASE}/-/raw/master/dist/${binaryName}?ref_type=heads&inline=false`;
59
+ // Use GitLab API v4 for raw file download to get actual binary
60
+ const encodedPath = encodeURIComponent(`dist/${binaryName}`);
61
+ const encodedProject = encodeURIComponent('alurkerja/on-premises/toolkits/alurkerja-cli');
62
+ const downloadUrl = `https://gitlab.javan.co.id/api/v4/projects/${encodedProject}/repository/files/${encodedPath}/raw?ref=master`;
61
63
 
62
64
  console.log(`📥 Downloading Alurkerja CLI ${version}...`);
63
65
 
@@ -69,54 +71,59 @@ async function downloadBinary(version, platform) {
69
71
 
70
72
  return new Promise((resolve, reject) => {
71
73
  const file = fs.createWriteStream(tempPath);
74
+ let downloadComplete = false;
72
75
 
73
- const url = new URL(downloadUrl);
74
- const options = {
75
- hostname: url.hostname,
76
- path: url.pathname + url.search,
77
- headers: {
78
- 'PRIVATE-TOKEN': GITLAB_TOKEN
79
- }
80
- };
81
-
82
- const req = https.get(options, (res) => {
83
- if (res.statusCode === 302 || res.statusCode === 301) {
84
- // Follow redirect with token
85
- const redirectUrl = new URL(res.headers.location);
86
- const redirectOptions = {
87
- hostname: redirectUrl.hostname,
88
- path: redirectUrl.pathname + redirectUrl.search,
89
- headers: {
90
- 'PRIVATE-TOKEN': GITLAB_TOKEN
91
- }
92
- };
93
- return https.get(redirectOptions, (redirectRes) => {
94
- redirectRes.pipe(file);
76
+ const downloadFile = (targetUrl) => {
77
+ const url = new URL(targetUrl);
78
+ const options = {
79
+ hostname: url.hostname,
80
+ path: url.pathname + url.search,
81
+ headers: {
82
+ 'PRIVATE-TOKEN': GITLAB_TOKEN
83
+ }
84
+ };
85
+
86
+ const req = https.get(options, (res) => {
87
+ if (res.statusCode === 302 || res.statusCode === 301) {
88
+ // Follow redirect
89
+ const redirectUrl = res.headers.location.startsWith('http')
90
+ ? res.headers.location
91
+ : `https://${url.hostname}${res.headers.location}`;
92
+ return downloadFile(redirectUrl);
93
+ }
94
+
95
+ if (res.statusCode !== 200) {
96
+ file.close();
97
+ try { fs.unlinkSync(tempPath); } catch {}
98
+ reject(new Error(`Download failed with status ${res.statusCode}`));
99
+ return;
100
+ }
101
+
102
+ res.pipe(file);
103
+ res.on('end', () => {
104
+ downloadComplete = true;
95
105
  });
96
- }
106
+ });
97
107
 
98
- if (res.statusCode !== 200) {
99
- file.close();
100
- fs.unlinkSync(tempPath);
101
- reject(new Error(`Download failed with status ${res.statusCode}`));
102
- return;
103
- }
108
+ req.on('error', (err) => {
109
+ if (!downloadComplete) {
110
+ file.close();
111
+ try { fs.unlinkSync(tempPath); } catch {}
112
+ reject(err);
113
+ }
114
+ });
104
115
 
105
- res.pipe(file);
106
- });
107
-
108
- req.on('error', (err) => {
109
- file.close();
110
- try { fs.unlinkSync(tempPath); } catch {}
111
- reject(err);
112
- });
116
+ req.setTimeout(30000, () => {
117
+ if (!downloadComplete) {
118
+ req.abort();
119
+ file.close();
120
+ try { fs.unlinkSync(tempPath); } catch {}
121
+ reject(new Error('Download timeout'));
122
+ }
123
+ });
124
+ };
113
125
 
114
- req.setTimeout(30000, () => {
115
- req.abort();
116
- file.close();
117
- try { fs.unlinkSync(tempPath); } catch {}
118
- reject(new Error('Download timeout'));
119
- });
126
+ downloadFile(downloadUrl);
120
127
 
121
128
  file.on('error', (err) => {
122
129
  try { fs.unlinkSync(tempPath); } catch {}
@@ -131,7 +138,15 @@ async function downloadBinary(version, platform) {
131
138
  return;
132
139
  }
133
140
 
134
- // Make executable on Unix systems before renaming
141
+ // Validate the downloaded file
142
+ const stats = fs.statSync(tempPath);
143
+ if (stats.size < 1000) {
144
+ try { fs.unlinkSync(tempPath); } catch {}
145
+ reject(new Error(`Download failed: received invalid file (${stats.size} bytes)`));
146
+ return;
147
+ }
148
+
149
+ // Make executable on Unix systems
135
150
  if (platform.os !== 'windows') {
136
151
  try {
137
152
  fs.chmodSync(tempPath, '755');
@@ -142,19 +157,8 @@ async function downloadBinary(version, platform) {
142
157
  }
143
158
  }
144
159
 
145
- // Validate the downloaded file
146
- const stats = fs.statSync(tempPath);
147
- if (stats.size < 1000) {
148
- // File too small, likely an error page
149
- const content = fs.readFileSync(tempPath, 'utf8');
150
- try { fs.unlinkSync(tempPath); } catch {}
151
- reject(new Error(`Download failed: received invalid file (${stats.size} bytes)`));
152
- return;
153
- }
154
-
155
160
  // Rename temp file to final path
156
161
  try {
157
- // Remove old binary if exists
158
162
  if (fs.existsSync(BINARY_PATH)) {
159
163
  fs.unlinkSync(BINARY_PATH);
160
164
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alurkerja-cli",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Alurkerja CLI - npx wrapper for Go binary",
5
5
  "main": "index.js",
6
6
  "private": false,
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alurkerja-cli",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Alurkerja CLI - npx wrapper for Go binary",
5
5
  "main": "index.js",
6
6
  "private": false,