mirror of https://github.com/nodejs/node.git
Move node.inherit, node.path, node.cat to new file: util.js
parent
22e85cbc0b
commit
eb10553634
31
src/http.js
31
src/http.js
|
@ -1,27 +1,4 @@
|
|||
(function () {
|
||||
|
||||
/**
|
||||
* Inherit the prototype methods from one constructor into another.
|
||||
*
|
||||
* The Function.prototype.inherits from lang.js rewritten as a standalone
|
||||
* function (not on Function.prototype). NOTE: If this file is to be loaded
|
||||
* during bootstrapping this function needs to be revritten using some native
|
||||
* functions as prototype setup using normal JavaScript does not work as
|
||||
* expected during bootstrapping (see mirror.js in r114903).
|
||||
*
|
||||
* @param {function} ctor Constructor function which needs to inherit the
|
||||
* prototype
|
||||
* @param {function} superCtor Constructor function to inherit prototype from
|
||||
*/
|
||||
function inherits(ctor, superCtor) {
|
||||
var tempCtor = function(){};
|
||||
tempCtor.prototype = superCtor.prototype;
|
||||
ctor.super_ = superCtor.prototype;
|
||||
ctor.prototype = new tempCtor();
|
||||
ctor.prototype.constructor = ctor;
|
||||
}
|
||||
|
||||
|
||||
CRLF = "\r\n";
|
||||
node.http.STATUS_CODES = {
|
||||
100 : 'Continue',
|
||||
|
@ -150,7 +127,7 @@ function IncomingMessage (connection) {
|
|||
this.statusCode = null;
|
||||
this.client = this.connection;
|
||||
}
|
||||
inherits(IncomingMessage, node.EventEmitter);
|
||||
node.inherits(IncomingMessage, node.EventEmitter);
|
||||
|
||||
IncomingMessage.prototype.setBodyEncoding = function (enc) {
|
||||
// TODO: Find a cleaner way of doing this.
|
||||
|
@ -174,7 +151,7 @@ function OutgoingMessage () {
|
|||
|
||||
this.finished = false;
|
||||
}
|
||||
inherits(OutgoingMessage, node.EventEmitter);
|
||||
node.inherits(OutgoingMessage, node.EventEmitter);
|
||||
|
||||
OutgoingMessage.prototype.send = function (data, encoding) {
|
||||
data.encoding = data.constructor === String ? encoding || "ascii" : "raw";
|
||||
|
@ -261,7 +238,7 @@ function ServerResponse () {
|
|||
this.should_keep_alive = true;
|
||||
this.use_chunked_encoding_by_default = true;
|
||||
}
|
||||
inherits(ServerResponse, OutgoingMessage);
|
||||
node.inherits(ServerResponse, OutgoingMessage);
|
||||
|
||||
ServerResponse.prototype.sendHeader = function (statusCode, headers) {
|
||||
var reason = node.http.STATUS_CODES[statusCode] || "unknown";
|
||||
|
@ -279,7 +256,7 @@ function ClientRequest (method, uri, header_lines) {
|
|||
|
||||
this.sendHeaderLines(method + " " + uri + " HTTP/1.1\r\n", header_lines);
|
||||
}
|
||||
inherits(ClientRequest, OutgoingMessage);
|
||||
node.inherits(ClientRequest, OutgoingMessage);
|
||||
|
||||
ClientRequest.prototype.finish = function (responseListener) {
|
||||
this.addListener("response", responseListener);
|
||||
|
|
|
@ -261,6 +261,7 @@ Load (int argc, char *argv[])
|
|||
HTTPServer::Initialize(http);
|
||||
HTTPConnection::Initialize(http);
|
||||
|
||||
ExecuteNativeJS("util.js", native_util);
|
||||
ExecuteNativeJS("events.js", native_events);
|
||||
ExecuteNativeJS("http.js", native_http);
|
||||
ExecuteNativeJS("file.js", native_file);
|
||||
|
|
46
src/node.js
46
src/node.js
|
@ -40,52 +40,6 @@ function clearTimeout (timer) {
|
|||
|
||||
clearInterval = clearTimeout;
|
||||
|
||||
// This is useful for dealing with raw encodings.
|
||||
Array.prototype.encodeUtf8 = function () {
|
||||
return String.fromCharCode.apply(String, this);
|
||||
};
|
||||
|
||||
node.path = new function () {
|
||||
this.join = function () {
|
||||
var joined = "";
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
var part = arguments[i].toString();
|
||||
|
||||
/* Some logic to shorten paths */
|
||||
if (part === ".") continue;
|
||||
while (/^\.\//.exec(part)) part = part.replace(/^\.\//, "");
|
||||
|
||||
if (i === 0) {
|
||||
part = part.replace(/\/*$/, "/");
|
||||
} else if (i === arguments.length - 1) {
|
||||
part = part.replace(/^\/*/, "");
|
||||
} else {
|
||||
part = part.replace(/^\/*/, "").replace(/\/*$/, "/");
|
||||
}
|
||||
joined += part;
|
||||
}
|
||||
return joined;
|
||||
};
|
||||
|
||||
this.dirname = function (path) {
|
||||
if (path.charAt(0) !== "/") path = "./" + path;
|
||||
var parts = path.split("/");
|
||||
return parts.slice(0, parts.length-1).join("/");
|
||||
};
|
||||
|
||||
this.filename = function (path) {
|
||||
if (path.charAt(0) !== "/") path = "./" + path;
|
||||
var parts = path.split("/");
|
||||
return parts[parts.length-1];
|
||||
};
|
||||
};
|
||||
|
||||
node.cat = function(location, encoding, callback) {
|
||||
var url_re = new RegExp("^http:\/\/");
|
||||
var f = url_re.exec(location) ? node.http.cat : node.fs.cat;
|
||||
return f(location, encoding, callback);
|
||||
};
|
||||
|
||||
// Module
|
||||
|
||||
node.Module = function (o) {
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
/**
|
||||
* Inherit the prototype methods from one constructor into another.
|
||||
*
|
||||
* The Function.prototype.inherits from lang.js rewritten as a standalone
|
||||
* function (not on Function.prototype). NOTE: If this file is to be loaded
|
||||
* during bootstrapping this function needs to be revritten using some native
|
||||
* functions as prototype setup using normal JavaScript does not work as
|
||||
* expected during bootstrapping (see mirror.js in r114903).
|
||||
*
|
||||
* @param {function} ctor Constructor function which needs to inherit the
|
||||
* prototype
|
||||
* @param {function} superCtor Constructor function to inherit prototype from
|
||||
*/
|
||||
node.inherits = function (ctor, superCtor) {
|
||||
var tempCtor = function(){};
|
||||
tempCtor.prototype = superCtor.prototype;
|
||||
ctor.super_ = superCtor.prototype;
|
||||
ctor.prototype = new tempCtor();
|
||||
ctor.prototype.constructor = ctor;
|
||||
};
|
||||
|
||||
// This is useful for dealing with raw encodings.
|
||||
Array.prototype.encodeUtf8 = function () {
|
||||
return String.fromCharCode.apply(String, this);
|
||||
};
|
||||
|
||||
node.cat = function(location, encoding, callback) {
|
||||
var url_re = new RegExp("^http:\/\/");
|
||||
var f = url_re.exec(location) ? node.http.cat : node.fs.cat;
|
||||
return f(location, encoding, callback);
|
||||
};
|
||||
|
||||
node.path = new function () {
|
||||
this.join = function () {
|
||||
var joined = "";
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
var part = arguments[i].toString();
|
||||
|
||||
/* Some logic to shorten paths */
|
||||
if (part === ".") continue;
|
||||
while (/^\.\//.exec(part)) part = part.replace(/^\.\//, "");
|
||||
|
||||
if (i === 0) {
|
||||
part = part.replace(/\/*$/, "/");
|
||||
} else if (i === arguments.length - 1) {
|
||||
part = part.replace(/^\/*/, "");
|
||||
} else {
|
||||
part = part.replace(/^\/*/, "").replace(/\/*$/, "/");
|
||||
}
|
||||
joined += part;
|
||||
}
|
||||
return joined;
|
||||
};
|
||||
|
||||
this.dirname = function (path) {
|
||||
if (path.charAt(0) !== "/") path = "./" + path;
|
||||
var parts = path.split("/");
|
||||
return parts.slice(0, parts.length-1).join("/");
|
||||
};
|
||||
|
||||
this.filename = function (path) {
|
||||
if (path.charAt(0) !== "/") path = "./" + path;
|
||||
var parts = path.split("/");
|
||||
return parts[parts.length-1];
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue