@contello/astro 3.0.0 → 3.2.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/dist/index.cjs +304 -414
- package/dist/index.d.cts +89 -86
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +89 -86
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +301 -377
- package/dist/index.js.map +1 -0
- package/package.json +15 -12
package/dist/index.js
CHANGED
|
@@ -1,400 +1,324 @@
|
|
|
1
|
-
|
|
2
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
2
|
import { i18n } from "@astroscope/i18n";
|
|
4
3
|
import { createMediaResolver } from "@contello/media";
|
|
5
|
-
import {
|
|
6
|
-
createStore
|
|
7
|
-
} from "@contello/store";
|
|
8
|
-
|
|
9
|
-
// src/assets-middleware.ts
|
|
4
|
+
import { createStore, maybeThen } from "@contello/store";
|
|
10
5
|
import { defineMiddleware } from "astro/middleware";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
};
|
|
19
|
-
var onStart = dc.channel(channels.start);
|
|
20
|
-
var onEnd = dc.channel(channels.end);
|
|
21
|
-
var onError = dc.channel(channels.error);
|
|
22
|
-
function hasSubscribers() {
|
|
23
|
-
return onStart.hasSubscribers || onEnd.hasSubscribers || onError.hasSubscribers;
|
|
24
|
-
}
|
|
25
|
-
function wrap(name, fn) {
|
|
26
|
-
if (!hasSubscribers()) {
|
|
27
|
-
return fn();
|
|
28
|
-
}
|
|
29
|
-
const start = performance.now();
|
|
30
|
-
onStart.publish({ name });
|
|
31
|
-
let result;
|
|
32
|
-
try {
|
|
33
|
-
result = fn();
|
|
34
|
-
} catch (error) {
|
|
35
|
-
onError.publish({ name, error, durationMs: performance.now() - start });
|
|
36
|
-
throw error;
|
|
37
|
-
}
|
|
38
|
-
if (result instanceof Promise) {
|
|
39
|
-
return result.then(
|
|
40
|
-
(r) => {
|
|
41
|
-
onEnd.publish({ name, durationMs: performance.now() - start });
|
|
42
|
-
return r;
|
|
43
|
-
},
|
|
44
|
-
(error) => {
|
|
45
|
-
onError.publish({ name, error, durationMs: performance.now() - start });
|
|
46
|
-
throw error;
|
|
47
|
-
}
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
onEnd.publish({ name, durationMs: performance.now() - start });
|
|
51
|
-
return result;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// src/assets-middleware.ts
|
|
55
|
-
var DEFAULT_IMAGES_CACHE_CONTROL = "public, max-age=31536000";
|
|
6
|
+
import { createOperationTelemetry } from "@contello/opentelemetry";
|
|
7
|
+
import { RECOMMENDED_EXCLUDES, shouldExclude } from "@astroscope/excludes";
|
|
8
|
+
//#region src/telemetry.ts
|
|
9
|
+
const { wrap } = createOperationTelemetry("@contello/astro");
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/assets-middleware.ts
|
|
12
|
+
const DEFAULT_IMAGES_CACHE_CONTROL = "public, max-age=31536000";
|
|
56
13
|
function createBoundAssetsMiddleware(contello, options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
if (isFile) {
|
|
78
|
-
return handleFile(contello.client, next, pathname.slice(filesPrefix.length), filesCacheControl);
|
|
79
|
-
}
|
|
80
|
-
return handleVideo(contello.client, next, pathname.slice(videoPrefix.length), ctx.request.signal);
|
|
81
|
-
});
|
|
14
|
+
const imagesPrefix = contello.media.imagesPath;
|
|
15
|
+
const imagesCacheControl = options?.images?.cacheControl ?? DEFAULT_IMAGES_CACHE_CONTROL;
|
|
16
|
+
const filesPrefix = contello.media.filesPath;
|
|
17
|
+
const filesCacheControl = options?.files?.cacheControl;
|
|
18
|
+
const videoPrefix = contello.media.videosPath;
|
|
19
|
+
return defineMiddleware((ctx, next) => {
|
|
20
|
+
const { pathname } = ctx.url;
|
|
21
|
+
const isImage = pathname.startsWith(imagesPrefix);
|
|
22
|
+
const isFile = !isImage && pathname.startsWith(filesPrefix);
|
|
23
|
+
const isVideo = !isImage && !isFile && pathname.startsWith(videoPrefix);
|
|
24
|
+
if (!isImage && !isFile && !isVideo) return next();
|
|
25
|
+
if (!contello.isReady) {
|
|
26
|
+
console.warn(`[@contello/astro] not initialized, passing through: ${pathname}`);
|
|
27
|
+
return next();
|
|
28
|
+
}
|
|
29
|
+
if (isImage) return handleFile(contello.client, next, pathname.slice(imagesPrefix.length), imagesCacheControl);
|
|
30
|
+
if (isFile) return handleFile(contello.client, next, pathname.slice(filesPrefix.length), filesCacheControl);
|
|
31
|
+
return handleVideo(contello.client, next, pathname.slice(videoPrefix.length), ctx.request.signal);
|
|
32
|
+
});
|
|
82
33
|
}
|
|
83
34
|
function handleFile(client, next, rest, cacheControl) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return new Response(result.stream(), { headers });
|
|
99
|
-
},
|
|
100
|
-
() => new Response(null, { status: 404 })
|
|
101
|
-
);
|
|
35
|
+
const dot = rest.indexOf(".");
|
|
36
|
+
const fileId = dot === -1 ? rest : rest.slice(0, dot);
|
|
37
|
+
if (!fileId) return next();
|
|
38
|
+
return (async () => {
|
|
39
|
+
try {
|
|
40
|
+
const result = await wrap("assets:file", () => client.download(fileId));
|
|
41
|
+
const headers = new Headers({ "content-type": result.mimeType });
|
|
42
|
+
if (cacheControl) headers.set("cache-control", cacheControl);
|
|
43
|
+
if (result.size > 0) headers.set("content-length", String(result.size));
|
|
44
|
+
return new Response(result.stream(), { headers });
|
|
45
|
+
} catch {
|
|
46
|
+
return new Response(null, { status: 404 });
|
|
47
|
+
}
|
|
48
|
+
})();
|
|
102
49
|
}
|
|
103
50
|
function handleVideo(client, next, path, signal) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
51
|
+
if (!path) return next();
|
|
52
|
+
return (async () => {
|
|
53
|
+
try {
|
|
54
|
+
const result = await wrap("assets:hls", () => client.proxyHls(path, signal));
|
|
55
|
+
return new Response(result.stream(), {
|
|
56
|
+
status: result.status,
|
|
57
|
+
headers: result.headers
|
|
58
|
+
});
|
|
59
|
+
} catch {
|
|
60
|
+
return new Response("Upstream is down", {
|
|
61
|
+
status: 502,
|
|
62
|
+
headers: { "content-type": "text/plain" }
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
})();
|
|
111
66
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
import { RECOMMENDED_EXCLUDES, shouldExclude } from "@astroscope/excludes";
|
|
115
|
-
import {
|
|
116
|
-
maybeThen
|
|
117
|
-
} from "@contello/store";
|
|
118
|
-
import { defineMiddleware as defineMiddleware2 } from "astro/middleware";
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/routing-middleware.ts
|
|
119
69
|
function customHeaders(headers) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
return result;
|
|
70
|
+
const result = {};
|
|
71
|
+
for (const h of headers) result[h.name] = h.value;
|
|
72
|
+
return result;
|
|
125
73
|
}
|
|
126
|
-
|
|
74
|
+
const VALID_REDIRECT_STATUSES = /* @__PURE__ */ new Set([
|
|
75
|
+
301,
|
|
76
|
+
302,
|
|
77
|
+
303,
|
|
78
|
+
307,
|
|
79
|
+
308
|
|
80
|
+
]);
|
|
127
81
|
function toRedirectStatus(status) {
|
|
128
|
-
|
|
82
|
+
return VALID_REDIRECT_STATUSES.has(status) ? status : 302;
|
|
129
83
|
}
|
|
130
84
|
function createBoundRoutingMiddleware(contello, routes, exclude, resolveRoutePath) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
case "entity": {
|
|
198
|
-
return contello[runRequest](
|
|
199
|
-
{ url, route, rewritten: true },
|
|
200
|
-
() => wrap(`route:entity:${route.model}`, async () => {
|
|
201
|
-
const response = await next(`/contello/entities/${route.model}/${route.entityId}${url.search}`);
|
|
202
|
-
for (const { name, value } of route.customHeaders) {
|
|
203
|
-
response.headers.set(name, value);
|
|
204
|
-
}
|
|
205
|
-
return response;
|
|
206
|
-
})
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
});
|
|
85
|
+
return defineMiddleware((ctx, next) => {
|
|
86
|
+
if (shouldExclude(ctx, exclude ?? RECOMMENDED_EXCLUDES)) return next();
|
|
87
|
+
if (!contello.isReady) {
|
|
88
|
+
console.warn(`[@contello/astro] not initialized, passing through: ${ctx.url.pathname}`);
|
|
89
|
+
return next();
|
|
90
|
+
}
|
|
91
|
+
const { url } = ctx;
|
|
92
|
+
if (url.pathname.startsWith("/contello/entities/")) return contello[runRequest]({
|
|
93
|
+
url,
|
|
94
|
+
route: void 0,
|
|
95
|
+
rewritten: false
|
|
96
|
+
}, () => next());
|
|
97
|
+
const path = resolveRoutePath ? resolveRoutePath(ctx) : url.pathname;
|
|
98
|
+
return maybeThen(routes.getByPath(path), (route) => {
|
|
99
|
+
if (!route) return contello[runRequest]({
|
|
100
|
+
url,
|
|
101
|
+
route: void 0,
|
|
102
|
+
rewritten: false
|
|
103
|
+
}, () => next());
|
|
104
|
+
switch (route.type) {
|
|
105
|
+
case "redirect": return contello[runRequest]({
|
|
106
|
+
url,
|
|
107
|
+
route,
|
|
108
|
+
rewritten: false
|
|
109
|
+
}, () => wrap("route:redirect", () => new Response(null, {
|
|
110
|
+
status: toRedirectStatus(route.status),
|
|
111
|
+
headers: {
|
|
112
|
+
Location: route.location,
|
|
113
|
+
...customHeaders(route.customHeaders)
|
|
114
|
+
}
|
|
115
|
+
})));
|
|
116
|
+
case "text": return contello[runRequest]({
|
|
117
|
+
url,
|
|
118
|
+
route,
|
|
119
|
+
rewritten: false
|
|
120
|
+
}, () => wrap("route:text", () => new Response(route.content, {
|
|
121
|
+
status: 200,
|
|
122
|
+
headers: {
|
|
123
|
+
"Content-Type": route.mimeType,
|
|
124
|
+
...customHeaders(route.customHeaders)
|
|
125
|
+
}
|
|
126
|
+
})));
|
|
127
|
+
case "asset": return contello[runRequest]({
|
|
128
|
+
url,
|
|
129
|
+
route,
|
|
130
|
+
rewritten: false
|
|
131
|
+
}, () => wrap("route:asset", async () => {
|
|
132
|
+
const result = await contello.client.download(route.fileId);
|
|
133
|
+
const headers = new Headers({ "content-type": route.mimeType || result.mimeType });
|
|
134
|
+
headers.set("content-disposition", route.contentDisposition);
|
|
135
|
+
if (result.size > 0) headers.set("content-length", String(result.size));
|
|
136
|
+
for (const { name, value } of route.customHeaders) headers.set(name, value);
|
|
137
|
+
return new Response(result.stream(), { headers });
|
|
138
|
+
}));
|
|
139
|
+
case "entity": return contello[runRequest]({
|
|
140
|
+
url,
|
|
141
|
+
route,
|
|
142
|
+
rewritten: true
|
|
143
|
+
}, () => wrap(`route:entity:${route.model}`, async () => {
|
|
144
|
+
const response = await next(`/contello/entities/${route.model}/${route.entityId}${url.search}`);
|
|
145
|
+
for (const { name, value } of route.customHeaders) response.headers.set(name, value);
|
|
146
|
+
return response;
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
});
|
|
212
151
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/contello.ts
|
|
154
|
+
const DEFAULT_IMAGES_PREFIX = "/_contello/i/";
|
|
155
|
+
const DEFAULT_FILES_PREFIX = "/_contello/f/";
|
|
156
|
+
const DEFAULT_VIDEO_PREFIX = "/_contello/v/";
|
|
157
|
+
const runRequest = Symbol("@contello/astro/runRequest");
|
|
219
158
|
function buildI18nRegistrations() {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
159
|
+
const manifest = i18n.getManifest();
|
|
160
|
+
const defaultLanguage = i18n.getConfig().defaultLocale;
|
|
161
|
+
return manifest.keys.map((extracted) => ({
|
|
162
|
+
token: extracted.key,
|
|
163
|
+
example: extracted.meta.fallback,
|
|
164
|
+
description: extracted.meta.description ?? "",
|
|
165
|
+
variables: Object.entries(extracted.meta.variables ?? {}).map(([name, def]) => ({
|
|
166
|
+
name,
|
|
167
|
+
description: def?.description ?? "",
|
|
168
|
+
example: def?.fallback ?? ""
|
|
169
|
+
})),
|
|
170
|
+
initialTranslations: [{
|
|
171
|
+
language: defaultLanguage,
|
|
172
|
+
message: extracted.meta.fallback
|
|
173
|
+
}]
|
|
174
|
+
}));
|
|
233
175
|
}
|
|
234
176
|
async function applyTranslations(messages) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
if (raw) {
|
|
245
|
-
raw[msg.token] = value;
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
for (const [locale, translations] of byLocale.entries()) {
|
|
250
|
-
i18n.setTranslations(locale, translations);
|
|
251
|
-
}
|
|
177
|
+
const config = i18n.getConfig();
|
|
178
|
+
const all = await messages.getAll();
|
|
179
|
+
const byLocale = /* @__PURE__ */ new Map();
|
|
180
|
+
for (const locale of config.locales) byLocale.set(locale, {});
|
|
181
|
+
for (const msg of all) msg.translations.forEach((value, language) => {
|
|
182
|
+
const raw = byLocale.get(language);
|
|
183
|
+
if (raw) raw[msg.token] = value;
|
|
184
|
+
});
|
|
185
|
+
for (const [locale, translations] of byLocale) i18n.setTranslations(locale, translations);
|
|
252
186
|
}
|
|
253
187
|
var Contello = class {
|
|
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
|
-
const routes = options?.routes ?? (this._autoLazyRoutes ??= this.defineLazyRoutes());
|
|
384
|
-
return createBoundRoutingMiddleware(this, routes, options?.exclude, options?.resolveRoutePath);
|
|
385
|
-
};
|
|
386
|
-
createAssetsMiddleware = (options) => {
|
|
387
|
-
return createBoundAssetsMiddleware(this, options);
|
|
388
|
-
};
|
|
389
|
-
// --- ALS run ---
|
|
390
|
-
[runRequest](ctx, fn) {
|
|
391
|
-
return this._als.run(ctx, fn);
|
|
392
|
-
}
|
|
188
|
+
_store;
|
|
189
|
+
_options;
|
|
190
|
+
_i18nMessages;
|
|
191
|
+
_i18nUnsubscribe;
|
|
192
|
+
_initialized = false;
|
|
193
|
+
_als = new AsyncLocalStorage();
|
|
194
|
+
_autoLazyRoutes;
|
|
195
|
+
media;
|
|
196
|
+
createRoutingMiddleware = (options) => {
|
|
197
|
+
const routes = options?.routes ?? (this._autoLazyRoutes ??= this.defineLazyRoutes());
|
|
198
|
+
return createBoundRoutingMiddleware(this, routes, options?.exclude, options?.resolveRoutePath);
|
|
199
|
+
};
|
|
200
|
+
createAssetsMiddleware = (options) => {
|
|
201
|
+
return createBoundAssetsMiddleware(this, options);
|
|
202
|
+
};
|
|
203
|
+
constructor(options) {
|
|
204
|
+
this._options = options;
|
|
205
|
+
this._store = createStore(options);
|
|
206
|
+
this.media = {
|
|
207
|
+
baseUrl: options.media?.baseUrl ?? "",
|
|
208
|
+
imagesPath: options.media?.imagesPath ?? DEFAULT_IMAGES_PREFIX,
|
|
209
|
+
videosPath: options.media?.videosPath ?? DEFAULT_VIDEO_PREFIX,
|
|
210
|
+
filesPath: options.media?.filesPath ?? DEFAULT_FILES_PREFIX
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
async init(options) {
|
|
214
|
+
try {
|
|
215
|
+
await this._store.init();
|
|
216
|
+
if (this._options.i18n) {
|
|
217
|
+
const { collection, languages, cache } = this._options.i18n;
|
|
218
|
+
const { register = true, load = true } = options?.i18n ?? {};
|
|
219
|
+
this._i18nMessages = this._store.defineI18nMessages({
|
|
220
|
+
collection,
|
|
221
|
+
cache
|
|
222
|
+
});
|
|
223
|
+
await i18n.configure({ locales: languages });
|
|
224
|
+
if (register) {
|
|
225
|
+
const registrations = buildI18nRegistrations();
|
|
226
|
+
if (registrations.length > 0) await this._i18nMessages.register(registrations);
|
|
227
|
+
}
|
|
228
|
+
if (load) {
|
|
229
|
+
await applyTranslations(this._i18nMessages);
|
|
230
|
+
const messages = this._i18nMessages;
|
|
231
|
+
const controller = new AbortController();
|
|
232
|
+
this._i18nUnsubscribe = () => controller.abort();
|
|
233
|
+
(async () => {
|
|
234
|
+
for await (const _ of messages.refresh$) {
|
|
235
|
+
if (controller.signal.aborted) return;
|
|
236
|
+
await applyTranslations(messages);
|
|
237
|
+
}
|
|
238
|
+
})();
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
await Promise.all((options?.load ?? []).map((l) => l.load()));
|
|
242
|
+
this._initialized = true;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
try {
|
|
245
|
+
await this.destroy();
|
|
246
|
+
} catch {}
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async destroy() {
|
|
251
|
+
this._initialized = false;
|
|
252
|
+
this._i18nUnsubscribe?.();
|
|
253
|
+
this._i18nUnsubscribe = void 0;
|
|
254
|
+
await this._store.destroy();
|
|
255
|
+
this._i18nMessages = void 0;
|
|
256
|
+
}
|
|
257
|
+
ping() {
|
|
258
|
+
return this._store.ping();
|
|
259
|
+
}
|
|
260
|
+
get isReady() {
|
|
261
|
+
return this._initialized;
|
|
262
|
+
}
|
|
263
|
+
get client() {
|
|
264
|
+
return this._store.client;
|
|
265
|
+
}
|
|
266
|
+
get i18nMessages() {
|
|
267
|
+
if (!this._i18nMessages) throw new Error("@contello/astro: .i18nMessages accessed before init() with i18n config");
|
|
268
|
+
return this._i18nMessages;
|
|
269
|
+
}
|
|
270
|
+
get request() {
|
|
271
|
+
const ctx = this._als.getStore();
|
|
272
|
+
if (!ctx) throw new Error("@contello/astro: .request accessed outside of request context");
|
|
273
|
+
return ctx;
|
|
274
|
+
}
|
|
275
|
+
defineMediaResolver(options) {
|
|
276
|
+
return createMediaResolver({
|
|
277
|
+
...this.media,
|
|
278
|
+
...options
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
defineSingleton(sourceOrKey, options) {
|
|
282
|
+
return this._store.defineSingleton(sourceOrKey, options);
|
|
283
|
+
}
|
|
284
|
+
defineSingletonSync(sourceOrKey, options) {
|
|
285
|
+
return this._store.defineSingletonSync(sourceOrKey, options);
|
|
286
|
+
}
|
|
287
|
+
defineCollection(sourceOrKey, options) {
|
|
288
|
+
return this._store.defineCollection(sourceOrKey, options);
|
|
289
|
+
}
|
|
290
|
+
defineCollectionSync(sourceOrKey, options) {
|
|
291
|
+
return this._store.defineCollectionSync(sourceOrKey, options);
|
|
292
|
+
}
|
|
293
|
+
defineLazyCollection(sourceOrKey, options) {
|
|
294
|
+
return this._store.defineLazyCollection(sourceOrKey, options);
|
|
295
|
+
}
|
|
296
|
+
defineRoutes(options) {
|
|
297
|
+
return this._store.defineRoutes(options);
|
|
298
|
+
}
|
|
299
|
+
defineRoutesSync(options) {
|
|
300
|
+
return this._store.defineRoutesSync(options);
|
|
301
|
+
}
|
|
302
|
+
defineLazyRoutes(options) {
|
|
303
|
+
return this._store.defineLazyRoutes(options);
|
|
304
|
+
}
|
|
305
|
+
defineAssets(options) {
|
|
306
|
+
return this._store.defineAssets(options);
|
|
307
|
+
}
|
|
308
|
+
defineAssetsSync(options) {
|
|
309
|
+
return this._store.defineAssetsSync(options);
|
|
310
|
+
}
|
|
311
|
+
defineLazyAssets(options) {
|
|
312
|
+
return this._store.defineLazyAssets(options);
|
|
313
|
+
}
|
|
314
|
+
[runRequest](ctx, fn) {
|
|
315
|
+
return this._als.run(ctx, fn);
|
|
316
|
+
}
|
|
393
317
|
};
|
|
394
318
|
function createContello(options) {
|
|
395
|
-
|
|
319
|
+
return new Contello(options);
|
|
396
320
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
321
|
+
//#endregion
|
|
322
|
+
export { Contello, createContello };
|
|
323
|
+
|
|
324
|
+
//# sourceMappingURL=index.js.map
|