memcache 0.2.0 → 1.0.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/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
6
+ "baseUrl": "./src", /* Specify the base directory to resolve non-relative module names. */
7
+
8
+ /* Emit */
9
+ "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
10
+ "sourceMap": true, /* Create source map files for emitted JavaScript files. */
11
+ "outDir": "./dist", /* Specify an output folder for all emitted files. */
12
+
13
+ /* Interop Constraints */
14
+ "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
15
+ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
16
+
17
+ /* Type Checking */
18
+ "strict": true, /* Enable all strict type-checking options. */
19
+
20
+ /* Completeness */
21
+ "skipLibCheck": true, /* Skip type checking all .d.ts files. */
22
+ "lib": [
23
+ "ESNext", "DOM"
24
+ ]
25
+ },
26
+ "include": [
27
+ "src/**/*.ts"
28
+ ]
29
+ }
@@ -0,0 +1,16 @@
1
+ import {defineConfig} from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ include: ['test/*.test.ts'],
6
+ coverage: {
7
+ reporter: ['text', 'json', 'lcov'],
8
+ exclude: [
9
+ 'site/**',
10
+ 'vitest.config.ts',
11
+ 'dist/**',
12
+ 'test/**',
13
+ ],
14
+ },
15
+ },
16
+ });
package/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- lib-cov
2
- *.swp
package/Makefile DELETED
@@ -1,13 +0,0 @@
1
- NODE = node
2
- TEST = expresso
3
- TESTS ?= test/*.js
4
-
5
- test:
6
- @CONNECT_ENV=test $(TEST) \
7
- -I lib \
8
- $(TEST_FLAGS) $(TESTS)
9
-
10
- test-cov:
11
- @$(MAKE) test TEST_FLAGS="--cov"
12
-
13
- .PHONY: test test-cov
package/example.js DELETED
@@ -1,68 +0,0 @@
1
- var sys = require('sys');
2
- var memcache = require('./lib/memcache');
3
-
4
- function microtime(get_as_float) {
5
- var now = new Date().getTime() / 1000;
6
- var s = parseInt(now);
7
- return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
8
- }
9
-
10
- var onConnect = function() {
11
- mcClient.get('test', function(err, data) {
12
- sys.debug(data);
13
- mcClient.close();
14
- });
15
- };
16
-
17
- var benchmark = function() {
18
- var count = 20000;
19
- start = microtime(true);
20
- var x = 0;
21
-
22
- for (var i=0; i<=count; i++) {
23
- mcClient.get('test', function(err, data) {
24
- x += 1;
25
- if (x == count) {
26
- end = microtime(true);
27
- sys.debug('total time: ' + (end - start));
28
- }
29
- });
30
- }
31
-
32
- mcClient.close();
33
- };
34
-
35
- var setKey = function() {
36
- mcClient.set('test', 'hello \r\n node-memcache', function(err, response) {
37
- mcClient.get('test', function(err, data) {
38
- sys.debug(data);
39
- mcClient.close();
40
- });
41
- });
42
- };
43
-
44
- var version = function() {
45
- mcClient.version(function(err, version) {
46
- sys.debug(version);
47
- mcClient.close();
48
- });
49
- };
50
-
51
- var incr = function() {
52
- mcClient.increment('x', 2, function(err, new_value) {
53
- sys.debug(new_value);
54
- mcClient.close();
55
- });
56
- };
57
-
58
- var decr = function() {
59
- mcClient.decrement('x', 1, function(err, new_value) {
60
- sys.debug(new_value);
61
- mcClient.close();
62
- });
63
- };
64
-
65
- mcClient = new memcache.Client();
66
- mcClient.connect();
67
- mcClient.addHandler(onConnect);
68
-
package/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./lib/memcache');
package/lib/memcache.js DELETED
@@ -1,344 +0,0 @@
1
- /**
2
- * Copyright (c) 2011 Tim Eggert
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining a copy
5
- * of this software and associated documentation files (the "Software"), to deal
6
- * in the Software without restriction, including without limitation the rights
7
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- * copies of the Software, and to permit persons to whom the Software is
9
- * furnished to do so, subject to the following conditions:
10
- *
11
- * The above copyright notice and this permission notice shall be included in
12
- * all copies or substantial portions of the Software.
13
- *
14
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
- * THE SOFTWARE.
21
- *
22
- * @author Tim Eggert <tim@elbart.com>
23
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
24
- */
25
-
26
- var tcp = require('net'),
27
- sys = require('sys');
28
-
29
- var crlf = "\r\n";
30
- var crlf_len = crlf.length;
31
-
32
- var error_replies = ['ERROR', 'NOT_FOUND', 'CLIENT_ERROR', 'SERVER_ERROR'];
33
-
34
- var Client = exports.Client = function(port, host) {
35
- this.port = port || 11211;
36
- this.host = host || 'localhost';
37
- this.buffer = '';
38
- this.conn = null;
39
- this.sends = 0;
40
- this.replies = 0;
41
- this.callbacks = [];
42
- this.handles = [];
43
- };
44
-
45
- sys.inherits(Client, process.EventEmitter);
46
-
47
- Client.prototype.connect = function () {
48
- if (!this.conn) {
49
- this.conn = new tcp.createConnection(this.port, this.host);
50
- var self = this;
51
- this.conn.addListener("connect", function () {
52
- this.setTimeout(0); // try to stay connected.
53
- this.setNoDelay();
54
- self.emit("connect");
55
- self.dispatchHandles();
56
- });
57
-
58
- this.conn.addListener("data", function (data) {
59
- self.buffer += data;
60
- // sys.debug(data);
61
- self.recieves += 1;
62
- self.handle_received_data();
63
- });
64
-
65
- this.conn.addListener("end", function () {
66
- if (self.conn && self.conn.readyState) {
67
- self.conn.end();
68
- self.conn = null;
69
- }
70
- });
71
-
72
- this.conn.addListener("close", function () {
73
- self.conn = null;
74
- self.emit("close");
75
- });
76
-
77
- this.conn.addListener("timeout", function () {
78
- self.conn = null;
79
- self.emit("timeout");
80
- });
81
-
82
- this.conn.addListener("error", function (ex) {
83
- self.conn = null;
84
- self.emit("error", ex);
85
- });
86
- }
87
- };
88
-
89
- Client.prototype.addHandler = function(callback) {
90
- this.handles.push(callback);
91
-
92
- if (this.conn.readyState == 'open') {
93
- this.dispatchHandles();
94
- }
95
- };
96
-
97
- Client.prototype.dispatchHandles = function() {
98
- for (var i in this.handles) {
99
- var handle = this.handles.shift();
100
- // sys.debug('dispatching handle ' + handle);
101
- if (typeof handle !== 'undefined') {
102
- handle();
103
- }
104
- }
105
- };
106
-
107
- Client.prototype.query = function(query, type, callback) {
108
- this.callbacks.push({ type: type, fun: callback });
109
- this.sends++;
110
- this.conn.write(query + crlf);
111
- };
112
-
113
- Client.prototype.close = function() {
114
- if (this.conn && this.conn.readyState === "open") {
115
- this.conn.end();
116
- this.conn = null;
117
- }
118
- };
119
-
120
- Client.prototype.get = function(key, callback) {
121
- return this.query('get ' + key, 'get', callback);
122
- };
123
-
124
-
125
- // all of these store ops (everything bu "cas") have the same format
126
- Client.prototype.set = function(key, value, callback, lifetime, flags) { return this.store('set', key, value, callback, lifetime, flags); }
127
- Client.prototype.add = function(key, value, callback, lifetime, flags) { return this.store('add', key, value, callback, lifetime, flags); }
128
- Client.prototype.replace = function(key, value, callback, lifetime, flags) { return this.store('replace', key, value, callback, lifetime, flags); }
129
- Client.prototype.append = function(key, value, callback, lifetime, flags) { return this.store('append', key, value, callback, lifetime, flags); }
130
- Client.prototype.prepend = function(key, value, callback, lifetime, flags) { return this.store('prepend', key, value, callback, lifetime, flags); }
131
- Client.prototype.store = function(cmd, key, value, callback, lifetime, flags) {
132
-
133
- if (typeof(callback) != 'function') {
134
- lifetime = callback;
135
- callback = null;
136
- }
137
-
138
- var set_flags = flags || 0;
139
- var exp_time = lifetime || 0;
140
- var tml_buf = new Buffer(value.toString());
141
- var value_len = tml_buf.length || 0;
142
- var query = [cmd, key, set_flags, exp_time, value_len];
143
-
144
- return this.query(query.join(' ') + crlf + value, 'simple', callback);
145
- };
146
-
147
- // "cas" is a store op that takes an extra "unique" argument
148
- Client.prototype.cas = function(key, value, unique, callback, lifetime, flags) {
149
-
150
- if (typeof(callback) != 'function') {
151
- lifetime = callback;
152
- callback = null;
153
- }
154
-
155
- var set_flags = flags || 0;
156
- var exp_time = lifetime || 0;
157
- var value_len = value.length || 0;
158
- var query = ['cas', key, set_flags, exp_time, value_len, unique];
159
-
160
- return this.query(query.join(' ') + crlf + value, 'simple', callback);
161
- };
162
-
163
-
164
- Client.prototype.del = function(key, callback){
165
- sys.error("mc.del() is deprecated - use mc.delete() instead");
166
- return this.delete(key, callback);
167
- };
168
-
169
- Client.prototype.delete = function(key, callback){
170
- return this.query('delete ' + key, 'simple', callback);
171
- };
172
-
173
- Client.prototype.version = function(callback) {
174
- return this.query('version', 'version', callback);
175
- };
176
-
177
- Client.prototype.increment = function(key, value, callback) {
178
-
179
- if (typeof(value) == 'function') {
180
- callback = value;
181
- value = 1;;
182
- }
183
-
184
- value = value || 1;
185
- return this.query('incr ' + key + ' ' + value, 'simple', callback);
186
- };
187
-
188
- Client.prototype.decrement = function(key, value, callback) {
189
-
190
- if (typeof(value) == 'function') {
191
- callback = value;
192
- value = 1;;
193
- }
194
-
195
- value = value || 1;
196
- return this.query('decr ' + key + ' ' + value, 'simple', callback);
197
- };
198
-
199
- Client.prototype.stats = function(type, callback){
200
-
201
- if (typeof(type) == 'function'){
202
- callback = type;
203
- type = null;
204
- }
205
-
206
- if (type){
207
- return this.query('stats '+type, 'stats', callback);
208
- }else{
209
- return this.query('stats', 'stats', callback);
210
- }
211
- }
212
-
213
- Client.prototype.handle_received_data = function(){
214
-
215
- while (this.buffer.length > 0){
216
-
217
- var result = this.determine_reply_handler(this.buffer);
218
-
219
- if (result == null){
220
- break;
221
- }
222
-
223
- var result_value = result[0];
224
- var next_result_at = result[1];
225
- var result_error = result[2];
226
-
227
- // does the current message need more data than we have?
228
- // (this is how "get" ops ensure we've gotten all the data)
229
- if (next_result_at > this.buffer.length){
230
- break;
231
- }
232
-
233
- this.buffer = this.buffer.substring(next_result_at);
234
-
235
- var callback = this.callbacks.shift();
236
- if (callback != null && callback.fun){
237
- this.replies++;
238
- callback.fun(result_error, result_value);
239
- }
240
- }
241
- };
242
-
243
- Client.prototype.determine_reply_handler = function (buffer){
244
-
245
- // check we have a whole line in the buffer
246
- var crlf_at = buffer.indexOf(crlf);
247
- if (crlf_at == -1){
248
- return null;
249
- }
250
-
251
- // determine errors
252
- for (var error_idx in error_replies){
253
- var error_indicator = error_replies[error_idx];
254
- if (buffer.indexOf(error_indicator) == 0) {
255
- return this.handle_error(buffer);
256
- }
257
- }
258
-
259
- // call the handler for the current message type
260
- var type = this.callbacks[0].type;
261
- if (type){
262
- return this['handle_' + type](buffer);
263
- }
264
-
265
- return null;
266
- };
267
-
268
- Client.prototype.handle_get = function(buffer) {
269
- var next_result_at = 0;
270
- var result_value = null;
271
- var end_indicator_len = 3;
272
- var result_len = 0;
273
-
274
- if (buffer.indexOf('END') == 0) {
275
- return [result_value, end_indicator_len + crlf_len];
276
- } else if (buffer.indexOf('VALUE') == 0 && buffer.indexOf('END') != -1) {
277
- first_line_len = buffer.indexOf(crlf) + crlf_len;
278
- var end_indicator_start = buffer.indexOf('END');
279
- result_len = end_indicator_start - first_line_len - crlf_len;
280
- result_value = buffer.substr(first_line_len, result_len);
281
- return [result_value, first_line_len + parseInt(result_len, 10) + crlf_len + end_indicator_len + crlf_len]
282
- } else {
283
- var first_line_len = buffer.indexOf(crlf) + crlf_len;
284
- var result_len = buffer.substr(0, first_line_len).split(' ')[3];
285
- result_value = buffer.substr(first_line_len, result_len);
286
-
287
- return [result_value, first_line_len + parseInt(result_len ) + crlf_len + end_indicator_len + crlf_len];
288
- }
289
- };
290
-
291
- Client.prototype.handle_stats = function(buffer){
292
-
293
- // special case - no stats at all
294
- if (buffer.indexOf('END') == 0){
295
- return [{}, 5];
296
- }
297
-
298
- // find the terminator
299
- var idx = buffer.indexOf('\r\nEND\r\n');
300
- if (idx == -1){
301
- // wait for more data if we don't have an end yet
302
- return null;
303
- }
304
-
305
- // read the lines
306
- var our_data = buffer.substr(0, idx+2);
307
- var out = {};
308
- var line = null;
309
- var i=0;
310
- while (line = readLine(our_data)){
311
- our_data = our_data.substr(line.length + 2);
312
- if (line.substr(0, 5) == 'STAT '){
313
- var idx2 = line.indexOf(' ', 5);
314
- var k = line.substr(5, idx2-5);
315
- var v = line.substr(idx2+1);
316
- out[k] = v;
317
- }
318
- }
319
-
320
- return [out, idx + 7, null];
321
- };
322
-
323
- Client.prototype.handle_simple = function(buffer){
324
- var line = readLine(buffer);
325
- return [line, (line.length + crlf_len), null];
326
- };
327
-
328
- Client.prototype.handle_version = function(buffer){
329
- var line_len = buffer.indexOf(crlf);
330
- var indicator_len = 'VERSION '.length;
331
- var result_value = buffer.substr(indicator_len, (line_len - indicator_len));
332
- return [result_value, line_len + crlf_len, null];
333
- };
334
-
335
- Client.prototype.handle_error = function(buffer){
336
- line = readLine(buffer);
337
- return [null, (line.length + crlf_len), line];
338
- };
339
-
340
- readLine = function(string){
341
- var line_len = string.indexOf(crlf);
342
- return string.substr(0, line_len);
343
- };
344
-