@koryla/node 0.1.1 → 0.1.3
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/dist/index.cjs +12 -10
- package/dist/index.js +13 -11
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -40,17 +40,18 @@ function korylaMiddleware(options) {
|
|
|
40
40
|
const variantPath = new URL(result.targetUrl).pathname;
|
|
41
41
|
req.url = variantPath;
|
|
42
42
|
if (req.originalUrl) req.originalUrl = variantPath;
|
|
43
|
+
const maxAge = 60 * 60 * 24 * 30;
|
|
44
|
+
const newCookies = [];
|
|
43
45
|
if (result.isNewAssignment) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
newCookies.push(`${result.cookieName}=${result.variantId}; Path=/; Max-Age=${maxAge}; SameSite=Lax`);
|
|
47
|
+
}
|
|
48
|
+
if (result.isNewSession) {
|
|
49
|
+
newCookies.push(`ky_sid=${result.sessionId}; Path=/; Max-Age=${maxAge}; SameSite=Lax`);
|
|
50
|
+
}
|
|
51
|
+
if (newCookies.length) {
|
|
46
52
|
const existing = res.getHeader("Set-Cookie");
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
} else if (existing) {
|
|
50
|
-
res.setHeader("Set-Cookie", [existing, cookie]);
|
|
51
|
-
} else {
|
|
52
|
-
res.setHeader("Set-Cookie", cookie);
|
|
53
|
-
}
|
|
53
|
+
const all = [...Array.isArray(existing) ? existing : existing ? [existing] : [], ...newCookies];
|
|
54
|
+
res.setHeader("Set-Cookie", all.length === 1 ? all[0] : all);
|
|
54
55
|
}
|
|
55
56
|
next();
|
|
56
57
|
} catch {
|
|
@@ -65,13 +66,14 @@ async function trackConversion(req, options) {
|
|
|
65
66
|
);
|
|
66
67
|
const sessionId = cookies["ky_sid"];
|
|
67
68
|
if (!sessionId) return;
|
|
69
|
+
const host = (0, import_core.hostFromUrl)(`http://${req.headers["x-forwarded-host"] ?? req.headers["host"] ?? ""}`);
|
|
68
70
|
for (const [key, value] of Object.entries(cookies)) {
|
|
69
71
|
if (!key.startsWith("ky_") || key === "ky_sid") continue;
|
|
70
72
|
const experimentId = key.slice(3);
|
|
71
73
|
try {
|
|
72
74
|
await fetch(`${options.apiUrl}/api/worker/event`, {
|
|
73
75
|
method: "POST",
|
|
74
|
-
headers: { "Content-Type": "application/json", Authorization: `Bearer ${options.apiKey}
|
|
76
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${options.apiKey}`, ...(0, import_core.hostHeader)(host) },
|
|
75
77
|
body: JSON.stringify({ experiment_id: experimentId, variant_id: value, session_id: sessionId, event_type: "conversion" })
|
|
76
78
|
});
|
|
77
79
|
} catch {
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { createSplitEngine } from "@koryla/core";
|
|
2
|
+
import { createSplitEngine, hostFromUrl, hostHeader } from "@koryla/core";
|
|
3
3
|
var engine = null;
|
|
4
4
|
function korylaMiddleware(options) {
|
|
5
5
|
return async function middleware(req, res, next) {
|
|
@@ -15,17 +15,18 @@ function korylaMiddleware(options) {
|
|
|
15
15
|
const variantPath = new URL(result.targetUrl).pathname;
|
|
16
16
|
req.url = variantPath;
|
|
17
17
|
if (req.originalUrl) req.originalUrl = variantPath;
|
|
18
|
+
const maxAge = 60 * 60 * 24 * 30;
|
|
19
|
+
const newCookies = [];
|
|
18
20
|
if (result.isNewAssignment) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
newCookies.push(`${result.cookieName}=${result.variantId}; Path=/; Max-Age=${maxAge}; SameSite=Lax`);
|
|
22
|
+
}
|
|
23
|
+
if (result.isNewSession) {
|
|
24
|
+
newCookies.push(`ky_sid=${result.sessionId}; Path=/; Max-Age=${maxAge}; SameSite=Lax`);
|
|
25
|
+
}
|
|
26
|
+
if (newCookies.length) {
|
|
21
27
|
const existing = res.getHeader("Set-Cookie");
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
} else if (existing) {
|
|
25
|
-
res.setHeader("Set-Cookie", [existing, cookie]);
|
|
26
|
-
} else {
|
|
27
|
-
res.setHeader("Set-Cookie", cookie);
|
|
28
|
-
}
|
|
28
|
+
const all = [...Array.isArray(existing) ? existing : existing ? [existing] : [], ...newCookies];
|
|
29
|
+
res.setHeader("Set-Cookie", all.length === 1 ? all[0] : all);
|
|
29
30
|
}
|
|
30
31
|
next();
|
|
31
32
|
} catch {
|
|
@@ -40,13 +41,14 @@ async function trackConversion(req, options) {
|
|
|
40
41
|
);
|
|
41
42
|
const sessionId = cookies["ky_sid"];
|
|
42
43
|
if (!sessionId) return;
|
|
44
|
+
const host = hostFromUrl(`http://${req.headers["x-forwarded-host"] ?? req.headers["host"] ?? ""}`);
|
|
43
45
|
for (const [key, value] of Object.entries(cookies)) {
|
|
44
46
|
if (!key.startsWith("ky_") || key === "ky_sid") continue;
|
|
45
47
|
const experimentId = key.slice(3);
|
|
46
48
|
try {
|
|
47
49
|
await fetch(`${options.apiUrl}/api/worker/event`, {
|
|
48
50
|
method: "POST",
|
|
49
|
-
headers: { "Content-Type": "application/json", Authorization: `Bearer ${options.apiKey}
|
|
51
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${options.apiKey}`, ...hostHeader(host) },
|
|
50
52
|
body: JSON.stringify({ experiment_id: experimentId, variant_id: value, session_id: sessionId, event_type: "conversion" })
|
|
51
53
|
});
|
|
52
54
|
} catch {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koryla/node",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"dev": "tsup src/index.ts --format esm,cjs --dts --watch"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@koryla/core": "^0.2.
|
|
26
|
+
"@koryla/core": "^0.2.4"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"tsup": "^8.0.0",
|