Firebase: ‘接收到由内部客户端错误触发的RST_STREAM,代码为2:协议错误

huangapple go评论73阅读模式
英文:

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

英文:
 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

答案1

得分: 1

在第二次运行firebase init之后,问题得以解决。可能是因为我在firebase方面是新手,第一次运行初始化时没有使用正确的参数。但当我在探索过程中,无法回忆起我做错了什么。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月13日 18:00:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定