UNPKG

3.24 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Copyright 2021 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.isRawHttpRequest = exports.makeHttpRequestData = void 0;
19/**
20 * makeHttpRequestData turns raw incoming HTTPRequests into structured
21 * HTTPRequest objects interpreted by Cloud Logging.
22 *
23 * @param req
24 * @param res
25 * @param latencyMilliseconds
26 */
27function makeHttpRequestData(req, res, latencyMilliseconds) {
28 let requestUrl, protocol, requestMethod, userAgent, referer, status, responseSize, latency;
29 // Format request properties
30 if (req.url)
31 requestUrl = req.url;
32 // OriginalURL overwrites inferred url
33 if ('originalUrl' in req && req.originalUrl)
34 requestUrl = req.originalUrl;
35 // Format protocol from valid URL
36 if (requestUrl) {
37 try {
38 const url = new URL(requestUrl);
39 protocol = url.protocol;
40 }
41 catch (e) {
42 // Library should not panic
43 }
44 }
45 req.method ? (requestMethod = req.method) : null;
46 if (req.headers && req.headers['user-agent']) {
47 req.headers['user-agent'] ? (userAgent = req.headers['user-agent']) : null;
48 req.headers['referer'] ? (referer = req.headers['referer']) : null;
49 }
50 // Format response properties
51 if (res) {
52 res.statusCode ? (status = res.statusCode) : null;
53 responseSize =
54 (res.getHeader && Number(res.getHeader('Content-Length'))) || 0;
55 }
56 // Format latency
57 if (latencyMilliseconds) {
58 latency = {
59 seconds: Math.floor(latencyMilliseconds / 1e3),
60 nanos: Math.floor((latencyMilliseconds % 1e3) * 1e6),
61 };
62 }
63 // Only include the property if its value exists
64 return Object.assign({}, requestUrl ? { requestUrl } : null, protocol ? { protocol } : null, requestMethod ? { requestMethod } : null, userAgent ? { userAgent } : null, referer ? { referer } : null, responseSize ? { responseSize } : null, status ? { status } : null, latency ? { latency } : null);
65}
66exports.makeHttpRequestData = makeHttpRequestData;
67/**
68 * isRawHttpRequest detects whether a request object extends the
69 * http.IncomingMessage class. It should return true on HTTP compliant requests
70 * and all requests created by an http.Server.
71 *
72 * @param req
73 */
74// eslint-disable-next-line @typescript-eslint/no-explicit-any
75function isRawHttpRequest(req) {
76 if (req &&
77 ('originalUrl' in req ||
78 'headers' in req ||
79 'method' in req ||
80 'url' in req)) {
81 return true;
82 }
83 return false;
84}
85exports.isRawHttpRequest = isRawHttpRequest;
86//# sourceMappingURL=http-request.js.map
\No newline at end of file