@koryla/node 0.1.1 → 0.1.2

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 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
- const maxAge = 60 * 60 * 24 * 30;
45
- const cookie = `${result.cookieName}=${result.variantId}; Path=/; Max-Age=${maxAge}; SameSite=Lax`;
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
- if (Array.isArray(existing)) {
48
- res.setHeader("Set-Cookie", [...existing, cookie]);
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 {
package/dist/index.js CHANGED
@@ -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
- const maxAge = 60 * 60 * 24 * 30;
20
- const cookie = `${result.cookieName}=${result.variantId}; Path=/; Max-Age=${maxAge}; SameSite=Lax`;
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
- if (Array.isArray(existing)) {
23
- res.setHeader("Set-Cookie", [...existing, cookie]);
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 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koryla/node",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -18,15 +18,15 @@
18
18
  "publishConfig": {
19
19
  "access": "public"
20
20
  },
21
- "scripts": {
22
- "build": "tsup src/index.ts --format esm,cjs --dts --clean",
23
- "dev": "tsup src/index.ts --format esm,cjs --dts --watch"
24
- },
25
21
  "dependencies": {
26
22
  "@koryla/core": "^0.2.0"
27
23
  },
28
24
  "devDependencies": {
29
25
  "tsup": "^8.0.0",
30
26
  "typescript": "^5"
27
+ },
28
+ "scripts": {
29
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
30
+ "dev": "tsup src/index.ts --format esm,cjs --dts --watch"
31
31
  }
32
- }
32
+ }