@contello/astro 1.1.3 → 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
  });
@@ -179,8 +180,7 @@ function createContello(options) {
179
180
  return new Contello(options);
180
181
  }
181
182
 
182
- // src/middleware.ts
183
- var import_excludes = require("@astroscope/excludes");
183
+ // src/assets-middleware.ts
184
184
  var import_middleware = require("astro/middleware");
185
185
 
186
186
  // src/diagnostics.ts
@@ -225,7 +225,69 @@ function wrap(name, fn) {
225
225
  return result;
226
226
  }
227
227
 
228
- // 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");
229
291
  function customHeaders(headers) {
230
292
  const result = {};
231
293
  for (const h of headers) {
@@ -235,7 +297,7 @@ function customHeaders(headers) {
235
297
  }
236
298
  function createRoutingMiddleware(instance, options) {
237
299
  const { exclude } = options ?? {};
238
- return (0, import_middleware.defineMiddleware)(async (ctx, next) => {
300
+ return (0, import_middleware2.defineMiddleware)(async (ctx, next) => {
239
301
  if ((0, import_excludes.shouldExclude)(ctx, exclude ?? import_excludes.RECOMMENDED_EXCLUDES)) {
240
302
  return next();
241
303
  }
@@ -312,6 +374,7 @@ function createRoutingMiddleware(instance, options) {
312
374
  // Annotate the CommonJS export names for ESM import in node:
313
375
  0 && (module.exports = {
314
376
  Contello,
377
+ createAssetsMiddleware,
315
378
  createContello,
316
379
  createRoutingMiddleware
317
380
  });
package/dist/index.d.cts CHANGED
@@ -57,9 +57,27 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
57
57
  }
58
58
  declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: ContelloOptions<TOps, TModels>): Contello<TOps, TModels>;
59
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
+
60
78
  type ContelloMiddlewareOptions = {
61
79
  exclude?: ExcludePattern[] | undefined;
62
80
  };
63
81
  declare function createRoutingMiddleware(instance: Contello<any, any>, options?: ContelloMiddlewareOptions | undefined): astro.MiddlewareHandler;
64
82
 
65
- export { Contello, type ContelloI18nOptions, type ContelloInitOptions, 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
@@ -57,9 +57,27 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
57
57
  }
58
58
  declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: ContelloOptions<TOps, TModels>): Contello<TOps, TModels>;
59
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
+
60
78
  type ContelloMiddlewareOptions = {
61
79
  exclude?: ExcludePattern[] | undefined;
62
80
  };
63
81
  declare function createRoutingMiddleware(instance: Contello<any, any>, options?: ContelloMiddlewareOptions | undefined): astro.MiddlewareHandler;
64
82
 
65
- export { Contello, type ContelloI18nOptions, type ContelloInitOptions, 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
@@ -143,8 +143,7 @@ function createContello(options) {
143
143
  return new Contello(options);
144
144
  }
145
145
 
146
- // src/middleware.ts
147
- import { RECOMMENDED_EXCLUDES, shouldExclude } from "@astroscope/excludes";
146
+ // src/assets-middleware.ts
148
147
  import { defineMiddleware } from "astro/middleware";
149
148
 
150
149
  // src/diagnostics.ts
@@ -189,7 +188,69 @@ function wrap(name, fn) {
189
188
  return result;
190
189
  }
191
190
 
192
- // 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";
193
254
  function customHeaders(headers) {
194
255
  const result = {};
195
256
  for (const h of headers) {
@@ -199,7 +260,7 @@ function customHeaders(headers) {
199
260
  }
200
261
  function createRoutingMiddleware(instance, options) {
201
262
  const { exclude } = options ?? {};
202
- return defineMiddleware(async (ctx, next) => {
263
+ return defineMiddleware2(async (ctx, next) => {
203
264
  if (shouldExclude(ctx, exclude ?? RECOMMENDED_EXCLUDES)) {
204
265
  return next();
205
266
  }
@@ -275,6 +336,7 @@ function createRoutingMiddleware(instance, options) {
275
336
  }
276
337
  export {
277
338
  Contello,
339
+ createAssetsMiddleware,
278
340
  createContello,
279
341
  createRoutingMiddleware
280
342
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/astro",
3
- "version": "1.1.3",
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,12 +21,12 @@
21
21
  }
22
22
  },
23
23
  "dependencies": {
24
- "@contello/client": "^1.0.3",
25
- "@contello/store": "^1.1.2"
24
+ "@contello/store": "^1.3.0",
25
+ "@contello/client": "^1.1.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@astroscope/excludes": "^0.3.1",
29
- "@astroscope/i18n": "^0.4.4",
28
+ "@astroscope/excludes": "^0.3.2",
29
+ "@astroscope/i18n": "^0.4.5",
30
30
  "astro": "^6.1.8"
31
31
  },
32
32
  "files": [