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