firebase-tools 15.23.0 → 15.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MAX_REQUEST_BODY_BYTES = void 0;
3
4
  exports.isUrl = isUrl;
4
5
  exports.readJSON = readJSON;
5
6
  exports.warnIfCustomBuildScript = warnIfCustomBuildScript;
6
7
  exports.proxyResponse = proxyResponse;
8
+ exports.bufferRequestBody = bufferRequestBody;
7
9
  exports.simpleProxy = simpleProxy;
8
10
  exports.getNpmRoot = getNpmRoot;
9
11
  exports.getNodeModuleBin = getNodeModuleBin;
@@ -86,12 +88,29 @@ function proxyResponse(req, res, next) {
86
88
  });
87
89
  return proxiedRes;
88
90
  }
91
+ exports.MAX_REQUEST_BODY_BYTES = 32 * 1024 * 1024;
92
+ async function bufferRequestBody(req) {
93
+ const chunks = [];
94
+ let total = 0;
95
+ for await (const chunk of req) {
96
+ const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
97
+ total += buf.length;
98
+ if (total > exports.MAX_REQUEST_BODY_BYTES) {
99
+ throw new error_1.FirebaseError(`Request body exceeds the ${exports.MAX_REQUEST_BODY_BYTES} byte limit`, {
100
+ status: 413,
101
+ });
102
+ }
103
+ chunks.push(buf);
104
+ }
105
+ return Buffer.concat(chunks);
106
+ }
89
107
  function simpleProxy(hostOrRequestHandler) {
90
108
  const agent = new http_1.Agent({ keepAlive: true });
91
109
  const firebaseDefaultsJSON = process.env.__FIREBASE_DEFAULTS__;
92
110
  const authTokenSyncURL = firebaseDefaultsJSON && JSON.parse(firebaseDefaultsJSON)._authTokenSyncURL;
93
111
  return async (originalReq, originalRes, next) => {
94
112
  const { method, headers, url: path } = originalReq;
113
+ originalRes.on("error", (err) => logger_1.logger.debug("Response stream error:", method, path, err));
95
114
  if (!method || !path) {
96
115
  originalRes.end();
97
116
  return;
@@ -117,6 +136,31 @@ function simpleProxy(hostOrRequestHandler) {
117
136
  "X-Forwarded-Host": headers.host,
118
137
  },
119
138
  };
139
+ let rawBody;
140
+ if (!["GET", "HEAD"].includes(method)) {
141
+ try {
142
+ rawBody = await bufferRequestBody(originalReq);
143
+ }
144
+ catch (err) {
145
+ if (err instanceof error_1.FirebaseError && err.status === 413) {
146
+ originalRes.statusCode = 413;
147
+ originalRes.end();
148
+ return;
149
+ }
150
+ logger_1.logger.debug("Error buffering request body:", method, path, err);
151
+ if (originalRes.writable) {
152
+ originalRes.statusCode = 400;
153
+ originalRes.end();
154
+ }
155
+ else {
156
+ originalRes.destroy();
157
+ }
158
+ return;
159
+ }
160
+ originalReq.rawBody = rawBody;
161
+ opts.headers["content-length"] = rawBody.length.toString();
162
+ delete opts.headers["transfer-encoding"];
163
+ }
120
164
  const req = (0, http_1.request)(opts, (response) => {
121
165
  const { statusCode, statusMessage, headers } = response;
122
166
  if (statusCode === 404) {
@@ -127,11 +171,16 @@ function simpleProxy(hostOrRequestHandler) {
127
171
  response.pipe(originalRes);
128
172
  }
129
173
  });
130
- originalReq.pipe(req);
131
174
  req.on("error", (err) => {
132
175
  logger_1.logger.debug("Error encountered while proxying request:", method, path, err);
133
176
  originalRes.end();
134
177
  });
178
+ if (rawBody !== undefined) {
179
+ req.end(rawBody);
180
+ }
181
+ else {
182
+ originalReq.pipe(req);
183
+ }
135
184
  }
136
185
  else {
137
186
  const proxiedRes = proxyResponse(originalReq, originalRes, () => {
@@ -35,10 +35,18 @@ function proxyRequestHandler(url, rewriteIdentifier, options = {}) {
35
35
  });
36
36
  const u = new url_1.URL(url + req.url);
37
37
  const c = new apiv2_1.Client({ urlPrefix: u.origin, auth: false });
38
+ let body;
38
39
  let passThrough;
39
40
  if (req.method && !["GET", "HEAD"].includes(req.method)) {
40
- passThrough = new stream_1.PassThrough();
41
- req.pipe(passThrough);
41
+ const rawBody = req.rawBody;
42
+ if (rawBody !== undefined) {
43
+ body = rawBody;
44
+ }
45
+ else {
46
+ passThrough = new stream_1.PassThrough();
47
+ req.pipe(passThrough);
48
+ body = passThrough;
49
+ }
42
50
  }
43
51
  const headers = new Headers({
44
52
  "X-Forwarded-Host": req.headers.host || "",
@@ -48,6 +56,10 @@ function proxyRequestHandler(url, rewriteIdentifier, options = {}) {
48
56
  Cookie: sessionCookie || "",
49
57
  });
50
58
  const headersToSkip = new Set(["host"]);
59
+ if (Buffer.isBuffer(body)) {
60
+ headersToSkip.add("content-length");
61
+ headersToSkip.add("transfer-encoding");
62
+ }
51
63
  for (const key of Object.keys(req.headers)) {
52
64
  if (headersToSkip.has(key)) {
53
65
  continue;
@@ -76,7 +88,7 @@ function proxyRequestHandler(url, rewriteIdentifier, options = {}) {
76
88
  resolveOnHTTPError: true,
77
89
  responseType: "stream",
78
90
  redirect: "manual",
79
- body: passThrough,
91
+ body,
80
92
  timeout: 60000,
81
93
  compress: false,
82
94
  });
package/lib/index.js CHANGED
@@ -97,7 +97,6 @@ program.action((_, args) => {
97
97
  client.cli.parse(process.argv);
98
98
  return;
99
99
  }
100
- (0, logger_1.useConsoleLoggers)();
101
100
  logger_1.logger.error(clc.bold(clc.red("Error:")), clc.bold(cmd), "is not a Firebase command");
102
101
  if (RENAMED_COMMANDS[cmd]) {
103
102
  logger_1.logger.error();