@smileline/astro 0.0.0 → 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/README.md +116 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +69 -0
- package/dist/runtime.d.ts +36 -0
- package/dist/runtime.js +402 -0
- package/package.json +29 -1
package/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
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. You pick what decides the winner: bookings (the default), leads,
|
|
52
|
+
calls and WhatsApp taps, or a step both versions have.
|
|
53
|
+
|
|
54
|
+
Every visitor is counted, whatever they answer to a cookie banner:
|
|
55
|
+
Smileline counts them with an anonymous code made from their connection and
|
|
56
|
+
browser that changes every month, and nothing is stored on their device.
|
|
57
|
+
|
|
58
|
+
Requirements:
|
|
59
|
+
|
|
60
|
+
- **A server adapter**, and **both pages render on demand**:
|
|
61
|
+
`export const prerender = false;` in each. A prerendered B cannot be shown
|
|
62
|
+
at A's URL; visitors then get A.
|
|
63
|
+
- **Leave the `/_sl/` path free** for the integration's route.
|
|
64
|
+
- **Do not cache tested pages at a CDN.** Their responses are marked
|
|
65
|
+
`Cache-Control: private, no-cache` and CDN cache headers are removed;
|
|
66
|
+
a CDN rule that caches them anyway would show everyone one version.
|
|
67
|
+
|
|
68
|
+
What the integration does on a tested page:
|
|
69
|
+
|
|
70
|
+
- It never sets a cookie. A visitor whose browser has stored a Smileline
|
|
71
|
+
visitor id is assigned from it; anyone else from their address and
|
|
72
|
+
browser, so they keep seeing the same version while those stay the same.
|
|
73
|
+
- It adds `<meta name="smileline-test">` to the page, which the script
|
|
74
|
+
reports with the page view.
|
|
75
|
+
- A canonical link or `og:url` on B that points at B is pointed at A, and
|
|
76
|
+
B's own URL is answered with `X-Robots-Tag: noindex`.
|
|
77
|
+
- If B fails to render, the visitor gets A.
|
|
78
|
+
- Once you pick a winner, everyone gets it, until you archive the test.
|
|
79
|
+
|
|
80
|
+
## Steps
|
|
81
|
+
|
|
82
|
+
The script measures on its own when a visitor sees a form, starts filling
|
|
83
|
+
it in, sends it, or taps your phone number or WhatsApp. Name any other step
|
|
84
|
+
your page has — each question of a quiz, a "Book now" button — and the
|
|
85
|
+
test shows how far each version's visitors got:
|
|
86
|
+
|
|
87
|
+
```html
|
|
88
|
+
<!-- counted when it's clicked -->
|
|
89
|
+
<button data-sl-step="Book now clicked">Book now</button>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
// or from your own code
|
|
94
|
+
window.sl?.("step", "What is your age?");
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
A lead sent from a tested page counts for the version it was on. If your
|
|
98
|
+
form posts to your own server and you forward the lead to Smileline, add
|
|
99
|
+
the page's version to the `_sl` attribution you send:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
const test = document
|
|
103
|
+
.querySelector('meta[name="smileline-test"]')
|
|
104
|
+
?.getAttribute("content");
|
|
105
|
+
// _sl: JSON.stringify({ ..., ...(test ? { test } : {}) })
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Without an adapter
|
|
109
|
+
|
|
110
|
+
A fully static build still gets the script, loaded straight from
|
|
111
|
+
Smileline, and page analytics. A/B tests and the `/_sl/` route need an
|
|
112
|
+
adapter, and the build prints a warning.
|
|
113
|
+
|
|
114
|
+
## Licence
|
|
115
|
+
|
|
116
|
+
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,69 @@
|
|
|
1
|
+
import { writeFileSync as e } from "node:fs";
|
|
2
|
+
// 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
|
+
export { a as default };
|
|
@@ -0,0 +1,36 @@
|
|
|
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. `clientAddress`
|
|
19
|
+
* throws where the adapter cannot tell. */
|
|
20
|
+
export interface MiddlewareContext {
|
|
21
|
+
request: Request;
|
|
22
|
+
url: URL;
|
|
23
|
+
isPrerendered: boolean;
|
|
24
|
+
locals: object;
|
|
25
|
+
readonly clientAddress: string;
|
|
26
|
+
rewrite(payload: string | URL | Request): Promise<Response>;
|
|
27
|
+
}
|
|
28
|
+
export type MiddlewareNext = (payload?: string | URL | Request) => Promise<Response>;
|
|
29
|
+
/** The part of Astro's APIContext the proxy route uses. `clientAddress`
|
|
30
|
+
* throws where the adapter cannot tell. */
|
|
31
|
+
export interface ProxyContext {
|
|
32
|
+
request: Request;
|
|
33
|
+
readonly clientAddress: string;
|
|
34
|
+
}
|
|
35
|
+
export declare function createMiddleware(options: RuntimeOptions): (context: MiddlewareContext, next: MiddlewareNext) => Promise<Response>;
|
|
36
|
+
export declare function createProxy(options: ProxyOptions): (context: ProxyContext) => Promise<Response>;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
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
|
+
// ../tracking-core/src/envelope.ts
|
|
17
|
+
function i(e) {
|
|
18
|
+
let t = {};
|
|
19
|
+
for (let n of e.split(";")) {
|
|
20
|
+
let e = n.indexOf("=");
|
|
21
|
+
if (e === -1) continue;
|
|
22
|
+
let r = n.slice(0, e).trim();
|
|
23
|
+
if (r === "" || r in t) continue;
|
|
24
|
+
let i = n.slice(e + 1).trim();
|
|
25
|
+
try {
|
|
26
|
+
i = decodeURIComponent(i);
|
|
27
|
+
} catch {}
|
|
28
|
+
i !== "" && (t[r] = i);
|
|
29
|
+
}
|
|
30
|
+
return t;
|
|
31
|
+
}
|
|
32
|
+
// ../tracking-core/src/state.ts
|
|
33
|
+
var a = "X-Smileline-Server-Key", o = "X-Smileline-Proxy", s = 6e4, c = 262144, l = [
|
|
34
|
+
"CDN-Cache-Control",
|
|
35
|
+
"Cloudflare-CDN-Cache-Control",
|
|
36
|
+
"Vercel-CDN-Cache-Control",
|
|
37
|
+
"Netlify-CDN-Cache-Control",
|
|
38
|
+
"Surrogate-Control"
|
|
39
|
+
], u = Symbol.for("@smileline/astro:fallback");
|
|
40
|
+
function d(e) {
|
|
41
|
+
return e.length > 1 && e.endsWith("/") ? e.slice(0, -1) : e;
|
|
42
|
+
}
|
|
43
|
+
function f(e, t) {
|
|
44
|
+
return d(e) === d(t);
|
|
45
|
+
}
|
|
46
|
+
function p(e, t) {
|
|
47
|
+
return e === `${t}/_sl` || e.startsWith(`${t}/_sl/`);
|
|
48
|
+
}
|
|
49
|
+
function m() {
|
|
50
|
+
return (crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(1))[0] ?? 0) & 1 ? "b" : "a";
|
|
51
|
+
}
|
|
52
|
+
function h(e, t, r) {
|
|
53
|
+
let a = i(t.headers.get("cookie") ?? "")._sl_vid;
|
|
54
|
+
if (a) return n(a, e);
|
|
55
|
+
let o = t.headers.get("user-agent");
|
|
56
|
+
return r !== null && o ? n(`anon:${r}|${o}`, 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) i = [], o = t();
|
|
213
|
+
else try {
|
|
214
|
+
let s = await (e.fetch ?? fetch)(n, {
|
|
215
|
+
headers: {
|
|
216
|
+
[a]: r,
|
|
217
|
+
accept: "application/json"
|
|
218
|
+
},
|
|
219
|
+
redirect: "manual",
|
|
220
|
+
signal: AbortSignal.timeout(1e4)
|
|
221
|
+
});
|
|
222
|
+
if ([
|
|
223
|
+
401,
|
|
224
|
+
403,
|
|
225
|
+
404
|
|
226
|
+
].includes(s.status)) {
|
|
227
|
+
await s.body?.cancel(), i = [], o = t();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (!s.ok) {
|
|
231
|
+
await s.body?.cancel();
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
let c = await v(s.body, 1048576), l = c === null ? null : y(JSON.parse(new TextDecoder().decode(c)));
|
|
235
|
+
l !== null && (i = l, o = t());
|
|
236
|
+
} catch {}
|
|
237
|
+
}, m = async (e) => {
|
|
238
|
+
let n = t();
|
|
239
|
+
if (n - o < s || n - c < s) return i;
|
|
240
|
+
c = n;
|
|
241
|
+
let r = d();
|
|
242
|
+
if (e(r), o !== -Infinity) return i;
|
|
243
|
+
let a;
|
|
244
|
+
return await Promise.race([r, new Promise((e) => {
|
|
245
|
+
a = setTimeout(e, 1e3);
|
|
246
|
+
})]), clearTimeout(a), i;
|
|
247
|
+
}, _ = (e, t, n, r) => {
|
|
248
|
+
let i = new Headers(e.headers);
|
|
249
|
+
if (t.status === "running") {
|
|
250
|
+
i.set("Cache-Control", "private, no-cache");
|
|
251
|
+
for (let e of l) i.delete(e);
|
|
252
|
+
}
|
|
253
|
+
let a = e.body;
|
|
254
|
+
return a !== null && A(i) && (i.delete("content-length"), a = a.pipeThrough(w((e) => {
|
|
255
|
+
let i = e.replace(E, (e, n) => k(e, n.toLowerCase(), t.variantPath, t.testedPath, r));
|
|
256
|
+
return n === null ? i : `${i}<meta name="smileline-test" content="${T(n, "\"")}">`;
|
|
257
|
+
}))), j(e, i, a);
|
|
258
|
+
};
|
|
259
|
+
return async (t, n) => {
|
|
260
|
+
if (Reflect.get(t.locals, u) === !0) return n();
|
|
261
|
+
let { request: i } = t;
|
|
262
|
+
if (t.isPrerendered || i.method !== "GET" && i.method !== "HEAD") return n();
|
|
263
|
+
let a = new URL(t.url.href), o = a.pathname;
|
|
264
|
+
if (p(o, e.base)) return n();
|
|
265
|
+
let s = await m(g(t.locals)), c = s.find((e) => f(e.testedPath, o));
|
|
266
|
+
if (c === void 0) {
|
|
267
|
+
let e = await n();
|
|
268
|
+
if (!s.some((e) => f(e.variantPath, o))) return e;
|
|
269
|
+
let t = new Headers(e.headers);
|
|
270
|
+
return t.append("X-Robots-Tag", "noindex"), j(e, t, e.body);
|
|
271
|
+
}
|
|
272
|
+
let l = c.status === "running";
|
|
273
|
+
if ((l ? h(c.id, i, U(t)) : c.winner) !== "b") {
|
|
274
|
+
let e = await n();
|
|
275
|
+
return l ? _(e, c, r(c.id, "a"), a) : e;
|
|
276
|
+
}
|
|
277
|
+
let d = null;
|
|
278
|
+
try {
|
|
279
|
+
let e = await n(`${c.variantPath}${a.search}`);
|
|
280
|
+
e.ok ? d = e : await e.body?.cancel();
|
|
281
|
+
} catch {}
|
|
282
|
+
if (d !== null) return _(d, c, l ? r(c.id, "b") : null, a);
|
|
283
|
+
Reflect.set(t.locals, u, !0);
|
|
284
|
+
let v = await t.rewrite(new URL(a.href));
|
|
285
|
+
return l ? _(v, c, r(c.id, "a"), a) : v;
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
var N = /* @__PURE__ */ new Set([
|
|
289
|
+
"sl.js",
|
|
290
|
+
"sl-tracking.js",
|
|
291
|
+
"sl-insights.js"
|
|
292
|
+
]), P = /* @__PURE__ */ new Map([
|
|
293
|
+
["forms", 65536],
|
|
294
|
+
["dni", 65536],
|
|
295
|
+
["events", 32768],
|
|
296
|
+
["recording", 16777216]
|
|
297
|
+
]), F = 262144, I = 10747904, L = 25e3, R = [
|
|
298
|
+
"content-type",
|
|
299
|
+
"accept",
|
|
300
|
+
"origin",
|
|
301
|
+
"referer",
|
|
302
|
+
"user-agent"
|
|
303
|
+
], z = [
|
|
304
|
+
"cache-control",
|
|
305
|
+
"content-type",
|
|
306
|
+
"etag",
|
|
307
|
+
"last-modified",
|
|
308
|
+
"retry-after"
|
|
309
|
+
];
|
|
310
|
+
function B(e, t = !1) {
|
|
311
|
+
let n = new Headers({
|
|
312
|
+
"Cache-Control": "no-store",
|
|
313
|
+
[o]: "1"
|
|
314
|
+
});
|
|
315
|
+
return t && n.set("X-Smileline-Direct", "1"), new Response(null, {
|
|
316
|
+
status: e,
|
|
317
|
+
headers: n
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function V() {
|
|
321
|
+
return B(503, !0);
|
|
322
|
+
}
|
|
323
|
+
function H(e, t, n, r) {
|
|
324
|
+
let i = t.split("/");
|
|
325
|
+
if (i[0] === "site" && i.length === 3) {
|
|
326
|
+
let [, t, n = ""] = i, a = e === "GET" && n === "config" ? 0 : e === "POST" ? P.get(n) : void 0;
|
|
327
|
+
return a === void 0 ? null : t === r ? { maxBody: a } : "direct";
|
|
328
|
+
}
|
|
329
|
+
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;
|
|
330
|
+
}
|
|
331
|
+
function U(e) {
|
|
332
|
+
try {
|
|
333
|
+
let t = e.clientAddress;
|
|
334
|
+
return typeof t == "string" && t !== "" ? t : null;
|
|
335
|
+
} catch {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
function W(e) {
|
|
340
|
+
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");
|
|
341
|
+
return r !== null && /^[A-Z]{2}$/u.test(r) ? r : null;
|
|
342
|
+
}
|
|
343
|
+
function G(e) {
|
|
344
|
+
let t = `${e.base}/_sl/`, n = async (t, n) => {
|
|
345
|
+
let r;
|
|
346
|
+
try {
|
|
347
|
+
r = await (e.fetch ?? fetch)(t, {
|
|
348
|
+
...n,
|
|
349
|
+
redirect: "manual",
|
|
350
|
+
signal: AbortSignal.timeout(L)
|
|
351
|
+
});
|
|
352
|
+
} catch {
|
|
353
|
+
return V();
|
|
354
|
+
}
|
|
355
|
+
let i = new Headers({
|
|
356
|
+
"X-Content-Type-Options": "nosniff",
|
|
357
|
+
[o]: "1"
|
|
358
|
+
});
|
|
359
|
+
for (let e of z) {
|
|
360
|
+
let t = r.headers.get(e);
|
|
361
|
+
t !== null && i.set(e, t);
|
|
362
|
+
}
|
|
363
|
+
return new Response(r.body, {
|
|
364
|
+
status: r.status,
|
|
365
|
+
statusText: r.statusText,
|
|
366
|
+
headers: i
|
|
367
|
+
});
|
|
368
|
+
};
|
|
369
|
+
return async (r) => {
|
|
370
|
+
let { request: i } = r, o = new URL(i.url);
|
|
371
|
+
if (!o.pathname.startsWith(t)) return B(404);
|
|
372
|
+
let s = o.pathname.slice(t.length).replace(/\/$/u, ""), c = i.method.toUpperCase();
|
|
373
|
+
if (N.has(s)) return c !== "GET" && c !== "HEAD" ? B(404) : n(`${e.widgetOrigin}/${s}`, { method: c });
|
|
374
|
+
let l = H(c, s, i.headers.get("content-type"), e.siteKey);
|
|
375
|
+
if (l === null) return B(404);
|
|
376
|
+
if (l === "direct") return V();
|
|
377
|
+
let u = e.serverKey(), d = U(r);
|
|
378
|
+
if (!u || d === null) return V();
|
|
379
|
+
let f = null;
|
|
380
|
+
if (l.maxBody > 0) {
|
|
381
|
+
let e = i.headers.get("content-length");
|
|
382
|
+
if (e !== null && (!/^\d+$/u.test(e) || Number(e) > l.maxBody) || (f = await v(i.body, l.maxBody), f === null)) return V();
|
|
383
|
+
}
|
|
384
|
+
let p = new Headers();
|
|
385
|
+
for (let e of R) {
|
|
386
|
+
let t = i.headers.get(e);
|
|
387
|
+
t !== null && p.set(e, t);
|
|
388
|
+
}
|
|
389
|
+
p.set(a, u), p.set("X-Smileline-Client-IP", d);
|
|
390
|
+
let m = W(i);
|
|
391
|
+
m !== null && p.set("X-Smileline-Client-Country", m);
|
|
392
|
+
let h = i.headers.get("user-agent");
|
|
393
|
+
h !== null && p.set("X-Smileline-Client-UA", h);
|
|
394
|
+
let g = i.headers.get("sec-gpc");
|
|
395
|
+
return g !== null && p.set("Sec-GPC", g), n(`${e.apiOrigin}/${s}${o.search}`, {
|
|
396
|
+
method: c,
|
|
397
|
+
headers: p,
|
|
398
|
+
body: f
|
|
399
|
+
});
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
export { M as createMiddleware, G as createProxy };
|
package/package.json
CHANGED
|
@@ -1 +1,29 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@smileline/astro",
|
|
3
|
+
"version": "0.2.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": "^6.0.0 || ^7.0.0"
|
|
27
|
+
},
|
|
28
|
+
"types": "./dist/index.d.ts"
|
|
29
|
+
}
|