Skip to content

Bare for native application development

License

Notifications You must be signed in to change notification settings

holepunchto/bare-kit

Repository files navigation

bare-kit

Bare for native application development. The kit provides a web worker-like API to start and manage an isolated Bare thread, called a worklet1, that exposes an RPC abstraction with bindings for various languages.

Usage

JavaScript

const rpc = new BareKit.RPC((req) => {
  if (req.command === 'ping') {
    console.log(req.data.toString())

    req.reply('pong')
  }
})
const req = rpc.request('ping')
req.send('ping')
const data = await req.reply()
console.log(data.toString())

iOS

#import <BareKit/BareKit.h>
BareWorklet *worklet = [[BareWorklet alloc] init];
[worklet start:@"/app.bundle" source:/* Source for `app.bundle` */];
BareIPC *ipc = [[BareIPC alloc] initWithWorklet:worklet];
BareRPCRequestHandler requestHandler = ^(BareRPCIncomingRequest *req, NSError *error) {
  if ([req.command isEqualToString:@"ping"]) {
    CFShow([req dataWithEncoding:NSUTF8StringEncoding]);

    [req reply:@"pong" encoding:NSUTF8StringEncoding];
  }
};
BareRPC *rpc = [[BareRPC alloc] initWithIPC:ipc requestHandler:requestHandler];
BareRPCOutgoingRequest *req = [rpc request:@"ping"];
[req send:@"ping" encoding:NSUTF8StringEncoding];
[req reply:NSUTF8StringEncoding completion:^(NSString *data, NSError *error) {
  CFShow(data);
}];

See https://github.com/holepunchto/bare-ios for a complete example of using the kit in an iOS application.

Android

import to.holepunch.bare.kit.IPC;
import to.holepunch.bare.kit.RPC;
import to.holepunch.bare.kit.Worklet;
Worklet worklet = new Worklet();
try {
  worklet.start("/app.bundle", getAssets().open("app.bundle"));
} catch (Exception e) {
  throw new RuntimeException(e);
}
IPC ipc = new IPC(worklet);
RPC rpc = new RPC(ipc, (req, error) -> {
  if (req.command.equals("ping")) {
    Log.i(TAG, req.data("UTF-8"));

    req.reply("pong", "UTF-8");
  }
});
RPC.OutgoingRequest req = rpc.request("ping");
req.send("ping", "UTF-8");
req.reply("UTF-8", (data, error) -> {
  Log.i(TAG, data);
});

See https://github.com/holepunchto/bare-android for a complete example of using the kit in an Android application.

License

Apache-2.0

Footnotes

  1. This term was chosen to avoid ambiguity with worker threads as implemented by https://github.com/holepunchto/bare-worker.