@obelism/improve-sdk-next 0.5.2 → 1.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/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/server");let t=RegExp(["Vercelbot","DotBot","Yeti","AhrefsBot","SeznamBot","YandexBot","UptimeRobot","Bytespider","Baiduspider","fidget-spinner-bot","AASA-Bot","FullStoryBot","SemrushBot","PetalBot","RyteBot","Pinterestbot","AntBot","Monsidobot","blowra","JobRoboter","Qwantify","YisouSpider"].map(e=>e.toLowerCase()).join("|"),"i"),o=/(\[[a-zA-Z0-9-]+\])+/g,r=/\[\[\.\.\.[a-zA-Z0-9-]+\]\]/g,s=/\[\.\.\.[a-zA-Z0-9-]+\]/g,n=e=>e.endsWith("/")?e.substring(0,e.length-1):e,i=new Map;exports.generateImproveNextMiddleware=a=>l=>{try{let p,{ua:u="",isBot:g=!1}=e.userAgent(l);if(g||u&&t.test(u.toLowerCase()))return e.NextResponse.next();let d=a.serverABtests.find(e=>((e,t)=>{let a=n(t.split("?")[0]),l=n(e.split("?")[0]);if(a===l)return!0;let p=i.get(l);return p||(p=new RegExp(`^${l.replace(o,"[a-zA-Z0-9-]+")}$`.replace(r,"?.*").replace(s,".*")),i.set(l,p)),p.test(a)})(e.routeHandler,l.nextUrl.pathname));if(!d)return e.NextResponse.next();let x=d.options.at(0)?.value;if(!x)return e.NextResponse.next();let
|
|
1
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/server");let t=RegExp(["Vercelbot","DotBot","Yeti","AhrefsBot","SeznamBot","YandexBot","UptimeRobot","Bytespider","Baiduspider","fidget-spinner-bot","AASA-Bot","FullStoryBot","SemrushBot","PetalBot","RyteBot","Pinterestbot","AntBot","Monsidobot","blowra","JobRoboter","Qwantify","YisouSpider"].map(e=>e.toLowerCase()).join("|"),"i"),o=/(\[[a-zA-Z0-9-]+\])+/g,r=/\[\[\.\.\.[a-zA-Z0-9-]+\]\]/g,s=/\[\.\.\.[a-zA-Z0-9-]+\]/g,n=e=>e.endsWith("/")?e.substring(0,e.length-1):e,i=new Map;exports.generateImproveNextMiddleware=a=>l=>{try{let p,{ua:u="",isBot:g=!1}=e.userAgent(l);if(g||u&&t.test(u.toLowerCase()))return e.NextResponse.next();let d=a.serverABtests.find(e=>((e,t)=>{let a=n(t.split("?")[0]),l=n(e.split("?")[0]);if(a===l)return!0;let p=i.get(l);return p||(p=new RegExp(`^${l.replace(o,"[a-zA-Z0-9-]+")}$`.replace(r,"?.*").replace(s,".*")),i.set(l,p)),p.test(a)})(e.routeHandler,l.nextUrl.pathname));if(!d)return e.NextResponse.next();let x=d.options.at(0)?.value;if(!x)return e.NextResponse.next();let v=a.improveSdk.getVisitorCookieName(),m=l.cookies.get(v)?.value,c=m&&a.improveSdk.validateVisitorId(m)?m:a.improveSdk.generateVisitorId(),f=e=>e&&a.improveSdk.validateTestValue(d.slug,e)?e:null,A={country:l.headers.get("x-vercel-ip-country")??l.headers.get("cf-ipcountry")??void 0},B=f(l.nextUrl.searchParams.get(d.slug))||f(l.cookies.get(d.slug)?.value)||a.improveSdk.getTestValue(d.slug,c,u,A),S=d.options.find(e=>e.value===B);if(!S)return e.NextResponse.next();if(S.value!==x){let t=l.nextUrl.clone();t.pathname=d?.formatSlug?d?.formatSlug(t,S)?.pathname:S.slug,p=e.NextResponse.rewrite(t)}else p=e.NextResponse.next();return p.cookies.set(v,c,{maxAge:604800,...a.options?.visitorId}),p.cookies.set(d.slug,B,{maxAge:604800,...a.options?.testValue}),p}catch(t){return console.error("[Improve SDK]",t),e.NextResponse.next()}};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/extendedBotCheck.ts","../src/matchesRoute.ts","../src/index.ts"],"sourcesContent":["const EXTENDED_BOT_REGEX = new RegExp(\n\t[\n\t\t'Vercelbot',\n\t\t'DotBot',\n\t\t'Yeti',\n\t\t'AhrefsBot',\n\t\t'SeznamBot',\n\t\t'YandexBot',\n\t\t'UptimeRobot',\n\t\t'Bytespider',\n\t\t'Baiduspider',\n\t\t'fidget-spinner-bot',\n\t\t'AASA-Bot',\n\t\t'FullStoryBot',\n\t\t'SemrushBot',\n\t\t'PetalBot',\n\t\t'RyteBot',\n\t\t'Pinterestbot',\n\t\t'AntBot',\n\t\t'Monsidobot',\n\t\t'blowra',\n\t\t'JobRoboter',\n\t\t'Qwantify',\n\t\t'YisouSpider',\n\t]\n\t\t.map((name) => name.toLowerCase())\n\t\t.join('|'),\n\t'i',\n)\n\nexport const extendedBotCheck = (userAgent: string): boolean => {\n\tif (!userAgent) return false\n\treturn EXTENDED_BOT_REGEX.test(userAgent.toLowerCase())\n}\n","const BASE_REGEX = /(\\[[a-zA-Z0-9-]+\\])+/g\n\nconst SECONDARY_REGEX = /\\[\\[\\.\\.\\.[a-zA-Z0-9-]+\\]\\]/g\n\nconst TERTIARY_REGEX = /\\[\\.\\.\\.[a-zA-Z0-9-]+\\]/g\n\nexport const removeTrailingSlash = (val: string) =>\n\tval.endsWith('/') ? val.substring(0, val.length - 1) : val\n\nconst regexCache = new Map<string, RegExp>()\n\nexport const matchesRoute = (route: string, pathname: string) => {\n\tconst basePathname = removeTrailingSlash(pathname.split('?')[0] as string)\n\tconst baseRoute = removeTrailingSlash(route.split('?')[0] as string)\n\n\tif (basePathname === baseRoute) return true\n\n\tlet routeRegex = regexCache.get(baseRoute)\n\tif (!routeRegex) {\n\t\trouteRegex = new RegExp(\n\t\t\t`^${baseRoute.replace(BASE_REGEX, '[a-zA-Z0-9-]+')}$`\n\t\t\t\t.replace(SECONDARY_REGEX, '?.*')\n\t\t\t\t.replace(TERTIARY_REGEX, '.*'),\n\t\t)\n\t\tregexCache.set(baseRoute, routeRegex)\n\t}\n\n\treturn routeRegex.test(basePathname)\n}\n","import { ImproveServerSDK } from '@obelism/improve-sdk/server'\nimport type { NextURL } from 'next/dist/server/web/next-url'\nimport type { NextRequest } from 'next/server'\nimport { NextResponse, userAgent } from 'next/server'\n\nimport { extendedBotCheck } from './extendedBotCheck'\nimport { matchesRoute } from './matchesRoute'\nimport type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies'\n\nconst DEFAULT_COOKIE_TTL = 60 * 60 * 24 * 7\n\nexport type OptionConfig = { value: string; slug: string }\n\nexport type ServerABTestConfig = {\n\tslug: string\n\trouteHandler: string\n\tformatSlug?: (url: NextURL, matchingOption: OptionConfig) => NextURL\n\toptions: OptionConfig[]\n}\n\nexport type GenerateImproveNextMiddlewareArgs = {\n\timproveSdk: ImproveServerSDK\n\tserverABtests: ServerABTestConfig[]\n\toptions?: {\n\t\tvisitorId?: ResponseCookie\n\t\ttestValue?: ResponseCookie\n\t}\n}\n\nexport const generateImproveNextMiddleware = (\n\targs: GenerateImproveNextMiddlewareArgs,\n) => {\n\treturn (request: NextRequest) => {\n\t\ttry {\n\t\t\t// Early esacape for bots that shouldn't be getting AB tested\n\t\t\tconst { ua = '', isBot = false } = userAgent(request)\n\t\t\tif (isBot || extendedBotCheck(ua)) return NextResponse.next()\n\n\t\t\t// When there is no (valid) server AB config escape\n\t\t\tconst serverABTestConfig = args.serverABtests.find((route) =>\n\t\t\t\tmatchesRoute(route.routeHandler, request.nextUrl.pathname),\n\t\t\t)\n\t\t\tif (!serverABTestConfig) return NextResponse.next()\n\n\t\t\tconst basePath = serverABTestConfig.options.at(0)?.value\n\n\t\t\tif (!basePath) return NextResponse.next()\n\n\t\t\tconst visitorCookieName = args.improveSdk.getVisitorCookieName()\n\t\t\tconst cookieVisitorId = request.cookies.get(visitorCookieName)?.value\n\t\t\tconst validCookieVisitorId =\n\t\t\t\tcookieVisitorId
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/extendedBotCheck.ts","../src/matchesRoute.ts","../src/index.ts"],"sourcesContent":["const EXTENDED_BOT_REGEX = new RegExp(\n\t[\n\t\t'Vercelbot',\n\t\t'DotBot',\n\t\t'Yeti',\n\t\t'AhrefsBot',\n\t\t'SeznamBot',\n\t\t'YandexBot',\n\t\t'UptimeRobot',\n\t\t'Bytespider',\n\t\t'Baiduspider',\n\t\t'fidget-spinner-bot',\n\t\t'AASA-Bot',\n\t\t'FullStoryBot',\n\t\t'SemrushBot',\n\t\t'PetalBot',\n\t\t'RyteBot',\n\t\t'Pinterestbot',\n\t\t'AntBot',\n\t\t'Monsidobot',\n\t\t'blowra',\n\t\t'JobRoboter',\n\t\t'Qwantify',\n\t\t'YisouSpider',\n\t]\n\t\t.map((name) => name.toLowerCase())\n\t\t.join('|'),\n\t'i',\n)\n\nexport const extendedBotCheck = (userAgent: string): boolean => {\n\tif (!userAgent) return false\n\treturn EXTENDED_BOT_REGEX.test(userAgent.toLowerCase())\n}\n","const BASE_REGEX = /(\\[[a-zA-Z0-9-]+\\])+/g\n\nconst SECONDARY_REGEX = /\\[\\[\\.\\.\\.[a-zA-Z0-9-]+\\]\\]/g\n\nconst TERTIARY_REGEX = /\\[\\.\\.\\.[a-zA-Z0-9-]+\\]/g\n\nexport const removeTrailingSlash = (val: string) =>\n\tval.endsWith('/') ? val.substring(0, val.length - 1) : val\n\nconst regexCache = new Map<string, RegExp>()\n\nexport const matchesRoute = (route: string, pathname: string) => {\n\tconst basePathname = removeTrailingSlash(pathname.split('?')[0] as string)\n\tconst baseRoute = removeTrailingSlash(route.split('?')[0] as string)\n\n\tif (basePathname === baseRoute) return true\n\n\tlet routeRegex = regexCache.get(baseRoute)\n\tif (!routeRegex) {\n\t\trouteRegex = new RegExp(\n\t\t\t`^${baseRoute.replace(BASE_REGEX, '[a-zA-Z0-9-]+')}$`\n\t\t\t\t.replace(SECONDARY_REGEX, '?.*')\n\t\t\t\t.replace(TERTIARY_REGEX, '.*'),\n\t\t)\n\t\tregexCache.set(baseRoute, routeRegex)\n\t}\n\n\treturn routeRegex.test(basePathname)\n}\n","import { ImproveServerSDK } from '@obelism/improve-sdk/server'\nimport type { NextURL } from 'next/dist/server/web/next-url'\nimport type { NextRequest } from 'next/server'\nimport { NextResponse, userAgent } from 'next/server'\n\nimport { extendedBotCheck } from './extendedBotCheck'\nimport { matchesRoute } from './matchesRoute'\nimport type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies'\n\nconst DEFAULT_COOKIE_TTL = 60 * 60 * 24 * 7\n\nexport type OptionConfig = { value: string; slug: string }\n\nexport type ServerABTestConfig = {\n\tslug: string\n\trouteHandler: string\n\tformatSlug?: (url: NextURL, matchingOption: OptionConfig) => NextURL\n\toptions: OptionConfig[]\n}\n\nexport type GenerateImproveNextMiddlewareArgs = {\n\timproveSdk: ImproveServerSDK\n\tserverABtests: ServerABTestConfig[]\n\toptions?: {\n\t\tvisitorId?: ResponseCookie\n\t\ttestValue?: ResponseCookie\n\t}\n}\n\nexport const generateImproveNextMiddleware = (\n\targs: GenerateImproveNextMiddlewareArgs,\n) => {\n\treturn (request: NextRequest) => {\n\t\ttry {\n\t\t\t// Early esacape for bots that shouldn't be getting AB tested\n\t\t\tconst { ua = '', isBot = false } = userAgent(request)\n\t\t\tif (isBot || extendedBotCheck(ua)) return NextResponse.next()\n\n\t\t\t// When there is no (valid) server AB config escape\n\t\t\tconst serverABTestConfig = args.serverABtests.find((route) =>\n\t\t\t\tmatchesRoute(route.routeHandler, request.nextUrl.pathname),\n\t\t\t)\n\t\t\tif (!serverABTestConfig) return NextResponse.next()\n\n\t\t\tconst basePath = serverABTestConfig.options.at(0)?.value\n\n\t\t\tif (!basePath) return NextResponse.next()\n\n\t\t\tconst visitorCookieName = args.improveSdk.getVisitorCookieName()\n\t\t\tconst cookieVisitorId = request.cookies.get(visitorCookieName)?.value\n\t\t\tconst validCookieVisitorId =\n\t\t\t\tcookieVisitorId && args.improveSdk.validateVisitorId(cookieVisitorId)\n\t\t\tconst visitorId = validCookieVisitorId\n\t\t\t\t? cookieVisitorId\n\t\t\t\t: args.improveSdk.generateVisitorId()\n\n\t\t\tconst validateValue = (value: string | null | undefined) => {\n\t\t\t\treturn value &&\n\t\t\t\t\targs.improveSdk.validateTestValue(serverABTestConfig.slug, value)\n\t\t\t\t\t? value\n\t\t\t\t\t: null\n\t\t\t}\n\n\t\t\t// Coarse geo from the edge, so country-targeted audiences resolve\n\t\t\t// server-side (the browser can't determine country). Vercel sets\n\t\t\t// x-vercel-ip-country; Cloudflare sets cf-ipcountry.\n\t\t\tconst geo = {\n\t\t\t\tcountry:\n\t\t\t\t\trequest.headers.get('x-vercel-ip-country') ??\n\t\t\t\t\trequest.headers.get('cf-ipcountry') ??\n\t\t\t\t\tundefined,\n\t\t\t}\n\n\t\t\t// Get AB test value from: searchParam, cookies or get generate a new value\n\t\t\tconst testValue =\n\t\t\t\tvalidateValue(\n\t\t\t\t\trequest.nextUrl.searchParams.get(serverABTestConfig.slug),\n\t\t\t\t) ||\n\t\t\t\tvalidateValue(request.cookies.get(serverABTestConfig.slug)?.value) ||\n\t\t\t\targs.improveSdk.getTestValue(\n\t\t\t\t\tserverABTestConfig.slug,\n\t\t\t\t\tvisitorId,\n\t\t\t\t\tua,\n\t\t\t\t\tgeo,\n\t\t\t\t)\n\n\t\t\tconst testMatchOption = serverABTestConfig.options.find(\n\t\t\t\t(option) => option.value === testValue,\n\t\t\t)\n\n\t\t\tif (!testMatchOption) return NextResponse.next()\n\n\t\t\t// Update the response with cookies response\n\t\t\tlet response: undefined | NextResponse<unknown>\n\n\t\t\tif (testMatchOption.value !== basePath) {\n\t\t\t\tconst url = request.nextUrl.clone()\n\n\t\t\t\turl.pathname = serverABTestConfig?.formatSlug\n\t\t\t\t\t? serverABTestConfig?.formatSlug(url, testMatchOption)?.pathname\n\t\t\t\t\t: testMatchOption.slug\n\n\t\t\t\tresponse = NextResponse.rewrite(url)\n\t\t\t} else {\n\t\t\t\tresponse = NextResponse.next()\n\t\t\t}\n\n\t\t\tresponse.cookies.set(visitorCookieName, visitorId, {\n\t\t\t\tmaxAge: DEFAULT_COOKIE_TTL,\n\t\t\t\t...args.options?.visitorId,\n\t\t\t})\n\t\t\tresponse.cookies.set(serverABTestConfig.slug, testValue, {\n\t\t\t\tmaxAge: DEFAULT_COOKIE_TTL,\n\t\t\t\t...args.options?.testValue,\n\t\t\t})\n\n\t\t\treturn response\n\t\t} catch (e) {\n\t\t\tconsole.error('[Improve SDK]', e)\n\t\t\treturn NextResponse.next()\n\t\t}\n\t}\n}\n"],"names":["EXTENDED_BOT_REGEX","RegExp","map","name","toLowerCase","join","BASE_REGEX","SECONDARY_REGEX","TERTIARY_REGEX","removeTrailingSlash","val","endsWith","substring","length","regexCache","Map","args","request","response","ua","isBot","userAgent","test","NextResponse","next","serverABTestConfig","serverABtests","find","route","matchesRoute","pathname","basePathname","split","baseRoute","routeRegex","get","replace","set","routeHandler","nextUrl","basePath","options","at","value","visitorCookieName","improveSdk","getVisitorCookieName","cookieVisitorId","cookies","visitorId","validCookieVisitorId","validateVisitorId","generateVisitorId","validateValue","validateTestValue","slug","geo","country","headers","undefined","testValue","searchParams","getTestValue","testMatchOption","option","url","clone","formatSlug","rewrite","maxAge","e","console","error"],"mappings":"oFAAA,IAAMA,EAAqB,AAAIC,OAC9B,CACC,YACA,SACA,OACA,YACA,YACA,YACA,cACA,aACA,cACA,qBACA,WACA,eACA,aACA,WACA,UACA,eACA,SACA,aACA,SACA,aACA,WACA,cACA,CACCC,GAAG,CAAC,AAACC,GAASA,EAAKC,WAAW,IAC9BC,IAAI,CAAC,KACP,KC3BKC,EAAa,wBAEbC,EAAkB,+BAElBC,EAAiB,2BAEVC,EAAsB,AAACC,GACnCA,EAAIC,QAAQ,CAAC,KAAOD,EAAIE,SAAS,CAAC,EAAGF,EAAIG,MAAM,CAAG,GAAKH,EAElDI,EAAa,IAAIC,0CCoBsB,AAC5CC,GAEO,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ClB,GAEO1C,CACR,CAAE,MAAOoD,EAAG,CAEX,OADAC,QAAQC,KAAK,CAAC,gBAAiBF,GACxB/C,EAAAA,aAAaC,IAAI,EACzB,CACD"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{userAgent as e,NextResponse as t}from"next/server";let
|
|
1
|
+
import{userAgent as e,NextResponse as t}from"next/server";let r=RegExp(["Vercelbot","DotBot","Yeti","AhrefsBot","SeznamBot","YandexBot","UptimeRobot","Bytespider","Baiduspider","fidget-spinner-bot","AASA-Bot","FullStoryBot","SemrushBot","PetalBot","RyteBot","Pinterestbot","AntBot","Monsidobot","blowra","JobRoboter","Qwantify","YisouSpider"].map(e=>e.toLowerCase()).join("|"),"i"),o=/(\[[a-zA-Z0-9-]+\])+/g,i=/\[\[\.\.\.[a-zA-Z0-9-]+\]\]/g,a=/\[\.\.\.[a-zA-Z0-9-]+\]/g,s=e=>e.endsWith("/")?e.substring(0,e.length-1):e,n=new Map,l=l=>u=>{try{let p,{ua:g="",isBot:d=!1}=e(u);if(d||g&&r.test(g.toLowerCase()))return t.next();let m=l.serverABtests.find(e=>((e,t)=>{let r=s(t.split("?")[0]),l=s(e.split("?")[0]);if(r===l)return!0;let u=n.get(l);return u||(u=new RegExp(`^${l.replace(o,"[a-zA-Z0-9-]+")}$`.replace(i,"?.*").replace(a,".*")),n.set(l,u)),u.test(r)})(e.routeHandler,u.nextUrl.pathname));if(!m)return t.next();let v=m.options.at(0)?.value;if(!v)return t.next();let c=l.improveSdk.getVisitorCookieName(),x=u.cookies.get(c)?.value,f=x&&l.improveSdk.validateVisitorId(x)?x:l.improveSdk.generateVisitorId(),B=e=>e&&l.improveSdk.validateTestValue(m.slug,e)?e:null,S={country:u.headers.get("x-vercel-ip-country")??u.headers.get("cf-ipcountry")??void 0},A=B(u.nextUrl.searchParams.get(m.slug))||B(u.cookies.get(m.slug)?.value)||l.improveSdk.getTestValue(m.slug,f,g,S),h=m.options.find(e=>e.value===A);if(!h)return t.next();if(h.value!==v){let e=u.nextUrl.clone();e.pathname=m?.formatSlug?m?.formatSlug(e,h)?.pathname:h.slug,p=t.rewrite(e)}else p=t.next();return p.cookies.set(c,f,{maxAge:604800,...l.options?.visitorId}),p.cookies.set(m.slug,A,{maxAge:604800,...l.options?.testValue}),p}catch(e){return console.error("[Improve SDK]",e),t.next()}};export{l as generateImproveNextMiddleware};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/extendedBotCheck.ts","../src/matchesRoute.ts","../src/index.ts"],"sourcesContent":["const EXTENDED_BOT_REGEX = new RegExp(\n\t[\n\t\t'Vercelbot',\n\t\t'DotBot',\n\t\t'Yeti',\n\t\t'AhrefsBot',\n\t\t'SeznamBot',\n\t\t'YandexBot',\n\t\t'UptimeRobot',\n\t\t'Bytespider',\n\t\t'Baiduspider',\n\t\t'fidget-spinner-bot',\n\t\t'AASA-Bot',\n\t\t'FullStoryBot',\n\t\t'SemrushBot',\n\t\t'PetalBot',\n\t\t'RyteBot',\n\t\t'Pinterestbot',\n\t\t'AntBot',\n\t\t'Monsidobot',\n\t\t'blowra',\n\t\t'JobRoboter',\n\t\t'Qwantify',\n\t\t'YisouSpider',\n\t]\n\t\t.map((name) => name.toLowerCase())\n\t\t.join('|'),\n\t'i',\n)\n\nexport const extendedBotCheck = (userAgent: string): boolean => {\n\tif (!userAgent) return false\n\treturn EXTENDED_BOT_REGEX.test(userAgent.toLowerCase())\n}\n","const BASE_REGEX = /(\\[[a-zA-Z0-9-]+\\])+/g\n\nconst SECONDARY_REGEX = /\\[\\[\\.\\.\\.[a-zA-Z0-9-]+\\]\\]/g\n\nconst TERTIARY_REGEX = /\\[\\.\\.\\.[a-zA-Z0-9-]+\\]/g\n\nexport const removeTrailingSlash = (val: string) =>\n\tval.endsWith('/') ? val.substring(0, val.length - 1) : val\n\nconst regexCache = new Map<string, RegExp>()\n\nexport const matchesRoute = (route: string, pathname: string) => {\n\tconst basePathname = removeTrailingSlash(pathname.split('?')[0] as string)\n\tconst baseRoute = removeTrailingSlash(route.split('?')[0] as string)\n\n\tif (basePathname === baseRoute) return true\n\n\tlet routeRegex = regexCache.get(baseRoute)\n\tif (!routeRegex) {\n\t\trouteRegex = new RegExp(\n\t\t\t`^${baseRoute.replace(BASE_REGEX, '[a-zA-Z0-9-]+')}$`\n\t\t\t\t.replace(SECONDARY_REGEX, '?.*')\n\t\t\t\t.replace(TERTIARY_REGEX, '.*'),\n\t\t)\n\t\tregexCache.set(baseRoute, routeRegex)\n\t}\n\n\treturn routeRegex.test(basePathname)\n}\n","import { ImproveServerSDK } from '@obelism/improve-sdk/server'\nimport type { NextURL } from 'next/dist/server/web/next-url'\nimport type { NextRequest } from 'next/server'\nimport { NextResponse, userAgent } from 'next/server'\n\nimport { extendedBotCheck } from './extendedBotCheck'\nimport { matchesRoute } from './matchesRoute'\nimport type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies'\n\nconst DEFAULT_COOKIE_TTL = 60 * 60 * 24 * 7\n\nexport type OptionConfig = { value: string; slug: string }\n\nexport type ServerABTestConfig = {\n\tslug: string\n\trouteHandler: string\n\tformatSlug?: (url: NextURL, matchingOption: OptionConfig) => NextURL\n\toptions: OptionConfig[]\n}\n\nexport type GenerateImproveNextMiddlewareArgs = {\n\timproveSdk: ImproveServerSDK\n\tserverABtests: ServerABTestConfig[]\n\toptions?: {\n\t\tvisitorId?: ResponseCookie\n\t\ttestValue?: ResponseCookie\n\t}\n}\n\nexport const generateImproveNextMiddleware = (\n\targs: GenerateImproveNextMiddlewareArgs,\n) => {\n\treturn (request: NextRequest) => {\n\t\ttry {\n\t\t\t// Early esacape for bots that shouldn't be getting AB tested\n\t\t\tconst { ua = '', isBot = false } = userAgent(request)\n\t\t\tif (isBot || extendedBotCheck(ua)) return NextResponse.next()\n\n\t\t\t// When there is no (valid) server AB config escape\n\t\t\tconst serverABTestConfig = args.serverABtests.find((route) =>\n\t\t\t\tmatchesRoute(route.routeHandler, request.nextUrl.pathname),\n\t\t\t)\n\t\t\tif (!serverABTestConfig) return NextResponse.next()\n\n\t\t\tconst basePath = serverABTestConfig.options.at(0)?.value\n\n\t\t\tif (!basePath) return NextResponse.next()\n\n\t\t\tconst visitorCookieName = args.improveSdk.getVisitorCookieName()\n\t\t\tconst cookieVisitorId = request.cookies.get(visitorCookieName)?.value\n\t\t\tconst validCookieVisitorId =\n\t\t\t\tcookieVisitorId
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/extendedBotCheck.ts","../src/matchesRoute.ts","../src/index.ts"],"sourcesContent":["const EXTENDED_BOT_REGEX = new RegExp(\n\t[\n\t\t'Vercelbot',\n\t\t'DotBot',\n\t\t'Yeti',\n\t\t'AhrefsBot',\n\t\t'SeznamBot',\n\t\t'YandexBot',\n\t\t'UptimeRobot',\n\t\t'Bytespider',\n\t\t'Baiduspider',\n\t\t'fidget-spinner-bot',\n\t\t'AASA-Bot',\n\t\t'FullStoryBot',\n\t\t'SemrushBot',\n\t\t'PetalBot',\n\t\t'RyteBot',\n\t\t'Pinterestbot',\n\t\t'AntBot',\n\t\t'Monsidobot',\n\t\t'blowra',\n\t\t'JobRoboter',\n\t\t'Qwantify',\n\t\t'YisouSpider',\n\t]\n\t\t.map((name) => name.toLowerCase())\n\t\t.join('|'),\n\t'i',\n)\n\nexport const extendedBotCheck = (userAgent: string): boolean => {\n\tif (!userAgent) return false\n\treturn EXTENDED_BOT_REGEX.test(userAgent.toLowerCase())\n}\n","const BASE_REGEX = /(\\[[a-zA-Z0-9-]+\\])+/g\n\nconst SECONDARY_REGEX = /\\[\\[\\.\\.\\.[a-zA-Z0-9-]+\\]\\]/g\n\nconst TERTIARY_REGEX = /\\[\\.\\.\\.[a-zA-Z0-9-]+\\]/g\n\nexport const removeTrailingSlash = (val: string) =>\n\tval.endsWith('/') ? val.substring(0, val.length - 1) : val\n\nconst regexCache = new Map<string, RegExp>()\n\nexport const matchesRoute = (route: string, pathname: string) => {\n\tconst basePathname = removeTrailingSlash(pathname.split('?')[0] as string)\n\tconst baseRoute = removeTrailingSlash(route.split('?')[0] as string)\n\n\tif (basePathname === baseRoute) return true\n\n\tlet routeRegex = regexCache.get(baseRoute)\n\tif (!routeRegex) {\n\t\trouteRegex = new RegExp(\n\t\t\t`^${baseRoute.replace(BASE_REGEX, '[a-zA-Z0-9-]+')}$`\n\t\t\t\t.replace(SECONDARY_REGEX, '?.*')\n\t\t\t\t.replace(TERTIARY_REGEX, '.*'),\n\t\t)\n\t\tregexCache.set(baseRoute, routeRegex)\n\t}\n\n\treturn routeRegex.test(basePathname)\n}\n","import { ImproveServerSDK } from '@obelism/improve-sdk/server'\nimport type { NextURL } from 'next/dist/server/web/next-url'\nimport type { NextRequest } from 'next/server'\nimport { NextResponse, userAgent } from 'next/server'\n\nimport { extendedBotCheck } from './extendedBotCheck'\nimport { matchesRoute } from './matchesRoute'\nimport type { ResponseCookie } from 'next/dist/compiled/@edge-runtime/cookies'\n\nconst DEFAULT_COOKIE_TTL = 60 * 60 * 24 * 7\n\nexport type OptionConfig = { value: string; slug: string }\n\nexport type ServerABTestConfig = {\n\tslug: string\n\trouteHandler: string\n\tformatSlug?: (url: NextURL, matchingOption: OptionConfig) => NextURL\n\toptions: OptionConfig[]\n}\n\nexport type GenerateImproveNextMiddlewareArgs = {\n\timproveSdk: ImproveServerSDK\n\tserverABtests: ServerABTestConfig[]\n\toptions?: {\n\t\tvisitorId?: ResponseCookie\n\t\ttestValue?: ResponseCookie\n\t}\n}\n\nexport const generateImproveNextMiddleware = (\n\targs: GenerateImproveNextMiddlewareArgs,\n) => {\n\treturn (request: NextRequest) => {\n\t\ttry {\n\t\t\t// Early esacape for bots that shouldn't be getting AB tested\n\t\t\tconst { ua = '', isBot = false } = userAgent(request)\n\t\t\tif (isBot || extendedBotCheck(ua)) return NextResponse.next()\n\n\t\t\t// When there is no (valid) server AB config escape\n\t\t\tconst serverABTestConfig = args.serverABtests.find((route) =>\n\t\t\t\tmatchesRoute(route.routeHandler, request.nextUrl.pathname),\n\t\t\t)\n\t\t\tif (!serverABTestConfig) return NextResponse.next()\n\n\t\t\tconst basePath = serverABTestConfig.options.at(0)?.value\n\n\t\t\tif (!basePath) return NextResponse.next()\n\n\t\t\tconst visitorCookieName = args.improveSdk.getVisitorCookieName()\n\t\t\tconst cookieVisitorId = request.cookies.get(visitorCookieName)?.value\n\t\t\tconst validCookieVisitorId =\n\t\t\t\tcookieVisitorId && args.improveSdk.validateVisitorId(cookieVisitorId)\n\t\t\tconst visitorId = validCookieVisitorId\n\t\t\t\t? cookieVisitorId\n\t\t\t\t: args.improveSdk.generateVisitorId()\n\n\t\t\tconst validateValue = (value: string | null | undefined) => {\n\t\t\t\treturn value &&\n\t\t\t\t\targs.improveSdk.validateTestValue(serverABTestConfig.slug, value)\n\t\t\t\t\t? value\n\t\t\t\t\t: null\n\t\t\t}\n\n\t\t\t// Coarse geo from the edge, so country-targeted audiences resolve\n\t\t\t// server-side (the browser can't determine country). Vercel sets\n\t\t\t// x-vercel-ip-country; Cloudflare sets cf-ipcountry.\n\t\t\tconst geo = {\n\t\t\t\tcountry:\n\t\t\t\t\trequest.headers.get('x-vercel-ip-country') ??\n\t\t\t\t\trequest.headers.get('cf-ipcountry') ??\n\t\t\t\t\tundefined,\n\t\t\t}\n\n\t\t\t// Get AB test value from: searchParam, cookies or get generate a new value\n\t\t\tconst testValue =\n\t\t\t\tvalidateValue(\n\t\t\t\t\trequest.nextUrl.searchParams.get(serverABTestConfig.slug),\n\t\t\t\t) ||\n\t\t\t\tvalidateValue(request.cookies.get(serverABTestConfig.slug)?.value) ||\n\t\t\t\targs.improveSdk.getTestValue(\n\t\t\t\t\tserverABTestConfig.slug,\n\t\t\t\t\tvisitorId,\n\t\t\t\t\tua,\n\t\t\t\t\tgeo,\n\t\t\t\t)\n\n\t\t\tconst testMatchOption = serverABTestConfig.options.find(\n\t\t\t\t(option) => option.value === testValue,\n\t\t\t)\n\n\t\t\tif (!testMatchOption) return NextResponse.next()\n\n\t\t\t// Update the response with cookies response\n\t\t\tlet response: undefined | NextResponse<unknown>\n\n\t\t\tif (testMatchOption.value !== basePath) {\n\t\t\t\tconst url = request.nextUrl.clone()\n\n\t\t\t\turl.pathname = serverABTestConfig?.formatSlug\n\t\t\t\t\t? serverABTestConfig?.formatSlug(url, testMatchOption)?.pathname\n\t\t\t\t\t: testMatchOption.slug\n\n\t\t\t\tresponse = NextResponse.rewrite(url)\n\t\t\t} else {\n\t\t\t\tresponse = NextResponse.next()\n\t\t\t}\n\n\t\t\tresponse.cookies.set(visitorCookieName, visitorId, {\n\t\t\t\tmaxAge: DEFAULT_COOKIE_TTL,\n\t\t\t\t...args.options?.visitorId,\n\t\t\t})\n\t\t\tresponse.cookies.set(serverABTestConfig.slug, testValue, {\n\t\t\t\tmaxAge: DEFAULT_COOKIE_TTL,\n\t\t\t\t...args.options?.testValue,\n\t\t\t})\n\n\t\t\treturn response\n\t\t} catch (e) {\n\t\t\tconsole.error('[Improve SDK]', e)\n\t\t\treturn NextResponse.next()\n\t\t}\n\t}\n}\n"],"names":["EXTENDED_BOT_REGEX","RegExp","map","name","toLowerCase","join","BASE_REGEX","SECONDARY_REGEX","TERTIARY_REGEX","removeTrailingSlash","val","endsWith","substring","length","regexCache","Map","generateImproveNextMiddleware","args","request","response","ua","isBot","userAgent","test","NextResponse","next","serverABTestConfig","serverABtests","find","route","matchesRoute","pathname","basePathname","split","baseRoute","routeRegex","get","replace","set","routeHandler","nextUrl","basePath","options","at","value","visitorCookieName","improveSdk","getVisitorCookieName","cookieVisitorId","cookies","visitorId","validCookieVisitorId","validateVisitorId","generateVisitorId","validateValue","validateTestValue","slug","geo","country","headers","undefined","testValue","searchParams","getTestValue","testMatchOption","option","url","clone","formatSlug","rewrite","maxAge","e","console","error"],"mappings":"0DAAA,IAAMA,EAAqB,AAAIC,OAC9B,CACC,YACA,SACA,OACA,YACA,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AaC,IAAI,GAY7B,OATAN,EAAS8B,OAAO,CAACX,GAAG,CAACO,EAAmBK,EAAW,CAClDoB,OAnGuB,OAoGvB,GAAGrD,EAAKyB,OAAO,EAAEQ,SAAAA,AAClB,GACA/B,EAAS8B,OAAO,CAACX,GAAG,CAACZ,EAAmB8B,IAAI,CAAEK,EAAW,CACxDS,OAvGuB,OAwGvB,GAAGrD,EAAKyB,OAAO,EAAEmB,SAAAA,AAClB,GAEO1C,CACR,CAAE,MAAOoD,EAAG,CAEX,OADAC,QAAQC,KAAK,CAAC,gBAAiBF,GACxB/C,EAAaC,IAAI,EACzB,CACD"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@obelism/improve-sdk-next",
|
|
3
3
|
"description": "Obelism Improve NextJS SDK",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ab-tests",
|
|
7
7
|
"feature-flags",
|
|
@@ -56,6 +56,6 @@
|
|
|
56
56
|
"vitest": "^4.1.9"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@obelism/improve-sdk": "^1.0
|
|
59
|
+
"@obelism/improve-sdk": "^2.1.0"
|
|
60
60
|
}
|
|
61
61
|
}
|