如何暂停节点REPL并在稍后阶段恢复并保留所有环境? [英] How to suspend node REPL and resume at a later stage with all the environment preserved?

查看:95
本文介绍了如何暂停节点REPL并在稍后阶段恢复并保留所有环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望暂停REPL会话,以便我可以关闭系统,然后在以后继续处理REPL会话,好像我从未关闭它,即不必丢失所有环境。

I wish to suspend a REPL session so that I could shut down the system and then at a later time continue to work on the REPL session as if I'd never closed it, i.e. without having to lose all the environment.

我认为可能的解决方案可能是

I think that the possible solutions to this could be


  1. 快照内存,保存到文件并稍后从文件加载env :我认为这将是最好的解决方案,就像使用Windows的休眠功能时一样。我发现了这个 heapdump 实用程序,它用于获取内存快照以分析内存泄漏,但我不知道你是否可以从该快照恢复整个环境,我找不到这样做的工具。

  1. Snapshot memory, save to file and load env from file later: I think this would be the neatest solution, like happens when you use the 'hibernate' feature of Windows. I've found this heapdump utility which is intended to take a memory snapshot for analysis of memory leaks, but I don't know if you could resurrect the whole environment from that snapshot and I have found no tools that do so.

保存命令并重播它们:这种方法的一个主要缺点是它适用于简单的事情,如 var x =Hello World; ,它不适用于像 var reciptId = bankAccount.makePayment(1000); 之类的东西,因为它将重复每次重播的操作,而不是保存原始函数调用的细节。

Save commands and replay them: A major shortcoming of this method is while it works for simple things like var x = "Hello World";, it wouldn't work for things like var reciptId = bankAccount.makePayment(1000); as it will repeat actions on each replay rather than saving the details of the original function call.

序列化/反序列化整个环境:这将涉及制作环境中存在的所有对象的列表,然后建立一个机制来编写每个对象它们是一个文件,即序列化它们,然后创建一个反序列化这些并加载它们的机制 在需要的时候。我还没有看到一个干净的方法来序列化和反序列化js变量没有限制。我认为这种方法的主要限制是它无法保留引用,因此对象松散了它们的类,在序列化时必须重复事物并且在反序列化时失去它们的相等性 - 例如

var f = function(x){...};
var a = {};
a.f = f;
a.f === f? //是真的,如果你的序列化机制单独为f和af保存一个函数defn并且单独反序列化

并且循环引用可能不起作用( x = {}; x.cyclic = x; ...)。所以这种方法,如果它工作将需要大量的脏工作。

Serialize / Deserialize the whole environment: This would involve making a list of all objects that exist in the environment, and then make a mechanism to write each of them to a file i.e. serialize them, and then make a mechanism that deserializes these and loads them when required. I am yet to see a clean way to serialize and deserialize js variables without limitations. I think that the major limitation of this method is its inability to retain references, so the objects loose their class, things would have to be duplicated upon serialization and lose their equality on deserialization - e.g. var f = function (x) {...}; var a = {}; a.f = f; a.f === f? //is true, not true if your serialization mechanism saves a function defn for f and a.f separately and deserializes them separately and cyclic references would probably not work (x = {}; x.cyclic = x;...). So this method, if it ever works would require a lot of dirty work.

所以问题是,如何难以实现我希望实现的目标吗?有什么其他解决方案可以做到这一点?是否存在阻碍我实现这一目标的重大障碍?

So the question really is, how difficult is it to achieve what I wish to achieve? What could be some other solutions to do this? Is there a major obstruction to achieving this which I'm overlooking?

此外,是否有节点repl程序的任何替代方案(如浏览器中的控制台)可以这样暂停吗?

Also, are there any alternatives to the node repl program (like a console in a browser) that can be suspended like this?

相关:

  • Swift REPL: how to save/load the REPL state? (a.k.a. suspend/resume, snapshot, clone)

推荐答案

所以如果你希望能够暂停一个REPL会话,然后在关闭后,在Node.js的REPL中似乎没有直接可用的地方,从中断处继续。最接近这一点的是REPL的持久历史功能,它是在节点4.2.1中添加的(我认为)。这将允许您以纯文本格式查看REPL中命令的历史记录,但这是与Node一起开箱即用的最接近的东西。

So if you want to be able to "suspend" a REPL session and then pick up where you left off after a shut down doesn't seem to be directly available in Node.js's REPL. The closest thing to this is the Persistent History feature of the REPL which was added (i think) in Node 4.2.1. This will allow you to view the history of the commands in your REPL in plain text but thats the closest thing available out of the box with Node.


< h2>持久性历史记录

默认情况下,REPL将通过保存到用户主目录中的.node_repl_history文件来在节点REPL会话之间保留历史记录。这可以通过设置环境变量NODE_REPL_HISTORY =来禁用。

Persistent History

By default, the REPL will persist history between node REPL sessions by saving to a .node_repl_history file in the user's home directory. This can be disabled by setting the environment variable NODE_REPL_HISTORY="".

以前在Node.js / io.js v2.x中,使用NODE_REPL_HISTORY_FILE控制REPL历史记录环境变量,历史记录以JSON格式保存。此变量现已弃用,您的REPL历史记录将自动转换为使用纯文本。新文件将保存到您的主目录或NODE_REPL_HISTORY变量定义的目录中,如下所示。

Previously in Node.js/io.js v2.x, REPL history was controlled by using a NODE_REPL_HISTORY_FILE environment variable, and the history was saved in JSON format. This variable has now been deprecated, and your REPL history will automatically be converted to using plain text. The new file will be saved to either your home directory, or a directory defined by the NODE_REPL_HISTORY variable, as documented below.

完整文档有关REPL模块的信息,请此处

Full docs for the REPL module are available here.

但是,有一个REPL包装器节点模块可以满足您的要求。您可以做的是,将REPL历史记录保存到文件中,然后在下一个会话中加载历史文件,并访问下次REPL会话中保存到文件的内容。模块 Nesh 。它还有许多其他功能,包括配置shell和评估不同版本的JS,如ES6 / ES7(使用Babel)和& CoffeeScript的。

However, there is a REPL "wrapper" node module that will do what you're asking. What you can do is, save your REPL history out to a file and then load the history file on the next session and gain access to what you saved to the file in your next REPL session.The module is Nesh. It has a lot of additional features including configuring your shell and evaluating different version of JS such as ES6/ES7 (Using Babel) & Coffeescript.

安装nesh:

npm install -g nesh

只需输入 nesh 即可在终端中启动nesh。通常在任何其他REPL会话中工作,当您想要保存时,可以在nesh中键入以下内容以将REPL历史记录保存到给定文件:

Launch nesh in the terminal by simply typing nesh. Work as you normally would within any other REPL session and when you want to save you can type the following in nesh to save your REPL history to the given file:

.save< filepath>

在您的下一个REPL会话中,即使在关机后,您也可以重新启动您的nesh会话通过输入以下内容重新加载您的历史记录:

In your next REPL session, even after a shutdown, you can relaunch your nesh session and reload your history by typing:

.load< filepath>

这将重新评估整个历史文件,并使当前REPL / nesh会话中的任何变量或函数可用。

This will re-evaluate the entire history file and will makes any variables or functions available in the current REPL/nesh session.

希望这有用,我认为它符合您的需求。

Hope this is helpful and I think it meets your needs.

这篇关于如何暂停节点REPL并在稍后阶段恢复并保留所有环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆