Decoding Uint8Array to Other Data Types in Effect Typescript Library

I see helpers in schema that decode other datatypes to a Uint8Array (Schema.Uint8ArrayFromX), but I'm not seeing any helpers that take a Uint8Array and decode it to other datatypes (Schema.XFromUint8Array).

I feel like I'm missing something obvious or being dumb here 😂. Are they missing because we just haven't gotten around to it yet, or do I have some incorrect assumptions somewhere 🤔?

Context:

I'm working on a service that consumes messages from google pubsub. The data you publish ends up as a string | Uint8Array<ArrayBufferLike> when consuming. Without effect, decoding the data as the consumer looks something like this:

  const message = Buffer.from(event.data, 'base64').toString('utf8');
  const messageData = JSON.parse(message)

  // technically you could just do `JSON.parse(event.data.toString())`, but the above is the answer from stack overflow


I was hoping to instead to use something like Schema.StringFromUint8Array and compose that with Schema.parseJson(MyMessageType), but that doesn't exist.

It seems simple enough to make my own (code below), but I wanted to make sure I wasn't missing something obvious/built-in. Seems like it would be a pretty common use-case for Uint8Array to be treated as the "encoded" format that you'd decode to other datatypes 🤔

class MessageV1 extends S.TaggedClass<MessageV1>()('MessageV1', {
  // ...
}) {}

const MessageFromString = S.parseJson(MessageV1);

const StringFromUint8Array = S.transform(S.Uint8ArrayFromSelf, S.String, {
  strict: true,
  encode: str => Buffer.from(str),
  decode: uint8Array => uint8Array.toString()
});

const MessageFromUint8Array = S.compose(StringFromUint8Array, MessageFromString);
Was this page helpful?