Skip to content

Serialising responses and state management #655

@infu

Description

@infu

Is your feature request related to a problem? Please describe.
Currently Agent responses include Bigint, Principal (class) and typed arrays (Uint8array)
I believe thats made to increase performance.
But these can't be stored in state.
One of the rules in state management libraries: "Do Not Put Non-Serializable Values in State"
So a developer would have to convert every response before storing it in state and then convert it back when making requests with values coming from state.

I am using this to transform my responses and store them.

export const SerializableIC = (x) => {
  if (x === undefined || x === null) return x;
  if (typeof x === "bigint") return x.toString();
  if (ArrayBuffer.isView(x) || x instanceof ArrayBuffer)
    return [...x].map((y) => SerializableIC(y));

  if (Array.isArray(x)) {
    return x.map((y) => SerializableIC(y));
  }

  if (typeof x === "object") {
    if ("toText" in x) return x.toText();

    return Object.fromEntries(
      Object.keys(x).map((k) => {
        return [k, SerializableIC(x[k])];
      })
    );
  }
  return x;
};

Describe the solution you'd like
While it's beneficial to have optimal performance when dealing with large files, I would like to opt out of it and be able to store responses and later use them in other requests without having to transform them two times.

This probably means a new createAgent option, which makes all BigInt become text. all Principals become text and uint8arrays become arrays. And then later accepts these in requests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions