|
| 1 | +const express = require('express'); |
| 2 | +const {HTTP} = require("cloudevents"); |
| 3 | +const jsonata = require('jsonata'); |
| 4 | +const fs = require('node:fs'); |
| 5 | + |
| 6 | +const port = process.env.PORT = process.env.PORT || 8080; |
| 7 | +const k_sink = process.env.K_SINK || undefined; |
| 8 | +const jsonata_transform_file_name = process.env.JSONATA_TRANSFORM_FILE_NAME || undefined; |
| 9 | + |
| 10 | +if (!jsonata_transform_file_name) { |
| 11 | + throw new Error("undefined JSONATA_TRANSFORM_FILE_NAME env variable"); |
| 12 | +} |
| 13 | + |
| 14 | +let jsonata_transform = null |
| 15 | + |
| 16 | +try { |
| 17 | + const jsonata_transform_file_content = fs.readFileSync(jsonata_transform_file_name, "utf-8") |
| 18 | + jsonata_transform = jsonata(jsonata_transform_file_content); |
| 19 | +} catch (error) { |
| 20 | + throw new Error(`Failed to parse Jsonata transform file in ${jsonata_transform_file_name}: ${error}`); |
| 21 | +} |
| 22 | + |
| 23 | +function logDebug(...inputs) { |
| 24 | + if (process.env.NODE_ENV === "development") { |
| 25 | + console.debug(...inputs); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const app = express() |
| 30 | +app.use(express.json()); |
| 31 | +app.use(express.text()); |
| 32 | +app.use(express.raw({type: '*/*'})); |
| 33 | +app.use((req, res, next) => { |
| 34 | + if (Buffer.isBuffer(req.body)) { |
| 35 | + try { |
| 36 | + req.rawBody = JSON.parse(req.body); |
| 37 | + } catch (error) { |
| 38 | + logDebug("Falling back to convert body to string"); |
| 39 | + req.rawBody = req.body.toString() |
| 40 | + } |
| 41 | + } else { |
| 42 | + req.rawBody = req.body; |
| 43 | + } |
| 44 | + next(); |
| 45 | +}); |
| 46 | + |
| 47 | +app.post("/", async (req, res) => { |
| 48 | + try { |
| 49 | + let input = null |
| 50 | + try { |
| 51 | + const ceInput = HTTP.toEvent({headers: req.headers, body: req.rawBody}); |
| 52 | + if (Array.isArray(ceInput)) { |
| 53 | + logDebug("Unsupported batch ceInput") |
| 54 | + return res |
| 55 | + .header("Reason", "Unsupported batch input") |
| 56 | + .status(400) |
| 57 | + .send(); |
| 58 | + } |
| 59 | + input = JSON.parse(HTTP.structured(ceInput).body) |
| 60 | + } catch (error) { |
| 61 | + logDebug(`Failed to deserialize CloudEvent, falling back to raw body`, JSON.stringify(req.rawBody), error) |
| 62 | + input = req.rawBody |
| 63 | + } |
| 64 | + |
| 65 | + logDebug("input", JSON.stringify(input)); |
| 66 | + |
| 67 | + const transformed = await jsonata_transform.evaluate(input) |
| 68 | + if (k_sink) { |
| 69 | + logDebug(`K_SINK is set, sending event to it ${k_sink}`) |
| 70 | + |
| 71 | + try { |
| 72 | + const response = await fetch(k_sink, { |
| 73 | + method: "POST", |
| 74 | + headers: { |
| 75 | + "Content-Type": "application/json", |
| 76 | + }, |
| 77 | + body: JSON.stringify(transformed), |
| 78 | + }) |
| 79 | + logDebug(`K_SINK received response ${response.status}`) |
| 80 | + |
| 81 | + return res |
| 82 | + .status(response.status) |
| 83 | + .send() |
| 84 | + } catch (error) { |
| 85 | + return res |
| 86 | + .header("Reason", error.toString()) |
| 87 | + .status(502) |
| 88 | + .send() |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + logDebug("Transformed input", JSON.stringify(transformed, null, 2)) |
| 93 | + |
| 94 | + return res |
| 95 | + .header("Content-Type", "application/json") |
| 96 | + .status(200) |
| 97 | + .send(transformed); |
| 98 | + |
| 99 | + } catch (error) { |
| 100 | + console.error(error); |
| 101 | + return res |
| 102 | + .header("Reason", error.toString()) |
| 103 | + .status(500) |
| 104 | + .send() |
| 105 | + } |
| 106 | +}); |
| 107 | + |
| 108 | +app.get('/healthz', (req, res) => { |
| 109 | + res.status(200).send('OK'); |
| 110 | +}); |
| 111 | + |
| 112 | +app.get('/readyz', (req, res) => { |
| 113 | + res.status(200).send('READY'); |
| 114 | +}); |
| 115 | + |
| 116 | +app.disable('x-powered-by'); |
| 117 | + |
| 118 | +const server = app.listen(port, () => { |
| 119 | + console.log(`Jsonata server listening on port ${port}`) |
| 120 | +}) |
| 121 | + |
| 122 | +process.on('SIGINT', shutDown); |
| 123 | +process.on('SIGTERM', shutDownNow); |
| 124 | + |
| 125 | +let connections = []; |
| 126 | + |
| 127 | +server.on('connection', connection => { |
| 128 | + connections.push(connection); |
| 129 | + connection.on('close', () => connections = connections.filter(curr => curr !== connection)); |
| 130 | +}); |
| 131 | + |
| 132 | +function shutDown() { |
| 133 | + console.log('Received interrupt signal, shutting down gracefully'); |
| 134 | + |
| 135 | + if (connections.length === 0) { |
| 136 | + shutDownNow() |
| 137 | + } else { |
| 138 | + setTimeout(() => { |
| 139 | + shutDownNow() |
| 140 | + }, 5000); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +function shutDownNow() { |
| 145 | + console.log('Shutting down gracefully'); |
| 146 | + |
| 147 | + server.close(() => { |
| 148 | + console.log('Closed out remaining connections'); |
| 149 | + process.exit(0); |
| 150 | + }); |
| 151 | + |
| 152 | + setTimeout(() => { |
| 153 | + console.error('Could not close connections in time, forcefully shutting down'); |
| 154 | + process.exit(1); |
| 155 | + }, 10000); |
| 156 | + |
| 157 | + connections.forEach(curr => { |
| 158 | + if (curr) { |
| 159 | + curr.end() |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + setTimeout(() => connections.forEach(curr => { |
| 164 | + if (curr) { |
| 165 | + curr.destroy() |
| 166 | + } |
| 167 | + }), 7000); |
| 168 | +} |
0 commit comments