@taggor/component 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +67 -2
- package/assets.d.ts +35 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/assets.d.ts +26 -0
- package/dist/assets.d.ts.map +1 -0
- package/dist/assets.js +14 -0
- package/dist/assets.js.map +1 -0
- package/dist/bounded-json.d.ts +21 -0
- package/dist/bounded-json.d.ts.map +1 -0
- package/dist/bounded-json.js +55 -0
- package/dist/bounded-json.js.map +1 -0
- package/dist/build/check-messages.d.ts +41 -0
- package/dist/build/check-messages.d.ts.map +1 -0
- package/dist/build/check-messages.js +142 -0
- package/dist/build/check-messages.js.map +1 -0
- package/dist/build/extract-manifest.d.ts +48 -0
- package/dist/build/extract-manifest.d.ts.map +1 -0
- package/dist/build/extract-manifest.js +81 -0
- package/dist/build/extract-manifest.js.map +1 -0
- package/dist/build/extract-styles.d.ts +36 -0
- package/dist/build/extract-styles.d.ts.map +1 -0
- package/dist/build/extract-styles.js +48 -0
- package/dist/build/extract-styles.js.map +1 -0
- package/dist/build/find-definition.d.ts +14 -0
- package/dist/build/find-definition.d.ts.map +1 -0
- package/dist/build/find-definition.js +23 -0
- package/dist/build/find-definition.js.map +1 -0
- package/dist/build/index.d.ts +10 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +10 -0
- package/dist/build/index.js.map +1 -0
- package/dist/consent.d.ts +100 -0
- package/dist/consent.d.ts.map +1 -0
- package/dist/consent.js +147 -0
- package/dist/consent.js.map +1 -0
- package/dist/data-client.d.ts +34 -0
- package/dist/data-client.d.ts.map +1 -0
- package/dist/data-client.js +78 -0
- package/dist/data-client.js.map +1 -0
- package/dist/data-declaration.d.ts +59 -0
- package/dist/data-declaration.d.ts.map +1 -0
- package/dist/data-declaration.js +2 -0
- package/dist/data-declaration.js.map +1 -0
- package/dist/data-errors.d.ts +33 -0
- package/dist/data-errors.d.ts.map +1 -0
- package/dist/data-errors.js +73 -0
- package/dist/data-errors.js.map +1 -0
- package/dist/data-transport.d.ts +73 -0
- package/dist/data-transport.d.ts.map +1 -0
- package/dist/data-transport.js +2 -0
- package/dist/data-transport.js.map +1 -0
- package/dist/debug.d.ts +14 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +11 -0
- package/dist/debug.js.map +1 -0
- package/dist/define-component.d.ts +162 -0
- package/dist/define-component.d.ts.map +1 -0
- package/dist/define-component.js +79 -0
- package/dist/define-component.js.map +1 -0
- package/dist/define-library.d.ts +24 -0
- package/dist/define-library.d.ts.map +1 -0
- package/dist/define-library.js +24 -0
- package/dist/define-library.js.map +1 -0
- package/dist/edge-transport.d.ts +25 -0
- package/dist/edge-transport.d.ts.map +1 -0
- package/dist/edge-transport.js +94 -0
- package/dist/edge-transport.js.map +1 -0
- package/dist/events.d.ts +50 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +2 -0
- package/dist/events.js.map +1 -0
- package/dist/form-field.d.ts +59 -0
- package/dist/form-field.d.ts.map +1 -0
- package/dist/form-field.js +114 -0
- package/dist/form-field.js.map +1 -0
- package/dist/host.d.ts +45 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +30 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/intl-format.d.ts +37 -0
- package/dist/intl-format.d.ts.map +1 -0
- package/dist/intl-format.js +56 -0
- package/dist/intl-format.js.map +1 -0
- package/dist/issued-proofs.d.ts +19 -0
- package/dist/issued-proofs.d.ts.map +1 -0
- package/dist/issued-proofs.js +19 -0
- package/dist/issued-proofs.js.map +1 -0
- package/dist/locale.d.ts +28 -0
- package/dist/locale.d.ts.map +1 -0
- package/dist/locale.js +62 -0
- package/dist/locale.js.map +1 -0
- package/dist/memory-transport.d.ts +39 -0
- package/dist/memory-transport.d.ts.map +1 -0
- package/dist/memory-transport.js +34 -0
- package/dist/memory-transport.js.map +1 -0
- package/dist/messages.d.ts +34 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +54 -0
- package/dist/messages.js.map +1 -0
- package/dist/none.d.ts +7 -0
- package/dist/none.d.ts.map +1 -0
- package/dist/none.js +6 -0
- package/dist/none.js.map +1 -0
- package/dist/props.d.ts +89 -0
- package/dist/props.d.ts.map +1 -0
- package/dist/props.js +25 -0
- package/dist/props.js.map +1 -0
- package/dist/slots.d.ts +26 -0
- package/dist/slots.d.ts.map +1 -0
- package/dist/slots.js +70 -0
- package/dist/slots.js.map +1 -0
- package/dist/submit-result.d.ts +19 -0
- package/dist/submit-result.d.ts.map +1 -0
- package/dist/submit-result.js +58 -0
- package/dist/submit-result.js.map +1 -0
- package/dist/testing/form-internals.d.ts +59 -0
- package/dist/testing/form-internals.d.ts.map +1 -0
- package/dist/testing/form-internals.js +150 -0
- package/dist/testing/form-internals.js.map +1 -0
- package/dist/testing/index.d.ts +15 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +14 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/mount-component.d.ts +105 -0
- package/dist/testing/mount-component.d.ts.map +1 -0
- package/dist/testing/mount-component.js +270 -0
- package/dist/testing/mount-component.js.map +1 -0
- package/dist/testing/visitor.d.ts +23 -0
- package/dist/testing/visitor.d.ts.map +1 -0
- package/dist/testing/visitor.js +34 -0
- package/dist/testing/visitor.js.map +1 -0
- package/dist/theming.d.ts +42 -0
- package/dist/theming.d.ts.map +1 -0
- package/dist/theming.js +19 -0
- package/dist/theming.js.map +1 -0
- package/dist/turnstile.d.ts +60 -0
- package/dist/turnstile.d.ts.map +1 -0
- package/dist/turnstile.js +179 -0
- package/dist/turnstile.js.map +1 -0
- package/dist/use-font.d.ts +65 -0
- package/dist/use-font.d.ts.map +1 -0
- package/dist/use-font.js +145 -0
- package/dist/use-font.js.map +1 -0
- package/dist/use-locale.d.ts +46 -0
- package/dist/use-locale.d.ts.map +1 -0
- package/dist/use-locale.js +57 -0
- package/dist/use-locale.js.map +1 -0
- package/dist/use-taggor-data.d.ts +72 -0
- package/dist/use-taggor-data.d.ts.map +1 -0
- package/dist/use-taggor-data.js +179 -0
- package/dist/use-taggor-data.js.map +1 -0
- package/dist/use-track.d.ts +17 -0
- package/dist/use-track.d.ts.map +1 -0
- package/dist/use-track.js +64 -0
- package/dist/use-track.js.map +1 -0
- package/dist/value-types.d.ts +69 -0
- package/dist/value-types.d.ts.map +1 -0
- package/dist/value-types.js +15 -0
- package/dist/value-types.js.map +1 -0
- package/dist/visitor-action.d.ts +49 -0
- package/dist/visitor-action.d.ts.map +1 -0
- package/dist/visitor-action.js +77 -0
- package/dist/visitor-action.js.map +1 -0
- package/package.json +71 -3
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { type MountableComponent, type RuntimeWarning, type ComponentFailure } from "@taggor/runtime";
|
|
2
|
+
import type { ComponentEventsOf, ComponentPropsOf } from "../define-component.js";
|
|
3
|
+
import type { EventDetail } from "../events.js";
|
|
4
|
+
import { type MemoryResponder, type MemoryTransport } from "../memory-transport.js";
|
|
5
|
+
import type { DeepReadonly } from "../value-types.js";
|
|
6
|
+
import { type FormFieldRecord } from "./form-internals.js";
|
|
7
|
+
/** How to mount a component; every field is optional. */
|
|
8
|
+
export interface MountOptions<W> {
|
|
9
|
+
/** Attributes of the element, as a page writes them (`{ headline: "Hi", "max-amount": "5" }`). */
|
|
10
|
+
readonly attrs?: Readonly<Record<string, string>>;
|
|
11
|
+
/** Props set by property before the element connects (`el.items = [...]`, TAG-304). */
|
|
12
|
+
readonly props?: {
|
|
13
|
+
readonly [K in keyof ComponentPropsOf<W>]?: ComponentPropsOf<W>[K];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Light DOM content per slot name (`"default"` for the unnamed slot): a text becomes a `<span>`
|
|
17
|
+
* with `slot` set (a text node for `"default"`), a node is appended with `slot` set.
|
|
18
|
+
*/
|
|
19
|
+
readonly slots?: Readonly<Record<string, string | Node>>;
|
|
20
|
+
/** How the in-memory transport answers each submission (default: accepted). */
|
|
21
|
+
readonly respond?: MemoryResponder;
|
|
22
|
+
/** Turns on the data hooks' debug warnings (`host.debug`). */
|
|
23
|
+
readonly debug?: boolean;
|
|
24
|
+
/** The manifest's contract version (default: the one this SDK was built with). */
|
|
25
|
+
readonly contractVersion?: string;
|
|
26
|
+
}
|
|
27
|
+
/** One event the component dispatched on its element. */
|
|
28
|
+
export interface RecordedEvent {
|
|
29
|
+
/** The declared event name, without the `<tag>:` prefix. */
|
|
30
|
+
readonly name: string;
|
|
31
|
+
/** The validated, frozen `detail` the runtime dispatched. */
|
|
32
|
+
readonly detail: unknown;
|
|
33
|
+
}
|
|
34
|
+
/** A mounted component and the helpers to drive and inspect it. */
|
|
35
|
+
export interface MountedComponent<W> {
|
|
36
|
+
/** The custom element on the page. */
|
|
37
|
+
readonly element: HTMLElement;
|
|
38
|
+
/** The component's open shadow root. */
|
|
39
|
+
readonly shadowRoot: ShadowRoot;
|
|
40
|
+
/** The in-memory transport: `submissions` and `tracked` in order. */
|
|
41
|
+
readonly transport: MemoryTransport;
|
|
42
|
+
/** Every declared event dispatched since the mount, in order. */
|
|
43
|
+
readonly events: readonly RecordedEvent[];
|
|
44
|
+
/** Runtime warnings for this component since the mount (refused attributes, dropped events…). */
|
|
45
|
+
readonly warnings: readonly RuntimeWarning[];
|
|
46
|
+
/** Failures the error boundary caught for this component since the mount (the fallback shows). */
|
|
47
|
+
readonly failures: readonly ComponentFailure[];
|
|
48
|
+
/**
|
|
49
|
+
* For a form field (`formAssociated: true`): what the component set on its internals so far, read
|
|
50
|
+
* live (`form.value` as given to `setFormValue`, `form.validity.valid`, `form.validationMessage`
|
|
51
|
+
* and its `anchor`; TAG-235). `undefined` for any other component.
|
|
52
|
+
*/
|
|
53
|
+
readonly form: FormFieldRecord | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* The first element in the shadow root matching `selector`, typed by tag name like
|
|
56
|
+
* `querySelector` (`query("input")` is an `HTMLInputElement`); throws when there is none.
|
|
57
|
+
*/
|
|
58
|
+
query<K extends keyof HTMLElementTagNameMap>(selector: K): HTMLElementTagNameMap[K];
|
|
59
|
+
query(selector: string): HTMLElement;
|
|
60
|
+
/** Every element in the shadow root matching `selector`, typed by tag name like `query`. */
|
|
61
|
+
queryAll<K extends keyof HTMLElementTagNameMap>(selector: K): HTMLElementTagNameMap[K][];
|
|
62
|
+
queryAll(selector: string): HTMLElement[];
|
|
63
|
+
/** Sets (or, with `null`, removes) an attribute and waits for the re-render. */
|
|
64
|
+
setAttribute(name: string, value: string | null): Promise<void>;
|
|
65
|
+
/** Sets props by property and waits for the re-render. */
|
|
66
|
+
setProps(props: MountOptions<W>["props"]): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Runs `interaction` (a click, typing) as the visitor and waits until the component settled:
|
|
69
|
+
* effects, re-renders, pending promise callbacks such as a submission's answer, and pending
|
|
70
|
+
* `crypto.subtle` digests such as the consent label's hash (TAG-2236). The events the
|
|
71
|
+
* interaction dispatches synchronously are trusted (`event.isTrusted`), as a visitor's input is;
|
|
72
|
+
* the ones dispatched later are not (TAG-237: consent is ticked only inside a visitor's action).
|
|
73
|
+
*/
|
|
74
|
+
act(interaction?: () => unknown): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* The `detail` of the last `name` event the component dispatched; throws, listing what was
|
|
77
|
+
* dispatched, when there was none.
|
|
78
|
+
*/
|
|
79
|
+
expectEvent<K extends keyof ComponentEventsOf<W> & string>(name: K): DeepReadonly<EventDetail<ComponentEventsOf<W>, K>>;
|
|
80
|
+
/** Removes the element from the page and waits for the unmount (effect cleanups, abort). */
|
|
81
|
+
unmount(): Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Mounts a component on the page the way the runtime does, for a unit test.
|
|
85
|
+
*
|
|
86
|
+
* Inputs: a definition made by `defineTaggorComponent` and `MountOptions` (`attrs`, `props`, `slots`,
|
|
87
|
+
* `respond`, `debug`, `contractVersion`). The tag is registered once per test file through
|
|
88
|
+
* `createComponentElement` (later mounts reuse it); a form field's class gets the recording
|
|
89
|
+
* `attachInternals` then (TAG-235). Output: a `MountedComponent`, after the first render
|
|
90
|
+
* and its effects. Errors: rejects when the runtime refuses to define the tag (another component took
|
|
91
|
+
* it in this file, or the contract version is unusable); a component that throws while rendering is
|
|
92
|
+
* not an error here: it shows its fallback and the failure is in `failures`. Side effects: defines
|
|
93
|
+
* the tag in `customElements`, appends the element to `document.body`, listens to its declared
|
|
94
|
+
* events and, once per document, wraps `crypto.subtle.digest` in a pass-through that records the
|
|
95
|
+
* pending digests, so `mountComponent` and every helper that settles wait for them (TAG-2236).
|
|
96
|
+
*/
|
|
97
|
+
export declare function mountComponent<W extends MountableComponent>(definition: W, options?: MountOptions<W>): Promise<MountedComponent<W>>;
|
|
98
|
+
/**
|
|
99
|
+
* Unmounts every component `mountComponent` put on the page and not yet unmounted; for an `afterEach`.
|
|
100
|
+
*
|
|
101
|
+
* Inputs: none. Output: resolves after the unmounts (effect cleanups, aborted signals). Errors:
|
|
102
|
+
* none. Side effects: removes those elements from the document.
|
|
103
|
+
*/
|
|
104
|
+
export declare function cleanup(): Promise<void>;
|
|
105
|
+
//# sourceMappingURL=mount-component.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mount-component.d.ts","sourceRoot":"","sources":["../../src/testing/mount-component.ts"],"names":[],"mappings":"AAyBA,OAAO,EAEL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EAEtB,MAAM,iBAAiB,CAAC;AAIzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAClF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,eAAe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,IAAI,CAAC,CAAC;CAC1B;AA0JD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,cAAc,CAAC,CAAC,SAAS,kBAAkB,EAC/D,UAAU,EAAE,CAAC,EACb,OAAO,GAAE,YAAY,CAAC,CAAC,CAAM,GAC5B,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CA6E9B;AAED;;;;;GAKG;AACH,wBAAsB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAK7C"}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mountComponent` (TAG-209): mounts a component definition for an author's unit test, in the runtime's
|
|
3
|
+
* real Custom Element wrapper (`createComponentElement` of `@taggor/runtime`, TAG-302), so the test
|
|
4
|
+
* sees what a visitor's page sees: an open shadow root, attributes coerced by the contract, props
|
|
5
|
+
* deep-frozen (TAG-304, TAG-227), events validated and dispatched as `<tag>:<event>` (TAG-305),
|
|
6
|
+
* slots projected natively and the error boundary's fallback. Nothing here re-implements the
|
|
7
|
+
* runtime; the additions are the data transport, an in-memory one (`createMemoryTransport`),
|
|
8
|
+
* because the runtime's host has no transport until the loader injects one (TAG-301), and, for a
|
|
9
|
+
* form field (`formAssociated: true`, TAG-314), recording `ElementInternals` on the component's
|
|
10
|
+
* element class (happy-dom has none; TAG-235), which the runtime attaches and hands to the component
|
|
11
|
+
* as on a page, so `form` shows the value and validity the component set.
|
|
12
|
+
*
|
|
13
|
+
* `act(interaction)` runs the interaction as the visitor (`asVisitor`, TAG-237): the events it
|
|
14
|
+
* dispatches synchronously are trusted, as a visitor's own click is, so `<Consent>` records the
|
|
15
|
+
* tick; events dispatched anywhere else (attribute and prop changes, timers) stay untrusted.
|
|
16
|
+
*
|
|
17
|
+
* Settling waits for the state, never for a time: one macrotask for promise callbacks, then every
|
|
18
|
+
* pending `crypto.subtle` digest (the consent hash runs on Node's threadpool and may land after
|
|
19
|
+
* any number of macrotasks under load; TAG-2236).
|
|
20
|
+
*
|
|
21
|
+
* Needs a DOM: Vitest with `environment: "happy-dom"` (or `// @vitest-environment happy-dom`).
|
|
22
|
+
* Test-only code: it lives in the `@taggor/component/testing` entry and never reaches a component bundle
|
|
23
|
+
* (`bundle.test.ts`).
|
|
24
|
+
*/
|
|
25
|
+
import { CONTRACT_VERSION } from "@taggor/contract/zod-free";
|
|
26
|
+
import { createComponentElement, } from "@taggor/runtime";
|
|
27
|
+
import { h } from "preact";
|
|
28
|
+
import { act } from "preact/test-utils";
|
|
29
|
+
import { createMemoryTransport, } from "../memory-transport.js";
|
|
30
|
+
import { DEFAULT_SLOT } from "../slots.js";
|
|
31
|
+
import { installFormInternals, readFormField } from "./form-internals.js";
|
|
32
|
+
import { asVisitor } from "./visitor.js";
|
|
33
|
+
/** Definitions already registered in this document's registry (one per test file in Vitest). */
|
|
34
|
+
const registrations = new WeakMap();
|
|
35
|
+
/** The test host extras of each mounted element, read by the wrapping component. */
|
|
36
|
+
const extrasByElement = new WeakMap();
|
|
37
|
+
/** The elements mounted and not yet unmounted, for `cleanup`. */
|
|
38
|
+
const mounted = new Set();
|
|
39
|
+
/**
|
|
40
|
+
* The definition with its component wrapped to add the test transport and debug flag to the
|
|
41
|
+
* runtime's host. The host keeps the runtime's live getters (`shadowRoot`, `signal`), its `emit`,
|
|
42
|
+
* so events still go through the runtime's validation, and its `internals` (TAG-314; for a form
|
|
43
|
+
* field, the recording ones of `installFormInternals`, TAG-235).
|
|
44
|
+
*/
|
|
45
|
+
function withTestHost(definition) {
|
|
46
|
+
const hosts = new WeakMap();
|
|
47
|
+
const testHost = (host) => {
|
|
48
|
+
let found = hosts.get(host);
|
|
49
|
+
if (found === undefined) {
|
|
50
|
+
const extras = extrasByElement.get(host.element);
|
|
51
|
+
found = Object.freeze({
|
|
52
|
+
element: host.element,
|
|
53
|
+
get shadowRoot() {
|
|
54
|
+
return host.shadowRoot;
|
|
55
|
+
},
|
|
56
|
+
get signal() {
|
|
57
|
+
return host.signal;
|
|
58
|
+
},
|
|
59
|
+
emit: host.emit,
|
|
60
|
+
internals: host.internals,
|
|
61
|
+
transport: extras?.transport,
|
|
62
|
+
debug: extras?.debug,
|
|
63
|
+
});
|
|
64
|
+
hosts.set(host, found);
|
|
65
|
+
}
|
|
66
|
+
return found;
|
|
67
|
+
};
|
|
68
|
+
const component = definition.component;
|
|
69
|
+
return Object.freeze({
|
|
70
|
+
...definition,
|
|
71
|
+
component: ({ host, props }) => h(component, { host: testHost(host), props }),
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/** Registers `component` once per document, through the runtime's own `createComponentElement`. */
|
|
75
|
+
function register(definition, contractVersion) {
|
|
76
|
+
const known = registrations.get(definition);
|
|
77
|
+
if (known !== undefined)
|
|
78
|
+
return known;
|
|
79
|
+
const registration = { tag: definition.manifest.tag, warnings: [], failures: [] };
|
|
80
|
+
const outcome = createComponentElement(withTestHost(definition), {
|
|
81
|
+
contractVersion,
|
|
82
|
+
diagnostics: {
|
|
83
|
+
warn: (warning) => registration.warnings.push(warning),
|
|
84
|
+
reportError: (failure) => registration.failures.push(failure),
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
if (outcome.status !== "defined") {
|
|
88
|
+
const why = registration.warnings.map((warning) => warning.message).join("; ");
|
|
89
|
+
throw new Error(`mountComponent: the runtime did not define <${outcome.tag}> (${outcome.status}: ${why}); ` +
|
|
90
|
+
"in one test file, each tag must belong to one component");
|
|
91
|
+
}
|
|
92
|
+
// Before any element of the tag exists: the runtime attaches internals in the constructor.
|
|
93
|
+
const elementClass = customElements.get(outcome.tag);
|
|
94
|
+
if (definition.manifest.formAssociated === true && elementClass !== undefined) {
|
|
95
|
+
installFormInternals(elementClass);
|
|
96
|
+
}
|
|
97
|
+
registrations.set(definition, registration);
|
|
98
|
+
return registration;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Digests started through `crypto.subtle` and not yet settled. `useConsent` hashes its label with
|
|
102
|
+
* `crypto.subtle.digest` in an effect (TAG-212); Node runs that on the libuv threadpool, so it can
|
|
103
|
+
* resolve after any number of macrotasks when the pool is busy (TAG-2236: a loaded `make check`
|
|
104
|
+
* submitted before the proof existed). Tracking them lets `settle` wait for the state, not a time.
|
|
105
|
+
*/
|
|
106
|
+
const pendingDigests = new Set();
|
|
107
|
+
let digestsTracked = false;
|
|
108
|
+
/**
|
|
109
|
+
* Wraps `crypto.subtle.digest` once per document so `settle` sees every pending digest; a
|
|
110
|
+
* pass-through (same arguments, same promise), shadowing the prototype method on this instance.
|
|
111
|
+
*/
|
|
112
|
+
function trackDigests() {
|
|
113
|
+
if (digestsTracked)
|
|
114
|
+
return;
|
|
115
|
+
digestsTracked = true;
|
|
116
|
+
// Absent outside a secure context or in an environment without webcrypto, whatever the types say.
|
|
117
|
+
const subtle = globalThis.crypto?.subtle;
|
|
118
|
+
if (subtle === undefined)
|
|
119
|
+
return;
|
|
120
|
+
const digest = subtle.digest.bind(subtle);
|
|
121
|
+
Object.defineProperty(subtle, "digest", {
|
|
122
|
+
configurable: true,
|
|
123
|
+
writable: true,
|
|
124
|
+
value: (...args) => {
|
|
125
|
+
const promise = digest(...args);
|
|
126
|
+
const done = () => pendingDigests.delete(promise);
|
|
127
|
+
pendingDigests.add(promise);
|
|
128
|
+
promise.then(done, done);
|
|
129
|
+
return promise;
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/** One macrotask: a memory transport's answer and the re-render it causes land before it. */
|
|
134
|
+
const macrotask = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
135
|
+
/** Lets effects, re-renders, pending promise callbacks and pending digests run. */
|
|
136
|
+
async function settle(interaction) {
|
|
137
|
+
await act(async () => {
|
|
138
|
+
await interaction?.();
|
|
139
|
+
await macrotask();
|
|
140
|
+
});
|
|
141
|
+
// Effects run when `act` finishes, so a digest they start is waited for in an `act` of its own;
|
|
142
|
+
// the re-render it causes may start another (a new label), hence the loop.
|
|
143
|
+
while (pendingDigests.size > 0) {
|
|
144
|
+
await act(async () => {
|
|
145
|
+
await Promise.allSettled([...pendingDigests]);
|
|
146
|
+
await macrotask();
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function appendSlots(element, slots) {
|
|
151
|
+
for (const [name, content] of Object.entries(slots)) {
|
|
152
|
+
const isDefault = name === DEFAULT_SLOT;
|
|
153
|
+
let node;
|
|
154
|
+
if (typeof content === "string") {
|
|
155
|
+
node = isDefault ? document.createTextNode(content) : document.createElement("span");
|
|
156
|
+
if (!isDefault)
|
|
157
|
+
node.textContent = content;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
node = content;
|
|
161
|
+
}
|
|
162
|
+
if (!isDefault && node instanceof Element)
|
|
163
|
+
node.setAttribute("slot", name);
|
|
164
|
+
element.append(node);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function setProperties(element, props) {
|
|
168
|
+
for (const [name, value] of Object.entries(props ?? {})) {
|
|
169
|
+
Reflect.set(element, name, value);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Mounts a component on the page the way the runtime does, for a unit test.
|
|
174
|
+
*
|
|
175
|
+
* Inputs: a definition made by `defineTaggorComponent` and `MountOptions` (`attrs`, `props`, `slots`,
|
|
176
|
+
* `respond`, `debug`, `contractVersion`). The tag is registered once per test file through
|
|
177
|
+
* `createComponentElement` (later mounts reuse it); a form field's class gets the recording
|
|
178
|
+
* `attachInternals` then (TAG-235). Output: a `MountedComponent`, after the first render
|
|
179
|
+
* and its effects. Errors: rejects when the runtime refuses to define the tag (another component took
|
|
180
|
+
* it in this file, or the contract version is unusable); a component that throws while rendering is
|
|
181
|
+
* not an error here: it shows its fallback and the failure is in `failures`. Side effects: defines
|
|
182
|
+
* the tag in `customElements`, appends the element to `document.body`, listens to its declared
|
|
183
|
+
* events and, once per document, wraps `crypto.subtle.digest` in a pass-through that records the
|
|
184
|
+
* pending digests, so `mountComponent` and every helper that settles wait for them (TAG-2236).
|
|
185
|
+
*/
|
|
186
|
+
export async function mountComponent(definition, options = {}) {
|
|
187
|
+
trackDigests();
|
|
188
|
+
const registration = register(definition, options.contractVersion ?? CONTRACT_VERSION);
|
|
189
|
+
const { tag } = registration;
|
|
190
|
+
const since = { warnings: registration.warnings.length, failures: registration.failures.length };
|
|
191
|
+
const transport = createMemoryTransport(options.respond);
|
|
192
|
+
const element = document.createElement(tag);
|
|
193
|
+
extrasByElement.set(element, { transport, debug: options.debug ?? false });
|
|
194
|
+
const events = [];
|
|
195
|
+
for (const name of Object.keys(definition.manifest.events ?? {})) {
|
|
196
|
+
element.addEventListener(`${tag}:${name}`, (event) => {
|
|
197
|
+
events.push({ name, detail: event.detail });
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
for (const [name, value] of Object.entries(options.attrs ?? {}))
|
|
201
|
+
element.setAttribute(name, value);
|
|
202
|
+
setProperties(element, options.props);
|
|
203
|
+
appendSlots(element, options.slots ?? {});
|
|
204
|
+
await settle(() => {
|
|
205
|
+
document.body.append(element);
|
|
206
|
+
mounted.add(element);
|
|
207
|
+
});
|
|
208
|
+
// The runtime's `connect` attaches the open shadow root before any component code runs.
|
|
209
|
+
const shadowRoot = element.shadowRoot;
|
|
210
|
+
const query = (selector) => {
|
|
211
|
+
const found = shadowRoot.querySelector(selector);
|
|
212
|
+
if (found === null) {
|
|
213
|
+
throw new Error(`mountComponent: nothing matches "${selector}" in the shadow root of <${tag}>`);
|
|
214
|
+
}
|
|
215
|
+
return found;
|
|
216
|
+
};
|
|
217
|
+
return {
|
|
218
|
+
element,
|
|
219
|
+
shadowRoot,
|
|
220
|
+
transport,
|
|
221
|
+
events,
|
|
222
|
+
get warnings() {
|
|
223
|
+
return registration.warnings.slice(since.warnings);
|
|
224
|
+
},
|
|
225
|
+
get failures() {
|
|
226
|
+
return registration.failures.slice(since.failures);
|
|
227
|
+
},
|
|
228
|
+
get form() {
|
|
229
|
+
return readFormField(element);
|
|
230
|
+
},
|
|
231
|
+
query,
|
|
232
|
+
queryAll: (selector) => [...shadowRoot.querySelectorAll(selector)],
|
|
233
|
+
setAttribute: (name, value) => settle(() => {
|
|
234
|
+
if (value === null)
|
|
235
|
+
element.removeAttribute(name);
|
|
236
|
+
else
|
|
237
|
+
element.setAttribute(name, value);
|
|
238
|
+
}),
|
|
239
|
+
setProps: (props) => settle(() => {
|
|
240
|
+
setProperties(element, props);
|
|
241
|
+
}),
|
|
242
|
+
act: (interaction) => settle(interaction && (() => asVisitor(interaction))),
|
|
243
|
+
expectEvent: (name) => {
|
|
244
|
+
const found = events.findLast((event) => event.name === name);
|
|
245
|
+
if (found === undefined) {
|
|
246
|
+
const seen = events.length === 0 ? "none" : events.map((event) => event.name).join(", ");
|
|
247
|
+
throw new Error(`mountComponent: <${tag}> did not emit "${name}" (emitted: ${seen})`);
|
|
248
|
+
}
|
|
249
|
+
return found.detail;
|
|
250
|
+
},
|
|
251
|
+
unmount: () => settle(() => {
|
|
252
|
+
element.remove();
|
|
253
|
+
mounted.delete(element);
|
|
254
|
+
}),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Unmounts every component `mountComponent` put on the page and not yet unmounted; for an `afterEach`.
|
|
259
|
+
*
|
|
260
|
+
* Inputs: none. Output: resolves after the unmounts (effect cleanups, aborted signals). Errors:
|
|
261
|
+
* none. Side effects: removes those elements from the document.
|
|
262
|
+
*/
|
|
263
|
+
export async function cleanup() {
|
|
264
|
+
await settle(() => {
|
|
265
|
+
for (const element of mounted)
|
|
266
|
+
element.remove();
|
|
267
|
+
mounted.clear();
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
//# sourceMappingURL=mount-component.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mount-component.js","sourceRoot":"","sources":["../../src/testing/mount-component.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BA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AAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACzF,MAAM,IAAI,KAAK,CAAC,oBAAoB,GAAG,mBAAmB,IAAI,eAAe,IAAI,GAAG,CAAC,CAAC;YACxF,CAAC;YACD,OAAO,KAAK,CAAC,MAAe,CAAC;QAC/B,CAAC;QACD,OAAO,EAAE,GAAG,EAAE,CACZ,MAAM,CAAC,GAAG,EAAE;YACV,OAAO,CAAC,MAAM,EAAE,CAAC;YACjB,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1B,CAAC,CAAC;KACL,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO;IAC3B,MAAM,MAAM,CAAC,GAAG,EAAE;QAChB,KAAK,MAAM,OAAO,IAAI,OAAO;YAAE,OAAO,CAAC,MAAM,EAAE,CAAC;QAChD,OAAO,CAAC,KAAK,EAAE,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visitor actions in a test (TAG-237): `useConsent` records a tick only inside a trusted event
|
|
3
|
+
* (`event.isTrusted`, set by the browser for the visitor's own input), and happy-dom marks no
|
|
4
|
+
* event trusted, so a test's `click()` would never tick consent. `asVisitor` runs an interaction
|
|
5
|
+
* as the visitor: every event dispatched synchronously inside it reads `isTrusted: true`. Events
|
|
6
|
+
* dispatched later (a timer, a promise callback, an effect) stay untrusted, as on a page.
|
|
7
|
+
*
|
|
8
|
+
* Works by a getter on the root event prototype of the DOM in use (happy-dom's `Event`), removed
|
|
9
|
+
* when the interaction returns. In a real browser `isTrusted` is an own, unforgeable property of
|
|
10
|
+
* every event, which shadows the getter: there `asVisitor` changes nothing, and a real input
|
|
11
|
+
* (Playwright) is what makes an event trusted. Test-only: reached from `@taggor/component/testing`
|
|
12
|
+
* and the SDK's own tests, never from a component bundle (`bundle.test.ts`).
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Runs `interaction` as the visitor.
|
|
16
|
+
*
|
|
17
|
+
* Inputs: the interaction (a `click()`, a `dispatchEvent`). Output: what it returns (a promise is
|
|
18
|
+
* returned as is; only its synchronous part runs as the visitor). Errors: rethrows what the
|
|
19
|
+
* interaction throws. Side effects: while it runs, `isTrusted` reads true on every event of the
|
|
20
|
+
* global `Event` prototype chain that has no own `isTrusted`; nested calls keep the outer one's.
|
|
21
|
+
*/
|
|
22
|
+
export declare function asVisitor<T>(interaction: () => T): T;
|
|
23
|
+
//# sourceMappingURL=visitor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"visitor.d.ts","sourceRoot":"","sources":["../../src/testing/visitor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,WAAW,EAAE,MAAM,CAAC,GAAG,CAAC,CASpD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visitor actions in a test (TAG-237): `useConsent` records a tick only inside a trusted event
|
|
3
|
+
* (`event.isTrusted`, set by the browser for the visitor's own input), and happy-dom marks no
|
|
4
|
+
* event trusted, so a test's `click()` would never tick consent. `asVisitor` runs an interaction
|
|
5
|
+
* as the visitor: every event dispatched synchronously inside it reads `isTrusted: true`. Events
|
|
6
|
+
* dispatched later (a timer, a promise callback, an effect) stay untrusted, as on a page.
|
|
7
|
+
*
|
|
8
|
+
* Works by a getter on the root event prototype of the DOM in use (happy-dom's `Event`), removed
|
|
9
|
+
* when the interaction returns. In a real browser `isTrusted` is an own, unforgeable property of
|
|
10
|
+
* every event, which shadows the getter: there `asVisitor` changes nothing, and a real input
|
|
11
|
+
* (Playwright) is what makes an event trusted. Test-only: reached from `@taggor/component/testing`
|
|
12
|
+
* and the SDK's own tests, never from a component bundle (`bundle.test.ts`).
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Runs `interaction` as the visitor.
|
|
16
|
+
*
|
|
17
|
+
* Inputs: the interaction (a `click()`, a `dispatchEvent`). Output: what it returns (a promise is
|
|
18
|
+
* returned as is; only its synchronous part runs as the visitor). Errors: rethrows what the
|
|
19
|
+
* interaction throws. Side effects: while it runs, `isTrusted` reads true on every event of the
|
|
20
|
+
* global `Event` prototype chain that has no own `isTrusted`; nested calls keep the outer one's.
|
|
21
|
+
*/
|
|
22
|
+
export function asVisitor(interaction) {
|
|
23
|
+
const prototype = Event.prototype;
|
|
24
|
+
if (Object.hasOwn(prototype, "isTrusted"))
|
|
25
|
+
return interaction();
|
|
26
|
+
Object.defineProperty(prototype, "isTrusted", { configurable: true, get: () => true });
|
|
27
|
+
try {
|
|
28
|
+
return interaction();
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
Reflect.deleteProperty(prototype, "isTrusted");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=visitor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"visitor.js","sourceRoot":"","sources":["../../src/testing/visitor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAI,WAAoB;IAC/C,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC;IAClC,IAAI,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,WAAW,CAAC;QAAE,OAAO,WAAW,EAAE,CAAC;IAChE,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;IACvF,IAAI,CAAC;QACH,OAAO,WAAW,EAAE,CAAC;IACvB,CAAC;YAAS,CAAC;QACT,OAAO,CAAC,cAAc,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;IACjD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Theming helpers for authors (TAG-206): `cssVar` reads a declared public CSS variable and
|
|
3
|
+
* `part` names declared parts, so the styling API a host page relies on (`--fs-primary-color`,
|
|
4
|
+
* `finance-simulator::part(button)`) is exactly the one the manifest declares. Both are typed by
|
|
5
|
+
* the component's `cssVars` and `parts`: an undeclared name is a compile error. The builder checks
|
|
6
|
+
* the same thing on the final CSS with `checkStyleReferences` from `@taggor/contract`.
|
|
7
|
+
*
|
|
8
|
+
* `defineTaggorComponent` builds one `Theme` per definition and hands it to `styles` (when it is a
|
|
9
|
+
* function) and to `render`'s context. Pure string building: no DOM, no global, nothing parsed.
|
|
10
|
+
*/
|
|
11
|
+
import type { ComponentManifest } from "@taggor/contract";
|
|
12
|
+
/** The `parts` map of a component: part name (kebab-case) → info. */
|
|
13
|
+
export type PartsDeclaration = NonNullable<ComponentManifest["parts"]>;
|
|
14
|
+
/** The `cssVars` map of a component: full custom property name (`--fs-primary-color`) → declaration. */
|
|
15
|
+
export type CssVarsDeclaration = NonNullable<ComponentManifest["cssVars"]>;
|
|
16
|
+
/** Names of the parts declared in `R`. */
|
|
17
|
+
export type PartName<R> = keyof R & string;
|
|
18
|
+
/** Names of the CSS variables declared in `C`. */
|
|
19
|
+
export type CssVarName<C> = keyof C & string;
|
|
20
|
+
/**
|
|
21
|
+
* The theming helpers of one component.
|
|
22
|
+
*
|
|
23
|
+
* - `cssVar(name)`: `var(<name>, <declared default>)`, ready for a stylesheet or an inline style;
|
|
24
|
+
* the host page overrides it by setting the variable on the element (`finance-simulator {
|
|
25
|
+
* --fs-primary-color: #c2410c }`), and the default applies otherwise.
|
|
26
|
+
* - `part(...names)`: the value of a `part` attribute (`h("button", { part: part("button") })`),
|
|
27
|
+
* the names joined by spaces; the host page styles it with `::part(name)`.
|
|
28
|
+
*/
|
|
29
|
+
export interface Theme<R, C> {
|
|
30
|
+
readonly cssVar: (name: CssVarName<C>) => string;
|
|
31
|
+
readonly part: (...names: [PartName<R>, ...PartName<R>[]]) => string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Builds the theming helpers of a manifest.
|
|
35
|
+
*
|
|
36
|
+
* Inputs: the manifest draft's `cssVars` (typed `C`; only its own keys are read). Output: a
|
|
37
|
+
* frozen `Theme`. `cssVar` of a name without a declaration (untyped code) gives `var(<name>)`
|
|
38
|
+
* with no fallback, which the builder's style check then reports. Errors: none. Side effects:
|
|
39
|
+
* none.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createTheme<R, C>(cssVars: C | undefined): Theme<R, C>;
|
|
42
|
+
//# sourceMappingURL=theming.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theming.d.ts","sourceRoot":"","sources":["../src/theming.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE1D,qEAAqE;AACrE,MAAM,MAAM,gBAAgB,GAAG,WAAW,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC;AAEvE,wGAAwG;AACxG,MAAM,MAAM,kBAAkB,GAAG,WAAW,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC,CAAC;AAE3E,0CAA0C;AAC1C,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,CAAC;AAE3C,kDAAkD;AAClD,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,CAAC;AAE7C;;;;;;;;GAQG;AACH,MAAM,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC;IACjD,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC;CACtE;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,GAAG,SAAS,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CASrE"}
|
package/dist/theming.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the theming helpers of a manifest.
|
|
3
|
+
*
|
|
4
|
+
* Inputs: the manifest draft's `cssVars` (typed `C`; only its own keys are read). Output: a
|
|
5
|
+
* frozen `Theme`. `cssVar` of a name without a declaration (untyped code) gives `var(<name>)`
|
|
6
|
+
* with no fallback, which the builder's style check then reports. Errors: none. Side effects:
|
|
7
|
+
* none.
|
|
8
|
+
*/
|
|
9
|
+
export function createTheme(cssVars) {
|
|
10
|
+
const declared = (cssVars ?? {});
|
|
11
|
+
return Object.freeze({
|
|
12
|
+
cssVar: (name) => {
|
|
13
|
+
const fallback = Object.hasOwn(declared, name) ? declared[name]?.default : undefined;
|
|
14
|
+
return typeof fallback === "string" ? `var(${name}, ${fallback})` : `var(${name})`;
|
|
15
|
+
},
|
|
16
|
+
part: (...names) => names.join(" "),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=theming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theming.js","sourceRoot":"","sources":["../src/theming.ts"],"names":[],"mappings":"AAsCA;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CAAO,OAAsB;IACtD,MAAM,QAAQ,GAAG,CAAC,OAAO,IAAI,EAAE,CAA6D,CAAC;IAC7F,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,MAAM,EAAE,CAAC,IAAY,EAAE,EAAE;YACvB,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;YACrF,OAAO,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,IAAI,KAAK,QAAQ,GAAG,CAAC,CAAC,CAAC,OAAO,IAAI,GAAG,CAAC;QACrF,CAAC;QACD,IAAI,EAAE,CAAC,GAAG,KAAe,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;KAC9C,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Turnstile client (TAG-709): loads Cloudflare's script on demand and solves one challenge
|
|
3
|
+
* per submission, in the invisible way, for the data transport (`data-client.ts`). Nothing here
|
|
4
|
+
* runs at import and nothing is loaded until the first `token()` call: a page whose components never
|
|
5
|
+
* submit never contacts `challenges.cloudflare.com` (golden rule 6), and the base loader never
|
|
6
|
+
* pulls this module (it is the lazy data module, `docs/agentes/runtime.md` §2.19).
|
|
7
|
+
*
|
|
8
|
+
* Where the challenge lives (the spike's decision, runtime.md §2.19): in a dedicated `<div>` the
|
|
9
|
+
* client appends to `<body>`, out of every component, with its own closed shadow root holding the
|
|
10
|
+
* container Turnstile renders into. `appearance: "interaction-only"` makes a managed key behave
|
|
11
|
+
* as invisible until Cloudflare needs the visitor, and only then (`before-interactive-callback`)
|
|
12
|
+
* is the box shown, fixed at the bottom right; an invisible-type key never shows it. The host is
|
|
13
|
+
* removed as soon as the token (or a failure) is known, so nothing stays in the page.
|
|
14
|
+
*
|
|
15
|
+
* TAG-310: under a strict CSP the injected script carries the page's nonce and, when the page
|
|
16
|
+
* requires Trusted Types, a `TrustedScriptURL` from the loader's `taggor` policy (see
|
|
17
|
+
* `docs/guias/csp-e-taggor.md`).
|
|
18
|
+
*
|
|
19
|
+
* Golden rule 2: `token()` never rejects and never throws; a script that does not load, a
|
|
20
|
+
* Turnstile error, a timeout, a missing `turnstile` global or an abort all resolve to `undefined`
|
|
21
|
+
* (the caller reports `captcha_failed` and the component shows its friendly message).
|
|
22
|
+
*/
|
|
23
|
+
/** What `createTurnstile` needs. */
|
|
24
|
+
export interface TurnstileOptions {
|
|
25
|
+
/** The site's Turnstile public key (the embed carries it; tests use Cloudflare's test keys). */
|
|
26
|
+
readonly siteKey: string;
|
|
27
|
+
/** The document to load into (default: the page's). */
|
|
28
|
+
readonly document?: Document | undefined;
|
|
29
|
+
/** Milliseconds before a load or a challenge is given up on (default 30 s). */
|
|
30
|
+
readonly timeoutMs?: number | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* The page's CSP nonce (TAG-310): set on the injected `<script>`, so a page whose `script-src`
|
|
33
|
+
* is a nonce (with or without `'strict-dynamic'`) lets it run without listing Cloudflare.
|
|
34
|
+
*/
|
|
35
|
+
readonly nonce?: string | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Turns the script URL into what `script.src` takes (TAG-310): the loader's `taggor` Trusted
|
|
38
|
+
* Types policy, so a page with `require-trusted-types-for 'script'` accepts the assignment.
|
|
39
|
+
* Default: the URL itself. May throw; the load then fails like any other.
|
|
40
|
+
*/
|
|
41
|
+
readonly scriptUrl?: ((url: string, origin: string) => unknown) | undefined;
|
|
42
|
+
}
|
|
43
|
+
/** Solves one challenge per call. */
|
|
44
|
+
export interface Turnstile {
|
|
45
|
+
/**
|
|
46
|
+
* Resolves to a fresh Turnstile token (single use), or `undefined` when none could be had.
|
|
47
|
+
* Never rejects. `signal` aborts the wait (the component was unmounted).
|
|
48
|
+
*/
|
|
49
|
+
readonly token: (signal?: AbortSignal) => Promise<string | undefined>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates the Turnstile client of one site.
|
|
53
|
+
*
|
|
54
|
+
* Inputs: `TurnstileOptions`. Output: a `Turnstile`. Errors: none. Side effects: none until
|
|
55
|
+
* `token()`; then one `<script>` in `<head>` (once per client; a failed load is tried again by
|
|
56
|
+
* the next call) and, for the length of a challenge, one host `<div>` in `<body>`. Turnstile
|
|
57
|
+
* itself talks to Cloudflare; this code reads no cookie and writes no storage.
|
|
58
|
+
*/
|
|
59
|
+
export declare function createTurnstile(options: TurnstileOptions): Turnstile;
|
|
60
|
+
//# sourceMappingURL=turnstile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"turnstile.d.ts","sourceRoot":"","sources":["../src/turnstile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAqBH,oCAAoC;AACpC,MAAM,WAAW,gBAAgB;IAC/B,gGAAgG;IAChG,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,uDAAuD;IACvD,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;IACzC,+EAA+E;IAC/E,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,SAAS,CAAC;CAC7E;AAED,qCAAqC;AACrC,MAAM,WAAW,SAAS;IACxB;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;CACvE;AASD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CA+HpE"}
|