【问题标题】:Unable to access URLs other than googleapis.com from Firebase emulator using fetch()无法使用 fetch() 从 Firebase 模拟器访问除 googleapis.com 以外的 URL
【发布时间】:2019-11-23 15:18:24
【问题描述】:

我正在尝试创建一些 Firebase 云函数并使用

在本地测试它们
firebase emulators:start --only functions

这些函数应该使用 fetch 调用一些外部服务。

我发现这些函数部署到Firebase云时可以调用这些外部服务,但在模拟器本地运行时无法调用:

import 'cross-fetch/polyfill';

export const fetchTest = functions
    .region(config.firebaseRegion)
    .https.onRequest((request: Request, response: Response) => {
        fetch("https://www.wikipedia.org/", {
            method: 'GET',
        }).then(value => {
            console.log("Fetched: ", value);
        }).catch(reason => {
            console.log("Fetch failed: ", reason);
        });

        fetch("https://googleapis.com/foo", {
            method: 'GET',
        }).then(value => {
            console.log("Fetched: ", value);
        }).catch(reason => {
            console.log("Fetch failed: ", reason);
        });
        response.send("Done");
    });

这是我在模拟器中调用 fetchTest 时得到的输出:

⚠  Unknown network resource requested!
   - URL: "https://www.wikipedia.org/"
⚠  Google API requested!
   - URL: "https://googleapis.com/foo"
   - Be careful, this may be a production service.

查看源代码似乎在模拟器中实现了一些过滤:

https://github.com/firebase/firebase-tools/blob/0586ee1e23adc64b0fe8607a026ba472a6bd7d2e/src/emulator/functionsEmulatorRuntime.ts

  if (href && !history[href]) {
    history[href] = true;
    if (href.indexOf("googleapis.com") !== -1) {
      new EmulatorLog("SYSTEM", "googleapis-network-access", "", {
        href,
        module: bundle.name,
      }).log();
    } else {
      new EmulatorLog("SYSTEM", "unidentified-network-access", "", {
        href,
        module: bundle.name,
      }).log();
    }
  }

这种限制有什么理由吗?有没有解决方法?

谢谢!

【问题讨论】:

  • 如果您对模拟器的工作方式有疑问,您应该在 GitHub 上而不是 Stack Overflow 上发布问题。 Firebase 工程师肯定会发现其中的问题。
  • 谢谢,我就是这么做的:github.com/firebase/firebase-tools/issues/1503

标签: firebase fetch emulation


【解决方案1】:

当您看到类似这样的“请求未知网络”日志时:

⚠  Unknown network resource requested!
   - URL: "https://www.wikipedia.org/"

它们只是警告。实际请求被允许通过。该日志旨在告诉您模拟器正在访问您机器之外的资源。这通常是人们想要避免的事情,因为本地测试是封闭的,但有时这是您想要做的,您可以安全地忽略警告!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 2020-09-30
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多