@ebitex/content-sdk 0.21.192 → 0.22.201
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 +18 -0
- package/dist/apply/modelFiles.d.ts.map +1 -1
- package/dist/apply/modelFiles.js +0 -1
- package/dist/apply/modelFiles.js.map +1 -1
- package/dist/audience/client.d.ts +63 -0
- package/dist/audience/client.d.ts.map +1 -0
- package/dist/audience/client.js +36 -0
- package/dist/audience/client.js.map +1 -0
- package/dist/delivery/client.d.ts +6 -0
- package/dist/delivery/client.d.ts.map +1 -1
- package/dist/delivery/client.js +3 -3
- package/dist/delivery/client.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/model/compile.d.ts.map +1 -1
- package/dist/model/compile.js +1 -11
- package/dist/model/compile.js.map +1 -1
- package/dist/model/decompile.d.ts.map +1 -1
- package/dist/model/decompile.js +0 -6
- package/dist/model/decompile.js.map +1 -1
- package/dist/model/types.d.ts +2 -8
- package/dist/model/types.d.ts.map +1 -1
- package/dist/model/types.js.map +1 -1
- package/dist/preview/protocol.d.ts +1 -1
- package/dist/preview/protocol.d.ts.map +1 -1
- package/dist/render/email.d.ts +39 -0
- package/dist/render/email.d.ts.map +1 -0
- package/dist/render/email.js +259 -0
- package/dist/render/email.js.map +1 -0
- package/dist/render/handler.d.ts +28 -0
- package/dist/render/handler.d.ts.map +1 -0
- package/dist/render/handler.js +81 -0
- package/dist/render/handler.js.map +1 -0
- package/dist/render/index.d.ts +11 -0
- package/dist/render/index.d.ts.map +1 -0
- package/dist/render/index.js +11 -0
- package/dist/render/index.js.map +1 -0
- package/dist/render/protocol.d.ts +66 -0
- package/dist/render/protocol.d.ts.map +1 -0
- package/dist/render/protocol.js +92 -0
- package/dist/render/protocol.js.map +1 -0
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -1
|
@@ -21,7 +21,7 @@ export interface PreviewEntity {
|
|
|
21
21
|
}
|
|
22
22
|
export type PreviewPath = (string | number)[];
|
|
23
23
|
export interface PreviewFieldFacts {
|
|
24
|
-
/** The personalization variant that won — an
|
|
24
|
+
/** The personalization variant that won — an audience id, or `variant-N` for one built from a bare predicate. Absent when the default won. */
|
|
25
25
|
variant?: string;
|
|
26
26
|
/** The locale that actually served the value when it is not the requested one; `default` for a fallback to the unlocalized value. */
|
|
27
27
|
locale?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/preview/protocol.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAC5D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAEpD,eAAO,MAAM,sBAAsB,EAAG,wBAAiC,CAAA;AACvE,eAAO,MAAM,wBAAwB,EAAG,CAAU,CAAA;AAClD,qHAAqH;AACrH,eAAO,MAAM,kBAAkB,EAAG,gBAAyB,CAAA;AAC3D,qEAAqE;AACrE,eAAO,MAAM,uBAAuB,EAAE,SAAS,MAAM,EAAkC,CAAA;AAEvF,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,QAAQ,CAAA;AAI3C,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,WAAW,GAAG,MAAM,GAAG,UAAU,CAAA;IACvC,EAAE,EAAE,MAAM,CAAA;IACV,6EAA6E;IAC7E,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,MAAM,WAAW,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;AAE7C,MAAM,WAAW,iBAAiB;IAChC,
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/preview/protocol.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,gBA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AA;AAE1D,kIAAkI;AAClI,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,GAAE,MAAiC,GAAG,IAAI,IAAI,cAAc,CA0BlH;AAED,mIAAmI;AACnI,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,cAAc,IAAI,CAAC,SAAS,OAAO,GAAG,IAAI,CAAC,CAAC,EAAE,QAAQ,GAAG,GAAG,CAAC,GAAG,KAAK,CAAA;AAEvG,+CAA+C;AAC/C,wBAAgB,cAAc,CAAC,CAAC,SAAS,cAAc,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,CAEhF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A default way to turn a published page into an email, for a site that has no email renderer of its own.
|
|
3
|
+
*
|
|
4
|
+
* Email is a hostile target: no scripts, no external styles, table layout, inline CSS. This does not know your Templates, so it reads
|
|
5
|
+
* a page by what its fields are called and shaped like rather than by which Template made it: a field named like a title becomes the
|
|
6
|
+
* heading, text becomes paragraphs, an image becomes a full-width picture, a link becomes a button. A site that wants its own look
|
|
7
|
+
* renders its own emails and serves the protocol itself; this exists so a small site gets something good enough on day one.
|
|
8
|
+
*
|
|
9
|
+
* Everything is escaped, only `http`, `https` and `mailto` addresses survive, and any `{{individual.firstName}}` placeholder is left in
|
|
10
|
+
* place for the sender to fill in per person.
|
|
11
|
+
*/
|
|
12
|
+
export interface EmailPage {
|
|
13
|
+
/** The page's own title, used for the subject when the content names none. */
|
|
14
|
+
title?: string | null;
|
|
15
|
+
/** The delivered Presentation envelope: `{ template, settings, component }`. */
|
|
16
|
+
presentation: unknown;
|
|
17
|
+
}
|
|
18
|
+
export interface RenderedEmail {
|
|
19
|
+
/** False when the page produced no heading, text, picture or button at all: the message would be blank, so a caller must not send it. */
|
|
20
|
+
hasContent: boolean;
|
|
21
|
+
subject: string | null;
|
|
22
|
+
html: string;
|
|
23
|
+
text: string;
|
|
24
|
+
tokensUsed: string[];
|
|
25
|
+
}
|
|
26
|
+
export interface RenderEmailOptions {
|
|
27
|
+
/** Maximum width of the message in pixels. Defaults to 600. */
|
|
28
|
+
width?: number;
|
|
29
|
+
}
|
|
30
|
+
export declare function escapeHtml(text: string): string;
|
|
31
|
+
/** `http`, `https` and `mailto` only. Anything else (`javascript:`, `data:`, a relative path) is dropped. */
|
|
32
|
+
export declare function safeUrl(value: unknown): string | null;
|
|
33
|
+
/** A small, safe subset of markdown: paragraphs, `#` headings, `-` lists, `**bold**`, `*italic*` and `[text](url)`. Raw HTML is escaped, not passed through. */
|
|
34
|
+
export declare function markdownToEmailHtml(markdown: string): string;
|
|
35
|
+
/** The tokens a piece of text leaves in place. */
|
|
36
|
+
export declare function tokensIn(...texts: Array<string | null | undefined>): string[];
|
|
37
|
+
/** Turns a delivered page into `{ subject, html, text, tokensUsed }`. */
|
|
38
|
+
export declare function renderEmail(page: EmailPage, options?: RenderEmailOptions): RenderedEmail;
|
|
39
|
+
//# sourceMappingURL=email.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email.d.ts","sourceRoot":"","sources":["../../src/render/email.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,SAAS;IACxB,8EAA8E;IAC9E,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,gFAAgF;IAChF,YAAY,EAAE,OAAO,CAAA;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B,yIAAyI;IACzI,UAAU,EAAE,OAAO,CAAA;IACnB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE,MAAM,EAAE,CAAA;CACrB;AAED,MAAM,WAAW,kBAAkB;IACjC,+DAA+D;IAC/D,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAeD,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED,6GAA6G;AAC7G,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAOrD;AAED,gKAAgK;AAChK,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CA8B5D;AA4ND,kDAAkD;AAClD,wBAAgB,QAAQ,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,GAAG,MAAM,EAAE,CAS7E;AAED,yEAAyE;AACzE,wBAAgB,WAAW,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,GAAE,kBAAuB,GAAG,aAAa,CAe5F"}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A default way to turn a published page into an email, for a site that has no email renderer of its own.
|
|
3
|
+
*
|
|
4
|
+
* Email is a hostile target: no scripts, no external styles, table layout, inline CSS. This does not know your Templates, so it reads
|
|
5
|
+
* a page by what its fields are called and shaped like rather than by which Template made it: a field named like a title becomes the
|
|
6
|
+
* heading, text becomes paragraphs, an image becomes a full-width picture, a link becomes a button. A site that wants its own look
|
|
7
|
+
* renders its own emails and serves the protocol itself; this exists so a small site gets something good enough on day one.
|
|
8
|
+
*
|
|
9
|
+
* Everything is escaped, only `http`, `https` and `mailto` addresses survive, and any `{{individual.firstName}}` placeholder is left in
|
|
10
|
+
* place for the sender to fill in per person.
|
|
11
|
+
*/
|
|
12
|
+
const TITLE_KEYS = ['headline', 'heading', 'title', 'name'];
|
|
13
|
+
const SUBJECT_KEYS = ['subject', 'emailSubject'];
|
|
14
|
+
const MAX_DEPTH = 6;
|
|
15
|
+
/** Members that describe a value rather than say something: never printed as text. */
|
|
16
|
+
const METADATA_KEYS = new Set(['kind', 'id', 'key', 'provider', 'url', 'alt', 'mimeType', 'externalId', 'resolved', 'unresolvable']);
|
|
17
|
+
function isRecord(value) {
|
|
18
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
export function escapeHtml(text) {
|
|
21
|
+
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
22
|
+
}
|
|
23
|
+
/** `http`, `https` and `mailto` only. Anything else (`javascript:`, `data:`, a relative path) is dropped. */
|
|
24
|
+
export function safeUrl(value) {
|
|
25
|
+
if (typeof value !== 'string') {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const trimmed = value.trim();
|
|
29
|
+
return /^(https?:\/\/|mailto:)/i.test(trimmed) ? trimmed : null;
|
|
30
|
+
}
|
|
31
|
+
/** A small, safe subset of markdown: paragraphs, `#` headings, `-` lists, `**bold**`, `*italic*` and `[text](url)`. Raw HTML is escaped, not passed through. */
|
|
32
|
+
export function markdownToEmailHtml(markdown) {
|
|
33
|
+
const inline = (text) => escapeHtml(text)
|
|
34
|
+
.replace(/\[([^\]]+)\]\(([^\s()]*(?:\([^\s()]*\))?[^\s()]*)\)/g, (_m, label, url) => {
|
|
35
|
+
const safe = safeUrl(url.replace(/&/g, '&'));
|
|
36
|
+
return safe === null ? label : `<a href="${escapeHtml(safe)}" style="color:#1a56db">${label}</a>`;
|
|
37
|
+
})
|
|
38
|
+
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
|
39
|
+
.replace(/(^|[^*])\*([^*\n]+)\*/g, '$1<em>$2</em>');
|
|
40
|
+
const blocks = markdown.replace(/\r\n/g, '\n').split(/\n{2,}/);
|
|
41
|
+
return blocks
|
|
42
|
+
.map((block) => block.trim())
|
|
43
|
+
.filter((block) => block.length > 0)
|
|
44
|
+
.map((block) => {
|
|
45
|
+
const heading = /^(#{1,3})\s+(.*)$/.exec(block);
|
|
46
|
+
if (heading) {
|
|
47
|
+
const size = heading[1].length === 1 ? 22 : 18;
|
|
48
|
+
return `<p style="margin:0 0 12px;font-size:${size}px;font-weight:bold;line-height:1.3">${inline(heading[2])}</p>`;
|
|
49
|
+
}
|
|
50
|
+
const lines = block.split('\n');
|
|
51
|
+
if (lines.every((line) => /^[-*]\s+/.test(line))) {
|
|
52
|
+
const items = lines.map((line) => `<li style="margin:0 0 4px">${inline(line.replace(/^[-*]\s+/, ''))}</li>`).join('');
|
|
53
|
+
return `<ul style="margin:0 0 12px;padding-left:20px">${items}</ul>`;
|
|
54
|
+
}
|
|
55
|
+
return `<p style="margin:0 0 12px;line-height:1.5">${inline(lines.join('<br>').replace(/<br>/g, '\n')).replace(/\n/g, '<br>')}</p>`;
|
|
56
|
+
})
|
|
57
|
+
.join('');
|
|
58
|
+
}
|
|
59
|
+
function humanize(key) {
|
|
60
|
+
const spaced = key.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/[-_]+/g, ' ').trim();
|
|
61
|
+
return spaced.charAt(0).toUpperCase() + spaced.slice(1);
|
|
62
|
+
}
|
|
63
|
+
function urlOf(value) {
|
|
64
|
+
return isRecord(value) ? safeUrl(value.url) : null;
|
|
65
|
+
}
|
|
66
|
+
function isRichText(value) {
|
|
67
|
+
return Array.isArray(value) && value.length > 0 && value.every((fragment) => isRecord(fragment) && typeof fragment.kind === 'string');
|
|
68
|
+
}
|
|
69
|
+
function pushImage(out, url, alt) {
|
|
70
|
+
out.html.push(`<img src="${escapeHtml(url)}" alt="${escapeHtml(alt)}" width="600" style="display:block;width:100%;max-width:100%;height:auto;border:0;margin:0 0 16px">`);
|
|
71
|
+
}
|
|
72
|
+
function pushLink(out, url, label) {
|
|
73
|
+
out.html.push(`<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="margin:0 0 16px"><tr><td style="background:#1a56db;border-radius:4px"><a href="${escapeHtml(url)}" style="display:inline-block;padding:10px 18px;color:#ffffff;text-decoration:none;font-weight:bold">${escapeHtml(label)}</a></td></tr></table>`);
|
|
74
|
+
out.text.push(`${label}: ${url}`, '');
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The shapes Content delivers for files and classification, read as they are delivered rather than guessed at: a category descriptor, an expanded reference to an image, video
|
|
78
|
+
* or document asset, and a plain blob. Returns whether the value was one of them (and so has been dealt with, including by saying nothing).
|
|
79
|
+
*/
|
|
80
|
+
function renderKnownShape(key, value, out) {
|
|
81
|
+
if (value.kind === 'category') {
|
|
82
|
+
// Only the label: the path and key are identity, not words. A category deleted since publish has no label and says nothing.
|
|
83
|
+
if (typeof value.value === 'string' && value.value.trim() !== '') {
|
|
84
|
+
out.html.push(`<p style="margin:0 0 12px;line-height:1.5">${escapeHtml(value.value)}</p>`);
|
|
85
|
+
out.text.push(value.value, '');
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
const assetContent = isRecord(value.content) && isRecord(value.content.file) ? value.content : null;
|
|
90
|
+
if (assetContent && typeof assetContent.kind === 'string' && ['image', 'video', 'document'].includes(assetContent.kind)) {
|
|
91
|
+
const file = assetContent.file;
|
|
92
|
+
const name = typeof assetContent.name === 'string' ? assetContent.name : humanize(key);
|
|
93
|
+
if (assetContent.kind === 'image') {
|
|
94
|
+
const url = safeUrl(file.url);
|
|
95
|
+
if (url !== null) {
|
|
96
|
+
pushImage(out, url, typeof assetContent.alt === 'string' && assetContent.alt.trim() !== '' ? assetContent.alt : name);
|
|
97
|
+
}
|
|
98
|
+
if (typeof assetContent.caption === 'string' && assetContent.caption.trim() !== '') {
|
|
99
|
+
out.html.push(`<p style="margin:-8px 0 16px;font-size:13px;color:#52525b">${escapeHtml(assetContent.caption)}</p>`);
|
|
100
|
+
out.text.push(assetContent.caption, '');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const url = safeUrl(file.url);
|
|
105
|
+
if (url !== null) {
|
|
106
|
+
pushLink(out, url, assetContent.kind === 'video' ? `Watch: ${name}` : `Download: ${name}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
if (typeof value.blobId === 'string' && 'url' in value) {
|
|
112
|
+
const url = safeUrl(value.url);
|
|
113
|
+
if (url !== null) {
|
|
114
|
+
if (typeof value.contentType === 'string' && value.contentType.startsWith('image/')) {
|
|
115
|
+
pushImage(out, url, humanize(key));
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
pushLink(out, url, humanize(key));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
function renderValue(key, value, content, out, depth) {
|
|
126
|
+
if (value === null || value === undefined || depth > MAX_DEPTH) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (typeof value === 'string') {
|
|
130
|
+
if (value.trim() === '' || SUBJECT_KEYS.includes(key) || METADATA_KEYS.has(key)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (out.heading === null && TITLE_KEYS.includes(key)) {
|
|
134
|
+
out.heading = value;
|
|
135
|
+
out.html.push(`<h1 style="margin:0 0 16px;font-size:26px;line-height:1.25">${escapeHtml(value)}</h1>`);
|
|
136
|
+
out.text.push(value, '');
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
out.html.push(`<p style="margin:0 0 12px;line-height:1.5">${escapeHtml(value).replace(/\n/g, '<br>')}</p>`);
|
|
140
|
+
out.text.push(value, '');
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (Array.isArray(value)) {
|
|
147
|
+
if (isRichText(value)) {
|
|
148
|
+
for (const fragment of value) {
|
|
149
|
+
if (fragment.kind === 'markdown' && typeof fragment.markdown === 'string') {
|
|
150
|
+
out.html.push(markdownToEmailHtml(fragment.markdown));
|
|
151
|
+
out.text.push(fragment.markdown, '');
|
|
152
|
+
}
|
|
153
|
+
else if (fragment.kind === 'personalized' && Array.isArray(fragment.default)) {
|
|
154
|
+
renderValue(key, fragment.default, content, out, depth + 1);
|
|
155
|
+
}
|
|
156
|
+
else if (fragment.kind === 'presentation') {
|
|
157
|
+
renderPresentation(fragment.presentation, out, depth + 1);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
for (const item of value) {
|
|
163
|
+
renderValue(key, item, content, out, depth + 1);
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (!isRecord(value)) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (renderKnownShape(key, value, out)) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
// A value with an address is a link or a picture. With an unsafe address it says nothing at all, rather than falling through to be printed as text.
|
|
174
|
+
if ('url' in value && urlOf(value) === null) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const url = urlOf(value);
|
|
178
|
+
const isAsset = url !== null && (typeof value.mimeType === 'string' ? value.mimeType.startsWith('image/') : /\.(png|jpe?g|gif|webp)(\?|$)/i.test(url) || 'alt' in value || 'width' in value);
|
|
179
|
+
if (isAsset && url !== null) {
|
|
180
|
+
pushImage(out, url, typeof value.alt === 'string' ? value.alt : humanize(key));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (url !== null) {
|
|
184
|
+
const label = ['label', 'text', 'title'].map((k) => content[`${key}${humanize(k).replace(/ /g, '')}`] ?? value[k]).find((v) => typeof v === 'string' && v.trim() !== '');
|
|
185
|
+
const shown = typeof label === 'string' ? label : humanize(key);
|
|
186
|
+
pushLink(out, url, shown);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (isRecord(value.content)) {
|
|
190
|
+
renderContent(value.content, out, depth + 1);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (isRecord(value.component) || isRecord(value.presentation)) {
|
|
194
|
+
renderPresentation(value, out, depth + 1);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
renderContent(value, out, depth + 1);
|
|
198
|
+
}
|
|
199
|
+
function renderContent(content, out, depth) {
|
|
200
|
+
for (const [key, value] of Object.entries(content)) {
|
|
201
|
+
renderValue(key, value, content, out, depth);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function renderPresentation(presentation, out, depth) {
|
|
205
|
+
if (!isRecord(presentation) || depth > MAX_DEPTH) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const component = isRecord(presentation.component) ? presentation.component : presentation;
|
|
209
|
+
if (isRecord(component.content)) {
|
|
210
|
+
renderContent(component.content, out, depth);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function findSubject(presentation, title, heading) {
|
|
214
|
+
const roots = [];
|
|
215
|
+
if (isRecord(presentation)) {
|
|
216
|
+
if (isRecord(presentation.settings)) {
|
|
217
|
+
roots.push(presentation.settings);
|
|
218
|
+
}
|
|
219
|
+
const content = isRecord(presentation.component) ? presentation.component.content : undefined;
|
|
220
|
+
if (isRecord(content)) {
|
|
221
|
+
roots.push(content);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
for (const root of roots) {
|
|
225
|
+
for (const key of SUBJECT_KEYS) {
|
|
226
|
+
const value = root[key];
|
|
227
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
228
|
+
return value.trim();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return (heading ?? title ?? null)?.toString().trim() || null;
|
|
233
|
+
}
|
|
234
|
+
const TOKEN_PATTERN = /\{\{\s*([A-Za-z0-9_.]+)\s*(?:\|[^}]*)?\}\}/g;
|
|
235
|
+
/** The tokens a piece of text leaves in place. */
|
|
236
|
+
export function tokensIn(...texts) {
|
|
237
|
+
const found = new Set();
|
|
238
|
+
for (const text of texts) {
|
|
239
|
+
for (const match of (text ?? '').matchAll(TOKEN_PATTERN)) {
|
|
240
|
+
found.add(match[1]);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return [...found].sort();
|
|
244
|
+
}
|
|
245
|
+
/** Turns a delivered page into `{ subject, html, text, tokensUsed }`. */
|
|
246
|
+
export function renderEmail(page, options = {}) {
|
|
247
|
+
const width = options.width ?? 600;
|
|
248
|
+
const out = { html: [], text: [], heading: null };
|
|
249
|
+
renderPresentation(page.presentation, out, 0);
|
|
250
|
+
const subject = findSubject(page.presentation, page.title, out.heading);
|
|
251
|
+
const body = out.html.join('');
|
|
252
|
+
const html = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${escapeHtml(subject ?? '')}</title></head>` +
|
|
253
|
+
`<body style="margin:0;padding:0;background:#f4f4f5"><table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="center" style="padding:24px 12px">` +
|
|
254
|
+
`<table role="presentation" width="${width}" cellpadding="0" cellspacing="0" border="0" style="width:100%;max-width:${width}px;background:#ffffff"><tr><td style="padding:24px;font-family:Arial,Helvetica,sans-serif;font-size:16px;color:#18181b">` +
|
|
255
|
+
`${body}</td></tr></table></td></tr></table></body></html>`;
|
|
256
|
+
const text = out.text.join('\n').replace(/\n{3,}/g, '\n\n').trim();
|
|
257
|
+
return { hasContent: out.html.length > 0, subject, html, text, tokensUsed: tokensIn(subject, html, text) };
|
|
258
|
+
}
|
|
259
|
+
//# sourceMappingURL=email.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"email.js","sourceRoot":"","sources":["../../src/render/email.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAyBH,MAAM,UAAU,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;AAC3D,MAAM,YAAY,GAAG,CAAC,SAAS,EAAE,cAAc,CAAC,CAAA;AAChD,MAAM,SAAS,GAAG,CAAC,CAAA;AAEnB,sFAAsF;AACtF,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,K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C,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,IAAI,EAAE,CAAA;IAElE,OAAO,EAAE,UAAU,EAAE,GAAG,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,CAAA;AAC5G,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type RenderRequest, type RenderResult } from './protocol.js';
|
|
2
|
+
export interface RenderHandlerOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The secret a connection signs its requests with, looked up by the request's `channelId`. Return `undefined` for a connection you do
|
|
5
|
+
* not know and the request is refused. It is read before the signature is checked, so treat the id as untrusted.
|
|
6
|
+
*/
|
|
7
|
+
secretFor: (channelId: string) => string | undefined | Promise<string | undefined>;
|
|
8
|
+
/** Turns a request into a message. Throw `RenderNotFoundError` when the page is not published; anything else answers 500. */
|
|
9
|
+
render: (request: RenderRequest) => RenderResult | Promise<RenderResult>;
|
|
10
|
+
/** Seconds since the epoch. Defaults to the clock; a parameter so a test need not wait. */
|
|
11
|
+
now?: () => number;
|
|
12
|
+
/** How far a request's timestamp may be from now, in seconds. Defaults to five minutes. */
|
|
13
|
+
toleranceSeconds?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A request handler for the render protocol, built on the standard `Request` and `Response`, so it mounts directly in a Node
|
|
17
|
+
* server, a Cloudflare Worker, or a Next.js route handler:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* export const POST = createRenderHandler({ secretFor: () => process.env.RENDER_SECRET, render })
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* It refuses anything but a POST, reads at most 64 KB, checks the signature over the exact bytes received (with a replay window) before it
|
|
24
|
+
* believes any field, and never echoes the delivery key. Answers: 200 with the message, 400 for a malformed request, 401 for a bad
|
|
25
|
+
* signature or an unknown connection, 404 when the page is not published, 413 when the body is too large, 500 for any other failure.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createRenderHandler(options: RenderHandlerOptions): (request: Request) => Promise<Response>;
|
|
28
|
+
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/render/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,aAAa,EAClB,KAAK,YAAY,EAClB,MAAM,eAAe,CAAA;AAEtB,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,SAAS,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;IAClF,6HAA6H;IAC7H,MAAM,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;IACxE,2FAA2F;IAC3F,GAAG,CAAC,EAAE,MAAM,MAAM,CAAA;IAClB,2FAA2F;IAC3F,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B;AAqCD;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,oBAAoB,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CA4C1G"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { RENDER_MAX_REQUEST_BYTES, RENDER_SIGNATURE_HEADER, RENDER_TOLERANCE_SECONDS, RenderNotFoundError, RenderUnrenderableError, verifyRenderSignature, } from './protocol.js';
|
|
2
|
+
function json(status, body) {
|
|
3
|
+
return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' } });
|
|
4
|
+
}
|
|
5
|
+
async function readBounded(request) {
|
|
6
|
+
const declared = Number(request.headers.get('content-length'));
|
|
7
|
+
if (Number.isFinite(declared) && declared > RENDER_MAX_REQUEST_BYTES) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const bytes = new Uint8Array(await request.arrayBuffer());
|
|
11
|
+
return bytes.length > RENDER_MAX_REQUEST_BYTES ? null : bytes;
|
|
12
|
+
}
|
|
13
|
+
function isRenderRequest(value) {
|
|
14
|
+
if (typeof value !== 'object' || value === null) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
const v = value;
|
|
18
|
+
const context = v.context;
|
|
19
|
+
return (typeof v.channelId === 'string' &&
|
|
20
|
+
typeof v.channel === 'string' &&
|
|
21
|
+
typeof v.site === 'string' &&
|
|
22
|
+
typeof v.path === 'string' &&
|
|
23
|
+
(v.locale === null || typeof v.locale === 'string') &&
|
|
24
|
+
typeof v.deliveryKey === 'string' &&
|
|
25
|
+
typeof context === 'object' &&
|
|
26
|
+
context !== null &&
|
|
27
|
+
Array.isArray(context.audiences) &&
|
|
28
|
+
context.audiences.every((a) => typeof a === 'string'));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A request handler for the render protocol, built on the standard `Request` and `Response`, so it mounts directly in a Node
|
|
32
|
+
* server, a Cloudflare Worker, or a Next.js route handler:
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* export const POST = createRenderHandler({ secretFor: () => process.env.RENDER_SECRET, render })
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* It refuses anything but a POST, reads at most 64 KB, checks the signature over the exact bytes received (with a replay window) before it
|
|
39
|
+
* believes any field, and never echoes the delivery key. Answers: 200 with the message, 400 for a malformed request, 401 for a bad
|
|
40
|
+
* signature or an unknown connection, 404 when the page is not published, 413 when the body is too large, 500 for any other failure.
|
|
41
|
+
*/
|
|
42
|
+
export function createRenderHandler(options) {
|
|
43
|
+
const now = options.now ?? (() => Math.floor(Date.now() / 1000));
|
|
44
|
+
const tolerance = options.toleranceSeconds ?? RENDER_TOLERANCE_SECONDS;
|
|
45
|
+
return async (request) => {
|
|
46
|
+
if (request.method !== 'POST') {
|
|
47
|
+
return json(405, { error: 'method_not_allowed' });
|
|
48
|
+
}
|
|
49
|
+
const body = await readBounded(request);
|
|
50
|
+
if (body === null) {
|
|
51
|
+
return json(413, { error: 'request_too_large' });
|
|
52
|
+
}
|
|
53
|
+
let parsed;
|
|
54
|
+
try {
|
|
55
|
+
parsed = JSON.parse(new TextDecoder().decode(body));
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return json(400, { error: 'invalid_json' });
|
|
59
|
+
}
|
|
60
|
+
if (!isRenderRequest(parsed)) {
|
|
61
|
+
return json(400, { error: 'invalid_request' });
|
|
62
|
+
}
|
|
63
|
+
const secret = await options.secretFor(parsed.channelId);
|
|
64
|
+
if (!secret || !(await verifyRenderSignature(body, request.headers.get(RENDER_SIGNATURE_HEADER), secret, now(), tolerance))) {
|
|
65
|
+
return json(401, { error: 'invalid_signature' });
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
return json(200, await options.render(parsed));
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof RenderNotFoundError) {
|
|
72
|
+
return json(404, { error: 'not_found' });
|
|
73
|
+
}
|
|
74
|
+
if (error instanceof RenderUnrenderableError) {
|
|
75
|
+
return json(422, { error: 'unrenderable', message: error.message });
|
|
76
|
+
}
|
|
77
|
+
return json(500, { error: 'render_failed' });
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/render/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,uBAAuB,EACvB,wBAAwB,EACxB,mBAAmB,EACnB,uBAAuB,EACvB,qBAAqB,GAGtB,MAAM,eAAe,CAAA;AAgBtB,SAAS,IAAI,CAAC,MAAc,EAAE,IAAa;IACzC,OAAO,IAAI,QA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,CAAA;QAChD,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;QACxD,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,qBAAqB,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC;YAC5H,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC,CAAA;QAClD,CAAC;QAED,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,GAAG,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;QAChD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,KAAK,YAAY,mBAAmB,EAAE,CAAC;gBACzC,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAA;YAC1C,CAAC;YAED,IAAI,KAAK,YAAY,uBAAuB,EAAE,CAAC;gBAC7C,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;YACrE,CAAC;YAED,OAAO,IAAI,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,eAAe,EAAE,CAAC,CAAA;QAC9C,CAAC;IACH,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@ebitex/content-sdk/render` — serve ebitex's render protocol from your own app, or reuse the default email renderer.
|
|
3
|
+
*
|
|
4
|
+
* A sender of messages (ebitex Audience) asks your app to turn one of your pages into a message. This entry has the
|
|
5
|
+
* handler that answers it and a default renderer for sites that have no email layout of their own. Nothing here needs React
|
|
6
|
+
* or a DOM: it runs anywhere with the standard `Request`, `Response` and Web Crypto.
|
|
7
|
+
*/
|
|
8
|
+
export { createRenderHandler, type RenderHandlerOptions } from './handler.js';
|
|
9
|
+
export { RENDER_MAX_REQUEST_BYTES, RENDER_PATH, RENDER_SIGNATURE_HEADER, RENDER_TOLERANCE_SECONDS, RenderNotFoundError, RenderUnrenderableError, signRenderBody, verifyRenderSignature, type RenderRequest, type RenderResult, } from './protocol.js';
|
|
10
|
+
export { escapeHtml, markdownToEmailHtml, renderEmail, safeUrl, tokensIn, type EmailPage, type RenderEmailOptions, type RenderedEmail } from './email.js';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/render/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,mBAAmB,EAAE,KAAK,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAC7E,OAAO,EACL,wBAAwB,EACxB,WAAW,EACX,uBAAuB,EACvB,wBAAwB,EACxB,mBAAmB,EACnB,uBAAuB,EACvB,cAAc,EACd,qBAAqB,EACrB,KAAK,aAAa,EAClB,KAAK,YAAY,GAClB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,SAAS,EAAE,KAAK,kBAAkB,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@ebitex/content-sdk/render` — serve ebitex's render protocol from your own app, or reuse the default email renderer.
|
|
3
|
+
*
|
|
4
|
+
* A sender of messages (ebitex Audience) asks your app to turn one of your pages into a message. This entry has the
|
|
5
|
+
* handler that answers it and a default renderer for sites that have no email layout of their own. Nothing here needs React
|
|
6
|
+
* or a DOM: it runs anywhere with the standard `Request`, `Response` and Web Crypto.
|
|
7
|
+
*/
|
|
8
|
+
export { createRenderHandler } from './handler.js';
|
|
9
|
+
export { RENDER_MAX_REQUEST_BYTES, RENDER_PATH, RENDER_SIGNATURE_HEADER, RENDER_TOLERANCE_SECONDS, RenderNotFoundError, RenderUnrenderableError, signRenderBody, verifyRenderSignature, } from './protocol.js';
|
|
10
|
+
export { escapeHtml, markdownToEmailHtml, renderEmail, safeUrl, tokensIn } from './email.js';
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/render/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,mBAAmB,EAA6B,MAAM,cAAc,CAAA;AAC7E,OAAO,EACL,wBAAwB,EACxB,WAAW,EACX,uBAAuB,EACvB,wBAAwB,EACxB,mBAAmB,EACnB,uBAAuB,EACvB,cAAc,EACd,qBAAqB,GAGtB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAA+D,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ebitex render protocol, version 1: how a sender of messages asks your app to turn a page into the parts of a message.
|
|
3
|
+
*
|
|
4
|
+
* The sender POSTs a JSON request to `{your address}/render/v1`, signed with a secret you share with it, and you answer with
|
|
5
|
+
* the message's subject, HTML and plain text. Nothing about a recipient is ever sent: the request names a page and the audiences
|
|
6
|
+
* a group of recipients share, and any `{{individual.firstName}}`-style placeholder is left in your output for the sender to fill
|
|
7
|
+
* in per person.
|
|
8
|
+
*/
|
|
9
|
+
/** The header carrying `v1,t=<unix seconds>,s=<hex hmac>`. */
|
|
10
|
+
export declare const RENDER_SIGNATURE_HEADER = "X-Ebitex-Signature";
|
|
11
|
+
/** The path the protocol is served at. */
|
|
12
|
+
export declare const RENDER_PATH = "/render/v1";
|
|
13
|
+
/** Requests older (or newer) than this are refused, so a captured request cannot be replayed later. */
|
|
14
|
+
export declare const RENDER_TOLERANCE_SECONDS = 300;
|
|
15
|
+
/** The largest request body a handler reads. A render request is a few hundred bytes plus a key. */
|
|
16
|
+
export declare const RENDER_MAX_REQUEST_BYTES: number;
|
|
17
|
+
/** What the sender asks for. */
|
|
18
|
+
export interface RenderRequest {
|
|
19
|
+
/** The sender's id for the connection this request is made through. A handler looks its secret up by it. */
|
|
20
|
+
channelId: string;
|
|
21
|
+
/** The kind of message being made, such as `email`. */
|
|
22
|
+
channel: string;
|
|
23
|
+
/** The id of the site the page lives in. */
|
|
24
|
+
site: string;
|
|
25
|
+
/** The page's path in that site. */
|
|
26
|
+
path: string;
|
|
27
|
+
/** A locale code, or `null` for the site's default. */
|
|
28
|
+
locale: string | null;
|
|
29
|
+
/** The audiences this render is for, as ids. Empty means the page as anyone sees it. */
|
|
30
|
+
context: {
|
|
31
|
+
audiences: string[];
|
|
32
|
+
};
|
|
33
|
+
/** A key for reading the site's published content. Use it for this request only and never store it. */
|
|
34
|
+
deliveryKey: string;
|
|
35
|
+
}
|
|
36
|
+
/** What you answer with. */
|
|
37
|
+
export interface RenderResult {
|
|
38
|
+
subject: string | null;
|
|
39
|
+
html: string | null;
|
|
40
|
+
text: string | null;
|
|
41
|
+
/** The `{{tokens}}` your output leaves in place. The sender checks them against what it may fill in. */
|
|
42
|
+
tokensUsed: string[];
|
|
43
|
+
/** Identifies exactly what was rendered: the same published page renders to the same hash. */
|
|
44
|
+
contentHash: string;
|
|
45
|
+
/** The audiences the page's variants read, so the sender can group recipients. Optional. */
|
|
46
|
+
audiencesUsed?: string[];
|
|
47
|
+
}
|
|
48
|
+
/** Thrown from a render function when the page does not exist or is not published. The handler answers 404. */
|
|
49
|
+
export declare class RenderNotFoundError extends Error {
|
|
50
|
+
constructor(message?: string);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Thrown from a render function when the page exists but has nothing to put in a message (for example a page whose content could not be read). The handler answers
|
|
54
|
+
* 422, so the sender fails that message with the reason instead of sending an empty one.
|
|
55
|
+
*/
|
|
56
|
+
export declare class RenderUnrenderableError extends Error {
|
|
57
|
+
constructor(message?: string);
|
|
58
|
+
}
|
|
59
|
+
/** Builds the `X-Ebitex-Signature` value for exactly the bytes you send. Exposed for tests of your own handler. */
|
|
60
|
+
export declare function signRenderBody(body: Uint8Array | string, secret: string, timestamp: number): Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Checks a signature header against the raw request bytes. The comparison is constant-time. `nowSeconds` and the tolerance bound the
|
|
63
|
+
* request's age in both directions; pass `Infinity` for the tolerance only in a test.
|
|
64
|
+
*/
|
|
65
|
+
export declare function verifyRenderSignature(body: Uint8Array, header: string | null | undefined, secret: string, nowSeconds: number, toleranceSeconds?: number): Promise<boolean>;
|
|
66
|
+
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/render/protocol.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,8DAA8D;AAC9D,eAAO,MAAM,uBAAuB,uBAAuB,CAAA;AAE3D,0CAA0C;AAC1C,eAAO,MAAM,WAAW,eAAe,CAAA;AAEvC,uGAAuG;AACvG,eAAO,MAAM,wBAAwB,MAAM,CAAA;AAE3C,oGAAoG;AACpG,eAAO,MAAM,wBAAwB,QAAY,CAAA;AAEjD,gCAAgC;AAChC,MAAM,WAAW,aAAa;IAC5B,4GAA4G;IAC5G,SAAS,EAAE,MAAM,CAAA;IACjB,uDAAuD;IACvD,OAAO,EAAE,MAAM,CAAA;IACf,4CAA4C;IAC5C,IAAI,EAAE,MAAM,CAAA;IACZ,oCAAoC;IACpC,IAAI,EAAE,MAAM,CAAA;IACZ,uDAAuD;IACvD,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,wFAAwF;IACxF,OAAO,EAAE;QAAE,SAAS,EAAE,MAAM,EAAE,CAAA;KAAE,CAAA;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