Exception when attempting to wrap Buffered-writer - node.js

Disclaimer: I'm fairly new to node (but not to JavaScript).
Im just trying to write a logger class, which holds a few lines at a time in memory, then flushes to disk when its buffer is reaching capacity
Problem
Calling my logger wrapper results in an exception in buffered writer
Im sure Ive misunderstood how require() works, and also various people advised me to create the object using new chatlogs.Chatlogger() but itI dont see many other node libs using this way of working
/www/im/node_modules/buffered-writer/lib/buffered-writer.js:125
cb ();
^
TypeError: undefined is not a function
at Writer.flush (/www/nodeim/node_modules/buffered-writer/lib/buffered-writer.js:125:3)
at Chatlogger.close (/www/nodeim/helpers/chatlogs.js:27:14)
at Object.<anonymous> (/www/nodeim/app.js:76:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
calling code...
var chatlogs = require('./helpers/chatlogs.js');
var chatlogger_obj = new chatlogs.Chatlogger();
chatlogger_obj.open("logs/log.txt");
chatlogger_obj.log("TESTING");
chatlogger_obj.close();
process.exit(0);
Wrapper class
./helpers/chatlogs.js
exports.version = '0.0.1';
var
buffer = require('buffered-writer'),
fs = require('fs');
var Chatlogger = function() {
this.handle = null,
this.filename = "",
this.dirtyops = 0;
}
Chatlogger.prototype.open = function (filename) {
//fs.unlink(filename);
this.filename = filename;
this.handle = buffer.open(filename)
.on ("error", function (error) {
//this.handle = null;
console.log (error);
});
}
Chatlogger.prototype.close = function() {
console.log("CLOSING");
this.handle.flush();
this.handle.close();
this.handle = null;
}
Chatlogger.prototype.log = function (str) {
console.log(str);
this.handle.writeln(str);
if (this.dirtyops++ > 5)
{
console.log("FLUSHING");
this.handle.flush();
this.dirtyops = 0;
}
}
module.exports.Chatlogger = Chatlogger;

I'm the author of this module. You need to pass a callback to the flush function, but you don't need to call to flush. When the buffered-writer closes or you exceed the buffer size when writing, the data is automatically flushed to disk.
Writer#flush(callback)

Related

Read file and write file JSON

In this, I am trying to make a hit counter where every time someone visits my site the variable will be read from the views.json file one is added to the number and then the .json will be updated with the new number. However when I tested it in a repl.it project I got an error saying
ReferenceError: writeFileSync is not defined
at /home/runner/hit-counter/index.js:6:1
at Script.runInContext (vm.js:133:20)
at Object.<anonymous> (/run_dir/interp.js:156:20)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
I don't know what this means if you know please tell me and how I may be able to fix it.
the reply project link:https://hit-counter.cohense.repl.run/
The JavaScript (ES6)
const fs = require('fs');
let views = fs.readFileSync('views.json');
views = JSON.parse(views);
views.total++;
let data = JSON.stringify(views, null, 2);
writeFileSync("views.json", data, finished);
function finished(err) {
if (views = JSON.parse(views)) {
console.log("Your view has been accounted for!")
} else {
console.error("Error occured please reload the page =(")
}
};
the JSON
{
"totalViews": 1
}
You can do like this, just fixed some errors.
Oh, you should use writeFileSync, to avoid that the file will not be edited at same time.
The question is, why don't you use a DB? It's a lot faster and fix concurrency writes.
var fs = require('fs')
var data = fs.readFileSync('views.json')
var views = JSON.parse(data);
console.log(views);
views.total = views.total + 1;
var data = JSON.stringify(views, null, 2)
writeFileSync("views.json", data, ()=>{
console.log("Your View Has Been Accounted For!")
})
I found out what I did wrong I didn't use fs.
writeFileSync("views.json", data, finished);
When I just needed to do
fs.writeFileSync("views.json", data[,finished]);

Error: Module did not self-register. (For onoff package require)

I am trying to require the package "onoff" in js file on one of he node js project. When i run a js file i get error as below
\node_modules\bindings\bindings.js:88
throw e
^
Error: Module did not self-register.
at Object.Module._extensions..node (module.js:670:18)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Module.require (module.js:585:17)
at require (internal/module.js:11:18)
at bindings (\node_modules\bindings\bindings.js:81:44)
at Object.<anonymous> (\node_modules\epoll\epoll.js:1:99)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
Please help through this.
Thanks in advance
Pallavi K
I've run into this issue as well and ended up mocking the library for local development. There has been a few issues created over the years, and it seems like the author either doesn't have OSX to test or he just isn't interested in supporting OSX in general.
Issues created related to this problem:
https://github.com/fivdi/epoll/issues/12
https://github.com/fivdi/onoff/issues/69
https://github.com/fivdi/onoff/issues/106
This is the work around I have:
// GpioFactory.js
class MockGPIO {
constructor(pin, direction) {
this._value = 0;
this._direction = direction;
}
readSync() { return this._value; }
read(cb) { cb(null, this._value) }
writeSync(value) { this._value = value }
write(value, cb) {
this._value = value;
cb(null, value);
}
watch(cb) {}
unwatch(cb) {}
unwatchAll() {}
direction() { return this._direction }
setDirection(direction) { this._direction = direction}
edge() { return 0; }
setEdge(edge) {}
activeLow() { return true; }
setActiveLow(invert) {}
unexport() {}
}
MockGPIO.accessible = false;
MockGPIO.HIGH = 1;
MockGPIO.LOW = 0;
module.exports = {
create: () => {
try {
return require('onoff').Gpio;
} catch (e) {
console.error('Using mock Gpio');
return MockGPIO;
}
}
};
The actual fix is the create() method that just returns the mock class. This allows my client code to use both the same way:
const GpioFactory = require('./GpioFactory');
const Gpio = GpioFactory.create();
const garageButton = new Gpio(4, 'out');
I don't use the full API of the library, so this example is likely missing some details.
Update: 12/15/2018
I submitted a PR to allow the accessible property to work on OSX as described in the docs. Hopefully it'll get merged.
PR: https://github.com/fivdi/onoff/pull/122

How to encode a node gd image to base 64 without saving the file?

I am manipulating some images using node-gd, trying to upload to twitter via their api.
Doing it like so works fine:
fs.readFileSync('test.png', { encoding: 'base64' })
But I was hoping there was a way to do this without saving it to a file then loading it again.
Other people suggest doing something like this:
var gd = require('node-gd');
var img = gd.openPng('test.png');
var buff = Buffer.from(img.pngPtr(), 'binary');
var b64 = buff.toString('base64');
console.log(b64);
But when I do this, I get an error saying TypeError: binary is not a function which makes no sense to me.
Full error:
/home/ubuntu/workspace/test.js:16
var buff = Buffer.from(img.pngPtr(), 'binary');
^
TypeError: binary is not a function
at Function.from (native)
at Function.from (native)
at Object.<anonymous> (/home/ubuntu/workspace/test.js:16:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
It seems buffer.from() is relatively new (v 5.10.0), here is the syntax for earlier versions:
new Buffer(pngFile.pngPtr(), 'binary')
A bit late but nevertheless:
var gd = require('node-gd');
var img = gd.openPng('test.png');
var buff = img.pngPtr(-1, true);
var b64 = buff.toString('base64');
console.log(b64);
Where for img.pngPtr(-1, true), -1 is default compression and true will return a Buffer.

Readable Stream Node : the _read method with the Number of bytes param

I am trying to figure out streams in node and playing around with some examples in the stream handbook
I am trying out the _read method of a readable stream. It says, it takes in a parameter which is the number of bytes the consumer wants to read.
I have two questions here.
Is the number of bytes the consumer wants to read the 'watermark'
Why do i get an error when I use _read with a parameter.
This is my code.
var Readable = require('stream').Readable;
var rs = Readable();
var c = 97;
rs._read = function (5) {
rs.push(String.fromCharCode(c++));
if (c > 'z'.charCodeAt(0)) {
rs.push('\n');
rs.push(null);
}
};
setTimeout(function () {
rs.pipe(process.stdout);
}, 2000);
And this is the error
/Users/nikhilkuria/Dev/git/node_demo/streams/streamRead.js:5
rs._read = function (5) {
^
SyntaxError: Unexpected number
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
function (5) { is not valid syntax for creating a function. Instead of a number, you'll want to pass in a variable name, like this: function (a) {
The error you're seeing is due to JavaScript not allowing variable names starting with or consisting of only numbers.

NodeJS, fs.readFileSync string-by-string reading, and operating. How?

Sorry for so dumb question,
How i can in NodeJS read from file string by string some value, for example - url, and do operation with each string eventually?
var contents = fs.readFileSync('test.txt', 'utf8');
and what then?
It's need for browserstack+selenium testing.
I want run some links one-by-one, from file and do something with them.
Changed code below:
from
console.log(lines[i++])
to
line = (lines[i++])
driver.get(line);
driver.getCurrentUrl()
.then(function(currentUrl) {
console.log(currentUrl);
But it works once.
And
var str=fs.readFileSync('test.txt');
str.split(/\n/).forEach(function(line){})
C:\nodejstest>node test1.js
C:\nodejstest\test1.js:57
str.split(/\n/).forEach(function(line){
^
TypeError: str.split is not a function
at Object.<anonymous> (C:\nodejstest\test1.js:57:5)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:142:18)
at node.js:939:3
Works!
Much thanx!
Another (simpler) method would be to read the entire file into a buffer, convert it to a string, split the string on your line-terminator to produce an array of lines, and then iterate over the array, as in:
var buf=fs.readFileSync(filepath);
buf.toString().split(/\n/).forEach(function(line){
// do something here with each line
});
read file with readable stream and do your operation when you find '\n'.
var fs=require('fs');
var readable = fs.createReadStream("data.txt", {
encoding: 'utf8',
fd: null
});
var lines=[];//this is array not a string!!!
readable.on('readable', function() {
var chunk,tmp='';
while (null !== (chunk = readable.read(1))) {
if(chunk==='\n'){
lines.push(tmp);
tmp='';
// this is how i store each line in lines array
}else
tmp+=chunk;
}
});
// readable.on('end',function(){
// console.log(lines);
// });
readable.on('end',function(){
var i=0,len=lines.length;
//lines is #array not string
while(i<len)
console.log(lines[i++]);
});
See if it works for you.

Resources