-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! module: implement NODE_COMPILE_CACHE for automatic on-disk cod…
…e caching
- Loading branch information
1 parent
f7806b3
commit b45062b
Showing
6 changed files
with
264 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
// This tests NODE_COMPILE_CACHE works. | ||
|
||
require('../common'); | ||
const { spawnSyncAndExit } = require('../common/child_process'); | ||
const fixtures = require('../common/fixtures'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
{ | ||
// Test that it throws if the script fails to parse, and no cache is created. | ||
tmpdir.refresh(); | ||
const dir = tmpdir.resolve('.compile_cache_dir'); | ||
|
||
spawnSyncAndExit( | ||
process.execPath, | ||
[fixtures.path('syntax', 'bad_syntax.js')], | ||
{ | ||
env: { | ||
...process.env, | ||
NODE_DEBUG_NATIVE: 'COMPILE_CACHE', | ||
NODE_COMPILE_CACHE: dir | ||
}, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
status: 1, | ||
stderr: /skip .*bad_syntax\.js because the cache was not initialized/, | ||
}); | ||
|
||
const cacheDir = fs.readdirSync(dir); | ||
assert.strictEqual(cacheDir.length, 1); | ||
const entries = fs.readdirSync(path.join(dir, cacheDir[0])); | ||
assert.strictEqual(entries.length, 0); | ||
|
||
spawnSyncAndExit( | ||
process.execPath, | ||
[fixtures.path('syntax', 'bad_syntax.mjs')], | ||
{ | ||
env: { | ||
...process.env, | ||
NODE_DEBUG_NATIVE: 'COMPILE_CACHE', | ||
NODE_COMPILE_CACHE: dir | ||
}, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
status: 1, | ||
stderr: /skip .*bad_syntax\.mjs because the cache was not initialized/, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
// This tests NODE_COMPILE_CACHE works in existing directory. | ||
|
||
require('../common'); | ||
const { spawnSyncAndAssert } = require('../common/child_process'); | ||
const assert = require('assert'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fixtures = require('../common/fixtures'); | ||
const fs = require('fs'); | ||
|
||
function testAllowed(readDir, writeDir, envDir) { | ||
console.log(readDir, writeDir, envDir); // Logging for debugging. | ||
|
||
tmpdir.refresh(); | ||
const dummyDir = tmpdir.resolve('dummy'); | ||
fs.mkdirSync(dummyDir); | ||
const script = tmpdir.resolve(dummyDir, 'empty.js'); | ||
fs.copyFileSync(fixtures.path('empty.js'), script); | ||
// If the directory doesn't exist, permission will just be disallowed. | ||
fs.mkdirSync(tmpdir.resolve(envDir)); | ||
|
||
spawnSyncAndAssert( | ||
process.execPath, | ||
[ | ||
'--experimental-permission', | ||
`--allow-fs-read=${dummyDir}`, | ||
`--allow-fs-read=${readDir}`, | ||
`--allow-fs-write=${writeDir}`, | ||
script, | ||
], | ||
{ | ||
env: { | ||
...process.env, | ||
NODE_DEBUG_NATIVE: 'COMPILE_CACHE', | ||
NODE_COMPILE_CACHE: `${envDir}` | ||
}, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
stderr(output) { | ||
assert.match(output, /writing cache for .*empty\.js.*success/); | ||
return true; | ||
} | ||
}); | ||
|
||
spawnSyncAndAssert( | ||
process.execPath, | ||
[ | ||
'--experimental-permission', | ||
`--allow-fs-read=${dummyDir}`, | ||
`--allow-fs-read=${readDir}`, | ||
`--allow-fs-write=${writeDir}`, | ||
script, | ||
], | ||
{ | ||
env: { | ||
...process.env, | ||
NODE_DEBUG_NATIVE: 'COMPILE_CACHE', | ||
NODE_COMPILE_CACHE: `${envDir}` | ||
}, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
stderr(output) { | ||
assert.match(output, /cache for .*empty\.js was accepted/); | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
{ | ||
testAllowed(tmpdir.resolve('.compile_cache'), tmpdir.resolve('.compile_cache'), '.compile_cache'); | ||
testAllowed(tmpdir.resolve('.compile_cache'), tmpdir.resolve('.compile_cache'), tmpdir.resolve('.compile_cache')); | ||
testAllowed('*', '*', '.compile_cache'); | ||
testAllowed('*', tmpdir.resolve('.compile_cache'), '.compile_cache'); | ||
testAllowed(tmpdir.resolve('.compile_cache'), '*', '.compile_cache'); | ||
} |
100 changes: 100 additions & 0 deletions
100
test/parallel/test-compile-cache-permission-disallowed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict'; | ||
|
||
// This tests NODE_COMPILE_CACHE works in existing directory. | ||
|
||
require('../common'); | ||
const { spawnSyncAndAssert } = require('../common/child_process'); | ||
const assert = require('assert'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fixtures = require('../common/fixtures'); | ||
const fs = require('fs'); | ||
|
||
function testDisallowed(dummyDir, cacheDirInPermission, cacheDirInEnv) { | ||
console.log(dummyDir, cacheDirInPermission, cacheDirInEnv); // Logging for debugging. | ||
|
||
tmpdir.refresh(); | ||
const script = tmpdir.resolve(dummyDir, 'empty.js'); | ||
fs.mkdirSync(tmpdir.resolve(dummyDir)); | ||
fs.copyFileSync(fixtures.path('empty.js'), script); | ||
// If the directory doesn't exist, permission will just be disallowed. | ||
if (cacheDirInPermission !== '*') { | ||
fs.mkdirSync(tmpdir.resolve(cacheDirInPermission)); | ||
} | ||
|
||
spawnSyncAndAssert( | ||
process.execPath, | ||
[ | ||
'--experimental-permission', | ||
`--allow-fs-read=${dummyDir}`, // No read or write permission for cache dir. | ||
`--allow-fs-write=${dummyDir}`, | ||
script, | ||
], | ||
{ | ||
env: { | ||
...process.env, | ||
NODE_DEBUG_NATIVE: 'COMPILE_CACHE', | ||
NODE_COMPILE_CACHE: `${cacheDirInEnv}` | ||
}, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
stderr(output) { | ||
assert.match(output, /skipping cache because write permission for .* is not granted/); | ||
return true; | ||
} | ||
}); | ||
|
||
spawnSyncAndAssert( | ||
process.execPath, | ||
[ | ||
'--experimental-permission', | ||
`--allow-fs-read=${dummyDir}`, | ||
`--allow-fs-read=${cacheDirInPermission}`, // Read-only | ||
`--allow-fs-write=${dummyDir}`, | ||
script, | ||
], | ||
{ | ||
env: { | ||
...process.env, | ||
NODE_DEBUG_NATIVE: 'COMPILE_CACHE', | ||
NODE_COMPILE_CACHE: `${cacheDirInEnv}` | ||
}, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
stderr(output) { | ||
assert.match(output, /skipping cache because write permission for .* is not granted/); | ||
return true; | ||
} | ||
}); | ||
|
||
spawnSyncAndAssert( | ||
process.execPath, | ||
[ | ||
'--experimental-permission', | ||
`--allow-fs-read=${dummyDir}`, | ||
`--allow-fs-write=${cacheDirInPermission}`, // Write-only | ||
script, | ||
], | ||
{ | ||
env: { | ||
...process.env, | ||
NODE_DEBUG_NATIVE: 'COMPILE_CACHE', | ||
NODE_COMPILE_CACHE: `${cacheDirInEnv}` | ||
}, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
stderr(output) { | ||
assert.match(output, /skipping cache because read permission for .* is not granted/); | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
{ | ||
testDisallowed(tmpdir.resolve('dummy'), tmpdir.resolve('.compile_cache') + '/', '.compile_cache'); | ||
testDisallowed(tmpdir.resolve('dummy'), tmpdir.resolve('.compile_cache/') + '/', tmpdir.resolve('.compile_cache')); | ||
testDisallowed(tmpdir.resolve('dummy'), '*', '.compile_cache'); | ||
testDisallowed(tmpdir.resolve('dummy'), '*', tmpdir.resolve('.compile_cache')); | ||
} |