Description
Hi gramJS team,
I'm encountering a TypeError: Bytes or str expected, not Buffer
when attempting to log in using the client.start
method in a browser environment.
Version Info:
gramJS
(telegram
package):2.26.21
- Session:
StringSession
Environment:
- Runtime: Browser (within a Next.js application)
- Bundler: Next.js internal (Webpack/Turbopack)
- (Optional: Node.js version used for building:
[Your Node.js Version, e.g., v18.18.0]
)
Problem Description:
After successfully connecting (client.connect()
) and requesting a phone code (client.sendCode()
), the subsequent call to client.start()
fails with the Bytes or str expected, not Buffer
error. This occurs specifically during the phase where gramJS
likely handles the password/2FA step internally (signInWithPassword
appears in the stack trace).
Steps to Reproduce (Conceptual):
- Initialize
TelegramClient
withapiId
,apiHash
, and an emptyStringSession
in a browser environment. - Call
await client.connect();
- Call
await client.sendCode({ apiId, apiHash }, phoneNumber);
- Prompt the user for the received phone code and optionally their 2FA password.
- Call
await client.start({ phoneNumber: ..., phoneCode: async () => code, password: async () => password, onError: ... });
Relevant Code Snippet:
This is how client.start
is being called within my Zustand store:
// Helper function to provide auth parameters
const createAuthParamCallback = <T extends string>(param: T): () => Promise<T> => {
return async () => {
return param;
};
};
// Inside an async function handling login:
async function loginWithCodePassword({ phoneCode, password = '' }) {
// ... retrieve client, apiId, apiHash, phoneNumber from state ...
console.log('[TG Store] Attempting to login with code/password...');
// Set loading state, etc.
try {
console.log('[TG Store] Calling client.start...');
await client.start({
phoneNumber: phoneNumber, // String from state
password: createAuthParamCallback(password), // Always a string (defaulted to '')
phoneCode: createAuthParamCallback(phoneCode), // String from user input
onError: (err: Error) => {
console.error('[TG Store] client.start onError callback triggered:', err);
// Propagate or handle error
throw err;
},
});
// ... handle success ...
} catch (error: any) {
// ... handle failure ...
console.error('[TG Store] Login failed during client.start:', error); // <-- This logs the error
}
}
Error Log & Stack Trace:
[TG Store] Calling client.start...
[TG Store] client.start onError callback triggered: Error: Bytes or str expected, not Buffer
at serializeBytes (generationHelpers.js:247:19)
at argToBytes (api.js:106:20)
at VirtualClass.getBytes (api.js:415:42)
at argToBytes (api.js:113:22)
at VirtualClass.getBytes (api.js:415:42)
at new RequestState (RequestState.js:13:29)
at Object.invoke (users.js:48:19)
at async Object.signInWithPassword (auth.js:319:31)
at async _authFlow (auth.js:363:11)
at async Object.start (auth.js:52:5)
at async loginWithCodePassword (telegramStore.ts:220:7) // <-- My code calling client.start
at async handleCodePasswordSubmit (page.tsx:57:5) // <-- UI handler calling loginWithCodePassword
(The same error/stack trace is caught by the outer try/catch block)
Expected Behavior:
client.start()
should resolve successfully, completing the login process and allowing the session to be saved.
Actual Behavior:
client.start()
throws the TypeError: Bytes or str expected, not Buffer
, preventing login. The error originates deep within the gramJS
library's internal serialization or authentication logic.