insite-cookie 1.0.46 → 2.0.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/bun.lockb CHANGED
Binary file
@@ -0,0 +1,18 @@
1
+ import { type InSiteWebSocket } from "insite-ws/client";
2
+ import { type Method } from "../common";
3
+ type Options = {
4
+ method?: Method;
5
+ url?: string;
6
+ onload?: (this: XMLHttpRequest, event: ProgressEvent) => unknown;
7
+ onerror?: (this: XMLHttpRequest, event: ProgressEvent) => unknown;
8
+ };
9
+ export declare class CookieSetter {
10
+ #private;
11
+ constructor(ws: InSiteWebSocket, options?: Options);
12
+ readonly method: Method;
13
+ readonly url: string;
14
+ static onXhrLoad(this: XMLHttpRequest): void;
15
+ static onXhrError(this: XMLHttpRequest): void;
16
+ }
17
+ export {};
18
+ //# sourceMappingURL=Setter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Setter.d.ts","sourceRoot":"","sources":["../../src/client/Setter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAKjD,KAAK,OAAO,GAAG;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,aAAa,KAAK,OAAO,CAAC;IACjE,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,aAAa,KAAK,OAAO,CAAC;CAClE,CAAC;AAGF,qBAAa,YAAY;;gBACZ,EAAE,EAAE,eAAe,EAAE,OAAO,GAAE,OAAY;IAkBtD,QAAQ,CAAC,MAAM,SAAC;IAChB,QAAQ,CAAC,GAAG,SAAC;IAkBb,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,cAAc;IAOrC,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,cAAc;CAMtC"}
@@ -0,0 +1,2 @@
1
+ export * from "./Setter.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ var x=Object.defineProperty;var l=o=>{throw TypeError(o)};var y=(o,t,e)=>t in o?x(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var p=(o,t,e)=>y(o,typeof t!="symbol"?t+"":t,e),c=(o,t,e)=>t.has(o)||l("Cannot "+e);var h=(o,t,e)=>(c(o,t,"read from private field"),e?e.call(o):t.get(o)),i=(o,t,e)=>t.has(o)?l("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(o):t.set(o,e),u=(o,t,e,n)=>(c(o,t,"write to private field"),n?n.call(o,e):t.set(o,e),e);var m={set:"~setcookie"};var r,s,d,a=class a{constructor(t,e={}){p(this,"method");p(this,"url");i(this,r);i(this,s);i(this,d,t=>{let e=new XMLHttpRequest;e.onload=h(this,r),e.onerror=h(this,s),e.open(this.method,`${this.url}?${t}`,!0),e.send(null)});let{method:n="GET",url:M="/cookie",onload:S=a.onXhrLoad,onerror:k=a.onXhrError}=e;this.method=n,this.url=M,u(this,r,S),u(this,s,k),t.on(`message:${m.set}`,h(this,d))}static onXhrLoad(){this.readyState===4&&this.status!==200&&console.error("Set cookie:",this.statusText)}static onXhrError(){console.error("Set cookie:",this.statusText)}};r=new WeakMap,s=new WeakMap,d=new WeakMap;var X=a;export{X as CookieSetter};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/common.ts", "../../src/client/Setter.ts"],
4
+ "sourcesContent": ["export const headers = {\n\tset: \"~setcookie\"\n};\n\nexport type Method = \"GET\" | \"POST\";\n", "import { type InSiteWebSocket } from \"insite-ws/client\";\nimport { headers, type Method } from \"../common\";\n\n\n/* eslint-disable unicorn/prefer-add-event-listener */\n\ntype Options = {\n\tmethod?: Method;\n\turl?: string;\n\tonload?: (this: XMLHttpRequest, event: ProgressEvent) => unknown;\n\tonerror?: (this: XMLHttpRequest, event: ProgressEvent) => unknown;\n};\n\n\nexport class CookieSetter {\n\tconstructor(ws: InSiteWebSocket, options: Options = {}) {\n\t\t\n\t\tconst {\n\t\t\tmethod = \"GET\",\n\t\t\turl = \"/cookie\",\n\t\t\tonload = CookieSetter.onXhrLoad,\n\t\t\tonerror = CookieSetter.onXhrError\n\t\t} = options;\n\t\t\n\t\tthis.method = method;\n\t\tthis.url = url;\n\t\tthis.#onload = onload;\n\t\tthis.#onerror = onerror;\n\t\t\n\t\tws.on(`message:${headers.set}`, this.#handleSet);\n\t\t\n\t}\n\t\n\treadonly method;\n\treadonly url;\n\t#onload;\n\t#onerror;\n\t\n\t#handleSet = (token: string) => {\n\t\t\n\t\tconst xhr = new XMLHttpRequest();\n\t\t\n\t\txhr.onload = this.#onload;\n\t\txhr.onerror = this.#onerror;\n\t\t\n\t\txhr.open(this.method, `${this.url}?${token}`, true);\n\t\t\n\t\txhr.send(null);\n\t\t\n\t};\n\t\n\t\n\tstatic onXhrLoad(this: XMLHttpRequest) {\n\t\t\n\t\tif (this.readyState === 4 && this.status !== 200)\n\t\t\tconsole.error(\"Set cookie:\", this.statusText);\n\t\t\n\t}\n\t\n\tstatic onXhrError(this: XMLHttpRequest) {\n\t\t\n\t\tconsole.error(\"Set cookie:\", this.statusText);\n\t\t\n\t}\n\t\n}\n"],
5
+ "mappings": "2eAAO,IAAMA,EAAU,CACtB,IAAK,YACN,ECFA,IAAAC,EAAAC,EAAAC,EAcaC,EAAN,MAAMA,CAAa,CACzB,YAAYC,EAAqBC,EAAmB,CAAC,EAAG,CAkBxDC,EAAA,KAAS,UACTA,EAAA,KAAS,OACTC,EAAA,KAAAP,GACAO,EAAA,KAAAN,GAEAM,EAAA,KAAAL,EAAcM,GAAkB,CAE/B,IAAMC,EAAM,IAAI,eAEhBA,EAAI,OAASC,EAAA,KAAKV,GAClBS,EAAI,QAAUC,EAAA,KAAKT,GAEnBQ,EAAI,KAAK,KAAK,OAAQ,GAAG,KAAK,GAAG,IAAID,CAAK,GAAI,EAAI,EAElDC,EAAI,KAAK,IAAI,CAEd,GAhCC,GAAM,CACL,OAAAE,EAAS,MACT,IAAAC,EAAM,UACN,OAAAC,EAASV,EAAa,UACtB,QAAAW,EAAUX,EAAa,UACxB,EAAIE,EAEJ,KAAK,OAASM,EACd,KAAK,IAAMC,EACXG,EAAA,KAAKf,EAAUa,GACfE,EAAA,KAAKd,EAAWa,GAEhBV,EAAG,GAAG,WAAWY,EAAQ,GAAG,GAAIN,EAAA,KAAKR,EAAU,CAEhD,CAqBA,OAAO,WAAgC,CAElC,KAAK,aAAe,GAAK,KAAK,SAAW,KAC5C,QAAQ,MAAM,cAAe,KAAK,UAAU,CAE9C,CAEA,OAAO,YAAiC,CAEvC,QAAQ,MAAM,cAAe,KAAK,UAAU,CAE7C,CAED,EA9BCF,EAAA,YACAC,EAAA,YAEAC,EAAA,YAxBM,IAAMe,EAANd",
6
+ "names": ["headers", "_onload", "_onerror", "_handleSet", "_CookieSetter", "ws", "options", "__publicField", "__privateAdd", "token", "xhr", "__privateGet", "method", "url", "onload", "onerror", "__privateSet", "headers", "CookieSetter"]
7
+ }
@@ -0,0 +1,5 @@
1
+ export declare const headers: {
2
+ set: string;
3
+ };
4
+ export type Method = "GET" | "POST";
5
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO;;CAEnB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG,KAAK,GAAG,MAAM,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { InSiteServerMiddleware } from "insite-http";
2
+ import type { Cookie, CookieOptions } from "./types";
3
+ type Options = {
4
+ requestRegExp?: RegExp;
5
+ };
6
+ export type { Options as CookieMiddlewareOptions };
7
+ type MakeArg = {
8
+ name: keyof Cookie;
9
+ value: Cookie[keyof Cookie];
10
+ } & CookieOptions;
11
+ export declare class InSiteCookieMiddleware extends InSiteServerMiddleware {
12
+ #private;
13
+ constructor(options?: Options);
14
+ static make({ name, value, maxAge, expiresAt, domain, path, secure, httpOnly, sameSite }: MakeArg): string;
15
+ static cookify(cookie: Cookie, options: CookieOptions): string[];
16
+ }
17
+ //# sourceMappingURL=Middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Middleware.d.ts","sourceRoot":"","sources":["../../src/server/Middleware.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGrD,KAAK,OAAO,GAAG;IACd,aAAa,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,YAAY,EAAE,OAAO,IAAI,uBAAuB,EAAE,CAAC;AAEnD,KAAK,OAAO,GAAG;IACd,IAAI,EAAE,MAAM,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC,MAAM,MAAM,CAAC,CAAC;CAC5B,GAAG,aAAa,CAAC;AAGlB,qBAAa,sBAAuB,SAAQ,sBAAsB;;gBACrD,OAAO,GAAE,OAAY;IA2BjC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAU,EAAE,MAAa,EAAE,QAAe,EAAE,QAAmB,EAAE,EAAE,OAAO;IA0BhI,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa;CAIrD"}
@@ -0,0 +1,22 @@
1
+ import type { WithUser } from "insite-subscriptions-server/ws";
2
+ import type { InSiteWebSocketServerClient } from "insite-ws/server";
3
+ import type { AbilitiesSchema } from "insite-users-server";
4
+ import type { UsersServer } from "insite-users-server-ws";
5
+ import { parseCookie } from "./parse";
6
+ import type { Cookie, CookieOptions } from "./types";
7
+ export type Options<AS extends AbilitiesSchema> = {
8
+ usersServer: UsersServer<AS>;
9
+ domain?: string;
10
+ maxAge?: number;
11
+ };
12
+ export declare class CookieSetter<AS extends AbilitiesSchema> {
13
+ #private;
14
+ constructor({ usersServer, domain, maxAge }: Options<AS>);
15
+ usersServer: UsersServer<AS>;
16
+ domain: string | undefined;
17
+ maxAge: number;
18
+ set(wssc: WithUser<InSiteWebSocketServerClient, AS>, cookie: Cookie, options: CookieOptions): void;
19
+ unset(wssc: WithUser<InSiteWebSocketServerClient, AS>, names: string[]): void;
20
+ static parse: typeof parseCookie;
21
+ }
22
+ //# sourceMappingURL=Setter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Setter.d.ts","sourceRoot":"","sources":["../../src/server/Setter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAC/D,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,kBAAkB,CAAC;AAEpE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAKrD,MAAM,MAAM,OAAO,CAAC,EAAE,SAAS,eAAe,IAAI;IACjD,WAAW,EAAE,WAAW,CAAC,EAAE,CAAC,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAGF,qBAAa,YAAY,CAAC,EAAE,SAAS,eAAe;;gBACvC,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,CAAC;IAYxD,WAAW,kBAAC;IACZ,MAAM,qBAAC;IACP,MAAM,SAAC;IAEP,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,2BAA2B,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa;IAU3F,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,2BAA2B,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE;IAoCtE,MAAM,CAAC,KAAK,qBAAe;CAE3B"}
@@ -0,0 +1,4 @@
1
+ export * from "./Middleware.js";
2
+ export * from "./parse.js";
3
+ export * from "./Setter.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ import{getWithTimeout as f}from"@nesvet/n";import{InSiteServerMiddleware as d}from"insite-http";var n=new Map;var S=class r extends d{constructor(e={}){super();let{requestRegExp:s=/^\/cookie\/?\?/}=e;this.listeners={GET:[[s,this.#e]]}}#e=(e,s)=>{let[,t]=e.url.split("?");return t&&n.has(t)?s.writeHead(200,{"Content-Type":"text/plain; charset=utf-8","Set-Cookie":r.cookify(...f(n,t))}).end():!1};static make({name:e,value:s,maxAge:t,expiresAt:o,domain:a,path:m="/",secure:k=!0,httpOnly:l=!0,sameSite:u="Strict"}){let i=[`${e}=${s}`];return t!==void 0?i.push(`Max-Age=${t}`):o&&i.push(`Max-Age=${Math.round((o.getTime()-Date.now())/1e3)}`),a&&i.push(`Domain=${a}`),m&&i.push(`Path=${m}`),k&&i.push("Secure"),l&&i.push("HttpOnly"),u&&i.push(`SameSite=${u}`),i.join("; ")}static cookify(e,s){return Object.entries(e).map(([t,o])=>this.make({name:t,value:o,...s}))}};function p(r){return r?r.split(/;\s*/).reduce((e,s)=>{let[t,o]=s.split(/=/);try{e[t]=JSON.parse(o)}catch{e[t]=o}return e},{}):{}}import g from"node:crypto";import{random as C,setWithTimeout as y}from"@nesvet/n";var h={set:"~setcookie"};var x=2e3,c=class{constructor({usersServer:e,domain:s,maxAge:t}){this.usersServer=e,this.domain=s??process.env.INSITE_HOST,this.maxAge=t??this.usersServer.users.sessions.collection.expireAfterSeconds,this.usersServer.wss.on("client-connect",this.#t),this.usersServer.wss.on("client-session",this.#s)}#e=new WeakMap;usersServer;domain;maxAge;set(e,s,t){let o=`${Math.round(performance.now()*Number.MAX_SAFE_INTEGER).toString(36)}$${g.randomBytes(C(4,8)).toString("hex")}`;y(n,o,[s,t],x),e.sendMessage(h.set,o)}unset(e,s){this.set(e,s.reduce((t,o)=>(t[o]="",t),{}),{maxAge:0})}#t=(e,s)=>{if(this.#e.set(e,null),s.headers.cookie){let{sessionId:t=null}=p(s.headers.cookie);this.#e.set(e,t),t&&this.usersServer.setSession(e,t,!0)}};#s=e=>{this.#e.get(e)!==e.session?._id&&(this.#e.set(e,e.session?._id??null),e.isOpen&&e.session?this.set(e,{sessionId:e.session._id},{domain:this.domain,maxAge:this.maxAge}):this.unset(e,["sessionId"]))};static parse=p};export{c as CookieSetter,S as InSiteCookieMiddleware,p as parseCookie};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/server/Middleware.ts", "../../src/server/tokenMap.ts", "../../src/server/parse.ts", "../../src/server/Setter.ts", "../../src/common.ts"],
4
+ "sourcesContent": ["import type { IncomingMessage, ServerResponse } from \"node:http\";\nimport { getWithTimeout } from \"@nesvet/n\";\nimport { InSiteServerMiddleware } from \"insite-http\";\nimport { tokenMap } from \"./tokenMap\";\nimport type { Cookie, CookieOptions } from \"./types\";\n\n\ntype Options = {\n\trequestRegExp?: RegExp;\n};\n\nexport type { Options as CookieMiddlewareOptions };\n\ntype MakeArg = {\n\tname: keyof Cookie;\n\tvalue: Cookie[keyof Cookie];\n} & CookieOptions;\n\n\nexport class InSiteCookieMiddleware extends InSiteServerMiddleware {\n\tconstructor(options: Options = {}) {\n\t\tsuper();\n\t\t\n\t\tconst {\n\t\t\trequestRegExp = /^\\/cookie\\/?\\?/\n\t\t} = options;\n\t\t\n\t\tthis.listeners = { GET: [\n\t\t\t[ requestRegExp, this.#handler ]\n\t\t] };\n\t\t\n\t}\n\t\n\t#handler = (request: IncomingMessage, response: ServerResponse) => {\n\t\tconst [ , token ] = request.url!.split(\"?\");\n\t\t\n\t\tif (token && tokenMap.has(token))\n\t\t\treturn response.writeHead(200, {\n\t\t\t\t\"Content-Type\": \"text/plain; charset=utf-8\",\n\t\t\t\t\"Set-Cookie\": InSiteCookieMiddleware.cookify(...getWithTimeout(tokenMap, token)!)\n\t\t\t}).end();\n\t\t\n\t\t\n\t\treturn false;\n\t};\n\t\n\t\n\tstatic make({ name, value, maxAge, expiresAt, domain, path = \"/\", secure = true, httpOnly = true, sameSite = \"Strict\" }: MakeArg) {\n\t\tconst cookie = [ `${name}=${value}` ];\n\t\t\n\t\tif (maxAge !== undefined)\n\t\t\tcookie.push(`Max-Age=${maxAge}`);\n\t\telse if (expiresAt)\n\t\t\tcookie.push(`Max-Age=${Math.round((expiresAt.getTime() - Date.now()) / 1000)}`);\n\t\t\n\t\tif (domain)\n\t\t\tcookie.push(`Domain=${domain}`);\n\t\t\n\t\tif (path)\n\t\t\tcookie.push(`Path=${path}`);\n\t\t\n\t\tif (secure)\n\t\t\tcookie.push(\"Secure\");\n\t\t\n\t\tif (httpOnly)\n\t\t\tcookie.push(\"HttpOnly\");\n\t\t\n\t\tif (sameSite)\n\t\t\tcookie.push(`SameSite=${sameSite}`);\n\t\t\n\t\treturn cookie.join(\"; \");\n\t}\n\t\n\tstatic cookify(cookie: Cookie, options: CookieOptions) {\n\t\treturn Object.entries(cookie).map(([ name, value ]) => this.make({ name, value, ...options }));\n\t}\n\t\n}\n", "import type { Cookie, CookieOptions } from \"./types\";\n\n\nexport const tokenMap = new Map<string, [ cookie: Cookie, options: CookieOptions ]>();\n", "export function parseCookie(cookie: string) {\n\treturn cookie ? cookie.split(/;\\s*/).reduce((object, keyEqValue) => {\n\t\tconst [ key, value ] = keyEqValue.split(/=/);\n\t\t\n\t\ttry {\n\t\t\tobject[key] = JSON.parse(value);\n\t\t} catch {\n\t\t\tobject[key] = value;\n\t\t}\n\t\t\n\t\treturn object;\n\t}, {} as Record<string, unknown>) : {};\n}\n", "import crypto from \"node:crypto\";\nimport type { IncomingMessage } from \"node:http\";\nimport type { WithUser } from \"insite-subscriptions-server/ws\";\nimport type { InSiteWebSocketServerClient } from \"insite-ws/server\";\nimport { random, setWithTimeout } from \"@nesvet/n\";\nimport type { AbilitiesSchema } from \"insite-users-server\";\nimport type { UsersServer } from \"insite-users-server-ws\";\nimport { headers } from \"../common\";\nimport { parseCookie } from \"./parse\";\nimport { tokenMap } from \"./tokenMap\";\nimport type { Cookie, CookieOptions } from \"./types\";\n\n\nconst cookieTokenTimeout = 2000;\n\nexport type Options<AS extends AbilitiesSchema> = {\n\tusersServer: UsersServer<AS>;\n\tdomain?: string;\n\tmaxAge?: number;\n};\n\n\nexport class CookieSetter<AS extends AbilitiesSchema> {\n\tconstructor({ usersServer, domain, maxAge }: Options<AS>) {\n\t\tthis.usersServer = usersServer;\n\t\tthis.domain = domain ?? process.env.INSITE_HOST;\n\t\tthis.maxAge = maxAge ?? this.usersServer.users.sessions.collection.expireAfterSeconds;\n\t\t\n\t\tthis.usersServer.wss.on(\"client-connect\", this.#handleClientConnect);\n\t\tthis.usersServer.wss.on(\"client-session\", this.#handleClientSession);\n\t\t\n\t}\n\t\n\t#wsSessionIdMap = new WeakMap<WithUser<InSiteWebSocketServerClient, AS>, null | string>();\n\t\n\tusersServer;\n\tdomain;\n\tmaxAge;\n\t\n\tset(wssc: WithUser<InSiteWebSocketServerClient, AS>, cookie: Cookie, options: CookieOptions) {\n\t\t\n\t\tconst token = `${Math.round(performance.now() * Number.MAX_SAFE_INTEGER).toString(36)}$${crypto.randomBytes(random(4, 8)).toString(\"hex\")}`;\n\t\t\n\t\tsetWithTimeout(tokenMap, token, [ cookie, options ], cookieTokenTimeout);\n\t\t\n\t\twssc.sendMessage(headers.set, token);\n\t\t\n\t}\n\t\n\tunset(wssc: WithUser<InSiteWebSocketServerClient, AS>, names: string[]) {\n\t\tthis.set(wssc, names.reduce((cookie, name) => {\n\t\t\tcookie[name] = \"\";\n\t\t\t\n\t\t\treturn cookie;\n\t\t}, {} as Cookie), { maxAge: 0 });\n\t\t\n\t}\n\t\n\t#handleClientConnect = (wssc: WithUser<InSiteWebSocketServerClient, AS>, request: IncomingMessage) => {\n\t\tthis.#wsSessionIdMap.set(wssc, null);\n\t\t\n\t\tif (request.headers.cookie) {\n\t\t\tconst { sessionId = null } = parseCookie(request.headers.cookie) as { sessionId: null | string };\n\t\t\t\n\t\t\tthis.#wsSessionIdMap.set(wssc, sessionId);\n\t\t\t\n\t\t\tif (sessionId)\n\t\t\t\tthis.usersServer.setSession(wssc, sessionId, true);\n\t\t}\n\t\t\n\t};\n\t\n\t#handleClientSession = (wssc: WithUser<InSiteWebSocketServerClient, AS>) => {\n\t\tif (this.#wsSessionIdMap.get(wssc) !== wssc.session?._id) {\n\t\t\tthis.#wsSessionIdMap.set(wssc, wssc.session?._id ?? null);\n\t\t\t\n\t\t\tif (wssc.isOpen && wssc.session)\n\t\t\t\tthis.set(wssc, { sessionId: wssc.session._id }, { domain: this.domain, maxAge: this.maxAge });\n\t\t\telse\n\t\t\t\tthis.unset(wssc, [ \"sessionId\" ]);\n\t\t}\n\t\t\n\t};\n\t\n\t\n\tstatic parse = parseCookie;\n\t\n}\n", "export const headers = {\n\tset: \"~setcookie\"\n};\n\nexport type Method = \"GET\" | \"POST\";\n"],
5
+ "mappings": "AACA,OAAS,kBAAAA,MAAsB,YAC/B,OAAS,0BAAAC,MAA8B,c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AiB,CACvE,KAAK,IAAIT,EAAMS,EAAM,OAAO,CAACR,EAAQS,KACpCT,EAAOS,CAAI,EAAI,GAERT,GACL,CAAC,CAAW,EAAG,CAAE,OAAQ,CAAE,CAAC,CAEhC,CAEAJ,GAAuB,CAACG,EAAiDW,IAA6B,CAGrG,GAFA,KAAKZ,GAAgB,IAAIC,EAAM,IAAI,EAE/BW,EAAQ,QAAQ,OAAQ,CAC3B,GAAM,CAAE,UAAAC,EAAY,IAAK,EAAIC,EAAYF,EAAQ,QAAQ,MAAM,EAE/D,KAAKZ,GAAgB,IAAIC,EAAMY,CAAS,EAEpCA,GACH,KAAK,YAAY,WAAWZ,EAAMY,EAAW,EAAI,CACnD,CAED,EAEAd,GAAwBE,GAAoD,CACvE,KAAKD,GAAgB,IAAIC,CAAI,IAAMA,EAAK,SAAS,MACpD,KAAKD,GAAgB,IAAIC,EAAMA,EAAK,SAAS,KAAO,IAAI,EAEpDA,EAAK,QAAUA,EAAK,QACvB,KAAK,IAAIA,EAAM,CAAE,UAAWA,EAAK,QAAQ,GAAI,EAAG,CAAE,OAAQ,KAAK,OAAQ,OAAQ,KAAK,MAAO,CAAC,EAE5F,KAAK,MAAMA,EAAM,CAAE,WAAY,CAAC,EAGnC,EAGA,OAAO,MAAQa,CAEhB",
6
+ "names": ["getWithTimeout", "InSiteServerMiddleware", "tokenMap", "InSiteCookieMiddleware", "_InSiteCookieMiddleware", "InSiteServerMiddleware", "options", "requestRegExp", "#handler", "request", "response", "token", "tokenMap", "getWithTimeout", "name", "value", "maxAge", "expiresAt", "domain", "path", "secure", "httpOnly", "sameSite", "cookie", "parseCookie", "cookie", "object", "keyEqValue", "key", "value", "crypto", "random", "setWithTimeout", "headers", "cookieTokenTimeout", "CookieSetter", "usersServer", "domain", "maxAge", "#handleClientConnect", "#handleClientSession", "#wsSessionIdMap", "wssc", "cookie", "options", "token", "crypto", "random", "setWithTimeout", "tokenMap", "headers", "names", "name", "request", "sessionId", "parseCookie"]
7
+ }
@@ -0,0 +1,2 @@
1
+ export declare function parseCookie(cookie: string): Record<string, unknown>;
2
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../src/server/parse.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,2BAYzC"}
@@ -0,0 +1,3 @@
1
+ import type { Cookie, CookieOptions } from "./types";
2
+ export declare const tokenMap: Map<string, [cookie: Cookie, options: CookieOptions]>;
3
+ //# sourceMappingURL=tokenMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tokenMap.d.ts","sourceRoot":"","sources":["../../src/server/tokenMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGrD,eAAO,MAAM,QAAQ,uDAAgE,CAAC"}
@@ -0,0 +1,11 @@
1
+ export type Cookie = Record<string, unknown>;
2
+ export type CookieOptions = {
3
+ maxAge?: number;
4
+ expiresAt?: Date;
5
+ domain?: string;
6
+ path?: false | null | string;
7
+ secure?: boolean;
8
+ httpOnly?: boolean;
9
+ sameSite?: string;
10
+ };
11
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/server/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE7C,MAAM,MAAM,aAAa,GAAG;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,IAAI,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,KAAK,GAAG,IAAI,GAAG,MAAM,CAAC;IAC7B,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC"}
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "insite-cookie",
3
- "version": "1.0.46",
3
+ "version": "2.0.2",
4
4
  "description": "Cookie support for inSite",
5
5
  "type": "module",
6
6
  "main": "./dist/server.js",
7
7
  "module": "./dist/server.js",
8
8
  "exports": {
9
- "./server": "./dist/server.js",
10
- "./client": "./dist/client.js"
9
+ "./server": "./dist/server/index.js",
10
+ "./client": "./dist/client/index.js"
11
11
  },
12
- "packageManager": "bun@1.1.17",
12
+ "packageManager": "bun@1.1.21",
13
13
  "scripts": {
14
14
  "test": "echo \"Error: no test specified\" && exit 1",
15
- "build": "bun .conveyer.js"
15
+ "build": "bun .conveyer.js",
16
+ "postbuild": "tsc -p ."
16
17
  },
17
18
  "repository": {
18
19
  "type": "git",
@@ -27,10 +28,18 @@
27
28
  "author": "Eugene Nesvetaev <dev@nesvet.dev> (https://nesvet.dev/)",
28
29
  "license": "MIT",
29
30
  "dependencies": {
30
- "insite-ws": "^1.0.406"
31
+ "@nesvet/n": "^1.1.1",
32
+ "insite-http": "^2.0.3",
33
+ "insite-ws": "^2.0.11"
31
34
  },
32
35
  "devDependencies": {
33
36
  "@nesvet/conveyer": "^2.2.1",
34
- "@nesvet/eslint-config": "^2.0.21"
37
+ "@nesvet/eslint-config": "^2.0.24",
38
+ "@types/node": "^20.14.14",
39
+ "@types/ws": "^8.5.12",
40
+ "insite-users-server": "^2.0.3",
41
+ "insite-users-server-ws": "^2.0.2",
42
+ "insite-subscriptions-server": "^2.0.2",
43
+ "typescript": "^5.5.4"
35
44
  }
36
45
  }
package/dist/client.js DELETED
@@ -1,2 +0,0 @@
1
- var h=e=>{throw TypeError(e)};var m=(e,o,t)=>o.has(e)||h("Cannot "+t);var i=(e,o,t)=>(m(e,o,"read from private field"),t?t.call(e):o.get(e)),a=(e,o,t)=>o.has(e)?h("Cannot add the same private member more than once"):o instanceof WeakSet?o.add(e):o.set(e,t);var l={set:"~setcookie"};var r,d=class{constructor(o,t={}){a(this,r,o=>{let t=new XMLHttpRequest;t.onload=this.onload,t.onerror=this.onerror,t.open(this.method,`${this.url}?${o}`,!0),t.send(null)});let{method:c="GET",url:u="/cookie",onload:s,onerror:n}=t;this.method=c,this.url=u,s&&(this.onload=s),n&&(this.onerror=n),o.on(`message:${l.set}`,i(this,r))}onload(){this.readyState===4&&this.status!==200&&console.error("Set cookie:",this.statusText)}onerror(){console.error("Set cookie:",this.statusText)}};r=new WeakMap;export{d as CookieSetter};
2
- //# sourceMappingURL=client.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/common.js", "../src/client/Setter.js"],
4
- "sourcesContent": ["export const headers = {\n\tset: \"~setcookie\"\n};\n", "import { headers } from \"../common\";\n\n\n/* eslint-disable unicorn/prefer-add-event-listener */\n\n\nexport class CookieSetter {\n\tconstructor(ws, options = {}) {\n\t\t\n\t\tconst {\n\t\t\tmethod = \"GET\",\n\t\t\turl = \"/cookie\",\n\t\t\tonload,\n\t\t\tonerror\n\t\t} = options;\n\t\t\n\t\tthis.method = method;\n\t\tthis.url = url;\n\t\t\n\t\tif (onload)\n\t\t\tthis.onload = onload;\n\t\t\n\t\tif (onerror)\n\t\t\tthis.onerror = onerror;\n\t\t\n\t\tws.on(`message:${headers.set}`, this.#handleSet);\n\t\t\n\t}\n\t\n\t#handleSet = token => {\n\t\t\n\t\tconst xhr = new XMLHttpRequest();\n\t\t\n\t\txhr.onload = this.onload;\n\t\txhr.onerror = this.onerror;\n\t\t\n\t\txhr.open(this.method, `${this.url}?${token}`, true);\n\t\t\n\t\txhr.send(null);\n\t\t\n\t};\n\t\n\tonload() {\n\t\t\n\t\tif (this.readyState === 4 && this.status !== 200)\n\t\t\tconsole.error(\"Set cookie:\", this.statusText);\n\t\t\n\t}\n\t\n\tonerror() {\n\t\t\n\t\tconsole.error(\"Set cookie:\", this.statusText);\n\t\t\n\t}\n\t\n}\n"],
5
- "mappings": "iQAAO,IAAMA,EAAU,CACtB,IAAK,YACN,ECFA,IAAAC,EAMaC,EAAN,KAAmB,CACzB,YAAYC,EAAIC,EAAU,CAAC,EAAG,CAsB9BC,EAAA,KAAAJ,EAAaK,GAAS,CAErB,IAAMC,EAAM,IAAI,eAEhBA,EAAI,OAAS,KAAK,OAClBA,EAAI,QAAU,KAAK,QAEnBA,EAAI,KAAK,KAAK,OAAQ,GAAG,KAAK,GAAG,IAAID,CAAK,GAAI,EAAI,EAElDC,EAAI,KAAK,IAAI,CAEd,GA/BC,GAAM,CACL,OAAAC,EAAS,MACT,IAAAC,EAAM,UACN,OAAAC,EACA,QAAAC,CACD,EAAIP,EAEJ,KAAK,OAASI,EACd,KAAK,IAAMC,EAEPC,IACH,KAAK,OAASA,GAEXC,IACH,KAAK,QAAUA,GAEhBR,EAAG,GAAG,WAAWS,EAAQ,GAAG,GAAIC,EAAA,KAAKZ,EAAU,CAEhD,CAeA,QAAS,CAEJ,KAAK,aAAe,GAAK,KAAK,SAAW,KAC5C,QAAQ,MAAM,cAAe,KAAK,UAAU,CAE9C,CAEA,SAAU,CAET,QAAQ,MAAM,cAAe,KAAK,UAAU,CAE7C,CAED,EA1BCA,EAAA",
6
- "names": ["headers", "_handleSet", "CookieSetter", "ws", "options", "__privateAdd", "token", "xhr", "method", "url", "onload", "onerror", "headers", "__privateGet"]
7
- }
package/dist/server.js DELETED
@@ -1,2 +0,0 @@
1
- var r=new Map;var l=class s{constructor(e={}){let{requestRegExp:o=/^\/cookie\/?\?/}=e;this.listeners={GET:[[o,this.handler]]}}handler=(e,o)=>{let[,t]=e.url.split("?");return t&&r.has(t)?o.writeHead(200,{"Content-Type":"text/plain; charset=utf-8","Set-Cookie":s.cookify(...r.getWithTimeout(t))}).end():!1};static make({name:e,value:o,maxAge:t,expiresAt:i,domain:p,path:u="/",secure:f=!0,httpOnly:k=!0,sameSite:h="Strict"}){let n=[`${e}=${o}`];return t!==void 0?n.push(`Max-Age=${t}`):i&&n.push(`Max-Age=${Math.round((i.getTime()-Date.now())/1e3)}`),p&&n.push(`Domain=${p}`),u&&n.push(`Path=${u}`),f&&n.push("Secure"),k&&n.push("HttpOnly"),h&&n.push(`SameSite=${h}`),n.join("; ")}static cookify(e,o){return Object.entries(e).map(([t,i])=>this.make({name:t,value:i,...o}))}};function a(s){return s?s.split(/;\s*/).reduce((e,o)=>{let[t,i]=o.split(/=/);try{e[t]=JSON.parse(i)}catch{e[t]=i}return e},{}):{}}import S from"node:crypto";var d={set:"~setcookie"};var x=2e3,c=Symbol("cookieSessionId"),m=class s{constructor(e,{users:o,domain:t,maxAge:i}){e.cookieSetter=this,this.domain=t??process.env.INSITE_HOST,this.maxAge=i??o?.sessions.collection.expireAfterSeconds,o&&(this.users=o,e.on("client-connect",s.handleClientConnect),e.on("client-session",s.handleClientSession))}static set(e,o){let t=`${Math.round(performance.now()*Number.MAX_SAFE_INTEGER).toString(36)}$${S.randomBytes(Math.randomInt(4,8)).toString("hex")}`;r.setWithTimeout(t,[e,o],x),this.sendMessage(d.set,t)}static unset(e){this.setCookie(e.reduce((o,t)=>(o[t]="",o),{}),{maxAge:0})}static parse=a;static handleClientConnect(e,o){if(Object.assign(e,{setCookie:s.set,unsetCookie:s.unset,[c]:null}),o.headers.cookie){let{sessionId:t=null}=a(o.headers.cookie);e[c]=t,t&&e.setSession(t,!0)}}static handleClientSession(e){if(e[c]!==e.session?._id)if(e[c]=e.session?._id,e.isOpen&&e.session){let{domain:o,maxAge:t}=this.cookieSetter;e.setCookie({sessionId:e.session._id},{domain:o,maxAge:t})}else e.unsetCookie(["sessionId"])}};export{l as CookieMiddleware,m as CookieSetter,a as parseCookie};
2
- //# sourceMappingURL=server.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/server/tokenMap.js", "../src/server/Middleware.js", "../src/server/parse.js", "../src/server/Setter.js", "../src/common.js"],
4
- "sourcesContent": ["export const tokenMap = new Map();\n", "import { tokenMap } from \"./tokenMap\";\n\n\nclass InSiteCookieMiddleware {\n\tconstructor(options = {}) {\n\t\tconst {\n\t\t\trequestRegExp = /^\\/cookie\\/?\\?/\n\t\t} = options;\n\t\t\n\t\tthis.listeners = { GET: [\n\t\t\t[ requestRegExp, this.handler ]\n\t\t] };\n\t\t\n\t}\n\t\n\thandler = (request, response) => {\n\t\tconst [ , token ] = request.url.split(\"?\");\n\t\t\n\t\tif (token && tokenMap.has(token))\n\t\t\treturn response.writeHead(200, {\n\t\t\t\t\"Content-Type\": \"text/plain; charset=utf-8\",\n\t\t\t\t\"Set-Cookie\": InSiteCookieMiddleware.cookify(...tokenMap.getWithTimeout(token))\n\t\t\t}).end();\n\t\t\n\t\treturn false;\n\t};\n\t\n\t\n\tstatic make({ name, value, maxAge, expiresAt, domain, path = \"/\", secure = true, httpOnly = true, sameSite = \"Strict\" }) {\n\t\tconst cookie = [ `${name}=${value}` ];\n\t\t\n\t\tif (maxAge !== undefined)\n\t\t\tcookie.push(`Max-Age=${maxAge}`);\n\t\telse if (expiresAt)\n\t\t\tcookie.push(`Max-Age=${Math.round((expiresAt.getTime() - Date.now()) / 1000)}`);\n\t\t\n\t\tif (domain)\n\t\t\tcookie.push(`Domain=${domain}`);\n\t\t\n\t\tif (path)\n\t\t\tcookie.push(`Path=${path}`);\n\t\t\n\t\tif (secure)\n\t\t\tcookie.push(\"Secure\");\n\t\t\n\t\tif (httpOnly)\n\t\t\tcookie.push(\"HttpOnly\");\n\t\t\n\t\tif (sameSite)\n\t\t\tcookie.push(`SameSite=${sameSite}`);\n\t\t\n\t\treturn cookie.join(\"; \");\n\t}\n\t\n\tstatic cookify(object, options) {\n\t\treturn Object.entries(object).map(([ name, value ]) => this.make({ name, value, ...options }));\n\t}\n\t\n}\n\nexport { InSiteCookieMiddleware as CookieMiddleware };\n", "export function parseCookie(cookie) {\n\treturn cookie ? cookie.split(/;\\s*/).reduce((object, keyEqValue) => {\n\t\tconst [ key, value ] = keyEqValue.split(/=/);\n\t\t\n\t\ttry {\n\t\t\tobject[key] = JSON.parse(value);\n\t\t} catch {\n\t\t\tobject[key] = value;\n\t\t}\n\t\t\n\t\treturn object;\n\t}, {}) : {};\n}\n", "import crypto from \"node:crypto\";\nimport { headers } from \"../common\";\nimport { parseCookie } from \"./parse\";\nimport { tokenMap } from \"./tokenMap\";\n\n\nconst cookieTokenTimeout = 2000;\nconst cookieSessionIdSymbol = Symbol(\"cookieSessionId\");\n\n\nexport class CookieSetter {\n\tconstructor(wss, { users, domain, maxAge }) {\n\t\twss.cookieSetter = this;\n\t\t\n\t\tthis.domain = domain ?? process.env.INSITE_HOST;\n\t\tthis.maxAge = maxAge ?? users?.sessions.collection.expireAfterSeconds;\n\t\t\n\t\tif (users) {\n\t\t\tthis.users = users;\n\t\t\t\n\t\t\twss.on(\"client-connect\", CookieSetter.handleClientConnect);\n\t\t\twss.on(\"client-session\", CookieSetter.handleClientSession);\n\t\t}\n\t\t\n\t}\n\t\n\t\n\tstatic set(cookie, options) {\n\t\t\n\t\tconst token = `${Math.round(performance.now() * Number.MAX_SAFE_INTEGER).toString(36)}$${crypto.randomBytes(Math.randomInt(4, 8)).toString(\"hex\")}`;\n\t\t\n\t\ttokenMap.setWithTimeout(token, [ cookie, options ], cookieTokenTimeout);\n\t\t\n\t\tthis.sendMessage(headers.set, token);\n\t\t\n\t}\n\t\n\tstatic unset(names) {\n\t\tthis.setCookie(names.reduce((cookie, name) => {\n\t\t\tcookie[name] = \"\";\n\t\t\t\n\t\t\treturn cookie;\n\t\t}, {}), { maxAge: 0 });\n\t\t\n\t}\n\t\n\tstatic parse = parseCookie;\n\t\n\t\n\tstatic handleClientConnect(ws, request) {\n\t\tObject.assign(ws, {\n\t\t\tsetCookie: CookieSetter.set,\n\t\t\tunsetCookie: CookieSetter.unset,\n\t\t\t[cookieSessionIdSymbol]: null\n\t\t});\n\t\t\n\t\tif (request.headers.cookie) {\n\t\t\tconst { sessionId = null } = parseCookie(request.headers.cookie);\n\t\t\t\n\t\t\tws[cookieSessionIdSymbol] = sessionId;\n\t\t\t\n\t\t\tif (sessionId)\n\t\t\t\tws.setSession(sessionId, true);\n\t\t}\n\t\t\n\t}\n\t\n\tstatic handleClientSession(ws) {\n\t\tif (ws[cookieSessionIdSymbol] !== ws.session?._id) {\n\t\t\tws[cookieSessionIdSymbol] = ws.session?._id;\n\t\t\t\n\t\t\tif (ws.isOpen && ws.session) {\n\t\t\t\tconst { domain, maxAge } = this.cookieSetter;\n\t\t\t\tws.setCookie({ sessionId: ws.session._id }, { domain, maxAge });\n\t\t\t} else\n\t\t\t\tws.unsetCookie([ \"sessionId\" ]);\n\t\t}\n\t\t\n\t}\n\t\n}\n", "export const headers = {\n\tset: \"~setcookie\"\n};\n"],
5
- "mappings": "AAAO,IAAMA,EAAW,IAAI,ICG5B,IAAMC,EAAN,MAAMC,CAAuB,CAC5B,YAAYC,EAAU,CAAC,EAAG,CACzB,GAAM,CACL,cAAAC,EAAgB,gBACjB,EAAID,EAEJ,KAAK,UAAY,CAAE,IAAK,CACvB,CAAEC,EAAe,KAAK,OAAQ,CAC/B,CAAE,CAEH,CAEA,QAAU,CAACC,EAASC,IAAa,CAChC,GAAM,CAAE,CAAEC,CAAM,EAAIF,EAAQ,IAAI,MAAM,GAAG,EAEzC,OAAIE,GAASC,EAAS,IAAID,CAAK,EACvBD,EAAS,UAAU,IAAK,CAC9B,eAAgB,4BAChB,aAAcJ,EAAuB,QAAQ,GAAGM,EAAS,eAAeD,CAAK,CAAC,CAC/E,CAAC,EAAE,IAAI,EAED,EACR,EAGA,OAAO,KAAK,CAAE,KAAAE,EAAM,MAAAC,EAAO,OAAAC,EAAQ,UAAAC,EAAW,OAAAC,EAAQ,KAAAC,EAAO,IAAK,OAAAC,EAAS,GAAM,SAAAC,EAAW,GAAM,SAAAC,EAAW,QAAS,EAAG,CACxH,IAAMC,EAAS,CAAE,GAAGT,CAAI,IAAIC,CAAK,EAAG,EAEpC,OAAIC,IAAW,OACdO,EAAO,KAAK,WAAWP,CAAM,EAAE,EACvB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",
6
- "names": ["tokenMap", "InSiteCookieMiddleware", "_InSiteCookieMiddleware", "options", "requestRegExp", "request", "response", "token", "tokenMap", "name", "value", "maxAge", "expiresAt", "domain", "path", "secure", "httpOnly", "sameSite", "cookie", "object", "parseCookie", "cookie", "object", "keyEqValue", "key", "value", "crypto", "headers", "cookieTokenTimeout", "cookieSessionIdSymbol", "CookieSetter", "_CookieSetter", "wss", "users", "domain", "maxAge", "cookie", "options", "token", "crypto", "tokenMap", "headers", "names", "name", "parseCookie", "ws", "request", "sessionId"]
7
- }