insite-cookie 2.0.14 → 2.0.22
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/client/Setter.d.ts +3 -1
- package/dist/client/Setter.d.ts.map +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client/index.js.map +3 -3
- package/dist/server/Middleware.d.ts +1 -1
- package/dist/server/Middleware.d.ts.map +1 -1
- package/dist/server/Setter.d.ts +3 -1
- package/dist/server/Setter.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +3 -3
- package/package.json +3 -3
package/dist/client/Setter.d.ts
CHANGED
|
@@ -7,10 +7,12 @@ export type Options = {
|
|
|
7
7
|
onerror?: (this: XMLHttpRequest, event: ProgressEvent) => unknown;
|
|
8
8
|
};
|
|
9
9
|
export declare class CookieSetter {
|
|
10
|
-
#private;
|
|
11
10
|
constructor(ws: InSiteWebSocket, options?: Options);
|
|
12
11
|
readonly method: Method;
|
|
13
12
|
readonly url: string;
|
|
13
|
+
private onload;
|
|
14
|
+
private onerror;
|
|
15
|
+
private handleSet;
|
|
14
16
|
static onXhrLoad(this: XMLHttpRequest): void;
|
|
15
17
|
static onXhrError(this: XMLHttpRequest): void;
|
|
16
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Setter.d.ts","sourceRoot":"","sources":["../../src/client/Setter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAMjD,MAAM,MAAM,OAAO,GAAG;IACrB,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
|
|
1
|
+
{"version":3,"file":"Setter.d.ts","sourceRoot":"","sources":["../../src/client/Setter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAMjD,MAAM,MAAM,OAAO,GAAG;IACrB,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;IACb,OAAO,CAAC,MAAM,CAAC;IACf,OAAO,CAAC,OAAO,CAAC;IAEhB,OAAO,CAAC,SAAS,CAWf;IAGF,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,cAAc;IAOrC,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,cAAc;CAMtC"}
|
package/dist/client/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var p=Object.defineProperty;var l=(o,t,e)=>t in o?p(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var r=(o,t,e)=>l(o,typeof t!="symbol"?t+"":t,e);var s={set:"~setcookie"};var n=class o{constructor(t,e={}){r(this,"method");r(this,"url");r(this,"onload");r(this,"onerror");r(this,"handleSet",t=>{let e=new XMLHttpRequest;e.onload=this.onload,e.onerror=this.onerror,e.open(this.method,`${this.url}?${t}`,!0),e.send(null)});let{method:i="GET",url:h="/cookie",onload:a=o.onXhrLoad,onerror:d=o.onXhrError}=e;this.method=i,this.url=h,this.onload=a,this.onerror=d,t.on(`message:${s.set}`,this.handleSet)}static onXhrLoad(){this.readyState===4&&this.status!==200&&console.error("Set cookie:",this.statusText)}static onXhrError(){console.error("Set cookie:",this.statusText)}};export{n as CookieSetter};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/client/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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\n\nexport type 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
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["headers", "
|
|
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\n\nexport type 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\tprivate onload;\n\tprivate onerror;\n\t\n\tprivate 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": "oKAAO,IAAMA,EAAU,CACtB,IAAK,YACN,ECaO,IAAMC,EAAN,MAAMC,CAAa,CACzB,YAAYC,EAAqBC,EAAmB,CAAC,EAAG,CAkBxDC,EAAA,KAAS,UACTA,EAAA,KAAS,OACTA,EAAA,KAAQ,UACRA,EAAA,KAAQ,WAERA,EAAA,KAAQ,YAAaC,GAAkB,CAEtC,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,GAhCC,GAAM,CACL,OAAAC,EAAS,MACT,IAAAC,EAAM,UACN,OAAAC,EAASR,EAAa,UACtB,QAAAS,EAAUT,EAAa,UACxB,EAAIE,EAEJ,KAAK,OAASI,EACd,KAAK,IAAMC,EACX,KAAK,OAASC,EACd,KAAK,QAAUC,EAEfR,EAAG,GAAG,WAAWS,EAAQ,GAAG,GAAI,KAAK,SAAS,CAE/C,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",
|
|
6
|
+
"names": ["headers", "CookieSetter", "_CookieSetter", "ws", "options", "__publicField", "token", "xhr", "method", "url", "onload", "onerror", "headers"]
|
|
7
7
|
}
|
|
@@ -9,8 +9,8 @@ type MakeArg = {
|
|
|
9
9
|
value: Cookie[keyof Cookie];
|
|
10
10
|
} & CookieOptions;
|
|
11
11
|
export declare class InSiteCookieMiddleware extends InSiteServerMiddleware {
|
|
12
|
-
#private;
|
|
13
12
|
constructor(options?: Options);
|
|
13
|
+
private handler;
|
|
14
14
|
static make({ name, value, maxAge, expiresAt, domain, path, secure, httpOnly, sameSite }: MakeArg): string;
|
|
15
15
|
static cookify(cookie: Cookie, options: CookieOptions): string[];
|
|
16
16
|
}
|
|
@@ -1 +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
|
|
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;IAajC,OAAO,CAAC,OAAO,CAWb;IAGF,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"}
|
package/dist/server/Setter.d.ts
CHANGED
|
@@ -8,13 +8,15 @@ export type Options<AS extends AbilitiesSchema> = {
|
|
|
8
8
|
maxAge?: number;
|
|
9
9
|
};
|
|
10
10
|
export declare class CookieSetter<AS extends AbilitiesSchema> {
|
|
11
|
-
#private;
|
|
12
11
|
constructor({ usersServer, domain, maxAge }: Options<AS>);
|
|
12
|
+
private wsSessionIdMap;
|
|
13
13
|
usersServer: UsersServer<AS>;
|
|
14
14
|
domain: string | undefined;
|
|
15
15
|
maxAge: number;
|
|
16
16
|
set(wssc: WSSCWithUser<AS>, cookie: Cookie, options: CookieOptions): void;
|
|
17
17
|
unset(wssc: WSSCWithUser<AS>, names: string[]): void;
|
|
18
|
+
private handleClientConnect;
|
|
19
|
+
private handleClientSession;
|
|
18
20
|
static parse: typeof parseCookie;
|
|
19
21
|
}
|
|
20
22
|
//# sourceMappingURL=Setter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Setter.d.ts","sourceRoot":"","sources":["../../src/server/Setter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAExE,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
|
|
1
|
+
{"version":3,"file":"Setter.d.ts","sourceRoot":"","sources":["../../src/server/Setter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAExE,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;IAUxD,OAAO,CAAC,cAAc,CAAkD;IAExE,WAAW,kBAAC;IACZ,MAAM,qBAAC;IACP,MAAM,SAAC;IAEP,GAAG,CAAC,IAAI,EAAE,YAAY,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa;IAUlE,KAAK,CAAC,IAAI,EAAE,YAAY,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE;IAS7C,OAAO,CAAC,mBAAmB,CAYzB;IAEF,OAAO,CAAC,mBAAmB,CAUzB;IAGF,MAAM,CAAC,KAAK,qBAAe;CAE3B"}
|
package/dist/server/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{getWithTimeout as
|
|
1
|
+
import{getWithTimeout as k}from"@nesvet/n";import{InSiteServerMiddleware as f}from"insite-http";var n=new Map;var S=class r extends f{constructor(e={}){super();let{requestRegExp:t=/^\/cookie\/?\?/}=e;this.listeners={GET:[[t,this.handler]]}}handler=(e,t)=>{let[,s]=e.url.split("?");return s&&n.has(s)?t.writeHead(200,{"Content-Type":"text/plain; charset=utf-8","Set-Cookie":r.cookify(...k(n,s))}).end():!1};static make({name:e,value:t,maxAge:s,expiresAt:o,domain:a,path:m="/",secure:c=!0,httpOnly:l=!0,sameSite:h="Strict"}){let i=[`${e}=${t}`];return s!==void 0?i.push(`Max-Age=${s}`):o&&i.push(`Max-Age=${Math.round((o.getTime()-Date.now())/1e3)}`),a&&i.push(`Domain=${a}`),m&&i.push(`Path=${m}`),c&&i.push("Secure"),l&&i.push("HttpOnly"),h&&i.push(`SameSite=${h}`),i.join("; ")}static cookify(e,t){return Object.entries(e).map(([s,o])=>this.make({name:s,value:o,...t}))}};function p(r){return r?r.split(/;\s*/).reduce((e,t)=>{let[s,o]=t.split(/=/);try{e[s]=JSON.parse(o)}catch{e[s]=o}return e},{}):{}}import g from"node:crypto";import{random as C,setWithTimeout as x}from"@nesvet/n";var u={set:"~setcookie"};var y=2e3,d=class{constructor({usersServer:e,domain:t,maxAge:s}){this.usersServer=e,this.domain=t??process.env.INSITE_HOST,this.maxAge=s??this.usersServer.users.sessions.collection.expireAfterSeconds,this.usersServer.wss.on("client-connect",this.handleClientConnect),this.usersServer.wss.on("client-session",this.handleClientSession)}wsSessionIdMap=new WeakMap;usersServer;domain;maxAge;set(e,t,s){let o=`${Math.round(performance.now()*Number.MAX_SAFE_INTEGER).toString(36)}$${g.randomBytes(C(4,8)).toString("hex")}`;x(n,o,[t,s],y),e.sendMessage(u.set,o)}unset(e,t){this.set(e,t.reduce((s,o)=>(s[o]="",s),{}),{maxAge:0})}handleClientConnect=(e,t)=>{if(this.wsSessionIdMap.set(e,null),t.headers.cookie){let{sessionId:s=null}=p(t.headers.cookie);this.wsSessionIdMap.set(e,s),s&&this.usersServer.setSession(e,s,!0)}};handleClientSession=e=>{this.wsSessionIdMap.get(e)!==e.session?._id&&(this.wsSessionIdMap.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{d as CookieSetter,S as InSiteCookieMiddleware,p as parseCookie};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/server/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
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
|
|
5
|
-
"mappings": "AACA,OAAS,kBAAAA,MAAsB,YAC/B,OAAS,0BAAAC,MAA8B,cCChC,IAAMC,EAAW,IAAI,IDgBrB,IAAMC,EAAN,MAAMC,UAA+BC,CAAuB,CAClE,YAAYC,EAAmB,CAAC,EAAG,CAClC,MAAM,EAEN,GAAM,CACL,cAAAC,EAAgB,gBACjB,EAAID,EAEJ,KAAK,UAAY,CAAE,IAAK,CACvB,CAAEC,EAAe,
|
|
6
|
-
"names": ["getWithTimeout", "InSiteServerMiddleware", "tokenMap", "InSiteCookieMiddleware", "_InSiteCookieMiddleware", "InSiteServerMiddleware", "options", "requestRegExp", "
|
|
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\tprivate 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 { random, setWithTimeout } from \"@nesvet/n\";\nimport type { AbilitiesSchema } from \"insite-common\";\nimport type { UsersServer, WSSCWithUser } 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\tprivate wsSessionIdMap = new WeakMap<WSSCWithUser<AS>, null | string>();\n\t\n\tusersServer;\n\tdomain;\n\tmaxAge;\n\t\n\tset(wssc: WSSCWithUser<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: WSSCWithUser<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\tprivate handleClientConnect = (wssc: WSSCWithUser<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\tprivate handleClientSession = (wssc: WSSCWithUser<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,cCChC,IAAMC,EAAW,IAAI,IDgBrB,IAAMC,EAAN,MAAMC,UAA+BC,CAAuB,CAClE,YAAYC,EAAmB,CAAC,EAAG,CAClC,MAAM,EAEN,GAAM,CACL,cAAAC,EAAgB,gBACjB,EAAID,EAEJ,KAAK,UA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wBW,IAA6B,CAGnF,GAFA,KAAK,eAAe,IAAIX,EAAM,IAAI,EAE9BW,EAAQ,QAAQ,OAAQ,CAC3B,GAAM,CAAE,UAAAC,EAAY,IAAK,EAAIC,EAAYF,EAAQ,QAAQ,MAAM,EAE/D,KAAK,eAAe,IAAIX,EAAMY,CAAS,EAEnCA,GACH,KAAK,YAAY,WAAWZ,EAAMY,EAAW,EAAI,CACnD,CAED,EAEQ,oBAAuBZ,GAA2B,CACrD,KAAK,eAAe,IAAIA,CAAI,IAAMA,EAAK,SAAS,MACnD,KAAK,eAAe,IAAIA,EAAMA,EAAK,SAAS,KAAO,IAAI,EAEnDA,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", "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", "wssc", "cookie", "options", "token", "crypto", "random", "setWithTimeout", "tokenMap", "headers", "names", "name", "request", "sessionId", "parseCookie"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "insite-cookie",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.22",
|
|
4
4
|
"description": "Cookie support for inSite",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/server.js",
|
|
@@ -31,13 +31,13 @@
|
|
|
31
31
|
"@nesvet/n": "^1.1.7",
|
|
32
32
|
"insite-common": "^2.0.0",
|
|
33
33
|
"insite-http": "^2.0.6",
|
|
34
|
-
"insite-users-server-ws": "^2.0.
|
|
34
|
+
"insite-users-server-ws": "^2.0.18",
|
|
35
35
|
"insite-ws": "^2.0.17"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@nesvet/conveyer": "^2.2.4",
|
|
39
39
|
"@nesvet/eslint-config": "^2.0.25",
|
|
40
|
-
"@types/node": "^22.5.
|
|
40
|
+
"@types/node": "^22.5.2",
|
|
41
41
|
"@types/ws": "^8.5.12",
|
|
42
42
|
"typescript": "^5.5.4"
|
|
43
43
|
}
|