polyfills

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package polyfills provides Node.js-compatible globals for the Goja runtime.

Many NPM packages assume a Node.js environment with globals like process, Buffer, and setTimeout. This package provides lightweight polyfills that enable those packages to work in the TypeGo runtime.

Available Polyfills

  • process: Environment variables, platform info, cwd, argv
  • Buffer: from(), alloc() for basic buffer operations
  • Timers: setTimeout, setInterval, clearInterval

Usage

Call EnableAll to inject all polyfills at once:

polyfills.EnableAll(vm, eventLoop)

Or enable individual polyfills as needed:

polyfills.EnableProcess(vm)
polyfills.EnableBuffer(vm)
polyfills.EnableTimers(vm, eventLoop)

Process Object

The process polyfill provides:

  • process.env: Maps to Go's os.Environ()
  • process.platform: Returns runtime.GOOS
  • process.cwd(): Returns os.Getwd()
  • process.argv: Returns os.Args
  • process.version: Returns runtime.Version()

The FORCE_COLOR environment variable is automatically set to enable colored output for libraries like chalk.

Buffer Object

A minimal Buffer implementation using Uint8Array:

  • Buffer.from(string): Converts string to Uint8Array
  • Buffer.alloc(size): Creates a zero-filled Uint8Array

Timers

Timer functions integrate with the event loop to properly track async work:

  • setTimeout(fn, ms): Schedules a one-time callback
  • setInterval(fn, ms): Schedules a recurring callback
  • clearInterval(id): Stops a recurring interval

Package polyfills provides Node.js-compatible globals for the Goja runtime

Index

Constants

View Source
const BufferShimJS = `` /* 421-byte string literal not displayed */

BufferShimJS is the JavaScript polyfill for the Buffer global

View Source
const EncodingShimJS = `` /* 1509-byte string literal not displayed */

EncodingShimJS provides TextEncoder and TextDecoder polyfills for the Goja runtime. These are Web APIs commonly used for string/byte conversion.

Variables

This section is empty.

Functions

func EnableAll

func EnableAll(vm *goja.Runtime, el *eventloop.EventLoop)

EnableAll injects all polyfills into the VM:

  • process: Environment variables, platform info
  • Buffer: from(), alloc() for buffer operations
  • Timers: setTimeout, setInterval, clearInterval
  • Encoding: TextEncoder, TextDecoder for string/byte conversion

func EnableBuffer

func EnableBuffer(vm *goja.Runtime)

EnableBuffer injects the Buffer global via JavaScript

func EnableEncoding

func EnableEncoding(vm *goja.Runtime)

EnableEncoding injects TextEncoder and TextDecoder globals.

func EnableProcess

func EnableProcess(vm *goja.Runtime)

EnableProcess injects the Node.js `process` global

func EnableTimers

func EnableTimers(vm *goja.Runtime, el *eventloop.EventLoop)

EnableTimers injects setTimeout and setInterval globals

Types

This section is empty.

Jump to

Keyboard shortcuts

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