How to stop a thread to change my initial buffer

I am using the npm module with promise flow to create the multipart / form-data payload in my jasmine test. My payload includes a buffer image file, but when the payload is streamed to promise that it changes or damages my original image buffer in the payload, and therefore my tests fail. Is there any way to prevent this?

  it('test /identity-verification/your-first-form-of-id POST with validation passing', function(done){
    var form = new FormData();
    var image = fs.createReadStream("image.png");
    streamToBuffer(image, function (err, buffer) {
      form.append('firstID', 'passport');
      form.append('firstIDImage', buffer);
      var headers = form.getHeaders();
      streamToPromise(form).then(function(payload) {
        var options = {
          method: 'POST',
          url: '/identity-verification/your-first-form-of-id',
          payload: payload,
          headers: headers
        };
        server.inject(options, function(response) {
          expect(response.statusCode).toBe(302);
          expect(response.headers.location).toMatch('/identity-verification/your-first-form-of-id/upload-successful');
          done();
        });
      });
    });
  });

The buffer in the payload after it is streamed will look like this:

+4
source share
1 answer

. createReadStream. , . , , , maxBytes , . , .

.

 var streamToPromise = require("stream-to-promise");
 var FormData = require("form-data");
 var fileTypeModule = require("file-type");
 var fs = require("fs");
 var q = require("q");
 var Hapi = require('hapi');
 var server = new Hapi.Server({ debug: { request: ['error'] } });

 server.connection({
     host: 'localhost',
     port: 8000
 });

 server.route({
     method: 'POST',
     path: '/test',
     handler: function(request, reply) {
         var data = request.payload.firstIDImage;
         var fileType = fileTypeModule(data);
         reply(fileType);
     },
     config: {
         payload: { maxBytes: 1048576 }
     }
 });

 var start = server.start();
 var form = new FormData();
 var headers = form.getHeaders();

 form.append('firstID', 'passport');
 form.append('firstIDImage', fs.createReadStream("image.png"));
 var append = streamToPromise(form);
 q.all([start, append]).then((results) => {
     var options = {
         method: 'POST',
         url: '/test',
         payload: results[1],
         headers: headers
     };
     server.inject(options, function(response) {
        console.log(response.payload);
     });
 });
+1

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


All Articles