diff --git a/index.js b/index.js index 1add503..d24b2de 100644 --- a/index.js +++ b/index.js @@ -208,6 +208,24 @@ class HRPCServiceHypercore { requestEncoding: messages.WaitEvent, responseEncoding: RPC.NULL }) + + this._watchDownloads = service.defineMethod({ + id: 22, + requestEncoding: messages.WatchDownloadsRequest, + responseEncoding: RPC.NULL + }) + + this._unwatchDownloads = service.defineMethod({ + id: 23, + requestEncoding: messages.UnwatchDownloadsRequest, + responseEncoding: RPC.NULL + }) + + this._onDownload = service.defineMethod({ + id: 24, + requestEncoding: messages.DownloadEvent, + responseEncoding: RPC.NULL + }) } onRequest (context, handlers = context) { @@ -232,6 +250,9 @@ class HRPCServiceHypercore { if (handlers.onPeerRemove) this._onPeerRemove.onrequest = handlers.onPeerRemove.bind(context) if (handlers.onExtension) this._onExtension.onrequest = handlers.onExtension.bind(context) if (handlers.onWait) this._onWait.onrequest = handlers.onWait.bind(context) + if (handlers.watchDownloads) this._watchDownloads.onrequest = handlers.watchDownloads.bind(context) + if (handlers.unwatchDownloads) this._unwatchDownloads.onrequest = handlers.unwatchDownloads.bind(context) + if (handlers.onDownload) this._onDownload.onrequest = handlers.onDownload.bind(context) } get (data) { @@ -401,6 +422,30 @@ class HRPCServiceHypercore { onWaitNoReply (data) { return this._onWait.requestNoReply(data) } + + watchDownloads (data) { + return this._watchDownloads.request(data) + } + + watchDownloadsNoReply (data) { + return this._watchDownloads.requestNoReply(data) + } + + unwatchDownloads (data) { + return this._unwatchDownloads.request(data) + } + + unwatchDownloadsNoReply (data) { + return this._unwatchDownloads.requestNoReply(data) + } + + onDownload (data) { + return this._onDownload.request(data) + } + + onDownloadNoReply (data) { + return this._onDownload.requestNoReply(data) + } } class HRPCServiceNetwork { diff --git a/messages.js b/messages.js index b78f809..1e6476e 100644 --- a/messages.js +++ b/messages.js @@ -212,6 +212,20 @@ var LockRequest = exports.LockRequest = { decode: null } +var WatchDownloadsRequest = exports.WatchDownloadsRequest = { + buffer: true, + encodingLength: null, + encode: null, + decode: null +} + +var UnwatchDownloadsRequest = exports.UnwatchDownloadsRequest = { + buffer: true, + encodingLength: null, + encode: null, + decode: null +} + var AppendEvent = exports.AppendEvent = { buffer: true, encodingLength: null, @@ -240,6 +254,13 @@ var WaitEvent = exports.WaitEvent = { decode: null } +var DownloadEvent = exports.DownloadEvent = { + buffer: true, + encodingLength: null, + encode: null, + decode: null +} + var RegisterExtensionRequest = exports.RegisterExtensionRequest = { buffer: true, encodingLength: null, @@ -297,10 +318,13 @@ defineDownloadedRequest() defineDownloadedResponse() defineUndownloadRequest() defineLockRequest() +defineWatchDownloadsRequest() +defineUnwatchDownloadsRequest() defineAppendEvent() definePeerEvent() defineCloseEvent() defineWaitEvent() +defineDownloadEvent() defineRegisterExtensionRequest() defineUnregisterExtensionRequest() defineExtensionMessage() @@ -2668,6 +2692,118 @@ function defineLockRequest () { } } +function defineWatchDownloadsRequest () { + WatchDownloadsRequest.encodingLength = encodingLength + WatchDownloadsRequest.encode = encode + WatchDownloadsRequest.decode = decode + + function encodingLength (obj) { + var length = 0 + if (!defined(obj.id)) throw new Error("id is required") + var len = encodings.varint.encodingLength(obj.id) + length += 1 + len + return length + } + + function encode (obj, buf, offset) { + if (!offset) offset = 0 + if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj)) + var oldOffset = offset + if (!defined(obj.id)) throw new Error("id is required") + buf[offset++] = 8 + encodings.varint.encode(obj.id, buf, offset) + offset += encodings.varint.encode.bytes + encode.bytes = offset - oldOffset + return buf + } + + function decode (buf, offset, end) { + if (!offset) offset = 0 + if (!end) end = buf.length + if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid") + var oldOffset = offset + var obj = { + id: 0 + } + var found0 = false + while (true) { + if (end <= offset) { + if (!found0) throw new Error("Decoded message is not valid") + decode.bytes = offset - oldOffset + return obj + } + var prefix = varint.decode(buf, offset) + offset += varint.decode.bytes + var tag = prefix >> 3 + switch (tag) { + case 1: + obj.id = encodings.varint.decode(buf, offset) + offset += encodings.varint.decode.bytes + found0 = true + break + default: + offset = skip(prefix & 7, buf, offset) + } + } + } +} + +function defineUnwatchDownloadsRequest () { + UnwatchDownloadsRequest.encodingLength = encodingLength + UnwatchDownloadsRequest.encode = encode + UnwatchDownloadsRequest.decode = decode + + function encodingLength (obj) { + var length = 0 + if (!defined(obj.id)) throw new Error("id is required") + var len = encodings.varint.encodingLength(obj.id) + length += 1 + len + return length + } + + function encode (obj, buf, offset) { + if (!offset) offset = 0 + if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj)) + var oldOffset = offset + if (!defined(obj.id)) throw new Error("id is required") + buf[offset++] = 8 + encodings.varint.encode(obj.id, buf, offset) + offset += encodings.varint.encode.bytes + encode.bytes = offset - oldOffset + return buf + } + + function decode (buf, offset, end) { + if (!offset) offset = 0 + if (!end) end = buf.length + if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid") + var oldOffset = offset + var obj = { + id: 0 + } + var found0 = false + while (true) { + if (end <= offset) { + if (!found0) throw new Error("Decoded message is not valid") + decode.bytes = offset - oldOffset + return obj + } + var prefix = varint.decode(buf, offset) + offset += varint.decode.bytes + var tag = prefix >> 3 + switch (tag) { + case 1: + obj.id = encodings.varint.decode(buf, offset) + offset += encodings.varint.decode.bytes + found0 = true + break + default: + offset = skip(prefix & 7, buf, offset) + } + } + } +} + function defineAppendEvent () { AppendEvent.encodingLength = encodingLength AppendEvent.encode = encode @@ -2967,6 +3103,76 @@ function defineWaitEvent () { } } +function defineDownloadEvent () { + DownloadEvent.encodingLength = encodingLength + DownloadEvent.encode = encode + DownloadEvent.decode = decode + + function encodingLength (obj) { + var length = 0 + if (!defined(obj.id)) throw new Error("id is required") + var len = encodings.varint.encodingLength(obj.id) + length += 1 + len + if (!defined(obj.seq)) throw new Error("seq is required") + var len = encodings.varint.encodingLength(obj.seq) + length += 1 + len + return length + } + + function encode (obj, buf, offset) { + if (!offset) offset = 0 + if (!buf) buf = Buffer.allocUnsafe(encodingLength(obj)) + var oldOffset = offset + if (!defined(obj.id)) throw new Error("id is required") + buf[offset++] = 8 + encodings.varint.encode(obj.id, buf, offset) + offset += encodings.varint.encode.bytes + if (!defined(obj.seq)) throw new Error("seq is required") + buf[offset++] = 16 + encodings.varint.encode(obj.seq, buf, offset) + offset += encodings.varint.encode.bytes + encode.bytes = offset - oldOffset + return buf + } + + function decode (buf, offset, end) { + if (!offset) offset = 0 + if (!end) end = buf.length + if (!(end <= buf.length && offset <= buf.length)) throw new Error("Decoded message is not valid") + var oldOffset = offset + var obj = { + id: 0, + seq: 0 + } + var found0 = false + var found1 = false + while (true) { + if (end <= offset) { + if (!found0 || !found1) throw new Error("Decoded message is not valid") + decode.bytes = offset - oldOffset + return obj + } + var prefix = varint.decode(buf, offset) + offset += varint.decode.bytes + var tag = prefix >> 3 + switch (tag) { + case 1: + obj.id = encodings.varint.decode(buf, offset) + offset += encodings.varint.decode.bytes + found0 = true + break + case 2: + obj.seq = encodings.varint.decode(buf, offset) + offset += encodings.varint.decode.bytes + found1 = true + break + default: + offset = skip(prefix & 7, buf, offset) + } + } + } +} + function defineRegisterExtensionRequest () { RegisterExtensionRequest.encodingLength = encodingLength RegisterExtensionRequest.encode = encode diff --git a/schema.proto b/schema.proto index decdc6a..19edd80 100644 --- a/schema.proto +++ b/schema.proto @@ -174,6 +174,13 @@ message LockRequest { required uint64 id = 1; } +message WatchDownloadsRequest { + required uint64 id = 1; +} + +message UnwatchDownloadsRequest { + required uint64 id = 1; +} // Hypercore Events @@ -198,6 +205,11 @@ message WaitEvent { required uint64 seq = 3; } +message DownloadEvent { + required uint64 id = 1; + required uint64 seq = 2; +} + // Extension Methods message RegisterExtensionRequest { @@ -263,6 +275,11 @@ service Hypercore { rpc OnPeerRemove (PeerEvent) returns (hrpc.Void) { option (hrpc.method) = 19; } rpc OnExtension (ExtensionMessage) returns (hrpc.Void) { option (hrpc.method) = 20; } rpc OnWait (WaitEvent) returns (hrpc.Void) { option (hrpc.method) = 21; } + + // Downloads + rpc WatchDownloads (WatchDownloadsRequest) returns (hrpc.Void) { option (hrpc.method) = 22; } + rpc UnwatchDownloads (UnwatchDownloadsRequest) returns (hrpc.Void) { option (hrpc.method) = 23; } + rpc OnDownload (DownloadEvent) returns (hrpc.Void) { option (hrpc.method) = 24; } } service Network {