Borsh Documentation

repository·master·Indexed 18 days ago

https://github.com/near/borsh

Borsh (Binary Object Representation Serializer for Hashing) is a high-performance, deterministic, and safe binary serialization format designed for security-critical applications like blockchain protocols. It provides a non-self-describing format with a bijective mapping between objects and binary representations, ensuring consistency for data hashing. Implementations are available for Rust, TypeScript, JavaScript, Java, Go, Python, Assemblyscript, C#, Elixir, Ruby, and C++.

Tokens
3.2K
Snippets
11
Records
15
Agent score
69%

What's inside Borsh

  1. What is Borsh and when to use it

    master

    Borsh (Binary Object Representation Serializer for Hashing) is a non-self-describing binary serialization format designed for security-critical applications.

    Key characteristics include:

    • Consistent and Deterministic: There is a bijective mapping between objects and their binary representations, meaning no two different binary representations can deserialize into the same object. This is essential for applications that compute hashes of binary data.
    • Safe: Implementations prioritize safe coding practices (in Rust, it uses almost exclusively safe code).
    • Fast: Optimized for high performance, particularly in Rust, by opting out of the Serde framework to reduce code size and increase speed.
  2. Borsh serialization specification and data format

    master

    Borsh is a non-self-describing format. It serializes objects into a canonical and deterministic set of bytes based on these general principles:

    • Integers: Little endian.
    • Dynamic Containers: The size of the container is written as a u32 before the values.
    • Unordered Containers (HashMap/HashSet): These are ordered lexicographically by key (using the value as a tie-breaker) before serialization to ensure determinism.
    • Structs: Serialized in the order of fields defined in the struct.
    • Enums: Serialized using a u8 for the enum ordinal, followed by the data inside the enum value (if present).

    Type Mapping Reference

    Informal TypePseudocode Logic
    Integerslittle_endian(x)
    Floatserr_if_nan(x) followed by little_endian(x as integer_type)
    Bool1 for true, 0 for false (as u8)
    Fixed-sized ArrayIterates through elements and represents each el
    Dynamic Array (Vec)repr(len() as u32) followed by elements
    Structrepr(fields) in order
    Enumrepr(variant_index as u8) followed by repr(variant_data)
    HashMap / HashSetrepr(len() as u32) followed by elements sorted by key
    Option1 + repr(value) if Some, else 0
    Stringrepr(utf8_len as u32) followed by the UTF-8 encoded bytes
  3. Quickstart: Use Highlight.js on a web page

    master

    To use Highlight.js on a web page with automatic language detection, include the library's CSS styles, the highlight.pack.js script, and call hljs.initHighlightingOnLoad(). The library will automatically find and highlight code within <pre><code> tags.

    <link rel="stylesheet" href="/path/to/styles/default.css">
    <script src="/path/to/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
  4. Disable highlighting for specific code blocks

    master

    If you want to treat text as code without applying any syntax highlighting, or if you want to disable highlighting entirely for a block, use the following classes on the <code> element:

    • plaintext: Makes arbitrary text look like code without highlighting.
    • nohighlight: Disables highlighting altogether.
    <pre><code class="plaintext">...</code></pre>
    <pre><code class="nohighlight">...</code></pre>
    <pre><code class="plaintext">...</code></pre>
    <pre><code class="nohighlight">...</code></pre>
  5. Install highlight.js via npm (CommonJS)

    master

    You can install highlight.js as a CommonJS module using npm.

    Note on Bundle Size: The default import import hljs from 'highlight.js' includes all languages, which can significantly increase your bundle size. For better efficiency, import only the core library and the specific languages you need, then register them using hljs.registerLanguage().

    npm install highlight.js --save
    // Efficient import pattern
    import hljs from 'highlight.js/lib/highlight';
    import javascript from 'highlight.js/lib/languages/javascript';
    
    hljs.registerLanguage('javascript', javascript);
    
    // To include styles in your JS entry point
    import 'highlight.js/styles/github.css';
  6. Run highlighting in a Web Worker

    master

    To prevent the main thread from freezing when processing large code blocks, you can offload the highlighting logic to a Web Worker. The main script sends the raw text to the worker, and the worker returns the highlighted HTML string.

    // In your main script:
    addEventListener('load', () => {
      const code = document.querySelector('#code');
      const worker = new Worker('worker.js');
      worker.onmessage = (event) => { code.innerHTML = event.data; }
      worker.postMessage(code.textContent);
    });
    
    // In worker.js:
    onmessage = (event) => {
      importScripts('<path>/highlight.pack.js');
      const result = self.hljs.highlightAuto(event.data);
      postMessage(result.value);
    };
  7. Install Highlight.js via CDN or Module

    master

    Highlight.js can be used via CDN, downloaded as a custom build, or installed as a module for server-side use.

    Important Notes:

    • Do not link directly to GitHub source code; the library requires a specific build.
    • CDN files do not contain all languages to keep file size small. If a language is missing, you can add it manually by including its specific minified script.
    • Almond optimization: If using an optimizer like Almond, specify the module name (e.g., hljs).
    <!-- Manually adding a missing language from CDN -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/languages/go.min.js"></script>
    
    <!-- Example Almond optimization command -->
    r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
  8. Get started with Highlight.js on a web page

    master

    To use Highlight.js with minimal setup, link the library and a style sheet to your HTML page, then call hljs.initHighlightingOnLoad(). This will automatically find and highlight code blocks within <pre><code tags by attempting to detect the language automatically.

    Basic Implementation

    <link rel="stylesheet" href="/path/to/styles/default.css">
    <script src="/path/to/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    <link rel="stylesheet" href="/path/to/styles/default.css">
    <script src="/path/to/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
  9. Specify language for code highlighting

    master

    If automatic language detection fails, you can manually specify the language by adding a class to the <code> element. You can use the language name directly, or prefix it with language- or lang-.

    Example for HTML:

    <pre><code class="html">...</code></pre>
    <pre><code class="html">...</code></pre>
  10. Custom Initialization with highlightBlock and configure

    master

    If you need more control than the automatic initHighlightingOnLoad, you can manually trigger highlighting using hljs.highlightBlock(block). This allows you to target specific elements or control exactly when the highlighting occurs.

    If your code containers do not preserve line breaks (e.g., using <div> instead of <pre>), you must configure the library to use <br> tags via hljs.configure({useBR: true}).

    // Manual initialization on DOMContentLoaded
    document.addEventListener('DOMContentLoaded', (event) => {
      document.querySelectorAll('pre code').forEach((block) => {
        hljs.highlightBlock(block);
      });
    });
    
    // Using non-standard containers with <br> support
    hljs.configure({useBR: true});
    document.querySelectorAll('div.code').forEach((block) => {
      hljs.highlightBlock(block);
    });
  11. Add missing languages from CDN

    master

    The CDN-hosted version of highlight.js does not include all languages to keep the file size manageable. If a language is missing, you can manually include it by adding a script tag for that specific language file from the CDN.

    <script
     charset="UTF-8"
     src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.9/languages/go.min.js"></script>
  12. Specify language or disable highlighting for code blocks

    master

    While Highlight.js attempts to auto-detect languages, you can explicitly specify a language using the class attribute on the <code> tag. Supported classes can be prefixed with language- or lang-. To prevent a specific block from being highlighted, use the nohighlight class.

    <!-- Explicitly specifying HTML language -->
    <pre><code class="html">...</code></pre>
    
    <!-- Disabling highlighting -->
    <pre><code class="nohighlight">...</code></pre>