【发布时间】: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.
查看源代码似乎在模拟器中实现了一些过滤:
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 工程师肯定会发现其中的问题。