Skip to content

Commit

Permalink
Watch downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Sep 7, 2020
1 parent b7fea59 commit 4d315f6
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 12 deletions.
57 changes: 51 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,41 +173,59 @@ class HRPCServiceHypercore {
responseEncoding: RPC.NULL
})

this._onAppend = service.defineMethod({
this._watchDownloads = service.defineMethod({
id: 16,
requestEncoding: messages.WatchDownloadsRequest,
responseEncoding: RPC.NULL
})

this._unwatchDownloads = service.defineMethod({
id: 17,
requestEncoding: messages.UnwatchDownloadsRequest,
responseEncoding: RPC.NULL
})

this._onAppend = service.defineMethod({
id: 18,
requestEncoding: messages.AppendEvent,
responseEncoding: RPC.NULL
})

this._onClose = service.defineMethod({
id: 17,
id: 19,
requestEncoding: messages.CloseEvent,
responseEncoding: RPC.NULL
})

this._onPeerOpen = service.defineMethod({
id: 18,
id: 20,
requestEncoding: messages.PeerEvent,
responseEncoding: RPC.NULL
})

this._onPeerRemove = service.defineMethod({
id: 19,
id: 21,
requestEncoding: messages.PeerEvent,
responseEncoding: RPC.NULL
})

this._onExtension = service.defineMethod({
id: 20,
id: 22,
requestEncoding: messages.ExtensionMessage,
responseEncoding: RPC.NULL
})

this._onWait = service.defineMethod({
id: 21,
id: 23,
requestEncoding: messages.WaitEvent,
responseEncoding: RPC.NULL
})

this._onDownload = service.defineMethod({
id: 24,
requestEncoding: messages.DownloadEvent,
responseEncoding: RPC.NULL
})
}

onRequest (context, handlers = context) {
Expand All @@ -226,12 +244,15 @@ class HRPCServiceHypercore {
if (handlers.sendExtension) this._sendExtension.onrequest = handlers.sendExtension.bind(context)
if (handlers.acquireLock) this._acquireLock.onrequest = handlers.acquireLock.bind(context)
if (handlers.releaseLock) this._releaseLock.onrequest = handlers.releaseLock.bind(context)
if (handlers.watchDownloads) this._watchDownloads.onrequest = handlers.watchDownloads.bind(context)
if (handlers.unwatchDownloads) this._unwatchDownloads.onrequest = handlers.unwatchDownloads.bind(context)
if (handlers.onAppend) this._onAppend.onrequest = handlers.onAppend.bind(context)
if (handlers.onClose) this._onClose.onrequest = handlers.onClose.bind(context)
if (handlers.onPeerOpen) this._onPeerOpen.onrequest = handlers.onPeerOpen.bind(context)
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.onDownload) this._onDownload.onrequest = handlers.onDownload.bind(context)
}

get (data) {
Expand Down Expand Up @@ -354,6 +375,22 @@ class HRPCServiceHypercore {
return this._releaseLock.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)
}

onAppend (data) {
return this._onAppend.request(data)
}
Expand Down Expand Up @@ -401,6 +438,14 @@ class HRPCServiceHypercore {
onWaitNoReply (data) {
return this._onWait.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
Loading

0 comments on commit 4d315f6

Please sign in to comment.