From 31ed87b0a964a9989c56a6e08906b96c492186ee Mon Sep 17 00:00:00 2001 From: Trevor Burnham Date: Sun, 1 May 2011 14:03:08 -0400 Subject: [PATCH 01/13] Documenting `require.main`, fixing #997 --- doc/api/modules.markdown | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/doc/api/modules.markdown b/doc/api/modules.markdown index 07ff31fe54d..89490ebafc9 100644 --- a/doc/api/modules.markdown +++ b/doc/api/modules.markdown @@ -305,6 +305,21 @@ the same process. As the application stack grows, we tend to assemble functionality, and it is a problem with those parts interact in ways that are difficult to predict. +### Accessing the main module + +When a file is run directly from Node, `require.main` is set to its +`module`. That means that you can determine whether a file has been run +directly by testing + + require.main === module + +For a file `foo.js`, this will be `true` if run via `node foo.js`, but +`false` if run by `require('./foo')`. + +Because `module` provides a `filename` property (normally equivalent to +`__filename`), the entry point of the current application can be obtained +by checking `require.main.filename`. + ## Addenda: Package Manager Tips The semantics of Node's `require()` function were designed to be general From 11beac70e22dd4fc2611cada19ca30031df8fa41 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 9 May 2011 10:49:20 -0700 Subject: [PATCH 02/13] Docs: server.pause() server.address() socket.address() --- doc/api/net.markdown | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/api/net.markdown b/doc/api/net.markdown index dded333a1f4..16baacb3dee 100644 --- a/doc/api/net.markdown +++ b/doc/api/net.markdown @@ -106,6 +106,12 @@ Start a server listening for connections on the given file descriptor. This file descriptor must have already had the `bind(2)` and `listen(2)` system calls invoked on it. +#### server.pause(msecs) + +Stop accepting connections for the given number of milliseconds (default is +one second). This could be useful for throttling new connections against +DoS attacks or other oversubscription. + #### server.close() Stops the server from accepting new connections. This function is @@ -115,8 +121,9 @@ event. #### server.address() -Returns the bound address of the server as seen by the operating system. -Useful to find which port was assigned when giving getting an OS-assigned address +Returns the bound address and port of the server as reported by the operating system. +Useful to find which port was assigned when giving getting an OS-assigned address. +Returns an object with two properties, e.g. `{"address":"127.0.0.1", "port":2121}` Example: @@ -298,6 +305,11 @@ data packet received and the first keepalive probe. Setting 0 for initialDelay will leave the value unchanged from the default (or previous) setting. +#### socket.address() + +Returns the bound address and port of the socket as reported by the operating system. +Returns an object with two properties, e.g. `{"address":"192.168.57.1", "port":62053}` + #### socket.remoteAddress The string representation of the remote IP address. For example, From 307f39ce9ed8f4d3de06f63cd1855157be2db82f Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 10 May 2011 13:42:49 -0700 Subject: [PATCH 03/13] Fix a url regression The change for #954 introduced a regression that would cause the url parser to fail on special chars found in the auth segment. Fix that, and also don't create invalid urls when format() is called on an object containing an auth member containing '@' characters or delimiters. --- lib/url.js | 45 ++++++++++++++++++++++++++++++++++------- test/simple/test-url.js | 26 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/lib/url.js b/lib/url.js index 99a0e67422b..21d3acb348e 100644 --- a/lib/url.js +++ b/lib/url.js @@ -42,6 +42,7 @@ var protocolPattern = /^([a-z0-9]+:)/i, // them. nonHostChars = ['%', '/', '?', ';', '#'] .concat(unwise).concat(autoEscape), + nonAuthChars = ['/', '@', '?', '#'].concat(delims), hostnameMaxLen = 255, hostnamePartPattern = /^[a-zA-Z0-9][a-z0-9A-Z-]{0,62}$/, hostnamePartStart = /^([a-zA-Z0-9][a-z0-9A-Z-]{0,62})(.*)$/, @@ -123,12 +124,37 @@ function urlParse(url, parseQueryString, slashesDenoteHost) { // there's a hostname. // the first instance of /, ?, ;, or # ends the host. // don't enforce full RFC correctness, just be unstupid about it. + + // If there is an @ in the hostname, then non-host chars *are* allowed + // to the left of the first @ sign, unless some non-auth character + // comes *before* the @-sign. + // URLs are obnoxious. + var atSign = rest.indexOf('@'); + if (atSign !== -1) { + // there *may be* an auth + var hasAuth = true; + for (var i = 0, l = nonAuthChars.length; i < l; i++) { + var index = rest.indexOf(nonAuthChars[i]); + if (index !== -1 && index < atSign) { + // not a valid auth. Something like http://foo.com/bar@baz/ + hasAuth = false; + break; + } + } + if (hasAuth) { + // pluck off the auth portion. + out.auth = rest.substr(0, atSign); + rest = rest.substr(atSign + 1); + } + } + var firstNonHost = -1; for (var i = 0, l = nonHostChars.length; i < l; i++) { var index = rest.indexOf(nonHostChars[i]); if (index !== -1 && (firstNonHost < 0 || index < firstNonHost)) firstNonHost = index; } + if (firstNonHost !== -1) { out.host = rest.substr(0, firstNonHost); rest = rest.substr(firstNonHost); @@ -137,8 +163,9 @@ function urlParse(url, parseQueryString, slashesDenoteHost) { rest = ''; } - // pull out the auth and port. + // pull out port. var p = parseHost(out.host); + if (out.auth) out.host = out.auth + '@' + out.host; var keys = Object.keys(p); for (var i = 0, l = keys.length; i < l; i++) { var key = keys[i]; @@ -250,10 +277,19 @@ function urlFormat(obj) { // to clean up potentially wonky urls. if (typeof(obj) === 'string') obj = urlParse(obj); + var auth = obj.auth; + if (auth) { + auth = auth.split('@').join('%40'); + for (var i = 0, l = nonAuthChars.length; i < l; i++) { + var nAC = nonAuthChars[i]; + auth = auth.split(nAC).join(encodeURIComponent(nAC)); + } + } + var protocol = obj.protocol || '', host = (obj.host !== undefined) ? obj.host : obj.hostname !== undefined ? ( - (obj.auth ? obj.auth + '@' : '') + + (auth ? auth + '@' : '') + obj.hostname + (obj.port ? ':' + obj.port : '') ) : @@ -476,11 +512,6 @@ function urlResolveObject(source, relative) { function parseHost(host) { var out = {}; - var at = host.indexOf('@'); - if (at !== -1) { - out.auth = host.substr(0, at); - host = host.substr(at + 1); // drop the @ - } var port = portPattern.exec(host); if (port) { port = port[0]; diff --git a/test/simple/test-url.js b/test/simple/test-url.js index e52dacd8bd0..954454ffa8a 100644 --- a/test/simple/test-url.js +++ b/test/simple/test-url.js @@ -236,6 +236,18 @@ var parseTests = { 'host': 'isaacschlueter@jabber.org', 'auth': 'isaacschlueter', 'hostname': 'jabber.org' + }, + 'http://atpass:foo%40bar@127.0.0.1:8080/path?search=foo#bar' : { + 'href' : 'http://atpass:foo%40bar@127.0.0.1:8080/path?search=foo#bar', + 'protocol' : 'http:', + 'host' : 'atpass:foo%40bar@127.0.0.1:8080', + 'auth' : 'atpass:foo%40bar', + 'hostname' : '127.0.0.1', + 'port' : '8080', + 'pathname': '/path', + 'search' : '?search=foo', + 'query' : 'search=foo', + 'hash' : '#bar' } }; for (var u in parseTests) { @@ -367,6 +379,20 @@ var formatTests = { 'host': 'isaacschlueter@jabber.org', 'auth': 'isaacschlueter', 'hostname': 'jabber.org' + }, + 'http://atpass:foo%40bar@127.0.0.1/' : { + 'href': 'http://atpass:foo%40bar@127.0.0.1/', + 'auth': 'atpass:foo@bar', + 'hostname': '127.0.0.1', + 'protocol': 'http:', + 'pathname': '/' + }, + 'http://atslash%2F%40:%2F%40@foo/' : { + 'href': 'http://atslash%2F%40:%2F%40@foo/', + 'auth': 'atslash/@:/@', + 'hostname': 'foo', + 'protocol': 'http:', + 'pathname': '/' } }; for (var u in formatTests) { From 9a3dd754be6531b01c0eea2e60d501cba0332793 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 11 May 2011 13:40:42 -0700 Subject: [PATCH 04/13] Add trademark stuff to homepage --- doc/index.html | 4 ++++ doc/trademark-policy.pdf | Bin 0 -> 111297 bytes 2 files changed, 4 insertions(+) create mode 100644 doc/trademark-policy.pdf diff --git a/doc/index.html b/doc/index.html index 42dcc5c172f..ae2c76e4cc1 100644 --- a/doc/index.html +++ b/doc/index.html @@ -215,6 +215,10 @@ server.listen(1337, "127.0.0.1");
Copyright 2010 Joyent, Inc +
+ Node.js is a trademark of Joyent, Inc. + See the trademark policy + for more information.