@teamkeel/functions-runtime 0.300.1 → 0.300.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/package.json +1 -1
- package/src/consts.js +0 -8
- package/src/handleRequest.js +30 -12
- package/src/permissions.js +0 -5
- package/src/permissions.test.js +0 -22
package/package.json
CHANGED
package/src/consts.js
CHANGED
|
@@ -9,12 +9,4 @@ const PROTO_ACTION_TYPES = {
|
|
|
9
9
|
WRITE: "OPERATION_TYPE_WRITE",
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
const PROTO_ACTION_TYPES_REQUEST_HANDLER = [
|
|
13
|
-
"OPERATION_TYPE_CREATE",
|
|
14
|
-
"OPERATION_TYPE_GET",
|
|
15
|
-
"OPERATION_TYPE_LIST",
|
|
16
|
-
];
|
|
17
|
-
|
|
18
|
-
module.exports.PROTO_ACTION_TYPES_REQUEST_HANDLER =
|
|
19
|
-
PROTO_ACTION_TYPES_REQUEST_HANDLER;
|
|
20
12
|
module.exports.PROTO_ACTION_TYPES = PROTO_ACTION_TYPES;
|
package/src/handleRequest.js
CHANGED
|
@@ -10,7 +10,7 @@ const {
|
|
|
10
10
|
PermissionError,
|
|
11
11
|
checkBuiltInPermissions,
|
|
12
12
|
} = require("./permissions");
|
|
13
|
-
const {
|
|
13
|
+
const { PROTO_ACTION_TYPES } = require("./consts");
|
|
14
14
|
|
|
15
15
|
const { errorToJSONRPCResponse, RuntimeErrors } = require("./errors");
|
|
16
16
|
|
|
@@ -72,19 +72,37 @@ async function handleRequest(request, config) {
|
|
|
72
72
|
const relevantPermissions = permissions[request.method];
|
|
73
73
|
|
|
74
74
|
const actionType = actionTypes[request.method];
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
75
|
+
|
|
76
|
+
const peakInsideTransaction =
|
|
77
|
+
actionType === PROTO_ACTION_TYPES.CREATE;
|
|
78
|
+
|
|
79
|
+
let rowsForPermissions = [];
|
|
80
|
+
switch (actionType) {
|
|
81
|
+
case PROTO_ACTION_TYPES.LIST:
|
|
82
|
+
rowsForPermissions = fnResult;
|
|
83
|
+
|
|
84
|
+
break;
|
|
85
|
+
case PROTO_ACTION_TYPES.DELETE:
|
|
86
|
+
rowsForPermissions = [{ id: fnResult }];
|
|
87
|
+
break;
|
|
88
|
+
default:
|
|
89
|
+
rowsForPermissions = [fnResult];
|
|
90
|
+
break;
|
|
86
91
|
}
|
|
87
92
|
|
|
93
|
+
// check will throw a PermissionError if a permission rule is invalid
|
|
94
|
+
await checkBuiltInPermissions({
|
|
95
|
+
rows: rowsForPermissions,
|
|
96
|
+
permissions: relevantPermissions,
|
|
97
|
+
// it is important that we pass db here as db represents the connection to the database
|
|
98
|
+
// *outside* of the current transaction. Given that any changes inside of a transaction
|
|
99
|
+
// are opaque to the outside, we can utilize this when running permission rules and then deciding to
|
|
100
|
+
// rollback any changes if they do not pass. However, for creates we need to be able to 'peak' inside the transaction to read the created record, as this won't exist outside of the transaction.
|
|
101
|
+
db: peakInsideTransaction ? transaction : db,
|
|
102
|
+
ctx,
|
|
103
|
+
functionName: request.method,
|
|
104
|
+
});
|
|
105
|
+
|
|
88
106
|
// If the built in permission check above doesn't throw, then it means that the request is permitted and we can continue returning the return value from the custom function out of the transaction
|
|
89
107
|
return fnResult;
|
|
90
108
|
}
|
package/src/permissions.js
CHANGED
|
@@ -57,11 +57,6 @@ const checkBuiltInPermissions = async ({
|
|
|
57
57
|
db,
|
|
58
58
|
functionName,
|
|
59
59
|
}) => {
|
|
60
|
-
// rows can actually just be a single record too so we need to wrap it
|
|
61
|
-
if (!Array.isArray(rows)) {
|
|
62
|
-
rows = [rows];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
60
|
for (const permissionFn of permissions) {
|
|
66
61
|
const result = await permissionFn(rows, ctx, db);
|
|
67
62
|
|
package/src/permissions.test.js
CHANGED
|
@@ -117,26 +117,4 @@ describe("check", () => {
|
|
|
117
117
|
})
|
|
118
118
|
).rejects.toThrow();
|
|
119
119
|
});
|
|
120
|
-
|
|
121
|
-
test("with a single row", async () => {
|
|
122
|
-
const permissionRule1 = (records, ctx, db) => {
|
|
123
|
-
// Only allow names starting with Adam
|
|
124
|
-
return records.every((r) => r.name.startsWith("Adam"));
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
const rows = {
|
|
128
|
-
id: "123",
|
|
129
|
-
name: "Adam Bull",
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
await expect(
|
|
133
|
-
checkBuiltInPermissions({
|
|
134
|
-
rows,
|
|
135
|
-
ctx,
|
|
136
|
-
db,
|
|
137
|
-
functionName,
|
|
138
|
-
permissions: [permissionRule1],
|
|
139
|
-
})
|
|
140
|
-
).resolves.ok;
|
|
141
|
-
});
|
|
142
120
|
});
|