2017-01-11 07:02:40 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
2017-10-06 09:36:35 -07:00
|
|
|
const fixtures = require('../common/fixtures');
|
2017-01-11 07:02:40 -08:00
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
2023-09-06 06:07:21 +09:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
2025-01-22 14:19:38 -08:00
|
|
|
const { isMainThread } = require('worker_threads');
|
|
|
|
|
2023-09-06 06:07:21 +09:00
|
|
|
tmpdir.refresh();
|
2017-01-11 07:02:40 -08:00
|
|
|
|
2023-08-03 23:36:13 +02:00
|
|
|
const url = fixtures.fileURL('a.js');
|
2017-01-11 07:02:40 -08:00
|
|
|
|
|
|
|
assert(url instanceof URL);
|
|
|
|
|
|
|
|
// Check that we can pass in a URL object successfully
|
2020-09-06 22:27:07 +02:00
|
|
|
fs.readFile(url, common.mustSucceed((data) => {
|
2017-01-11 07:02:40 -08:00
|
|
|
assert(Buffer.isBuffer(data));
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Check that using a non file:// URL reports an error
|
|
|
|
const httpUrl = new URL('http://example.org');
|
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-01-23 10:23:46 +08:00
|
|
|
() => {
|
|
|
|
fs.readFile(httpUrl, common.mustNotCall());
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_URL_SCHEME',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2018-01-23 10:23:46 +08:00
|
|
|
});
|
2017-01-11 07:02:40 -08:00
|
|
|
|
2018-01-23 10:23:46 +08:00
|
|
|
// pct-encoded characters in the path will be decoded and checked
|
2017-01-11 07:02:40 -08:00
|
|
|
if (common.isWindows) {
|
2018-12-10 13:27:32 +01:00
|
|
|
// Encoded back and forward slashes are not permitted on windows
|
2017-01-11 07:02:40 -08:00
|
|
|
['%2f', '%2F', '%5c', '%5C'].forEach((i) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-01-23 10:23:46 +08:00
|
|
|
() => {
|
|
|
|
fs.readFile(new URL(`file:///c:/tmp/${i}`), common.mustNotCall());
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_FILE_URL_PATH',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2018-01-23 10:23:46 +08:00
|
|
|
}
|
|
|
|
);
|
2017-01-11 07:02:40 -08:00
|
|
|
});
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-01-23 10:23:46 +08:00
|
|
|
() => {
|
|
|
|
fs.readFile(new URL('file:///c:/tmp/%00test'), common.mustNotCall());
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2018-01-23 10:23:46 +08:00
|
|
|
}
|
|
|
|
);
|
2017-01-11 07:02:40 -08:00
|
|
|
} else {
|
2018-12-10 13:27:32 +01:00
|
|
|
// Encoded forward slashes are not permitted on other platforms
|
2017-01-11 07:02:40 -08:00
|
|
|
['%2f', '%2F'].forEach((i) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-01-23 10:23:46 +08:00
|
|
|
() => {
|
|
|
|
fs.readFile(new URL(`file:///c:/tmp/${i}`), common.mustNotCall());
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_FILE_URL_PATH',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2018-01-23 10:23:46 +08:00
|
|
|
});
|
2017-01-11 07:02:40 -08:00
|
|
|
});
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-01-23 10:23:46 +08:00
|
|
|
() => {
|
|
|
|
fs.readFile(new URL('file://hostname/a/b/c'), common.mustNotCall());
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_FILE_URL_HOST',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2018-01-23 10:23:46 +08:00
|
|
|
}
|
|
|
|
);
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(
|
2018-01-23 10:23:46 +08:00
|
|
|
() => {
|
|
|
|
fs.readFile(new URL('file:///tmp/%00test'), common.mustNotCall());
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
2019-12-25 18:02:16 +01:00
|
|
|
name: 'TypeError',
|
2018-01-23 10:23:46 +08:00
|
|
|
}
|
|
|
|
);
|
2017-01-11 07:02:40 -08:00
|
|
|
}
|
2023-09-06 06:07:21 +09:00
|
|
|
|
|
|
|
// Test that strings are interpreted as paths and not as URL
|
|
|
|
// Can't use process.chdir in Workers
|
|
|
|
// Please avoid testing fs.rmdir('file:') or using it as cleanup
|
2025-01-22 14:19:38 -08:00
|
|
|
if (isMainThread && !common.isWindows) {
|
2023-09-06 06:07:21 +09:00
|
|
|
const oldCwd = process.cwd();
|
|
|
|
process.chdir(tmpdir.path);
|
|
|
|
|
|
|
|
for (let slashCount = 0; slashCount < 9; slashCount++) {
|
|
|
|
const slashes = '/'.repeat(slashCount);
|
|
|
|
|
|
|
|
const dirname = `file:${slashes}thisDirectoryWasMadeByFailingNodeJSTestSorry/subdir`;
|
|
|
|
fs.mkdirSync(dirname, { recursive: true });
|
|
|
|
fs.writeFileSync(`${dirname}/file`, `test failed with ${slashCount} slashes`);
|
|
|
|
|
|
|
|
const expected = fs.readFileSync(tmpdir.resolve(dirname, 'file'));
|
|
|
|
const actual = fs.readFileSync(`${dirname}/file`);
|
|
|
|
assert.deepStrictEqual(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.chdir(oldCwd);
|
|
|
|
}
|