Skip to content

Using the types TransferDescriptor in typescript #348

Description

@CMCDragonkai

I have a worker function like:

  encryptWithPublicKeyAsn1(
    publicKeyAsn1: PublicKeyAsn1,
    plainText: ArrayBuffer
  ): TransferDescriptor<ArrayBuffer> {
    const plainText_ = Buffer.from(plainText);
    const publicKey = keysUtils.publicKeyFromAsn1(publicKeyAsn1);
    const cipherText = keysUtils.encryptWithPublicKey(publicKey, plainText_);
    return Transfer(cipherText.buffer);
  },

However when I call this function from the main side:

      cipherText = await this.workerManager.call(
        async w => {
          const publicKeyAsn1 = keysUtils.publicKeyToAsn1(publicKey);
          return Buffer.from(
            await w.encryptWithPublicKeyAsn1(
              publicKeyAsn1,
              Transfer(plainText.buffer)
            )
          );
        }
      );

There's a type error:

No overload matches this call.
  The last overload gave the following error.
    Argument of type 'TransferDescriptor<ArrayBuffer>' is not assignable to parameter of type 'WithImplicitCoercion<string> | { [Symbol.toPrimitive](hint: "string"): string; }'.
      Property '[Symbol.toPrimitive]' is missing in type 'TransferDescriptor<ArrayBuffer>' but required in type '{ [Symbol.toPrimitive](hint: "string"): string; }'.

The problem appears that the return type is a TransferDescriptor<ArrayBuffer>.

Then when I call it, it must also be ArrayBuffer.

I have to typecast like this:

      cipherText = await this.workerManager.call(
        async w => {
          const publicKeyAsn1 = keysUtils.publicKeyToAsn1(publicKey);
          return Buffer.from(
            await w.encryptWithPublicKeyAsn1(
              publicKeyAsn1,
              Transfer(plainText.buffer) as unknown as ArrayBuffer
            ) as unknown as ArrayBuffer
          );
        }
      );

Is this the right way to do this?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions