memx 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,1147 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
25
+ var __export = (target, all) => {
26
+ for (var name in all)
27
+ __defProp(target, name, { get: all[name], enumerable: true });
28
+ };
29
+ var __reExport = (target, module2, copyDefault, desc) => {
30
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
31
+ for (let key of __getOwnPropNames(module2))
32
+ if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
33
+ __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
34
+ }
35
+ return target;
36
+ };
37
+ var __toESM = (module2, isNodeMode) => {
38
+ return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
39
+ };
40
+ var __toCommonJS = /* @__PURE__ */ ((cache) => {
41
+ return (module2, temp) => {
42
+ return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
43
+ };
44
+ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
45
+
46
+ // src/index.ts
47
+ var src_exports = {};
48
+ __export(src_exports, {
49
+ Client: () => Client,
50
+ ClusterAdapter: () => ClusterAdapter,
51
+ ServerAdapter: () => ServerAdapter,
52
+ connection: () => connection_exports,
53
+ constants: () => constants_exports,
54
+ decode: () => decode_exports,
55
+ encode: () => encode_exports
56
+ });
57
+
58
+ // src/decode.ts
59
+ var decode_exports = {};
60
+ __export(decode_exports, {
61
+ Decoder: () => Decoder
62
+ });
63
+
64
+ // src/constants.ts
65
+ var constants_exports = {};
66
+ __export(constants_exports, {
67
+ BUFFERS: () => BUFFERS,
68
+ DATA_TYPE: () => DATA_TYPE,
69
+ EMPTY_BUFFER: () => EMPTY_BUFFER,
70
+ FLAGS: () => FLAGS,
71
+ MAGIC: () => MAGIC,
72
+ OFFSETS: () => OFFSETS,
73
+ OPCODE: () => OPCODE,
74
+ STATUS: () => STATUS,
75
+ VBUCKET: () => VBUCKET
76
+ });
77
+ var EMPTY_BUFFER = Buffer.alloc(0);
78
+ var FLAGS = /* @__PURE__ */ ((FLAGS2) => {
79
+ FLAGS2[FLAGS2["BUFFER"] = 0] = "BUFFER";
80
+ FLAGS2[FLAGS2["BIGINT"] = 3402235904] = "BIGINT";
81
+ FLAGS2[FLAGS2["BOOLEAN"] = 3402235905] = "BOOLEAN";
82
+ FLAGS2[FLAGS2["NUMBER"] = 3402235906] = "NUMBER";
83
+ FLAGS2[FLAGS2["STRING"] = 3402235907] = "STRING";
84
+ FLAGS2[FLAGS2["NULL"] = 3402235918] = "NULL";
85
+ FLAGS2[FLAGS2["JSON"] = 3402235919] = "JSON";
86
+ FLAGS2[FLAGS2["UINT8ARRAY"] = 3402235920] = "UINT8ARRAY";
87
+ FLAGS2[FLAGS2["UINT8CLAMPEDARRAY"] = 3402235921] = "UINT8CLAMPEDARRAY";
88
+ FLAGS2[FLAGS2["UINT16ARRAY"] = 3402235922] = "UINT16ARRAY";
89
+ FLAGS2[FLAGS2["UINT32ARRAY"] = 3402235923] = "UINT32ARRAY";
90
+ FLAGS2[FLAGS2["INT8ARRAY"] = 3402235924] = "INT8ARRAY";
91
+ FLAGS2[FLAGS2["INT16ARRAY"] = 3402235925] = "INT16ARRAY";
92
+ FLAGS2[FLAGS2["INT32ARRAY"] = 3402235926] = "INT32ARRAY";
93
+ FLAGS2[FLAGS2["BIGUINT64ARRAY"] = 3402235927] = "BIGUINT64ARRAY";
94
+ FLAGS2[FLAGS2["BIGINT64ARRAY"] = 3402235928] = "BIGINT64ARRAY";
95
+ FLAGS2[FLAGS2["FLOAT32ARRAY"] = 3402235929] = "FLOAT32ARRAY";
96
+ FLAGS2[FLAGS2["FLOAT64ARRAY"] = 3402235930] = "FLOAT64ARRAY";
97
+ return FLAGS2;
98
+ })(FLAGS || {});
99
+ var BUFFERS = /* @__PURE__ */ ((BUFFERS2) => {
100
+ BUFFERS2[BUFFERS2["POOL_SIZE"] = 64] = "POOL_SIZE";
101
+ BUFFERS2[BUFFERS2["BUFFER_SIZE"] = 8192] = "BUFFER_SIZE";
102
+ BUFFERS2[BUFFERS2["HEADER_SIZE"] = 24] = "HEADER_SIZE";
103
+ BUFFERS2[BUFFERS2["KEY_SIZE"] = 250] = "KEY_SIZE";
104
+ BUFFERS2[BUFFERS2["KEY_TOO_BIG"] = 251] = "KEY_TOO_BIG";
105
+ return BUFFERS2;
106
+ })(BUFFERS || {});
107
+ var OFFSETS = /* @__PURE__ */ ((OFFSETS2) => {
108
+ OFFSETS2[OFFSETS2["MAGIC_$8"] = 0] = "MAGIC_$8";
109
+ OFFSETS2[OFFSETS2["OPCODE_$8"] = 1] = "OPCODE_$8";
110
+ OFFSETS2[OFFSETS2["KEY_LENGTH_$16"] = 2] = "KEY_LENGTH_$16";
111
+ OFFSETS2[OFFSETS2["EXTRAS_LENGTH_$8"] = 4] = "EXTRAS_LENGTH_$8";
112
+ OFFSETS2[OFFSETS2["DATA_TYPE_$8"] = 5] = "DATA_TYPE_$8";
113
+ OFFSETS2[OFFSETS2["STATUS_$16"] = 6] = "STATUS_$16";
114
+ OFFSETS2[OFFSETS2["BODY_LENGTH_$32"] = 8] = "BODY_LENGTH_$32";
115
+ OFFSETS2[OFFSETS2["SEQUENCE_$32"] = 12] = "SEQUENCE_$32";
116
+ OFFSETS2[OFFSETS2["CAS_$64"] = 16] = "CAS_$64";
117
+ OFFSETS2[OFFSETS2["BODY"] = 24] = "BODY";
118
+ return OFFSETS2;
119
+ })(OFFSETS || {});
120
+ var MAGIC = /* @__PURE__ */ ((MAGIC2) => {
121
+ MAGIC2[MAGIC2["REQUEST"] = 128] = "REQUEST";
122
+ MAGIC2[MAGIC2["RESPONSE"] = 129] = "RESPONSE";
123
+ return MAGIC2;
124
+ })(MAGIC || {});
125
+ var STATUS = /* @__PURE__ */ ((STATUS2) => {
126
+ STATUS2[STATUS2["OK"] = 0] = "OK";
127
+ STATUS2[STATUS2["KEY_NOT_FOUND"] = 1] = "KEY_NOT_FOUND";
128
+ STATUS2[STATUS2["KEY_EXISTS"] = 2] = "KEY_EXISTS";
129
+ STATUS2[STATUS2["TOO_LARGE"] = 3] = "TOO_LARGE";
130
+ STATUS2[STATUS2["INVALID_ARGS"] = 4] = "INVALID_ARGS";
131
+ STATUS2[STATUS2["ITEM_NOT_STORED"] = 5] = "ITEM_NOT_STORED";
132
+ STATUS2[STATUS2["NON_NUMERIC_VALUE"] = 6] = "NON_NUMERIC_VALUE";
133
+ STATUS2[STATUS2["WRONG_VBUCKET"] = 7] = "WRONG_VBUCKET";
134
+ STATUS2[STATUS2["AUTH_ERROR"] = 8] = "AUTH_ERROR";
135
+ STATUS2[STATUS2["AUTH_CONTINUE"] = 9] = "AUTH_CONTINUE";
136
+ STATUS2[STATUS2["UNKNOWN_COMMAND"] = 129] = "UNKNOWN_COMMAND";
137
+ STATUS2[STATUS2["OUT_OF_MEMORY"] = 130] = "OUT_OF_MEMORY";
138
+ STATUS2[STATUS2["NOT_SUPPORTED"] = 131] = "NOT_SUPPORTED";
139
+ STATUS2[STATUS2["INTERNAL_ERROR"] = 132] = "INTERNAL_ERROR";
140
+ STATUS2[STATUS2["BUSY"] = 133] = "BUSY";
141
+ STATUS2[STATUS2["TEMPORARY_FAILURE"] = 134] = "TEMPORARY_FAILURE";
142
+ return STATUS2;
143
+ })(STATUS || {});
144
+ var OPCODE = /* @__PURE__ */ ((OPCODE3) => {
145
+ OPCODE3[OPCODE3["GET"] = 0] = "GET";
146
+ OPCODE3[OPCODE3["SET"] = 1] = "SET";
147
+ OPCODE3[OPCODE3["ADD"] = 2] = "ADD";
148
+ OPCODE3[OPCODE3["REPLACE"] = 3] = "REPLACE";
149
+ OPCODE3[OPCODE3["DELETE"] = 4] = "DELETE";
150
+ OPCODE3[OPCODE3["INCREMENT"] = 5] = "INCREMENT";
151
+ OPCODE3[OPCODE3["DECREMENT"] = 6] = "DECREMENT";
152
+ OPCODE3[OPCODE3["QUIT"] = 7] = "QUIT";
153
+ OPCODE3[OPCODE3["FLUSH"] = 8] = "FLUSH";
154
+ OPCODE3[OPCODE3["NOOP"] = 10] = "NOOP";
155
+ OPCODE3[OPCODE3["VERSION"] = 11] = "VERSION";
156
+ OPCODE3[OPCODE3["APPEND"] = 14] = "APPEND";
157
+ OPCODE3[OPCODE3["PREPEND"] = 15] = "PREPEND";
158
+ OPCODE3[OPCODE3["STAT"] = 16] = "STAT";
159
+ OPCODE3[OPCODE3["TOUCH"] = 28] = "TOUCH";
160
+ OPCODE3[OPCODE3["GAT"] = 29] = "GAT";
161
+ return OPCODE3;
162
+ })(OPCODE || {});
163
+ var DATA_TYPE = /* @__PURE__ */ ((DATA_TYPE2) => {
164
+ DATA_TYPE2[DATA_TYPE2["RAW"] = 0] = "RAW";
165
+ return DATA_TYPE2;
166
+ })(DATA_TYPE || {});
167
+ var VBUCKET = /* @__PURE__ */ ((VBUCKET2) => {
168
+ VBUCKET2[VBUCKET2["NIL"] = 0] = "NIL";
169
+ return VBUCKET2;
170
+ })(VBUCKET || {});
171
+
172
+ // src/buffers.ts
173
+ var pool = new Array(64 /* POOL_SIZE */);
174
+ var offset = -1;
175
+ function allocateBuffer(size) {
176
+ if (size > 8192 /* BUFFER_SIZE */) {
177
+ const buffer2 = Buffer.allocUnsafeSlow(size);
178
+ buffer2.recycle = () => void 0;
179
+ return buffer2;
180
+ }
181
+ const buffer = offset >= 0 ? pool[offset--] : Buffer.allocUnsafeSlow(8192 /* BUFFER_SIZE */);
182
+ const recyclable = buffer.subarray(0, size);
183
+ let recycled = false;
184
+ recyclable.recycle = () => queueMicrotask(() => {
185
+ if (offset >= 64 /* POOL_SIZE */ || recycled)
186
+ return;
187
+ pool[++offset] = buffer;
188
+ recycled = true;
189
+ });
190
+ return recyclable;
191
+ }
192
+
193
+ // src/decode.ts
194
+ var import_assert = __toESM(require("assert"));
195
+ var Decoder = class {
196
+ #consumer;
197
+ #header = Buffer.allocUnsafeSlow(24);
198
+ #body;
199
+ #pos = 0;
200
+ constructor(consumer) {
201
+ this.#consumer = consumer;
202
+ }
203
+ append(buffer, start, end) {
204
+ const header = this.#header;
205
+ while (start < end) {
206
+ if (this.#pos < 24) {
207
+ const copied = buffer.copy(header, this.#pos, start, end);
208
+ this.#pos += copied;
209
+ start += copied;
210
+ }
211
+ if (this.#pos < 24)
212
+ return;
213
+ let extras = EMPTY_BUFFER;
214
+ let key = EMPTY_BUFFER;
215
+ let value = EMPTY_BUFFER;
216
+ let recycle = () => void 0;
217
+ const bodyLength = header.readUInt32BE(8 /* BODY_LENGTH_$32 */);
218
+ if (bodyLength) {
219
+ const body = this.#body || (this.#body = allocateBuffer(bodyLength));
220
+ const copied = buffer.copy(body, this.#pos - 24, start, end);
221
+ this.#pos += copied;
222
+ start += copied;
223
+ if (this.#pos - 24 < body.length)
224
+ return;
225
+ const keyLength = header.readUInt16BE(2 /* KEY_LENGTH_$16 */);
226
+ const extrasLength = header.readUInt8(4 /* EXTRAS_LENGTH_$8 */);
227
+ const valueLength = bodyLength - keyLength - extrasLength;
228
+ key = keyLength ? body.subarray(extrasLength, extrasLength + keyLength) : EMPTY_BUFFER;
229
+ value = valueLength ? body.subarray(extrasLength + keyLength) : EMPTY_BUFFER;
230
+ extras = extrasLength ? body.subarray(0, extrasLength) : EMPTY_BUFFER;
231
+ recycle = () => void body.recycle();
232
+ }
233
+ const packet = {
234
+ opcode: header.readUInt8(1 /* OPCODE_$8 */),
235
+ status: header.readUInt16BE(6 /* STATUS_$16 */),
236
+ sequence: header.readUInt32BE(12 /* SEQUENCE_$32 */),
237
+ cas: header.readBigUInt64BE(16 /* CAS_$64 */),
238
+ extras,
239
+ key,
240
+ value,
241
+ recycle
242
+ };
243
+ const magic = header.readUInt8(0 /* MAGIC_$8 */);
244
+ const dataType = header.readUInt8(5 /* DATA_TYPE_$8 */);
245
+ import_assert.default.equal(magic, 129 /* RESPONSE */, "Invalid magic in header");
246
+ import_assert.default.equal(dataType, 0 /* RAW */, "Invalid data type in header");
247
+ this.#pos = 0;
248
+ this.#body = void 0;
249
+ this.#consumer(packet);
250
+ }
251
+ }
252
+ };
253
+
254
+ // src/encode.ts
255
+ var encode_exports = {};
256
+ __export(encode_exports, {
257
+ Encoder: () => Encoder
258
+ });
259
+ var CONSTANTS = /* @__PURE__ */ ((CONSTANTS2) => {
260
+ CONSTANTS2[CONSTANTS2["HEADER_SIZE"] = 24] = "HEADER_SIZE";
261
+ return CONSTANTS2;
262
+ })(CONSTANTS || {});
263
+ var Encoder = class {
264
+ encode(packet, seq = 0) {
265
+ const {
266
+ opcode,
267
+ sequence = seq,
268
+ cas = 0n,
269
+ extras = EMPTY_BUFFER,
270
+ extrasOffset = 0,
271
+ extrasLength = extras.length,
272
+ key = EMPTY_BUFFER,
273
+ keyOffset = 0,
274
+ keyLength = key.length,
275
+ value = EMPTY_BUFFER,
276
+ valueOffset = 0,
277
+ valueLength = value.length
278
+ } = packet;
279
+ const bodyLength = extrasLength + keyLength + valueLength;
280
+ const length = bodyLength + 24 /* HEADER_SIZE */;
281
+ const buffer = allocateBuffer(length);
282
+ buffer.writeUInt8(128 /* REQUEST */, 0 /* MAGIC_$8 */);
283
+ buffer.writeUInt8(opcode, 1 /* OPCODE_$8 */);
284
+ buffer.writeUInt16BE(keyLength, 2 /* KEY_LENGTH_$16 */);
285
+ buffer.writeUInt8(extrasLength, 4 /* EXTRAS_LENGTH_$8 */);
286
+ buffer.writeUInt8(0 /* RAW */, 5 /* DATA_TYPE_$8 */);
287
+ buffer.writeUInt16BE(0 /* NIL */, 6 /* STATUS_$16 */);
288
+ buffer.writeUInt32BE(bodyLength, 8 /* BODY_LENGTH_$32 */);
289
+ buffer.writeUInt32BE(sequence, 12 /* SEQUENCE_$32 */);
290
+ buffer.writeBigUInt64BE(cas, 16 /* CAS_$64 */);
291
+ let pos = 24;
292
+ if (extrasLength)
293
+ pos += extras.copy(buffer, pos, extrasOffset, extrasOffset + extrasLength);
294
+ if (keyLength)
295
+ pos += key.copy(buffer, pos, keyOffset, keyOffset + keyLength);
296
+ if (valueLength)
297
+ pos += value.copy(buffer, pos, valueOffset, valueOffset + valueLength);
298
+ return buffer;
299
+ }
300
+ };
301
+
302
+ // src/connection.ts
303
+ var connection_exports = {};
304
+ __export(connection_exports, {
305
+ Connection: () => Connection
306
+ });
307
+
308
+ // src/internals.ts
309
+ var import_assert2 = __toESM(require("assert"));
310
+ var import_types = require("util/types");
311
+ var socketFinalizationRegistry = new FinalizationRegistry((socket) => {
312
+ if (!socket.destroyed)
313
+ socket.destroy();
314
+ });
315
+ function typedArrayFlags(value) {
316
+ const flags = (0, import_types.isUint8Array)(value) ? 3402235920 /* UINT8ARRAY */ : (0, import_types.isUint8ClampedArray)(value) ? 3402235921 /* UINT8CLAMPEDARRAY */ : (0, import_types.isUint16Array)(value) ? 3402235922 /* UINT16ARRAY */ : (0, import_types.isUint32Array)(value) ? 3402235923 /* UINT32ARRAY */ : (0, import_types.isInt8Array)(value) ? 3402235924 /* INT8ARRAY */ : (0, import_types.isInt16Array)(value) ? 3402235925 /* INT16ARRAY */ : (0, import_types.isInt32Array)(value) ? 3402235926 /* INT32ARRAY */ : (0, import_types.isBigUint64Array)(value) ? 3402235927 /* BIGUINT64ARRAY */ : (0, import_types.isBigInt64Array)(value) ? 3402235928 /* BIGINT64ARRAY */ : (0, import_types.isFloat32Array)(value) ? 3402235929 /* FLOAT32ARRAY */ : (0, import_types.isFloat64Array)(value) ? 3402235930 /* FLOAT64ARRAY */ : import_assert2.default.fail("Unsupported kind of TypedArray");
317
+ return flags;
318
+ }
319
+
320
+ // src/connection.ts
321
+ var import_assert3 = __toESM(require("assert"));
322
+ var import_net = __toESM(require("net"));
323
+ var Deferred = class {
324
+ #resolve;
325
+ #reject;
326
+ #packets = [];
327
+ promise;
328
+ opcode;
329
+ constructor(opcode) {
330
+ this.opcode = opcode;
331
+ this.promise = new Promise((resolve, reject) => {
332
+ this.#resolve = resolve;
333
+ this.#reject = reject;
334
+ });
335
+ }
336
+ append(packet) {
337
+ this.#packets.push(packet);
338
+ }
339
+ resolve(packet) {
340
+ this.#packets.push(packet);
341
+ this.#resolve(this.#packets);
342
+ }
343
+ reject(error) {
344
+ this.#reject(error);
345
+ }
346
+ };
347
+ var Connection = class {
348
+ #decoder = new Decoder((packet) => this.#receive(packet));
349
+ #encoder = new Encoder();
350
+ #buffer = Buffer.allocUnsafeSlow(8192 /* BUFFER_SIZE */);
351
+ #defers = /* @__PURE__ */ new Map();
352
+ #factory;
353
+ #socket;
354
+ #sequence = 0;
355
+ #timeout;
356
+ #host;
357
+ #port;
358
+ constructor(options) {
359
+ (0, import_assert3.default)(options, "No options specified");
360
+ const {
361
+ host,
362
+ port = 11211,
363
+ timeout = 1e3,
364
+ factory = import_net.default.connect
365
+ } = options;
366
+ this.#factory = factory;
367
+ (0, import_assert3.default)(host, "No host name specified");
368
+ (0, import_assert3.default)(port > 0 && port < 65536 && Math.floor(port) == port, `Invalid port ${port}`);
369
+ this.#timeout = timeout;
370
+ this.#host = host;
371
+ this.#port = port;
372
+ }
373
+ get connected() {
374
+ return !!this.#socket;
375
+ }
376
+ get host() {
377
+ return this.#host;
378
+ }
379
+ get port() {
380
+ return this.#port;
381
+ }
382
+ get timeout() {
383
+ return this.#timeout;
384
+ }
385
+ #connect() {
386
+ return this.#socket || (this.#socket = new Promise((resolve, reject) => {
387
+ const socket = this.#factory({
388
+ host: this.#host,
389
+ port: this.#port,
390
+ timeout: this.#timeout,
391
+ onread: {
392
+ buffer: this.#buffer,
393
+ callback: (bytes, buffer) => {
394
+ this.#decoder.append(buffer, 0, bytes);
395
+ return true;
396
+ }
397
+ }
398
+ });
399
+ socket.on("timeout", () => socket.destroy(new Error("Timeout")));
400
+ socket.on("error", reject);
401
+ socket.on("close", () => {
402
+ socketFinalizationRegistry.unregister(this);
403
+ this.#socket = void 0;
404
+ });
405
+ socket.on("connect", () => {
406
+ socketFinalizationRegistry.register(this, socket, this);
407
+ socket.off("error", reject);
408
+ socket.on("error", (error) => {
409
+ for (const deferred of this.#defers.values()) {
410
+ process.nextTick(() => deferred.reject(error));
411
+ }
412
+ this.#defers.clear();
413
+ this.#socket = void 0;
414
+ });
415
+ socket.unref();
416
+ resolve(socket);
417
+ });
418
+ }));
419
+ }
420
+ #receive(packet) {
421
+ const deferred = this.#defers.get(packet.sequence);
422
+ if (deferred) {
423
+ if (deferred.opcode === packet.opcode) {
424
+ if (packet.opcode === 16 /* STAT */ && packet.key.length !== 0) {
425
+ return deferred.append(packet);
426
+ }
427
+ return deferred.resolve(packet);
428
+ } else {
429
+ const sent = `0x${deferred.opcode.toString(16).padStart(2, "0")}`;
430
+ const received = `0x${packet.opcode.toString(16).padStart(2, "0")}`;
431
+ return deferred.reject(new Error(`Opcode mismatch (sent=${sent}, received=${received})`));
432
+ }
433
+ }
434
+ }
435
+ async send(packet) {
436
+ const sequence = ++this.#sequence;
437
+ const buffer = this.#encoder.encode(packet, sequence);
438
+ const deferred = new Deferred(packet.opcode);
439
+ this.#defers.set(sequence, deferred);
440
+ const socket = await this.#connect();
441
+ socket.write(buffer, (error) => {
442
+ buffer.recycle();
443
+ if (error)
444
+ return deferred.reject(error);
445
+ });
446
+ const timeout = setTimeout(() => deferred.reject(new Error("No response")), this.#timeout);
447
+ return deferred.promise.finally(() => {
448
+ clearTimeout(timeout);
449
+ this.#defers.delete(sequence);
450
+ });
451
+ }
452
+ async destroy() {
453
+ const socket = await this.#socket;
454
+ if (!socket)
455
+ return false;
456
+ return new Promise((resolve, reject) => {
457
+ socket.once("error", reject).once("close", resolve).destroy();
458
+ });
459
+ }
460
+ };
461
+
462
+ // src/server.ts
463
+ var statsBigInt = [
464
+ "auth_cmds",
465
+ "auth_errors",
466
+ "bytes",
467
+ "bytes_read",
468
+ "bytes_written",
469
+ "cas_badval",
470
+ "cas_hits",
471
+ "cas_misses",
472
+ "cmd_flush",
473
+ "cmd_get",
474
+ "cmd_set",
475
+ "cmd_touch",
476
+ "conn_yields",
477
+ "crawler_items_checked",
478
+ "crawler_reclaimed",
479
+ "curr_items",
480
+ "decr_hits",
481
+ "decr_misses",
482
+ "delete_hits",
483
+ "delete_misses",
484
+ "direct_reclaims",
485
+ "evicted_active",
486
+ "evicted_unfetched",
487
+ "evictions",
488
+ "expired_unfetched",
489
+ "get_expired",
490
+ "get_flushed",
491
+ "get_hits",
492
+ "get_misses",
493
+ "hash_bytes",
494
+ "idle_kicks",
495
+ "incr_hits",
496
+ "incr_misses",
497
+ "limit_maxbytes",
498
+ "listen_disabled_num",
499
+ "log_watcher_sent",
500
+ "log_watcher_skipped",
501
+ "log_watchers",
502
+ "log_worker_dropped",
503
+ "log_worker_written",
504
+ "lru_crawler_running",
505
+ "lru_crawler_starts",
506
+ "lru_maintainer_juggles",
507
+ "lrutail_reflocked",
508
+ "malloc_fails",
509
+ "moves_to_cold",
510
+ "moves_to_warm",
511
+ "moves_within_lru",
512
+ "read_buf_bytes",
513
+ "read_buf_bytes_free",
514
+ "read_buf_count",
515
+ "read_buf_oom",
516
+ "reclaimed",
517
+ "rejected_connections",
518
+ "response_obj_bytes",
519
+ "response_obj_count",
520
+ "response_obj_oom",
521
+ "round_robin_fallback",
522
+ "slab_reassign_busy_deletes",
523
+ "slab_reassign_busy_items",
524
+ "slab_reassign_chunk_rescues",
525
+ "slab_reassign_evictions_nomem",
526
+ "slab_reassign_inline_reclaim",
527
+ "slab_reassign_rescues",
528
+ "slabs_moved",
529
+ "store_no_memory",
530
+ "store_too_large",
531
+ "time_in_listen_disabled_us",
532
+ "total_items",
533
+ "touch_hits",
534
+ "touch_misses",
535
+ "unexpected_napi_ids"
536
+ ];
537
+ var statsNumber = [
538
+ "connection_structures",
539
+ "curr_connections",
540
+ "hash_power_level",
541
+ "max_connections",
542
+ "pid",
543
+ "pointer_size",
544
+ "reserved_fds",
545
+ "slab_global_page_pool",
546
+ "threads",
547
+ "time",
548
+ "total_connections",
549
+ "uptime"
550
+ ];
551
+ var statsBoolean = ["accepting_conns", "hash_is_expanding", "slab_reassign_running"];
552
+ var statsMicroseconds = ["rusage_system", "rusage_user"];
553
+ function injectStats(key, value, stats) {
554
+ if (!key)
555
+ return stats;
556
+ if (statsBigInt.includes(key)) {
557
+ stats[key] = BigInt(value);
558
+ } else if (statsNumber.includes(key)) {
559
+ stats[key] = Number(value);
560
+ } else if (statsBoolean.includes(key)) {
561
+ stats[key] = !!Number(value);
562
+ } else if (statsMicroseconds.includes(key)) {
563
+ const splits = value.split(".");
564
+ stats[key] = BigInt(`${splits[0]}${splits[1].padEnd(6, "0")}`);
565
+ } else {
566
+ stats[key] = value;
567
+ }
568
+ return stats;
569
+ }
570
+ function fail(packet, key) {
571
+ const message = packet.value.toString("utf-8") || "Unknown Error";
572
+ const status = STATUS[packet.status] || `0x${packet.status.toString(16).padStart(4, "0")}`;
573
+ throw new Error(`${message} (status=${status}${key ? `, key=${key}` : ""})`);
574
+ }
575
+ var ServerAdapter = class {
576
+ #buffer = Buffer.alloc(251 /* KEY_TOO_BIG */ + 20);
577
+ #connection;
578
+ #ttl;
579
+ #id;
580
+ constructor(options) {
581
+ this.#connection = new Connection(options);
582
+ this.#ttl = options.ttl || 0;
583
+ this.#id = `${this.#connection.host}:${this.#connection.port}`;
584
+ }
585
+ get connected() {
586
+ return this.#connection.connected;
587
+ }
588
+ get host() {
589
+ return this.#connection.host;
590
+ }
591
+ get port() {
592
+ return this.#connection.port;
593
+ }
594
+ get timeout() {
595
+ return this.#connection.timeout;
596
+ }
597
+ get ttl() {
598
+ return this.#ttl;
599
+ }
600
+ get id() {
601
+ return this.#id;
602
+ }
603
+ #writeKey(key, offset2 = 0) {
604
+ const keyLength = this.#buffer.write(key, offset2, 251 /* KEY_TOO_BIG */, "utf-8");
605
+ if (keyLength > 250 /* KEY_SIZE */)
606
+ throw new Error(`Key too long (len=${keyLength})`);
607
+ return keyLength;
608
+ }
609
+ async get(key, options = {}) {
610
+ const { ttl } = options;
611
+ let keyOffset = 0;
612
+ if (ttl)
613
+ keyOffset = this.#buffer.writeUInt32BE(ttl);
614
+ const keyLength = this.#writeKey(key, keyOffset);
615
+ const [response] = await this.#connection.send({
616
+ opcode: ttl ? 29 /* GAT */ : 0 /* GET */,
617
+ extras: this.#buffer,
618
+ extrasOffset: 0,
619
+ extrasLength: keyOffset,
620
+ key: this.#buffer,
621
+ keyOffset,
622
+ keyLength
623
+ });
624
+ switch (response.status) {
625
+ case 0 /* OK */:
626
+ return {
627
+ value: response.value,
628
+ flags: response.extras.readUInt32BE(),
629
+ cas: response.cas,
630
+ recycle: () => response.recycle()
631
+ };
632
+ case 1 /* KEY_NOT_FOUND */:
633
+ return;
634
+ default:
635
+ fail(response, key);
636
+ }
637
+ }
638
+ async touch(key, options = {}) {
639
+ const { ttl = this.#ttl } = options;
640
+ const keyOffset = this.#buffer.writeUInt32BE(ttl);
641
+ const keyLength = this.#writeKey(key, keyOffset);
642
+ const [response] = await this.#connection.send({
643
+ opcode: 28 /* TOUCH */,
644
+ extras: this.#buffer,
645
+ extrasOffset: 0,
646
+ extrasLength: keyOffset,
647
+ key: this.#buffer,
648
+ keyOffset,
649
+ keyLength
650
+ });
651
+ try {
652
+ switch (response.status) {
653
+ case 0 /* OK */:
654
+ return true;
655
+ case 1 /* KEY_NOT_FOUND */:
656
+ return false;
657
+ default:
658
+ fail(response, key);
659
+ }
660
+ } finally {
661
+ response.recycle();
662
+ }
663
+ }
664
+ async #sar(opcode, key, value, options) {
665
+ const { flags = 0, cas = 0n, ttl = this.#ttl } = options;
666
+ let keyOffset;
667
+ keyOffset = this.#buffer.writeUInt32BE(flags);
668
+ keyOffset = this.#buffer.writeUInt32BE(ttl, keyOffset);
669
+ const keyLength = this.#writeKey(key, keyOffset);
670
+ const [response] = await this.#connection.send({
671
+ opcode,
672
+ cas,
673
+ extras: this.#buffer,
674
+ extrasOffset: 0,
675
+ extrasLength: keyOffset,
676
+ key: this.#buffer,
677
+ keyOffset,
678
+ keyLength,
679
+ value
680
+ });
681
+ try {
682
+ switch (response.status) {
683
+ case 0 /* OK */:
684
+ return response.cas;
685
+ case 1 /* KEY_NOT_FOUND */:
686
+ case 2 /* KEY_EXISTS */:
687
+ return;
688
+ default:
689
+ fail(response, key);
690
+ }
691
+ } finally {
692
+ response.recycle();
693
+ }
694
+ }
695
+ set(key, value, options = {}) {
696
+ return this.#sar(1 /* SET */, key, value, options);
697
+ }
698
+ add(key, value, options = {}) {
699
+ return this.#sar(2 /* ADD */, key, value, options);
700
+ }
701
+ replace(key, value, options = {}) {
702
+ return this.#sar(3 /* REPLACE */, key, value, options);
703
+ }
704
+ async #pend(opcode, key, value, options) {
705
+ const { cas = 0n } = options;
706
+ const keyLength = this.#writeKey(key);
707
+ const [response] = await this.#connection.send({
708
+ opcode,
709
+ cas,
710
+ key: Buffer.from(key, "utf-8"),
711
+ keyOffset: 0,
712
+ keyLength,
713
+ value
714
+ });
715
+ try {
716
+ switch (response.status) {
717
+ case 0 /* OK */:
718
+ return true;
719
+ case 5 /* ITEM_NOT_STORED */:
720
+ case 2 /* KEY_EXISTS */:
721
+ return false;
722
+ default:
723
+ fail(response, key);
724
+ }
725
+ } finally {
726
+ response.recycle();
727
+ }
728
+ }
729
+ append(key, value, options = {}) {
730
+ return this.#pend(14 /* APPEND */, key, value, options);
731
+ }
732
+ prepend(key, value, options = {}) {
733
+ return this.#pend(15 /* PREPEND */, key, value, options);
734
+ }
735
+ async #counter(opcode, key, delta, options) {
736
+ const {
737
+ initial,
738
+ cas = 0n,
739
+ ttl = this.#ttl
740
+ } = options;
741
+ let keyOffset;
742
+ keyOffset = this.#buffer.writeBigUInt64BE(BigInt(delta));
743
+ keyOffset = this.#buffer.writeBigUInt64BE(BigInt(initial || 0n), keyOffset);
744
+ keyOffset = this.#buffer.writeUInt32BE(initial == void 0 ? 4294967295 : ttl, keyOffset);
745
+ const keyLength = this.#writeKey(key, keyOffset);
746
+ const [response] = await this.#connection.send({
747
+ opcode,
748
+ extras: this.#buffer,
749
+ extrasOffset: 0,
750
+ extrasLength: keyOffset,
751
+ key: this.#buffer,
752
+ keyOffset,
753
+ keyLength,
754
+ cas
755
+ });
756
+ try {
757
+ switch (response.status) {
758
+ case 0 /* OK */:
759
+ return {
760
+ value: response.value.readBigUInt64BE(0),
761
+ cas: response.cas
762
+ };
763
+ case 1 /* KEY_NOT_FOUND */:
764
+ case 2 /* KEY_EXISTS */:
765
+ return;
766
+ default:
767
+ fail(response, key);
768
+ }
769
+ } finally {
770
+ response.recycle();
771
+ }
772
+ }
773
+ increment(key, delta = 1, options = {}) {
774
+ return this.#counter(5 /* INCREMENT */, key, delta, options);
775
+ }
776
+ decrement(key, delta = 1, options = {}) {
777
+ return this.#counter(6 /* DECREMENT */, key, delta, options);
778
+ }
779
+ async delete(key, options = {}) {
780
+ const { cas = 0n } = options;
781
+ const keyLength = this.#writeKey(key);
782
+ const [response] = await this.#connection.send({
783
+ opcode: 4 /* DELETE */,
784
+ key: this.#buffer,
785
+ keyOffset: 0,
786
+ keyLength,
787
+ cas
788
+ });
789
+ try {
790
+ switch (response.status) {
791
+ case 0 /* OK */:
792
+ return true;
793
+ case 1 /* KEY_NOT_FOUND */:
794
+ case 2 /* KEY_EXISTS */:
795
+ return false;
796
+ default:
797
+ fail(response, key);
798
+ }
799
+ } finally {
800
+ response.recycle();
801
+ }
802
+ }
803
+ async flush(ttl = 0) {
804
+ const extrasLength = ttl ? this.#buffer.writeUInt32BE(ttl) : 0;
805
+ const [response] = await this.#connection.send({
806
+ opcode: 8 /* FLUSH */,
807
+ extras: this.#buffer,
808
+ extrasOffset: 0,
809
+ extrasLength
810
+ });
811
+ try {
812
+ switch (response.status) {
813
+ case 0 /* OK */:
814
+ return;
815
+ default:
816
+ fail(response);
817
+ }
818
+ } finally {
819
+ response.recycle();
820
+ }
821
+ }
822
+ async noop() {
823
+ const [response] = await this.#connection.send({
824
+ opcode: 10 /* NOOP */
825
+ });
826
+ try {
827
+ switch (response.status) {
828
+ case 0 /* OK */:
829
+ return;
830
+ default:
831
+ fail(response);
832
+ }
833
+ } finally {
834
+ response.recycle();
835
+ }
836
+ }
837
+ async quit() {
838
+ const [response] = await this.#connection.send({
839
+ opcode: 7 /* QUIT */
840
+ });
841
+ try {
842
+ switch (response.status) {
843
+ case 0 /* OK */:
844
+ return;
845
+ default:
846
+ fail(response);
847
+ }
848
+ } finally {
849
+ response.recycle();
850
+ }
851
+ }
852
+ async version() {
853
+ const [response] = await this.#connection.send({
854
+ opcode: 11 /* VERSION */
855
+ });
856
+ try {
857
+ switch (response.status) {
858
+ case 0 /* OK */:
859
+ return { [this.#id]: response.value.toString("utf-8") };
860
+ default:
861
+ fail(response);
862
+ }
863
+ } finally {
864
+ response.recycle();
865
+ }
866
+ }
867
+ async stats() {
868
+ const responses = await this.#connection.send({
869
+ opcode: 16 /* STAT */
870
+ });
871
+ const stats = responses.reduce((result, packet) => {
872
+ try {
873
+ if (packet.status !== 0 /* OK */)
874
+ fail(packet);
875
+ const key = packet.key.toString("utf-8");
876
+ const value = packet.value.toString("utf-8");
877
+ return injectStats(key, value, result);
878
+ } finally {
879
+ packet.recycle();
880
+ }
881
+ }, {});
882
+ return { [this.#id]: stats };
883
+ }
884
+ };
885
+
886
+ // src/cluster.ts
887
+ function parseHosts(hosts) {
888
+ const result = [];
889
+ if (!hosts)
890
+ return result;
891
+ for (const part of hosts.split(",")) {
892
+ const [host, p] = part.split(":");
893
+ const port = parseInt(p) || void 0;
894
+ result.push({ host, port });
895
+ }
896
+ return result;
897
+ }
898
+ var ClusterAdapter = class {
899
+ servers;
900
+ constructor(serversOrOptions) {
901
+ if (Array.isArray(serversOrOptions)) {
902
+ this.servers = [...serversOrOptions];
903
+ } else if (serversOrOptions) {
904
+ const { ttl, timeout, hosts: defs } = serversOrOptions;
905
+ const hosts = [];
906
+ if (Array.isArray(defs)) {
907
+ defs.forEach((def) => {
908
+ if (typeof def === "string")
909
+ hosts.push(...parseHosts(def));
910
+ else
911
+ hosts.push(__spreadValues({ port: 11211 }, def));
912
+ });
913
+ } else {
914
+ hosts.push(...parseHosts(defs));
915
+ }
916
+ this.servers = hosts.map((host) => new ServerAdapter(__spreadValues({ ttl, timeout }, host)));
917
+ } else {
918
+ const hosts = parseHosts(process.env.MEMCACHED_HOSTS);
919
+ const ttl = process.env.MEMCACHED_TTL && parseInt(process.env.MEMCACHED_TTL) || void 0;
920
+ const timeout = process.env.MEMCACHED_TIMEOUT && parseInt(process.env.MEMCACHED_TIMEOUT) || void 0;
921
+ this.servers = hosts.map((host) => new ServerAdapter(__spreadValues({ ttl, timeout }, host)));
922
+ }
923
+ if (this.servers.length < 1)
924
+ throw new Error("No hosts configured");
925
+ if (this.servers.length === 1)
926
+ this.server = () => this.servers[0];
927
+ Object.freeze(this.servers);
928
+ }
929
+ server(key) {
930
+ const length = key.length;
931
+ let hash = 0;
932
+ for (let i = 0; i < length; i++)
933
+ hash = hash * 31 + key.charCodeAt(i);
934
+ return this.servers[hash % this.servers.length];
935
+ }
936
+ get(key, options) {
937
+ return this.server(key).get(key, options);
938
+ }
939
+ touch(key, options) {
940
+ return this.server(key).touch(key, options);
941
+ }
942
+ set(key, value, options) {
943
+ return this.server(key).set(key, value, options);
944
+ }
945
+ add(key, value, options) {
946
+ return this.server(key).add(key, value, options);
947
+ }
948
+ replace(key, value, options) {
949
+ return this.server(key).replace(key, value, options);
950
+ }
951
+ append(key, value, options) {
952
+ return this.server(key).append(key, value, options);
953
+ }
954
+ prepend(key, value, options) {
955
+ return this.server(key).prepend(key, value, options);
956
+ }
957
+ increment(key, delta, options) {
958
+ return this.server(key).increment(key, delta, options);
959
+ }
960
+ decrement(key, delta, options) {
961
+ return this.server(key).decrement(key, delta, options);
962
+ }
963
+ delete(key, options) {
964
+ return this.server(key).delete(key, options);
965
+ }
966
+ async flush(ttl) {
967
+ await Promise.all(this.servers.map((server) => server.flush(ttl)));
968
+ }
969
+ async noop() {
970
+ await Promise.all(this.servers.map((server) => server.noop()));
971
+ }
972
+ async quit() {
973
+ await Promise.all(this.servers.map((server) => server.quit()));
974
+ }
975
+ async version() {
976
+ const versions = await Promise.all(this.servers.map((server) => server.version()));
977
+ return versions.reduce((v1, v2) => __spreadValues(__spreadValues({}, v1), v2));
978
+ }
979
+ async stats() {
980
+ const stats = await Promise.all(this.servers.map((server) => server.stats()));
981
+ return stats.reduce((v1, v2) => __spreadValues(__spreadValues({}, v1), v2));
982
+ }
983
+ };
984
+
985
+ // src/client.ts
986
+ var import_assert4 = __toESM(require("assert"));
987
+ var import_types4 = require("util/types");
988
+ function toBuffer(value, options) {
989
+ if (Buffer.isBuffer(value))
990
+ return [value, __spreadProps(__spreadValues({}, options), { flags: 0 /* BUFFER */ })];
991
+ switch (typeof value) {
992
+ case "bigint":
993
+ return [Buffer.from(value.toString(), "utf-8"), __spreadProps(__spreadValues({}, options), { flags: 3402235904 /* BIGINT */ })];
994
+ case "boolean":
995
+ return [Buffer.alloc(1, value ? 255 : 0), __spreadProps(__spreadValues({}, options), { flags: 3402235905 /* BOOLEAN */ })];
996
+ case "number":
997
+ return [Buffer.from(value.toString(), "utf-8"), __spreadProps(__spreadValues({}, options), { flags: 3402235906 /* NUMBER */ })];
998
+ case "string":
999
+ return [Buffer.from(value, "utf-8"), __spreadProps(__spreadValues({}, options), { flags: 3402235907 /* STRING */ })];
1000
+ case "object":
1001
+ break;
1002
+ default:
1003
+ import_assert4.default.fail(`Unable to store value of type "${typeof value}"`);
1004
+ }
1005
+ if ((0, import_types4.isTypedArray)(value)) {
1006
+ const flags = typedArrayFlags(value);
1007
+ const buffer = Buffer.from(value.buffer, value.byteOffset, value.byteLength);
1008
+ return [buffer, __spreadProps(__spreadValues({}, options), { flags })];
1009
+ }
1010
+ if (value === null)
1011
+ return [EMPTY_BUFFER, __spreadProps(__spreadValues({}, options), { flags: 3402235918 /* NULL */ })];
1012
+ return [Buffer.from(JSON.stringify(value), "utf-8"), __spreadProps(__spreadValues({}, options), { flags: 3402235919 /* JSON */ })];
1013
+ }
1014
+ function makeTypedArray(constructor, source, bytesPerValue) {
1015
+ const clone = Buffer.from(source);
1016
+ const { buffer, byteOffset, byteLength } = clone;
1017
+ return new constructor(buffer, byteOffset, byteLength / bytesPerValue);
1018
+ }
1019
+ var Client = class {
1020
+ #adapter;
1021
+ constructor(adapterOrOptions) {
1022
+ if (!adapterOrOptions) {
1023
+ this.#adapter = new ClusterAdapter();
1024
+ } else if ("get" in adapterOrOptions) {
1025
+ this.#adapter = adapterOrOptions;
1026
+ } else if ("hosts" in adapterOrOptions) {
1027
+ this.#adapter = new ClusterAdapter(adapterOrOptions);
1028
+ }
1029
+ (0, import_assert4.default)(this.#adapter, "Invalid client constructor arguments");
1030
+ }
1031
+ get adapter() {
1032
+ return this.#adapter;
1033
+ }
1034
+ async get(key, options) {
1035
+ const result = await this.#adapter.get(key, options);
1036
+ if (!result)
1037
+ return;
1038
+ try {
1039
+ const { flags, value, cas } = result;
1040
+ switch (flags) {
1041
+ case 3402235904 /* BIGINT */:
1042
+ return { value: BigInt(value.toString("utf-8")), cas };
1043
+ case 3402235905 /* BOOLEAN */:
1044
+ return { value: !!value[0], cas };
1045
+ case 3402235906 /* NUMBER */:
1046
+ return { value: Number(value.toString("utf-8")), cas };
1047
+ case 3402235907 /* STRING */:
1048
+ return { value: value.toString("utf-8"), cas };
1049
+ case 3402235918 /* NULL */:
1050
+ return { value: null, cas };
1051
+ case 3402235919 /* JSON */:
1052
+ return { value: JSON.parse(value.toString("utf-8")), cas };
1053
+ case 3402235920 /* UINT8ARRAY */:
1054
+ return { value: makeTypedArray(Uint8Array, value, 1), cas };
1055
+ case 3402235921 /* UINT8CLAMPEDARRAY */:
1056
+ return { value: makeTypedArray(Uint8ClampedArray, value, 1), cas };
1057
+ case 3402235922 /* UINT16ARRAY */:
1058
+ return { value: makeTypedArray(Uint16Array, value, 2), cas };
1059
+ case 3402235923 /* UINT32ARRAY */:
1060
+ return { value: makeTypedArray(Uint32Array, value, 4), cas };
1061
+ case 3402235924 /* INT8ARRAY */:
1062
+ return { value: makeTypedArray(Int8Array, value, 1), cas };
1063
+ case 3402235925 /* INT16ARRAY */:
1064
+ return { value: makeTypedArray(Int16Array, value, 2), cas };
1065
+ case 3402235926 /* INT32ARRAY */:
1066
+ return { value: makeTypedArray(Int32Array, value, 4), cas };
1067
+ case 3402235927 /* BIGUINT64ARRAY */:
1068
+ return { value: makeTypedArray(BigUint64Array, value, 8), cas };
1069
+ case 3402235928 /* BIGINT64ARRAY */:
1070
+ return { value: makeTypedArray(BigInt64Array, value, 8), cas };
1071
+ case 3402235929 /* FLOAT32ARRAY */:
1072
+ return { value: makeTypedArray(Float32Array, value, 4), cas };
1073
+ case 3402235930 /* FLOAT64ARRAY */:
1074
+ return { value: makeTypedArray(Float64Array, value, 8), cas };
1075
+ case 0 /* BUFFER */:
1076
+ default:
1077
+ return { value: Buffer.from(value), cas };
1078
+ }
1079
+ } finally {
1080
+ result.recycle();
1081
+ }
1082
+ }
1083
+ async set(key, value, options) {
1084
+ return this.#adapter.set(key, ...toBuffer(value, options));
1085
+ }
1086
+ async add(key, value, options) {
1087
+ return this.#adapter.add(key, ...toBuffer(value, options));
1088
+ }
1089
+ async replace(key, value, options) {
1090
+ return this.#adapter.replace(key, ...toBuffer(value, options));
1091
+ }
1092
+ append(key, value, options) {
1093
+ return this.#adapter.append(key, ...toBuffer(value, options));
1094
+ }
1095
+ prepend(key, value, options) {
1096
+ return this.#adapter.prepend(key, ...toBuffer(value, options));
1097
+ }
1098
+ async increment(key, delta, options) {
1099
+ const counter = await this.#adapter.increment(key, delta, options);
1100
+ if (counter && options && options.initial !== void 0 && counter.value === BigInt(options.initial)) {
1101
+ const cas = await this.replace(key, counter.value, { cas: counter.cas, ttl: options.ttl });
1102
+ counter.cas = cas ?? counter.cas;
1103
+ }
1104
+ return counter;
1105
+ }
1106
+ async decrement(key, delta, options) {
1107
+ const counter = await this.#adapter.decrement(key, delta, options);
1108
+ if (counter && options && options.initial !== void 0 && counter.value === BigInt(options.initial)) {
1109
+ const cas = await this.replace(key, counter.value, { cas: counter.cas, ttl: options.ttl });
1110
+ counter.cas = cas ?? counter.cas;
1111
+ }
1112
+ return counter;
1113
+ }
1114
+ touch(key, options) {
1115
+ return this.#adapter.touch(key, options);
1116
+ }
1117
+ delete(key, options) {
1118
+ return this.#adapter.delete(key, options);
1119
+ }
1120
+ flush(ttl) {
1121
+ return this.#adapter.flush(ttl);
1122
+ }
1123
+ noop() {
1124
+ return this.#adapter.noop();
1125
+ }
1126
+ quit() {
1127
+ return this.#adapter.quit();
1128
+ }
1129
+ version() {
1130
+ return this.#adapter.version();
1131
+ }
1132
+ stats() {
1133
+ return this.#adapter.stats();
1134
+ }
1135
+ };
1136
+ module.exports = __toCommonJS(src_exports);
1137
+ // Annotate the CommonJS export names for ESM import in node:
1138
+ 0 && (module.exports = {
1139
+ Client,
1140
+ ClusterAdapter,
1141
+ ServerAdapter,
1142
+ connection,
1143
+ constants,
1144
+ decode,
1145
+ encode
1146
+ });
1147
+ //# sourceMappingURL=index.js.map