@aeriajs/security 0.0.0
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/LICENSE +19 -0
- package/README.md +17 -0
- package/dist/define.d.ts +2 -0
- package/dist/define.js +7 -0
- package/dist/define.mjs +3 -0
- package/dist/immutability.d.ts +4 -0
- package/dist/immutability.js +54 -0
- package/dist/immutability.mjs +325 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +22 -0
- package/dist/index.mjs +6 -0
- package/dist/ownership.d.ts +5 -0
- package/dist/ownership.js +34 -0
- package/dist/ownership.mjs +180 -0
- package/dist/pagination.d.ts +3 -0
- package/dist/pagination.js +15 -0
- package/dist/pagination.mjs +149 -0
- package/dist/rateLimiting.d.ts +6 -0
- package/dist/rateLimiting.js +59 -0
- package/dist/rateLimiting.mjs +243 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.js +2 -0
- package/dist/types.mjs +1 -0
- package/dist/use.d.ts +5 -0
- package/dist/use.js +55 -0
- package/dist/use.mjs +266 -0
- package/package.json +38 -0
package/dist/use.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSecurity = void 0;
|
|
4
|
+
const common_1 = require("@aeriajs/common");
|
|
5
|
+
const index_js_1 = require("./index.js");
|
|
6
|
+
const chainFunctions = async (_props, context, functions) => {
|
|
7
|
+
const props = Object.assign({
|
|
8
|
+
filters: {},
|
|
9
|
+
}, _props);
|
|
10
|
+
for (const fn of functions) {
|
|
11
|
+
const resultEither = await fn(props, context);
|
|
12
|
+
if ((0, common_1.isLeft)(resultEither)) {
|
|
13
|
+
return resultEither;
|
|
14
|
+
}
|
|
15
|
+
const result = (0, common_1.unwrapEither)(resultEither);
|
|
16
|
+
Object.assign(props.payload, result);
|
|
17
|
+
}
|
|
18
|
+
return (0, common_1.right)(props.payload);
|
|
19
|
+
};
|
|
20
|
+
const useSecurity = (context) => {
|
|
21
|
+
const options = context.description.options
|
|
22
|
+
? Object.assign({}, context.description.options)
|
|
23
|
+
: {};
|
|
24
|
+
const beforeRead = async (payload) => {
|
|
25
|
+
const newPayload = Object.assign({}, payload);
|
|
26
|
+
newPayload.filters ??= {};
|
|
27
|
+
if (options.queryPreset) {
|
|
28
|
+
Object.assign(newPayload, (0, common_1.deepMerge)(newPayload, options.queryPreset));
|
|
29
|
+
}
|
|
30
|
+
const props = {
|
|
31
|
+
payload: newPayload,
|
|
32
|
+
};
|
|
33
|
+
return chainFunctions(props, context, [
|
|
34
|
+
index_js_1.checkPagination,
|
|
35
|
+
index_js_1.checkOwnershipRead,
|
|
36
|
+
]);
|
|
37
|
+
};
|
|
38
|
+
const beforeWrite = async (payload) => {
|
|
39
|
+
const newPayload = Object.assign({
|
|
40
|
+
what: {},
|
|
41
|
+
}, payload);
|
|
42
|
+
const props = {
|
|
43
|
+
payload: newPayload,
|
|
44
|
+
};
|
|
45
|
+
return chainFunctions(props, context, [
|
|
46
|
+
index_js_1.checkOwnershipWrite,
|
|
47
|
+
index_js_1.checkImmutability,
|
|
48
|
+
]);
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
beforeRead,
|
|
52
|
+
beforeWrite,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
exports.useSecurity = useSecurity;
|
package/dist/use.mjs
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
+
try {
|
|
3
|
+
var info = gen[key](arg);
|
|
4
|
+
var value = info.value;
|
|
5
|
+
} catch (error) {
|
|
6
|
+
reject(error);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (info.done) {
|
|
10
|
+
resolve(value);
|
|
11
|
+
} else {
|
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function _async_to_generator(fn) {
|
|
16
|
+
return function() {
|
|
17
|
+
var self = this, args = arguments;
|
|
18
|
+
return new Promise(function(resolve, reject) {
|
|
19
|
+
var gen = fn.apply(self, args);
|
|
20
|
+
function _next(value) {
|
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
+
}
|
|
23
|
+
function _throw(err) {
|
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
+
}
|
|
26
|
+
_next(undefined);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function _ts_generator(thisArg, body) {
|
|
31
|
+
var f, y, t, g, _ = {
|
|
32
|
+
label: 0,
|
|
33
|
+
sent: function() {
|
|
34
|
+
if (t[0] & 1) throw t[1];
|
|
35
|
+
return t[1];
|
|
36
|
+
},
|
|
37
|
+
trys: [],
|
|
38
|
+
ops: []
|
|
39
|
+
};
|
|
40
|
+
return g = {
|
|
41
|
+
next: verb(0),
|
|
42
|
+
"throw": verb(1),
|
|
43
|
+
"return": verb(2)
|
|
44
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
45
|
+
return this;
|
|
46
|
+
}), g;
|
|
47
|
+
function verb(n) {
|
|
48
|
+
return function(v) {
|
|
49
|
+
return step([
|
|
50
|
+
n,
|
|
51
|
+
v
|
|
52
|
+
]);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function step(op) {
|
|
56
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
57
|
+
while(_)try {
|
|
58
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
59
|
+
if (y = 0, t) op = [
|
|
60
|
+
op[0] & 2,
|
|
61
|
+
t.value
|
|
62
|
+
];
|
|
63
|
+
switch(op[0]){
|
|
64
|
+
case 0:
|
|
65
|
+
case 1:
|
|
66
|
+
t = op;
|
|
67
|
+
break;
|
|
68
|
+
case 4:
|
|
69
|
+
_.label++;
|
|
70
|
+
return {
|
|
71
|
+
value: op[1],
|
|
72
|
+
done: false
|
|
73
|
+
};
|
|
74
|
+
case 5:
|
|
75
|
+
_.label++;
|
|
76
|
+
y = op[1];
|
|
77
|
+
op = [
|
|
78
|
+
0
|
|
79
|
+
];
|
|
80
|
+
continue;
|
|
81
|
+
case 7:
|
|
82
|
+
op = _.ops.pop();
|
|
83
|
+
_.trys.pop();
|
|
84
|
+
continue;
|
|
85
|
+
default:
|
|
86
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
87
|
+
_ = 0;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
91
|
+
_.label = op[1];
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
95
|
+
_.label = t[1];
|
|
96
|
+
t = op;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (t && _.label < t[2]) {
|
|
100
|
+
_.label = t[2];
|
|
101
|
+
_.ops.push(op);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
if (t[2]) _.ops.pop();
|
|
105
|
+
_.trys.pop();
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
op = body.call(thisArg, _);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
op = [
|
|
111
|
+
6,
|
|
112
|
+
e
|
|
113
|
+
];
|
|
114
|
+
y = 0;
|
|
115
|
+
} finally{
|
|
116
|
+
f = t = 0;
|
|
117
|
+
}
|
|
118
|
+
if (op[0] & 5) throw op[1];
|
|
119
|
+
return {
|
|
120
|
+
value: op[0] ? op[1] : void 0,
|
|
121
|
+
done: true
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
import { deepMerge, right, isLeft, unwrapEither } from "@aeriajs/common";
|
|
126
|
+
import { checkImmutability, checkOwnershipRead, checkOwnershipWrite, checkPagination } from "./index.mjs";
|
|
127
|
+
var chainFunctions = function() {
|
|
128
|
+
var _ref = _async_to_generator(function(_props, context, functions) {
|
|
129
|
+
var props, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, fn, resultEither, result, err;
|
|
130
|
+
return _ts_generator(this, function(_state) {
|
|
131
|
+
switch(_state.label){
|
|
132
|
+
case 0:
|
|
133
|
+
props = Object.assign({
|
|
134
|
+
filters: {}
|
|
135
|
+
}, _props);
|
|
136
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
137
|
+
_state.label = 1;
|
|
138
|
+
case 1:
|
|
139
|
+
_state.trys.push([
|
|
140
|
+
1,
|
|
141
|
+
6,
|
|
142
|
+
7,
|
|
143
|
+
8
|
|
144
|
+
]);
|
|
145
|
+
_iterator = functions[Symbol.iterator]();
|
|
146
|
+
_state.label = 2;
|
|
147
|
+
case 2:
|
|
148
|
+
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
|
|
149
|
+
3,
|
|
150
|
+
5
|
|
151
|
+
];
|
|
152
|
+
fn = _step.value;
|
|
153
|
+
return [
|
|
154
|
+
4,
|
|
155
|
+
fn(props, context)
|
|
156
|
+
];
|
|
157
|
+
case 3:
|
|
158
|
+
resultEither = _state.sent();
|
|
159
|
+
if (isLeft(resultEither)) {
|
|
160
|
+
return [
|
|
161
|
+
2,
|
|
162
|
+
resultEither
|
|
163
|
+
];
|
|
164
|
+
}
|
|
165
|
+
result = unwrapEither(resultEither);
|
|
166
|
+
Object.assign(props.payload, result);
|
|
167
|
+
_state.label = 4;
|
|
168
|
+
case 4:
|
|
169
|
+
_iteratorNormalCompletion = true;
|
|
170
|
+
return [
|
|
171
|
+
3,
|
|
172
|
+
2
|
|
173
|
+
];
|
|
174
|
+
case 5:
|
|
175
|
+
return [
|
|
176
|
+
3,
|
|
177
|
+
8
|
|
178
|
+
];
|
|
179
|
+
case 6:
|
|
180
|
+
err = _state.sent();
|
|
181
|
+
_didIteratorError = true;
|
|
182
|
+
_iteratorError = err;
|
|
183
|
+
return [
|
|
184
|
+
3,
|
|
185
|
+
8
|
|
186
|
+
];
|
|
187
|
+
case 7:
|
|
188
|
+
try {
|
|
189
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
190
|
+
_iterator.return();
|
|
191
|
+
}
|
|
192
|
+
} finally{
|
|
193
|
+
if (_didIteratorError) {
|
|
194
|
+
throw _iteratorError;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return [
|
|
198
|
+
7
|
|
199
|
+
];
|
|
200
|
+
case 8:
|
|
201
|
+
return [
|
|
202
|
+
2,
|
|
203
|
+
right(props.payload)
|
|
204
|
+
];
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
return function chainFunctions(_props, context, functions) {
|
|
209
|
+
return _ref.apply(this, arguments);
|
|
210
|
+
};
|
|
211
|
+
}();
|
|
212
|
+
export var useSecurity = function(context) {
|
|
213
|
+
var options = context.description.options ? Object.assign({}, context.description.options) : {};
|
|
214
|
+
var beforeRead = function() {
|
|
215
|
+
var _ref = _async_to_generator(function(payload) {
|
|
216
|
+
var _newPayload, newPayload, _filters, props;
|
|
217
|
+
return _ts_generator(this, function(_state) {
|
|
218
|
+
newPayload = Object.assign({}, payload);
|
|
219
|
+
(_filters = (_newPayload = newPayload).filters) !== null && _filters !== void 0 ? _filters : _newPayload.filters = {};
|
|
220
|
+
if (options.queryPreset) {
|
|
221
|
+
Object.assign(newPayload, deepMerge(newPayload, options.queryPreset));
|
|
222
|
+
}
|
|
223
|
+
props = {
|
|
224
|
+
payload: newPayload
|
|
225
|
+
};
|
|
226
|
+
return [
|
|
227
|
+
2,
|
|
228
|
+
chainFunctions(props, context, [
|
|
229
|
+
checkPagination,
|
|
230
|
+
checkOwnershipRead
|
|
231
|
+
])
|
|
232
|
+
];
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
return function beforeRead(payload) {
|
|
236
|
+
return _ref.apply(this, arguments);
|
|
237
|
+
};
|
|
238
|
+
}();
|
|
239
|
+
var beforeWrite = function() {
|
|
240
|
+
var _ref = _async_to_generator(function(payload) {
|
|
241
|
+
var newPayload, props;
|
|
242
|
+
return _ts_generator(this, function(_state) {
|
|
243
|
+
newPayload = Object.assign({
|
|
244
|
+
what: {}
|
|
245
|
+
}, payload);
|
|
246
|
+
props = {
|
|
247
|
+
payload: newPayload
|
|
248
|
+
};
|
|
249
|
+
return [
|
|
250
|
+
2,
|
|
251
|
+
chainFunctions(props, context, [
|
|
252
|
+
checkOwnershipWrite,
|
|
253
|
+
checkImmutability
|
|
254
|
+
])
|
|
255
|
+
];
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
return function beforeWrite(payload) {
|
|
259
|
+
return _ref.apply(this, arguments);
|
|
260
|
+
};
|
|
261
|
+
}();
|
|
262
|
+
return {
|
|
263
|
+
beforeRead: beforeRead,
|
|
264
|
+
beforeWrite: beforeWrite
|
|
265
|
+
};
|
|
266
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aeriajs/security",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./dist/index.mjs",
|
|
17
|
+
"require": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@aeriajs/api": "^0.0.0",
|
|
26
|
+
"@aeriajs/common": "^0.0.0",
|
|
27
|
+
"@aeriajs/types": "^0.0.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "echo skipping",
|
|
31
|
+
"lint": "eslint src",
|
|
32
|
+
"lint:fix": "eslint src --fix",
|
|
33
|
+
"build": "pnpm build:cjs && pnpm build:esm",
|
|
34
|
+
"build:cjs": "tsc",
|
|
35
|
+
"build:esm": "swc src/* -d dist --strip-leading-paths -C module.type=es6 --out-file-extension=mjs && pnpm build:esm-transform",
|
|
36
|
+
"build:esm-transform": "pnpm -w esm-transform $PWD/dist"
|
|
37
|
+
}
|
|
38
|
+
}
|