-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
246 additions
and
248 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"description": "A simple yet powerful way to define binary protocols", | ||
"main": "index.js", | ||
"author": "roblabla <[email protected]>", | ||
"sideEffects": false, | ||
"scripts": { | ||
"prepare": "require-self", | ||
"lint": "standard", | ||
|
@@ -22,7 +23,7 @@ | |
"readable-stream": "^3.0.3" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=12" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/ProtoDef-io/node-protodef/issues" | ||
|
@@ -39,5 +40,10 @@ | |
"mocha": "^5.2.0", | ||
"require-self": "^0.1.0", | ||
"standard": "^12.0.1" | ||
} | ||
}, | ||
"files": [ | ||
"src/", | ||
"ProtoDef/schemas/", | ||
"*.js" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,69 @@ | ||
const { getField, getFieldInfo, tryDoc, PartialReadError } = require('../utils') | ||
|
||
module.exports = { | ||
'switch': [readSwitch, writeSwitch, sizeOfSwitch, require('../../ProtoDef/schemas/conditional.json')['switch']], | ||
'option': [readOption, writeOption, sizeOfOption, require('../../ProtoDef/schemas/conditional.json')['option']] | ||
} | ||
|
||
function readSwitch (buffer, offset, { compareTo, fields, compareToValue, 'default': defVal }, rootNode) { | ||
compareTo = compareToValue !== undefined ? compareToValue : getField(compareTo, rootNode) | ||
if (typeof fields[compareTo] === 'undefined' && typeof defVal === 'undefined') { throw new Error(compareTo + ' has no associated fieldInfo in switch') } | ||
|
||
const caseDefault = typeof fields[compareTo] === 'undefined' | ||
const resultingType = caseDefault ? defVal : fields[compareTo] | ||
const fieldInfo = getFieldInfo(resultingType) | ||
return tryDoc(() => this.read(buffer, offset, fieldInfo, rootNode), caseDefault ? 'default' : compareTo) | ||
if (fields[compareTo] === undefined) { | ||
compareTo = 'default' | ||
fields[compareTo] = defVal | ||
if (defVal === undefined) { | ||
throw new Error(`${compareTo} has no associated fieldInfo in switch`) | ||
} | ||
} | ||
const fieldInfo = getFieldInfo(fields[compareTo]) | ||
return tryDoc(() => this.read(buffer, offset, fieldInfo, rootNode), compareTo) | ||
} | ||
|
||
function writeSwitch (value, buffer, offset, { compareTo, fields, compareToValue, 'default': defVal }, rootNode) { | ||
compareTo = compareToValue !== undefined ? compareToValue : getField(compareTo, rootNode) | ||
if (typeof fields[compareTo] === 'undefined' && typeof defVal === 'undefined') { throw new Error(compareTo + ' has no associated fieldInfo in switch') } | ||
|
||
const caseDefault = typeof fields[compareTo] === 'undefined' | ||
const fieldInfo = getFieldInfo(caseDefault ? defVal : fields[compareTo]) | ||
return tryDoc(() => this.write(value, buffer, offset, fieldInfo, rootNode), caseDefault ? 'default' : compareTo) | ||
if (fields[compareTo] === undefined) { | ||
compareTo = 'default' | ||
fields[compareTo] = defVal | ||
if (defVal === undefined) { | ||
throw new Error(`${compareTo} has no associated fieldInfo in switch`) | ||
} | ||
} | ||
const fieldInfo = getFieldInfo(fields[compareTo]) | ||
return tryDoc(() => this.write(value, buffer, offset, fieldInfo, rootNode), compareTo) | ||
} | ||
|
||
function sizeOfSwitch (value, { compareTo, fields, compareToValue, 'default': defVal }, rootNode) { | ||
compareTo = compareToValue !== undefined ? compareToValue : getField(compareTo, rootNode) | ||
if (typeof fields[compareTo] === 'undefined' && typeof defVal === 'undefined') { throw new Error(compareTo + ' has no associated fieldInfo in switch') } | ||
|
||
const caseDefault = typeof fields[compareTo] === 'undefined' | ||
const fieldInfo = getFieldInfo(caseDefault ? defVal : fields[compareTo]) | ||
return tryDoc(() => this.sizeOf(value, fieldInfo, rootNode), caseDefault ? 'default' : compareTo) | ||
if (fields[compareTo] === undefined) { | ||
compareTo = 'default' | ||
fields[compareTo] = defVal | ||
if (defVal === undefined) { | ||
throw new Error(`${compareTo} has no associated fieldInfo in switch`) | ||
} | ||
} | ||
const fieldInfo = getFieldInfo(fields[compareTo]) | ||
return tryDoc(() => this.sizeOf(value, fieldInfo, rootNode), compareTo) | ||
} | ||
|
||
function readOption (buffer, offset, typeArgs, context) { | ||
if (buffer.length < offset + 1) { throw new PartialReadError() } | ||
const val = buffer.readUInt8(offset++) | ||
if (val !== 0) { | ||
const isPresent = buffer.readUInt8(offset++) !== 0 | ||
if (isPresent) { | ||
const retval = this.read(buffer, offset, typeArgs, context) | ||
retval.size++ | ||
return retval | ||
} else { return { size: 1 } } | ||
} | ||
return { size: 1 } | ||
} | ||
|
||
function writeOption (value, buffer, offset, typeArgs, context) { | ||
if (value != null) { | ||
buffer.writeUInt8(1, offset++) | ||
const isPresent = value != null | ||
buffer.writeUInt8(isPresent & 1, offset++) | ||
if (isPresent) { | ||
offset = this.write(value, buffer, offset, typeArgs, context) | ||
} else { buffer.writeUInt8(0, offset++) } | ||
} | ||
return offset | ||
} | ||
|
||
function sizeOfOption (value, typeArgs, context) { | ||
return value == null ? 1 : this.sizeOf(value, typeArgs, context) + 1 | ||
return (value != null && this.sizeOf(value, typeArgs, context)) + 1 | ||
} | ||
|
||
module.exports = { | ||
'switch': [readSwitch, writeSwitch, sizeOfSwitch, require('../../ProtoDef/schemas/conditional.json')['switch']], | ||
'option': [readOption, writeOption, sizeOfOption, require('../../ProtoDef/schemas/conditional.json')['option']] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.