Skip to content

Use inspect.custom for BatchId, Bytes, etc. #1220

Description

@Cafe137

Goal

When Swarm primitives are printed to the console (node or browser), they should be represented as hex strings rather than raw object dumps of hard-to-read bytes

Example

Applies to all primitives, such as BatchIds, Bytes, etc.

const id = new BatchId('ab'.repeat(32))

console.log(id)
console.log({ id })
console.log(JSON.stringify({ id }))

Actual:

BatchId {
    bytes: Uint8Array(32) [
      171, 171, 171, 171, 171, 171,
      171, 171, 171, 171, 171, 171,
      171, 171, 171, 171, 171, 171,
      171, 171, 171, 171, 171, 171,
      171, 171, 171, 171, 171, 171,
      171, 171
    ],
    length: 32
}

{
    id: BatchId {
      bytes: Uint8Array(32) [ … same 32 entries … ],
      length: 32
    }
}

SyntaxError: Unexpected token '«', "««««««««««"... is not valid JSON

Expected:

  abababababababababababababababababababababababababababababababab
  { id: 'abababababababababababababababababababababababababababababababab' }
  {"id":"abababababababababababababababababababababababababababababababab"}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions