@nmtjs/http-transport 0.1.1 → 0.1.4

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.
@@ -25,12 +25,10 @@ export class HttpTransportServer {
25
25
  const ac = new AbortController();
26
26
  res.onAborted(()=>ac.abort());
27
27
  const tryEnd = (cb)=>{
28
- if (!ac.signal.aborted) res.cork(()=>{
29
- this.applyCors(res, req);
30
- return cb();
31
- });
28
+ if (!ac.signal.aborted) res.cork(cb);
32
29
  };
33
30
  try {
31
+ this.applyCors(res, req);
34
32
  const requestData = getRequestData(req);
35
33
  const serviceName = req.getParameter(0);
36
34
  const procedureName = req.getParameter(1);
@@ -128,10 +126,10 @@ export class HttpTransportServer {
128
126
  applyCors(res, req) {
129
127
  const origin = req.getHeader('origin');
130
128
  if (!origin) return;
131
- res.headers.set('Access-Control-Allow-Origin', origin);
132
- res.headers.set('Access-Control-Allow-Headers', 'Content-Type');
133
- res.headers.set('Access-Control-Allow-Methods', 'GET, POST');
134
- res.headers.set('Access-Control-Allow-Credentials', 'true');
129
+ res.writeHeader('Access-Control-Allow-Origin', origin);
130
+ res.writeHeader('Access-Control-Allow-Headers', 'Content-Type');
131
+ res.writeHeader('Access-Control-Allow-Methods', 'GET, POST');
132
+ res.writeHeader('Access-Control-Allow-Credentials', 'true');
135
133
  }
136
134
  handleContainerDisposal(container) {
137
135
  container.dispose();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../lib/server.ts"],"sourcesContent":["import {\n type AnyProcedure,\n ApiError,\n type ApplicationContext,\n type Connection,\n type Container,\n Scope,\n type Service,\n injectables,\n} from '@nmtjs/application'\nimport { TransportType } from '@nmtjs/common'\nimport {\n App,\n type HttpRequest,\n type HttpResponse,\n SSLApp,\n type TemplatedApp,\n} from 'uWebSockets.js'\nimport { connectionData } from './injectables.ts'\nimport type { HttpTransportOptions } from './types.ts'\nimport { InternalError, getFormat, getRequestData } from './utils.ts'\n\nexport class HttpTransportServer {\n protected server!: TemplatedApp\n protected readonly transportType = TransportType.HTTP\n\n constructor(\n protected readonly application: ApplicationContext,\n protected readonly options: HttpTransportOptions,\n ) {\n this.server = this.options.tls ? SSLApp(options.tls!) : App()\n\n this.server\n .options('/*', (res, req) => {\n this.applyCors(res, req)\n res.writeStatus('200 OK')\n res.endWithoutBody()\n })\n .get('/healthy', (res, req) => {\n this.applyCors(res, req)\n res.writeHeader('Content-Type', 'text/plain')\n res.end('OK')\n })\n .post('/api/:service/:procudure', async (res, req) => {\n const ac = new AbortController()\n res.onAborted(() => ac.abort())\n const tryEnd = (cb) => {\n if (!ac.signal.aborted)\n res.cork(() => {\n this.applyCors(res, req)\n return cb()\n })\n }\n\n try {\n const requestData = getRequestData(req)\n\n const serviceName = req.getParameter(0)!\n const procedureName = req.getParameter(1)!\n\n const service = this.application.registry.services.get(serviceName)\n\n if (!service) throw new Error(`Service ${serviceName} not found`)\n if (this.transportType in service.contract.transports === false)\n throw new Error(`Service ${serviceName} not supported`)\n\n const format = getFormat(requestData, this.application.format)\n const body = await this.getBody(res)\n const container = this.application.container.createScope(Scope.Call)\n const payload = body.byteLength ? format.decoder.decode(body) : null\n const connection = this.application.connections.add({\n services: [serviceName],\n type: this.transportType,\n subscriptions: new Map(),\n })\n\n const responseHeaders = new Headers()\n\n container.provide(connectionData, {\n query: requestData.query,\n headers: requestData.headers,\n proxiedRemoteAddress: Buffer.from(\n res.getProxiedRemoteAddressAsText(),\n ).toString(),\n remoteAddress: Buffer.from(res.getRemoteAddressAsText()).toString(),\n responseHeaders,\n })\n container.provide(injectables.connection, connection)\n\n const { procedure } = this.api.find(\n serviceName,\n procedureName,\n this.transportType,\n )\n\n try {\n const response = await this.handleRPC({\n connection,\n service,\n procedure,\n container,\n signal: ac.signal,\n payload,\n })\n\n tryEnd(() => {\n res\n .writeStatus('200 OK')\n .writeHeader('Content-Type', format.encoder.contentType)\n responseHeaders.forEach((v, k) => res.writeHeader(k, v))\n res.end(format.encoder.encode({ error: null, result: response }))\n })\n } catch (error: any) {\n if (error instanceof ApiError) {\n tryEnd(() =>\n res\n .writeStatus('200 OK')\n .end(format.encoder.encode({ error, result: null })),\n )\n } else {\n tryEnd(() =>\n res.writeStatus('200 OK').end(\n format.encoder.encode({\n error: InternalError(),\n result: null,\n }),\n ),\n )\n }\n this.logError(error)\n } finally {\n this.application.connections.remove(connection)\n this.handleContainerDisposal(container)\n }\n } catch (error: any) {\n this.logError(error)\n tryEnd(() =>\n res.writeStatus('500 Internal Server Error').endWithoutBody(),\n )\n }\n })\n }\n\n async start() {\n return new Promise<void>((resolve, reject) => {\n const hostname = this.options.hostname ?? '127.0.0.1'\n this.server.listen(hostname, this.options.port!, (socket) => {\n if (socket) {\n this.logger.info(\n 'Server started on %s:%s',\n hostname,\n this.options.port!,\n )\n resolve()\n } else {\n reject(new Error('Failed to start server'))\n }\n })\n })\n }\n\n async stop() {\n this.server.close()\n }\n\n protected get api() {\n return this.application.api\n }\n\n protected get logger() {\n return this.application.logger\n }\n\n protected async logError(\n cause: Error,\n message = 'Unknown error while processing request',\n ) {\n this.logger.error(new Error(message, { cause }))\n }\n\n protected applyCors(res: HttpResponse, req: HttpRequest) {\n // TODO: this should be configurable\n const origin = req.getHeader('origin')\n if (!origin) return\n res.headers.set('Access-Control-Allow-Origin', origin)\n res.headers.set('Access-Control-Allow-Headers', 'Content-Type')\n res.headers.set('Access-Control-Allow-Methods', 'GET, POST')\n res.headers.set('Access-Control-Allow-Credentials', 'true')\n }\n\n protected handleContainerDisposal(container: Container) {\n container.dispose()\n }\n\n protected async handleRPC(options: {\n connection: Connection\n service: Service\n procedure: AnyProcedure\n container: Container\n signal: AbortSignal\n payload: any\n }) {\n return await this.api.call({\n ...options,\n transport: this.transportType,\n })\n }\n\n protected async getBody(res: HttpResponse) {\n return new Promise<Buffer>((resolve) => {\n const chunks: Buffer[] = []\n res.onData((chunk, isLast) => {\n chunks.push(Buffer.from(chunk))\n if (isLast) {\n resolve(Buffer.concat(chunks))\n }\n })\n })\n }\n}\n"],"names":["ApiError","Scope","injectables","TransportType","App","SSLApp","connectionData","InternalError","getFormat","getRequestData","HttpTransportServer","server","transportType","constructor","application","options","HTTP","tls","res","req","applyCors","writeStatus","endWithoutBody","get","writeHeader","end","post","ac","AbortController","onAborted","abort","tryEnd","cb","signal","aborted","cork","requestData","serviceName","getParameter","procedureName","service","registry","services","Error","contract","transports","format","body","getBody","container","createScope","Call","payload","byteLength","decoder","decode","connection","connections","add","type","subscriptions","Map","responseHeaders","Headers","provide","query","headers","proxiedRemoteAddress","Buffer","from","getProxiedRemoteAddressAsText","toString","remoteAddress","getRemoteAddressAsText","procedure","api","find","response","handleRPC","encoder","contentType","forEach","v","k","encode","error","result","logError","remove","handleContainerDisposal","start","Promise","resolve","reject","hostname","listen","port","socket","logger","info","stop","close","cause","message","origin","getHeader","set","dispose","call","transport","chunks","onData","chunk","isLast","push","concat"],"mappings":"AAAA,SAEEA,QAAQ,EAIRC,KAAK,EAELC,WAAW,QACN,qBAAoB;AAC3B,SAASC,aAAa,QAAQ,gBAAe;AAC7C,SACEC,GAAG,EAGHC,MAAM,QAED,iBAAgB;AACvB,SAASC,cAAc,QAAQ,mBAAkB;AAEjD,SAASC,aAAa,EAAEC,SAAS,EAAEC,cAAc,QAAQ,aAAY;AAErE,OAAO,MAAMC;;;IACDC,OAAqB;IACZC,cAAkC;IAErDC,YACE,AAAmBC,WAA+B,EAClD,AAAmBC,OAA6B,CAChD;aAFmBD,cAAAA;aACAC,UAAAA;aAJFH,gBAAgBT,cAAca,IAAI;QAMnD,IAAI,CAACL,MAAM,GAAG,IAAI,CAACI,OAAO,CAACE,GAAG,GAAGZ,OAAOU,QAAQE,GAAG,IAAKb;QAExD,IAAI,CAACO,MAAM,CACRI,OAAO,CAAC,MAAM,CAACG,KAAKC;YACnB,IAAI,CAACC,SAAS,CAACF,KAAKC;YACpBD,IAAIG,WAAW,CAAC;YAChBH,IAAII,cAAc;QACpB,GACCC,GAAG,CAAC,YAAY,CAACL,KAA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CtF,IAAIgD,OAAO,CAACwC,GAAG,CAAC,gCAAgC;QAChDxF,IAAIgD,OAAO,CAACwC,GAAG,CAAC,gCAAgC;QAChDxF,IAAIgD,OAAO,CAACwC,GAAG,CAAC,oCAAoC;IACtD;IAEUjB,wBAAwBxC,SAAoB,EAAE;QACtDA,UAAU0D,OAAO;IACnB;IAEA,MAAgB7B,UAAU/D,OAOzB,EAAE;QACD,OAAO,MAAM,IAAI,CAAC4D,GAAG,CAACiC,IAAI,CAAC;YACzB,GAAG7F,OAAO;YACV8F,WAAW,IAAI,CAACjG,aAAa;QAC/B;IACF;IAEA,MAAgBoC,QAAQ9B,GAAiB,EAAE;QACzC,OAAO,IAAIyE,QAAgB,CAACC;YAC1B,MAAMkB,SAAmB,EAAE;YAC3B5F,IAAI6F,MAAM,CAAC,CAACC,OAAOC;gBACjBH,OAAOI,IAAI,CAAC9C,OAAOC,IAAI,CAAC2C;gBACxB,IAAIC,QAAQ;oBACVrB,QAAQxB,OAAO+C,MAAM,CAACL;gBACxB;YACF;QACF;IACF;AACF"}
1
+ {"version":3,"sources":["../../../lib/server.ts"],"sourcesContent":["import {\n type AnyProcedure,\n ApiError,\n type ApplicationContext,\n type Connection,\n type Container,\n Scope,\n type Service,\n injectables,\n} from '@nmtjs/application'\nimport { TransportType } from '@nmtjs/common'\nimport {\n App,\n type HttpRequest,\n type HttpResponse,\n SSLApp,\n type TemplatedApp,\n} from 'uWebSockets.js'\nimport { connectionData } from './injectables.ts'\nimport type { HttpTransportOptions } from './types.ts'\nimport { InternalError, getFormat, getRequestData } from './utils.ts'\n\nexport class HttpTransportServer {\n protected server!: TemplatedApp\n protected readonly transportType = TransportType.HTTP\n\n constructor(\n protected readonly application: ApplicationContext,\n protected readonly options: HttpTransportOptions,\n ) {\n this.server = this.options.tls ? SSLApp(options.tls!) : App()\n\n this.server\n .options('/*', (res, req) => {\n this.applyCors(res, req)\n res.writeStatus('200 OK')\n res.endWithoutBody()\n })\n .get('/healthy', (res, req) => {\n this.applyCors(res, req)\n res.writeHeader('Content-Type', 'text/plain')\n res.end('OK')\n })\n .post('/api/:service/:procudure', async (res, req) => {\n const ac = new AbortController()\n res.onAborted(() => ac.abort())\n const tryEnd = (cb) => {\n if (!ac.signal.aborted) res.cork(cb)\n }\n\n try {\n this.applyCors(res, req)\n const requestData = getRequestData(req)\n\n const serviceName = req.getParameter(0)!\n const procedureName = req.getParameter(1)!\n\n const service = this.application.registry.services.get(serviceName)\n\n if (!service) throw new Error(`Service ${serviceName} not found`)\n if (this.transportType in service.contract.transports === false)\n throw new Error(`Service ${serviceName} not supported`)\n\n const format = getFormat(requestData, this.application.format)\n const body = await this.getBody(res)\n const container = this.application.container.createScope(Scope.Call)\n const payload = body.byteLength ? format.decoder.decode(body) : null\n const connection = this.application.connections.add({\n services: [serviceName],\n type: this.transportType,\n subscriptions: new Map(),\n })\n\n const responseHeaders = new Headers()\n\n container.provide(connectionData, {\n query: requestData.query,\n headers: requestData.headers,\n proxiedRemoteAddress: Buffer.from(\n res.getProxiedRemoteAddressAsText(),\n ).toString(),\n remoteAddress: Buffer.from(res.getRemoteAddressAsText()).toString(),\n responseHeaders,\n })\n container.provide(injectables.connection, connection)\n\n const { procedure } = this.api.find(\n serviceName,\n procedureName,\n this.transportType,\n )\n\n try {\n const response = await this.handleRPC({\n connection,\n service,\n procedure,\n container,\n signal: ac.signal,\n payload,\n })\n\n tryEnd(() => {\n res\n .writeStatus('200 OK')\n .writeHeader('Content-Type', format.encoder.contentType)\n responseHeaders.forEach((v, k) => res.writeHeader(k, v))\n res.end(format.encoder.encode({ error: null, result: response }))\n })\n } catch (error: any) {\n if (error instanceof ApiError) {\n tryEnd(() =>\n res\n .writeStatus('200 OK')\n .end(format.encoder.encode({ error, result: null })),\n )\n } else {\n tryEnd(() =>\n res.writeStatus('200 OK').end(\n format.encoder.encode({\n error: InternalError(),\n result: null,\n }),\n ),\n )\n }\n this.logError(error)\n } finally {\n this.application.connections.remove(connection)\n this.handleContainerDisposal(container)\n }\n } catch (error: any) {\n this.logError(error)\n tryEnd(() =>\n res.writeStatus('500 Internal Server Error').endWithoutBody(),\n )\n }\n })\n }\n\n async start() {\n return new Promise<void>((resolve, reject) => {\n const hostname = this.options.hostname ?? '127.0.0.1'\n this.server.listen(hostname, this.options.port!, (socket) => {\n if (socket) {\n this.logger.info(\n 'Server started on %s:%s',\n hostname,\n this.options.port!,\n )\n resolve()\n } else {\n reject(new Error('Failed to start server'))\n }\n })\n })\n }\n\n async stop() {\n this.server.close()\n }\n\n protected get api() {\n return this.application.api\n }\n\n protected get logger() {\n return this.application.logger\n }\n\n protected async logError(\n cause: Error,\n message = 'Unknown error while processing request',\n ) {\n this.logger.error(new Error(message, { cause }))\n }\n\n protected applyCors(res: HttpResponse, req: HttpRequest) {\n // TODO: this should be configurable\n const origin = req.getHeader('origin')\n if (!origin) return\n res.writeHeader('Access-Control-Allow-Origin', origin)\n res.writeHeader('Access-Control-Allow-Headers', 'Content-Type')\n res.writeHeader('Access-Control-Allow-Methods', 'GET, POST')\n res.writeHeader('Access-Control-Allow-Credentials', 'true')\n }\n\n protected handleContainerDisposal(container: Container) {\n container.dispose()\n }\n\n protected async handleRPC(options: {\n connection: Connection\n service: Service\n procedure: AnyProcedure\n container: Container\n signal: AbortSignal\n payload: any\n }) {\n return await this.api.call({\n ...options,\n transport: this.transportType,\n })\n }\n\n protected async getBody(res: HttpResponse) {\n return new Promise<Buffer>((resolve) => {\n const chunks: Buffer[] = []\n res.onData((chunk, isLast) => {\n chunks.push(Buffer.from(chunk))\n if (isLast) {\n resolve(Buffer.concat(chunks))\n }\n })\n })\n }\n}\n"],"names":["ApiError","Scope","injectables","TransportType","App","SSLApp","connectionData","InternalError","getFormat","getRequestData","HttpTransportServer","server","transportType","constructor","application","options","HTTP","tls","res","req","applyCors","writeStatus","endWithoutBody","get","writeHeader","end","post","ac","AbortController","onAborted","abort","tryEnd","cb","signal","aborted","cork","requestData","serviceName","getParameter","procedureName","service","registry","services","Error","contract","transports","format","body","getBody","container","createScope","Call","payload","byteLength","decoder","decode","connection","connections","add","type","subscriptions","Map","responseHeaders","Headers","provide","query","headers","proxiedRemoteAddress","Buffer","from","getProxiedRemoteAddressAsText","toString","remoteAddress","getRemoteAddressAsText","procedure","api","find","response","handleRPC","encoder","contentType","forEach","v","k","encode","error","result","logError","remove","handleContainerDisposal","start","Promise","resolve","reject","hostname","listen","port","socket","logger","info","stop","close","cause","message","origin","getHeader","dispose","call","transport","chunks","onData","chunk","isLast","push","concat"],"mappings":"AAAA,SAEEA,QAAQ,EAIRC,KAAK,EAELC,WAAW,QACN,qBAAoB;AAC3B,SAASC,aAAa,QAAQ,gBAAe;AAC7C,SACEC,GAAG,EAGHC,MAAM,QAED,iBAAgB;AACvB,SAASC,cAAc,QAAQ,mBAAkB;AAEjD,SAASC,aAAa,EAAEC,SAAS,EAAEC,cAAc,QAAQ,aAAY;AAErE,OAAO,MAAMC;;;IACDC,OAAqB;IACZC,cAAkC;IAErDC,YACE,AAAmBC,WAA+B,EAClD,AAAmBC,OAA6B,CAChD;aAFmBD,cAAAA;aACAC,UAAAA;aAJFH,gBAAgBT,cAAca,IAAI;QAMnD,IAAI,CAACL,MAAM,GAAG,IAAI,CAACI,OAAO,CAACE,GAAG,GAAGZ,OAAOU,QAAQE,GAAG,IAAKb;QAExD,IAAI,CAACO,MAAM,CACRI,OAAO,CAAC,MAAM,CAACG,KAAKC;YACnB,IAAI,CAACC,SAAS,CAACF,KAAKC;YACpBD,IAAIG,WAAW,CAAC;YAChBH,IAAII,cAAc;QACpB,GACCC,GAAG,CAAC,YAAY,CAACL,KAAKC;YACrB,IAAI,CAACC,SAAS,CAACF,KAAKC;YACpBD,IAAIM,WAAW,CAAC,gBAAgB;YAChCN,IAAIO,GAAG,CAAC;QACV,GACCC,IAAI,CAAC,4BAA4B,OAAOR,KAAKC;YAC5C,MAAMQ,KAAK,IAAIC;YACfV,IAAIW,SAAS,CAAC,IAAMF,GAAGG,KAAK;YAC5B,MAAMC,SA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AAG5F,OAAO;YACV6F,WAAW,IAAI,CAAChG,aAAa;QAC/B;IACF;IAEA,MAAgBoC,QAAQ9B,GAAiB,EAAE;QACzC,OAAO,IAAIyE,QAAgB,CAACC;YAC1B,MAAMiB,SAAmB,EAAE;YAC3B3F,IAAI4F,MAAM,CAAC,CAACC,OAAOC;gBACjBH,OAAOI,IAAI,CAAC7C,OAAOC,IAAI,CAAC0C;gBACxB,IAAIC,QAAQ;oBACVpB,QAAQxB,OAAO8C,MAAM,CAACL;gBACxB;YACF;QACF;IACF;AACF"}
package/lib/server.ts CHANGED
@@ -45,14 +45,11 @@ export class HttpTransportServer {
45
45
  const ac = new AbortController()
46
46
  res.onAborted(() => ac.abort())
47
47
  const tryEnd = (cb) => {
48
- if (!ac.signal.aborted)
49
- res.cork(() => {
50
- this.applyCors(res, req)
51
- return cb()
52
- })
48
+ if (!ac.signal.aborted) res.cork(cb)
53
49
  }
54
50
 
55
51
  try {
52
+ this.applyCors(res, req)
56
53
  const requestData = getRequestData(req)
57
54
 
58
55
  const serviceName = req.getParameter(0)!
@@ -182,10 +179,10 @@ export class HttpTransportServer {
182
179
  // TODO: this should be configurable
183
180
  const origin = req.getHeader('origin')
184
181
  if (!origin) return
185
- res.headers.set('Access-Control-Allow-Origin', origin)
186
- res.headers.set('Access-Control-Allow-Headers', 'Content-Type')
187
- res.headers.set('Access-Control-Allow-Methods', 'GET, POST')
188
- res.headers.set('Access-Control-Allow-Credentials', 'true')
182
+ res.writeHeader('Access-Control-Allow-Origin', origin)
183
+ res.writeHeader('Access-Control-Allow-Headers', 'Content-Type')
184
+ res.writeHeader('Access-Control-Allow-Methods', 'GET, POST')
185
+ res.writeHeader('Access-Control-Allow-Credentials', 'true')
189
186
  }
190
187
 
191
188
  protected handleContainerDisposal(container: Container) {
package/package.json CHANGED
@@ -30,7 +30,7 @@
30
30
  "LICENSE.md",
31
31
  "README.md"
32
32
  ],
33
- "version": "0.1.1",
33
+ "version": "0.1.4",
34
34
  "scripts": {
35
35
  "build": "neemata-build ./index.ts './lib/**/*.ts'",
36
36
  "type-check": "tsc --noEmit"