• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const fixtures = require('../common/fixtures');
5const assert = require('assert');
6const path = require('path');
7const fs = require('fs');
8const os = require('os');
9const URL = require('url').URL;
10
11function pathToFileURL(p) {
12  if (!path.isAbsolute(p))
13    throw new Error('Path must be absolute');
14  if (common.isWindows && p.startsWith('\\\\'))
15    p = p.slice(2);
16  return new URL(`file://${p}`);
17}
18
19const p = path.resolve(fixtures.fixturesDir, 'a.js');
20const url = pathToFileURL(p);
21
22assert(url instanceof URL);
23
24// Check that we can pass in a URL object successfully
25fs.readFile(url, common.mustCall((err, data) => {
26  assert.ifError(err);
27  assert(Buffer.isBuffer(data));
28}));
29
30// Check that using a non file:// URL reports an error
31const httpUrl = new URL('http://example.org');
32
33assert.throws(
34  () => {
35    fs.readFile(httpUrl, common.mustNotCall());
36  },
37  {
38    code: 'ERR_INVALID_URL_SCHEME',
39    name: 'TypeError',
40    message: 'The URL must be of scheme file'
41  });
42
43// pct-encoded characters in the path will be decoded and checked
44if (common.isWindows) {
45  // Encoded back and forward slashes are not permitted on windows
46  ['%2f', '%2F', '%5c', '%5C'].forEach((i) => {
47    assert.throws(
48      () => {
49        fs.readFile(new URL(`file:///c:/tmp/${i}`), common.mustNotCall());
50      },
51      {
52        code: 'ERR_INVALID_FILE_URL_PATH',
53        name: 'TypeError',
54        message: 'File URL path must not include encoded \\ or / characters'
55      }
56    );
57  });
58  assert.throws(
59    () => {
60      fs.readFile(new URL('file:///c:/tmp/%00test'), common.mustNotCall());
61    },
62    {
63      code: 'ERR_INVALID_ARG_VALUE',
64      name: 'TypeError',
65      message: 'The argument \'path\' must be a string or Uint8Array without ' +
66               'null bytes. Received \'c:\\\\tmp\\\\\\u0000test\''
67    }
68  );
69} else {
70  // Encoded forward slashes are not permitted on other platforms
71  ['%2f', '%2F'].forEach((i) => {
72    assert.throws(
73      () => {
74        fs.readFile(new URL(`file:///c:/tmp/${i}`), common.mustNotCall());
75      },
76      {
77        code: 'ERR_INVALID_FILE_URL_PATH',
78        name: 'TypeError',
79        message: 'File URL path must not include encoded / characters'
80      });
81  });
82  assert.throws(
83    () => {
84      fs.readFile(new URL('file://hostname/a/b/c'), common.mustNotCall());
85    },
86    {
87      code: 'ERR_INVALID_FILE_URL_HOST',
88      name: 'TypeError',
89      message: `File URL host must be "localhost" or empty on ${os.platform()}`
90    }
91  );
92  assert.throws(
93    () => {
94      fs.readFile(new URL('file:///tmp/%00test'), common.mustNotCall());
95    },
96    {
97      code: 'ERR_INVALID_ARG_VALUE',
98      name: 'TypeError',
99      message: "The argument 'path' must be a string or Uint8Array without " +
100               "null bytes. Received '/tmp/\\u0000test'"
101    }
102  );
103}
104