diff --git a/lib/internal/url.js b/lib/internal/url.js index 0749e07d6e7677..9f2beab9ca20c4 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -19,7 +19,6 @@ const { ReflectApply, ReflectGetOwnPropertyDescriptor, ReflectOwnKeys, - RegExpPrototypeExec, String, StringPrototypeCharCodeAt, StringPrototypeIncludes, @@ -40,7 +39,12 @@ const { isHexTable } = require('internal/querystring'); -const { getConstructorOf, removeColors } = require('internal/util'); +const { + getConstructorOf, + removeColors, + toUSVString +} = require('internal/util'); + const { ERR_ARG_NOT_ITERABLE, ERR_INVALID_ARG_TYPE, @@ -79,7 +83,6 @@ const { domainToASCII: _domainToASCII, domainToUnicode: _domainToUnicode, encodeAuth, - toUSVString: _toUSVString, parse, setURLConstructor, URL_FLAGS_CANNOT_BE_BASE, @@ -113,18 +116,6 @@ const IteratorPrototype = ObjectGetPrototypeOf( ObjectGetPrototypeOf([][SymbolIterator]()) ); -const unpairedSurrogateRe = - /(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/; -function toUSVString(val) { - const str = `${val}`; - // As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are - // slower than `unpairedSurrogateRe.exec()`. - const match = RegExpPrototypeExec(unpairedSurrogateRe, str); - if (!match) - return str; - return _toUSVString(str, match.index); -} - // Refs: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-opaque const kOpaqueOrigin = 'null'; diff --git a/lib/internal/util.js b/lib/internal/util.js index 9158fc8e52431e..126ba41aa4f0d3 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -17,6 +17,7 @@ const { Promise, ReflectApply, ReflectConstruct, + RegExpPrototypeExec, RegExpPrototypeTest, SafeMap, SafeSet, @@ -27,6 +28,10 @@ const { SymbolFor, } = primordials; +const { + toUSVString: _toUSVString, +} = internalBinding('url'); + const { hideStackFrames, codes: { @@ -53,6 +58,18 @@ const experimentalWarnings = new SafeSet(); const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex +const unpairedSurrogateRe = + /(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/; +function toUSVString(val) { + const str = `${val}`; + // As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are + // slower than `unpairedSurrogateRe.exec()`. + const match = RegExpPrototypeExec(unpairedSurrogateRe, str); + if (!match) + return str; + return _toUSVString(str, match.index); +} + let uvBinding; function lazyUv() { @@ -487,6 +504,7 @@ module.exports = { sleep, spliceOne, structuredClone, + toUSVString, removeColors, // Symbol used to customize promisify conversion diff --git a/test/parallel/test-util.js b/test/parallel/test-util.js index 204b2bca5b9f3b..06b0347de1f36f 100644 --- a/test/parallel/test-util.js +++ b/test/parallel/test-util.js @@ -148,6 +148,8 @@ assert.strictEqual(util.isFunction(function() {}), true); assert.strictEqual(util.isFunction(), false); assert.strictEqual(util.isFunction('string'), false); +assert.strictEqual(util.toUSVString('string'), 'string'); + { assert.strictEqual(util.types.isNativeError(new Error()), true); assert.strictEqual(util.types.isNativeError(new TypeError()), true);