nats
Version:
Node.js client for NATS, a lightweight, high-performance cloud native messaging system
222 lines • 6.18 kB
JavaScript
"use strict";
/*
* Copyright 2021 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.isConsumerOptsBuilder = exports.ConsumerOptsBuilderImpl = exports.consumerOpts = void 0;
const types_1 = require("./types");
const jsutil_1 = require("./jsutil");
function consumerOpts(opts) {
return new ConsumerOptsBuilderImpl(opts);
}
exports.consumerOpts = consumerOpts;
// FIXME: some items here that may need to be addressed
// 503s?
// maxRetries()
// retryBackoff()
// ackWait(time)
// replayOriginal()
// rateLimit(bytesPerSec)
class ConsumerOptsBuilderImpl {
constructor(opts) {
this.stream = "";
this.mack = false;
this.ordered = false;
this.config = (0, jsutil_1.defaultConsumer)("", opts || {});
}
getOpts() {
const o = {};
o.config = this.config;
o.mack = this.mack;
o.stream = this.stream;
o.callbackFn = this.callbackFn;
o.max = this.max;
o.queue = this.qname;
o.ordered = this.ordered;
o.config.ack_policy = o.ordered ? types_1.AckPolicy.None : o.config.ack_policy;
o.isBind = o.isBind || false;
return o;
}
description(description) {
this.config.description = description;
return this;
}
deliverTo(subject) {
this.config.deliver_subject = subject;
return this;
}
durable(name) {
(0, jsutil_1.validateDurableName)(name);
this.config.durable_name = name;
return this;
}
startSequence(seq) {
if (seq <= 0) {
throw new Error("sequence must be greater than 0");
}
this.config.deliver_policy = types_1.DeliverPolicy.StartSequence;
this.config.opt_start_seq = seq;
return this;
}
startTime(time) {
this.config.deliver_policy = types_1.DeliverPolicy.StartTime;
this.config.opt_start_time = time.toISOString();
return this;
}
deliverAll() {
this.config.deliver_policy = types_1.DeliverPolicy.All;
return this;
}
deliverLastPerSubject() {
this.config.deliver_policy = types_1.DeliverPolicy.LastPerSubject;
return this;
}
deliverLast() {
this.config.deliver_policy = types_1.DeliverPolicy.Last;
return this;
}
deliverNew() {
this.config.deliver_policy = types_1.DeliverPolicy.New;
return this;
}
startAtTimeDelta(millis) {
this.startTime(new Date(Date.now() - millis));
return this;
}
headersOnly() {
this.config.headers_only = true;
return this;
}
ackNone() {
this.config.ack_policy = types_1.AckPolicy.None;
return this;
}
ackAll() {
this.config.ack_policy = types_1.AckPolicy.All;
return this;
}
ackExplicit() {
this.config.ack_policy = types_1.AckPolicy.Explicit;
return this;
}
ackWait(millis) {
this.config.ack_wait = (0, jsutil_1.nanos)(millis);
return this;
}
maxDeliver(max) {
this.config.max_deliver = max;
return this;
}
filterSubject(s) {
this.config.filter_subject = s;
return this;
}
replayInstantly() {
this.config.replay_policy = types_1.ReplayPolicy.Instant;
return this;
}
replayOriginal() {
this.config.replay_policy = types_1.ReplayPolicy.Original;
return this;
}
sample(n) {
n = Math.trunc(n);
if (n < 0 || n > 100) {
throw new Error(`value must be between 0-100`);
}
this.config.sample_freq = `${n}%`;
return this;
}
limit(n) {
this.config.rate_limit_bps = n;
return this;
}
maxWaiting(max) {
this.config.max_waiting = max;
return this;
}
maxAckPending(max) {
this.config.max_ack_pending = max;
return this;
}
idleHeartbeat(millis) {
this.config.idle_heartbeat = (0, jsutil_1.nanos)(millis);
return this;
}
flowControl() {
this.config.flow_control = true;
return this;
}
deliverGroup(name) {
this.queue(name);
return this;
}
manualAck() {
this.mack = true;
return this;
}
maxMessages(max) {
this.max = max;
return this;
}
callback(fn) {
this.callbackFn = fn;
return this;
}
queue(n) {
this.qname = n;
this.config.deliver_group = n;
return this;
}
orderedConsumer() {
this.ordered = true;
return this;
}
bind(stream, durable) {
this.stream = stream;
this.config.durable_name = durable;
this.isBind = true;
return this;
}
bindStream(stream) {
this.stream = stream;
return this;
}
inactiveEphemeralThreshold(millis) {
this.config.inactive_threshold = (0, jsutil_1.nanos)(millis);
return this;
}
maxPullBatch(n) {
this.config.max_batch = n;
return this;
}
maxPullRequestExpires(millis) {
this.config.max_expires = (0, jsutil_1.nanos)(millis);
return this;
}
memory() {
this.config.mem_storage = true;
return this;
}
numReplicas(n) {
this.config.num_replicas = n;
return this;
}
}
exports.ConsumerOptsBuilderImpl = ConsumerOptsBuilderImpl;
function isConsumerOptsBuilder(o) {
return typeof o.getOpts === "function";
}
exports.isConsumerOptsBuilder = isConsumerOptsBuilder;
//# sourceMappingURL=jsconsumeropts.js.map