Table of Contents
Kiwi schema
How to use Kiwi schema
Example
// Import Kiwi
import * as kiwi from 'kiwi-schema';
// Define schema
const schemaDefinition = `
message Test {
string name = 1;
}
`
// Define KIWI
const schema = kiwi.compileSchema(kiwi.parseSchema(schemaDefinition));
// Encode data
function encode(data){
const binaryData = schema.encodeTest(data);
const base64Data = btoa(String.fromCharCode(...binaryData));
const urlSafeData = encodeURIComponent(base64Data);
return urlSafeData
}
// Decode data
function decode(urlSafeData){
const base64Data = decodeURIComponent(urlSafeData);
const binaryString = atob(base64Data);
const binaryData = new Uint8Array([...binaryString].map(char => char.charCodeAt(0)));
const decodedBinaryData = schema.decodeTest(binaryData)
return decodedBinaryData
}
const encodedData = encode({name: "Fabian"})
console.log("Encoded:",encodedData) // Encoded: AUZhYmlhbgAA
const decodedData = decode(encodedData)
console.log("Decoded:", decodedData) // Decoded: { name: "Fabian" }
Using schema
If the schema message (message Test) is called Test, your encode and decode functions will be called encodeTest and decodeTest.
Typescript
for typescript check out This page
Related snippets
Get License from package.json
How to get and show the projects license from package.json
QuickJS sandbox
A basic example on how to run user provided code in a sandbox in the browser with svelte
Svelte 5 Web Component
A basic example on how to create web components in svelte 5
Timelock encryption
Timelock encryption allows you to encrypt data that can only be decrypted after a certain time