@netlify/edge-bundler 1.14.0 → 1.14.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.
@@ -1,3 +1,3 @@
1
1
  import { Logger } from './logger.js';
2
- declare const download: (targetDirectory: string, versionRange: string, logger: Logger) => Promise<string>;
3
- export { download };
2
+ declare const downloadWithRetry: (targetDirectory: string, versionRange: string, logger: Logger) => Promise<string>;
3
+ export { downloadWithRetry as download };
@@ -1,51 +1,59 @@
1
- import fs from 'fs';
1
+ import { createWriteStream, promises as fs } from 'fs';
2
2
  import path from 'path';
3
+ import { promisify } from 'util';
3
4
  import fetch from 'node-fetch';
4
5
  import StreamZip from 'node-stream-zip';
5
6
  import pRetry from 'p-retry';
6
7
  import semver from 'semver';
7
8
  import { getBinaryExtension, getPlatformTarget } from './platform.js';
8
- const download = async (targetDirectory, versionRange, logger) => {
9
+ const downloadWithRetry = async (targetDirectory, versionRange, logger) => await pRetry(async () => await download(targetDirectory, versionRange), {
10
+ retries: 3,
11
+ onFailedAttempt: (error) => {
12
+ logger.system('Deno download with retry failed', error);
13
+ },
14
+ });
15
+ const download = async (targetDirectory, versionRange) => {
9
16
  const zipPath = path.join(targetDirectory, 'deno-cli-latest.zip');
10
- const data = await downloadVersionWithRetry(versionRange, logger);
17
+ const data = await downloadVersion(versionRange);
11
18
  const binaryName = `deno${getBinaryExtension()}`;
12
19
  const binaryPath = path.join(targetDirectory, binaryName);
13
- const file = fs.createWriteStream(zipPath);
14
- await new Promise((resolve, reject) => {
15
- data.pipe(file);
16
- data.on('error', reject);
17
- file.on('finish', resolve);
18
- });
19
- await extractBinaryFromZip(zipPath, binaryPath, binaryName);
20
+ const file = createWriteStream(zipPath);
20
21
  try {
21
- await fs.promises.unlink(zipPath);
22
+ await new Promise((resolve, reject) => {
23
+ data.pipe(file);
24
+ data.on('error', reject);
25
+ file.on('finish', resolve);
26
+ });
27
+ await extractBinaryFromZip(zipPath, binaryPath, binaryName);
28
+ return binaryPath;
22
29
  }
23
- catch {
24
- // no-op
30
+ finally {
31
+ // Try closing and deleting the zip file in any case, error or not
32
+ await promisify(file.close.bind(file))();
33
+ try {
34
+ await fs.unlink(zipPath);
35
+ }
36
+ catch {
37
+ // no-op
38
+ }
25
39
  }
26
- return binaryPath;
27
40
  };
28
41
  const downloadVersion = async (versionRange) => {
29
42
  const version = await getLatestVersionForRange(versionRange);
30
43
  const url = getReleaseURL(version);
31
44
  const res = await fetch(url);
32
- if (res.body === null) {
33
- throw new Error('Could not download Deno');
45
+ // eslint-disable-next-line no-magic-numbers
46
+ if (res.body === null || res.status < 200 || res.status > 299) {
47
+ throw new Error(`Download failed with status code ${res.status}`);
34
48
  }
35
49
  return res.body;
36
50
  };
37
- const downloadVersionWithRetry = async (versionRange, logger) => await pRetry(async () => await downloadVersion(versionRange), {
38
- retries: 3,
39
- onFailedAttempt: (error) => {
40
- logger.system('Deno CLI download retry attempt error', error);
41
- },
42
- });
43
51
  const extractBinaryFromZip = async (zipPath, binaryPath, binaryName) => {
44
52
  const { async: StreamZipAsync } = StreamZip;
45
53
  const zip = new StreamZipAsync({ file: zipPath });
46
54
  await zip.extract(binaryName, binaryPath);
47
55
  await zip.close();
48
- await fs.promises.chmod(binaryPath, '755');
56
+ await fs.chmod(binaryPath, '755');
49
57
  };
50
58
  const getLatestVersion = async () => {
51
59
  try {
@@ -82,4 +90,4 @@ const getReleaseURL = (version) => {
82
90
  const target = getPlatformTarget();
83
91
  return `https://dl.deno.land/release/v${version}/deno-${target}.zip`;
84
92
  };
85
- export { download };
93
+ export { downloadWithRetry as download };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "1.14.0",
3
+ "version": "1.14.1",
4
4
  "description": "Intelligently prepare Netlify Edge Functions for deployment",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",