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

leadvm

v2.0.0

Published

[Node js] Leadfisher script loader with vm wrapper

Downloads

2

Readme

npm i leadvm --save
  • Create script from string You can use it for configs, network packets, serialization format, etc. Function expression can be used as api endpoint, domain logic, etc. But you also can use any type of javascript expression inside the script
const leadvm = require('leadvm');
const ms = leadvm.createScript(`({ field: 'value' });`, {}, 'Example');
// const ms = leadvm.createScript(`(a, b) => a + b;`);
console.log(ms); // Output: { field: 'value' }
  • Read scripts from file You can read exact script from file. It will help you with code decomposition. The extension of the script may be any or may not be at all.
const leadvm = require('leadvm');
(async () => {
  const ms = await leadvm.readScript('./test/examples/simple.js');
  console.log(ms);
})();
// Output:
//   { field: 'value', add: [Function: add], sub: [Function: sub] }
  • Read scripts from folder Folder reading may be useful for api modules loading. By default it loads nested directories scripts too, you can change it by providing third parameter as false.
const leadvm = require('leadvm');
(async () => {
  const ms = await leadvm.readDir('./test/examples/readDir');
  console.log(ms);
})();
// Output:
//   {
//      simple: { field: 'value', add: [Function: add], sub: [Function: sub] },
//      deep: {
//        arrow: [Function: anonymous]
//      }
//   }
  • Nested modules scripts By default nested modules can't be required, to require them you must add access field in options:
const leadvm = require('leadvm');
(async () => {
  const sandbox = { console };
  sandbox.global = sandbox;
  const ms = await leadvm.execute(`module.exports = require('./examples/module.cjs');`, {
    type: 'cjs',
    dir: __dirname,
    context: leadvm.execute(Object.freeze(sandbox)),
    access: {
      // You can also use path to dir
      // [path.join(__dirname, 'examples']: true
      // NOTICE: Use it only with boolean value in this case
      [path.join(__dirname, 'examples', 'module.cjs')]: true,
      [path.join(__dirname, 'examples', 'module.nested.js')]: true,
    },
  });
  console.log(ms);
})();
// Output:
//    { name: 'module', value: 1, nested: { name: 'module.nested', value: 2 } }
  • Library substitution For example it can be use to provide custom fs module, with your strict methods
const leadvm = require('leadvm');
(async () => {
  const src = `
    const fs = require('fs');
    module.exports = {
      async useStub() {
        return new Promise((resolve) => {
          fs.readFile('name', (err,data) => {resolve(data);});
        });
      }
    };
  `;
  const ms = leadvm.execute(src, {
    access: {
      fs: {
        readFile(filename, callback) {
          callback(null, 'stub-content');
        },
      },
    },
    type: 'cjs',
  });
  const res = await ms.useStub();
  console.log(res);
})(); // Output: stub-content
  • Class script types
    • type: js Script execution returns last expression cjs Script execution returns all that module.exports includes
    • filename Stands for the name of the module, by default it's empty string
    • dir Stands for the name of the module directory, by default process.cwd()
    • npmIsolation Use it if you want to isolate your npm modules in vm context.
    • ctx Script execution closured context, by default it's clear that is why you can't use even setTimeout or setInterval.
    • access Contains absolute paths to nested modules or name of npm/origin libraries as keys, with stub-content or boolean values, by default you can't require nested modules.
import { Context, Script, ScriptOptions, RunningCodeOptions, BaseOptions } from 'node:vm';

type MODULE_TYPE = 'js' | 'cjs';
type TOptions<value> = { [key: string]: value };

interface VMScriptOptions extends BaseOptions {
  dir?: string;
  filename?: string;
  type?: MODULE_TYPE;
  access?: TOptions<boolean | object>;
  ctx?: Context;
  npmIsolation?: boolean;
  run?: RunningCodeOptions;
  script?: ScriptOptions;
}