feat: add cookie options (#12720)
* feat: add cookie options * feat: allow configuring hmr server port via the HMR_PORT env var * support configuring HMR host and proto * allow configuring the hmr client_port * cleanup * cleanup --------- Co-authored-by: Harminder Virk <virk.officials@gmail.com> Co-authored-by: Salvador Gironès <salvadorgirones@gmail.com> Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
4 files changed
+52
-8
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/framework": patch
|
||||||
|
"@medusajs/types": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(framework,types): add cookie options to project options
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { VIRTUAL_MODULES } from "@medusajs/admin-shared"
|
import { VIRTUAL_MODULES } from "@medusajs/admin-shared"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import type { InlineConfig } from "vite"
|
import type { HmrOptions, InlineConfig } from "vite"
|
||||||
import { injectTailwindCSS } from "../plugins/inject-tailwindcss"
|
import { injectTailwindCSS } from "../plugins/inject-tailwindcss"
|
||||||
import { writeStaticFiles } from "../plugins/write-static-files"
|
import { writeStaticFiles } from "../plugins/write-static-files"
|
||||||
import { BundlerOptions } from "../types"
|
import { BundlerOptions } from "../types"
|
||||||
@@ -13,7 +13,10 @@ export async function getViteConfig(
|
|||||||
const { default: medusa } = await import("@medusajs/admin-vite-plugin")
|
const { default: medusa } = await import("@medusajs/admin-vite-plugin")
|
||||||
|
|
||||||
const getPort = await import("get-port")
|
const getPort = await import("get-port")
|
||||||
const hmrPort = await getPort.default()
|
const hmrPort = process.env.HMR_PORT
|
||||||
|
? parseInt(process.env.HMR_PORT)
|
||||||
|
: await getPort.default()
|
||||||
|
const hmrOptions = getHmrConfig(hmrPort)
|
||||||
|
|
||||||
const root = path.resolve(process.cwd(), ".medusa/client")
|
const root = path.resolve(process.cwd(), ".medusa/client")
|
||||||
|
|
||||||
@@ -49,9 +52,7 @@ export async function getViteConfig(
|
|||||||
fs: {
|
fs: {
|
||||||
allow: [searchForWorkspaceRoot(process.cwd())],
|
allow: [searchForWorkspaceRoot(process.cwd())],
|
||||||
},
|
},
|
||||||
hmr: {
|
hmr: hmrOptions,
|
||||||
port: hmrPort,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
writeStaticFiles({
|
writeStaticFiles({
|
||||||
@@ -76,3 +77,21 @@ export async function getViteConfig(
|
|||||||
|
|
||||||
return baseConfig
|
return baseConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getHmrConfig(hmrPort: number): HmrOptions | boolean {
|
||||||
|
const options: HmrOptions = {
|
||||||
|
port: hmrPort,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.HMR_PROTOCOL) {
|
||||||
|
options.protocol = process.env.HMR_PROTOCOL
|
||||||
|
}
|
||||||
|
if (process.env.HMR_HOST) {
|
||||||
|
options.host = process.env.HMR_HOST
|
||||||
|
}
|
||||||
|
if (process.env.HMR_CLIENT_PORT) {
|
||||||
|
options.clientPort = parseInt(process.env.HMR_CLIENT_PORT)
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { dynamicImport } from "@medusajs/utils"
|
||||||
import createStore from "connect-redis"
|
import createStore from "connect-redis"
|
||||||
import cookieParser from "cookie-parser"
|
import cookieParser from "cookie-parser"
|
||||||
import express, { Express, RequestHandler } from "express"
|
import express, { Express, RequestHandler } from "express"
|
||||||
@@ -5,10 +6,9 @@ import session from "express-session"
|
|||||||
import Redis from "ioredis"
|
import Redis from "ioredis"
|
||||||
import morgan from "morgan"
|
import morgan from "morgan"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { logger } from "../logger"
|
|
||||||
import { configManager } from "../config"
|
import { configManager } from "../config"
|
||||||
|
import { logger } from "../logger"
|
||||||
import { MedusaRequest, MedusaResponse } from "./types"
|
import { MedusaRequest, MedusaResponse } from "./types"
|
||||||
import { dynamicImport } from "@medusajs/utils"
|
|
||||||
|
|
||||||
const NOISY_ENDPOINTS_CHUNKS = ["@fs", "@id", "@vite", "@react", "node_modules"]
|
const NOISY_ENDPOINTS_CHUNKS = ["@fs", "@id", "@vite", "@react", "node_modules"]
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
|||||||
sameSite = "none"
|
sameSite = "none"
|
||||||
}
|
}
|
||||||
|
|
||||||
const { http, sessionOptions } = configModule.projectConfig
|
const { http, sessionOptions, cookieOptions } = configModule.projectConfig
|
||||||
const sessionOpts = {
|
const sessionOpts = {
|
||||||
name: sessionOptions?.name ?? "connect.sid",
|
name: sessionOptions?.name ?? "connect.sid",
|
||||||
resave: sessionOptions?.resave ?? true,
|
resave: sessionOptions?.resave ?? true,
|
||||||
@@ -45,6 +45,7 @@ export async function expressLoader({ app }: { app: Express }): Promise<{
|
|||||||
sameSite,
|
sameSite,
|
||||||
secure,
|
secure,
|
||||||
maxAge: sessionOptions?.ttl ?? 10 * 60 * 60 * 1000,
|
maxAge: sessionOptions?.ttl ?? 10 * 60 * 60 * 1000,
|
||||||
|
...cookieOptions,
|
||||||
},
|
},
|
||||||
store: null,
|
store: null,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,6 +169,22 @@ export type SessionOptions = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @interface
|
||||||
|
*
|
||||||
|
* Options to pass to `express-session`.
|
||||||
|
*/
|
||||||
|
export type CookieOptions = Record<string, any> & {
|
||||||
|
secure?: boolean
|
||||||
|
sameSite?: "lax" | "strict" | "none"
|
||||||
|
maxAge?: number
|
||||||
|
httpOnly?: boolean
|
||||||
|
priority?: "low" | "medium" | "high"
|
||||||
|
domain?: string
|
||||||
|
path?: string
|
||||||
|
signed?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @interface
|
* @interface
|
||||||
*
|
*
|
||||||
@@ -413,6 +429,8 @@ export type ProjectConfigOptions = {
|
|||||||
*/
|
*/
|
||||||
sessionOptions?: SessionOptions
|
sessionOptions?: SessionOptions
|
||||||
|
|
||||||
|
cookieOptions?: CookieOptions
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure the number of staged jobs that are polled from the database. Default is `1000`.
|
* Configure the number of staged jobs that are polled from the database. Default is `1000`.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in new issue
Block a user