@anmiles/downloader 2.0.0 → 2.0.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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.0.1](../../tags/v1.0.1) - 2023-05-04
9
+ ### Changed
10
+ - Use `event-emitter` to mock subscriptions on request/response
11
+
8
12
  ## [1.0.0](../../tags/v1.0.0) - 2023-05-03
9
13
  ### Changed
10
14
  - Ready to release
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anmiles/downloader",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Wrapper for downloading data as string, buffer or complex types",
5
5
  "keywords": [
6
6
  "download",
@@ -27,10 +27,10 @@
27
27
  "test:report:coverage": "nyc report --nycrc-path ./coverage.config.js -t ./coverage --report-dir ./coverage"
28
28
  },
29
29
  "dependencies": {
30
- "colorette": "^2.0.19",
31
30
  "iconv-lite": "^0.6.3"
32
31
  },
33
32
  "devDependencies": {
33
+ "@types/event-emitter": "^0.3.3",
34
34
  "@types/jest": "^29.5.1",
35
35
  "@typescript-eslint/eslint-plugin": "^5.59.2",
36
36
  "@typescript-eslint/parser": "^5.59.2",
@@ -38,6 +38,7 @@
38
38
  "eslint-plugin-align-assignments": "^1.1.2",
39
39
  "eslint-plugin-import": "^2.27.5",
40
40
  "eslint-plugin-jest": "^27.2.1",
41
+ "event-emitter": "^0.3.5",
41
42
  "jest": "^29.5.0",
42
43
  "nyc": "^15.1.0",
43
44
  "rimraf": "^5.0.0",
@@ -1,6 +1,7 @@
1
1
  import fs from 'fs';
2
2
  import http from 'http';
3
3
  import https from 'https';
4
+ import emitter from 'event-emitter';
4
5
  import iconv from 'iconv-lite';
5
6
 
6
7
  import downloader from '../downloader';
@@ -11,20 +12,10 @@ jest.mock<Partial<typeof downloader>>('../downloader', () => ({
11
12
  downloadString : jest.fn().mockImplementation((...args: Parameters<typeof original.downloadString>) => original.downloadString(...args)),
12
13
  }));
13
14
 
14
- const request = {} as http.ClientRequest;
15
- request.on = jest.fn().mockImplementation((ev: string, listener: () => void) => {
16
- switch (ev) {
17
- case 'error':
18
- requestError = listener;
19
- break;
20
- }
21
- });
22
-
23
- const response = {} as http.IncomingMessage;
24
-
25
- let data: (data: Uint8Array) => void = () => {};
26
- let end: () => void = () => {};
27
- let requestError: (e: Error) => void = () => {};
15
+ const request = emitter() as http.ClientRequest;
16
+ const response = emitter() as http.IncomingMessage;
17
+ response.pipe = jest.fn();
18
+ response.resume = jest.fn();
28
19
 
29
20
  function get(url: string | URL, options: https.RequestOptions, callback?: ((res: http.IncomingMessage) => void) | undefined): http.ClientRequest {
30
21
  if (callback) {
@@ -36,18 +27,6 @@ function get(url: string | URL, options: https.RequestOptions, callback?: ((res:
36
27
 
37
28
  beforeEach(() => {
38
29
  response.statusCode = 200;
39
- response.on = jest.fn().mockImplementation((ev: string, listener: () => void) => {
40
- switch (ev) {
41
- case 'data':
42
- data = listener;
43
- break;
44
- case 'end':
45
- end = listener;
46
- break;
47
- }
48
- });
49
- response.pipe = jest.fn();
50
- response.resume = jest.fn();
51
30
  });
52
31
 
53
32
  let httpGetSpy: jest.SpyInstance;
@@ -76,21 +55,21 @@ describe('src/lib/downloader', () => {
76
55
 
77
56
  it('should call http.get if url protocol is http', async () => {
78
57
  const promise = original.download('http://url');
79
- end();
58
+ response.emit('end');
80
59
  await promise;
81
60
  expect(httpGetSpy.mock.calls[0][0]).toEqual('http://url');
82
61
  });
83
62
 
84
63
  it('should call https.get if url protocol is https', async () => {
85
64
  const promise = original.download('https://url');
86
- end();
65
+ response.emit('end');
87
66
  await promise;
88
67
  expect(httpsGetSpy.mock.calls[0][0]).toEqual('https://url');
89
68
  });
90
69
 
91
70
  it('should pass user-agent in options', async () => {
92
71
  const promise = original.download('http://url');
93
- end();
72
+ response.emit('end');
94
73
  await promise;
95
74
  expect(httpGetSpy.mock.calls[0][1]).toEqual(expect.objectContaining({
96
75
  headers : {
@@ -107,16 +86,16 @@ describe('src/lib/downloader', () => {
107
86
 
108
87
  it('should reject if response errored', async () => {
109
88
  const promise = original.download('http://url');
110
- requestError(new Error('request error'));
89
+ request.emit('error', new Error('request error'));
111
90
  await expect(() => promise).rejects.toEqual('Request to http://url failed with error: request error');
112
91
  });
113
92
 
114
93
  it('should concat and resolve received data if no file specified', async () => {
115
94
  const promise = original.download('http://url');
116
- data(new Uint8Array([ 10, 11, 12 ]));
117
- data(new Uint8Array([ 20, 21, 22 ]));
118
- data(new Uint8Array([ 30, 31, 32 ]));
119
- end();
95
+ response.emit('data', new Uint8Array([ 10, 11, 12 ]));
96
+ response.emit('data', new Uint8Array([ 20, 21, 22 ]));
97
+ response.emit('data', new Uint8Array([ 30, 31, 32 ]));
98
+ response.emit('end');
120
99
  const result = await promise;
121
100
  expect(result).toEqual(Buffer.from([ 10, 11, 12, 20, 21, 22, 30, 31, 32 ]));
122
101
  });
@@ -125,10 +104,10 @@ describe('src/lib/downloader', () => {
125
104
  const stream = {} as fs.WriteStream;
126
105
  const createWriteStreamSpy = jest.spyOn(fs, 'createWriteStream').mockReturnValue(stream);
127
106
  const promise = original.download('http://url', 'file');
128
- data(new Uint8Array([ 10, 11, 12 ]));
129
- data(new Uint8Array([ 20, 21, 22 ]));
130
- data(new Uint8Array([ 30, 31, 32 ]));
131
- end();
107
+ response.emit('data', new Uint8Array([ 10, 11, 12 ]));
108
+ response.emit('data', new Uint8Array([ 20, 21, 22 ]));
109
+ response.emit('data', new Uint8Array([ 30, 31, 32 ]));
110
+ response.emit('end');
132
111
  const result = await promise;
133
112
  expect(createWriteStreamSpy).toHaveBeenCalledWith('file');
134
113
  expect(response.pipe).toHaveBeenCalledWith(stream);