1.2.8 • Published 4 months ago

petit-node v1.2.8

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

petit-node

Node.js subset runs on browser. Supports ES module import including npm packages stored in virtual file system. Virtual file system is provided by @hoge1e3/fs. It also runs in Worker.

The system is developed to implement acepad, the programming environment for mobile device.

Example

import pNode from "https://esm.sh/petit-node@latest/dist/index.js";
pNode.boot({
   init({FS}) {
       // get file object via FS object
       // create main.js
       const main=FS.get("/tmp/main.js");
       main.text(`
 import {greet} from "./sub.js";
 document.body.innerHTML+=greet("petit-node");
 `);
       // create sub.js
       const sub=FS.get("/tmp/sub.js");
       sub.text(`
 export function greet(name) {
    return "Hello, "+name+"!";
 }   
 `);
       return main;
   } 
});

Run in codepen

API

  • pNode.boot(options)
    • Initialize petit-node
    • options can include following attributes:
      • init A function called on boot, the argument object has FS object in @hoge1e3/fs. If the return value is file object, petit-node imports the file.
      • aliases
        • Specifies object that configures aliases.
        • The key is module path (that specifies after import)
        • The value is the module object which is imported
        • This allows use import * as value from "key" from programs in vitual file system.
  • pNode.importModule(f)
    • import module from vitrual file system
    • f is a file object of @hoge1e3/fs
    • Return value is a promise of imported module object.

How does it works?

Each javascript source files in virtual file system is converted to BlobURL. If the file contains import ... from 'filepath', the depending files are also converted to BlobURL recursively.

Virtual file system

  • In browser(DOM) context, The virtual file system uses localStorage to store files. /tmp is mounted as a RAM disk, the content is cleared on reload.
  • In Worker context, The entire file system is mouted as RAM disk.
  • See @hoge1e3/fs for details of file system API
1.2.0

4 months ago

1.1.1

5 months ago

1.0.2

8 months ago

1.1.0

5 months ago

1.0.1

8 months ago

1.2.8

4 months ago

1.1.9

5 months ago

1.1.8

5 months ago

1.0.9

5 months ago

1.1.7

5 months ago

1.0.8

5 months ago

1.1.6

5 months ago

1.0.7

5 months ago

1.2.4

4 months ago

1.1.5

5 months ago

1.0.6

7 months ago

1.1.4

5 months ago

1.0.5

7 months ago

1.2.2

4 months ago

1.1.3

5 months ago

1.0.4

8 months ago

1.2.1

4 months ago

1.1.2

5 months ago

1.0.3

8 months ago

1.0.0

8 months ago