nats-micro
Version:
NATS micro compatible extra-lightweight microservice library
50 lines • 1.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResponseImpl = void 0;
const index_js_1 = require("./types/index.js");
const misc_js_1 = require("./utils/misc.js");
class ResponseImpl {
constructor() {
this.resData = {
data: index_js_1.noResponse,
headers: [],
};
this.id = (0, misc_js_1.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 = index_js_1.noResponse;
this._close();
}
_close() {
if (this._isClosed)
return;
this._isClosed = true;
if (this.closeCallback)
this.closeCallback(this.resData);
}
}
exports.ResponseImpl = ResponseImpl;
//# sourceMappingURL=response.js.map