mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
1287 lines
35 KiB
TypeScript
1287 lines
35 KiB
TypeScript
/// <reference path="node.d.ts" />
|
||
import * as assert from "assert";
|
||
import * as fs from "fs";
|
||
import * as events from "events";
|
||
import * as zlib from "zlib";
|
||
import * as url from "url";
|
||
import * as util from "util";
|
||
import * as crypto from "crypto";
|
||
import * as tls from "tls";
|
||
import * as http from "http";
|
||
import * as https from "https";
|
||
import * as net from "net";
|
||
import * as tty from "tty";
|
||
import * as dgram from "dgram";
|
||
import * as querystring from "querystring";
|
||
import * as path from "path";
|
||
import * as readline from "readline";
|
||
import * as childProcess from "child_process";
|
||
import * as cluster from "cluster";
|
||
import * as os from "os";
|
||
import * as vm from "vm";
|
||
import * as string_decoder from "string_decoder";
|
||
import * as stream from "stream";
|
||
import * as timers from "timers";
|
||
|
||
// Specifically test buffer module regression.
|
||
import {Buffer as ImportedBuffer, SlowBuffer as ImportedSlowBuffer} from "buffer";
|
||
|
||
//////////////////////////////////////////////////////////
|
||
/// Assert Tests : https://nodejs.org/api/assert.html ///
|
||
//////////////////////////////////////////////////////////
|
||
|
||
namespace assert_tests{
|
||
{
|
||
assert(1 + 1 - 2 === 0, "The universe isn't how it should.");
|
||
|
||
assert.deepEqual({ x: { y: 3 } }, { x: { y: 3 } }, "DEEP WENT DERP");
|
||
|
||
// TODO: assert.deepStrictEqual
|
||
|
||
assert.doesNotThrow(() => {
|
||
const b = false;
|
||
if (b) { throw "a hammer at your face"; }
|
||
}, undefined, "What the...*crunch*");
|
||
|
||
assert.equal(3, "3", "uses == comparator");
|
||
|
||
// TODO: assert.fail
|
||
|
||
// TODO: assert.ifError
|
||
|
||
assert.notDeepStrictEqual({ x: { y: "3" } }, { x: { y: 3 } }, "uses === comparator");
|
||
|
||
// TODO: assert.notDeepStrictEqual
|
||
|
||
// TODO: assert.notEqual
|
||
|
||
assert.notStrictEqual(2, "2", "uses === comparator");
|
||
|
||
// TODO: assert.ok
|
||
|
||
// TODO: assert.strictEqual
|
||
|
||
assert.throws(() => { throw "a hammer at your face"; }, undefined, "DODGED IT");
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// Events tests : http://nodejs.org/api/events.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace events_tests {
|
||
let emitter: events.EventEmitter;
|
||
let event: string;
|
||
let listener: Function;
|
||
let any: any;
|
||
|
||
{
|
||
let result: events.EventEmitter;
|
||
|
||
result = emitter.addListener(event, listener);
|
||
result = emitter.on(event, listener);
|
||
result = emitter.once(event, listener);
|
||
result = emitter.prependListener(event, listener);
|
||
result = emitter.prependOnceListener(event, listener);
|
||
result = emitter.removeListener(event, listener);
|
||
result = emitter.removeAllListeners();
|
||
result = emitter.removeAllListeners(event);
|
||
result = emitter.setMaxListeners(42);
|
||
}
|
||
|
||
{
|
||
let result: number;
|
||
|
||
result = events.EventEmitter.defaultMaxListeners;
|
||
result = events.EventEmitter.listenerCount(emitter, event); // deprecated
|
||
|
||
result = emitter.getMaxListeners();
|
||
result = emitter.listenerCount(event);
|
||
}
|
||
|
||
{
|
||
let result: Function[];
|
||
|
||
result = emitter.listeners(event);
|
||
}
|
||
|
||
{
|
||
let result: boolean;
|
||
|
||
result = emitter.emit(event);
|
||
result = emitter.emit(event, any);
|
||
result = emitter.emit(event, any, any);
|
||
result = emitter.emit(event, any, any, any);
|
||
}
|
||
|
||
{
|
||
let result: string[];
|
||
|
||
result = emitter.eventNames();
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// File system tests : http://nodejs.org/api/fs.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace fs_tests {
|
||
{
|
||
fs.writeFile("thebible.txt",
|
||
"Do unto others as you would have them do unto you.",
|
||
assert.ifError);
|
||
|
||
fs.write(1234, "test");
|
||
|
||
fs.writeFile("Harry Potter",
|
||
"\"You be wizzing, Harry,\" jived Dumbledore.",
|
||
{
|
||
encoding: "ascii"
|
||
},
|
||
assert.ifError);
|
||
}
|
||
|
||
{
|
||
var content: string;
|
||
var buffer: Buffer;
|
||
|
||
content = fs.readFileSync('testfile', 'utf8');
|
||
content = fs.readFileSync('testfile', {encoding : 'utf8'});
|
||
buffer = fs.readFileSync('testfile');
|
||
buffer = fs.readFileSync('testfile', {flag : 'r'});
|
||
fs.readFile('testfile', 'utf8', (err, data) => content = data);
|
||
fs.readFile('testfile', {encoding : 'utf8'}, (err, data) => content = data);
|
||
fs.readFile('testfile', (err, data) => buffer = data);
|
||
fs.readFile('testfile', {flag : 'r'}, (err, data) => buffer = data);
|
||
}
|
||
|
||
{
|
||
var errno: string;
|
||
fs.readFile('testfile', (err, data) => {
|
||
if (err && err.errno) {
|
||
errno = err.errno;
|
||
}
|
||
});
|
||
}
|
||
|
||
{
|
||
fs.mkdtemp('/tmp/foo-', (err, folder) => {
|
||
console.log(folder);
|
||
// Prints: /tmp/foo-itXde2
|
||
});
|
||
}
|
||
|
||
{
|
||
var tempDir: string;
|
||
tempDir = fs.mkdtempSync('/tmp/foo-');
|
||
}
|
||
|
||
{
|
||
fs.watch('/tmp/foo-', (event, filename) => {
|
||
console.log(event, filename);
|
||
});
|
||
|
||
fs.watch('/tmp/foo-', 'utf8', (event, filename) => {
|
||
console.log(event, filename);
|
||
});
|
||
|
||
fs.watch('/tmp/foo-', {
|
||
recursive: true,
|
||
persistent: true,
|
||
encoding: 'utf8'
|
||
}, (event, filename) => {
|
||
console.log(event, filename);
|
||
});
|
||
}
|
||
|
||
{
|
||
fs.access('/path/to/folder', (err) => {});
|
||
|
||
fs.access(Buffer.from(''), (err) => {});
|
||
|
||
fs.access('/path/to/folder', fs.constants.F_OK | fs.constants.R_OK, (err) => {});
|
||
|
||
fs.access(Buffer.from(''), fs.constants.F_OK | fs.constants.R_OK, (err) => {});
|
||
|
||
fs.accessSync('/path/to/folder');
|
||
|
||
fs.accessSync(Buffer.from(''));
|
||
|
||
fs.accessSync('path/to/folder', fs.constants.W_OK | fs.constants.X_OK);
|
||
|
||
fs.accessSync(Buffer.from(''), fs.constants.W_OK | fs.constants.X_OK);
|
||
}
|
||
}
|
||
|
||
class Networker extends events.EventEmitter {
|
||
constructor() {
|
||
super();
|
||
|
||
this.emit("mingling");
|
||
}
|
||
}
|
||
|
||
///////////////////////////////////////////////////////
|
||
/// Buffer tests : https://nodejs.org/api/buffer.html
|
||
///////////////////////////////////////////////////////
|
||
|
||
function bufferTests() {
|
||
var utf8Buffer = new Buffer('test');
|
||
var base64Buffer = new Buffer('','base64');
|
||
var octets: Uint8Array = null;
|
||
var octetBuffer = new Buffer(octets);
|
||
var sharedBuffer = new Buffer(octets.buffer);
|
||
var copiedBuffer = new Buffer(utf8Buffer);
|
||
console.log(Buffer.isBuffer(octetBuffer));
|
||
console.log(Buffer.isEncoding('utf8'));
|
||
console.log(Buffer.byteLength('xyz123'));
|
||
console.log(Buffer.byteLength('xyz123', 'ascii'));
|
||
var result1 = Buffer.concat([utf8Buffer, base64Buffer]);
|
||
var result2 = Buffer.concat([utf8Buffer, base64Buffer], 9999999);
|
||
|
||
// Class Methods: Buffer.swap16(), Buffer.swa32(), Buffer.swap64()
|
||
{
|
||
const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
|
||
buf.swap16();
|
||
buf.swap32();
|
||
buf.swap64();
|
||
}
|
||
|
||
// Class Method: Buffer.from(array)
|
||
{
|
||
const buf: Buffer = Buffer.from([0x62,0x75,0x66,0x66,0x65,0x72]);
|
||
}
|
||
|
||
// Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]])
|
||
{
|
||
const arr: Uint16Array = new Uint16Array(2);
|
||
arr[0] = 5000;
|
||
arr[1] = 4000;
|
||
|
||
let buf: Buffer;
|
||
buf = Buffer.from(arr.buffer);
|
||
buf = Buffer.from(arr.buffer, 1);
|
||
buf = Buffer.from(arr.buffer, 0, 1);
|
||
}
|
||
|
||
// Class Method: Buffer.from(buffer)
|
||
{
|
||
const buf1: Buffer = Buffer.from('buffer');
|
||
const buf2: Buffer = Buffer.from(buf1);
|
||
}
|
||
|
||
// Class Method: Buffer.from(str[, encoding])
|
||
{
|
||
const buf1: Buffer = Buffer.from('this is a tést');
|
||
const buf2: Buffer = Buffer.from('7468697320697320612074c3a97374', 'hex');
|
||
}
|
||
|
||
// Test that TS 1.6 works with the 'as Buffer' annotation
|
||
// on isBuffer.
|
||
var a: Buffer | number;
|
||
a = new Buffer(10);
|
||
if (Buffer.isBuffer(a)) {
|
||
a.writeUInt8(3, 4);
|
||
}
|
||
|
||
// write* methods return offsets.
|
||
var b = new Buffer(16);
|
||
var result: number = b.writeUInt32LE(0, 0);
|
||
result = b.writeUInt16LE(0, 4);
|
||
result = b.writeUInt8(0, 6);
|
||
result = b.writeInt8(0, 7);
|
||
result = b.writeDoubleLE(0, 8);
|
||
|
||
// fill returns the input buffer.
|
||
b.fill('a').fill('b');
|
||
|
||
{
|
||
let buffer = new Buffer('123');
|
||
let index: number;
|
||
index = buffer.indexOf("23");
|
||
index = buffer.indexOf("23", 1);
|
||
index = buffer.indexOf("23", 1, "utf8");
|
||
index = buffer.indexOf(23);
|
||
index = buffer.indexOf(buffer);
|
||
}
|
||
|
||
{
|
||
let buffer = new Buffer('123');
|
||
let index: number;
|
||
index = buffer.lastIndexOf("23");
|
||
index = buffer.lastIndexOf("23", 1);
|
||
index = buffer.lastIndexOf("23", 1, "utf8");
|
||
index = buffer.lastIndexOf(23);
|
||
index = buffer.lastIndexOf(buffer);
|
||
}
|
||
|
||
{
|
||
let buffer = new Buffer('123');
|
||
let val: [number, number];
|
||
|
||
/* comment out for --target es5
|
||
for (let entry of buffer.entries()) {
|
||
val = entry;
|
||
}
|
||
*/
|
||
}
|
||
|
||
{
|
||
let buffer = new Buffer('123');
|
||
let includes: boolean;
|
||
includes = buffer.includes("23");
|
||
includes = buffer.includes("23", 1);
|
||
includes = buffer.includes("23", 1, "utf8");
|
||
includes = buffer.includes(23);
|
||
includes = buffer.includes(23, 1);
|
||
includes = buffer.includes(23, 1, "utf8");
|
||
includes = buffer.includes(buffer);
|
||
includes = buffer.includes(buffer, 1);
|
||
includes = buffer.includes(buffer, 1, "utf8");
|
||
}
|
||
|
||
{
|
||
let buffer = new Buffer('123');
|
||
let val: number;
|
||
|
||
/* comment out for --target es5
|
||
for (let key of buffer.keys()) {
|
||
val = key;
|
||
}
|
||
*/
|
||
}
|
||
|
||
{
|
||
let buffer = new Buffer('123');
|
||
let val: number;
|
||
|
||
/* comment out for --target es5
|
||
for (let value of buffer.values()) {
|
||
val = value;
|
||
}
|
||
*/
|
||
}
|
||
|
||
// Imported Buffer from buffer module works properly
|
||
{
|
||
let b = new ImportedBuffer('123');
|
||
b.writeUInt8(0, 6);
|
||
let sb = new ImportedSlowBuffer(43);
|
||
b.writeUInt8(0, 6);
|
||
}
|
||
|
||
// Buffer has Uint8Array's buffer field (an ArrayBuffer).
|
||
{
|
||
let buffer = new Buffer('123');
|
||
let octets = new Uint8Array(buffer.buffer);
|
||
}
|
||
}
|
||
|
||
|
||
////////////////////////////////////////////////////
|
||
/// Url tests : http://nodejs.org/api/url.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace url_tests {
|
||
{
|
||
url.format(url.parse('http://www.example.com/xyz'));
|
||
|
||
// https://google.com/search?q=you're%20a%20lizard%2C%20gary
|
||
url.format({
|
||
protocol: 'https',
|
||
host: "google.com",
|
||
pathname: 'search',
|
||
query: { q: "you're a lizard, gary" }
|
||
});
|
||
}
|
||
|
||
{
|
||
var helloUrl = url.parse('http://example.com/?hello=world', true)
|
||
assert.equal(helloUrl.query.hello, 'world');
|
||
}
|
||
}
|
||
|
||
/////////////////////////////////////////////////////
|
||
/// util tests : https://nodejs.org/api/util.html ///
|
||
/////////////////////////////////////////////////////
|
||
|
||
namespace util_tests {
|
||
{
|
||
// Old and new util.inspect APIs
|
||
util.inspect(["This is nice"], false, 5);
|
||
util.inspect(["This is nice"], { colors: true, depth: 5, customInspect: false });
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// Stream tests : http://nodejs.org/api/stream.html
|
||
////////////////////////////////////////////////////
|
||
|
||
// http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
|
||
function stream_readable_pipe_test() {
|
||
var r = fs.createReadStream('file.txt');
|
||
var z = zlib.createGzip();
|
||
var w = fs.createWriteStream('file.txt.gz');
|
||
r.pipe(z).pipe(w);
|
||
r.close();
|
||
}
|
||
|
||
// Simplified constructors
|
||
function simplified_stream_ctor_test() {
|
||
new stream.Readable({
|
||
read: function (size) {
|
||
size.toFixed();
|
||
}
|
||
});
|
||
|
||
new stream.Writable({
|
||
write: function (chunk, enc, cb) {
|
||
chunk.slice(1);
|
||
enc.charAt(0);
|
||
cb()
|
||
},
|
||
writev: function (chunks, cb) {
|
||
chunks[0].chunk.slice(0);
|
||
chunks[0].encoding.charAt(0);
|
||
cb();
|
||
}
|
||
});
|
||
|
||
new stream.Duplex({
|
||
read: function (size) {
|
||
size.toFixed();
|
||
},
|
||
write: function (chunk, enc, cb) {
|
||
chunk.slice(1);
|
||
enc.charAt(0);
|
||
cb()
|
||
},
|
||
writev: function (chunks, cb) {
|
||
chunks[0].chunk.slice(0);
|
||
chunks[0].encoding.charAt(0);
|
||
cb();
|
||
},
|
||
readableObjectMode: true,
|
||
writableObjectMode: true
|
||
});
|
||
|
||
new stream.Transform({
|
||
transform: function (chunk, enc, cb) {
|
||
chunk.slice(1);
|
||
enc.charAt(0);
|
||
cb();
|
||
},
|
||
flush: function (cb) {
|
||
cb()
|
||
},
|
||
read: function (size) {
|
||
size.toFixed();
|
||
},
|
||
write: function (chunk, enc, cb) {
|
||
chunk.slice(1);
|
||
enc.charAt(0);
|
||
cb()
|
||
},
|
||
writev: function (chunks, cb) {
|
||
chunks[0].chunk.slice(0);
|
||
chunks[0].encoding.charAt(0);
|
||
cb();
|
||
}
|
||
})
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// Crypto tests : http://nodejs.org/api/crypto.html
|
||
////////////////////////////////////////////////////
|
||
|
||
var hmacResult: string = crypto.createHmac('md5', 'hello').update('world').digest('hex');
|
||
|
||
{
|
||
let hmac: crypto.Hmac;
|
||
(hmac = crypto.createHmac('md5', 'hello')).end('world', 'utf8', () => {
|
||
let hash: Buffer|string = hmac.read();
|
||
});
|
||
}
|
||
|
||
function crypto_cipher_decipher_string_test() {
|
||
var key:Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
|
||
var clearText:string = "This is the clear text.";
|
||
var cipher:crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
|
||
var cipherText:string = cipher.update(clearText, "utf8", "hex");
|
||
cipherText += cipher.final("hex");
|
||
|
||
var decipher:crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
|
||
var clearText2:string = decipher.update(cipherText, "hex", "utf8");
|
||
clearText2 += decipher.final("utf8");
|
||
|
||
assert.equal(clearText2, clearText);
|
||
}
|
||
|
||
function crypto_cipher_decipher_buffer_test() {
|
||
var key:Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
|
||
var clearText:Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]);
|
||
var cipher:crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
|
||
var cipherBuffers:Buffer[] = [];
|
||
cipherBuffers.push(cipher.update(clearText));
|
||
cipherBuffers.push(cipher.final());
|
||
|
||
var cipherText:Buffer = Buffer.concat(cipherBuffers);
|
||
|
||
var decipher:crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
|
||
var decipherBuffers:Buffer[] = [];
|
||
decipherBuffers.push(decipher.update(cipherText));
|
||
decipherBuffers.push(decipher.final());
|
||
|
||
var clearText2:Buffer = Buffer.concat(decipherBuffers);
|
||
|
||
assert.deepEqual(clearText2, clearText);
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// TLS tests : http://nodejs.org/api/tls.html
|
||
////////////////////////////////////////////////////
|
||
|
||
var ctx: tls.SecureContext = tls.createSecureContext({
|
||
key: "NOT REALLY A KEY",
|
||
cert: "SOME CERTIFICATE",
|
||
});
|
||
var blah = ctx.context;
|
||
|
||
var connOpts: tls.ConnectionOptions = {
|
||
host: "127.0.0.1",
|
||
port: 55
|
||
};
|
||
var tlsSocket = tls.connect(connOpts);
|
||
|
||
|
||
////////////////////////////////////////////////////
|
||
|
||
// Make sure .listen() and .close() retuern a Server instance
|
||
http.createServer().listen(0).close().address();
|
||
net.createServer().listen(0).close().address();
|
||
|
||
var request = http.request('http://0.0.0.0');
|
||
request.once('error', function () {});
|
||
request.setNoDelay(true);
|
||
request.abort();
|
||
|
||
////////////////////////////////////////////////////
|
||
/// Http tests : http://nodejs.org/api/http.html
|
||
////////////////////////////////////////////////////
|
||
namespace http_tests {
|
||
// Status codes
|
||
var code = 100;
|
||
var codeMessage = http.STATUS_CODES['400'];
|
||
var codeMessage = http.STATUS_CODES[400];
|
||
|
||
var agent: http.Agent = new http.Agent({
|
||
keepAlive: true,
|
||
keepAliveMsecs: 10000,
|
||
maxSockets: Infinity,
|
||
maxFreeSockets: 256
|
||
});
|
||
|
||
var agent: http.Agent = http.globalAgent;
|
||
|
||
http.request({
|
||
agent: false
|
||
});
|
||
http.request({
|
||
agent: agent
|
||
});
|
||
http.request({
|
||
agent: undefined
|
||
});
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// Https tests : http://nodejs.org/api/https.html
|
||
////////////////////////////////////////////////////
|
||
namespace https_tests {
|
||
var agent: https.Agent = new https.Agent({
|
||
keepAlive: true,
|
||
keepAliveMsecs: 10000,
|
||
maxSockets: Infinity,
|
||
maxFreeSockets: 256,
|
||
maxCachedSessions: 100
|
||
});
|
||
|
||
var agent: https.Agent = https.globalAgent;
|
||
|
||
https.request({
|
||
agent: false
|
||
});
|
||
https.request({
|
||
agent: agent
|
||
});
|
||
https.request({
|
||
agent: undefined
|
||
});
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// TTY tests : http://nodejs.org/api/tty.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace tty_tests {
|
||
let rs: tty.ReadStream;
|
||
let ws: tty.WriteStream;
|
||
|
||
let rsIsRaw: boolean = rs.isRaw;
|
||
rs.setRawMode(true);
|
||
|
||
let wsColumns: number = ws.columns;
|
||
let wsRows: number = ws.rows;
|
||
|
||
let isTTY: boolean = tty.isatty(1);
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// Dgram tests : http://nodejs.org/api/dgram.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace dgram_tests {
|
||
var ds: dgram.Socket = dgram.createSocket("udp4", (msg: Buffer, rinfo: dgram.RemoteInfo): void => {
|
||
});
|
||
ds.bind();
|
||
ds.bind(41234);
|
||
var ai: dgram.AddressInfo = ds.address();
|
||
ds.send(new Buffer("hello"), 0, 5, 5000, "127.0.0.1", (error: Error, bytes: number): void => {
|
||
});
|
||
ds.send(new Buffer("hello"), 5000, "127.0.0.1");
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
///Querystring tests : https://nodejs.org/api/querystring.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace querystring_tests {
|
||
type SampleObject = {a: string; b: number;}
|
||
|
||
{
|
||
let obj: SampleObject;
|
||
let sep: string;
|
||
let eq: string;
|
||
let options: querystring.StringifyOptions;
|
||
let result: string;
|
||
|
||
result = querystring.stringify<SampleObject>(obj);
|
||
result = querystring.stringify<SampleObject>(obj, sep);
|
||
result = querystring.stringify<SampleObject>(obj, sep, eq);
|
||
result = querystring.stringify<SampleObject>(obj, sep, eq);
|
||
result = querystring.stringify<SampleObject>(obj, sep, eq, options);
|
||
}
|
||
|
||
{
|
||
let str: string;
|
||
let sep: string;
|
||
let eq: string;
|
||
let options: querystring.ParseOptions;
|
||
let result: SampleObject;
|
||
|
||
result = querystring.parse<SampleObject>(str);
|
||
result = querystring.parse<SampleObject>(str, sep);
|
||
result = querystring.parse<SampleObject>(str, sep, eq);
|
||
result = querystring.parse<SampleObject>(str, sep, eq, options);
|
||
}
|
||
|
||
{
|
||
let str: string;
|
||
let result: string;
|
||
|
||
result = querystring.escape(str);
|
||
result = querystring.unescape(str);
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// path tests : http://nodejs.org/api/path.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace path_tests {
|
||
|
||
path.normalize('/foo/bar//baz/asdf/quux/..');
|
||
|
||
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
|
||
// returns
|
||
//'/foo/bar/baz/asdf'
|
||
|
||
try {
|
||
path.join('foo', {}, 'bar');
|
||
}
|
||
catch(error) {
|
||
|
||
}
|
||
|
||
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile');
|
||
//Is similar to:
|
||
//
|
||
//cd foo/bar
|
||
//cd /tmp/file/
|
||
//cd ..
|
||
// cd a/../subfile
|
||
//pwd
|
||
|
||
path.resolve('/foo/bar', './baz')
|
||
// returns
|
||
// '/foo/bar/baz'
|
||
|
||
path.resolve('/foo/bar', '/tmp/file/')
|
||
// returns
|
||
// '/tmp/file'
|
||
|
||
path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
|
||
// if currently in /home/myself/node, it returns
|
||
// '/home/myself/node/wwwroot/static_files/gif/image.gif'
|
||
|
||
path.isAbsolute('/foo/bar') // true
|
||
path.isAbsolute('/baz/..') // true
|
||
path.isAbsolute('qux/') // false
|
||
path.isAbsolute('.') // false
|
||
|
||
path.isAbsolute('//server') // true
|
||
path.isAbsolute('C:/foo/..') // true
|
||
path.isAbsolute('bar\\baz') // false
|
||
path.isAbsolute('.') // false
|
||
|
||
path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
|
||
// returns
|
||
// '..\\..\\impl\\bbb'
|
||
|
||
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
|
||
// returns
|
||
// '../../impl/bbb'
|
||
|
||
path.dirname('/foo/bar/baz/asdf/quux')
|
||
// returns
|
||
// '/foo/bar/baz/asdf'
|
||
|
||
path.basename('/foo/bar/baz/asdf/quux.html')
|
||
// returns
|
||
// 'quux.html'
|
||
|
||
path.basename('/foo/bar/baz/asdf/quux.html', '.html')
|
||
// returns
|
||
// 'quux'
|
||
|
||
path.extname('index.html')
|
||
// returns
|
||
// '.html'
|
||
|
||
path.extname('index.coffee.md')
|
||
// returns
|
||
// '.md'
|
||
|
||
path.extname('index.')
|
||
// returns
|
||
// '.'
|
||
|
||
path.extname('index')
|
||
// returns
|
||
// ''
|
||
|
||
'foo/bar/baz'.split(path.sep)
|
||
// returns
|
||
// ['foo', 'bar', 'baz']
|
||
|
||
'foo\\bar\\baz'.split(path.sep)
|
||
// returns
|
||
// ['foo', 'bar', 'baz']
|
||
|
||
console.log(process.env.PATH)
|
||
// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
|
||
|
||
process.env.PATH.split(path.delimiter)
|
||
// returns
|
||
// ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']
|
||
|
||
console.log(process.env.PATH)
|
||
// 'C:\Windows\system32;C:\Windows;C:\Program Files\nodejs\'
|
||
|
||
process.env.PATH.split(path.delimiter)
|
||
// returns
|
||
// ['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\']
|
||
|
||
path.parse('/home/user/dir/file.txt')
|
||
// returns
|
||
// {
|
||
// root : "/",
|
||
// dir : "/home/user/dir",
|
||
// base : "file.txt",
|
||
// ext : ".txt",
|
||
// name : "file"
|
||
// }
|
||
|
||
path.parse('C:\\path\\dir\\index.html')
|
||
// returns
|
||
// {
|
||
// root : "C:\",
|
||
// dir : "C:\path\dir",
|
||
// base : "index.html",
|
||
// ext : ".html",
|
||
// name : "index"
|
||
// }
|
||
|
||
path.format({
|
||
root : "/",
|
||
dir : "/home/user/dir",
|
||
base : "file.txt",
|
||
ext : ".txt",
|
||
name : "file"
|
||
});
|
||
// returns
|
||
// '/home/user/dir/file.txt'
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// readline tests : https://nodejs.org/api/readline.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace readline_tests {
|
||
let rl: readline.ReadLine;
|
||
|
||
{
|
||
let options: readline.ReadLineOptions;
|
||
let input: NodeJS.ReadableStream;
|
||
let output: NodeJS.WritableStream;
|
||
let completer: readline.Completer;
|
||
let terminal: boolean;
|
||
|
||
let result: readline.ReadLine;
|
||
|
||
result = readline.createInterface(options);
|
||
result = readline.createInterface(input);
|
||
result = readline.createInterface(input, output);
|
||
result = readline.createInterface(input, output, completer);
|
||
result = readline.createInterface(input, output, completer, terminal);
|
||
}
|
||
|
||
{
|
||
let prompt: string;
|
||
|
||
rl.setPrompt(prompt);
|
||
}
|
||
|
||
{
|
||
let preserveCursor: boolean;
|
||
|
||
rl.prompt();
|
||
rl.prompt(preserveCursor);
|
||
}
|
||
|
||
{
|
||
let query: string;
|
||
let callback: (answer: string) => void;
|
||
|
||
rl.question(query, callback);
|
||
}
|
||
|
||
{
|
||
let result: readline.ReadLine;
|
||
|
||
result = rl.pause();
|
||
}
|
||
|
||
{
|
||
let result: readline.ReadLine;
|
||
|
||
result = rl.resume();
|
||
}
|
||
|
||
{
|
||
rl.close();
|
||
}
|
||
|
||
{
|
||
let data: string|Buffer;
|
||
let key: readline.Key;
|
||
|
||
rl.write(data);
|
||
rl.write(null, key);
|
||
}
|
||
|
||
{
|
||
let stream: NodeJS.WritableStream;
|
||
let x: number;
|
||
let y: number;
|
||
|
||
readline.cursorTo(stream, x, y);
|
||
}
|
||
|
||
{
|
||
let stream: NodeJS.WritableStream;
|
||
let dx: number|string;
|
||
let dy: number|string;
|
||
|
||
readline.moveCursor(stream, dx, dy);
|
||
}
|
||
|
||
{
|
||
let stream: NodeJS.WritableStream;
|
||
let dir: number;
|
||
|
||
readline.clearLine(stream, dir);
|
||
}
|
||
|
||
{
|
||
let stream: NodeJS.WritableStream;
|
||
|
||
readline.clearScreenDown(stream);
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// string_decoder tests : https://nodejs.org/api/string_decoder.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace string_decoder_tests {
|
||
const StringDecoder = string_decoder.StringDecoder;
|
||
const buffer = new Buffer('test');
|
||
const decoder1 = new StringDecoder();
|
||
const decoder2 = new StringDecoder('utf8');
|
||
const part1: string = decoder1.write(new Buffer('test'));
|
||
const end1: string = decoder1.end();
|
||
const part2: string = decoder2.write(new Buffer('test'));
|
||
const end2: string = decoder1.end(new Buffer('test'));
|
||
}
|
||
|
||
//////////////////////////////////////////////////////////////////////
|
||
/// Child Process tests: https://nodejs.org/api/child_process.html ///
|
||
//////////////////////////////////////////////////////////////////////
|
||
|
||
namespace child_process_tests {
|
||
{
|
||
childProcess.exec("echo test");
|
||
childProcess.spawnSync("echo test");
|
||
}
|
||
}
|
||
|
||
//////////////////////////////////////////////////////////////////////
|
||
/// cluster tests: https://nodejs.org/api/cluster.html ///
|
||
//////////////////////////////////////////////////////////////////////
|
||
|
||
namespace cluster_tests {
|
||
{
|
||
cluster.fork();
|
||
Object.keys(cluster.workers).forEach(key => {
|
||
const worker = cluster.workers[key];
|
||
if (worker.isDead()) {
|
||
console.log('worker %d is dead', worker.process.pid);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// os tests : https://nodejs.org/api/os.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace os_tests {
|
||
{
|
||
let result: string;
|
||
|
||
result = os.tmpdir();
|
||
result = os.homedir();
|
||
result = os.endianness();
|
||
result = os.hostname();
|
||
result = os.type();
|
||
result = os.platform();
|
||
result = os.arch();
|
||
result = os.release();
|
||
result = os.EOL;
|
||
}
|
||
|
||
{
|
||
let result: number;
|
||
|
||
result = os.uptime();
|
||
result = os.totalmem();
|
||
result = os.freemem();
|
||
}
|
||
|
||
{
|
||
let result: number[];
|
||
|
||
result = os.loadavg();
|
||
}
|
||
|
||
{
|
||
let result: os.CpuInfo[];
|
||
|
||
result = os.cpus();
|
||
}
|
||
|
||
{
|
||
let result: {[index: string]: os.NetworkInterfaceInfo[]};
|
||
|
||
result = os.networkInterfaces();
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////
|
||
/// vm tests : https://nodejs.org/api/vm.html
|
||
////////////////////////////////////////////////////
|
||
|
||
namespace vm_tests {
|
||
{
|
||
const sandbox = {
|
||
animal: 'cat',
|
||
count: 2
|
||
};
|
||
|
||
const context = vm.createContext(sandbox);
|
||
console.log(vm.isContext(context));
|
||
const script = new vm.Script('count += 1; name = "kitty"');
|
||
|
||
for (let i = 0; i < 10; ++i) {
|
||
script.runInContext(context);
|
||
}
|
||
|
||
console.log(util.inspect(sandbox));
|
||
|
||
vm.runInNewContext('count += 1; name = "kitty"', sandbox);
|
||
console.log(util.inspect(sandbox));
|
||
}
|
||
|
||
{
|
||
const sandboxes = [{}, {}, {}];
|
||
|
||
const script = new vm.Script('globalVar = "set"');
|
||
|
||
sandboxes.forEach((sandbox) => {
|
||
script.runInNewContext(sandbox);
|
||
script.runInThisContext();
|
||
});
|
||
|
||
console.log(util.inspect(sandboxes));
|
||
|
||
var localVar = 'initial value';
|
||
vm.runInThisContext('localVar = "vm";');
|
||
|
||
console.log(localVar);
|
||
}
|
||
|
||
{
|
||
const Debug = vm.runInDebugContext('Debug');
|
||
Debug.scripts().forEach(function(script: any) { console.log(script.name); });
|
||
}
|
||
}
|
||
|
||
/////////////////////////////////////////////////////
|
||
/// Timers tests : https://nodejs.org/api/timers.html
|
||
/////////////////////////////////////////////////////
|
||
|
||
namespace timers_tests {
|
||
{
|
||
let immediateId = timers.setImmediate(function(){ console.log("immediate"); });
|
||
timers.clearImmediate(immediateId);
|
||
}
|
||
{
|
||
let counter = 0;
|
||
let timeout = timers.setInterval(function(){ console.log("interval"); }, 20);
|
||
timeout.unref();
|
||
timeout.ref();
|
||
timers.clearInterval(timeout);
|
||
}
|
||
{
|
||
let counter = 0;
|
||
let timeout = timers.setTimeout(function(){ console.log("timeout"); }, 20);
|
||
timeout.unref();
|
||
timeout.ref();
|
||
timers.clearTimeout(timeout);
|
||
}
|
||
}
|
||
|
||
/////////////////////////////////////////////////////////
|
||
/// Errors Tests : https://nodejs.org/api/errors.html ///
|
||
/////////////////////////////////////////////////////////
|
||
|
||
namespace errors_tests {
|
||
{
|
||
Error.stackTraceLimit = Infinity;
|
||
}
|
||
{
|
||
const myObject = {};
|
||
Error.captureStackTrace(myObject);
|
||
}
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////
|
||
/// Process Tests : https://nodejs.org/api/process.html ///
|
||
///////////////////////////////////////////////////////////
|
||
|
||
import * as p from "process";
|
||
namespace process_tests{
|
||
{
|
||
var eventEmitter: events.EventEmitter;
|
||
eventEmitter = process; // Test that process implements EventEmitter...
|
||
|
||
var _p: NodeJS.Process = process;
|
||
_p = p;
|
||
assert(p === process);
|
||
}
|
||
}
|
||
|
||
///////////////////////////////////////////////////////////
|
||
/// Console Tests : https://nodejs.org/api/console.html ///
|
||
///////////////////////////////////////////////////////////
|
||
|
||
import * as c from "console";
|
||
namespace console_tests{
|
||
{
|
||
assert(c === console);
|
||
}
|
||
}
|
||
|
||
/*****************************************************************************
|
||
* *
|
||
* The following tests are the modules not mentioned in document but existed *
|
||
* *
|
||
*****************************************************************************/
|
||
|
||
///////////////////////////////////////////////////////////
|
||
/// Constants Tests ///
|
||
///////////////////////////////////////////////////////////
|
||
|
||
import * as constants from 'constants';
|
||
namespace constants_tests {
|
||
var str: string;
|
||
var num: number;
|
||
num = constants.SIGHUP
|
||
num = constants.SIGINT
|
||
num = constants.SIGQUIT
|
||
num = constants.SIGILL
|
||
num = constants.SIGTRAP
|
||
num = constants.SIGABRT
|
||
num = constants.SIGIOT
|
||
num = constants.SIGBUS
|
||
num = constants.SIGFPE
|
||
num = constants.SIGKILL
|
||
num = constants.SIGUSR1
|
||
num = constants.SIGSEGV
|
||
num = constants.SIGUSR2
|
||
num = constants.SIGPIPE
|
||
num = constants.SIGALRM
|
||
num = constants.SIGTERM
|
||
num = constants.SIGCHLD
|
||
num = constants.SIGSTKFLT
|
||
num = constants.SIGCONT
|
||
num = constants.SIGSTOP
|
||
num = constants.SIGTSTP
|
||
num = constants.SIGTTIN
|
||
num = constants.SIGTTOU
|
||
num = constants.SIGURG
|
||
num = constants.SIGXCPU
|
||
num = constants.SIGXFSZ
|
||
num = constants.SIGVTALRM
|
||
num = constants.SIGPROF
|
||
num = constants.SIGWINCH
|
||
num = constants.SIGIO
|
||
num = constants.SIGPOLL
|
||
num = constants.SIGPWR
|
||
num = constants.SIGSYS
|
||
num = constants.SIGUNUSED
|
||
num = constants.O_RDONLY
|
||
num = constants.O_WRONLY
|
||
num = constants.O_RDWR
|
||
num = constants.S_IFMT
|
||
num = constants.S_IFREG
|
||
num = constants.S_IFDIR
|
||
num = constants.S_IFCHR
|
||
num = constants.S_IFBLK
|
||
num = constants.S_IFIFO
|
||
num = constants.S_IFLNK
|
||
num = constants.S_IFSOCK
|
||
num = constants.O_CREAT
|
||
num = constants.O_EXCL
|
||
num = constants.O_NOCTTY
|
||
num = constants.O_TRUNC
|
||
num = constants.O_APPEND
|
||
num = constants.O_DIRECTORY
|
||
num = constants.O_NOATIME
|
||
num = constants.O_NOFOLLOW
|
||
num = constants.O_SYNC
|
||
num = constants.O_DIRECT
|
||
num = constants.O_NONBLOCK
|
||
num = constants.S_IRWXU
|
||
num = constants.S_IRUSR
|
||
num = constants.S_IWUSR
|
||
num = constants.S_IXUSR
|
||
num = constants.S_IRWXG
|
||
num = constants.S_IRGRP
|
||
num = constants.S_IWGRP
|
||
num = constants.S_IXGRP
|
||
num = constants.S_IRWXO
|
||
num = constants.S_IROTH
|
||
num = constants.S_IWOTH
|
||
num = constants.S_IXOTH
|
||
num = constants.F_OK
|
||
num = constants.R_OK
|
||
num = constants.W_OK
|
||
num = constants.X_OK
|
||
num = constants.SSL_OP_ALL
|
||
num = constants.SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
|
||
num = constants.SSL_OP_CIPHER_SERVER_PREFERENCE
|
||
num = constants.SSL_OP_CISCO_ANYCONNECT
|
||
num = constants.SSL_OP_COOKIE_EXCHANGE
|
||
num = constants.SSL_OP_CRYPTOPRO_TLSEXT_BUG
|
||
num = constants.SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
|
||
num = constants.SSL_OP_EPHEMERAL_RSA
|
||
num = constants.SSL_OP_LEGACY_SERVER_CONNECT
|
||
num = constants.SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER
|
||
num = constants.SSL_OP_MICROSOFT_SESS_ID_BUG
|
||
num = constants.SSL_OP_MSIE_SSLV2_RSA_PADDING
|
||
num = constants.SSL_OP_NETSCAPE_CA_DN_BUG
|
||
num = constants.SSL_OP_NETSCAPE_CHALLENGE_BUG
|
||
num = constants.SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG
|
||
num = constants.SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG
|
||
num = constants.SSL_OP_NO_COMPRESSION
|
||
num = constants.SSL_OP_NO_QUERY_MTU
|
||
num = constants.SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
|
||
num = constants.SSL_OP_NO_SSLv2
|
||
num = constants.SSL_OP_NO_SSLv3
|
||
num = constants.SSL_OP_NO_TICKET
|
||
num = constants.SSL_OP_NO_TLSv1
|
||
num = constants.SSL_OP_NO_TLSv1_1
|
||
num = constants.SSL_OP_NO_TLSv1_2
|
||
num = constants.SSL_OP_PKCS1_CHECK_1
|
||
num = constants.SSL_OP_PKCS1_CHECK_2
|
||
num = constants.SSL_OP_SINGLE_DH_USE
|
||
num = constants.SSL_OP_SINGLE_ECDH_USE
|
||
num = constants.SSL_OP_SSLEAY_080_CLIENT_DH_BUG
|
||
num = constants.SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG
|
||
num = constants.SSL_OP_TLS_BLOCK_PADDING_BUG
|
||
num = constants.SSL_OP_TLS_D5_BUG
|
||
num = constants.SSL_OP_TLS_ROLLBACK_BUG
|
||
num = constants.ENGINE_METHOD_RSA
|
||
num = constants.ENGINE_METHOD_DSA
|
||
num = constants.ENGINE_METHOD_DH
|
||
num = constants.ENGINE_METHOD_RAND
|
||
num = constants.ENGINE_METHOD_ECDH
|
||
num = constants.ENGINE_METHOD_ECDSA
|
||
num = constants.ENGINE_METHOD_CIPHERS
|
||
num = constants.ENGINE_METHOD_DIGESTS
|
||
num = constants.ENGINE_METHOD_STORE
|
||
num = constants.ENGINE_METHOD_PKEY_METHS
|
||
num = constants.ENGINE_METHOD_PKEY_ASN1_METHS
|
||
num = constants.ENGINE_METHOD_ALL
|
||
num = constants.ENGINE_METHOD_NONE
|
||
num = constants.DH_CHECK_P_NOT_SAFE_PRIME
|
||
num = constants.DH_CHECK_P_NOT_PRIME
|
||
num = constants.DH_UNABLE_TO_CHECK_GENERATOR
|
||
num = constants.DH_NOT_SUITABLE_GENERATOR
|
||
num = constants.NPN_ENABLED
|
||
num = constants.ALPN_ENABLED
|
||
num = constants.RSA_PKCS1_PADDING
|
||
num = constants.RSA_SSLV23_PADDING
|
||
num = constants.RSA_NO_PADDING
|
||
num = constants.RSA_PKCS1_OAEP_PADDING
|
||
num = constants.RSA_X931_PADDING
|
||
num = constants.RSA_PKCS1_PSS_PADDING
|
||
num = constants.POINT_CONVERSION_COMPRESSED
|
||
num = constants.POINT_CONVERSION_UNCOMPRESSED
|
||
num = constants.POINT_CONVERSION_HYBRID
|
||
str = constants.defaultCoreCipherList
|
||
str = constants.defaultCipherList
|
||
}
|