@niledatabase/express 5.3.1 → 5.3.2-alpha.1
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/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var R=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var O=(s,e)=>{for(var a in e)R(s,a,{get:e[a],enumerable:!0})},P=(s,e,a,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of T(e))!I.call(s,r)&&r!==a&&R(s,r,{get:()=>e[r],enumerable:!(n=N(e,r))||n.enumerable});return s};var b=s=>P(R({},"__esModule",{value:!0}),s);var U={};O(U,{cleaner:()=>f,express:()=>C});module.exports=b(U);var q=require("async_hooks"),S=require("@niledatabase/server");function f(s){return s.replaceAll(/\{([^}]+)\}/g,":$1")}var w=new q.AsyncLocalStorage,C=s=>{let e;return()=>({id:"express",onConfigure:a=>{e=a;let{debug:n}=e.logger("[EXTENSION][express]"),{paths:r}=e,t={get:r.get.map(f),post:r.post.map(f),put:r.put.map(f),delete:r.delete.map(f)};if(n(`paths configured ${JSON.stringify(t)}`),e.paths=t,s){n("initializing express extension with middleware"),s.use((o,c,i)=>{w.run(new Map,()=>i())});let d=o=>{w.getStore()?.set("context",o),e.withContext(o)};s.param("tenantId",(o,c,i,E)=>{n(`tenantId param set: ${E}`),d({tenantId:E,headers:new Headers(m(o.headers))}),i()}),s.use((o,c,i)=>{d({headers:new Headers(m(o.headers))}),i()}),n("routes configured");let u=e.handlers;s.get(t.get,u.GET),s.post(t.post,u.POST),s.put(t.put,u.PUT),s.delete(t.delete,u.DELETE)}},onHandleRequest:async a=>{if(!e)return;let{error:n,debug:r}=e.logger("[EXTENSION][express]");if(!a)return;let[t,d,u]=a;r("handling response");let o="",c=t.method,i={method:c,headers:new Headers};if(t instanceof Request)o=t.url,i.headers=t.headers;else try{o=t.protocol+"://"+t.get("host")+t.originalUrl}catch{}try{new URL(o)}catch{throw new Error("Invalid URL \u2014 are you running Express?")}t.headers?.cookie&&i.headers.set("cookie",t.headers.cookie),["POST","PUT"].includes(c)&&t.body&&(i.body=JSON.stringify(t.body));let E=new Request(o,i);r(`[${c}] proxy: ${o} ${JSON.stringify(i)}`);let x,H={headers:new Headers(t.headers),tenantId:t.params?.tenantId||void 0};try{x=await e.withContext(H,async p=>await p.handlers[c](E,{disableExtensions:["express"]}))}catch(p){return n(p),u()}let
|
|
1
|
+
"use strict";var R=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var O=(s,e)=>{for(var a in e)R(s,a,{get:e[a],enumerable:!0})},P=(s,e,a,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of T(e))!I.call(s,r)&&r!==a&&R(s,r,{get:()=>e[r],enumerable:!(n=N(e,r))||n.enumerable});return s};var b=s=>P(R({},"__esModule",{value:!0}),s);var U={};O(U,{cleaner:()=>f,express:()=>C});module.exports=b(U);var q=require("async_hooks"),S=require("@niledatabase/server");function f(s){return s.replaceAll(/\{([^}]+)\}/g,":$1")}var w=new q.AsyncLocalStorage,C=s=>{let e;return()=>({id:"express",onConfigure:a=>{e=a;let{debug:n}=e.logger("[EXTENSION][express]"),{paths:r}=e,t={get:r.get.map(f),post:r.post.map(f),put:r.put.map(f),delete:r.delete.map(f)};if(n(`paths configured ${JSON.stringify(t)}`),e.paths=t,s){n("initializing express extension with middleware"),s.use((o,c,i)=>{w.run(new Map,()=>i())});let d=o=>{w.getStore()?.set("context",o),e.withContext(o)};s.param("tenantId",(o,c,i,E)=>{n(`tenantId param set: ${E}`),d({tenantId:E,headers:new Headers(m(o.headers))}),i()}),s.use((o,c,i)=>{d({headers:new Headers(m(o.headers))}),i()}),n("routes configured");let u=e.handlers;s.get(t.get,u.GET),s.post(t.post,u.POST),s.put(t.put,u.PUT),s.delete(t.delete,u.DELETE)}},onHandleRequest:async a=>{if(!e)return;let{error:n,debug:r}=e.logger("[EXTENSION][express]");if(!a)return;let[t,d,u]=a;r("handling response");let o="",c=t.method,i={method:c,headers:new Headers};if(t instanceof Request)o=t.url,i.headers=t.headers;else try{o=t.protocol+"://"+t.get("host")+t.originalUrl}catch{}try{new URL(o)}catch{throw new Error("Invalid URL \u2014 are you running Express?")}t.headers?.cookie&&i.headers.set("cookie",t.headers.cookie),["POST","PUT"].includes(c)&&t.body&&(i.body=JSON.stringify(t.body));let E=new Request(o,i);r(`[${c}] proxy: ${o} ${JSON.stringify(i)}`);let x,H={headers:new Headers(t.headers),tenantId:t.params?.tenantId||void 0};try{x=await e.withContext(H,async p=>await p.handlers[c](E,{disableExtensions:["express"]}))}catch(p){return n(p),u()}let g;try{g=await x.clone().json()}catch{g=await x.text()}let l={};return x.headers.forEach((p,h)=>{if(!["content-encoding","content-length","transfer-encoding"].includes(h.toLowerCase()))if(l[h]){let y=l[h];l[h]=Array.isArray(y)?[...y,p]:[y,p]}else l[h]=p}),d?(d.headersSent||(r("sending response"),d.status(x.status).set(l),typeof g=="string"?d.send(g):d.json(g??{})),S.ExtensionState.onHandleRequest):x}})};function m(s){let e={};for(let[a,n]of Object.entries(s))typeof n=="string"?e[a]=n:Array.isArray(n)&&(e[a]=n.join(","));return e}0&&(module.exports={cleaner,express});
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'async_hooks';\nimport { IncomingHttpHeaders } from 'http';\n\nimport {\n ExtensionState,\n Server,\n Extension,\n PartialContext,\n} from '@niledatabase/server';\nimport type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n NextFunction,\n Express,\n} from 'express';\n\nexport function cleaner(val: string) {\n return val.replaceAll(/\\{([^}]+)\\}/g, ':$1');\n}\n\nconst contextStore = new AsyncLocalStorage<Map<string, unknown>>();\n\nconst express = (\n app: Express\n): Extension<[ExpressRequest, ExpressResponse, NextFunction]> => {\n let instance: Server;\n\n return () => ({\n id: 'express',\n\n onConfigure: (server: Server) => {\n instance = server;\n const { debug } = instance.logger('[EXTENSION][express]');\n const { paths: rawPaths } = instance;\n const paths = {\n get: rawPaths.get.map(cleaner),\n post: rawPaths.post.map(cleaner),\n put: rawPaths.put.map(cleaner),\n delete: rawPaths.delete.map(cleaner),\n };\n debug(`paths configured ${JSON.stringify(paths)}`);\n instance.paths = paths;\n\n if (app) {\n debug('initializing express extension with middleware');\n\n app.use(\n (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {\n contextStore.run(new Map(), () => next());\n }\n );\n\n const setRequestContext = (context: PartialContext) => {\n contextStore.getStore()?.set('context', context);\n instance.withContext(context);\n };\n\n app.param(\n 'tenantId',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (req: any, res: any, next: any, tenantId: string) => {\n debug(`tenantId param set: ${tenantId}`);\n setRequestContext({\n tenantId,\n headers: new Headers(normalizeHeaders(req.headers)),\n });\n next();\n }\n );\n\n app.use(\n (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {\n setRequestContext({\n headers: new Headers(normalizeHeaders(req.headers)),\n });\n next();\n }\n );\n\n debug('routes configured');\n // We cast handlers to any because Server handlers are Fetch-based but we mount them on Express\n // The Express extension assumes the Server handlers can handle the request or that\n // they are replaced/wrapped. In the original implementation, this was just passed through.\n // However, standard Nile handlers expect (req: Request). Express passes (req, res, next).\n // If this works, it's because Nile handlers might be handling the args or ignoring extra args?\n // Actually, the original code had 'server.handlers.GET' passed to 'app.get'.\n // We will do the same.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const handlers = instance.handlers as any;\n app.get(paths.get, handlers.GET);\n app.post(paths.post, handlers.POST);\n app.put(paths.put, handlers.PUT);\n app.delete(paths.delete, handlers.DELETE);\n }\n },\n\n onHandleRequest: async (\n params?: [ExpressRequest, ExpressResponse, NextFunction]\n ) => {\n if (!instance) {\n return;\n }\n const { error, debug } = instance.logger('[EXTENSION][express]');\n\n if (!params) {\n return;\n }\n const [req, res, next] = params;\n debug('handling response');\n\n let reqUrl = '';\n const method = req.method;\n const init: RequestInit = { method, headers: new Headers() };\n if (req instanceof Request) {\n reqUrl = req.url;\n init.headers = req.headers;\n } else {\n try {\n reqUrl = req.protocol + '://' + req.get('host') + req.originalUrl;\n } catch {\n // handled later\n }\n }\n\n try {\n new URL(reqUrl);\n } catch {\n throw new Error('Invalid URL — are you running Express?');\n }\n\n if (req.headers?.cookie) {\n (init.headers as Headers).set('cookie', req.headers.cookie);\n }\n\n if (['POST', 'PUT'].includes(method) && req.body) {\n init.body = JSON.stringify(req.body);\n }\n\n const proxyRequest = new Request(reqUrl, init);\n debug(`[${method}] proxy: ${reqUrl} ${JSON.stringify(init)}`);\n\n let response: Response;\n const context = {\n headers: new Headers(req.headers as HeadersInit),\n tenantId: req.params?.tenantId || undefined,\n };\n try {\n response = await instance.withContext(context, async (ctx) => {\n return (await ctx.handlers[\n method as 'GET' | 'POST' | 'PUT' | 'DELETE'\n ](proxyRequest, { disableExtensions: ['express'] })) as Response;\n });\n } catch (e) {\n error(e);\n return next();\n }\n\n let body;\n try {\n body = await response.clone().json();\n } catch {\n body = await response.text();\n }\n\n const newHeaders: Record<string, string | string[]> = {};\n response.headers.forEach((value, key) => {\n if (\n !['content-length', 'transfer-encoding'].includes(key.toLowerCase())\n ) {\n if (newHeaders[key]) {\n const prev = newHeaders[key];\n newHeaders[key] = Array.isArray(prev)\n ? [...prev, value]\n : [prev, value];\n } else {\n newHeaders[key] = value;\n }\n }\n });\n\n if (!res) {\n return response;\n }\n\n if (!res.headersSent) {\n debug('sending response');\n res.status(response.status).set(newHeaders);\n typeof body === 'string' ? res.send(body) : res.json(body ?? {});\n }\n\n return ExtensionState.onHandleRequest;\n },\n });\n};\nexport { express };\n\nfunction normalizeHeaders(headers: IncomingHttpHeaders): HeadersInit {\n const normalized: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n if (typeof value === 'string') {\n normalized[key] = value;\n } else if (Array.isArray(value)) {\n normalized[key] = value.join(',');\n }\n }\n return normalized;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,YAAAC,IAAA,eAAAC,EAAAJ,GAAA,IAAAK,EAAkC,uBAGlCC,EAKO,gCAQA,SAASJ,EAAQK,EAAa,CACnC,OAAOA,EAAI,WAAW,eAAgB,KAAK,CAC7C,CAEA,IAAMC,EAAe,IAAI,oBAEnBL,EACJM,GAC+D,CAC/D,IAAIC,EAEJ,MAAO,KAAO,CACZ,GAAI,UAEJ,YAAcC,GAAmB,CAC/BD,EAAWC,EACX,GAAM,CAAE,MAAAC,CAAM,EAAIF,EAAS,OAAO,sBAAsB,EAClD,CAAE,MAAOG,CAAS,EAAIH,EACtBI,EAAQ,CACZ,IAAKD,EAAS,IAAI,IAAIX,CAAO,EAC7B,KAAMW,EAAS,KAAK,IAAIX,CAAO,EAC/B,IAAKW,EAAS,IAAI,IAAIX,CAAO,EAC7B,OAAQW,EAAS,OAAO,IAAIX,CAAO,CACrC,EAIA,GAHAU,EAAM,oBAAoB,KAAK,UAAUE,CAAK,CAAC,EAAE,EACjDJ,EAAS,MAAQI,EAEbL,EAAK,CACPG,EAAM,gDAAgD,EAEtDH,EAAI,IACF,CAACM,EAAqBC,EAAsBC,IAAuB,CACjET,EAAa,IAAI,IAAI,IAAO,IAAMS,EAAK,CAAC,CAC1C,CACF,EAEA,IAAMC,EAAqBC,GAA4B,CACrDX,EAAa,SAAS,GAAG,IAAI,UAAWW,CAAO,EAC/CT,EAAS,YAAYS,CAAO,CAC9B,EAEAV,EAAI,MACF,WAEA,CAACM,EAAUC,EAAUC,EAAWG,IAAqB,CACnDR,EAAM,uBAAuBQ,CAAQ,E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,CAACC,EAAKC,CAAK,IAAK,OAAO,QAAQH,CAAO,EAC3C,OAAOG,GAAU,SACnBF,EAAWC,CAAG,EAAIC,EACT,MAAM,QAAQA,CAAK,IAC5BF,EAAWC,CAAG,EAAIC,EAAM,KAAK,GAAG,GAGpC,OAAOF,CACT","names":["index_exports","__export","cleaner","express","__toCommonJS","import_async_hooks","import_server","val","contextStore","app","instance","server","debug","rawPaths","paths","req","res","next","setRequestContext","context","tenantId","normalizeHeaders","handlers","params","error","reqUrl","method","init","proxyRequest","response","ctx","e","body","newHeaders","value","key","prev","normalizeHeaders","headers","normalized","key","value"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'async_hooks';\nimport { IncomingHttpHeaders } from 'http';\n\nimport {\n ExtensionState,\n Server,\n Extension,\n PartialContext,\n} from '@niledatabase/server';\nimport type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n NextFunction,\n Express,\n} from 'express';\n\nexport function cleaner(val: string) {\n return val.replaceAll(/\\{([^}]+)\\}/g, ':$1');\n}\n\nconst contextStore = new AsyncLocalStorage<Map<string, unknown>>();\n\nconst express = (\n app: Express\n): Extension<[ExpressRequest, ExpressResponse, NextFunction]> => {\n let instance: Server;\n\n return () => ({\n id: 'express',\n\n onConfigure: (server: Server) => {\n instance = server;\n const { debug } = instance.logger('[EXTENSION][express]');\n const { paths: rawPaths } = instance;\n const paths = {\n get: rawPaths.get.map(cleaner),\n post: rawPaths.post.map(cleaner),\n put: rawPaths.put.map(cleaner),\n delete: rawPaths.delete.map(cleaner),\n };\n debug(`paths configured ${JSON.stringify(paths)}`);\n instance.paths = paths;\n\n if (app) {\n debug('initializing express extension with middleware');\n\n app.use(\n (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {\n contextStore.run(new Map(), () => next());\n }\n );\n\n const setRequestContext = (context: PartialContext) => {\n contextStore.getStore()?.set('context', context);\n instance.withContext(context);\n };\n\n app.param(\n 'tenantId',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (req: any, res: any, next: any, tenantId: string) => {\n debug(`tenantId param set: ${tenantId}`);\n setRequestContext({\n tenantId,\n headers: new Headers(normalizeHeaders(req.headers)),\n });\n next();\n }\n );\n\n app.use(\n (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {\n setRequestContext({\n headers: new Headers(normalizeHeaders(req.headers)),\n });\n next();\n }\n );\n\n debug('routes configured');\n // We cast handlers to any because Server handlers are Fetch-based but we mount them on Express\n // The Express extension assumes the Server handlers can handle the request or that\n // they are replaced/wrapped. In the original implementation, this was just passed through.\n // However, standard Nile handlers expect (req: Request). Express passes (req, res, next).\n // If this works, it's because Nile handlers might be handling the args or ignoring extra args?\n // Actually, the original code had 'server.handlers.GET' passed to 'app.get'.\n // We will do the same.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const handlers = instance.handlers as any;\n app.get(paths.get, handlers.GET);\n app.post(paths.post, handlers.POST);\n app.put(paths.put, handlers.PUT);\n app.delete(paths.delete, handlers.DELETE);\n }\n },\n\n onHandleRequest: async (\n params?: [ExpressRequest, ExpressResponse, NextFunction]\n ) => {\n if (!instance) {\n return;\n }\n const { error, debug } = instance.logger('[EXTENSION][express]');\n\n if (!params) {\n return;\n }\n const [req, res, next] = params;\n debug('handling response');\n\n let reqUrl = '';\n const method = req.method;\n const init: RequestInit = { method, headers: new Headers() };\n if (req instanceof Request) {\n reqUrl = req.url;\n init.headers = req.headers;\n } else {\n try {\n reqUrl = req.protocol + '://' + req.get('host') + req.originalUrl;\n } catch {\n // handled later\n }\n }\n\n try {\n new URL(reqUrl);\n } catch {\n throw new Error('Invalid URL — are you running Express?');\n }\n\n if (req.headers?.cookie) {\n (init.headers as Headers).set('cookie', req.headers.cookie);\n }\n\n if (['POST', 'PUT'].includes(method) && req.body) {\n init.body = JSON.stringify(req.body);\n }\n\n const proxyRequest = new Request(reqUrl, init);\n debug(`[${method}] proxy: ${reqUrl} ${JSON.stringify(init)}`);\n\n let response: Response;\n const context = {\n headers: new Headers(req.headers as HeadersInit),\n tenantId: req.params?.tenantId || undefined,\n };\n try {\n response = await instance.withContext(context, async (ctx) => {\n return (await ctx.handlers[\n method as 'GET' | 'POST' | 'PUT' | 'DELETE'\n ](proxyRequest, { disableExtensions: ['express'] })) as Response;\n });\n } catch (e) {\n error(e);\n return next();\n }\n\n let body;\n try {\n body = await response.clone().json();\n } catch {\n body = await response.text();\n }\n\n const newHeaders: Record<string, string | string[]> = {};\n response.headers.forEach((value, key) => {\n if (\n !['content-encoding', 'content-length', 'transfer-encoding'].includes(\n key.toLowerCase()\n )\n ) {\n if (newHeaders[key]) {\n const prev = newHeaders[key];\n newHeaders[key] = Array.isArray(prev)\n ? [...prev, value]\n : [prev, value];\n } else {\n newHeaders[key] = value;\n }\n }\n });\n\n if (!res) {\n return response;\n }\n\n if (!res.headersSent) {\n debug('sending response');\n res.status(response.status).set(newHeaders);\n typeof body === 'string' ? res.send(body) : res.json(body ?? {});\n }\n\n return ExtensionState.onHandleRequest;\n },\n });\n};\nexport { express };\n\nfunction normalizeHeaders(headers: IncomingHttpHeaders): HeadersInit {\n const normalized: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n if (typeof value === 'string') {\n normalized[key] = value;\n } else if (Array.isArray(value)) {\n normalized[key] = value.join(',');\n }\n }\n return normalized;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,YAAAC,IAAA,eAAAC,EAAAJ,GAAA,IAAAK,EAAkC,uBAGlCC,EAKO,gCAQA,SAASJ,EAAQK,EAAa,CACnC,OAAOA,EAAI,WAAW,eAAgB,KAAK,CAC7C,CAEA,IAAMC,EAAe,IAAI,oBAEnBL,EACJM,GAC+D,CAC/D,IAAIC,EAEJ,MAAO,KAAO,CACZ,GAAI,UAEJ,YAAcC,GAAmB,CAC/BD,EAAWC,EACX,GAAM,CAAE,MAAAC,CAAM,EAAIF,EAAS,OAAO,sBAAsB,EAClD,C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BvD,OAjBAJ,EAAS,QAAQ,QAAQ,CAACK,EAAOC,IAAQ,CACvC,GACE,CAAC,CAAC,mBAAoB,iBAAkB,mBAAmB,EAAE,SAC3DA,EAAI,YAAY,CAClB,EAEA,GAAIF,EAAWE,CAAG,EAAG,CACnB,IAAMC,EAAOH,EAAWE,CAAG,EAC3BF,EAAWE,CAAG,EAAI,MAAM,QAAQC,CAAI,EAChC,CAAC,GAAGA,EAAMF,CAAK,EACf,CAACE,EAAMF,CAAK,CAClB,MACED,EAAWE,CAAG,EAAID,CAGxB,CAAC,EAEIlB,GAIAA,EAAI,cACPJ,EAAM,kBAAkB,EACxBI,EAAI,OAAOa,EAAS,MAAM,EAAE,IAAII,CAAU,EAC1C,OAAOD,GAAS,SAAWhB,EAAI,KAAKgB,CAAI,EAAIhB,EAAI,KAAKgB,GAAQ,CAAC,CAAC,GAG1D,iBAAe,iBATbH,CAUX,CACF,EACF,EAGA,SAASQ,EAAiBC,EAA2C,CACnE,IAAMC,EAAqC,CAAC,EAC5C,OAAW,CAACC,EAAKC,CAAK,IAAK,OAAO,QAAQH,CAAO,EAC3C,OAAOG,GAAU,SACnBF,EAAWC,CAAG,EAAIC,EACT,MAAM,QAAQA,CAAK,IAC5BF,EAAWC,CAAG,EAAIC,EAAM,KAAK,GAAG,GAGpC,OAAOF,CACT","names":["index_exports","__export","cleaner","express","__toCommonJS","import_async_hooks","import_server","val","contextStore","app","instance","server","debug","rawPaths","paths","req","res","next","setRequestContext","context","tenantId","normalizeHeaders","handlers","params","error","reqUrl","method","init","proxyRequest","response","ctx","e","body","newHeaders","value","key","prev","normalizeHeaders","headers","normalized","key","value"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{AsyncLocalStorage as q}from"async_hooks";import{ExtensionState as S}from"@niledatabase/server";function E(n){return n.replaceAll(/\{([^}]+)\}/g,":$1")}var R=new q,P=n=>{let s;return()=>({id:"express",onConfigure:d=>{s=d;let{debug:r}=s.logger("[EXTENSION][express]"),{paths:a}=s,e={get:a.get.map(E),post:a.post.map(E),put:a.put.map(E),delete:a.delete.map(E)};if(r(`paths configured ${JSON.stringify(e)}`),s.paths=e,n){r("initializing express extension with middleware"),n.use((t,c,o)=>{R.run(new Map,()=>o())});let i=t=>{R.getStore()?.set("context",t),s.withContext(t)};n.param("tenantId",(t,c,o,f)=>{r(`tenantId param set: ${f}`),i({tenantId:f,headers:new Headers(w(t.headers))}),o()}),n.use((t,c,o)=>{i({headers:new Headers(w(t.headers))}),o()}),r("routes configured");let u=s.handlers;n.get(e.get,u.GET),n.post(e.post,u.POST),n.put(e.put,u.PUT),n.delete(e.delete,u.DELETE)}},onHandleRequest:async d=>{if(!s)return;let{error:r,debug:a}=s.logger("[EXTENSION][express]");if(!d)return;let[e,i,u]=d;a("handling response");let t="",c=e.method,o={method:c,headers:new Headers};if(e instanceof Request)t=e.url,o.headers=e.headers;else try{t=e.protocol+"://"+e.get("host")+e.originalUrl}catch{}try{new URL(t)}catch{throw new Error("Invalid URL \u2014 are you running Express?")}e.headers?.cookie&&o.headers.set("cookie",e.headers.cookie),["POST","PUT"].includes(c)&&e.body&&(o.body=JSON.stringify(e.body));let f=new Request(t,o);a(`[${c}] proxy: ${t} ${JSON.stringify(o)}`);let x,m={headers:new Headers(e.headers),tenantId:e.params?.tenantId||void 0};try{x=await s.withContext(m,async p=>await p.handlers[c](f,{disableExtensions:["express"]}))}catch(p){return r(p),u()}let
|
|
1
|
+
import{AsyncLocalStorage as q}from"async_hooks";import{ExtensionState as S}from"@niledatabase/server";function E(n){return n.replaceAll(/\{([^}]+)\}/g,":$1")}var R=new q,P=n=>{let s;return()=>({id:"express",onConfigure:d=>{s=d;let{debug:r}=s.logger("[EXTENSION][express]"),{paths:a}=s,e={get:a.get.map(E),post:a.post.map(E),put:a.put.map(E),delete:a.delete.map(E)};if(r(`paths configured ${JSON.stringify(e)}`),s.paths=e,n){r("initializing express extension with middleware"),n.use((t,c,o)=>{R.run(new Map,()=>o())});let i=t=>{R.getStore()?.set("context",t),s.withContext(t)};n.param("tenantId",(t,c,o,f)=>{r(`tenantId param set: ${f}`),i({tenantId:f,headers:new Headers(w(t.headers))}),o()}),n.use((t,c,o)=>{i({headers:new Headers(w(t.headers))}),o()}),r("routes configured");let u=s.handlers;n.get(e.get,u.GET),n.post(e.post,u.POST),n.put(e.put,u.PUT),n.delete(e.delete,u.DELETE)}},onHandleRequest:async d=>{if(!s)return;let{error:r,debug:a}=s.logger("[EXTENSION][express]");if(!d)return;let[e,i,u]=d;a("handling response");let t="",c=e.method,o={method:c,headers:new Headers};if(e instanceof Request)t=e.url,o.headers=e.headers;else try{t=e.protocol+"://"+e.get("host")+e.originalUrl}catch{}try{new URL(t)}catch{throw new Error("Invalid URL \u2014 are you running Express?")}e.headers?.cookie&&o.headers.set("cookie",e.headers.cookie),["POST","PUT"].includes(c)&&e.body&&(o.body=JSON.stringify(e.body));let f=new Request(t,o);a(`[${c}] proxy: ${t} ${JSON.stringify(o)}`);let x,m={headers:new Headers(e.headers),tenantId:e.params?.tenantId||void 0};try{x=await s.withContext(m,async p=>await p.handlers[c](f,{disableExtensions:["express"]}))}catch(p){return r(p),u()}let g;try{g=await x.clone().json()}catch{g=await x.text()}let l={};return x.headers.forEach((p,h)=>{if(!["content-encoding","content-length","transfer-encoding"].includes(h.toLowerCase()))if(l[h]){let y=l[h];l[h]=Array.isArray(y)?[...y,p]:[y,p]}else l[h]=p}),i?(i.headersSent||(a("sending response"),i.status(x.status).set(l),typeof g=="string"?i.send(g):i.json(g??{})),S.onHandleRequest):x}})};function w(n){let s={};for(let[d,r]of Object.entries(n))typeof r=="string"?s[d]=r:Array.isArray(r)&&(s[d]=r.join(","));return s}export{E as cleaner,P as express};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'async_hooks';\nimport { IncomingHttpHeaders } from 'http';\n\nimport {\n ExtensionState,\n Server,\n Extension,\n PartialContext,\n} from '@niledatabase/server';\nimport type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n NextFunction,\n Express,\n} from 'express';\n\nexport function cleaner(val: string) {\n return val.replaceAll(/\\{([^}]+)\\}/g, ':$1');\n}\n\nconst contextStore = new AsyncLocalStorage<Map<string, unknown>>();\n\nconst express = (\n app: Express\n): Extension<[ExpressRequest, ExpressResponse, NextFunction]> => {\n let instance: Server;\n\n return () => ({\n id: 'express',\n\n onConfigure: (server: Server) => {\n instance = server;\n const { debug } = instance.logger('[EXTENSION][express]');\n const { paths: rawPaths } = instance;\n const paths = {\n get: rawPaths.get.map(cleaner),\n post: rawPaths.post.map(cleaner),\n put: rawPaths.put.map(cleaner),\n delete: rawPaths.delete.map(cleaner),\n };\n debug(`paths configured ${JSON.stringify(paths)}`);\n instance.paths = paths;\n\n if (app) {\n debug('initializing express extension with middleware');\n\n app.use(\n (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {\n contextStore.run(new Map(), () => next());\n }\n );\n\n const setRequestContext = (context: PartialContext) => {\n contextStore.getStore()?.set('context', context);\n instance.withContext(context);\n };\n\n app.param(\n 'tenantId',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (req: any, res: any, next: any, tenantId: string) => {\n debug(`tenantId param set: ${tenantId}`);\n setRequestContext({\n tenantId,\n headers: new Headers(normalizeHeaders(req.headers)),\n });\n next();\n }\n );\n\n app.use(\n (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {\n setRequestContext({\n headers: new Headers(normalizeHeaders(req.headers)),\n });\n next();\n }\n );\n\n debug('routes configured');\n // We cast handlers to any because Server handlers are Fetch-based but we mount them on Express\n // The Express extension assumes the Server handlers can handle the request or that\n // they are replaced/wrapped. In the original implementation, this was just passed through.\n // However, standard Nile handlers expect (req: Request). Express passes (req, res, next).\n // If this works, it's because Nile handlers might be handling the args or ignoring extra args?\n // Actually, the original code had 'server.handlers.GET' passed to 'app.get'.\n // We will do the same.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const handlers = instance.handlers as any;\n app.get(paths.get, handlers.GET);\n app.post(paths.post, handlers.POST);\n app.put(paths.put, handlers.PUT);\n app.delete(paths.delete, handlers.DELETE);\n }\n },\n\n onHandleRequest: async (\n params?: [ExpressRequest, ExpressResponse, NextFunction]\n ) => {\n if (!instance) {\n return;\n }\n const { error, debug } = instance.logger('[EXTENSION][express]');\n\n if (!params) {\n return;\n }\n const [req, res, next] = params;\n debug('handling response');\n\n let reqUrl = '';\n const method = req.method;\n const init: RequestInit = { method, headers: new Headers() };\n if (req instanceof Request) {\n reqUrl = req.url;\n init.headers = req.headers;\n } else {\n try {\n reqUrl = req.protocol + '://' + req.get('host') + req.originalUrl;\n } catch {\n // handled later\n }\n }\n\n try {\n new URL(reqUrl);\n } catch {\n throw new Error('Invalid URL — are you running Express?');\n }\n\n if (req.headers?.cookie) {\n (init.headers as Headers).set('cookie', req.headers.cookie);\n }\n\n if (['POST', 'PUT'].includes(method) && req.body) {\n init.body = JSON.stringify(req.body);\n }\n\n const proxyRequest = new Request(reqUrl, init);\n debug(`[${method}] proxy: ${reqUrl} ${JSON.stringify(init)}`);\n\n let response: Response;\n const context = {\n headers: new Headers(req.headers as HeadersInit),\n tenantId: req.params?.tenantId || undefined,\n };\n try {\n response = await instance.withContext(context, async (ctx) => {\n return (await ctx.handlers[\n method as 'GET' | 'POST' | 'PUT' | 'DELETE'\n ](proxyRequest, { disableExtensions: ['express'] })) as Response;\n });\n } catch (e) {\n error(e);\n return next();\n }\n\n let body;\n try {\n body = await response.clone().json();\n } catch {\n body = await response.text();\n }\n\n const newHeaders: Record<string, string | string[]> = {};\n response.headers.forEach((value, key) => {\n if (\n !['content-length', 'transfer-encoding'].includes(key.toLowerCase())\n ) {\n if (newHeaders[key]) {\n const prev = newHeaders[key];\n newHeaders[key] = Array.isArray(prev)\n ? [...prev, value]\n : [prev, value];\n } else {\n newHeaders[key] = value;\n }\n }\n });\n\n if (!res) {\n return response;\n }\n\n if (!res.headersSent) {\n debug('sending response');\n res.status(response.status).set(newHeaders);\n typeof body === 'string' ? res.send(body) : res.json(body ?? {});\n }\n\n return ExtensionState.onHandleRequest;\n },\n });\n};\nexport { express };\n\nfunction normalizeHeaders(headers: IncomingHttpHeaders): HeadersInit {\n const normalized: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n if (typeof value === 'string') {\n normalized[key] = value;\n } else if (Array.isArray(value)) {\n normalized[key] = value.join(',');\n }\n }\n return normalized;\n}\n"],"mappings":"AAAA,OAAS,qBAAAA,MAAyB,cAGlC,OACE,kBAAAC,MAIK,uBAQA,SAASC,EAAQC,EAAa,CACnC,OAAOA,EAAI,WAAW,eAAgB,KAAK,CAC7C,CAEA,IAAMC,EAAe,IAAIJ,EAEnBK,EACJC,GAC+D,CAC/D,IAAIC,EAEJ,MAAO,KAAO,CACZ,GAAI,UAEJ,YAAcC,GAAmB,CAC/BD,EAAWC,EACX,GAAM,CAAE,MAAAC,CAAM,EAAIF,EAAS,OAAO,sBAAsB,EAClD,CAAE,MAAOG,CAAS,EAAIH,EACtBI,EAAQ,CACZ,IAAKD,EAAS,IAAI,IAAIR,CAAO,EAC7B,KAAMQ,EAAS,KAAK,IAAIR,CAAO,EAC/B,IAAKQ,EAAS,IAAI,IAAIR,CAAO,EAC7B,OAAQQ,EAAS,OAAO,IAAIR,CAAO,CACrC,EAIA,GAHAO,EAAM,oBAAoB,KAAK,UAAUE,CAAK,CAAC,EAAE,EACjDJ,EAAS,MAAQI,EAEbL,EAAK,CACPG,EAAM,gDAAgD,EAEtDH,EAAI,IACF,CAACM,EAAqBC,EAAsBC,IAAuB,CACjEV,EAAa,IAAI,IAAI,IAAO,IAAMU,EAAK,CAAC,CAC1C,CACF,EAEA,IAAMC,EAAqBC,GAA4B,CACrDZ,EAAa,SAAS,GAAG,IAAI,UAAWY,CAAO,EAC/CT,EAAS,YAAYS,CAAO,C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B,EAAe,iBATbyB,CAUX,CACF,EACF,EAGA,SAASQ,EAAiBC,EAA2C,CACnE,IAAMC,EAAqC,CAAC,EAC5C,OAAW,CAACC,EAAKC,CAAK,IAAK,OAAO,QAAQH,CAAO,EAC3C,OAAOG,GAAU,SACnBF,EAAWC,CAAG,EAAIC,EACT,MAAM,QAAQA,CAAK,IAC5BF,EAAWC,CAAG,EAAIC,EAAM,KAAK,GAAG,GAGpC,OAAOF,CACT","names":["AsyncLocalStorage","ExtensionState","cleaner","val","contextStore","express","app","instance","server","debug","rawPaths","paths","req","res","next","setRequestContext","context","tenantId","normalizeHeaders","handlers","params","error","reqUrl","method","init","proxyRequest","response","ctx","e","body","newHeaders","value","key","prev","normalizeHeaders","headers","normalized","key","value"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'async_hooks';\nimport { IncomingHttpHeaders } from 'http';\n\nimport {\n ExtensionState,\n Server,\n Extension,\n PartialContext,\n} from '@niledatabase/server';\nimport type {\n Request as ExpressRequest,\n Response as ExpressResponse,\n NextFunction,\n Express,\n} from 'express';\n\nexport function cleaner(val: string) {\n return val.replaceAll(/\\{([^}]+)\\}/g, ':$1');\n}\n\nconst contextStore = new AsyncLocalStorage<Map<string, unknown>>();\n\nconst express = (\n app: Express\n): Extension<[ExpressRequest, ExpressResponse, NextFunction]> => {\n let instance: Server;\n\n return () => ({\n id: 'express',\n\n onConfigure: (server: Server) => {\n instance = server;\n const { debug } = instance.logger('[EXTENSION][express]');\n const { paths: rawPaths } = instance;\n const paths = {\n get: rawPaths.get.map(cleaner),\n post: rawPaths.post.map(cleaner),\n put: rawPaths.put.map(cleaner),\n delete: rawPaths.delete.map(cleaner),\n };\n debug(`paths configured ${JSON.stringify(paths)}`);\n instance.paths = paths;\n\n if (app) {\n debug('initializing express extension with middleware');\n\n app.use(\n (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {\n contextStore.run(new Map(), () => next());\n }\n );\n\n const setRequestContext = (context: PartialContext) => {\n contextStore.getStore()?.set('context', context);\n instance.withContext(context);\n };\n\n app.param(\n 'tenantId',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (req: any, res: any, next: any, tenantId: string) => {\n debug(`tenantId param set: ${tenantId}`);\n setRequestContext({\n tenantId,\n headers: new Headers(normalizeHeaders(req.headers)),\n });\n next();\n }\n );\n\n app.use(\n (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {\n setRequestContext({\n headers: new Headers(normalizeHeaders(req.headers)),\n });\n next();\n }\n );\n\n debug('routes configured');\n // We cast handlers to any because Server handlers are Fetch-based but we mount them on Express\n // The Express extension assumes the Server handlers can handle the request or that\n // they are replaced/wrapped. In the original implementation, this was just passed through.\n // However, standard Nile handlers expect (req: Request). Express passes (req, res, next).\n // If this works, it's because Nile handlers might be handling the args or ignoring extra args?\n // Actually, the original code had 'server.handlers.GET' passed to 'app.get'.\n // We will do the same.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const handlers = instance.handlers as any;\n app.get(paths.get, handlers.GET);\n app.post(paths.post, handlers.POST);\n app.put(paths.put, handlers.PUT);\n app.delete(paths.delete, handlers.DELETE);\n }\n },\n\n onHandleRequest: async (\n params?: [ExpressRequest, ExpressResponse, NextFunction]\n ) => {\n if (!instance) {\n return;\n }\n const { error, debug } = instance.logger('[EXTENSION][express]');\n\n if (!params) {\n return;\n }\n const [req, res, next] = params;\n debug('handling response');\n\n let reqUrl = '';\n const method = req.method;\n const init: RequestInit = { method, headers: new Headers() };\n if (req instanceof Request) {\n reqUrl = req.url;\n init.headers = req.headers;\n } else {\n try {\n reqUrl = req.protocol + '://' + req.get('host') + req.originalUrl;\n } catch {\n // handled later\n }\n }\n\n try {\n new URL(reqUrl);\n } catch {\n throw new Error('Invalid URL — are you running Express?');\n }\n\n if (req.headers?.cookie) {\n (init.headers as Headers).set('cookie', req.headers.cookie);\n }\n\n if (['POST', 'PUT'].includes(method) && req.body) {\n init.body = JSON.stringify(req.body);\n }\n\n const proxyRequest = new Request(reqUrl, init);\n debug(`[${method}] proxy: ${reqUrl} ${JSON.stringify(init)}`);\n\n let response: Response;\n const context = {\n headers: new Headers(req.headers as HeadersInit),\n tenantId: req.params?.tenantId || undefined,\n };\n try {\n response = await instance.withContext(context, async (ctx) => {\n return (await ctx.handlers[\n method as 'GET' | 'POST' | 'PUT' | 'DELETE'\n ](proxyRequest, { disableExtensions: ['express'] })) as Response;\n });\n } catch (e) {\n error(e);\n return next();\n }\n\n let body;\n try {\n body = await response.clone().json();\n } catch {\n body = await response.text();\n }\n\n const newHeaders: Record<string, string | string[]> = {};\n response.headers.forEach((value, key) => {\n if (\n !['content-encoding', 'content-length', 'transfer-encoding'].includes(\n key.toLowerCase()\n )\n ) {\n if (newHeaders[key]) {\n const prev = newHeaders[key];\n newHeaders[key] = Array.isArray(prev)\n ? [...prev, value]\n : [prev, value];\n } else {\n newHeaders[key] = value;\n }\n }\n });\n\n if (!res) {\n return response;\n }\n\n if (!res.headersSent) {\n debug('sending response');\n res.status(response.status).set(newHeaders);\n typeof body === 'string' ? res.send(body) : res.json(body ?? {});\n }\n\n return ExtensionState.onHandleRequest;\n },\n });\n};\nexport { express };\n\nfunction normalizeHeaders(headers: IncomingHttpHeaders): HeadersInit {\n const normalized: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers)) {\n if (typeof value === 'string') {\n normalized[key] = value;\n } else if (Array.isArray(value)) {\n normalized[key] = value.join(',');\n }\n }\n return normalized;\n}\n"],"mappings":"AAAA,OAAS,qBAAAA,MAAyB,cAGlC,OACE,kBAAAC,MAIK,uBAQA,SAASC,EAAQC,EAAa,CACnC,OAAOA,EAAI,WAAW,eAAgB,KAAK,CAC7C,CAEA,IAAMC,EAAe,IAAIJ,EAEnBK,EACJC,GAC+D,CAC/D,IAAIC,EAEJ,MAAO,KAAO,CACZ,GAAI,UAEJ,YAAcC,GAAmB,CAC/BD,EAAWC,EACX,GAAM,CAAE,MAAAC,CAAM,EAAIF,EAAS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AEA,IAAMI,EAAgD,CAAC,EAkBvD,OAjBAJ,EAAS,QAAQ,QAAQ,CAACK,EAAOC,IAAQ,CACvC,GACE,CAAC,CAAC,mBAAoB,iBAAkB,mBAAmB,EAAE,SAC3DA,EAAI,YAAY,CAClB,EAEA,GAAIF,EAAWE,CAAG,EAAG,CACnB,IAAMC,EAAOH,EAAWE,CAAG,EAC3BF,EAAWE,CAAG,EAAI,MAAM,QAAQC,CAAI,EAChC,CAAC,GAAGA,EAAMF,CAAK,EACf,CAACE,EAAMF,CAAK,CAClB,MACED,EAAWE,CAAG,EAAID,CAGxB,CAAC,EAEIlB,GAIAA,EAAI,cACPJ,EAAM,kBAAkB,EACxBI,EAAI,OAAOa,EAAS,MAAM,EAAE,IAAII,CAAU,EAC1C,OAAOD,GAAS,SAAWhB,EAAI,KAAKgB,CAAI,EAAIhB,EAAI,KAAKgB,GAAQ,CAAC,CAAC,GAG1D5B,EAAe,iBATbyB,CAUX,CACF,EACF,EAGA,SAASQ,EAAiBC,EAA2C,CACnE,IAAMC,EAAqC,CAAC,EAC5C,OAAW,CAACC,EAAKC,CAAK,IAAK,OAAO,QAAQH,CAAO,EAC3C,OAAOG,GAAU,SACnBF,EAAWC,CAAG,EAAIC,EACT,MAAM,QAAQA,CAAK,IAC5BF,EAAWC,CAAG,EAAIC,EAAM,KAAK,GAAG,GAGpC,OAAOF,CACT","names":["AsyncLocalStorage","ExtensionState","cleaner","val","contextStore","express","app","instance","server","debug","rawPaths","paths","req","res","next","setRequestContext","context","tenantId","normalizeHeaders","handlers","params","error","reqUrl","method","init","proxyRequest","response","ctx","e","body","newHeaders","value","key","prev","normalizeHeaders","headers","normalized","key","value"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@niledatabase/express",
|
|
3
|
-
"version": "5.3.1",
|
|
3
|
+
"version": "5.3.2-alpha.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
"express": "^5.0.0 || ^4.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@niledatabase/server": "^5.3.1",
|
|
41
|
+
"@niledatabase/server": "^5.3.2-alpha.1",
|
|
42
42
|
"@types/express": "^5",
|
|
43
43
|
"express": "^5.1.0",
|
|
44
44
|
"jest": "^29.7.0",
|
|
45
45
|
"ts-jest": "^29.3.4",
|
|
46
46
|
"tsup": "^8.5.0"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "ce338d8c3dded552558346d160666d42c81800ea"
|
|
49
49
|
}
|