How read stream .pipe(myfunction()) - javascript

How read stream .pipe(myfunction())?
I try , but give errors. How read stream of gulp.src('./userdata.json') and .pipe()? I not know how is it make.
gulpfile.js
var upmodul = require("modul-json");
//......
return gulp.src('./userdata.json')
.pipe(upmodul());
......//
node_modules / modul-json / index.js
'use strict';
var Stream = require('stream');
var loger = function () {
var readable = new Stream.Readable({
read: function (n) {
this.push("ll");
}
});
}
module.exports = loger;
Error
[00:19:39] TypeError: Cannot read property 'on' of undefined
at DestroyableTransform.Readable.pipe (E:\Developers\WebDeveloper\OpenServer
-WebProg\domains\progectapi2\node_modules\vinyl-fs\node_modules\readable-stream\
lib\_stream_readable.js:516:7)
at Gulp.<anonymous> (E:\Developers\WebDeveloper\OpenServer-WebProg\domains\p
rogectapi2\gulpfile.js:159:9)
at module.exports (E:\Developers\WebDeveloper\OpenServer-WebProg\domains\pro
gectapi2\node_modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (E:\Developers\WebDeveloper\OpenServer-WebProg
\domains\progectapi2\node_modules\orchestrator\index.js:273:3)
at Gulp.Orchestrator._runStep (E:\Developers\WebDeveloper\OpenServer-WebProg
\domains\progectapi2\node_modules\orchestrator\index.js:214:10)
at Gulp.Orchestrator.start (E:\Developers\WebDeveloper\OpenServer-WebProg\do
mains\progectapi2\node_modules\orchestrator\index.js:134:8)
at C:\Users\Tiki
\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:129:20
at nextTickCallbackWith0Args (node.js:433:9)
at process._tickCallback (node.js:362:13)
at Function.Module.runMain (module.js:432:11)

The gulp documentation has some information on building a plugin that might be useful to you. Just a sample from that page talks about transforming streams.
All gulp plugins essentially boil down to this:
var Transform = require('stream').Transform;
module.exports = function() {
// Monkey patch Transform or create your own subclass,
// implementing `_transform()` and optionally `_flush()`
var transformStream = new Transform({objectMode: true});
/**
* #param {Buffer|string} file
* #param {string=} encoding - ignored if file contains a Buffer
* #param {function(Error, object)} callback - Call this function (optionally with an
* error argument and data) when you are done processing the supplied chunk.
*/
transformStream._transform = function(file, encoding, callback) {
var error = null,
output = doSomethingWithTheFile(file);
callback(error, output);
});
return transformStream;
};

Related

ReferenceError: File is not defined - express

I`m trying to convert some base64 string to a image file and pass it to firebase via express.
Everything works fine on front end, except this part:
const convertBase64ToFile = (base64String, fileName) => {
let arr = base64String.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let uint8Array = new Uint8Array(n);
while (n--) {
uint8Array[n] = bstr.charCodeAt(n);
}
const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
return file
}
Which library i have to import?
Error:
const file = new File([uint8Array], fileName, { type: mime }); /// getting Error in this line
^
ReferenceError: File is not defined
at convertBase64ToFile (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:19:16)
at conversor (C:\Users\rahto\devel\new\maissaudeapi\api\firestore\write.js:33:16)
at C:\Users\rahto\devel\new\maissaudeapi\mainServer.js:31:21
at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:346:12)
at next (C:\Users\rahto\devel\new\maissaudeapi\node_modules\express\lib\router\index.js:280:10)
Node.js v18.6.0
[nodemon] app crashed - waiting for file changes before starting...
Then, i changed to this:
const convertBase64ToFile = (base64String, fileName) => {
let arr = base64String.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let uint8Array = new Uint8Array(n);
while (n--) {
uint8Array[n] = bstr.charCodeAt(n);
}
const file = fs.writeFileSync(fileName, uint8Array)
let fiz = fs.readFileSync(fileName, file);
// const file = new File([uint8Array], fileName, { type: mime });
return fiz
}
And got this error:
C:\Users\rahto\devel\new\maissaudeapi\node_modules\#firebase\storage\dist\index.node.cjs.js:3036
const newPath = child(ref._location.path, childPath);
^
TypeError: Cannot read properties of undefined (reading 'path')
It seems you are using node runtime. You can use fs module to access file system.
fs.writeFile( file, data, options, callback )
Parameters: This method accept four parameters as mentioned above and described below:
file: It is a string, Buffer, URL or file description integer that denotes the path of the file where it has to be written. Using a file descriptor will make it behave similar to fs.write() method.
data: It is a string, Buffer, TypedArray or DataView that will be written to the file.
options: It is an string or object that can be used to specify optional parameters that will affect the output. It has three optional parameter:
encoding: It is a string value that specifies the encoding of the file. The default value is ‘utf8’.
mode: It is an integer value that specifies the file mode. The default value is 0o666.
flag: It is a string value that specifies the flag used while writing to the file. The default value is ‘w’.
callback: It is the function that would be called when the method is executed.
err: It is an error that would be thrown if the operation fails.
Usage:
var fs = require('fs');
fs.writeFile('file.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
Also check more here and here for usage.
And also check here for documentation.

TypeError thrown when library is included in express

I've come across a very peculiar bug that I can't seem to solve. In Node.js, I have an API wrapper library called Markitable:
var request = require('request');
var Q = require('q');
/**
* Markit on Demand API Client
* #constructor
* #author Chandler Freeman <chandler.freeman#gmail.com>
*/
var Markitable = function() {
};
module.exports = Markitable;
endpoints = {
lookup: 'http://dev.markitondemand.com/Api/v2/Lookup',
quote: 'http://dev.markitondemand.com/Api/v2/Quote',
interactivechart: 'http://dev.markitondemand.com/Api/v2/InteractiveChart'
};
/**
* Company lookup
* #httpMethod GET
* #param {ticker} String A string containing the ticker for the stock
* #param {callback} callback (Optional) If callback is passed, than the function will use callbacks
* #promises yes By default, this function uses promises
*/
Markitable.prototype.lookup = function(ticker, callback) {
var options = {
url: endpoints.lookup,
headers: this.headers,
qs: { 'input': ticker.toUpperCase() },
method: 'GET',
json: true
};
if (!callback) {
var d = Q.defer();
request(options, function(err, httpResponse, body) {
if (err) return d.reject(err);
d.resolve(body);
});
return d.promise;
}
else
request(options, callback);
};
As you can see, all it does is fetch data from the Markit on Demand API and return a Q promise. However, every time I include the
var Markitable = require('markitable');
statement anywhere in my code, when I navigate to the / route of my application I receive
Failure on / route: TypeError: Cannot read property 'user' of undefined
I am using Express I have absolutely no idea at all why this is happening; I've reread the source several times, checked source control changes, everything, yet I can't find the root of the issue. This behavior only persists when the library is included; as soon as I remove that statement, everything works perfectly. I don't understand because the code for this library was the exact same code I used from another library I wrote, and the first one looks great. Here is the code for my routes file:
var User = require('../app/models/user');
var Robinhood = require('marian');
var util = require('util');
var Q = require('q');
module.exports = function(app, passport) {
// =========================================
// Table of Contents(Search by name)
//
// 1. Root
//
// =========================================
// ********************* 1. Root *********************
app.get('/', isLoggedIn, asyncCallback(function*(req, res) {
rh = new Robinhood(req.user.local.rhtoken);
var viewData = { user : req.user };
try {
// e: <Cannot read property user of undefined>
viewData.rhUser = yield rh.getUser();
viewData.rhUser.basic_info = yield rh.getBasicInfo();
viewData.rhPortfolio = yield rh.getPortfolio();
viewData.getOrders = yield rh.getOrders();
viewData.rhAccount = yield rh.getAccount();
viewData.active = { page : 'dashboard' };
res.render('main_pages/maindashboard.ejs', viewData);
}
catch(e) {
console.log("Failure on / route: " + e);
res.render('meta/500.ejs');
}
}));
};
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/login');
}
// Code originally from James Long
// http://jlongster.com/A-Study-on-Solving-Callbacks-with-JavaScript-Generators
function asyncCallback(gen) {
return function() {
return Q.async(gen).apply(null, arguments).done();
};
}
Any ideas about why this strange behavior may be occurring? I can't imagine why importing a library would affect the 'req' object, but somehow it does. Could this be a bug in Express?
EDIT:
Forgot the stack trace:
TypeError: Cannot read property 'user' of undefined
at Robinhood.getUser (/vagrant/StockFire/node_modules/marian/index.js:110:26)
at /vagrant/StockFire/app/routes.js:28:40
at next (native)
at Function.continuer (/vagrant/StockFire/node_modules/q/q.js:1278:45)
at /vagrant/StockFire/node_modules/q/q.js:1305:16
at /vagrant/StockFire/app/routes.js:226:29
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at next (/vagrant/StockFire/node_modules/express/lib/router/route.js:131:13)
at isLoggedIn (/vagrant/StockFire/app/routes.js:217:16)
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at next (/vagrant/StockFire/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/vagrant/StockFire/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/vagrant/StockFire/node_modules/express/lib/router/layer.js:95:5)
at /vagrant/StockFire/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/vagrant/StockFire/node_modules/express/lib/router/index.js:330:12)
at next (/vagrant/StockFire/node_modules/express/lib/router/index.js:271:10)
Solved the issue. Simple bug, but very hard to find. I discovered that a variable named endpoints existed in both libraries, but neither was declared with var, meaning that the variable was overwritten since they both existed in the javascript global scope. Lessons learned: Always check variable scope.

Errors when using socket.io in AngularJs on user updates

Hi I'm trying to make an automated update on a list of articles whenever the user changes his preferred language.
The way I'm trying to do this is by having a IO socket update whenever the user changes in the database.
However I seem to be unsuccesfull in my endeavors, and I have no idea as to why.
Since I'm new to socket.io I thought I'll ask the coding gods in here for some help.
May the software be with you ^^
PS: the project is a Angular fullstack project, scaffolded with Yeoman
Code time!
client/ components/ articlebar/ articlebar.controller.js
'use strict';
angular.module('unityAcademyApp')
.controller('ArticlebarCtrl', function ($scope, $location, Auth, socket) {
$scope.articles = {};
function populateArticles(){
...
Some functionality where $scope.articles are set
...
};
socket.syncUpdates('user', $scope.articles, function() {
console.log('hit');
populateArticles();
});
});
client/ components/ socket/ socket.service.js
/* global io */
'use strict';
angular.module('unityAcademyApp')
.factory('socket', function(socketFactory) {
// socket.io now auto-configures its connection when we ommit a connection url
var ioSocket = io('', {
// Send auth token on connection, you will need to DI the Auth service above
// 'query': 'token=' + Auth.getToken()
path: '/socket.io-client'
});
var socket = socketFactory({
ioSocket: ioSocket
});
return {
socket: socket,
/**
* Register listeners to sync an array with updates on a model
*
* Takes the array we want to sync, the model name that socket updates are sent from,
* and an optional callback function after new items are updated.
*
* #param {String} modelName
* #param {Array} array
* #param {Function} cb
*/
syncUpdates: function (modelName, array, cb) {
cb = cb || angular.noop;
/**
* Syncs item creation/updates on 'model:save'
*/
socket.on(modelName + ':save', function (item) {
var oldItem = _.find(array, {_id: item._id});
var index = array.indexOf(oldItem); // this is line 39
var event = 'created';
// replace oldItem if it exists
// otherwise just add item to the collection
if (oldItem) {
array.splice(index, 1, item);
event = 'updated';
} else {
array.push(item);
}
cb(event, item, array);
});
/**
* Syncs removed items on 'model:remove'
*/
socket.on(modelName + ':remove', function (item) {
var event = 'deleted';
_.remove(array, {_id: item._id});
cb(event, item, array);
});
},
/**
* Removes listeners for a models updates on the socket
*
* #param modelName
*/
unsyncUpdates: function (modelName) {
socket.removeAllListeners(modelName + ':save');
socket.removeAllListeners(modelName + ':remove');
}
};
});
server/ config/ socketio.js
/**
* Socket.io configuration
*/
'use strict';
var config = require('./environment');
// When the user disconnects.. perform this
function onDisconnect(socket) {}
// When the user connects.. perform this
function onConnect(socket) {
// When the client emits 'info', this listens and executes
socket.on('info', function (data) {
console.info('[%s] %s', socket.address, JSON.stringify(data, null, 2));
});
// Insert sockets below
require('../api/translation/translation.socket').register(socket);
require('../api/comment/comment.socket').register(socket);
require('../api/article/article.socket').register(socket);
require('../api/language/language.socket').register(socket);
require('../api/thing/thing.socket').register(socket);
require('../api/user/user.socket').register(socket);
}
module.exports = function (socketio) {
// socket.io (v1.x.x) is powered by debug.
// In order to see all the debug output, set DEBUG (in server/config/local.env.js) to including the desired scope.
//
// ex: DEBUG: "http*,socket.io:socket"
// We can authenticate socket.io users and access their token through socket.handshake.decoded_token
//
// 1. You will need to send the token in `client/components/socket/socket.service.js`
//
// 2. Require authentication here:
// socketio.use(require('socketio-jwt').authorize({
// secret: config.secrets.session,
// handshake: true
// }));
socketio.on('connection', function (socket) {
socket.address = socket.handshake.address !== null ?
socket.handshake.address.address + ':' + socket.handshake.address.port :
process.env.DOMAIN;
socket.connectedAt = new Date();
// Call onDisconnect.
socket.on('disconnect', function () {
onDisconnect(socket);
console.info('[%s] DISCONNECTED', socket.address);
});
// Call onConnect.
onConnect(socket);
console.info('[%s] CONNECTED', socket.address);
});
};
server/ api/ user/ user.socket.js
/**
* Broadcast updates to client when the model changes
*/
'use strict';
var User = require('./user.model');
exports.register = function(socket) {
User.schema.post('save', function (doc) {
onSave(socket, doc);
});
User.schema.post('remove', function (doc) {
onRemove(socket, doc);
});
}
function onSave(socket, doc, cb) {
socket.emit('user:save', doc);
}
function onRemove(socket, doc, cb) {
socket.emit('user:remove', doc);
}
Errors encountered so far
So far I get the following error when running the code
TypeError: array.indexOf is not a function
at Socket.<anonymous> (socket.service.js:39)
at socket.js:24
at angular.js:17782
at completeOutstandingRequest (angular.js:5490)
at angular.js:5762
(anonymous function) # angular.js:12416
$get # angular.js:9203
(anonymous function) # angular.js:17785
completeOutstandingRequest # angular.js:5490
(anonymous function) # angular.js:5762
I'm not sure why you get that error, but I think I know why your data isn't updating.
You have to wrap your callback functions inside a $timeout function in order to trigger your changes. For example, you could do this:
$timeout(function(){
cb(event, item, array);
}, 0);
Remember to include $timeout directive in your socket factory.
what's 'undescore' mean? I'm not sure about the 'undescore', but I guess that's alias for 'this'. I think you should init var _ = this.
I just guessing.
I found the problem.
The error where due to it looking for a user in a list of articles, which returned undefined due to there not being any match. The solution was therefore to change the code in client/ components/ articlebar/ articlebar.controller.js
from
socket.syncUpdates('user', $scope.articles, function() {
console.log('hit');
populateArticles();
});
to
socket.syncUpdates('user', $scope.users, function() {
console.log('hit');
populateArticles();
});

TypeError: Object function.....has no method 'on'

I am new to node.js and trying to create my first module.
But i am getting an error
TypeError: Object function SetAstAppLog.....has no method 'on'
My module file contains following code :
var EventEmitter = require('events').EventEmitter;
var util = require('util');
module.exports = SetAstAppLog;
util.inherits(SetAstAppLog, EventEmitter);
function SetAstAppLog(logFolderPath,fileNamePrefix,fileSize,logStreamObject) {
EventEmitter.call(this);
.
.
.
.
this.emit('objCreated');
}
and in app.js i am doing following things :
var SetAstAppLog = require('astAppLog');
var fileSize = 1024;
SetAstAppLog.on('objCreated', function () {
console.log('an object was created');
});
SetAstAppLog.on('written', function () {
console.log('Write operation completed.');
});
var objCommLogger = new SetAstAppLog(logFolderPath,logCommFilePrefix,fileSize,logCommMsg);
var objErrorLogger = new SetAstAppLog(logFolderPath,logErrorFilePrefix,fileSize,logErrorMsg);
Here, i am using node js with v0.10.21.
I am not able to find out why i am getting this error even my module file contains EventEmitter
Can anyone help to solve this issue?
You are exporting the function without calling it. You need to call it:
var SetAstAppLog = require('astAppLog')();
Edit: I failed to notice that you called the function lower in your script. You should be attaching the event handlers to the instances, not the constructor.
var objCommLogger = new SetAstAppLog(logFolderPath, logCommFilePrefix, fileSize, logCommMsg);
objCommLogger.on(...);

Stream in NodeJS

I need some help to understand how stream work in NodeJS
I explain, i need to write a module which call a UNIX process (with spawn) and I want to redirect the stdout of this process to a Readable Stream.
I want this behavior to exports the Readable Stream and allow another module to read them.
To do this, I have write a little piece of code :
var spawn = require('child_process').spawn
var Duplex = require('stream').Duplex;
var stream = new Duplex;
var start = function() {
ps = spawn('mycmd', [/*... args ...*/]);
ps.stdout.pipe(stream);
};
exports.stream = stream;
exports.start = start;
But if I use this module I throw an exception which say that the stream doesn't implement the _read method.
Can you help me with this problem ?
Thanks in advance.
[EDIT] I have try the solution of creating a Stream object, but that's doesnt work, here is the code:
var spawn = require('child_process').spawn;
var Stream = require('stream');
var ps = null;
var audio = new Stream;
audio.readable = audio.writable = true;
var start = function() {
if(ps == null) {
ps = spawn('mycmd', []);
ps.stdout.pipe(stream);
}
};
var stop = function() {
if(ps) {
ps.kill();
ps = null;
}
};
exports.stream = stream;
exports.start = start;
exports.stop = stop;
But when I try to listen the stream, I encount an new error :
_stream_readable.js:583
var written = dest.write(chunk);
^
TypeError: Object #<Stream> has no method 'write'
Most of Node's Stream classes aren't meant to be used directly, but as the base of a custom type:
Note that stream.Duplex is an abstract class designed to be extended with an underlying implementation of the _read(size) and _write(chunk, encoding, callback) methods as you would with a Readable or Writable stream class.
One notable exception is stream.PassThrough, which is a simple echo stream implementation.
var PassThrough = require('stream').PassThrough;
var stream = new PassThrough;
Also note that ps will be a global, making it directly accessible in all other modules.
If you simply want to use stream then you should do :
var stream = new Stream;
stream.readable = stream.writable = true;
Duplex is meant for developers. Some methods like _read and _write need to be implemented for it.
[Update]
OK, you have data source, from the stdout. You will need write function, use this :
stream.write = function(data){this.emit('data', data);};

Categories