Proposal: built-in YAML support, public API and placement

Aperta
#67,347 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js, yaml
Ambito
api

Direzione di ricerca

Read the proposal and linked #67157, which contains the implementation and measurements; this issue focuses on deciding the public API and module placement. Review the proposed parsing behavior and alternatives, then check the discussion for an agreed direction. Done means the API and placement proposal has a decision; the issue leaves several follow-up questions open.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

feature request
What is the problem this feature will solve?

An application that reads a YAML config file needs a third-party parser. Should Node provide one directly? If so, should it live in its own module?

YAML also came up in #53787, the discussion about Node's own configuration file.

What is the feature you are proposing to solve the problem?

#67157 proposes one function behind --experimental-yaml:

import { parse } from 'node:yaml';

parse('port: 8080'); // { port: 8080 }

Whether the parser is written in JS, vendored from yaml or js-yaml, or built in C++ or wasm is discussed in the PR, with measurements; the API is the same in every case. This issue is about the API.

  • parse(input[, options]) accepts a string or UTF-8 bytes in an ArrayBuffer, SharedArrayBuffer, typed array or DataView.
  • One document returns its value, multiple documents return an array, and an empty stream returns null.
  • Invalid YAML throws a SyntaxError with code ERR_INVALID_YAML and a line and column. With throwIfInvalid: false, it returns undefined instead.
  • The parser uses the YAML 1.2 core schema: yes and no are strings, and 0777 is decimal. It doesn't convert timestamps to dates, expand merge keys or decode !!binary.
  • Mappings become plain objects. Even __proto__ is an own property. Duplicate keys are errors. Aliases to a collection refer to the same object, so the result can contain cycles.

Possible follow-ups:

  • stringify(value[, replacer[, space]]), with arguments like JSON.stringify().
  • A version option. Should we reserve it now with '1.2' as the only supported value, or wait until there's a second version to choose from?
  • A reviver option to transform parsed values. We'd need to define how it visits shared references and cycles.
  • A file or stream API that can parse large inputs without keeping the whole source in memory.

I'd favor node:yaml if we expect to offer both parsing and stringifying. It keeps those operations together. One concern raised in review is whether that sets a precedent for a module per format. I'd judge TOML, XML and other formats on their own use cases.

What alternatives have you considered?
  1. Add util.parseYAML() alongside util.parseEnv(). This seems like a good fit if parsing is all we want.
  2. Use a shared module such as node:serdes, with serdes.yaml.parse(). Other formats could use it later, but we'd be designing the shared API with only one format in mind.
  3. Keep YAML internal if Node adopts it for a feature such as config files or import config from './config.yaml' with { type: 'yaml' }. Applications would still choose their own parser.
  4. Leave YAML to packages such as yaml and js-yaml. Users can already choose between them, and core wouldn't take on another parser to maintain.
Lingua principale
JavaScript
Stelle
124k
Fork
38k
Merge medio
4g 2h
PR unite (30g)
266

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nodejs/node

Tutte le issue di nodejs/node

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.