Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version and versionAll #6

Merged
merged 2 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"semi": [
2,
"always"
]
],
"no-unused-vars": 0
},
"globals": {
"Promise": "readonly"
Expand All @@ -24,5 +25,10 @@
"node": true,
"browser": true
},
"parserOptions": {
"es6": true,
blackmad marked this conversation as resolved.
Show resolved Hide resolved
"ecmaVersion": 6,
"sourceType": "module"
},
"extends": "eslint:recommended"
}
43 changes: 42 additions & 1 deletion lib/memjs/memjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class Client {
*
* The callback signature is:
*
* callback(err, values, flags)
* callback(err, value, flags)
*
* @param keys
* @param callback
Expand Down Expand Up @@ -528,4 +528,45 @@ export class Client {
callback?: (err: Error | null, ...args: any[]) => void,
retries?: number
): void;

/**
* VERSION
*
* Retrieves the server version from the "first" server in the backend pool
*
* The callback signature is:
*
* callback(err, value, flags)
*
* @param keys
* @param callback
*/
version(): Promise<{ value: string; flags: Buffer | null }>;
version(callback: (
err: Error | null,
value: { [K in Keys]: Buffer | null } | null,
flags: Buffer | null
) => void): void


/**
* VERSION-ALL
*
* Retrieves the server version from all the servers
* in the backend pool, errors if any one of them has an
* error
*
* The callback signature is:
*
* callback(err, value, flags)
*
* @param keys
* @param callback
*/
versionAll(): Promise<{ values: Record<string, string>; flags: Buffer | null }>;
versionAll(callback: (
err: Error | null,
values: Record<string, string>,
flags: Buffer | null
) => void): void
}
50 changes: 41 additions & 9 deletions lib/memjs/memjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,31 +927,26 @@ Client.prototype.quit = function() {
}
};

// VERSION
//
// Request the server version from the "first" server in the backend pool
//
// The server responds with a packet containing the version string in the body with the following format: "x.y.z"
Client.prototype.version = function(callback) {
Client.prototype._version = function(server, callback) {
var self = this;
if(callback === undefined) {
return promisify(function(callback) {
self.version(function(err, value, flags) {
self._version(server, function(err, value, flags) {
callback(err, {value: value, flags: flags});
});
});
}

this.incrSeq();
var request = makeRequestBuffer(constants.OP_VERSION, '', '', '', this.seq);
var serverKey = this.serverKeys[0];
var logger = this.options.logger;

this.perform(serverKey, request, this.seq, function (err, response) {
this.performOnServer(server, request, this.seq, function (err, response) {
if (err) {
if (callback) { callback(err, null, null); }
return;
}

switch (response.header.status) {
case constants.RESPONSE_STATUS_SUCCCESS:
var deserialized = self.serializer.deserialize(response.header.opcode, response.val, response.extras);
Expand All @@ -965,6 +960,43 @@ Client.prototype.version = function(callback) {
});
};

// VERSION
//
// Request the server version from the "first" server in the backend pool
//
// The server responds with a packet containing the version string in the body with the following format: "x.y.z"

Client.prototype.version = function(callback) {
const server = this.serverKeyToServer(this.serverKeys[0]);

return this._version(server, callback);
};

Client.prototype.versionAll = function(callback) {
const promise = Promise.all(this.serverKeys.map((serverKey) => {
const server = this.serverKeyToServer(serverKey);

return this._version(server).then((response) => {
return {serverKey: serverKey, value: response.value};
});
})).then(versionObjects => {
const values = versionObjects.reduce((accumulator, versionObject) => {
accumulator[versionObject.serverKey] = versionObject.value;
return accumulator;
}, {});
return {values: values};
});

if(callback === undefined) {
return promise;
}
return promise.then((response) =>{
callback(null, response.values);
}).catch((err) => {
callback(err, null);
});
};

// CLOSE
//
// Closes (abruptly) connections to all the servers.
Expand Down
7 changes: 2 additions & 5 deletions lib/memjs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ Server.prototype.error = function(err) {
this._socket.destroy();
delete(this._socket);
}
var k;
for (k in errcalls) {
if (errcalls.hasOwnProperty(k)) {
errcalls[k](err);
}
for (let errcall of Object.values(errcalls)) {
blackmad marked this conversation as resolved.
Show resolved Hide resolved
errcall(err);
}
};

Expand Down
11 changes: 4 additions & 7 deletions lib/memjs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,11 @@ exports.parseMessages = function(dataBuf) {
};

exports.merge = function(original, deflt) {
var attr, originalValue;
for (attr in deflt) {
if (deflt.hasOwnProperty(attr)) {
originalValue = original[attr];
for (let attr of Object.keys(deflt)) {
blackmad marked this conversation as resolved.
Show resolved Hide resolved
const originalValue = original[attr];

if (originalValue === undefined || originalValue === null) {
original[attr] = deflt[attr];
}
if (originalValue === undefined || originalValue === null) {
original[attr] = deflt[attr];
}
}
return original;
Expand Down
Loading