Socket
Socket
Sign inDemoInstall

@grpc/grpc-js

Package Overview
Dependencies
Maintainers
0
Versions
174
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@grpc/grpc-js - npm Package Compare versions

Comparing version 1.10.9 to 1.10.10

9

build/src/client.js

@@ -169,3 +169,3 @@ "use strict";

if (responseMessage !== null) {
call.cancelWithStatus(constants_1.Status.INTERNAL, 'Too many responses received');
call.cancelWithStatus(constants_1.Status.UNIMPLEMENTED, 'Too many responses received');
}

@@ -183,3 +183,3 @@ responseMessage = message;

callProperties.callback((0, call_1.callErrorFromStatus)({
code: constants_1.Status.INTERNAL,
code: constants_1.Status.UNIMPLEMENTED,
details: 'No message received',

@@ -251,5 +251,6 @@ metadata: status.metadata,

if (responseMessage !== null) {
call.cancelWithStatus(constants_1.Status.INTERNAL, 'Too many responses received');
call.cancelWithStatus(constants_1.Status.UNIMPLEMENTED, 'Too many responses received');
}
responseMessage = message;
call.startRead();
},

@@ -265,3 +266,3 @@ onReceiveStatus(status) {

callProperties.callback((0, call_1.callErrorFromStatus)({
code: constants_1.Status.INTERNAL,
code: constants_1.Status.UNIMPLEMENTED,
details: 'No message received',

@@ -268,0 +269,0 @@ metadata: status.metadata,

@@ -16,2 +16,3 @@ /// <reference types="node" />

private maxReceiveMessageLength;
private maxSendMessageLength;
constructor(channelOptions: ChannelOptions, sharedFilterConfig: SharedCompressionFilterConfig);

@@ -18,0 +19,0 @@ sendMetadata(metadata: Promise<Metadata>): Promise<Metadata>;

@@ -184,3 +184,3 @@ "use strict";

constructor(channelOptions, sharedFilterConfig) {
var _a, _b;
var _a, _b, _c;
super();

@@ -193,6 +193,7 @@ this.sharedFilterConfig = sharedFilterConfig;

this.maxReceiveMessageLength = (_a = channelOptions['grpc.max_receive_message_length']) !== null && _a !== void 0 ? _a : constants_1.DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH;
this.maxSendMessageLength = (_b = channelOptions['grpc.max_send_message_length']) !== null && _b !== void 0 ? _b : constants_1.DEFAULT_MAX_SEND_MESSAGE_LENGTH;
if (compressionAlgorithmKey !== undefined) {
if (isCompressionAlgorithmKey(compressionAlgorithmKey)) {
const clientSelectedEncoding = compression_algorithms_1.CompressionAlgorithms[compressionAlgorithmKey];
const serverSupportedEncodings = (_b = sharedFilterConfig.serverSupportedEncodingHeader) === null || _b === void 0 ? void 0 : _b.split(',');
const serverSupportedEncodings = (_c = sharedFilterConfig.serverSupportedEncodingHeader) === null || _c === void 0 ? void 0 : _c.split(',');
/**

@@ -259,2 +260,8 @@ * There are two possible situations here:

const resolvedMessage = await message;
if (this.maxSendMessageLength !== -1 && resolvedMessage.message.length > this.maxSendMessageLength) {
throw {
code: constants_1.Status.RESOURCE_EXHAUSTED,
details: `Attempted to send message with a size larger than ${this.maxSendMessageLength}`
};
}
let compress;

@@ -261,0 +268,0 @@ if (this.sendCompression instanceof IdentityHandler) {

@@ -72,3 +72,3 @@ "use strict";

(0, logging_1.log)(constants_1.LogVerbosity.INFO, 'userinfo found in proxy URI');
userCred = `${proxyUrl.username}:${proxyUrl.password}`;
userCred = decodeURIComponent(`${proxyUrl.username}:${proxyUrl.password}`);
}

@@ -203,2 +203,8 @@ else {

proxyAddressString);
// The HTTP client may have already read a few bytes of the proxied
// connection. If that's the case, put them back into the socket.
// See https://github.com/grpc/grpc-node/issues/2744.
if (head.length > 0) {
socket.unshift(head);
}
if ('secureContext' in connectionOptions) {

@@ -205,0 +211,0 @@ /* The proxy is connecting to a TLS server, so upgrade this socket

@@ -49,2 +49,3 @@ import { Deserialize, Serialize, ServiceDefinition } from './make-client';

private trace;
private keepaliveTrace;
addProtoService(): never;

@@ -51,0 +52,0 @@ addService(service: ServiceDefinition, implementation: UntypedServiceImplementation): void;

@@ -39,12 +39,16 @@ /// <reference types="node" />

*/
private keepaliveTimeMs;
private readonly keepaliveTimeMs;
/**
* The amount of time to wait for an acknowledgement after sending a ping
*/
private keepaliveTimeoutMs;
private readonly keepaliveTimeoutMs;
/**
* Timer reference for timeout that indicates when to send the next ping
* Indicates whether keepalive pings should be sent without any active calls
*/
private keepaliveTimerId;
private readonly keepaliveWithoutCalls;
/**
* Timer reference indicating when to send the next ping or when the most recent ping will be considered lost.
*/
private keepaliveTimer;
/**
* Indicates that the keepalive timer ran out while there were no active

@@ -54,10 +58,2 @@ * calls, and a ping should be sent the next time a call starts.

private pendingSendKeepalivePing;
/**
* Timer reference tracking when the most recent ping will be considered lost
*/
private keepaliveTimeoutId;
/**
* Indicates whether keepalive pings should be sent without any active calls
*/
private keepaliveWithoutCalls;
private userAgent;

@@ -102,4 +98,2 @@ private activeCalls;

addDisconnectListener(listener: TransportDisconnectListener): void;
private clearKeepaliveTimer;
private clearKeepaliveTimeout;
private canSendPing;

@@ -114,3 +108,6 @@ private maybeSendPing;

private maybeStartKeepalivePingTimer;
private stopKeepalivePings;
/**
* Clears whichever keepalive timeout is currently active, if any.
*/
private clearKeepaliveTimeout;
private removeActiveCall;

@@ -117,0 +114,0 @@ private addActiveCall;

@@ -49,14 +49,6 @@ "use strict";

/**
* The amount of time in between sending pings
* Timer reference indicating when to send the next ping or when the most recent ping will be considered lost.
*/
this.keepaliveTimeMs = -1;
this.keepaliveTimer = null;
/**
* The amount of time to wait for an acknowledgement after sending a ping
*/
this.keepaliveTimeoutMs = KEEPALIVE_TIMEOUT_MS;
/**
* Timer reference for timeout that indicates when to send the next ping
*/
this.keepaliveTimerId = null;
/**
* Indicates that the keepalive timer ran out while there were no active

@@ -66,10 +58,2 @@ * calls, and a ping should be sent the next time a call starts.

this.pendingSendKeepalivePing = false;
/**
* Timer reference tracking when the most recent ping will be considered lost
*/
this.keepaliveTimeoutId = null;
/**
* Indicates whether keepalive pings should be sent without any active calls
*/
this.keepaliveWithoutCalls = false;
this.activeCalls = new Set();

@@ -106,5 +90,11 @@ this.disconnectListeners = [];

}
else {
this.keepaliveTimeMs = -1;
}
if ('grpc.keepalive_timeout_ms' in options) {
this.keepaliveTimeoutMs = options['grpc.keepalive_timeout_ms'];
}
else {
this.keepaliveTimeoutMs = KEEPALIVE_TIMEOUT_MS;
}
if ('grpc.keepalive_permit_without_calls' in options) {

@@ -119,3 +109,2 @@ this.keepaliveWithoutCalls =

this.trace('session closed');
this.stopKeepalivePings();
this.handleDisconnect();

@@ -262,2 +251,3 @@ });

handleDisconnect() {
this.clearKeepaliveTimeout();
this.reportDisconnectToOwner(false);

@@ -270,2 +260,3 @@ /* Give calls an event loop cycle to finish naturally before reporting the

}
this.session.destroy();
});

@@ -276,18 +267,5 @@ }

}
clearKeepaliveTimer() {
if (!this.keepaliveTimerId) {
return;
}
clearTimeout(this.keepaliveTimerId);
this.keepaliveTimerId = null;
}
clearKeepaliveTimeout() {
if (!this.keepaliveTimeoutId) {
return;
}
clearTimeout(this.keepaliveTimeoutId);
this.keepaliveTimeoutId = null;
}
canSendPing() {
return (this.keepaliveTimeMs > 0 &&
return (!this.session.destroyed &&
this.keepaliveTimeMs > 0 &&
(this.keepaliveWithoutCalls || this.activeCalls.size > 0));

@@ -297,3 +275,2 @@ }

var _a, _b;
this.clearKeepaliveTimer();
if (!this.canSendPing()) {

@@ -303,2 +280,6 @@ this.pendingSendKeepalivePing = true;

}
if (this.keepaliveTimer) {
console.error('keepaliveTimeout is not null');
return;
}
if (this.channelzEnabled) {

@@ -308,11 +289,12 @@ this.keepalivesSent += 1;

this.keepaliveTrace('Sending ping with timeout ' + this.keepaliveTimeoutMs + 'ms');
if (!this.keepaliveTimeoutId) {
this.keepaliveTimeoutId = setTimeout(() => {
this.keepaliveTrace('Ping timeout passed without response');
this.handleDisconnect();
}, this.keepaliveTimeoutMs);
(_b = (_a = this.keepaliveTimeoutId).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
}
this.keepaliveTimer = setTimeout(() => {
this.keepaliveTimer = null;
this.keepaliveTrace('Ping timeout passed without response');
this.handleDisconnect();
}, this.keepaliveTimeoutMs);
(_b = (_a = this.keepaliveTimer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
let pingSendError = '';
try {
this.session.ping((err, duration, payload) => {
const pingSentSuccessfully = this.session.ping((err, duration, payload) => {
this.clearKeepaliveTimeout();
if (err) {

@@ -322,10 +304,17 @@ this.keepaliveTrace('Ping failed with error ' + err.message);

}
this.keepaliveTrace('Received ping response');
this.clearKeepaliveTimeout();
this.maybeStartKeepalivePingTimer();
else {
this.keepaliveTrace('Received ping response');
this.maybeStartKeepalivePingTimer();
}
});
if (!pingSentSuccessfully) {
pingSendError = 'Ping returned false';
}
}
catch (e) {
/* If we fail to send a ping, the connection is no longer functional, so
* we should discard it. */
// grpc/grpc-node#2139
pingSendError = (e instanceof Error ? e.message : '') || 'Unknown error';
}
if (pingSendError) {
this.keepaliveTrace('Ping send failed: ' + pingSendError);
this.handleDisconnect();

@@ -349,8 +338,9 @@ }

}
else if (!this.keepaliveTimerId && !this.keepaliveTimeoutId) {
else if (!this.keepaliveTimer) {
this.keepaliveTrace('Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms');
this.keepaliveTimerId = setTimeout(() => {
this.keepaliveTimer = setTimeout(() => {
this.keepaliveTimer = null;
this.maybeSendPing();
}, this.keepaliveTimeMs);
(_b = (_a = this.keepaliveTimerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
(_b = (_a = this.keepaliveTimer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
}

@@ -360,8 +350,10 @@ /* Otherwise, there is already either a keepalive timer or a ping pending,

}
stopKeepalivePings() {
if (this.keepaliveTimerId) {
clearTimeout(this.keepaliveTimerId);
this.keepaliveTimerId = null;
/**
* Clears whichever keepalive timeout is currently active, if any.
*/
clearKeepaliveTimeout() {
if (this.keepaliveTimer) {
clearTimeout(this.keepaliveTimer);
this.keepaliveTimer = null;
}
this.clearKeepaliveTimeout();
}

@@ -368,0 +360,0 @@ removeActiveCall(call) {

{
"name": "@grpc/grpc-js",
"version": "1.10.9",
"version": "1.10.10",
"description": "gRPC Library for Node - pure JS implementation",

@@ -5,0 +5,0 @@ "homepage": "https://grpc.io/",

@@ -333,3 +333,3 @@ /*

if (responseMessage !== null) {
call.cancelWithStatus(Status.INTERNAL, 'Too many responses received');
call.cancelWithStatus(Status.UNIMPLEMENTED, 'Too many responses received');
}

@@ -349,3 +349,3 @@ responseMessage = message;

{
code: Status.INTERNAL,
code: Status.UNIMPLEMENTED,
details: 'No message received',

@@ -468,5 +468,6 @@ metadata: status.metadata,

if (responseMessage !== null) {
call.cancelWithStatus(Status.INTERNAL, 'Too many responses received');
call.cancelWithStatus(Status.UNIMPLEMENTED, 'Too many responses received');
}
responseMessage = message;
call.startRead();
},

@@ -484,3 +485,3 @@ onReceiveStatus(status: StatusObject) {

{
code: Status.INTERNAL,
code: Status.UNIMPLEMENTED,
details: 'No message received',

@@ -487,0 +488,0 @@ metadata: status.metadata,

@@ -24,3 +24,3 @@ /*

import { CompressionAlgorithms } from './compression-algorithms';
import { DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH, LogVerbosity, Status } from './constants';
import { DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH, DEFAULT_MAX_SEND_MESSAGE_LENGTH, LogVerbosity, Status } from './constants';
import { BaseFilter, Filter, FilterFactory } from './filter';

@@ -223,2 +223,3 @@ import * as logging from './logging';

private maxReceiveMessageLength: number;
private maxSendMessageLength: number;

@@ -233,3 +234,4 @@ constructor(

channelOptions['grpc.default_compression_algorithm'];
this.maxReceiveMessageLength = channelOptions['grpc.max_receive_message_length'] ?? DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH
this.maxReceiveMessageLength = channelOptions['grpc.max_receive_message_length'] ?? DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH;
this.maxSendMessageLength = channelOptions['grpc.max_send_message_length'] ?? DEFAULT_MAX_SEND_MESSAGE_LENGTH;
if (compressionAlgorithmKey !== undefined) {

@@ -320,2 +322,8 @@ if (isCompressionAlgorithmKey(compressionAlgorithmKey)) {

const resolvedMessage: WriteObject = await message;
if (this.maxSendMessageLength !== -1 && resolvedMessage.message.length > this.maxSendMessageLength) {
throw {
code: Status.RESOURCE_EXHAUSTED,
details: `Attempted to send message with a size larger than ${this.maxSendMessageLength}`
};
}
let compress: boolean;

@@ -322,0 +330,0 @@ if (this.sendCompression instanceof IdentityHandler) {

@@ -83,3 +83,3 @@ /*

log(LogVerbosity.INFO, 'userinfo found in proxy URI');
userCred = `${proxyUrl.username}:${proxyUrl.password}`;
userCred = decodeURIComponent(`${proxyUrl.username}:${proxyUrl.password}`);
} else {

@@ -237,2 +237,8 @@ userCred = proxyUrl.username;

);
// The HTTP client may have already read a few bytes of the proxied
// connection. If that's the case, put them back into the socket.
// See https://github.com/grpc/grpc-node/issues/2744.
if (head.length > 0) {
socket.unshift(head);
}
if ('secureContext' in connectionOptions) {

@@ -239,0 +245,0 @@ /* The proxy is connecting to a TLS server, so upgrade this socket

@@ -438,2 +438,10 @@ /*

private keepaliveTrace(text: string): void {
logging.trace(
LogVerbosity.DEBUG,
'keepalive',
'(' + this.channelzRef.id + ') ' + text
);
}
addProtoService(): never {

@@ -1380,4 +1388,3 @@ throw new Error('Not implemented. Use addService() instead');

let connectionAgeGraceTimer: NodeJS.Timeout | null = null;
let keeapliveTimeTimer: NodeJS.Timeout | null = null;
let keepaliveTimeoutTimer: NodeJS.Timeout | null = null;
let keepaliveTimer: NodeJS.Timeout | null = null;
let sessionClosedByServer = false;

@@ -1425,38 +1432,87 @@

if (this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS) {
keeapliveTimeTimer = setInterval(() => {
keepaliveTimeoutTimer = setTimeout(() => {
sessionClosedByServer = true;
session.close();
}, this.keepaliveTimeoutMs);
keepaliveTimeoutTimer.unref?.();
const clearKeepaliveTimeout = () => {
if (keepaliveTimer) {
clearTimeout(keepaliveTimer);
keepaliveTimer = null;
}
};
try {
session.ping(
(err: Error | null, duration: number, payload: Buffer) => {
if (keepaliveTimeoutTimer) {
clearTimeout(keepaliveTimeoutTimer);
}
const canSendPing = () => {
return (
!session.destroyed &&
this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS &&
this.keepaliveTimeMs > 0
);
};
if (err) {
sessionClosedByServer = true;
this.trace(
'Connection dropped due to error of a ping frame ' +
err.message +
' return in ' +
duration
);
session.close();
}
/* eslint-disable-next-line prefer-const */
let sendPing: () => void; // hoisted for use in maybeStartKeepalivePingTimer
const maybeStartKeepalivePingTimer = () => {
if (!canSendPing()) {
return;
}
this.keepaliveTrace(
'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms'
);
keepaliveTimer = setTimeout(() => {
clearKeepaliveTimeout();
sendPing();
}, this.keepaliveTimeMs);
keepaliveTimer.unref?.();
};
sendPing = () => {
if (!canSendPing()) {
return;
}
this.keepaliveTrace(
'Sending ping with timeout ' + this.keepaliveTimeoutMs + 'ms'
);
let pingSendError = '';
try {
const pingSentSuccessfully = session.ping(
(err: Error | null, duration: number, payload: Buffer) => {
clearKeepaliveTimeout();
if (err) {
this.keepaliveTrace('Ping failed with error: ' + err.message);
sessionClosedByServer = true;
session.close();
} else {
this.keepaliveTrace('Received ping response');
maybeStartKeepalivePingTimer();
}
);
} catch (e) {
clearTimeout(keepaliveTimeoutTimer);
// The ping can't be sent because the session is already closed
session.destroy();
}
);
if (!pingSentSuccessfully) {
pingSendError = 'Ping returned false';
}
}, this.keepaliveTimeMs);
keeapliveTimeTimer.unref?.();
}
} catch (e) {
// grpc/grpc-node#2139
pingSendError =
(e instanceof Error ? e.message : '') || 'Unknown error';
}
if (pingSendError) {
this.keepaliveTrace('Ping send failed: ' + pingSendError);
this.trace(
'Connection dropped due to ping send error: ' + pingSendError
);
sessionClosedByServer = true;
session.close();
return;
}
keepaliveTimer = setTimeout(() => {
clearKeepaliveTimeout();
this.keepaliveTrace('Ping timeout passed without response');
this.trace('Connection dropped by keepalive timeout');
sessionClosedByServer = true;
session.close();
}, this.keepaliveTimeoutMs);
keepaliveTimer.unref?.();
};
maybeStartKeepalivePingTimer();
session.on('close', () => {

@@ -1477,8 +1533,3 @@ if (!sessionClosedByServer) {

if (keeapliveTimeTimer) {
clearInterval(keeapliveTimeTimer);
if (keepaliveTimeoutTimer) {
clearTimeout(keepaliveTimeoutTimer);
}
}
clearKeepaliveTimeout();

@@ -1528,4 +1579,3 @@ if (idleTimeoutObj !== null) {

let connectionAgeGraceTimer: NodeJS.Timeout | null = null;
let keeapliveTimeTimer: NodeJS.Timeout | null = null;
let keepaliveTimeoutTimer: NodeJS.Timeout | null = null;
let keepaliveTimeout: NodeJS.Timeout | null = null;
let sessionClosedByServer = false;

@@ -1572,46 +1622,100 @@

if (this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS) {
keeapliveTimeTimer = setInterval(() => {
keepaliveTimeoutTimer = setTimeout(() => {
sessionClosedByServer = true;
this.channelzTrace.addTrace(
'CT_INFO',
'Connection dropped by keepalive timeout from ' + clientAddress
);
const clearKeepaliveTimeout = () => {
if (keepaliveTimeout) {
clearTimeout(keepaliveTimeout);
keepaliveTimeout = null;
}
};
session.close();
}, this.keepaliveTimeoutMs);
keepaliveTimeoutTimer.unref?.();
const canSendPing = () => {
return (
!session.destroyed &&
this.keepaliveTimeMs < KEEPALIVE_MAX_TIME_MS &&
this.keepaliveTimeMs > 0
);
};
try {
session.ping(
(err: Error | null, duration: number, payload: Buffer) => {
if (keepaliveTimeoutTimer) {
clearTimeout(keepaliveTimeoutTimer);
}
/* eslint-disable-next-line prefer-const */
let sendPing: () => void; // hoisted for use in maybeStartKeepalivePingTimer
if (err) {
sessionClosedByServer = true;
this.channelzTrace.addTrace(
'CT_INFO',
'Connection dropped due to error of a ping frame ' +
err.message +
' return in ' +
duration
);
const maybeStartKeepalivePingTimer = () => {
if (!canSendPing()) {
return;
}
this.keepaliveTrace(
'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms'
);
keepaliveTimeout = setTimeout(() => {
clearKeepaliveTimeout();
sendPing();
}, this.keepaliveTimeMs);
keepaliveTimeout.unref?.();
};
session.close();
}
sendPing = () => {
if (!canSendPing()) {
return;
}
this.keepaliveTrace(
'Sending ping with timeout ' + this.keepaliveTimeoutMs + 'ms'
);
let pingSendError = '';
try {
const pingSentSuccessfully = session.ping(
(err: Error | null, duration: number, payload: Buffer) => {
clearKeepaliveTimeout();
if (err) {
this.keepaliveTrace('Ping failed with error: ' + err.message);
this.channelzTrace.addTrace(
'CT_INFO',
'Connection dropped due to error of a ping frame ' +
err.message +
' return in ' +
duration
);
sessionClosedByServer = true;
session.close();
} else {
this.keepaliveTrace('Received ping response');
maybeStartKeepalivePingTimer();
}
);
channelzSessionInfo.keepAlivesSent += 1;
} catch (e) {
clearTimeout(keepaliveTimeoutTimer);
// The ping can't be sent because the session is already closed
session.destroy();
}
);
if (!pingSentSuccessfully) {
pingSendError = 'Ping returned false';
}
}, this.keepaliveTimeMs);
keeapliveTimeTimer.unref?.();
}
} catch (e) {
// grpc/grpc-node#2139
pingSendError =
(e instanceof Error ? e.message : '') || 'Unknown error';
}
if (pingSendError) {
this.keepaliveTrace('Ping send failed: ' + pingSendError);
this.channelzTrace.addTrace(
'CT_INFO',
'Connection dropped due to ping send error: ' + pingSendError
);
sessionClosedByServer = true;
session.close();
return;
}
channelzSessionInfo.keepAlivesSent += 1;
keepaliveTimeout = setTimeout(() => {
clearKeepaliveTimeout();
this.keepaliveTrace('Ping timeout passed without response');
this.channelzTrace.addTrace(
'CT_INFO',
'Connection dropped by keepalive timeout from ' + clientAddress
);
sessionClosedByServer = true;
session.close();
}, this.keepaliveTimeoutMs);
keepaliveTimeout.unref?.();
};
maybeStartKeepalivePingTimer();
session.on('close', () => {

@@ -1636,8 +1740,3 @@ if (!sessionClosedByServer) {

if (keeapliveTimeTimer) {
clearInterval(keeapliveTimeTimer);
if (keepaliveTimeoutTimer) {
clearTimeout(keepaliveTimeoutTimer);
}
}
clearKeepaliveTimeout();

@@ -1644,0 +1743,0 @@ if (idleTimeoutObj !== null) {

@@ -105,12 +105,16 @@ /*

*/
private keepaliveTimeMs = -1;
private readonly keepaliveTimeMs: number;
/**
* The amount of time to wait for an acknowledgement after sending a ping
*/
private keepaliveTimeoutMs: number = KEEPALIVE_TIMEOUT_MS;
private readonly keepaliveTimeoutMs: number;
/**
* Timer reference for timeout that indicates when to send the next ping
* Indicates whether keepalive pings should be sent without any active calls
*/
private keepaliveTimerId: NodeJS.Timeout | null = null;
private readonly keepaliveWithoutCalls: boolean;
/**
* Timer reference indicating when to send the next ping or when the most recent ping will be considered lost.
*/
private keepaliveTimer: NodeJS.Timeout | null = null;
/**
* Indicates that the keepalive timer ran out while there were no active

@@ -120,10 +124,2 @@ * calls, and a ping should be sent the next time a call starts.

private pendingSendKeepalivePing = false;
/**
* Timer reference tracking when the most recent ping will be considered lost
*/
private keepaliveTimeoutId: NodeJS.Timeout | null = null;
/**
* Indicates whether keepalive pings should be sent without any active calls
*/
private keepaliveWithoutCalls = false;

@@ -188,5 +184,9 @@ private userAgent: string;

this.keepaliveTimeMs = options['grpc.keepalive_time_ms']!;
} else {
this.keepaliveTimeMs = -1;
}
if ('grpc.keepalive_timeout_ms' in options) {
this.keepaliveTimeoutMs = options['grpc.keepalive_timeout_ms']!;
} else {
this.keepaliveTimeoutMs = KEEPALIVE_TIMEOUT_MS;
}

@@ -202,3 +202,2 @@ if ('grpc.keepalive_permit_without_calls' in options) {

this.trace('session closed');
this.stopKeepalivePings();
this.handleDisconnect();

@@ -391,2 +390,3 @@ });

private handleDisconnect() {
this.clearKeepaliveTimeout();
this.reportDisconnectToOwner(false);

@@ -399,2 +399,3 @@ /* Give calls an event loop cycle to finish naturally before reporting the

}
this.session.destroy();
});

@@ -407,20 +408,5 @@ }

private clearKeepaliveTimer() {
if (!this.keepaliveTimerId) {
return;
}
clearTimeout(this.keepaliveTimerId);
this.keepaliveTimerId = null;
}
private clearKeepaliveTimeout() {
if (!this.keepaliveTimeoutId) {
return;
}
clearTimeout(this.keepaliveTimeoutId);
this.keepaliveTimeoutId = null;
}
private canSendPing() {
return (
!this.session.destroyed &&
this.keepaliveTimeMs > 0 &&

@@ -432,3 +418,2 @@ (this.keepaliveWithoutCalls || this.activeCalls.size > 0)

private maybeSendPing() {
this.clearKeepaliveTimer();
if (!this.canSendPing()) {

@@ -438,2 +423,6 @@ this.pendingSendKeepalivePing = true;

}
if (this.keepaliveTimer) {
console.error('keepaliveTimeout is not null');
return;
}
if (this.channelzEnabled) {

@@ -445,24 +434,31 @@ this.keepalivesSent += 1;

);
if (!this.keepaliveTimeoutId) {
this.keepaliveTimeoutId = setTimeout(() => {
this.keepaliveTrace('Ping timeout passed without response');
this.handleDisconnect();
}, this.keepaliveTimeoutMs);
this.keepaliveTimeoutId.unref?.();
}
this.keepaliveTimer = setTimeout(() => {
this.keepaliveTimer = null;
this.keepaliveTrace('Ping timeout passed without response');
this.handleDisconnect();
}, this.keepaliveTimeoutMs);
this.keepaliveTimer.unref?.();
let pingSendError = '';
try {
this.session!.ping(
const pingSentSuccessfully = this.session.ping(
(err: Error | null, duration: number, payload: Buffer) => {
this.clearKeepaliveTimeout();
if (err) {
this.keepaliveTrace('Ping failed with error ' + err.message);
this.handleDisconnect();
} else {
this.keepaliveTrace('Received ping response');
this.maybeStartKeepalivePingTimer();
}
this.keepaliveTrace('Received ping response');
this.clearKeepaliveTimeout();
this.maybeStartKeepalivePingTimer();
}
);
if (!pingSentSuccessfully) {
pingSendError = 'Ping returned false';
}
} catch (e) {
/* If we fail to send a ping, the connection is no longer functional, so
* we should discard it. */
// grpc/grpc-node#2139
pingSendError = (e instanceof Error ? e.message : '') || 'Unknown error';
}
if (pingSendError) {
this.keepaliveTrace('Ping send failed: ' + pingSendError);
this.handleDisconnect();

@@ -485,10 +481,11 @@ }

this.maybeSendPing();
} else if (!this.keepaliveTimerId && !this.keepaliveTimeoutId) {
} else if (!this.keepaliveTimer) {
this.keepaliveTrace(
'Starting keepalive timer for ' + this.keepaliveTimeMs + 'ms'
);
this.keepaliveTimerId = setTimeout(() => {
this.keepaliveTimer = setTimeout(() => {
this.keepaliveTimer = null;
this.maybeSendPing();
}, this.keepaliveTimeMs);
this.keepaliveTimerId.unref?.();
this.keepaliveTimer.unref?.();
}

@@ -499,8 +496,10 @@ /* Otherwise, there is already either a keepalive timer or a ping pending,

private stopKeepalivePings() {
if (this.keepaliveTimerId) {
clearTimeout(this.keepaliveTimerId);
this.keepaliveTimerId = null;
/**
* Clears whichever keepalive timeout is currently active, if any.
*/
private clearKeepaliveTimeout() {
if (this.keepaliveTimer) {
clearTimeout(this.keepaliveTimer);
this.keepaliveTimer = null;
}
this.clearKeepaliveTimeout();
}

@@ -549,3 +548,3 @@

try {
http2Stream = this.session!.request(headers);
http2Stream = this.session.request(headers);
} catch (e) {

@@ -552,0 +551,0 @@ this.handleDisconnect();

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc