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