Skip to content

Commit 0471ed4

Browse files
artifact header cleanup (actions#441)
* Update NPM packages for @actions/artifact * Clarifications around headers * Revert NPM updates * Apply suggestions from code review Co-authored-by: Josh Gross <joshmgross@github.com> Co-authored-by: Josh Gross <joshmgross@github.com>
1 parent d1b52e7 commit 0471ed4

File tree

4 files changed

+47
-52
lines changed

4 files changed

+47
-52
lines changed

packages/artifact/__tests__/util.test.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,55 +120,55 @@ describe('Utils', () => {
120120
const size = 24
121121
const uncompressedLength = 100
122122
const range = 'bytes 0-199/200'
123-
const options = utils.getUploadRequestOptions(
123+
const headers = utils.getUploadHeaders(
124124
contentType,
125125
true,
126126
true,
127127
uncompressedLength,
128128
size,
129129
range
130130
)
131-
expect(Object.keys(options).length).toEqual(8)
132-
expect(options['Accept']).toEqual(
131+
expect(Object.keys(headers).length).toEqual(8)
132+
expect(headers['Accept']).toEqual(
133133
`application/json;api-version=${utils.getApiVersion()}`
134134
)
135-
expect(options['Content-Type']).toEqual(contentType)
136-
expect(options['Connection']).toEqual('Keep-Alive')
137-
expect(options['Keep-Alive']).toEqual('10')
138-
expect(options['Content-Encoding']).toEqual('gzip')
139-
expect(options['x-tfs-filelength']).toEqual(uncompressedLength)
140-
expect(options['Content-Length']).toEqual(size)
141-
expect(options['Content-Range']).toEqual(range)
135+
expect(headers['Content-Type']).toEqual(contentType)
136+
expect(headers['Connection']).toEqual('Keep-Alive')
137+
expect(headers['Keep-Alive']).toEqual('10')
138+
expect(headers['Content-Encoding']).toEqual('gzip')
139+
expect(headers['x-tfs-filelength']).toEqual(uncompressedLength)
140+
expect(headers['Content-Length']).toEqual(size)
141+
expect(headers['Content-Range']).toEqual(range)
142142
})
143143

144144
it('Test constructing upload headers with only required parameter', () => {
145-
const options = utils.getUploadRequestOptions('application/octet-stream')
146-
expect(Object.keys(options).length).toEqual(2)
147-
expect(options['Accept']).toEqual(
145+
const headers = utils.getUploadHeaders('application/octet-stream')
146+
expect(Object.keys(headers).length).toEqual(2)
147+
expect(headers['Accept']).toEqual(
148148
`application/json;api-version=${utils.getApiVersion()}`
149149
)
150-
expect(options['Content-Type']).toEqual('application/octet-stream')
150+
expect(headers['Content-Type']).toEqual('application/octet-stream')
151151
})
152152

153153
it('Test constructing download headers with all optional parameters', () => {
154154
const contentType = 'application/json'
155-
const options = utils.getDownloadRequestOptions(contentType, true, true)
156-
expect(Object.keys(options).length).toEqual(5)
157-
expect(options['Content-Type']).toEqual(contentType)
158-
expect(options['Connection']).toEqual('Keep-Alive')
159-
expect(options['Keep-Alive']).toEqual('10')
160-
expect(options['Accept-Encoding']).toEqual('gzip')
161-
expect(options['Accept']).toEqual(
155+
const headers = utils.getDownloadHeaders(contentType, true, true)
156+
expect(Object.keys(headers).length).toEqual(5)
157+
expect(headers['Content-Type']).toEqual(contentType)
158+
expect(headers['Connection']).toEqual('Keep-Alive')
159+
expect(headers['Keep-Alive']).toEqual('10')
160+
expect(headers['Accept-Encoding']).toEqual('gzip')
161+
expect(headers['Accept']).toEqual(
162162
`application/octet-stream;api-version=${utils.getApiVersion()}`
163163
)
164164
})
165165

166166
it('Test constructing download headers with only required parameter', () => {
167-
const options = utils.getDownloadRequestOptions('application/octet-stream')
168-
expect(Object.keys(options).length).toEqual(2)
169-
expect(options['Content-Type']).toEqual('application/octet-stream')
167+
const headers = utils.getDownloadHeaders('application/octet-stream')
168+
expect(Object.keys(headers).length).toEqual(2)
169+
expect(headers['Content-Type']).toEqual('application/octet-stream')
170170
// check for default accept type
171-
expect(options['Accept']).toEqual(
171+
expect(headers['Accept']).toEqual(
172172
`application/json;api-version=${utils.getApiVersion()}`
173173
)
174174
})

packages/artifact/src/internal/download-http-client.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as core from '@actions/core'
33
import * as zlib from 'zlib'
44
import {
55
getArtifactUrl,
6-
getDownloadRequestOptions,
6+
getDownloadHeaders,
77
isSuccessStatusCode,
88
isRetryableStatusCode,
99
isThrottledStatusCode,
@@ -40,8 +40,8 @@ export class DownloadHttpClient {
4040

4141
// use the first client from the httpManager, `keep-alive` is not used so the connection will close immediately
4242
const client = this.downloadHttpManager.getClient(0)
43-
const requestOptions = getDownloadRequestOptions('application/json')
44-
const response = await client.get(artifactUrl, requestOptions)
43+
const headers = getDownloadHeaders('application/json')
44+
const response = await client.get(artifactUrl, headers)
4545
const body: string = await response.readBody()
4646

4747
if (isSuccessStatusCode(response.message.statusCode) && body) {
@@ -68,8 +68,8 @@ export class DownloadHttpClient {
6868

6969
// use the first client from the httpManager, `keep-alive` is not used so the connection will close immediately
7070
const client = this.downloadHttpManager.getClient(0)
71-
const requestOptions = getDownloadRequestOptions('application/json')
72-
const response = await client.get(resourceUrl.toString(), requestOptions)
71+
const headers = getDownloadHeaders('application/json')
72+
const response = await client.get(resourceUrl.toString(), headers)
7373
const body: string = await response.readBody()
7474

7575
if (isSuccessStatusCode(response.message.statusCode) && body) {
@@ -149,22 +149,19 @@ export class DownloadHttpClient {
149149
let retryCount = 0
150150
const retryLimit = getRetryLimit()
151151
const destinationStream = fs.createWriteStream(downloadPath)
152-
const requestOptions = getDownloadRequestOptions(
153-
'application/json',
154-
true,
155-
true
156-
)
152+
const headers = getDownloadHeaders('application/json', true, true)
157153

158154
// a single GET request is used to download a file
159155
const makeDownloadRequest = async (): Promise<IHttpClientResponse> => {
160156
const client = this.downloadHttpManager.getClient(httpClientIndex)
161-
return await client.get(artifactLocation, requestOptions)
157+
return await client.get(artifactLocation, headers)
162158
}
163159

164160
// check the response headers to determine if the file was compressed using gzip
165-
const isGzip = (headers: IncomingHttpHeaders): boolean => {
161+
const isGzip = (incomingHeaders: IncomingHttpHeaders): boolean => {
166162
return (
167-
'content-encoding' in headers && headers['content-encoding'] === 'gzip'
163+
'content-encoding' in incomingHeaders &&
164+
incomingHeaders['content-encoding'] === 'gzip'
168165
)
169166
}
170167

packages/artifact/src/internal/upload-http-client.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import {
1212
getArtifactUrl,
1313
getContentRange,
14-
getUploadRequestOptions,
14+
getUploadHeaders,
1515
isRetryableStatusCode,
1616
isSuccessStatusCode,
1717
isThrottledStatusCode,
@@ -63,8 +63,8 @@ export class UploadHttpClient {
6363

6464
// use the first client from the httpManager, `keep-alive` is not used so the connection will close immediately
6565
const client = this.uploadHttpManager.getClient(0)
66-
const requestOptions = getUploadRequestOptions('application/json', false)
67-
const rawResponse = await client.post(artifactUrl, data, requestOptions)
66+
const headers = getUploadHeaders('application/json', false)
67+
const rawResponse = await client.post(artifactUrl, data, headers)
6868
const body: string = await rawResponse.readBody()
6969

7070
if (isSuccessStatusCode(rawResponse.message.statusCode) && body) {
@@ -354,7 +354,7 @@ export class UploadHttpClient {
354354
totalFileSize: number
355355
): Promise<boolean> {
356356
// prepare all the necessary headers before making any http call
357-
const requestOptions = getUploadRequestOptions(
357+
const headers = getUploadHeaders(
358358
'application/octet-stream',
359359
true,
360360
isGzip,
@@ -365,7 +365,7 @@ export class UploadHttpClient {
365365

366366
const uploadChunkRequest = async (): Promise<IHttpClientResponse> => {
367367
const client = this.uploadHttpManager.getClient(httpClientIndex)
368-
return await client.sendStream('PUT', resourceUrl, data, requestOptions)
368+
return await client.sendStream('PUT', resourceUrl, data, headers)
369369
}
370370

371371
let retryCount = 0
@@ -464,7 +464,7 @@ export class UploadHttpClient {
464464
* Updating the size indicates that we are done uploading all the contents of the artifact
465465
*/
466466
async patchArtifactSize(size: number, artifactName: string): Promise<void> {
467-
const requestOptions = getUploadRequestOptions('application/json', false)
467+
const headers = getUploadHeaders('application/json', false)
468468
const resourceUrl = new URL(getArtifactUrl())
469469
resourceUrl.searchParams.append('artifactName', artifactName)
470470

@@ -477,7 +477,7 @@ export class UploadHttpClient {
477477
const response: HttpClientResponse = await client.patch(
478478
resourceUrl.toString(),
479479
data,
480-
requestOptions
480+
headers
481481
)
482482
const body: string = await response.readBody()
483483
if (isSuccessStatusCode(response.message.statusCode)) {

packages/artifact/src/internal/utils.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export function isForbiddenStatusCode(statusCode?: number): boolean {
6262
if (!statusCode) {
6363
return false
6464
}
65-
6665
return statusCode === HttpCodes.Forbidden
6766
}
6867

@@ -84,7 +83,6 @@ export function isThrottledStatusCode(statusCode?: number): boolean {
8483
if (!statusCode) {
8584
return false
8685
}
87-
8886
return statusCode === HttpCodes.TooManyRequests
8987
}
9088

@@ -133,9 +131,9 @@ export function getContentRange(
133131
* @param {boolean} isKeepAlive is the same connection being used to make multiple calls
134132
* @param {boolean} acceptGzip can we accept a gzip encoded response
135133
* @param {string} acceptType the type of content that we can accept
136-
* @returns appropriate request options to make a specific http call during artifact download
134+
* @returns appropriate headers to make a specific http call during artifact download
137135
*/
138-
export function getDownloadRequestOptions(
136+
export function getDownloadHeaders(
139137
contentType: string,
140138
isKeepAlive?: boolean,
141139
acceptGzip?: boolean
@@ -172,9 +170,9 @@ export function getDownloadRequestOptions(
172170
* @param {number} uncompressedLength the original size of the content if something is being uploaded that has been compressed
173171
* @param {number} contentLength the length of the content that is being uploaded
174172
* @param {string} contentRange the range of the content that is being uploaded
175-
* @returns appropriate request options to make a specific http call during artifact upload
173+
* @returns appropriate headers to make a specific http call during artifact upload
176174
*/
177-
export function getUploadRequestOptions(
175+
export function getUploadHeaders(
178176
contentType: string,
179177
isKeepAlive?: boolean,
180178
isGzip?: boolean,
@@ -207,7 +205,7 @@ export function getUploadRequestOptions(
207205
}
208206

209207
export function createHttpClient(): HttpClient {
210-
return new HttpClient('action/artifact', [
208+
return new HttpClient('actions/artifact', [
211209
new BearerCredentialHandler(getRuntimeToken())
212210
])
213211
}

0 commit comments

Comments
 (0)