Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: lazy load path
  • Loading branch information
himself65 committed Apr 14, 2024
commit 7a62d4105e7ee48b0646e5878f5248e09a01e8f8
13 changes: 10 additions & 3 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ const {
CHAR_PERCENT,
CHAR_PLUS,
} = require('internal/constants');
const path = require('path');

const {
validateFunction,
} = require('internal/validators');

const querystring = require('querystring');

let lazyLoadPath;

function lazyLoadPathSync() {
lazyLoadPath ??= require('path');
return lazyLoadPath;
}

const { platform } = process;
const isWindows = platform === 'win32';

Expand Down Expand Up @@ -1529,13 +1535,14 @@ function pathToFileURL(filepath) {
RegExpPrototypeSymbolReplace(backslashRegEx, StringPrototypeSlice(filepath, hostnameEndIndex), '/'));
return outURL;
}
let resolved = path.resolve(filepath);
const { resolve, sep } = lazyLoadPathSync();
let resolved = resolve(filepath);
// path.resolve strips trailing slashes so we must add them back
const filePathLast = StringPrototypeCharCodeAt(filepath,
filepath.length - 1);
if ((filePathLast === CHAR_FORWARD_SLASH ||
(isWindows && filePathLast === CHAR_BACKWARD_SLASH)) &&
resolved[resolved.length - 1] !== path.sep)
resolved[resolved.length - 1] !== sep)
resolved += '/';

// Call encodePathChars first to avoid encoding % again for ? and #.
Expand Down