Skip to content

RPC serialization fails for Invalid/NaN Date #150

@VastBlast

Description

@VastBlast

Returning an Invalid Date, new Date(NaN), causes a serialization error because JSON converts NaN values to null. It should be returned as Invalid Date without throwing an error.

Reproduction:

import { RpcTarget, newMessagePortRpcSession } from "capnweb";

class MyApi extends RpcTarget {
  getInvalidDate(): Date {
    return new Date(NaN);
  }
}

const localApi = new MyApi();
console.log(localApi.getInvalidDate()); // Invalid Date, no error (as expected)

async function main() {
  const channel = new MessageChannel();
  newMessagePortRpcSession(channel.port1, new MyApi());
  const api = newMessagePortRpcSession<MyApi>(channel.port2);

  const invalidDate = await api.getInvalidDate(); // TypeError: unknown special value: ["date",null]
  console.log(invalidDate); // expected: Invalid Date, without throwing an error
}

main();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions