mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
A compile cache generated ahead of time and shipped inside an application package should only ever be read: the package may be immutable or covered by an integrity check, and a cache directory that appears at run time would be a surprise. Add readOnly to module.enableCompileCache() and NODE_COMPILE_CACHE_READONLY=1: existing entries are loaded as before, nothing is serialized or persisted, flushCompileCache() is a no-op, and the cache directory is used as found rather than created, so enabling against a missing directory fails instead of making one. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: https://github.com/nodejs/node/pull/65302 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk>
66 lines
2.3 KiB
JavaScript
66 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
// This tests module.enableCompileCache({ directory, readOnly: true }): existing
|
|
// entries are read, nothing is written, and a missing directory is not created.
|
|
|
|
const common = require('../common');
|
|
const { spawnSyncAndAssert } = require('../common/child_process');
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const tmpdir = require('../common/tmpdir');
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
tmpdir.refresh();
|
|
const wrapper = fixtures.path('compile-cache-wrapper-options.js');
|
|
const target = path.join(tmpdir.path, 'target.js');
|
|
fs.writeFileSync(target, 'module.exports = 1;');
|
|
const other = path.join(tmpdir.path, 'other.js');
|
|
fs.writeFileSync(other, 'module.exports = 2;');
|
|
const directory = path.join(tmpdir.path, 'cache');
|
|
const list = () => fs.readdirSync(directory, { recursive: true }).sort();
|
|
const run = (options, extraEnv, requires, check) => spawnSyncAndAssert(
|
|
process.execPath,
|
|
[...requires.flatMap((r) => ['-r', r]), target],
|
|
{
|
|
env: {
|
|
...process.env,
|
|
NODE_DEBUG_NATIVE: 'COMPILE_CACHE',
|
|
NODE_TEST_COMPILE_CACHE_OPTIONS: JSON.stringify(options),
|
|
...extraEnv,
|
|
},
|
|
},
|
|
check);
|
|
|
|
// Read-only against a directory that does not exist: enabling fails and
|
|
// nothing is created.
|
|
run({ directory, readOnly: true }, {}, [wrapper], {
|
|
stderr: /read-only cache directory .*\.\.\.not found/,
|
|
});
|
|
assert(!fs.existsSync(directory));
|
|
|
|
// A normal run generates the cache for target.js.
|
|
run({ directory }, {}, [wrapper], {
|
|
stderr: /writing cache for .*target\.js.*success/,
|
|
});
|
|
const generated = list();
|
|
assert.notStrictEqual(generated.length, 0);
|
|
|
|
// Read-only against it: target.js's entry is accepted, other.js is compiled
|
|
// but not written, and persistence is skipped.
|
|
run({ directory, readOnly: true }, {}, [wrapper, other], {
|
|
stderr: common.mustCall((output) => {
|
|
assert.match(output, /cache for .*target\.js was accepted/);
|
|
assert.match(output, /read-only, skipping persistence/);
|
|
assert.doesNotMatch(output, /writing cache for/);
|
|
return true;
|
|
}),
|
|
});
|
|
assert.deepStrictEqual(list(), generated);
|
|
|
|
// The environment variable form.
|
|
run({ directory }, { NODE_COMPILE_CACHE_READONLY: '1' }, [wrapper, other], {
|
|
stderr: /read-only, skipping persistence/,
|
|
});
|
|
assert.deepStrictEqual(list(), generated);
|