@itd-api/cache 0.0.1 → 0.1.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/README.md +48 -28
- package/dist/index.cjs +792 -653
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +171 -251
- package/dist/index.d.ts +171 -251
- package/dist/index.js +787 -648
- package/dist/index.js.map +1 -1
- package/package.json +11 -10
package/dist/index.js
CHANGED
|
@@ -1,676 +1,815 @@
|
|
|
1
|
-
import { LRUCache } from
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { LRUCache } from "lru-cache";
|
|
2
|
+
//#region src/errors.ts
|
|
3
|
+
/** Ошибка настройки или использования плагина кэша. */
|
|
4
4
|
var CacheError = class extends Error {
|
|
5
|
-
|
|
5
|
+
name = "CacheError";
|
|
6
6
|
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/key.ts
|
|
9
|
+
const OMITTED_FIELDS = /* @__PURE__ */ new Set([
|
|
10
|
+
"method",
|
|
11
|
+
"operationId",
|
|
12
|
+
"path",
|
|
13
|
+
"service",
|
|
14
|
+
"baseUrl",
|
|
15
|
+
"query",
|
|
16
|
+
"body",
|
|
17
|
+
"headers",
|
|
18
|
+
"raw",
|
|
19
|
+
"skipAuth",
|
|
20
|
+
"signal",
|
|
21
|
+
"timeout",
|
|
22
|
+
"retry",
|
|
23
|
+
"retrySafety",
|
|
24
|
+
"skipQueue",
|
|
25
|
+
"skipAuthRefresh",
|
|
26
|
+
"extensions"
|
|
25
27
|
]);
|
|
28
|
+
/** Строка query в том же порядке и с теми же правилами, что использует itd-api. */
|
|
26
29
|
function queryKey(query) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return search.toString();
|
|
30
|
+
if (!query) return "";
|
|
31
|
+
const search = new URLSearchParams();
|
|
32
|
+
for (const [key, value] of Object.entries(query)) {
|
|
33
|
+
if (value === void 0 || value === null) continue;
|
|
34
|
+
if (Array.isArray(value)) for (const item of value) search.append(key, String(item));
|
|
35
|
+
else search.append(key, String(value));
|
|
36
|
+
}
|
|
37
|
+
return search.toString();
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Собирает ключ из значений, влияющих на адрес, тело или разобранный ответ.
|
|
41
|
+
*
|
|
42
|
+
* Заголовки и транспортные опции намеренно не входят. Если тело либо опция другого
|
|
43
|
+
* плагина не сериализуются как JSON, запрос выполняется без кэша.
|
|
44
|
+
*/
|
|
45
|
+
function buildCacheKey(operation, request) {
|
|
46
|
+
const extras = {};
|
|
47
|
+
for (const [key, value] of Object.entries(request)) if (!OMITTED_FIELDS.has(key) && value !== void 0) extras[key] = value;
|
|
48
|
+
const { cache: _cacheMode, ...extensions } = request.extensions ?? {};
|
|
49
|
+
if (Object.keys(extensions).length > 0) extras.extensions = extensions;
|
|
50
|
+
try {
|
|
51
|
+
return JSON.stringify({
|
|
52
|
+
operation,
|
|
53
|
+
method: request.method.toUpperCase(),
|
|
54
|
+
service: request.service ?? null,
|
|
55
|
+
baseUrl: request.baseUrl ?? null,
|
|
56
|
+
path: request.path,
|
|
57
|
+
query: queryKey(request.query),
|
|
58
|
+
body: request.body ?? null,
|
|
59
|
+
raw: request.raw ?? false,
|
|
60
|
+
skipAuth: request.skipAuth ?? null,
|
|
61
|
+
extras
|
|
62
|
+
});
|
|
63
|
+
} catch {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
60
66
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/operations.ts
|
|
69
|
+
function freezeOperations(operations) {
|
|
70
|
+
for (const operation of operations) Object.freeze(operation);
|
|
71
|
+
return Object.freeze(operations);
|
|
72
|
+
}
|
|
73
|
+
/** Читающие операции, которые можно кэшировать. */
|
|
74
|
+
const CACHE_OPERATIONS = freezeOperations([
|
|
75
|
+
{
|
|
76
|
+
id: "auth.sessions",
|
|
77
|
+
category: "auth"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: "users.me",
|
|
81
|
+
category: "users"
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: "users.checkUsername",
|
|
85
|
+
category: "users"
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
id: "users.search",
|
|
89
|
+
category: "users"
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
id: "users.whoToFollow",
|
|
93
|
+
category: "users"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
id: "users.topClans",
|
|
97
|
+
category: "users"
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
id: "users.followers",
|
|
101
|
+
category: "users"
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
id: "users.following",
|
|
105
|
+
category: "users"
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
id: "users.blocked",
|
|
109
|
+
category: "users"
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
id: "users.getPrivacy",
|
|
113
|
+
category: "users"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
id: "users.pins",
|
|
117
|
+
category: "users"
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
id: "users.followStatus",
|
|
121
|
+
category: "users"
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: "users.get",
|
|
125
|
+
category: "users"
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
id: "posts.list",
|
|
129
|
+
category: "posts"
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
id: "posts.likedByUser",
|
|
133
|
+
category: "posts"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
id: "posts.byUser",
|
|
137
|
+
category: "posts"
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
id: "posts.comments",
|
|
141
|
+
category: "posts"
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
id: "posts.stats",
|
|
145
|
+
category: "posts"
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
id: "posts.get",
|
|
149
|
+
category: "posts"
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
id: "comments.replies",
|
|
153
|
+
category: "comments"
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
id: "notifications.list",
|
|
157
|
+
category: "notifications"
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
id: "notifications.count",
|
|
161
|
+
category: "notifications"
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
id: "notifications.getSettings",
|
|
165
|
+
category: "notifications"
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
id: "hashtags.search",
|
|
169
|
+
category: "hashtags"
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
id: "hashtags.trending",
|
|
173
|
+
category: "hashtags"
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
id: "hashtags.posts",
|
|
177
|
+
category: "hashtags"
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
id: "search.all",
|
|
181
|
+
category: "search"
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
id: "files.get",
|
|
185
|
+
category: "files"
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
id: "subscription.status",
|
|
189
|
+
category: "subscription"
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
id: "subscription.methods",
|
|
193
|
+
category: "subscription"
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
id: "verification.status",
|
|
197
|
+
category: "verification"
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
id: "platform.changelog",
|
|
201
|
+
category: "platform"
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
id: "platform.announcements",
|
|
205
|
+
category: "platform"
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
id: "platform.portal",
|
|
209
|
+
category: "platform"
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
id: "platform.status",
|
|
213
|
+
category: "platform"
|
|
214
|
+
}
|
|
215
|
+
]);
|
|
216
|
+
const OPERATIONS = new Map(CACHE_OPERATIONS.map((operation) => [operation.id, operation]));
|
|
217
|
+
/** Проверяет публичное имя кэшируемой операции. */
|
|
218
|
+
function isCacheOperationId(value) {
|
|
219
|
+
return OPERATIONS.has(value);
|
|
220
|
+
}
|
|
221
|
+
/** Находит читающую операцию по стабильному семантическому ID. */
|
|
222
|
+
function cacheOperation(operationId) {
|
|
223
|
+
return OPERATIONS.get(operationId);
|
|
224
|
+
}
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/mutations.ts
|
|
227
|
+
const POST_CONTENT = [
|
|
228
|
+
"posts.list",
|
|
229
|
+
"posts.get",
|
|
230
|
+
"posts.byUser",
|
|
231
|
+
"posts.likedByUser",
|
|
232
|
+
"posts.comments",
|
|
233
|
+
"posts.stats",
|
|
234
|
+
"comments.replies",
|
|
235
|
+
"hashtags.search",
|
|
236
|
+
"hashtags.trending",
|
|
237
|
+
"hashtags.posts",
|
|
238
|
+
"search.all",
|
|
239
|
+
"users.me",
|
|
240
|
+
"users.get",
|
|
241
|
+
"users.pins"
|
|
96
242
|
];
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
"users.following",
|
|
106
|
-
"users.blocked",
|
|
107
|
-
"users.getPrivacy",
|
|
108
|
-
"users.pins",
|
|
109
|
-
"users.followStatus",
|
|
110
|
-
"posts.list",
|
|
111
|
-
"posts.get",
|
|
112
|
-
"posts.byUser",
|
|
113
|
-
"posts.likedByUser",
|
|
114
|
-
"posts.comments",
|
|
115
|
-
"comments.replies",
|
|
116
|
-
"hashtags.posts",
|
|
117
|
-
"search.all"
|
|
243
|
+
const POST_REACTIONS = [
|
|
244
|
+
"posts.list",
|
|
245
|
+
"posts.get",
|
|
246
|
+
"posts.byUser",
|
|
247
|
+
"posts.likedByUser",
|
|
248
|
+
"posts.stats",
|
|
249
|
+
"hashtags.posts",
|
|
250
|
+
"search.all"
|
|
118
251
|
];
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
"posts.list",
|
|
128
|
-
"posts.byUser",
|
|
129
|
-
"search.all"
|
|
252
|
+
const COMMENTS = [
|
|
253
|
+
"posts.list",
|
|
254
|
+
"posts.get",
|
|
255
|
+
"posts.comments",
|
|
256
|
+
"posts.stats",
|
|
257
|
+
"comments.replies",
|
|
258
|
+
"hashtags.posts",
|
|
259
|
+
"search.all"
|
|
130
260
|
];
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
261
|
+
const PROFILE = [
|
|
262
|
+
"users.me",
|
|
263
|
+
"users.get",
|
|
264
|
+
"users.checkUsername",
|
|
265
|
+
"users.search",
|
|
266
|
+
"users.whoToFollow",
|
|
267
|
+
"users.topClans",
|
|
268
|
+
"users.followers",
|
|
269
|
+
"users.following",
|
|
270
|
+
"users.blocked",
|
|
271
|
+
"users.getPrivacy",
|
|
272
|
+
"users.pins",
|
|
273
|
+
"users.followStatus",
|
|
274
|
+
"posts.list",
|
|
275
|
+
"posts.get",
|
|
276
|
+
"posts.byUser",
|
|
277
|
+
"posts.likedByUser",
|
|
278
|
+
"posts.comments",
|
|
279
|
+
"comments.replies",
|
|
280
|
+
"hashtags.posts",
|
|
281
|
+
"search.all"
|
|
142
282
|
];
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
283
|
+
const FOLLOWING = [
|
|
284
|
+
"users.me",
|
|
285
|
+
"users.get",
|
|
286
|
+
"users.search",
|
|
287
|
+
"users.whoToFollow",
|
|
288
|
+
"users.followers",
|
|
289
|
+
"users.following",
|
|
290
|
+
"users.followStatus",
|
|
291
|
+
"posts.list",
|
|
292
|
+
"posts.byUser",
|
|
293
|
+
"search.all"
|
|
150
294
|
];
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
295
|
+
const BLOCKS = [
|
|
296
|
+
"users.me",
|
|
297
|
+
"users.get",
|
|
298
|
+
"users.search",
|
|
299
|
+
"users.whoToFollow",
|
|
300
|
+
"users.followers",
|
|
301
|
+
"users.following",
|
|
302
|
+
"users.blocked",
|
|
303
|
+
"users.followStatus",
|
|
304
|
+
"posts.list",
|
|
305
|
+
"search.all"
|
|
154
306
|
];
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
307
|
+
const PINS = [
|
|
308
|
+
"users.me",
|
|
309
|
+
"users.get",
|
|
310
|
+
"users.pins",
|
|
311
|
+
"posts.list",
|
|
312
|
+
"posts.get",
|
|
313
|
+
"posts.byUser"
|
|
158
314
|
];
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
{
|
|
406
|
-
id: "subscription.methods",
|
|
407
|
-
category: "subscription",
|
|
408
|
-
method: "GET",
|
|
409
|
-
path: /^\/api\/v1\/subscription\/methods$/
|
|
410
|
-
},
|
|
411
|
-
{
|
|
412
|
-
id: "verification.status",
|
|
413
|
-
category: "verification",
|
|
414
|
-
method: "GET",
|
|
415
|
-
path: /^\/api\/verification\/status$/
|
|
416
|
-
},
|
|
417
|
-
{
|
|
418
|
-
id: "platform.changelog",
|
|
419
|
-
category: "platform",
|
|
420
|
-
method: "GET",
|
|
421
|
-
path: /^\/api\/platform\/changelog$/
|
|
422
|
-
},
|
|
423
|
-
{
|
|
424
|
-
id: "platform.announcements",
|
|
425
|
-
category: "platform",
|
|
426
|
-
method: "GET",
|
|
427
|
-
path: /^\/api\/platform\/announcements$/
|
|
428
|
-
},
|
|
429
|
-
{ id: "platform.portal", category: "platform", method: "GET", path: /^\/api\/v1\/portal$/ },
|
|
430
|
-
{ id: "platform.status", category: "platform", method: "GET", path: /^\/api\/status$/ }
|
|
315
|
+
const NOTIFICATIONS = ["notifications.list", "notifications.count"];
|
|
316
|
+
const SUBSCRIPTION = ["subscription.status", "subscription.methods"];
|
|
317
|
+
const NOTHING = [];
|
|
318
|
+
/**
|
|
319
|
+
* Известные изменяющие запросы и читающие операции, чьи ответы они могут изменить.
|
|
320
|
+
*
|
|
321
|
+
* Каталог намеренно консервативен: лучше удалить несколько связанных списков, чем оставить
|
|
322
|
+
* персонализированное поле или вложенный объект устаревшим.
|
|
323
|
+
*/
|
|
324
|
+
const CACHE_MUTATIONS = Object.freeze([
|
|
325
|
+
{
|
|
326
|
+
operationId: "auth.refresh",
|
|
327
|
+
invalidates: NOTHING
|
|
328
|
+
},
|
|
329
|
+
{
|
|
330
|
+
operationId: "auth.resendOtp",
|
|
331
|
+
invalidates: NOTHING
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
operationId: "auth.forgotPassword",
|
|
335
|
+
invalidates: NOTHING
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
operationId: "auth.signUp",
|
|
339
|
+
invalidates: "all"
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
operationId: "auth.signIn",
|
|
343
|
+
invalidates: "all"
|
|
344
|
+
},
|
|
345
|
+
{
|
|
346
|
+
operationId: "auth.verifyOtp",
|
|
347
|
+
invalidates: "all"
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
operationId: "auth.logout",
|
|
351
|
+
invalidates: "all"
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
operationId: "auth.resetPassword",
|
|
355
|
+
invalidates: "all"
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
operationId: "auth.changePassword",
|
|
359
|
+
invalidates: "all"
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
operationId: "auth.revokeSession",
|
|
363
|
+
invalidates: ["auth.sessions"],
|
|
364
|
+
scope: "account"
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
operationId: "auth.revokeOtherSessions",
|
|
368
|
+
invalidates: ["auth.sessions"],
|
|
369
|
+
scope: "account"
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
operationId: "posts.create",
|
|
373
|
+
invalidates: POST_CONTENT
|
|
374
|
+
},
|
|
375
|
+
{
|
|
376
|
+
operationId: "posts.update",
|
|
377
|
+
invalidates: POST_CONTENT
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
operationId: "posts.remove",
|
|
381
|
+
invalidates: POST_CONTENT
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
operationId: "posts.restore",
|
|
385
|
+
invalidates: POST_CONTENT
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
operationId: "posts.like",
|
|
389
|
+
invalidates: POST_REACTIONS
|
|
390
|
+
},
|
|
391
|
+
{
|
|
392
|
+
operationId: "posts.unlike",
|
|
393
|
+
invalidates: POST_REACTIONS
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
operationId: "posts.repost",
|
|
397
|
+
invalidates: POST_CONTENT
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
operationId: "posts.unrepost",
|
|
401
|
+
invalidates: POST_CONTENT
|
|
402
|
+
},
|
|
403
|
+
{
|
|
404
|
+
operationId: "posts.pin",
|
|
405
|
+
invalidates: PINS
|
|
406
|
+
},
|
|
407
|
+
{
|
|
408
|
+
operationId: "posts.unpin",
|
|
409
|
+
invalidates: PINS
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
operationId: "posts.vote",
|
|
413
|
+
invalidates: POST_REACTIONS
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
operationId: "posts.comment",
|
|
417
|
+
invalidates: COMMENTS
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
operationId: "comments.reply",
|
|
421
|
+
invalidates: COMMENTS
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
operationId: "comments.update",
|
|
425
|
+
invalidates: COMMENTS
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
operationId: "comments.remove",
|
|
429
|
+
invalidates: COMMENTS
|
|
430
|
+
},
|
|
431
|
+
{
|
|
432
|
+
operationId: "comments.restore",
|
|
433
|
+
invalidates: COMMENTS
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
operationId: "comments.like",
|
|
437
|
+
invalidates: COMMENTS
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
operationId: "comments.unlike",
|
|
441
|
+
invalidates: COMMENTS
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
operationId: "users.updateMe",
|
|
445
|
+
invalidates: PROFILE
|
|
446
|
+
},
|
|
447
|
+
{
|
|
448
|
+
operationId: "users.deactivate",
|
|
449
|
+
invalidates: PROFILE
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
operationId: "users.restore",
|
|
453
|
+
invalidates: PROFILE
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
operationId: "users.createProfile",
|
|
457
|
+
invalidates: PROFILE
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
operationId: "users.follow",
|
|
461
|
+
invalidates: FOLLOWING
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
operationId: "users.unfollow",
|
|
465
|
+
invalidates: FOLLOWING
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
operationId: "users.block",
|
|
469
|
+
invalidates: BLOCKS
|
|
470
|
+
},
|
|
471
|
+
{
|
|
472
|
+
operationId: "users.unblock",
|
|
473
|
+
invalidates: BLOCKS
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
operationId: "users.updatePrivacy",
|
|
477
|
+
invalidates: [
|
|
478
|
+
"users.me",
|
|
479
|
+
"users.get",
|
|
480
|
+
"users.getPrivacy"
|
|
481
|
+
]
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
operationId: "users.setPin",
|
|
485
|
+
invalidates: PINS
|
|
486
|
+
},
|
|
487
|
+
{
|
|
488
|
+
operationId: "users.removePin",
|
|
489
|
+
invalidates: PINS
|
|
490
|
+
},
|
|
491
|
+
{
|
|
492
|
+
operationId: "notifications.markRead",
|
|
493
|
+
invalidates: NOTIFICATIONS,
|
|
494
|
+
scope: "account"
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
operationId: "notifications.markReadBatch",
|
|
498
|
+
invalidates: NOTIFICATIONS,
|
|
499
|
+
scope: "account"
|
|
500
|
+
},
|
|
501
|
+
{
|
|
502
|
+
operationId: "notifications.markAllRead",
|
|
503
|
+
invalidates: NOTIFICATIONS,
|
|
504
|
+
scope: "account"
|
|
505
|
+
},
|
|
506
|
+
{
|
|
507
|
+
operationId: "notifications.updateSettings",
|
|
508
|
+
invalidates: ["notifications.getSettings"],
|
|
509
|
+
scope: "account"
|
|
510
|
+
},
|
|
511
|
+
{
|
|
512
|
+
operationId: "files.upload",
|
|
513
|
+
invalidates: NOTHING
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
operationId: "files.remove",
|
|
517
|
+
invalidates: ["files.get", ...POST_CONTENT]
|
|
518
|
+
},
|
|
519
|
+
{
|
|
520
|
+
operationId: "verification.submit",
|
|
521
|
+
invalidates: ["verification.status"],
|
|
522
|
+
scope: "account"
|
|
523
|
+
},
|
|
524
|
+
{
|
|
525
|
+
operationId: "subscription.pay",
|
|
526
|
+
invalidates: SUBSCRIPTION,
|
|
527
|
+
scope: "account"
|
|
528
|
+
},
|
|
529
|
+
{
|
|
530
|
+
operationId: "subscription.setAutoRenewal",
|
|
531
|
+
invalidates: SUBSCRIPTION,
|
|
532
|
+
scope: "account"
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
operationId: "subscription.bindCard",
|
|
536
|
+
invalidates: SUBSCRIPTION,
|
|
537
|
+
scope: "account"
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
operationId: "subscription.setDefaultMethod",
|
|
541
|
+
invalidates: SUBSCRIPTION,
|
|
542
|
+
scope: "account"
|
|
543
|
+
},
|
|
544
|
+
{
|
|
545
|
+
operationId: "subscription.removeMethod",
|
|
546
|
+
invalidates: SUBSCRIPTION,
|
|
547
|
+
scope: "account"
|
|
548
|
+
},
|
|
549
|
+
{
|
|
550
|
+
operationId: "reports.create",
|
|
551
|
+
invalidates: NOTHING
|
|
552
|
+
},
|
|
553
|
+
{
|
|
554
|
+
operationId: "telemetry.dwell",
|
|
555
|
+
invalidates: NOTHING
|
|
556
|
+
},
|
|
557
|
+
{
|
|
558
|
+
operationId: "telemetry.interaction",
|
|
559
|
+
invalidates: NOTHING
|
|
560
|
+
}
|
|
431
561
|
]);
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
562
|
+
for (const mutation of CACHE_MUTATIONS) {
|
|
563
|
+
Object.freeze(mutation.invalidates);
|
|
564
|
+
Object.freeze(mutation);
|
|
435
565
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
566
|
+
const MUTATIONS = new Map(CACHE_MUTATIONS.map((mutation) => [mutation.operationId, mutation]));
|
|
567
|
+
/** Находит известную мутацию по стабильному семантическому ID. */
|
|
568
|
+
function cacheMutation(operationId) {
|
|
569
|
+
return MUTATIONS.get(operationId);
|
|
439
570
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
571
|
+
//#endregion
|
|
572
|
+
//#region src/plugin.ts
|
|
573
|
+
/** Режимы кэширования отдельного запроса. */
|
|
574
|
+
const CacheModes = Object.freeze({
|
|
575
|
+
/** Отдать свежий кэш, иначе выполнить запрос и сохранить ответ. */
|
|
576
|
+
Default: "default",
|
|
577
|
+
/** Пропустить сохранённое значение, выполнить запрос и перезаписать кэш. */
|
|
578
|
+
Reload: "reload",
|
|
579
|
+
/** Не читать и не писать кэш для этого запроса. */
|
|
580
|
+
NoStore: "no-store"
|
|
581
|
+
});
|
|
582
|
+
const DEFAULT_MAX_ENTRIES = 500;
|
|
583
|
+
const CACHE_MODES = new Set(Object.values(CacheModes));
|
|
444
584
|
function assertPositive(value, name, integer = false) {
|
|
445
|
-
|
|
446
|
-
throw new CacheError(
|
|
447
|
-
`${name} \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C ${integer ? "\u0446\u0435\u043B\u044B\u043C " : ""}\u043F\u043E\u043B\u043E\u0436\u0438\u0442\u0435\u043B\u044C\u043D\u044B\u043C \u0447\u0438\u0441\u043B\u043E\u043C, \u043F\u043E\u043B\u0443\u0447\u0435\u043D\u043E: ${value}`
|
|
448
|
-
);
|
|
449
|
-
}
|
|
585
|
+
if (!Number.isFinite(value) || value <= 0 || integer && !Number.isInteger(value)) throw new CacheError(`${name} должен быть ${integer ? "целым " : ""}положительным числом, получено: ${value}`);
|
|
450
586
|
}
|
|
451
587
|
function resolveOptions(options) {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
throw new CacheError(`cache.deduplicate \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C boolean, \u043F\u043E\u043B\u0443\u0447\u0435\u043D\u043E: ${deduplicate}`);
|
|
471
|
-
}
|
|
472
|
-
return { ttl: options.ttl, routes, maxEntries, deduplicate };
|
|
588
|
+
if (!options || typeof options !== "object") throw new CacheError("cache() принимает объект настроек");
|
|
589
|
+
assertPositive(options.ttl, "cache.ttl");
|
|
590
|
+
if (!Array.isArray(options.operations) || options.operations.length === 0) throw new CacheError("cache.operations должен содержать хотя бы одну операцию");
|
|
591
|
+
const operations = /* @__PURE__ */ new Set();
|
|
592
|
+
for (const operation of options.operations) {
|
|
593
|
+
if (typeof operation !== "string" || !isCacheOperationId(operation)) throw new CacheError(`Неизвестная операция кэша: ${JSON.stringify(operation)}`);
|
|
594
|
+
operations.add(operation);
|
|
595
|
+
}
|
|
596
|
+
const maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES;
|
|
597
|
+
assertPositive(maxEntries, "cache.maxEntries", true);
|
|
598
|
+
const deduplicate = options.deduplicate ?? true;
|
|
599
|
+
if (typeof deduplicate !== "boolean") throw new CacheError(`cache.deduplicate должен быть boolean, получено: ${deduplicate}`);
|
|
600
|
+
return {
|
|
601
|
+
ttl: options.ttl,
|
|
602
|
+
operations,
|
|
603
|
+
maxEntries,
|
|
604
|
+
deduplicate
|
|
605
|
+
};
|
|
473
606
|
}
|
|
474
607
|
function cloneValue(value) {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
608
|
+
try {
|
|
609
|
+
return {
|
|
610
|
+
cacheable: true,
|
|
611
|
+
value: structuredClone(value)
|
|
612
|
+
};
|
|
613
|
+
} catch {
|
|
614
|
+
return {
|
|
615
|
+
cacheable: false,
|
|
616
|
+
value
|
|
617
|
+
};
|
|
618
|
+
}
|
|
480
619
|
}
|
|
481
620
|
function cacheMode(request) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
`cache \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C 'default', 'reload' \u0438\u043B\u0438 'no-store', \u043F\u043E\u043B\u0443\u0447\u0435\u043D\u043E: ${String(mode)}`
|
|
486
|
-
);
|
|
487
|
-
}
|
|
488
|
-
return mode;
|
|
621
|
+
const mode = request.extensions?.cache ?? CacheModes.Default;
|
|
622
|
+
if (!CACHE_MODES.has(mode)) throw new CacheError(`cache должен быть '${CacheModes.Default}', '${CacheModes.Reload}' или '${CacheModes.NoStore}', получено: ${String(mode)}`);
|
|
623
|
+
return mode;
|
|
489
624
|
}
|
|
625
|
+
/** Создаёт TTL/LRU-кэш разобранных ответов itd-api. */
|
|
490
626
|
function cache(options) {
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
627
|
+
const config = resolveOptions(options);
|
|
628
|
+
const values = new LRUCache({
|
|
629
|
+
max: config.maxEntries,
|
|
630
|
+
ttl: config.ttl,
|
|
631
|
+
updateAgeOnGet: false,
|
|
632
|
+
allowStale: false
|
|
633
|
+
});
|
|
634
|
+
const pending = /* @__PURE__ */ new Map();
|
|
635
|
+
const operationGenerations = /* @__PURE__ */ new Map();
|
|
636
|
+
const scopeGenerations = /* @__PURE__ */ new Map();
|
|
637
|
+
const scopeOperationGenerations = /* @__PURE__ */ new Map();
|
|
638
|
+
const keyStates = /* @__PURE__ */ new Map();
|
|
639
|
+
let generation = 0;
|
|
640
|
+
let installationSequence = 0;
|
|
641
|
+
const scopeOperationKey = (accountScope, operation) => JSON.stringify([accountScope, operation]);
|
|
642
|
+
const clear = () => {
|
|
643
|
+
generation += 1;
|
|
644
|
+
values.clear();
|
|
645
|
+
pending.clear();
|
|
646
|
+
};
|
|
647
|
+
const invalidate = (...operations) => {
|
|
648
|
+
if (operations.length === 0) return;
|
|
649
|
+
const selected = /* @__PURE__ */ new Set();
|
|
650
|
+
for (const operation of operations) {
|
|
651
|
+
if (!isCacheOperationId(operation)) throw new CacheError(`Неизвестная операция кэша: ${JSON.stringify(operation)}`);
|
|
652
|
+
selected.add(operation);
|
|
653
|
+
operationGenerations.set(operation, (operationGenerations.get(operation) ?? 0) + 1);
|
|
654
|
+
}
|
|
655
|
+
for (const [key, entry] of values.entries()) if (selected.has(entry.operation)) values.delete(key);
|
|
656
|
+
for (const [key, entry] of pending) if (selected.has(entry.operation)) pending.delete(key);
|
|
657
|
+
};
|
|
658
|
+
const clearScope = (accountScope) => {
|
|
659
|
+
scopeGenerations.set(accountScope, (scopeGenerations.get(accountScope) ?? 0) + 1);
|
|
660
|
+
for (const [key, entry] of values.entries()) if (entry.accountScope === accountScope) values.delete(key);
|
|
661
|
+
for (const [key, entry] of pending) if (entry.accountScope === accountScope) pending.delete(key);
|
|
662
|
+
};
|
|
663
|
+
const invalidateScope = (accountScope, operations) => {
|
|
664
|
+
if (operations.length === 0) return;
|
|
665
|
+
const selected = new Set(operations);
|
|
666
|
+
for (const operation of selected) {
|
|
667
|
+
const key = scopeOperationKey(accountScope, operation);
|
|
668
|
+
scopeOperationGenerations.set(key, (scopeOperationGenerations.get(key) ?? 0) + 1);
|
|
669
|
+
}
|
|
670
|
+
for (const [key, entry] of values.entries()) if (entry.accountScope === accountScope && selected.has(entry.operation)) values.delete(key);
|
|
671
|
+
for (const [key, entry] of pending) if (entry.accountScope === accountScope && selected.has(entry.operation)) pending.delete(key);
|
|
672
|
+
};
|
|
673
|
+
const applyMutation = (accountScope, mutation) => {
|
|
674
|
+
if (mutation.invalidates === "all") clearScope(accountScope);
|
|
675
|
+
else if (mutation.scope === "account") invalidateScope(accountScope, mutation.invalidates);
|
|
676
|
+
else invalidate(...mutation.invalidates);
|
|
677
|
+
};
|
|
678
|
+
const createTransformer = (installation, baseUrl, getAuthIdentity, getAuthScope) => {
|
|
679
|
+
const fallbackAuthScope = JSON.stringify([baseUrl, `installation:${installation}`]);
|
|
680
|
+
const resolveIdentity = async () => {
|
|
681
|
+
const identity = await getAuthIdentity?.();
|
|
682
|
+
const accountScope = identity?.userId ? JSON.stringify([baseUrl, identity.userId]) : getAuthScope ? JSON.stringify([baseUrl, getAuthScope()]) : fallbackAuthScope;
|
|
683
|
+
return {
|
|
684
|
+
accountScope,
|
|
685
|
+
sessionScope: identity?.userId && identity.sessionId ? JSON.stringify([
|
|
686
|
+
baseUrl,
|
|
687
|
+
identity.userId,
|
|
688
|
+
identity.sessionId
|
|
689
|
+
]) : JSON.stringify([accountScope, getAuthScope ? getAuthScope() : `installation:${installation}`])
|
|
690
|
+
};
|
|
691
|
+
};
|
|
692
|
+
return async (request, next) => {
|
|
693
|
+
const operation = cacheOperation(request.operationId);
|
|
694
|
+
const method = request.method.toUpperCase();
|
|
695
|
+
if (!(operation !== void 0 || method === "GET" || method === "HEAD")) {
|
|
696
|
+
const mutation = cacheMutation(request.operationId);
|
|
697
|
+
const startedIdentity = mutation ? await resolveIdentity() : void 0;
|
|
698
|
+
const result = await next(request);
|
|
699
|
+
if (mutation && startedIdentity) {
|
|
700
|
+
applyMutation(startedIdentity.accountScope, mutation);
|
|
701
|
+
const currentIdentity = await resolveIdentity();
|
|
702
|
+
if (currentIdentity.accountScope !== startedIdentity.accountScope && (mutation.invalidates === "all" || mutation.scope === "account")) applyMutation(currentIdentity.accountScope, mutation);
|
|
703
|
+
} else clear();
|
|
704
|
+
return result;
|
|
705
|
+
}
|
|
706
|
+
if (!operation || !config.operations.has(operation.id)) return next(request);
|
|
707
|
+
const mode = cacheMode(request);
|
|
708
|
+
if (mode === CacheModes.NoStore) return next(request);
|
|
709
|
+
const unscopedKey = buildCacheKey(operation.id, request);
|
|
710
|
+
if (unscopedKey === void 0) return next(request);
|
|
711
|
+
const identity = await resolveIdentity();
|
|
712
|
+
const scope = operation.id === "auth.sessions" ? identity.sessionScope : identity.accountScope;
|
|
713
|
+
const key = JSON.stringify([scope, unscopedKey]);
|
|
714
|
+
if (mode === CacheModes.Reload) {
|
|
715
|
+
const state = keyStates.get(key) ?? {
|
|
716
|
+
active: 0,
|
|
717
|
+
generation: 0
|
|
718
|
+
};
|
|
719
|
+
state.generation += 1;
|
|
720
|
+
keyStates.set(key, state);
|
|
721
|
+
values.delete(key);
|
|
722
|
+
pending.delete(key);
|
|
723
|
+
}
|
|
724
|
+
if (mode === CacheModes.Default) {
|
|
725
|
+
const hit = values.get(key);
|
|
726
|
+
if (hit) {
|
|
727
|
+
const cloned = cloneValue(hit.value);
|
|
728
|
+
if (cloned.cacheable) return cloned.value;
|
|
729
|
+
values.delete(key);
|
|
730
|
+
}
|
|
731
|
+
const existing = pending.get(key);
|
|
732
|
+
if (config.deduplicate && request.signal === void 0 && request.timeout === void 0 && existing) return cloneValue((await existing.promise).value).value;
|
|
733
|
+
}
|
|
734
|
+
const startedGeneration = generation;
|
|
735
|
+
const startedScopeGeneration = scopeGenerations.get(identity.accountScope) ?? 0;
|
|
736
|
+
const startedOperationGeneration = operationGenerations.get(operation.id) ?? 0;
|
|
737
|
+
const scopedOperationKey = scopeOperationKey(identity.accountScope, operation.id);
|
|
738
|
+
const startedScopeOperationGeneration = scopeOperationGenerations.get(scopedOperationKey) ?? 0;
|
|
739
|
+
const keyState = keyStates.get(key) ?? {
|
|
740
|
+
active: 0,
|
|
741
|
+
generation: 0
|
|
742
|
+
};
|
|
743
|
+
keyState.active += 1;
|
|
744
|
+
keyStates.set(key, keyState);
|
|
745
|
+
const startedKeyGeneration = keyState.generation;
|
|
746
|
+
const load = (async () => {
|
|
747
|
+
try {
|
|
748
|
+
const result = await next(request);
|
|
749
|
+
const stored = cloneValue(result);
|
|
750
|
+
const currentIdentity = await resolveIdentity();
|
|
751
|
+
const currentScope = operation.id === "auth.sessions" ? currentIdentity.sessionScope : currentIdentity.accountScope;
|
|
752
|
+
if (stored.cacheable && currentScope === scope && generation === startedGeneration && (scopeGenerations.get(identity.accountScope) ?? 0) === startedScopeGeneration && (operationGenerations.get(operation.id) ?? 0) === startedOperationGeneration && (scopeOperationGenerations.get(scopedOperationKey) ?? 0) === startedScopeOperationGeneration && keyState.generation === startedKeyGeneration) values.set(key, {
|
|
753
|
+
accountScope: identity.accountScope,
|
|
754
|
+
operation: operation.id,
|
|
755
|
+
value: stored.value
|
|
756
|
+
});
|
|
757
|
+
return {
|
|
758
|
+
cacheable: stored.cacheable,
|
|
759
|
+
value: result
|
|
760
|
+
};
|
|
761
|
+
} finally {
|
|
762
|
+
keyState.active -= 1;
|
|
763
|
+
if (keyState.active === 0 && keyStates.get(key) === keyState) keyStates.delete(key);
|
|
764
|
+
}
|
|
765
|
+
})();
|
|
766
|
+
const mayDeduplicate = mode === CacheModes.Default && config.deduplicate && request.signal === void 0 && request.timeout === void 0;
|
|
767
|
+
const entry = {
|
|
768
|
+
accountScope: identity.accountScope,
|
|
769
|
+
operation: operation.id,
|
|
770
|
+
promise: load
|
|
771
|
+
};
|
|
772
|
+
if (mayDeduplicate) pending.set(key, entry);
|
|
773
|
+
try {
|
|
774
|
+
return (await load).value;
|
|
775
|
+
} finally {
|
|
776
|
+
if (pending.get(key) === entry) pending.delete(key);
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
};
|
|
780
|
+
return {
|
|
781
|
+
name: "cache",
|
|
782
|
+
get size() {
|
|
783
|
+
values.purgeStale();
|
|
784
|
+
return values.size;
|
|
785
|
+
},
|
|
786
|
+
clear,
|
|
787
|
+
invalidate,
|
|
788
|
+
attachRealtime(stream) {
|
|
789
|
+
if (!stream || typeof stream.on !== "function") throw new CacheError("attachRealtime() принимает поток из itd.realtime()");
|
|
790
|
+
const invalidateStream = (...operations) => {
|
|
791
|
+
const identity = typeof stream.getAuthIdentity === "function" ? stream.getAuthIdentity() : void 0;
|
|
792
|
+
const streamBaseUrl = typeof stream.baseUrl === "string" && stream.baseUrl.length > 0 ? stream.baseUrl : void 0;
|
|
793
|
+
const legacyScope = typeof stream.getAuthScope === "function" ? stream.getAuthScope() : void 0;
|
|
794
|
+
const accountScope = identity?.userId && streamBaseUrl ? JSON.stringify([streamBaseUrl, identity.userId]) : legacyScope !== void 0 && streamBaseUrl ? JSON.stringify([streamBaseUrl, legacyScope]) : void 0;
|
|
795
|
+
if (accountScope === void 0) invalidate(...operations);
|
|
796
|
+
else invalidateScope(accountScope, operations);
|
|
797
|
+
};
|
|
798
|
+
invalidateStream("notifications.list", "notifications.count");
|
|
799
|
+
const offNotification = stream.on("notification", () => invalidateStream("notifications.list", "notifications.count"));
|
|
800
|
+
const offUnreadCount = stream.on("unreadCount", () => invalidateStream("notifications.count"));
|
|
801
|
+
return () => {
|
|
802
|
+
offNotification();
|
|
803
|
+
offUnreadCount();
|
|
804
|
+
};
|
|
805
|
+
},
|
|
806
|
+
install({ operations, baseUrl, getAuthIdentity, getAuthScope }) {
|
|
807
|
+
installationSequence += 1;
|
|
808
|
+
operations.use(createTransformer(installationSequence, baseUrl, getAuthIdentity, getAuthScope));
|
|
809
|
+
}
|
|
810
|
+
};
|
|
672
811
|
}
|
|
812
|
+
//#endregion
|
|
813
|
+
export { CACHE_OPERATIONS, CacheError, CacheModes, buildCacheKey, cache, cacheOperation, isCacheOperationId };
|
|
673
814
|
|
|
674
|
-
export { CACHE_ROUTES, CacheError, buildCacheKey, cache, cacheRoute, isCacheRouteId };
|
|
675
|
-
//# sourceMappingURL=index.js.map
|
|
676
815
|
//# sourceMappingURL=index.js.map
|