|
9 | 9 | SandboxedImageOptimizerWorker, |
10 | 10 | } from 'next/dist/server/image-optimizer/sandbox-worker' |
11 | 11 | import type { ImageOptimizerOperation } from 'next/dist/server/image-optimizer/operation' |
| 12 | +import { resolveImageOptimizerWorker } from 'next/dist/server/image-optimizer/sandbox-support' |
12 | 13 | import { |
13 | 14 | defaultConfig, |
14 | 15 | getNextConfigRuntime, |
@@ -79,6 +80,65 @@ describe('SandboxedImageOptimizerWorker', () => { |
79 | 80 | worker = undefined |
80 | 81 | }) |
81 | 82 |
|
| 83 | + it('enables subprocesses by default when sandboxing is supported', async () => { |
| 84 | + await expect( |
| 85 | + resolveImageOptimizerWorker(undefined, async () => undefined) |
| 86 | + ).resolves.toBe(true) |
| 87 | + }) |
| 88 | + |
| 89 | + it('disables subprocesses without probing when explicitly opted out', async () => { |
| 90 | + const check = jest.fn() |
| 91 | + await expect(resolveImageOptimizerWorker(false, check)).resolves.toBe(false) |
| 92 | + expect(check).not.toHaveBeenCalled() |
| 93 | + }) |
| 94 | + |
| 95 | + it.each([ |
| 96 | + 'unsupported platform win32', |
| 97 | + 'bubblewrap not installed', |
| 98 | + 'sandbox creation denied', |
| 99 | + ])('only auto mode falls back when %s', async (reason) => { |
| 100 | + const check = async () => reason |
| 101 | + await expect(resolveImageOptimizerWorker(undefined, check)).resolves.toBe( |
| 102 | + false |
| 103 | + ) |
| 104 | + await expect(resolveImageOptimizerWorker(true, check)).rejects.toThrow( |
| 105 | + `experimental.imgOptWorker cannot be enabled: ${reason}` |
| 106 | + ) |
| 107 | + }) |
| 108 | + |
| 109 | + it('enables explicitly requested subprocesses when supported', async () => { |
| 110 | + await expect( |
| 111 | + resolveImageOptimizerWorker(true, async () => undefined) |
| 112 | + ).resolves.toBe(true) |
| 113 | + }) |
| 114 | + |
| 115 | + it.each(['auto', 'false'])( |
| 116 | + 'starts with subprocesses disabled on an unsupported host (%s)', |
| 117 | + async (requested) => { |
| 118 | + const { stdout } = await execFileAsync( |
| 119 | + process.execPath, |
| 120 | + [join(__dirname, 'fixtures/sandbox-worker-unsupported.js'), requested], |
| 121 | + { timeout: 10000 } |
| 122 | + ) |
| 123 | + expect(stdout).toContain('WORKER_ENABLED=false') |
| 124 | + } |
| 125 | + ) |
| 126 | + |
| 127 | + it('fails config loading when subprocesses are explicitly enabled on an unsupported host', async () => { |
| 128 | + await expect( |
| 129 | + execFileAsync( |
| 130 | + process.execPath, |
| 131 | + [join(__dirname, 'fixtures/sandbox-worker-unsupported.js'), 'true'], |
| 132 | + { timeout: 10000 } |
| 133 | + ) |
| 134 | + ).rejects.toMatchObject({ |
| 135 | + code: 1, |
| 136 | + stderr: expect.stringContaining( |
| 137 | + 'experimental.imgOptWorker cannot be enabled: unsupported platform win32' |
| 138 | + ), |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
82 | 142 | it('shares shutdown listeners and removes them when workers close', async () => { |
83 | 143 | const listenerCounts = () => [ |
84 | 144 | process.listeners('SIGINT').length, |
@@ -175,6 +235,25 @@ describe('SandboxedImageOptimizerWorker', () => { |
175 | 235 | expect(config.filesystem.denyRead).toEqual(['/']) |
176 | 236 | }) |
177 | 237 |
|
| 238 | + it('only grants the source checkout allowance during local development', () => { |
| 239 | + const previous = process.env.NEXT_PRIVATE_LOCAL_DEV |
| 240 | + const dist = join(__dirname, '../../../packages/next/dist') |
| 241 | + const packageJson = join(dist, '../package.json') |
| 242 | + try { |
| 243 | + delete process.env.NEXT_PRIVATE_LOCAL_DEV |
| 244 | + const normal = getImageOptimizerSandboxConfig(fixtureWorker) |
| 245 | + expect(normal.filesystem.allowRead).not.toContain(dist) |
| 246 | + expect(normal.filesystem.allowRead).not.toContain(packageJson) |
| 247 | + process.env.NEXT_PRIVATE_LOCAL_DEV = '1' |
| 248 | + const local = getImageOptimizerSandboxConfig(fixtureWorker) |
| 249 | + expect(local.filesystem.allowRead).toContain(dist) |
| 250 | + expect(local.filesystem.allowRead).toContain(packageJson) |
| 251 | + } finally { |
| 252 | + if (previous === undefined) delete process.env.NEXT_PRIVATE_LOCAL_DEV |
| 253 | + else process.env.NEXT_PRIVATE_LOCAL_DEV = previous |
| 254 | + } |
| 255 | + }) |
| 256 | + |
178 | 257 | it.each(['relative/path', '/tmp/*', '/tmp/[abc]', '/tmp/file?'])( |
179 | 258 | 'rejects ambiguous read allowance %s', |
180 | 259 | (path) => { |
@@ -276,7 +355,8 @@ describe('SandboxedImageOptimizerWorker', () => { |
276 | 355 | read: expect.stringMatching(/^read-blocked:/), |
277 | 356 | readHome: expect.stringMatching(/^read-blocked:/), |
278 | 357 | readApplication: expect.stringMatching(/^read-blocked:/), |
279 | | - readDependency: 'read-allowed', |
| 358 | + readDependency: expect.stringMatching(/^read-blocked:/), |
| 359 | + readCheckout: expect.stringMatching(/^read-blocked:/), |
280 | 360 | libraries: Array(6).fill(expect.stringMatching(/^read-blocked:/)), |
281 | 361 | customRead: 'read-allowed', |
282 | 362 | customOutsideRead: expect.stringMatching(/^read-blocked:/), |
|
0 commit comments