2

I want to create a "fake file" from a javascript object, as the libarry I am using is expecting a file as input, but I have an object in memory instead.

So it is expecting code something like this

var file = fs.readFileSync('{/path/to/file}');

lib.addDocument(config, file),

I want to create a fake file from a object I have called payload, and send that instead, my closest attempt so far looks like this:

  var fake_file = new stream.Readable({ objectMode: true });
  fake_file.push(msg.payload);
  fake_file.push(null);
  lib.addDocument(config, fake_file),

I feel I am close, but I cant quite get it to work. Current error is

{ Error: Unexpected end of multipart data
Gwilym
  • 33
  • 1
  • 7

2 Answers2

1
var Readable = require('stream').Readable
var obj = { objectMode : true};

var rStream = new Readable
rStream.push(JSON.stringify(obj));
rStream.push(null);    //EOF
lib.addDocument(config, rStream)

For older versions below v10 and above v4:

var Readable = require('stream').Readable
var obj = { objectMode : true};
var chars=JSON.stringify(obj).split('');
function read(n){this.push(chars.shift())}
var rStream = new Readable({read:read});
lib.addDocument(config, rStream)
Chirag Ravindra
  • 4,760
  • 1
  • 24
  • 35
  • 1
    My code is now: var Readable = require('stream').Readable var fake_file = new Readable fake_file.push(JSON.stringify(msg.payload)); fake_file.push(null); but I am still getting the "Unexpected end of multipart data" Error – Gwilym Jun 27 '17 at 09:43
  • What version of Node are you using? There has been some API changes in v10. What you need is basically creating a Readable Stream from a string. Figure that out for your node version and pass your object through JSON.stringify to get a string representation of it – Chirag Ravindra Jun 27 '17 at 09:47
  • [info] Node.js version: v6.10.3 Yeah, I have docs open to that effect, I just cant quite seem to find the right set of function calls – Gwilym Jun 27 '17 at 09:54
  • Updated the answer. Found this answer to be helpful: https://stackoverflow.com/a/35607417/2649853 – Chirag Ravindra Jun 27 '17 at 10:06
  • Tried it out, no error but the call the libarry now hangs. I dont think the data is getting into the fake file, when I console.log it I see "length: 0" – Gwilym Jun 27 '17 at 11:36
  • That's interesting. I tried `rStream.pipe(process.stdout)` and it output my string. Which library is that? – Chirag Ravindra Jun 27 '17 at 14:53
  • https://www.ibm.com/watson/developercloud/discovery/api/v1/?node#add-doc. – Gwilym Jun 27 '17 at 15:04
0

Your code is missing an Object -> String conversion. May use JSON.stringify for that:

lib.addDocument(config, JSON.stringify(msg.payload));
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • But the library, is expecting a stream.Readable , so passing it just a string wont work right? – Gwilym Jun 27 '17 at 11:50
  • @gwilym readFileSync returns a string (or buffer). If your first code is right, my code will work too... – Jonas Wilms Jun 27 '17 at 12:23
  • That got me close. I had to force metadata: {"content-type":"application/json"} to get it work, but now i am hitting an error in the library I think "TypeError: source.on is not a function" – Gwilym Jun 27 '17 at 13:33