Skip to content

Commit a54551d

Browse files
committed
Use node-fetch for fetchJSONFromURL
1 parent f6f7847 commit a54551d

File tree

3 files changed

+24
-56
lines changed

3 files changed

+24
-56
lines changed

__tests__/downloader.test.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {ClientRequest, IncomingMessage} from 'http'
2-
import https from 'https'
3-
import {mocked} from 'ts-jest/utils'
41
import {get} from '../src/downloader'
2+
import {mocked} from 'ts-jest/utils'
3+
import fetch from 'node-fetch'
54

65
const buildIdResponse = {
76
count: 1,
@@ -43,38 +42,14 @@ const buildIdResponse = {
4342
]
4443
}
4544

46-
jest.mock('https')
47-
48-
declare type Callback = (data?: object) => void
45+
jest.mock('node-fetch')
46+
const {Response} = jest.requireActual('node-fetch')
4947

5048
test('can obtain build ID', async () => {
51-
mocked(https.request).mockImplementation(
52-
(
53-
_url,
54-
_options,
55-
callback: ((res: IncomingMessage) => void) | undefined
56-
): ClientRequest => {
57-
const res = {
58-
statusCode: 200,
59-
on: (eventType: string, eventCallback: Callback): object => {
60-
switch (eventType) {
61-
case 'data':
62-
eventCallback(Buffer.from(JSON.stringify(buildIdResponse)))
63-
break
64-
case 'end':
65-
eventCallback()
66-
break
67-
}
68-
return res
69-
}
70-
} as IncomingMessage
71-
expect(callback).not.toBeUndefined()
72-
callback && callback(res)
73-
const req = {} as ClientRequest
74-
Object.assign(req, {on: () => {}})
75-
return req
76-
}
49+
mocked(fetch).mockReturnValue(
50+
Promise.resolve(new Response(JSON.stringify(buildIdResponse)))
7751
)
7852
const {id} = await get('minimal', 'x86_64')
53+
expect(fetch).toHaveBeenCalledTimes(1)
7954
expect(id).toEqual('git-sdk-64-minimal-71000')
8055
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dependencies": {
3030
"@actions/cache": "^1.0.6",
3131
"@actions/core": "^1.2.6",
32+
"node-fetch": "^2.6.1",
3233
"unzipper": "^0.10.11"
3334
},
3435
"devDependencies": {

src/downloader.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,28 @@ import {Readable} from 'stream'
44
import unzipper from 'unzipper'
55
import {spawn} from 'child_process'
66
import {delimiter} from 'path'
7+
import fetch from 'node-fetch'
78

89
const gitForWindowsUsrBinPath = 'C:/Program Files/Git/usr/bin'
910
const gitForWindowsMINGW64BinPath = 'C:/Program Files/Git/mingw64/bin'
1011

1112
async function fetchJSONFromURL<T>(url: string): Promise<T> {
12-
return new Promise<T>((resolve, reject) => {
13-
https
14-
.request(url, {}, res => {
15-
if (res.statusCode !== 200) {
16-
reject(
17-
new Error(
18-
`Got code ${res.statusCode}, URL: ${url}, message: ${res.statusMessage}`
19-
)
13+
return new Promise<T>(async (resolve, reject) => {
14+
try {
15+
const res = await fetch(url)
16+
if (res.status !== 200) {
17+
reject(
18+
new Error(
19+
`Got code ${res.status}, URL: ${url}, message: ${res.statusText}`
2020
)
21-
return
22-
}
23-
const data: Uint8Array[] = []
24-
res
25-
.on('data', (chunk: Uint8Array) => data.push(chunk))
26-
.on('end', () => {
27-
try {
28-
resolve(JSON.parse(Buffer.concat(data).toString('utf-8')))
29-
} catch (e) {
30-
reject(e)
31-
}
32-
})
33-
.on('error', e => reject(e))
34-
})
35-
.on('error', e => reject(e))
36-
.end()
21+
)
22+
return
23+
}
24+
25+
resolve(res.json())
26+
} catch (e) {
27+
reject(e)
28+
}
3729
})
3830
}
3931

0 commit comments

Comments
 (0)