extism / js-pdk

Write Extism plugins in JavaScript & TypeScript
BSD 3-Clause "New" or "Revised" License
56 stars 16 forks source link

ArrayBufferLike not descriptive enough #90

Open bhelx opened 3 months ago

bhelx commented 3 months ago

Currently we use the type ArrayBufferLike for Host.outputBytes. But this is too restrictive. It doesn't accept typed array buffers. Which means that the user needs to unwrap their buffer. Example:

var buffer = (new TextEncoder()).encode("Hello, World!").buffer
Host.outputBytes(buffer)

Ideally it could take typed and untyped buffers, but i'm not sure if typescript has a known type for this. We could create one like:

type ArrayBufferLike = ArrayBuffer | SharedArrayBuffer;

type TypedArray =
  | Int8Array
  | Uint8Array
  | Uint8ClampedArray
  | Int16Array
  | Uint16Array
  | Int32Array
  | Uint32Array
  | Float32Array
  | Float64Array
  | BigInt64Array
  | BigUint64Array;

type BufferLike = ArrayBufferLike | TypedArray;