@agentage/extension 0.10.0 → 0.11.1

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.
@@ -20,13 +20,37 @@ export const platformSources = (env) => {
20
20
  return [issuer, isAudience(audience) ? `${scheme}//${audience}` : undefined].filter((source) => Boolean(source));
21
21
  };
22
22
  const BODY = z.union([z.string(), z.instanceof(Uint8Array)]);
23
- const ENTRY = z.union([BODY, z.object({ type: z.string().min(1), body: BODY })]);
24
- const SiteSchema = z.record(z.string().regex(/^\/[A-Za-z0-9._~$/-]*$/), ENTRY);
23
+ const ENTRY = z.union([
24
+ BODY,
25
+ z.object({ type: z.string().min(1), body: BODY }),
26
+ // A host then a path: no userinfo, query or fragment, so nothing appended reaches the host.
27
+ z.object({ redirect: z.string().regex(/^https?:\/\/[^/?#@\\]+\/[^?#]*$/) }),
28
+ ]);
29
+ const KEY = /^\/[A-Za-z0-9._~$/-]*$|^(?:\/[A-Za-z0-9._~$-]+)+\/\*$/;
30
+ const SiteSchema = z.record(z.string().regex(KEY), ENTRY).superRefine((site, ctx) => {
31
+ for (const [key, entry] of Object.entries(site)) {
32
+ const to = typeof entry === 'object' && 'redirect' in entry ? entry.redirect : '/';
33
+ if (key.endsWith('/*') && !to.endsWith('/'))
34
+ ctx.addIssue({ code: 'custom', path: [key, 'redirect'], message: 'a /* redirect ends in /' });
35
+ }
36
+ });
37
+ const lookup = (site, path) => {
38
+ if (site[path] !== undefined)
39
+ return { entry: site[path], rest: '' };
40
+ const key = Object.keys(site)
41
+ .filter((k) => k.endsWith('/*') && path.startsWith(k.slice(0, -1)))
42
+ .sort((a, b) => b.length - a.length)[0];
43
+ return key ? { entry: site[key], rest: path.slice(key.length - 1) } : undefined;
44
+ };
25
45
  export const NOT_FOUND_PAGE = '<!doctype html>\n<html lang="en"><head><meta charset="utf-8"><title>Not found</title></head><body><p>Not found</p></body></html>\n';
26
46
  const serve = (site) => (req, res, next) => {
27
- const entry = site[req.path];
28
- if (entry === undefined)
47
+ const found = lookup(site, req.path);
48
+ if (!found)
29
49
  return next();
50
+ const { entry, rest } = found;
51
+ const query = req.originalUrl.indexOf('?');
52
+ if (typeof entry === 'object' && 'redirect' in entry)
53
+ return res.redirect(308, entry.redirect + rest + (query < 0 ? '' : req.originalUrl.slice(query)));
30
54
  const { type, body } = typeof entry === 'object' && 'type' in entry ? entry : { body: entry };
31
55
  res.type(type ?? typeOf(req.path)).setHeader('Cache-Control', 'no-cache');
32
56
  // express sends a Buffer as bytes but a plain Uint8Array as JSON.
@@ -3,6 +3,8 @@ export type PagesContext = PartContext;
3
3
  export type PagesEntry = string | Uint8Array | {
4
4
  type: string;
5
5
  body: string | Uint8Array;
6
+ } | {
7
+ redirect: string;
6
8
  };
7
9
  export type PagesSite = Readonly<Record<string, PagesEntry>>;
8
10
  export declare const versionPage: (ctx: PagesContext) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentage/extension",
3
- "version": "0.10.0",
3
+ "version": "0.11.1",
4
4
  "description": "The agentage extension kit: create(ctx) per part (pages, ui, api, mcp, worker, cli), the store, /health, discovery, the envelope, and the build tool with up and token",
5
5
  "type": "module",
6
6
  "license": "MIT",