@contello/astro 1.1.2 → 1.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 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
  });
@@ -93,7 +94,8 @@ var Contello = class {
93
94
  this._assets = this._store.defineAssets(this._options.assets);
94
95
  this._routes = this._store.defineRoutes(this._options.routes);
95
96
  if (this._options.i18n) {
96
- const { collection, languages, register = true, load = true } = this._options.i18n;
97
+ const { collection, languages } = this._options.i18n;
98
+ const { register = true, load = true } = options?.i18n ?? {};
97
99
  this._i18nMessages = this._store.defineI18nMessages({ collection });
98
100
  await import_i18n.i18n.configure({ locales: languages });
99
101
  if (register) {
@@ -123,6 +125,9 @@ var Contello = class {
123
125
  ping() {
124
126
  return this._store.ping();
125
127
  }
128
+ get isReady() {
129
+ return !!this._routes;
130
+ }
126
131
  // --- pre-wired collections ---
127
132
  get assets() {
128
133
  if (!this._assets) {
@@ -175,8 +180,7 @@ function createContello(options) {
175
180
  return new Contello(options);
176
181
  }
177
182
 
178
- // src/middleware.ts
179
- var import_excludes = require("@astroscope/excludes");
183
+ // src/assets-middleware.ts
180
184
  var import_middleware = require("astro/middleware");
181
185
 
182
186
  // src/diagnostics.ts
@@ -221,7 +225,69 @@ function wrap(name, fn) {
221
225
  return result;
222
226
  }
223
227
 
224
- // src/middleware.ts
228
+ // src/assets-middleware.ts
229
+ var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
230
+ var DEFAULT_FILES_PREFIX = "/_contello/f/";
231
+ var DEFAULT_VIDEO_PREFIX = "/_contello/v/";
232
+ var DEFAULT_IMAGES_CACHE_CONTROL = "public, max-age=31536000";
233
+ function createAssetsMiddleware(instance, options) {
234
+ const imagesPrefix = options?.images?.prefix ?? DEFAULT_IMAGES_PREFIX;
235
+ const imagesCacheControl = options?.images?.cacheControl ?? DEFAULT_IMAGES_CACHE_CONTROL;
236
+ const filesPrefix = options?.files?.prefix ?? DEFAULT_FILES_PREFIX;
237
+ const filesCacheControl = options?.files?.cacheControl;
238
+ const videoPrefix = options?.video?.prefix ?? DEFAULT_VIDEO_PREFIX;
239
+ return (0, import_middleware.defineMiddleware)((ctx, next) => {
240
+ const { pathname } = ctx.url;
241
+ const matches = pathname.startsWith(imagesPrefix) || pathname.startsWith(filesPrefix) || pathname.startsWith(videoPrefix);
242
+ if (!matches) {
243
+ return next();
244
+ }
245
+ if (!instance.isReady) {
246
+ console.warn(`[@contello/astro] not initialized, passing through: ${pathname}`);
247
+ return next();
248
+ }
249
+ if (pathname.startsWith(imagesPrefix)) {
250
+ return handleFile(instance, next, pathname.slice(imagesPrefix.length), imagesCacheControl);
251
+ }
252
+ if (pathname.startsWith(filesPrefix)) {
253
+ return handleFile(instance, next, pathname.slice(filesPrefix.length), filesCacheControl);
254
+ }
255
+ return handleVideo(instance, next, pathname.slice(videoPrefix.length), ctx.request.signal);
256
+ });
257
+ }
258
+ function handleFile(instance, next, rest, cacheControl) {
259
+ const dot = rest.indexOf(".");
260
+ const fileId = dot === -1 ? rest : rest.slice(0, dot);
261
+ if (!fileId) {
262
+ return next();
263
+ }
264
+ return wrap("assets:file", () => instance.assets.download(fileId)).then(
265
+ (result) => {
266
+ const headers = new Headers({ "content-type": result.mimeType });
267
+ if (cacheControl) {
268
+ headers.set("cache-control", cacheControl);
269
+ }
270
+ if (result.size) {
271
+ headers.set("content-length", String(result.size));
272
+ }
273
+ return new Response(result.stream(), { headers });
274
+ },
275
+ () => new Response(null, { status: 404 })
276
+ );
277
+ }
278
+ function handleVideo(instance, next, path, signal) {
279
+ if (!path) {
280
+ return next();
281
+ }
282
+ return wrap("assets:hls", () => instance.assets.proxyHls(path, signal)).then(
283
+ (result) => new Response(result.stream(), { status: result.status, headers: result.headers }),
284
+ () => new Response("Upstream is down", { status: 502, headers: { "content-type": "text/plain" } })
285
+ );
286
+ }
287
+
288
+ // src/routing-middleware.ts
289
+ var import_excludes = require("@astroscope/excludes");
290
+ var import_middleware2 = require("astro/middleware");
225
291
  function customHeaders(headers) {
226
292
  const result = {};
227
293
  for (const h of headers) {
@@ -231,10 +297,14 @@ function customHeaders(headers) {
231
297
  }
232
298
  function createRoutingMiddleware(instance, options) {
233
299
  const { exclude } = options ?? {};
234
- return (0, import_middleware.defineMiddleware)(async (ctx, next) => {
300
+ return (0, import_middleware2.defineMiddleware)(async (ctx, next) => {
235
301
  if ((0, import_excludes.shouldExclude)(ctx, exclude ?? import_excludes.RECOMMENDED_EXCLUDES)) {
236
302
  return next();
237
303
  }
304
+ if (!instance.isReady) {
305
+ console.warn(`[@contello/astro] not initialized, passing through: ${ctx.url.pathname}`);
306
+ return next();
307
+ }
238
308
  const { url } = ctx;
239
309
  if (url.pathname.startsWith("/contello/entities/")) {
240
310
  return instance[runRequest]({ url, route: void 0, rewritten: false }, () => next());
@@ -304,6 +374,7 @@ function createRoutingMiddleware(instance, options) {
304
374
  // Annotate the CommonJS export names for ESM import in node:
305
375
  0 && (module.exports = {
306
376
  Contello,
377
+ createAssetsMiddleware,
307
378
  createContello,
308
379
  createRoutingMiddleware
309
380
  });
package/dist/index.d.cts CHANGED
@@ -11,8 +11,13 @@ type ContelloRequestContext = {
11
11
  type ContelloI18nOptions = {
12
12
  collection: string;
13
13
  languages: string[];
14
- register?: boolean | undefined;
15
- load?: boolean | undefined;
14
+ };
15
+ type ContelloInitOptions = {
16
+ load?: Loadable[] | undefined;
17
+ i18n?: {
18
+ register?: boolean | undefined;
19
+ load?: boolean | undefined;
20
+ } | undefined;
16
21
  };
17
22
  type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = CreateStoreOptions<TOps, TModels> & {
18
23
  assets?: AssetCollectionOptions | undefined;
@@ -29,11 +34,10 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
29
34
  private _i18nSubscription;
30
35
  private readonly _als;
31
36
  constructor(options: ContelloOptions<TOps, TModels>);
32
- init(options?: {
33
- load?: Loadable[] | undefined;
34
- } | undefined): Promise<void>;
37
+ init(options?: ContelloInitOptions | undefined): Promise<void>;
35
38
  destroy(): Promise<void>;
36
39
  ping(): Promise<void>;
40
+ get isReady(): boolean;
37
41
  get assets(): Assets;
38
42
  get routes(): Routes;
39
43
  get i18nMessages(): I18nMessages;
@@ -53,9 +57,27 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
53
57
  }
54
58
  declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: ContelloOptions<TOps, TModels>): Contello<TOps, TModels>;
55
59
 
60
+ type ContelloAssetsImagesOptions = {
61
+ prefix?: string | undefined;
62
+ cacheControl?: string | undefined;
63
+ };
64
+ type ContelloAssetsFilesOptions = {
65
+ prefix?: string | undefined;
66
+ cacheControl?: string | undefined;
67
+ };
68
+ type ContelloAssetsVideoOptions = {
69
+ prefix?: string | undefined;
70
+ };
71
+ type ContelloAssetsMiddlewareOptions = {
72
+ images?: ContelloAssetsImagesOptions | undefined;
73
+ files?: ContelloAssetsFilesOptions | undefined;
74
+ video?: ContelloAssetsVideoOptions | undefined;
75
+ };
76
+ declare function createAssetsMiddleware(instance: Contello<any, any>, options?: ContelloAssetsMiddlewareOptions | undefined): astro.MiddlewareHandler;
77
+
56
78
  type ContelloMiddlewareOptions = {
57
79
  exclude?: ExcludePattern[] | undefined;
58
80
  };
59
81
  declare function createRoutingMiddleware(instance: Contello<any, any>, options?: ContelloMiddlewareOptions | undefined): astro.MiddlewareHandler;
60
82
 
61
- export { Contello, type ContelloI18nOptions, type ContelloMiddlewareOptions, type ContelloOptions, type ContelloRequestContext, createContello, createRoutingMiddleware };
83
+ 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
@@ -11,8 +11,13 @@ type ContelloRequestContext = {
11
11
  type ContelloI18nOptions = {
12
12
  collection: string;
13
13
  languages: string[];
14
- register?: boolean | undefined;
15
- load?: boolean | undefined;
14
+ };
15
+ type ContelloInitOptions = {
16
+ load?: Loadable[] | undefined;
17
+ i18n?: {
18
+ register?: boolean | undefined;
19
+ load?: boolean | undefined;
20
+ } | undefined;
16
21
  };
17
22
  type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = CreateStoreOptions<TOps, TModels> & {
18
23
  assets?: AssetCollectionOptions | undefined;
@@ -29,11 +34,10 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
29
34
  private _i18nSubscription;
30
35
  private readonly _als;
31
36
  constructor(options: ContelloOptions<TOps, TModels>);
32
- init(options?: {
33
- load?: Loadable[] | undefined;
34
- } | undefined): Promise<void>;
37
+ init(options?: ContelloInitOptions | undefined): Promise<void>;
35
38
  destroy(): Promise<void>;
36
39
  ping(): Promise<void>;
40
+ get isReady(): boolean;
37
41
  get assets(): Assets;
38
42
  get routes(): Routes;
39
43
  get i18nMessages(): I18nMessages;
@@ -53,9 +57,27 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
53
57
  }
54
58
  declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: ContelloOptions<TOps, TModels>): Contello<TOps, TModels>;
55
59
 
60
+ type ContelloAssetsImagesOptions = {
61
+ prefix?: string | undefined;
62
+ cacheControl?: string | undefined;
63
+ };
64
+ type ContelloAssetsFilesOptions = {
65
+ prefix?: string | undefined;
66
+ cacheControl?: string | undefined;
67
+ };
68
+ type ContelloAssetsVideoOptions = {
69
+ prefix?: string | undefined;
70
+ };
71
+ type ContelloAssetsMiddlewareOptions = {
72
+ images?: ContelloAssetsImagesOptions | undefined;
73
+ files?: ContelloAssetsFilesOptions | undefined;
74
+ video?: ContelloAssetsVideoOptions | undefined;
75
+ };
76
+ declare function createAssetsMiddleware(instance: Contello<any, any>, options?: ContelloAssetsMiddlewareOptions | undefined): astro.MiddlewareHandler;
77
+
56
78
  type ContelloMiddlewareOptions = {
57
79
  exclude?: ExcludePattern[] | undefined;
58
80
  };
59
81
  declare function createRoutingMiddleware(instance: Contello<any, any>, options?: ContelloMiddlewareOptions | undefined): astro.MiddlewareHandler;
60
82
 
61
- export { Contello, type ContelloI18nOptions, type ContelloMiddlewareOptions, type ContelloOptions, type ContelloRequestContext, createContello, createRoutingMiddleware };
83
+ 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
@@ -57,7 +57,8 @@ var Contello = class {
57
57
  this._assets = this._store.defineAssets(this._options.assets);
58
58
  this._routes = this._store.defineRoutes(this._options.routes);
59
59
  if (this._options.i18n) {
60
- const { collection, languages, register = true, load = true } = this._options.i18n;
60
+ const { collection, languages } = this._options.i18n;
61
+ const { register = true, load = true } = options?.i18n ?? {};
61
62
  this._i18nMessages = this._store.defineI18nMessages({ collection });
62
63
  await i18n.configure({ locales: languages });
63
64
  if (register) {
@@ -87,6 +88,9 @@ var Contello = class {
87
88
  ping() {
88
89
  return this._store.ping();
89
90
  }
91
+ get isReady() {
92
+ return !!this._routes;
93
+ }
90
94
  // --- pre-wired collections ---
91
95
  get assets() {
92
96
  if (!this._assets) {
@@ -139,8 +143,7 @@ function createContello(options) {
139
143
  return new Contello(options);
140
144
  }
141
145
 
142
- // src/middleware.ts
143
- import { RECOMMENDED_EXCLUDES, shouldExclude } from "@astroscope/excludes";
146
+ // src/assets-middleware.ts
144
147
  import { defineMiddleware } from "astro/middleware";
145
148
 
146
149
  // src/diagnostics.ts
@@ -185,7 +188,69 @@ function wrap(name, fn) {
185
188
  return result;
186
189
  }
187
190
 
188
- // src/middleware.ts
191
+ // src/assets-middleware.ts
192
+ var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
193
+ var DEFAULT_FILES_PREFIX = "/_contello/f/";
194
+ var DEFAULT_VIDEO_PREFIX = "/_contello/v/";
195
+ var DEFAULT_IMAGES_CACHE_CONTROL = "public, max-age=31536000";
196
+ function createAssetsMiddleware(instance, options) {
197
+ const imagesPrefix = options?.images?.prefix ?? DEFAULT_IMAGES_PREFIX;
198
+ const imagesCacheControl = options?.images?.cacheControl ?? DEFAULT_IMAGES_CACHE_CONTROL;
199
+ const filesPrefix = options?.files?.prefix ?? DEFAULT_FILES_PREFIX;
200
+ const filesCacheControl = options?.files?.cacheControl;
201
+ const videoPrefix = options?.video?.prefix ?? DEFAULT_VIDEO_PREFIX;
202
+ return defineMiddleware((ctx, next) => {
203
+ const { pathname } = ctx.url;
204
+ const matches = pathname.startsWith(imagesPrefix) || pathname.startsWith(filesPrefix) || pathname.startsWith(videoPrefix);
205
+ if (!matches) {
206
+ return next();
207
+ }
208
+ if (!instance.isReady) {
209
+ console.warn(`[@contello/astro] not initialized, passing through: ${pathname}`);
210
+ return next();
211
+ }
212
+ if (pathname.startsWith(imagesPrefix)) {
213
+ return handleFile(instance, next, pathname.slice(imagesPrefix.length), imagesCacheControl);
214
+ }
215
+ if (pathname.startsWith(filesPrefix)) {
216
+ return handleFile(instance, next, pathname.slice(filesPrefix.length), filesCacheControl);
217
+ }
218
+ return handleVideo(instance, next, pathname.slice(videoPrefix.length), ctx.request.signal);
219
+ });
220
+ }
221
+ function handleFile(instance, next, rest, cacheControl) {
222
+ const dot = rest.indexOf(".");
223
+ const fileId = dot === -1 ? rest : rest.slice(0, dot);
224
+ if (!fileId) {
225
+ return next();
226
+ }
227
+ return wrap("assets:file", () => instance.assets.download(fileId)).then(
228
+ (result) => {
229
+ const headers = new Headers({ "content-type": result.mimeType });
230
+ if (cacheControl) {
231
+ headers.set("cache-control", cacheControl);
232
+ }
233
+ if (result.size) {
234
+ headers.set("content-length", String(result.size));
235
+ }
236
+ return new Response(result.stream(), { headers });
237
+ },
238
+ () => new Response(null, { status: 404 })
239
+ );
240
+ }
241
+ function handleVideo(instance, next, path, signal) {
242
+ if (!path) {
243
+ return next();
244
+ }
245
+ return wrap("assets:hls", () => instance.assets.proxyHls(path, signal)).then(
246
+ (result) => new Response(result.stream(), { status: result.status, headers: result.headers }),
247
+ () => new Response("Upstream is down", { status: 502, headers: { "content-type": "text/plain" } })
248
+ );
249
+ }
250
+
251
+ // src/routing-middleware.ts
252
+ import { RECOMMENDED_EXCLUDES, shouldExclude } from "@astroscope/excludes";
253
+ import { defineMiddleware as defineMiddleware2 } from "astro/middleware";
189
254
  function customHeaders(headers) {
190
255
  const result = {};
191
256
  for (const h of headers) {
@@ -195,10 +260,14 @@ function customHeaders(headers) {
195
260
  }
196
261
  function createRoutingMiddleware(instance, options) {
197
262
  const { exclude } = options ?? {};
198
- return defineMiddleware(async (ctx, next) => {
263
+ return defineMiddleware2(async (ctx, next) => {
199
264
  if (shouldExclude(ctx, exclude ?? RECOMMENDED_EXCLUDES)) {
200
265
  return next();
201
266
  }
267
+ if (!instance.isReady) {
268
+ console.warn(`[@contello/astro] not initialized, passing through: ${ctx.url.pathname}`);
269
+ return next();
270
+ }
202
271
  const { url } = ctx;
203
272
  if (url.pathname.startsWith("/contello/entities/")) {
204
273
  return instance[runRequest]({ url, route: void 0, rewritten: false }, () => next());
@@ -267,6 +336,7 @@ function createRoutingMiddleware(instance, options) {
267
336
  }
268
337
  export {
269
338
  Contello,
339
+ createAssetsMiddleware,
270
340
  createContello,
271
341
  createRoutingMiddleware
272
342
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/astro",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "Astro integration for Contello CMS — route middleware and i18n bridge",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -21,13 +21,13 @@
21
21
  }
22
22
  },
23
23
  "dependencies": {
24
- "@contello/client": "^1.0.3",
25
- "@contello/store": "^1.1.1"
24
+ "@contello/store": "^1.3.0",
25
+ "@contello/client": "^1.1.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@astroscope/excludes": "^0.3.0",
29
- "@astroscope/i18n": "^0.4.0",
30
- "astro": "^6.0.7"
28
+ "@astroscope/excludes": "^0.3.2",
29
+ "@astroscope/i18n": "^0.4.5",
30
+ "astro": "^6.1.8"
31
31
  },
32
32
  "files": [
33
33
  "dist"