Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'request' from 'functions:shell' #5808

Merged
merged 21 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
removing request and api from functions shell
  • Loading branch information
bkendall committed May 20, 2022
commit ae274f022529142df49b7317050b5e1ed2886441
5 changes: 2 additions & 3 deletions src/deploy/functions/runtimes/discovery/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from "node-fetch";
import fetch, { Response } from "node-fetch";
import * as fs from "fs";
import * as path from "path";
import * as yaml from "js-yaml";
Expand Down Expand Up @@ -62,8 +62,7 @@ export async function detectFromPort(
runtime: runtimes.Runtime,
timeout = 30_000 /* 30s to boot up */
): Promise<backend.Backend> {
// The result type of fetch isn't exported
let res: { text(): Promise<string> };
let res: Response;
const timedOut = new Promise<never>((resolve, reject) => {
setTimeout(() => {
reject(new FirebaseError("User code failed to load. Cannot determine backend specification"));
Expand Down
22 changes: 7 additions & 15 deletions src/functionsShellCommandAction.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import * as clc from "cli-color";
import * as repl from "repl";
import * as _ from "lodash";
import * as request from "request";
import * as util from "util";

import { FunctionsServer } from "./serve/functions";
import * as LocalFunction from "./localFunction";
import * as utils from "./utils";
import { logger } from "./logger";
import * as shell from "./emulator/functionsEmulatorShell";
import * as commandUtils from "./emulator/commandUtils";
import { EMULATORS_SUPPORTED_BY_FUNCTIONS, EmulatorInfo, Emulators } from "./emulator/types";
import { EmulatorHubClient } from "./emulator/hubClient";
import { Constants } from "./emulator/constants";
import { EMULATORS_SUPPORTED_BY_FUNCTIONS, EmulatorInfo, Emulators } from "./emulator/types";
import { findAvailablePort } from "./emulator/portUtils";
import { FunctionsServer } from "./serve/functions";
import { logger } from "./logger";
import { Options } from "./options";
import * as commandUtils from "./emulator/commandUtils";
import * as LocalFunction from "./localFunction";
import * as shell from "./emulator/functionsEmulatorShell";
import * as utils from "./utils";

const serveFunctions = new FunctionsServer();

Expand Down Expand Up @@ -111,11 +108,6 @@ export const actionFunction = async (options: Options) => {
);

const writer = (output: any) => {
// Prevent full print out of Request object when a request is made
// @ts-ignore
if (output instanceof request.Request) {
return "Sent request to function.";
}
return util.inspect(output);
};

Expand Down
48 changes: 33 additions & 15 deletions src/localFunction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var _ = require("lodash");
var request = require("request");
var { Client } = require("./apiv2");

var { encodeFirestoreValue } = require("./firestore/encodeFirestoreValue");
var utils = require("./utils");
Expand All @@ -28,11 +28,28 @@ var LocalFunction = function (trigger, urls, controller) {
if (isCallable == "true") {
this.call = this._constructCallableFunc.bind(this);
} else {
this.call = request.defaults({
callback: this._requestCallBack,
baseUrl: this.url,
uri: "",
});
const callClient = new Client({ urlPrefix: this.url, auth: false });
this.call = (data, opts) => {
callClient
.get("", opts)
.then((res) => {
this._requestCallBack(undefined, res, res.body);
})
.catch((err) => {
this._requestCallBack(err);
});
};
for (const method of ["get", "post", "put", "patch", "delete"]) {
this.call[method] = (data, opts) => {
callClient[method]("", data, opts)
.then((res) => {
this._requestCallBack(undefined, res, res.body);
})
.catch((err) => {
this._requestCallBack(err);
});
};
}
}
} else {
this.call = this._call.bind(this);
Expand Down Expand Up @@ -64,14 +81,15 @@ LocalFunction.prototype._constructCallableFunc = function (data, opts) {
headers["Firebase-Instance-ID-Token"] = opts.instanceIdToken;
}

return request.post({
callback: this._requestCallBack,
baseUrl: this.url,
uri: "",
body: { data: data },
json: true,
headers: headers,
});
const client = new Client({ urlPrefix: this.url, auth: false });
void client
.post("", data, { headers })
.then((res) => {
this._requestCallBack(undefined, res, res.body);
})
.catch((err) => {
this._requestCallBack(err);
});
};

LocalFunction.prototype._constructAuth = function (auth, authType) {
Expand Down Expand Up @@ -135,7 +153,7 @@ LocalFunction.prototype._requestCallBack = function (err, response, body) {
if (err) {
return console.warn("\nERROR SENDING REQUEST: " + err);
}
var status = response ? response.statusCode + ", " : "";
var status = response ? response.status + ", " : "";

// If the body is a string we want to check if we can parse it as JSON
// and pretty-print it. We can't blindly stringify because stringifying
Expand Down