1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
5 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6 | desc = { enumerable: true, get: function() { return m[k]; } };
|
7 | }
|
8 | Object.defineProperty(o, k2, desc);
|
9 | }) : (function(o, m, k, k2) {
|
10 | if (k2 === undefined) k2 = k;
|
11 | o[k2] = m[k];
|
12 | }));
|
13 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15 | }) : function(o, v) {
|
16 | o["default"] = v;
|
17 | });
|
18 | var __importStar = (this && this.__importStar) || function (mod) {
|
19 | if (mod && mod.__esModule) return mod;
|
20 | var result = {};
|
21 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22 | __setModuleDefault(result, mod);
|
23 | return result;
|
24 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | exports.expiryToTimestamp = exports.cbQsStringify = exports.nsServerStrToDuraLevel = exports.duraLevelToNsServerStr = exports.CompoundTimeout = exports.PromiseHelper = void 0;
|
27 | const generaltypes_1 = require("./generaltypes");
|
28 | const errors_1 = require("./errors");
|
29 | const qs = __importStar(require("querystring"));
|
30 |
|
31 |
|
32 |
|
33 | class PromiseHelper {
|
34 | |
35 |
|
36 |
|
37 | static wrapAsync(fn, callback) {
|
38 |
|
39 |
|
40 |
|
41 | if (callback) {
|
42 | const prom = fn();
|
43 | prom
|
44 | .then((res) => callback(null, res))
|
45 | .catch((err) => callback(err, null));
|
46 | return prom;
|
47 | }
|
48 | return fn();
|
49 | }
|
50 | |
51 |
|
52 |
|
53 | static wrap(fn, callback) {
|
54 | const prom = new Promise((resolve, reject) => {
|
55 | fn((err, res) => {
|
56 | if (err) {
|
57 | reject(err);
|
58 | }
|
59 | else {
|
60 | resolve(res);
|
61 | }
|
62 | });
|
63 | });
|
64 | if (callback) {
|
65 | prom
|
66 | .then((res) => callback(null, res))
|
67 | .catch((err) => callback(err, null));
|
68 | }
|
69 | return prom;
|
70 | }
|
71 | }
|
72 | exports.PromiseHelper = PromiseHelper;
|
73 |
|
74 |
|
75 |
|
76 | class CompoundTimeout {
|
77 | |
78 |
|
79 |
|
80 | constructor(timeout) {
|
81 | this._start = process.hrtime();
|
82 | this._timeout = timeout;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | left() {
|
88 | if (this._timeout === undefined) {
|
89 | return undefined;
|
90 | }
|
91 | const period = process.hrtime(this._start);
|
92 | const periodMs = period[0] * 1e3 + period[1] / 1e6;
|
93 | if (periodMs > this._timeout) {
|
94 | return 0;
|
95 | }
|
96 | return this._timeout - periodMs;
|
97 | }
|
98 | |
99 |
|
100 |
|
101 | expired() {
|
102 | const timeLeft = this.left();
|
103 | if (timeLeft === undefined) {
|
104 | return false;
|
105 | }
|
106 | return timeLeft <= 0;
|
107 | }
|
108 | }
|
109 | exports.CompoundTimeout = CompoundTimeout;
|
110 |
|
111 |
|
112 |
|
113 | function duraLevelToNsServerStr(level) {
|
114 | if (level === undefined) {
|
115 | return undefined;
|
116 | }
|
117 | if (typeof level === 'string') {
|
118 | return level;
|
119 | }
|
120 | if (level === generaltypes_1.DurabilityLevel.None) {
|
121 | return 'none';
|
122 | }
|
123 | else if (level === generaltypes_1.DurabilityLevel.Majority) {
|
124 | return 'majority';
|
125 | }
|
126 | else if (level === generaltypes_1.DurabilityLevel.MajorityAndPersistOnMaster) {
|
127 | return 'majorityAndPersistActive';
|
128 | }
|
129 | else if (level === generaltypes_1.DurabilityLevel.PersistToMajority) {
|
130 | return 'persistToMajority';
|
131 | }
|
132 | else {
|
133 | throw new Error('invalid durability level specified');
|
134 | }
|
135 | }
|
136 | exports.duraLevelToNsServerStr = duraLevelToNsServerStr;
|
137 |
|
138 |
|
139 |
|
140 | function nsServerStrToDuraLevel(level) {
|
141 | if (level === undefined) {
|
142 | return generaltypes_1.DurabilityLevel.None;
|
143 | }
|
144 | if (level === 'none') {
|
145 | return generaltypes_1.DurabilityLevel.None;
|
146 | }
|
147 | else if (level === 'majority') {
|
148 | return generaltypes_1.DurabilityLevel.Majority;
|
149 | }
|
150 | else if (level === 'majorityAndPersistActive') {
|
151 | return generaltypes_1.DurabilityLevel.MajorityAndPersistOnMaster;
|
152 | }
|
153 | else if (level === 'persistToMajority') {
|
154 | return generaltypes_1.DurabilityLevel.PersistToMajority;
|
155 | }
|
156 | else {
|
157 | throw new Error('invalid durability level string');
|
158 | }
|
159 | }
|
160 | exports.nsServerStrToDuraLevel = nsServerStrToDuraLevel;
|
161 |
|
162 |
|
163 |
|
164 | function cbQsStringify(values, options) {
|
165 | const cbValues = {};
|
166 | for (const i in values) {
|
167 | if (values[i] === undefined) {
|
168 |
|
169 | }
|
170 | else if (typeof values[i] === 'boolean') {
|
171 | if (options && options.boolAsString) {
|
172 | cbValues[i] = values[i] ? 'true' : 'false';
|
173 | }
|
174 | else {
|
175 | cbValues[i] = values[i] ? 1 : 0;
|
176 | }
|
177 | }
|
178 | else {
|
179 | cbValues[i] = values[i];
|
180 | }
|
181 | }
|
182 | return qs.stringify(cbValues);
|
183 | }
|
184 | exports.cbQsStringify = cbQsStringify;
|
185 | const thirtyDaysInSeconds = 30 * 24 * 60 * 60;
|
186 |
|
187 |
|
188 |
|
189 | function expiryToTimestamp(expiry) {
|
190 | if (typeof expiry !== 'number') {
|
191 | throw new errors_1.InvalidArgumentError(new Error('Expected expiry to be a number.'));
|
192 | }
|
193 | if (expiry < 0) {
|
194 | throw new errors_1.InvalidArgumentError(new Error(`Expected expiry to be either zero (for no expiry) or greater but got ${expiry}.`));
|
195 | }
|
196 | if (expiry < thirtyDaysInSeconds) {
|
197 | return expiry;
|
198 | }
|
199 | return expiry + Math.floor(Date.now() / 1000);
|
200 | }
|
201 | exports.expiryToTimestamp = expiryToTimestamp;
|