-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Unknown encoding when undefined is used #3252
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Marcos Candeia <[email protected]>
All contributors have signed the CLA ✍️ ✅ |
Signed-off-by: Marcos Candeia <[email protected]>
Signed-off-by: Marcos Candeia <[email protected]>
I have read the CLA Document and I hereby sign the CLA |
@anonrig fixed a test that was failing due to this expected behavior: https://github.com/cloudflare/workerd/blob/main/src/workerd/api/node/tests/buffer-nodejs-test.js#L5803 |
Signed-off-by: Marcos Candeia <[email protected]>
a7af939
to
cab07c8
Compare
@anonrig can you help me to decide one thing? The NodeJs implementation does not fallback for This test was failing before because of the wrong conversion to The nodejs implementation on the other hand does the following:
The options we have:
|
Hi @mcandeia, Thank you for the awesome comment. I love it! Here's a suggestion to fix the issue: diff --git a/src/node/internal/internal_buffer.ts b/src/node/internal/internal_buffer.ts
index cfde61ea..f57ba153 100644
--- a/src/node/internal/internal_buffer.ts
+++ b/src/node/internal/internal_buffer.ts
@@ -28,7 +28,10 @@ import {
isUint8Array,
} from 'node-internal:internal_types';
-import { normalizeEncoding } from 'node-internal:internal_utils';
+import {
+ normalizeEncoding,
+ getEncodingOps,
+} from 'node-internal:internal_utils';
import { validateString } from 'node-internal:validators';
@@ -648,7 +651,7 @@ Buffer.prototype.toString = function toString(
return '';
}
- const normalizedEncoding = normalizeEncoding(encoding?.toString());
+ const normalizedEncoding = getEncodingOps(encoding);
if (normalizedEncoding === undefined) {
throw new ERR_UNKNOWN_ENCODING(`${encoding}`);
}
diff --git a/src/node/internal/internal_utils.ts b/src/node/internal/internal_utils.ts
index 7290f4f5..3a80b921 100644
--- a/src/node/internal/internal_utils.ts
+++ b/src/node/internal/internal_utils.ts
@@ -42,10 +42,12 @@ export function normalizeEncoding(enc?: string): Encoding | undefined {
enc === 'UTF-8'
)
return UTF8;
- return slowCases(enc);
+ return getEncodingOps(enc);
}
-export function slowCases(enc: unknown): Encoding | undefined {
+export function getEncodingOps(enc: unknown): Encoding | undefined {
+ // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
+ enc += '';
// @ts-expect-error TS18046 TS complains about unknown can not have length.
switch (enc.length) {
case 4:
diff --git a/src/workerd/api/node/tests/buffer-nodejs-test.js b/src/workerd/api/node/tests/buffer-nodejs-test.js
index 3dc88c3a..09c56f80 100644
--- a/src/workerd/api/node/tests/buffer-nodejs-test.js
+++ b/src/workerd/api/node/tests/buffer-nodejs-test.js
@@ -5815,7 +5815,8 @@ export const toStringRange = {
},
{
name: 'TypeError',
- }
+ },
+ 'toString() with 0 and null as the encoding should have thrown'
);
throws(
@@ -5824,7 +5825,8 @@ export const toStringRange = {
},
{
name: 'TypeError',
- }
+ },
+ 'toString() with null encoding should have thrown'
);
},
};
|
By the way, I forgot. Can you add a test so we don't regress in the future? Thank you. |
Signed-off-by: Marcos Candeia <[email protected]>
Signed-off-by: Marcos Candeia <[email protected]>
It seems there is a formatting issue on [warn] src/workerd/api/node/tests/buffer-nodejs-test.js |
Signed-off-by: Marcos Candeia <[email protected]>
fixed @anonrig |
Signed-off-by: Marcos Candeia <[email protected]>
ok, had to fix it again @anonrig |
Do you have any idea why these tests are failing @anonrig ? |
The behavior of
buffer.toString(encoding)
is not compliant with nodejs API. it throws an errorUNKNOWN_ENCODING: null
(or undefined) because it's being passed as a string "undefined" or "null"