Cloud Functions for Firebase Image download function Error - node.js

I am building a web application using Firebase and the new feature Cloud Functions for Firebase. I have created a function that takes a URL and downloads the image into a 64-bit encoded string as below using the node modules request and request-promise-native:
module.exports = {
downloadImageFromUrl: function (url) {
var options = {
method: 'GET',
uri: url,
resolveWithFullResponse: true,
simple: false,
family: 4
};
return rp.get(options)
.then(function (res) {
return "data:" + res.headers["content-type"] + ";base64," + new Buffer(res.body).toString('base64');
})
.catch(function (error) {
console.log("ERROR GETTING image", error);
return error;
});
}
};
The top function works perfectly running locally but once on firebase it gives the error:
RequestError: Error: getaddrinfo EAI_AGAIN lh6.googleusercontent.com:443
at new RequestError (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/user_code/node_modules/request/request.js:188:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (/user_code/node_modules/request/request.js:884:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at TLSSocket.socketErrorListener (_http_client.js:310:9)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at connectErrorNT (net.js:1020:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
I am calling the function in the firebase auth trigger when a user is created as below:
exports.createUser = functions.auth.user().onCreate(event => {
if (event.data.photoURL) {
utils.downloadImageFromUrl(event.data.photoURL)
.then(function(res){
console.log("User Photo", res);
})
.catch(function(error){
console.log("Error", error);
})
}
});
Any help would be greatly appreciated.

Not entirely sure yet if this is the answer, but after reading the documentation, I read their free plan which says you cannot make any out bound requests. So I guess getting an image from a Url counts as an outbound request. After I start paying for their service, I will come back to verify if this was the problem.

Related

How can i fix this call back error in Node.js?

At first, I installed this library,
> npm install rocketchat-api
and then I've ran this code to use rocket.chat REST API
// Node.js code
const RocketChatApi = require('rocketchat-api');
const result='https://rocket.chat/docs/developer-guides/rest-api/users/setavatar';
const userId='A user id';
const avatarUrl='http://www.coca.ir/wp-content/uploads/2017/05/flowers-love-profile-pictures-1.jpg'
const rocketChatClient = new RocketChatApi(
'https',
'rocket.chat',
443,
'username',
'password',
(err, result)=>{
console.info('RC connected', result)
});
rocketChatClient.users.setAvatar(userId, avatarUrl, (err, body)=>{
});
After run this in Node.js, I got following error, anyone can help me?
\node_modules\rocketchat-api\lib\net.js:144
return callback ? callback(error, null) : reject(error);
^
TypeError: callback is not a function
at Request.clientRequest [as _callback] (D:\node js projects\rocketTest\node_modules\rocketchat-api\lib\net.js:144:39)
at self.callback (D:\node js projects\rocketTest\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
at ClientRequest.<anonymous> (D:\node js projects\rocketTest\node_modules\request\request.js:819:16)
at Object.onceWrapper (events.js:286:20)
at ClientRequest.emit (events.js:198:13)
at TLSSocket.emitRequestTimeout (_http_client.js:662:40)
at Object.onceWrapper (events.js:286:20)
at TLSSocket.emit (events.js:198:13)
at TLSSocket.Socket._onTimeout (net.js:442:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
That's because setAvatar() only accepts 2 parameters: avatarUrl and callback. If you pass 3 parameters, it would take the second one as callback, and throw TypeError if it is not a function (In your example, the 2nd parameter avatarUrl is a string).
Please refer to the source code of lib/api/users.js in rocketchat-api source code:
setAvatar (avatarUrl, callback) {
return this.client.request("POST", "users.setAvatar", {
avatarUrl
}, callback);
}
Unfortunately, the rocketchat-api module's document is incorrect, which leads the problem.

Unknown command when using promise-ftp in node

I'm trying to use a promise approach when interacting with my ftp server. I'm using the node package 'promise-ftp' v1.3.5.
The code I have is as follows:
const appFeedBuffer = Buffer.from('somedatastring', 'utf8');
var Client = require('promise-ftp');
var ftpClient = new Client();
ftpClient.connect({ host: 'myHost', user: 'me', password: 'mypassword' })
.then(() => {
return ftpClient.mkdir('newDir')
}).then(() => {
return ftpClient.put(appFeedBuffer, 'fileName')
}).then(()=> {
return ftpClient.rmdir('oldDir')
}).then(() => {
return ftpClient.rename('newDir', 'oldDir')
}).then(() => {
return ftpClient.end();
}).catch(err => {
console.log("something went wrong: " + err);
res.status(500).send('SERVER ERROR');
});
The problem is the put command - I get the error 'UNKNOWN COMMAND' which appears to come from the #icetee/ftp library, of which promise-ftp is a dependant. put is most certainly a valid command in the promise-ftp library!
Stack:
Unhandled rejection Error: Unknown command
at makeError (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:1128:13)
at Parser.<anonymous> (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:122:25)
at emitTwo (events.js:126:13)
at Parser.emit (events.js:214:7)
at Parser._write (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/parser.js:61:10)
at doWrite (_stream_writable.js:397:12)
at writeOrBuffer (_stream_writable.js:383:5)
at Parser.Writable.write (_stream_writable.js:290:11)
at Socket.ondata (/Users/projects/ftpProject/node_modules/#icetee/ftp/lib/connection.js:298:20)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:597:20)
I can't find anyone else having this problem. What am I doing wrong?

Firebase: Delete files in Cloud Starage using Cloud Functions

I'm trying to delete all files, related to some specific entry in Realtime Database, from Cloud Storage on the entry deletion, using Cloud Functions. My functions looks as fallows:
exports.onDeleteRequest = functions.database.ref('/requests/{requestid}/').onDelete(event => {
const storage = functions.config().firebase.storageBucket;
const userId = event.data.previous.child('uid').val();
const requestId = event.params.requestid;
const path = storage + '/photos/' + userId + '/' + requestId;
console.log('path: ', path);
return gcs.bucket(path).delete().then(function() {
console.log("Files removed");
}).catch(function(error) {
console.error("Failed removing files", error);
});
});
But, I continue getting following error in log:
Failed removing files { ApiError: Not Found
at Object.parseHttpRespBody (/user_code/node_modules/#google-cloud/storage/node_modules/#google-cloud/common/src/util.js:192:30)
at Object.handleResp (/user_code/node_modules/#google-cloud/storage/node_modules/#google-cloud/common/src/util.js:132:18)
at /user_code/node_modules/#google-cloud/storage/node_modules/#google-cloud/common/src/util.js:465:12
at Request.onResponse [as _callback] (/user_code/node_modules/#google-cloud/storage/node_modules/retry-request/index.js:182:7)
at Request.self.callback (/user_code/node_modules/#google-cloud/storage/node_modules/request/request.js:186:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/user_code/node_modules/#google-cloud/storage/node_modules/request/request.js:1163:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
code: 404,
errors: [ { domain: 'global', reason: 'notFound', message: 'Not Found' } ],
response: undefined,
message: 'Not Found' }
path: photos/gRplE2ujnIb93Ji5HSzEj3O4i3d2/-L-DHkKMhvLH9l6dWmR2
All subfolders are 100% existing at the time of the function execution. Can anyone help?
I apologize for asking the question, that, without a doubt, was asked before in one way or another, but I didn't find any suggestion of how to solve this issue or why does it occurs in the first place.

Unable to load default credentials in node.js google translate

I need to translate some text in node.js using google translate. I end up getting the following Error: Could not load the default credentials. Browse to here to get more details. This problem has costed me more than 48 hours. I have followed all the instructions in that link but problem still persists. Please someone point what is it that I've missed.
Error log:
ERROR: Error: Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials
for more information.
at /user_code/node_modules/#google-cloud/translate/node_modules/google-auth-library/lib/auth/googleauth.js:316:21
at /user_code/node_modules/#google-cloud/translate/node_modules/google-auth-library/lib/auth/googleauth.js:346:7
at Request._callback (/user_code/node_modules/#google-cloud/translate/node_modules/google-auth-library/lib/transporters.js:70:30)
at self.callback (/user_code/node_modules/#google-cloud/translate/node_modules/request/request.js:186:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (/user_code/node_modules/#google-cloud/translate/node_modules/request/request.js:878:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at Socket.socketErrorListener (_http_client.js:309:9)
function translateText(Text, Target) {
// [START translate_translate_text]
// Imports the Google Cloud client library
const Translate = require('#google-cloud/translate');
// Your Google Cloud Platform project ID
const projectId = 'xxxxxxxxxx';
// Instantiates a client
const translate = new Translate({
projectId: projectId,
});
// Translates some text into Russian
translate
.translate(Text, Target)
.then(results => {
const translation = results[0];
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
return translation;
})
.catch(err => {
console.error('ERROR:', err);
});
}

Node googleapis: getting Error: invalid_request after short period of time

I'm using the node googleapis library to make requests to the youtube data api. I'm starting of with authenticating a user using passport the passport-youtube-v3 library. Everything works fine. I'm able to authenticate and I can make authorized requests to the youtube data api. But after a certain amount of time (around 1-2h) the credentials seem to have expired or just become invalid and I get the following error:
{ Error: Invalid Credentials
at Request._callback (/Users/flavio/Code/BA/node_modules/google-auth-library/lib/transporters.js:85:15)
at Request.self.callback (/Users/flavio/Code/BA/node_modules/request/request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:286:16)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:926:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
code: 401,
errors:
[ { domain: 'global',
reason: 'authError',
message: 'Invalid Credentials',
locationType: 'header',
location: 'Authorization' } ] }
I understand that auth tokens expire after some time. But according to the documentation this shouldn't happen before 6 months of inactivity. Also I'm not making an excessive amount of requests that could render the tokens invalid. Nevertheless I tried to implement a manual toke refresh of the access token like this:
const fetch = (user, ressource, operation, opts) => {
let oauth2Client = new OAuth2();
let client = google.youtube({ version: 'v3',auth: oauth2Client })[ressource][operation];
oauth2Client.credentials = {
access_token: user.youtube.token,
refresh_token: user.youtube.refreshToken
};
return new Promise((success, failure) => {
client(opts, function(err, data, response) {
if (err)
if(err.code === 401)
oauth2Client.refreshAccessToken(function(err, tokens) {
console.log(err);
});
else
failure(err);
if (data)
success(data);
});
});
}
I'm not sure if I implemented this correctly of if it even makes sense to do this. I get the following error:
{ Error: invalid_request
at Request._callback (/Users/flavio/Code/BA/node_modules/google-auth-library/lib/transporters.js:81:15)
at Request.self.callback (/Users/flavio/Code/BA/node_modules/request/request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:286:16)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:926:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9) code: 400 }
What else could be the issue when getting the first error? Is it possible that the access token expires so quickly? If so, how do I refresh it correctly?
Any help is greatly appreciated!
It seems like the problem was that I didn't instantiate OAuth2 correctly. I didn't pass the credentials of my app to the constructor upon creation. Everything seems to work now.
let oauth2Client = new OAuth2(
clientID,
clientSecret,
callbackURL
);
The only confusion I still have is as to why it worked for a couple of requests before it started to throw the error described above.

Resources