@contello/astro 1.1.3 → 1.3.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 +82 -5
- package/dist/index.d.cts +38 -4
- package/dist/index.d.ts +38 -4
- package/dist/index.js +81 -5
- package/package.json +6 -5
package/dist/index.cjs
CHANGED
|
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
Contello: () => Contello,
|
|
34
|
+
createAssetsMiddleware: () => createAssetsMiddleware,
|
|
34
35
|
createContello: () => createContello,
|
|
35
36
|
createRoutingMiddleware: () => createRoutingMiddleware
|
|
36
37
|
});
|
|
@@ -40,6 +41,9 @@ module.exports = __toCommonJS(index_exports);
|
|
|
40
41
|
var import_node_async_hooks = require("async_hooks");
|
|
41
42
|
var import_i18n = require("@astroscope/i18n");
|
|
42
43
|
var import_store = require("@contello/store");
|
|
44
|
+
var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
|
|
45
|
+
var DEFAULT_FILES_PREFIX = "/_contello/f/";
|
|
46
|
+
var DEFAULT_VIDEO_PREFIX = "/_contello/v/";
|
|
43
47
|
var runRequest = /* @__PURE__ */ Symbol("@contello/astro/runRequest");
|
|
44
48
|
function buildI18nRegistrations() {
|
|
45
49
|
const manifest = import_i18n.i18n.getManifest();
|
|
@@ -85,7 +89,16 @@ var Contello = class {
|
|
|
85
89
|
_als = new import_node_async_hooks.AsyncLocalStorage();
|
|
86
90
|
constructor(options) {
|
|
87
91
|
this._options = options;
|
|
88
|
-
|
|
92
|
+
const mediaOptions = {
|
|
93
|
+
baseUrl: options.media?.baseUrl ?? "",
|
|
94
|
+
imagesPath: options.media?.imagesPath ?? DEFAULT_IMAGES_PREFIX,
|
|
95
|
+
videosPath: options.media?.videosPath ?? DEFAULT_VIDEO_PREFIX,
|
|
96
|
+
filesPath: options.media?.filesPath ?? DEFAULT_FILES_PREFIX,
|
|
97
|
+
...options.media?.fallback !== void 0 ? { fallback: options.media.fallback } : {},
|
|
98
|
+
...options.media?.breakpoints !== void 0 ? { breakpoints: options.media.breakpoints } : {},
|
|
99
|
+
...options.media?.pictureFormats !== void 0 ? { pictureFormats: options.media.pictureFormats } : {}
|
|
100
|
+
};
|
|
101
|
+
this._store = (0, import_store.createStore)({ ...options, media: mediaOptions });
|
|
89
102
|
}
|
|
90
103
|
// --- lifecycle ---
|
|
91
104
|
async init(options) {
|
|
@@ -146,6 +159,9 @@ var Contello = class {
|
|
|
146
159
|
}
|
|
147
160
|
return this._i18nMessages;
|
|
148
161
|
}
|
|
162
|
+
get media() {
|
|
163
|
+
return this._store.media;
|
|
164
|
+
}
|
|
149
165
|
// --- ALS request context ---
|
|
150
166
|
get request() {
|
|
151
167
|
const ctx = this._als.getStore();
|
|
@@ -179,8 +195,7 @@ function createContello(options) {
|
|
|
179
195
|
return new Contello(options);
|
|
180
196
|
}
|
|
181
197
|
|
|
182
|
-
// src/middleware.ts
|
|
183
|
-
var import_excludes = require("@astroscope/excludes");
|
|
198
|
+
// src/assets-middleware.ts
|
|
184
199
|
var import_middleware = require("astro/middleware");
|
|
185
200
|
|
|
186
201
|
// src/diagnostics.ts
|
|
@@ -225,7 +240,68 @@ function wrap(name, fn) {
|
|
|
225
240
|
return result;
|
|
226
241
|
}
|
|
227
242
|
|
|
228
|
-
// src/middleware.ts
|
|
243
|
+
// src/assets-middleware.ts
|
|
244
|
+
var DEFAULT_IMAGES_CACHE_CONTROL = "public, max-age=31536000";
|
|
245
|
+
function createAssetsMiddleware(instance, options) {
|
|
246
|
+
const imagesPrefix = instance.media.imagesPath;
|
|
247
|
+
const imagesCacheControl = options?.images?.cacheControl ?? DEFAULT_IMAGES_CACHE_CONTROL;
|
|
248
|
+
const filesPrefix = instance.media.filesPath;
|
|
249
|
+
const filesCacheControl = options?.files?.cacheControl;
|
|
250
|
+
const videoPrefix = instance.media.videosPath;
|
|
251
|
+
return (0, import_middleware.defineMiddleware)((ctx, next) => {
|
|
252
|
+
const { pathname } = ctx.url;
|
|
253
|
+
const isImage = pathname.startsWith(imagesPrefix);
|
|
254
|
+
const isFile = !isImage && pathname.startsWith(filesPrefix);
|
|
255
|
+
const isVideo = !isImage && !isFile && pathname.startsWith(videoPrefix);
|
|
256
|
+
if (!isImage && !isFile && !isVideo) {
|
|
257
|
+
return next();
|
|
258
|
+
}
|
|
259
|
+
if (!instance.isReady) {
|
|
260
|
+
console.warn(`[@contello/astro] not initialized, passing through: ${pathname}`);
|
|
261
|
+
return next();
|
|
262
|
+
}
|
|
263
|
+
if (isImage) {
|
|
264
|
+
return handleFile(instance, next, pathname.slice(imagesPrefix.length), imagesCacheControl);
|
|
265
|
+
}
|
|
266
|
+
if (isFile) {
|
|
267
|
+
return handleFile(instance, next, pathname.slice(filesPrefix.length), filesCacheControl);
|
|
268
|
+
}
|
|
269
|
+
return handleVideo(instance, next, pathname.slice(videoPrefix.length), ctx.request.signal);
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
function handleFile(instance, next, rest, cacheControl) {
|
|
273
|
+
const dot = rest.indexOf(".");
|
|
274
|
+
const fileId = dot === -1 ? rest : rest.slice(0, dot);
|
|
275
|
+
if (!fileId) {
|
|
276
|
+
return next();
|
|
277
|
+
}
|
|
278
|
+
return wrap("assets:file", () => instance.assets.download(fileId)).then(
|
|
279
|
+
(result) => {
|
|
280
|
+
const headers = new Headers({ "content-type": result.mimeType });
|
|
281
|
+
if (cacheControl) {
|
|
282
|
+
headers.set("cache-control", cacheControl);
|
|
283
|
+
}
|
|
284
|
+
if (result.size) {
|
|
285
|
+
headers.set("content-length", String(result.size));
|
|
286
|
+
}
|
|
287
|
+
return new Response(result.stream(), { headers });
|
|
288
|
+
},
|
|
289
|
+
() => new Response(null, { status: 404 })
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
function handleVideo(instance, next, path, signal) {
|
|
293
|
+
if (!path) {
|
|
294
|
+
return next();
|
|
295
|
+
}
|
|
296
|
+
return wrap("assets:hls", () => instance.assets.proxyHls(path, signal)).then(
|
|
297
|
+
(result) => new Response(result.stream(), { status: result.status, headers: result.headers }),
|
|
298
|
+
() => new Response("Upstream is down", { status: 502, headers: { "content-type": "text/plain" } })
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// src/routing-middleware.ts
|
|
303
|
+
var import_excludes = require("@astroscope/excludes");
|
|
304
|
+
var import_middleware2 = require("astro/middleware");
|
|
229
305
|
function customHeaders(headers) {
|
|
230
306
|
const result = {};
|
|
231
307
|
for (const h of headers) {
|
|
@@ -235,7 +311,7 @@ function customHeaders(headers) {
|
|
|
235
311
|
}
|
|
236
312
|
function createRoutingMiddleware(instance, options) {
|
|
237
313
|
const { exclude } = options ?? {};
|
|
238
|
-
return (0,
|
|
314
|
+
return (0, import_middleware2.defineMiddleware)(async (ctx, next) => {
|
|
239
315
|
if ((0, import_excludes.shouldExclude)(ctx, exclude ?? import_excludes.RECOMMENDED_EXCLUDES)) {
|
|
240
316
|
return next();
|
|
241
317
|
}
|
|
@@ -312,6 +388,7 @@ function createRoutingMiddleware(instance, options) {
|
|
|
312
388
|
// Annotate the CommonJS export names for ESM import in node:
|
|
313
389
|
0 && (module.exports = {
|
|
314
390
|
Contello,
|
|
391
|
+
createAssetsMiddleware,
|
|
315
392
|
createContello,
|
|
316
393
|
createRoutingMiddleware
|
|
317
394
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OperationMap } from '@contello/client';
|
|
2
|
+
import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
|
|
2
3
|
import { CreateStoreOptions, AssetCollectionOptions, RouteCollectionOptions, Loadable, Assets, Routes, I18nMessages, StoreRoute, SingletonDef, Singleton, SingletonSyncDef, SingletonSync, CollectionDef, Collection, CollectionSyncDef, CollectionSync, LazyCollectionDef, LazyCollection } from '@contello/store';
|
|
3
4
|
import * as astro from 'astro';
|
|
4
5
|
import { ExcludePattern } from '@astroscope/excludes';
|
|
@@ -19,13 +20,31 @@ type ContelloInitOptions = {
|
|
|
19
20
|
load?: boolean | undefined;
|
|
20
21
|
} | undefined;
|
|
21
22
|
};
|
|
22
|
-
|
|
23
|
+
/**
|
|
24
|
+
* full `MediaResolverOptions` surface — everything is optional because astro
|
|
25
|
+
* auto-defaults `baseUrl` to `''` (same-origin) and `imagesPath` / `videosPath`
|
|
26
|
+
* to the assets middleware prefixes (`/_contello/i/`, `/_contello/v/`). all
|
|
27
|
+
* other fields (`fallback`, `breakpoints`, `pictureFormats`) pass through as-is.
|
|
28
|
+
*/
|
|
29
|
+
type ContelloMediaOptions = Partial<MediaResolverOptions>;
|
|
30
|
+
type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = Omit<CreateStoreOptions<TOps, TModels>, 'media'> & {
|
|
23
31
|
assets?: AssetCollectionOptions | undefined;
|
|
24
32
|
i18n?: ContelloI18nOptions | undefined;
|
|
25
33
|
routes?: RouteCollectionOptions | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* MediaResolver configuration. baseUrl / imagesPath / videosPath are
|
|
36
|
+
* auto-defaulted to match the assets middleware proxy prefixes, so
|
|
37
|
+
* `contello.media` is always available in astro projects.
|
|
38
|
+
*/
|
|
39
|
+
media?: ContelloMediaOptions | undefined;
|
|
26
40
|
};
|
|
41
|
+
type HasMediaFallback<O> = O extends {
|
|
42
|
+
media: {
|
|
43
|
+
fallback: ImageDef;
|
|
44
|
+
};
|
|
45
|
+
} ? true : false;
|
|
27
46
|
declare const runRequest: unique symbol;
|
|
28
|
-
declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
|
|
47
|
+
declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string, THasMediaFallback extends boolean = false> {
|
|
29
48
|
private readonly _store;
|
|
30
49
|
private readonly _options;
|
|
31
50
|
private _assets;
|
|
@@ -41,6 +60,7 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
|
|
|
41
60
|
get assets(): Assets;
|
|
42
61
|
get routes(): Routes;
|
|
43
62
|
get i18nMessages(): I18nMessages;
|
|
63
|
+
get media(): MediaResolver<THasMediaFallback>;
|
|
44
64
|
get request(): ContelloRequestContext;
|
|
45
65
|
defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
|
|
46
66
|
defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
|
|
@@ -55,11 +75,25 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
|
|
|
55
75
|
}>(def: LazyCollectionDef<TOps, TModel, TRaw, TMapped, TModels>): LazyCollection<TMapped>;
|
|
56
76
|
[runRequest]<T>(ctx: ContelloRequestContext, fn: () => T): T;
|
|
57
77
|
}
|
|
58
|
-
declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>
|
|
78
|
+
declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string, O extends ContelloOptions<TOps, TModels> = ContelloOptions<TOps, TModels>>(options: O): Contello<TOps, TModels, HasMediaFallback<O>>;
|
|
79
|
+
|
|
80
|
+
type ContelloAssetsImagesOptions = {
|
|
81
|
+
cacheControl?: string | undefined;
|
|
82
|
+
};
|
|
83
|
+
type ContelloAssetsFilesOptions = {
|
|
84
|
+
cacheControl?: string | undefined;
|
|
85
|
+
};
|
|
86
|
+
type ContelloAssetsVideoOptions = {};
|
|
87
|
+
type ContelloAssetsMiddlewareOptions = {
|
|
88
|
+
images?: ContelloAssetsImagesOptions | undefined;
|
|
89
|
+
files?: ContelloAssetsFilesOptions | undefined;
|
|
90
|
+
video?: ContelloAssetsVideoOptions | undefined;
|
|
91
|
+
};
|
|
92
|
+
declare function createAssetsMiddleware(instance: Contello<any, any>, options?: ContelloAssetsMiddlewareOptions | undefined): astro.MiddlewareHandler;
|
|
59
93
|
|
|
60
94
|
type ContelloMiddlewareOptions = {
|
|
61
95
|
exclude?: ExcludePattern[] | undefined;
|
|
62
96
|
};
|
|
63
97
|
declare function createRoutingMiddleware(instance: Contello<any, any>, options?: ContelloMiddlewareOptions | undefined): astro.MiddlewareHandler;
|
|
64
98
|
|
|
65
|
-
export { Contello, type ContelloI18nOptions, type ContelloInitOptions, type ContelloMiddlewareOptions, type ContelloOptions, type ContelloRequestContext, createContello, createRoutingMiddleware };
|
|
99
|
+
export { Contello, type ContelloAssetsFilesOptions, type ContelloAssetsImagesOptions, type ContelloAssetsMiddlewareOptions, type ContelloAssetsVideoOptions, type ContelloI18nOptions, type ContelloInitOptions, type ContelloMiddlewareOptions, type ContelloOptions, type ContelloRequestContext, createAssetsMiddleware, createContello, createRoutingMiddleware };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OperationMap } from '@contello/client';
|
|
2
|
+
import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
|
|
2
3
|
import { CreateStoreOptions, AssetCollectionOptions, RouteCollectionOptions, Loadable, Assets, Routes, I18nMessages, StoreRoute, SingletonDef, Singleton, SingletonSyncDef, SingletonSync, CollectionDef, Collection, CollectionSyncDef, CollectionSync, LazyCollectionDef, LazyCollection } from '@contello/store';
|
|
3
4
|
import * as astro from 'astro';
|
|
4
5
|
import { ExcludePattern } from '@astroscope/excludes';
|
|
@@ -19,13 +20,31 @@ type ContelloInitOptions = {
|
|
|
19
20
|
load?: boolean | undefined;
|
|
20
21
|
} | undefined;
|
|
21
22
|
};
|
|
22
|
-
|
|
23
|
+
/**
|
|
24
|
+
* full `MediaResolverOptions` surface — everything is optional because astro
|
|
25
|
+
* auto-defaults `baseUrl` to `''` (same-origin) and `imagesPath` / `videosPath`
|
|
26
|
+
* to the assets middleware prefixes (`/_contello/i/`, `/_contello/v/`). all
|
|
27
|
+
* other fields (`fallback`, `breakpoints`, `pictureFormats`) pass through as-is.
|
|
28
|
+
*/
|
|
29
|
+
type ContelloMediaOptions = Partial<MediaResolverOptions>;
|
|
30
|
+
type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = Omit<CreateStoreOptions<TOps, TModels>, 'media'> & {
|
|
23
31
|
assets?: AssetCollectionOptions | undefined;
|
|
24
32
|
i18n?: ContelloI18nOptions | undefined;
|
|
25
33
|
routes?: RouteCollectionOptions | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* MediaResolver configuration. baseUrl / imagesPath / videosPath are
|
|
36
|
+
* auto-defaulted to match the assets middleware proxy prefixes, so
|
|
37
|
+
* `contello.media` is always available in astro projects.
|
|
38
|
+
*/
|
|
39
|
+
media?: ContelloMediaOptions | undefined;
|
|
26
40
|
};
|
|
41
|
+
type HasMediaFallback<O> = O extends {
|
|
42
|
+
media: {
|
|
43
|
+
fallback: ImageDef;
|
|
44
|
+
};
|
|
45
|
+
} ? true : false;
|
|
27
46
|
declare const runRequest: unique symbol;
|
|
28
|
-
declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
|
|
47
|
+
declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string, THasMediaFallback extends boolean = false> {
|
|
29
48
|
private readonly _store;
|
|
30
49
|
private readonly _options;
|
|
31
50
|
private _assets;
|
|
@@ -41,6 +60,7 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
|
|
|
41
60
|
get assets(): Assets;
|
|
42
61
|
get routes(): Routes;
|
|
43
62
|
get i18nMessages(): I18nMessages;
|
|
63
|
+
get media(): MediaResolver<THasMediaFallback>;
|
|
44
64
|
get request(): ContelloRequestContext;
|
|
45
65
|
defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
|
|
46
66
|
defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
|
|
@@ -55,11 +75,25 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
|
|
|
55
75
|
}>(def: LazyCollectionDef<TOps, TModel, TRaw, TMapped, TModels>): LazyCollection<TMapped>;
|
|
56
76
|
[runRequest]<T>(ctx: ContelloRequestContext, fn: () => T): T;
|
|
57
77
|
}
|
|
58
|
-
declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>
|
|
78
|
+
declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string, O extends ContelloOptions<TOps, TModels> = ContelloOptions<TOps, TModels>>(options: O): Contello<TOps, TModels, HasMediaFallback<O>>;
|
|
79
|
+
|
|
80
|
+
type ContelloAssetsImagesOptions = {
|
|
81
|
+
cacheControl?: string | undefined;
|
|
82
|
+
};
|
|
83
|
+
type ContelloAssetsFilesOptions = {
|
|
84
|
+
cacheControl?: string | undefined;
|
|
85
|
+
};
|
|
86
|
+
type ContelloAssetsVideoOptions = {};
|
|
87
|
+
type ContelloAssetsMiddlewareOptions = {
|
|
88
|
+
images?: ContelloAssetsImagesOptions | undefined;
|
|
89
|
+
files?: ContelloAssetsFilesOptions | undefined;
|
|
90
|
+
video?: ContelloAssetsVideoOptions | undefined;
|
|
91
|
+
};
|
|
92
|
+
declare function createAssetsMiddleware(instance: Contello<any, any>, options?: ContelloAssetsMiddlewareOptions | undefined): astro.MiddlewareHandler;
|
|
59
93
|
|
|
60
94
|
type ContelloMiddlewareOptions = {
|
|
61
95
|
exclude?: ExcludePattern[] | undefined;
|
|
62
96
|
};
|
|
63
97
|
declare function createRoutingMiddleware(instance: Contello<any, any>, options?: ContelloMiddlewareOptions | undefined): astro.MiddlewareHandler;
|
|
64
98
|
|
|
65
|
-
export { Contello, type ContelloI18nOptions, type ContelloInitOptions, type ContelloMiddlewareOptions, type ContelloOptions, type ContelloRequestContext, createContello, createRoutingMiddleware };
|
|
99
|
+
export { Contello, type ContelloAssetsFilesOptions, type ContelloAssetsImagesOptions, type ContelloAssetsMiddlewareOptions, type ContelloAssetsVideoOptions, type ContelloI18nOptions, type ContelloInitOptions, type ContelloMiddlewareOptions, type ContelloOptions, type ContelloRequestContext, createAssetsMiddleware, createContello, createRoutingMiddleware };
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,9 @@ import { i18n } from "@astroscope/i18n";
|
|
|
4
4
|
import {
|
|
5
5
|
createStore
|
|
6
6
|
} from "@contello/store";
|
|
7
|
+
var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
|
|
8
|
+
var DEFAULT_FILES_PREFIX = "/_contello/f/";
|
|
9
|
+
var DEFAULT_VIDEO_PREFIX = "/_contello/v/";
|
|
7
10
|
var runRequest = /* @__PURE__ */ Symbol("@contello/astro/runRequest");
|
|
8
11
|
function buildI18nRegistrations() {
|
|
9
12
|
const manifest = i18n.getManifest();
|
|
@@ -49,7 +52,16 @@ var Contello = class {
|
|
|
49
52
|
_als = new AsyncLocalStorage();
|
|
50
53
|
constructor(options) {
|
|
51
54
|
this._options = options;
|
|
52
|
-
|
|
55
|
+
const mediaOptions = {
|
|
56
|
+
baseUrl: options.media?.baseUrl ?? "",
|
|
57
|
+
imagesPath: options.media?.imagesPath ?? DEFAULT_IMAGES_PREFIX,
|
|
58
|
+
videosPath: options.media?.videosPath ?? DEFAULT_VIDEO_PREFIX,
|
|
59
|
+
filesPath: options.media?.filesPath ?? DEFAULT_FILES_PREFIX,
|
|
60
|
+
...options.media?.fallback !== void 0 ? { fallback: options.media.fallback } : {},
|
|
61
|
+
...options.media?.breakpoints !== void 0 ? { breakpoints: options.media.breakpoints } : {},
|
|
62
|
+
...options.media?.pictureFormats !== void 0 ? { pictureFormats: options.media.pictureFormats } : {}
|
|
63
|
+
};
|
|
64
|
+
this._store = createStore({ ...options, media: mediaOptions });
|
|
53
65
|
}
|
|
54
66
|
// --- lifecycle ---
|
|
55
67
|
async init(options) {
|
|
@@ -110,6 +122,9 @@ var Contello = class {
|
|
|
110
122
|
}
|
|
111
123
|
return this._i18nMessages;
|
|
112
124
|
}
|
|
125
|
+
get media() {
|
|
126
|
+
return this._store.media;
|
|
127
|
+
}
|
|
113
128
|
// --- ALS request context ---
|
|
114
129
|
get request() {
|
|
115
130
|
const ctx = this._als.getStore();
|
|
@@ -143,8 +158,7 @@ function createContello(options) {
|
|
|
143
158
|
return new Contello(options);
|
|
144
159
|
}
|
|
145
160
|
|
|
146
|
-
// src/middleware.ts
|
|
147
|
-
import { RECOMMENDED_EXCLUDES, shouldExclude } from "@astroscope/excludes";
|
|
161
|
+
// src/assets-middleware.ts
|
|
148
162
|
import { defineMiddleware } from "astro/middleware";
|
|
149
163
|
|
|
150
164
|
// src/diagnostics.ts
|
|
@@ -189,7 +203,68 @@ function wrap(name, fn) {
|
|
|
189
203
|
return result;
|
|
190
204
|
}
|
|
191
205
|
|
|
192
|
-
// src/middleware.ts
|
|
206
|
+
// src/assets-middleware.ts
|
|
207
|
+
var DEFAULT_IMAGES_CACHE_CONTROL = "public, max-age=31536000";
|
|
208
|
+
function createAssetsMiddleware(instance, options) {
|
|
209
|
+
const imagesPrefix = instance.media.imagesPath;
|
|
210
|
+
const imagesCacheControl = options?.images?.cacheControl ?? DEFAULT_IMAGES_CACHE_CONTROL;
|
|
211
|
+
const filesPrefix = instance.media.filesPath;
|
|
212
|
+
const filesCacheControl = options?.files?.cacheControl;
|
|
213
|
+
const videoPrefix = instance.media.videosPath;
|
|
214
|
+
return defineMiddleware((ctx, next) => {
|
|
215
|
+
const { pathname } = ctx.url;
|
|
216
|
+
const isImage = pathname.startsWith(imagesPrefix);
|
|
217
|
+
const isFile = !isImage && pathname.startsWith(filesPrefix);
|
|
218
|
+
const isVideo = !isImage && !isFile && pathname.startsWith(videoPrefix);
|
|
219
|
+
if (!isImage && !isFile && !isVideo) {
|
|
220
|
+
return next();
|
|
221
|
+
}
|
|
222
|
+
if (!instance.isReady) {
|
|
223
|
+
console.warn(`[@contello/astro] not initialized, passing through: ${pathname}`);
|
|
224
|
+
return next();
|
|
225
|
+
}
|
|
226
|
+
if (isImage) {
|
|
227
|
+
return handleFile(instance, next, pathname.slice(imagesPrefix.length), imagesCacheControl);
|
|
228
|
+
}
|
|
229
|
+
if (isFile) {
|
|
230
|
+
return handleFile(instance, next, pathname.slice(filesPrefix.length), filesCacheControl);
|
|
231
|
+
}
|
|
232
|
+
return handleVideo(instance, next, pathname.slice(videoPrefix.length), ctx.request.signal);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function handleFile(instance, next, rest, cacheControl) {
|
|
236
|
+
const dot = rest.indexOf(".");
|
|
237
|
+
const fileId = dot === -1 ? rest : rest.slice(0, dot);
|
|
238
|
+
if (!fileId) {
|
|
239
|
+
return next();
|
|
240
|
+
}
|
|
241
|
+
return wrap("assets:file", () => instance.assets.download(fileId)).then(
|
|
242
|
+
(result) => {
|
|
243
|
+
const headers = new Headers({ "content-type": result.mimeType });
|
|
244
|
+
if (cacheControl) {
|
|
245
|
+
headers.set("cache-control", cacheControl);
|
|
246
|
+
}
|
|
247
|
+
if (result.size) {
|
|
248
|
+
headers.set("content-length", String(result.size));
|
|
249
|
+
}
|
|
250
|
+
return new Response(result.stream(), { headers });
|
|
251
|
+
},
|
|
252
|
+
() => new Response(null, { status: 404 })
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
function handleVideo(instance, next, path, signal) {
|
|
256
|
+
if (!path) {
|
|
257
|
+
return next();
|
|
258
|
+
}
|
|
259
|
+
return wrap("assets:hls", () => instance.assets.proxyHls(path, signal)).then(
|
|
260
|
+
(result) => new Response(result.stream(), { status: result.status, headers: result.headers }),
|
|
261
|
+
() => new Response("Upstream is down", { status: 502, headers: { "content-type": "text/plain" } })
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// src/routing-middleware.ts
|
|
266
|
+
import { RECOMMENDED_EXCLUDES, shouldExclude } from "@astroscope/excludes";
|
|
267
|
+
import { defineMiddleware as defineMiddleware2 } from "astro/middleware";
|
|
193
268
|
function customHeaders(headers) {
|
|
194
269
|
const result = {};
|
|
195
270
|
for (const h of headers) {
|
|
@@ -199,7 +274,7 @@ function customHeaders(headers) {
|
|
|
199
274
|
}
|
|
200
275
|
function createRoutingMiddleware(instance, options) {
|
|
201
276
|
const { exclude } = options ?? {};
|
|
202
|
-
return
|
|
277
|
+
return defineMiddleware2(async (ctx, next) => {
|
|
203
278
|
if (shouldExclude(ctx, exclude ?? RECOMMENDED_EXCLUDES)) {
|
|
204
279
|
return next();
|
|
205
280
|
}
|
|
@@ -275,6 +350,7 @@ function createRoutingMiddleware(instance, options) {
|
|
|
275
350
|
}
|
|
276
351
|
export {
|
|
277
352
|
Contello,
|
|
353
|
+
createAssetsMiddleware,
|
|
278
354
|
createContello,
|
|
279
355
|
createRoutingMiddleware
|
|
280
356
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contello/astro",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Astro integration for Contello CMS — route middleware and i18n bridge",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,12 +21,13 @@
|
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@contello/client": "^1.0
|
|
25
|
-
"@contello/store": "^1.
|
|
24
|
+
"@contello/client": "^1.1.0",
|
|
25
|
+
"@contello/store": "^1.4.0",
|
|
26
|
+
"@contello/media": "^1.0.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
|
-
"@astroscope/excludes": "^0.3.
|
|
29
|
-
"@astroscope/i18n": "^0.4.
|
|
29
|
+
"@astroscope/excludes": "^0.3.2",
|
|
30
|
+
"@astroscope/i18n": "^0.4.5",
|
|
30
31
|
"astro": "^6.1.8"
|
|
31
32
|
},
|
|
32
33
|
"files": [
|