Deprecation warnings have been added to help the conversion to this new API.
</div>\r
<h2 id="_api">API</h2>\r
<div class="sectionbody">\r
-<div class="paragraph"><p>Node supports 4 byte-string encodings. ASCII (<tt>"ascii"</tt>), UTF-8 (<tt>"utf8"</tt>)\r
-both use the string object, obviously. Then two "raw binary" encodings - one\r
-uses an array of integers (<tt>"raw"</tt>) and the other uses a string (<tt>"raws"</tt>).\r
-Neither raw encodings are perfect and their implementations are rather\r
-inefficient. Hopefully the raw encoding situation will improve in the\r
-future.</p></div>\r
+<div class="paragraph"><p>Node supports 3 string encodings. UTF-8 (<tt>"utf8"</tt>), ASCII (<tt>"ascii"</tt>), and\r
+Binary (<tt>"binary"</tt>). <tt>"ascii"</tt> and <tt>"binary"</tt> only look at the first 8 bits\r
+of the 16bit javascript string characters. Both are relatively fast—use\r
+them if you can. <tt>"utf8"</tt> is slower and should be avoided when possible.</p></div>\r
<div class="paragraph"><p>Unless otherwise noted, functions are all asynchronous and do not block\r
execution.</p></div>\r
<h3 id="_helpers">Helpers</h3><div style="clear:left"></div>\r
<td align="left" valign="top"><p class="table"><tt>data</tt></p></td>\r
<td align="left" valign="top"><p class="table">Made when stdin has received a chunk of data.\r
Depending on the encoding that stdin was opened\r
- with, <tt>data</tt> will be either an array of integers\r
- (raw encoding) or a string (ascii or utf8\r
- encoding). This event will only be emited after\r
- <tt>node.stdio.open()</tt> has been called.</p></td>\r
+ with, <tt>data</tt> will be a string. This event will\r
+ only be emited after <tt>node.stdio.open()</tt> has\r
+ been called.</p></td>\r
</tr>\r
<tr>\r
<td align="left" valign="top"><p class="table"><tt>"close"</tt></p></td>\r
<dd>\r
<p>\r
Write data to the child process’s <tt>stdin</tt>. The second argument is optional and\r
-specifies the encoding: possible values are <tt>"utf8"</tt>, <tt>"ascii"</tt>, and <tt>"raw"</tt>.\r
+specifies the encoding: possible values are <tt>"utf8"</tt>, <tt>"ascii"</tt>, and\r
+<tt>"binary"</tt>.\r
</p>\r
</dd>\r
<dt class="hdlist1">\r
<td align="left" valign="top"><p class="table"><tt>chunk</tt></p></td>\r
<td align="left" valign="top"><p class="table">Emitted when a piece of the message body is received. Example: A chunk of\r
the body is given as the single argument. The transfer-encoding has been\r
-decoded. The body chunk is either a String in the case of UTF-8 encoding or\r
-an array of numbers in the case of raw encoding. The body encoding is set\r
+decoded. The body chunk is a String. The body encoding is set\r
with <tt>request.setBodyEncoding()</tt>.</p></td>\r
</tr>\r
<tr>\r
</dt>\r
<dd>\r
<p>\r
-Set the encoding for the request body. Either <tt>"utf8"</tt> or <tt>"raw"</tt>. Defaults\r
-to raw.\r
+Set the encoding for the request body. Either <tt>"utf8"</tt> or <tt>"binary"</tt>. Defaults\r
+to <tt>"binary"</tt>.\r
</p>\r
</dd>\r
<dt class="hdlist1">\r
<td align="left" valign="top"><p class="table"><tt>chunk</tt></p></td>\r
<td align="left" valign="top"><p class="table">Emitted when a piece of the message body is received. Example: A chunk of\r
the body is given as the single argument. The transfer-encoding has been\r
-decoded. The body chunk is either a String in the case of UTF-8 encoding or\r
-an array of numbers in the case of raw encoding. The body encoding is set\r
-with <tt>response.setBodyEncoding()</tt>.</p></td>\r
+decoded. The body chunk a String. The body encoding is set with\r
+<tt>response.setBodyEncoding()</tt>.</p></td>\r
</tr>\r
<tr>\r
<td align="left" valign="top"><p class="table"><tt>"complete"</tt></p></td>\r
</dt>\r
<dd>\r
<p>\r
- Set the encoding for the response body. Either <tt>"utf8"</tt> or <tt>"raw"</tt>.\r
- Defaults to raw.\r
+ Set the encoding for the response body. Either <tt>"utf8"</tt> or <tt>"binary"</tt>.\r
+ Defaults to <tt>"binary"</tt>.\r
</p>\r
</dd>\r
<dt class="hdlist1">\r
<td align="left" valign="top"><p class="table"><tt>"receive"</tt></p></td>\r
<td align="left" valign="top"><p class="table"><tt>data</tt></p></td>\r
<td align="left" valign="top"><p class="table">Called when data is received on the\r
- connection. Encoding of data is set\r
- by <tt>connection.setEncoding()</tt>. <tt>data</tt>\r
- will either be a string, in the case of\r
- utf8, or an array of integer in the case\r
- of raw encoding.</p></td>\r
+ connection. <tt>data</tt> will be a string.\r
+ Encoding of data is set by\r
+ <tt>connection.setEncoding()</tt>.</p></td>\r
</tr>\r
<tr>\r
<td align="left" valign="top"><p class="table"><tt>"eof"</tt></p></td>\r
</dt>\r
<dd>\r
<p>\r
-Sets the encoding (either <tt>"utf8"</tt> or <tt>"raw"</tt>) for data that is received.\r
+Sets the encoding (either <tt>"ascii"</tt>, <tt>"utf8"</tt>, or <tt>"binary"</tt>) for data that is received.\r
</p>\r
</dd>\r
<dt class="hdlist1">\r
</dt>\r
<dd>\r
<p>\r
-Sends data on the connection. The data should be eithre an array\r
-of integers (for raw binary) or a string (for utf8 or ascii).\r
-The second parameter specifies the encoding in the case of a\r
-string—it defaults to ASCII because encoding to UTF8 is\r
+Sends data on the connection. The second parameter specifies the encoding\r
+in the case of a string—it defaults to ASCII because encoding to UTF8 is\r
rather slow.\r
</p>\r
</dd>\r
<div id="footer">\r
<div id="footer-text">\r
Version 0.1.11<br />\r
-Last updated 2009-09-18 18:31:06 CEST\r
+Last updated 2009-09-21 12:26:35 CEST\r
</div>\r
</div>\r
</body>\r
== API
-Node supports 4 byte-string encodings. ASCII (+"ascii"+), UTF-8 (+"utf8"+)
-both use the string object, obviously. Then two "raw binary" encodings - one
-uses an array of integers (+"raw"+) and the other uses a string (+"raws"+).
-Neither raw encodings are perfect and their implementations are rather
-inefficient. Hopefully the raw encoding situation will improve in the
-future.
+Node supports 3 string encodings. UTF-8 (+"utf8"+), ASCII (+"ascii"+), and
+Binary (+"binary"+). +"ascii"+ and +"binary"+ only look at the first 8 bits
+of the 16bit javascript string characters. Both are relatively fast--use
+them if you can. +"utf8"+ is slower and should be avoided when possible.
Unless otherwise noted, functions are all asynchronous and do not block
execution.
| +"data"+ | +data+ | Made when stdin has received a chunk of data.
Depending on the encoding that stdin was opened
- with, +data+ will be either an array of integers
- (raw encoding) or a string (ascii or utf8
- encoding). This event will only be emited after
- +node.stdio.open()+ has been called.
+ with, +data+ will be a string. This event will
+ only be emited after +node.stdio.open()+ has
+ been called.
| +"close"+ | | Made when stdin has been closed.
|=========================================================
+child.write(data, encoding="ascii")+ ::
Write data to the child process's +stdin+. The second argument is optional and
-specifies the encoding: possible values are +"utf8"+, +"ascii"+, and +"raw"+.
+specifies the encoding: possible values are +"utf8"+, +"ascii"+, and
++"binary"+.
+child.close()+ ::
|+"body"+ | +chunk+ |
Emitted when a piece of the message body is received. Example: A chunk of
the body is given as the single argument. The transfer-encoding has been
-decoded. The body chunk is either a String in the case of UTF-8 encoding or
-an array of numbers in the case of raw encoding. The body encoding is set
+decoded. The body chunk is a String. The body encoding is set
with +request.setBodyEncoding()+.
|+"complete"+ | |
+request.setBodyEncoding(encoding)+ ::
-Set the encoding for the request body. Either +"utf8"+ or +"raw"+. Defaults
-to raw.
+Set the encoding for the request body. Either +"utf8"+ or +"binary"+. Defaults
+to +"binary"+.
+request.pause()+ ::
|+"body"+ | +chunk+ |
Emitted when a piece of the message body is received. Example: A chunk of
the body is given as the single argument. The transfer-encoding has been
-decoded. The body chunk is either a String in the case of UTF-8 encoding or
-an array of numbers in the case of raw encoding. The body encoding is set
-with +response.setBodyEncoding()+.
+decoded. The body chunk a String. The body encoding is set with
++response.setBodyEncoding()+.
|+"complete"+ | |
Emitted exactly once for each message. No arguments.
The response headers.
+response.setBodyEncoding(encoding)+ ::
- Set the encoding for the response body. Either +"utf8"+ or +"raw"+.
- Defaults to raw.
+ Set the encoding for the response body. Either +"utf8"+ or +"binary"+.
+ Defaults to +"binary"+.
+response.pause()+ ::
Pauses response from emitting events. Useful to throttle back a download.
after a call to +createConnection()+ or
+connect()+.
|+"receive"+ | +data+ | Called when data is received on the
- connection. Encoding of data is set
- by +connection.setEncoding()+. +data+
- will either be a string, in the case of
- utf8, or an array of integer in the case
- of raw encoding.
+ connection. +data+ will be a string.
+ Encoding of data is set by
+ +connection.setEncoding()+.
|+"eof"+ | | Called when the other end of the
connection sends a FIN packet.
After this is emitted the +readyState+
+connection.setEncoding(encoding)+::
-Sets the encoding (either +"utf8"+ or +"raw"+) for data that is received.
+Sets the encoding (either +"ascii"+, +"utf8"+, or +"binary"+) for data that is received.
+connection.send(data, encoding="ascii")+::
-Sends data on the connection. The data should be eithre an array
-of integers (for raw binary) or a string (for utf8 or ascii).
-The second parameter specifies the encoding in the case of a
-string--it defaults to ASCII because encoding to UTF8 is
+Sends data on the connection. The second parameter specifies the encoding
+in the case of a string--it defaults to ASCII because encoding to UTF8 is
rather slow.
.\" Title: node
.\" Author:
.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
-.\" Date: 09/18/2009
+.\" Date: 09/21/2009
.\" Manual:
.\" Source:
.\"
-.TH "NODE" "1" "09/18/2009" "" ""
+.TH "NODE" "1" "09/21/2009" "" ""
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.fi
.RE
.SH "API"
-Node supports 4 byte\-string encodings\. ASCII ("ascii"), UTF\-8 ("utf8") both use the string object, obviously\. Then two "raw binary" encodings \- one uses an array of integers ("raw") and the other uses a string ("raws")\. Neither raw encodings are perfect and their implementations are rather inefficient\. Hopefully the raw encoding situation will improve in the future\.
+Node supports 3 string encodings\. UTF\-8 ("utf8"), ASCII ("ascii"), and Binary ("binary")\. "ascii" and "binary" only look at the first 8 bits of the 16bit javascript string characters\. Both are relatively fast\(emuse them if you can\. "utf8" is slower and should be avoided when possible\.
.sp
Unless otherwise noted, functions are all asynchronous and do not block execution\.
.sp
data
.sp
T}:T{
-Made when stdin has received a chunk of data\. Depending on the encoding that stdin was opened with, data will be either an array of integers (raw encoding) or a string (ascii or utf8 encoding)\. This event will only be emited after node\.stdio\.open() has been called\.
+Made when stdin has received a chunk of data\. Depending on the encoding that stdin was opened with, data will be a string\. This event will only be emited after node\.stdio\.open() has been called\.
.sp
T}
T{
stdin\. The second argument is optional and specifies the encoding: possible values are
"utf8",
"ascii", and
-"raw"\.
+"binary"\.
.RE
.PP
child\.close()
chunk
.sp
T}:T{
-Emitted when a piece of the message body is received\. Example: A chunk of the body is given as the single argument\. The transfer\-encoding has been decoded\. The body chunk is either a String in the case of UTF\-8 encoding or an array of numbers in the case of raw encoding\. The body encoding is set with request\.setBodyEncoding()\.
+Emitted when a piece of the message body is received\. Example: A chunk of the body is given as the single argument\. The transfer\-encoding has been decoded\. The body chunk is a String\. The body encoding is set with request\.setBodyEncoding()\.
.sp
T}
T{
Set the encoding for the request body\. Either
"utf8"
or
-"raw"\. Defaults to raw\.
+"binary"\. Defaults to
+"binary"\.
.RE
.PP
request\.pause()
chunk
.sp
T}:T{
-Emitted when a piece of the message body is received\. Example: A chunk of the body is given as the single argument\. The transfer\-encoding has been decoded\. The body chunk is either a String in the case of UTF\-8 encoding or an array of numbers in the case of raw encoding\. The body encoding is set with response\.setBodyEncoding()\.
+Emitted when a piece of the message body is received\. Example: A chunk of the body is given as the single argument\. The transfer\-encoding has been decoded\. The body chunk a String\. The body encoding is set with response\.setBodyEncoding()\.
.sp
T}
T{
Set the encoding for the response body\. Either
"utf8"
or
-"raw"\. Defaults to raw\.
+"binary"\. Defaults to
+"binary"\.
.RE
.PP
response\.pause()
data
.sp
T}:T{
-Called when data is received on the connection\. Encoding of data is set by connection\.setEncoding()\. data will either be a string, in the case of utf8, or an array of integer in the case of raw encoding\.
+Called when data is received on the connection\. data will be a string\. Encoding of data is set by connection\.setEncoding()\.
.sp
T}
T{
connection\.setEncoding(encoding)
.RS 4
Sets the encoding (either
-"utf8"
-or
-"raw") for data that is received\.
+"ascii",
+"utf8", or
+"binary") for data that is received\.
.RE
.PP
connection\.send(data, encoding="ascii")
.RS 4
-Sends data on the connection\. The data should be eithre an array of integers (for raw binary) or a string (for utf8 or ascii)\. The second parameter specifies the encoding in the case of a string\(emit defaults to ASCII because encoding to UTF8 is rather slow\.
+Sends data on the connection\. The second parameter specifies the encoding in the case of a string\(emit defaults to ASCII because encoding to UTF8 is rather slow\.
.RE
.PP
connection\.close()
cat_promise.emitError(new Error("Could not open " + path));
});
open_promise.addCallback(function (fd) {
- var content = (encoding === "raw" ? [] : "");
+ var content = "";
var pos = 0;
function readChunk () {
if (length === 0) {
this.output.push(data);
- encoding = encoding || (data.constructor === Array ? "raw" : "ascii");
+ encoding = encoding || "ascii";
this.outputEncodings.push(encoding);
return;
}
}
this.output.push(data);
- encoding = encoding || (data.constructor === Array ? "raw" : "ascii");
+ encoding = encoding || "ascii";
this.outputEncodings.push(encoding);
};
namespace node {
#define UTF8_SYMBOL String::NewSymbol("utf8")
-#define RAW_SYMBOL String::NewSymbol("raw")
-#define RAWS_SYMBOL String::NewSymbol("raws")
+#define BINARY_SYMBOL String::NewSymbol("binary")
#define ASCII_SYMBOL String::NewSymbol("ascii")
#define SERVER_SYMBOL String::NewSymbol("server")
assert(connection);
if (!args[0]->IsString()) {
- connection->encoding_ = RAW;
- return scope.Close(RAW_SYMBOL);
+ connection->encoding_ = BINARY;
+ return scope.Close(BINARY_SYMBOL);
}
switch (ParseEncoding(args[0])) {
connection->encoding_ = UTF8;
return scope.Close(UTF8_SYMBOL);
- case RAW:
- connection->encoding_ = RAW;
- return scope.Close(RAW_SYMBOL);
-
- case RAWS:
- connection->encoding_ = RAWS;
- return scope.Close(RAWS_SYMBOL);
+ case BINARY:
+ connection->encoding_ = BINARY;
+ return scope.Close(BINARY_SYMBOL);
}
assert(0 && "this shouldn't happen");
return ThrowException(Exception::Error(
const v8::AccessorInfo& info);
Connection() : EventEmitter() {
- encoding_ = RAW;
+ encoding_ = BINARY;
host_ = NULL;
port_ = NULL;
static int dash_dash_index = 0;
-Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
+enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
HandleScope scope;
- if (!len) return scope.Close(Null());
+ if (!encoding_v->IsString()) return _default;
- if (encoding == RAW) {
- // raw encoding
- Local<Array> array = Array::New(len);
- for (size_t i = 0; i < len; i++) {
- unsigned char val = static_cast<const unsigned char*>(buf)[i];
- array->Set(Integer::New(i), Integer::New(val));
- }
- return scope.Close(array);
+ String::Utf8Value encoding(encoding_v->ToString());
+
+ if (strcasecmp(*encoding, "utf8") == 0) {
+ return UTF8;
+ } else if (strcasecmp(*encoding, "ascii") == 0) {
+ return ASCII;
+ } else if (strcasecmp(*encoding, "binary") == 0) {
+ return BINARY;
+ } else if (strcasecmp(*encoding, "raw") == 0) {
+ fprintf(stderr, "'raw' (array of integers) has been removed. "
+ "Use 'binary'.\n");
+ return BINARY;
+ } else if (strcasecmp(*encoding, "raws") == 0) {
+ fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
+ "Please update your code.\n");
+ return BINARY;
+ } else {
+ return _default;
}
+}
+
+Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
+ HandleScope scope;
+
+ if (!len) return scope.Close(Null());
- if (encoding == RAWS) {
+ if (encoding == BINARY) {
const unsigned char *cbuf = static_cast<const unsigned char*>(buf);
uint16_t * twobytebuf = new uint16_t[len];
for (size_t i = 0; i < len; i++) {
twobytebuf[i] = cbuf[i];
}
Local<String> chunk = String::New(twobytebuf, len);
- delete twobytebuf;
+ delete twobytebuf; // TODO use ExternalTwoByteString?
return scope.Close(chunk);
}
HandleScope scope;
if (val->IsArray()) {
- if (encoding != RAW) return -1;
- Handle<Array> array = Handle<Array>::Cast(val);
- return array->Length();
+ fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
+ "Use 'binary'.\n");
+ assert(0);
+ return -1;
}
- if (!val->IsString()) return -1;
-
- Handle<String> str = Handle<String>::Cast(val);
+ Local<String> str = val->ToString();
if (encoding == UTF8) return str->Utf8Length();
// http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611
if (val->IsArray()) {
- if (encoding != RAW) return -1;
- Handle<Array> array = Handle<Array>::Cast(val);
- size_t array_len = array->Length();
- for (i = 0; i < MIN(buflen, array_len); i++) {
- Local<Value> int_value = array->Get(Integer::New(i));
- buf[i] = int_value->IntegerValue();
- }
- return i;
+ fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
+ "Use 'binary'.\n");
+ assert(0);
+ return -1;
}
- assert(val->IsString());
- Handle<String> str = Handle<String>::Cast(val);
+ Local<String> str = val->ToString();
if (encoding == UTF8) {
str->WriteUtf8(buf, buflen);
// THIS IS AWFUL!!! FIXME
+ assert(encoding == BINARY);
+
uint16_t * twobytebuf = new uint16_t[buflen];
str->Write(twobytebuf, 0, buflen);
ev_async_send(EV_DEFAULT_UC_ &eio_watcher);
}
-enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
- HandleScope scope;
-
- if (!encoding_v->IsString())
- return RAW;
-
- String::Utf8Value encoding(encoding_v->ToString());
-
- if (strcasecmp(*encoding, "utf8") == 0) {
- return UTF8;
- } else if (strcasecmp(*encoding, "ascii") == 0) {
- return ASCII;
- } else if (strcasecmp(*encoding, "raw") == 0) {
- return RAW;
- } else if (strcasecmp(*encoding, "raws") == 0) {
- return RAWS;
- } else {
- return _default;
- }
-}
-
static void ExecuteNativeJS(const char *filename, const char *data) {
HandleScope scope;
TryCatch try_catch;
__callback##_TEM); \
} while (0)
-enum encoding {ASCII, UTF8, RAW, RAWS};
+enum encoding {ASCII, UTF8, BINARY};
enum encoding ParseEncoding(v8::Handle<v8::Value> encoding_v,
- enum encoding _default = RAW);
+ enum encoding _default = BINARY);
void FatalException(v8::TryCatch &try_catch);
v8::Local<v8::Value> Encode(const void *buf, size_t len,
- enum encoding encoding = RAW);
+ enum encoding encoding = BINARY);
// Returns -1 if the handle was not valid for decoding
ssize_t DecodeBytes(v8::Handle<v8::Value>,
- enum encoding encoding = RAW);
+ enum encoding encoding = BINARY);
// returns bytes written.
ssize_t DecodeWrite(char *buf,
size_t buflen,
v8::Handle<v8::Value>,
- enum encoding encoding = RAW);
+ enum encoding encoding = BINARY);
} // namespace node
if (!(x)) throw new Error(msg || "assertion error");
};
-// This is useful for dealing with raw encodings.
-node.encodeUtf8 = function (array) {
- var string = "";
- var i = 0;
- var c = c1 = c2 = 0;
-
- while (i < array.length) {
- c = array[i];
-
- if (c < 128) {
- string += String.fromCharCode(c);
- i++;
- } else if ((c > 191) && (c < 224)) {
- c2 = array[i+1];
- string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
- i += 2;
- } else {
- c2 = array[i+1];
- c3 = array[i+2];
- string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
- i += 3;
- }
- }
-
- return string;
-};
-
node.cat = function(location, encoding) {
var url_re = new RegExp("^http:\/\/");
var f = url_re.exec(location) ? node.http.cat : node.fs.cat;
+++ /dev/null
-include("common.js");
-
-var a = [116,101,115,116,32,206,163,207,131,207,128,206,177,32,226,161,140,226,160, 129,226,160,167,226,160,145];
-var s = node.encodeUtf8(a);
-assertEquals("test Σσπα ⡌⠁⠧⠑", s);
-
-a = [104, 101, 108, 108, 111];
-s = node.encodeUtf8(a);
-assertEquals("hello", s);
--- /dev/null
+include("common.js");
+PORT = 23123;
+
+binaryString = "";
+for (var i = 255; i >= 0; i--) {
+ var s = "'\\" + i.toString(8) + "'";
+ S = eval(s);
+ node.error( s
+ + " "
+ + JSON.stringify(S)
+ + " "
+ + JSON.stringify(String.fromCharCode(i))
+ + " "
+ + S.charCodeAt(0)
+ );
+ node.assert(S.charCodeAt(0) == i);
+ node.assert(S == String.fromCharCode(i));
+ binaryString += S;
+}
+
+var echoServer = node.tcp.createServer(function (connection) {
+ connection.setEncoding("binary");
+ connection.addListener("receive", function (chunk) {
+ node.error("recved: " + JSON.stringify(chunk));
+ connection.send(chunk, "binary");
+ });
+ connection.addListener("eof", function () {
+ connection.close();
+ });
+});
+echoServer.listen(PORT);
+
+var recv = "";
+var j = 0;
+
+var c = node.tcp.createConnection(PORT);
+
+c.setEncoding("binary");
+c.addListener("receive", function (chunk) {
+ if (j < 256) {
+ node.error("send " + j);
+ c.send(String.fromCharCode(j), "binary");
+ j++;
+ } else {
+ c.close();
+ }
+ recv += chunk;
+});
+
+c.addListener("connect", function () {
+ c.send(binaryString, "binary");
+});
+
+c.addListener("close", function () {
+ p(recv);
+ echoServer.close();
+});
+
+process.addListener("exit", function () {
+ puts("recv: " + JSON.stringify(recv));
+
+ assertEquals(2*256, recv.length);
+
+ var a = recv.split("");
+
+ var first = a.slice(0,256).reverse().join("");
+ puts("first: " + JSON.stringify(first));
+
+ var second = a.slice(256,2*256).join("");
+ puts("second: " + JSON.stringify(second));
+
+ assertEquals(first, second);
+});
+++ /dev/null
-include("common.js");
-PORT = 23123;
-
-var echoServer = node.tcp.createServer(function (connection) {
- connection.addListener("receive", function (chunk) {
- connection.send(chunk, "raw");
- });
- connection.addListener("eof", function () {
- connection.close();
- });
-});
-echoServer.listen(PORT);
-
-var recv = [];
-var j = 0;
-
-var c = node.tcp.createConnection(PORT);
-
-c.addListener("receive", function (chunk) {
- if (++j < 256) {
- c.send([j], "raw");
- } else {
- c.close();
- }
- for (var i = 0; i < chunk.length; i++) {
- recv.push(chunk[i]);
- }
-});
-
-c.addListener("connect", function () {
- c.send([j], "raw");
-});
-
-c.addListener("close", function () {
- p(recv);
- echoServer.close();
-});
-
-process.addListener("exit", function () {
- var expected = [];
- for (var i = 0; i < 256; i++) {
- expected.push(i);
- }
- assertEquals(expected, recv);
-});
+++ /dev/null
-include("common.js");
-PORT = 23123;
-
-binaryString = "";
-for (var i = 0; i < 256; i++) {
- var j = 255 - i;
- var s = "'\\" + j.toString(8) + "'";
- S = eval(s);
- puts(s + " " + JSON.stringify(S) + " " + S.charCodeAt(0));
- node.assert(S.charCodeAt(0) == j);
- binaryString += S;
-}
-
-var echoServer = node.tcp.createServer(function (connection) {
- connection.setEncoding("raws");
- connection.addListener("receive", function (chunk) {
- puts("recved: " + JSON.stringify(chunk));
- connection.send(chunk, "raws");
- });
- connection.addListener("eof", function () {
- connection.close();
- });
-});
-echoServer.listen(PORT);
-
-var recv = "";
-var j = 0;
-
-var c = node.tcp.createConnection(PORT);
-
-c.setEncoding("raws");
-c.addListener("receive", function (chunk) {
- if (j++ < 256) {
- c.send([j]);
- } else {
- c.close();
- }
- recv += chunk;
-});
-
-c.addListener("connect", function () {
- c.send(binaryString, "raws");
-});
-
-c.addListener("close", function () {
- p(recv);
- echoServer.close();
-});
-
-process.addListener("exit", function () {
- assertEquals(2*256, recv.length);
- for (var i = 0; i < 256; i++) {
- assertEquals(i, recv.charCodeAt(255+i));
- assertEquals(i, recv.charCodeAt(255-i));
- }
-});