mailschema 0.1.5 → 0.2.2
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/README.md +93 -9
- package/dist/core/artifacts.d.ts +36 -0
- package/dist/core/artifacts.js +78 -0
- package/dist/core/binding.d.ts +24 -0
- package/dist/core/binding.js +50 -0
- package/dist/core/bundled.d.ts +4 -0
- package/dist/core/bundled.js +7 -0
- package/dist/core/contract.d.ts +66 -0
- package/dist/core/contract.js +327 -0
- package/dist/core/document.d.ts +24 -0
- package/dist/core/document.js +190 -0
- package/dist/core/documents.d.ts +68 -0
- package/dist/core/documents.js +145 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.js +9 -0
- package/dist/core/limits.d.ts +19 -0
- package/dist/core/limits.js +55 -0
- package/dist/core/pointer.d.ts +12 -0
- package/dist/core/pointer.js +48 -0
- package/dist/core/schemas.d.ts +21 -0
- package/dist/core/schemas.js +125 -0
- package/dist/core/types.d.ts +127 -0
- package/dist/core/types.js +2 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +6 -2
- package/dist/map.d.ts +0 -2
- package/dist/map.js +0 -2
- package/dist/validation.d.ts +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/mailschema)
|
|
4
4
|
[](https://github.com/mailschema/javascript/actions/workflows/test.yml)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
Mail Action Protocol 0.2 for JavaScript and TypeScript. The package parses, canonicalizes, digests and validates MAP documents, verifies the type contracts an implementation vendors, and builds results and problems. It also carries the MAP 0.2 core artifacts and validates MailSchema Registry contributions locally.
|
|
7
|
+
|
|
8
|
+
It does not establish endpoint trust, verify email authentication, grant authority or send email. Possession mode needs DKIM and DMARC checks on the raw message, which this package leaves to the client.
|
|
7
9
|
|
|
8
10
|
[Specification](https://mailschema.org/specification) · [Registry](https://mailschema.org/registry) · [Tools](https://mailschema.org/tools) · [Source](https://github.com/mailschema/javascript)
|
|
9
11
|
|
|
@@ -15,19 +17,101 @@ npm install mailschema
|
|
|
15
17
|
|
|
16
18
|
Node.js 22 or newer is required. The package is ESM and includes TypeScript declarations.
|
|
17
19
|
|
|
20
|
+
## A service
|
|
21
|
+
|
|
22
|
+
Vendor the exact contract and request schema your service implements, and pin the contract by the digest you reviewed. The package refuses any other contract.
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { readFileSync } from 'node:fs';
|
|
26
|
+
import { Contract, digest } from 'mailschema';
|
|
27
|
+
|
|
28
|
+
const read = (path: string) => JSON.parse(readFileSync(path, 'utf8'));
|
|
29
|
+
const contract = new Contract(
|
|
30
|
+
read('mailschema/content-review-0.3.json'),
|
|
31
|
+
read('mailschema/content-review-0.3.schema.json'),
|
|
32
|
+
{ digest: 'sha-256:…' },
|
|
33
|
+
);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Before sending a description, check it against every rule of the core and the contract, and store its digest or keep it rebuildable from the interaction:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
contract.descriptionErrors(description); // []
|
|
40
|
+
digest(description); // 'sha-256:…'
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
When a request arrives, check it in this order. No step applies an effect for a problem, and a refusal before the description is resolved and its digest matched leaves the request identifier unclaimed:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { isJsonRequest, parse, problem, requestErrors, result, resultUrl } from 'mailschema';
|
|
47
|
+
import type { MapRequest } from 'mailschema';
|
|
48
|
+
|
|
49
|
+
// Answer 415 unless isJsonRequest(contentType). Under possession authority, answer an
|
|
50
|
+
// unknown capability with a plain 404 and a lapsed one with a plain 410 before reading
|
|
51
|
+
// the body; under credential authority, authenticate the principal.
|
|
52
|
+
const request = parse(body) as MapRequest; // throws InvalidDocument
|
|
53
|
+
requestErrors(request); // any errors: invalid-request
|
|
54
|
+
|
|
55
|
+
// Resolve the description you issued for request.interactionId. Refuse an unknown
|
|
56
|
+
// interaction, or a request whose descriptionDigest is not digest(description).
|
|
57
|
+
|
|
58
|
+
// Request identifiers are claimed within your tenant: the account, or the capability.
|
|
59
|
+
// If request.requestId is already claimed there, refuse another principal, and a principal
|
|
60
|
+
// whose permission has been revoked, with an uncorrelated refused problem (problem()
|
|
61
|
+
// without requestId). Answer any change to the request, compared by digest, with
|
|
62
|
+
// idempotency-conflict. Otherwise settle the recorded result with settle() and return it,
|
|
63
|
+
// or expired-interaction once retainUntil() has passed. Nothing is applied again.
|
|
64
|
+
|
|
65
|
+
const refused = contract.requestProblem(description, request, { now: new Date() });
|
|
66
|
+
// refused.code is unsupported-type, unsupported-operation or expired-interaction, with the
|
|
67
|
+
// title and detail problem() takes. Then your own state: already-decided, or stale-target.
|
|
68
|
+
|
|
69
|
+
contract.inputErrors(description, request);
|
|
70
|
+
// [{ detail: '…', pointer: '/feedback' }], a claimed invalid-request
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Then apply the approval lifecycle or the effect, and record the result:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
result(request, {
|
|
77
|
+
state: 'accepted',
|
|
78
|
+
target: description.target,
|
|
79
|
+
resultUrl: resultUrl(description, request.requestId),
|
|
80
|
+
recordedAt: new Date(),
|
|
81
|
+
output: { feedbackRecorded: true, feedbackId: 'fb-1' },
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## API
|
|
86
|
+
|
|
87
|
+
| Call | Does |
|
|
88
|
+
| ----------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
89
|
+
| `parse(text \| bytes)` | Parses a MAP document as I-JSON within the core limits, or throws `InvalidDocument` |
|
|
90
|
+
| `canonicalize(value)`, `digest(value)` | RFC 8785 canonical JSON, and `sha-256:` over it |
|
|
91
|
+
| `mapErrors(document)` | Checks a description, request, result or problem against the core schema |
|
|
92
|
+
| `descriptionErrors`, `requestErrors`, `resultErrors`, `problemErrors` | Check one document against its core definition only |
|
|
93
|
+
| `new Contract(contract, requestSchema, { digest, dependencies })` | Checks the contract's format and profile, refuses any contract but the one pinned by `digest`, checks its pinned dependencies and request schema, and compiles every validator once, or throws `InvalidContract`; `dependencies` supplies, by URL, any pinned schema the package does not bundle |
|
|
94
|
+
| `contract.descriptionErrors(description)` | Checks a description against every rule of the core and the contract |
|
|
95
|
+
| `contract.requestProblem(description, request, { now })` | The exact type, an offered operation the authority permits, and expiry, as a problem code, title and detail, or undefined |
|
|
96
|
+
| `contract.inputErrors(description, request)` | Checks an operation's input and field bindings, with JSON Pointers into `input` |
|
|
97
|
+
| `contract.requestErrors(request)`, `contract.resultErrors(result)` | Check a request against the core and the request schema, and a result against the core, the declared output schema and the declared reason |
|
|
98
|
+
| `contract.typeReference`, `contract.operation(id)`, `contract.isDecision(id)` | The type reference descriptions and requests name, one of its operations, and whether completing it decides the interaction |
|
|
99
|
+
| `result`, `transition`, `problem` | Build results, state transitions and problems with their correlation members, or throw rather than return a document the core refuses. A problem stays within the core limits |
|
|
100
|
+
| `resultUrl`, `resultStatus`, `isRequestId` | The result resource of a request, a result's HTTP status, and whether a value is a request identifier, the only thing a result URL names |
|
|
101
|
+
| `isJsonRequest(contentType)` | Whether a `Content-Type` admits a request rather than a 415 |
|
|
102
|
+
| `DESCRIPTION_MEDIA_TYPE`, `isDescriptionPart(mediaType, profileParameter)` | The `Content-Type` of the part that carries a description, and whether a designated part is labelled with this profile |
|
|
103
|
+
| `retainUntil`, `settle`, `reached` | Result retention, the expiry of an undecided approval, and whether a deadline has passed |
|
|
104
|
+
| `capability(description)`, `writtenPath(url)` | The possession capability: the last segment of the execution URL's path as written |
|
|
105
|
+
|
|
106
|
+
Type rules a contract cannot express, such as a meeting slot being one of the slots offered, belong to each type's implementation. Validation uses Ajv with JSON Schema 2020-12. Lexical forms are core schema patterns, so `format` is never asserted, and references resolve only against pinned schemas, never over the network.
|
|
107
|
+
|
|
18
108
|
## MAP 0.2 core artifacts
|
|
19
109
|
|
|
20
110
|
```js
|
|
21
|
-
import {
|
|
22
|
-
MAP_PROFILE,
|
|
23
|
-
getContractFormatSchema,
|
|
24
|
-
getFormsSchema,
|
|
25
|
-
getMapContext,
|
|
26
|
-
getMapSchema,
|
|
27
|
-
} from 'mailschema';
|
|
111
|
+
import { getContractFormatSchema, getFormsSchema, getMapContext, getMapSchema } from 'mailschema';
|
|
28
112
|
```
|
|
29
113
|
|
|
30
|
-
Each is byte-identical to the file the [profile record](https://mailschema.org/profiles/map/0.2.json) binds by SHA-256, and is also exported raw as `mailschema/map-0.2.schema.json`, `mailschema/map-0.2.jsonld`, `mailschema/type-contract-0.2.schema.json` and `mailschema/forms-0.1.schema.json`. Type contracts are not bundled:
|
|
114
|
+
Each is byte-identical to the file the [profile record](https://mailschema.org/profiles/map/0.2.json) binds by SHA-256, and is also exported raw as `mailschema/map-0.2.schema.json`, `mailschema/map-0.2.jsonld`, `mailschema/type-contract-0.2.schema.json` and `mailschema/forms-0.1.schema.json`. Type contracts are not bundled: an implementation vendors the contracts it supports, and `Contract` verifies them by digest.
|
|
31
115
|
|
|
32
116
|
## Registry data
|
|
33
117
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ErrorObject, type ValidateFunction } from 'ajv/dist/2020.js';
|
|
2
|
+
import type { JsonObject } from './types.ts';
|
|
3
|
+
export declare const MAP_PROFILE = "https://mailschema.org/profiles/map/0.2";
|
|
4
|
+
export declare const CORE_SCHEMA = "https://mailschema.org/schemas/map-0.2.schema.json";
|
|
5
|
+
export declare const FORMS_SCHEMA = "https://mailschema.org/schemas/forms-0.1.schema.json";
|
|
6
|
+
export declare const CONTRACT_FORMAT = "https://mailschema.org/schemas/type-contract-0.2.schema.json";
|
|
7
|
+
export declare const CONTEXT = "https://mailschema.org/contexts/map-0.2.jsonld";
|
|
8
|
+
export declare const JSON_SCHEMA_2020_12 = "https://json-schema.org/draft/2020-12/schema";
|
|
9
|
+
/** The schemas a contract may depend on without supplying them itself. */
|
|
10
|
+
export declare const BUNDLED: ReadonlyMap<string, JsonObject>;
|
|
11
|
+
/** A core or form fields definition by name, for the shared lexical vectors. */
|
|
12
|
+
export declare const definition: (schema: string, name: string) => ValidateFunction;
|
|
13
|
+
/** Errors as `pointer message` lines, as the other MailSchema packages report them. */
|
|
14
|
+
export declare function errorList(errors: ErrorObject[] | null | undefined, prefix?: string): string[];
|
|
15
|
+
/** Checks a type contract against the contract format. */
|
|
16
|
+
export declare const contractFormatErrors: (contract: unknown) => string[];
|
|
17
|
+
/** Checks any MAP 0.2 description, request, result or problem against the core schema. */
|
|
18
|
+
export declare const mapErrors: (document: unknown) => string[];
|
|
19
|
+
/**
|
|
20
|
+
* Checks a description against the core description definition only. A description of a
|
|
21
|
+
* type the caller has no contract for can be checked this far.
|
|
22
|
+
*/
|
|
23
|
+
export declare const descriptionErrors: (description: unknown) => string[];
|
|
24
|
+
/** Checks a request against the core request definition. Its identifier stays free if it fails. */
|
|
25
|
+
export declare const requestErrors: (request: unknown) => string[];
|
|
26
|
+
/** Checks a result against the core result definition only. */
|
|
27
|
+
export declare const resultErrors: (result: unknown) => string[];
|
|
28
|
+
/** Checks a problem against the core problem definition, including its type, status and code. */
|
|
29
|
+
export declare const problemErrors: (problem: unknown) => string[];
|
|
30
|
+
/** Whether a value is a request identifier: the core UUID URN form. Only one names a result resource. */
|
|
31
|
+
export declare const isRequestId: (value: unknown) => boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether a deadline, a core date-time plus `afterSeconds`, has been reached. Anything but
|
|
34
|
+
* a core date-time counts as reached, so a mistake fails closed.
|
|
35
|
+
*/
|
|
36
|
+
export declare const reached: (now: Date, deadline: string, afterSeconds?: number) => boolean;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// The bundled core artifacts and the validators built from them.
|
|
2
|
+
import Ajv2020 from 'ajv/dist/2020.js';
|
|
3
|
+
import { contractFormat, coreSchema, formsSchema } from "./bundled.js";
|
|
4
|
+
export const MAP_PROFILE = 'https://mailschema.org/profiles/map/0.2';
|
|
5
|
+
export const CORE_SCHEMA = 'https://mailschema.org/schemas/map-0.2.schema.json';
|
|
6
|
+
export const FORMS_SCHEMA = 'https://mailschema.org/schemas/forms-0.1.schema.json';
|
|
7
|
+
export const CONTRACT_FORMAT = 'https://mailschema.org/schemas/type-contract-0.2.schema.json';
|
|
8
|
+
export const CONTEXT = 'https://mailschema.org/contexts/map-0.2.jsonld';
|
|
9
|
+
export const JSON_SCHEMA_2020_12 = 'https://json-schema.org/draft/2020-12/schema';
|
|
10
|
+
/** The schemas a contract may depend on without supplying them itself. */
|
|
11
|
+
export const BUNDLED = new Map([
|
|
12
|
+
[CORE_SCHEMA, coreSchema],
|
|
13
|
+
[FORMS_SCHEMA, formsSchema],
|
|
14
|
+
]);
|
|
15
|
+
/**
|
|
16
|
+
* JSON Schema 2020-12 as MAP reads it. Lexical forms are the core schema's patterns, so
|
|
17
|
+
* `format` stays an annotation: format checkers differ between validators. References
|
|
18
|
+
* resolve only against schemas already added, never over the network.
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export function createValidator() {
|
|
22
|
+
const ajv = new Ajv2020({
|
|
23
|
+
allErrors: true,
|
|
24
|
+
strict: true,
|
|
25
|
+
strictRequired: false,
|
|
26
|
+
validateFormats: false,
|
|
27
|
+
});
|
|
28
|
+
// HTML autofill field names label form fields; they are annotations, not assertions.
|
|
29
|
+
ajv.addKeyword({ keyword: 'autocomplete', schemaType: 'string' });
|
|
30
|
+
// Only JSON Schema 2020-12 vocabulary keywords. Ajv also reads OpenAPI's `nullable`,
|
|
31
|
+
// which admits a null other validators refuse, and the earlier `dependencies` and
|
|
32
|
+
// `definitions`; strict mode now refuses them as unknown.
|
|
33
|
+
for (const keyword of ['nullable', 'dependencies', 'definitions'])
|
|
34
|
+
ajv.removeKeyword(keyword);
|
|
35
|
+
for (const schema of BUNDLED.values())
|
|
36
|
+
ajv.addSchema(schema);
|
|
37
|
+
return ajv;
|
|
38
|
+
}
|
|
39
|
+
const core = createValidator();
|
|
40
|
+
/** A core or form fields definition by name, for the shared lexical vectors. */
|
|
41
|
+
export const definition = (schema, name) => core.getSchema(`${schema}#/$defs/${name}`);
|
|
42
|
+
/** Errors as `pointer message` lines, as the other MailSchema packages report them. */
|
|
43
|
+
export function errorList(errors, prefix = '') {
|
|
44
|
+
return (errors ?? [])
|
|
45
|
+
.slice(0, 100)
|
|
46
|
+
.map((error) => `${prefix + error.instancePath || '/'} ${error.message}${error.keyword === 'required' ? `: ${error.params.missingProperty}` : ''}`);
|
|
47
|
+
}
|
|
48
|
+
const errorsOf = (validate, value) => validate(value) ? [] : errorList(validate.errors);
|
|
49
|
+
const DOCUMENT = core.getSchema(CORE_SCHEMA);
|
|
50
|
+
const DEFINITIONS = Object.fromEntries(['description', 'request', 'result', 'problem'].map((name) => [
|
|
51
|
+
name,
|
|
52
|
+
definition(CORE_SCHEMA, name),
|
|
53
|
+
]));
|
|
54
|
+
const DATE_TIME = new RegExp(coreSchema.$defs.dateTime.pattern, 'u');
|
|
55
|
+
const REQUEST_ID = definition(CORE_SCHEMA, 'uuidUrn');
|
|
56
|
+
const CONTRACT = core.compile(contractFormat);
|
|
57
|
+
/** Checks a type contract against the contract format. */
|
|
58
|
+
export const contractFormatErrors = (contract) => errorsOf(CONTRACT, contract);
|
|
59
|
+
/** Checks any MAP 0.2 description, request, result or problem against the core schema. */
|
|
60
|
+
export const mapErrors = (document) => errorsOf(DOCUMENT, document);
|
|
61
|
+
/**
|
|
62
|
+
* Checks a description against the core description definition only. A description of a
|
|
63
|
+
* type the caller has no contract for can be checked this far.
|
|
64
|
+
*/
|
|
65
|
+
export const descriptionErrors = (description) => errorsOf(DEFINITIONS.description, description);
|
|
66
|
+
/** Checks a request against the core request definition. Its identifier stays free if it fails. */
|
|
67
|
+
export const requestErrors = (request) => errorsOf(DEFINITIONS.request, request);
|
|
68
|
+
/** Checks a result against the core result definition only. */
|
|
69
|
+
export const resultErrors = (result) => errorsOf(DEFINITIONS.result, result);
|
|
70
|
+
/** Checks a problem against the core problem definition, including its type, status and code. */
|
|
71
|
+
export const problemErrors = (problem) => errorsOf(DEFINITIONS.problem, problem);
|
|
72
|
+
/** Whether a value is a request identifier: the core UUID URN form. Only one names a result resource. */
|
|
73
|
+
export const isRequestId = (value) => REQUEST_ID(value);
|
|
74
|
+
/**
|
|
75
|
+
* Whether a deadline, a core date-time plus `afterSeconds`, has been reached. Anything but
|
|
76
|
+
* a core date-time counts as reached, so a mistake fails closed.
|
|
77
|
+
*/
|
|
78
|
+
export const reached = (now, deadline, afterSeconds = 0) => !(DATE_TIME.test(deadline) && now.getTime() < Date.parse(deadline) + afterSeconds * 1000);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { MapDescription } from './types.ts';
|
|
2
|
+
/** The Content-Type of the part that carries a description, labelled with the profile. */
|
|
3
|
+
export declare const DESCRIPTION_MEDIA_TYPE = "application/ld+json; profile=\"https://mailschema.org/profiles/map/0.2\"";
|
|
4
|
+
/**
|
|
5
|
+
* Whether a designated part carries a description of this profile: its media type is
|
|
6
|
+
* application/ld+json, compared case-insensitively, and its profile parameter lists the
|
|
7
|
+
* profile among its space-separated URIs.
|
|
8
|
+
*/
|
|
9
|
+
export declare const isDescriptionPart: (mediaType: string, profileParameter: string | undefined) => boolean;
|
|
10
|
+
/** The path of an absolute URL exactly as written, with no dot segments removed and nothing decoded. */
|
|
11
|
+
export declare const writtenPath: (url: string) => string;
|
|
12
|
+
/** The possession capability: the last segment of the execution URL path, as written. */
|
|
13
|
+
export declare const capability: (description: MapDescription) => string;
|
|
14
|
+
/** The rules a possession description's capability URLs meet when it is issued. */
|
|
15
|
+
export declare function capabilityProblems(description: MapDescription): string[];
|
|
16
|
+
/**
|
|
17
|
+
* Whether a Content-Type admits a request rather than a 415: the media type
|
|
18
|
+
* application/json, compared case-insensitively, with no parameter other than a UTF-8
|
|
19
|
+
* charset. Only HTTP's optional whitespace, spaces and tabs, may surround each part, and
|
|
20
|
+
* empty parameter slots are ignored, as RFC 9110 permits.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isJsonRequest(contentType: string | undefined): boolean;
|
|
23
|
+
/** The result resource of a request: the description's template with the identifier encoded. */
|
|
24
|
+
export declare const resultUrl: (description: MapDescription, requestId: string) => string;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The HTTP and Structured Email bindings: capability URLs, the requests the execution URL
|
|
2
|
+
// admits, where results live and which message part carries a description.
|
|
3
|
+
import { MAP_PROFILE } from "./artifacts.js";
|
|
4
|
+
/** The Content-Type of the part that carries a description, labelled with the profile. */
|
|
5
|
+
export const DESCRIPTION_MEDIA_TYPE = `application/ld+json; profile="${MAP_PROFILE}"`;
|
|
6
|
+
/**
|
|
7
|
+
* Whether a designated part carries a description of this profile: its media type is
|
|
8
|
+
* application/ld+json, compared case-insensitively, and its profile parameter lists the
|
|
9
|
+
* profile among its space-separated URIs.
|
|
10
|
+
*/
|
|
11
|
+
export const isDescriptionPart = (mediaType, profileParameter) => mediaType.toLowerCase() === 'application/ld+json' &&
|
|
12
|
+
(profileParameter ?? '').split(/[ \t]+/).includes(MAP_PROFILE);
|
|
13
|
+
/** The path of an absolute URL exactly as written, with no dot segments removed and nothing decoded. */
|
|
14
|
+
export const writtenPath = (url) => /^[A-Za-z][A-Za-z0-9+.-]*:\/\/[^/?#]*([^?#]*)/.exec(url)?.[1] ?? '';
|
|
15
|
+
/** The possession capability: the last segment of the execution URL path, as written. */
|
|
16
|
+
export const capability = (description) => writtenPath(description.service.execution.url).split('/').at(-1) ?? '';
|
|
17
|
+
const UNGUESSABLE = /^[A-Za-z0-9_-]{22,}$/;
|
|
18
|
+
const dotSegment = (url) => writtenPath(url)
|
|
19
|
+
.split('/')
|
|
20
|
+
.some((segment) => /^(?:\.|%2e){1,2}$/i.test(segment));
|
|
21
|
+
/** The rules a possession description's capability URLs meet when it is issued. */
|
|
22
|
+
export function capabilityProblems(description) {
|
|
23
|
+
const { url, resultUrlTemplate } = description.service.execution;
|
|
24
|
+
const issued = capability(description);
|
|
25
|
+
const problems = [];
|
|
26
|
+
if (!UNGUESSABLE.test(issued))
|
|
27
|
+
problems.push('The capability is too short to be unguessable.');
|
|
28
|
+
if ([url, resultUrlTemplate].some(dotSegment))
|
|
29
|
+
problems.push('A capability URL must not contain dot segments.');
|
|
30
|
+
if (!resultUrlTemplate.startsWith(`${url}/`))
|
|
31
|
+
problems.push('The result template must extend the execution URL and its capability.');
|
|
32
|
+
if (description.service.humanUrl.includes(issued))
|
|
33
|
+
problems.push('The human route must not carry the capability.');
|
|
34
|
+
return problems;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether a Content-Type admits a request rather than a 415: the media type
|
|
38
|
+
* application/json, compared case-insensitively, with no parameter other than a UTF-8
|
|
39
|
+
* charset. Only HTTP's optional whitespace, spaces and tabs, may surround each part, and
|
|
40
|
+
* empty parameter slots are ignored, as RFC 9110 permits.
|
|
41
|
+
*/
|
|
42
|
+
export function isJsonRequest(contentType) {
|
|
43
|
+
const [type, ...parameters] = (contentType ?? '')
|
|
44
|
+
.split(';')
|
|
45
|
+
.map((part) => part.replace(/^[ \t]+|[ \t]+$/g, '').toLowerCase());
|
|
46
|
+
return (type === 'application/json' &&
|
|
47
|
+
parameters.every((parameter) => parameter === '' || /^charset=(?:utf-8|"utf-8")$/.test(parameter)));
|
|
48
|
+
}
|
|
49
|
+
/** The result resource of a request: the description's template with the identifier encoded. */
|
|
50
|
+
export const resultUrl = (description, requestId) => description.service.execution.resultUrlTemplate.replace('{requestId}', () => encodeURIComponent(requestId));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// The core artifacts, by their canonical files. Package preparation points these three
|
|
2
|
+
// imports at the package's own copies; no other line differs between the repository and
|
|
3
|
+
// the npm package.
|
|
4
|
+
import coreSchema from '../map-0.2.schema.json' with { type: 'json' };
|
|
5
|
+
import formsSchema from '../forms-0.1.schema.json' with { type: 'json' };
|
|
6
|
+
import contractFormat from '../type-contract-0.2.schema.json' with { type: 'json' };
|
|
7
|
+
export { coreSchema, formsSchema, contractFormat };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ProblemCode } from './documents.ts';
|
|
2
|
+
import type { InputError, JsonObject, MapDescription, MapRequest, TypeContract, TypeReference } from './types.ts';
|
|
3
|
+
/** A type contract that fails its own checks or its pinned digest. */
|
|
4
|
+
export declare class InvalidContract extends Error {
|
|
5
|
+
}
|
|
6
|
+
/** Reasons the core assigns to the approval lifecycle. */
|
|
7
|
+
export declare const APPROVAL_REASONS: readonly ["declined", "stale-target", "expired", "superseded"];
|
|
8
|
+
/** A MAP problem a request earns before the service's own state is consulted. */
|
|
9
|
+
export type RequestProblem = {
|
|
10
|
+
code: ProblemCode;
|
|
11
|
+
title: string;
|
|
12
|
+
detail: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A type contract an implementation vendors, verified against the digest it was pinned by
|
|
16
|
+
* and compiled once. It checks the descriptions, requests, inputs and results of that exact
|
|
17
|
+
* type. The Registry has already applied the contract rules, such as portable patterns, to
|
|
18
|
+
* the contract that digest names; loading refuses anything that would otherwise fail when a
|
|
19
|
+
* request arrives.
|
|
20
|
+
*/
|
|
21
|
+
export declare class Contract {
|
|
22
|
+
#private;
|
|
23
|
+
readonly document: TypeContract;
|
|
24
|
+
readonly digest: string;
|
|
25
|
+
readonly requestSchema: JsonObject;
|
|
26
|
+
/**
|
|
27
|
+
* `digest` is the contract digest the implementation pinned; `dependencies` supplies, by
|
|
28
|
+
* URL, any pinned schema the package does not bundle.
|
|
29
|
+
*/
|
|
30
|
+
constructor(contract: unknown, requestSchema: unknown, { digest: pinned, dependencies, }: {
|
|
31
|
+
digest: string;
|
|
32
|
+
dependencies?: Record<string, unknown>;
|
|
33
|
+
});
|
|
34
|
+
get id(): string;
|
|
35
|
+
get version(): string;
|
|
36
|
+
/** The type reference a description and a request of this contract name exactly. */
|
|
37
|
+
get typeReference(): TypeReference;
|
|
38
|
+
operation(id: string): import("./types.ts").ContractOperation | undefined;
|
|
39
|
+
/** Whether completing the operation decides the interaction. */
|
|
40
|
+
isDecision(id: string): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Every rule a description must satisfy beyond the core schema, for the service that issues
|
|
43
|
+
* it and the client that receives it.
|
|
44
|
+
*/
|
|
45
|
+
descriptionErrors(description: unknown): string[];
|
|
46
|
+
/**
|
|
47
|
+
* The checks a service makes on a request once it has resolved the description it issued,
|
|
48
|
+
* compared the description digest and established the caller: the exact type, an offered
|
|
49
|
+
* operation the authority permits, and expiry. Undefined when they pass. The service then
|
|
50
|
+
* applies its own state (a decided interaction, a stale target) and `inputErrors`, in that
|
|
51
|
+
* order, before any effect.
|
|
52
|
+
*/
|
|
53
|
+
requestProblem(description: MapDescription, request: MapRequest, { now }: {
|
|
54
|
+
now: Date;
|
|
55
|
+
}): RequestProblem | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Input problems for one request, each with a detail and a JSON Pointer into its input: the
|
|
58
|
+
* operation's input schema, then its field bindings against the description's details. Type
|
|
59
|
+
* rules a contract cannot express are the caller's.
|
|
60
|
+
*/
|
|
61
|
+
inputErrors(description: MapDescription, request: MapRequest): InputError[];
|
|
62
|
+
/** A request against the core request definition and this contract's request schema. */
|
|
63
|
+
requestErrors(request: unknown): string[];
|
|
64
|
+
/** The core result definition, then the output schema and reason the operation declares. */
|
|
65
|
+
resultErrors(result: unknown): string[];
|
|
66
|
+
}
|