|
2 | 2 |
|
3 | 3 | 'use strict';
|
4 | 4 |
|
| 5 | +const {EOL} = require('os'); |
5 | 6 | const {promises: fsp} = require('fs');
|
6 | 7 | const path = require('path');
|
7 | 8 | const zlib = require('zlib');
|
@@ -91,7 +92,12 @@ const withParams = (url, params) => {
|
91 | 92 |
|
92 | 93 | const artifactPath = getParam('artifact'),
|
93 | 94 | prefix = getParam('prefix'),
|
94 |
| - suffix = getParam('suffix'); |
| 95 | + suffix = getParam('suffix'), |
| 96 | + format = getParam('format', 'br'), |
| 97 | + requestedFormats = new Set(...format.toLowerCase().split(/\s*,\s*/)), |
| 98 | + skipBrotli = !zlib.brotliCompress || !requestedFormats.has('br'), |
| 99 | + skipGzip = !zlib.gzip || !requestedFormats.has('gz'), |
| 100 | + skipUncompressed = !requestedFormats.has('none'); |
95 | 101 |
|
96 | 102 | const main = async () => {
|
97 | 103 | const [OWNER, REPO] = process.env.GITHUB_REPOSITORY.split('/'),
|
@@ -119,54 +125,52 @@ const main = async () => {
|
119 | 125 | })
|
120 | 126 | ]);
|
121 | 127 |
|
| 128 | + const postArtifact = (name, label, data, contentType = 'application/octet-stream') => |
| 129 | + post( |
| 130 | + withParams(uploadUrl, {name, label}), |
| 131 | + { |
| 132 | + auth: TOKEN ? OWNER + ':' + TOKEN : null, |
| 133 | + headers: { |
| 134 | + Accept: 'application/vnd.github.v3+json', |
| 135 | + 'Content-Type': contentType, |
| 136 | + 'Content-Length': data.length, |
| 137 | + 'User-Agent': 'uhop/install-artifact-from-github', |
| 138 | + Authorization: !TOKEN && PERSONAL_TOKEN ? 'Bearer ' + PERSONAL_TOKEN : null |
| 139 | + } |
| 140 | + }, |
| 141 | + data |
| 142 | + ); |
| 143 | + |
122 | 144 | console.log('Compressing and uploading ...');
|
123 | 145 |
|
124 | 146 | await Promise.all([
|
125 | 147 | (async () => {
|
126 |
| - if (!zlib.brotliCompress) return null; |
| 148 | + if (skipBrotli) return null; |
127 | 149 | const compressed = await promisify(zlib.brotliCompress)(data, {params: {[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY}}),
|
128 | 150 | name = fileName + '.br',
|
129 | 151 | label = `Binary artifact: ${artifactPath} (${platform}, ${process.arch}, ${process.versions.modules}, brotli).`;
|
130 |
| - return post( |
131 |
| - withParams(uploadUrl, {name, label}), |
132 |
| - { |
133 |
| - auth: TOKEN ? OWNER + ':' + TOKEN : null, |
134 |
| - headers: { |
135 |
| - Accept: 'application/vnd.github.v3+json', |
136 |
| - 'Content-Type': 'application/brotli', |
137 |
| - 'Content-Length': compressed.length, |
138 |
| - 'User-Agent': 'uhop/install-artifact-from-github', |
139 |
| - Authorization: !TOKEN && PERSONAL_TOKEN ? 'Bearer ' + PERSONAL_TOKEN : null |
140 |
| - } |
141 |
| - }, |
142 |
| - compressed |
143 |
| - ) |
| 152 | + return postArtifact(name, label, compressed, 'application/brotli') |
144 | 153 | .then(({res}) => console.log('Uploaded BR:', res.statusCode))
|
145 | 154 | .catch(error => console.error('BR has failed to upload:', error));
|
146 | 155 | })(),
|
147 | 156 | (async () => {
|
148 |
| - if (!zlib.gzip) return null; |
| 157 | + if (skipGzip) return null; |
149 | 158 | const compressed = await promisify(zlib.gzip)(data, {level: zlib.constants.Z_BEST_COMPRESSION}),
|
150 | 159 | name = fileName + '.gz',
|
151 | 160 | label = `Binary artifact: ${artifactPath} (${platform}, ${process.arch}, ${process.versions.modules}, gzip).`;
|
152 |
| - return post( |
153 |
| - withParams(uploadUrl, {name, label}), |
154 |
| - { |
155 |
| - auth: TOKEN ? OWNER + ':' + TOKEN : null, |
156 |
| - headers: { |
157 |
| - Accept: 'application/vnd.github.v3+json', |
158 |
| - 'Content-Type': 'application/gzip', |
159 |
| - 'Content-Length': compressed.length, |
160 |
| - 'User-Agent': 'uhop/install-artifact-from-github', |
161 |
| - Authorization: !TOKEN && PERSONAL_TOKEN ? 'Bearer ' + PERSONAL_TOKEN : null |
162 |
| - } |
163 |
| - }, |
164 |
| - compressed |
165 |
| - ) |
| 161 | + return postArtifact(name, label, compressed, 'application/gzip') |
166 | 162 | .then(({res}) => console.log('Uploaded GZ:', res.statusCode))
|
167 | 163 | .catch(error => console.error('GZ has failed to upload:', error));
|
| 164 | + })(), |
| 165 | + (async () => { |
| 166 | + if (skipUncompressed) return null; |
| 167 | + const label = `Binary artifact: ${artifactPath} (${platform}, ${process.arch}, ${process.versions.modules}, uncompressed).`; |
| 168 | + return postArtifact(fileName, label, data) |
| 169 | + .then(({res}) => console.log('Uploaded Uncompressed:', res.statusCode)) |
| 170 | + .catch(error => console.error('Uncompressed has failed to upload:', error)); |
168 | 171 | })()
|
169 | 172 | ]);
|
| 173 | + if (process.env.GITHUB_ENV) await fsp.appendFile(process.env.GITHUB_ENV, 'CREATED_ASSET_NAME=' + fileName + EOL); |
170 | 174 | console.log('Done.');
|
171 | 175 | };
|
172 | 176 |
|
|
0 commit comments