export function trySerializerForEgress()

in statefun-sdk-js/src/egress.ts [29:46]


export function trySerializerForEgress(type: Type<any> | undefined | null, value: any): Buffer {
    if (!(type === undefined || type === null)) {
        return type.serialize(value);
    }
    if (typeof value === "string") {
        return Buffer.from(value);
    }
    if (Number.isSafeInteger(value)) {
        let buf = Buffer.alloc(4);
        buf.writeInt32BE(value);
        return buf;
    }
    if (value instanceof Uint8Array) {
        // this includes node's Buffer.
        return Buffer.from(value);
    }
    throw new Error("Unable to deduce a type automatically. Please provide an explicit type, or string/Buffer as a value.");
}