The string_decoder module in Node.js provides a way to decode buffer objects into strings. It’s particularly useful when working with streams of binary data, allowing developers to convert raw binary data into readable strings while handling different character encodings. This module is available in the Azion Runtime through Node.js compatibility, and a typical use case is decoding chunked request or response bodies in a function without breaking multi-byte characters.
The example below shows how to use the string_decoder module in a function:
/** * An example of using Node.js StringDecoder API in an Azion Function. * Support: * - Partially supported (Extended by library `string_decoder`) * @module runtime-apis/nodejs/string-decoder/main * @example * // Execute with Azion Bundler: * npx edge-functions build * npx edge-functions dev */import string_decoder from "node:string_decoder";
/** * An example of using the Node.js StringDecoder API in an Azion Function. * @param {*} event * @returns {Promise<Response>} */const main = async (event) => { const decoder = new string_decoder.StringDecoder("utf8"); const buffer = Buffer.from([0xc2, 0xa2]); const decoded = decoder.write(buffer); console.log(decoded); // ¢
return new Response(decoded, { status: 200 });};
export default main;