agent-notify 0.2.6 → 0.3.0

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/lib/download.js CHANGED
@@ -9,7 +9,9 @@ function downloadToFile(url, destinationPath, client = https) {
9
9
  return new Promise((resolve, reject) => {
10
10
  const file = fs.createWriteStream(destinationPath);
11
11
 
12
- const request = client.get(url, (response) => {
12
+ const request = client.get(url, {
13
+ timeout: 300000, // 300 seconds
14
+ }, (response) => {
13
15
  if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
14
16
  const redirectUrl = new URL(response.headers.location, url).toString();
15
17
  file.close(() => {
@@ -38,6 +40,15 @@ function downloadToFile(url, destinationPath, client = https) {
38
40
  removeFileIfExists(destinationPath, () => reject(err));
39
41
  });
40
42
  });
43
+
44
+ request.on('timeout', () => {
45
+ request.destroy();
46
+ file.close(() => {
47
+ removeFileIfExists(destinationPath, () => {
48
+ reject(new Error(`download timeout after 300s: ${url}`));
49
+ });
50
+ });
51
+ });
41
52
  });
42
53
  }
43
54
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-notify",
3
- "version": "0.2.6",
3
+ "version": "0.3.0",
4
4
  "description": "NPX launcher for the agent-notify binary",
5
5
  "bin": {
6
6
  "agent-notify": "./bin/agent-notify.js"
@@ -76,7 +76,7 @@ test('follows relative redirects when downloading', async (t) => {
76
76
  const destinationPath = path.join(root, 'archive.tar.gz');
77
77
 
78
78
  const client = {
79
- get(url, handler) {
79
+ get(url, options, handler) {
80
80
  const request = new PassThrough();
81
81
  process.nextTick(() => {
82
82
  if (url === 'https://example.com/releases/archive.tar.gz') {
@@ -109,7 +109,7 @@ test('removes destination file when download fails with non-200 response', async
109
109
  const destinationPath = path.join(root, 'archive.tar.gz');
110
110
 
111
111
  const client = {
112
- get(_url, handler) {
112
+ get(_url, _options, handler) {
113
113
  const request = new PassThrough();
114
114
  process.nextTick(() => {
115
115
  const response = new PassThrough();
@@ -128,3 +128,25 @@ test('removes destination file when download fails with non-200 response', async
128
128
  );
129
129
  assert.equal(fs.existsSync(destinationPath), false);
130
130
  });
131
+
132
+ test('rejects with timeout error when download takes too long', async (t) => {
133
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-notify-download-'));
134
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
135
+ const destinationPath = path.join(root, 'archive.tar.gz');
136
+
137
+ const client = {
138
+ get(_url, _options, handler) {
139
+ const request = new PassThrough();
140
+ process.nextTick(() => {
141
+ request.emit('timeout');
142
+ });
143
+ return request;
144
+ },
145
+ };
146
+
147
+ await assert.rejects(
148
+ downloadToFile('https://example.com/releases/archive.tar.gz', destinationPath, client),
149
+ /download timeout after 300s/,
150
+ );
151
+ assert.equal(fs.existsSync(destinationPath), false);
152
+ });