@pexis/core 0.1.1 → 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 +65 -15
- package/API.md +11 -8
- package/README.md +1 -1
- package/api.json +5951 -0
- package/dist/app.d.ts +77 -3
- package/dist/app.js +284 -38
- package/dist/app.js.map +1 -1
- package/dist/components.d.ts +100 -6
- package/dist/components.js +87 -19
- 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 +3 -3
- package/dist/index.js +1 -1
- 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/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { agentRefusal, createApp, definePlugin, jsonLd, speculationRules, TOOLS_PATH } from './app.js';
|
|
2
2
|
export { definePage, defineLayout, notFound, redirect, isSignal, atPath } from './page.js';
|
|
3
3
|
export { getContext, peekContext, setContextStorage, runWithContext } from './context.js';
|
|
4
|
-
export { ActionForm, HumanOnly, Untrusted, hasInPlaceForms } from './components.js';
|
|
4
|
+
export { ActionForm, HumanOnly, Region, Untrusted, hasInPlaceForms } from './components.js';
|
|
5
5
|
export { css, globalCss, minifyCss, isStyleSheet } from './styles.js';
|
|
6
6
|
export { crossSiteRefusal } from './csrf.js';
|
|
7
7
|
export { createCors } from './cors.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,gBAAgB,EAAE,UAAU,EAAqD,MAAM,UAAU,CAAC;AAC1J,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAqB,MAAM,WAAW,CAAC;AAC9G,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,iBAAiB,EAAE,cAAc,EAAuB,MAAM,cAAc,CAAC;AAC/G,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,gBAAgB,EAAE,UAAU,EAAqD,MAAM,UAAU,CAAC;AAC1J,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAqB,MAAM,WAAW,CAAC;AAC9G,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,iBAAiB,EAAE,cAAc,EAAuB,MAAM,cAAc,CAAC;AAC/G,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,eAAe,EAA+D,MAAM,iBAAiB,CAAC;AACzJ,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAqC,MAAM,aAAa,CAAC;AACzG,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,YAAY,EAA2C,MAAM,eAAe,CAAC;AAC3K,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,eAAe,EAAE,WAAW,EAAqF,MAAM,iBAAiB,CAAC;AAC9K,OAAO,EACL,KAAK,EACL,YAAY,EACZ,WAAW,EACX,QAAQ,EAeR,eAAe,GAIhB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,YAAY,EACZ,OAAO,EACP,UAAU,EACV,cAAc,EACd,cAAc,GAWf,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,UAAU,EAAE,YAAY,EAAE,cAAc,EAAE,UAAU,EAA8B,MAAM,WAAW,CAAC;AAExH,OAAO,EAAE,UAAU,EAAoC,MAAM,WAAW,CAAC"}
|
package/dist/jsx-runtime.d.ts
CHANGED
|
@@ -2,14 +2,35 @@ import { type Markup, type SafeHtml } from './html.js';
|
|
|
2
2
|
import { type StyleSheet } from './styles.js';
|
|
3
3
|
import { BLOCKED_URL, isScriptUrl } from './urls.js';
|
|
4
4
|
export { BLOCKED_URL, isScriptUrl };
|
|
5
|
+
/**
|
|
6
|
+
* What JSX children can be: markup, text and numbers (escaped), lists of
|
|
7
|
+
* them, and nothing (null, undefined, booleans).
|
|
8
|
+
*/
|
|
5
9
|
export type Child = Markup | string | number | bigint | boolean | null | undefined | readonly Child[];
|
|
6
10
|
type Props = Record<string, unknown> & {
|
|
7
11
|
children?: unknown;
|
|
8
12
|
};
|
|
9
13
|
type Component = (props: never) => Markup | null | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Groups children without an element around them: `<>...</>`.
|
|
16
|
+
*
|
|
17
|
+
* @param props The children.
|
|
18
|
+
*/
|
|
10
19
|
export declare function Fragment(props: {
|
|
20
|
+
/** What the fragment holds. */
|
|
11
21
|
children?: Child;
|
|
12
22
|
}): Markup;
|
|
23
|
+
/**
|
|
24
|
+
* The JSX runtime's element function, which the compiler calls for every
|
|
25
|
+
* element: renders an HTML element with escaped attributes and children, or
|
|
26
|
+
* calls a component. Apps never call it directly; set `jsxImportSource` to
|
|
27
|
+
* "@pexis/core". `jsxs` and `jsxDEV` are the same function.
|
|
28
|
+
*
|
|
29
|
+
* @category plugins
|
|
30
|
+
* @param type A tag name or a component.
|
|
31
|
+
* @param props Attributes and children, or the component's props.
|
|
32
|
+
* @param _key Ignored: there is no client-side diffing to use it.
|
|
33
|
+
*/
|
|
13
34
|
export declare function jsx(type: string | Component, props: Props, _key?: unknown): Markup;
|
|
14
35
|
export { jsx as jsxs, jsx as jsxDEV };
|
|
15
36
|
type AttrValue = string | number | boolean | null | undefined;
|
|
@@ -449,15 +470,26 @@ interface Elements {
|
|
|
449
470
|
};
|
|
450
471
|
wbr: Plain;
|
|
451
472
|
}
|
|
473
|
+
/**
|
|
474
|
+
* The JSX types TypeScript checks markup against: HTML elements with their
|
|
475
|
+
* own attributes, where attribute typos are errors, and custom elements.
|
|
476
|
+
*
|
|
477
|
+
* @category plugins
|
|
478
|
+
*/
|
|
452
479
|
export declare namespace JSX {
|
|
480
|
+
/** What an element expression produces: markup. */
|
|
453
481
|
type Element = Markup;
|
|
482
|
+
/** What may be used as an element: a known tag, or a component. */
|
|
454
483
|
type ElementType = keyof IntrinsicElements | ((props: any) => Markup | null | undefined);
|
|
484
|
+
/** Tells TypeScript that children are passed as the `children` prop. */
|
|
455
485
|
interface ElementChildrenAttribute {
|
|
456
486
|
children: unknown;
|
|
457
487
|
}
|
|
488
|
+
/** Attributes every element accepts. */
|
|
458
489
|
interface IntrinsicAttributes {
|
|
459
490
|
key?: string | number;
|
|
460
491
|
}
|
|
492
|
+
/** Every HTML element and its attributes, plus custom elements with a hyphen in their name. */
|
|
461
493
|
type IntrinsicElements = Elements & {
|
|
462
494
|
[tag: `${string}-${string}`]: GlobalAttributes & Record<string, unknown>;
|
|
463
495
|
};
|
package/dist/jsx-runtime.js
CHANGED
|
@@ -150,9 +150,25 @@ function element(tag, props) {
|
|
|
150
150
|
const inner = renderChildren(props.children);
|
|
151
151
|
return isSafeHtml(inner) ? raw(`${open}${inner.value}</${tag}>`) : handled(inner.then((c) => raw(`${open}${c.value}</${tag}>`)));
|
|
152
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Groups children without an element around them: `<>...</>`.
|
|
155
|
+
*
|
|
156
|
+
* @param props The children.
|
|
157
|
+
*/
|
|
153
158
|
export function Fragment(props) {
|
|
154
159
|
return renderChildren(props.children);
|
|
155
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* The JSX runtime's element function, which the compiler calls for every
|
|
163
|
+
* element: renders an HTML element with escaped attributes and children, or
|
|
164
|
+
* calls a component. Apps never call it directly; set `jsxImportSource` to
|
|
165
|
+
* "@pexis/core". `jsxs` and `jsxDEV` are the same function.
|
|
166
|
+
*
|
|
167
|
+
* @category plugins
|
|
168
|
+
* @param type A tag name or a component.
|
|
169
|
+
* @param props Attributes and children, or the component's props.
|
|
170
|
+
* @param _key Ignored: there is no client-side diffing to use it.
|
|
171
|
+
*/
|
|
156
172
|
export function jsx(type, props, _key) {
|
|
157
173
|
if (typeof type === 'function') {
|
|
158
174
|
const result = type(props);
|
package/dist/jsx-runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsx-runtime.js","sourceRoot":"","sources":["../src/jsx-runtime.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,8EAA8E;AAC9E,6EAA6E;AAC7E,uCAAuC;AACvC,EAAE;AACF,qEAAqE;AAErE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,EAA8B,MAAM,WAAW,CAAC;AACpF,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAmB,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAErE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC;AAEpC,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;AAC7H,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;AAC9C,MAAM,GAAG,GAAG,mBAAmB,CAAC;AAChC,MAAM,SAAS,GAAG,8BAA8B,CAAC;AACjD,0EAA0E;AAC1E,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,YAAY,EAAE,iBAAiB,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"jsx-runtime.js","sourceRoot":"","sources":["../src/jsx-runtime.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,8EAA8E;AAC9E,6EAA6E;AAC7E,uCAAuC;AACvC,EAAE;AACF,qEAAqE;AAErE,OAAO,EAAE,UAAU,EAAE,UAAU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,CAAC,CAAC,CAAC,MAAM,CAAC;IAC9D,CAAC;IACD,OAAO,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC9B,CAAC;AAED,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE,GAAG,IAAI,MAAM,EAAE,CAAC"}
|
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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
|
|
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,IA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