UNPKG

3.35 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.deadlineToString = exports.getRelativeTimeout = exports.getDeadlineTimeoutString = exports.minDeadline = void 0;
20function minDeadline(...deadlineList) {
21 let minValue = Infinity;
22 for (const deadline of deadlineList) {
23 const deadlineMsecs = deadline instanceof Date ? deadline.getTime() : deadline;
24 if (deadlineMsecs < minValue) {
25 minValue = deadlineMsecs;
26 }
27 }
28 return minValue;
29}
30exports.minDeadline = minDeadline;
31const units = [
32 ['m', 1],
33 ['S', 1000],
34 ['M', 60 * 1000],
35 ['H', 60 * 60 * 1000],
36];
37function getDeadlineTimeoutString(deadline) {
38 const now = new Date().getTime();
39 if (deadline instanceof Date) {
40 deadline = deadline.getTime();
41 }
42 const timeoutMs = Math.max(deadline - now, 0);
43 for (const [unit, factor] of units) {
44 const amount = timeoutMs / factor;
45 if (amount < 1e8) {
46 return String(Math.ceil(amount)) + unit;
47 }
48 }
49 throw new Error('Deadline is too far in the future');
50}
51exports.getDeadlineTimeoutString = getDeadlineTimeoutString;
52/**
53 * See https://nodejs.org/api/timers.html#settimeoutcallback-delay-args
54 * In particular, "When delay is larger than 2147483647 or less than 1, the
55 * delay will be set to 1. Non-integer delays are truncated to an integer."
56 * This number of milliseconds is almost 25 days.
57 */
58const MAX_TIMEOUT_TIME = 2147483647;
59/**
60 * Get the timeout value that should be passed to setTimeout now for the timer
61 * to end at the deadline. For any deadline before now, the timer should end
62 * immediately, represented by a value of 0. For any deadline more than
63 * MAX_TIMEOUT_TIME milliseconds in the future, a timer cannot be set that will
64 * end at that time, so it is treated as infinitely far in the future.
65 * @param deadline
66 * @returns
67 */
68function getRelativeTimeout(deadline) {
69 const deadlineMs = deadline instanceof Date ? deadline.getTime() : deadline;
70 const now = new Date().getTime();
71 const timeout = deadlineMs - now;
72 if (timeout < 0) {
73 return 0;
74 }
75 else if (timeout > MAX_TIMEOUT_TIME) {
76 return Infinity;
77 }
78 else {
79 return timeout;
80 }
81}
82exports.getRelativeTimeout = getRelativeTimeout;
83function deadlineToString(deadline) {
84 if (deadline instanceof Date) {
85 return deadline.toISOString();
86 }
87 else {
88 const dateDeadline = new Date(deadline);
89 if (Number.isNaN(dateDeadline.getTime())) {
90 return '' + deadline;
91 }
92 else {
93 return dateDeadline.toISOString();
94 }
95 }
96}
97exports.deadlineToString = deadlineToString;
98//# sourceMappingURL=deadline.js.map
\No newline at end of file