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