@vkontakte/vk-miniapps-deploy 1.0.2 → 1.0.3

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.
@@ -7,7 +7,7 @@ runs:
7
7
  - name: Setup NodeJS
8
8
  uses: actions/setup-node@v4
9
9
  with:
10
- node-version: 20
10
+ node-version: 22
11
11
  cache: 'yarn'
12
12
  always-auth: true
13
13
  registry-url: 'https://registry.npmjs.org'
package/index.js CHANGED
@@ -66,6 +66,8 @@ const URL_NAMES_MAP = {
66
66
  [URL_NAMES.MOBILE_WEB]: PLATFORMS.MOBILE_WEB,
67
67
  };
68
68
 
69
+ const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
70
+
69
71
  function getTraceId() {
70
72
  return crypto.randomBytes(4).toString('hex');
71
73
  }
@@ -78,33 +80,70 @@ function getProxyAgent() {
78
80
  /**
79
81
  * @param {nodeFetch.RequestInfo} url
80
82
  * @param {nodeFetch.RequestInit} options
83
+ * @param {number} retries
84
+ * @param {number} backoff
81
85
  * @returns {Promise<nodeFetch.Response>}
82
86
  */
83
- async function fetch(url, options) {
87
+ async function fetch(url, options, retries = 3, backoff = 1000) {
84
88
  const fetchOptions = { ...options, agent: getProxyAgent() || options?.agent };
85
89
 
86
- if (!DEBUG_MODE) {
87
- return nodeFetch(url, fetchOptions);
88
- }
90
+ for (let attempt = 1; attempt <= retries; attempt++) {
91
+ try {
92
+ if (DEBUG_MODE) {
93
+ const traceId = chalk.hex(`#${((Math.random() * 0xffffff) << 0).toString(16)}`)(
94
+ getTraceId(),
95
+ );
96
+ const logLine = (line, type) => console.log(`[${traceId}][${type}]`, chalk.cyan(line));
89
97
 
90
- const traceId = chalk.hex(`#${((Math.random() * 0xffffff) << 0).toString(16)}`)(getTraceId());
91
- const logLine = (line, type) => console.log(`[${traceId}][${type}]`, chalk.cyan(line));
92
- const logError = (error) => console.error(`[${traceId}][ERROR]`, chalk.red(error));
98
+ logLine(url, 'REQ');
99
+ options && logLine(JSON.stringify(fetchOptions), 'REQ');
93
100
 
94
- try {
95
- logLine(url, 'REQ');
96
- options && logLine(JSON.stringify(fetchOptions), 'REQ');
101
+ const response = await nodeFetch(url, fetchOptions);
102
+
103
+ if (!response.ok) {
104
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
105
+ }
106
+
107
+ const body = await response.clone().text();
97
108
 
98
- const response = await nodeFetch(url, fetchOptions);
99
- const body = await response.clone().text();
109
+ if (body.trim().startsWith('<!DOCTYPE') || body.trim().startsWith('<html')) {
110
+ throw new Error('Server returned HTML page instead of expected response');
111
+ }
100
112
 
101
- logLine(`${response.status} ${response.statusText}`, 'RESP');
102
- logLine(body, 'RESP');
113
+ logLine(`${response.status} ${response.statusText}`, 'RESP');
114
+ logLine(body, 'RESP');
103
115
 
104
- return response;
105
- } catch (e) {
106
- logError(e);
107
- throw e;
116
+ return response;
117
+ }
118
+
119
+ const response = await nodeFetch(url, fetchOptions);
120
+
121
+ if (!response.ok) {
122
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
123
+ }
124
+
125
+ const clonedResponse = response.clone();
126
+ const bodyText = await clonedResponse.text();
127
+ if (bodyText.trim().startsWith('<!DOCTYPE') || bodyText.trim().startsWith('<html')) {
128
+ throw new Error('Server returned HTML page instead of expected response');
129
+ }
130
+
131
+ return response;
132
+ } catch (e) {
133
+ if (attempt < retries) {
134
+ console.warn(
135
+ chalk.yellow(
136
+ `[RETRY ${attempt}/${retries - 1}] Request failed (${
137
+ e.message
138
+ }). Retrying in ${backoff}ms...`,
139
+ ),
140
+ );
141
+ await delay(backoff);
142
+ } else {
143
+ console.error(chalk.red(`[FETCH FAILED] Request failed after ${retries} attempts.`));
144
+ throw e;
145
+ }
146
+ }
108
147
  }
109
148
  }
110
149
 
@@ -180,57 +219,49 @@ async function api(method, params) {
180
219
  return k + '=' + encodeURIComponent(params[k]);
181
220
  })
182
221
  .join('&');
183
- try {
184
- const query = await fetch(API_HOST + method + '?' + queryParams);
185
- const res = await query.json();
186
- if (res.error !== void 0) {
187
- const errorCode = res.error.error_code;
188
- const errorMessage = chalk.red(errorCode + ': ' + res.error.error_msg);
189
- if (errorCode === 5 && !process.env.MINI_APPS_ACCESS_TOKEN && !cfg.noprompt) {
190
- console.error(errorMessage);
191
-
192
- const questions = [
193
- {
194
- type: 'confirm',
195
- initial: true,
196
- name: 'updateToken',
197
- message: chalk.yellow('Would you like to try to retrieve a new token?'),
198
- },
199
- ];
200
-
201
- const { updateToken } = await prompt(questions);
202
- if (updateToken) {
203
- await retrieveAndSaveAccessToken(cfg);
204
- return api(method, params);
205
- } else {
206
- throw new Error(errorMessage);
207
- }
222
+ const query = await fetch(API_HOST + method + '?' + queryParams);
223
+ const res = await query.json();
224
+ if (res.error !== void 0) {
225
+ const errorCode = res.error.error_code;
226
+ const errorMessage = chalk.red(errorCode + ': ' + res.error.error_msg);
227
+ if (errorCode === 5 && !process.env.MINI_APPS_ACCESS_TOKEN && !cfg.noprompt) {
228
+ console.error(errorMessage);
229
+
230
+ const questions = [
231
+ {
232
+ type: 'confirm',
233
+ initial: true,
234
+ name: 'updateToken',
235
+ message: chalk.yellow('Would you like to try to retrieve a new token?'),
236
+ },
237
+ ];
238
+
239
+ const { updateToken } = await prompt(questions);
240
+ if (updateToken) {
241
+ await retrieveAndSaveAccessToken(cfg);
242
+ return api(method, params);
208
243
  } else {
209
244
  throw new Error(errorMessage);
210
245
  }
246
+ } else {
247
+ throw new Error(errorMessage);
211
248
  }
249
+ }
212
250
 
213
- if (res.response !== void 0) {
214
- return res.response;
215
- }
216
- } catch (e) {
217
- console.error(e);
251
+ if (res.response !== void 0) {
252
+ return res.response;
218
253
  }
219
254
  }
220
255
 
221
256
  async function upload(uploadUrl, bundleFile) {
222
257
  const formData = new FormData();
223
258
  formData.append('file', fs.createReadStream(bundleFile), { contentType: 'application/zip' });
224
- try {
225
- const upload = await fetch(uploadUrl, {
226
- method: 'POST',
227
- headers: formData.getHeaders(),
228
- body: formData,
229
- });
230
- return await upload.json();
231
- } catch (e) {
232
- console.error('upload error', e);
233
- }
259
+ const upload = await fetch(uploadUrl, {
260
+ method: 'POST',
261
+ headers: formData.getHeaders(),
262
+ body: formData,
263
+ });
264
+ return await upload.json();
234
265
  }
235
266
 
236
267
  async function handleQueue(user_id, base_url, key, ts, version, handled, cfg) {
@@ -479,7 +510,7 @@ async function run(cfg) {
479
510
  const uploadServer = await api('apps.getGoHostingUploadServer', params);
480
511
 
481
512
  if (!uploadServer || !uploadServer.upload_url) {
482
- throw new Error(JSON.stringify('upload_url is undefined', r));
513
+ throw new Error(`upload_url is undefined: ${JSON.stringify(uploadServer)}`);
483
514
  }
484
515
 
485
516
  const uploadURL = uploadServer.upload_url;
@@ -508,7 +539,7 @@ async function run(cfg) {
508
539
  console.log('Uploaded version ' + taskData.version + '!');
509
540
  return getQueue(taskData.version, cfg);
510
541
  } else {
511
- console.error('Upload error:', r);
542
+ console.error('Upload error: missing task version', taskData);
512
543
  process.exit(1);
513
544
  }
514
545
  } catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vkontakte/vk-miniapps-deploy",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Deploy to VK Mini Apps hosting with one simple command",
5
5
  "main": "index.js",
6
6
  "scripts": {