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