🤖 Merge PR #63763 [memjs] Update to 1.3, improve types by @BendingBender

This commit is contained in:
Dimitri B
2023-02-01 21:10:12 +01:00
committed by GitHub
parent 34d9929d10
commit cd40a09caa
2 changed files with 636 additions and 231 deletions

488
types/memjs/index.d.ts vendored
View File

@@ -1,13 +1,20 @@
// Type definitions for memjs 1.2
// Type definitions for memjs 1.3
// Project: https://github.com/memcachier/memjs
// Definitions by: Zongmin Lei <https://github.com/leizongmin>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
/// <reference types="node" />
export interface ClientOptions {
import EventEmitter = require('node:events');
import { Socket } from 'node:net';
export interface ClientOptions<TIn = string | Buffer, TOut = Buffer | null> {
/**
* Whether to failover to next server.
* @default false
*/
failover?: boolean;
/**
* How many seconds to wait until retrying a failed server.
* @default 60
@@ -33,10 +40,23 @@ export interface ClientOptions {
* A logger object that responds to `log(string)` method calls.
* @default console
*/
logger?: {
log(...args: any[]): void;
} | undefined;
logger?: Logger | undefined;
/**
* The object which will (de)serialize the data.
* @default a passthrough function which will leave the parameters unchanged
*/
serializer?: Serializer<TIn, TOut>;
}
export interface Logger {
log(...args: any[]): void;
}
export interface Serializer<TIn, TOut> {
serialize(opcode: number, value: TIn, extras: Buffer): { value: string | Buffer; extras: Buffer };
deserialize(opcode: number, value: Buffer | null, extras: Buffer): { value: TOut; extras: Buffer };
}
export interface ServerOptions {
/**
* Server username for fallback SASL authentication credentials.
@@ -68,104 +88,92 @@ export interface ServerOptions {
*/
keepAliveDelay?: number | undefined;
}
export class Client {
export class Client<TIn = string | Buffer, TOut = Buffer | null> {
/**
* Creates a new client given an optional config string and optional hash of
* options. The config string should be of the form:
* options.
*
* @param serversStr The config string should be of the form:
*
* "[user:pass@]server1[:11211],[user:pass@]server2[:11211],..."
*
* If the argument is not given, fallback on the `MEMCACHIER_SERVERS` environment
* variable, `MEMCACHE_SERVERS` environment variable or `"localhost:11211"`.
*
* The options hash may contain the options:
*
* * `retries` - the number of times to retry an operation in lieu of failures
* (default 2)
* * `expires` - the default expiration in seconds to use (default 0 - never
* expire). If `expires` is greater than 30 days (60 x 60 x 24 x 30), it is
* treated as a UNIX time (number of seconds since January 1, 1970).
* * `logger` - a logger object that responds to `log(string)` method calls.
* * `failover` - whether to failover to next server. Defaults to false.
* * `failoverTime` - how much to wait until retring a failed server. Default
* is 60 seconds.
*
* ~~~~
* log(msg1[, msg2[, msg3[...]]])
* ~~~~
*
* Defaults to `console`.
*
* Or options for the servers including:
* * `username` and `password` for fallback SASL authentication credentials.
* * `timeout` in seconds to determine failure for operations. Default is 0.5
* seconds.
* * 'conntimeout' in seconds to connection failure. Default is twice the value
* of `timeout`.
* * `keepAlive` whether to enable keep-alive functionality. Defaults to false.
* * `keepAliveDelay` in seconds to the initial delay before the first keepalive
* probe is sent on an idle socket. Defaults is 30 seconds.
*/
static create(serversStr?: string, options?: ClientOptions | ServerOptions): Client;
static create<TIn = string | Buffer, TOut = Buffer | null>(
serversStr?: string,
options?: ClientOptions<TIn, TOut> & ServerOptions,
): Client<TIn, TOut>;
servers: string[];
readonly seq: number;
readonly servers: readonly Server[];
readonly options: ClientOptions<TIn, TOut>;
readonly serializer: Serializer<TIn, TOut>;
/**
* Client initializer takes a list of Servers and an options dictionary. See Client.create for details.
* @param servers
* @param options
*/
constructor(servers: string, options?: ClientOptions);
constructor(servers: readonly Server[], options?: ClientOptions);
/**
* An overridable method you can use for determing
* server selection. Should return the server index
* in the list of servers on Client#servers.
*
* @example
* // example using node-hashring
* import * as memjs from 'memjs';
* import HashRing = require('node-hashring');
*
* const servers = ['localhost:11211', 'localhost:11212'];
* // build a map of server addresses to their index in the server list
* const serverMap: { [key: string]: number } = {};
* servers.forEach((server, index) => serverMap[server] = index);
* const client = memjs.Client.create(servers.join(','));
* // build the hashring
* const hashRing = new HashRing(servers);
* // override the getServer method
* client.getServer = (key) => serverMap[hashRing.findNode(key)];
*/
getServer(key: string): string;
/**
* Chooses the server to talk to by hashing the given key.
* @param key
*
* @param key The key in memcache.
*/
server(key: string): string;
server(key: string): Server | undefined;
/**
* GET
*
* Retrieves the value at the given key in memcache.
*
* The callback signature is:
*
* callback(err, value, flags)
*
* _value_ and _flags_ are both `Buffer`s. If the key is not found, the
* callback is invoked with null for both arguments and no error
* @param key
* @param callback
* @param key The key in memcache.
* @param callback The callback invoked when a response is received or the request fails.
* If the key is not found, the callback is invoked with null for both arguments and no error.
*/
get(key: string): Promise<{ value: Buffer | null; flags: Buffer | null }>;
get(
key: string,
callback: (err: Error | null, value: Buffer | null, flags: Buffer | null) => void
): void;
get(key: string): Promise<{ value: TOut; flags: Buffer | null }>;
get(key: string, callback: (err: Error | null, value: TOut, flags: Buffer | null) => void): void;
/**
* SET
*
* Sets the given _key_ and _value_ in memcache.
*
* The options dictionary takes:
* * _expires_: overrides the default expiration (see `Client.create`) for this
* particular key-value pair.
*
* The callback signature is:
*
* callback(err, success)
* @param key
* @param value
* @param options
* @param callback
* @param key The key in memcache.
* @param value The value to set in memcache.
* @param options Additional request options.
* @param callback The callback invoked when a response is received or the request fails.
*/
set(key: string, value: string | Buffer, options: { expires?: number | undefined }): Promise<boolean>;
set(key: string, value: TIn, options?: InsertOptions): Promise<boolean>;
set(key: string, value: TIn, callback: (err: Error | null, success: boolean | null) => void): void;
set(
key: string,
value: string | Buffer,
options: { expires?: number | undefined },
callback: (err: Error | null, success: boolean | null) => void
value: TIn,
options: InsertOptions,
callback: (err: Error | null, success: boolean | null) => void,
): void;
/**
@@ -174,24 +182,18 @@ export class Client {
* Adds the given _key_ and _value_ to memcache. The operation only succeeds
* if the key is not already set.
*
* The options dictionary takes:
* * _expires_: overrides the default expiration (see `Client.create`) for this
* particular key-value pair.
*
* The callback signature is:
*
* callback(err, success)
* @param key
* @param value
* @param options
* @param callback
* @param key The key in memcache.
* @param value The value to set in memcache.
* @param options Additional request options.
* @param callback The callback invoked when a response is received or the request fails.
*/
add(key: string, value: string | Buffer, options: { expires?: number | undefined }): Promise<boolean>;
add(key: string, value: TIn, options?: InsertOptions): Promise<boolean>;
add(key: string, value: TIn, callback: (err: Error | null, success: boolean | null) => void): void;
add(
key: string,
value: string | Buffer,
options: { expires?: number | undefined },
callback: (err: Error | null, success: boolean | null) => void
value: TIn,
options: InsertOptions,
callback: (err: Error | null, success: boolean | null) => void,
): void;
/**
@@ -200,24 +202,18 @@ export class Client {
* Replaces the given _key_ and _value_ to memcache. The operation only succeeds
* if the key is already present.
*
* The options dictionary takes:
* * _expires_: overrides the default expiration (see `Client.create`) for this
* particular key-value pair.
*
* The callback signature is:
*
* callback(err, success)
* @param key
* @param value
* @param options
* @param callback
* @param key The key in memcache.
* @param value The value with which to replace the value in memcache.
* @param options Additional request options.
* @param callback The callback invoked when a response is received or the request fails.
*/
replace(key: string, value: string | Buffer, options: { expires?: number | undefined }): Promise<boolean>;
replace(key: string, value: TIn, options?: InsertOptions): Promise<boolean>;
replace(key: string, value: TIn, callback: (err: Error | null, success: boolean | null) => void): void;
replace(
key: string,
value: string | Buffer,
options: { expires?: number | undefined },
callback: (err: Error | null, success: boolean | null) => void
value: TIn,
options: InsertOptions,
callback: (err: Error | null, success: boolean | null) => void,
): void;
/**
@@ -226,11 +222,8 @@ export class Client {
* Deletes the given _key_ from memcache. The operation only succeeds
* if the key is already present.
*
* The callback signature is:
*
* callback(err, success)
* @param key
* @param callback
* @param key The key in memcache.
* @param callback The callback invoked when a response is received or the request fails.
*/
delete(key: string): Promise<boolean>;
delete(key: string, callback: (err: Error | null, success: boolean | null) => void): void;
@@ -240,29 +233,26 @@ export class Client {
*
* Increments the given _key_ in memcache.
*
* The options dictionary takes:
* * _initial_: the value for the key if not already present, defaults to 0.
* * _expires_: overrides the default expiration (see `Client.create`) for this
* particular key-value pair.
*
* The callback signature is:
*
* callback(err, success, value)
* @param key
* @param amount
* @param options
* @param callback
* @param key The key in memcache.
* @param amount The amount by which to increment the value.
* @param options Additional request options.
* @param callback The callback invoked when a response is received or the request fails.
*/
increment(
key: string,
amount: number,
options: { initial?: number | undefined; expires?: number | undefined }
options?: IncrementDecrementOptions,
): Promise<{ success: boolean; value?: number | null | undefined }>;
increment(
key: string,
amount: number,
options: { initial?: number | undefined; expires?: number | undefined },
callback: (err: Error | null, success: boolean | null, value?: number | null) => void
callback: (err: Error | null, success: boolean | null, value?: number | null) => void,
): void;
increment(
key: string,
amount: number,
options: IncrementDecrementOptions,
callback: (err: Error | null, success: boolean | null, value?: number | null) => void,
): void;
/**
@@ -270,136 +260,105 @@ export class Client {
*
* Decrements the given _key_ in memcache.
*
* The options dictionary takes:
* * _initial_: the value for the key if not already present, defaults to 0.
* * _expires_: overrides the default expiration (see `Client.create`) for this
* particular key-value pair.
*
* The callback signature is:
*
* callback(err, success, value)
* @param key
* @param amount
* @param options
* @param callback
* @param key The key in memcache.
* @param amount The amount by which to decrement the value.
* @param options Additional request options.
* @param callback The callback invoked when a response is received or the request fails.
*/
decrement(
key: string,
amount: number,
options: { initial?: number | undefined; expires?: number | undefined }
options?: IncrementDecrementOptions,
): Promise<{ success: boolean; value?: number | null | undefined }>;
decrement(
key: string,
amount: number,
options: { initial?: number | undefined; expires?: number | undefined },
callback: (err: Error | null, success: boolean | null, value?: number | null) => void
callback: (err: Error | null, success: boolean | null, value?: number | null) => void,
): void;
decrement(
key: string,
amount: number,
options: IncrementDecrementOptions,
callback: (err: Error | null, success: boolean | null, value?: number | null) => void,
): void;
/**
* APPEND
*
* Append the given _value_ to the value associated with the given _key_ in
* memcache. The operation only succeeds if the key is already present. The
* callback signature is:
* Append the given _value_ to the value associated with the given _key_.
* The operation only succeeds if the key is already present.
*
* callback(err, success)
* @param key
* @param value
* @param callback
* @param key The key in memcache.
* @param value The value to prepend.
* @param callback The callback invoked when a response is received or the request fails.
*/
append(key: string, value: string | Buffer): Promise<boolean>;
append(
key: string,
value: string | Buffer,
callback: (err: Error | null, success: boolean | null) => void
): void;
append(key: string, value: TIn): Promise<boolean>;
append(key: string, value: TIn, callback: (err: Error | null, success: boolean | null) => void): void;
/**
* PREPEND
*
* Prepend the given _value_ to the value associated with the given _key_ in
* memcache. The operation only succeeds if the key is already present. The
* callback signature is:
* Prepend the given _value_ to the value associated with the given _key_.
* The operation only succeeds if the key is already present.
*
* callback(err, success)
* @param key
* @param value
* @param callback
* @param key The key in memcache.
* @param value The value to prepend.
* @param callback The callback invoked when a response is received or the request fails.
*/
prepend(key: string, value: string | Buffer): Promise<boolean>;
prepend(
key: string,
value: string | Buffer,
callback: (err: Error | null, success: boolean | null) => void
): void;
prepend(key: string, value: TIn): Promise<boolean>;
prepend(key: string, value: TIn, callback: (err: Error | null, success: boolean | null) => void): void;
/**
* TOUCH
*
* Touch sets an expiration value, given by _expires_, on the given _key_ in
* memcache. The operation only succeeds if the key is already present. The
* callback signature is:
* Touch sets an expiration value. The operation only succeeds if the key is already present.
*
* callback(err, success)
* @param key
* @param expires
* @param callback
* @param key The key in memcache.
* @param expires The expiration value to set.
* @param callback The callback invoked when a response is received or the request fails.
*/
touch(key: string, expires: number): Promise<boolean>;
touch(
key: string,
expires: number,
callback: (err: Error | null, success: boolean | null) => void
): void;
touch(key: string, expires?: number): Promise<boolean>;
touch(key: string, callback: (err: Error | null, success: boolean | null) => void): void;
touch(key: string, expires: number, callback: (err: Error | null, success: boolean | null) => void): void;
/**
* FLUSH
*
* Flushes the cache on each connected server. The callback signature is:
* Flushes the cache on each connected server.
*
* callback(lastErr, results)
*
* where _lastErr_ is the last error encountered (or null, in the common case
* @param callback The callback invoked when a response is received or the request fails.
* Its _lastErr_ argument is the last error encountered (or null, in the common case
* of no errors). _results_ is a dictionary mapping `"hostname:port"` to either
* `true` (if the operation was successful), or an error.
* @param callback
*/
flush(): Promise<Record<string, boolean>>;
flush(callback: (err: Error | null, results: Record<string, boolean>) => void): void;
flush(callback: (lastErr: Error | null, results: Record<string, boolean | Error>) => void): void;
/**
* STATS_WITH_KEY
*
* Sends a memcache stats command with a key to each connected server. The
* callback is invoked **ONCE PER SERVER** and has the signature:
* Sends a memcache stats command with a key to each connected server.
*
* callback(err, server, stats)
*
* _server_ is the `"hostname:port"` of the server, and _stats_ is a dictionary
* mapping the stat name to the value of the statistic as a string.
* @param key
* @param callback
* @param key The key to perform the operation on.
* @param callback The callback invoked when a response is received or the request fails.
* Its invoked **ONCE PER SERVER**, its _server_ parameter is the `"hostname:port"` of the server,
* and _stats_ is a dictionary mapping the stat name to the value of the statistic as a string.
*/
statsWithKey(
key: string,
callback?: (err: Error | null, server: string, stats: Record<string, string> | null) => void
callback?: (err: Error | null, server: string, stats: Record<string, string> | null) => void,
): void;
/**
* STATS
*
* Fetches memcache stats from each connected server. The callback is invoked
* **ONCE PER SERVER** and has the signature:
* Fetches memcache stats from each connected server.
*
* callback(err, server, stats)
*
* _server_ is the `"hostname:port"` of the server, and _stats_ is a
* dictionary mapping the stat name to the value of the statistic as a string.
* @param callback
* @param callback The callback invoked when a response is received or the request fails.
* Its invoked **ONCE PER SERVER**, its _server_ parameter is the `"hostname:port"` of the server,
* and _stats_ is a dictionary mapping the stat name to the value of the statistic as a string.
*/
stats(
callback?: (err: Error | null, server: string, stats: Record<string, string> | null) => void
): void;
stats(callback?: (err: Error | null, server: string, stats: Record<string, string> | null) => void): void;
/**
* RESET_STATS
@@ -408,16 +367,10 @@ export class Client {
* stats such as item count, but temporary stats such as total number of
* connections over time.
*
* The callback is invoked **ONCE PER SERVER** and has the signature:
*
* callback(err, server)
*
* _server_ is the `"hostname:port"` of the server.
* @param callback
* @param callback The callback invoked when a response is received or the request fails.
* Its invoked **ONCE PER SERVER**, its _server_ parameter is the `"hostname:port"` of the server.
*/
resetStats(
callback?: (err: Error | null, server: string, stats: Record<string, string> | null) => void
): void;
resetStats(callback?: (err: Error | null, server: string, stats: Record<string, string> | null) => void): void;
/**
* QUIT
@@ -437,25 +390,114 @@ export class Client {
close(): void;
/**
* Perform a generic single response operation (get, set etc) on a server
* serv: the server to perform the operation on
* request: a buffer containing the request
* seq: the sequence number of the operation. It is used to pin the callbacks
* to a specific operation and should never change during a `perform`.
* callback: a callback invoked when a response is received or the request
* fails
* retries: number of times to retry request on failure
* @param key
* @param request
* @param seq
* @param callback
* @param retries
* Perform a generic single response operation (get, set etc) on a server.
*
* @param key The key to perform the operation on.
* @param request A buffer containing the request.
* @param seq The sequence number of the operation. It is used to pin the callbacks
* to a specific operation and should never change during a `perform`.
* @param callback The callback invoked when a response is received or the request fails.
* @param retries Number of times to retry request on failure.
*/
perform(
key: string,
request: Buffer,
seq: number,
callback?: (err: Error | null, ...args: any[]) => void,
retries?: number
retries?: number,
): void;
/**
* Increment the seq value.
*/
incrSeq(): void;
}
export interface InsertOptions {
/**
* Overrides the default expiration (see `Client.create`) for this particular key-value pair.
*/
expires?: number | undefined;
}
export interface IncrementDecrementOptions extends InsertOptions {
/**
* The value for the key if not already present.
* @default 0
*/
initial?: number | undefined;
}
export class Server extends EventEmitter {
readonly responseBuffer: Buffer;
readonly host: string;
readonly port: number;
readonly connected: boolean;
readonly timeoutSet: boolean;
readonly connectCallbacks: ReadonlyArray<(socket: Socket) => void>;
readonly responseCallbacks: { [key: number]: (response: Response) => void };
readonly requestTimeouts: readonly number[];
readonly errorCallbacks: { [key: number]: (error: Error) => void };
readonly options: ServerOptions;
readonly username: string | undefined;
readonly password: string | undefined;
constructor(host: string, port: number, username?: string, password?: string, options?: ServerOptions);
onConnect(fn: (socket: Socket) => void): void;
onResponse(seq: number, fn: (response: Response) => void): void;
onError(seq: number, fn: (error: Error) => void): void;
error(error: Error): void;
listSasl(): void;
saslAuth(): void;
appendToBuffer(dataBuf: Buffer): Buffer;
responseHandler(dataBuf: Buffer): void;
sock(sasl: boolean, go: (socket: Socket) => void): void;
write(blob: Uint8Array | string): void;
writeSASL(blob: Uint8Array | string): void;
close(): void;
toString(): string;
}
export const Utils: {
makeRequestBuffer(
opcode: number,
key: string | Buffer,
extras: string | Buffer,
value: string | Buffer,
opaque: number,
): Buffer;
makeAmountInitialAndExpiration(amount: number, amountIfEmpty: number, expiration: number): Buffer;
makeExpiration(expiration: number): Buffer;
hashCode(str: string): number;
parseMessage(dataBuf: Buffer): Response;
merge<TOriginal extends object, TDefault extends object>(
original: TOriginal,
deflt: TDefault,
): TOriginal & TDefault;
timestamp(): number;
};
export interface Response {
header: Required<Header>;
key: Buffer;
extras: Buffer;
val: Buffer;
}
export const Header: {
fromBuffer(headerBuf: Buffer): Required<Header>;
toBuffer(header: Header): Buffer;
};
export interface Header {
magic: number;
opcode: number;
keyLength: number;
extrasLength: number;
dataType?: number;
status?: number;
totalBodyLength: number;
opaque?: number;
cas?: Buffer;
}

View File

@@ -1,16 +1,379 @@
import * as memjs from 'memjs';
const client = memjs.Client.create();
// test type exports
type Client = memjs.Client;
type ClientCtor = typeof memjs.Client;
type Server = memjs.Server;
type ServerCtor = typeof memjs.Server;
type ClientOptions = memjs.ClientOptions;
type Logger = memjs.Logger;
type Serializer = memjs.Serializer<number, string>;
type ServerOptions = memjs.ServerOptions;
type InsertOptions = memjs.InsertOptions;
type IncrementDecrementOptions = memjs.IncrementDecrementOptions;
type Response = memjs.Response;
type Header = memjs.Header;
client.set('hello', 'world', { expires: 600 }, (err, val) => {
console.log(err, val);
const client = memjs.Client.create(); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create(''); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { failoverTime: 1 }); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { retries: 1 }); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { retry_delay: 1 }); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { expires: 1 }); // $ExpectType Client<string | Buffer, Buffer | null>
// $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', {
logger: {
log(...args) {
args; // $ExpectType any[]
console.log(...args);
},
},
});
client.set('hello', 'world', { expires: 600 }); // $ExpectType Promise<boolean>
// $ExpectType Client<number, number>
const numClient = memjs.Client.create('', {
serializer: {
serialize(opcode, value: number, extras) {
opcode; // $ExpectType number
extras; // $ExpectType Buffer
return { value: Buffer.from(String(value)), extras };
},
deserialize(opcode, value, extras) {
opcode; // $ExpectType number
value; // $ExpectType Buffer | null
extras; // $ExpectType Buffer
return { value: 1, extras };
},
},
});
memjs.Client.create('', { username: '' }); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { password: '' }); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { timeout: 1 }); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { conntimeout: 1 }); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { keepAlive: true }); // $ExpectType Client<string | Buffer, Buffer | null>
memjs.Client.create('', { keepAliveDelay: 1 }); // $ExpectType Client<string | Buffer, Buffer | null>
new memjs.Client([]); // $ExpectType Client<string | Buffer, Buffer | null>
new memjs.Client([], {}); // $ExpectType Client<string | Buffer, Buffer | null>
client.seq; // $ExpectType number
client.servers; // $ExpectType readonly Server[]
client.options; // $ExpectType ClientOptions<string | Buffer, Buffer | null>
client.serializer; // $ExpectType Serializer<string | Buffer, Buffer | null>
numClient.options; // $ExpectType ClientOptions<number, number>
numClient.serializer; // $ExpectType Serializer<number, number>
client.get('hello', (err, val) => {
console.log(err, val);
err; // $ExpectType Error | null
val; // $ExpectType Buffer | null
});
client.get('hello'); // $ExpectType Promise<{ value: Buffer | null; flags: Buffer | null; }>
numClient.get('hello'); // $ExpectType Promise<{ value: number; flags: Buffer | null; }>
numClient.get('hello', (err, val) => {
err; // $ExpectType Error | null
val; // $ExpectType number
});
// Can initialize clients with client and server options
memjs.Client.create('12345', { username: 'hello' }); // $ExpectType Client
memjs.Client.create('12345', { retries: 3 }); // $ExpectType Client
client.set('hello', 'world'); // $ExpectType Promise<boolean>
client.set('hello', 'world', { expires: 600 }); // $ExpectType Promise<boolean>
// $ExpectType void
client.set('hello', 'world', (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
// $ExpectType void
client.set('hello', 'world', { expires: 600 }, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
numClient.set('hello', 1); // $ExpectType Promise<boolean>
numClient.set('hello', 1, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
client.add('hello', 'world'); // $ExpectType Promise<boolean>
client.add('hello', 'world', { expires: 600 }); // $ExpectType Promise<boolean>
// $ExpectType void
client.add('hello', 'world', (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
// $ExpectType void
client.add('hello', 'world', { expires: 600 }, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
numClient.add('hello', 1); // $ExpectType Promise<boolean>
numClient.add('hello', 1, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
client.replace('hello', 'world'); // $ExpectType Promise<boolean>
client.replace('hello', 'world', { expires: 600 }); // $ExpectType Promise<boolean>
// $ExpectType void
client.replace('hello', 'world', (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
// $ExpectType void
client.replace('hello', 'world', { expires: 600 }, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
numClient.replace('hello', 1); // $ExpectType Promise<boolean>
numClient.replace('hello', 1, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
client.delete('hello'); // $ExpectType Promise<boolean>
// $ExpectType void
client.delete('hello', (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
client.increment('hello', 1); // $ExpectType Promise<{ success: boolean; value?: number | null | undefined; }>
client.increment('hello', 1, { expires: 600 }); // $ExpectType Promise<{ success: boolean; value?: number | null | undefined; }>
client.increment('hello', 1, { initial: 1 }); // $ExpectType Promise<{ success: boolean; value?: number | null | undefined; }>
// $ExpectType void
client.increment('hello', 1, (err, success, value) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
value; // $ExpectType number | null | undefined
});
// $ExpectType void
client.increment('hello', 1, { expires: 600 }, (err, success, value) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
value; // $ExpectType number | null | undefined
});
// $ExpectType void
client.increment('hello', 1, { initial: 1 }, (err, success, value) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
value; // $ExpectType number | null | undefined
});
client.decrement('hello', 1); // $ExpectType Promise<{ success: boolean; value?: number | null | undefined; }>
client.decrement('hello', 1, { expires: 600 }); // $ExpectType Promise<{ success: boolean; value?: number | null | undefined; }>
client.decrement('hello', 1, { initial: 1 }); // $ExpectType Promise<{ success: boolean; value?: number | null | undefined; }>
// $ExpectType void
client.decrement('hello', 1, (err, success, value) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
value; // $ExpectType number | null | undefined
});
// $ExpectType void
client.decrement('hello', 1, { expires: 600 }, (err, success, value) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
value; // $ExpectType number | null | undefined
});
// $ExpectType void
client.decrement('hello', 1, { initial: 1 }, (err, success, value) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
value; // $ExpectType number | null | undefined
});
client.append('hello', 'world'); // $ExpectType Promise<boolean>
// $ExpectType void
client.append('hello', 'world', (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
numClient.append('hello', 1); // $ExpectType Promise<boolean>
numClient.append('hello', 1, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
client.prepend('hello', 'world'); // $ExpectType Promise<boolean>
// $ExpectType void
client.prepend('hello', 'world', (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
numClient.prepend('hello', 1); // $ExpectType Promise<boolean>
numClient.prepend('hello', 1, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
client.touch('hello'); // $ExpectType Promise<boolean>
client.touch('hello', 1); // $ExpectType Promise<boolean>
// $ExpectType void
client.touch('hello', (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
// $ExpectType void
client.touch('hello', 1, (err, success) => {
err; // $ExpectType Error | null
success; // $ExpectType boolean | null
});
client.flush(); // $ExpectType Promise<Record<string, boolean>>
// $ExpectType void
client.flush((err, results) => {
err; // $ExpectType Error | null
results; // $ExpectType Record<string, boolean | Error>
});
// $ExpectType void
client.statsWithKey('hello', (err, server, stats) => {
err; // $ExpectType Error | null
server; // $ExpectType string
stats; // $ExpectType Record<string, string> | null
});
// $ExpectType void
client.stats((err, server, stats) => {
err; // $ExpectType Error | null
server; // $ExpectType string
stats; // $ExpectType Record<string, string> | null
});
// $ExpectType void
client.resetStats((err, server, stats) => {
err; // $ExpectType Error | null
server; // $ExpectType string
stats; // $ExpectType Record<string, string> | null
});
client.quit(); // $ExpectType void
client.close(); // $ExpectType void
client.perform('hello', Buffer.alloc(10), 1); // $ExpectType void
// $ExpectType void
client.perform('hello', Buffer.alloc(10), 1, (err, ...args) => {
err; // $ExpectType Error | null
args; // $ExpectType any[]
});
// $ExpectType void
client.perform(
'hello',
Buffer.alloc(10),
1,
(err, ...args) => {
err; // $ExpectType Error | null
args; // $ExpectType any[]
},
1,
);
client.incrSeq(); // $ExpectType void
const server = new memjs.Server('foo', 123);
new memjs.Server('foo', 123, 'foo');
new memjs.Server('foo', 123, 'foo', 'bar', {});
new memjs.Server('foo', 123, undefined, undefined, { username: '' }); // $ExpectType Server
new memjs.Server('foo', 123, undefined, undefined, { password: '' }); // $ExpectType Server
new memjs.Server('foo', 123, undefined, undefined, { timeout: 1 }); // $ExpectType Server
new memjs.Server('foo', 123, undefined, undefined, { conntimeout: 1 }); // $ExpectType Server
new memjs.Server('foo', 123, undefined, undefined, { keepAlive: true }); // $ExpectType Server
new memjs.Server('foo', 123, undefined, undefined, { keepAliveDelay: 1 }); // $ExpectType Server
server.responseBuffer; // $ExpectType Buffer
server.host; // $ExpectType string
server.port; // $ExpectType number
server.connected; // $ExpectType boolean
server.timeoutSet; // $ExpectType boolean
server.connectCallbacks; // $ExpectType readonly ((socket: Socket) => void)[]
server.responseCallbacks; // $ExpectType { [key: number]: (response: Response) => void; }
server.requestTimeouts; // $ExpectType readonly number[]
server.errorCallbacks; // $ExpectType { [key: number]: (error: Error) => void; }
server.options; // $ExpectType ServerOptions
server.username; // $ExpectType string | undefined
server.password; // $ExpectType string | undefined
// $ExpectType void
server.onConnect(socket => {
socket; // $ExpectType Socket
});
// $ExpectType void
server.onResponse(1, response => {
response; // $ExpectType Response
});
// $ExpectType void
server.onError(1, error => {
error; // $ExpectType Error
});
server.error(new Error()); // $ExpectType void
server.listSasl(); // $ExpectType void
server.saslAuth(); // $ExpectType void
server.appendToBuffer(Buffer.alloc(1)); // $ExpectType Buffer
server.responseHandler(Buffer.alloc(1)); // $ExpectType void
// $ExpectType void
server.sock(true, socket => {
socket; // $ExpectType Socket
});
server.write(new Uint8Array()); // $ExpectType void
server.write('foo'); // $ExpectType void
server.writeSASL(new Uint8Array()); // $ExpectType void
server.writeSASL('foo'); // $ExpectType void
server.close(); // $ExpectType void
server.toString(); // $ExpectType string
memjs.Utils.makeRequestBuffer(1, 'foo', '123', 'bar', 1); // $ExpectType Buffer
memjs.Utils.makeRequestBuffer(1, Buffer.from('foo'), Buffer.from('123'), Buffer.from('bar'), 1);
memjs.Utils.makeAmountInitialAndExpiration(1, 0, 1); // $ExpectType Buffer
memjs.Utils.makeExpiration(1); // $ExpectType Buffer
memjs.Utils.hashCode('foo'); // $ExpectType number
const response = memjs.Utils.parseMessage(Buffer.alloc(10)); // $ExpectType Response
memjs.Utils.merge({ foo: 'bar' }, { quux: 'baz' }); // $ExpectType { foo: string; } & { quux: string; }
memjs.Utils.timestamp(); // $ExpectType number
response.header; // $ExpectType Required<Header>
response.key; // $ExpectType Buffer
response.extras; // $ExpectType Buffer
response.val; // $ExpectType Buffer
const header = memjs.Header.fromBuffer(Buffer.alloc(10)); // $ExpectType Required<Header>
header.magic; // $ExpectType number
header.opcode; // $ExpectType number
header.keyLength; // $ExpectType number
header.extrasLength; // $ExpectType number
header.dataType; // $ExpectType number
header.status; // $ExpectType number
header.totalBodyLength; // $ExpectType number
header.opaque; // $ExpectType number
header.cas; // $ExpectType Buffer
// $ExpectType Buffer
memjs.Header.toBuffer({
magic: 1,
opcode: 1,
keyLength: 1,
extrasLength: 1,
dataType: 1,
totalBodyLength: 1,
});
// $ExpectType Buffer
memjs.Header.toBuffer({
magic: 1,
opcode: 1,
keyLength: 1,
extrasLength: 1,
status: 1,
totalBodyLength: 1,
});
// $ExpectType Buffer
memjs.Header.toBuffer({
magic: 1,
opcode: 1,
keyLength: 1,
extrasLength: 1,
totalBodyLength: 1,
opaque: 1,
});
// $ExpectType Buffer
memjs.Header.toBuffer({
magic: 1,
opcode: 1,
keyLength: 1,
extrasLength: 1,
totalBodyLength: 1,
cas: Buffer.alloc(10),
});