@softistx/pulsarix-widget 0.3.2 → 0.5.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/README.md +181 -21
- package/dist/index.js +35 -35
- package/dist/server.js +60 -0
- package/dist/types/config.d.ts +27 -1
- package/dist/types/element.d.ts +20 -46
- package/dist/types/index.d.ts +3 -2
- package/dist/types/mount.d.ts +75 -0
- package/dist/types/server.d.ts +22 -0
- package/docs/integration.md +550 -0
- package/package.json +29 -10
package/dist/server.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/server.ts
|
|
2
|
+
import { createHmac } from "node:crypto";
|
|
3
|
+
var MAX_TTL_SECONDS = 24 * 60 * 60;
|
|
4
|
+
var DEFAULT_TTL_SECONDS = 60 * 60;
|
|
5
|
+
var MAX_TOKEN_LENGTH = 2048;
|
|
6
|
+
var EMAIL = /^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9-]*\.)+[A-Za-z]{2,}$/;
|
|
7
|
+
function signPulsarixIdentity(options) {
|
|
8
|
+
const {
|
|
9
|
+
secret,
|
|
10
|
+
publicKey,
|
|
11
|
+
sub,
|
|
12
|
+
email,
|
|
13
|
+
ttlSeconds = DEFAULT_TTL_SECONDS,
|
|
14
|
+
now = Date.now
|
|
15
|
+
} = options;
|
|
16
|
+
if (typeof secret !== "string" || secret.length === 0) {
|
|
17
|
+
throw new Error("signPulsarixIdentity: `secret` must be a non-empty string");
|
|
18
|
+
}
|
|
19
|
+
if (typeof publicKey !== "string" || publicKey.length === 0) {
|
|
20
|
+
throw new Error("signPulsarixIdentity: `publicKey` must be the widget key's public key (pk_…)");
|
|
21
|
+
}
|
|
22
|
+
if (typeof sub !== "string" || sub.length < 1 || sub.length > 200) {
|
|
23
|
+
throw new Error("signPulsarixIdentity: `sub` must be 1 to 200 characters");
|
|
24
|
+
}
|
|
25
|
+
if (typeof ttlSeconds !== "number" || !Number.isInteger(ttlSeconds) || ttlSeconds < 1 || ttlSeconds > MAX_TTL_SECONDS) {
|
|
26
|
+
throw new Error(`signPulsarixIdentity: \`ttlSeconds\` must be a whole number from 1 to ${MAX_TTL_SECONDS} (24 hours)`);
|
|
27
|
+
}
|
|
28
|
+
if (email !== undefined && (typeof email !== "string" || email.length > 254 || !EMAIL.test(email))) {
|
|
29
|
+
throw new Error("signPulsarixIdentity: `email` must be a valid address of at most 254 characters");
|
|
30
|
+
}
|
|
31
|
+
if (options.name !== undefined && typeof options.name !== "string") {
|
|
32
|
+
throw new Error("signPulsarixIdentity: `name` must be a string");
|
|
33
|
+
}
|
|
34
|
+
const name = options.name?.trim();
|
|
35
|
+
if (name !== undefined && name.length > 100) {
|
|
36
|
+
throw new Error("signPulsarixIdentity: `name` must be at most 100 characters");
|
|
37
|
+
}
|
|
38
|
+
const iat = Math.floor(now() / 1000);
|
|
39
|
+
if (!Number.isFinite(iat)) {
|
|
40
|
+
throw new Error("signPulsarixIdentity: `now` must return milliseconds");
|
|
41
|
+
}
|
|
42
|
+
const claims = {
|
|
43
|
+
sub,
|
|
44
|
+
...email === undefined ? {} : { email },
|
|
45
|
+
...name ? { name } : {},
|
|
46
|
+
aud: publicKey,
|
|
47
|
+
iat,
|
|
48
|
+
exp: iat + ttlSeconds
|
|
49
|
+
};
|
|
50
|
+
const body = Buffer.from(JSON.stringify(claims), "utf8").toString("base64url");
|
|
51
|
+
const signature = createHmac("sha256", secret).update(body).digest("base64url");
|
|
52
|
+
const token = `${body}.${signature}`;
|
|
53
|
+
if (token.length > MAX_TOKEN_LENGTH) {
|
|
54
|
+
throw new Error(`signPulsarixIdentity: the token is ${token.length} characters, over the ${MAX_TOKEN_LENGTH} the API reads: shorten \`sub\`, \`email\` or \`name\``);
|
|
55
|
+
}
|
|
56
|
+
return token;
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
signPulsarixIdentity
|
|
60
|
+
};
|
package/dist/types/config.d.ts
CHANGED
|
@@ -2,13 +2,32 @@
|
|
|
2
2
|
export type Locale = "en" | "fr";
|
|
3
3
|
export type ThemeMode = "light" | "dark" | "auto";
|
|
4
4
|
export type Position = "bottom-right" | "bottom-left";
|
|
5
|
+
/** `auto` shows the icon below 640 px of viewport width and the text pill above it. */
|
|
6
|
+
export type LauncherMode = "auto" | "pill" | "icon";
|
|
7
|
+
/** A launcher offset: a number is pixels, a string is any CSS length (`"2rem"`, `"env(...)"`). */
|
|
8
|
+
export interface LauncherOffset {
|
|
9
|
+
bottom?: number | string;
|
|
10
|
+
side?: number | string;
|
|
11
|
+
}
|
|
5
12
|
export interface WidgetConfig {
|
|
6
13
|
projectKey: string | null;
|
|
7
14
|
locale: Locale;
|
|
8
15
|
theme: ThemeMode;
|
|
9
16
|
position: Position;
|
|
17
|
+
/** The launcher's look (`launcher="icon"`); `launcher="false"` is read by the element. */
|
|
18
|
+
launcherMode: LauncherMode;
|
|
19
|
+
/** The launcher's offsets (`offset-bottom`, `offset-side`), as CSS lengths, or null. */
|
|
20
|
+
offset: {
|
|
21
|
+
bottom: string | null;
|
|
22
|
+
side: string | null;
|
|
23
|
+
};
|
|
10
24
|
/** A valid hex seed from the host, or null: the built-in lavande theme then applies. */
|
|
11
25
|
accentColor: string | null;
|
|
26
|
+
/**
|
|
27
|
+
* The widget wears the host's `@softistx/material` theme when the host has one (`true`, the
|
|
28
|
+
* default); `data-inherit-theme="false"` keeps the widget's own look.
|
|
29
|
+
*/
|
|
30
|
+
inheritTheme: boolean;
|
|
12
31
|
/** Origin of the pulsarix host that serves `/public/graphql`; null until given. */
|
|
13
32
|
apiBase: string | null;
|
|
14
33
|
/** The "Mask the text on the page" option starts on (`data-mask-text="true"`). */
|
|
@@ -40,7 +59,14 @@ export declare const METADATA_KEY_MAX = 64;
|
|
|
40
59
|
export declare const METADATA_VALUE_MAX = 512;
|
|
41
60
|
export declare const METADATA_MAX_BYTES = 2048;
|
|
42
61
|
/** The attributes the element observes, in their HTML spelling. */
|
|
43
|
-
export declare const OBSERVED_ATTRIBUTES: readonly ["project-key", "locale", "theme", "position", "accent-color", "api-base", "mask-text", "attachments", "privacy-url", "prefill-email", "prefill-title", "metadata", "identity"];
|
|
62
|
+
export declare const OBSERVED_ATTRIBUTES: readonly ["project-key", "locale", "theme", "position", "launcher", "offset-bottom", "offset-side", "accent-color", "inherit-theme", "api-base", "mask-text", "attachments", "privacy-url", "prefill-email", "prefill-title", "metadata", "identity"];
|
|
63
|
+
/**
|
|
64
|
+
* A CSS length for the launcher offset (a number of pixels, or `<number><unit>`, or a balanced
|
|
65
|
+
* `calc()`, `min()`, `max()`, `clamp()`, `var()`, `env()` call), or null. A negative value, or
|
|
66
|
+
* anything else (`;`, `}`, quotes, a bare word), is dropped. The browser's `CSS.supports` has
|
|
67
|
+
* the last word where it exists.
|
|
68
|
+
*/
|
|
69
|
+
export declare function lengthOf(value: string | number | null | undefined): string | null;
|
|
44
70
|
/** A hex colour (#rgb or #rrggbb): the only form accepted as a seed. */
|
|
45
71
|
export declare function isHexColor(value: string | null): value is string;
|
|
46
72
|
/** An http(s) URL's origin (plus path prefix), or null: anything else is not a base. */
|
package/dist/types/element.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { type WidgetConfig } from "./config";
|
|
1
|
+
import { type Locale, type ThemeMode, type WidgetConfig } from "./config";
|
|
2
2
|
export declare const TAG_NAME = "pulsarix-widget";
|
|
3
|
+
/** Flat strings stored with each report. */
|
|
4
|
+
export type MountMetadata = Record<string, string | number | boolean>;
|
|
3
5
|
export type WidgetEvent = "pulsarix:ready" | "pulsarix:open" | "pulsarix:close" | "pulsarix:submit" | "pulsarix:reply" | "pulsarix:unread" | "pulsarix:erase" | "pulsarix:error";
|
|
4
6
|
/**
|
|
5
7
|
* `<pulsarix-widget>`: an open shadow root holding material's stylesheet, the
|
|
@@ -8,62 +10,34 @@ export type WidgetEvent = "pulsarix:ready" | "pulsarix:open" | "pulsarix:close"
|
|
|
8
10
|
*/
|
|
9
11
|
export declare class PulsarixWidgetElement extends HTMLElement {
|
|
10
12
|
#private;
|
|
11
|
-
static observedAttributes:
|
|
13
|
+
static observedAttributes: ("accent-color" | "api-base" | "attachments" | "identity" | "inherit-theme" | "launcher" | "locale" | "mask-text" | "metadata" | "offset-bottom" | "offset-side" | "position" | "prefill-email" | "prefill-title" | "privacy-url" | "project-key" | "theme")[];
|
|
12
14
|
get config(): WidgetConfig;
|
|
13
15
|
get isOpen(): boolean;
|
|
14
16
|
connectedCallback(): void;
|
|
15
17
|
disconnectedCallback(): void;
|
|
16
|
-
attributeChangedCallback(): void;
|
|
18
|
+
attributeChangedCallback(name: string): void;
|
|
17
19
|
/**
|
|
18
20
|
* Signs the reporter in with the identity token the host's server signed, or out with `null`
|
|
19
21
|
* (on the host's sign-out): the dialog then shows or drops New request and My requests.
|
|
20
22
|
*/
|
|
21
23
|
identify(token: string | null): void;
|
|
22
|
-
open
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
export declare function defineWidget(): void;
|
|
26
|
-
export interface MountOptions {
|
|
27
|
-
projectKey: string;
|
|
28
|
-
locale?: "en" | "fr";
|
|
29
|
-
theme?: "light" | "dark" | "auto";
|
|
30
|
-
position?: "bottom-left" | "bottom-right";
|
|
31
|
-
accentColor?: string;
|
|
32
|
-
/** Origin of the pulsarix host (`https://pulsarix.example.com`); the loader fills it in. */
|
|
33
|
-
apiBase?: string;
|
|
34
|
-
launcher?: boolean;
|
|
35
|
-
/** The screenshot's "Mask the text on the page" option starts on. */
|
|
36
|
-
maskText?: boolean;
|
|
37
|
-
/** `false` hides the screenshot and file controls (`data-attachments="false"`). */
|
|
38
|
-
attachments?: boolean;
|
|
39
|
-
/** Starting values of the form; the reporter can change them. */
|
|
40
|
-
prefill?: {
|
|
41
|
-
email?: string;
|
|
42
|
-
title?: string;
|
|
43
|
-
};
|
|
44
|
-
/** Flat strings stored with each report (20 entries, 2 KB at most; the rest is dropped). */
|
|
45
|
-
metadata?: Record<string, string | number | boolean>;
|
|
46
|
-
/** Link to your own privacy page, shown after the privacy line (http or https). */
|
|
47
|
-
privacyUrl?: string;
|
|
24
|
+
/** Shows or hides the launcher only; an open dialog stays. */
|
|
25
|
+
setLauncherVisible(visible: boolean): void;
|
|
48
26
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
27
|
+
* Changes the widget's own light or dark mode at run time. No remount: an open dialog and its
|
|
28
|
+
* draft stay as they are. `"auto"` follows the OS (`prefers-color-scheme`), never the host
|
|
29
|
+
* page's own mode: pass `"light"` or `"dark"` to follow the host's. With `inheritTheme`, an
|
|
30
|
+
* explicit `"light"` or `"dark"` wins over the host's mode, `"auto"` gives it back to the host.
|
|
51
31
|
*/
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
32
|
+
setTheme(theme: ThemeMode): void;
|
|
33
|
+
/** Changes the language of the strings at run time; the dialog and its draft stay. */
|
|
34
|
+
setLocale(locale: Locale): void;
|
|
35
|
+
/**
|
|
36
|
+
* Replaces the metadata sent with the next report (the same limits as `mount()`); `null` or
|
|
37
|
+
* `{}` clears it. A report already sent is not changed, and the draft stays.
|
|
38
|
+
*/
|
|
39
|
+
setMetadata(metadata: MountMetadata | null): void;
|
|
58
40
|
open(): void;
|
|
59
41
|
close(): void;
|
|
60
|
-
/** Signs the reporter in with a signed identity token, or out with `null`. */
|
|
61
|
-
identify(token: string | null): void;
|
|
62
|
-
destroy(): void;
|
|
63
|
-
on(name: WidgetEvent, callback: (event: CustomEvent) => void): () => void;
|
|
64
42
|
}
|
|
65
|
-
|
|
66
|
-
* Mounts the widget. One widget per page: while a mounted one is still attached, a second call
|
|
67
|
-
* returns its handle (and warns) instead of adding another element.
|
|
68
|
-
*/
|
|
69
|
-
export declare function mount(options: MountOptions): WidgetHandle;
|
|
43
|
+
export declare function defineWidget(): void;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export type { Locale, Position, ThemeMode, WidgetConfig } from "./config";
|
|
2
|
-
export { defineWidget, type
|
|
1
|
+
export type { LauncherMode, LauncherOffset, Locale, Position, ThemeMode, WidgetConfig, } from "./config";
|
|
2
|
+
export { defineWidget, type MountMetadata, PulsarixWidgetElement, TAG_NAME, type WidgetEvent, } from "./element";
|
|
3
|
+
export { type MountOptions, mount, type WidgetHandle } from "./mount";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type LauncherMode, type LauncherOffset, type Locale, type ThemeMode } from "./config";
|
|
2
|
+
import { type MountMetadata, type PulsarixWidgetElement, type WidgetEvent } from "./element";
|
|
3
|
+
export interface MountOptions {
|
|
4
|
+
projectKey: string;
|
|
5
|
+
locale?: "en" | "fr";
|
|
6
|
+
theme?: "light" | "dark" | "auto";
|
|
7
|
+
position?: "bottom-left" | "bottom-right";
|
|
8
|
+
accentColor?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Wear the host app's `@softistx/material` theme (colours, radius, light or dark) and follow
|
|
11
|
+
* its changes; a token the host lacks keeps the widget's own. `true` by default; an explicit
|
|
12
|
+
* `accentColor` still wins over the host's primary. `false` keeps the widget's own look.
|
|
13
|
+
*/
|
|
14
|
+
inheritTheme?: boolean;
|
|
15
|
+
/** Origin of the pulsarix host (`https://pulsarix.example.com`); the loader fills it in. */
|
|
16
|
+
apiBase?: string;
|
|
17
|
+
/** The floating button: `"auto"` (icon below 640 px, pill above), `"pill"`, `"icon"`, or `false`. */
|
|
18
|
+
launcher?: boolean | LauncherMode;
|
|
19
|
+
/** Launcher distance from the bottom and its side (16 px default): pixels, or a CSS length. */
|
|
20
|
+
offset?: LauncherOffset;
|
|
21
|
+
/** The screenshot's "Mask the text on the page" option starts on. */
|
|
22
|
+
maskText?: boolean;
|
|
23
|
+
/** `false` hides the screenshot and file controls (`data-attachments="false"`). */
|
|
24
|
+
attachments?: boolean;
|
|
25
|
+
/** Starting values of the form; the reporter can change them. */
|
|
26
|
+
prefill?: {
|
|
27
|
+
email?: string;
|
|
28
|
+
title?: string;
|
|
29
|
+
};
|
|
30
|
+
/** Flat strings stored with each report (20 entries, 2 KB at most; the rest is dropped). */
|
|
31
|
+
metadata?: MountMetadata;
|
|
32
|
+
/** Link to your own privacy page, shown after the privacy line (http or https). */
|
|
33
|
+
privacyUrl?: string;
|
|
34
|
+
/**
|
|
35
|
+
* The identity token your server signed for the signed-in user (`<payload>.<signature>`).
|
|
36
|
+
* With it the widget shows New request and My requests; change it later with `identify()`.
|
|
37
|
+
*/
|
|
38
|
+
identity?: string;
|
|
39
|
+
/** Where to append the element; `document.body` by default. */
|
|
40
|
+
target?: Element;
|
|
41
|
+
}
|
|
42
|
+
export interface WidgetHandle {
|
|
43
|
+
element: PulsarixWidgetElement;
|
|
44
|
+
open(): void;
|
|
45
|
+
close(): void;
|
|
46
|
+
/** Shows or hides the launcher only; an open dialog is never closed or hidden by it. */
|
|
47
|
+
setLauncherVisible(visible: boolean): void;
|
|
48
|
+
/**
|
|
49
|
+
* Hides the launcher until the returned function is called, for a host's own sticky bar.
|
|
50
|
+
* Ref-counted: the launcher stays hidden while any hold is open, and shows again once every
|
|
51
|
+
* hold is released and `setLauncherVisible` is not `false`. Releasing twice does nothing.
|
|
52
|
+
*/
|
|
53
|
+
holdLauncher(): () => void;
|
|
54
|
+
/** Signs the reporter in with a signed identity token, or out with `null`. */
|
|
55
|
+
identify(token: string | null): void;
|
|
56
|
+
/** Light or dark now, without a remount; `"auto"` follows the OS, not the host page. */
|
|
57
|
+
setTheme(theme: ThemeMode): void;
|
|
58
|
+
/**
|
|
59
|
+
* Switches the language. The widget's strings change at once; `@softistx/material`'s built-in labels
|
|
60
|
+
* follow when the form is idle (typed fields, type, impact and tab kept, attachments dropped), never
|
|
61
|
+
* during a send, on the confirmation or on the requests tab.
|
|
62
|
+
*/
|
|
63
|
+
setLocale(locale: Locale): void;
|
|
64
|
+
/** Replaces the metadata sent with the next report (`null` clears it). */
|
|
65
|
+
setMetadata(metadata: MountMetadata | null): void;
|
|
66
|
+
destroy(): void;
|
|
67
|
+
on(name: WidgetEvent, callback: (event: CustomEvent) => void): () => void;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Mounts the widget. One widget per page: while a mounted one is still attached, a second call
|
|
71
|
+
* returns its handle (and warns) instead of adding another element.
|
|
72
|
+
*/
|
|
73
|
+
export declare function mount(options: MountOptions): WidgetHandle;
|
|
74
|
+
/** Writes the `mount()` options onto the element as the attributes `readConfig` reads. */
|
|
75
|
+
export declare function applyOptions(element: HTMLElement, options: MountOptions): void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface SignPulsarixIdentityOptions {
|
|
2
|
+
/** The key's identity secret, shown once in pulsarix. Keep it on the server, in an env var. */
|
|
3
|
+
secret: string;
|
|
4
|
+
/** The widget key's public key (`pk_…`): the token's audience. */
|
|
5
|
+
publicKey: string;
|
|
6
|
+
/** The signed-in user's stable id in your app (1 to 200 characters). Never stored by pulsarix. */
|
|
7
|
+
sub: string;
|
|
8
|
+
/** The user's e-mail address (at most 254 characters), used for replies. */
|
|
9
|
+
email?: string | undefined;
|
|
10
|
+
/** The user's display name (at most 100 characters once trimmed). */
|
|
11
|
+
name?: string | undefined;
|
|
12
|
+
/** How long the token lives, in whole seconds, 1 to 86 400. Defaults to 3 600 (1 hour). */
|
|
13
|
+
ttlSeconds?: number;
|
|
14
|
+
/** The clock, in milliseconds since the epoch. Defaults to `Date.now`; tests pass their own. */
|
|
15
|
+
now?: () => number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Signs the identity token of one signed-in user. Call it per request (or per session) and
|
|
19
|
+
* re-sign before the token expires. Throws an `Error` naming the option when the input is one
|
|
20
|
+
* the API would refuse.
|
|
21
|
+
*/
|
|
22
|
+
export declare function signPulsarixIdentity(options: SignPulsarixIdentityOptions): string;
|