Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

repl-preview

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

repl-preview

Preview output results in a REPL context.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

repl-preview

Preview output results in a REPL context.

Installation

$ npm install repl-preview

Usage

const { preview } = require('repl-preview')
const keypress = require('repl-preview/keypress') // optional
const repl = require('repl')

const server = repl.start()
keypress(server, (line, key) => {
  // run generic JavaScript in VM
  // or preview custom programming language here
  try {
    const result = vm.runInContext(vm.createContent({ ...server.context }), line)
    preview(server, result)
  } catch (err) {
    // handle possible error
  }
})

API

preview(server, result[, opts])

Renders a preview of a mixed result value with the output truncated to fit the viewport of the terminal.

const { preview } = require('repl-preview')
const { inspect } = require('util')

preview(server, result, {
  // truncates output by default
  truncate: true,

  // default result value pretty handler
  pretty(value) {
    return inspect(value, {
      getters: true,
      colors: true,
      sorted: true,
      depth: 3,
      ...opts
    })
  },

  // default result error handler, called upon render failure
  onerror(err) {
    throw err
  }
})

keypress(stdin, onkeypress)

Creates a 'keypress' listener on for a given stdin stream.

const keypress = require('repl-preview/keypress')

// preview eval'd input from context swalling errors
keypress(server.input, (line, key) => {
  try {
    const context = vm.createContent({ ...server.context })
    const result = vm.runInContext(context, line)
    preview(server, result)
  } catch (err) {
    // handle possible error
  }
})

Example

In the included example, a fake filesystem is contrived and JSONata is used as a query syntax to query the filesystem paths. The query syntax is preprocessed to allow / in place of . so file path queries feel like normal path traversal like:

> /var/log
[
  '/var/log/reboot_ai.mxu',
  '/var/log/research_forward.rcprofile',
  '/var/log/circuit.ipfix',
  '/var/log/security_maroon.mime',
  '/var/log/matrix_infrastructure_payment.mbk'
]

License

MIT

Keywords

FAQs

Package last updated on 18 Mar 2021

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc