Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/azure-pipelines/upload-cdn.ts
5310 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import es from 'event-stream';
7
import Vinyl from 'vinyl';
8
import vfs from 'vinyl-fs';
9
import filter from 'gulp-filter';
10
import gzip from 'gulp-gzip';
11
import mime from 'mime';
12
import { ClientAssertionCredential } from '@azure/identity';
13
import { VinylStat } from '../lib/util.ts';
14
import azure from 'gulp-azure-storage';
15
16
const commit = process.env['BUILD_SOURCEVERSION'];
17
const credential = new ClientAssertionCredential(process.env['AZURE_TENANT_ID']!, process.env['AZURE_CLIENT_ID']!, () => Promise.resolve(process.env['AZURE_ID_TOKEN']!));
18
19
mime.define({
20
'application/typescript': ['ts'],
21
'application/json': ['code-snippets'],
22
});
23
24
// From default AFD configuration
25
const MimeTypesToCompress = new Set([
26
'application/eot',
27
'application/font',
28
'application/font-sfnt',
29
'application/javascript',
30
'application/json',
31
'application/opentype',
32
'application/otf',
33
'application/pkcs7-mime',
34
'application/truetype',
35
'application/ttf',
36
'application/typescript',
37
'application/vnd.ms-fontobject',
38
'application/xhtml+xml',
39
'application/xml',
40
'application/xml+rss',
41
'application/x-font-opentype',
42
'application/x-font-truetype',
43
'application/x-font-ttf',
44
'application/x-httpd-cgi',
45
'application/x-javascript',
46
'application/x-mpegurl',
47
'application/x-opentype',
48
'application/x-otf',
49
'application/x-perl',
50
'application/x-ttf',
51
'font/eot',
52
'font/ttf',
53
'font/otf',
54
'font/opentype',
55
'image/svg+xml',
56
'text/css',
57
'text/csv',
58
'text/html',
59
'text/javascript',
60
'text/js',
61
'text/markdown',
62
'text/plain',
63
'text/richtext',
64
'text/tab-separated-values',
65
'text/xml',
66
'text/x-script',
67
'text/x-component',
68
'text/x-java-source'
69
]);
70
71
function wait(stream: es.ThroughStream): Promise<void> {
72
return new Promise<void>((c, e) => {
73
stream.on('end', () => c());
74
stream.on('error', (err) => e(err));
75
});
76
}
77
78
async function main(): Promise<void> {
79
const files: string[] = [];
80
const options = (compressed: boolean) => ({
81
account: process.env.AZURE_STORAGE_ACCOUNT,
82
credential,
83
container: '$web',
84
prefix: `${process.env.VSCODE_QUALITY}/${commit}/`,
85
contentSettings: {
86
contentEncoding: compressed ? 'gzip' : undefined,
87
cacheControl: 'max-age=31536000, public'
88
}
89
});
90
91
const all = vfs.src('**', { cwd: '../vscode-web', base: '../vscode-web', dot: true })
92
.pipe(filter(f => !f.isDirectory()));
93
94
const compressed = all
95
.pipe(filter(f => MimeTypesToCompress.has(mime.lookup(f.path))))
96
.pipe(gzip({ append: false }))
97
.pipe(azure.upload(options(true)));
98
99
const uncompressed = all
100
.pipe(filter(f => !MimeTypesToCompress.has(mime.lookup(f.path))))
101
.pipe(azure.upload(options(false)));
102
103
const out = es.merge(compressed, uncompressed)
104
.pipe(es.through(function (f) {
105
console.log('Uploaded:', f.relative);
106
files.push(f.relative);
107
this.emit('data', f);
108
}));
109
110
console.log(`Uploading files to CDN...`); // debug
111
await wait(out);
112
113
const listing = new Vinyl({
114
path: 'files.txt',
115
contents: Buffer.from(files.join('\n')),
116
stat: new VinylStat({ mode: 0o666 })
117
});
118
119
const filesOut = es.readArray([listing])
120
.pipe(gzip({ append: false }))
121
.pipe(azure.upload(options(true)));
122
123
console.log(`Uploading: files.txt (${files.length} files)`); // debug
124
await wait(filesOut);
125
}
126
127
main().catch(err => {
128
console.error(err);
129
process.exit(1);
130
});
131
132