nats-micro
Version:
NATS micro compatible extra-lightweight microservice library
46 lines • 1.2 kB
JavaScript
import { noResponse, } from './types/index.js';
import { randomId } from './utils/misc.js';
export class ResponseImpl {
constructor() {
this.resData = {
data: noResponse,
headers: [],
};
this.id = randomId();
this._isClosed = false;
}
get closeWaiter() {
return new Promise((closedRes) => {
this.closeCallback = closedRes;
if (this.isClosed)
closedRes(this.resData);
});
}
get isClosed() {
return this._isClosed;
}
setHeaders(responseHeaders) {
this.resData.headers = [];
this.resData.headers.push(...Array.from(responseHeaders));
}
send(responseData) {
if (this.isClosed)
return;
this.resData.data = responseData;
this._close();
}
sendNoResponse() {
if (this.isClosed)
return;
this.resData.data = noResponse;
this._close();
}
_close() {
if (this._isClosed)
return;
this._isClosed = true;
if (this.closeCallback)
this.closeCallback(this.resData);
}
}
//# sourceMappingURL=response.js.map