UNPKG

nats

Version:

Node.js client for NATS, a lightweight, high-performance cloud native messaging system

84 lines 3.51 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectMsgImpl = exports.DirectStreamAPIImpl = void 0; /* * Copyright 2022 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. */ const jsbaseclient_api_1 = require("./jsbaseclient_api"); const types_1 = require("./types"); const jsutil_1 = require("./jsutil"); class DirectStreamAPIImpl extends jsbaseclient_api_1.BaseApiClient { constructor(nc, opts) { super(nc, opts); } getMessage(stream, query) { return __awaiter(this, void 0, void 0, function* () { (0, jsutil_1.validateStreamName)(stream); // if doing a last_by_subj request, we append the subject // this allows last_by_subj to be subject to permissions (KV) let qq = query; const { last_by_subj } = qq; if (last_by_subj) { qq = null; } const payload = qq ? this.jc.encode(qq) : types_1.Empty; const pre = this.opts.apiPrefix || "$JS.API"; const subj = last_by_subj ? `${pre}.DIRECT.GET.${stream}.${last_by_subj}` : `${pre}.DIRECT.GET.${stream}`; const r = yield this.nc.request(subj, payload); // response is not a JS.API response const err = (0, jsutil_1.checkJsError)(r); if (err) { return Promise.reject(err); } const dm = new DirectMsgImpl(r); return Promise.resolve(dm); }); } } exports.DirectStreamAPIImpl = DirectStreamAPIImpl; class DirectMsgImpl { constructor(m) { if (!m.headers) { throw new Error("headers expected"); } this.data = m.data; this.header = m.headers; } get subject() { return this.header.get(types_1.DirectMsgHeaders.Subject); } get seq() { const v = this.header.get(types_1.DirectMsgHeaders.Sequence); return typeof v === "string" ? parseInt(v) : 0; } get time() { return new Date(Date.parse(this.header.get(types_1.DirectMsgHeaders.TimeStamp))); } get stream() { return this.header.get(types_1.DirectMsgHeaders.Stream); } } exports.DirectMsgImpl = DirectMsgImpl; //# sourceMappingURL=jsmdirect_api.js.map