简体   繁体   English

Node.js 子进程中的数据丢失

[英]Data loss in Node.js child process

I'm trying to send data (as an object) to a child process in Node.js, however, all of my regular expressions get lost in transfer.我正在尝试将数据(作为对象)发送到 Node.js 中的子进程,但是,我的所有正则表达式在传输中都丢失了。

var arguments = {
    something: { 
       name: 'test',       
       age: 28,
       active; true          
    },
    otherThing: 'some string',
    regex: /test/i,
    regex2: new RegExp('test')
};

var child = cp.fork(path.join(__dirname, 'child.js'));

child.on('message', function (data) {
   console.log(data);
});

child.send(arguments);

In the child.js file I have this at the top:在 child.js 文件中,我在顶部有这个:

process.on('message', function () {
   console.log(arguments); // This is where the data has changed
});

When the log is output from the child process the arguments object instead looks like this:当从子进程输出日志时,参数对象看起来像这样:

{
    something: {
        name: 'test',
        age: 28,
        active: true
    },
    otherThing: 'some string',
    regex: {},
    regex2: {}
}

So far unable to find anything elsewhere about why this may be happening, any ideas?到目前为止无法在其他地方找到任何关于为什么会发生这种情况的任何想法,任何想法?

Because they are completely separate JavaScript processes, you can't send objects.因为它们是完全独立的 JavaScript 进程,所以不能发送对象。 When you pass an object, it gets serialized to JSON and parsed by the child.当您传递一个对象时,它会被序列化为 JSON 并由子对象解析。 (See the docs .) (请参阅文档。)

JSON does not support serializing regex objects. JSON 不支持序列化正则表达式对象。 (Try putting JSON.stringify(/abc/) through your console -- you get back "{}" .) (尝试将JSON.stringify(/abc/)通过您的控制台 - 您会返回"{}" 。)

To include regexes in a JSON object, you can use the json-fn module.要在 JSON 对象中包含正则表达式,您可以使用json-fn模块。 It supports serializing functions, dates, and regexes.它支持序列化函数、日期和正则表达式。 (It was actually thanks to an issue i raised that they added regex support. :)) (这实际上是由于我提出的一个问题,他们添加了正则表达式支持。:))

You could then do something like:然后,您可以执行以下操作:

var JSONfn = require('json-fn');
var arguments = {
    something: { 
       name: 'test',       
       age: 28,
       active; true          
    },
    otherThing: 'some string',
    regex: /test/i,
    regex2: new RegExp('test')
};

var child = cp.fork(path.join(__dirname, 'child.js'));
});

child.send(JSONfn.stringify(arguments));

and:和:

var JSONfn = require('json-fn');
process.on('message', function (data) {
   console.log(JSONfn.parse(data))); // This is where the data has changed
});

You can store the regex as a string like您可以将正则表达式存储为字符串

myRegex.string = "/test/";
myRegex.modif = "i";

Send it to child and then use it like将它发送给孩子,然后像这样使用它

new RegExp(myRegex.string, myRegex.modif);

I tried json-fn but Date objects stringified are not reverted back to Date.我试过 json-fn 但字符串化的 Date 对象没有恢复到 Date。 This module JSON4Process stringifies the objects' properties of type Date, RegExp, Function, Set and Map while maintaining the object as a javascript object.此模块JSON4Process将对象的属性类型字符串化为Date、RegExp、Function、Set 和 Map,同时将对象维护为 javascript 对象。 You don't need to stringify the whole object if you're using fork, you can directly send it.如果您使用 fork,则不需要对整个对象进行字符串化,您可以直接发送它。

const { fork } = require('child_process');
const JSON4Process = require('json4process');

let obj = {
    date: new Date(),
    regex: new RegExp(/regex/g),
    func: () => console.log('func')
}

obj = JSON4Process.stringifyProps(obj);

const child = fork('child.js');
child.send(obj);

And then parse the properties back in the other file:然后解析另一个文件中的属性:

process.on('message', data => {
    let obj = JSON4Process.parseProps(data);
});

In case you need to use spawn or exec you can just use the default JSON.stringify over the modified object with json4process:如果您需要使用 spawn 或 exec,您可以使用默认的 JSON.stringify 通过 json4process 修改对象:

let newObj = JSON.stringify(JSON4Process.stringifyProps(obj));
let originalObj = JSON4Process.parseProps(JSON.parse(newObj));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM