From 8eb11356dd6a9e66422920dfe3ef289ac282318f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Thu, 19 Aug 2021 21:40:18 +0200 Subject: [PATCH] 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 Reviewed-By: Luigi Pinca --- doc/api/util.md | 12 ++++++++++++ lib/internal/url.js | 21 ++++++--------------- lib/internal/util.js | 18 ++++++++++++++++++ lib/util.js | 4 +++- test/parallel/test-util.js | 2 ++ 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 18733103413be5..d25d0f48817927 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -2468,6 +2468,18 @@ const util = require('util'); util.log('Timestamped message.'); ``` + +### `util.toUSVString(string)` + + +* `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 12402ca237e8fa..e8568a68ed043a 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, @@ -76,7 +80,6 @@ const { domainToASCII: _domainToASCII, domainToUnicode: _domainToUnicode, encodeAuth, - toUSVString: _toUSVString, parse, setURLConstructor, URL_FLAGS_CANNOT_BE_BASE, @@ -110,18 +113,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 be3a4eef644469..174385241ce3b5 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -14,11 +14,16 @@ const { ObjectSetPrototypeOf, Promise, ReflectConstruct, + RegExpPrototypeExec, Set, Symbol, SymbolFor, } = primordials; +const { + toUSVString: _toUSVString, +} = internalBinding('url'); + const { codes: { ERR_INVALID_ARG_TYPE, @@ -44,6 +49,18 @@ const experimentalWarnings = new Set(); 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() { @@ -452,6 +469,7 @@ module.exports = { promisify, sleep, spliceOne, + toUSVString, removeColors, // Symbol used to customize promisify conversion diff --git a/lib/util.js b/lib/util.js index 8827c621fbc0e8..47f9154a820108 100644 --- a/lib/util.js +++ b/lib/util.js @@ -60,7 +60,8 @@ const { deprecate, getSystemErrorMap, getSystemErrorName: internalErrorName, - promisify + promisify, + toUSVString, } = require('internal/util'); let internalDeepEqual; @@ -358,6 +359,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 204b2bca5b9f3b..42218be8581579 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);