Skip to content

Commit e190c96

Browse files
miksagory
authored andcommitted
Splitting documentation
1 parent 1eb547f commit e190c96

33 files changed

Lines changed: 3369 additions & 3408 deletions

doc/api.markdown

Lines changed: 0 additions & 3408 deletions
This file was deleted.

doc/api/_index.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@include _toc.markdown

doc/api/_toc.markdown

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Table of Contents
2+
3+
* [Synopsis](synopsis.html)
4+
* [Globals](globals.html)
5+
* [Timers](timers.html)
6+
* [Modules](modules.html)
7+
* [C/C++ Addons](addons.html)
8+
* [Process](process.html)
9+
* [Constants](constants.html)
10+
* [Utilities](util.html)
11+
* [FreeList](freelist.html)
12+
* [Events](events.html)
13+
* [Buffers](buffers.html)
14+
* [Streams](streams.html)
15+
* [Crypto](crypto.html)
16+
* [Secure Streams](securepair.html)
17+
* [String Decoder](string_decoder.html)
18+
* [File System](fs.html)
19+
* [Path](path.html)
20+
* [Net](net.html)
21+
* [DNS](dns.html)
22+
* [Datagram](dgram.html)
23+
* [HTTP](http.html)
24+
* [URL](url.html)
25+
* [Query Strings](querystring.html)
26+
* [Readline](readline.html)
27+
* [REPL](repl.html)
28+
* [Script](script.html)
29+
* [Child Processes](child_processes.html)
30+
* [Assertion Testing](assert.html)
31+
* Appendixes
32+
* [Appendix 1: Recommended Third-party Modules](appendix_1.html)
33+
* [Appendix 2: Deprecated API's](appendix_2.html)

doc/api/addons.markdown

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
## Addons
2+
3+
Addons are dynamically linked shared objects. They can provide glue to C and
4+
C++ libraries. The API (at the moment) is rather complex, involving
5+
knowledge of several libraries:
6+
7+
- V8 JavaScript, a C++ library. Used for interfacing with JavaScript:
8+
creating objects, calling functions, etc. Documented mostly in the
9+
`v8.h` header file (`deps/v8/include/v8.h` in the Node source tree).
10+
11+
- libev, C event loop library. Anytime one needs to wait for a file
12+
descriptor to become readable, wait for a timer, or wait for a signal to
13+
received one will need to interface with libev. That is, if you perform
14+
any I/O, libev will need to be used. Node uses the `EV_DEFAULT` event
15+
loop. Documentation can be found http:/cvs.schmorp.de/libev/ev.html[here].
16+
17+
- libeio, C thread pool library. Used to execute blocking POSIX system
18+
calls asynchronously. Mostly wrappers already exist for such calls, in
19+
`src/file.cc` so you will probably not need to use it. If you do need it,
20+
look at the header file `deps/libeio/eio.h`.
21+
22+
- Internal Node libraries. Most importantly is the `node::ObjectWrap`
23+
class which you will likely want to derive from.
24+
25+
- Others. Look in `deps/` for what else is available.
26+
27+
Node statically compiles all its dependencies into the executable. When
28+
compiling your module, you don't need to worry about linking to any of these
29+
libraries.
30+
31+
To get started let's make a small Addon which does the following except in
32+
C++:
33+
34+
exports.hello = 'world';
35+
36+
To get started we create a file `hello.cc`:
37+
38+
#include <v8.h>
39+
40+
using namespace v8;
41+
42+
extern "C" void
43+
init (Handle<Object> target)
44+
{
45+
HandleScope scope;
46+
target->Set(String::New("hello"), String::New("World"));
47+
}
48+
49+
This source code needs to be built into `hello.node`, the binary Addon. To
50+
do this we create a file called `wscript` which is python code and looks
51+
like this:
52+
53+
srcdir = '.'
54+
blddir = 'build'
55+
VERSION = '0.0.1'
56+
57+
def set_options(opt):
58+
opt.tool_options('compiler_cxx')
59+
60+
def configure(conf):
61+
conf.check_tool('compiler_cxx')
62+
conf.check_tool('node_addon')
63+
64+
def build(bld):
65+
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
66+
obj.target = 'hello'
67+
obj.source = 'hello.cc'
68+
69+
Running `node-waf configure build` will create a file
70+
`build/default/hello.node` which is our Addon.
71+
72+
`node-waf` is just http://code.google.com/p/waf/[WAF], the python-based build system. `node-waf` is
73+
provided for the ease of users.
74+
75+
All Node addons must export a function called `init` with this signature:
76+
77+
extern 'C' void init (Handle<Object> target)
78+
79+
For the moment, that is all the documentation on addons. Please see
80+
<http://github.com/ry/node_postgres> for a real example.

doc/api/appendix_1.markdown

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## Appendix 1 - Third Party Modules
2+
3+
There are many third party modules for Node. At the time of writing, August
4+
2010, the master repository of modules is
5+
http://github.com/ry/node/wiki/modules[the wiki page].
6+
7+
This appendix is intended as a SMALL guide to new-comers to help them
8+
quickly find what are considered to be quality modules. It is not intended
9+
to be a complete list. There may be better more complete modules found
10+
elsewhere.
11+
12+
- Module Installer: [npm](http://github.com/isaacs/npm)
13+
14+
- HTTP Middleware: [Connect](http://github.com/senchalabs/connect)
15+
16+
- Web Framework: [Express](http://github.com/visionmedia/express)
17+
18+
- Web Sockets: [Socket.IO](http://github.com/LearnBoost/Socket.IO-node)
19+
20+
- HTML Parsing: [HTML5](http://github.com/aredridel/html5)
21+
22+
- [mDNS/Zeroconf/Bonjour](http://github.com/agnat/node_mdns)
23+
24+
- [RabbitMQ, AMQP](http://github.com/ry/node-amqp)
25+
26+
- [mysql](http://github.com/felixge/node-mysql)
27+
28+
- Serialization: [msgpack](http://github.com/pgriess/node-msgpack)
29+
30+
- Scraping: [Apricot](http://github.com/silentrob/Apricot)
31+
32+
- Debugger: [ndb](http://github.com/smtlaissezfaire/ndb) is a CLI debugger
33+
[inspector](http://github.com/dannycoates/node-inspector) is a web based
34+
tool.
35+
36+
- [pcap binding](http://github.com/mranney/node_pcap)
37+
38+
- [ncurses](http://github.com/mscdex/node-ncurses)
39+
40+
- Testing/TDD/BDD: [vows](http://vowsjs.org/),
41+
[expresso](http://github.com/visionmedia/expresso),
42+
[mjsunit.runner](http://github.com/tmpvar/mjsunit.runner)
43+
44+
Patches to this list are welcome.

doc/api/appendix_2.markdown

Whitespace-only changes.

doc/api/assert.markdown

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Assert
2+
3+
This module is used for writing unit tests for your applications, you can
4+
access it with `require('assert')`.
5+
6+
### assert.fail(actual, expected, message, operator)
7+
8+
Tests if `actual` is equal to `expected` using the operator provided.
9+
10+
### assert.ok(value, [message])
11+
12+
Tests if value is a `true` value, it is equivalent to `assert.equal(true, value, message);`
13+
14+
### assert.equal(actual, expected, [message])
15+
16+
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
17+
18+
### assert.notEqual(actual, expected, [message])
19+
20+
Tests shallow, coercive non-equality with the not equal comparison operator ( `!=` ).
21+
22+
### assert.deepEqual(actual, expected, [message])
23+
24+
Tests for deep equality.
25+
26+
### assert.notDeepEqual(actual, expected, [message])
27+
28+
Tests for any deep inequality.
29+
30+
### assert.strictEqual(actual, expected, [message])
31+
32+
Tests strict equality, as determined by the strict equality operator ( `===` )
33+
34+
### assert.notStrictEqual(actual, expected, [message])
35+
36+
Tests strict non-equality, as determined by the strict not equal operator ( `!==` )
37+
38+
### assert.throws(block, [error], [message])
39+
40+
Expects `block` to throw an error.
41+
42+
### assert.doesNotThrow(block, [error], [message])
43+
44+
Expects `block` not to throw an error.
45+
46+
### assert.ifError(value)
47+
48+
Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, `error` in callbacks.

doc/api/buffers.markdown

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
## Buffers
2+
3+
Pure Javascript is Unicode friendly but not nice to binary data. When
4+
dealing with TCP streams or the file system, it's necessary to handle octet
5+
streams. Node has several strategies for manipulating, creating, and
6+
consuming octet streams.
7+
8+
Raw data is stored in instances of the `Buffer` class. A `Buffer` is similar
9+
to an array of integers but corresponds to a raw memory allocation outside
10+
the V8 heap. A `Buffer` cannot be resized.
11+
12+
The `Buffer` object is global.
13+
14+
Converting between Buffers and JavaScript string objects requires an explicit encoding
15+
method. Here are the different string encodings;
16+
17+
* `'ascii'` - for 7 bit ASCII data only. This encoding method is very fast, and will
18+
strip the high bit if set.
19+
20+
* `'utf8'` - Unicode characters. Many web pages and other document formats use UTF-8.
21+
22+
* `'base64'` - Base64 string encoding.
23+
24+
* `'binary'` - A way of encoding raw binary data into strings by using only
25+
the first 8 bits of each character. This encoding method is depreciated and
26+
should be avoided in favor of `Buffer` objects where possible. This encoding
27+
will be removed in future versions of Node.
28+
29+
30+
### new Buffer(size)
31+
32+
Allocates a new buffer of `size` octets.
33+
34+
### new Buffer(array)
35+
36+
Allocates a new buffer using an `array` of octets.
37+
38+
### new Buffer(str, encoding='utf8')
39+
40+
Allocates a new buffer containing the given `str`.
41+
42+
### buffer.write(string, offset=0, encoding='utf8')
43+
44+
Writes `string` to the buffer at `offset` using the given encoding. Returns
45+
number of octets written. If `buffer` did not contain enough space to fit
46+
the entire string it will write a partial amount of the string. In the case
47+
of `'utf8'` encoding, the method will not write partial characters.
48+
49+
Example: write a utf8 string into a buffer, then print it
50+
51+
buf = new Buffer(256);
52+
len = buf.write('\u00bd + \u00bc = \u00be', 0);
53+
console.log(len + " bytes: " + buf.toString('utf8', 0, len));
54+
55+
// 12 bytes: ½ + ¼ = ¾
56+
57+
58+
### buffer.toString(encoding, start=0, end=buffer.length)
59+
60+
Decodes and returns a string from buffer data encoded with `encoding`
61+
beginning at `start` and ending at `end`.
62+
63+
See `buffer.write()` example, above.
64+
65+
66+
### buffer[index]
67+
68+
Get and set the octet at `index`. The values refer to individual bytes,
69+
so the legal range is between `0x00` and `0xFF` hex or `0` and `255`.
70+
71+
Example: copy an ASCII string into a buffer, one byte at a time:
72+
73+
str = "node.js";
74+
buf = new Buffer(str.length);
75+
76+
for (var i = 0; i < str.length ; i++) {
77+
buf[i] = str.charCodeAt(i);
78+
}
79+
80+
console.log(buf);
81+
82+
// node.js
83+
84+
85+
### Buffer.byteLength(string, encoding='utf8')
86+
87+
Gives the actual byte length of a string. This is not the same as
88+
`String.prototype.length` since that returns the number of *characters* in a
89+
string.
90+
91+
Example:
92+
93+
str = '\u00bd + \u00bc = \u00be';
94+
95+
console.log(str + ": " + str.length + " characters, " +
96+
Buffer.byteLength(str, 'utf8') + " bytes");
97+
98+
// ½ + ¼ = ¾: 9 characters, 12 bytes
99+
100+
101+
### buffer.length
102+
103+
The size of the buffer in bytes. Note that this is not necessarily the size
104+
of the contents. `length` refers to the amount of memory allocated for the
105+
buffer object. It does not change when the contents of the buffer are changed.
106+
107+
buf = new Buffer(1234);
108+
109+
console.log(buf.length);
110+
buf.write("some string", "ascii", 0);
111+
console.log(buf.length);
112+
113+
// 1234
114+
// 1234
115+
116+
### buffer.copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length)
117+
118+
Does a memcpy() between buffers.
119+
120+
Example: build two Buffers, then copy `buf1` from byte 16 through byte 19
121+
into `buf2`, starting at the 8th byte in `buf2`.
122+
123+
buf1 = new Buffer(26);
124+
buf2 = new Buffer(26);
125+
126+
for (var i = 0 ; i < 26 ; i++) {
127+
buf1[i] = i + 97; // 97 is ASCII a
128+
buf2[i] = 33; // ASCII !
129+
}
130+
131+
buf1.copy(buf2, 8, 16, 20);
132+
console.log(buf2.toString('ascii', 0, 25));
133+
134+
// !!!!!!!!qrst!!!!!!!!!!!!!
135+
136+
137+
### buffer.slice(start, end=buffer.length)
138+
139+
Returns a new buffer which references the
140+
same memory as the old, but offset and cropped by the `start` and `end`
141+
indexes.
142+
143+
**Modifying the new buffer slice will modify memory in the original buffer!**
144+
145+
Example: build a Buffer with the ASCII alphabet, take a slice, then modify one byte
146+
from the original Buffer.
147+
148+
var buf1 = new Buffer(26);
149+
150+
for (var i = 0 ; i < 26 ; i++) {
151+
buf1[i] = i + 97; // 97 is ASCII a
152+
}
153+
154+
var buf2 = buf1.slice(0, 3);
155+
console.log(buf2.toString('ascii', 0, buf2.length));
156+
buf1[0] = 33;
157+
console.log(buf2.toString('ascii', 0, buf2.length));
158+
159+
// abc
160+
// !bc

0 commit comments

Comments
 (0)