Skip to content
Permalink
Browse files

util: do not throw when inspecting detached ArrayBuffer

PR-URL: #29318
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
  • Loading branch information...
addaleax authored and BridgeAR committed Aug 25, 2019
1 parent 71aaf59 commit 5abbd51c609d4c814276e09e1bac9721ccc62a93
Showing with 16 additions and 1 deletion.
  1. +6 −1 lib/internal/util/inspect.js
  2. +10 −0 test/parallel/test-util-inspect.js
@@ -1164,7 +1164,12 @@ function formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) {
}
function formatArrayBuffer(ctx, value) {
const buffer = new Uint8Array(value);
let buffer;
try {
buffer = new Uint8Array(value);
} catch {
return [ctx.stylize('(detached)', 'special')];
}
if (hexSlice === undefined)
hexSlice = uncurryThis(require('buffer').Buffer.prototype.hexSlice);
let str = hexSlice(buffer, 0, Math.min(ctx.maxArrayLength, buffer.length))
@@ -28,6 +28,7 @@ const util = require('util');
const vm = require('vm');
const { previewEntries } = internalBinding('util');
const { inspect } = util;
const { MessageChannel } = require('worker_threads');

assert.strictEqual(util.inspect(1), '1');
assert.strictEqual(util.inspect(false), 'false');
@@ -198,6 +199,15 @@ assert(!/Object/.test(
' y: 1337\n}');
}

{
const ab = new ArrayBuffer(42);
assert.strictEqual(ab.byteLength, 42);
new MessageChannel().port1.postMessage(ab, [ ab ]);
assert.strictEqual(ab.byteLength, 0);
assert.strictEqual(util.inspect(ab),
'ArrayBuffer { (detached), byteLength: 0 }');
}

// Now do the same checks but from a different context.
{
const showHidden = false;

0 comments on commit 5abbd51

Please sign in to comment.
You can’t perform that action at this time.