refactor: polish

This commit is contained in:
cha0s 2022-09-15 07:29:05 -05:00
parent 90fcb90831
commit a70b8c2d76
2 changed files with 43 additions and 31 deletions

View File

@ -69,6 +69,9 @@ export default class Schema {
const normalized = {};
for (const i in spec) {
normalized[i] = 'string' === typeof spec[i] ? {type: spec[i]} : spec[i];
if (!this.validateType(normalized[i].type)) {
throw new TypeError(`unknown schema type: ${normalized[i].type}`);
}
}
return normalized;
}
@ -98,11 +101,20 @@ export default class Schema {
switch (type) {
case 'uint8': case 'int8': return 1;
case 'uint16': case 'int16': return 2;
case 'uint32': case 'int32': return 4;
case 'float32': return 4;
case 'uint32': case 'int32': case 'float32': return 4;
case 'float64': case 'int64': case 'uint64': return 8;
default: return 0;
}
}
static validateType(type) {
return [
'float32', 'float64',
'int8', 'int16', 'int32', 'int64',
'string',
'uint8', 'uint16', 'uint32', 'uint64',
]
.includes(type);
}
}

View File

@ -9,27 +9,27 @@ export default class Serializer {
decode(view, destination, offset = 0) {
let cursor = offset;
for (const [key, def] of this.schema) {
const {type} = def;
const size = Schema.sizeOfType(type);
for (const [key, {type}] of this.schema) {
const viewMethod = Schema.viewMethodFromType(type);
let value;
if (viewMethod) {
// eslint-disable-next-line no-param-reassign
value = view[`get${viewMethod}`](cursor, true);
cursor += size;
cursor += Schema.sizeOfType(type);
}
switch (type) {
case 'string': {
const length = view.getUint32(cursor, true);
cursor += 4;
const {buffer, byteOffset} = view;
const decoder = new TextDecoder();
value = decoder.decode(new DataView(buffer, byteOffset + cursor, length));
cursor += length;
break;
else {
switch (type) {
case 'string': {
const length = view.getUint32(cursor, true);
cursor += 4;
const {buffer, byteOffset} = view;
const decoder = new TextDecoder();
value = decoder.decode(new DataView(buffer, byteOffset + cursor, length));
cursor += length;
break;
}
default: break;
}
default: break;
}
// eslint-disable-next-line no-param-reassign
destination[key] = value;
@ -38,27 +38,27 @@ export default class Serializer {
encode(source, view, offset = 0) {
let cursor = offset;
for (const [key, def] of this.schema) {
const {type} = def;
const size = Schema.sizeOfType(type);
for (const [key, {type}] of this.schema) {
const viewMethod = Schema.viewMethodFromType(type);
if (viewMethod) {
view[`set${viewMethod}`](cursor, source[key], true);
cursor += size;
cursor += Schema.sizeOfType(type);
}
switch (type) {
case 'string': {
const {length} = source[key];
view.setUint32(cursor, length, true);
cursor += 4;
const encoder = new TextEncoder();
const bytes = encoder.encode(source[key]);
for (let i = 0; i < bytes.length; ++i) {
view.setUint8(cursor++, bytes[i]);
else {
switch (type) {
case 'string': {
const {length} = source[key];
view.setUint32(cursor, length, true);
cursor += 4;
const encoder = new TextEncoder();
const bytes = encoder.encode(source[key]);
for (let i = 0; i < bytes.length; ++i) {
view.setUint8(cursor++, bytes[i]);
}
break;
}
break;
default: break;
}
default: break;
}
}
}