@saasquatch/program-boilerplate 3.6.1-1 → 3.6.1-3
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.
- package/README.md +6 -3
- package/dist/index.js +4 -0
- package/dist/jsonata.js +3 -4
- package/dist/trigger.js +25 -12
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -177,10 +177,13 @@ export const program: Program = {
|
|
|
177
177
|
`<my-program>.ts`
|
|
178
178
|
```typescript
|
|
179
179
|
import {program} from './program';
|
|
180
|
-
import {webtask} from '@saasquatch/program-boilerplate';
|
|
180
|
+
import {webtask, getLogger} from '@saasquatch/program-boilerplate';
|
|
181
181
|
|
|
182
|
-
|
|
183
|
-
|
|
182
|
+
const logger = getLogger();
|
|
183
|
+
const port = process.env.PORT ?? 3000;
|
|
184
|
+
|
|
185
|
+
webtask(program).listen(port, () => {
|
|
186
|
+
logger.notice(`My program running on port ${port}`);
|
|
184
187
|
});
|
|
185
188
|
```
|
|
186
189
|
|
package/dist/index.js
CHANGED
|
@@ -37,6 +37,7 @@ Object.defineProperty(exports, "triggerProgram", { enumerable: true, get: functi
|
|
|
37
37
|
const logger_1 = require("./logger");
|
|
38
38
|
Object.defineProperty(exports, "getLogger", { enumerable: true, get: function () { return logger_1.getLogger; } });
|
|
39
39
|
Object.defineProperty(exports, "setLogLevel", { enumerable: true, get: function () { return logger_1.setLogLevel; } });
|
|
40
|
+
const logger_2 = require("@saasquatch/logger");
|
|
40
41
|
const types = __importStar(require("./types"));
|
|
41
42
|
exports.types = types;
|
|
42
43
|
const jsonata_1 = require("./jsonata");
|
|
@@ -50,6 +51,7 @@ Object.defineProperty(exports, "setRewardSchedule", { enumerable: true, get: fun
|
|
|
50
51
|
Object.defineProperty(exports, "numToEquality", { enumerable: true, get: function () { return utils_1.numToEquality; } });
|
|
51
52
|
Object.defineProperty(exports, "getTriggerSchema", { enumerable: true, get: function () { return utils_1.getTriggerSchema; } });
|
|
52
53
|
Object.defineProperty(exports, "getUserCustomFieldsFromJsonata", { enumerable: true, get: function () { return utils_1.getUserCustomFieldsFromJsonata; } });
|
|
54
|
+
const logger_3 = require("@saasquatch/logger");
|
|
53
55
|
/**
|
|
54
56
|
* Returns an express server that serves the provided handlers
|
|
55
57
|
* as a program
|
|
@@ -61,8 +63,10 @@ Object.defineProperty(exports, "getUserCustomFieldsFromJsonata", { enumerable: t
|
|
|
61
63
|
function webtask(program = {}) {
|
|
62
64
|
const compression = require("compression");
|
|
63
65
|
const app = express_1.default();
|
|
66
|
+
const logger = logger_2.getLogger("program-boilerplate");
|
|
64
67
|
app.use(express_1.default.json({ limit: process.env.MAX_PAYLOAD_SIZE || "1mb" }));
|
|
65
68
|
app.use(compression());
|
|
69
|
+
app.use(logger_3.httpLogMiddleware(logger));
|
|
66
70
|
// Enforce HTTPS. The server does not redirect http -> https
|
|
67
71
|
// because OWASP advises not to
|
|
68
72
|
app.use((req, res, next) => {
|
package/dist/jsonata.js
CHANGED
|
@@ -5,10 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.timeboxedJsonata = exports.safeJsonata = exports.timeboxExpression = void 0;
|
|
7
7
|
const jsonata_1 = __importDefault(require("jsonata"));
|
|
8
|
-
const logger_1 = require("
|
|
8
|
+
const logger_1 = require("@saasquatch/logger");
|
|
9
9
|
const TIMEOUT = 5000;
|
|
10
10
|
const MAXDEPTH = 1000;
|
|
11
|
-
const logger = logger_1.getLogger(process.env.PROGRAM_LOG_LEVEL || "debug");
|
|
12
11
|
/**
|
|
13
12
|
* Protect the process/browser from a runnaway expression
|
|
14
13
|
* i.e. Infinite loop (tail recursion), or excessive stack growth
|
|
@@ -61,7 +60,7 @@ function safeJsonata(expression, inputData) {
|
|
|
61
60
|
return jsonataQuery.evaluate(inputData);
|
|
62
61
|
}
|
|
63
62
|
catch (e) {
|
|
64
|
-
|
|
63
|
+
logger_1.getLogger("program-boilerplate").warn(`Failed to evaluate JSONata expression: ${e.message}`);
|
|
65
64
|
}
|
|
66
65
|
}
|
|
67
66
|
exports.safeJsonata = safeJsonata;
|
|
@@ -74,7 +73,7 @@ function timeboxedJsonata(expression, inputData, timeout, maxDepth) {
|
|
|
74
73
|
}
|
|
75
74
|
catch (e) {
|
|
76
75
|
if (e instanceof Error) {
|
|
77
|
-
|
|
76
|
+
logger_1.getLogger("program-boilerplate").warn(`Failed to evaluate JSONata expression: ${e.message}`);
|
|
78
77
|
}
|
|
79
78
|
return { success: false, result: undefined };
|
|
80
79
|
}
|
package/dist/trigger.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.triggerProgram = void 0;
|
|
7
7
|
const transaction_1 = __importDefault(require("./transaction"));
|
|
8
|
+
const logger_1 = require("@saasquatch/logger");
|
|
8
9
|
/**
|
|
9
10
|
* Triggers the program and returns the result (JSON + HTTP code)
|
|
10
11
|
*
|
|
@@ -38,12 +39,10 @@ function triggerProgram(body, program = {}) {
|
|
|
38
39
|
body = body;
|
|
39
40
|
return handleProgramVariableSchemaRequest(body, program);
|
|
40
41
|
default:
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
const message = `Unrecognized messageType ${body.messageType}`;
|
|
43
|
+
logger_1.getLogger("program-boilerplate").warn(message);
|
|
43
44
|
return {
|
|
44
|
-
json: {
|
|
45
|
-
message: `Unrecognized messageType ${body.messageType}`,
|
|
46
|
-
},
|
|
45
|
+
json: { message },
|
|
47
46
|
code: 501,
|
|
48
47
|
};
|
|
49
48
|
}
|
|
@@ -76,8 +75,11 @@ function handleProgramTrigger(body, program) {
|
|
|
76
75
|
// consider not returning stack trace for security reasons
|
|
77
76
|
message: e.stack,
|
|
78
77
|
};
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
logger_1.getLogger("program-boilerplate").error({
|
|
79
|
+
message: errorMes.error,
|
|
80
|
+
["error.message"]: e.message,
|
|
81
|
+
["error.stack"]: e.stack,
|
|
82
|
+
});
|
|
81
83
|
return {
|
|
82
84
|
json: errorMes,
|
|
83
85
|
code: 500,
|
|
@@ -115,8 +117,11 @@ function handleProgramIntrospection(body, program) {
|
|
|
115
117
|
// consider not returning stack trace for security reasons
|
|
116
118
|
message: e.stack,
|
|
117
119
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
+
logger_1.getLogger("program-boilerplate").error({
|
|
121
|
+
message: "Error ocurred in a webtask",
|
|
122
|
+
["error.message"]: e.message,
|
|
123
|
+
["error.stack"]: e.stack,
|
|
124
|
+
});
|
|
120
125
|
return {
|
|
121
126
|
json: errorMes,
|
|
122
127
|
code: 500,
|
|
@@ -179,11 +184,19 @@ function handleProgramVariableSchemaRequest(body, program) {
|
|
|
179
184
|
catch (e) {
|
|
180
185
|
const errorMes = {
|
|
181
186
|
error: "An error occurred in a webtask (PROGRAM_TRIGGER_VARIABLES_SCHEMA_REQUEST)",
|
|
182
|
-
// consider not returning stack trace for security reasons
|
|
183
187
|
message: e.stack,
|
|
184
188
|
};
|
|
185
|
-
|
|
186
|
-
|
|
189
|
+
logger_1.getLogger("program-boilerplate").error({
|
|
190
|
+
message: errorMes.error,
|
|
191
|
+
["error.message"]: e.message,
|
|
192
|
+
["error.stack"]: e.stack,
|
|
193
|
+
});
|
|
194
|
+
// FIXME: This code wasn't actually returning a 500 before this, was that meant to be the case
|
|
195
|
+
// or did we just forget?
|
|
196
|
+
return {
|
|
197
|
+
json: errorMes,
|
|
198
|
+
code: 500,
|
|
199
|
+
};
|
|
187
200
|
}
|
|
188
201
|
if (!newSchema) {
|
|
189
202
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saasquatch/program-boilerplate",
|
|
3
|
-
"version": "3.6.1-
|
|
3
|
+
"version": "3.6.1-3",
|
|
4
4
|
"description": "Boilerplate for writing programs",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@saasquatch/jsonata-paths-extractor": "^1.0.1",
|
|
36
|
-
"@saasquatch/logger": "^1.0.0-
|
|
36
|
+
"@saasquatch/logger": "^1.0.0-3",
|
|
37
37
|
"bson-objectid": "^1.3.1",
|
|
38
38
|
"compression": "^1.7.4",
|
|
39
39
|
"express": "^4.17.1",
|