@@ -16,13 +16,11 @@ If false, the body will be allocated on the message object as it is parsed. It w
16
16
17
17
### ` encoding `
18
18
19
- <code >
20
- "encodings": {
21
- "BINARY": 0,
22
- "ASCII": 1,
23
- "UTF8": 2
24
- },
25
- </code >
19
+ "encodings": {
20
+ "BINARY": 0,
21
+ "ASCII": 1,
22
+ "UTF8": 2
23
+ }
26
24
27
25
Default is BINARY.
28
26
@@ -36,12 +34,10 @@ Only applies if chunked is false. When chunked is true, only raw buffers will be
36
34
37
35
You can set encoding and chunked by passing the constructor an options object as follows:
38
36
39
- <code >
40
- {
41
- encoding: 0|1|2,
42
- chunked: true|false
43
- }
44
- </code >
37
+ {
38
+ encoding: 0|1|2,
39
+ chunked: true|false
40
+ }
45
41
46
42
### ` execute(buffer, start, end) `
47
43
@@ -80,103 +76,99 @@ fired if chunked = true.
80
76
81
77
## Message format
82
78
83
- <code >
84
- {
85
- [ m] header: {
86
- magic: int,
87
- opcode: int,
88
- keylen: int16,
89
- exlen: int,
90
- datatype: int,
91
- status/reserved: int16,
92
- totlen: int32,
93
- opaque: int32,
94
- cashi: int32,
95
- caslo: int32,
96
- bodylen: int32
97
- },
98
- [ o] key: string,
99
- [ o] extra: {},
100
- [ o] body: string
101
- }
102
- </code >
79
+ {
80
+ [m]header: {
81
+ magic: int,
82
+ opcode: int,
83
+ keylen: int16,
84
+ exlen: int,
85
+ datatype: int,
86
+ status/reserved: int16,
87
+ totlen: int32,
88
+ opaque: int32,
89
+ cashi: int32,
90
+ caslo: int32,
91
+ bodylen: int32
92
+ },
93
+ [o]key: string,
94
+ [o]extra: {},
95
+ [o]body: string
96
+ }
103
97
104
98
# Example
105
- <code >
106
- var memc = require("../lib/parser");
107
99
108
- ...
109
-
110
- var current = null;
100
+ var memc = require("../lib/parser");
101
+
102
+ ...
111
103
112
- var parser = new memc.parser({
113
- "chunked": true,
114
- "encoding": memc.constants.encodings.BINARY
115
- });
104
+ var current = null;
105
+
106
+ var parser = new memc.parser({
107
+ "chunked": true,
108
+ "encoding": memc.constants.encodings.BINARY
109
+ });
110
+
111
+ parser.onMessage = function(message) {
112
+ // Will fire when a message has completed fully (i.e. body has been fully parsed),
113
+ // even if the message is only a header with no body. This means for a message
114
+ // that is only a header, you will get it in th onHeader and the onMessage callbacks
115
+ sys.puts("message\n" + JSON.stringify(message, null, "\t"));
116
+ switch(message.header.opcode) {
117
+ case memc.constants.opcodes.SET:
118
+ break;
119
+ case memc.constants.opcodes.GET:
120
+ break;
121
+ case memc.constants.opcodes.QUIT:
122
+ break;
123
+ }
124
+ };
116
125
117
- parser.onMessage = function(message) {
118
- // Will fire when a message has completed fully (i.e. body has been fully parsed),
119
- // even if the message is only a header with no body. This means for a message
120
- // that is only a header, you will get it in th onHeader and the onMessage callbacks
121
- sys.puts("message\n" + JSON.stringify(message, null, "\t"));
122
- switch(message.header.opcode) {
123
- case memc.constants.opcodes.SET:
124
- break;
125
- case memc.constants.opcodes.GET:
126
- break;
127
- case memc.constants.opcodes.QUIT:
128
- break;
129
- }
130
- };
131
-
132
- parser.onHeader = function(message) {
133
- // Will fire after the header (first 24 bytes) of a message has been parsed.
134
- sys.puts("header\n" + JSON.stringify(message, null, "\t"));
135
- switch(message.header.opcode) {
136
- case memc.constants.opcodes.SET:
137
- break;
138
- case memc.constants.opcodes.GET:
139
- break;
140
- case memc.constants.opcodes.QUIT:
141
- break;
142
- }
143
- if(message.header.bodylen > 0) {
144
- if(parser.chunked) {
145
- // save a pointer to the message in current so we can access it the onBody callback
146
- // as we will not get anything in the body when onMessage fires while in chunked mode
147
- current = message;
148
- current.body = [];
126
+ parser.onHeader = function(message) {
127
+ // Will fire after the header (first 24 bytes) of a message has been parsed.
128
+ sys.puts("header\n" + JSON.stringify(message, null, "\t"));
129
+ switch(message.header.opcode) {
130
+ case memc.constants.opcodes.SET:
131
+ break;
132
+ case memc.constants.opcodes.GET:
133
+ break;
134
+ case memc.constants.opcodes.QUIT:
135
+ break;
149
136
}
150
- else {
151
- // we will get the body on the message returned in the onMessage callback
137
+ if(message.header.bodylen > 0) {
138
+ if(parser.chunked) {
139
+ // save a pointer to the message in current so we can access it the onBody callback
140
+ // as we will not get anything in the body when onMessage fires while in chunked mode
141
+ current = message;
142
+ current.body = [];
143
+ }
144
+ else {
145
+ // we will get the body on the message returned in the onMessage callback
146
+ }
152
147
}
153
- }
154
- };
155
-
156
- parser.onBody = function(buffer, start, end) {
157
- // this will only fire if chunked is set to true. the parser will not set the body of
158
- // the message and will just forward on the chunks of the body in this callback.
159
- // NOTE: the parser does not use Buffer.slice(), it is giving you the actual buffer which was passed into it.
160
- sys.puts("chunk: " + (end-start));
161
- current.body.push(buffer.slice(start, end));
162
- };
163
-
164
- parser.onError = function(err) {
165
- sys.puts("error\n" + JSON.stringify(err, null, "\t"));
166
- };
148
+ };
167
149
168
- ...
169
-
170
- stream.ondata = function (buffer, start, end) {
171
- parser.execute(buffer, start, end);
172
- };
150
+ parser.onBody = function(buffer, start, end) {
151
+ // this will only fire if chunked is set to true. the parser will not set the body of
152
+ // the message and will just forward on the chunks of the body in this callback.
153
+ // NOTE: the parser does not use Buffer.slice(), it is giving you the actual buffer which was passed into it.
154
+ sys.puts("chunk: " + (end-start));
155
+ current.body.push(buffer.slice(start, end));
156
+ };
157
+
158
+ parser.onError = function(err) {
159
+ sys.puts("error\n" + JSON.stringify(err, null, "\t"));
160
+ };
161
+
162
+ ...
173
163
174
- </code >
164
+ stream.ondata = function (buffer, start, end) {
165
+ parser.execute(buffer, start, end);
166
+ };
175
167
176
168
# Dependencies
177
169
178
170
For the test script in examples/test.js you need to copy binary.node from this project:
179
171
180
- http://github.com/billywhizz/node-binary
172
+ - < http://github.com/billywhizz/node-binary >
181
173
182
174
to the lib directory so that the binary messages for sending to memcached can be created.
0 commit comments