🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

better-call

Package Overview
Dependencies
Maintainers
1
Versions
204
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-call - npm Package Compare versions

Comparing version
2.0.0
to
2.0.1
+3
-3
dist/middleware.cjs

@@ -23,3 +23,3 @@ const require_error = require('./error.cjs');

enumerable: false,
configurable: false,
configurable: true,
get() {

@@ -54,3 +54,3 @@ return internalContext.responseHeaders;

enumerable: false,
configurable: false,
configurable: true,
get() {

@@ -84,3 +84,3 @@ return internalContext.responseHeaders;

enumerable: false,
configurable: false,
configurable: true,
get() {

@@ -87,0 +87,0 @@ return internalContext.responseHeaders;

@@ -1,1 +0,1 @@

{"version":3,"file":"middleware.cjs","names":["createInternalContext","isAPIError","kAPIErrorHeaderSymbol"],"sources":["../src/middleware.ts"],"sourcesContent":["import { createInternalContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport type { Status, statusCodes } from \"./error\";\nimport { type APIError, kAPIErrorHeaderSymbol } from \"./error\";\nimport type { Prettify } from \"./helper\";\nimport type { InferUse } from \"./types\";\nimport { isAPIError } from \"./utils\";\n\nexport type MiddlewareContext<Context = {}> = {\n\t/**\n\t * Method\n\t *\n\t * The request method\n\t */\n\tmethod: string;\n\t/**\n\t * Path\n\t *\n\t * The path of the endpoint\n\t */\n\tpath: string;\n\t/**\n\t * Body\n\t *\n\t * The body object will be the parsed JSON from the request and validated\n\t * against the body schema if it exists\n\t */\n\tbody: any;\n\t/**\n\t * Query\n\t *\n\t * The query object will be the parsed query string from the request\n\t * and validated against the query schema if it exists\n\t */\n\tquery: Record<string, any> | undefined;\n\t/**\n\t * Params\n\t *\n\t * If the path is `/user/:id` and the request is `/user/1` then the\n\t * params will be `{ id: \"1\" }` and if the path includes a wildcard like\n\t * `/user/*` then the params will be `{ _: \"1\" }` where `_` is the wildcard\n\t * key. If the wildcard is named like `/user/**:name` then the params will\n\t * be `{ name: string }`\n\t */\n\tparams: Record<string, any> | undefined;\n\t/**\n\t * Request object\n\t *\n\t * If `requireRequest` is set to true in the endpoint options this will be\n\t * required\n\t */\n\trequest: Request | undefined;\n\t/**\n\t * Headers\n\t *\n\t * If `requireHeaders` is set to true in the endpoint options this will be\n\t * required\n\t */\n\theaders: Headers | undefined;\n\t/**\n\t * Set header\n\t *\n\t * If it's called outside of a request it will just be ignored.\n\t */\n\tsetHeader: (key: string, value: string) => void;\n\t/**\n\t * Set the response status code\n\t */\n\tsetStatus: (status: Status) => void;\n\t/**\n\t * Get header\n\t *\n\t * If it's called outside of a request it will just return null\n\t *\n\t * @param key - The key of the header\n\t */\n\tgetHeader: (key: string) => string | null;\n\t/**\n\t * Get a cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie\n\t */\n\tgetCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;\n\t/**\n\t * Get a signed cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param secret - The secret of the signed cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie or null if the cookie is not found or false if the signature is invalid\n\t */\n\tgetSignedCookie: (\n\t\tkey: string,\n\t\tsecret: string,\n\t\tprefix?: CookiePrefixOptions,\n\t) => Promise<string | null | false>;\n\t/**\n\t * Set a cookie value in the response\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetCookie: (key: string, value: string, options?: CookieOptions) => string;\n\t/**\n\t * Set signed cookie\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param secret - The secret to sign the cookie with\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetSignedCookie: (\n\t\tkey: string,\n\t\tvalue: string,\n\t\tsecret: string,\n\t\toptions?: CookieOptions,\n\t) => Promise<string>;\n\t/**\n\t * JSON\n\t *\n\t * A helper function to create a JSON response with the correct headers\n\t * and status code. If `asResponse` is set to true in the context then\n\t * it will return a Response object instead of the JSON object.\n\t *\n\t * @param json - The JSON object to return\n\t * @param routerResponse - The response object to return if `asResponse` is\n\t * true in the context this will take precedence\n\t */\n\tjson: <R extends Record<string, any> | null>(\n\t\tjson: R,\n\t\trouterResponse?:\n\t\t\t| {\n\t\t\t\t\tstatus?: number;\n\t\t\t\t\theaders?: Record<string, string>;\n\t\t\t\t\tresponse?: Response;\n\t\t\t\t\tbody?: Record<string, any>;\n\t\t\t }\n\t\t\t| Response,\n\t) => R;\n\t/**\n\t * Middleware context\n\t */\n\tcontext: Prettify<Context>;\n\t/**\n\t * Redirect to a new URL\n\t */\n\tredirect: (url: string) => APIError;\n\t/**\n\t * Return error\n\t */\n\terror: (\n\t\tstatus: keyof typeof statusCodes | Status,\n\t\tbody?: {\n\t\t\tmessage?: string;\n\t\t\tcode?: string;\n\t\t} & Record<string, any>,\n\t\theaders?: HeadersInit,\n\t) => APIError;\n\tasResponse?: boolean;\n\treturnHeaders?: boolean;\n\treturnStatus?: boolean;\n\tresponseHeaders: Headers;\n};\n\ntype DefaultHandler = (inputCtx: MiddlewareContext<any>) => Promise<any>;\n\nexport type Middleware<\n\tHandler extends (\n\t\tinputCtx: MiddlewareContext<any>,\n\t) => Promise<any> = DefaultHandler,\n> = Handler & {\n\toptions: Record<string, any>;\n};\n\nexport function createMiddleware<Context = {}, R = unknown>(\n\thandler: (context: MiddlewareContext<Context>) => Promise<R>,\n): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\nexport function createMiddleware(handler: any) {\n\tconst internalHandler = async (inputCtx: any) => {\n\t\tconst context = inputCtx as Record<string, any>;\n\t\tconst internalContext = await createInternalContext(context, {\n\t\t\toptions: {},\n\t\t\tpath: \"/\",\n\t\t});\n\n\t\ttry {\n\t\t\tconst response = await handler(internalContext as any);\n\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\treturn context.returnHeaders\n\t\t\t\t? {\n\t\t\t\t\t\theaders,\n\t\t\t\t\t\tresponse,\n\t\t\t\t\t}\n\t\t\t\t: response;\n\t\t} catch (e) {\n\t\t\t// fixme(alex): this is workaround that set-cookie headers are not accessible when error is thrown from middleware\n\t\t\tif (isAPIError(e)) {\n\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\tenumerable: false,\n\t\t\t\t\tconfigurable: false,\n\t\t\t\t\tget() {\n\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow e;\n\t\t}\n\t};\n\tinternalHandler.options = {};\n\treturn internalHandler;\n}\n\ncreateMiddleware.create = <\n\tE extends {\n\t\tuse?: Middleware[];\n\t},\n>(\n\topts?: E,\n) => {\n\ttype InferredContext = InferUse<E[\"use\"]>;\n\n\tfunction fn<R>(\n\t\toptions: { use?: Middleware[] },\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn<R>(\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn(optionsOrHandler: any, handler?: any) {\n\t\tif (typeof optionsOrHandler === \"function\") {\n\t\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\t\toptions: { use: opts?.use },\n\t\t\t\t\tpath: \"/\",\n\t\t\t\t});\n\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await optionsOrHandler(internalContext as any);\n\t\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t\t} catch (e) {\n\t\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\t\tconfigurable: false,\n\t\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow e;\n\t\t\t\t}\n\t\t\t};\n\t\t\tinternalHandler.options = { use: opts?.use };\n\t\t\treturn internalHandler;\n\t\t}\n\t\tif (!handler) {\n\t\t\tthrow new Error(\"Middleware handler is required\");\n\t\t}\n\t\tconst use = [...(opts?.use || []), ...(optionsOrHandler.use || [])];\n\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\toptions: { use },\n\t\t\t\tpath: \"/\",\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tconst response = await handler(internalContext as any);\n\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t} catch (e) {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\tconfigurable: false,\n\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t};\n\t\tinternalHandler.options = { use };\n\t\treturn internalHandler as any;\n\t}\n\treturn fn;\n};\n"],"mappings":";;;;;AAsLA,SAAgB,iBAAiB,SAAc;CAC9C,MAAM,kBAAkB,OAAO,aAAkB;EAChD,MAAM,UAAU;EAChB,MAAM,kBAAkB,MAAMA,sCAAsB,SAAS;GAC5D,SAAS,EAAE;GACX,MAAM;GACN,CAAC;AAEF,MAAI;GACH,MAAM,WAAW,MAAM,QAAQ,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AAO,QAAQ,gBAAgB;KAAE;KAAS;KAAU,GAAG;YAC/C,GAAG;AACX,QAAIC,yBAAW,EAAE,CAChB,QAAO,eAAe,GAAGC,qCAAuB;KAC/C,YAAY;KACZ,cAAc;KACd,MAAM;AACL,aAAO,gBAAgB;;KAExB,CAAC;AAEH,UAAM;;;AAGR,kBAAgB,UAAU,EAAE,KAAK;AACjC,SAAO;;AAER,QAAO"}
{"version":3,"file":"middleware.cjs","names":["createInternalContext","isAPIError","kAPIErrorHeaderSymbol"],"sources":["../src/middleware.ts"],"sourcesContent":["import { createInternalContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport type { Status, statusCodes } from \"./error\";\nimport { type APIError, kAPIErrorHeaderSymbol } from \"./error\";\nimport type { Prettify } from \"./helper\";\nimport type { InferUse } from \"./types\";\nimport { isAPIError } from \"./utils\";\n\nexport type MiddlewareContext<Context = {}> = {\n\t/**\n\t * Method\n\t *\n\t * The request method\n\t */\n\tmethod: string;\n\t/**\n\t * Path\n\t *\n\t * The path of the endpoint\n\t */\n\tpath: string;\n\t/**\n\t * Body\n\t *\n\t * The body object will be the parsed JSON from the request and validated\n\t * against the body schema if it exists\n\t */\n\tbody: any;\n\t/**\n\t * Query\n\t *\n\t * The query object will be the parsed query string from the request\n\t * and validated against the query schema if it exists\n\t */\n\tquery: Record<string, any> | undefined;\n\t/**\n\t * Params\n\t *\n\t * If the path is `/user/:id` and the request is `/user/1` then the\n\t * params will be `{ id: \"1\" }` and if the path includes a wildcard like\n\t * `/user/*` then the params will be `{ _: \"1\" }` where `_` is the wildcard\n\t * key. If the wildcard is named like `/user/**:name` then the params will\n\t * be `{ name: string }`\n\t */\n\tparams: Record<string, any> | undefined;\n\t/**\n\t * Request object\n\t *\n\t * If `requireRequest` is set to true in the endpoint options this will be\n\t * required\n\t */\n\trequest: Request | undefined;\n\t/**\n\t * Headers\n\t *\n\t * If `requireHeaders` is set to true in the endpoint options this will be\n\t * required\n\t */\n\theaders: Headers | undefined;\n\t/**\n\t * Set header\n\t *\n\t * If it's called outside of a request it will just be ignored.\n\t */\n\tsetHeader: (key: string, value: string) => void;\n\t/**\n\t * Set the response status code\n\t */\n\tsetStatus: (status: Status) => void;\n\t/**\n\t * Get header\n\t *\n\t * If it's called outside of a request it will just return null\n\t *\n\t * @param key - The key of the header\n\t */\n\tgetHeader: (key: string) => string | null;\n\t/**\n\t * Get a cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie\n\t */\n\tgetCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;\n\t/**\n\t * Get a signed cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param secret - The secret of the signed cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie or null if the cookie is not found or false if the signature is invalid\n\t */\n\tgetSignedCookie: (\n\t\tkey: string,\n\t\tsecret: string,\n\t\tprefix?: CookiePrefixOptions,\n\t) => Promise<string | null | false>;\n\t/**\n\t * Set a cookie value in the response\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetCookie: (key: string, value: string, options?: CookieOptions) => string;\n\t/**\n\t * Set signed cookie\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param secret - The secret to sign the cookie with\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetSignedCookie: (\n\t\tkey: string,\n\t\tvalue: string,\n\t\tsecret: string,\n\t\toptions?: CookieOptions,\n\t) => Promise<string>;\n\t/**\n\t * JSON\n\t *\n\t * A helper function to create a JSON response with the correct headers\n\t * and status code. If `asResponse` is set to true in the context then\n\t * it will return a Response object instead of the JSON object.\n\t *\n\t * @param json - The JSON object to return\n\t * @param routerResponse - The response object to return if `asResponse` is\n\t * true in the context this will take precedence\n\t */\n\tjson: <R extends Record<string, any> | null>(\n\t\tjson: R,\n\t\trouterResponse?:\n\t\t\t| {\n\t\t\t\t\tstatus?: number;\n\t\t\t\t\theaders?: Record<string, string>;\n\t\t\t\t\tresponse?: Response;\n\t\t\t\t\tbody?: Record<string, any>;\n\t\t\t }\n\t\t\t| Response,\n\t) => R;\n\t/**\n\t * Middleware context\n\t */\n\tcontext: Prettify<Context>;\n\t/**\n\t * Redirect to a new URL\n\t */\n\tredirect: (url: string) => APIError;\n\t/**\n\t * Return error\n\t */\n\terror: (\n\t\tstatus: keyof typeof statusCodes | Status,\n\t\tbody?: {\n\t\t\tmessage?: string;\n\t\t\tcode?: string;\n\t\t} & Record<string, any>,\n\t\theaders?: HeadersInit,\n\t) => APIError;\n\tasResponse?: boolean;\n\treturnHeaders?: boolean;\n\treturnStatus?: boolean;\n\tresponseHeaders: Headers;\n};\n\ntype DefaultHandler = (inputCtx: MiddlewareContext<any>) => Promise<any>;\n\nexport type Middleware<\n\tHandler extends (\n\t\tinputCtx: MiddlewareContext<any>,\n\t) => Promise<any> = DefaultHandler,\n> = Handler & {\n\toptions: Record<string, any>;\n};\n\nexport function createMiddleware<Context = {}, R = unknown>(\n\thandler: (context: MiddlewareContext<Context>) => Promise<R>,\n): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\nexport function createMiddleware(handler: any) {\n\tconst internalHandler = async (inputCtx: any) => {\n\t\tconst context = inputCtx as Record<string, any>;\n\t\tconst internalContext = await createInternalContext(context, {\n\t\t\toptions: {},\n\t\t\tpath: \"/\",\n\t\t});\n\n\t\ttry {\n\t\t\tconst response = await handler(internalContext as any);\n\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\treturn context.returnHeaders\n\t\t\t\t? {\n\t\t\t\t\t\theaders,\n\t\t\t\t\t\tresponse,\n\t\t\t\t\t}\n\t\t\t\t: response;\n\t\t} catch (e) {\n\t\t\t// fixme(alex): this is workaround that set-cookie headers are not accessible when error is thrown from middleware\n\t\t\tif (isAPIError(e)) {\n\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\tenumerable: false,\n\t\t\t\t\tconfigurable: true,\n\t\t\t\t\tget() {\n\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow e;\n\t\t}\n\t};\n\tinternalHandler.options = {};\n\treturn internalHandler;\n}\n\ncreateMiddleware.create = <\n\tE extends {\n\t\tuse?: Middleware[];\n\t},\n>(\n\topts?: E,\n) => {\n\ttype InferredContext = InferUse<E[\"use\"]>;\n\n\tfunction fn<R>(\n\t\toptions: { use?: Middleware[] },\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn<R>(\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn(optionsOrHandler: any, handler?: any) {\n\t\tif (typeof optionsOrHandler === \"function\") {\n\t\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\t\toptions: { use: opts?.use },\n\t\t\t\t\tpath: \"/\",\n\t\t\t\t});\n\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await optionsOrHandler(internalContext as any);\n\t\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t\t} catch (e) {\n\t\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\t\tconfigurable: true,\n\t\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow e;\n\t\t\t\t}\n\t\t\t};\n\t\t\tinternalHandler.options = { use: opts?.use };\n\t\t\treturn internalHandler;\n\t\t}\n\t\tif (!handler) {\n\t\t\tthrow new Error(\"Middleware handler is required\");\n\t\t}\n\t\tconst use = [...(opts?.use || []), ...(optionsOrHandler.use || [])];\n\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\toptions: { use },\n\t\t\t\tpath: \"/\",\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tconst response = await handler(internalContext as any);\n\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t} catch (e) {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\tconfigurable: true,\n\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t};\n\t\tinternalHandler.options = { use };\n\t\treturn internalHandler as any;\n\t}\n\treturn fn;\n};\n"],"mappings":";;;;;AAsLA,SAAgB,iBAAiB,SAAc;CAC9C,MAAM,kBAAkB,OAAO,aAAkB;EAChD,MAAM,UAAU;EAChB,MAAM,kBAAkB,MAAMA,sCAAsB,SAAS;GAC5D,SAAS,EAAE;GACX,MAAM;GACN,CAAC;AAEF,MAAI;GACH,MAAM,WAAW,MAAM,QAAQ,gBAAuB;GACtD,MAAM,UAAU,gBAAgB;AAChC,UAAO,QAAQ,gBACZ;IACA;IACA;IACA,GACA;WACK,GAAG;AAEX,OAAIC,yBAAW,EAAE,CAChB,QAAO,eAAe,GAAGC,qCAAuB;IAC/C,YAAY;IACZ,cAAc;IACd,MAAM;AACL,YAAO,gBAAgB;;IAExB,CA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AU,EAAE,KAAK;AACjC,SAAO;;AAER,QAAO"}

@@ -23,3 +23,3 @@ import { kAPIErrorHeaderSymbol } from "./error.mjs";

enumerable: false,
configurable: false,
configurable: true,
get() {

@@ -54,3 +54,3 @@ return internalContext.responseHeaders;

enumerable: false,
configurable: false,
configurable: true,
get() {

@@ -84,3 +84,3 @@ return internalContext.responseHeaders;

enumerable: false,
configurable: false,
configurable: true,
get() {

@@ -87,0 +87,0 @@ return internalContext.responseHeaders;

@@ -1,1 +0,1 @@

{"version":3,"file":"middleware.mjs","names":[],"sources":["../src/middleware.ts"],"sourcesContent":["import { createInternalContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport type { Status, statusCodes } from \"./error\";\nimport { type APIError, kAPIErrorHeaderSymbol } from \"./error\";\nimport type { Prettify } from \"./helper\";\nimport type { InferUse } from \"./types\";\nimport { isAPIError } from \"./utils\";\n\nexport type MiddlewareContext<Context = {}> = {\n\t/**\n\t * Method\n\t *\n\t * The request method\n\t */\n\tmethod: string;\n\t/**\n\t * Path\n\t *\n\t * The path of the endpoint\n\t */\n\tpath: string;\n\t/**\n\t * Body\n\t *\n\t * The body object will be the parsed JSON from the request and validated\n\t * against the body schema if it exists\n\t */\n\tbody: any;\n\t/**\n\t * Query\n\t *\n\t * The query object will be the parsed query string from the request\n\t * and validated against the query schema if it exists\n\t */\n\tquery: Record<string, any> | undefined;\n\t/**\n\t * Params\n\t *\n\t * If the path is `/user/:id` and the request is `/user/1` then the\n\t * params will be `{ id: \"1\" }` and if the path includes a wildcard like\n\t * `/user/*` then the params will be `{ _: \"1\" }` where `_` is the wildcard\n\t * key. If the wildcard is named like `/user/**:name` then the params will\n\t * be `{ name: string }`\n\t */\n\tparams: Record<string, any> | undefined;\n\t/**\n\t * Request object\n\t *\n\t * If `requireRequest` is set to true in the endpoint options this will be\n\t * required\n\t */\n\trequest: Request | undefined;\n\t/**\n\t * Headers\n\t *\n\t * If `requireHeaders` is set to true in the endpoint options this will be\n\t * required\n\t */\n\theaders: Headers | undefined;\n\t/**\n\t * Set header\n\t *\n\t * If it's called outside of a request it will just be ignored.\n\t */\n\tsetHeader: (key: string, value: string) => void;\n\t/**\n\t * Set the response status code\n\t */\n\tsetStatus: (status: Status) => void;\n\t/**\n\t * Get header\n\t *\n\t * If it's called outside of a request it will just return null\n\t *\n\t * @param key - The key of the header\n\t */\n\tgetHeader: (key: string) => string | null;\n\t/**\n\t * Get a cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie\n\t */\n\tgetCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;\n\t/**\n\t * Get a signed cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param secret - The secret of the signed cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie or null if the cookie is not found or false if the signature is invalid\n\t */\n\tgetSignedCookie: (\n\t\tkey: string,\n\t\tsecret: string,\n\t\tprefix?: CookiePrefixOptions,\n\t) => Promise<string | null | false>;\n\t/**\n\t * Set a cookie value in the response\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetCookie: (key: string, value: string, options?: CookieOptions) => string;\n\t/**\n\t * Set signed cookie\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param secret - The secret to sign the cookie with\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetSignedCookie: (\n\t\tkey: string,\n\t\tvalue: string,\n\t\tsecret: string,\n\t\toptions?: CookieOptions,\n\t) => Promise<string>;\n\t/**\n\t * JSON\n\t *\n\t * A helper function to create a JSON response with the correct headers\n\t * and status code. If `asResponse` is set to true in the context then\n\t * it will return a Response object instead of the JSON object.\n\t *\n\t * @param json - The JSON object to return\n\t * @param routerResponse - The response object to return if `asResponse` is\n\t * true in the context this will take precedence\n\t */\n\tjson: <R extends Record<string, any> | null>(\n\t\tjson: R,\n\t\trouterResponse?:\n\t\t\t| {\n\t\t\t\t\tstatus?: number;\n\t\t\t\t\theaders?: Record<string, string>;\n\t\t\t\t\tresponse?: Response;\n\t\t\t\t\tbody?: Record<string, any>;\n\t\t\t }\n\t\t\t| Response,\n\t) => R;\n\t/**\n\t * Middleware context\n\t */\n\tcontext: Prettify<Context>;\n\t/**\n\t * Redirect to a new URL\n\t */\n\tredirect: (url: string) => APIError;\n\t/**\n\t * Return error\n\t */\n\terror: (\n\t\tstatus: keyof typeof statusCodes | Status,\n\t\tbody?: {\n\t\t\tmessage?: string;\n\t\t\tcode?: string;\n\t\t} & Record<string, any>,\n\t\theaders?: HeadersInit,\n\t) => APIError;\n\tasResponse?: boolean;\n\treturnHeaders?: boolean;\n\treturnStatus?: boolean;\n\tresponseHeaders: Headers;\n};\n\ntype DefaultHandler = (inputCtx: MiddlewareContext<any>) => Promise<any>;\n\nexport type Middleware<\n\tHandler extends (\n\t\tinputCtx: MiddlewareContext<any>,\n\t) => Promise<any> = DefaultHandler,\n> = Handler & {\n\toptions: Record<string, any>;\n};\n\nexport function createMiddleware<Context = {}, R = unknown>(\n\thandler: (context: MiddlewareContext<Context>) => Promise<R>,\n): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\nexport function createMiddleware(handler: any) {\n\tconst internalHandler = async (inputCtx: any) => {\n\t\tconst context = inputCtx as Record<string, any>;\n\t\tconst internalContext = await createInternalContext(context, {\n\t\t\toptions: {},\n\t\t\tpath: \"/\",\n\t\t});\n\n\t\ttry {\n\t\t\tconst response = await handler(internalContext as any);\n\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\treturn context.returnHeaders\n\t\t\t\t? {\n\t\t\t\t\t\theaders,\n\t\t\t\t\t\tresponse,\n\t\t\t\t\t}\n\t\t\t\t: response;\n\t\t} catch (e) {\n\t\t\t// fixme(alex): this is workaround that set-cookie headers are not accessible when error is thrown from middleware\n\t\t\tif (isAPIError(e)) {\n\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\tenumerable: false,\n\t\t\t\t\tconfigurable: false,\n\t\t\t\t\tget() {\n\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow e;\n\t\t}\n\t};\n\tinternalHandler.options = {};\n\treturn internalHandler;\n}\n\ncreateMiddleware.create = <\n\tE extends {\n\t\tuse?: Middleware[];\n\t},\n>(\n\topts?: E,\n) => {\n\ttype InferredContext = InferUse<E[\"use\"]>;\n\n\tfunction fn<R>(\n\t\toptions: { use?: Middleware[] },\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn<R>(\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn(optionsOrHandler: any, handler?: any) {\n\t\tif (typeof optionsOrHandler === \"function\") {\n\t\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\t\toptions: { use: opts?.use },\n\t\t\t\t\tpath: \"/\",\n\t\t\t\t});\n\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await optionsOrHandler(internalContext as any);\n\t\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t\t} catch (e) {\n\t\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\t\tconfigurable: false,\n\t\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow e;\n\t\t\t\t}\n\t\t\t};\n\t\t\tinternalHandler.options = { use: opts?.use };\n\t\t\treturn internalHandler;\n\t\t}\n\t\tif (!handler) {\n\t\t\tthrow new Error(\"Middleware handler is required\");\n\t\t}\n\t\tconst use = [...(opts?.use || []), ...(optionsOrHandler.use || [])];\n\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\toptions: { use },\n\t\t\t\tpath: \"/\",\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tconst response = await handler(internalContext as any);\n\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t} catch (e) {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\tconfigurable: false,\n\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t};\n\t\tinternalHandler.options = { use };\n\t\treturn internalHandler as any;\n\t}\n\treturn fn;\n};\n"],"mappings":";;;;;AAsLA,SAAgB,iBAAiB,SAAc;CAC9C,MAAM,kBAAkB,OAAO,aAAkB;EAChD,MAAM,UAAU;EAChB,MAAM,kBAAkB,MAAM,sBAAsB,SAAS;GAC5D,SAAS,EAAE;GACX,MAAM;GACN,CAAC;AAEF,MAAI;GACH,MAAM,WAAW,MAAM,QAAQ,gBAAuB;GACtD,MAAM,UAAU,gBAAgB;AAChC,UAAO,QAAQ,gBACZ;IACA;IACA;IACA,GACA;WACK,GAAG;AAEX,OAAI,WAAW,EAAE,CAChB,QAAO,eAAe,GAAG,uBAAuB;IAC/C,YAAY;IACZ,cAAc;IACd,MAAM;AACL,YAAO,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AAgB,UAAU,EAAE,KAAK;AACjC,SAAO;;AAER,QAAO"}
{"version":3,"file":"middleware.mjs","names":[],"sources":["../src/middleware.ts"],"sourcesContent":["import { createInternalContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport type { Status, statusCodes } from \"./error\";\nimport { type APIError, kAPIErrorHeaderSymbol } from \"./error\";\nimport type { Prettify } from \"./helper\";\nimport type { InferUse } from \"./types\";\nimport { isAPIError } from \"./utils\";\n\nexport type MiddlewareContext<Context = {}> = {\n\t/**\n\t * Method\n\t *\n\t * The request method\n\t */\n\tmethod: string;\n\t/**\n\t * Path\n\t *\n\t * The path of the endpoint\n\t */\n\tpath: string;\n\t/**\n\t * Body\n\t *\n\t * The body object will be the parsed JSON from the request and validated\n\t * against the body schema if it exists\n\t */\n\tbody: any;\n\t/**\n\t * Query\n\t *\n\t * The query object will be the parsed query string from the request\n\t * and validated against the query schema if it exists\n\t */\n\tquery: Record<string, any> | undefined;\n\t/**\n\t * Params\n\t *\n\t * If the path is `/user/:id` and the request is `/user/1` then the\n\t * params will be `{ id: \"1\" }` and if the path includes a wildcard like\n\t * `/user/*` then the params will be `{ _: \"1\" }` where `_` is the wildcard\n\t * key. If the wildcard is named like `/user/**:name` then the params will\n\t * be `{ name: string }`\n\t */\n\tparams: Record<string, any> | undefined;\n\t/**\n\t * Request object\n\t *\n\t * If `requireRequest` is set to true in the endpoint options this will be\n\t * required\n\t */\n\trequest: Request | undefined;\n\t/**\n\t * Headers\n\t *\n\t * If `requireHeaders` is set to true in the endpoint options this will be\n\t * required\n\t */\n\theaders: Headers | undefined;\n\t/**\n\t * Set header\n\t *\n\t * If it's called outside of a request it will just be ignored.\n\t */\n\tsetHeader: (key: string, value: string) => void;\n\t/**\n\t * Set the response status code\n\t */\n\tsetStatus: (status: Status) => void;\n\t/**\n\t * Get header\n\t *\n\t * If it's called outside of a request it will just return null\n\t *\n\t * @param key - The key of the header\n\t */\n\tgetHeader: (key: string) => string | null;\n\t/**\n\t * Get a cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie\n\t */\n\tgetCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;\n\t/**\n\t * Get a signed cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param secret - The secret of the signed cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie or null if the cookie is not found or false if the signature is invalid\n\t */\n\tgetSignedCookie: (\n\t\tkey: string,\n\t\tsecret: string,\n\t\tprefix?: CookiePrefixOptions,\n\t) => Promise<string | null | false>;\n\t/**\n\t * Set a cookie value in the response\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetCookie: (key: string, value: string, options?: CookieOptions) => string;\n\t/**\n\t * Set signed cookie\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param secret - The secret to sign the cookie with\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetSignedCookie: (\n\t\tkey: string,\n\t\tvalue: string,\n\t\tsecret: string,\n\t\toptions?: CookieOptions,\n\t) => Promise<string>;\n\t/**\n\t * JSON\n\t *\n\t * A helper function to create a JSON response with the correct headers\n\t * and status code. If `asResponse` is set to true in the context then\n\t * it will return a Response object instead of the JSON object.\n\t *\n\t * @param json - The JSON object to return\n\t * @param routerResponse - The response object to return if `asResponse` is\n\t * true in the context this will take precedence\n\t */\n\tjson: <R extends Record<string, any> | null>(\n\t\tjson: R,\n\t\trouterResponse?:\n\t\t\t| {\n\t\t\t\t\tstatus?: number;\n\t\t\t\t\theaders?: Record<string, string>;\n\t\t\t\t\tresponse?: Response;\n\t\t\t\t\tbody?: Record<string, any>;\n\t\t\t }\n\t\t\t| Response,\n\t) => R;\n\t/**\n\t * Middleware context\n\t */\n\tcontext: Prettify<Context>;\n\t/**\n\t * Redirect to a new URL\n\t */\n\tredirect: (url: string) => APIError;\n\t/**\n\t * Return error\n\t */\n\terror: (\n\t\tstatus: keyof typeof statusCodes | Status,\n\t\tbody?: {\n\t\t\tmessage?: string;\n\t\t\tcode?: string;\n\t\t} & Record<string, any>,\n\t\theaders?: HeadersInit,\n\t) => APIError;\n\tasResponse?: boolean;\n\treturnHeaders?: boolean;\n\treturnStatus?: boolean;\n\tresponseHeaders: Headers;\n};\n\ntype DefaultHandler = (inputCtx: MiddlewareContext<any>) => Promise<any>;\n\nexport type Middleware<\n\tHandler extends (\n\t\tinputCtx: MiddlewareContext<any>,\n\t) => Promise<any> = DefaultHandler,\n> = Handler & {\n\toptions: Record<string, any>;\n};\n\nexport function createMiddleware<Context = {}, R = unknown>(\n\thandler: (context: MiddlewareContext<Context>) => Promise<R>,\n): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\nexport function createMiddleware(handler: any) {\n\tconst internalHandler = async (inputCtx: any) => {\n\t\tconst context = inputCtx as Record<string, any>;\n\t\tconst internalContext = await createInternalContext(context, {\n\t\t\toptions: {},\n\t\t\tpath: \"/\",\n\t\t});\n\n\t\ttry {\n\t\t\tconst response = await handler(internalContext as any);\n\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\treturn context.returnHeaders\n\t\t\t\t? {\n\t\t\t\t\t\theaders,\n\t\t\t\t\t\tresponse,\n\t\t\t\t\t}\n\t\t\t\t: response;\n\t\t} catch (e) {\n\t\t\t// fixme(alex): this is workaround that set-cookie headers are not accessible when error is thrown from middleware\n\t\t\tif (isAPIError(e)) {\n\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\tenumerable: false,\n\t\t\t\t\tconfigurable: true,\n\t\t\t\t\tget() {\n\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow e;\n\t\t}\n\t};\n\tinternalHandler.options = {};\n\treturn internalHandler;\n}\n\ncreateMiddleware.create = <\n\tE extends {\n\t\tuse?: Middleware[];\n\t},\n>(\n\topts?: E,\n) => {\n\ttype InferredContext = InferUse<E[\"use\"]>;\n\n\tfunction fn<R>(\n\t\toptions: { use?: Middleware[] },\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn<R>(\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn(optionsOrHandler: any, handler?: any) {\n\t\tif (typeof optionsOrHandler === \"function\") {\n\t\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\t\toptions: { use: opts?.use },\n\t\t\t\t\tpath: \"/\",\n\t\t\t\t});\n\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await optionsOrHandler(internalContext as any);\n\t\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t\t} catch (e) {\n\t\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\t\tconfigurable: true,\n\t\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow e;\n\t\t\t\t}\n\t\t\t};\n\t\t\tinternalHandler.options = { use: opts?.use };\n\t\t\treturn internalHandler;\n\t\t}\n\t\tif (!handler) {\n\t\t\tthrow new Error(\"Middleware handler is required\");\n\t\t}\n\t\tconst use = [...(opts?.use || []), ...(optionsOrHandler.use || [])];\n\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\toptions: { use },\n\t\t\t\tpath: \"/\",\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tconst response = await handler(internalContext as any);\n\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t} catch (e) {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\tconfigurable: true,\n\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t};\n\t\tinternalHandler.options = { use };\n\t\treturn internalHandler as any;\n\t}\n\treturn fn;\n};\n"],"mappings":";;;;;AAsLA,SAAgB,iBAAiB,SAAc;CAC9C,MAAM,kBAAkB,OAAO,aAAkB;EAChD,MAAM,UAAU;EAChB,MAAM,kBAAkB,MAAM,sBAAsB,SAAS;GAC5D,SAAS,EAAE;GACX,MAAM;GACN,CAAC;AAEF,MAAI;GACH,MAAM,WAAW,MAAM,QAAQ,gBAAuB;GACtD,MAAM,UAAU,gBAAgB;AAChC,UAAO,QAAQ,gBACZ;IACA;IACA;IACA,GACA;WACK,GAAG;AAEX,OAAI,WAAW,EAAE,CAChB,QAAO,eAAe,GAAG,uBAAuB;IAC/C,YAAY;IACZ,cAAc;IACd,MAAM;AACL,YAAO,gBAAgB;;IAExB,CAAC;AAEH,SAAM;;;AAGR,iBAAgB,UAAU,EAAE;AAC5B,QAAO;;AAGR,iBAAiB,UAKhB,SACI;CAUJ,SAAS,GAAG,kBAAuB,SAAe;AACjD,MAAI,OAAO,qBAAqB,YAAY;GAC3C,MAAM,kBAAkB,OAAO,aAAkB;IAChD,MAAM,UAAU;IAChB,MAAM,kBAAkB,MAAM,sBAAsB,SAAS;KA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