Skip to content

Formatting of Base64 encoded binaries, i.e. long lines with no spaces, separated by newlines is not ideal #585

@rjmunro

Description

@rjmunro

Describe the bug

I had some Base64 data which I formatted onto lines of length 76 separated by newlines. I embedded it into some data, stringified with YAML and the results looked like:

foo:
  bar:
    baz:
      base64: >-
        dGVzdGluZyB0ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0

        ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0ZXN0aW5nIHRl

        c3RpbmcgdGVzdGluZyB0ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0ZXN0aW5nIA==

It would make more sense to use the | instead of > and then it wouldn't need to put a blank line between each line of data. I can see that it hasn't done that because it sees the lines are long, so it wants to wrap them at spaces, but there are no spaces to wrap.

I was able to fix it by shortening my base64 line length, but 76 is the standard for base64.

To Reproduce

const yaml = require("yaml");

// Get some base64 data (any long string with no whitespace would do)
base64data = btoa("testing ".repeat(20));

// Split it over lines of length 76
base64lines =  base64data.replace(/(.{76})/g, "$1\n"),

data = {
  foo: {
    bar: {
      baz: {
        base64: base64lines
      },
    },
  },
};

console.log(yaml.stringify(data));

Expected behaviour

foo:
  bar:
    baz:
      base64: |-
        dGVzdGluZyB0ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0
        ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0ZXN0aW5nIHRl
        c3RpbmcgdGVzdGluZyB0ZXN0aW5nIHRlc3RpbmcgdGVzdGluZyB0ZXN0aW5nIA==

Versions (please complete the following information):

  • Environment: Node v21.7.1
  • yaml: 2.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions