@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/registry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAChC,OAAO,EAAE,YAAY,EAA0D,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAChC,OAAO,EAAE,YAAY,EAA0D,MAAM,aAAa,CAAC;AA4CnG;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO,CAAC,MAA8B;IACpD,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,+DAA+D,CAAC,CAAC;IACvH,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,MAAM,EAAE,EAAE,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAK,KAAoC,CAAC,YAAY,KAAK,IAAI,CAAC;AACpH,CAAC;AA6GD,MAAM,IAAI,GAAG,wBAAwB,CAAC;AACtC,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,kBAAkB,EAAE,cAAc,EAAE,eAAe,CAAC,CAAC,CAAC;AAEtF;;;;GAIG;AACH,SAAS,cAAc,CAAC,CAAQ,EAAE,IAAY;IAC5C,IAAI,CAAC,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,CAAC,YAAY,IAAI,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,cAAc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CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,KAAK,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC;KAC3F,CAAC;AACJ,CAAC"}
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,54 +1,98 @@
|
|
|
1
1
|
import type { SafeHtml } from './html.js';
|
|
2
|
+
/** What every field kind takes. */
|
|
2
3
|
export interface FieldBase {
|
|
4
|
+
/** The control's label, shown to people and used as the input's title for agents. */
|
|
3
5
|
label: string;
|
|
6
|
+
/** Hint text shown under the label, and the input's description for agents. */
|
|
4
7
|
description?: string;
|
|
8
|
+
/** The field may be left empty. Fields are required unless this is true. */
|
|
5
9
|
optional?: boolean;
|
|
6
10
|
}
|
|
11
|
+
/** A text input, or a textarea with `multiline`. The value is a string. */
|
|
7
12
|
export interface TextField extends FieldBase {
|
|
13
|
+
/** Set by field.text(). */
|
|
8
14
|
kind: 'text';
|
|
15
|
+
/** The fewest characters (minlength). Counted as people count them, so an emoji is one. */
|
|
9
16
|
min?: number;
|
|
17
|
+
/** The most characters (maxlength). */
|
|
10
18
|
max?: number;
|
|
19
|
+
/** An email address, a full http(s) web address, or a telephone number. Sets the input's type. A URL must start with http:// or https://. */
|
|
11
20
|
format?: 'email' | 'url' | 'tel';
|
|
12
21
|
/** HTML pattern semantics: must match the whole value. */
|
|
13
22
|
pattern?: string;
|
|
23
|
+
/** A textarea instead of a one-line input. */
|
|
14
24
|
multiline?: boolean;
|
|
15
25
|
/** Passed to the HTML autocomplete attribute. Not part of the schema. */
|
|
16
26
|
autocomplete?: string;
|
|
17
27
|
}
|
|
28
|
+
/** A whole number, as a number input. The value is a number. */
|
|
18
29
|
export interface IntegerField extends FieldBase {
|
|
30
|
+
/** Set by field.integer(). */
|
|
19
31
|
kind: 'integer';
|
|
32
|
+
/** The smallest value allowed. */
|
|
20
33
|
min?: number;
|
|
34
|
+
/** The largest value allowed. */
|
|
21
35
|
max?: number;
|
|
22
36
|
}
|
|
37
|
+
/** Any number, decimals included, as a number input. The value is a number. */
|
|
23
38
|
export interface NumberField extends FieldBase {
|
|
39
|
+
/** Set by field.number(). */
|
|
24
40
|
kind: 'number';
|
|
41
|
+
/** The smallest value allowed. */
|
|
25
42
|
min?: number;
|
|
43
|
+
/** The largest value allowed. */
|
|
26
44
|
max?: number;
|
|
27
45
|
}
|
|
46
|
+
/** A date input. The value is an ISO date string, YYYY-MM-DD. */
|
|
28
47
|
export interface DateField extends FieldBase {
|
|
48
|
+
/** Set by field.date(). */
|
|
29
49
|
kind: 'date';
|
|
30
50
|
/** ISO date, YYYY-MM-DD. */
|
|
31
51
|
min?: string;
|
|
52
|
+
/** The latest date allowed, YYYY-MM-DD. */
|
|
32
53
|
max?: string;
|
|
33
54
|
}
|
|
55
|
+
/** One option of a choice or choices field. */
|
|
34
56
|
export interface ChoiceOption<V extends string = string> {
|
|
57
|
+
/** What is sent and what agents choose: the value `run` receives. */
|
|
35
58
|
value: V;
|
|
59
|
+
/** What people see. */
|
|
36
60
|
label: string;
|
|
37
61
|
}
|
|
62
|
+
/** One of a list of options, as a select. The value is the chosen option's value. */
|
|
38
63
|
export interface ChoiceField<V extends string = string> extends FieldBase {
|
|
64
|
+
/** Set by field.choice(). */
|
|
39
65
|
kind: 'choice';
|
|
66
|
+
/** The options, in order. */
|
|
40
67
|
options: readonly ChoiceOption<V>[];
|
|
41
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Any number of options, as a group of checkboxes sharing one name. Like a
|
|
71
|
+
* single checkbox, none checked is a valid answer (an empty list), because
|
|
72
|
+
* HTML cannot require "at least one" of a group without a script.
|
|
73
|
+
*/
|
|
74
|
+
export interface ChoicesField<V extends string = string> extends Omit<FieldBase, 'optional'> {
|
|
75
|
+
/** Set by field.choices(). */
|
|
76
|
+
kind: 'choices';
|
|
77
|
+
/** The options, in order; at least two. */
|
|
78
|
+
options: readonly ChoiceOption<V>[];
|
|
79
|
+
}
|
|
80
|
+
/** A checkbox. The value is true when checked and false otherwise, so it is never required. */
|
|
42
81
|
export interface BooleanField extends Omit<FieldBase, 'optional'> {
|
|
82
|
+
/** Set by field.boolean(). */
|
|
43
83
|
kind: 'boolean';
|
|
44
84
|
}
|
|
45
85
|
/** What a custom field kind gets to render its control. */
|
|
46
86
|
export interface ControlProps {
|
|
87
|
+
/** The control's id, which the label points at. */
|
|
47
88
|
id: string;
|
|
89
|
+
/** The control's name: the field's name, sent with the form. */
|
|
48
90
|
name: string;
|
|
49
91
|
/** The value to show (after a failed submission, for example). */
|
|
50
92
|
value: unknown;
|
|
93
|
+
/** Whether the field is required. */
|
|
51
94
|
required: boolean;
|
|
95
|
+
/** Whether the last submission had an error for this field. */
|
|
52
96
|
invalid: boolean;
|
|
53
97
|
/**
|
|
54
98
|
* id, name, px-description, aria-describedby, aria-invalid and required,
|
|
@@ -70,6 +114,7 @@ export interface FieldKind<O extends FieldBase = FieldBase, V = unknown> {
|
|
|
70
114
|
name: string;
|
|
71
115
|
/** JSON Schema for the value, without title and description (added for you). */
|
|
72
116
|
schema(field: O): JsonSchemaProperty;
|
|
117
|
+
/** Renders the control: a standard input, select or textarea carrying `props.attributes`. */
|
|
73
118
|
control(field: O, props: ControlProps): SafeHtml;
|
|
74
119
|
/** Turns raw input into a value. Only called with a non-empty value. */
|
|
75
120
|
parse(raw: unknown, field: O): {
|
|
@@ -86,15 +131,34 @@ export interface FieldKind<O extends FieldBase = FieldBase, V = unknown> {
|
|
|
86
131
|
*/
|
|
87
132
|
group?: boolean;
|
|
88
133
|
}
|
|
134
|
+
/** A field of a kind made with defineFieldKind(). */
|
|
89
135
|
export interface CustomField<O extends FieldBase = FieldBase, V = unknown> extends FieldBase {
|
|
136
|
+
/** Set by the function defineFieldKind() returns. */
|
|
90
137
|
kind: 'custom';
|
|
138
|
+
/** The kind: how it renders, its schema and how it parses. */
|
|
91
139
|
fieldKind: FieldKind<O, V>;
|
|
92
140
|
}
|
|
93
|
-
|
|
94
|
-
|
|
141
|
+
/** Any field: one of the built-in kinds, or a custom one. */
|
|
142
|
+
export type Field = TextField | IntegerField | NumberField | DateField | ChoiceField | ChoicesField | BooleanField | CustomField<any, any>;
|
|
143
|
+
/**
|
|
144
|
+
* Defines a new field kind. Returns a function to use it in action inputs,
|
|
145
|
+
* like the built-in `field.*` ones.
|
|
146
|
+
*
|
|
147
|
+
* @param kind Its name, schema, control and parser.
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* const postcode = defineFieldKind<FieldBase, string>({
|
|
151
|
+
* name: 'postcode',
|
|
152
|
+
* schema: () => ({ type: 'string', pattern: '^(?:[0-9]{5})$' }),
|
|
153
|
+
* control: (f, p) => html`<input type="text" inputmode="numeric" pattern="[0-9]{5}"${p.attributes} value="${String(p.value ?? '')}">`,
|
|
154
|
+
* parse: (raw) => (typeof raw === 'string' && /^[0-9]{5}$/.test(raw) ? { value: raw } : { error: 'Enter five digits.' }),
|
|
155
|
+
* });
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
95
158
|
export declare function defineFieldKind<O extends FieldBase, V>(kind: FieldKind<O, V>): <const P extends O>(options: P) => P & CustomField<O, V>;
|
|
159
|
+
/** An action's input: fields by name. */
|
|
96
160
|
export type Fields = Record<string, Field>;
|
|
97
|
-
type ValueOf<F> = F extends CustomField<any, infer V> ? V : F extends TextField | DateField ? string : F extends IntegerField | NumberField ? number : F extends BooleanField ? boolean : F extends ChoiceField<infer V> ? V : never;
|
|
161
|
+
type ValueOf<F> = F extends CustomField<any, infer V> ? V : F extends TextField | DateField ? string : F extends IntegerField | NumberField ? number : F extends BooleanField ? boolean : F extends ChoiceField<infer V> ? V : F extends ChoicesField<infer V> ? V[] : never;
|
|
98
162
|
type OptionalKeys<F extends Fields> = {
|
|
99
163
|
[K in keyof F]: F[K] extends {
|
|
100
164
|
optional: true;
|
|
@@ -110,39 +174,92 @@ export type Infer<F extends Fields> = Simplify<{
|
|
|
110
174
|
[K in OptionalKeys<F>]?: ValueOf<F[K]>;
|
|
111
175
|
}>;
|
|
112
176
|
type Without<T, K extends PropertyKey> = Omit<T, K>;
|
|
177
|
+
/**
|
|
178
|
+
* The built-in field kinds, for an action's `input`. Each maps to one standard
|
|
179
|
+
* HTML control and one JSON Schema type, so people and agents see the same
|
|
180
|
+
* input with the same constraints.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* input: {
|
|
185
|
+
* name: field.text({ label: 'Name', max: 80 }),
|
|
186
|
+
* email: field.text({ label: 'Email', format: 'email' }),
|
|
187
|
+
* guests: field.integer({ label: 'Guests', min: 1, max: 12 }),
|
|
188
|
+
* date: field.date({ label: 'Date', min: '2026-12-01' }),
|
|
189
|
+
* seating: field.choice({ label: 'Seating', options: [{ value: 'inside', label: 'Inside' }, { value: 'terrace', label: 'Terrace' }] }),
|
|
190
|
+
* notes: field.text({ label: 'Notes', multiline: true, optional: true }),
|
|
191
|
+
* newsletter: field.boolean({ label: 'Send me news' }),
|
|
192
|
+
* }
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
113
195
|
export declare const field: {
|
|
196
|
+
/** Text: a one-line input, or a textarea with `multiline`. Formats: email, url, tel. */
|
|
114
197
|
text<const O extends Without<TextField, 'kind'>>(options: O): O & {
|
|
115
198
|
kind: 'text';
|
|
116
199
|
};
|
|
200
|
+
/** A whole number. */
|
|
117
201
|
integer<const O extends Without<IntegerField, 'kind'>>(options: O): O & {
|
|
118
202
|
kind: 'integer';
|
|
119
203
|
};
|
|
204
|
+
/** Any number, decimals included. */
|
|
120
205
|
number<const O extends Without<NumberField, 'kind'>>(options: O): O & {
|
|
121
206
|
kind: 'number';
|
|
122
207
|
};
|
|
208
|
+
/** A date, YYYY-MM-DD. */
|
|
123
209
|
date<const O extends Without<DateField, 'kind'>>(options: O): O & {
|
|
124
210
|
kind: 'date';
|
|
125
211
|
};
|
|
212
|
+
/** One of the options, as a select. Agents send the option's value. */
|
|
126
213
|
choice<const V extends string, const O extends Without<ChoiceField<V>, 'kind'>>(options: O & {
|
|
127
214
|
options: readonly ChoiceOption<V>[];
|
|
128
215
|
}): O & {
|
|
129
216
|
kind: 'choice';
|
|
130
217
|
options: readonly ChoiceOption<V>[];
|
|
131
218
|
};
|
|
219
|
+
/** Any number of the options, as checkboxes. The value is a list, empty when none is checked. */
|
|
220
|
+
choices<const V extends string, const O extends Without<ChoicesField<V>, 'kind'>>(options: O & {
|
|
221
|
+
options: readonly ChoiceOption<V>[];
|
|
222
|
+
}): O & {
|
|
223
|
+
kind: 'choices';
|
|
224
|
+
options: readonly ChoiceOption<V>[];
|
|
225
|
+
};
|
|
226
|
+
/** A checkbox: true or false. */
|
|
132
227
|
boolean<const O extends Without<BooleanField, 'kind'>>(options: O): O & {
|
|
133
228
|
kind: 'boolean';
|
|
134
229
|
};
|
|
135
230
|
};
|
|
136
231
|
export declare function isRequired(f: Field): boolean;
|
|
137
232
|
export type JsonSchemaProperty = Record<string, unknown>;
|
|
233
|
+
/**
|
|
234
|
+
* An action's input as a JSON Schema object, as agents and OpenAPI see it.
|
|
235
|
+
*
|
|
236
|
+
* @category plugins
|
|
237
|
+
*/
|
|
138
238
|
export interface ObjectSchema {
|
|
239
|
+
/** Always "object". */
|
|
139
240
|
type: 'object';
|
|
241
|
+
/** One JSON Schema per field, by name, in order. */
|
|
140
242
|
properties: Record<string, JsonSchemaProperty>;
|
|
243
|
+
/** The names of required fields. */
|
|
141
244
|
required: string[];
|
|
245
|
+
/** Always false: no other properties are accepted. */
|
|
142
246
|
additionalProperties: false;
|
|
143
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* One field as JSON Schema, with its label as `title` and its hint as `description`.
|
|
250
|
+
*
|
|
251
|
+
* @category plugins
|
|
252
|
+
* @param f The field.
|
|
253
|
+
*/
|
|
144
254
|
export declare function fieldSchema(f: Field): JsonSchemaProperty;
|
|
255
|
+
/**
|
|
256
|
+
* An action's fields as a JSON Schema object: what agents and OpenAPI see as its input.
|
|
257
|
+
*
|
|
258
|
+
* @category plugins
|
|
259
|
+
* @param fields The action's input fields.
|
|
260
|
+
*/
|
|
145
261
|
export declare function toJsonSchema(fields: Fields): ObjectSchema;
|
|
262
|
+
/** What validate() returns: the typed value, or an error message per field name. */
|
|
146
263
|
export type ValidationResult<T> = {
|
|
147
264
|
ok: true;
|
|
148
265
|
value: T;
|
|
@@ -150,5 +267,14 @@ export type ValidationResult<T> = {
|
|
|
150
267
|
ok: false;
|
|
151
268
|
errors: Record<string, string>;
|
|
152
269
|
};
|
|
270
|
+
/**
|
|
271
|
+
* Validates and coerces raw input (from a form submission or JSON) against the
|
|
272
|
+
* fields, as the server does on every call. Unknown keys are dropped, so
|
|
273
|
+
* hidden inputs such as CSRF tokens do not leak into the action's input.
|
|
274
|
+
*
|
|
275
|
+
* @param fields The fields to check against.
|
|
276
|
+
* @param input Raw values: strings from a form, or JSON values.
|
|
277
|
+
* @returns The typed values, or an error message per field.
|
|
278
|
+
*/
|
|
153
279
|
export declare function validate<F extends Fields>(fields: F, input: Record<string, unknown>): ValidationResult<Infer<F>>;
|
|
154
280
|
export {};
|
package/dist/schema.js
CHANGED
|
@@ -1,38 +1,89 @@
|
|
|
1
1
|
// Field definitions for action input. Each kind maps one-to-one onto an HTML
|
|
2
2
|
// control and a JSON Schema property, so the form a person sees and the tool
|
|
3
3
|
// an agent sees can be derived from the same definition and checked for parity.
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Defines a new field kind. Returns a function to use it in action inputs,
|
|
6
|
+
* like the built-in `field.*` ones.
|
|
7
|
+
*
|
|
8
|
+
* @param kind Its name, schema, control and parser.
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const postcode = defineFieldKind<FieldBase, string>({
|
|
12
|
+
* name: 'postcode',
|
|
13
|
+
* schema: () => ({ type: 'string', pattern: '^(?:[0-9]{5})$' }),
|
|
14
|
+
* control: (f, p) => html`<input type="text" inputmode="numeric" pattern="[0-9]{5}"${p.attributes} value="${String(p.value ?? '')}">`,
|
|
15
|
+
* parse: (raw) => (typeof raw === 'string' && /^[0-9]{5}$/.test(raw) ? { value: raw } : { error: 'Enter five digits.' }),
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
5
19
|
export function defineFieldKind(kind) {
|
|
6
20
|
if (!/^[a-z][a-z0-9-]*$/.test(kind.name))
|
|
7
21
|
throw new Error(`defineFieldKind: "${kind.name}" must be lowercase letters, digits and hyphens.`);
|
|
8
22
|
return (options) => ({ ...options, kind: 'custom', fieldKind: kind });
|
|
9
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* The built-in field kinds, for an action's `input`. Each maps to one standard
|
|
26
|
+
* HTML control and one JSON Schema type, so people and agents see the same
|
|
27
|
+
* input with the same constraints.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* input: {
|
|
32
|
+
* name: field.text({ label: 'Name', max: 80 }),
|
|
33
|
+
* email: field.text({ label: 'Email', format: 'email' }),
|
|
34
|
+
* guests: field.integer({ label: 'Guests', min: 1, max: 12 }),
|
|
35
|
+
* date: field.date({ label: 'Date', min: '2026-12-01' }),
|
|
36
|
+
* seating: field.choice({ label: 'Seating', options: [{ value: 'inside', label: 'Inside' }, { value: 'terrace', label: 'Terrace' }] }),
|
|
37
|
+
* notes: field.text({ label: 'Notes', multiline: true, optional: true }),
|
|
38
|
+
* newsletter: field.boolean({ label: 'Send me news' }),
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
10
42
|
export const field = {
|
|
43
|
+
/** Text: a one-line input, or a textarea with `multiline`. Formats: email, url, tel. */
|
|
11
44
|
text(options) {
|
|
12
45
|
return { ...options, kind: 'text' };
|
|
13
46
|
},
|
|
47
|
+
/** A whole number. */
|
|
14
48
|
integer(options) {
|
|
15
49
|
return { ...options, kind: 'integer' };
|
|
16
50
|
},
|
|
51
|
+
/** Any number, decimals included. */
|
|
17
52
|
number(options) {
|
|
18
53
|
return { ...options, kind: 'number' };
|
|
19
54
|
},
|
|
55
|
+
/** A date, YYYY-MM-DD. */
|
|
20
56
|
date(options) {
|
|
21
57
|
return { ...options, kind: 'date' };
|
|
22
58
|
},
|
|
59
|
+
/** One of the options, as a select. Agents send the option's value. */
|
|
23
60
|
choice(options) {
|
|
24
61
|
return { ...options, kind: 'choice' };
|
|
25
62
|
},
|
|
63
|
+
/** Any number of the options, as checkboxes. The value is a list, empty when none is checked. */
|
|
64
|
+
choices(options) {
|
|
65
|
+
// With one option the group would read as a single yes-or-no checkbox.
|
|
66
|
+
if (options.options.length < 2)
|
|
67
|
+
throw new Error(`field.choices "${options.label}" needs at least two options; use field.boolean for one.`);
|
|
68
|
+
return { ...options, kind: 'choices' };
|
|
69
|
+
},
|
|
70
|
+
/** A checkbox: true or false. */
|
|
26
71
|
boolean(options) {
|
|
27
72
|
return { ...options, kind: 'boolean' };
|
|
28
73
|
},
|
|
29
74
|
};
|
|
30
75
|
export function isRequired(f) {
|
|
31
|
-
return f.kind !== 'boolean' && !f.optional;
|
|
76
|
+
return f.kind !== 'boolean' && f.kind !== 'choices' && !f.optional;
|
|
32
77
|
}
|
|
33
78
|
function compact(o) {
|
|
34
79
|
return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
35
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* One field as JSON Schema, with its label as `title` and its hint as `description`.
|
|
83
|
+
*
|
|
84
|
+
* @category plugins
|
|
85
|
+
* @param f The field.
|
|
86
|
+
*/
|
|
36
87
|
export function fieldSchema(f) {
|
|
37
88
|
const base = { title: f.label, description: f.description };
|
|
38
89
|
switch (f.kind) {
|
|
@@ -60,12 +111,20 @@ export function fieldSchema(f) {
|
|
|
60
111
|
});
|
|
61
112
|
case 'choice':
|
|
62
113
|
return compact({ ...base, type: 'string', enum: f.options.map((o) => o.value) });
|
|
114
|
+
case 'choices':
|
|
115
|
+
return compact({ ...base, type: 'array', items: { type: 'string', enum: f.options.map((o) => o.value) }, uniqueItems: true });
|
|
63
116
|
case 'boolean':
|
|
64
117
|
return compact({ ...base, type: 'boolean' });
|
|
65
118
|
case 'custom':
|
|
66
119
|
return compact({ ...base, ...f.fieldKind.schema(f) });
|
|
67
120
|
}
|
|
68
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* An action's fields as a JSON Schema object: what agents and OpenAPI see as its input.
|
|
124
|
+
*
|
|
125
|
+
* @category plugins
|
|
126
|
+
* @param fields The action's input fields.
|
|
127
|
+
*/
|
|
69
128
|
export function toJsonSchema(fields) {
|
|
70
129
|
const properties = {};
|
|
71
130
|
const required = [];
|
|
@@ -102,6 +161,16 @@ function checkField(f, raw) {
|
|
|
102
161
|
return { value: true };
|
|
103
162
|
return { error: `${f.label} must be true or false.` };
|
|
104
163
|
}
|
|
164
|
+
if (f.kind === 'choices') {
|
|
165
|
+
// A form sends one value per checked box: none, one (a string) or several.
|
|
166
|
+
const list = isMissing(raw) ? [] : typeof raw === 'string' ? [raw] : raw;
|
|
167
|
+
const allowed = f.options.map((o) => o.value);
|
|
168
|
+
if (!Array.isArray(list) || !list.every((v) => typeof v === 'string' && allowed.includes(v)))
|
|
169
|
+
return { error: `${f.label} must be a list of: ${allowed.join(', ')}.` };
|
|
170
|
+
if (new Set(list).size !== list.length)
|
|
171
|
+
return { error: `${f.label} lists the same choice twice.` };
|
|
172
|
+
return { value: list };
|
|
173
|
+
}
|
|
105
174
|
if (isMissing(raw)) {
|
|
106
175
|
return f.optional ? {} : { error: `${f.label} is required.` };
|
|
107
176
|
}
|
|
@@ -163,17 +232,21 @@ function checkField(f, raw) {
|
|
|
163
232
|
}
|
|
164
233
|
}
|
|
165
234
|
}
|
|
166
|
-
/**
|
|
167
|
-
* Validates and coerces raw input (from a form submission or JSON) against the
|
|
168
|
-
* fields. Unknown keys are dropped, so hidden inputs such as CSRF tokens do not
|
|
169
|
-
* leak into the action's input.
|
|
170
|
-
*/
|
|
171
235
|
function isWebUrl(value) {
|
|
172
236
|
if (!URL.canParse(value))
|
|
173
237
|
return false;
|
|
174
238
|
const { protocol, hostname } = new URL(value);
|
|
175
239
|
return (protocol === 'https:' || protocol === 'http:') && hostname !== '';
|
|
176
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Validates and coerces raw input (from a form submission or JSON) against the
|
|
243
|
+
* fields, as the server does on every call. Unknown keys are dropped, so
|
|
244
|
+
* hidden inputs such as CSRF tokens do not leak into the action's input.
|
|
245
|
+
*
|
|
246
|
+
* @param fields The fields to check against.
|
|
247
|
+
* @param input Raw values: strings from a form, or JSON values.
|
|
248
|
+
* @returns The typed values, or an error message per field.
|
|
249
|
+
*/
|
|
177
250
|
export function validate(fields, input) {
|
|
178
251
|
const value = {};
|
|
179
252
|
const errors = {};
|
package/dist/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,6EAA6E;AAC7E,gFAAgF;
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,6EAA6E;AAC7E,gFAAgF;AAmKhF;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,eAAe,CAAyB,IAAqB;IAC3E,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,qBAAqB,IAAI,CAAC,IAAI,kDAAkD,CAAC,CAAC;IAC5I,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,CAAU,CAAC;AACjF,CAAC;AAkCD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,wFAAwF;IACxF,IAAI,CAA6C,OAAU;QACzD,OAAO,EAAE,GAAG,OAAO,EAAE,IAAI,E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|
import type { CorsOptions } from './cors.js';
|
|
2
2
|
import type { RateLimitRule, RateLimitStore } from './rate-limit.js';
|
|
3
|
+
/** The largest request body accepted by default: 1,000,000 bytes. Change it with `security.maxBodyBytes`. */
|
|
3
4
|
export declare const DEFAULT_MAX_BODY_BYTES = 1000000;
|
|
5
|
+
/**
|
|
6
|
+
* A request body Pexis refused: too large, the wrong type, or unreadable.
|
|
7
|
+
*
|
|
8
|
+
* @category plugins
|
|
9
|
+
*/
|
|
4
10
|
export declare class BodyError extends Error {
|
|
11
|
+
/** 400 for an unreadable body, 413 for one too large, 415 for an unsupported type. */
|
|
5
12
|
readonly status: 400 | 413 | 415;
|
|
13
|
+
/** A refused body, with the status to answer and a message safe to show. */
|
|
6
14
|
constructor(status: 400 | 413 | 415, message: string);
|
|
7
15
|
}
|
|
8
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Reads the body as text, refusing more than `max` bytes even without a
|
|
18
|
+
* Content-Length. Throws BodyError.
|
|
19
|
+
*
|
|
20
|
+
* @category plugins
|
|
21
|
+
* @param request The request.
|
|
22
|
+
* @param max The most bytes to read.
|
|
23
|
+
*/
|
|
9
24
|
export declare function readBodyText(request: Request, max: number): Promise<string>;
|
|
10
25
|
/**
|
|
11
26
|
* Action input from a request. Only URL-encoded forms and JSON are accepted:
|
|
12
27
|
* Pexis forms never upload files, so multipart bodies are refused outright.
|
|
13
28
|
*/
|
|
14
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Reads an action's input from a query string, a form body or JSON. In form
|
|
31
|
+
* data, a name in `lists` (a group of checkboxes) gives every value sent, and
|
|
32
|
+
* any other name its first value.
|
|
33
|
+
*/
|
|
34
|
+
export declare function readActionInput(request: Request, url: URL, max: number, lists?: ReadonlySet<string>): Promise<Record<string, unknown>>;
|
|
15
35
|
export declare function createNonce(): string;
|
|
36
|
+
/** What createApp({ security }) takes: the Content Security Policy and request limits. Secure by default. */
|
|
16
37
|
export interface SecurityOptions {
|
|
17
38
|
/**
|
|
18
39
|
* Content Security Policy. On by default: scripts and styles must come from
|
|
@@ -46,8 +67,23 @@ export interface SecurityOptions {
|
|
|
46
67
|
*/
|
|
47
68
|
timeoutSeconds?: number | false;
|
|
48
69
|
}
|
|
70
|
+
/** The default action rate limit: 60 calls a minute per client, reads and changes counted separately. */
|
|
49
71
|
export declare const DEFAULT_ACTION_RATE_LIMIT: RateLimitRule;
|
|
72
|
+
/**
|
|
73
|
+
* The Content Security Policy directives an app may add sources to.
|
|
74
|
+
*
|
|
75
|
+
* @category plugins
|
|
76
|
+
*/
|
|
50
77
|
export type CspDirective = 'default-src' | 'script-src' | 'style-src' | 'img-src' | 'font-src' | 'connect-src' | 'media-src' | 'frame-src' | 'form-action' | 'frame-ancestors';
|
|
78
|
+
/**
|
|
79
|
+
* The Content Security Policy header value: same-origin by default, with the
|
|
80
|
+
* request's nonce for scripts and styles, and any extra sources added. Throws
|
|
81
|
+
* when a source would break the policy.
|
|
82
|
+
*
|
|
83
|
+
* @category plugins
|
|
84
|
+
* @param nonce The request's nonce.
|
|
85
|
+
* @param extra Sources to add, per directive.
|
|
86
|
+
*/
|
|
51
87
|
export declare function contentSecurityPolicy(nonce: string, extra?: Partial<Record<CspDirective, string[]>>): string;
|
|
52
88
|
/** Headers every HTML page gets. */
|
|
53
89
|
export declare function documentSecurityHeaders(headers: Headers, origin: string, csp: string | null): void;
|
package/dist/security.js
CHANGED
|
@@ -1,14 +1,29 @@
|
|
|
1
1
|
// Security defaults: request body limits, accepted content types, a per-request
|
|
2
2
|
// nonce, a strict Content Security Policy, and response headers.
|
|
3
|
+
/** The largest request body accepted by default: 1,000,000 bytes. Change it with `security.maxBodyBytes`. */
|
|
3
4
|
export const DEFAULT_MAX_BODY_BYTES = 1_000_000;
|
|
5
|
+
/**
|
|
6
|
+
* A request body Pexis refused: too large, the wrong type, or unreadable.
|
|
7
|
+
*
|
|
8
|
+
* @category plugins
|
|
9
|
+
*/
|
|
4
10
|
export class BodyError extends Error {
|
|
11
|
+
/** 400 for an unreadable body, 413 for one too large, 415 for an unsupported type. */
|
|
5
12
|
status;
|
|
13
|
+
/** A refused body, with the status to answer and a message safe to show. */
|
|
6
14
|
constructor(status, message) {
|
|
7
15
|
super(message);
|
|
8
16
|
this.status = status;
|
|
9
17
|
}
|
|
10
18
|
}
|
|
11
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Reads the body as text, refusing more than `max` bytes even without a
|
|
21
|
+
* Content-Length. Throws BodyError.
|
|
22
|
+
*
|
|
23
|
+
* @category plugins
|
|
24
|
+
* @param request The request.
|
|
25
|
+
* @param max The most bytes to read.
|
|
26
|
+
*/
|
|
12
27
|
export async function readBodyText(request, max) {
|
|
13
28
|
const declared = request.headers.get('content-length');
|
|
14
29
|
if (declared !== null && Number(declared) > max)
|
|
@@ -41,9 +56,14 @@ export async function readBodyText(request, max) {
|
|
|
41
56
|
* Action input from a request. Only URL-encoded forms and JSON are accepted:
|
|
42
57
|
* Pexis forms never upload files, so multipart bodies are refused outright.
|
|
43
58
|
*/
|
|
44
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Reads an action's input from a query string, a form body or JSON. In form
|
|
61
|
+
* data, a name in `lists` (a group of checkboxes) gives every value sent, and
|
|
62
|
+
* any other name its first value.
|
|
63
|
+
*/
|
|
64
|
+
export async function readActionInput(request, url, max, lists = new Set()) {
|
|
45
65
|
if (request.method === 'GET' || request.method === 'HEAD')
|
|
46
|
-
return
|
|
66
|
+
return formValues(url.searchParams, lists);
|
|
47
67
|
const type = (request.headers.get('content-type') ?? '').split(';')[0].trim().toLowerCase();
|
|
48
68
|
// Refuse unsupported types before reading anything.
|
|
49
69
|
if (type !== '' && type !== 'application/x-www-form-urlencoded' && type !== 'application/json') {
|
|
@@ -60,12 +80,7 @@ export async function readActionInput(request, url, max) {
|
|
|
60
80
|
throw new BodyError(400, 'The request body could not be read as UTF-8 text.');
|
|
61
81
|
}
|
|
62
82
|
if (type === 'application/x-www-form-urlencoded' || (type === '' && text === '')) {
|
|
63
|
-
|
|
64
|
-
// Single-value inputs only in this draft; the first value wins.
|
|
65
|
-
for (const [key, value] of new URLSearchParams(text))
|
|
66
|
-
if (!Object.hasOwn(out, key))
|
|
67
|
-
out[key] = value;
|
|
68
|
-
return out;
|
|
83
|
+
return formValues(new URLSearchParams(text), lists);
|
|
69
84
|
}
|
|
70
85
|
if (type === 'application/json') {
|
|
71
86
|
let body;
|
|
@@ -81,12 +96,32 @@ export async function readActionInput(request, url, max) {
|
|
|
81
96
|
}
|
|
82
97
|
throw new BodyError(415, 'Send application/x-www-form-urlencoded or application/json.');
|
|
83
98
|
}
|
|
99
|
+
function formValues(params, lists) {
|
|
100
|
+
const out = {};
|
|
101
|
+
for (const [key, value] of params) {
|
|
102
|
+
if (lists.has(key))
|
|
103
|
+
out[key] = params.getAll(key);
|
|
104
|
+
else if (!Object.hasOwn(out, key))
|
|
105
|
+
out[key] = value;
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
84
109
|
export function createNonce() {
|
|
85
110
|
const bytes = new Uint8Array(16);
|
|
86
111
|
crypto.getRandomValues(bytes);
|
|
87
112
|
return btoa(String.fromCharCode(...bytes));
|
|
88
113
|
}
|
|
114
|
+
/** The default action rate limit: 60 calls a minute per client, reads and changes counted separately. */
|
|
89
115
|
export const DEFAULT_ACTION_RATE_LIMIT = { limit: 60, windowSeconds: 60 };
|
|
116
|
+
/**
|
|
117
|
+
* The Content Security Policy header value: same-origin by default, with the
|
|
118
|
+
* request's nonce for scripts and styles, and any extra sources added. Throws
|
|
119
|
+
* when a source would break the policy.
|
|
120
|
+
*
|
|
121
|
+
* @category plugins
|
|
122
|
+
* @param nonce The request's nonce.
|
|
123
|
+
* @param extra Sources to add, per directive.
|
|
124
|
+
*/
|
|
90
125
|
export function contentSecurityPolicy(nonce, extra = {}) {
|
|
91
126
|
const base = {
|
|
92
127
|
'default-src': ["'self'"],
|