Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: 'Received RST_STREAM with code 2 triggered by internal client error: Protocol error

 code: 13,
  details: 'Received RST_STREAM with code 2 triggered by internal client error: Protocol error',
  metadata: Metadata { internalRepr: Map(0) {}, options: {} },
  note: 'Exception occurred in retry method that was not classified as transient'

This error happens to me with the emulator on every attempt, so may be an easy case to reproduce

Under Node.js /sveltekit server endpoint. Here are my lib versions:

pnpm view firebase-admin dependencies

{
  '@fastify/busboy': '^1.1.0',
  '@firebase/database-compat': '^0.3.0',
  '@firebase/database-types': '^0.10.0',
  '@types/node': '>=12.12.47',
  jsonwebtoken: '^9.0.0',
  'jwks-rsa': '^3.0.1',
  'node-forge': '^1.3.1',
  uuid: '^9.0.0',
  '@google-cloud/firestore': '^6.4.0',
  '@google-cloud/storage': '^6.5.2'
}

My use case is pretty simple: firebaseInit.js:

import admin from 'firebase-admin'
const useEmulator = true;

if (useEmulator){
    process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:4000';
}

// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
    apiKey: "XXXXX",
    authDomain: "linkedincv-bdbec.firebaseapp.com",
    projectId: "linkedincv-bdbec",
    storageBucket: "linkedincv-bdbec.appspot.com",
    messagingSenderId: "XXXXX",
    appId: "XXXXX",
    measurementId: "XXXXX",
};

// Initialize Firebase
const app = admin.initializeApp(firebaseConfig);
// const db = getFirestore(app);

const db = admin.firestore();

export {app, db}

My code using the initialized db variable:

+server.js

import { json } from '@sveltejs/kit';
import {db} from '../../../lib/middleware/firebaseInit'

export async function POST({request}) {
    try {
        const payload = await request.json();
        const uniqueUrl = payload.uniqueUrl;

        const docRef = await db.collection("json_cv").doc("zied").set( payload)
        // Return the document ID of the stored object
        return json({
            body: JSON.stringify({ documentId: docRef.id }),
            headers: { 'Content-Type': 'application/json' },
            status: 200
        });
    } catch (error) {
        // Handle any errors that occur during processing
        console.error(error);
        return json({
            body: JSON.stringify({ error }),
            headers: { 'Content-Type': 'application/json' },
            status: 500
        });
    }
}

Just to be complete if you want to reproduce, here's a file specific to sveltekit allowing CORS hooks.server.js

import { Handle } from '@sveltejs/kit';

export const handle = async ({ resolve, event }) => {
    const response = await resolve(event);

    // Apply CORS header for API routes
    if (event.url.pathname.startsWith('/cv/submit')) {
        // Required for CORS to work
        if(event.request.method === 'OPTIONS') {
            return new Response(null, {
                headers: {
                    'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, DELETE',
                    'Access-Control-Allow-Origin': '*',
                }
            });
        }

        response.headers.append('Access-Control-Allow-Origin', `*`);
    }

    return response;
}; 

Note: I saw there was a bug talking about this issue, but it doesn't seem to be systematic for them

like image 683
Zied Hamdi Avatar asked Feb 21 '26 06:02

Zied Hamdi


1 Answers

After running firebase init a second time, things solved. It might be that as I'm a newbie in firebase, I didn't run the initialization with the right parameters the first time. But as I was discovering things, I couldn't remind what I did wrong.

like image 132
Zied Hamdi Avatar answered Feb 23 '26 20:02

Zied Hamdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!