| 1 | # Level 2 vendor bundle |
| 2 | |
| 3 | The runtime that lets Elffuss Code run the code of VS Code web extensions: |
| 4 | a headless VS Code (`@codingame/monaco-vscode-api` 37.3.11, VS Code 1.138.0) |
| 5 | that runs inside a sandbox iframe on a separate site with no network. Code's |
| 6 | own JavaScript stays buildless; only this bundle is built, and only here. |
| 7 | |
| 8 | This folder is the only place in the repository where npm runs. |
| 9 | |
| 10 | ## What it produces |
| 11 | |
| 12 | Three single-file ES modules, gzipped, plus the licences of what they bundle: |
| 13 | |
| 14 | | File | Role | |
| 15 | |---|---| |
| 16 | | `sandbox-main.js.gz` | VS Code services, extension manager, hidden mirror editor | |
| 17 | | `exthost-worker.js.gz` | the extension host, which runs extension code in a worker | |
| 18 | | `editor-worker.js.gz` | VS Code's editor worker for the mirror editor | |
| 19 | | `LICENSES.txt` | every bundled package's licence | |
| 20 | |
| 21 | They go to `web/vendor/x2/<buildId>/`, which git ignores. The `buildId` is the |
| 22 | first 16 hex characters of a sha256 over the files' hashes, so it changes |
| 23 | exactly when the bytes change. The build also writes |
| 24 | `web/js/ext2/vendor-manifest.json` (committed, a few KB): sizes, the sha256 of |
| 25 | each raw file and an SRI value for each `.gz`. Code's page checks both before |
| 26 | it hands a byte to the sandbox (`web/js/ext2/vendor.js`). |
| 27 | |
| 28 | Not shipped: jschardet (LGPL-2.1), the code-page tables of |
| 29 | `@vscode/iconv-lite-umd`, `@vscode/diff` (a wasm binary and its own worker) |
| 30 | and VS Code's keyboard layout tables. `src/stubs/` replaces them with small |
| 31 | stand-ins; the sandbox only ever sees UTF-8 text and never receives key |
| 32 | events. |
| 33 | |
| 34 | ## Build |
| 35 | |
| 36 | ```sh |
| 37 | cd vendor/vscode-ext2 |
| 38 | npm ci --ignore-scripts # .npmrc also sets ignore-scripts |
| 39 | node build.mjs |
| 40 | ``` |
| 41 | |
| 42 | `build.mjs` refuses to run on any Node version other than `.node-version`, or |
| 43 | when an installed package differs from the lockfile. It fails, writing |
| 44 | nothing, when: |
| 45 | |
| 46 | - the path sweep finds anything (below); |
| 47 | - an entry produces more than one file; |
| 48 | - the three `.gz` files together exceed 2.8 MB (`BUDGET_GZ` in `postbuild.mjs`). |
| 49 | |
| 50 | The `.gz` files are written by `postbuild.mjs` itself (level 9, no time stamp, |
| 51 | no OS byte). Their bytes depend on Node's zlib, so they are reproducible for |
| 52 | the pinned Node version on the same CPU architecture; the raw files, and so |
| 53 | the `buildId`, do not depend on zlib. |
| 54 | |
| 55 | ## Checks |
| 56 | |
| 57 | ```sh |
| 58 | node verify-repro.mjs # two clean builds in different folders: byte-identical, equal to the committed manifest |
| 59 | node sweep.mjs # sweep the built folder named by the manifest |
| 60 | node sweep.mjs --list <dir> # print every generic hit with its context, to review an allowlist change |
| 61 | node check-output.mjs # the built folder against the manifest: only its files, SRI, sha256, sweep |
| 62 | ``` |
| 63 | |
| 64 | `verify-repro.mjs` needs 15 GB of free disk and puts its temporary folders |
| 65 | under `$X2_REPRO_TMP` (default: the system temp folder). |
| 66 | |
| 67 | The sweep fails on the user name, the home folder, the build folder or the |
| 68 | repository folder anywhere in the output, including inside base64 `data:` |
| 69 | URLs. Generic patterns (home prefixes of macOS, Linux and Windows, file URLs |
| 70 | and source-map comments) are allowed only when the exact hit is listed in |
| 71 | `sweep-allowlist.json` with its count. A hit is identified by the sha256 of |
| 72 | the literal it sits in (up to 40 characters either side, cut at quotes and |
| 73 | line breaks), so renamed minified code around it does not change the list, |
| 74 | but any new literal, or a different count, fails the build. Today's allowed |
| 75 | hits are VS Code's own help-text example paths and its file-URL checks. |
| 76 | |
| 77 | ## Where it is served from |
| 78 | |
| 79 | From Code's own origin, `https://code.elffuss.com/vendor/x2/<buildId>/…`, with: |
| 80 | |
| 81 | ``` |
| 82 | location ^~ /vendor/x2/ { default_type application/octet-stream; gzip off; |
| 83 | add_header Cache-Control "public, max-age=31536000, immutable"; |
| 84 | add_header X-Content-Type-Options nosniff always; } |
| 85 | ``` |
| 86 | |
| 87 | `application/octet-stream` keeps browsers from decoding the `.gz` on their own: |
| 88 | the page decompresses it with `DecompressionStream`, then checks the sha256 of |
| 89 | the raw bytes. That works whatever transfer compression a proxy adds. |
| 90 | |
| 91 | Cache-Control has no `always`, so nginx adds it only to successful responses. |
| 92 | With `always`, a 404 for a build that is not on the server yet would be cached |
| 93 | as immutable for a year; because the `buildId` is a content hash, uploading |
| 94 | the same bytes afterwards would not reach the browsers that cached the |
| 95 | failure: only a new `buildId` would. `tests/x2_headers.mjs` (H5) checks that a |
| 96 | missing file is not cached that way, also against a deployment. |
| 97 | |
| 98 | Nothing is published to npm or a CDN, and nothing large goes into git. |
| 99 | |
| 100 | ## Deploy |
| 101 | |
| 102 | 0. **Merge first.** Only this branch's `deploy.sh` protects `vendor/x2/` from |
| 103 | its `rsync --delete`; a Code deploy from a tree without that filter (main |
| 104 | today) deletes the uploaded bundle. `deploy-vendor.sh` refuses to run until |
| 105 | `main`'s `deploy.sh` has the filter. |
| 106 | 1. `./deploy-vendor.sh` (repository root; reads `ELFFUSS_HOST` and |
| 107 | `ELFFUSS_KEY`, like `deploy.sh`). It checks the local folder against the |
| 108 | manifest (`check-output.mjs`: exactly the manifest's files and nothing |
| 109 | else), uploads only those files with `rsync --ignore-existing` (never |
| 110 | `--delete`), then requires the server's folder to hold exactly those files |
| 111 | with the manifest's sha256 sums. |
| 112 | 2. `./deploy.sh`, which ships the manifest naming the new `buildId`. A normal |
| 113 | deploy neither uploads nor deletes anything under `vendor/x2/`. Once level |
| 114 | 2 is live (the manifest enabled and `web/js/ext2/config.js` naming a |
| 115 | sandbox for Code), it refuses to ship a manifest whose build the server |
| 116 | does not hold, byte for byte (`check-output.mjs --manifest-sums`, from the |
| 117 | manifest alone). |
| 118 | |
| 119 | If a browser still finds a build missing (a partial upload, a pruned |
| 120 | rollback), level 2 fails closed and the default path is unaffected; the |
| 121 | failure is not cached as long as Cache-Control has no `always` (above). Old |
| 122 | builds stay until a manual prune keeps the last three; reverting the |
| 123 | manifest and redeploying is the rollback. Setting `"enabled": false` in the |
| 124 | manifest is the global kill switch. |
| 125 | |
| 126 | ## Upgrade checklist |
| 127 | |
| 128 | 1. Bump the exact versions in `package.json` (all `@codingame/*` packages |
| 129 | and the `monaco-editor` alias together). |
| 130 | 2. `npm install --ignore-scripts`, then `node build.mjs`, |
| 131 | `node verify-repro.mjs` and `node sweep.mjs`. Review any allowlist change |
| 132 | with `--list`. |
| 133 | 3. Check that the library's `webWorkerExtensionHostIframe.html` still matches |
| 134 | `sandbox/x2/v1/exthost-iframe.html` apart from the CSP meta, and that its |
| 135 | inline-script hash is unchanged. |
| 136 | 4. Diff the API-surface snapshot (`tests/x2-surface.json`, rewritten with |
| 137 | `node tests/x2_smoke.mjs --update`) and review the RPC allowlist |
| 138 | (`web/js/ext2/rpc-allowlist.json`): every actor must be either allowed or |
| 139 | listed in `deniedActors`, and `node tests/x2_unit.mjs` fails until it is. |
| 140 | 5. Run all suites in both modes, plus the canary. |
| 141 | 6. Re-run the runtime census (`tests/x2_census.mjs`). |
| 142 | |
| 143 | No library source is patched on disk; every hook is a runtime wrap in |
| 144 | `web/js/ext2/sandbox-relay.js`. |