| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6deeef1 commit bc6b630
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5317,6 +5317,11 @@ npx codemod@latest @nodejs/buffer-atob-btoa | |||
| 5317 | 5317 | added: | |
| 5318 | 5318 | - v19.6.0 | |
| 5319 | 5319 | - v18.15.0 | |
| 5320 | + changes: | ||
| 5321 | + - version: REPLACEME | ||
| 5322 | + pr-url: https://github.com/nodejs/node/pull/64504 | ||
| 5323 | + description: Detached `ArrayBuffer`s and views backed by them are treated | ||
| 5324 | + as empty. | ||
| 5320 | 5325 | --> | |
| 5321 | 5326 | ||
| 5322 | 5327 | * `input` {Buffer | ArrayBuffer | TypedArray} The input to validate. | |
@@ -5325,14 +5330,19 @@ added: | |||
| 5325 | 5330 | This function returns `true` if `input` contains only valid ASCII-encoded data, | |
| 5326 | 5331 | including the case in which `input` is empty. | |
| 5327 | 5332 | ||
| 5328 | - Throws if the `input` is a detached array buffer. | ||
| 5333 | + A detached `ArrayBuffer`, or a `TypedArray` backed by one, is treated as empty. | ||
| 5329 | 5334 | ||
| 5330 | 5335 | ### `buffer.isUtf8(input)` | |
| 5331 | 5336 | ||
| 5332 | 5337 | <!-- YAML | |
| 5333 | 5338 | added: | |
| 5334 | 5339 | - v19.4.0 | |
| 5335 | 5340 | - v18.14.0 | |
| 5341 | + changes: | ||
| 5342 | + - version: REPLACEME | ||
| 5343 | + pr-url: https://github.com/nodejs/node/pull/64504 | ||
| 5344 | + description: Detached `ArrayBuffer`s and views backed by them are treated | ||
| 5345 | + as empty. | ||
| 5336 | 5346 | --> | |
| 5337 | 5347 | ||
| 5338 | 5348 | * `input` {Buffer | ArrayBuffer | TypedArray} The input to validate. | |
@@ -5341,7 +5351,7 @@ added: | |||
| 5341 | 5351 | This function returns `true` if `input` contains only valid UTF-8-encoded data, | |
| 5342 | 5352 | including the case in which `input` is empty. | |
| 5343 | 5353 | ||
| 5344 | - Throws if the `input` is a detached array buffer. | ||
| 5354 | + A detached `ArrayBuffer`, or a `TypedArray` backed by one, is treated as empty. | ||
| 5345 | 5355 | ||
| 5346 | 5356 | ### `buffer.INSPECT_MAX_BYTES` | |
| 5347 | 5357 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1360,31 +1360,17 @@ void FastSwap64(Local<Value> receiver, | |||
| 1360 | 1360 | ||
| 1361 | 1361 | static CFunction fast_swap64(CFunction::Make(FastSwap64)); | |
| 1362 | 1362 | ||
| 1363 | - struct ValidationResult { | ||
| 1364 | - bool is_valid; | ||
| 1365 | - bool was_detached; | ||
| 1366 | - }; | ||
| 1367 | - | ||
| 1368 | - static ValidationResult ValidateUtf8(Local<Value> value) { | ||
| 1363 | + static bool ValidateUtf8(Local<Value> value) { | ||
| 1369 | 1364 | ArrayBufferViewContents<char> abv(value); | |
| 1370 | - bool was_detached = abv.WasDetached(); | ||
| 1371 | - return {!was_detached && simdutf::validate_utf8(abv.data(), abv.length()), | ||
| 1372 | - was_detached}; | ||
| 1365 | + return abv.length() == 0 || simdutf::validate_utf8(abv.data(), abv.length()); | ||
| 1373 | 1366 | } | |
| 1374 | 1367 | ||
| 1375 | 1368 | static void IsUtf8(const FunctionCallbackInfo<Value>& args) { | |
| 1376 | - Environment* env = Environment::GetCurrent(args); | ||
| 1377 | 1369 | CHECK_EQ(args.Length(), 1); | |
| 1378 | 1370 | CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() || | |
| 1379 | 1371 | args[0]->IsSharedArrayBuffer()); | |
| 1380 | 1372 | ||
| 1381 | - const ValidationResult result = ValidateUtf8(args[0]); | ||
| 1382 | - if (result.was_detached) { | ||
| 1383 | - return node::THROW_ERR_INVALID_STATE( | ||
| 1384 | - env, "Cannot validate on a detached buffer"); | ||
| 1385 | - } | ||
| 1386 | - | ||
| 1387 | - args.GetReturnValue().Set(result.is_valid); | ||
| 1373 | + args.GetReturnValue().Set(ValidateUtf8(args[0])); | ||
| 1388 | 1374 | } | |
| 1389 | 1375 | ||
| 1390 | 1376 | static bool FastIsUtf8(Local<Value> receiver, | |
@@ -1393,40 +1379,23 @@ static bool FastIsUtf8(Local<Value> receiver, | |||
| 1393 | 1379 | FastApiCallbackOptions& options) { | |
| 1394 | 1380 | TRACK_V8_FAST_API_CALL("buffer.isUtf8"); | |
| 1395 | 1381 | HandleScope scope(options.isolate); | |
| 1396 | - | ||
| 1397 | - const ValidationResult result = ValidateUtf8(value); | ||
| 1398 | - if (result.was_detached) { | ||
| 1399 | - node::THROW_ERR_INVALID_STATE(options.isolate, | ||
| 1400 | - "Cannot validate on a detached buffer"); | ||
| 1401 | - return false; | ||
| 1402 | - } | ||
| 1403 | - return result.is_valid; | ||
| 1382 | + return ValidateUtf8(value); | ||
| 1404 | 1383 | } | |
| 1405 | 1384 | ||
| 1406 | 1385 | static CFunction fast_is_utf8(CFunction::Make(FastIsUtf8)); | |
| 1407 | 1386 | ||
| 1408 | - static ValidationResult ValidateAscii(Local<Value> value) { | ||
| 1387 | + static bool ValidateAscii(Local<Value> value) { | ||
| 1409 | 1388 | ArrayBufferViewContents<char> abv(value); | |
| 1410 | - bool was_detached = abv.WasDetached(); | ||
| 1411 | - return { | ||
| 1412 | - !was_detached && | ||
| 1413 | - !simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error, | ||
| 1414 | - was_detached}; | ||
| 1389 | + return abv.length() == 0 || | ||
| 1390 | + !simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error; | ||
| 1415 | 1391 | } | |
| 1416 | 1392 | ||
| 1417 | 1393 | static void IsAscii(const FunctionCallbackInfo<Value>& args) { | |
| 1418 | - Environment* env = Environment::GetCurrent(args); | ||
| 1419 | 1394 | CHECK_EQ(args.Length(), 1); | |
| 1420 | 1395 | CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() || | |
| 1421 | 1396 | args[0]->IsSharedArrayBuffer()); | |
| 1422 | 1397 | ||
| 1423 | - const ValidationResult result = ValidateAscii(args[0]); | ||
| 1424 | - if (result.was_detached) { | ||
| 1425 | - return node::THROW_ERR_INVALID_STATE( | ||
| 1426 | - env, "Cannot validate on a detached buffer"); | ||
| 1427 | - } | ||
| 1428 | - | ||
| 1429 | - args.GetReturnValue().Set(result.is_valid); | ||
| 1398 | + args.GetReturnValue().Set(ValidateAscii(args[0])); | ||
| 1430 | 1399 | } | |
| 1431 | 1400 | ||
| 1432 | 1401 | static bool FastIsAscii(Local<Value> receiver, | |
@@ -1435,14 +1404,7 @@ static bool FastIsAscii(Local<Value> receiver, | |||
| 1435 | 1404 | FastApiCallbackOptions& options) { | |
| 1436 | 1405 | TRACK_V8_FAST_API_CALL("buffer.isAscii"); | |
| 1437 | 1406 | HandleScope scope(options.isolate); | |
| 1438 | - | ||
| 1439 | - const ValidationResult result = ValidateAscii(value); | ||
| 1440 | - if (result.was_detached) { | ||
| 1441 | - node::THROW_ERR_INVALID_STATE(options.isolate, | ||
| 1442 | - "Cannot validate on a detached buffer"); | ||
| 1443 | - return false; | ||
| 1444 | - } | ||
| 1445 | - return result.is_valid; | ||
| 1407 | + return ValidateAscii(value); | ||
| 1446 | 1408 | } | |
| 1447 | 1409 | ||
| 1448 | 1410 | static CFunction fast_is_ascii(CFunction::Make(FastIsAscii)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,13 +30,20 @@ assert.strictEqual(isAscii(Buffer.from([])), true); | |||
| 30 | 30 | }); | |
| 31 | 31 | ||
| 32 | 32 | { | |
| 33 | - // Test with detached array buffers | ||
| 34 | - const arrayBuffer = new ArrayBuffer(1024); | ||
| 33 | + // Detached array buffers and views are treated as empty. | ||
| 34 | + const arrayBuffer = new ArrayBuffer(1); | ||
| 35 | + const typedArray = new Uint8Array(arrayBuffer); | ||
| 36 | + typedArray[0] = 0xff; | ||
| 37 | + const inputs = [ | ||
| 38 | + arrayBuffer, | ||
| 39 | + typedArray, | ||
| 40 | + Buffer.from(arrayBuffer), | ||
| 41 | + ]; | ||
| 42 | + for (const input of inputs) { | ||
| 43 | + assert.strictEqual(isAscii(input), false); | ||
| 44 | + } | ||
| 35 | 45 | structuredClone(arrayBuffer, { transfer: [arrayBuffer] }); | |
| 36 | - assert.throws( | ||
| 37 | - () => { isAscii(arrayBuffer); }, | ||
| 38 | - { | ||
| 39 | - code: 'ERR_INVALID_STATE' | ||
| 40 | - } | ||
| 41 | - ); | ||
| 46 | + for (const input of inputs) { | ||
| 47 | + assert.strictEqual(isAscii(input), true); | ||
| 48 | + } | ||
| 42 | 49 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,13 +74,20 @@ assert.strictEqual(isUtf8(Buffer.from([])), true); | |||
| 74 | 74 | }); | |
| 75 | 75 | ||
| 76 | 76 | { | |
| 77 | - // Test with detached array buffers | ||
| 78 | - const arrayBuffer = new ArrayBuffer(1024); | ||
| 77 | + // Detached array buffers and views are treated as empty. | ||
| 78 | + const arrayBuffer = new ArrayBuffer(1); | ||
| 79 | + const typedArray = new Uint8Array(arrayBuffer); | ||
| 80 | + typedArray[0] = 0xff; | ||
| 81 | + const inputs = [ | ||
| 82 | + arrayBuffer, | ||
| 83 | + typedArray, | ||
| 84 | + Buffer.from(arrayBuffer), | ||
| 85 | + ]; | ||
| 86 | + for (const input of inputs) { | ||
| 87 | + assert.strictEqual(isUtf8(input), false); | ||
| 88 | + } | ||
| 79 | 89 | structuredClone(arrayBuffer, { transfer: [arrayBuffer] }); | |
| 80 | - assert.throws( | ||
| 81 | - () => { isUtf8(arrayBuffer); }, | ||
| 82 | - { | ||
| 83 | - code: 'ERR_INVALID_STATE' | ||
| 84 | - } | ||
| 85 | - ); | ||
| 90 | + for (const input of inputs) { | ||
| 91 | + assert.strictEqual(isUtf8(input), true); | ||
| 92 | + } | ||
| 86 | 93 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments