Node: how can I use the channel and change one file from multipart

I have an http service that needs to redirect a request, I don’t use streams because I deal with large files in multipart and it overflows RAM or disk (see How to make Node.js Streams work? )

Now I use pipes, and it works, the code is something like this:

var Req = getReq(response);
request.pipe(Req);

The only drawback to this is that in this multipart, I re-send a single JSON file to the tube that needs to change several fields.

Can I use the handset and change one file in a multi-threaded channel?

+4
source share
1 answer

, Transform Stream.

var Req = getReq(response);
var transformStream = new TransformStream();

// the boundary key for the multipart is in the headers['content-type']
// if this isn't set, the multipart request would be invalid
Req.headers['content-type'] = request.headers['content-type'];

// pipe from request to our transform stream, and then to Req
// it will pipe chunks, so it won't use too much RAM
// however, you will have to keep the JSON you want to modify in memory
request.pipe(transformStream).pipe(Req);

:

var Transform = require('stream').Transform,
    util = require('util');

var TransformStream = function() {
  Transform.call(this, {objectMode: true});
};
util.inherits(TransformStream, Transform);

TransformStream.prototype._transform = function(chunk, encoding, callback) {

// here should be the "modify" logic;

// this will push all chunks as they come, leaving the multipart unchanged
// there no limitation on what you can push
// you can push nothing, or you can push an entire file
  this.push(chunk);
  callback();
};

TransformStream.prototype._flush = function (callback) {
    // you can push in _flush
    // this.push( SOMETHING );      
    callback();
};

_transform :

  • JSON, ,

    <SOME_DATA_BEFORE_JSON> <MY_JSON_START>

    this.push(SOME_DATA_BEFORE_JSON); MY_JSON_START var

  • JSON , var

  • JSON :

    <JSON_END> <SOME_DATA_AFTER_JSON>

    JSON_END var, , , : this.push(local_var); this.push(SOME_DATA_AFTER_JSON);

  • JSON,

    this.push(chunk);

, multipart format. SOME_DATA_BEFORE_JSON :

--frontier
Content-Type: text/plain

<JSON_START>

Content-Type, , .. - , ( ). ; (frontier), , JSON . :

  • chunk: <SOME_DATA> --frontier <FILE METADATA> <FILE_DATA>
  • chunk 1: <SOME_DATA> --fron chunk 2: ier <FILE METADATA> <FILE_DATA>

, !

+1

Source: https://habr.com/ru/post/1650689/


All Articles