cobalt-ui icon indicating copy to clipboard operation
cobalt-ui copied to clipboard

If a file Buffer is passed to the parser 0 tokens is found without error

Open pvignau opened this issue 3 months ago • 1 comments

Versions

  • terrazzo/parser: 0.10.3

Node.js version

24.4.1

OS + version

macOS 26

Description

Seems that the parser handles Buffer differently than the cli. But if it gets one it executes itself without an error.

As seen in the documentation :

const filename = new URL("./tokens/my-tokens.json", import.meta.url);
const { tokens, sources } = await parse(
  [{ filename, src: await fs.readFile(filename) }],
  { config }
);

This runs without any error but do not find any error

The idea here is to update documentation to go from await fs.readFile(filename) to await fs.readFile(filename, 'utf-8') and add an error handling if a buffer is found in the parser.

Reproduction

Use JS API to generate tokens using the doc.

Expected result

If the tokens.json input is filled with DTCG compliant tokens it builds tokens without error

Extra

pvignau avatar Sep 19 '25 13:09 pvignau

Thank you! Would love a PR if you have time. I think there are 2 parts here:

  1. In @terrazzo/cli, we check for src instanceof Buffer and convert
  2. In parser, we should make some loose check for “object in memory that’s a binary” (maybe src && typeof src === 'object' && !Object.keys.length(src)?) and throw an error. Remember that the parser can run in a browser so we can’t run instanceof Buffer there, or use Node libraries to check it.

drwpow avatar Sep 19 '25 19:09 drwpow