securityos/node_modules/multiformats/examples/multicodec-interface.js

14 lines
453 B
JavaScript
Raw Normal View History

2024-09-06 15:32:35 +00:00
// Example of multicodec implementation for JSON (UTF-8-encoded)
// Codec implementations should conform to the BlockCodec interface which implements both BlockEncoder and BlockDecoder
/**
* @template T
* @type {BlockCodec<0x0200, T>}
*/
export const { name, code, encode, decode } = {
name: 'json',
code: 0x0200,
encode: json => new TextEncoder().encode(JSON.stringify(json)),
decode: bytes => JSON.parse(new TextDecoder().decode(bytes))
}