diff --git a/.auri/$ymzad0k8.md b/.auri/$ymzad0k8.md new file mode 100644 index 000000000..f6d7319a9 --- /dev/null +++ b/.auri/$ymzad0k8.md @@ -0,0 +1,6 @@ +--- +package: "lucia" # package name +type: "patch" # "major", "minor", "patch" +--- + +Remove dev dependencies for typing \ No newline at end of file diff --git a/packages/lucia/package.json b/packages/lucia/package.json index c8028880c..1261c2603 100644 --- a/packages/lucia/package.json +++ b/packages/lucia/package.json @@ -50,10 +50,7 @@ "author": "pilcrowonpaper", "license": "MIT", "devDependencies": { - "@sveltejs/kit": "1.10.0", - "@types/express": "^4.17.17", "@types/node": "^18.6.2", - "fastify": "^4.21.0", "prettier": "^2.3.0", "vitest": "^0.33.0" } diff --git a/packages/lucia/src/middleware/index.ts b/packages/lucia/src/middleware/index.ts index c386f0b21..fa8d368ad 100644 --- a/packages/lucia/src/middleware/index.ts +++ b/packages/lucia/src/middleware/index.ts @@ -7,18 +7,19 @@ import type { RequestContext } from "../auth/request.js"; -import type { - IncomingMessage, - OutgoingMessage, - ServerResponse -} from "node:http"; -import type { - Request as ExpressRequest, - Response as ExpressResponse -} from "express"; -import type { FastifyReply, FastifyRequest } from "fastify"; +type NodeIncomingMessage = { + method?: string; + headers: Record; +}; -export const node = (): Middleware<[IncomingMessage, OutgoingMessage]> => { +type NodeOutGoingMessage = { + getHeader: (name: string) => string | string[] | number | undefined; + setHeader: (name: string, value: string | number | readonly string[]) => void; +}; + +export const node = (): Middleware< + [NodeIncomingMessage, NodeOutGoingMessage] +> => { return ({ args }) => { const [incomingMessage, outgoingMessage] = args; const requestContext = { @@ -27,16 +28,16 @@ export const node = (): Middleware<[IncomingMessage, OutgoingMessage]> => { headers: createHeadersFromObject(incomingMessage.headers) }, setCookie: (cookie) => { - const setCookieHeaderValues = - outgoingMessage - .getHeader("Set-Cookie") - ?.toString() - .split(",") - .filter((val) => val) ?? []; - + let parsedSetCookieHeaderValues: string[] = []; + const setCookieHeaderValue = outgoingMessage.getHeader("Set-Cookie"); + if (typeof setCookieHeaderValue === "string") { + parsedSetCookieHeaderValues = [setCookieHeaderValue]; + } else if (Array.isArray(setCookieHeaderValue)) { + parsedSetCookieHeaderValues = setCookieHeaderValue; + } outgoingMessage.setHeader("Set-Cookie", [ cookie.serialize(), - ...setCookieHeaderValues + ...parsedSetCookieHeaderValues ]); } } as const satisfies RequestContext; @@ -45,6 +46,15 @@ export const node = (): Middleware<[IncomingMessage, OutgoingMessage]> => { }; }; +type ExpressRequest = { + method: string; + headers: Record; +}; + +type ExpressResponse = { + cookie: (name: string, val: string, options?: CookieAttributes) => void; +}; + export const express = (): Middleware<[ExpressRequest, ExpressResponse]> => { return ({ args }) => { const [req, res] = args; @@ -61,6 +71,15 @@ export const express = (): Middleware<[ExpressRequest, ExpressResponse]> => { }; }; +type FastifyRequest = { + method: string; + headers: Record; +}; + +type FastifyReply = { + header: (name: string, val: any) => void; +}; + export const fastify = (): Middleware<[FastifyRequest, FastifyReply]> => { return ({ args }) => { const [req, res] = args; @@ -197,8 +216,8 @@ export const web = (): Middleware<[Request]> => { }; type NextJsPagesServerContext = { - req: IncomingMessage; - res?: OutgoingMessage; + req: NodeIncomingMessage; + res?: NodeOutGoingMessage; }; type NextCookie = @@ -362,8 +381,8 @@ export const nextjs_future = (): Middleware< type H3Event = { node: { - req: IncomingMessage; - res: ServerResponse; + req: NodeIncomingMessage; + res: NodeOutGoingMessage; }; };