Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cdn: only gzip encode known mimetypes #174577

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 86 additions & 28 deletions build/azure-pipelines/upload-cdn.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 95 additions & 29 deletions build/azure-pipelines/upload-cdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,119 @@ import * as Vinyl from 'vinyl';
import * as vfs from 'vinyl-fs';
import * as filter from 'gulp-filter';
import * as gzip from 'gulp-gzip';
import * as mime from 'mime';
import { ClientSecretCredential } from '@azure/identity';
const azure = require('gulp-azure-storage');

const commit = process.env['VSCODE_DISTRO_COMMIT'] || process.env['BUILD_SOURCEVERSION'];
const credential = new ClientSecretCredential(process.env['AZURE_TENANT_ID']!, process.env['AZURE_CLIENT_ID']!, process.env['AZURE_CLIENT_SECRET']!);

mime.define({
'application/typescript': ['ts'],
'application/json': ['code-snippets'],
});

// From default AFD configuration
const MimeTypesToCompress = new Set([
'application/eot',
'application/font',
'application/font-sfnt',
'application/javascript',
'application/json',
'application/opentype',
'application/otf',
'application/pkcs7-mime',
'application/truetype',
'application/ttf',
'application/typescript',
'application/vnd.ms-fontobject',
'application/xhtml+xml',
'application/xml',
'application/xml+rss',
'application/x-font-opentype',
'application/x-font-truetype',
'application/x-font-ttf',
'application/x-httpd-cgi',
'application/x-javascript',
'application/x-mpegurl',
'application/x-opentype',
'application/x-otf',
'application/x-perl',
'application/x-ttf',
'font/eot',
'font/ttf',
'font/otf',
'font/opentype',
'image/svg+xml',
'text/css',
'text/csv',
'text/html',
'text/javascript',
'text/js',
'text/markdown',
'text/plain',
'text/richtext',
'text/tab-separated-values',
'text/xml',
'text/x-script',
'text/x-component',
'text/x-java-source'
]);

function wait(stream: es.ThroughStream): Promise<void> {
return new Promise<void>((c, e) => {
stream.on('end', () => c());
stream.on('error', (err: any) => e(err));
});
}

async function main(): Promise<void> {
const files: string[] = [];
const options = {
const options = (compressed: boolean) => ({
account: process.env.AZURE_STORAGE_ACCOUNT,
credential,
container: process.env.VSCODE_QUALITY,
prefix: commit + '/',
contentSettings: {
contentEncoding: 'gzip',
contentEncoding: compressed ? 'gzip' : undefined,
cacheControl: 'max-age=31536000, public'
}
};

await new Promise<void>((c, e) => {
vfs.src('**', { cwd: '../vscode-web', base: '../vscode-web', dot: true })
.pipe(filter(f => !f.isDirectory()))
.pipe(gzip({ append: false }))
.pipe(es.through(function (data: Vinyl) {
console.log('Uploading:', data.relative); // debug
files.push(data.relative);
this.emit('data', data);
}))
.pipe(azure.upload(options))
.on('end', () => c())
.on('error', (err: any) => e(err));
});

await new Promise<void>((c, e) => {
const listing = new Vinyl({
path: 'files.txt',
contents: Buffer.from(files.join('\n')),
stat: { mode: 0o666 } as any
});

console.log(`Uploading: files.txt (${files.length} files)`); // debug
es.readArray([listing])
.pipe(gzip({ append: false }))
.pipe(azure.upload(options))
.on('end', () => c())
.on('error', (err: any) => e(err));
const all = vfs.src('**', { cwd: '../vscode-web', base: '../vscode-web', dot: true })
.pipe(filter(f => !f.isDirectory()));

const compressed = all
.pipe(filter(f => MimeTypesToCompress.has(mime.lookup(f.path))))
.pipe(gzip({ append: false }))
.pipe(azure.upload(options(true)));

const uncompressed = all
.pipe(filter(f => !MimeTypesToCompress.has(mime.lookup(f.path))))
.pipe(azure.upload(options(false)));

const out = es.merge(compressed, uncompressed)
.pipe(es.through(function (f) {
console.log('Uploaded:', f.relative);
files.push(f.relative);
this.emit('data', f);
}));

console.log(`Uploading files to CDN...`); // debug
await wait(out);

const listing = new Vinyl({
path: 'files.txt',
contents: Buffer.from(files.join('\n')),
stat: { mode: 0o666 } as any
});

const filesOut = es.readArray([listing])
.pipe(gzip({ append: false }))
.pipe(azure.upload(options(true)));

console.log(`Uploading: files.txt (${files.length} files)`); // debug
await wait(filesOut);
}

main().catch(err => {
Expand Down