node:string_decoder
The node:string_decoder module in Azion Runtime: how StringDecoder turns Buffer data into strings, and a sample that decodes a multi-byte character.
The node:string_decoder module decodes Buffer objects into strings, as the Node.js string_decoder documentation describes. Use it when binary data arrives in pieces, such as a chunked request or response body, and a multi-byte character can be split between two pieces. In Azion Runtime, import string_decoder from "node:string_decoder" builds, and StringDecoder returns the same values in a deployed function and under azion dev.
A StringDecoder created with utf8 keeps an incomplete character between write() calls. Written the bytes 0xe2 0x82, it returns an empty string; written the next byte, 0xac, it returns €; and end() then returns an empty string. For a decoder created with base64, write() of ab followed by end() of c returns YWJj across the two calls. Buffer is a global, so a function can call Buffer.from() without importing node:buffer.
Example
The function below decodes the two bytes of the ¢ character, logs the result, and returns it in the response body:
The function logs ¢ and responds with status 200 and this body: