barekit

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

bare-kit-golang

Go bindings for Bare Kit, which runs Bare in a thread of its own within the application, known as a worklet, with an IPC to it exposed as Bare.IPC inside the worklet.

go get github.com/holepunchto/bare-kit-golang

Usage

package main

import (
	_ "embed"
	"fmt"
	"io"

	barekit "github.com/holepunchto/bare-kit-golang"
)

//go:embed app.bundle
var bundle []byte

func main() {
	lib, err := barekit.Open("path/to/libbare-kit.dylib")
	if err != nil {
		panic(err)
	}

	w, err := lib.Start("/app.bundle", &barekit.Options{Source: bundle})
	if err != nil {
		panic(err)
	}

	defer w.Close()

	if _, err := w.Write([]byte("Hello world")); err != nil {
		panic(err)
	}

	data, err := io.ReadAll(w)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(data))
}

Where app.bundle is a Bare bundle using Bare.IPC:

Bare.IPC.on('data', (data) => Bare.IPC.end(data))

Reading a worklet yields the data that it writes to Bare.IPC until it ends its side, after which Read() returns io.EOF. As Bare.IPC keeps the worklet alive, CloseWrite() ends the data sent to it so that it may exit on its own, and Wait() returns its exit code. Writes return io.ErrClosedPipe once the data sent has ended or the worklet has closed its side. Closing a worklet terminates it, so keep it open for as long as it should run. Once closed, Terminate() does nothing and Suspend(), Resume() and Wakeup() return io.ErrClosedPipe.

These bindings are a thin layer over Bare Kit, whose README covers the lifecycle, IPC, suspension, addons, and error handling of a worklet. In Go terms:

  • If Options.Source is nil, Start() reads filename at runtime, so the worklet may be a module next to its node_modules rather than a bundle.
  • Open() loads Bare Kit as a shared library, so native addons resolve the symbols of Bare from it as is.
  • Read() and Write() block the calling goroutine until the IPC is ready, and one goroutine may read while another writes.
  • An uncaught error in the worklet aborts the process, the Go program included, so report errors over Bare.IPC as described in error handling.

Platforms

The module supports Android, iOS, Linux, macOS, and Windows, and loads Bare Kit at runtime. The releases of the module provide prebuilt Bare Kit libraries, which the prebuild package downloads, verifies, and caches:

path, err := prebuild.Download(ctx, nil)

To download them as a build step instead, such as to bundle them with an application, add the bare-kit-prebuild tool to the module, which then always downloads the libraries of the version that the module depends on:

go get -tool github.com/holepunchto/bare-kit-golang/cmd/bare-kit-prebuild
go tool bare-kit-prebuild -dir prebuilds -target android-arm64

On Android and iOS, the application bundles the library itself. Loading it requires cgo on those platforms, and on Android the Java virtual machine and application context are passed with Options.Android.

License

Apache-2.0

Documentation

Overview

Package barekit runs Bare worklets in process through a Bare Kit library loaded at runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AndroidOptions

type AndroidOptions struct {
	// JavaVM is the Java virtual machine, as a `JavaVM *`, which defaults to
	// the one that loaded Bare Kit through `JNI_OnLoad()`.
	JavaVM uintptr

	// Context is the application context, as a JNI reference to an
	// `android.content.Context`, which need only be valid for the call.
	Context uintptr
}

AndroidOptions are the options of a worklet that apply on Android.

type Error

type Error struct {
	Status int
}

An Error is a call into Bare Kit that failed with Status.

func (*Error) Error

func (e *Error) Error() string

type Library

type Library struct {
	// contains filtered or unexported fields
}

A Library is a loaded Bare Kit library, which stays loaded for as long as the process lives.

func Open

func Open(path string) (*Library, error)

Open loads the Bare Kit library at path, such as one downloaded with the prebuild package. On Android, path may name a library that the application bundles.

func (*Library) OptimizeForMemory

func (l *Library) OptimizeForMemory(enabled bool) error

OptimizeForMemory trades performance for memory. Must be configured before starting the first worklet.

func (*Library) Start

func (l *Library) Start(filename string, opts *Options) (*Worklet, error)

Start starts filename in a worklet with an IPC connection to it.

func (*Library) Teardown

func (l *Library) Teardown() error

Teardown tears down the state shared by the worklets of the process.

Every worklet must have been closed first. Must be called at most once, after which starting a worklet fails. A process that runs worklets for as long as it lives need not call it.

type Options

type Options struct {
	// Args are passed to the worklet as `Bare.argv`.
	Args []string

	// Source is the source of the module, which is read from the filename if
	// nil.
	Source []byte

	// MemoryLimit is the memory limit of each JavaScript heap in bytes, which
	// is inferred from the amount of physical memory of the device if 0.
	MemoryLimit int

	// Android holds the options that apply on Android.
	Android AndroidOptions
}

Options are the options of Library.Start.

type Worklet

type Worklet struct {
	// contains filtered or unexported fields
}

A Worklet is a Bare runtime running on a thread of its own, connected to its `Bare.IPC`.

Reading yields the data that the worklet writes to `Bare.IPC` until it ends its side of the connection, and writing sends data to it. One goroutine may read while another writes. CloseWrite ends the data sent to the worklet, while Close closes the connection and terminates the worklet.

func (*Worklet) Close

func (w *Worklet) Close() error

Close closes the connection, terminating the worklet if it is still running, and releases its resources. Afterwards, Terminate does nothing and Suspend, Resume and Wakeup return io.ErrClosedPipe, while Wait still returns once the worklet has exited.

func (*Worklet) CloseWrite

func (w *Worklet) CloseWrite() error

CloseWrite ends the data sent to the worklet, after which `Bare.IPC` reads end-of-stream while reading continues.

func (*Worklet) Read

func (w *Worklet) Read(b []byte) (int, error)

Read reads data that the worklet has written to `Bare.IPC`, returning io.EOF once it has ended its side of the connection.

func (*Worklet) Resume

func (w *Worklet) Resume() error

Resume resumes a suspended worklet, emitting `resume` on `Bare`.

func (*Worklet) Suspend

func (w *Worklet) Suspend(linger time.Duration) error

Suspend suspends the worklet, emitting `suspend` on `Bare`. It keeps running for linger before going idle, or for as long as the platform allows if linger is negative.

func (*Worklet) Terminate

func (w *Worklet) Terminate() error

Terminate asks the worklet to exit unless it has already exited or been closed.

func (*Worklet) Wait

func (w *Worklet) Wait() int

Wait waits for the worklet to exit and returns its exit code.

func (*Worklet) Wakeup

func (w *Worklet) Wakeup(deadline time.Duration) error

Wakeup wakes up a suspended worklet, emitting `wakeup` on `Bare`. It runs for deadline before going idle again, or for as long as the platform allows if deadline is negative.

func (*Worklet) Write

func (w *Worklet) Write(b []byte) (int, error)

Write sends data to `Bare.IPC` of the worklet, returning io.ErrClosedPipe once the connection has ended or closed.

Directories

Path Synopsis
cmd
bare-kit-prebuild command
Command bare-kit-prebuild downloads the prebuilt Bare Kit library of the version of this module and prints its path.
Command bare-kit-prebuild downloads the prebuilt Bare Kit library of the version of this module and prints its path.
Package prebuild downloads the prebuilt Bare Kit libraries that the releases of this module provide.
Package prebuild downloads the prebuilt Bare Kit libraries that the releases of this module provide.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址