Comparing version 1.13.0 to 1.14.0
@@ -29,2 +29,6 @@ # API | ||
### ProtoDef.setVariable(name, value) | ||
Sets a primitive variable type for the specified `name`, which can be dynamically updated. Can be refrenced in switch statements with the "/" prefix. | ||
### ProtoDef.read(buffer, cursor, _fieldInfo, rootNodes) | ||
@@ -84,2 +88,6 @@ | ||
### ProtoDefCompiler.addVariable(name, value) | ||
Adds a primitive variable type for the specified `name`, which can be dynamically updated. Can be refrenced in switch statements with the "/" prefix. | ||
### ProtoDefCompiler.compileProtoDefSync(options = { printCode: false }) | ||
@@ -99,2 +107,7 @@ | ||
### CompiledProtodef.setVariable(name, value) | ||
Sets a primitive variable type for the specified `name`, which can be dynamically updated. Can be refrenced in switch statements with the "/" prefix. | ||
## utils | ||
@@ -101,0 +114,0 @@ |
# History | ||
## 1.14.0 | ||
* Allow JSON stringified buffers to be converted to Buffer when serializing (@extremeheat) | ||
* Add primitive variables (@extremeheat) | ||
## 1.13.0 | ||
@@ -4,0 +9,0 @@ |
{ | ||
"name": "protodef", | ||
"version": "1.13.0", | ||
"version": "1.14.0", | ||
"description": "A simple yet powerful way to define binary protocols", | ||
@@ -19,3 +19,3 @@ "main": "index.js", | ||
"lodash.reduce": "^4.6.0", | ||
"protodef-validator": "^1.2.2", | ||
"protodef-validator": "^1.3.0", | ||
"readable-stream": "^3.0.3" | ||
@@ -22,0 +22,0 @@ }, |
@@ -13,2 +13,6 @@ ## Conditional | ||
For `compareTo` variables, to go up one level when referencing a field, use "../". | ||
Switch statement field names starting with "/" will reference a root variable. Root variables are primitives that can be used for comparisons and changed dynamically. | ||
Example: | ||
@@ -15,0 +19,0 @@ |
@@ -21,3 +21,3 @@ { | ||
"patternProperties": { | ||
"^[-a-zA-Z0-9 _:]+$": { | ||
"^[-a-zA-Z0-9 _:/]+$": { | ||
"$ref": "dataType" | ||
@@ -24,0 +24,0 @@ } |
@@ -46,2 +46,11 @@ [ | ||
] | ||
}, | ||
{ | ||
"description": "container with a variable", | ||
"vars": [["colorTransparent", 3]], | ||
"type": [ "container", [ { "name": "color", "type": "i32" }, { "name": "opacity", "type": [ "switch", { "compareTo": "color", "fields": { "/colorTransparent": "void" }, "default": "u8" } ] } ] ], | ||
"values": [ | ||
{ "description": "active", "value": { "color": 3, "opacity": "undefined" }, "buffer": ["0x00","0x00","0x00","0x03"] }, | ||
{ "description": "inactive", "value": { "color": 2, "opacity": 4 }, "buffer": ["0x00","0x00","0x00","0x02", "0x04"] } | ||
] | ||
} | ||
@@ -48,0 +57,0 @@ ] |
@@ -43,2 +43,3 @@ { | ||
}, | ||
"vars": { "type": "array" }, | ||
"type": { | ||
@@ -74,2 +75,3 @@ "type": ["object","array"] | ||
}, | ||
"vars": { "type": "array" }, | ||
"value": { | ||
@@ -76,0 +78,0 @@ "type":[ "array", "boolean", "integer", "null", "number", "object", "string" ] |
@@ -35,2 +35,8 @@ const numeric = require('./datatypes/numeric') | ||
addVariable (key, val) { | ||
this.readCompiler.addContextType(key, val) | ||
this.writeCompiler.addContextType(key, val) | ||
this.sizeOfCompiler.addContextType(key, val) | ||
} | ||
compileProtoDefSync (options = { printCode: false }) { | ||
@@ -74,2 +80,8 @@ const sizeOfCode = this.sizeOfCompiler.generate() | ||
setVariable (key, val) { | ||
this.sizeOfCtx[key] = val | ||
this.readCtx[key] = val | ||
this.writeCtx[key] = val | ||
} | ||
sizeOf (value, type) { | ||
@@ -191,3 +203,3 @@ const sizeFn = this.sizeOfCtx[type] | ||
let i = this.scopeStack.length - 1 | ||
const reserved = ['value', 'enum', 'default', 'size'] | ||
const reserved = ['value', 'enum', 'default', 'size', 'offset'] | ||
while (path.length) { | ||
@@ -194,0 +206,0 @@ const scope = this.scopeStack[i] |
@@ -13,3 +13,4 @@ module.exports = { | ||
let val = key | ||
if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"` | ||
if (val.startsWith('/')) val = 'ctx.' + val.slice(1) // Root context variable | ||
else if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"` | ||
code += compiler.indent(`case ${val}: return ` + compiler.callType(struct.fields[key])) + '\n' | ||
@@ -43,3 +44,4 @@ } | ||
let val = key | ||
if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"` | ||
if (val.startsWith('/')) val = 'ctx.' + val.slice(1) // Root context variable | ||
else if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"` | ||
code += compiler.indent(`case ${val}: return ` + compiler.callType('value', struct.fields[key])) + '\n' | ||
@@ -74,3 +76,4 @@ } | ||
let val = key | ||
if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"` | ||
if (val.startsWith('/')) val = 'ctx.' + val.slice(1) // Root context variable | ||
else if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"` | ||
code += compiler.indent(`case ${val}: return ` + compiler.callType('value', struct.fields[key])) + '\n' | ||
@@ -77,0 +80,0 @@ } |
@@ -81,3 +81,3 @@ module.exports = { | ||
buffer: ['parametrizable', (compiler, buffer) => { | ||
let code = '' | ||
let code = 'if (!(value instanceof Buffer)) value = Buffer.from(value)\n' | ||
if (buffer.countType) { | ||
@@ -139,3 +139,3 @@ code += 'offset = ' + compiler.callType('value.length', buffer.countType) + '\n' | ||
buffer: ['parametrizable', (compiler, buffer) => { | ||
let code = 'let size = value.length\n' | ||
let code = 'let size = value instanceof Buffer ? value.length : Buffer.from(value).length\n' | ||
if (buffer.countType) { | ||
@@ -142,0 +142,0 @@ code += 'size += ' + compiler.callType('size', buffer.countType) + '\n' |
@@ -11,3 +11,8 @@ const { getField, getFieldInfo, tryDoc, PartialReadError } = require('../utils') | ||
if (typeof fields[compareTo] === 'undefined' && typeof defVal === 'undefined') { throw new Error(compareTo + ' has no associated fieldInfo in switch') } | ||
for (const field in fields) { | ||
if (field.startsWith('/')) { | ||
fields[this.types[field.slice(1)]] = fields[field] | ||
delete fields[field] | ||
} | ||
} | ||
const caseDefault = typeof fields[compareTo] === 'undefined' | ||
@@ -22,3 +27,8 @@ const resultingType = caseDefault ? defVal : fields[compareTo] | ||
if (typeof fields[compareTo] === 'undefined' && typeof defVal === 'undefined') { throw new Error(compareTo + ' has no associated fieldInfo in switch') } | ||
for (const field in fields) { | ||
if (field.startsWith('/')) { | ||
fields[this.types[field.slice(1)]] = fields[field] | ||
delete fields[field] | ||
} | ||
} | ||
const caseDefault = typeof fields[compareTo] === 'undefined' | ||
@@ -32,3 +42,8 @@ const fieldInfo = getFieldInfo(caseDefault ? defVal : fields[compareTo]) | ||
if (typeof fields[compareTo] === 'undefined' && typeof defVal === 'undefined') { throw new Error(compareTo + ' has no associated fieldInfo in switch') } | ||
for (const field in fields) { | ||
if (field.startsWith('/')) { | ||
fields[this.types[field.slice(1)]] = fields[field] | ||
delete fields[field] | ||
} | ||
} | ||
const caseDefault = typeof fields[compareTo] === 'undefined' | ||
@@ -35,0 +50,0 @@ const fieldInfo = getFieldInfo(caseDefault ? defVal : fields[compareTo]) |
@@ -157,2 +157,3 @@ const assert = require('assert') | ||
function writeBuffer (value, buffer, offset, typeArgs, rootNode) { | ||
if (!(value instanceof Buffer)) value = Buffer.from(value) | ||
offset = sendCount.call(this, value.length, buffer, offset, typeArgs, rootNode) | ||
@@ -164,2 +165,3 @@ value.copy(buffer, offset) | ||
function sizeOfBuffer (value, typeArgs, rootNode) { | ||
if (!(value instanceof Buffer)) value = Buffer.from(value) | ||
const size = calcCount.call(this, value.length, typeArgs, rootNode) | ||
@@ -166,0 +168,0 @@ return size + value.length |
@@ -106,2 +106,6 @@ const { getFieldInfo, tryCatch } = require('./utils') | ||
setVariable (key, val) { | ||
this.types[key] = val | ||
} | ||
read (buffer, cursor, _fieldInfo, rootNodes) { | ||
@@ -108,0 +112,0 @@ const { type, typeArgs } = getFieldInfo(_fieldInfo) |
@@ -31,7 +31,18 @@ const ProtoDef = require('protodef').ProtoDef | ||
function transformValues (type, values) { | ||
return values.map(value => ({ | ||
buffer: arrayToBuffer(value.buffer), | ||
value: type.indexOf('buffer') === 0 ? arrayToBuffer(value.value) : value.value, | ||
description: value.description | ||
})) | ||
return values.map(val => { | ||
let value = val.value | ||
if (type.indexOf('buffer') === 0) { | ||
value = arrayToBuffer(value) | ||
} else if (value) { | ||
// we cannot use undefined type in JSON so need to convert it here to pass strictEquals test | ||
for (const key in value) { | ||
if (value[key] === 'undefined') value[key] = undefined | ||
} | ||
} | ||
return { | ||
buffer: arrayToBuffer(val.buffer), | ||
value, | ||
description: val.description | ||
} | ||
}) | ||
} | ||
@@ -51,2 +62,3 @@ | ||
subtype.vars?.forEach(([k, v]) => { proto.setVariable(k, v); compiler.addVariable(k, v) }) | ||
subtype.values = transformValues(test.type, subtype.values) | ||
@@ -53,0 +65,0 @@ subtype.type = type |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
166772
61
4598
Updatedprotodef-validator@^1.3.0