| 1 | // Post-build for the level 2 vendor bundle (called by build.mjs). |
| 2 | // |
| 3 | // 1. Sweep the raw files for machine paths (sweep.mjs); nothing is written if |
| 4 | // the sweep fails. |
| 5 | // 2. Gzip each file at level 9 into a gzip container written here, so the |
| 6 | // header carries no time stamp and no OS byte. |
| 7 | // 3. sha256 of every raw file, SRI (sha256-…) of every .gz. |
| 8 | // 4. buildId = first 16 hex characters of the sha256 over the sorted |
| 9 | // [name, sha256] list: the folder name changes exactly when the bytes do. |
| 10 | // 5. Write <outRoot>/vendor/x2/<buildId>/{*.js.gz, LICENSES.txt} (git-ignored) |
| 11 | // and <outRoot>/js/ext2/vendor-manifest.json (committed, a few KB). |
| 12 | // 6. Fail if the three .gz files together exceed the size budget. |
| 13 | import fs from 'node:fs'; |
| 14 | import path from 'node:path'; |
| 15 | import zlib from 'node:zlib'; |
| 16 | import crypto from 'node:crypto'; |
| 17 | import { sweepBuffers } from './sweep.mjs'; |
| 18 | |
| 19 | export const BUDGET_GZ = 2_800_000; // bytes, the three .gz files together |
| 20 | export const ROLES = { 'sandbox-main': 'main', 'exthost-worker': 'extensionHostWorkerMain', 'editor-worker': 'editorWorkerService' }; |
| 21 | export const PROTOCOL = 1; |
| 22 | |
| 23 | const sha256 = buf => crypto.createHash('sha256').update(buf).digest(); |
| 24 | const hex = buf => sha256(buf).toString('hex'); |
| 25 | |
| 26 | // RFC 1952 container around zlib's raw deflate: ID1 ID2 CM FLG, MTIME = 0, |
| 27 | // XFL = 2 (maximum compression), OS = 255 (unknown); then CRC32 and ISIZE. |
| 28 | export function gzip(raw) { |
| 29 | const header = Buffer.from([0x1f, 0x8b, 0x08, 0x00, 0, 0, 0, 0, 0x02, 0xff]); |
| 30 | const body = zlib.deflateRawSync(raw, { level: 9, memLevel: 9, windowBits: 15, strategy: zlib.constants.Z_DEFAULT_STRATEGY }); |
| 31 | const trailer = Buffer.alloc(8); |
| 32 | trailer.writeUInt32LE(zlib.crc32(raw) >>> 0, 0); |
| 33 | trailer.writeUInt32LE(raw.length % 0x100000000, 4); |
| 34 | return Buffer.concat([header, body, trailer]); |
| 35 | } |
| 36 | |
| 37 | // Packages that publish no licence file of their own. The @codingame |
| 38 | // packages are VS Code's own code (MIT, Microsoft) repackaged by CodinGame |
| 39 | // (MIT). |
| 40 | const MIT = `Permission is hereby granted, free of charge, to any person obtaining a copy |
| 41 | of this software and associated documentation files (the "Software"), to deal |
| 42 | in the Software without restriction, including without limitation the rights |
| 43 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 44 | copies of the Software, and to permit persons to whom the Software is |
| 45 | furnished to do so, subject to the following conditions: |
| 46 | |
| 47 | The above copyright notice and this permission notice shall be included in all |
| 48 | copies or substantial portions of the Software. |
| 49 | |
| 50 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 51 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 52 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 53 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 54 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 55 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 56 | SOFTWARE.`; |
| 57 | |
| 58 | export function licensesText(entries, { vscode }) { |
| 59 | const byKey = new Map(); |
| 60 | for (const e of entries) byKey.set(`${e.name}@${e.version}`, e); |
| 61 | const list = [...byKey.values()].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : a.version < b.version ? -1 : 1)); |
| 62 | const out = [ |
| 63 | 'Elffuss Code: level 2 extension runtime (vendor bundle)', |
| 64 | '', |
| 65 | 'These files bundle the packages listed below, unmodified apart from', |
| 66 | 'minification. The following are NOT included: jschardet, @vscode/iconv-lite-umd,', |
| 67 | '@vscode/diff and the keyboard layout tables of VS Code\'s keybinding service;', |
| 68 | 'Elffuss Code replaces them with small stand-ins of its own.', |
| 69 | '', |
| 70 | `The @codingame/monaco-vscode-* packages contain the source code of Visual`, |
| 71 | `Studio Code ${vscode} (Copyright (c) 2015 - present Microsoft Corporation, MIT`, |
| 72 | 'licence), repackaged by CodinGame under the MIT licence.', |
| 73 | ]; |
| 74 | for (const e of list) { |
| 75 | out.push('', '='.repeat(78), `${e.name} ${e.version}${e.identifier ? ` (${e.identifier})` : ''}`, '='.repeat(78), ''); |
| 76 | if (e.text) out.push(e.text.replace(/\r\n/g, '\n').trim()); |
| 77 | else if (/^MIT$/i.test(e.identifier || '')) out.push('This package ships no licence file; its package.json declares MIT.', '', 'MIT License', '', MIT); |
| 78 | else out.push('This package ships no licence file; see its package.json: ' + (e.identifier || 'no licence declared')); |
| 79 | } |
| 80 | return out.join('\n') + '\n'; |
| 81 | } |
| 82 | |
| 83 | function vscodeVersion(buildRoot) { |
| 84 | const src = fs.readFileSync(path.join(buildRoot, 'node_modules/@codingame/monaco-vscode-api/vscode/src/vs/platform/product/common/product.js'), 'utf8'); |
| 85 | const m = /version:\s*'(\d+\.\d+\.\d+)'/.exec(src); |
| 86 | if (!m) throw new Error('cannot read the VS Code version of the library'); |
| 87 | return m[1]; |
| 88 | } |
| 89 | |
| 90 | // Builds every output in memory; writes nothing until all checks passed. |
| 91 | export async function postbuild(raw, { outRoot, buildRoot, write = true }) { |
| 92 | const pkg = JSON.parse(fs.readFileSync(path.join(buildRoot, 'package.json'), 'utf8')); |
| 93 | const vscode = vscodeVersion(buildRoot); |
| 94 | const library = '@codingame/monaco-vscode-api@' + pkg.dependencies['@codingame/monaco-vscode-api']; |
| 95 | const node = fs.readFileSync(path.join(buildRoot, '.node-version'), 'utf8').trim(); |
| 96 | const lockfileSha256 = hex(fs.readFileSync(path.join(buildRoot, 'package-lock.json'))); |
| 97 | |
| 98 | const outputs = []; // { name, role?, raw: Buffer } |
| 99 | const licenseEntries = []; |
| 100 | for (const [entry, { file, licenses }] of Object.entries(raw)) { |
| 101 | outputs.push({ name: entry + '.js', role: ROLES[entry], raw: fs.readFileSync(file) }); |
| 102 | licenseEntries.push(...licenses); |
| 103 | } |
| 104 | const licenses = Buffer.from(licensesText(licenseEntries, { vscode }), 'utf8'); |
| 105 | |
| 106 | // 1. sweep before anything is written |
| 107 | const allow = JSON.parse(fs.readFileSync(path.join(buildRoot, 'sweep-allowlist.json'), 'utf8')); |
| 108 | const report = sweepBuffers([...outputs.map(o => ({ name: o.name, bytes: o.raw })), { name: 'LICENSES.txt', bytes: licenses }], allow, { buildRoot }); |
| 109 | if (!report.ok) throw new Error('path sweep failed:\n ' + report.problems.join('\n ')); |
| 110 | |
| 111 | // 2–4. |
| 112 | const files = outputs.map(o => { |
| 113 | const gz = gzip(o.raw); |
| 114 | return { name: o.name, role: o.role, raw: o.raw.length, gz: gz.length, sha256Raw: hex(o.raw), sriGz: 'sha256-' + sha256(gz).toString('base64'), _gz: gz }; |
| 115 | }); |
| 116 | const licensesSha256 = hex(licenses); |
| 117 | const idList = [...files.map(f => [f.name, f.sha256Raw]), ['LICENSES.txt', licensesSha256]].sort((a, b) => (a[0] < b[0] ? -1 : 1)); |
| 118 | const buildId = hex(Buffer.from(JSON.stringify(idList))).slice(0, 16); |
| 119 | const totalGz = files.reduce((n, f) => n + f.gz, 0); |
| 120 | if (totalGz > BUDGET_GZ) throw new Error(`size budget exceeded: ${totalGz} bytes gzip > ${BUDGET_GZ}`); |
| 121 | |
| 122 | const manifest = { |
| 123 | schema: 1, enabled: true, buildId, protocol: PROTOCOL, |
| 124 | library, vscode, node, lockfileSha256, |
| 125 | budgetGz: BUDGET_GZ, totalGz, |
| 126 | files: files.map(({ _gz, ...f }) => f), |
| 127 | licenses: { name: 'LICENSES.txt', bytes: licenses.length, sha256: licensesSha256 }, |
| 128 | }; |
| 129 | const manifestText = JSON.stringify(manifest, null, 2) + '\n'; |
| 130 | |
| 131 | const dir = path.join(outRoot, 'vendor', 'x2', buildId); |
| 132 | const manifestPath = path.join(outRoot, 'js', 'ext2', 'vendor-manifest.json'); |
| 133 | if (write) { |
| 134 | fs.mkdirSync(dir, { recursive: true }); |
| 135 | for (const f of files) fs.writeFileSync(path.join(dir, f.name + '.gz'), f._gz); |
| 136 | fs.writeFileSync(path.join(dir, 'LICENSES.txt'), licenses); |
| 137 | fs.mkdirSync(path.dirname(manifestPath), { recursive: true }); |
| 138 | fs.writeFileSync(manifestPath, manifestText); |
| 139 | } |
| 140 | return { manifest, manifestText, manifestPath, dir, totalGz, sweep: report }; |
| 141 | } |