How to upload assets to a github release from node.js - node.js

I am trying to automatically post some assets on a Github release I programmatically create.
Here is my upload function:
function uploadFile(fileName, uploadUrl, callback){
var uploadEndPoint = url.parse(uploadUrl.substring(0,uploadUrl.indexOf('{')));
options.host = uploadEndPoint.hostname;
options.path = uploadEndPoint.pathname+'?name=' + fileName;
options.method = 'POST';
options.headers['content-type'] = 'application/zip';
var uploadRequest = https.request(options, callback);
uploadRequest.on('error', function(e) {
console.log('release.js - problem with uploadRequest request: ' + e.message);
});
var readStream = fs.ReadStream(path.resolve(__dirname,'builds',fileName));
readStream.pipe(uploadRequest);
readStream.on('close', function () {
req.end();
console.log('release.js - ' + fileName + ' sent to the server');
});
}
At the end of this I get a 404 not found
I tried auth from token and user/password
I checked the url
I though it might be because of SNI, but I don't know how to check that.
Any clue ? Thanks !

I found a solution to that problem by NOT using the low level node.js modules and using instead restler which is a library that handles SNI.
Here is how is used it:
rest = require('restler'),
path = require('path'),
fs = require('fs');
fs.stat(path.resolve(__dirname,'builds',fileName), function(err, stats){
rest.post(uploadEndPoint.href+'?name=' + fileName, {
multipart: true,
username: GITHUB_OAUTH_TOKEN,
password: '',
data: rest.file(path.resolve(__dirname,'builds',fileName), null, stats.size, null, 'application/zip')
}).on('complete', callback);
});
Hope that will help someone :)
EDIT on 27/02/2015: We recently switched from restler to request.
var
request = require('request'),
fs = require('fs');
var stats = fs.statSync(filePath);
var options = {
url: upload_url.replace('{?name}', ''),
port: 443,
auth: {
pass: 'x-oauth-basic',
user: GITHUB_OAUTH_TOKEN
},
json:true,
headers: {
'User-Agent': 'Release-Agent',
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/zip',
'Content-Length': stats.size
},
qs: {
name: assetName
}
};
// Better as a stream
fs.createReadStream(filePath).pipe(request.post(options, function(err, res){
// Do whatever you will like with the result
}));
The upload_uri can be retrieved through a get request on an existing release or in the response directly after the release creation.

Related

How to stop stream-to-promise changing my original buffer

I'm using the stream-to-promise npm module to create the multipart/form-data payload in my jasmine test. My payload includes an image file as a buffer but when the payload is put through stream-to-promise it changes or corrupts my original image buffer in the payload somehow and so my tests are failing. Is there a 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 being put through stream-to-promise looks like this:
You don't need stream-to-buffer. You've already got the buffer from createReadStream. If you get rid of that, it should work. One thing to be careful of is that your maxBytes is set high enough to accomodate your test image. This was causing some cryptic errors when I was testing.
The code below is working fine for me.
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);
});
});

nodejs app - download file from sharepoint with NTLM AUTH

I must confess I'm stuck. I need a nodejs app to download a file from a SharePoint library. Simple, huh? No. Not the simple OOTB SharePoint. The only-ssl allowed, with specific mandatory header added and surely only domain-based NTLM authentication method.
I've tried httpntlm (https://www.npmjs.org/package/httpntlm) that seemed to just might work in advance, but no. The SP responses with something went wrong message.
I've tried node-sharepoint, but it doesn't support NTLM yet. The app gets ETIMEDOUT response.
Any ideas, please welcome.
I am able to download the file using httpntlm module.you need to change the few lines of code.Replace the waterfall logic with below code in httpntlm.js.
async.waterfall([
function ($){
var type1msg = ntlm.createType1Message(options);
httpreq.get(options.url, {
headers:{
'Connection' : 'keep-alive',
'Authorization': type1msg
},
agent: keepaliveAgent
}, $);
},
function (res, $){
if(!res.headers['www-authenticate'])
return $(new Error('www-authenticate not found on response of second request'));
var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
var type3msg = ntlm.createType3Message(type2msg, options);
if(method!=='download'){
httpreq[method](options.url, {
headers:{
'Connection' : 'Close',
'Authorization': type3msg
},
allowRedirects: false,
agent: keepaliveAgent
}, $);
}else{
//By Dheeraj for file download
httpreq.download(
url,
{
headers:{
'Connection' : 'Close',
'Authorization': type3msg
},
//allowRedirects: false,
agent: keepaliveAgent
},
__dirname + 'your_filename',
function (err, progress){
if (err) return console.log(err);
console.log(progress);
}, function (err, res){
if (err) return console.log(err);
console.log(res);
});
}
}
], callback);
};
['get', 'put', 'post', 'delete', 'head','download'].forEach(function(method){
exports[method] = exports.method.bind(exports, method);
});
and replace download method of httpreq.js(httpntm_module/node_modules/httpreq_module/httpreq.js) You can find it at Line number 79 approx.
exports.download = function (url,options, downloadlocation, progressCallback, callback) {
//var options = {};
options.url = url;
options.method = 'GET';
options.downloadlocation = downloadlocation;
options.allowRedirects = true;
// if only 3 args are provided, so no progressCallback
if(callback === undefined && progressCallback && typeof(progressCallback)==="function")
callback = progressCallback;
else
options.progressCallback = progressCallback;
doRequest(options, callback);
}
Please let me know if you are still getting issues.

NodeJS: sending/uploading a local file to a remote server

I have used the Winston module to create a daily log file for my offline app. I now need to be able to send or upload that file to a remote server via POST (that part already exists)
I know I need to write the file in chunks so it doesn't hog the memory so I'm using fs.createReadStream however I seem to only get a 503 response, even if sending just sample text.
EDIT
I worked out that the receiver was expecting the data to be named 'data'. I have removed the createReadSteam as I could only get it to work with 'application/x-www-form-urlencoded' and a synchronous fs.readFileSync. If I change this to 'multipart/form-data' on the php server would I be able to use createReadStream again, or is that only if I change to physically uploading the json file.
I've only been learning node for the past couple of weeks so any pointers would be gratefully received.
var http = require('http'),
fs = require('fs');
var post_options = {
host: 'logger.mysite.co.uk',
path: '/',
port: 80,
timeout: 120000,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
var sender = http.request(post_options, function(res) {
if (res.statusCode < 399) {
var text = ""
res.on('data', function(chunk) {
text += chunk
})
res.on('end', function(data) {
console.log(text)
})
} else {
console.log("ERROR", res.statusCode)
}
})
var POST_DATA = 'data={['
POST_DATA += fs.readFileSync('./path/file.log').toString().replace(/\,+$/,'')
POST_DATA += ']}'
console.log(POST_DATA)
sender.write(POST_DATA)
sender.end()
After gazillion of trial-failure this worked for me. Using FormData with node-fetch. Oh, and request deprecated two days ago, btw.
const FormData = require('form-data');
const fetch = require('node-fetch');
function uploadImage(imageBuffer) {
const form = new FormData();
form.append('file', imageBuffer, {
contentType: 'image/jpeg',
filename: 'dummy.jpg',
});
return fetch(`myserver.cz/upload`, { method: 'POST', body: form })
};
In place of imageBuffer there can be numerous things. I had a buffer containing the image, but you can also pass the result of fs.createReadStream('/foo/bar.jpg') to upload a file from drive.
copied from https://github.com/mikeal/request#forms
var r = request.post('http://service.com/upload', function optionalCallback (err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
})
var form = r.form()
form.append('my_field1', 'my_value23_321')
form.append('my_field2', '123123sdas')
form.append('my_file', fs.createReadStream(path.join(__dirname, 'doodle.png')))
Have a look at the request module.
It will provide you the ability to stream a file to POST requests.

Asana POST Task

I am trying to post a task in asana with the following node/express function
exports.addTask = function(req, res) {
var url ='/api/1.0/tasks?workspace=' + req.session.workspace_id
var postBase = "app.asana.com";
var options = {
host: postBase,
port: 443,
path: url,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + JSON.parse(req.session.user).access_token,
}
};
var req2 = https.request(options, function(res2) {
res2.on('data', function(chunk) {
console.log(chunk + "");
});
res2.on('error', function(e){
console.log(e.message);
});
});
req2.end();
}
I get the correct response back from asana which is:
{"data":{"id":8253508011735,"created_at":"2013-10-20T16:17:53.140Z","modified_at":"2013-10-20T16:17:53.140Z","name":"","notes":"","completed":false,"assignee_status":"upcoming","completed_at":null,"due_on":null,"workspace":{"id":1361701377437,"name":"getspur.com"},"assignee":null,"parent":null,"followers":[{"id":1050147548705,"name":"Gorkem Yurtseven"}],"projects":[],"tags":[]}}
but nothing seems to be added in my asana tasks..
ps. I am currently at the Facebook Hackathon in New York,so bring it on!
It could be because the assignee is null, and it's not in any projects - that basically makes it impossible to find (except perhaps via Search?)
Without seeing the post body I'm not sure if that's intentional or just a formatting issue.

Is there a way to send a JPG with Jasmine NodeJS?

I'm trying to test out my API using FrisbyJS (which sits on top of Jasmine for Node). I was wondering if anyone knew how to submit/send an image using Jasmine?
http://frisbyjs.com/
My current code is...
var frisby = require('frisby');
var URL = 'http://localhost/api';
frisby.globalSetup({
request: {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'api': 'key'
}
},
timeout: (30 * 1000)
});
frisby.create('submit an image')
.post(URL + 'image', {images: '#test.jpg'}, {method: 'POST'})
.expectStatus(200)
.afterJSON(function(cart){
console.log('made it!');
})
}).toss();
And I get the following error:
1) Frisby Test: submit an image
[ POST http://localhost/api ]
Message:
timeout: timed out after 30000 msec waiting for HTTP Request timed out before completing
Stacktrace:
undefined
Finished in 31.458 seconds
And yes, the image test.jpg does exist in the same folder :) as the spec file and where I'm executing the spec itself (jasmine-node .).
I found a solution based on the example given at https://github.com/vlucas/frisby/blob/master/examples/httpbin_multipart_spec.js
var frisby = require('frisby');
var fs = require('fs');
var path = require('path');
var FormData = require('form-data');
var contentPath = path.resolve(__dirname, './path/to/image.jpg');
var form = new FormData();
form.append('file', fs.createReadStream(contentPath), {
knownLength: fs.statSync(contentPath).size
});
frisby.create('Create content asset')
.post(apiPath + '/assets', form, {
json: false,
headers: {
'content-type': 'multipart/form-data; boundary=' + form.getBoundary(),
'content-length': form.getLengthSync(),
}
})
.expectStatus(201)
.toss()

Resources