nats-micro
Version:
NATS micro compatible extra-lightweight microservice library
166 lines • 8.21 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryBroker = void 0;
const debug_js_1 = require("./debug.js");
const eventBucket_js_1 = require("./eventBucket.js");
const tokenEventEmitter_js_1 = require("./tokenEventEmitter.js");
const index_js_1 = require("./utils/index.js");
class InMemoryBroker {
constructor() {
this.ee = new tokenEventEmitter_js_1.TokenEventEmitter();
this.name = 'test';
this.clientId = InMemoryBroker.nextClientId++;
}
connect() {
return __awaiter(this, void 0, void 0, function* () {
// nothing to do
return this;
});
}
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
// nothing to do
});
}
createInbox() {
return `_INBOX.${Math.floor(Math.random() * 1e10)}`;
}
on(subject, listener,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
queue) {
this.ee.on((0, index_js_1.subjectToString)(subject), listener);
}
off(subject, listener) {
this.ee.off((0, index_js_1.subjectToString)(subject), listener);
}
offAll() {
this.ee.offAll();
}
send(subject, data, options) {
return __awaiter(this, void 0, void 0, function* () {
debug_js_1.debug.broker.debug(`Sending ${JSON.stringify(data)} to ${JSON.stringify(subject)}`);
this.ee.emit((0, index_js_1.subjectToString)(subject), {
data,
headers: (0, index_js_1.addThreadContextHeaders)(options === null || options === void 0 ? void 0 : options.headers),
replyTo: options === null || options === void 0 ? void 0 : options.replyTo,
});
});
}
requestMany(subject, data, options) {
var _a;
return __asyncGenerator(this, arguments, function* requestMany_1() {
var _b, e_1, _c, _d;
const inbox = this.createInbox();
debug_js_1.debug.broker.debug(`Requesting ${JSON.stringify(data)} from ${JSON.stringify(subject)}, reply to ${JSON.stringify(inbox)}`);
const bucket = (0, eventBucket_js_1.eventBucket)();
let responseCount = 0;
const responseLimit = (_a = options === null || options === void 0 ? void 0 : options.limit) !== null && _a !== void 0 ? _a : -1;
const responseHandler = (msg) => {
bucket.push(msg);
if (responseLimit > 0 && (++responseCount >= responseLimit))
// eslint-disable-next-line no-use-before-define
close();
};
const close = () => {
bucket.close();
this.off(inbox, responseHandler);
};
if (options === null || options === void 0 ? void 0 : options.timeout) {
setTimeout(close, options.timeout);
}
this.on(inbox, responseHandler);
this.send(subject, data, {
replyTo: inbox,
headers: (0, index_js_1.addThreadContextHeaders)(options === null || options === void 0 ? void 0 : options.headers),
});
try {
for (var _e = true, bucket_1 = __asyncValues(bucket), bucket_1_1; bucket_1_1 = yield __await(bucket_1.next()), _b = bucket_1_1.done, !_b;) {
_d = bucket_1_1.value;
_e = false;
try {
const item = _d;
if ('done' in item)
break;
else {
const error = item.value.headers
? (0, index_js_1.errorFromHeaders)(Array.from(item.value.headers))
: undefined;
if (error)
yield yield __await(Promise.resolve({
subject: inbox,
headers: item.value.headers,
error,
}));
else
yield yield __await(Promise.resolve({
subject: inbox,
data: item.value.data,
headers: item.value.headers,
}));
}
}
finally {
_e = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_e && !_b && (_c = bucket_1.return)) yield __await(_c.call(bucket_1));
}
finally { if (e_1) throw e_1.error; }
}
});
}
request(subject, data, options) {
return __awaiter(this, void 0, void 0, function* () {
const results = this.requestMany(subject, data, {
headers: options === null || options === void 0 ? void 0 : options.headers,
timeout: options === null || options === void 0 ? void 0 : options.timeout,
limit: 1,
});
const result = yield results.next();
if (!result.done) {
if (result.value.error)
throw result.value.error;
return result.value;
}
return {
data: undefined,
subject: '',
};
});
}
}
exports.InMemoryBroker = InMemoryBroker;
InMemoryBroker.nextClientId = 0;
//# sourceMappingURL=inMemoryBroker.js.map