@pexis/core 0.1.2 → 0.1.3
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/AGENTS.md +53 -10
- package/API.md +6 -5
- package/README.md +1 -1
- package/api.json +5951 -0
- package/dist/app.d.ts +77 -3
- package/dist/app.js +207 -31
- package/dist/app.js.map +1 -1
- package/dist/components.d.ts +54 -4
- package/dist/components.js +34 -3
- package/dist/components.js.map +1 -1
- package/dist/context.d.ts +27 -2
- package/dist/context.js +18 -2
- package/dist/context.js.map +1 -1
- package/dist/cors.d.ts +14 -0
- package/dist/cors.js +8 -0
- package/dist/cors.js.map +1 -1
- package/dist/csrf.d.ts +9 -0
- package/dist/csrf.js +9 -0
- package/dist/csrf.js.map +1 -1
- package/dist/form.d.ts +21 -3
- package/dist/form.js +38 -4
- package/dist/form.js.map +1 -1
- package/dist/host.d.ts +6 -1
- package/dist/host.js +6 -1
- package/dist/host.js.map +1 -1
- package/dist/html.d.ts +46 -5
- package/dist/html.js +112 -5
- package/dist/html.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js.map +1 -1
- package/dist/jsx-runtime.d.ts +32 -0
- package/dist/jsx-runtime.js +16 -0
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/page.d.ts +74 -1
- package/dist/page.js +48 -5
- package/dist/page.js.map +1 -1
- package/dist/rate-limit.d.ts +46 -0
- package/dist/rate-limit.js +19 -0
- package/dist/rate-limit.js.map +1 -1
- package/dist/registry.d.ts +126 -3
- package/dist/registry.js +53 -1
- package/dist/registry.js.map +1 -1
- package/dist/schema.d.ts +129 -3
- package/dist/schema.js +80 -7
- package/dist/schema.js.map +1 -1
- package/dist/security.d.ts +38 -2
- package/dist/security.js +44 -9
- package/dist/security.js.map +1 -1
- package/dist/styles.d.ts +37 -2
- package/dist/styles.js +32 -2
- package/dist/styles.js.map +1 -1
- package/dist/types.d.ts +163 -19
- package/dist/urls.d.ts +8 -1
- package/dist/urls.js +8 -1
- package/dist/urls.js.map +1 -1
- package/package.json +2 -1
package/dist/page.d.ts
CHANGED
|
@@ -2,26 +2,99 @@ import type { Markup } from './html.js';
|
|
|
2
2
|
import type { Context, Layout, NotFound, Page, PageDefinition, Redirect, Signal } from './types.js';
|
|
3
3
|
/** A page defined in its own file under pages/, whose path comes from the file name. */
|
|
4
4
|
export interface UnroutedPage {
|
|
5
|
+
/** Marks the object as a page without a path yet. */
|
|
5
6
|
readonly kind: 'unrouted-page';
|
|
7
|
+
/** The page's definition, without its path. */
|
|
6
8
|
readonly definition: Omit<PageDefinition<string, unknown>, 'path'>;
|
|
7
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Declares a page: its path, the data it loads, its head and how it renders.
|
|
12
|
+
* In a file under pages/, leave out `path`: it comes from the file name
|
|
13
|
+
* (pages/menu/[dish].tsx is "/menu/:dish"). Throws when the path is not valid
|
|
14
|
+
* or a fixed head has no title or description.
|
|
15
|
+
*
|
|
16
|
+
* @param def The page: path, load, head, render, and options.
|
|
17
|
+
* @example
|
|
18
|
+
* A page with a parameter
|
|
19
|
+
* ```tsx
|
|
20
|
+
* export default definePage({
|
|
21
|
+
* load: async ({ params }) => (await findDish(params.dish)) ?? notFound(),
|
|
22
|
+
* head: (dish) => ({ title: dish.name, description: dish.summary }),
|
|
23
|
+
* render: ({ data: dish }) => <h1>{dish.name}</h1>,
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
8
27
|
export declare function definePage<P extends string, D = undefined>(def: PageDefinition<P, D>): Page;
|
|
28
|
+
/**
|
|
29
|
+
* Declares a page in a file under pages/, whose path comes from the file name.
|
|
30
|
+
*
|
|
31
|
+
* @param def The page, without its path.
|
|
32
|
+
*/
|
|
9
33
|
export declare function definePage<D = undefined>(def: Omit<PageDefinition<string, D>, 'path'>): UnroutedPage;
|
|
10
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Gives a page from a pages/ file its path. Used by the generated route list.
|
|
36
|
+
*
|
|
37
|
+
* @category plugins
|
|
38
|
+
* @param path The path the file's name gives.
|
|
39
|
+
* @param page The file's default export.
|
|
40
|
+
*/
|
|
11
41
|
export declare function atPath(path: string, page: UnroutedPage | Page): Page;
|
|
12
42
|
/**
|
|
13
43
|
* Checks a page or route path: segments like /menu, /menu/:dish or /docs/*,
|
|
14
44
|
* a * only last, no parameter twice. Returns whether it has parameters.
|
|
15
45
|
*/
|
|
16
46
|
export declare function checkPath(path: string, what: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Declares a layout: the shell around every page's body, with its header,
|
|
49
|
+
* navigation and footer. It must include `<main>` with `children` inside it.
|
|
50
|
+
*
|
|
51
|
+
* @param render Renders the shell around `children`.
|
|
52
|
+
* @example
|
|
53
|
+
* ```tsx
|
|
54
|
+
* export const site = defineLayout(({ children }) => (
|
|
55
|
+
* <>
|
|
56
|
+
* <header><a href="/">Home</a></header>
|
|
57
|
+
* <main>{children}</main>
|
|
58
|
+
* </>
|
|
59
|
+
* ));
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
17
62
|
export declare function defineLayout(render: (props: {
|
|
18
63
|
children: Markup;
|
|
19
64
|
ctx: Context;
|
|
20
65
|
}) => Markup | null | undefined): Layout;
|
|
66
|
+
/**
|
|
67
|
+
* Return it from a page's load() to answer 404 with the not-found page.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* load: async ({ params }) => (await findDish(params.dish)) ?? notFound(),
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
21
74
|
export declare function notFound(): NotFound;
|
|
75
|
+
/**
|
|
76
|
+
* Return it from a page's load() to send the visitor elsewhere. Targets on
|
|
77
|
+
* other sites are refused unless you pass `{ external: true }`, so a URL taken
|
|
78
|
+
* from user input cannot turn your site into an open redirect.
|
|
79
|
+
*
|
|
80
|
+
* @param location A path on this site, or a full URL with `external`.
|
|
81
|
+
* @param status The redirect status. Default: 303.
|
|
82
|
+
* @param options `external: true` allows another site.
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* load: (ctx) => (ctx.user ? undefined : redirect('/sign-in')),
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
22
88
|
export declare function redirect(location: string, status?: Redirect['status'], options?: {
|
|
89
|
+
/** Allow a target on another site. */
|
|
23
90
|
external?: boolean;
|
|
24
91
|
}): Redirect;
|
|
92
|
+
/**
|
|
93
|
+
* Whether a value is what notFound() or redirect() returns.
|
|
94
|
+
*
|
|
95
|
+
* @category plugins
|
|
96
|
+
* @param value Anything, such as what a page's load() returned.
|
|
97
|
+
*/
|
|
25
98
|
export declare function isSignal(value: unknown): value is Signal;
|
|
26
99
|
export interface CompiledPath {
|
|
27
100
|
path: string;
|
package/dist/page.js
CHANGED
|
@@ -5,7 +5,13 @@ export function definePage(def) {
|
|
|
5
5
|
}
|
|
6
6
|
return routedPage(def);
|
|
7
7
|
}
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Gives a page from a pages/ file its path. Used by the generated route list.
|
|
10
|
+
*
|
|
11
|
+
* @category plugins
|
|
12
|
+
* @param path The path the file's name gives.
|
|
13
|
+
* @param page The file's default export.
|
|
14
|
+
*/
|
|
9
15
|
export function atPath(path, page) {
|
|
10
16
|
if (page?.kind === 'page')
|
|
11
17
|
throw new Error(`The page file for "${path}" sets its own path ("${page.path}"). Remove path; it comes from the file name.`);
|
|
@@ -48,24 +54,61 @@ function routedPage(def) {
|
|
|
48
54
|
stream: def.stream === true,
|
|
49
55
|
};
|
|
50
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Declares a layout: the shell around every page's body, with its header,
|
|
59
|
+
* navigation and footer. It must include `<main>` with `children` inside it.
|
|
60
|
+
*
|
|
61
|
+
* @param render Renders the shell around `children`.
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* export const site = defineLayout(({ children }) => (
|
|
65
|
+
* <>
|
|
66
|
+
* <header><a href="/">Home</a></header>
|
|
67
|
+
* <main>{children}</main>
|
|
68
|
+
* </>
|
|
69
|
+
* ));
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
51
72
|
export function defineLayout(render) {
|
|
52
73
|
return { kind: 'layout', render };
|
|
53
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Return it from a page's load() to answer 404 with the not-found page.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* load: async ({ params }) => (await findDish(params.dish)) ?? notFound(),
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
54
83
|
export function notFound() {
|
|
55
84
|
return { pexisSignal: 'not-found' };
|
|
56
85
|
}
|
|
86
|
+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
57
87
|
/**
|
|
58
|
-
*
|
|
59
|
-
* pass { external: true }
|
|
60
|
-
* site into an open redirect.
|
|
88
|
+
* Return it from a page's load() to send the visitor elsewhere. Targets on
|
|
89
|
+
* other sites are refused unless you pass `{ external: true }`, so a URL taken
|
|
90
|
+
* from user input cannot turn your site into an open redirect.
|
|
91
|
+
*
|
|
92
|
+
* @param location A path on this site, or a full URL with `external`.
|
|
93
|
+
* @param status The redirect status. Default: 303.
|
|
94
|
+
* @param options `external: true` allows another site.
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* load: (ctx) => (ctx.user ? undefined : redirect('/sign-in')),
|
|
98
|
+
* ```
|
|
61
99
|
*/
|
|
62
|
-
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
63
100
|
export function redirect(location, status = 303, options = {}) {
|
|
64
101
|
// Checked here too, for JavaScript callers the types cannot help.
|
|
65
102
|
if (!REDIRECT_STATUSES.has(status))
|
|
66
103
|
throw new Error(`redirect(): ${String(status)} is not a redirect status. Use 301, 302, 303, 307 or 308.`);
|
|
67
104
|
return { pexisSignal: 'redirect', location, status, external: options.external === true };
|
|
68
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Whether a value is what notFound() or redirect() returns.
|
|
108
|
+
*
|
|
109
|
+
* @category plugins
|
|
110
|
+
* @param value Anything, such as what a page's load() returned.
|
|
111
|
+
*/
|
|
69
112
|
export function isSignal(value) {
|
|
70
113
|
return typeof value === 'object' && value !== null && 'pexisSignal' in value;
|
|
71
114
|
}
|
package/dist/page.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page.js","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAGA,MAAM,IAAI,GAAG,+GAA+G,CAAC;
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAGA,MAAM,IAAI,GAAG,+GAA+G,CAAC;AAkC7H,MAAM,UAAU,UAAU,CAAC,GAAoF;IAC7G,IAAI,CAAC,CAAC,MAAM,IAAI,GAAG,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAC/C,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC;IACpD,CAAC;IACD,OAAO,UAAU,CAAC,GAAsC,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,MAAM,CAAC,IAAY,EAAE,IAAyB;IAC5D,IAAI,IAAI,EAAE,IAAI,KAAK,MAAM;QAAE,MAAM,IAAI,KAAK,CAAC,sBAAsB,IAAI,yBAAyB,IAAI,CAAC,IAAI,+CAA+C,CAAC,CAAC;IACxJ,IAAI,IAAI,EAAE,IAAI,KAAK,eAAe;QAAE,MAAM,IAAI,KAAK,CAAC,sBAAsB,IAAI,0CAA0C,CAAC,CAAC;IAC1H,OAAO,UAAU,CAAC,EAAE,GAAG,IAAI,CAAC,UAAU,EAAE,IAAI,EAAqC,CAAC,CAAC;AACrF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,IAAY;IAClD,IAAI,CAAC,IAAI,CAAC,IAA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AE,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,GAAG,CAAC,CAAC;oBAC/D,sFAAsF;oBACtF,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBAC5D,CAAC;gBACD,IAAI,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;oBACtB,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;oBACjC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE;wBAAE,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,IAAI,GAAG,CAAC,CAAC;oBACzG,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAC;gBACnC,CAAC;gBACD,OAAO,CAAC,CAAC;YACX,CAAC,CAAC,CAAC;YACH,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO,GAAG,CAAC;YACpC,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;QACtD,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,UAAU,CAAC,CAAS;IAC3B,IAAI,CAAC;QACH,OAAO,kBAAkB,CAAC,CAAC,CAAC,CAAC;IAC/B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC"}
|
package/dist/rate-limit.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where rate-limit counts are kept. The default is in memory, per process;
|
|
3
|
+
* a shared store (such as `rateLimitStore(db)` from @pexis/data) counts across servers.
|
|
4
|
+
*
|
|
5
|
+
* @category plugins
|
|
6
|
+
*/
|
|
1
7
|
export interface RateLimitStore {
|
|
2
8
|
/** Counts one hit on key in the current window and returns the count and when the window ends (ms since epoch). */
|
|
3
9
|
hit(key: string, windowMs: number): Promise<{
|
|
@@ -10,17 +16,33 @@ export interface RateLimitStore {
|
|
|
10
16
|
resetAt: number;
|
|
11
17
|
} | null>;
|
|
12
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* How many hits a client may make in a window.
|
|
21
|
+
*
|
|
22
|
+
* @category plugins
|
|
23
|
+
*/
|
|
13
24
|
export interface RateLimitRule {
|
|
14
25
|
/** Most hits allowed per window. */
|
|
15
26
|
limit: number;
|
|
16
27
|
/** Window length, in seconds. */
|
|
17
28
|
windowSeconds: number;
|
|
18
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Whether a hit is within the limit.
|
|
32
|
+
*
|
|
33
|
+
* @category plugins
|
|
34
|
+
*/
|
|
19
35
|
export interface RateLimitDecision {
|
|
36
|
+
/** True when the client is within the limit. */
|
|
20
37
|
allowed: boolean;
|
|
21
38
|
/** Seconds until the window resets, for the Retry-After header. */
|
|
22
39
|
retryAfter: number;
|
|
23
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Counts hits per client and says whether each is within the limit.
|
|
43
|
+
*
|
|
44
|
+
* @category plugins
|
|
45
|
+
*/
|
|
24
46
|
export interface RateLimiter {
|
|
25
47
|
/** Counts a hit for this client and says whether it is within the limit. Always allowed with no address. */
|
|
26
48
|
hit(clientAddress: string | undefined, scope?: string): Promise<RateLimitDecision>;
|
|
@@ -33,14 +55,25 @@ export interface RateLimiter {
|
|
|
33
55
|
* dropped first, then the ones with the fewest hits: a flood of new
|
|
34
56
|
* addresses then pushes out clients who barely called, never the one close
|
|
35
57
|
* to its limit, whose count would otherwise reset.
|
|
58
|
+
*
|
|
59
|
+
* @category plugins
|
|
60
|
+
* @param options How many clients to track at most.
|
|
36
61
|
*/
|
|
37
62
|
export declare function memoryRateStore(options?: {
|
|
63
|
+
/**
|
|
64
|
+
* The most clients tracked at once.
|
|
65
|
+
*
|
|
66
|
+
* @default 100000
|
|
67
|
+
*/
|
|
38
68
|
maxKeys?: number;
|
|
39
69
|
}): RateLimitStore;
|
|
40
70
|
/**
|
|
41
71
|
* The part of an address that identifies one client. IPv6 clients usually
|
|
42
72
|
* hold a whole /64 and can switch addresses inside it at will, so they are
|
|
43
73
|
* counted by that prefix.
|
|
74
|
+
*
|
|
75
|
+
* @category plugins
|
|
76
|
+
* @param address An IPv4 or IPv6 address.
|
|
44
77
|
*/
|
|
45
78
|
export declare function clientKey(address: string): string;
|
|
46
79
|
/**
|
|
@@ -49,6 +82,19 @@ export declare function clientKey(address: string): string;
|
|
|
49
82
|
* trusted proxies the client is the Nth entry from the right, counting the
|
|
50
83
|
* socket's address. Entries further left were written by the client and can
|
|
51
84
|
* be forged, so they are never used.
|
|
85
|
+
*
|
|
86
|
+
* @category plugins
|
|
87
|
+
* @param socketAddress The address the connection came from.
|
|
88
|
+
* @param forwardedFor The X-Forwarded-For header, if any.
|
|
89
|
+
* @param trustedProxies How many proxies in front of the server append to it.
|
|
52
90
|
*/
|
|
53
91
|
export declare function forwardedClient(socketAddress: string | undefined, forwardedFor: string | null, trustedProxies: number): string | undefined;
|
|
92
|
+
/**
|
|
93
|
+
* A rate limiter for one rule, counting by client address (IPv6 by /64).
|
|
94
|
+
* Throws when the rule's numbers are not positive.
|
|
95
|
+
*
|
|
96
|
+
* @category plugins
|
|
97
|
+
* @param rule How many hits per window.
|
|
98
|
+
* @param store Where counts are kept. Default: in memory, per process.
|
|
99
|
+
*/
|
|
54
100
|
export declare function rateLimiter(rule: RateLimitRule, store?: RateLimitStore): RateLimiter;
|
package/dist/rate-limit.js
CHANGED
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
* dropped first, then the ones with the fewest hits: a flood of new
|
|
14
14
|
* addresses then pushes out clients who barely called, never the one close
|
|
15
15
|
* to its limit, whose count would otherwise reset.
|
|
16
|
+
*
|
|
17
|
+
* @category plugins
|
|
18
|
+
* @param options How many clients to track at most.
|
|
16
19
|
*/
|
|
17
20
|
export function memoryRateStore(options = {}) {
|
|
18
21
|
const maxKeys = options.maxKeys ?? 100_000;
|
|
@@ -52,6 +55,9 @@ export function memoryRateStore(options = {}) {
|
|
|
52
55
|
* The part of an address that identifies one client. IPv6 clients usually
|
|
53
56
|
* hold a whole /64 and can switch addresses inside it at will, so they are
|
|
54
57
|
* counted by that prefix.
|
|
58
|
+
*
|
|
59
|
+
* @category plugins
|
|
60
|
+
* @param address An IPv4 or IPv6 address.
|
|
55
61
|
*/
|
|
56
62
|
export function clientKey(address) {
|
|
57
63
|
const plain = address.trim().toLowerCase().replace(/^\[|\]$/g, '');
|
|
@@ -75,6 +81,11 @@ export function clientKey(address) {
|
|
|
75
81
|
* trusted proxies the client is the Nth entry from the right, counting the
|
|
76
82
|
* socket's address. Entries further left were written by the client and can
|
|
77
83
|
* be forged, so they are never used.
|
|
84
|
+
*
|
|
85
|
+
* @category plugins
|
|
86
|
+
* @param socketAddress The address the connection came from.
|
|
87
|
+
* @param forwardedFor The X-Forwarded-For header, if any.
|
|
88
|
+
* @param trustedProxies How many proxies in front of the server append to it.
|
|
78
89
|
*/
|
|
79
90
|
export function forwardedClient(socketAddress, forwardedFor, trustedProxies) {
|
|
80
91
|
if (!socketAddress || trustedProxies <= 0)
|
|
@@ -90,6 +101,14 @@ export function forwardedClient(socketAddress, forwardedFor, trustedProxies) {
|
|
|
90
101
|
// furthest address we have rather than guess.
|
|
91
102
|
return chain[Math.max(0, chain.length - 1 - trustedProxies)];
|
|
92
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* A rate limiter for one rule, counting by client address (IPv6 by /64).
|
|
106
|
+
* Throws when the rule's numbers are not positive.
|
|
107
|
+
*
|
|
108
|
+
* @category plugins
|
|
109
|
+
* @param rule How many hits per window.
|
|
110
|
+
* @param store Where counts are kept. Default: in memory, per process.
|
|
111
|
+
*/
|
|
93
112
|
export function rateLimiter(rule, store = memoryRateStore()) {
|
|
94
113
|
if (!(rule.limit > 0) || !(rule.windowSeconds > 0))
|
|
95
114
|
throw new Error('rateLimiter: limit and windowSeconds must be positive.');
|
package/dist/rate-limit.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,EAAE;AACF,+EAA+E;AAC/E,wEAAwE;AACxE,2EAA2E;AAC3E,oDAAoD;AACpD,EAAE;AACF,8EAA8E;AAC9E,2EAA2E;
|
|
1
|
+
{"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,EAAE;AACF,+EAA+E;AAC/E,wEAAwE;AACxE,2EAA2E;AAC3E,oDAAoD;AACpD,EAAE;AACF,8EAA8E;AAC9E,2EAA2E;AAmD3E;;;;;;;;;GASG;AACH,MAAM,UAAU,eAAe,CAAC,OAAO,GAOnC,EAAE;IACJ,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC;IAC3C,MAAM,OAAO,GAAG,IAAI,GAAG,EAA8C,CAAC;IACtE,OAAO;QACL,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ;YACrB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACvB,IAAI,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC7B,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC;gBACnC,IAAI,KAAK;oBAAE,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBAC/B,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,EAAE,CAAC;oBAC5B,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,OAAO;wBAAE,IAAI,CAAC,CAAC,OAAO,IAAI,GAAG;4BAAE,OAAO,CAA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,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;YAC5D,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,IAAI,SAAS,CAAC,aAAa,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;YAC7F,OAAO,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAChC,CAAC;QACD,KAAK,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,GAAG,EAAE;YACnC,IAAI,CAAC,aAAa;gBAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;YAC5D,MAAM,KAAK,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,IAAI,SAAS,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;YACtE,gEAAgE;YAChE,OAAO,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;QAC3F,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/registry.d.ts
CHANGED
|
@@ -1,16 +1,39 @@
|
|
|
1
1
|
import { type Fields, type Infer, type ObjectSchema } from './schema.js';
|
|
2
2
|
import type { Context } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* How much an action changes. `read` changes nothing and is sent as a GET
|
|
5
|
+
* form; `write` changes data; `irreversible` cannot be undone, so people
|
|
6
|
+
* confirm it on the page and agents must get the person's approval.
|
|
7
|
+
*/
|
|
3
8
|
export type Risk = 'read' | 'write' | 'irreversible';
|
|
4
9
|
/** Actions receive the full request context, including fields plugins add. */
|
|
5
10
|
export type ActionContext = Context;
|
|
11
|
+
/** What an action's `run` returns when it succeeds. */
|
|
6
12
|
export interface ActionResult {
|
|
7
13
|
/** Shown to a person and returned to an agent. */
|
|
8
14
|
message: string;
|
|
15
|
+
/** Structured data for agents and scripts, returned as `data` in the JSON reply. People see only the message. */
|
|
9
16
|
data?: unknown;
|
|
17
|
+
/**
|
|
18
|
+
* One link to show with the message, such as the page for what was just
|
|
19
|
+
* made: a path on this site, or a URL on its own origin. Anything else is
|
|
20
|
+
* dropped and logged, never rendered. People see it with the message;
|
|
21
|
+
* agents get it as an absolute URL.
|
|
22
|
+
*/
|
|
23
|
+
link?: ResultLink;
|
|
24
|
+
}
|
|
25
|
+
/** A link in an action's result. */
|
|
26
|
+
export interface ResultLink {
|
|
27
|
+
/** A path on this site, or a URL on its own origin. */
|
|
28
|
+
href: string;
|
|
29
|
+
/** The link's text. */
|
|
30
|
+
text: string;
|
|
10
31
|
}
|
|
11
32
|
/** Values run() refused, from invalid(). */
|
|
12
33
|
export interface Invalid {
|
|
34
|
+
/** Marks the object as a refusal from invalid(). */
|
|
13
35
|
readonly pexisInvalid: true;
|
|
36
|
+
/** One message per field name. */
|
|
14
37
|
readonly errors: Readonly<Record<string, string>>;
|
|
15
38
|
}
|
|
16
39
|
/**
|
|
@@ -19,10 +42,15 @@ export interface Invalid {
|
|
|
19
42
|
* these errors, and a person sees the form again with each message under its
|
|
20
43
|
* field. Keys are field names.
|
|
21
44
|
*
|
|
22
|
-
*
|
|
45
|
+
* @param errors One message per field name; at least one.
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* if (input.pickup < today()) return invalid({ pickup: 'Choose a date from tomorrow on.' });
|
|
49
|
+
* ```
|
|
23
50
|
*/
|
|
24
51
|
export declare function invalid(errors: Record<string, string>): Invalid;
|
|
25
52
|
export declare function isInvalid(value: unknown): value is Invalid;
|
|
53
|
+
/** What defineAction() takes: everything an action is, declared once. */
|
|
26
54
|
export interface ActionDefinition<F extends Fields> {
|
|
27
55
|
/** Tool name: lowercase letters, digits and underscores, starting with a letter. */
|
|
28
56
|
name: string;
|
|
@@ -30,7 +58,14 @@ export interface ActionDefinition<F extends Fields> {
|
|
|
30
58
|
title: string;
|
|
31
59
|
/** What the action does and what it returns, written for a model to read. */
|
|
32
60
|
description: string;
|
|
61
|
+
/**
|
|
62
|
+
* The fields the action takes, by name, made with `field.*` or a custom field
|
|
63
|
+
* kind. They become the form's controls, the tool's input schema and the
|
|
64
|
+
* typed input `run` receives. Use `{}` for an action that takes no input,
|
|
65
|
+
* shown as a single button.
|
|
66
|
+
*/
|
|
33
67
|
input: F;
|
|
68
|
+
/** How much the action changes: `read`, `write` or `irreversible`. Sets the form's method and whether people and agents must confirm. */
|
|
34
69
|
risk: Risk;
|
|
35
70
|
/** Require the user's approval before an agent submits. Always true for irreversible actions. */
|
|
36
71
|
confirm?: boolean;
|
|
@@ -53,13 +88,29 @@ export interface ActionDefinition<F extends Fields> {
|
|
|
53
88
|
* (see @pexis/approvals). The undo is another registered action, run as the
|
|
54
89
|
* person. It must be idempotent, and should check the thing is still as this
|
|
55
90
|
* write left it. A write with outside effects (mail, payments) is
|
|
56
|
-
* irreversible, or its undo must compensate.
|
|
91
|
+
* irreversible, or its undo must compensate.
|
|
92
|
+
*
|
|
93
|
+
* @experimental
|
|
57
94
|
*/
|
|
58
95
|
undo?: UndoDefinition<F>;
|
|
96
|
+
/**
|
|
97
|
+
* Who may run the action, checked on the server before every call from a
|
|
98
|
+
* form, an agent or `ctx.call`. Return false to refuse with 403. An action
|
|
99
|
+
* needs this or `public: true`.
|
|
100
|
+
*/
|
|
59
101
|
permission?(ctx: ActionContext): boolean | Promise<boolean>;
|
|
102
|
+
/**
|
|
103
|
+
* Does the work, with input already validated against the fields. Returns a
|
|
104
|
+
* result with a message, or invalid() to refuse values with a message per
|
|
105
|
+
* field. Throwing answers 500 without showing the error.
|
|
106
|
+
*/
|
|
60
107
|
run(input: Infer<F>, ctx: ActionContext): ActionResult | Invalid | Promise<ActionResult | Invalid>;
|
|
61
108
|
}
|
|
62
|
-
/**
|
|
109
|
+
/**
|
|
110
|
+
* How a write is undone.
|
|
111
|
+
*
|
|
112
|
+
* @experimental
|
|
113
|
+
*/
|
|
63
114
|
export interface UndoDefinition<F extends Fields> {
|
|
64
115
|
/** The registered action that reverses this one, such as cancel_booking. */
|
|
65
116
|
action: Action<any>;
|
|
@@ -72,28 +123,100 @@ export interface UndoDefinition<F extends Fields> {
|
|
|
72
123
|
/** How long the person can undo it, in hours. Default 24, at most 168. */
|
|
73
124
|
hours?: number;
|
|
74
125
|
}
|
|
126
|
+
/** An action, as defineAction() returns it: the definition with its defaults filled in. */
|
|
75
127
|
export interface Action<F extends Fields = Fields> extends ActionDefinition<F> {
|
|
128
|
+
/** Whether an agent needs the person's approval first: as declared, and always true for irreversible actions. */
|
|
76
129
|
confirm: boolean;
|
|
130
|
+
/** The form's method: GET for `read` actions, POST otherwise. */
|
|
77
131
|
method: 'GET' | 'POST';
|
|
78
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* An action as the tool description agents read, in `/pexis/actions.json`,
|
|
135
|
+
* MCP and the page's own forms.
|
|
136
|
+
*
|
|
137
|
+
* @category plugins
|
|
138
|
+
*/
|
|
79
139
|
export interface ToolDescriptor {
|
|
140
|
+
/** The action's name. */
|
|
80
141
|
name: string;
|
|
142
|
+
/** The action's title. */
|
|
81
143
|
title: string;
|
|
144
|
+
/** What the action does, for a model to read. */
|
|
82
145
|
description: string;
|
|
146
|
+
/** How much the action changes. */
|
|
83
147
|
risk: Risk;
|
|
148
|
+
/** Whether the agent must get the person's approval first. */
|
|
84
149
|
confirm: boolean;
|
|
150
|
+
/** The input as JSON Schema (draft 2020-12). */
|
|
85
151
|
inputSchema: ObjectSchema;
|
|
86
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Declares an action once: its name, input fields, risk, who may run it, and
|
|
155
|
+
* what it does. The same definition becomes the form people fill in, the tool
|
|
156
|
+
* agents call (in `/pexis/actions.json`, MCP and the page), the JSON endpoint
|
|
157
|
+
* `POST /actions/<name>` and an OpenAPI operation. The server validates every
|
|
158
|
+
* call against the fields and checks the permission before `run`.
|
|
159
|
+
*
|
|
160
|
+
* Throws when the definition is invalid: a bad name, an empty title or
|
|
161
|
+
* description, `public` together with `permission`, a field that asks for a
|
|
162
|
+
* password or a file, or a broken undo.
|
|
163
|
+
*
|
|
164
|
+
* @param def The action: name, title, description, input, risk, and run.
|
|
165
|
+
* @returns The action, with `confirm` and `method` filled in.
|
|
166
|
+
* @example
|
|
167
|
+
* A booking form
|
|
168
|
+
* ```ts
|
|
169
|
+
* export default defineAction({
|
|
170
|
+
* name: 'book_table',
|
|
171
|
+
* title: 'Book a table',
|
|
172
|
+
* description: 'Reserves a table. Returns a booking reference.',
|
|
173
|
+
* risk: 'write',
|
|
174
|
+
* public: true,
|
|
175
|
+
* input: {
|
|
176
|
+
* name: field.text({ label: 'Name', max: 80 }),
|
|
177
|
+
* guests: field.integer({ label: 'Guests', min: 1, max: 12 }),
|
|
178
|
+
* },
|
|
179
|
+
* run: async (input, ctx) => ({ message: `Booked for ${input.guests}, ${input.name}.` }),
|
|
180
|
+
* });
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
87
183
|
export declare function defineAction<F extends Fields>(def: ActionDefinition<F>): Action<F>;
|
|
88
184
|
/**
|
|
89
185
|
* Checks an action file found in actions/: it must default-export one action,
|
|
90
186
|
* named like the file. Used by the generated pexis.gen.ts.
|
|
187
|
+
*
|
|
188
|
+
* @category plugins
|
|
189
|
+
* @param file The file's path, for error messages.
|
|
190
|
+
* @param name The action name the file's name implies.
|
|
191
|
+
* @param value The file's default export.
|
|
192
|
+
* @returns The action, when it is one and its name matches.
|
|
91
193
|
*/
|
|
92
194
|
export declare function actionFile(file: string, name: string, value: unknown): Action;
|
|
195
|
+
/**
|
|
196
|
+
* An action as the tool description agents read, with its input as JSON Schema.
|
|
197
|
+
*
|
|
198
|
+
* @category plugins
|
|
199
|
+
* @param action A defined action.
|
|
200
|
+
*/
|
|
93
201
|
export declare function describeAction(action: Action): ToolDescriptor;
|
|
202
|
+
/**
|
|
203
|
+
* The registry of actions behind createApp: one place every action is
|
|
204
|
+
* declared, looked up and described.
|
|
205
|
+
*
|
|
206
|
+
* @category plugins
|
|
207
|
+
*/
|
|
94
208
|
export interface Registry {
|
|
209
|
+
/** The action with this name, or undefined. */
|
|
95
210
|
get(name: string): Action | undefined;
|
|
211
|
+
/** Every action, in the order given. */
|
|
96
212
|
list(): readonly Action[];
|
|
213
|
+
/** The tool descriptions of the actions offered to agents (not `agents: false` ones). */
|
|
97
214
|
describe(): ToolDescriptor[];
|
|
98
215
|
}
|
|
216
|
+
/**
|
|
217
|
+
* Builds a registry from actions. Throws when two share a name.
|
|
218
|
+
*
|
|
219
|
+
* @category plugins
|
|
220
|
+
* @param actions The app's actions.
|
|
221
|
+
*/
|
|
99
222
|
export declare function createRegistry(actions: readonly Action[]): Registry;
|
package/dist/registry.js
CHANGED
|
@@ -6,7 +6,11 @@ import { toJsonSchema } from './schema.js';
|
|
|
6
6
|
* these errors, and a person sees the form again with each message under its
|
|
7
7
|
* field. Keys are field names.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* @param errors One message per field name; at least one.
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* if (input.pickup < today()) return invalid({ pickup: 'Choose a date from tomorrow on.' });
|
|
13
|
+
* ```
|
|
10
14
|
*/
|
|
11
15
|
export function invalid(errors) {
|
|
12
16
|
if (Object.keys(errors).length === 0)
|
|
@@ -41,6 +45,36 @@ function forbiddenInput(f, name) {
|
|
|
41
45
|
return null;
|
|
42
46
|
}
|
|
43
47
|
const RISKS = ['read', 'write', 'irreversible'];
|
|
48
|
+
/**
|
|
49
|
+
* Declares an action once: its name, input fields, risk, who may run it, and
|
|
50
|
+
* what it does. The same definition becomes the form people fill in, the tool
|
|
51
|
+
* agents call (in `/pexis/actions.json`, MCP and the page), the JSON endpoint
|
|
52
|
+
* `POST /actions/<name>` and an OpenAPI operation. The server validates every
|
|
53
|
+
* call against the fields and checks the permission before `run`.
|
|
54
|
+
*
|
|
55
|
+
* Throws when the definition is invalid: a bad name, an empty title or
|
|
56
|
+
* description, `public` together with `permission`, a field that asks for a
|
|
57
|
+
* password or a file, or a broken undo.
|
|
58
|
+
*
|
|
59
|
+
* @param def The action: name, title, description, input, risk, and run.
|
|
60
|
+
* @returns The action, with `confirm` and `method` filled in.
|
|
61
|
+
* @example
|
|
62
|
+
* A booking form
|
|
63
|
+
* ```ts
|
|
64
|
+
* export default defineAction({
|
|
65
|
+
* name: 'book_table',
|
|
66
|
+
* title: 'Book a table',
|
|
67
|
+
* description: 'Reserves a table. Returns a booking reference.',
|
|
68
|
+
* risk: 'write',
|
|
69
|
+
* public: true,
|
|
70
|
+
* input: {
|
|
71
|
+
* name: field.text({ label: 'Name', max: 80 }),
|
|
72
|
+
* guests: field.integer({ label: 'Guests', min: 1, max: 12 }),
|
|
73
|
+
* },
|
|
74
|
+
* run: async (input, ctx) => ({ message: `Booked for ${input.guests}, ${input.name}.` }),
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
44
78
|
export function defineAction(def) {
|
|
45
79
|
if (!NAME.test(def.name)) {
|
|
46
80
|
throw new Error(`Action name "${def.name}" must start with a lowercase letter and use only lowercase letters, digits and underscores (64 characters at most).`);
|
|
@@ -106,6 +140,12 @@ export function defineAction(def) {
|
|
|
106
140
|
/**
|
|
107
141
|
* Checks an action file found in actions/: it must default-export one action,
|
|
108
142
|
* named like the file. Used by the generated pexis.gen.ts.
|
|
143
|
+
*
|
|
144
|
+
* @category plugins
|
|
145
|
+
* @param file The file's path, for error messages.
|
|
146
|
+
* @param name The action name the file's name implies.
|
|
147
|
+
* @param value The file's default export.
|
|
148
|
+
* @returns The action, when it is one and its name matches.
|
|
109
149
|
*/
|
|
110
150
|
export function actionFile(file, name, value) {
|
|
111
151
|
const action = value;
|
|
@@ -117,6 +157,12 @@ export function actionFile(file, name, value) {
|
|
|
117
157
|
}
|
|
118
158
|
return action;
|
|
119
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* An action as the tool description agents read, with its input as JSON Schema.
|
|
162
|
+
*
|
|
163
|
+
* @category plugins
|
|
164
|
+
* @param action A defined action.
|
|
165
|
+
*/
|
|
120
166
|
export function describeAction(action) {
|
|
121
167
|
return {
|
|
122
168
|
name: action.name,
|
|
@@ -127,6 +173,12 @@ export function describeAction(action) {
|
|
|
127
173
|
inputSchema: toJsonSchema(action.input),
|
|
128
174
|
};
|
|
129
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Builds a registry from actions. Throws when two share a name.
|
|
178
|
+
*
|
|
179
|
+
* @category plugins
|
|
180
|
+
* @param actions The app's actions.
|
|
181
|
+
*/
|
|
130
182
|
export function createRegistry(actions) {
|
|
131
183
|
const byName = new Map();
|
|
132
184
|
for (const action of actions) {
|