npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

luajit-shell

v1.0.0

Published

Running Lua | LuaJIT scripts from Node.js | Bun via stdio

Downloads

5

Readme

LuaJIT-shell

Running Lua | LuaJIT scripts from Node.js | Bun via stdio. With support for synchronous, asynchronous and with event subscription

Requirements

You must have the lua interpreter or luaJIT installed and added to the PATH. To check, write lua or luaajit in cmd. Alternatively, you can specify a foreign interpreter or path to it in options.luaPath

Installation

npm i luajit-shell

Usage

import LJShell from "luajit-shell";
import {
  run,
  runAsync,
  runString,
  runAsyncString,
  createLua,
} from "luajit-shell";
const LJShell = require("luajit-shell");
const {
  run,
  runAsync,
  runString,
  runAsyncString,
  createLua,
} = require("luajit-shell");

Run the script in synchronous mode:

const result = run({ scriptPath: "lua/print.lua" }, { encoding: "utf8" });
result: LuaBodyStringEncoding

Asynchronous promis:

const result = await runAsync({ scriptPath: "lua/print.lua" });
runAsync({ scriptPath: "lua/print.lua" }, { timeout: 1000 })
  .then((value) => console.log("result", value))
  .catch((error) => console.log("error", error));
result: LuaBodyBufferEncoding, because there is no coding

API

run(options, childOptions)

const result = run({ scriptPath: "lua/print.lua" });

const result = run(
  { luaOptions: ["-e a=1", "-e print(a)"], args: ["value"], luaPath: "lua" },
  { encoding: "utf8", input: "end", timeout: 3000 }
);

runAsync(options, childOptions)

Same as run, except it returns Promise

const result = runAsync(
  {
    scriptPath: "lua/timeout.lua",
    // stdout to JSON parsing
    parser: (str: string) => {
      if (str === "") return [];

      // sub \r\n
      const json = str.substring(-4);
      return JSON.parse(json);
    },
  },
  { encoding: "utf8" }
);

runString(string, options, childOptions)

Runs the entered line of code

const result = runString("print(1)", {}, { encoding: "utf8" });

runAsyncString(string, options, childOptions)

Same as runString, except it returns Promise

runAsyncString("os.exit(1)")
  .then((value) => console.log("result", value))
  .catch((error) => console.log("error", error));

createLua(options, childOptions)

Creates and returns a pure std stream

Note! To receive messages without buffering, use io.output():setvbuf("no") or io.flush() after print() in the lua script.

const lua = test.createLua({ scriptPath: "lua/threads.lua" });

lua.stdout.on("data", (data: Buffer) => {
  console.log(`stdout: ${data}`);
});

lua.stderr.on("data", (data: Buffer) => {
  console.error(`stderr: ${data}`);
});

lua.on("close", (code) => {
  console.log(`child process exited with code ${code}`);
});

Documentation

LuaError

Called exception on lua error. Consists of one property: data: LuaBodyStringEncoding | LuaBodyBufferEncoding

try {
  const result = await runAsync({ scriptPath: "lua/error.lua" });
} catch (error) {
  if (error instanceof LuaError) {
    console.log(error);
  }
}

LuaOptions

Argument type options:

  • scriptPath: string | undefined. Path to the script to be run
  • luaPath: string | undefined. Interpreter or path to it. According to the luajit standard
  • luaOptions: string[] | undefined. Command Line Options
  • args: string[] | undefined. Passed arguments
  • parser: (str: string) => string[] | undefined. Function used for parsing stdout and stderr

LuaBody

Return value type:

  • status: number | null;
  • signal: NodeJS.Signals | null;
  • pid: number | undefined;
  • If encoding is set. LuaBodyStringEncoding:
    • output: [NodeJS.Signals | null, ...string[]];
    • stdout: string[];
    • stderr: string[];
  • If encoding buffer or not set. LuaBodyBufferEncoding:
    • output: [NodeJS.Signals | null, ...Buffer[]];
    • stdout: Buffer;
    • stderr: Buffer;

Test

You can run the tests by using before npm run dev, after npm run test. Note: Tests depend on the architecture and version of interpreters

LICENSE

The MIT License (MIT)