memx 0.0.6 → 0.1.0
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/buffers.cjs +50 -0
- package/dist/buffers.cjs.map +6 -0
- package/dist/buffers.d.ts +14 -0
- package/dist/buffers.mjs +25 -0
- package/dist/buffers.mjs.map +6 -0
- package/dist/client.cjs +251 -0
- package/dist/client.cjs.map +6 -0
- package/dist/client.d.ts +76 -0
- package/dist/client.mjs +216 -0
- package/dist/client.mjs.map +6 -0
- package/dist/cluster.cjs +147 -0
- package/dist/cluster.cjs.map +6 -0
- package/dist/cluster.d.ts +60 -0
- package/dist/cluster.mjs +112 -0
- package/dist/cluster.mjs.map +6 -0
- package/dist/connection.cjs +186 -0
- package/dist/connection.cjs.map +6 -0
- package/dist/connection.d.ts +18 -0
- package/dist/connection.mjs +151 -0
- package/dist/connection.mjs.map +6 -0
- package/dist/constants.cjs +140 -0
- package/dist/constants.cjs.map +6 -0
- package/dist/constants.d.ts +87 -0
- package/dist/constants.mjs +107 -0
- package/dist/constants.mjs.map +6 -0
- package/dist/decode.cjs +101 -0
- package/dist/decode.cjs.map +6 -0
- package/dist/decode.d.ts +16 -0
- package/dist/decode.mjs +66 -0
- package/dist/decode.mjs.map +6 -0
- package/dist/encode.cjs +70 -0
- package/dist/encode.cjs.map +6 -0
- package/dist/encode.d.ts +20 -0
- package/dist/encode.mjs +45 -0
- package/dist/encode.mjs.map +6 -0
- package/dist/fake.cjs +183 -0
- package/dist/fake.cjs.map +6 -0
- package/dist/fake.d.ts +53 -0
- package/dist/fake.mjs +157 -0
- package/dist/fake.mjs.map +6 -0
- package/dist/index.cjs +57 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +15 -1454
- package/dist/index.mjs.map +2 -2
- package/dist/internals.cjs +58 -0
- package/dist/internals.cjs.map +6 -0
- package/dist/internals.d.ts +7 -0
- package/dist/internals.mjs +21 -0
- package/dist/internals.mjs.map +6 -0
- package/dist/server.cjs +468 -0
- package/dist/server.cjs.map +6 -0
- package/dist/server.d.ts +59 -0
- package/dist/server.mjs +443 -0
- package/dist/server.mjs.map +6 -0
- package/dist/types.cjs +19 -0
- package/dist/types.cjs.map +6 -0
- package/dist/types.d.ts +239 -0
- package/dist/types.mjs +1 -0
- package/dist/types.mjs.map +6 -0
- package/dist/utils.cjs +182 -0
- package/dist/utils.cjs.map +6 -0
- package/dist/utils.d.ts +22 -0
- package/dist/utils.mjs +145 -0
- package/dist/utils.mjs.map +6 -0
- package/package.json +29 -27
- package/src/client.ts +37 -35
- package/src/cluster.ts +6 -3
- package/src/connection.ts +8 -4
- package/src/decode.ts +5 -2
- package/src/encode.ts +5 -2
- package/src/fake.ts +41 -33
- package/src/internals.ts +7 -7
- package/src/server.ts +47 -45
- package/src/utils.ts +13 -10
- package/dist/index.js +0 -1489
- package/dist/index.js.map +0 -6
- package/index.d.ts +0 -706
package/src/server.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Connection, ConnectionOptions } from './connection'
|
|
1
|
+
import { Connection } from './connection'
|
|
3
2
|
import { BUFFERS, OPCODE, STATUS } from './constants'
|
|
4
|
-
|
|
3
|
+
|
|
4
|
+
import type { Adapter, Counter, AdapterResult, Stats } from './types'
|
|
5
|
+
import type { ConnectionOptions } from './connection'
|
|
6
|
+
import type { RawIncomingPacket } from './decode'
|
|
5
7
|
|
|
6
8
|
const statsBigInt: readonly string[] = [
|
|
7
9
|
'auth_cmds', 'auth_errors', 'bytes', 'bytes_read', 'bytes_written', 'cas_badval', 'cas_hits', 'cas_misses',
|
|
@@ -108,8 +110,8 @@ export class ServerAdapter implements Adapter {
|
|
|
108
110
|
/* ======================================================================== */
|
|
109
111
|
|
|
110
112
|
async #get(
|
|
111
|
-
|
|
112
|
-
|
|
113
|
+
key: string,
|
|
114
|
+
ttl?: number,
|
|
113
115
|
): Promise<AdapterResult | undefined> {
|
|
114
116
|
let keyOffset = 0
|
|
115
117
|
if (ttl !== undefined) keyOffset = this.#buffer.writeUInt32BE(ttl)
|
|
@@ -141,21 +143,21 @@ export class ServerAdapter implements Adapter {
|
|
|
141
143
|
}
|
|
142
144
|
|
|
143
145
|
async get(
|
|
144
|
-
|
|
146
|
+
key: string,
|
|
145
147
|
): Promise<AdapterResult | undefined> {
|
|
146
148
|
return this.#get(key)
|
|
147
149
|
}
|
|
148
150
|
|
|
149
151
|
async gat(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
+
key: string,
|
|
153
|
+
ttl: number,
|
|
152
154
|
): Promise<AdapterResult | undefined> {
|
|
153
155
|
return this.#get(key, ttl || 2592000) // TTL 0 is "invalid arg"
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
async touch(
|
|
157
|
-
|
|
158
|
-
|
|
159
|
+
key: string,
|
|
160
|
+
ttl?: number,
|
|
159
161
|
): Promise<boolean> {
|
|
160
162
|
const timeToLive = ttl ?? this.#ttl
|
|
161
163
|
|
|
@@ -189,10 +191,10 @@ export class ServerAdapter implements Adapter {
|
|
|
189
191
|
/* ======================================================================== */
|
|
190
192
|
|
|
191
193
|
async #sar(
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
194
|
+
opcode: OPCODE.SET | OPCODE.ADD | OPCODE.REPLACE,
|
|
195
|
+
key: string,
|
|
196
|
+
value: Buffer,
|
|
197
|
+
options: { flags?: number, cas?: bigint, ttl?: number },
|
|
196
198
|
): Promise<bigint | undefined> {
|
|
197
199
|
const { flags = 0, cas = 0n, ttl = this.#ttl } = options
|
|
198
200
|
|
|
@@ -229,25 +231,25 @@ export class ServerAdapter implements Adapter {
|
|
|
229
231
|
}
|
|
230
232
|
|
|
231
233
|
set(
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
234
|
+
key: string,
|
|
235
|
+
value: Buffer,
|
|
236
|
+
options: { flags?: number, cas?: bigint, ttl?: number } = {},
|
|
235
237
|
): Promise<bigint | undefined> {
|
|
236
238
|
return this.#sar(OPCODE.SET, key, value, options)
|
|
237
239
|
}
|
|
238
240
|
|
|
239
241
|
add(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
242
|
+
key: string,
|
|
243
|
+
value: Buffer,
|
|
244
|
+
options: { flags?: number, ttl?: number } = {},
|
|
243
245
|
): Promise<bigint | undefined> {
|
|
244
246
|
return this.#sar(OPCODE.ADD, key, value, options)
|
|
245
247
|
}
|
|
246
248
|
|
|
247
249
|
replace(
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
250
|
+
key: string,
|
|
251
|
+
value: Buffer,
|
|
252
|
+
options: { flags?: number, cas?: bigint, ttl?: number } = {},
|
|
251
253
|
): Promise<bigint | undefined> {
|
|
252
254
|
return this.#sar(OPCODE.REPLACE, key, value, options)
|
|
253
255
|
}
|
|
@@ -255,10 +257,10 @@ export class ServerAdapter implements Adapter {
|
|
|
255
257
|
/* ======================================================================== */
|
|
256
258
|
|
|
257
259
|
async #pend(
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
260
|
+
opcode: OPCODE.APPEND | OPCODE.PREPEND,
|
|
261
|
+
key: string,
|
|
262
|
+
value: Buffer,
|
|
263
|
+
options: { cas?: bigint },
|
|
262
264
|
): Promise<boolean> {
|
|
263
265
|
const { cas = 0n } = options
|
|
264
266
|
|
|
@@ -289,17 +291,17 @@ export class ServerAdapter implements Adapter {
|
|
|
289
291
|
}
|
|
290
292
|
|
|
291
293
|
append(
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
294
|
+
key: string,
|
|
295
|
+
value: Buffer,
|
|
296
|
+
options: { cas?: bigint } = {},
|
|
295
297
|
): Promise<boolean> {
|
|
296
298
|
return this.#pend(OPCODE.APPEND, key, value, options)
|
|
297
299
|
}
|
|
298
300
|
|
|
299
301
|
prepend(
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
302
|
+
key: string,
|
|
303
|
+
value: Buffer,
|
|
304
|
+
options: { cas?: bigint } = {},
|
|
303
305
|
): Promise<boolean> {
|
|
304
306
|
return this.#pend(OPCODE.PREPEND, key, value, options)
|
|
305
307
|
}
|
|
@@ -307,10 +309,10 @@ export class ServerAdapter implements Adapter {
|
|
|
307
309
|
/* ======================================================================== */
|
|
308
310
|
|
|
309
311
|
async #counter(
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
312
|
+
opcode: OPCODE.INCREMENT | OPCODE.DECREMENT,
|
|
313
|
+
key: string,
|
|
314
|
+
delta: bigint | number,
|
|
315
|
+
options: { initial?: bigint | number, cas?: bigint, ttl?: number },
|
|
314
316
|
): Promise<Counter | undefined> {
|
|
315
317
|
const {
|
|
316
318
|
initial,
|
|
@@ -354,17 +356,17 @@ export class ServerAdapter implements Adapter {
|
|
|
354
356
|
}
|
|
355
357
|
|
|
356
358
|
increment(
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
359
|
+
key: string,
|
|
360
|
+
delta: bigint | number = 1,
|
|
361
|
+
options: { initial?: bigint | number, cas?: bigint, ttl?: number, create?: boolean } = {},
|
|
360
362
|
): Promise<Counter | undefined> {
|
|
361
363
|
return this.#counter(OPCODE.INCREMENT, key, delta, options)
|
|
362
364
|
}
|
|
363
365
|
|
|
364
366
|
decrement(
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
367
|
+
key: string,
|
|
368
|
+
delta: bigint | number = 1,
|
|
369
|
+
options: { initial?: bigint | number, cas?: bigint, ttl?: number, create?: boolean } = {},
|
|
368
370
|
): Promise<Counter | undefined> {
|
|
369
371
|
return this.#counter(OPCODE.DECREMENT, key, delta, options)
|
|
370
372
|
}
|
|
@@ -372,8 +374,8 @@ export class ServerAdapter implements Adapter {
|
|
|
372
374
|
/* ======================================================================== */
|
|
373
375
|
|
|
374
376
|
async delete(
|
|
375
|
-
|
|
376
|
-
|
|
377
|
+
key: string,
|
|
378
|
+
options: { cas?: bigint } = {},
|
|
377
379
|
): Promise<boolean> {
|
|
378
380
|
const { cas = 0n } = options
|
|
379
381
|
|
package/src/utils.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
|
|
3
|
-
import assert from 'assert'
|
|
4
|
-
|
|
3
|
+
import assert from 'node:assert'
|
|
4
|
+
|
|
5
5
|
import { logPromiseError } from './internals'
|
|
6
6
|
|
|
7
|
+
import type { MemxClient, Serializable } from './client'
|
|
8
|
+
|
|
7
9
|
export class Factory<T extends Serializable> {
|
|
8
10
|
#factory: (key: string) => T | Promise<T>
|
|
9
|
-
#client:
|
|
11
|
+
#client: MemxClient
|
|
10
12
|
#ttl?: number
|
|
11
13
|
|
|
12
|
-
constructor(client:
|
|
14
|
+
constructor(client: MemxClient, factory: (key: string) => T | Promise<T>, ttl?: number) {
|
|
13
15
|
assert(typeof factory === 'function', 'Invalid or no factory specified')
|
|
14
16
|
assert(client, 'No client specified')
|
|
15
17
|
|
|
@@ -38,11 +40,11 @@ export class Factory<T extends Serializable> {
|
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
export class Bundle<T extends Serializable = Serializable> {
|
|
41
|
-
#client:
|
|
43
|
+
#client: MemxClient
|
|
42
44
|
#name: string
|
|
43
45
|
#ttl: number
|
|
44
46
|
|
|
45
|
-
constructor(client:
|
|
47
|
+
constructor(client: MemxClient, name: string, ttl?: number) {
|
|
46
48
|
assert(client, 'No client specified')
|
|
47
49
|
assert(name, 'No bundle name specified')
|
|
48
50
|
|
|
@@ -79,6 +81,7 @@ export class Bundle<T extends Serializable = Serializable> {
|
|
|
79
81
|
const result = await this.#client.getc<T>(`${this.#name}:${key}`)
|
|
80
82
|
if (result) return result.value
|
|
81
83
|
await this.#removeKey(key)
|
|
84
|
+
return undefined
|
|
82
85
|
}
|
|
83
86
|
|
|
84
87
|
async delete(key: string): Promise<void> {
|
|
@@ -110,10 +113,10 @@ export class Bundle<T extends Serializable = Serializable> {
|
|
|
110
113
|
}
|
|
111
114
|
|
|
112
115
|
export class PoorManLock {
|
|
113
|
-
#client:
|
|
116
|
+
#client: MemxClient
|
|
114
117
|
#name: string
|
|
115
118
|
|
|
116
|
-
constructor(client:
|
|
119
|
+
constructor(client: MemxClient, name: string) {
|
|
117
120
|
assert(client, 'No client specified')
|
|
118
121
|
assert(name, 'No lock name specified')
|
|
119
122
|
|
|
@@ -122,8 +125,8 @@ export class PoorManLock {
|
|
|
122
125
|
}
|
|
123
126
|
|
|
124
127
|
async execute<T>(
|
|
125
|
-
|
|
126
|
-
|
|
128
|
+
executor: () => T | Promise<T>,
|
|
129
|
+
options?: { timeout?: number, owner?: string },
|
|
127
130
|
): Promise<T> {
|
|
128
131
|
const { timeout = 5000, owner = false } = options || {}
|
|
129
132
|
const end = Date.now() + timeout
|