Skip to content
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
32 changes: 16 additions & 16 deletions packages/memcache-client/src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import assert from "assert";
import { optionalRequire } from "optional-require";
import { Socket } from "net";

const Zstd = optionalRequire("zstd.ts");
import * as Zstd from "zstd.ts";

import nodeify from "./nodeify";
import ValuePacker from "./value-packer";
Expand Down Expand Up @@ -226,19 +225,19 @@ export class MemcacheClient extends EventEmitter {
options = {};
}

return this._callbackSend(data, key, options, callback) as unknown as Promise<ValueType>;
return this._callbackSend(data, key, options, callback);
}

// the promise only version of send
xsend(
xsend<ValueType>(
data: StoreParams | SocketCallback,
key: string,
options?: StoreCommandOptions
): Promise<unknown> {
): Promise<ValueType> {
return this._servers.doCmd(
(c: MemcacheConnection) => this._send(c, data, options || {}),
key
) as Promise<unknown>;
) as Promise<ValueType>;
}

// a convenient method to send a single line as a command to the server
Expand All @@ -249,7 +248,7 @@ export class MemcacheClient extends EventEmitter {
options?: CommonCommandOption,
callback?: ErrorFirstCallback
): Promise<Response> {
return this.send(
return this.send<Response>(
(socket) => {
let line = data;
if (options?.noreply) {
Expand All @@ -260,7 +259,7 @@ export class MemcacheClient extends EventEmitter {
key || "",
options,
callback
) as unknown as Promise<Response>;
);
}

// "set" means "store this data".
Expand Down Expand Up @@ -346,7 +345,7 @@ export class MemcacheClient extends EventEmitter {
key,
options as CommonCommandOption,
callback
) as unknown as Promise<string[]>;
);
}

// incr key by value, fire & forget with options.noreply
Expand All @@ -361,7 +360,7 @@ export class MemcacheClient extends EventEmitter {
key,
options as StoreCommandOptions,
callback
) as unknown as Promise<string>;
);
}

// decrease key by value, fire & forget with options.noreply
Expand All @@ -376,7 +375,7 @@ export class MemcacheClient extends EventEmitter {
key,
options as StoreCommandOptions,
callback
) as unknown as Promise<string>;
);
}

// touch key with exp time, fire & forget with options.noreply
Expand All @@ -391,12 +390,12 @@ export class MemcacheClient extends EventEmitter {
key,
options as CommonCommandOption,
callback
) as unknown as Promise<string[]>;
);
}

// get version of server
version(callback?: OperationCallback<Error, string[]>): Promise<string[]> {
return this.cmd(`version`, "", {}, callback) as unknown as Promise<string[]>;
return this.cmd(`version`, "", {}, callback);
}

async versionAll(
Expand Down Expand Up @@ -662,6 +661,7 @@ export class MemcacheClient extends EventEmitter {
serverKey,
keys: serverKeys,
});
return undefined;
}
})
);
Expand Down Expand Up @@ -721,13 +721,13 @@ export class MemcacheClient extends EventEmitter {
}

// internal send that expects all params passed (even if they are undefined)
_callbackSend(
_callbackSend<ValueType>(
data: StoreParams | SocketCallback,
key: string,
options?: Partial<CasCommandOptions>,
callback?: ErrorFirstCallback
): Promise<unknown> {
return nodeify(this.xsend(data, key, options), callback);
): Promise<ValueType> {
return nodeify<ValueType>(this.xsend(data, key, options), callback);
}

_unpackValue(result: PackedData): number | string | Record<string, unknown> | Buffer {
Expand Down
6 changes: 3 additions & 3 deletions packages/memcache-client/src/lib/nodeify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export type CallbackablePromise<T extends any> = Promise<T> & {
nodeify?: (callback: ErrorFirstCallback) => void;
};

export default function nodeify(
promise: CallbackablePromise<unknown>,
export default function nodeify<ValueType>(
promise: CallbackablePromise<ValueType>,
callback?: ErrorFirstCallback
): Promise<unknown> {
): Promise<ValueType> {
if (callback) {
if (promise.nodeify !== undefined) {
promise.nodeify(callback);
Expand Down