diff options
author | Luca Casonato <hello@lcas.dev> | 2024-06-08 18:36:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-08 18:36:13 +0200 |
commit | c1f23c578881b85ae79b524a60160d8f4fb7151b (patch) | |
tree | c6c945fb4b42cd4ac6fae7135c7ad1039630a34e /cli/resolver.rs | |
parent | 22d34f7012c48a25435b38c0c306085c614bbea7 (diff) |
fix(ext/node): lossy UTF-8 read node_modules files (#24140)
Previously various reads of files in `node_modules` would error on
invalid UTF-8. These were cases involving:
- reading package.json from Rust
- reading package.json from JS
- reading CommonJS files from JS
- reading CommonJS files from Rust (for ESM translation)
- reading ESM files from Rust
Diffstat (limited to 'cli/resolver.rs')
-rw-r--r-- | cli/resolver.rs | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/cli/resolver.rs b/cli/resolver.rs index 301cd0666..3edc6f429 100644 --- a/cli/resolver.rs +++ b/cli/resolver.rs @@ -320,7 +320,12 @@ impl NpmModuleLoader { let code = if self.cjs_resolutions.contains(specifier) { // translate cjs to esm if it's cjs and inject node globals - let code = String::from_utf8(code)?; + let code = match String::from_utf8_lossy(&code) { + Cow::Owned(code) => code, + // SAFETY: `String::from_utf8_lossy` guarantees that the result is valid + // UTF-8 if `Cow::Borrowed` is returned. + Cow::Borrowed(_) => unsafe { String::from_utf8_unchecked(code) }, + }; ModuleSourceCode::String( self .node_code_translator |