memx 0.0.7 → 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 -1460
- 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 +25 -23
- 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 +34 -33
- package/src/internals.ts +7 -7
- package/src/server.ts +47 -45
- package/src/utils.ts +7 -4
- package/dist/index.js +0 -1496
- package/dist/index.js.map +0 -6
- package/index.d.ts +0 -710
package/package.json
CHANGED
|
@@ -1,48 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "memx",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Simple and fast memcached client",
|
|
5
|
-
"main": "./dist/index.
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"type": "commonjs",
|
|
7
9
|
"exports": {
|
|
8
10
|
".": {
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
+
"require": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"import": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.mjs"
|
|
18
|
+
}
|
|
11
19
|
}
|
|
12
20
|
},
|
|
13
|
-
"types": "./index.d.ts",
|
|
14
21
|
"scripts": {
|
|
15
|
-
"build": "
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
22
|
+
"build": "plug",
|
|
23
|
+
"coverage": "plug coverage",
|
|
24
|
+
"dev": "plug coverage -w src -w test",
|
|
25
|
+
"lint": "plug lint",
|
|
26
|
+
"test": "plug test",
|
|
27
|
+
"transpile": "plug transpile"
|
|
19
28
|
},
|
|
20
29
|
"author": "Juit Developers <developers@juit.com>",
|
|
21
30
|
"license": "Apache-2.0",
|
|
22
31
|
"devDependencies": {
|
|
23
|
-
"@
|
|
24
|
-
"@types/chai": "^4.3.
|
|
32
|
+
"@plugjs/build": "^0.2.2",
|
|
33
|
+
"@types/chai": "^4.3.4",
|
|
25
34
|
"@types/chai-as-promised": "^7.1.5",
|
|
26
|
-
"@types/memjs": "^1.
|
|
27
|
-
"
|
|
28
|
-
"@types/node": "~16.11.26",
|
|
29
|
-
"@types/source-map-support": "^0.5.4",
|
|
30
|
-
"@typescript-eslint/eslint-plugin": "^5.17.0",
|
|
31
|
-
"@typescript-eslint/parser": "^5.17.0",
|
|
32
|
-
"chai": "^4.3.6",
|
|
35
|
+
"@types/memjs": "^1.3.0",
|
|
36
|
+
"chai": "^4.3.7",
|
|
33
37
|
"chai-as-promised": "^7.1.1",
|
|
34
38
|
"chai-exclude": "^2.1.0",
|
|
35
|
-
"esbuild": "^0.14.28",
|
|
36
|
-
"eslint": "^8.12.0",
|
|
37
|
-
"eslint-config-google": "^0.14.0",
|
|
38
39
|
"memjs": "^1.3.0",
|
|
39
|
-
"
|
|
40
|
-
"nodemon": "^2.0.15",
|
|
41
|
-
"nyc": "^15.1.0",
|
|
42
|
-
"source-map-support": "^0.5.21",
|
|
43
|
-
"typescript": "^4.6.3"
|
|
40
|
+
"typescript": "^4.9.5"
|
|
44
41
|
},
|
|
45
42
|
"directories": {
|
|
46
43
|
"test": "test"
|
|
47
|
-
}
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"*.md",
|
|
47
|
+
"dist/",
|
|
48
|
+
"src/"
|
|
49
|
+
]
|
|
48
50
|
}
|
package/src/client.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import assert from 'assert'
|
|
2
|
-
import { types } from 'util'
|
|
1
|
+
import assert from 'node:assert'
|
|
2
|
+
import { types } from 'node:util'
|
|
3
3
|
|
|
4
|
-
import { ClusterAdapter
|
|
4
|
+
import { ClusterAdapter } from './cluster'
|
|
5
5
|
import { EMPTY_BUFFER, FLAGS } from './constants'
|
|
6
6
|
import { typedArrayFlags } from './internals'
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
import type { ClusterOptions } from './cluster'
|
|
9
|
+
import type { Adapter, AdapterResult, Counter, Stats } from './types'
|
|
8
10
|
|
|
9
11
|
/** JSON replacere function serializing `bigint`, {@link Date}, {@link Set} and {@link Map}. */
|
|
10
12
|
function replacer(this: any, key: string, value: any): any {
|
|
@@ -16,7 +18,7 @@ function replacer(this: any, key: string, value: any): any {
|
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
/** JSON reviver function deserializing `bigint`, {@link Date}, {@link Set} and {@link Map}. */
|
|
19
|
-
function reviver(this: any,
|
|
21
|
+
function reviver(this: any, _key: string, value: any): any {
|
|
20
22
|
if (Array.isArray(value)) {
|
|
21
23
|
switch (value[0]) {
|
|
22
24
|
case '\0__$BIGINT$__\0': return BigInt(value[1])
|
|
@@ -120,8 +122,8 @@ function fromBuffer<T extends Serializable>(result: AdapterResult): ClientResult
|
|
|
120
122
|
|
|
121
123
|
/** Create a {@link NodeJS.TypedArray} copying the contents of its source {@link Buffer} */
|
|
122
124
|
function makeTypedArray<T extends NodeJS.TypedArray>(
|
|
123
|
-
|
|
124
|
-
|
|
125
|
+
constructor: TypedArrayConstructor<T>,
|
|
126
|
+
source: Buffer,
|
|
125
127
|
): T {
|
|
126
128
|
const clone = Buffer.from(source)
|
|
127
129
|
const { buffer, byteOffset, byteLength } = clone
|
|
@@ -227,25 +229,25 @@ export class MemxClient {
|
|
|
227
229
|
}
|
|
228
230
|
|
|
229
231
|
append(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
232
|
+
key: string,
|
|
233
|
+
value: Appendable,
|
|
234
|
+
options?: { cas?: bigint },
|
|
233
235
|
): Promise<boolean> {
|
|
234
236
|
return this.#adapter.append(this.#prefix + key, ...toBuffer(value, options))
|
|
235
237
|
}
|
|
236
238
|
|
|
237
239
|
prepend(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
240
|
+
key: string,
|
|
241
|
+
value: Appendable,
|
|
242
|
+
options?: { cas?: bigint },
|
|
241
243
|
): Promise<boolean> {
|
|
242
244
|
return this.#adapter.prepend(this.#prefix + key, ...toBuffer(value, options))
|
|
243
245
|
}
|
|
244
246
|
|
|
245
247
|
async increment(
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
248
|
+
key: string,
|
|
249
|
+
delta?: bigint | number,
|
|
250
|
+
options?: { initial?: bigint | number, cas?: bigint, ttl?: number },
|
|
249
251
|
): Promise<Counter | undefined> {
|
|
250
252
|
const counter = await this.#adapter.increment(this.#prefix + key, delta, options)
|
|
251
253
|
|
|
@@ -257,9 +259,9 @@ export class MemxClient {
|
|
|
257
259
|
}
|
|
258
260
|
|
|
259
261
|
async decrement(
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
262
|
+
key: string,
|
|
263
|
+
delta?: bigint | number,
|
|
264
|
+
options?: { initial?: bigint | number, cas?: bigint, ttl?: number },
|
|
263
265
|
): Promise<Counter | undefined> {
|
|
264
266
|
const counter = await this.#adapter.decrement(this.#prefix + key, delta, options)
|
|
265
267
|
|
|
@@ -271,15 +273,15 @@ export class MemxClient {
|
|
|
271
273
|
}
|
|
272
274
|
|
|
273
275
|
touch(
|
|
274
|
-
|
|
275
|
-
|
|
276
|
+
key: string,
|
|
277
|
+
ttl?: number,
|
|
276
278
|
): Promise<boolean> {
|
|
277
279
|
return this.#adapter.touch(this.#prefix + key, ttl)
|
|
278
280
|
}
|
|
279
281
|
|
|
280
282
|
delete(
|
|
281
|
-
|
|
282
|
-
|
|
283
|
+
key: string,
|
|
284
|
+
options?: { cas?: bigint },
|
|
283
285
|
): Promise<boolean> {
|
|
284
286
|
return this.#adapter.delete(this.#prefix + key, options)
|
|
285
287
|
}
|
package/src/cluster.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import
|
|
1
|
+
import assert from 'node:assert'
|
|
2
|
+
|
|
3
|
+
import { ServerAdapter } from './server'
|
|
4
|
+
|
|
5
|
+
import type { Adapter, Counter, AdapterResult, Stats } from './types'
|
|
6
|
+
import type { ServerOptions } from './server'
|
|
4
7
|
|
|
5
8
|
function parseHosts(hosts?: string): ServerOptions[] {
|
|
6
9
|
const result: { host: string, port?: number }[] = []
|
package/src/connection.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import assert from 'assert'
|
|
2
|
-
import net
|
|
1
|
+
import assert from 'node:assert'
|
|
2
|
+
import net from 'node:net'
|
|
3
3
|
|
|
4
|
-
import { Encoder
|
|
5
|
-
import { Decoder
|
|
4
|
+
import { Encoder } from './encode'
|
|
5
|
+
import { Decoder } from './decode'
|
|
6
6
|
import { BUFFERS, OPCODE } from './constants'
|
|
7
7
|
import { socketFinalizationRegistry } from './internals'
|
|
8
8
|
|
|
9
|
+
import type { RawIncomingPacket } from './decode'
|
|
10
|
+
import type { RawOutgoingPacket } from './encode'
|
|
11
|
+
import type { Socket } from 'node:net'
|
|
12
|
+
|
|
9
13
|
export type RawIncomingPackets = [ RawIncomingPacket, ...RawIncomingPacket[] ]
|
|
10
14
|
|
|
11
15
|
class Deferred {
|
package/src/decode.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import assert from 'node:assert'
|
|
2
|
+
|
|
1
3
|
import { DATA_TYPE, EMPTY_BUFFER, MAGIC, OFFSETS } from './constants'
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
+
import { allocateBuffer } from './buffers'
|
|
5
|
+
|
|
6
|
+
import type { RecyclableBuffer } from './buffers'
|
|
4
7
|
|
|
5
8
|
// Reading the full header would be like
|
|
6
9
|
// const magic = header.readUInt8 (0)
|
package/src/encode.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { allocateBuffer
|
|
2
|
-
import { DATA_TYPE, EMPTY_BUFFER, MAGIC, VBUCKET,
|
|
1
|
+
import { allocateBuffer } from './buffers'
|
|
2
|
+
import { DATA_TYPE, EMPTY_BUFFER, MAGIC, VBUCKET, OFFSETS } from './constants'
|
|
3
|
+
|
|
4
|
+
import type { RecyclableBuffer } from './buffers'
|
|
5
|
+
import type { OPCODE } from './constants'
|
|
3
6
|
|
|
4
7
|
// Writing the full header would be like
|
|
5
8
|
// header.writeUInt8 (magic, 0)
|
package/src/fake.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Adapter, AdapterResult, Counter, Stats } from './types'
|
|
2
1
|
import { MemxClient } from './client'
|
|
3
2
|
|
|
3
|
+
import type { Adapter, AdapterResult, Counter, Stats } from './types'
|
|
4
|
+
|
|
4
5
|
interface Entry {
|
|
5
6
|
value: Buffer,
|
|
6
7
|
flags: number,
|
|
@@ -43,7 +44,7 @@ export class FakeAdapter implements Adapter {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
async get(
|
|
46
|
-
|
|
47
|
+
key: string,
|
|
47
48
|
): Promise<AdapterResult | undefined> {
|
|
48
49
|
const entry = this.#get(key)
|
|
49
50
|
if (! entry) return
|
|
@@ -57,8 +58,8 @@ export class FakeAdapter implements Adapter {
|
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
async gat(
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
key: string,
|
|
62
|
+
ttl: number,
|
|
62
63
|
): Promise<AdapterResult | undefined> {
|
|
63
64
|
const entry = this.#get(key)
|
|
64
65
|
if (! entry) return
|
|
@@ -74,8 +75,8 @@ export class FakeAdapter implements Adapter {
|
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
async touch(
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
key: string,
|
|
79
|
+
ttl: number,
|
|
79
80
|
): Promise<boolean> {
|
|
80
81
|
const entry = this.#get(key)
|
|
81
82
|
if (entry) entry.exp = toExp(ttl)
|
|
@@ -83,9 +84,9 @@ export class FakeAdapter implements Adapter {
|
|
|
83
84
|
}
|
|
84
85
|
|
|
85
86
|
async set(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
key: string,
|
|
88
|
+
value: Buffer,
|
|
89
|
+
options: { flags?: number; cas?: bigint; ttl?: number } = {},
|
|
89
90
|
): Promise<bigint | undefined> {
|
|
90
91
|
const entry = this.#get(key)
|
|
91
92
|
if (entry && (options.cas !== undefined) && (entry.cas !== options.cas)) {
|
|
@@ -96,27 +97,27 @@ export class FakeAdapter implements Adapter {
|
|
|
96
97
|
}
|
|
97
98
|
|
|
98
99
|
async add(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
key: string,
|
|
101
|
+
value: Buffer,
|
|
102
|
+
options: { flags?: number; ttl?: number } = {},
|
|
102
103
|
): Promise<bigint | undefined> {
|
|
103
104
|
if (this.#get(key)) return
|
|
104
105
|
return this.#set(key, value, options.flags, options.ttl)
|
|
105
106
|
}
|
|
106
107
|
|
|
107
108
|
async replace(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
key: string,
|
|
110
|
+
value: Buffer,
|
|
111
|
+
options: { flags?: number; cas?: bigint; ttl?: number } = {},
|
|
111
112
|
): Promise<bigint | undefined> {
|
|
112
113
|
if (! this.#get(key)) return
|
|
113
114
|
return this.#set(key, value, options.flags, options.ttl)
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
async append(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
key: string,
|
|
119
|
+
value: Buffer,
|
|
120
|
+
options: { cas?: bigint } = {},
|
|
120
121
|
): Promise<boolean> {
|
|
121
122
|
const entry = this.#get(key)
|
|
122
123
|
if (! entry) return false
|
|
@@ -128,9 +129,9 @@ export class FakeAdapter implements Adapter {
|
|
|
128
129
|
}
|
|
129
130
|
|
|
130
131
|
async prepend(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
132
|
+
key: string,
|
|
133
|
+
value: Buffer,
|
|
134
|
+
options: { cas?: bigint } = {},
|
|
134
135
|
): Promise<boolean> {
|
|
135
136
|
const entry = this.#get(key)
|
|
136
137
|
if (! entry) return false
|
|
@@ -142,9 +143,9 @@ export class FakeAdapter implements Adapter {
|
|
|
142
143
|
}
|
|
143
144
|
|
|
144
145
|
async #counter(
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
146
|
+
key: string,
|
|
147
|
+
delta: number | bigint,
|
|
148
|
+
options: { initial?: number | bigint; cas?: bigint; ttl?: number; create?: boolean },
|
|
148
149
|
): Promise<Counter | undefined> {
|
|
149
150
|
const entry = this.#get(key)
|
|
150
151
|
|
|
@@ -170,24 +171,24 @@ export class FakeAdapter implements Adapter {
|
|
|
170
171
|
}
|
|
171
172
|
|
|
172
173
|
increment(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
174
|
+
key: string,
|
|
175
|
+
delta: number | bigint = 1n,
|
|
176
|
+
options: { initial?: number | bigint; cas?: bigint; ttl?: number; create?: boolean } = {},
|
|
176
177
|
): Promise<Counter | undefined> {
|
|
177
178
|
return this.#counter(key, delta, options)
|
|
178
179
|
}
|
|
179
180
|
|
|
180
181
|
decrement(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
182
|
+
key: string,
|
|
183
|
+
delta: number | bigint = 1n,
|
|
184
|
+
options: { initial?: number | bigint; cas?: bigint; ttl?: number; create?: boolean } = {},
|
|
184
185
|
): Promise<Counter | undefined> {
|
|
185
186
|
return this.#counter(key, -delta, options)
|
|
186
187
|
}
|
|
187
188
|
|
|
188
189
|
async delete(
|
|
189
|
-
|
|
190
|
-
|
|
190
|
+
key: string,
|
|
191
|
+
options: { cas?: bigint } = {},
|
|
191
192
|
): Promise<boolean> {
|
|
192
193
|
const entry = this.#get(key)
|
|
193
194
|
if (entry && (options.cas !== undefined) && (entry.cas !== options.cas)) {
|
|
@@ -198,7 +199,7 @@ export class FakeAdapter implements Adapter {
|
|
|
198
199
|
}
|
|
199
200
|
|
|
200
201
|
async flush(
|
|
201
|
-
|
|
202
|
+
ttl?: number,
|
|
202
203
|
): Promise<void> {
|
|
203
204
|
if (! ttl) return this.#cache.clear()
|
|
204
205
|
|
package/src/internals.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import assert from 'assert'
|
|
5
|
-
import type { Socket } from 'net'
|
|
6
|
-
import { types } from 'util'
|
|
1
|
+
import assert from 'node:assert'
|
|
2
|
+
import { types } from 'node:util'
|
|
7
3
|
|
|
8
4
|
import { FLAGS } from './constants'
|
|
9
5
|
|
|
6
|
+
import type { Socket } from 'node:net'
|
|
7
|
+
|
|
8
|
+
/* coverage ignore next */
|
|
10
9
|
export const socketFinalizationRegistry = new FinalizationRegistry((socket: Socket): void => {
|
|
11
10
|
if (! socket.destroyed) socket.destroy()
|
|
12
11
|
})
|
|
@@ -28,7 +27,8 @@ export function typedArrayFlags(value: NodeJS.TypedArray): FLAGS {
|
|
|
28
27
|
return flags
|
|
29
28
|
}
|
|
30
29
|
|
|
30
|
+
/* coverage ignore next */
|
|
31
31
|
export function logPromiseError(promise: Promise<any>, message: string): Promise<void> {
|
|
32
|
-
|
|
32
|
+
/* eslint-disable-next-line no-console */
|
|
33
33
|
return promise.catch((error) => console.log(message, error)).then(() => void 0)
|
|
34
34
|
}
|
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,9 +1,11 @@
|
|
|
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
11
|
#client: MemxClient
|
|
@@ -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> {
|
|
@@ -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
|