New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

kiwi-schema

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kiwi-schema - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

test/test-schema.js

128

kiwi.js

@@ -94,7 +94,7 @@ var kiwi = exports || kiwi || {}, exports;

var c = this.readByte();
if (a < 0xF0) {
if (a < 0xF0) {
codePoint = ((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F);
} else {
var d = this.readByte();
codePoint = ((a & 0x07) << 18) | ((b & 0x3F) << 12) | ((c & 0x3F) << 6) | (d & 0x3F);
codePoint = ((a & 0x07) << 18) | ((b & 0x3F) << 12) | ((c & 0x3F) << 6) | (d & 0x3F);
}

@@ -436,12 +436,14 @@ }

var definition = definitions[name];
if (state[name] === 1) {
error('Recursive nesting of ' + quote(name) + ' is not allowed', definition.line, definition.column);
}
if (state[name] !== 2 && definition) {
state[name] = 1;
var fields = definition.fields;
for (var i = 0; i < fields.length; i++) {
check(fields[i].type);
if (definition && definition.kind === 'STRUCT') {
if (state[name] === 1) {
error('Recursive nesting of ' + quote(name) + ' is not allowed', definition.line, definition.column);
}
state[name] = 2;
if (state[name] !== 2 && definition) {
state[name] = 1;
var fields = definition.fields;
for (var i = 0; i < fields.length; i++) {
check(fields[i].type);
}
state[name] = 2;
}
}

@@ -472,3 +474,3 @@ return true;

var lines = [];
var indent = '';
var indent = ' ';

@@ -480,2 +482,3 @@ lines.push('function(bb) {');

lines.push(' }');
lines.push('');

@@ -495,2 +498,3 @@ if (definition.kind === 'MESSAGE') {

lines.push(' return result;');
lines.push('');
indent = ' ';

@@ -562,2 +566,3 @@ }

lines.push(' break;');
lines.push('');
}

@@ -639,2 +644,3 @@ }

lines.push('');
lines.push(' var value = message[' + quote(field.name) + '];');

@@ -673,2 +679,3 @@ lines.push(' if (value != null) {'); // Comparing with null using "!=" also checks for undefined

lines.push('');
lines.push(' if (isTopLevel) return bb.toUint8Array();');

@@ -720,3 +727,5 @@ lines.push('}');

case 'MESSAGE': {
js.push('');
js.push(name + '[' + quote('decode' + definition.name) + '] = ' + compileDecodeJS(definition, definitions) + ';');
js.push('');
js.push(name + '[' + quote('encode' + definition.name) + '] = ' + compileEncodeJS(definition, definitions) + ';');

@@ -783,2 +792,18 @@ break;

function cppFieldName(field) {
return '_data_' + field.name;
}
function cppFlagIndex(i) {
return i >> 5;
}
function cppFlagMask(i) {
return 1 << (i % 32) >>> 0;
}
function cppIsFieldPointer(definitions, field) {
return !field.isArray && field.type in definitions && definitions[field.type].kind !== 'ENUM';
}
function compileSchemaCPP(schema) {

@@ -833,7 +858,6 @@ if (typeof schema === 'string') {

var name = definition.name;
var fields = definition.fields;
if (pass === 0) {
cpp.push('class ' + name + ';');
cpp.push('class ' + definition.name + ';');
newline = true;

@@ -843,3 +867,3 @@ }

else if (pass === 1) {
cpp.push('class ' + name + ' {');
cpp.push('class ' + definition.name + ' {');
cpp.push('public:');

@@ -849,14 +873,22 @@

var field = fields[j];
var name = cppFieldName(field);
var type = cppType(definitions, field, field.isArray);
var flagIndex = cppFlagIndex(j);
var flagMask = cppFlagMask(j);
cpp.push(' ' + type + ' *' + field.name + '() { return _' + field.name + '; }');
if (cppIsFieldPointer(definitions, field)) {
cpp.push(' ' + type + ' *' + field.name + '() { return ' + name + '; }');
cpp.push(' void set_' + field.name + '(' + type + ' *value) { ' + name + ' = value; }');
}
if (field.isArray) {
cpp.push(' ' + type + ' &add_' + field.name + '(kiwi::MemoryPool &pool, uint32_t count) { return *(_' +
field.name + ' = pool.array<' + cppType(definitions, field, false) + '>(count)); }');
else if (field.isArray) {
cpp.push(' ' + type + ' *' + field.name + '() { return _flags[' + flagIndex + '] & ' + flagMask + ' ? &' + name + ' : nullptr; }');
cpp.push(' ' + type + ' &set_' + field.name + '(kiwi::MemoryPool &pool, uint32_t count) { _flags[' + flagIndex +
'] |= ' + flagMask + '; return ' + name + ' = pool.array<' + cppType(definitions, field, false) + '>(count); }');
}
else {
cpp.push(' ' + type + ' &add_' + field.name + '(kiwi::MemoryPool &pool) { return *(_' +
field.name + ' ? _' + field.name + ' : _' + field.name + ' = pool.allocate<' + type + '>(1)); }');
cpp.push(' ' + type + ' *' + field.name + '() { return _flags[' + flagIndex + '] & ' + flagMask + ' ? &' + name + ' : nullptr; }');
cpp.push(' void set_' + field.name + '(const ' + type + ' &value) { _flags[' +
flagIndex + '] |= ' + flagMask + '; ' + name + ' = value; }');
}

@@ -871,6 +903,14 @@

cpp.push('private:');
cpp.push(' uint32_t _flags[' + (fields.length + 31 >> 5) + '] = {};');
for (var j = 0; j < fields.length; j++) {
var field = fields[j];
cpp.push(' ' + cppType(definitions, field, field.isArray) + ' *_' + field.name + ' = nullptr;');
var name = cppFieldName(field);
var type = cppType(definitions, field, field.isArray);
if (cppIsFieldPointer(definitions, field)) {
cpp.push(' ' + type + ' *' + name + ' = {};');
} else {
cpp.push(' ' + type + ' ' + name + ' = {};');
}
}

@@ -883,7 +923,10 @@

else {
cpp.push('bool ' + name + '::encode(kiwi::ByteBuffer &bb) {');
cpp.push('bool ' + definition.name + '::encode(kiwi::ByteBuffer &bb) {');
for (var j = 0; j < fields.length; j++) {
var field = fields[j];
var value = field.isArray ? 'it' : '*_' + field.name;
var name = cppFieldName(field);
var value = field.isArray ? 'it' : name;
var flagIndex = cppFlagIndex(j);
var flagMask = cppFlagMask(j);
var code;

@@ -918,3 +961,3 @@

case 'string': {
code = 'bb.writeString((' + value + ').c_str());';
code = 'bb.writeString(' + value + '.c_str());';
break;

@@ -935,3 +978,3 @@ }

else {
code = 'if (!(' + value + ').encode(bb)) return false;';
code = 'if (!' + value + (cppIsFieldPointer(definitions, field) ? '->' : '.') + 'encode(bb)) return false;';
}

@@ -943,5 +986,5 @@ }

if (field.isRequired) {
cpp.push(' if (!_' + field.name + ') return false;');
cpp.push(' if (' + field.name + '() == nullptr) return false;');
} else {
cpp.push(' if (_' + field.name + ') {');
cpp.push(' if (' + field.name + '() != nullptr) {');
indent = ' ';

@@ -955,4 +998,4 @@ }

if (field.isArray) {
cpp.push(indent + 'bb.writeVarUint(_' + field.name + '->size());');
cpp.push(indent + 'for (' + cppType(definitions, field, false) + ' &it : *_' + field.name + ') ' + code);
cpp.push(indent + 'bb.writeVarUint(' + name + '.size());');
cpp.push(indent + 'for (' + cppType(definitions, field, false) + ' &it : ' + name + ') ' + code);
} else {

@@ -975,3 +1018,3 @@ cpp.push(indent + code);

cpp.push('bool ' + name + '::decode(kiwi::ByteBuffer &bb, kiwi::MemoryPool &pool) {');
cpp.push('bool ' + definition.name + '::decode(kiwi::ByteBuffer &bb, kiwi::MemoryPool &pool) {');

@@ -995,3 +1038,3 @@ for (var j = 0; j < fields.length; j++) {

if (field.isRequired) {
cpp.push(' if (!_' + field.name + ') return false;');
cpp.push(' if (' + field.name + '() == nullptr) return false;');
}

@@ -1005,3 +1048,5 @@ }

var field = fields[j];
var value = field.isArray ? 'it' : 'add_' + field.name + '(pool)';
var name = cppFieldName(field);
var value = field.isArray ? 'it' : name;
var isPointer = cppIsFieldPointer(definitions, field);
var code;

@@ -1052,3 +1097,3 @@

else {
code = value + '.decode(bb, pool)';
code = value + (isPointer ? '->' : '.') + 'decode(bb, pool)';
}

@@ -1058,2 +1103,3 @@ }

var type = cppType(definitions, field, false);
var indent = ' ';

@@ -1068,5 +1114,15 @@

cpp.push(indent + 'if (!bb.readVarUint(count)) return false;');
cpp.push(indent + 'for (' + cppType(definitions, field, false) + ' &it : add_' + field.name + '(pool, count)) if (!' + code + ') return false;');
} else {
cpp.push(indent + 'for (' + type + ' &it : set_' + field.name + '(pool, count)) if (!' + code + ') return false;');
}
else {
if (isPointer) {
cpp.push(indent + name + ' = pool.allocate<' + type + '>();');
}
cpp.push(indent + 'if (!' + code + ') return false;');
if (!isPointer) {
cpp.push(indent + 'set_' + field.name + '(' + name + ');');
}
}

@@ -1073,0 +1129,0 @@

{
"name": "kiwi-schema",
"version": "0.0.4",
"version": "0.0.5",
"description": "",

@@ -5,0 +5,0 @@ "main": "kiwi.js",

@@ -80,6 +80,4 @@ # Kiwi Message Format

int main() {
kiwi::MemoryPool pool;
Test test;
test.add_x(pool) = 123;
test.set_x(123);

@@ -90,2 +88,3 @@ kiwi::ByteBuffer buffer;

Test test2;
kiwi::MemoryPool pool;
bool decode_success = test2.decode(buffer, pool);

@@ -92,0 +91,0 @@

@@ -8,4 +8,4 @@ var assert = require('assert');

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeBoolStruct({x: i})), Buffer(o));
assert.deepEqual(schema.decodeBoolStruct(new Uint8Array(o)), {x: i});
assert.deepEqual(schema.encodeBoolStruct({x: i}), Buffer(o));
assert.deepEqual(schema.decodeBoolStruct(Buffer(o)), {x: i});
}

@@ -19,4 +19,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeByteStruct({x: i})), Buffer(o));
assert.deepEqual(schema.decodeByteStruct(new Uint8Array(o)), {x: i});
assert.deepEqual(schema.encodeByteStruct({x: i}), Buffer(o));
assert.deepEqual(schema.decodeByteStruct(Buffer(o)), {x: i});
}

@@ -33,4 +33,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeUintStruct({x: i})), Buffer(o));
assert.deepEqual(schema.decodeUintStruct(new Uint8Array(o)), {x: i});
assert.deepEqual(schema.encodeUintStruct({x: i}), Buffer(o));
assert.deepEqual(schema.decodeUintStruct(Buffer(o)), {x: i});
}

@@ -58,4 +58,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeIntStruct({x: i})), Buffer(o));
assert.deepEqual(schema.decodeIntStruct(new Uint8Array(o)), {x: i});
assert.deepEqual(schema.encodeIntStruct({x: i}), Buffer(o));
assert.deepEqual(schema.decodeIntStruct(Buffer(o)), {x: i});
}

@@ -87,4 +87,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeFloatStruct({x: i})), Buffer(o));
assert.deepEqual(schema.decodeFloatStruct(new Uint8Array(o)), {x: i});
assert.deepEqual(schema.encodeFloatStruct({x: i}), Buffer(o));
assert.deepEqual(schema.decodeFloatStruct(Buffer(o)), {x: i});
}

@@ -101,4 +101,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeStringStruct({x: i})), Buffer(o));
assert.deepEqual(schema.decodeStringStruct(new Uint8Array(o)), {x: i});
assert.deepEqual(schema.encodeStringStruct({x: i}), Buffer(o));
assert.deepEqual(schema.decodeStringStruct(Buffer(o)), {x: i});
}

@@ -113,4 +113,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeCompoundStruct(i)), Buffer(o));
assert.deepEqual(schema.decodeCompoundStruct(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeCompoundStruct(i), Buffer(o));
assert.deepEqual(schema.decodeCompoundStruct(Buffer(o)), i);
}

@@ -125,4 +125,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeNestedStruct(i)), Buffer(o));
assert.deepEqual(schema.decodeNestedStruct(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeNestedStruct(i), Buffer(o));
assert.deepEqual(schema.decodeNestedStruct(Buffer(o)), i);
}

@@ -137,4 +137,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeBoolMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeBoolMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeBoolMessage(i), Buffer(o));
assert.deepEqual(schema.decodeBoolMessage(Buffer(o)), i);
}

@@ -149,4 +149,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeByteMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeByteMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeByteMessage(i), Buffer(o));
assert.deepEqual(schema.decodeByteMessage(Buffer(o)), i);
}

@@ -160,4 +160,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeUintMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeUintMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeUintMessage(i), Buffer(o));
assert.deepEqual(schema.decodeUintMessage(Buffer(o)), i);
}

@@ -171,4 +171,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeIntMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeIntMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeIntMessage(i), Buffer(o));
assert.deepEqual(schema.decodeIntMessage(Buffer(o)), i);
}

@@ -182,4 +182,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeFloatMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeFloatMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeFloatMessage(i), Buffer(o));
assert.deepEqual(schema.decodeFloatMessage(Buffer(o)), i);
}

@@ -193,4 +193,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeStringMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeStringMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeStringMessage(i), Buffer(o));
assert.deepEqual(schema.decodeStringMessage(Buffer(o)), i);
}

@@ -205,4 +205,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeCompoundMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeCompoundMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeCompoundMessage(i), Buffer(o));
assert.deepEqual(schema.decodeCompoundMessage(Buffer(o)), i);
}

@@ -219,4 +219,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeNestedMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeNestedMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeNestedMessage(i), Buffer(o));
assert.deepEqual(schema.decodeNestedMessage(Buffer(o)), i);
}

@@ -233,4 +233,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeBoolArrayStruct({x: i})), Buffer(o));
assert.deepEqual(schema.decodeBoolArrayStruct(new Uint8Array(o)), {x: i});
assert.deepEqual(schema.encodeBoolArrayStruct({x: i}), Buffer(o));
assert.deepEqual(schema.decodeBoolArrayStruct(Buffer(o)), {x: i});
}

@@ -244,4 +244,4 @@

function check(i, o) {
assert.deepEqual(Buffer(schema.encodeBoolArrayMessage(i)), Buffer(o));
assert.deepEqual(schema.decodeBoolArrayMessage(new Uint8Array(o)), i);
assert.deepEqual(schema.encodeBoolArrayMessage(i), Buffer(o));
assert.deepEqual(schema.decodeBoolArrayMessage(Buffer(o)), i);
}

@@ -254,2 +254,13 @@

it('recursive message', function() {
function check(i, o) {
assert.deepEqual(schema.encodeRecursiveMessage(i), Buffer(o));
assert.deepEqual(schema.decodeRecursiveMessage(Buffer(o)), i);
}
check({}, [0]);
check({x: {}}, [1, 0, 0]);
check({x: {x: {}}}, [1, 1, 0, 0, 0]);
});
it('required field', function() {

@@ -256,0 +267,0 @@ assert.throws(function() { schema.encodeRequiredField({}); }, Error);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc