Example: Adding to Deno API

In this example, we will walk through a simple example that adds the functionality of generating a random number inside of a range.

(This is just an example. You can definitely implement with JavaScript Math.random if you want.)

Define Messages

As mentioned in previous sections, Deno relies on message passing to communicate between TypeScript to Rust and do all the fancy stuff. Therefore, we must first define the messages we want to send and receive.

Go to src/msg.fbs and do the following:

src/msg.fbs
union Any {
    // ... Other message types, omitted
    // ADD THE FOLLOWING LINES
    RandRange,
    RandRangeRes
}

// ADD THE FOLLOWING TABLES
table RandRange {
  from: int32;
  to: int32;
}

table RandRangeRes {
  result: int32;
}

With such code, we tell FlatBuffers that we want 2 new message types, RandRange and RandRangeRes. RandRange contains the range from and to we want. RandRangeRes contains a single value result that represents the random number we will get from the Rust side.

Now, run ./tools/build.py to update the corresponding serialization and deserialization code. Such auto-generated code are store in target/debug/gen/msg_generated.ts and target/debug/gen/msg_generated.rs, if you are interested.

Add Frontend Code

Go to js/ folder. We will now create the TypeScript frontend interface.

Create a new file, js/rand_range.ts. Add the following imports:

We will be using dispatch.sendAsync and dispatch.sendSync to dispatch the request as async or sync operation.

Since we are dealing with messages, we need to do some serialization and deserialization work. For request, we need to put user provided from and to to the table RandRange (as defined above); for responses, we need to extract the result from the table RandRangeRes.

Therefore, let's define 2 functions, req and res, that does the work:

Great! With req and res defined, we can very easily define the actual sync/async APIs:

Expose the Interface

Go to js/deno.ts and add the following line:

Also, go to BUILD.gn and add our file to ts_sources:

Add Backend Code

Go to src/ops.rs. This is the only Rust backend file we need to modify to add this functionality.

Inside ops.rs, let's first import the utility for generating random numbers. Fortunately, Deno already includes a Rust crate called rand that could handle the job. See more about its API here.

Let's import it:

Now, we can create a function (with some boilerplate code) that implements the random range logic:

After completing our implementation, let's tell Rust when we should invoke it.

Notice there is a function called dispatch in src/ops.rs. Inside, we could find a match statement on message types to set corresponding handlers. Let's set our function to be the handler of the msg::Any::RandRange type, by inserting this line:

Done! This is all the code you need to add for this call!

Run ./tools/build.py to build your project! You would now be able to call deno.randRangeSync and deno.randRange .

Try it out in the terminal!

Congrats!

Add Tests

Now let's add a basic test for our code.

Create a file js/rand_range_test.ts:

Include this test in js/unit_tests.ts:

Now, run ./tools/test.py. From all the tests, you will be able to find that your tests are passing:

Submit a PR!

Let's suppose that Deno really needs this functionality and you are resolved to contribute to Deno. You can submit a PR!

Remember to run the following commands to ensure your code is ready for submit!

Last updated

Was this helpful?