diff --git a/doc/api.html b/doc/api.html index 9e3eb40e90..8cf60291e3 100644 --- a/doc/api.html +++ b/doc/api.html @@ -55,12 +55,10 @@ Server running at http://127.0.0.1:8000/

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.

Helpers

@@ -373,10 +371,9 @@ cellspacing="0" cellpadding="4">

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"

@@ -599,7 +596,8 @@ The PID of the child process.

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".

@@ -1050,8 +1048,7 @@ cellspacing="0" cellpadding="4">

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().

@@ -1123,8 +1120,8 @@ The HTTP protocol version as a string. Read only. Examples:

-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".

@@ -1380,9 +1377,8 @@ cellspacing="0" cellpadding="4">

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"

@@ -1424,8 +1420,8 @@ After emitted no other events will be emitted on the response.

- 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".

@@ -1574,11 +1570,9 @@ cellspacing="0" cellpadding="4">

"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"

@@ -1658,7 +1652,7 @@ Either "closed", "open", "opening", "readOnly"

-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.

@@ -1666,10 +1660,8 @@ Sets the encoding (either "utf8" or "raw") for data that is re

-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.

@@ -1930,7 +1922,7 @@ init (Handle<Object> target) diff --git a/doc/api.txt b/doc/api.txt index 63ce96f81e..9e211cd9d0 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -38,12 +38,10 @@ Server running at http://127.0.0.1:8000/ == 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. @@ -222,10 +220,9 @@ synchronous. | +"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. |========================================================= @@ -394,7 +391,8 @@ The PID of the child process. +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()+ :: @@ -640,8 +638,7 @@ the user--and passed as the first argument to a +"request"+ listener. |+"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"+ | | @@ -692,8 +689,8 @@ The HTTP protocol version as a string. Read only. Examples: +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()+ :: @@ -895,9 +892,8 @@ This object is created internally and passed to the +"response"+ event. |+"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. @@ -916,8 +912,8 @@ After emitted no other events will be emitted on the response. 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. @@ -1005,11 +1001,9 @@ socket for +node.tcp.Server+. 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+ @@ -1055,13 +1049,11 @@ Either +"closed"+, +"open"+, +"opening"+, +"readOnly"+, or +"writeOnly"+. +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. diff --git a/doc/node.1 b/doc/node.1 index c98d01bfcb..368230a923 100644 --- a/doc/node.1 +++ b/doc/node.1 @@ -1,11 +1,11 @@ .\" Title: node .\" Author: .\" Generator: DocBook XSL Stylesheets v1.73.2 -.\" 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) @@ -38,7 +38,7 @@ Server running at http://127\.0\.0\.1:8000/ .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 @@ -329,7 +329,7 @@ T}:T{ 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{ @@ -558,7 +558,7 @@ Write data to the child process\(cqs stdin\. The second argument is optional and specifies the encoding: possible values are "utf8", "ascii", and -"raw"\. +"binary"\. .RE .PP child\.close() @@ -946,7 +946,7 @@ T}:T{ 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{ @@ -1030,7 +1030,8 @@ request\.setBodyEncoding(encoding) Set the encoding for the request body\. Either "utf8" or -"raw"\. Defaults to raw\. +"binary"\. Defaults to +"binary"\. .RE .PP request\.pause() @@ -1300,7 +1301,7 @@ T}:T{ 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{ @@ -1338,7 +1339,8 @@ response\.setBodyEncoding(encoding) Set the encoding for the response body\. Either "utf8" or -"raw"\. Defaults to raw\. +"binary"\. Defaults to +"binary"\. .RE .PP response\.pause() @@ -1502,7 +1504,7 @@ T}:T{ 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{ @@ -1589,14 +1591,14 @@ Either 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() diff --git a/src/file.js b/src/file.js index 1b05bf2015..e5d7c0eb49 100644 --- a/src/file.js +++ b/src/file.js @@ -14,7 +14,7 @@ node.fs.cat = function (path, encoding) { 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 () { diff --git a/src/http.js b/src/http.js index ea558cd1f5..736fb1b756 100644 --- a/src/http.js +++ b/src/http.js @@ -178,7 +178,7 @@ OutgoingMessage.prototype.send = function (data, encoding) { if (length === 0) { this.output.push(data); - encoding = encoding || (data.constructor === Array ? "raw" : "ascii"); + encoding = encoding || "ascii"; this.outputEncodings.push(encoding); return; } @@ -197,7 +197,7 @@ OutgoingMessage.prototype.send = function (data, encoding) { } this.output.push(data); - encoding = encoding || (data.constructor === Array ? "raw" : "ascii"); + encoding = encoding || "ascii"; this.outputEncodings.push(encoding); }; diff --git a/src/net.cc b/src/net.cc index bf4f574e29..59949dace5 100644 --- a/src/net.cc +++ b/src/net.cc @@ -17,8 +17,7 @@ using namespace v8; 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") @@ -257,8 +256,8 @@ Handle Connection::SetEncoding(const Arguments& args) { 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])) { @@ -270,13 +269,9 @@ Handle Connection::SetEncoding(const Arguments& args) { 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( diff --git a/src/net.h b/src/net.h index a450d33016..0b682a6600 100644 --- a/src/net.h +++ b/src/net.h @@ -33,7 +33,7 @@ class Connection : public EventEmitter { const v8::AccessorInfo& info); Connection() : EventEmitter() { - encoding_ = RAW; + encoding_ = BINARY; host_ = NULL; port_ = NULL; diff --git a/src/node.cc b/src/node.cc index e4a8404632..2e46c0a7d0 100644 --- a/src/node.cc +++ b/src/node.cc @@ -30,22 +30,38 @@ namespace node { static int dash_dash_index = 0; -Local Encode(const void *buf, size_t len, enum encoding encoding) { +enum encoding ParseEncoding(Handle 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::New(len); - for (size_t i = 0; i < len; i++) { - unsigned char val = static_cast(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 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(buf); uint16_t * twobytebuf = new uint16_t[len]; for (size_t i = 0; i < len; i++) { @@ -53,7 +69,7 @@ Local Encode(const void *buf, size_t len, enum encoding encoding) { twobytebuf[i] = cbuf[i]; } Local chunk = String::New(twobytebuf, len); - delete twobytebuf; + delete twobytebuf; // TODO use ExternalTwoByteString? return scope.Close(chunk); } @@ -67,14 +83,13 @@ ssize_t DecodeBytes(v8::Handle val, enum encoding encoding) { HandleScope scope; if (val->IsArray()) { - if (encoding != RAW) return -1; - Handle array = Handle::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 str = Handle::Cast(val); + Local str = val->ToString(); if (encoding == UTF8) return str->Utf8Length(); @@ -99,18 +114,13 @@ ssize_t DecodeWrite(char *buf, size_t buflen, // http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611 if (val->IsArray()) { - if (encoding != RAW) return -1; - Handle array = Handle::Cast(val); - size_t array_len = array->Length(); - for (i = 0; i < MIN(buflen, array_len); i++) { - Local 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 str = Handle::Cast(val); + Local str = val->ToString(); if (encoding == UTF8) { str->WriteUtf8(buf, buflen); @@ -124,6 +134,8 @@ ssize_t DecodeWrite(char *buf, size_t buflen, // THIS IS AWFUL!!! FIXME + assert(encoding == BINARY); + uint16_t * twobytebuf = new uint16_t[buflen]; str->Write(twobytebuf, 0, buflen); @@ -310,27 +322,6 @@ static void EIOWantPoll(void) { ev_async_send(EV_DEFAULT_UC_ &eio_watcher); } -enum encoding ParseEncoding(Handle 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; diff --git a/src/node.h b/src/node.h index be8c191c37..ce717f1c49 100644 --- a/src/node.h +++ b/src/node.h @@ -34,23 +34,23 @@ do { \ __callback##_TEM); \ } while (0) -enum encoding {ASCII, UTF8, RAW, RAWS}; +enum encoding {ASCII, UTF8, BINARY}; enum encoding ParseEncoding(v8::Handle encoding_v, - enum encoding _default = RAW); + enum encoding _default = BINARY); void FatalException(v8::TryCatch &try_catch); v8::Local 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, - enum encoding encoding = RAW); + enum encoding encoding = BINARY); // returns bytes written. ssize_t DecodeWrite(char *buf, size_t buflen, v8::Handle, - enum encoding encoding = RAW); + enum encoding encoding = BINARY); } // namespace node diff --git a/src/util.js b/src/util.js index ad0a4cd88c..9850f6eef8 100644 --- a/src/util.js +++ b/src/util.js @@ -23,33 +23,6 @@ node.assert = function (x, msg) { 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; diff --git a/test/mjsunit/test-encode-utf8.js b/test/mjsunit/test-encode-utf8.js deleted file mode 100644 index 374ead6f05..0000000000 --- a/test/mjsunit/test-encode-utf8.js +++ /dev/null @@ -1,9 +0,0 @@ -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); diff --git a/test/mjsunit/test-tcp-binary.js b/test/mjsunit/test-tcp-binary.js new file mode 100644 index 0000000000..0cf2b358c3 --- /dev/null +++ b/test/mjsunit/test-tcp-binary.js @@ -0,0 +1,73 @@ +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); +}); diff --git a/test/mjsunit/test-tcp-raw.js b/test/mjsunit/test-tcp-raw.js deleted file mode 100644 index 53c2ee1255..0000000000 --- a/test/mjsunit/test-tcp-raw.js +++ /dev/null @@ -1,45 +0,0 @@ -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); -}); diff --git a/test/mjsunit/test-tcp-raws.js b/test/mjsunit/test-tcp-raws.js deleted file mode 100644 index a10ee1841b..0000000000 --- a/test/mjsunit/test-tcp-raws.js +++ /dev/null @@ -1,56 +0,0 @@ -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)); - } -});