@stacksjs/defaults 0.75.572 → 0.75.573

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  // Shared Chrome executable preparation for the standalone skill and testing package.
2
- import { existsSync, mkdirSync, mkdtempSync, readdirSync, renameSync, rmSync } from 'node:fs'
2
+ import { createHash } from 'node:crypto'
3
+ import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, renameSync, rmSync, writeFileSync } from 'node:fs'
3
4
  import { tmpdir } from 'node:os'
4
5
  import { basename, join } from 'node:path'
5
6
  import process from 'node:process'
@@ -72,17 +73,111 @@ function frameworkVersions(directory: string): { path: string, identifier: strin
72
73
  return result
73
74
  }
74
75
 
76
+ /**
77
+ * The framework versions a private copy has no use for: every one that
78
+ * `Versions/Current` does not point at.
79
+ *
80
+ * Chrome's updater keeps the version it replaced beside the new one, for the
81
+ * browser that is still running it, and keeps it until that browser is
82
+ * relaunched. Each is some 700 MB of code. A private copy is always started
83
+ * fresh and only ever loads `Current`, yet every one of them was signed when
84
+ * the copy was made and verified again before each reuse: with two updates
85
+ * pending that was 2.1 GB and 20 to 28 seconds before a browser could start,
86
+ * which is longer than a test waits. A framework with no `Current` link is
87
+ * left whole, since nothing says which of its versions is the one in use.
88
+ */
89
+ function supersededFrameworkVersions(directory: string): string[] {
90
+ const result: string[] = []
91
+ for (const entry of readdirSync(directory, { withFileTypes: true })) {
92
+ if (!entry.isDirectory()) continue
93
+ const child = join(directory, entry.name)
94
+ result.push(...supersededFrameworkVersions(child))
95
+ const versions = join(child, 'Versions')
96
+ if (!entry.name.endsWith('.framework') || !existsSync(versions)) continue
97
+ let current: string
98
+ try { current = basename(readlinkSync(join(versions, 'Current'))) }
99
+ catch { continue }
100
+ if (!existsSync(join(versions, current))) continue
101
+ for (const version of readdirSync(versions, { withFileTypes: true })) {
102
+ if (version.isDirectory() && version.name !== current)
103
+ result.push(join(versions, version.name))
104
+ }
105
+ }
106
+ return result
107
+ }
108
+
75
109
  function validSignature(app: string): boolean {
76
110
  return Bun.spawnSync(['codesign', '--verify', '--deep', '--strict', app], { stdout: 'pipe', stderr: 'pipe' }).exitCode === 0
77
111
  }
78
112
 
113
+ /**
114
+ * What a copy looked like when its signature was last verified: every file's
115
+ * path, size and modification time, and every link's target, as one digest.
116
+ *
117
+ * Strict verification reads the whole bundle, about 15 seconds for one Chrome
118
+ * version, and every test process that started a browser paid it again for a
119
+ * copy nothing had touched. A copy whose files are all where they were, the
120
+ * size they were and as old as they were is the copy that was verified, so
121
+ * it is reused without being read again. Anything else, a file rewritten,
122
+ * removed by the temp cleaner or added, changes the digest and the copy is
123
+ * verified in full before it is trusted.
124
+ */
125
+ function bundleFingerprint(app: string): string {
126
+ const lines: string[] = []
127
+ const walk = (directory: string, prefix: string): void => {
128
+ for (const entry of readdirSync(directory, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))) {
129
+ const path = join(directory, entry.name), relative = `${prefix}${entry.name}`
130
+ if (entry.isSymbolicLink()) { lines.push(`${relative}\tlink\t${readlinkSync(path)}`); continue }
131
+ if (entry.isDirectory()) { walk(path, `${relative}/`); continue }
132
+ const stat = lstatSync(path, { bigint: true })
133
+ lines.push(`${relative}\tfile\t${stat.size}\t${stat.mtimeNs}`)
134
+ }
135
+ }
136
+ walk(app, '')
137
+ return createHash('sha256').update(lines.join('\n')).digest('hex')
138
+ }
139
+
140
+ /** Kept beside the copy, never inside it: a file added to a bundle breaks its seal. */
141
+ const stampOf = (app: string): string => `${app}.verified`
142
+ const STAMP_FORMAT = 'stacks-chrome-verified-1'
143
+
144
+ function verifiedUnchanged(app: string): boolean {
145
+ try { return readFileSync(stampOf(app), 'utf8') === `${STAMP_FORMAT}\n${bundleFingerprint(app)}\n` }
146
+ catch { return false }
147
+ }
148
+
149
+ /** Record that `app`, as it is now, passed strict verification. Written whole or not at all. */
150
+ function recordVerified(app: string): void {
151
+ const pending = `${stampOf(app)}.${process.pid}`
152
+ try {
153
+ writeFileSync(pending, `${STAMP_FORMAT}\n${bundleFingerprint(app)}\n`)
154
+ renameSync(pending, stampOf(app))
155
+ }
156
+ catch {
157
+ // The copy is still valid without its stamp; the next run verifies it again.
158
+ rmSync(pending, { force: true })
159
+ }
160
+ }
161
+
162
+ /** A cached copy that may be launched: unchanged since it was verified, or verified now. */
163
+ function trustedCopy(app: string): boolean {
164
+ if (verifiedUnchanged(app)) return true
165
+ if (!validSignature(app)) return false
166
+ recordVerified(app)
167
+ return true
168
+ }
169
+
79
170
  /**
80
171
  * A private clone of a macOS Chrome that GoogleUpdater leaves alone (see the
81
172
  * module docs): `cp -cR`, the `KS*` keys deleted from `Info.plist`, its own
82
173
  * `CFBundleIdentifier`, and a complete ad-hoc signature. Every framework
83
174
  * version shares an explicit designated requirement, as required for a
84
- * multi-version bundle (Apple TN2206). Kept in `directory` (default a
85
- * per-version temp folder) and reused only after strict signature validation.
175
+ * multi-version bundle (Apple TN2206). Framework versions the updater kept
176
+ * for a browser that is still running are left out of the copy, which only
177
+ * ever runs the current one. Kept in `directory` (default a per-version temp
178
+ * folder) and reused only when it is the copy that passed strict signature
179
+ * validation: unchanged since, by every file's size and age, or validated
180
+ * again now.
86
181
  */
87
182
  export function prepareMacChrome(options: { source?: string, directory?: string } = {}): PreparedChrome {
88
183
  if (process.platform !== 'darwin') throw new Error('prepareMacChrome is for macOS; elsewhere launch Chrome as installed')
@@ -99,8 +194,11 @@ export function prepareMacChrome(options: { source?: string, directory?: string
99
194
  .map(entry => join(directory, entry.name)).sort() : []
100
195
  for (const candidate of candidates) {
101
196
  const executable = join(candidate, 'Contents/MacOS', executableName), info = join(candidate, 'Contents/Info.plist')
197
+ // A copy made before superseded versions were left out still verifies,
198
+ // slowly. It is passed over, not removed: a browser may be running it.
102
199
  if (existsSync(executable) && plist(info, 'Print :CFBundleIdentifier').out === PRIVATE_BUNDLE_ID
103
- && plist(info, 'Print :CFBundleShortVersionString').out === version && validSignature(candidate))
200
+ && plist(info, 'Print :CFBundleShortVersionString').out === version
201
+ && supersededFrameworkVersions(candidate).length === 0 && trustedCopy(candidate))
104
202
  return { executable, app: candidate, reused: true }
105
203
  }
106
204
 
@@ -122,6 +220,9 @@ export function prepareMacChrome(options: { source?: string, directory?: string
122
220
  const stagedInfo = join(staged, 'Contents/Info.plist')
123
221
  for (const key of UPDATER_KEYS) plist(stagedInfo, `Delete :${key}`)
124
222
  run(['/usr/libexec/PlistBuddy', '-c', `Set :CFBundleIdentifier ${PRIVATE_BUNDLE_ID}`, stagedInfo], 'Renaming the Chrome bundle')
223
+ // Before signing, so what is left out is neither signed nor sealed into
224
+ // the copy. Only the clone is touched: the installed app keeps them all.
225
+ for (const superseded of supersededFrameworkVersions(staged)) rmSync(superseded, { recursive: true, force: true })
125
226
  const versions = frameworkVersions(staged)
126
227
  // --deep only signs the current framework version. Prepare the code in
127
228
  // every version first, then seal nested frameworks and their parents.
@@ -138,6 +239,8 @@ export function prepareMacChrome(options: { source?: string, directory?: string
138
239
  destination = join(directory, `Chrome-${basename(staging)}.app`)
139
240
  renameSync(staged, destination)
140
241
  }
242
+ // Verified a moment ago, as the staged copy. Moving it changed no file.
243
+ recordVerified(destination)
141
244
  return { executable: join(destination, 'Contents/MacOS', executableName), app: destination, reused: false }
142
245
  }
143
246
  finally {
@@ -320,9 +320,19 @@ expect(pageAuditClean(report)).toBe(true) // no duplicate /api/ requests, failed
320
320
  Chrome's bundle id while an update is pending; to a test that looks like a hung page. When
321
321
  `CHROME_PATH` is not set, `launch()` runs `prepareMacChrome()`: a `cp -cR` clone in the temp
322
322
  directory with the `KS*` updater keys deleted from `Info.plist`, its own `CFBundleIdentifier`,
323
- and `codesign --force --deep --sign -`, reused per Chrome version. `--use-mock-keychain` is what
323
+ and a complete ad-hoc signature, reused per Chrome version. `--use-mock-keychain` is what
324
324
  keeps that clone from hanging on an invisible "Chrome Safe Storage" Keychain prompt at its first
325
325
  http page. Measured: the installed Chrome closed at 32 s, the clone kept navigating past 60 s.
326
+ - **The clone is made once and then reused in about a tenth of a second** (stacksjs/stacks#3302).
327
+ It leaves out the framework versions the updater keeps for a browser that is still running
328
+ (each about 730 MB; only `Versions/Current` is ever loaded), and a copy that passed
329
+ `codesign --verify --deep --strict` is remembered by a digest of every file's path, size and
330
+ modification time in `Chrome.app.verified` beside it. Unchanged, it is not read again; changed
331
+ in any of those, it is verified in full, and an invalid one gets a replacement generation
332
+ while the old bundle is kept. Before this, strict verification ran before every reuse: 19 to
333
+ 28 s with two updates pending, which a test that gives its page 30 s never survived. The first
334
+ browser test after a Chrome update still pays for the copy, about 15 to 30 s once; if that
335
+ test's own deadline is shorter, it fails once and the next browser test finishes the copy.
326
336
  - The package's own real-Chrome tests (`core/testing/tests/browser.test.ts`) run wherever a Chrome
327
337
  is found, except in CI unless `STACKS_BROWSER_TESTS=1`; `STACKS_BROWSER_TESTS=0` skips them locally.
328
338
 
@@ -2,7 +2,7 @@
2
2
  "publisher": "Stacks",
3
3
  "name": "vscode-stacks",
4
4
  "displayName": "Stacks",
5
- "version": "0.75.572",
5
+ "version": "0.75.573",
6
6
  "description": "Stacks, built into VS Code: stx templates, pickier linting and formatting, encrypted env files, the dev server preview and every buddy command. No other extensions required.",
7
7
  "license": "MIT",
8
8
  "funding": "https://github.com/sponsors/chrisbbreuer",
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/defaults",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.572",
5
+ "version": "0.75.573",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/stacksjs/stacks.git",
@@ -45,10 +45,10 @@
45
45
  "dependencies": {
46
46
  "@iconify-json/f7": "^1.2.2",
47
47
  "@iconify-json/hugeicons": "^1.2.35",
48
- "@stacksjs/mobile": "0.75.572",
48
+ "@stacksjs/mobile": "0.75.573",
49
49
  "@stacksjs/sanitizer": "^0.2.864",
50
50
  "@stacksjs/stx": "^0.2.864",
51
- "@stacksjs/strings": "0.75.572",
51
+ "@stacksjs/strings": "0.75.573",
52
52
  "@xterm/addon-fit": "^0.11.0",
53
53
  "@xterm/xterm": "^6.0.0",
54
54
  "ts-qr-codes": "^0.1.8"