Skip to content

Commit e04a10b

Browse files
committed
util: expose toUSVString
Expose toUSVString to it can be used by user libraries. Refs: nodejs/undici#986
1 parent 4832d1c commit e04a10b

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

lib/internal/url.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
ReflectApply,
2020
ReflectGetOwnPropertyDescriptor,
2121
ReflectOwnKeys,
22-
RegExpPrototypeExec,
2322
String,
2423
StringPrototypeCharCodeAt,
2524
StringPrototypeIncludes,
@@ -40,7 +39,12 @@ const {
4039
isHexTable
4140
} = require('internal/querystring');
4241

43-
const { getConstructorOf, removeColors } = require('internal/util');
42+
const {
43+
getConstructorOf,
44+
removeColors,
45+
toUSVString
46+
} = require('internal/util');
47+
4448
const {
4549
ERR_ARG_NOT_ITERABLE,
4650
ERR_INVALID_ARG_TYPE,
@@ -79,7 +83,6 @@ const {
7983
domainToASCII: _domainToASCII,
8084
domainToUnicode: _domainToUnicode,
8185
encodeAuth,
82-
toUSVString: _toUSVString,
8386
parse,
8487
setURLConstructor,
8588
URL_FLAGS_CANNOT_BE_BASE,
@@ -113,18 +116,6 @@ const IteratorPrototype = ObjectGetPrototypeOf(
113116
ObjectGetPrototypeOf([][SymbolIterator]())
114117
);
115118

116-
const unpairedSurrogateRe =
117-
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
118-
function toUSVString(val) {
119-
const str = `${val}`;
120-
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
121-
// slower than `unpairedSurrogateRe.exec()`.
122-
const match = RegExpPrototypeExec(unpairedSurrogateRe, str);
123-
if (!match)
124-
return str;
125-
return _toUSVString(str, match.index);
126-
}
127-
128119
// Refs: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-opaque
129120
const kOpaqueOrigin = 'null';
130121

lib/internal/util.js

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
Promise,
1818
ReflectApply,
1919
ReflectConstruct,
20+
RegExpPrototypeExec,
2021
RegExpPrototypeTest,
2122
SafeMap,
2223
SafeSet,
@@ -27,6 +28,10 @@ const {
2728
SymbolFor,
2829
} = primordials;
2930

31+
const {
32+
toUSVString: _toUSVString,
33+
} = internalBinding('url');
34+
3035
const {
3136
hideStackFrames,
3237
codes: {
@@ -53,6 +58,18 @@ const experimentalWarnings = new SafeSet();
5358

5459
const colorRegExp = /\u001b\[\d\d?m/g; // eslint-disable-line no-control-regex
5560

61+
const unpairedSurrogateRe =
62+
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/;
63+
function toUSVString(val) {
64+
const str = `${val}`;
65+
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
66+
// slower than `unpairedSurrogateRe.exec()`.
67+
const match = RegExpPrototypeExec(unpairedSurrogateRe, str);
68+
if (!match)
69+
return str;
70+
return _toUSVString(str, match.index);
71+
}
72+
5673
let uvBinding;
5774

5875
function lazyUv() {
@@ -487,6 +504,7 @@ module.exports = {
487504
sleep,
488505
spliceOne,
489506
structuredClone,
507+
toUSVString,
490508
removeColors,
491509

492510
// Symbol used to customize promisify conversion

lib/util.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ const {
7272
deprecate,
7373
getSystemErrorMap,
7474
getSystemErrorName: internalErrorName,
75-
promisify
75+
promisify,
76+
toUSVString
7677
} = require('internal/util');
7778

7879
let internalDeepEqual;
@@ -368,6 +369,7 @@ module.exports = {
368369
isPrimitive,
369370
log,
370371
promisify,
372+
toUSVString,
371373
TextDecoder,
372374
TextEncoder,
373375
types

test/parallel/test-util.js

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ assert.strictEqual(util.isFunction(function() {}), true);
148148
assert.strictEqual(util.isFunction(), false);
149149
assert.strictEqual(util.isFunction('string'), false);
150150

151+
assert.strictEqual(util.toUSVString('string'), 'string');
152+
151153
{
152154
assert.strictEqual(util.types.isNativeError(new Error()), true);
153155
assert.strictEqual(util.types.isNativeError(new TypeError()), true);

0 commit comments

Comments
 (0)