From 527d6686f5a68ba592a84e1cb99ed9102887ddd1 Mon Sep 17 00:00:00 2001 From: wokim Date: Wed, 10 Dec 2014 03:06:53 +0900 Subject: [PATCH] Add definitions for rabbit.js --- rabbit.js/rabbit.js-tests.ts | 33 +++++++++++ rabbit.js/rabbit.js.d.ts | 108 +++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 rabbit.js/rabbit.js-tests.ts create mode 100644 rabbit.js/rabbit.js.d.ts diff --git a/rabbit.js/rabbit.js-tests.ts b/rabbit.js/rabbit.js-tests.ts new file mode 100644 index 0000000000..3e5a4d6d21 --- /dev/null +++ b/rabbit.js/rabbit.js-tests.ts @@ -0,0 +1,33 @@ +/// + +import rabbit = require('rabbit.js'); +var context = rabbit.createContext(); + +context.on('ready', function () { console.log('ready'); }); + +var pub = context.socket('PUB'); +var sub = context.socket('SUB'); +var push = context.socket('PUSH'); +var pull = context.socket('PULL'); +var req = context.socket('REQ'); +var rep = context.socket('REP'); +var task = context.socket('TASK'); +var worker = context.socket('WORKER'); + +pub.connect('chat'); +pub.write('hello', 'utf8'); +pub.close(); + +sub.connect('chat'); +sub.on('data', function (msg: string) { console.log(msg); }); +sub.close(); + +rep.setEncoding('utf8'); +rep.on('data', function (msg: string) { rep.write('msg', 'utf8'); }); +rep.connect('uppercase'); +req.connect('uppercase', function () { req.pipe(process.stdout); }); + +push.connect('items', function () { pull.pipe(push); }); +push.close(); +pull.connect('items', function () {}); +pull.close(); \ No newline at end of file diff --git a/rabbit.js/rabbit.js.d.ts b/rabbit.js/rabbit.js.d.ts new file mode 100644 index 0000000000..98d8172fd9 --- /dev/null +++ b/rabbit.js/rabbit.js.d.ts @@ -0,0 +1,108 @@ +// Type definitions for rabbit.js v0.4.2 +// Project: https://github.com/squaremo/rabbit.js +// Definitions by: Wonshik Kim +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "rabbit.js" { + import events = require('events'); + import stream = require('stream'); + + export function createContext(url?: string): Context; + + export class Context extends events.EventEmitter { + public socket(type: string, options?: SocketOptions):T; + public close(callback: Function): any; + } + + export interface SocketOptions { + prefetch?: any; + expiration?: any; + persistent?: any; + topic?: any; + task?: any; + } + + export interface Socket { + connect(destination: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + } + + export class PubSocket extends stream.Writable implements Socket { + constructor(channel: string, opts: SocketOptions); + connect(destination: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + + publish(topic: string, chunk: string, encoding?: string): any; + publish(topic: string, chunk: Buffer, encoding?: string): any; + } + + export class SubSocket extends stream.Readable implements Socket { + constructor(channel: string, opts: SocketOptions); + connect(source: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + } + + export class PushSocket extends stream.Writable implements Socket { + constructor(channel: string, opts: SocketOptions); + connect(destination: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + } + + export class PullSocket extends stream.Readable implements Socket { + constructor(channel: string, opts: SocketOptions); + connect(source: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + } + + export class WorkerSocket extends stream.Readable implements Socket { + constructor(channel: string, opts: SocketOptions); + connect(source: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + + ack(): any; + requeue(): any; + discard(): any; + } + + export interface RequestMessage { + properties: { correlationId: number }; + content: any; + } + + export class ReqSocket extends stream.Duplex implements Socket { + constructor(channel: string, opts: SocketOptions); + connect(destination: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + + handleReply(msg: RequestMessage): any; + } + + export class RepSocket extends stream.Duplex implements Socket { + constructor(channel: string, opts: SocketOptions); + connect(source: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + + requeue(): any; + discard(): any; + } + + export class TaskSocket extends stream.Writable implements Socket { + constructor(channel: string, opts: SocketOptions); + connect(destination: string, callback?: Function): any; + setsockopt(opt: string, value: string): any; + close(): any; + + post(task: string, chunk: string, encoding?: string): any; + post(task: string, chunk: Buffer, encoding?: string): any; + } +}