MCPcopy Create free account
hub / github.com/SpookyJS/SpookyJS / Spooky

Function Spooky

lib/spooky.js:88–161  ·  view source on GitHub ↗
(options, callback)

Source from the content-addressed store, hash-verified

86}
87
88function Spooky(options, callback) {
89 EventEmitter.call(this);
90 this.options = options = _.defaults(_.clone(options || {}), defaults);
91
92 for (var k in defaults) {
93 if (defaults[k] && _.isObject(defaults[k]) && !_.isArray(defaults[k])) {
94 this.options[k] =
95 _.defaults(_.clone(options[k] || {}), defaults[k]);
96 }
97 }
98 options.transport = _.defaults(options.transport, defaults.transport);
99
100 this._q = async.queue(this._callWorker.bind(this), 1);
101
102 serializeMethods(options.casper);
103
104 if (options.child.transport === 'http') {
105 this._child = Spooky._instances[options.port] = this._spawnChild();
106
107 this._rpcClient = new tinyjsonrpc.StreamClient({
108 server: new RequestStream({
109 host: options.transport.http.host,
110 port: options.child.port
111 })
112 });
113 } else if (options.child.transport === 'stdio') {
114 this._child =
115 Spooky._instances['stdio' + Spooky._nextInstanceId++] =
116 this._spawnChild();
117
118 this._rpcClient = new tinyjsonrpc.StreamClient({
119 server: duplex(this._child.stdin,
120 new FilteredStream(this._child.stdout, isJsonRpcResponse))
121 });
122
123 // must terminate requests with a linefeed
124 this._rpcClient._send = function _send (request) {
125 if (this._server.full) {
126 this._server.buffer.push(request);
127 } else {
128 try {
129 request = JSON.stringify(request);
130 } catch (e) {
131 throw 'Could not serialize request to JSON';
132 }
133
134 this._server.full = !this._server.stream.write(request + '\n');
135 }
136 };
137 } else {
138 throw new Error('Unknown transport ' + options.child.transport);
139 }
140
141 // listen for JSON-RPC requests from the child
142 this._rpcServer = new tinyjsonrpc.StreamServer();
143 this._rpcServer.listen(duplex(
144 this._child.stdin,
145 new FilteredStream(this._child.stdout, isJsonRpcRequest)));

Callers

nothing calls this directly

Calls 1

serializeMethodsFunction · 0.85

Tested by

no test coverage detected