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.
Install the Rollup Tool with the following command
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
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)
As you continue your development, you might need to update multiple times your rollup config file to keep testing
If you encounter dependency issues, please ensure you are not relying on native Node.js libraries. If you are, try to find an alternative library to resolve the problem.
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.
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
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
Example using Bun.js (Node.js crypto API):
Deno
To use Deno, import the library into the function task using the following ES6 module syntax
Example using Deno (WebCrypto API):
Last updated
Was this helpful?