@flowerforce/flowerbase 1.2.0 → 1.2.1-beta.11
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 +28 -3
- package/dist/auth/controller.d.ts.map +1 -1
- package/dist/auth/controller.js +57 -3
- package/dist/auth/plugins/jwt.d.ts.map +1 -1
- package/dist/auth/plugins/jwt.js +49 -3
- package/dist/auth/providers/custom-function/controller.d.ts.map +1 -1
- package/dist/auth/providers/custom-function/controller.js +19 -3
- package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -1
- package/dist/auth/providers/local-userpass/controller.js +125 -71
- package/dist/auth/providers/local-userpass/dtos.d.ts +11 -2
- package/dist/auth/providers/local-userpass/dtos.d.ts.map +1 -1
- package/dist/auth/utils.d.ts +53 -14
- package/dist/auth/utils.d.ts.map +1 -1
- package/dist/auth/utils.js +46 -63
- package/dist/constants.d.ts +14 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +18 -5
- package/dist/features/functions/controller.d.ts.map +1 -1
- package/dist/features/functions/controller.js +32 -3
- package/dist/features/functions/dtos.d.ts +3 -0
- package/dist/features/functions/dtos.d.ts.map +1 -1
- package/dist/features/functions/interface.d.ts +3 -0
- package/dist/features/functions/interface.d.ts.map +1 -1
- package/dist/features/functions/utils.d.ts +2 -1
- package/dist/features/functions/utils.d.ts.map +1 -1
- package/dist/features/functions/utils.js +19 -7
- package/dist/features/rules/utils.d.ts.map +1 -1
- package/dist/features/rules/utils.js +11 -2
- package/dist/features/triggers/index.d.ts.map +1 -1
- package/dist/features/triggers/index.js +48 -7
- package/dist/features/triggers/utils.d.ts.map +1 -1
- package/dist/features/triggers/utils.js +118 -27
- package/dist/index.d.ts +8 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +57 -21
- package/dist/services/mongodb-atlas/index.d.ts.map +1 -1
- package/dist/services/mongodb-atlas/index.js +605 -478
- package/dist/services/mongodb-atlas/model.d.ts +2 -1
- package/dist/services/mongodb-atlas/model.d.ts.map +1 -1
- package/dist/services/mongodb-atlas/utils.d.ts +9 -2
- package/dist/services/mongodb-atlas/utils.d.ts.map +1 -1
- package/dist/services/mongodb-atlas/utils.js +113 -23
- package/dist/shared/handleUserRegistration.d.ts.map +1 -1
- package/dist/shared/handleUserRegistration.js +4 -1
- package/dist/shared/models/handleUserRegistration.model.d.ts +6 -2
- package/dist/shared/models/handleUserRegistration.model.d.ts.map +1 -1
- package/dist/utils/context/helpers.d.ts +7 -6
- package/dist/utils/context/helpers.d.ts.map +1 -1
- package/dist/utils/context/helpers.js +3 -0
- package/dist/utils/context/index.d.ts +1 -1
- package/dist/utils/context/index.d.ts.map +1 -1
- package/dist/utils/context/index.js +176 -5
- package/dist/utils/context/interface.d.ts +1 -1
- package/dist/utils/context/interface.d.ts.map +1 -1
- package/dist/utils/crypto/index.d.ts +1 -0
- package/dist/utils/crypto/index.d.ts.map +1 -1
- package/dist/utils/crypto/index.js +6 -2
- package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -1
- package/dist/utils/initializer/exposeRoutes.js +11 -4
- package/dist/utils/initializer/registerPlugins.d.ts +3 -1
- package/dist/utils/initializer/registerPlugins.d.ts.map +1 -1
- package/dist/utils/initializer/registerPlugins.js +9 -6
- package/dist/utils/roles/helpers.js +11 -3
- package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -1
- package/dist/utils/roles/machines/commonValidators.js +10 -6
- package/dist/utils/roles/machines/read/B/validators.d.ts +4 -0
- package/dist/utils/roles/machines/read/B/validators.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/B/validators.js +8 -0
- package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/C/index.js +10 -7
- package/dist/utils/roles/machines/read/C/validators.d.ts +5 -0
- package/dist/utils/roles/machines/read/C/validators.d.ts.map +1 -0
- package/dist/utils/roles/machines/read/C/validators.js +29 -0
- package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/D/index.js +13 -11
- package/dist/utils/rules.d.ts +1 -1
- package/dist/utils/rules.d.ts.map +1 -1
- package/dist/utils/rules.js +26 -17
- package/jest.config.ts +2 -12
- package/jest.setup.ts +28 -0
- package/package.json +1 -2
- package/src/auth/controller.ts +70 -4
- package/src/auth/plugins/jwt.test.ts +93 -0
- package/src/auth/plugins/jwt.ts +62 -3
- package/src/auth/providers/custom-function/controller.ts +22 -5
- package/src/auth/providers/local-userpass/controller.ts +168 -96
- package/src/auth/providers/local-userpass/dtos.ts +13 -2
- package/src/auth/utils.ts +51 -86
- package/src/constants.ts +17 -3
- package/src/fastify.d.ts +32 -15
- package/src/features/functions/controller.ts +51 -3
- package/src/features/functions/dtos.ts +3 -0
- package/src/features/functions/interface.ts +3 -0
- package/src/features/functions/utils.ts +29 -8
- package/src/features/rules/utils.ts +11 -2
- package/src/features/triggers/index.ts +43 -1
- package/src/features/triggers/utils.ts +146 -38
- package/src/index.ts +69 -20
- package/src/services/mongodb-atlas/__tests__/findOneAndUpdate.test.ts +95 -0
- package/src/services/mongodb-atlas/__tests__/utils.test.ts +141 -0
- package/src/services/mongodb-atlas/index.ts +241 -90
- package/src/services/mongodb-atlas/model.ts +15 -2
- package/src/services/mongodb-atlas/utils.ts +158 -22
- package/src/shared/handleUserRegistration.ts +5 -4
- package/src/shared/models/handleUserRegistration.model.ts +8 -3
- package/src/types/fastify-raw-body.d.ts +22 -0
- package/src/utils/__tests__/STEP_B_STATES.test.ts +1 -1
- package/src/utils/__tests__/STEP_C_STATES.test.ts +1 -1
- package/src/utils/__tests__/STEP_D_STATES.test.ts +2 -2
- package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +9 -4
- package/src/utils/__tests__/registerPlugins.test.ts +16 -1
- package/src/utils/context/helpers.ts +3 -0
- package/src/utils/context/index.ts +238 -13
- package/src/utils/context/interface.ts +1 -1
- package/src/utils/crypto/index.ts +5 -1
- package/src/utils/initializer/exposeRoutes.ts +15 -8
- package/src/utils/initializer/registerPlugins.ts +15 -7
- package/src/utils/roles/helpers.ts +23 -5
- package/src/utils/roles/machines/commonValidators.ts +10 -5
- package/src/utils/roles/machines/read/B/validators.ts +8 -0
- package/src/utils/roles/machines/read/C/index.ts +11 -7
- package/src/utils/roles/machines/read/C/validators.ts +21 -0
- package/src/utils/roles/machines/read/D/index.ts +22 -12
- package/src/utils/rules.ts +31 -22
- package/tsconfig.spec.json +7 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { FastifyInstance } from 'fastify';
|
|
2
|
-
import { Collection, Document, FindCursor, WithId } from 'mongodb';
|
|
2
|
+
import { Collection, Document, Filter as MongoFilter, FindCursor, FindOneAndUpdateOptions, UpdateFilter, WithId } from 'mongodb';
|
|
3
3
|
import { User } from '../../auth/dtos';
|
|
4
4
|
import { Filter, Rules } from '../../features/rules/interface';
|
|
5
5
|
import { Role } from '../../utils/roles/interface';
|
|
@@ -28,6 +28,7 @@ export type GetOperatorsFunction = (collection: Collection<Document>, { rules, c
|
|
|
28
28
|
deleteOne: (...params: Parameters<Method<'findOne'>>) => ReturnType<Method<'findOne'>>;
|
|
29
29
|
insertOne: (...params: Parameters<Method<'insertOne'>>) => ReturnType<Method<'insertOne'>>;
|
|
30
30
|
updateOne: (...params: Parameters<Method<'updateOne'>>) => ReturnType<Method<'updateOne'>>;
|
|
31
|
+
findOneAndUpdate: (filter: MongoFilter<Document>, update: UpdateFilter<Document> | Document[], options?: FindOneAndUpdateOptions) => Promise<Document | null>;
|
|
31
32
|
find: (...params: Parameters<Method<'find'>>) => FindCursor;
|
|
32
33
|
watch: (...params: Parameters<Method<'watch'>>) => ReturnType<Method<'watch'>>;
|
|
33
34
|
aggregate: (...params: [...Parameters<Method<'aggregate'>>, isClient: boolean]) => Promise<ReturnType<Method<'aggregate'>>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/services/mongodb-atlas/model.ts"],"names":[],"mappings":"AAA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,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA;IAC9E,SAAS,EAAE,CACT,GAAG,MAAM,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,KAC/D,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;IAC7C,UAAU,EAAE,CACV,GAAG,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,KACxC,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAA;IACrC,UAAU,EAAE,CACV,GAAG,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,KACxC,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAA;IACrC,UAAU,EAAE,CACV,GAAG,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,KACxC,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAA;CACtC,CAAA;AAGD,oBAAY,eAAe;IACzB,MAAM,WAAW;IACjB,IAAI,SAAS;IACb,MAAM,WAAW;IACjB,MAAM,WAAW;CAElB"}
|
|
@@ -5,12 +5,14 @@ import { AggregationPipeline, Filter, Projection, Rules } from '../../features/r
|
|
|
5
5
|
import { Role } from '../../utils/roles/interface';
|
|
6
6
|
import { CRUD_OPERATIONS, GetValidRuleParams } from './model';
|
|
7
7
|
export declare const getValidRule: <T extends Role | Filter>({ filters, user, record }: GetValidRuleParams<T>) => T[];
|
|
8
|
-
export declare const getFormattedQuery: (filters: Filter[] | undefined, query: Parameters<Collection<Document>["findOne"]>[0], user?: User) =>
|
|
8
|
+
export declare const getFormattedQuery: (filters: Filter[] | undefined, query: Parameters<Collection<Document>["findOne"]>[0], user?: User) => FilterMongoDB<Document>[];
|
|
9
9
|
export declare const getFormattedProjection: (filters?: Filter[], user?: User) => Projection | null;
|
|
10
10
|
export declare const applyAccessControlToPipeline: (pipeline: AggregationPipeline, rules: Record<string, {
|
|
11
11
|
filters?: Filter[];
|
|
12
12
|
roles?: Role[];
|
|
13
|
-
}>, user: User
|
|
13
|
+
}>, user: User, collectionName: string, options?: {
|
|
14
|
+
isClientPipeline?: boolean;
|
|
15
|
+
}) => AggregationPipeline;
|
|
14
16
|
export declare const checkDenyOperation: (rules: Rules, collectionName: string, operation: CRUD_OPERATIONS) => void;
|
|
15
17
|
export declare function normalizeQuery(query: FilterMongoDB<Document>[]): {
|
|
16
18
|
[x: string]: any;
|
|
@@ -28,4 +30,9 @@ export declare function normalizeQuery(query: FilterMongoDB<Document>[]): {
|
|
|
28
30
|
$comment?: string | Document;
|
|
29
31
|
}[];
|
|
30
32
|
export declare const getCollectionsFromPipeline: (pipeline: Document[]) => string[];
|
|
33
|
+
export declare function ensureClientPipelineStages(pipeline: AggregationPipeline): void;
|
|
34
|
+
export declare function getHiddenFieldsFromRulesConfig(rulesConfig?: {
|
|
35
|
+
roles?: Role[];
|
|
36
|
+
}): string[];
|
|
37
|
+
export declare function prependUnsetStage(pipeline: AggregationPipeline, hiddenFields: string[]): AggregationPipeline;
|
|
31
38
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/services/mongodb-atlas/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAElC,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,SAAS,CAAA;AACvE,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AACtC,OAAO,EACL,mBAAmB,EAEnB,MAAM,EAEN,UAAU,EACV,KAAK,EAGN,MAAM,gCAAgC,CAAA;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAA;AAGlD,OAAO,EAAE,eAAe,EAAE,kBAAkB,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AO,MAAM,CACX,MAAM,EACN;IACE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;IAClB,KAAK,CAAC,EAAE,IAAI,EAAE,CAAA;CACf,CACF,EACD,MAAM,IAAI,EACV,gBAAgB,MAAM,EACtB,UAAU;IACR,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B,KACA,mBA6GF,CAAA;AAED,eAAO,MAAM,kBAAkB,GAC7B,OAAO,KAAK,EACZ,gBAAgB,MAAM,EACtB,WAAW,eAAe,SAM3B,CAAA;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,CAAC,QAAQ,CAAC,EAAE;;;;;;;;iBA2Hs1rS,CAAC;sBAAgC,CAAC;2BAAsC,CAAC;;;;IAnH79rS;AAED,eAAO,MAAM,0BAA0B,GAAI,UAAU,QAAQ,EAAE,aAgC9D,CAAA;AAYD,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,mBAAmB,QA+BvE;AAED,wBAAgB,8BAA8B,CAAC,WAAW,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,IAAI,EAAE,CAAA;CAAE,YAK9E;AAyBD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,EAAE,uBAKtF"}
|
|
@@ -5,10 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.getCollectionsFromPipeline = exports.checkDenyOperation = exports.applyAccessControlToPipeline = exports.getFormattedProjection = exports.getFormattedQuery = exports.getValidRule = void 0;
|
|
7
7
|
exports.normalizeQuery = normalizeQuery;
|
|
8
|
+
exports.ensureClientPipelineStages = ensureClientPipelineStages;
|
|
9
|
+
exports.getHiddenFieldsFromRulesConfig = getHiddenFieldsFromRulesConfig;
|
|
10
|
+
exports.prependUnsetStage = prependUnsetStage;
|
|
8
11
|
const mongodb_1 = require("mongodb");
|
|
9
12
|
const interface_1 = require("../../features/rules/interface");
|
|
10
13
|
const rules_1 = require("../../utils/rules");
|
|
11
14
|
const utils_1 = __importDefault(require("../../utils/rules-matcher/utils"));
|
|
15
|
+
const model_1 = require("./model");
|
|
12
16
|
const getValidRule = ({ filters = [], user, record = null }) => {
|
|
13
17
|
if (!filters.length)
|
|
14
18
|
return [];
|
|
@@ -31,10 +35,14 @@ exports.getValidRule = getValidRule;
|
|
|
31
35
|
const getFormattedQuery = (filters = [], query, user) => {
|
|
32
36
|
const preFilter = (0, exports.getValidRule)({ filters, user });
|
|
33
37
|
const isValidPreFilter = !!(preFilter === null || preFilter === void 0 ? void 0 : preFilter.length);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
query
|
|
37
|
-
|
|
38
|
+
const formatted = [];
|
|
39
|
+
if (isValidPreFilter) {
|
|
40
|
+
formatted.push((0, rules_1.expandQuery)(preFilter[0].query, { '%%user': user }));
|
|
41
|
+
}
|
|
42
|
+
if (query && Object.keys(query).length > 0) {
|
|
43
|
+
formatted.push(query);
|
|
44
|
+
}
|
|
45
|
+
return formatted;
|
|
38
46
|
};
|
|
39
47
|
exports.getFormattedQuery = getFormattedQuery;
|
|
40
48
|
const getFormattedProjection = (filters = [], user) => {
|
|
@@ -53,49 +61,66 @@ const getFormattedProjection = (filters = [], user) => {
|
|
|
53
61
|
return Object.assign({}, ...projections);
|
|
54
62
|
};
|
|
55
63
|
exports.getFormattedProjection = getFormattedProjection;
|
|
56
|
-
const applyAccessControlToPipeline = (pipeline, rules, user) => {
|
|
64
|
+
const applyAccessControlToPipeline = (pipeline, rules, user, collectionName, options) => {
|
|
65
|
+
const { isClientPipeline = false } = options || {};
|
|
66
|
+
const hiddenFieldsForCollection = isClientPipeline
|
|
67
|
+
? getHiddenFieldsFromRulesConfig(rules[collectionName])
|
|
68
|
+
: [];
|
|
57
69
|
return pipeline.map((stage) => {
|
|
58
70
|
const [stageName] = Object.keys(stage);
|
|
59
71
|
const value = stage[stageName];
|
|
60
|
-
// CASE LOOKUP
|
|
61
72
|
if (stageName === interface_1.STAGES_TO_SEARCH.LOOKUP) {
|
|
62
73
|
const lookUpStage = value;
|
|
63
74
|
const currentCollection = lookUpStage.from;
|
|
75
|
+
(0, exports.checkDenyOperation)(rules, currentCollection, model_1.CRUD_OPERATIONS.READ);
|
|
64
76
|
const lookupRules = rules[currentCollection] || {};
|
|
65
77
|
const formattedQuery = (0, exports.getFormattedQuery)(lookupRules.filters, {}, user);
|
|
66
78
|
const projection = (0, exports.getFormattedProjection)(lookupRules.filters);
|
|
79
|
+
const nestedPipeline = (0, exports.applyAccessControlToPipeline)(lookUpStage.pipeline || [], rules, user, currentCollection, { isClientPipeline });
|
|
80
|
+
const lookupPipeline = [
|
|
81
|
+
...(formattedQuery.length ? [{ $match: { $and: formattedQuery } }] : []),
|
|
82
|
+
...(projection ? [{ $project: projection }] : []),
|
|
83
|
+
...nestedPipeline
|
|
84
|
+
];
|
|
85
|
+
const pipelineWithHiddenFields = isClientPipeline
|
|
86
|
+
? prependUnsetStage(lookupPipeline, getHiddenFieldsFromRulesConfig(lookupRules))
|
|
87
|
+
: lookupPipeline;
|
|
67
88
|
return {
|
|
68
|
-
$lookup: Object.assign(Object.assign({}, lookUpStage), { pipeline:
|
|
69
|
-
...(formattedQuery.length ? [{ $match: { $and: formattedQuery } }] : []),
|
|
70
|
-
...(projection ? [{ $project: projection }] : []),
|
|
71
|
-
...(0, exports.applyAccessControlToPipeline)(lookUpStage.pipeline || [], rules, user)
|
|
72
|
-
] })
|
|
89
|
+
$lookup: Object.assign(Object.assign({}, lookUpStage), { pipeline: pipelineWithHiddenFields })
|
|
73
90
|
};
|
|
74
91
|
}
|
|
75
|
-
// CASE LOOKUP
|
|
76
92
|
if (stageName === interface_1.STAGES_TO_SEARCH.UNION_WITH) {
|
|
77
93
|
const unionWithStage = value;
|
|
78
94
|
const isSimpleStage = typeof unionWithStage === 'string';
|
|
79
95
|
const currentCollection = isSimpleStage ? unionWithStage : unionWithStage.coll;
|
|
96
|
+
(0, exports.checkDenyOperation)(rules, currentCollection, model_1.CRUD_OPERATIONS.READ);
|
|
80
97
|
const unionRules = rules[currentCollection] || {};
|
|
81
98
|
const formattedQuery = (0, exports.getFormattedQuery)(unionRules.filters, {}, user);
|
|
82
99
|
const projection = (0, exports.getFormattedProjection)(unionRules.filters);
|
|
83
|
-
|
|
100
|
+
if (isSimpleStage) {
|
|
101
|
+
return stage;
|
|
102
|
+
}
|
|
103
|
+
const nestedPipeline = unionWithStage.pipeline || [];
|
|
104
|
+
const sanitizedNestedPipeline = (0, exports.applyAccessControlToPipeline)(nestedPipeline, rules, user, currentCollection, { isClientPipeline });
|
|
105
|
+
const unionPipeline = [
|
|
106
|
+
...(formattedQuery.length ? [{ $match: { $and: formattedQuery } }] : []),
|
|
107
|
+
...(projection ? [{ $project: projection }] : []),
|
|
108
|
+
...sanitizedNestedPipeline
|
|
109
|
+
];
|
|
110
|
+
const pipelineWithHiddenFields = isClientPipeline
|
|
111
|
+
? prependUnsetStage(unionPipeline, getHiddenFieldsFromRulesConfig(unionRules))
|
|
112
|
+
: unionPipeline;
|
|
84
113
|
return {
|
|
85
|
-
$unionWith: {
|
|
86
|
-
coll: currentCollection,
|
|
87
|
-
pipeline: [
|
|
88
|
-
...(formattedQuery.length ? [{ $match: { $and: formattedQuery } }] : []),
|
|
89
|
-
...(projection ? [{ $project: projection }] : []),
|
|
90
|
-
...(0, exports.applyAccessControlToPipeline)(nestedPipeline, rules, user)
|
|
91
|
-
]
|
|
92
|
-
}
|
|
114
|
+
$unionWith: Object.assign(Object.assign({}, unionWithStage), { pipeline: pipelineWithHiddenFields })
|
|
93
115
|
};
|
|
94
116
|
}
|
|
95
|
-
// CASE FACET
|
|
96
117
|
if (stageName === interface_1.STAGES_TO_SEARCH.FACET) {
|
|
97
118
|
const modifiedFacets = Object.fromEntries(Object.entries(value).map(([facetKey, facetPipeline]) => {
|
|
98
|
-
|
|
119
|
+
const sanitizedFacetPipeline = (0, exports.applyAccessControlToPipeline)(facetPipeline, rules, user, collectionName, { isClientPipeline });
|
|
120
|
+
const facetPipelineWithHiddenFields = isClientPipeline
|
|
121
|
+
? prependUnsetStage(sanitizedFacetPipeline, hiddenFieldsForCollection)
|
|
122
|
+
: sanitizedFacetPipeline;
|
|
123
|
+
return [facetKey, facetPipelineWithHiddenFields];
|
|
99
124
|
}));
|
|
100
125
|
return { $facet: modifiedFacets };
|
|
101
126
|
}
|
|
@@ -147,3 +172,68 @@ const getCollectionsFromPipeline = (pipeline) => {
|
|
|
147
172
|
}, []);
|
|
148
173
|
};
|
|
149
174
|
exports.getCollectionsFromPipeline = getCollectionsFromPipeline;
|
|
175
|
+
const CLIENT_STAGE_BLACKLIST = new Set([
|
|
176
|
+
'$replaceRoot',
|
|
177
|
+
'$merge',
|
|
178
|
+
'$out',
|
|
179
|
+
'$function',
|
|
180
|
+
'$where',
|
|
181
|
+
'$accumulator',
|
|
182
|
+
'$graphLookup'
|
|
183
|
+
]);
|
|
184
|
+
function ensureClientPipelineStages(pipeline) {
|
|
185
|
+
pipeline.forEach((stage) => {
|
|
186
|
+
const [stageName] = Object.keys(stage);
|
|
187
|
+
if (!stageName)
|
|
188
|
+
return;
|
|
189
|
+
if (CLIENT_STAGE_BLACKLIST.has(stageName)) {
|
|
190
|
+
throw new Error(`Stage ${stageName} is not allowed in client aggregate pipelines`);
|
|
191
|
+
}
|
|
192
|
+
const value = stage[stageName];
|
|
193
|
+
if (stageName === interface_1.STAGES_TO_SEARCH.LOOKUP) {
|
|
194
|
+
ensureClientPipelineStages(value.pipeline || []);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (stageName === interface_1.STAGES_TO_SEARCH.UNION_WITH) {
|
|
198
|
+
if (typeof value === 'string') {
|
|
199
|
+
throw new Error('$unionWith must provide a pipeline when called from the client');
|
|
200
|
+
}
|
|
201
|
+
const unionStage = value;
|
|
202
|
+
ensureClientPipelineStages(unionStage.pipeline || []);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (stageName === interface_1.STAGES_TO_SEARCH.FACET) {
|
|
206
|
+
Object.values(value).forEach((facetPipeline) => ensureClientPipelineStages(facetPipeline));
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
function getHiddenFieldsFromRulesConfig(rulesConfig) {
|
|
211
|
+
if (!rulesConfig) {
|
|
212
|
+
return [];
|
|
213
|
+
}
|
|
214
|
+
return collectHiddenFieldsFromRoles(rulesConfig.roles);
|
|
215
|
+
}
|
|
216
|
+
function collectHiddenFieldsFromRoles(roles = []) {
|
|
217
|
+
const hiddenFields = new Set();
|
|
218
|
+
const collectFromFields = (fields) => {
|
|
219
|
+
if (!fields)
|
|
220
|
+
return;
|
|
221
|
+
Object.entries(fields).forEach(([fieldName, permissions]) => {
|
|
222
|
+
const canRead = Boolean((permissions === null || permissions === void 0 ? void 0 : permissions.read) || (permissions === null || permissions === void 0 ? void 0 : permissions.write));
|
|
223
|
+
if (!canRead) {
|
|
224
|
+
hiddenFields.add(fieldName);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
roles.forEach((role) => {
|
|
229
|
+
collectFromFields(role.fields);
|
|
230
|
+
collectFromFields(role.additional_fields);
|
|
231
|
+
});
|
|
232
|
+
return Array.from(hiddenFields);
|
|
233
|
+
}
|
|
234
|
+
function prependUnsetStage(pipeline, hiddenFields) {
|
|
235
|
+
if (!hiddenFields.length) {
|
|
236
|
+
return pipeline;
|
|
237
|
+
}
|
|
238
|
+
return [{ $unset: hiddenFields }, ...pipeline];
|
|
239
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleUserRegistration.d.ts","sourceRoot":"","sources":["../../src/shared/handleUserRegistration.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"handleUserRegistration.d.ts","sourceRoot":"","sources":["../../src/shared/handleUserRegistration.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,uCAAuC,CAAA;AAE9E;;;;;;GAMG;AACH,QAAA,MAAM,sBAAsB,EAAE,sBAoD7B,CAAA;AAED,eAAe,sBAAsB,CAAA"}
|
|
@@ -19,11 +19,13 @@ const crypto_1 = require("../utils/crypto");
|
|
|
19
19
|
* @returns {Promise<InsertOneResult<Document>>} A promise resolving to the result of the insert operation.
|
|
20
20
|
*/
|
|
21
21
|
const handleUserRegistration = (app, opt) => (_a) => __awaiter(void 0, [_a], void 0, function* ({ email, password }) {
|
|
22
|
+
var _b;
|
|
22
23
|
const { run_as_system, skipUserCheck, provider } = opt !== null && opt !== void 0 ? opt : {};
|
|
23
24
|
if (!run_as_system) {
|
|
24
25
|
throw new Error('only run_as_system');
|
|
25
26
|
}
|
|
26
27
|
const { authCollection } = constants_1.AUTH_CONFIG;
|
|
28
|
+
const autoConfirm = ((_b = constants_1.AUTH_CONFIG.localUserpassConfig) === null || _b === void 0 ? void 0 : _b.autoConfirm) === true;
|
|
27
29
|
const mongo = app === null || app === void 0 ? void 0 : app.mongo;
|
|
28
30
|
const db = mongo.client.db(constants_1.DB_NAME);
|
|
29
31
|
const hashedPassword = yield (0, crypto_1.hashPassword)(password);
|
|
@@ -34,7 +36,8 @@ const handleUserRegistration = (app, opt) => (_a) => __awaiter(void 0, [_a], voi
|
|
|
34
36
|
const result = yield (db === null || db === void 0 ? void 0 : db.collection(authCollection).insertOne({
|
|
35
37
|
email,
|
|
36
38
|
password: hashedPassword,
|
|
37
|
-
status: skipUserCheck ? 'confirmed' : 'pending',
|
|
39
|
+
status: skipUserCheck || autoConfirm ? 'confirmed' : 'pending',
|
|
40
|
+
createdAt: new Date(),
|
|
38
41
|
custom_data: {
|
|
39
42
|
// TODO: aggiungere dati personalizzati alla registrazione
|
|
40
43
|
},
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { FastifyInstance } from "fastify/types/instance";
|
|
2
|
-
import { InsertOneResult } from "mongodb/mongodb";
|
|
3
2
|
import { User } from "../../auth/dtos";
|
|
4
3
|
import { Rules } from "../../features/rules/interface";
|
|
5
4
|
type RegistrationParams = {
|
|
@@ -13,7 +12,12 @@ export type Options = {
|
|
|
13
12
|
provider?: PROVIDER;
|
|
14
13
|
run_as_system?: boolean;
|
|
15
14
|
};
|
|
16
|
-
|
|
15
|
+
type RegistrationResult = {
|
|
16
|
+
insertedId?: {
|
|
17
|
+
toString: () => string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export type HandleUserRegistration = (app: FastifyInstance, opt: Options) => (params: RegistrationParams) => Promise<RegistrationResult>;
|
|
17
21
|
export declare enum PROVIDER {
|
|
18
22
|
LOCAL_USERPASS = "local-userpass",
|
|
19
23
|
CUSTOM_FUNCTION = "custom-function"
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleUserRegistration.model.d.ts","sourceRoot":"","sources":["../../../src/shared/models/handleUserRegistration.model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"handleUserRegistration.model.d.ts","sourceRoot":"","sources":["../../../src/shared/models/handleUserRegistration.model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAA;AAEtD,KAAK,kBAAkB,GAAG;IACxB,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,KAAK,CAAC,EAAE,KAAK,CAAA;IACb,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,QAAQ,CAAC,EAAE,QAAQ,CAAA;IACnB,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB,CAAA;AAED,KAAK,kBAAkB,GAAG;IACxB,UAAU,CAAC,EAAE;QACX,QAAQ,EAAE,MAAM,MAAM,CAAA;KACvB,CAAA;CACF,CAAA;AAED,MAAM,MAAM,sBAAsB,GAAG,CACnC,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,OAAO,KACT,CAAC,MAAM,EAAE,kBAAkB,KAAK,OAAO,CAAC,kBAAkB,CAAC,CAAA;AAEhE,oBAAY,QAAQ;IAClB,cAAc,mBAAmB;IACjC,eAAe,oBAAoB;CACpC"}
|
|
@@ -15,14 +15,15 @@ export declare const generateContextData: ({ user, services, app, rules, current
|
|
|
15
15
|
BSON: typeof mongodb.BSON;
|
|
16
16
|
console: {
|
|
17
17
|
log: (...args: Arguments) => void;
|
|
18
|
+
error: (...args: Arguments) => void;
|
|
18
19
|
};
|
|
19
20
|
context: {
|
|
20
21
|
request: {
|
|
21
22
|
remoteIPAddress: string | undefined;
|
|
22
|
-
id?: string | undefined;
|
|
23
23
|
method?: string | undefined;
|
|
24
24
|
url?: string | undefined;
|
|
25
25
|
host?: string | undefined;
|
|
26
|
+
id?: string | undefined;
|
|
26
27
|
ips?: string[];
|
|
27
28
|
hostname?: string | undefined;
|
|
28
29
|
ip?: string | undefined;
|
|
@@ -36,6 +37,10 @@ export declare const generateContextData: ({ user, services, app, rules, current
|
|
|
36
37
|
};
|
|
37
38
|
services: {
|
|
38
39
|
get: (serviceName: keyof typeof services) => {
|
|
40
|
+
db: (dbName: string) => {
|
|
41
|
+
collection: (collName: string) => ReturnType<import("../../services/mongodb-atlas/model").GetOperatorsFunction>;
|
|
42
|
+
};
|
|
43
|
+
} | {
|
|
39
44
|
get: <T = null>({ url, headers }: import("../../services/api/model").GetParams) => Promise<{
|
|
40
45
|
status: number;
|
|
41
46
|
headers: import("undici/types/header").IncomingHttpHeaders;
|
|
@@ -74,14 +79,10 @@ export declare const generateContextData: ({ user, services, app, rules, current
|
|
|
74
79
|
InvokeAsync: import("aws-sdk").Lambda["invokeAsync"];
|
|
75
80
|
};
|
|
76
81
|
s3: (region: string) => import("aws-sdk").S3;
|
|
77
|
-
} | {
|
|
78
|
-
db: (dbName: string) => {
|
|
79
|
-
collection: (collName: string) => ReturnType<import("../../services/mongodb-atlas/model").GetOperatorsFunction>;
|
|
80
|
-
};
|
|
81
82
|
} | undefined;
|
|
82
83
|
};
|
|
83
84
|
functions: {
|
|
84
|
-
execute: (name: keyof typeof functionsList, ...args: Arguments) => Promise<
|
|
85
|
+
execute: (name: keyof typeof functionsList, ...args: Arguments) => Promise<unknown>;
|
|
85
86
|
};
|
|
86
87
|
};
|
|
87
88
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/utils/context/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAA;AAEvD;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,GAAI,0FASjC,yBAAyB;;;uBAGT,SAAS;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/utils/context/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAA;AAEvD;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,GAAI,0FASjC,yBAAyB;;;uBAGT,SAAS;yBAGP,SAAS;;;;;;;;;;;;;;;;;;uBAcb,MAAM;;;+BAGE,MAAM,OAAO,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6BAlCG,GAE3C;;;;;;;4BAiDgB,MAAM,OAAO,aAAa,WAAW,SAAS;;;CAclE,CAAA"}
|
|
@@ -10,5 +10,5 @@ import { GenerateContextParams } from './interface';
|
|
|
10
10
|
* @param functionsList -> the list of all functions
|
|
11
11
|
* @param services -> the list of all services
|
|
12
12
|
*/
|
|
13
|
-
export declare function GenerateContext({ args, app, rules, user, currentFunction, functionsList, services, runAsSystem, deserializeArgs, enqueue, request }: GenerateContextParams): Promise<
|
|
13
|
+
export declare function GenerateContext({ args, app, rules, user, currentFunction, functionsList, services, runAsSystem, deserializeArgs, enqueue, request }: GenerateContextParams): Promise<unknown>;
|
|
14
14
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/context/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/context/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AA0GnD;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CAAC,EACpC,IAAI,EACJ,GAAG,EACH,KAAK,EACL,IAAI,EACJ,eAAe,EACf,aAAa,EACb,QAAQ,EACR,WAAW,EACX,eAAsB,EACtB,OAAO,EACP,OAAO,EACR,EAAE,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,CAkK1C"}
|
|
@@ -14,10 +14,100 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.GenerateContext = GenerateContext;
|
|
16
16
|
const node_module_1 = require("node:module");
|
|
17
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
18
|
+
const node_url_1 = require("node:url");
|
|
17
19
|
const vm_1 = __importDefault(require("vm"));
|
|
18
20
|
const bson_1 = require("bson");
|
|
19
21
|
const state_1 = require("../../state");
|
|
20
22
|
const helpers_1 = require("./helpers");
|
|
23
|
+
const dynamicImport = new Function('specifier', 'return import(specifier)');
|
|
24
|
+
const transformImportsToRequire = (code) => {
|
|
25
|
+
let importIndex = 0;
|
|
26
|
+
const lines = code.split(/\r?\n/);
|
|
27
|
+
return lines
|
|
28
|
+
.map((line) => {
|
|
29
|
+
const trimmed = line.trim();
|
|
30
|
+
if (/^import\s+type\s+/.test(trimmed)) {
|
|
31
|
+
return '';
|
|
32
|
+
}
|
|
33
|
+
const sideEffectMatch = trimmed.match(/^import\s+['"]([^'"]+)['"]\s*;?$/);
|
|
34
|
+
if (sideEffectMatch) {
|
|
35
|
+
return `require('${sideEffectMatch[1]}')`;
|
|
36
|
+
}
|
|
37
|
+
const match = trimmed.match(/^import\s+(.+?)\s+from\s+['"]([^'"]+)['"]\s*;?$/);
|
|
38
|
+
if (!match)
|
|
39
|
+
return line;
|
|
40
|
+
const [, importClause, source] = match;
|
|
41
|
+
const clause = importClause.trim();
|
|
42
|
+
if (clause.startsWith('{') && clause.endsWith('}')) {
|
|
43
|
+
const named = clause.slice(1, -1).trim();
|
|
44
|
+
return `const { ${named} } = require('${source}')`;
|
|
45
|
+
}
|
|
46
|
+
const namespaceMatch = clause.match(/^\*\s+as\s+(\w+)$/);
|
|
47
|
+
if (namespaceMatch) {
|
|
48
|
+
return `const ${namespaceMatch[1]} = require('${source}')`;
|
|
49
|
+
}
|
|
50
|
+
if (clause.includes(',')) {
|
|
51
|
+
const [defaultPart, restRaw] = clause.split(',', 2);
|
|
52
|
+
const defaultName = defaultPart.trim();
|
|
53
|
+
const rest = restRaw.trim();
|
|
54
|
+
const tmpName = `__fb_import_${importIndex++}`;
|
|
55
|
+
const linesOut = [`const ${tmpName} = require('${source}')`];
|
|
56
|
+
if (defaultName) {
|
|
57
|
+
linesOut.push(`const ${defaultName} = ${tmpName}`);
|
|
58
|
+
}
|
|
59
|
+
if (rest.startsWith('{') && rest.endsWith('}')) {
|
|
60
|
+
const named = rest.slice(1, -1).trim();
|
|
61
|
+
linesOut.push(`const { ${named} } = ${tmpName}`);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const nsMatch = rest.match(/^\*\s+as\s+(\w+)$/);
|
|
65
|
+
if (nsMatch) {
|
|
66
|
+
linesOut.push(`const ${nsMatch[1]} = ${tmpName}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return linesOut.join('\n');
|
|
70
|
+
}
|
|
71
|
+
return `const ${clause} = require('${source}')`;
|
|
72
|
+
})
|
|
73
|
+
.join('\n');
|
|
74
|
+
};
|
|
75
|
+
const wrapEsmModule = (code) => {
|
|
76
|
+
const prelude = [
|
|
77
|
+
'const __fb_module = { exports: {} };',
|
|
78
|
+
'let exports = __fb_module.exports;',
|
|
79
|
+
'let module = __fb_module;',
|
|
80
|
+
'const __fb_require = globalThis.__fb_require;',
|
|
81
|
+
'const require = __fb_require;',
|
|
82
|
+
'const __filename = globalThis.__fb_filename;',
|
|
83
|
+
'const __dirname = globalThis.__fb_dirname;'
|
|
84
|
+
].join('\n');
|
|
85
|
+
const trailer = [
|
|
86
|
+
'globalThis.__fb_module = __fb_module;',
|
|
87
|
+
'globalThis.__fb_exports = exports;'
|
|
88
|
+
].join('\n');
|
|
89
|
+
return `${prelude}\n${code}\n${trailer}`;
|
|
90
|
+
};
|
|
91
|
+
const resolveImportTarget = (specifier, customRequire) => {
|
|
92
|
+
try {
|
|
93
|
+
const resolved = customRequire.resolve(specifier);
|
|
94
|
+
if (resolved.startsWith('node:'))
|
|
95
|
+
return resolved;
|
|
96
|
+
if (node_path_1.default.isAbsolute(resolved)) {
|
|
97
|
+
return (0, node_url_1.pathToFileURL)(resolved).href;
|
|
98
|
+
}
|
|
99
|
+
return resolved;
|
|
100
|
+
}
|
|
101
|
+
catch (_a) {
|
|
102
|
+
return specifier;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const shouldFallbackFromVmModules = (error) => {
|
|
106
|
+
if (!error || typeof error !== 'object')
|
|
107
|
+
return false;
|
|
108
|
+
const code = error.code;
|
|
109
|
+
return code === 'ERR_VM_MODULES_DISABLED' || code === 'ERR_VM_MODULES_NOT_SUPPORTED';
|
|
110
|
+
};
|
|
21
111
|
/**
|
|
22
112
|
* > Used to generate the current context
|
|
23
113
|
* @testable
|
|
@@ -31,10 +121,12 @@ const helpers_1 = require("./helpers");
|
|
|
31
121
|
*/
|
|
32
122
|
function GenerateContext(_a) {
|
|
33
123
|
return __awaiter(this, arguments, void 0, function* ({ args, app, rules, user, currentFunction, functionsList, services, runAsSystem, deserializeArgs = true, enqueue, request }) {
|
|
124
|
+
if (!currentFunction)
|
|
125
|
+
return;
|
|
34
126
|
const functionsQueue = state_1.StateManager.select("functionsQueue");
|
|
35
127
|
const functionToRun = Object.assign({ run_as_system: runAsSystem }, currentFunction);
|
|
36
128
|
const run = () => __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
var _a, _b;
|
|
129
|
+
var _a, _b, _c;
|
|
38
130
|
const contextData = (0, helpers_1.generateContextData)({
|
|
39
131
|
user,
|
|
40
132
|
services,
|
|
@@ -45,19 +137,98 @@ function GenerateContext(_a) {
|
|
|
45
137
|
GenerateContext,
|
|
46
138
|
request
|
|
47
139
|
});
|
|
140
|
+
const isExportedFunction = (value) => typeof value === 'function';
|
|
141
|
+
const getDefaultExport = (value) => {
|
|
142
|
+
if (!value || typeof value !== 'object')
|
|
143
|
+
return undefined;
|
|
144
|
+
if (!('default' in value))
|
|
145
|
+
return undefined;
|
|
146
|
+
const maybeDefault = value.default;
|
|
147
|
+
return isExportedFunction(maybeDefault) ? maybeDefault : undefined;
|
|
148
|
+
};
|
|
149
|
+
const resolveExport = (ctx) => {
|
|
150
|
+
var _a, _b, _c, _d, _e;
|
|
151
|
+
const moduleExports = (_b = (_a = ctx.module) === null || _a === void 0 ? void 0 : _a.exports) !== null && _b !== void 0 ? _b : (_c = ctx.__fb_module) === null || _c === void 0 ? void 0 : _c.exports;
|
|
152
|
+
if (isExportedFunction(moduleExports))
|
|
153
|
+
return moduleExports;
|
|
154
|
+
const contextExports = (_d = ctx.exports) !== null && _d !== void 0 ? _d : ctx.__fb_exports;
|
|
155
|
+
if (isExportedFunction(contextExports))
|
|
156
|
+
return contextExports;
|
|
157
|
+
return (_e = getDefaultExport(moduleExports)) !== null && _e !== void 0 ? _e : getDefaultExport(contextExports);
|
|
158
|
+
};
|
|
159
|
+
const sandboxModule = { exports: {} };
|
|
48
160
|
try {
|
|
49
161
|
const entryFile = (_b = (_a = require.main) === null || _a === void 0 ? void 0 : _a.filename) !== null && _b !== void 0 ? _b : process.cwd();
|
|
50
162
|
const customRequire = (0, node_module_1.createRequire)(entryFile);
|
|
51
|
-
|
|
52
|
-
|
|
163
|
+
const vmContext = vm_1.default.createContext(Object.assign(Object.assign({}, contextData), { require: customRequire, exports: sandboxModule.exports, module: sandboxModule, __filename,
|
|
164
|
+
__dirname, __fb_require: customRequire, __fb_filename: __filename, __fb_dirname: __dirname }));
|
|
165
|
+
const vmModules = vm_1.default;
|
|
166
|
+
const hasStaticImport = /\bimport\s+/.test(functionToRun.code);
|
|
167
|
+
let usedVmModules = false;
|
|
168
|
+
if (hasStaticImport && vmModules.SourceTextModule && vmModules.SyntheticModule) {
|
|
169
|
+
try {
|
|
170
|
+
const moduleCache = new Map();
|
|
171
|
+
const loadModule = (specifier) => __awaiter(this, void 0, void 0, function* () {
|
|
172
|
+
const importTarget = resolveImportTarget(specifier, customRequire);
|
|
173
|
+
const cached = moduleCache.get(importTarget);
|
|
174
|
+
if (cached)
|
|
175
|
+
return cached;
|
|
176
|
+
const namespace = yield dynamicImport(importTarget);
|
|
177
|
+
const exportNames = Object.keys(namespace);
|
|
178
|
+
if ('default' in namespace && !exportNames.includes('default')) {
|
|
179
|
+
exportNames.push('default');
|
|
180
|
+
}
|
|
181
|
+
const syntheticModule = new vmModules.SyntheticModule(exportNames, function () {
|
|
182
|
+
for (const name of exportNames) {
|
|
183
|
+
this.setExport(name, namespace[name]);
|
|
184
|
+
}
|
|
185
|
+
}, { context: vmContext, identifier: importTarget });
|
|
186
|
+
moduleCache.set(importTarget, syntheticModule);
|
|
187
|
+
return syntheticModule;
|
|
188
|
+
});
|
|
189
|
+
const importModuleDynamically = ((specifier) => __awaiter(this, void 0, void 0, function* () {
|
|
190
|
+
const module = yield loadModule(specifier);
|
|
191
|
+
if (module.status === 'unlinked') {
|
|
192
|
+
yield module.link(loadModule);
|
|
193
|
+
}
|
|
194
|
+
if (module.status === 'linked') {
|
|
195
|
+
yield module.evaluate();
|
|
196
|
+
}
|
|
197
|
+
return module;
|
|
198
|
+
}));
|
|
199
|
+
const sourceModule = new vmModules.SourceTextModule(wrapEsmModule(functionToRun.code), {
|
|
200
|
+
context: vmContext,
|
|
201
|
+
identifier: entryFile,
|
|
202
|
+
initializeImportMeta: (meta) => {
|
|
203
|
+
meta.url = (0, node_url_1.pathToFileURL)(entryFile).href;
|
|
204
|
+
},
|
|
205
|
+
importModuleDynamically
|
|
206
|
+
});
|
|
207
|
+
yield sourceModule.link(loadModule);
|
|
208
|
+
yield sourceModule.evaluate();
|
|
209
|
+
usedVmModules = true;
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
if (!shouldFallbackFromVmModules(error)) {
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (!usedVmModules) {
|
|
218
|
+
const codeToRun = functionToRun.code.includes('import ')
|
|
219
|
+
? transformImportsToRequire(functionToRun.code)
|
|
220
|
+
: functionToRun.code;
|
|
221
|
+
vm_1.default.runInContext(codeToRun, vmContext);
|
|
222
|
+
}
|
|
223
|
+
sandboxModule.exports = (_c = resolveExport(vmContext)) !== null && _c !== void 0 ? _c : sandboxModule.exports;
|
|
53
224
|
}
|
|
54
225
|
catch (e) {
|
|
55
226
|
console.log(e);
|
|
56
227
|
}
|
|
57
228
|
if (deserializeArgs) {
|
|
58
|
-
return yield
|
|
229
|
+
return yield sandboxModule.exports(...bson_1.EJSON.deserialize(args));
|
|
59
230
|
}
|
|
60
|
-
return yield
|
|
231
|
+
return yield sandboxModule.exports(...args);
|
|
61
232
|
});
|
|
62
233
|
const res = yield functionsQueue.add(run, enqueue);
|
|
63
234
|
return res;
|
|
@@ -18,7 +18,7 @@ export interface GenerateContextParams {
|
|
|
18
18
|
}
|
|
19
19
|
type ContextRequest = Pick<FastifyRequest, "ips" | "host" | "hostname" | "url" | "method" | "ip" | "id">;
|
|
20
20
|
export interface GenerateContextDataParams extends Omit<GenerateContextParams, 'args'> {
|
|
21
|
-
GenerateContext: (params: GenerateContextParams) => Promise<
|
|
21
|
+
GenerateContext: (params: GenerateContextParams) => Promise<unknown>;
|
|
22
22
|
}
|
|
23
23
|
export {};
|
|
24
24
|
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/utils/context/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,oCAAoC,CAAA;AACxE,OAAO,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAA;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AAEnD,MAAM,WAAW,qBAAqB;IACpC,GAAG,EAAE,eAAe,CAAA;IACpB,eAAe,EAAE,QAAQ,CAAA;IACzB,aAAa,EAAE,SAAS,CAAA;IACxB,KAAK,EAAE,KAAK,CAAA;IACZ,IAAI,EAAE,IAAI,CAAA;IACV,QAAQ,EAAE,QAAQ,CAAA;IAClB,IAAI,EAAE,SAAS,CAAA;IACf,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,OAAO,CAAC,EAAE,cAAc,CAAA;C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,EAAE,MAAM,CAAC;IACpF,eAAe,EAAE,CAAC,MAAM,EAAE,qBAAqB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CACrE"}
|
|
@@ -16,4 +16,5 @@ export declare const comparePassword: (plaintext: string, storedPassword: string
|
|
|
16
16
|
* @param length -> the token length
|
|
17
17
|
*/
|
|
18
18
|
export declare const generateToken: (length?: number) => string;
|
|
19
|
+
export declare const hashToken: (token: string) => string;
|
|
19
20
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/crypto/index.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAU,WAAW,MAAM,oBAInD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAU,WAAW,MAAM,EAAE,gBAAgB,MAAM,qBAU9E,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,eAAW,WAExC,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/crypto/index.ts"],"names":[],"mappings":"AAKA;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAU,WAAW,MAAM,oBAInD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAU,WAAW,MAAM,EAAE,gBAAgB,MAAM,qBAU9E,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,eAAW,WAExC,CAAA;AAED,eAAO,MAAM,SAAS,GAAI,OAAO,MAAM,WAEtC,CAAA"}
|