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 🙏

© 2026 – Pkg Stats / Ryan Hefner

dynamic-repl

v1.0.0

Published

dynamic repl

Readme

dynamic-repl

一个动态的、可自定义的 Node.js REPL 环境,提供增强的交互式 JavaScript 执行功能。

特性

  • 动态上下文:支持自定义执行上下文,可以注入自定义对象和函数
  • 智能表达式返回:自动为最后一个表达式添加返回语句,无需手动输入 return
  • 交互式调试:提供带彩色提示的交互式 REPL 环境
  • 异步支持:原生支持异步函数和 Promise
  • 代码编辑模式:支持 .editor 命令进行多行代码编辑
  • 自定义变量访问:通过 evaluator 函数控制所有变量的访问过程

安装

npm install dynamic-repl

使用

基本使用

import { startDynamicRepl } from 'dynamic-repl';

// 启动一个基本的 REPL
await startDynamicRepl();

自定义上下文

import { startDynamicRepl } from 'dynamic-repl';

// 注入自定义对象到 REPL 上下文
const customContext = {
  greeting: 'Hello World',
  multiply: (a, b) => a * b,
  obj: { name: 'dynamic-repl', version: '1.0.0' }
};

await startDynamicRepl(customContext);

使用自定义评估函数

import { startDynamicRepl } from 'dynamic-repl';

// 使用自定义的评估函数,允许访问本地作用域变量
const localVar = 'Hello from local context';

// evaluator 函数用于控制 REPL 中所有变量的访问过程
await startDynamicRepl({}, (param) => {
  // 当 REPL 中的代码访问任何变量时,都会调用此函数
  // 可以访问 localVar 等本地作用域变量
  return eval(param);
});

高级用法 - 完全自定义变量访问控制

import { startDynamicRepl } from 'dynamic-repl';

// 实现完全自定义的变量访问控制逻辑
await startDynamicRepl(
  { /* 自定义上下文对象 */ }, 
  (param) => {
    // 在此处实现自定义的变量访问控制逻辑
    // 例如:添加安全检查、变量过滤、日志记录等
    console.log('访问变量:', param);
    return eval(param);
  }
);

API

startDynamicRepl(ctx?, evaluator?)

启动一个动态的 REPL 会话。

  • ctx {Record<string, any>} - (可选) 注入到 REPL 上下文的对象,默认为空对象 {}
  • evaluator {(param: string) => any} - (可选) 自定义评估函数,默认为 (param) => eval(param)
    • 当 evaluator 函数被提供时,所有在 REPL 中访问的变量都会通过此函数处理
    • 通过 Proxy 实现,控制所有变量的访问过程
    • 可以访问启动 REPL 时的本地作用域变量

特殊命令

  • .reset - 重置评估上下文
  • .exit - 退出 REPL
  • .editor - 进入多行代码编辑模式

功能说明

智能表达式返回

dynamic-repl 会自动为最后一个表达式添加 return 语句,使得表达式的结果可以直接返回:

> 1 + 1
2
> const a = 5
undefined
> a * 2
10

异步代码执行

支持异步代码的执行:

> await fetch('https://api.example.com')
Response { ... }
> (async () => { 
    const response = await fetch('...'); 
    return await response.json(); 
  })()
{ ... }

测试

运行测试套件:

npm test

依赖

  • Node.js (14+)
  • acorn - JavaScript 解析器
  • escodegen - JavaScript 代码生成器

许可证

ISC