-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract getFlag fmap detection to a tested module
- Loading branch information
Showing
3 changed files
with
68 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Get the appropriate flag to use for creating files | ||
// We use fmap on Windows platforms for files less than | ||
// 512kb. This is a fairly low limit, but avoids making | ||
// things slower in some cases. Since most of what this | ||
// library is used for is extracting tarballs of many | ||
// relatively small files in npm packages and the like, | ||
// it can be a big boost on Windows platforms. | ||
// Only supported in Node v12.9.0 and above. | ||
This comment has been minimized.
Sorry, something went wrong. |
||
const platform = process.env.__FAKE_PLATFORM__ || process.platform | ||
const isWindows = platform === 'win32' | ||
const fs = require('fs') | ||
const { O_CREAT, O_TRUNC, O_WRONLY, UV_FS_O_FILEMAP = 0 } = fs.constants | ||
const fMapEnabled = isWindows && !!UV_FS_O_FILEMAP | ||
const fMapLimit = 512 * 1024 | ||
const fMapFlag = UV_FS_O_FILEMAP | O_TRUNC | O_CREAT | O_WRONLY | ||
module.exports = size => (fMapEnabled && size < fMapLimit) ? fMapFlag : 'w' |
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,51 @@ | ||
const t = require('tap') | ||
|
||
// run three scenarios | ||
// unix (no fmap) | ||
// win32 (without fmap support) | ||
// win32 (with fmap support) | ||
|
||
const fs = require('fs') | ||
const hasFmap = !!fs.constants.UV_FS_O_FILEMAP | ||
const platform = process.platform | ||
|
||
switch (process.argv[2]) { | ||
case 'win32-fmap': { | ||
if (!hasFmap) | ||
fs.constants.UV_FS_O_FILEMAP = 0x20000000 | ||
const { O_CREAT, O_TRUNC, O_WRONLY, UV_FS_O_FILEMAP } = fs.constants | ||
if (platform !== 'win32') | ||
process.env.__FAKE_PLATFORM__ = 'win32' | ||
const getFlag = require('../lib/get-write-flag.js') | ||
t.equal(getFlag(1), UV_FS_O_FILEMAP | O_TRUNC | O_CREAT | O_WRONLY) | ||
t.equal(getFlag(512 * 1024 + 1), 'w') | ||
break | ||
} | ||
|
||
case 'win32-nofmap': { | ||
if (hasFmap) | ||
fs.constants.UV_FS_O_FILEMAP = 0 | ||
if (platform !== 'win32') | ||
process.env.__FAKE_PLATFORM__ = 'win32' | ||
const getFlag = require('../lib/get-write-flag.js') | ||
t.equal(getFlag(1), 'w') | ||
t.equal(getFlag(512 * 1024 + 1), 'w') | ||
break | ||
} | ||
|
||
case 'unix': { | ||
if (platform === 'win32') | ||
process.env.__FAKE_PLATFORM__ = 'darwin' | ||
const getFlag = require('../lib/get-write-flag.js') | ||
t.equal(getFlag(1), 'w') | ||
t.equal(getFlag(512 * 1024 + 1), 'w') | ||
break | ||
} | ||
|
||
default: { | ||
const node = process.execPath | ||
t.spawn(node, [__filename, 'win32-fmap']) | ||
t.spawn(node, [__filename, 'win32-nofmap']) | ||
t.spawn(node, [__filename, 'unix']) | ||
} | ||
} |
Actually, v12.11.0. The constant
UV_FS_O_FILEMAP
was not exposed infs.constants
before so this won't work.