`TextEncoder.encodeInto()` underfills the destination for some non-ASCII text
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- JavaScript
- Estrellas
- 122k
- Forks
- 37.3k
- Merge medio
- 4 d 2 h
- PR fusionados (30 d)
- 283
Descripción
There are two problems I found with TextEncoder.encodeInto(), both of which can cause encoding to stall even when the next char can fit the destination.
-
A 2-byte char requires a 3-byte destination
const encoder = new TextEncoder(); const text = '\u0400'.repeat(33); console.log(encoder.encodeInto(text, new Uint8Array(2))); // { read: 0, written: 0 } console.log(encoder.encodeInto(text, new Uint8Array(3))); // { read: 1, written: 2 }The second call proves that '\u0400' should fit into a 2-byte array.
-
Appending an unread character changes encoding progress
const encoder = new TextEncoder(); const text = 'é'.repeat(33); console.log(encoder.encodeInto(text, new Uint8Array(2))); // { read: 0, written: 0 } console.log(encoder.encodeInto(text + '☺', new Uint8Array(2))); // { read: 1, written: 2 }Appending
☺should not change whether preceding chars can be read into the buffer, but there it is.
The bugs were introduced by the encodeInto() performance change in Node.js v25.4.0. The examples above use length 33 strings to exercise that optimized path(kSmallStringThreshold = 32). Unfortunately the current encodeInto.any.js WPT tests fail to expose the problems because:
- all input cases use 7 or fewer code units
- even then, the cases don't use chars between U+0400 and U+07FF, and
- their cases don't contain a narrow dst capacity to reveal the signed-byte problem.
Proposed fixes
-
Incorrect cutoff in
simpleUtfEncodingLength()- if (c < 0x400) return 2; + if (c < 0x800) return 2;(very likely a typo, given the comment immediately below it says "Code points < 0x800: 2 bytes")
-
Signed-byte handling in
findBestFit()- size_t extra = simpleUtfEncodingLength(data[pos]); + size_t extra = simpleUtfEncodingLength(UTF16 ? data[pos] : static_cast<uint8_t>(data[pos]));
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza en src/encoding_binding.cc, especialmente en simpleUtfEncodingLength() y findBestFit(), y luego inspecciona la cobertura de WPT en encodeInto.any.js. Reproduce los ejemplos de 33 caracteres y añade cobertura para U+0400–U+07FF y destinos estrechos; se considera terminado cuando encodeInto informa de los valores esperados de lectura/escritura y las pruebas WPT pertinentes pasan.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- cpp, javascript, nodejs
- Área
- api, backend
- Tipo de issue
- Error
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Activo
- Claridad
- Bien especificado
- Aptitud para principiantes
- 84/100