@lilaquadrat/studio 10.0.0-beta.10 → 10.0.0-beta.12
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/eslint.config.js +146 -0
- package/lib/helpers.d.ts +6 -2
- package/lib/helpers.js +9 -2
- package/lib/helpers.js.map +1 -1
- package/lib/main.d.ts +1 -3
- package/lib/main.js +7 -3
- package/lib/main.js.map +1 -1
- package/lib/services.d.ts +1 -6
- package/lib/services.js +18 -7
- package/lib/services.js.map +1 -1
- package/lib/src/ShareClientFactory.class.d.ts +1 -3
- package/lib/src/ShareClientFactory.class.js +1 -9
- package/lib/src/ShareClientFactory.class.js.map +1 -1
- package/lib/src/classes/models.class.js.map +1 -1
- package/lib/src/classes/mongo.class.js +4 -14
- package/lib/src/classes/mongo.class.js.map +1 -1
- package/lib/src/functions/handleError.d.ts +2 -3
- package/lib/src/functions/handleError.js +3 -16
- package/lib/src/functions/handleError.js.map +1 -1
- package/lib/src/functions/optionsHelper.d.ts +4 -4
- package/lib/src/functions/optionsHelper.js +5 -4
- package/lib/src/functions/optionsHelper.js.map +1 -1
- package/lib/src/helpers/ControllerHelper.d.ts +73 -0
- package/lib/src/helpers/ControllerHelper.js +242 -0
- package/lib/src/helpers/ControllerHelper.js.map +1 -0
- package/lib/src/helpers/EnvMapper.d.ts +0 -0
- package/lib/src/helpers/EnvMapper.js +1 -0
- package/lib/src/helpers/EnvMapper.js.map +1 -0
- package/lib/src/helpers/authPlugin.d.ts +29 -0
- package/lib/src/helpers/authPlugin.js +72 -0
- package/lib/src/helpers/authPlugin.js.map +1 -0
- package/lib/src/helpers/cacheHelper.d.ts +69 -0
- package/lib/src/helpers/cacheHelper.js +235 -0
- package/lib/src/helpers/cacheHelper.js.map +1 -0
- package/lib/src/helpers/createSasToken.d.ts +0 -2
- package/lib/src/helpers/createSasToken.js +35 -32
- package/lib/src/helpers/createSasToken.js.map +1 -1
- package/lib/src/helpers/getSecrets.d.ts +1 -1
- package/lib/src/helpers/getSecrets.js +10 -12
- package/lib/src/helpers/getSecrets.js.map +1 -1
- package/lib/src/helpers/queryAssertionPlugin.d.ts +3 -0
- package/lib/src/helpers/queryAssertionPlugin.js +20 -0
- package/lib/src/helpers/queryAssertionPlugin.js.map +1 -0
- package/lib/src/helpers/safeObjectId.d.ts +1 -1
- package/lib/src/helpers/safeObjectId.js +5 -1
- package/lib/src/helpers/safeObjectId.js.map +1 -1
- package/lib/src/helpers/studioAppPlugin.d.ts +3 -0
- package/lib/src/helpers/studioAppPlugin.js +16 -0
- package/lib/src/helpers/studioAppPlugin.js.map +1 -0
- package/lib/src/logger.js +40 -2
- package/lib/src/logger.js.map +1 -1
- package/lib/src/models/certificate-action.model.js +0 -1
- package/lib/src/models/certificate-action.model.js.map +1 -1
- package/lib/src/models/customers.model.js +3 -0
- package/lib/src/models/customers.model.js.map +1 -1
- package/lib/src/models/editor.model.js +7 -0
- package/lib/src/models/editor.model.js.map +1 -1
- package/lib/src/models/project.model.js +2 -4
- package/lib/src/models/project.model.js.map +1 -1
- package/lib/src/services/access.service.d.ts +0 -37
- package/lib/src/services/access.service.js +119 -108
- package/lib/src/services/access.service.js.map +1 -1
- package/lib/src/services/editor.service.d.ts +4 -0
- package/lib/src/services/editor.service.js +28 -0
- package/lib/src/services/editor.service.js.map +1 -1
- package/lib/src/services/editorBase.service.d.ts +0 -46
- package/lib/src/services/editorBase.service.js +184 -160
- package/lib/src/services/editorBase.service.js.map +1 -1
- package/lib/src/services/hosting.service.js +1 -1
- package/lib/src/services/hosting.service.js.map +1 -1
- package/lib/src/services/import.service.d.ts +5 -21
- package/lib/src/services/import.service.js +63 -65
- package/lib/src/services/import.service.js.map +1 -1
- package/lib/src/services/listParticipants.service.d.ts +0 -3
- package/lib/src/services/listParticipants.service.js +16 -14
- package/lib/src/services/listParticipants.service.js.map +1 -1
- package/lib/src/services/me.service.d.ts +17 -7
- package/lib/src/services/me.service.js +48 -86
- package/lib/src/services/me.service.js.map +1 -1
- package/lib/src/services/publish.service.d.ts +0 -4
- package/lib/src/services/publish.service.js +33 -32
- package/lib/src/services/publish.service.js.map +1 -1
- package/lib/src/services/publishData.service.d.ts +10 -7
- package/lib/src/services/publishData.service.js +32 -75
- package/lib/src/services/publishData.service.js.map +1 -1
- package/lib/src/services/storage.service.d.ts +1 -23
- package/lib/src/services/storage.service.js +6 -28
- package/lib/src/services/storage.service.js.map +1 -1
- package/package.json +21 -23
- package/lib/src/AzureBlobStorage.share.d.ts +0 -19
- package/lib/src/AzureBlobStorage.share.js +0 -162
- package/lib/src/AzureBlobStorage.share.js.map +0 -1
- package/lib/src/AzureFileStorage.share.d.ts +0 -22
- package/lib/src/AzureFileStorage.share.js +0 -139
- package/lib/src/AzureFileStorage.share.js.map +0 -1
- package/lib/src/AzureVault.d.ts +0 -14
- package/lib/src/AzureVault.js +0 -28
- package/lib/src/AzureVault.js.map +0 -1
- package/lib/src/dns.challenge.class.d.ts +0 -17
- package/lib/src/dns.challenge.class.js +0 -41
- package/lib/src/dns.challenge.class.js.map +0 -1
- package/lib/src/http.challenge.class.d.ts +0 -33
- package/lib/src/http.challenge.class.js +0 -58
- package/lib/src/http.challenge.class.js.map +0 -1
- package/lib/src/services/handleFile.service.d.ts +0 -9
- package/lib/src/services/handleFile.service.js +0 -45
- package/lib/src/services/handleFile.service.js.map +0 -1
- package/lib/src/services/media.service.d.ts +0 -35
- package/lib/src/services/media.service.js +0 -418
- package/lib/src/services/media.service.js.map +0 -1
- package/lib/src/services/share.service.d.ts +0 -6
- package/lib/src/services/share.service.js +0 -4
- package/lib/src/services/share.service.js.map +0 -1
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
export default class ControllerHelper {
|
|
2
|
+
cleanErrors(errors) {
|
|
3
|
+
const errorObject = {};
|
|
4
|
+
if (errors.errors) {
|
|
5
|
+
Object.keys(errors.errors).forEach((key) => {
|
|
6
|
+
errorObject[key] = errors.errors[key].message;
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
else if (typeof errors === 'object') {
|
|
10
|
+
if (errors.code === 11000) {
|
|
11
|
+
const message = errors.message.match(/index: (.*)_/i);
|
|
12
|
+
errorObject[message[1]] = `${message[1]}_duplicate`;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
errorObject.error = errors.message;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return errorObject;
|
|
19
|
+
}
|
|
20
|
+
response(res, data, count) {
|
|
21
|
+
count || count === 0
|
|
22
|
+
? res.json({ data, count })
|
|
23
|
+
: res.json({ data, count: data.length });
|
|
24
|
+
}
|
|
25
|
+
static buildResponse(data, site, limit, hardLimit = 30) {
|
|
26
|
+
return {
|
|
27
|
+
data: data.data,
|
|
28
|
+
all: data.all,
|
|
29
|
+
sites: ControllerHelper.getSites(+site, ControllerHelper.getRealLimit(limit, hardLimit), data.all, data.count),
|
|
30
|
+
count: data.count,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
static getLimit(site, limit, hardLimit) {
|
|
34
|
+
const realLimit = ControllerHelper.getRealLimit(limit, hardLimit);
|
|
35
|
+
return {
|
|
36
|
+
skip: site * realLimit || 0,
|
|
37
|
+
limit: realLimit,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
static getRealLimit(limit, hardLimit) {
|
|
41
|
+
let realLimit;
|
|
42
|
+
if (hardLimit) {
|
|
43
|
+
realLimit = limit > hardLimit ? hardLimit : limit;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
realLimit = limit;
|
|
47
|
+
}
|
|
48
|
+
return realLimit;
|
|
49
|
+
}
|
|
50
|
+
// getSort(sort?: string, order?: '1' | 0) {
|
|
51
|
+
// const sortObject = {};
|
|
52
|
+
// if (sort && order) {
|
|
53
|
+
// sortObject[sort] = order === '1' ? 1 : -1;
|
|
54
|
+
// return sortObject;
|
|
55
|
+
// }
|
|
56
|
+
// return { _id: 1 };
|
|
57
|
+
// }
|
|
58
|
+
/**
|
|
59
|
+
*
|
|
60
|
+
*
|
|
61
|
+
* @param {number} site
|
|
62
|
+
* @param {number} limit
|
|
63
|
+
* @param {number} count all data count regardless of site
|
|
64
|
+
* @param {number} current current count of documents
|
|
65
|
+
* @returns {DatabaseQuerySites}
|
|
66
|
+
* @memberof ControllerBase
|
|
67
|
+
*/
|
|
68
|
+
static getSites(site, limit, count, current) {
|
|
69
|
+
const realSite = site - 1;
|
|
70
|
+
if (count === 0) {
|
|
71
|
+
return {
|
|
72
|
+
first: 0,
|
|
73
|
+
current: 0,
|
|
74
|
+
last: 0,
|
|
75
|
+
range: [0, 0],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
first: 1,
|
|
80
|
+
current: site || 1,
|
|
81
|
+
last: count ? Math.ceil(count / limit) : 1,
|
|
82
|
+
range: [
|
|
83
|
+
(realSite * limit) + 1 || 1,
|
|
84
|
+
realSite * limit + current || current,
|
|
85
|
+
],
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
*
|
|
90
|
+
*
|
|
91
|
+
* @param {DatabaseQuery} query
|
|
92
|
+
* @param {string} [company]
|
|
93
|
+
* @param {string} [project]
|
|
94
|
+
* @param {DatabaseQueryOptions} [options]
|
|
95
|
+
* @returns
|
|
96
|
+
* @memberof ControllerBase
|
|
97
|
+
*/
|
|
98
|
+
static buildQuery(query, company, project, options) {
|
|
99
|
+
const realQuery = { ...query };
|
|
100
|
+
const orArray = [];
|
|
101
|
+
let tags;
|
|
102
|
+
let category;
|
|
103
|
+
let keepTags = false;
|
|
104
|
+
let keepSearch = false;
|
|
105
|
+
let keepSort = false;
|
|
106
|
+
if (company)
|
|
107
|
+
realQuery.company = company;
|
|
108
|
+
if (project)
|
|
109
|
+
realQuery.project = project;
|
|
110
|
+
if (project === false)
|
|
111
|
+
realQuery.project = { $exists: false };
|
|
112
|
+
if (options) {
|
|
113
|
+
if (options.keepTags)
|
|
114
|
+
keepTags = true;
|
|
115
|
+
if (options.keepSearch)
|
|
116
|
+
keepSearch = true;
|
|
117
|
+
if (options.keepSort)
|
|
118
|
+
keepSort = true;
|
|
119
|
+
}
|
|
120
|
+
if (!keepSort) {
|
|
121
|
+
delete realQuery.sort;
|
|
122
|
+
delete realQuery.order;
|
|
123
|
+
}
|
|
124
|
+
if (query.partial !== undefined) {
|
|
125
|
+
realQuery.partial = query.partial
|
|
126
|
+
? true
|
|
127
|
+
: { $exists: false };
|
|
128
|
+
}
|
|
129
|
+
if (query.layout !== undefined) {
|
|
130
|
+
realQuery.layout = query.layout
|
|
131
|
+
? true
|
|
132
|
+
: { $exists: false };
|
|
133
|
+
}
|
|
134
|
+
if (query.available) {
|
|
135
|
+
realQuery.available = query.available === true || (query.available === 'true');
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* make sure tags are an array
|
|
139
|
+
*/
|
|
140
|
+
if (query.tags && !keepTags) {
|
|
141
|
+
if (!Array.isArray(query.tags)) {
|
|
142
|
+
tags = [query.tags];
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
tags = query.tags;
|
|
146
|
+
}
|
|
147
|
+
realQuery.tags = { $in: tags };
|
|
148
|
+
}
|
|
149
|
+
if (query.category) {
|
|
150
|
+
if (!Array.isArray(query.category)) {
|
|
151
|
+
category = [query.category];
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
category = query.category;
|
|
155
|
+
}
|
|
156
|
+
realQuery.category = { $in: category };
|
|
157
|
+
}
|
|
158
|
+
if (query.search && !keepSearch) {
|
|
159
|
+
if (options?.search) {
|
|
160
|
+
if (options.search.length > 1) {
|
|
161
|
+
options.search.forEach((single) => {
|
|
162
|
+
orArray.push({ [single]: { $regex: query.search.toString().toLowerCase().trim(), $options: 'i' } });
|
|
163
|
+
});
|
|
164
|
+
realQuery.$or = orArray;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
realQuery[options.search[0]] = { $regex: query.search.toString().toLowerCase().trim(), $options: 'i' };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
delete realQuery.search;
|
|
171
|
+
}
|
|
172
|
+
return realQuery;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
*
|
|
176
|
+
*
|
|
177
|
+
* @param {DatabaseQuery} query
|
|
178
|
+
* @param {string} select
|
|
179
|
+
* @param {number} site start counting at 1 instead of zero. no site / zero will be the same as 1
|
|
180
|
+
* @param {number} limit
|
|
181
|
+
* @param {Model<Document>} model
|
|
182
|
+
* @param {string} [sort]
|
|
183
|
+
* @param {string} [order]
|
|
184
|
+
* @returns
|
|
185
|
+
* @memberof ControllerBase
|
|
186
|
+
*/
|
|
187
|
+
static async list(query, select, site, limit, model, sort, order, selectDefault) {
|
|
188
|
+
let projection;
|
|
189
|
+
const skipLimitSort = ControllerHelper.getSkipLimitSort(site, limit, sort, order);
|
|
190
|
+
if (select && selectDefault) {
|
|
191
|
+
projection = { projection: { ...select, ...selectDefault } };
|
|
192
|
+
}
|
|
193
|
+
else if (select) {
|
|
194
|
+
projection = { projection: select };
|
|
195
|
+
}
|
|
196
|
+
else if (selectDefault) {
|
|
197
|
+
projection = { projection: selectDefault };
|
|
198
|
+
}
|
|
199
|
+
const all = await model.db.countDocuments(query);
|
|
200
|
+
const data = await model.db.find(query, projection)
|
|
201
|
+
.limit(skipLimitSort.limit)
|
|
202
|
+
.skip(skipLimitSort.skip)
|
|
203
|
+
.sort(skipLimitSort.sort)
|
|
204
|
+
.toArray();
|
|
205
|
+
return { data, all, count: data.length };
|
|
206
|
+
}
|
|
207
|
+
static async listCustomQuery(queries, model) {
|
|
208
|
+
const all = await model.db.countDocuments(queries.countQuery);
|
|
209
|
+
const data = await model.db.aggregate(queries.aggregation)
|
|
210
|
+
.toArray();
|
|
211
|
+
return { all, data, count: data.length };
|
|
212
|
+
}
|
|
213
|
+
static getSkipLimitSort(site, limit, sort, order) {
|
|
214
|
+
let realSite = 0;
|
|
215
|
+
let realSort = {};
|
|
216
|
+
let sortKey;
|
|
217
|
+
/**
|
|
218
|
+
* see comment from this function
|
|
219
|
+
*/
|
|
220
|
+
if (site)
|
|
221
|
+
realSite = site - 1;
|
|
222
|
+
const skipLimit = ControllerHelper.getLimit(realSite, limit, limit);
|
|
223
|
+
if (sort && order) {
|
|
224
|
+
if (sort === 'updated') {
|
|
225
|
+
sortKey = 'history.updated';
|
|
226
|
+
}
|
|
227
|
+
else if (sort === 'created') {
|
|
228
|
+
sortKey = 'history.created';
|
|
229
|
+
}
|
|
230
|
+
else
|
|
231
|
+
sortKey = sort;
|
|
232
|
+
realSort[sortKey] = +order;
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
realSort = { _id: 1 };
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
sort: realSort, ...skipLimit,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
//# sourceMappingURL=ControllerHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ControllerHelper.js","sourceRoot":"","sources":["../../../src/helpers/ControllerHelper.ts"],"names":[],"mappings":"AAOA,MAAM,CAAC,OAAO,OAAO,gBAAgB;IAEnC,WAAW,CAAC,MAA8B;QAExC,MAAM,WAAW,GAA2B,EAAE,CAAC;QAE/C,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YAElB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,GAAW,EAAE,EAAE;gBAEjD,WAAW,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC;YAEhD,CAAC,CAAC,CAAC;QAEL,CAAC;aAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAEtC,IAAI,MAAM,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;gBAE1B,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,eAAe,CAAC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AC,GAAG,CAAC,KAAsB,CAAC;QAE9C,CAAC;aAAM,CAAC;YAEN,QAAQ,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;QAExB,CAAC;QAED,OAAO;YACL,IAAI,EAAE,QAAQ,EAAE,GAAG,SAAS;SAC7B,CAAC;IAEJ,CAAC;CAEF"}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=EnvMapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvMapper.js","sourceRoot":"","sources":["../../../src/helpers/EnvMapper.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FastifyPluginAsync } from 'fastify';
|
|
2
|
+
type OAuthJwtUser = {
|
|
3
|
+
given_name: string;
|
|
4
|
+
family_name: string;
|
|
5
|
+
nickname: string;
|
|
6
|
+
name: string;
|
|
7
|
+
picture: string;
|
|
8
|
+
updated_at: string;
|
|
9
|
+
iss: string;
|
|
10
|
+
sub: string;
|
|
11
|
+
aud: string;
|
|
12
|
+
iat: number;
|
|
13
|
+
exp: number;
|
|
14
|
+
at_hash: string;
|
|
15
|
+
nonce: string;
|
|
16
|
+
email_verified: boolean;
|
|
17
|
+
gty?: string;
|
|
18
|
+
scope?: string;
|
|
19
|
+
permissions?: string[];
|
|
20
|
+
azp?: string;
|
|
21
|
+
customerInternalId?: any;
|
|
22
|
+
};
|
|
23
|
+
declare module '@fastify/jwt' {
|
|
24
|
+
interface FastifyJWT {
|
|
25
|
+
user: OAuthJwtUser;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
declare const _default: FastifyPluginAsync;
|
|
29
|
+
export default _default;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import fp from 'fastify-plugin';
|
|
2
|
+
import fastifyJwt from '@fastify/jwt';
|
|
3
|
+
import jwksRsa from 'jwks-rsa';
|
|
4
|
+
import respondCode from '../functions/respondCode.js';
|
|
5
|
+
const authPlugin = async (fastify) => {
|
|
6
|
+
if (process.env.ENV !== 'dev') {
|
|
7
|
+
// Create a single JWKS client instance with aggressive caching
|
|
8
|
+
const jwksClient = jwksRsa({
|
|
9
|
+
cache: true,
|
|
10
|
+
cacheMaxEntries: 5,
|
|
11
|
+
cacheMaxAge: 600000, // 10 minutes in ms
|
|
12
|
+
rateLimit: true,
|
|
13
|
+
jwksRequestsPerMinute: 10,
|
|
14
|
+
jwksUri: process.env.JWT_URL,
|
|
15
|
+
});
|
|
16
|
+
// Production: Register JWT with JWKS
|
|
17
|
+
await fastify.register(fastifyJwt, {
|
|
18
|
+
decode: { complete: true },
|
|
19
|
+
secret: (request, token, callback) => {
|
|
20
|
+
const { header } = token;
|
|
21
|
+
// Use the cached client instance
|
|
22
|
+
jwksClient.getSigningKey(header.kid, (err, key) => {
|
|
23
|
+
if (err) {
|
|
24
|
+
callback(err, undefined);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const signingKey = key?.getPublicKey();
|
|
28
|
+
callback(null, signingKey);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
verify: {
|
|
33
|
+
algorithms: ['RS256']
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
// Decorate with authenticate method for production
|
|
37
|
+
fastify.decorate('authenticate', async (request, reply) => {
|
|
38
|
+
try {
|
|
39
|
+
await request.jwtVerify();
|
|
40
|
+
request.auth = request.user;
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
return respondCode("UNAUTHORIZED", reply);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// Dev: Mock authentication
|
|
49
|
+
fastify.decorate('authenticate', async (request, reply) => {
|
|
50
|
+
request.auth = {
|
|
51
|
+
sub: process.env.DEV_USER,
|
|
52
|
+
given_name: 'Dev',
|
|
53
|
+
family_name: 'User',
|
|
54
|
+
nickname: 'devuser',
|
|
55
|
+
name: 'Dev User',
|
|
56
|
+
picture: '',
|
|
57
|
+
updated_at: new Date().toISOString(),
|
|
58
|
+
iss: 'dev',
|
|
59
|
+
aud: 'dev',
|
|
60
|
+
iat: Math.floor(Date.now() / 1000),
|
|
61
|
+
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
62
|
+
at_hash: '',
|
|
63
|
+
nonce: '',
|
|
64
|
+
email_verified: true
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
export default fp(authPlugin, {
|
|
70
|
+
name: 'auth-plugin'
|
|
71
|
+
});
|
|
72
|
+
//# sourceMappingURL=authPlugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authPlugin.js","sourceRoot":"","sources":["../../../src/helpers/authPlugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAChC,OAAO,UAAU,MAAM,cAAc,CAAC;AACtC,OAAO,OAAO,MAAM,UAAU,CAAC;AAC/B,OAAO,WAAW,MAAM,6BAA6B,CAAC;AA+BtD,MAAM,UAAU,GAAuB,KAAK,EAAE,OAAwB,EAAE,EAAE;IAExE,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,KAAK,EAAE,CAAC;QAE9B,+DAA+D;QAC/D,MAAM,UAAU,GAAG,OAAO,CAAC;YACzB,KAAK,EAAE,IAAI;YACX,eAAe,EAAE,CAAC;YAClB,WAAW,EAAE,MAAM,EAAE,mBAAmB;YACxC,SAAS,EAAE,IAAI;YACf,qBAAqB,EAAE,EAAE;YACzB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,OAAiB;SACvC,CAAC,CAAC;QAEH,qCAAqC;QACrC,MAAM,OAAO,CAAC,QAAQ,CAAC,UAAU,EAAE;YACjC,MAAM,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE;YAC1B,MAAM,EAAE,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,EAA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,CAAC,WAAW,EAAE;gBACpC,GAAG,EAAE,KAAK;gBACV,GAAG,EAAE,KAAK;gBACV,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;gBAClC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,GAAG,IAAI;gBACzC,OAAO,EAAE,EAAE;gBACX,KAAK,EAAE,EAAE;gBACT,cAAc,EAAE,IAAI;aACrB,CAAC;QAEJ,CAAC,CAAC,CAAC;IAEL,CAAC;AAEH,CAAC,CAAC;AAEF,eAAe,EAAE,CAAC,UAAU,EAAE;IAC5B,IAAI,EAAE,aAAa;CACpB,CAAC,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache duration constants (in seconds)
|
|
3
|
+
*/
|
|
4
|
+
export declare const CacheDuration: {
|
|
5
|
+
/** 1 year - for immutable/versioned assets */
|
|
6
|
+
readonly IMMUTABLE: 31536000;
|
|
7
|
+
/** 1 month - for stable assets */
|
|
8
|
+
readonly LONG: 2592000;
|
|
9
|
+
/** 1 week - for semi-stable assets */
|
|
10
|
+
readonly MEDIUM: 604800;
|
|
11
|
+
/** 1 day - for frequently updated assets */
|
|
12
|
+
readonly SHORT: 86400;
|
|
13
|
+
/** 1 hour - for dynamic content */
|
|
14
|
+
readonly DYNAMIC: 3600;
|
|
15
|
+
/** No cache - for always fresh content */
|
|
16
|
+
readonly NONE: 0;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Cache helper class for managing HTTP cache headers and durations
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Get cache duration
|
|
23
|
+
* const duration = CacheHelper.getCacheDuration('image/jpeg'); // 2592000
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Build cache control header
|
|
27
|
+
* const header = CacheHelper.buildCacheControlHeader('image/jpeg');
|
|
28
|
+
* // "public, max-age=2592000, stale-while-revalidate=86400"
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* // Get full config
|
|
32
|
+
* const config = CacheHelper.getCacheConfig('font/woff2');
|
|
33
|
+
* // { cache: 31536000, immutable: true }
|
|
34
|
+
*/
|
|
35
|
+
export default class CacheHelper {
|
|
36
|
+
/**
|
|
37
|
+
* Get cache configuration for a given mime type
|
|
38
|
+
*
|
|
39
|
+
* @param mimeType - The mime type to get configuration for
|
|
40
|
+
* @returns Cache configuration object
|
|
41
|
+
*/
|
|
42
|
+
static getCacheConfig(mimeType: string): {
|
|
43
|
+
cache: number;
|
|
44
|
+
immutable?: boolean;
|
|
45
|
+
staleWhileRevalidate?: number;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Get cache duration in seconds for a given mime type
|
|
49
|
+
*
|
|
50
|
+
* @param mimeType - The mime type to get cache duration for
|
|
51
|
+
* @returns Cache duration in seconds
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* const cacheTime = CacheHelper.getCacheDuration('image/jpeg'); // Returns 2592000 (1 month)
|
|
55
|
+
* const cacheTime = CacheHelper.getCacheDuration('text/html'); // Returns 0 (no cache)
|
|
56
|
+
*/
|
|
57
|
+
static getCacheDuration(mimeType: string): number;
|
|
58
|
+
/**
|
|
59
|
+
* Build Cache-Control header value for a given mime type
|
|
60
|
+
*
|
|
61
|
+
* @param mimeType - The mime type to build header for
|
|
62
|
+
* @returns Cache-Control header value
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* const header = CacheHelper.buildCacheControlHeader('image/jpeg');
|
|
66
|
+
* // Returns: "public, max-age=2592000, stale-while-revalidate=86400"
|
|
67
|
+
*/
|
|
68
|
+
static buildCacheControlHeader(mimeType: string): string;
|
|
69
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache duration constants (in seconds)
|
|
3
|
+
*/
|
|
4
|
+
export const CacheDuration = {
|
|
5
|
+
/** 1 year - for immutable/versioned assets */
|
|
6
|
+
IMMUTABLE: 31536000,
|
|
7
|
+
/** 1 month - for stable assets */
|
|
8
|
+
LONG: 2592000,
|
|
9
|
+
/** 1 week - for semi-stable assets */
|
|
10
|
+
MEDIUM: 604800,
|
|
11
|
+
/** 1 day - for frequently updated assets */
|
|
12
|
+
SHORT: 86400,
|
|
13
|
+
/** 1 hour - for dynamic content */
|
|
14
|
+
DYNAMIC: 3600,
|
|
15
|
+
/** No cache - for always fresh content */
|
|
16
|
+
NONE: 0,
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Mime type cache configuration
|
|
20
|
+
* Maps mime types to cache duration and other headers
|
|
21
|
+
*/
|
|
22
|
+
const MimeCacheConfig = {
|
|
23
|
+
// HTML - no cache (always fetch fresh)
|
|
24
|
+
'text/html': {
|
|
25
|
+
cache: CacheDuration.NONE,
|
|
26
|
+
},
|
|
27
|
+
// JSON/API responses - no cache
|
|
28
|
+
'application/json': {
|
|
29
|
+
cache: CacheDuration.NONE,
|
|
30
|
+
},
|
|
31
|
+
// Images - long cache (1 month)
|
|
32
|
+
'image/jpeg': {
|
|
33
|
+
cache: CacheDuration.LONG,
|
|
34
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
35
|
+
},
|
|
36
|
+
'image/jpg': {
|
|
37
|
+
cache: CacheDuration.LONG,
|
|
38
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
39
|
+
},
|
|
40
|
+
'image/png': {
|
|
41
|
+
cache: CacheDuration.LONG,
|
|
42
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
43
|
+
},
|
|
44
|
+
'image/gif': {
|
|
45
|
+
cache: CacheDuration.LONG,
|
|
46
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
47
|
+
},
|
|
48
|
+
'image/webp': {
|
|
49
|
+
cache: CacheDuration.LONG,
|
|
50
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
51
|
+
},
|
|
52
|
+
'image/avif': {
|
|
53
|
+
cache: CacheDuration.LONG,
|
|
54
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
55
|
+
},
|
|
56
|
+
'image/svg+xml': {
|
|
57
|
+
cache: CacheDuration.LONG,
|
|
58
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
59
|
+
},
|
|
60
|
+
// Videos - long cache
|
|
61
|
+
'video/mp4': {
|
|
62
|
+
cache: CacheDuration.LONG,
|
|
63
|
+
},
|
|
64
|
+
'video/webm': {
|
|
65
|
+
cache: CacheDuration.LONG,
|
|
66
|
+
},
|
|
67
|
+
'video/ogg': {
|
|
68
|
+
cache: CacheDuration.LONG,
|
|
69
|
+
},
|
|
70
|
+
// Audio - long cache
|
|
71
|
+
'audio/mpeg': {
|
|
72
|
+
cache: CacheDuration.LONG,
|
|
73
|
+
},
|
|
74
|
+
'audio/mp3': {
|
|
75
|
+
cache: CacheDuration.LONG,
|
|
76
|
+
},
|
|
77
|
+
'audio/wav': {
|
|
78
|
+
cache: CacheDuration.LONG,
|
|
79
|
+
},
|
|
80
|
+
'audio/ogg': {
|
|
81
|
+
cache: CacheDuration.LONG,
|
|
82
|
+
},
|
|
83
|
+
// Fonts - immutable (1 year)
|
|
84
|
+
'font/woff': {
|
|
85
|
+
cache: CacheDuration.IMMUTABLE,
|
|
86
|
+
immutable: true,
|
|
87
|
+
},
|
|
88
|
+
'font/woff2': {
|
|
89
|
+
cache: CacheDuration.IMMUTABLE,
|
|
90
|
+
immutable: true,
|
|
91
|
+
},
|
|
92
|
+
'font/ttf': {
|
|
93
|
+
cache: CacheDuration.IMMUTABLE,
|
|
94
|
+
immutable: true,
|
|
95
|
+
},
|
|
96
|
+
'font/otf': {
|
|
97
|
+
cache: CacheDuration.IMMUTABLE,
|
|
98
|
+
immutable: true,
|
|
99
|
+
},
|
|
100
|
+
'application/font-woff': {
|
|
101
|
+
cache: CacheDuration.IMMUTABLE,
|
|
102
|
+
immutable: true,
|
|
103
|
+
},
|
|
104
|
+
'application/font-woff2': {
|
|
105
|
+
cache: CacheDuration.IMMUTABLE,
|
|
106
|
+
immutable: true,
|
|
107
|
+
},
|
|
108
|
+
// CSS/JS - long cache with stale-while-revalidate
|
|
109
|
+
'text/css': {
|
|
110
|
+
cache: CacheDuration.LONG,
|
|
111
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
112
|
+
},
|
|
113
|
+
'application/javascript': {
|
|
114
|
+
cache: CacheDuration.LONG,
|
|
115
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
116
|
+
},
|
|
117
|
+
'text/javascript': {
|
|
118
|
+
cache: CacheDuration.LONG,
|
|
119
|
+
staleWhileRevalidate: CacheDuration.SHORT,
|
|
120
|
+
},
|
|
121
|
+
// Documents - medium cache
|
|
122
|
+
'application/pdf': {
|
|
123
|
+
cache: CacheDuration.MEDIUM,
|
|
124
|
+
},
|
|
125
|
+
'application/msword': {
|
|
126
|
+
cache: CacheDuration.MEDIUM,
|
|
127
|
+
},
|
|
128
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': {
|
|
129
|
+
cache: CacheDuration.MEDIUM,
|
|
130
|
+
},
|
|
131
|
+
'application/vnd.ms-excel': {
|
|
132
|
+
cache: CacheDuration.MEDIUM,
|
|
133
|
+
},
|
|
134
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': {
|
|
135
|
+
cache: CacheDuration.MEDIUM,
|
|
136
|
+
},
|
|
137
|
+
// Archives - long cache
|
|
138
|
+
'application/zip': {
|
|
139
|
+
cache: CacheDuration.LONG,
|
|
140
|
+
},
|
|
141
|
+
'application/x-rar-compressed': {
|
|
142
|
+
cache: CacheDuration.LONG,
|
|
143
|
+
},
|
|
144
|
+
'application/x-7z-compressed': {
|
|
145
|
+
cache: CacheDuration.LONG,
|
|
146
|
+
},
|
|
147
|
+
// Text files - short cache
|
|
148
|
+
'text/plain': {
|
|
149
|
+
cache: CacheDuration.SHORT,
|
|
150
|
+
},
|
|
151
|
+
'text/csv': {
|
|
152
|
+
cache: CacheDuration.SHORT,
|
|
153
|
+
},
|
|
154
|
+
// XML - no cache (often dynamic)
|
|
155
|
+
'application/xml': {
|
|
156
|
+
cache: CacheDuration.NONE,
|
|
157
|
+
},
|
|
158
|
+
'text/xml': {
|
|
159
|
+
cache: CacheDuration.NONE,
|
|
160
|
+
},
|
|
161
|
+
// Default fallback - 1 year cache for everything else
|
|
162
|
+
'*': {
|
|
163
|
+
cache: CacheDuration.IMMUTABLE,
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Cache helper class for managing HTTP cache headers and durations
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* // Get cache duration
|
|
171
|
+
* const duration = CacheHelper.getCacheDuration('image/jpeg'); // 2592000
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* // Build cache control header
|
|
175
|
+
* const header = CacheHelper.buildCacheControlHeader('image/jpeg');
|
|
176
|
+
* // "public, max-age=2592000, stale-while-revalidate=86400"
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* // Get full config
|
|
180
|
+
* const config = CacheHelper.getCacheConfig('font/woff2');
|
|
181
|
+
* // { cache: 31536000, immutable: true }
|
|
182
|
+
*/
|
|
183
|
+
export default class CacheHelper {
|
|
184
|
+
/**
|
|
185
|
+
* Get cache configuration for a given mime type
|
|
186
|
+
*
|
|
187
|
+
* @param mimeType - The mime type to get configuration for
|
|
188
|
+
* @returns Cache configuration object
|
|
189
|
+
*/
|
|
190
|
+
static getCacheConfig(mimeType) {
|
|
191
|
+
return MimeCacheConfig[mimeType] || MimeCacheConfig['*'];
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Get cache duration in seconds for a given mime type
|
|
195
|
+
*
|
|
196
|
+
* @param mimeType - The mime type to get cache duration for
|
|
197
|
+
* @returns Cache duration in seconds
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* const cacheTime = CacheHelper.getCacheDuration('image/jpeg'); // Returns 2592000 (1 month)
|
|
201
|
+
* const cacheTime = CacheHelper.getCacheDuration('text/html'); // Returns 0 (no cache)
|
|
202
|
+
*/
|
|
203
|
+
static getCacheDuration(mimeType) {
|
|
204
|
+
const config = this.getCacheConfig(mimeType);
|
|
205
|
+
return config.cache;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Build Cache-Control header value for a given mime type
|
|
209
|
+
*
|
|
210
|
+
* @param mimeType - The mime type to build header for
|
|
211
|
+
* @returns Cache-Control header value
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* const header = CacheHelper.buildCacheControlHeader('image/jpeg');
|
|
215
|
+
* // Returns: "public, max-age=2592000, stale-while-revalidate=86400"
|
|
216
|
+
*/
|
|
217
|
+
static buildCacheControlHeader(mimeType) {
|
|
218
|
+
const config = this.getCacheConfig(mimeType);
|
|
219
|
+
const parts = [];
|
|
220
|
+
if (config.cache === 0) {
|
|
221
|
+
parts.push('no-cache', 'no-store', 'must-revalidate');
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
parts.push('public', `max-age=${config.cache}`);
|
|
225
|
+
if (config.immutable) {
|
|
226
|
+
parts.push('immutable');
|
|
227
|
+
}
|
|
228
|
+
if (config.staleWhileRevalidate) {
|
|
229
|
+
parts.push(`stale-while-revalidate=${config.staleWhileRevalidate}`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return parts.join(', ');
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
//# sourceMappingURL=cacheHelper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cacheHelper.js","sourceRoot":"","sources":["../../../src/helpers/cacheHelper.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,8CAA8C;IAC9C,SAAS,EAAE,QAAQ;IACnB,kCAAkC;IAClC,IAAI,EA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