Skip to content

Commit

Permalink
Watch downloads (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh authored Sep 16, 2020
2 parents b7fea59 + 4e3cb56 commit c38fb5a
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 0 deletions.
45 changes: 45 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
206 changes: 206 additions & 0 deletions messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -297,10 +318,13 @@ defineDownloadedRequest()
defineDownloadedResponse()
defineUndownloadRequest()
defineLockRequest()
defineWatchDownloadsRequest()
defineUnwatchDownloadsRequest()
defineAppendEvent()
definePeerEvent()
defineCloseEvent()
defineWaitEvent()
defineDownloadEvent()
defineRegisterExtensionRequest()
defineUnregisterExtensionRequest()
defineExtensionMessage()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions schema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ message LockRequest {
required uint64 id = 1;
}

message WatchDownloadsRequest {
required uint64 id = 1;
}

message UnwatchDownloadsRequest {
required uint64 id = 1;
}

// Hypercore Events

Expand All @@ -198,6 +205,11 @@ message WaitEvent {
required uint64 seq = 3;
}

message DownloadEvent {
required uint64 id = 1;
required uint64 seq = 2;
}

// Extension Methods

message RegisterExtensionRequest {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c38fb5a

Please sign in to comment.