k6 icon indicating copy to clipboard operation
k6 copied to clipboard

Add `console.log` support for `ArrayBuffer` and `TypedArray` (Option 1)

Open pkalsi97 opened this issue 4 weeks ago • 1 comments

What?

This PR adds support for console.log for ArrayBuffer and TypedArray.

Before

INFO[0000] --- Direct Cases ---                          source=console
INFO[0000] {"0":4,"1":2}                                 source=console
INFO[0000] {}                                            source=console
INFO[0000] --- Nested Cases ---                          source=console
INFO[0000] {"v":{"0":4,"1":2}}                           source=console
INFO[0000] {"b":{}}                                      source=console
INFO[0000] --- Regular Objects  ---   source=console
INFO[0000] {"foo":"bar"}                                 source=console
INFO[0000] [1,2,3]                                       source=console

After

INFO[0000] --- Direct Cases ---                          source=console
INFO[0000] Int32Array(2) [ 4, 2 ]                        source=console
INFO[0000] ArrayBuffer { [Uint8Contents]: <04 00 00 00 02 00 00 00>, byteLength: 8 }  source=console
INFO[0000] --- Nested Cases ---                          source=console
INFO[0000] {"v":"Int32Array(2) [ 4, 2 ]"}                source=console
INFO[0000] {"b":"ArrayBuffer { [Uint8Contents]: <04 00 00 00 02 00 00 00>, byteLength: 8 }"}  source=console
INFO[0000] --- Regular Objects (should still work) ---   source=console
INFO[0000] {"foo":"bar"}                                 source=console
INFO[0000] [1,2,3]                                       source=console

Why?

Issue: ArrayBuffer and TypedArray is used lot in k6, and console.log doesn't print them.

Note: This PR Implements Option 1: Uses json.Encoder with SetEscapeHTML(false). This prevents <>\u003c\u003e escaping but retains extra quotes around formatted strings for nested objects.

Changes

  • Added isBinaryData Detects ArrayBuffer or any TypedArray by inspecting exported type and slice element kind.
  • Added formatBinaryData: Produces printable binary output: hex-dump for ArrayBuffer, Type(n) [ … ] for TypedArrays.
  • Added typedArrayName Maps reflect element kinds to JS typed-array names.
  • Updated valueString and traverseValue, checks if obj is of TypedArray or ArrayBuffer Type, uses formatBinaryData to format it
  • Updated console tests: TypedArrays, ArrayBuffer cases, nested structures
  • Removed json.Marshal and now uses json.Encoder with SetEscapeHTML(false) this will prevent the < > to \u003c \u003e conversion. but double quotes will still be present in the output.

Checklist

  • [x] I have performed a self-review of my code.
  • [x] I have commented on my code, particularly in hard-to-understand areas.
  • [x] I have added tests for my changes.
  • [x] I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • [ ] I have added the correct milestone and labels to the PR.
  • [ ] I have updated the release notes: link
  • [ ] I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • [ ] I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

Original Issue: #5469 Issues for Option 2: #5495 PR Implementing Option 2: #5495

pkalsi97 avatar Dec 10 '25 12:12 pkalsi97

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Dec 10 '25 12:12 CLAassistant