blind-peer-encodings
Version:
Blind-peer encodings
324 lines (286 loc) • 8.59 kB
JavaScript
// This file is autogenerated by the hyperschema compiler
// Schema Version: 1
/* eslint-disable camelcase */
/* eslint-disable quotes */
const VERSION = 1
const { c } = require('hyperschema/runtime')
// eslint-disable-next-line no-unused-vars
let version = VERSION
// @blind-peer/auth
const encoding0 = {
preencode (state, m) {
c.fixed32.preencode(state, m.swarming)
c.fixed32.preencode(state, m.encryption)
},
encode (state, m) {
c.fixed32.encode(state, m.swarming)
c.fixed32.encode(state, m.encryption)
},
decode (state) {
const r0 = c.fixed32.decode(state)
const r1 = c.fixed32.decode(state)
return {
swarming: r0,
encryption: r1
}
}
}
// @blind-peer/digest
const encoding1 = {
preencode (state, m) {
c.uint.preencode(state, m.referrers)
c.uint.preencode(state, m.cores)
c.uint.preencode(state, m.bytesAllocated)
state.end++ // max flag is 1 so always one byte
},
encode (state, m) {
const flags = m.flushed ? 1 : 0
c.uint.encode(state, m.referrers)
c.uint.encode(state, m.cores)
c.uint.encode(state, m.bytesAllocated)
c.uint.encode(state, flags)
},
decode (state) {
const r0 = c.uint.decode(state)
const r1 = c.uint.decode(state)
const r2 = c.uint.decode(state)
const flags = c.uint.decode(state)
return {
referrers: r0,
cores: r1,
bytesAllocated: r2,
flushed: (flags & 1) !== 0
}
}
}
// @blind-peer/mailbox
const encoding2 = {
preencode (state, m) {
c.uint.preencode(state, m.version)
c.fixed32.preencode(state, m.seed)
state.end++ // max flag is 2 so always one byte
if (m.referrer) c.fixed32.preencode(state, m.referrer)
if (m.blockEncryptionKey) c.fixed32.preencode(state, m.blockEncryptionKey)
},
encode (state, m) {
const flags =
(m.referrer ? 1 : 0) |
(m.blockEncryptionKey ? 2 : 0)
c.uint.encode(state, m.version)
c.fixed32.encode(state, m.seed)
c.uint.encode(state, flags)
if (m.referrer) c.fixed32.encode(state, m.referrer)
if (m.blockEncryptionKey) c.fixed32.encode(state, m.blockEncryptionKey)
},
decode (state) {
const r0 = c.uint.decode(state)
const r1 = c.fixed32.decode(state)
const flags = c.uint.decode(state)
return {
version: r0,
seed: r1,
referrer: (flags & 1) !== 0 ? c.fixed32.decode(state) : null,
blockEncryptionKey: (flags & 2) !== 0 ? c.fixed32.decode(state) : null
}
}
}
// @blind-peer/add-core-request
const encoding3 = {
preencode (state, m) {
state.end++ // max flag is 16 so always one byte
c.fixed32.preencode(state, m.key)
if (m.referrer) c.fixed32.preencode(state, m.referrer)
if (m.deprecatedAutobase) c.fixed32.preencode(state, m.deprecatedAutobase)
if (m.deprecatedAutobaseBlockKey) c.fixed32.preencode(state, m.deprecatedAutobaseBlockKey)
if (m.priority) c.uint.preencode(state, m.priority)
},
encode (state, m) {
const flags =
(m.referrer ? 1 : 0) |
(m.deprecatedAutobase ? 2 : 0) |
(m.deprecatedAutobaseBlockKey ? 4 : 0) |
(m.priority ? 8 : 0) |
(m.announce ? 16 : 0)
c.uint.encode(state, flags)
c.fixed32.encode(state, m.key)
if (m.referrer) c.fixed32.encode(state, m.referrer)
if (m.deprecatedAutobase) c.fixed32.encode(state, m.deprecatedAutobase)
if (m.deprecatedAutobaseBlockKey) c.fixed32.encode(state, m.deprecatedAutobaseBlockKey)
if (m.priority) c.uint.encode(state, m.priority)
},
decode (state) {
const flags = c.uint.decode(state)
return {
key: c.fixed32.decode(state),
referrer: (flags & 1) !== 0 ? c.fixed32.decode(state) : null,
deprecatedAutobase: (flags & 2) !== 0 ? c.fixed32.decode(state) : null,
deprecatedAutobaseBlockKey: (flags & 4) !== 0 ? c.fixed32.decode(state) : null,
priority: (flags & 8) !== 0 ? c.uint.decode(state) : 0,
announce: (flags & 16) !== 0
}
}
}
// @blind-peer/post-to-mailbox-request
const encoding4 = {
preencode (state, m) {
c.buffer.preencode(state, m.mailbox)
c.buffer.preencode(state, m.message)
},
encode (state, m) {
c.buffer.encode(state, m.mailbox)
c.buffer.encode(state, m.message)
},
decode (state) {
const r0 = c.buffer.decode(state)
const r1 = c.buffer.decode(state)
return {
mailbox: r0,
message: r1
}
}
}
// @blind-peer/wakeup-entry
const encoding5 = {
preencode (state, m) {
c.fixed32.preencode(state, m.key)
c.uint.preencode(state, m.length)
},
encode (state, m) {
c.fixed32.encode(state, m.key)
c.uint.encode(state, m.length)
},
decode (state) {
const r0 = c.fixed32.decode(state)
const r1 = c.uint.decode(state)
return {
key: r0,
length: r1
}
}
}
// @blind-peer/wakeup-reply.writers
const encoding6_2 = c.array(encoding5)
// @blind-peer/wakeup-reply
const encoding6 = {
preencode (state, m) {
c.uint.preencode(state, m.version)
c.uint.preencode(state, m.type)
encoding6_2.preencode(state, m.writers)
},
encode (state, m) {
c.uint.encode(state, m.version)
c.uint.encode(state, m.type)
encoding6_2.encode(state, m.writers)
},
decode (state) {
const r0 = c.uint.decode(state)
const r1 = c.uint.decode(state)
const r2 = encoding6_2.decode(state)
return {
version: r0,
type: r1,
writers: r2
}
}
}
// @blind-peer/core
const encoding7 = {
preencode (state, m) {
c.fixed32.preencode(state, m.key)
c.uint.preencode(state, m.length)
c.uint.preencode(state, m.bytesAllocated)
c.uint.preencode(state, m.updated)
c.uint.preencode(state, m.active)
c.uint.preencode(state, m.priority)
state.end++ // max flag is 8 so always one byte
if (m.referrer) c.fixed32.preencode(state, m.referrer)
if (m.blocksCleared) c.uint.preencode(state, m.blocksCleared)
if (m.bytesCleared) c.uint.preencode(state, m.bytesCleared)
},
encode (state, m) {
const flags =
(m.announce ? 1 : 0) |
(m.referrer ? 2 : 0) |
(m.blocksCleared ? 4 : 0) |
(m.bytesCleared ? 8 : 0)
c.fixed32.encode(state, m.key)
c.uint.encode(state, m.length)
c.uint.encode(state, m.bytesAllocated)
c.uint.encode(state, m.updated)
c.uint.encode(state, m.active)
c.uint.encode(state, m.priority)
c.uint.encode(state, flags)
if (m.referrer) c.fixed32.encode(state, m.referrer)
if (m.blocksCleared) c.uint.encode(state, m.blocksCleared)
if (m.bytesCleared) c.uint.encode(state, m.bytesCleared)
},
decode (state) {
const r0 = c.fixed32.decode(state)
const r1 = c.uint.decode(state)
const r2 = c.uint.decode(state)
const r3 = c.uint.decode(state)
const r4 = c.uint.decode(state)
const r5 = c.uint.decode(state)
const flags = c.uint.decode(state)
return {
key: r0,
length: r1,
bytesAllocated: r2,
updated: r3,
active: r4,
priority: r5,
announce: (flags & 1) !== 0,
referrer: (flags & 2) !== 0 ? c.fixed32.decode(state) : null,
blocksCleared: (flags & 4) !== 0 ? c.uint.decode(state) : 0,
bytesCleared: (flags & 8) !== 0 ? c.uint.decode(state) : 0
}
}
}
function setVersion (v) {
version = v
}
function encode (name, value, v = VERSION) {
version = v
return c.encode(getEncoding(name), value)
}
function decode (name, buffer, v = VERSION) {
version = v
return c.decode(getEncoding(name), buffer)
}
function getEnum (name) {
switch (name) {
default: throw new Error('Enum not found ' + name)
}
}
function getEncoding (name) {
switch (name) {
case '@blind-peer/auth': return encoding0
case '@blind-peer/digest': return encoding1
case '@blind-peer/mailbox': return encoding2
case '@blind-peer/add-core-request': return encoding3
case '@blind-peer/post-to-mailbox-request': return encoding4
case '@blind-peer/wakeup-entry': return encoding5
case '@blind-peer/wakeup-reply': return encoding6
case '@blind-peer/core': return encoding7
default: throw new Error('Encoder not found ' + name)
}
}
function getStruct (name, v = VERSION) {
const enc = getEncoding(name)
return {
preencode (state, m) {
version = v
enc.preencode(state, m)
},
encode (state, m) {
version = v
enc.encode(state, m)
},
decode (state) {
version = v
return enc.decode(state)
}
}
}
const resolveStruct = getStruct // compat
module.exports = { resolveStruct, getStruct, getEnum, getEncoding, encode, decode, setVersion, version }