@smileline/astro 0.0.0 → 0.1.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 +83 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +70 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +406 -0
- package/package.json +29 -1
package/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# @smileline/astro
|
|
2
|
+
|
|
3
|
+
Smileline for Astro sites. One integration:
|
|
4
|
+
|
|
5
|
+
- adds the Smileline script to every page (form capture with marketing
|
|
6
|
+
attribution, call-tracking numbers, page views and time on page);
|
|
7
|
+
- with a server adapter, serves the script and its requests from your own
|
|
8
|
+
domain under `/_sl/`;
|
|
9
|
+
- runs the A/B page tests you start in Smileline: both versions of a page
|
|
10
|
+
live behind the same URL and are split on the server, with no flash.
|
|
11
|
+
|
|
12
|
+
It replaces the `<script>` snippet and `@smileline/browser`: install only
|
|
13
|
+
one of the three.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @smileline/astro
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
// astro.config.mjs
|
|
23
|
+
import { defineConfig } from "astro/config";
|
|
24
|
+
import smileline from "@smileline/astro";
|
|
25
|
+
|
|
26
|
+
export default defineConfig({
|
|
27
|
+
integrations: [smileline({ siteKey: "pk_YOUR_SITE_KEY", region: "eu" })],
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`siteKey` and `region` (`"eu"` or `"us"`) are printed under
|
|
32
|
+
**Settings → Website → Tracking → Astro** in Smileline.
|
|
33
|
+
|
|
34
|
+
Then give the server that renders the site its **server key**, from the
|
|
35
|
+
same page (shown once; rotating it there replaces it within two minutes):
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
SMILELINE_SERVER_KEY=sk_…
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
It is read at request time with `getSecret` from `astro:env/server`, so it
|
|
42
|
+
must be a runtime environment variable or secret of your host (on
|
|
43
|
+
Cloudflare: a Worker secret) — never in your code or your repository, and
|
|
44
|
+
never sent to browsers.
|
|
45
|
+
|
|
46
|
+
## A/B page tests
|
|
47
|
+
|
|
48
|
+
Start a test in Smileline (Ads → A/B tests) by naming the page to test
|
|
49
|
+
(version A) and another page of your site to show instead (version B).
|
|
50
|
+
Visitors of A's URL then see A or B, half each, and keep seeing the same
|
|
51
|
+
version. Results are judged on bookings.
|
|
52
|
+
|
|
53
|
+
Requirements:
|
|
54
|
+
|
|
55
|
+
- **A server adapter**, and **both pages render on demand**:
|
|
56
|
+
`export const prerender = false;` in each. A prerendered B cannot be shown
|
|
57
|
+
at A's URL; visitors then get A.
|
|
58
|
+
- **Leave the `/_sl/` path free** for the integration's route.
|
|
59
|
+
- **Do not cache tested pages at a CDN.** Their responses are marked
|
|
60
|
+
`Cache-Control: private, no-cache` and CDN cache headers are removed;
|
|
61
|
+
a CDN rule that caches them anyway would show everyone one version.
|
|
62
|
+
|
|
63
|
+
What the integration does on a tested page:
|
|
64
|
+
|
|
65
|
+
- It never sets a cookie. A visitor whose browser has stored a Smileline
|
|
66
|
+
visitor id is assigned from it; anyone else at random, and the script
|
|
67
|
+
then stores an id that keeps them on the version they saw.
|
|
68
|
+
- It adds `<meta name="smileline-test">` to the page, which the script
|
|
69
|
+
reports with the page view.
|
|
70
|
+
- A canonical link or `og:url` on B that points at B is pointed at A, and
|
|
71
|
+
B's own URL is answered with `X-Robots-Tag: noindex`.
|
|
72
|
+
- If B fails to render, the visitor gets A.
|
|
73
|
+
- Once you pick a winner, everyone gets it, until you archive the test.
|
|
74
|
+
|
|
75
|
+
## Without an adapter
|
|
76
|
+
|
|
77
|
+
A fully static build still gets the script, loaded straight from
|
|
78
|
+
Smileline, and page analytics. A/B tests and the `/_sl/` route need an
|
|
79
|
+
adapter, and the build prints a warning.
|
|
80
|
+
|
|
81
|
+
## Licence
|
|
82
|
+
|
|
83
|
+
Proprietary (`UNLICENSED`): for use on websites of Smileline customers.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AstroIntegration } from "astro";
|
|
2
|
+
export interface SmilelineOptions {
|
|
3
|
+
/** The site key from Smileline's Settings → Website, as printed (`pk_…`). */
|
|
4
|
+
siteKey: string;
|
|
5
|
+
/** The Smileline region the practice's data lives in. */
|
|
6
|
+
region: "eu" | "us" | "stage";
|
|
7
|
+
}
|
|
8
|
+
export default function smileline(options: SmilelineOptions): AstroIntegration;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { writeFileSync as e } from "node:fs";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
var t = {
|
|
4
|
+
eu: {
|
|
5
|
+
api: "https://api-eu.smileline.io",
|
|
6
|
+
widget: "https://widget-eu.smileline.io"
|
|
7
|
+
},
|
|
8
|
+
us: {
|
|
9
|
+
api: "https://api-us.smileline.io",
|
|
10
|
+
widget: "https://widget-us.smileline.io"
|
|
11
|
+
},
|
|
12
|
+
stage: {
|
|
13
|
+
api: "https://api-stage.smileline.io",
|
|
14
|
+
widget: "https://widget-stage.smileline.io"
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
function n(e) {
|
|
18
|
+
return JSON.stringify(e).replaceAll("<", "\\u003c");
|
|
19
|
+
}
|
|
20
|
+
function r(e, t, r) {
|
|
21
|
+
return `(()=>{const s=document.createElement("script");s.async=true;s.src=${n(e)};s.dataset.site=${n(t)};${r === null ? "" : `s.dataset.api=${n(r)};`}document.head.appendChild(s)})();`;
|
|
22
|
+
}
|
|
23
|
+
function i(e, t) {
|
|
24
|
+
return [
|
|
25
|
+
"import { getSecret } from \"astro:env/server\";",
|
|
26
|
+
`import { ${e} } from "@smileline/astro/runtime";`,
|
|
27
|
+
"",
|
|
28
|
+
`const handler = ${e}({`,
|
|
29
|
+
`\t...${t},`,
|
|
30
|
+
" serverKey: () => getSecret(\"SMILELINE_SERVER_KEY\"),",
|
|
31
|
+
"});",
|
|
32
|
+
""
|
|
33
|
+
].join("\n");
|
|
34
|
+
}
|
|
35
|
+
function a(n) {
|
|
36
|
+
let a = Object.hasOwn(t, n.region) ? t[n.region] : void 0;
|
|
37
|
+
if (a === void 0) throw Error("@smileline/astro: region must be \"eu\", \"us\" or \"stage\" — the region shown with the site key in Smileline.");
|
|
38
|
+
let o = n.siteKey.trim().replace(/^pk_/u, "");
|
|
39
|
+
if (!/^[A-Za-z0-9_-]+$/u.test(o)) throw Error("@smileline/astro: siteKey must be the site key from Smileline's Settings → Website (pk_…).");
|
|
40
|
+
let s = `pk_${o}`;
|
|
41
|
+
return {
|
|
42
|
+
name: "@smileline/astro",
|
|
43
|
+
hooks: { "astro:config:setup": ({ config: t, injectScript: n, injectRoute: o, addMiddleware: c, createCodegenDir: l, logger: u }) => {
|
|
44
|
+
if (t.adapter === void 0) {
|
|
45
|
+
n("head-inline", r(`${a.widget}/sl.js`, s, null)), u.warn("No server adapter: the Smileline script loads straight from Smileline and records analytics, but A/B page tests need on-demand rendering. Add an adapter (for example @astrojs/cloudflare) to run them.");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let d = t.base.replace(/\/+$/u, ""), f = JSON.stringify({
|
|
49
|
+
siteKey: s,
|
|
50
|
+
apiOrigin: a.api,
|
|
51
|
+
widgetOrigin: a.widget,
|
|
52
|
+
base: d
|
|
53
|
+
}), p = l(), m = new URL("middleware.mjs", p), h = new URL("proxy.mjs", p);
|
|
54
|
+
e(m, `${i("createMiddleware", f)}export const onRequest = handler;\n`), e(h, `${i("createProxy", f)}export const prerender = false;\nexport const ALL = handler;\n`), c({
|
|
55
|
+
order: "pre",
|
|
56
|
+
entrypoint: m
|
|
57
|
+
}), o({
|
|
58
|
+
pattern: "/_sl/[asset].js",
|
|
59
|
+
entrypoint: h,
|
|
60
|
+
prerender: !1
|
|
61
|
+
}), o({
|
|
62
|
+
pattern: "/_sl/[...path]",
|
|
63
|
+
entrypoint: h,
|
|
64
|
+
prerender: !1
|
|
65
|
+
}), n("head-inline", r(`${d}/_sl/sl.js`, s, `${d}/_sl`));
|
|
66
|
+
} }
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
export { a as default };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface RuntimeOptions {
|
|
2
|
+
/** Wire form `pk_…`. */
|
|
3
|
+
siteKey: string;
|
|
4
|
+
/** The API origin of the practice's region. */
|
|
5
|
+
apiOrigin: string;
|
|
6
|
+
/** Astro's `base` without its trailing slash ("" at the root). */
|
|
7
|
+
base: string;
|
|
8
|
+
/** SMILELINE_SERVER_KEY from the runtime environment, read per request. */
|
|
9
|
+
serverKey: () => string | undefined;
|
|
10
|
+
/** Tests inject these. */
|
|
11
|
+
fetch?: (input: string, init?: RequestInit) => Promise<Response>;
|
|
12
|
+
now?: () => number;
|
|
13
|
+
}
|
|
14
|
+
export interface ProxyOptions extends RuntimeOptions {
|
|
15
|
+
/** The widget origin of the practice's region (the script files). */
|
|
16
|
+
widgetOrigin: string;
|
|
17
|
+
}
|
|
18
|
+
/** The part of Astro's APIContext the middleware uses. */
|
|
19
|
+
export interface MiddlewareContext {
|
|
20
|
+
request: Request;
|
|
21
|
+
url: URL;
|
|
22
|
+
isPrerendered: boolean;
|
|
23
|
+
locals: object;
|
|
24
|
+
rewrite(payload: string | URL | Request): Promise<Response>;
|
|
25
|
+
}
|
|
26
|
+
export type MiddlewareNext = (payload?: string | URL | Request) => Promise<Response>;
|
|
27
|
+
/** The part of Astro's APIContext the proxy route uses. `clientAddress`
|
|
28
|
+
* throws where the adapter cannot tell. */
|
|
29
|
+
export interface ProxyContext {
|
|
30
|
+
request: Request;
|
|
31
|
+
readonly clientAddress: string;
|
|
32
|
+
}
|
|
33
|
+
export declare function createMiddleware(options: RuntimeOptions): (context: MiddlewareContext, next: MiddlewareNext) => Promise<Response>;
|
|
34
|
+
export declare function createProxy(options: ProxyOptions): (context: ProxyContext) => Promise<Response>;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
function e(e) {
|
|
2
|
+
let t = 2166136261;
|
|
3
|
+
for (let n = 0; n < e.length; n++) t ^= e.charCodeAt(n), t = Math.imul(t, 16777619);
|
|
4
|
+
return t >>> 0;
|
|
5
|
+
}
|
|
6
|
+
function t(e) {
|
|
7
|
+
let t = e;
|
|
8
|
+
return t ^= t >>> 16, t = Math.imul(t, 2246822507), t ^= t >>> 13, t = Math.imul(t, 3266489909), t ^= t >>> 16, t >>> 0;
|
|
9
|
+
}
|
|
10
|
+
function n(n, r) {
|
|
11
|
+
return t(e(`${r}\u0000${n}`)) >>> 31 ? "b" : "a";
|
|
12
|
+
}
|
|
13
|
+
function r(e, t) {
|
|
14
|
+
return `${e}:${t}`;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region ../tracking-core/src/envelope.ts
|
|
18
|
+
function i(e) {
|
|
19
|
+
let t = {};
|
|
20
|
+
for (let n of e.split(";")) {
|
|
21
|
+
let e = n.indexOf("=");
|
|
22
|
+
if (e === -1) continue;
|
|
23
|
+
let r = n.slice(0, e).trim();
|
|
24
|
+
if (r === "" || r in t) continue;
|
|
25
|
+
let i = n.slice(e + 1).trim();
|
|
26
|
+
try {
|
|
27
|
+
i = decodeURIComponent(i);
|
|
28
|
+
} catch {}
|
|
29
|
+
i !== "" && (t[r] = i);
|
|
30
|
+
}
|
|
31
|
+
return t;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region ../tracking-core/src/state.ts
|
|
35
|
+
var a = "X-Smileline-Server-Key", o = "X-Smileline-Proxy", s = 6e4, c = 256 * 1024, l = [
|
|
36
|
+
"CDN-Cache-Control",
|
|
37
|
+
"Cloudflare-CDN-Cache-Control",
|
|
38
|
+
"Vercel-CDN-Cache-Control",
|
|
39
|
+
"Netlify-CDN-Cache-Control",
|
|
40
|
+
"Surrogate-Control"
|
|
41
|
+
], u = Symbol.for("@smileline/astro:fallback");
|
|
42
|
+
function d(e) {
|
|
43
|
+
return e.length > 1 && e.endsWith("/") ? e.slice(0, -1) : e;
|
|
44
|
+
}
|
|
45
|
+
function f(e, t) {
|
|
46
|
+
return d(e) === d(t);
|
|
47
|
+
}
|
|
48
|
+
function p(e, t) {
|
|
49
|
+
return e === `${t}/_sl` || e.startsWith(`${t}/_sl/`);
|
|
50
|
+
}
|
|
51
|
+
function m() {
|
|
52
|
+
return (crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(1))[0] ?? 0) & 1 ? "b" : "a";
|
|
53
|
+
}
|
|
54
|
+
function h(e, t) {
|
|
55
|
+
let r = i(t.headers.get("cookie") ?? "")._sl_vid;
|
|
56
|
+
return r ? n(r, e) : m();
|
|
57
|
+
}
|
|
58
|
+
function g(e) {
|
|
59
|
+
let t = Reflect.get(e, "cfContext");
|
|
60
|
+
if (typeof t == "object" && t) {
|
|
61
|
+
let e = Reflect.get(t, "waitUntil");
|
|
62
|
+
if (typeof e == "function") return (n) => {
|
|
63
|
+
e.call(t, n);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return (e) => {
|
|
67
|
+
e.catch(() => {});
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function _(e) {
|
|
71
|
+
let t = 0;
|
|
72
|
+
for (let n of e) t += n.byteLength;
|
|
73
|
+
let n = new Uint8Array(t), r = 0;
|
|
74
|
+
for (let t of e) n.set(t, r), r += t.byteLength;
|
|
75
|
+
return n;
|
|
76
|
+
}
|
|
77
|
+
async function v(e, t) {
|
|
78
|
+
if (e === null) return /* @__PURE__ */ new ArrayBuffer(0);
|
|
79
|
+
let n = e.getReader(), r = [], i = 0;
|
|
80
|
+
for (;;) {
|
|
81
|
+
let { done: e, value: a } = await n.read();
|
|
82
|
+
if (e) break;
|
|
83
|
+
if (i += a.byteLength, i > t) return await n.cancel(), null;
|
|
84
|
+
r.push(a);
|
|
85
|
+
}
|
|
86
|
+
let a = new ArrayBuffer(i);
|
|
87
|
+
return new Uint8Array(a).set(_(r)), a;
|
|
88
|
+
}
|
|
89
|
+
function y(e) {
|
|
90
|
+
if (typeof e != "object" || !e) return null;
|
|
91
|
+
let { tests: t } = e;
|
|
92
|
+
if (!Array.isArray(t)) return null;
|
|
93
|
+
let n = [];
|
|
94
|
+
for (let e of t) {
|
|
95
|
+
if (typeof e != "object" || !e) continue;
|
|
96
|
+
let { id: t, testedPath: r, variantPath: i, status: a, winner: o } = e;
|
|
97
|
+
typeof t != "string" || typeof r != "string" || typeof i != "string" || !r.startsWith("/") || !i.startsWith("/") || a !== "running" && a !== "stopped" || o !== null && o !== "a" && o !== "b" || n.push({
|
|
98
|
+
id: t,
|
|
99
|
+
testedPath: r,
|
|
100
|
+
variantPath: i,
|
|
101
|
+
status: a,
|
|
102
|
+
winner: o
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return n;
|
|
106
|
+
}
|
|
107
|
+
var b = [
|
|
108
|
+
60,
|
|
109
|
+
47,
|
|
110
|
+
104,
|
|
111
|
+
101,
|
|
112
|
+
97,
|
|
113
|
+
100
|
|
114
|
+
];
|
|
115
|
+
function x(e) {
|
|
116
|
+
return e === 62 || e === 47 || e === 32 || e === 9 || e === 10 || e === 12 || e === 13;
|
|
117
|
+
}
|
|
118
|
+
function S(e) {
|
|
119
|
+
for (let t = 0; t + b.length < e.length; t++) {
|
|
120
|
+
let n = !0;
|
|
121
|
+
for (let r = 0; r < b.length; r++) {
|
|
122
|
+
let i = e[t + r] ?? 0;
|
|
123
|
+
if ((i >= 65 && i <= 90 ? i + 32 : i) !== b[r]) {
|
|
124
|
+
n = !1;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (n && x(e[t + b.length])) return t;
|
|
129
|
+
}
|
|
130
|
+
return -1;
|
|
131
|
+
}
|
|
132
|
+
var C = c + b.length + 1;
|
|
133
|
+
function w(e) {
|
|
134
|
+
let t = [], n = 0, r = /* @__PURE__ */ new Uint8Array(), i = !1, a = (e) => {
|
|
135
|
+
for (let n of t) e.enqueue(n);
|
|
136
|
+
t.length = 0, i = !0;
|
|
137
|
+
};
|
|
138
|
+
return new TransformStream({
|
|
139
|
+
transform(o, s) {
|
|
140
|
+
if (i) {
|
|
141
|
+
s.enqueue(o);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
let c = _([r, o.subarray(0, Math.min(o.byteLength, C - n))]), l = S(c);
|
|
145
|
+
if (l === -1) {
|
|
146
|
+
if (n + o.byteLength >= C) {
|
|
147
|
+
a(s), s.enqueue(o);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
t.push(o), n += o.byteLength, r = c.slice(Math.max(0, c.byteLength - b.length));
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
let u = n - r.byteLength + l, d = _(t);
|
|
154
|
+
t.length = 0, i = !0;
|
|
155
|
+
let f = u <= n ? d.subarray(0, u) : _([d, o.subarray(0, u - n)]), p = new TextDecoder("utf-8", { ignoreBOM: !0 }).decode(f);
|
|
156
|
+
s.enqueue(new TextEncoder().encode(e(p))), u < n && s.enqueue(d.subarray(u)), s.enqueue(o.subarray(Math.max(0, u - n)));
|
|
157
|
+
},
|
|
158
|
+
flush(e) {
|
|
159
|
+
a(e);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
function T(e, t) {
|
|
164
|
+
let n = e.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
165
|
+
return t === "'" ? n.replaceAll("'", "'") : n.replaceAll("\"", """);
|
|
166
|
+
}
|
|
167
|
+
var E = /<(link|meta)\b(?:[^>"']|"[^"]*"|'[^']*')*>/giu, D = /([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gu;
|
|
168
|
+
function O(e, t) {
|
|
169
|
+
let n = /* @__PURE__ */ new Map(), r = new RegExp(D.source, "gu");
|
|
170
|
+
r.lastIndex = t + 1;
|
|
171
|
+
for (let t = r.exec(e); t !== null; t = r.exec(e)) {
|
|
172
|
+
let e = t[1] ?? "", r = e.toLowerCase();
|
|
173
|
+
n.has(r) || n.set(r, {
|
|
174
|
+
name: e,
|
|
175
|
+
value: t[2] ?? t[3] ?? t[4] ?? "",
|
|
176
|
+
quote: t[3] === void 0 ? "\"" : "'",
|
|
177
|
+
start: t.index,
|
|
178
|
+
end: t.index + t[0].length
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return n;
|
|
182
|
+
}
|
|
183
|
+
function k(e, t, n, r, i) {
|
|
184
|
+
let a = O(e, t.length), o;
|
|
185
|
+
if (t === "link" ? (a.get("rel")?.value.toLowerCase().split(/\s+/u))?.includes("canonical") && (o = a.get("href")) : (a.get("property") ?? a.get("name"))?.value.toLowerCase() === "og:url" && (o = a.get("content")), o === void 0) return e;
|
|
186
|
+
let s = o.value.search(/[?#]/u), c = s === -1 ? o.value : o.value.slice(0, s), l = s === -1 ? "" : o.value.slice(s), u;
|
|
187
|
+
try {
|
|
188
|
+
u = new URL(c, i);
|
|
189
|
+
} catch {
|
|
190
|
+
return e;
|
|
191
|
+
}
|
|
192
|
+
if (!f(u.pathname, n)) return e;
|
|
193
|
+
let d = `${T(`${/^(?:[a-z][a-z\d+.-]*:)?\/\//iu.test(c) ? u.origin : ""}${r}`, o.quote)}${l}`;
|
|
194
|
+
return `${e.slice(0, o.start)}${o.name}=${o.quote}${d}${o.quote}${e.slice(o.end)}`;
|
|
195
|
+
}
|
|
196
|
+
function A(e) {
|
|
197
|
+
let t = e.get("content-type")?.toLowerCase() ?? "", n = e.get("content-encoding")?.toLowerCase() ?? "";
|
|
198
|
+
if (!t.startsWith("text/html") || n !== "" && n !== "identity") return !1;
|
|
199
|
+
let r = /charset\s*=\s*"?([^";\s]+)/u.exec(t)?.[1];
|
|
200
|
+
return r === void 0 || r === "utf-8" || r === "utf8";
|
|
201
|
+
}
|
|
202
|
+
function j(e, t, n) {
|
|
203
|
+
return new Response(n, {
|
|
204
|
+
status: e.status,
|
|
205
|
+
statusText: e.statusText,
|
|
206
|
+
headers: t
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
function M(e) {
|
|
210
|
+
let t = e.now ?? Date.now, n = `${e.apiOrigin}/site/${encodeURIComponent(e.siteKey)}/server-config`, i = [], o = -Infinity, c = -Infinity, d = async () => {
|
|
211
|
+
let r = e.serverKey();
|
|
212
|
+
if (!r) {
|
|
213
|
+
i = [], o = t();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
let s = await (e.fetch ?? fetch)(n, {
|
|
218
|
+
headers: {
|
|
219
|
+
[a]: r,
|
|
220
|
+
accept: "application/json"
|
|
221
|
+
},
|
|
222
|
+
redirect: "manual",
|
|
223
|
+
signal: AbortSignal.timeout(1e4)
|
|
224
|
+
});
|
|
225
|
+
if ([
|
|
226
|
+
401,
|
|
227
|
+
403,
|
|
228
|
+
404
|
|
229
|
+
].includes(s.status)) {
|
|
230
|
+
await s.body?.cancel(), i = [], o = t();
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (!s.ok) {
|
|
234
|
+
await s.body?.cancel();
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
let c = await v(s.body, 1048576), l = c === null ? null : y(JSON.parse(new TextDecoder().decode(c)));
|
|
238
|
+
l !== null && (i = l, o = t());
|
|
239
|
+
} catch {}
|
|
240
|
+
}, m = async (e) => {
|
|
241
|
+
let n = t();
|
|
242
|
+
if (n - o < s || n - c < s) return i;
|
|
243
|
+
c = n;
|
|
244
|
+
let r = d();
|
|
245
|
+
if (e(r), o !== -Infinity) return i;
|
|
246
|
+
let a;
|
|
247
|
+
return await Promise.race([r, new Promise((e) => {
|
|
248
|
+
a = setTimeout(e, 1e3);
|
|
249
|
+
})]), clearTimeout(a), i;
|
|
250
|
+
}, _ = (e, t, n, r) => {
|
|
251
|
+
let i = new Headers(e.headers);
|
|
252
|
+
if (t.status === "running") {
|
|
253
|
+
i.set("Cache-Control", "private, no-cache");
|
|
254
|
+
for (let e of l) i.delete(e);
|
|
255
|
+
}
|
|
256
|
+
let a = e.body;
|
|
257
|
+
return a !== null && A(i) && (i.delete("content-length"), a = a.pipeThrough(w((e) => {
|
|
258
|
+
let i = e.replace(E, (e, n) => k(e, n.toLowerCase(), t.variantPath, t.testedPath, r));
|
|
259
|
+
return n === null ? i : `${i}<meta name="smileline-test" content="${T(n, "\"")}">`;
|
|
260
|
+
}))), j(e, i, a);
|
|
261
|
+
};
|
|
262
|
+
return async (t, n) => {
|
|
263
|
+
if (Reflect.get(t.locals, u) === !0) return n();
|
|
264
|
+
let { request: i } = t;
|
|
265
|
+
if (t.isPrerendered || i.method !== "GET" && i.method !== "HEAD") return n();
|
|
266
|
+
let a = new URL(t.url.href), o = a.pathname;
|
|
267
|
+
if (p(o, e.base)) return n();
|
|
268
|
+
let s = await m(g(t.locals)), c = s.find((e) => f(e.testedPath, o));
|
|
269
|
+
if (c === void 0) {
|
|
270
|
+
let e = await n();
|
|
271
|
+
if (!s.some((e) => f(e.variantPath, o))) return e;
|
|
272
|
+
let t = new Headers(e.headers);
|
|
273
|
+
return t.append("X-Robots-Tag", "noindex"), j(e, t, e.body);
|
|
274
|
+
}
|
|
275
|
+
let l = c.status === "running";
|
|
276
|
+
if ((l ? h(c.id, i) : c.winner) !== "b") {
|
|
277
|
+
let e = await n();
|
|
278
|
+
return l ? _(e, c, r(c.id, "a"), a) : e;
|
|
279
|
+
}
|
|
280
|
+
let d = null;
|
|
281
|
+
try {
|
|
282
|
+
let e = await n(`${c.variantPath}${a.search}`);
|
|
283
|
+
e.ok ? d = e : await e.body?.cancel();
|
|
284
|
+
} catch {}
|
|
285
|
+
if (d !== null) return _(d, c, l ? r(c.id, "b") : null, a);
|
|
286
|
+
Reflect.set(t.locals, u, !0);
|
|
287
|
+
let v = await t.rewrite(new URL(a.href));
|
|
288
|
+
return l ? _(v, c, r(c.id, "a"), a) : v;
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
var N = /* @__PURE__ */ new Set([
|
|
292
|
+
"sl.js",
|
|
293
|
+
"sl-tracking.js",
|
|
294
|
+
"sl-insights.js"
|
|
295
|
+
]), P = /* @__PURE__ */ new Map([
|
|
296
|
+
["forms", 64 * 1024],
|
|
297
|
+
["dni", 64 * 1024],
|
|
298
|
+
["events", 32 * 1024],
|
|
299
|
+
["recording", 16 * 1024 * 1024]
|
|
300
|
+
]), F = 256 * 1024, I = 10747904, L = 25e3, R = [
|
|
301
|
+
"content-type",
|
|
302
|
+
"accept",
|
|
303
|
+
"origin",
|
|
304
|
+
"referer",
|
|
305
|
+
"user-agent"
|
|
306
|
+
], z = [
|
|
307
|
+
"cache-control",
|
|
308
|
+
"content-type",
|
|
309
|
+
"etag",
|
|
310
|
+
"last-modified",
|
|
311
|
+
"retry-after"
|
|
312
|
+
];
|
|
313
|
+
function B(e, t = !1) {
|
|
314
|
+
let n = new Headers({
|
|
315
|
+
"Cache-Control": "no-store",
|
|
316
|
+
[o]: "1"
|
|
317
|
+
});
|
|
318
|
+
return t && n.set("X-Smileline-Direct", "1"), new Response(null, {
|
|
319
|
+
status: e,
|
|
320
|
+
headers: n
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
function V() {
|
|
324
|
+
return B(503, !0);
|
|
325
|
+
}
|
|
326
|
+
function H(e, t, n, r) {
|
|
327
|
+
let i = t.split("/");
|
|
328
|
+
if (i[0] === "site" && i.length === 3) {
|
|
329
|
+
let [, t, n = ""] = i, a = e === "GET" && n === "config" ? 0 : e === "POST" ? P.get(n) : void 0;
|
|
330
|
+
return a === void 0 ? null : t === r ? { maxBody: a } : "direct";
|
|
331
|
+
}
|
|
332
|
+
return i[0] === "capture" && i.length === 2 && e === "POST" ? /^[A-Za-z0-9_-]+$/u.test(i[1] ?? "") ? { maxBody: n?.toLowerCase().includes("multipart/form-data") ? I : F } : "direct" : null;
|
|
333
|
+
}
|
|
334
|
+
function U(e) {
|
|
335
|
+
try {
|
|
336
|
+
let t = e.clientAddress;
|
|
337
|
+
return typeof t == "string" && t !== "" ? t : null;
|
|
338
|
+
} catch {
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function W(e) {
|
|
343
|
+
let t = Reflect.get(e, "cf"), n = typeof t == "object" && t ? Reflect.get(t, "country") : null, r = typeof n == "string" ? n : e.headers.get("cf-ipcountry");
|
|
344
|
+
return r !== null && /^[A-Z]{2}$/u.test(r) ? r : null;
|
|
345
|
+
}
|
|
346
|
+
function G(e) {
|
|
347
|
+
let t = `${e.base}/_sl/`, n = async (t, n) => {
|
|
348
|
+
let r;
|
|
349
|
+
try {
|
|
350
|
+
r = await (e.fetch ?? fetch)(t, {
|
|
351
|
+
...n,
|
|
352
|
+
redirect: "manual",
|
|
353
|
+
signal: AbortSignal.timeout(L)
|
|
354
|
+
});
|
|
355
|
+
} catch {
|
|
356
|
+
return V();
|
|
357
|
+
}
|
|
358
|
+
let i = new Headers({
|
|
359
|
+
"X-Content-Type-Options": "nosniff",
|
|
360
|
+
[o]: "1"
|
|
361
|
+
});
|
|
362
|
+
for (let e of z) {
|
|
363
|
+
let t = r.headers.get(e);
|
|
364
|
+
t !== null && i.set(e, t);
|
|
365
|
+
}
|
|
366
|
+
return new Response(r.body, {
|
|
367
|
+
status: r.status,
|
|
368
|
+
statusText: r.statusText,
|
|
369
|
+
headers: i
|
|
370
|
+
});
|
|
371
|
+
};
|
|
372
|
+
return async (r) => {
|
|
373
|
+
let { request: i } = r, o = new URL(i.url);
|
|
374
|
+
if (!o.pathname.startsWith(t)) return B(404);
|
|
375
|
+
let s = o.pathname.slice(t.length).replace(/\/$/u, ""), c = i.method.toUpperCase();
|
|
376
|
+
if (N.has(s)) return c !== "GET" && c !== "HEAD" ? B(404) : n(`${e.widgetOrigin}/${s}`, { method: c });
|
|
377
|
+
let l = H(c, s, i.headers.get("content-type"), e.siteKey);
|
|
378
|
+
if (l === null) return B(404);
|
|
379
|
+
if (l === "direct") return V();
|
|
380
|
+
let u = e.serverKey(), d = U(r);
|
|
381
|
+
if (!u || d === null) return V();
|
|
382
|
+
let f = null;
|
|
383
|
+
if (l.maxBody > 0) {
|
|
384
|
+
let e = i.headers.get("content-length");
|
|
385
|
+
if (e !== null && (!/^\d+$/u.test(e) || Number(e) > l.maxBody) || (f = await v(i.body, l.maxBody), f === null)) return V();
|
|
386
|
+
}
|
|
387
|
+
let p = new Headers();
|
|
388
|
+
for (let e of R) {
|
|
389
|
+
let t = i.headers.get(e);
|
|
390
|
+
t !== null && p.set(e, t);
|
|
391
|
+
}
|
|
392
|
+
p.set(a, u), p.set("X-Smileline-Client-IP", d);
|
|
393
|
+
let m = W(i);
|
|
394
|
+
m !== null && p.set("X-Smileline-Client-Country", m);
|
|
395
|
+
let h = i.headers.get("user-agent");
|
|
396
|
+
h !== null && p.set("X-Smileline-Client-UA", h);
|
|
397
|
+
let g = i.headers.get("sec-gpc");
|
|
398
|
+
return g !== null && p.set("Sec-GPC", g), n(`${e.apiOrigin}/${s}${o.search}`, {
|
|
399
|
+
method: c,
|
|
400
|
+
headers: p,
|
|
401
|
+
body: f
|
|
402
|
+
});
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
//#endregion
|
|
406
|
+
export { M as createMiddleware, G as createProxy };
|
package/package.json
CHANGED
|
@@ -1 +1,29 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@smileline/astro",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Smileline for Astro sites: the tracking script served from your own domain, and A/B page tests split on the server.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"private": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./runtime": {
|
|
21
|
+
"types": "./dist/runtime.d.ts",
|
|
22
|
+
"default": "./dist/runtime.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"astro": "^7.0.0"
|
|
27
|
+
},
|
|
28
|
+
"types": "./dist/index.d.ts"
|
|
29
|
+
}
|