Javascript

Limitations

  • The "os" module (files, setTimeout, etc.) support is not available yet.

  • We support ES6 modules but use rollup to bundle dependencies in a single file.

  • The use of sub-directories is not available yet.

  • Node libraries are not available (alternative libraries with no Node dependencies should be used instead).

  • Currently, only calling synchronous functions is permitted within the context of the fs module.

Task Preparation

To execute Function tasks, you may need to perform additional steps before creating them. Please refer to the following instructions to create a Function task in Javascript.

Setup rollup

We need to roll up your JS code with all required dependencies into a single file to be able to execute it. Please follow the following steps to merge all the files into a single one.

  1. Install the Rollup Tool with the following command

npm i rollup
npm i
  1. Once the task is developed and working correctly, use the following command to bundle dependencies in a single file. After running this command you will get the following file dist/main.js

npx rollup -c
  1. The rollup command generates a single JS file that includes your code and all its dependencies. Now you can test your task in Truebit (run the 'Build' and 'Start' commands in the command-line interface)

  2. As you continue your development, you might need to update multiple times your rollup config file to keep testing

Rollup config file

The rollup.config.mjs file is where you provide instructions to Rollup on how to bundle your JavaScript code, making it a fundamental part of the Rollup setup.

For more information, visit the rollup official website

Rollup config example
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default [
  {
    input: 'src/main.js',
    output: {
      file: 'dist/main.js',
      format: 'es',
      name: 'fibonacci',
    },
    plugins: [nodeResolve(), commonjs()],
  },
];

Variables

  • Input: Set up the file that Rollup will use as the starting point for bundling everything together

  • Output: Define how the output file will be generated.

    • file: Output rollup file

    • format: Specifies the format of the generated bundle. Available format

    • name: This is the name of the bundle

  • plugins: Set up the Rollup plugins that perform additional tasks during bundling. This could include things like converting code, making it smaller, or dealing with outside modules.

  • Optional sections: You can add additional sections to configure specific aspects of your project, such as module resolution or custom settings.

Built-in Libraries

Crypto Libraries

Truebit provides built-in Cryptographic library functions within our interpreter to make it easier to use and to boost execution speeds. At the moment, we support Bun.js and Deno.

Comparison

Library
API
Performance
Adoption

Older. It's Node.js Propietary

Slower. It's implemented in JS

Widely adopted. The API is easy to understand for the large community of Node.js developers.

Newer. WebCrypto is a web standard

Faster. It's implemented in Rust, then compiled as WebAssembly code.

Limited adopted. The API is familiar to web developers and those using Deno (the next generation of Node.js).

Performance

When running the examples below, Deno's compiled WebAssembly performs much better than Bun.js, surpassing it by a huge margin.

Bun.js

To use Bun.js, import the library into the function task using the following ES6 module syntax

import crypto from 'crypto';

Example using Bun.js (Node.js crypto API):

import crypto from 'crypto';

function main() {
    const message = 'truebit'.repeat(5000);
    
    let i = 100;
    while (i--) {
    	const hash = crypto.createHash('sha256');
    	const result = hash.update(message).digest('hex');
        console.log(result);
    }
}

main();

Click Here to check the node.js crypto API documentation

Deno

To use Deno, import the library into the function task using the following ES6 module syntax

import crypto from 'webcrypto';

Example using Deno (WebCrypto API):

import crypto from 'webcrypto';
import TextEncoder from 'encoding';

function buf2hex(buffer) {
return [...new Uint8Array(buffer)]
    .map(x => x.toString(16).padStart(2, '0'))
    .join('');
}

async function main() {
    const message = 'truebit'.repeat(5000);
    const encoder = new TextEncoder('ascii');
    const data = encoder.encode(message).buffer;
    let i = 100;
    while (i--) {
        const hash = await crypto.subtle.digest('sha-256', data);
        const result = buf2hex(hash);
        console.log(result);
    }
}
main();

Click Here to check the WebCrypto API documentation

Last updated

Was this helpful?