@@ -17,6 +17,7 @@ const {
1717 SafeSet,
1818 StringPrototypeCharCodeAt,
1919 StringPrototypeIncludes,
20+ StringPrototypeIsWellFormed,
2021 StringPrototypeSlice,
2122 Uint8Array,
2223} = primordials ;
@@ -145,12 +146,11 @@ function getRequired(object, property, name) {
145146 return value ;
146147}
147148
148- function snapshotBinary ( value , name ) {
149+ function snapshotBinary ( value , name , expectedTypes = [
150+ 'ArrayBuffer' , 'SharedArrayBuffer' , 'Buffer' , 'TypedArray' , 'DataView' ,
151+ ] ) {
149152 if ( ! isAnyArrayBuffer ( value ) && ! isArrayBufferView ( value ) ) {
150- throw new ERR_INVALID_ARG_TYPE (
151- name ,
152- [ 'ArrayBuffer' , 'SharedArrayBuffer' , 'Buffer' , 'TypedArray' , 'DataView' ] ,
153- value ) ;
153+ throw new ERR_INVALID_ARG_TYPE ( name , expectedTypes , value ) ;
154154 }
155155 const view = isAnyArrayBuffer ( value ) ?
156156 new Uint8Array ( value ) :
@@ -160,39 +160,21 @@ function snapshotBinary(value, name) {
160160
161161function snapshotCertificateRequest ( value ) {
162162 if ( typeof value === 'string' ) return Buffer . from ( value ) ;
163- if ( ! isAnyArrayBuffer ( value ) && ! isArrayBufferView ( value ) ) {
164- throw new ERR_INVALID_ARG_TYPE (
165- 'options.csr' ,
166- [
167- 'string' ,
168- 'ArrayBuffer' ,
169- 'SharedArrayBuffer' ,
170- 'Buffer' ,
171- 'TypedArray' ,
172- 'DataView' ,
173- ] ,
174- value ) ;
175- }
176- const view = isAnyArrayBuffer ( value ) ?
177- new Uint8Array ( value ) :
178- new Uint8Array ( value . buffer , value . byteOffset , value . byteLength ) ;
179- return Buffer . from ( view ) ;
163+ return snapshotBinary ( value , 'options.csr' , [
164+ 'string' ,
165+ 'ArrayBuffer' ,
166+ 'SharedArrayBuffer' ,
167+ 'Buffer' ,
168+ 'TypedArray' ,
169+ 'DataView' ,
170+ ] ) ;
180171}
181172
182173function validateUnicode ( value , name ) {
183174 validateString ( value , name ) ;
184- for ( let i = 0 ; i < value . length ; i ++ ) {
185- const first = StringPrototypeCharCodeAt ( value , i ) ;
186- if ( first >= 0xd800 && first <= 0xdbff ) {
187- const second = StringPrototypeCharCodeAt ( value , ++ i ) ;
188- if ( ! ( second >= 0xdc00 && second <= 0xdfff ) ) {
189- throw new ERR_INVALID_ARG_VALUE (
190- name , value , 'contains an unpaired surrogate' ) ;
191- }
192- } else if ( first >= 0xdc00 && first <= 0xdfff ) {
193- throw new ERR_INVALID_ARG_VALUE (
194- name , value , 'contains an unpaired surrogate' ) ;
195- }
175+ if ( ! StringPrototypeIsWellFormed ( value ) ) {
176+ throw new ERR_INVALID_ARG_VALUE (
177+ name , value , 'contains an unpaired surrogate' ) ;
196178 }
197179}
198180
0 commit comments