@@ -25,7 +25,6 @@ const { Math, Object } = primordials;
25
25
26
26
const {
27
27
byteLengthUtf8,
28
- copy : _copy ,
29
28
compare : _compare ,
30
29
compareOffset,
31
30
createFromString,
@@ -69,6 +68,7 @@ const {
69
68
ERR_INVALID_ARG_VALUE ,
70
69
ERR_INVALID_BUFFER_SIZE ,
71
70
ERR_INVALID_OPT_VALUE ,
71
+ ERR_OUT_OF_RANGE ,
72
72
ERR_UNKNOWN_ENCODING
73
73
} ,
74
74
hideStackFrames
@@ -157,6 +157,74 @@ function showFlaggedDeprecation() {
157
157
bufferWarningAlreadyEmitted = true ;
158
158
}
159
159
160
+ function toInteger ( n , defaultVal ) {
161
+ n = + n ;
162
+ if ( ! Number . isNaN ( n ) &&
163
+ n >= Number . MIN_SAFE_INTEGER &&
164
+ n <= Number . MAX_SAFE_INTEGER ) {
165
+ return ( ( n % 1 ) === 0 ? n : Math . floor ( n ) ) ;
166
+ }
167
+ return defaultVal ;
168
+ }
169
+
170
+ function _copy ( source , target , targetStart , sourceStart , sourceEnd ) {
171
+ if ( ! isUint8Array ( source ) )
172
+ throw new ERR_INVALID_ARG_TYPE ( 'source' , [ 'Buffer' , 'Uint8Array' ] , source ) ;
173
+ if ( ! isUint8Array ( target ) )
174
+ throw new ERR_INVALID_ARG_TYPE ( 'target' , [ 'Buffer' , 'Uint8Array' ] , target ) ;
175
+
176
+ if ( targetStart === undefined ) {
177
+ targetStart = 0 ;
178
+ } else {
179
+ targetStart = toInteger ( targetStart , 0 ) ;
180
+ if ( targetStart < 0 )
181
+ throw new ERR_OUT_OF_RANGE ( 'targetStart' , '>= 0' , targetStart ) ;
182
+ }
183
+
184
+ if ( sourceStart === undefined ) {
185
+ sourceStart = 0 ;
186
+ } else {
187
+ sourceStart = toInteger ( sourceStart , 0 ) ;
188
+ if ( sourceStart < 0 )
189
+ throw new ERR_OUT_OF_RANGE ( 'sourceStart' , '>= 0' , sourceStart ) ;
190
+ }
191
+
192
+ if ( sourceEnd === undefined ) {
193
+ sourceEnd = source . length ;
194
+ } else {
195
+ sourceEnd = toInteger ( sourceEnd , 0 ) ;
196
+ if ( sourceEnd < 0 )
197
+ throw new ERR_OUT_OF_RANGE ( 'sourceEnd' , '>= 0' , sourceEnd ) ;
198
+ }
199
+
200
+ if ( targetStart >= target . length || sourceStart >= sourceEnd )
201
+ return 0 ;
202
+
203
+ if ( sourceStart > source . length ) {
204
+ throw new ERR_OUT_OF_RANGE ( 'sourceStart' ,
205
+ `<= ${ source . length } ` ,
206
+ sourceStart ) ;
207
+ }
208
+
209
+ if ( sourceEnd - sourceStart > target . length - targetStart )
210
+ sourceEnd = sourceStart + target . length - targetStart ;
211
+
212
+ let nb = sourceEnd - sourceStart ;
213
+ const targetLen = target . length - targetStart ;
214
+ const sourceLen = source . length - sourceStart ;
215
+ if ( nb > targetLen )
216
+ nb = targetLen ;
217
+ if ( nb > sourceLen )
218
+ nb = sourceLen ;
219
+
220
+ if ( sourceStart !== 0 || sourceEnd !== source . length )
221
+ source = new Uint8Array ( source . buffer , source . byteOffset + sourceStart , nb ) ;
222
+
223
+ target . set ( source , targetStart ) ;
224
+
225
+ return nb ;
226
+ }
227
+
160
228
/**
161
229
* The Buffer() constructor is deprecated in documentation and should not be
162
230
* used moving forward. Rather, developers should use one of the three new
0 commit comments