@nmtjs/http-transport 0.1.0 → 0.1.3
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/lib/server.js +20 -10
- package/dist/lib/server.js.map +1 -1
- package/lib/server.ts +25 -11
- package/package.json +1 -1
package/dist/lib/server.js
CHANGED
|
@@ -13,19 +13,22 @@ export class HttpTransportServer {
|
|
|
13
13
|
this.options = options;
|
|
14
14
|
this.transportType = TransportType.HTTP;
|
|
15
15
|
this.server = this.options.tls ? SSLApp(options.tls) : App();
|
|
16
|
-
this.server.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
this.server.options('/*', (res, req)=>{
|
|
17
|
+
this.applyCors(res, req);
|
|
18
|
+
res.writeStatus('200 OK');
|
|
19
|
+
res.endWithoutBody();
|
|
20
|
+
}).get('/healthy', (res, req)=>{
|
|
21
|
+
this.applyCors(res, req);
|
|
22
|
+
res.writeHeader('Content-Type', 'text/plain');
|
|
23
|
+
res.end('OK');
|
|
24
24
|
}).post('/api/:service/:procudure', async (res, req)=>{
|
|
25
25
|
const ac = new AbortController();
|
|
26
26
|
res.onAborted(()=>ac.abort());
|
|
27
27
|
const tryEnd = (cb)=>{
|
|
28
|
-
if (!ac.signal.aborted) res.cork(
|
|
28
|
+
if (!ac.signal.aborted) res.cork(()=>{
|
|
29
|
+
this.applyCors(res, req);
|
|
30
|
+
return cb();
|
|
31
|
+
});
|
|
29
32
|
};
|
|
30
33
|
try {
|
|
31
34
|
const requestData = getRequestData(req);
|
|
@@ -54,7 +57,6 @@ export class HttpTransportServer {
|
|
|
54
57
|
responseHeaders
|
|
55
58
|
});
|
|
56
59
|
container.provide(injectables.connection, connection);
|
|
57
|
-
container.provide(injectables.callSignal, ac.signal);
|
|
58
60
|
const { procedure } = this.api.find(serviceName, procedureName, this.transportType);
|
|
59
61
|
try {
|
|
60
62
|
const response = await this.handleRPC({
|
|
@@ -123,6 +125,14 @@ export class HttpTransportServer {
|
|
|
123
125
|
cause
|
|
124
126
|
}));
|
|
125
127
|
}
|
|
128
|
+
applyCors(res, req) {
|
|
129
|
+
const origin = req.getHeader('origin');
|
|
130
|
+
if (!origin) return;
|
|
131
|
+
res.writeHeader('Access-Control-Allow-Origin', origin);
|
|
132
|
+
res.writeHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
133
|
+
res.writeHeader('Access-Control-Allow-Methods', 'GET, POST');
|
|
134
|
+
res.writeHeader('Access-Control-Allow-Credentials', 'true');
|
|
135
|
+
}
|
|
126
136
|
handleContainerDisposal(container) {
|
|
127
137
|
container.dispose();
|
|
128
138
|
}
|
package/dist/lib/server.js.map
CHANGED
|
@@ -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 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 .get('/healthy', (res) => {\n res.cork(() => {\n // cors\n res.writeHeader('Access-Control-Allow-Origin', '*')\n res.writeHeader('Access-Control-Allow-Headers', 'Content-Type')\n res.writeHeader('Access-Control-Allow-Methods', 'GET')\n res.writeHeader('Content-Type', 'text/plain')\n res.end('OK')\n })\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 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 container.provide(injectables.callSignal, ac.signal)\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 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","get","res","cork","writeHeader","end","post","req","ac","AbortController","onAborted","abort","tryEnd","cb","signal","aborted","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","callSignal","procedure","api","find","response","handleRPC","writeStatus","encoder","contentType","forEach","v","k","encode","error","result","logError","remove","handleContainerDisposal","endWithoutBody","start","Promise","resolve","reject","hostname","listen","port","socket","logger","info","stop","close","cause","message","dispose","call","transport","chunks","onData","chunk","isLast","push","concat"],"mappings":"AAAA,SAEEA,QAAQ,EAIRC,KAAK,EAELC,W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AC/E,OAAO,CAACiF,IAAI;oBAEnBJ;gBACF,OAAO;oBACLC,OAAO,IAAIrD,MAAM;gBACnB;YACF;QACF;IACF;IAEA,MAAM4D,OAAO;QACX,IAAI,CAACzF,MAAM,CAAC0F,KAAK;IACnB;IAEA,IAAc5B,MAAM;QAClB,OAAO,IAAI,CAAC3D,WAAW,CAAC2D,GAAG;IAC7B;IAEA,IAAcyB,SAAS;QACrB,OAAO,IAAI,CAACpF,WAAW,CAACoF,MAAM;IAChC;IAEA,MAAgBZ,SACdgB,KAAY,EACZC,UAAU,wCAAwC,EAClD;QACA,IAAI,CAACL,MAAM,CAACd,KAAK,CAAC,IAAI5C,MAAM+D,SAAS;YAAED;QAAM;IAC/C;IAEUd,wBAAwB1C,SAAoB,EAAE;QACtDA,UAAU0D,OAAO;IACnB;IAEA,MAAgB5B,UAAU7D,OAOzB,EAAE;QACD,OAAO,MAAM,IAAI,CAAC0D,GAAG,CAACgC,IAAI,CAAC;YACzB,GAAG1F,OAAO;YACV2F,WAAW,IAAI,CAAC9F,aAAa;QAC/B;IACF;IAEA,MAAgBiC,QAAQ1B,GAAiB,EAAE;QACzC,OAAO,IAAIwE,QAAgB,CAACC;YAC1B,MAAMe,SAAmB,EAAE;YAC3BxF,IAAIyF,MAAM,CAAC,CAACC,OAAOC;gBACjBH,OAAOI,IAAI,CAAC9C,OAAOC,IAAI,CAAC2C;gBACxB,IAAIC,QAAQ;oBACVlB,QAAQ3B,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)\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.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,IAAI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AAI,CAACD,QAAQ;QACbtF,IAAIM,WAAW,CAAC,+BAA+BgF;QAC/CtF,IAAIM,WAAW,CAAC,gCAAgC;QAChDN,IAAIM,WAAW,CAAC,gCAAgC;QAChDN,IAAIM,WAAW,CAAC,oCAAoC;IACtD;IAEUiE,wBAAwBxC,SAAoB,EAAE;QACtDA,UAAUyD,OAAO;IACnB;IAEA,MAAgB5B,UAAU/D,OAOzB,EAAE;QACD,OAAO,MAAM,IAAI,CAAC4D,GAAG,CAACgC,IAAI,CAAC;YACzB,GAAG5F,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
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
import { TransportType } from '@nmtjs/common'
|
|
12
12
|
import {
|
|
13
13
|
App,
|
|
14
|
+
type HttpRequest,
|
|
14
15
|
type HttpResponse,
|
|
15
16
|
SSLApp,
|
|
16
17
|
type TemplatedApp,
|
|
@@ -30,21 +31,25 @@ export class HttpTransportServer {
|
|
|
30
31
|
this.server = this.options.tls ? SSLApp(options.tls!) : App()
|
|
31
32
|
|
|
32
33
|
this.server
|
|
33
|
-
.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
.options('/*', (res, req) => {
|
|
35
|
+
this.applyCors(res, req)
|
|
36
|
+
res.writeStatus('200 OK')
|
|
37
|
+
res.endWithoutBody()
|
|
38
|
+
})
|
|
39
|
+
.get('/healthy', (res, req) => {
|
|
40
|
+
this.applyCors(res, req)
|
|
41
|
+
res.writeHeader('Content-Type', 'text/plain')
|
|
42
|
+
res.end('OK')
|
|
42
43
|
})
|
|
43
44
|
.post('/api/:service/:procudure', async (res, req) => {
|
|
44
45
|
const ac = new AbortController()
|
|
45
46
|
res.onAborted(() => ac.abort())
|
|
46
47
|
const tryEnd = (cb) => {
|
|
47
|
-
if (!ac.signal.aborted)
|
|
48
|
+
if (!ac.signal.aborted)
|
|
49
|
+
res.cork(() => {
|
|
50
|
+
this.applyCors(res, req)
|
|
51
|
+
return cb()
|
|
52
|
+
})
|
|
48
53
|
}
|
|
49
54
|
|
|
50
55
|
try {
|
|
@@ -81,7 +86,6 @@ export class HttpTransportServer {
|
|
|
81
86
|
responseHeaders,
|
|
82
87
|
})
|
|
83
88
|
container.provide(injectables.connection, connection)
|
|
84
|
-
container.provide(injectables.callSignal, ac.signal)
|
|
85
89
|
|
|
86
90
|
const { procedure } = this.api.find(
|
|
87
91
|
serviceName,
|
|
@@ -174,6 +178,16 @@ export class HttpTransportServer {
|
|
|
174
178
|
this.logger.error(new Error(message, { cause }))
|
|
175
179
|
}
|
|
176
180
|
|
|
181
|
+
protected applyCors(res: HttpResponse, req: HttpRequest) {
|
|
182
|
+
// TODO: this should be configurable
|
|
183
|
+
const origin = req.getHeader('origin')
|
|
184
|
+
if (!origin) return
|
|
185
|
+
res.writeHeader('Access-Control-Allow-Origin', origin)
|
|
186
|
+
res.writeHeader('Access-Control-Allow-Headers', 'Content-Type')
|
|
187
|
+
res.writeHeader('Access-Control-Allow-Methods', 'GET, POST')
|
|
188
|
+
res.writeHeader('Access-Control-Allow-Credentials', 'true')
|
|
189
|
+
}
|
|
190
|
+
|
|
177
191
|
protected handleContainerDisposal(container: Container) {
|
|
178
192
|
container.dispose()
|
|
179
193
|
}
|