summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Nagy <ronagy@icloud.com>2021-08-19 21:40:18 +0200
committerMichaƫl Zasso <targos@protonmail.com>2021-08-22 08:48:42 +0200
commit66400374de6f13c4f28682f13a7c0121ceb1cfbf (patch)
tree97a79363204f34fb69702e6226d5fa900537c43d
parent79079ea01b2c292d866bfeb8e3407e296f7c60dd (diff)
downloadnode-new-66400374de6f13c4f28682f13a7c0121ceb1cfbf.tar.gz
util: expose toUSVString
Expose toUSVString so it can be used by user libraries. PR-URL: https://github.com/nodejs/node/pull/39814 Refs: https://github.com/nodejs/undici/pull/986 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
-rw-r--r--doc/api/util.md12
-rw-r--r--lib/internal/url.js21
-rw-r--r--lib/internal/util.js18
-rw-r--r--lib/util.js4
-rw-r--r--test/parallel/test-util.js2
5 files changed, 41 insertions, 16 deletions
diff --git a/doc/api/util.md b/doc/api/util.md
index 41522bf888..8bcc9ebda1 100644
--- a/doc/api/util.md
+++ b/doc/api/util.md
@@ -2490,6 +2490,18 @@ const util = require('util');
util.log('Timestamped message.');
```
+
+### `util.toUSVString(string)`
+<!-- YAML
+added: REPLACEME
+-->
+
+* `string` {string}
+
+Returns the `string` after replacing any surrogate code points
+(or equivalently, any unpaired surrogate code units) with the
+Unicode "replacement character" U+FFFD.
+
[Common System Errors]: errors.md#errors_common_system_errors
[Custom inspection functions on objects]: #util_custom_inspection_functions_on_objects
[Custom promisified functions]: #util_custom_promisified_functions
diff --git a/lib/internal/url.js b/lib/internal/url.js
index 378248b948..4d9e978f83 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 {
codes: {
ERR_ARG_NOT_ITERABLE,
@@ -82,7 +86,6 @@ const {
domainToASCII: _domainToASCII,
domainToUnicode: _domainToUnicode,
encodeAuth,
- toUSVString: _toUSVString,
parse,
setURLConstructor,
URL_FLAGS_CANNOT_BE_BASE,
@@ -139,18 +142,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 acaffb9ff2..ba98a41e40 100644
--- a/lib/internal/util.js
+++ b/lib/internal/util.js
@@ -17,6 +17,7 @@ const {
Promise,
ReflectApply,
ReflectConstruct,
+ RegExpPrototypeExec,
RegExpPrototypeTest,
SafeMap,
SafeSet,
@@ -28,6 +29,10 @@ const {
} = primordials;
const {
+ toUSVString: _toUSVString,
+} = internalBinding('url');
+
+const {
hideStackFrames,
codes: {
ERR_NO_CRYPTO,
@@ -52,6 +57,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() {
@@ -497,6 +514,7 @@ module.exports = {
sleep,
spliceOne,
structuredClone,
+ toUSVString,
removeColors,
// Symbol used to customize promisify conversion
diff --git a/lib/util.js b/lib/util.js
index a8d9a35648..e3f151541e 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -72,7 +72,8 @@ const {
deprecate,
getSystemErrorMap,
getSystemErrorName: internalErrorName,
- promisify
+ promisify,
+ toUSVString,
} = require('internal/util');
let internalDeepEqual;
@@ -368,6 +369,7 @@ module.exports = {
isPrimitive,
log,
promisify,
+ toUSVString,
TextDecoder,
TextEncoder,
types
diff --git a/test/parallel/test-util.js b/test/parallel/test-util.js
index 204b2bca5b..42218be858 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\ud801'), 'string\ufffd');
+
{
assert.strictEqual(util.types.isNativeError(new Error()), true);
assert.strictEqual(util.types.isNativeError(new TypeError()), true);