Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First steps in fixing #758 #767

Merged
merged 6 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/codecs/base64-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class Base64Codec implements ContentCodec {
valueToBytes(value: unknown, schema: DataSchema, parameters?: { [key: string]: string }): Buffer {
let body = "";
if (value !== undefined) {
body = value.toString();
body = JSON.stringify(value);
}
return Buffer.from(body, "base64");
}
Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/codecs/octetstream-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ export default class OctetstreamCodec implements ContentCodec {
return "application/octet-stream";
}

bytesToValue(bytes: Buffer, schema: DataSchema, parameters?: { [key: string]: string }): DataSchemaValue {
bytesToValue(
bytes: Buffer,
schema: DataSchema,
parameters: { [key: string]: string | undefined } = {}
): DataSchemaValue {
// console.debug(`OctetstreamCodec parsing '${bytes.toString()}'`);

const bigendian = parameters.byteorder ? parameters.byteorder === "bigendian" : true;
let signed = parameters.signed ? parameters.signed === "true" : false;
const bigendian = parameters.byteorder !== "littleendian"; // default to big endian
let signed = parameters.signed !== "false"; // default to signed

// check length if specified
if (parameters.length && parseInt(parameters.length) !== bytes.length) {
Expand Down Expand Up @@ -160,15 +164,15 @@ export default class OctetstreamCodec implements ContentCodec {
}
}

valueToBytes(value: unknown, schema: DataSchema, parameters?: { [key: string]: string }): Buffer {
valueToBytes(value: unknown, schema: DataSchema, parameters: { [key: string]: string | undefined } = {}): Buffer {
// console.debug(`OctetstreamCodec serializing '${value}'`);

if (!parameters.length) {
console.warn("[core/octetstream-codec]", "Missing 'length' parameter necessary for write. I'll do my best");
}

const bigendian = parameters.byteorder ? parameters.byteorder === "bigendian" : true;
let signed = parameters.signed ? parameters.signed === "true" : true; // default is signed
const bigendian = parameters.byteorder !== "littleendian"; // default to bigendian
let signed = parameters.signed !== "false"; // if signed is undefined -> true (default)
let length = parameters.length ? parseInt(parameters.length) : undefined;
let buf: Buffer;

Expand All @@ -192,7 +196,7 @@ export default class OctetstreamCodec implements ContentCodec {

switch (dataType) {
case "boolean":
return Buffer.alloc(length, value ? 255 : 0);
return Buffer.alloc(length ?? 1, value ? 255 : 0);
case "byte":
case "short":
case "int":
Expand Down Expand Up @@ -302,7 +306,7 @@ export default class OctetstreamCodec implements ContentCodec {
throw new Error("Unable to handle object type " + dataType);

case "null":
return null;
return Buffer.alloc(0);
default:
throw new Error("Unable to handle object type " + dataType);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/codecs/text-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export default class TextCodec implements ContentCodec {
// console.debug(`TextCodec serializing '${value}'`);
let body = "";
if (value !== undefined) {
body = value.toString();
body = JSON.stringify(value);
}

// type BufferEncoding = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex";
let be: BufferEncoding;
let be: BufferEncoding | undefined;
if (parameters && parameters.charset) {
switch (parameters.charset) {
case "ascii":
Expand Down
Loading