Node API

Draft

This version:
http://tinyclouds.org/node

Abstract

This specification defines a javascript API for creating servers and clients based around an event loop. It is provided to document Node's interface and provide a specification for similar efforts.

Table of contents


1 Introduction

This specification defines an API for creating evented servers and clients in javascript. It can be considered documentation for the Node project and will be versioned with that software. However, in places the API is only a specification and does not reflect Node's behavior—there I will try to note the difference.

Unless otherwise noted, all functions can be considered non-blocking. Non-blocking means that program execution will continue without waiting for I/O (be that network or device).

1.1 The event loop

...

1.2 Execution context

...

2 HTTP Server

3 TCP Client

interface TCP.Client  {
  readonly attribute DOMString host;
  readonly attribute DOMString port;

  // ready state
  const unsigned short CONNECTING = 0;
  const unsigned short OPEN = 1;
  const unsigned short CLOSED = 2;
  readonly attribute long readyState;

  // networking                
           attribute Function onopen;
           attribute Function onrecv;
           attribute Function onclose;
  void send(in DOMString data);
  void disconnect();           
};

4 Timers

Timers and Intervals allow one to schedule an event at a later date. There are four globally exposed functions setTimeout, clearTimeout, setInterval, and clearInterval. These functions work similarly as in the browser except that the timerID and intervalID do not necessarily have type long but are rather opaque objects.

setTimeout(function, milliseconds)

This method calls the function once after a specified number of milliseconds elapses, until canceled by a call to clearTimeout. The methods returns a timerID which may be used in a subsequent call to clearTimeout to cancel the callback.

setInterval(function, milliseconds)

This method calls the function every time a specified number of milliseconds elapses, until canceled by a call to clearInterval. The methods returns a intervalID which may be used in a subsequent call to clearInterval to cancel the interval.

clearTimeout(timerID)

Cancels a timeout that was set with the setTimeout method.

clearInterval(intervalID)

Cancels an interval that was set with the setInterval method.