Socket
Socket
Sign inDemoInstall

live-directory

Package Overview
Dependencies
16
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    live-directory

A Simple-To-Use Dynamic File Content Manager For Webservers


Version published
Maintainers
1
Install size
386 kB
Created

Readme

Source

LiveDirectory: Dynamic File Content Manager

NPM version NPM downloads Language grade: JavaScript GitHub issues GitHub stars GitHub license

Motivation

Implementing your own template/file management system which consistently reads/updates file content can be tedious. LiveDirectory aims to solve that by acting as an automated file content store making a directory truly come alive. Powered by the efficient file watching library chokidar, LiveDirectory can be an efficient solution for fast and iterative web development.

Features

  • Simple-to-use API
  • Sub-Directory Support
  • Asynchronous By Nature
  • Instantaneous Hot Reloading
  • Memory Efficient
  • Supports Windows, Linux & MacOS

Installation

LiveDirectory can be installed using node package manager (npm)

npm i live-directory

Table Of Contents

Examples

Below are varios examples that make use of most classes and methods in LiveDirectory. The micromustache template renderer is used in the examples below but you can use any other renderer/framework.

Serving a basic HTML page
const LiveDirectory = require('live-directory');

// Create LiveDirectory instance
const live_templates = new LiveDirectory({
    root_path: './templates/html'
});

// Create server route for dashboard user page
some_server.get('/dashboard/user', (request, response) => {
    // Some processing done here which generates some user_options for rendering page uniquely

    // Retrieve template LiveFile instance
    // Below relative path is translated under the hood to './templates/html/dashboard/user.html'
    let template = live_templates.get('/dashboard/user.html');

    // Send rendered html code in response
    return response.send(template.content);
});
Customized Dashboard User Page With Compiled Renderer
const MicroMustache = require('micromustache');
const LiveDirectory = require('live-directory');

// Create LiveDirectory instance
const live_templates = new LiveDirectory({
    root_path: './templates/html'
});

// Handle 'reload' event from LiveDirectory so we can re-generate a new compiled micromustache instance on each file content update
live_templates.on('file_reload', (file) => {
    // We can attach our own properties to the LiveFile object
    // Using this, we can recompile a micromustache renderer and attach onto LiveFile
    const compiled = MicroMustache.compile(file.content);
    compiled_templates[file.path].render = compiled.render;
});

// Create server route for dashboard user page
some_server.get('/dashboard/user', (request, response) => {
    // Some processing done here which generates some user_options for rendering page uniquely

    // Retrieve template LiveFile instance
    // Below relative path is translated under the hood to './templates/html/dashboard/user.html'
    let template = live_templates.get('/dashboard/user.html');

    // Generate rendered template code
    let html = template.render(user_options);

    // Send rendered html code in response
    return response.send(html);
});

LiveDirectory

Below is a breakdown of the LiveDirectory class generated when creating a new LiveDirectory instance.

Constructor Options
  • path [String]: Path to the directory.
    • Example: ./templates/
    • Required for a LiveDirectory Instance.
  • ignore [Function]: Ignore/Filter function for deciding which files to load.
    • Example: (String: path) => path.includes('node_modules')
    • Usage: Return true through the function when ignoring a file and vice versa.
  • retry [Object]: File content reading retry policy.
    • every [Number]: Delay between retries in milliseconds.
    • max [Number]: Maximum number of retries.
LiveDirectory Properties
PropertyTypeDescription
pathStringRoot directory path.
watcherFS.WatcherUnderlying Chokidar watcher instance.
treeObjectDirectory tree with heirarchy.
filesObjectAll loaded files with their relative paths.
LiveDirectory Methods
  • ready(): Returns a Promise which is then resolved once instance is fully ready.
  • get(String: path): Returns LiveFile instance for file at specified path.
    • Returns a LiveFile instance or undefined
    • Supported Formats: When root path is /root/var/www/webserver/templates.
      • System Path: /root/var/www/webserver/templates/dashboard/index.html
      • Relative Path: /dashboard/index.html
      • Simple Path: dashboard/index.html
  • on(String: type, Function: handler): Binds a handler for LiveDirectory events.
    • Event 'directory_create': Reports newly created directories.
      • handler: (String: path) => {}
    • Event 'directory_destroy': Reports when a directory is deleted.
      • handler: (String: path) => {}
    • Event 'file_reload': Reports when a file is created/is reloaded.
      • handler: (LiveFile: file) => {}
      • See LiveFile documentation for available properties and methods.
    • Event 'file_destroy': Reports when a file is destroyed.
      • handler: (LiveFile: file) => {}
      • See LiveFile documentation for available properties and methods.
    • Event 'file_error': Reports FileSystem errors for a file.
      • handler: (LiveFile: file, Error: error) => {}
      • See LiveFile documentation for available properties and methods.
    • Event 'error': Reports LiveDirectory instance errors.
      • handler: (Error: error) => {}

LiveFile

Below is a breakdown of the LiveFile instance class that represents all files.

LiveFile Properties
PropertyTypeDescription
pathStringSystem file path.
extensionStringFile extension.
etagStringUnique etag compatible file hash.
contentStringFile text content.
bufferBufferFile raw content.
last_updateNumberLast file text content update timestamp in milliseconds
LiveFile Methods
  • ready(): Returns a Promise which is resolved once file is ready with initial content.
  • reload(): Returns a Promise which is resolved once the File's content is reloaded.

License

MIT

Keywords

FAQs

Last updated on 26 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc