KRPC.js
Simple KRPC protocol implementation of bencoded messages. See BitTorent DHT specifications for more details.
Install
$ npm install krpc
API
- KRPC (constructor)
- KRPC.Errors
- KRPC.ForeignError
- krpc.parse
- krpc.genTransId
- krpc.query
- krpc.respond
- krpc.error
- krpc.on('parseError')
- krpc.on('query')
- krpc.on('query_{type}')
- krpc.on('respond')
- krpc.on('{transId}')
- krpc.on('error')
KRPC (constructor)
krpc = opts
Create a new krpc
instance.
If opts
is specified, then the default options (shown below) will be overridden.
var KRPC = ;var krpc =transIdBytes: 2 // transaction id string lengthqueryTimeout: 2000 // in milliseconds, maximum time to wait for response;
KRPC.Errors
KRPC default error codes:
KRPCErrors =GENERIC: 201SERVER: 202PROTOCOL: 203METHOD_UNKNOWN: 204;
KRPC.ForeignError
err = caughtError;
An Error
class for foreign errors that caught while parsing messages.
socket;
krpc.parse
krpc;
Parse a massage. See events section for handling parsed messages.
Returns the parsed message. Throws an Error
on failure or ForeignError
when
some emit throws an error.
socket;
krpc.genTransId
transId = krpc;
Returns a new transaction id as Buffer
.
callback(err, res)
will be called when a parsed message with that transaction
id will parse within the query timeout. If no message received within timeout
a callback with an err
will be called.
If ip
or port
are not null
callback would be called only if the message
received form that ip and port.
You may set timeout
and change the default timeout queryTimeout
that given
on the constructor. Set timeout
to null
will disable the timeout for this
transaction.
var transId = krpc;
krpc.query
buffer = krpc;
Create an query message with type type
and the query data query
. Returns the
message as Buffer
.
transId
is the query transaction id.
var buffer = krpc;socket;
krpc.respond
buffer = krpc;
Create a respond message with the data res
. Returns the
message as Buffer
.
transId
is the query transaction id.
krpc;
krpc.error
buffer = krpc
Create an error message with the code errorCode
and message errorMsg
. Returns the
message as Buffer
.
transId
is the query transaction id.
krpc;
krpc.on('parseError')
krpc
Emits when a parse error should send back to the querying node.
krpc;
krpc.on('query')
krpc
Emits for each parsed query message.
krpc.on('query_{type}')
krpc
Emits for each parsed query message with type {type}
.
krpc;
krpc.on('respond')
krpc
Emits for each parsed respond message.
krpc.on('{transId}')
krpc
Emits for each parsed respond or error message with transaction id {transId}
has hex string.
krpc.on('error')
krpc
Emits for each parsed error message.
License
KRPC.js is freely distributable under the terms of the MIT license.
Copyright (c) Moshe Simantov