UNPKG

6 kBJavaScriptView Raw
1"use strict";
2var __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}));
13var __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});
18var __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};
25Object.defineProperty(exports, "__esModule", { value: true });
26exports.expiryToTimestamp = exports.cbQsStringify = exports.nsServerStrToDuraLevel = exports.duraLevelToNsServerStr = exports.CompoundTimeout = exports.PromiseHelper = void 0;
27const generaltypes_1 = require("./generaltypes");
28const errors_1 = require("./errors");
29const qs = __importStar(require("querystring"));
30/**
31 * @internal
32 */
33class PromiseHelper {
34 /**
35 * @internal
36 */
37 static wrapAsync(fn, callback) {
38 // If a callback in in use, we wrap the promise with a handler which
39 // forwards to the callback and return undefined. If there is no
40 // callback specified. We directly return the promise.
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 * @internal
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}
72exports.PromiseHelper = PromiseHelper;
73/**
74 * @internal
75 */
76class CompoundTimeout {
77 /**
78 * @internal
79 */
80 constructor(timeout) {
81 this._start = process.hrtime();
82 this._timeout = timeout;
83 }
84 /**
85 * @internal
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 * @internal
100 */
101 expired() {
102 const timeLeft = this.left();
103 if (timeLeft === undefined) {
104 return false;
105 }
106 return timeLeft <= 0;
107 }
108}
109exports.CompoundTimeout = CompoundTimeout;
110/**
111 * @internal
112 */
113function 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}
136exports.duraLevelToNsServerStr = duraLevelToNsServerStr;
137/**
138 * @internal
139 */
140function 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}
160exports.nsServerStrToDuraLevel = nsServerStrToDuraLevel;
161/**
162 * @internal
163 */
164function cbQsStringify(values, options) {
165 const cbValues = {};
166 for (const i in values) {
167 if (values[i] === undefined) {
168 // skipped
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}
184exports.cbQsStringify = cbQsStringify;
185const thirtyDaysInSeconds = 30 * 24 * 60 * 60;
186/**
187 * @internal
188 */
189function 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}
201exports.expiryToTimestamp = expiryToTimestamp;