-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (110 loc) · 3.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Firestore } from '@google-cloud/firestore';
import cors from 'cors';
import 'dotenv/config';
import express from 'express';
import fs from 'fs';
import helmet from 'helmet';
import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware';
import https from 'https';
const firestore = new Firestore();
export const WEB_MAP_AS_JSON = 'Web_Map_as_JSON';
if (!process.env.OPEN_QUAD_WORD) {
throw 'OPEN_QUAD_WORD environment variable is not defined!';
}
let app = express();
app.use(
helmet({
crossOriginResourcePolicy: { policy: 'cross-origin' },
}),
);
app.use(cors());
// required for parsing submitted form data
app.use(
express.urlencoded({
extended: true,
// these options are to address the "PayloadTooLargeError: request entity too large errors" that we are seeing
limit: '50mb',
parameterLimit: 2000,
}),
);
app.use(express.json());
async function getAccountSnapshot(accountNumber) {
return await firestore.collection('accounts').doc(accountNumber).get();
}
// verify account number for all paths requests except "/"
app.use(async (request, response, next) => {
if (request.path !== '/') {
const accountNumber = request.path.split('/')[2];
if (!accountNumber) {
response.status(400);
return response.send('Account number is required');
}
const snapshot = await getAccountSnapshot(accountNumber);
if (!snapshot.exists) {
response.status(400);
return response.send(`Invalid account number: ${accountNumber}`);
}
console.log({
version: process.env.npm_package_version,
accountNumber: accountNumber,
url: request.path,
method: request.method,
});
response.locals.account = snapshot.data();
}
return next();
});
app.use(
'/v2',
createProxyMiddleware({
changeOrigin: true,
pathRewrite: {
'^/.*?/': '/', // remove the account prefix
},
on: {
proxyReq: (proxyReq, request, response) => {
// POST is used for requests with too much data to fit in query parameters
if (request.method === 'POST' && request.body[WEB_MAP_AS_JSON]) {
request.body[WEB_MAP_AS_JSON] = request.body[WEB_MAP_AS_JSON].replace(
new RegExp(response.locals.account.quadWord, 'g'),
process.env.OPEN_QUAD_WORD,
);
} else if (proxyReq.path.includes(WEB_MAP_AS_JSON)) {
proxyReq.path = proxyReq.path.replace(
new RegExp(response.locals.account.quadWord, 'g'),
process.env.OPEN_QUAD_WORD,
);
}
// used to fix proxied POST requests when bodyParser is applied before http-proxy-middleware
// ref: https://www.npmjs.com/package/http-proxy-middleware#intercept-and-manipulate-requests
fixRequestBody(proxyReq, request);
},
},
router: async (request) => {
// response is not passed to this function
const accountNumber = request.path.split('/')[1];
const snapshot = await getAccountSnapshot(accountNumber);
const url = new URL(snapshot.data().arcgisServer);
return {
protocol: url.protocol,
host: url.hostname,
port: url.port,
};
},
}),
);
app.get('/', (_, response) => response.redirect(301, 'https://github.com/agrc/serverless-print-proxy#readme'));
const port = process.env.PORT || 8080;
if (process.env.NODE_ENV !== 'test') {
if (process.env.NODE_ENV === 'development') {
const credentials = {
key: fs.readFileSync('./localhost-key.pem'),
cert: fs.readFileSync('./localhost.pem'),
};
app = https.createServer(credentials, app);
}
app.listen(port, () => {
console.log(`printproxy listing on port ${port}`);
});
}
export default app;