window.addEventListener does not work for empty windows - node.js

JSDOM version : 0.6.5
Nitrous.io Cloud IDE environment
Issue observed:
Creating an empty Window and then attaching an event handler fails with exception trace:
TypeError: Cannot read property 'Node' of undefined
at Object.DOMWindow.addEventListener (/home/action/workspace/Projects/nodejs/node_modules/jsdom/lib/jsdom/browser/index.js:180:10)
at Request._callback (/home/action/workspace/Projects/nodejs/page_test_demo.js:22:14)
at Request.self.callback (/home/action/workspace/Projects/nodejs/node_modules/request/index.js:148:22)
at Request.EventEmitter.emit (events.js:98:17)
at Request. (/home/action/workspace/Projects/nodejs/node_modules/request/index.js:886:14)
at Request.EventEmitter.emit (events.js:117:20)
at IncomingMessage. (/home/action/workspace/Projects/nodejs/node_modules/request/index.js:837:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
Here is the code snippet in question:
var window = jsdom.createWindow();
console.log(window);
window.addEventListener('load', function() {
console.log('Load event completed');
});
window.document = jsdom.jsdom(body);
Can anyone tell me what I am missing out here?

The issue you mention in your comment has fixed 7 months ago, so as long as you have a recent version of JSDOM, you should be able to do this:
var jsdom = require('jsdom');
var body = '<html><body></body></html>';
var document = jsdom.jsdom(body);
var window = document.createWindow();
window.addEventListener('load', function() {
console.log('Load event completed');
});

Related

Etherpad plugin development: Document update conflict on using db.findKeys()

I am currently developing an Etherpad Lite plug-in, using Etherpad Lite (1.8.14) and a CouchDB (3.2.2) database. I have first written a small test function which I connected to the server-side hook 'expressCreateServer'.
const db = require('ep_etherpad-lite/node/db/DB');
function expressCreateServer(hook, args, cb) {
args.app.get(`/test`,
async (req, res) => {
await db.init();
const keys = await db.findKeys(`pad:*:revs:*`, null);
res.json(keys);
}
);
return cb;
}
exports.expressCreateServer = expressCreateServer;
I can use db.get() or db.set() without any problems, but Etherpad terminates when I use db.findKeys() and throws the following error.
[2022-08-31 08:41:43.480] [ERROR] server - Error: Error: Document update conflict.
at handleError (/opt/etherpad-lite/src/node_modules/ueberdb2/databases/couch_db.js:26:17)
at /opt/etherpad-lite/src/node_modules/ueberdb2/databases/couch_db.js:127:9
at Request._callback (/opt/etherpad-lite/src/node_modules/nano/lib/nano.js:174:9)
at Request.self.callback (/opt/etherpad-lite/src/node_modules/request/request.js:185:22)
at Request.emit (events.js:375:28)
at Request.emit (domain.js:470:12)
at Request.<anonymous> (/opt/etherpad-lite/src/node_modules/request/request.js:1154:10)
at Request.emit (events.js:375:28)
at Request.emit (domain.js:470:12)
at IncomingMessage.<anonymous> (/opt/etherpad-lite/src/node_modules/request/request.js:1076:12)
This error always occurs exactly once when Etherpad and the CouchDB are used for the first time.
Does anyone have any idea what the problem is?

How to not show stack trace in Gulp task error callback?

When I error out in one of my tasks:
gulp.task('foo', function(onDone) {
onDone('It did not work');
});
I get a large irrelevant stack trace:
[13:10:25] 'foo' errored after 4.27 s
[13:10:25] Error: It did not work
at formatError (C:\npm\node_modules\gulp\bin\gulp.js:169:10)
at Gulp.<anonymous> (C:\npm\node_modules\gulp\bin\gulp.js:195:15)
at emitOne (events.js:96:13)
at Gulp.emit (events.js:188:7)
at Gulp.Orchestrator._emitTaskDone (D:\Code\my-project\node_modules\orchestrator\index.js:264:8)
at D:\Code\my-project\node_modules\orchestrator\index.js:275:23
at finish (D:\Code\my-project\node_modules\orchestrator\lib\runTask.js:21:8)
at cb (D:\Code\my-project\node_modules\orchestrator\lib\runTask.js:29:3)
at D:\Code\my-project\gulp-tasks\insert-docs.js:116:25
at Request._callback (D:\Code\my-project\gulpfile.js:112:17)
How do I not show that? I'd prefer to use the in build gulp features rather than a third party module or console.error().
Gulp checks for the presence of a showStack property on Error objects passed to the callback. This was probably introduced to support that option on gutil.PluginError, but can be used with any Error object:
function withError(msg) {
var err = new Error(msg);
err.showStack = false;
return err;
}
gulp.task('foo', function(done) {
done(withError('It did not work'));
});

gcloud error: ApiError: Not Found at new util.ApiError

Anyone familiar with this gcloud exception:
ApiError: Not Found at new util.ApiError
(/site/node_modules/gcloud/lib/common/util.js:128:10) at
Object.parseHttpRespBody
(/site/node_modules/gcloud/lib/common/util.js:206:30) at
Object.handleResp
(/site/node_modules/gcloud/lib/common/util.js:146:18) at
/site/node_modules/gcloud/lib/common/util.js:447:12 at
Request.onResponse [as _callback]
(/site/node_modules/gcloud/node_modules/retry-request/index.js:120:7)
at Request.self.callback
(/site/node_modules/request/request.js:187:22) at Request.emit
(events.js:98:17) at Request.
(/site/node_modules/request/request.js:1044:10) at Request.emit
(events.js:95:17) at IncomingMessage.
(/site/node_modules/request/request.js:965:12) at IncomingMessage.emit
(events.js:117:20) at _stream_readable.js:944:16 at
process._tickDomainCallback (node.js:492:13)
It appears only in production (of course) and currently consistently. It used to appear periodically and the assumption was that it is a glitch # gCloud since locally it could not be reproduced. It is related to the part of the code which uses the simplest gCloud lib method bucket.upload with no parameters other than the file ... Here is the current function which does it:
function uploadToGoogleCloud(filePath, makePublic) {
var gstorage = gcloud.storage({
projectId: EXAMPLE_projectId,
credentials: EXAMPLE_credentials,
});
var spBucket = Promise.promisifyAll(gstorage.bucket(EXAMPLE_bucket));
return spBucket.uploadAsync(filePath).then(function(file) {
if (makePublic) {
var fileAsync = Promise.promisifyAll(file);
return fileAsync.makePublicAsync().then(function() {
return file;
});
}
return file;
});
}
Any feedback is greatly appreciated.
The error is a bit obscure, but was correct for me. I got this error when I in some cases were trying to write to a bigquery table that did not exist (I accidentally passed "undefined" as the tablename), I suspect that there is some 404 error coercion going on under the hood.

can we run node js child process with code instead of file or command?

I have a situation, in which I want to run some code (some call to API), in child process. So, can we run code in a child process instead of file or command ? -- old
Now I have used this code:
<code>process = require('child_process');
var ls = process.spawnSync('node','lookUp.js ' + serialNumber);
ls.stdout.on('data', function(data) {
console.log('stdout: ' + data);
}); </code>
And received this erroe :
<code>
child_process.js:1254
for (i = 0; i < options.stdio.length; i++) {
^
TypeError: Cannot read property 'length' of undefined
at Object.spawnSync (child_process.js:1254:32)
at /home/eweba1-pc-22/htdocs/geoAdmin/myAdminTester/zohocrm-myadmin-module-to-get-devices/service2.js:252:22
at Object.whilst (/home/eweba1-pc-22/htdocs/geoAdmin/myAdminTester/zohocrm-myadmin-module-to-get-devices/node_modules/async/dist/async.js:4578:9)
at Request._callback (/home/eweba1-pc-22/htdocs/geoAdmin/myAdminTester/zohocrm-myadmin-module-to-get-devices/service2.js:211:10)
at Request.self.callback (/home/eweba1-pc-22/htdocs/geoAdmin/myAdminTester/zohocrm-myadmin-module-to-get-devices/node_modules/request/request.js:200:22)
at Request.EventEmitter.emit (events.js:110:17)
at Request.<anonymous> (/home/eweba1-pc-22/htdocs/geoAdmin/myAdminTester/zohocrm-myadmin-module-to-get-devices/node_modules/request/request.js:1067:10)
at Request.EventEmitter.emit (events.js:129:20)
at IncomingMessage.<anonymous> (/home/eweba1-pc-22/htdocs/geoAdmin/myAdminTester/zohocrm-myadmin-module-to-get-devices/node_modules/request/request.js:988:12)
at IncomingMessage.EventEmitter.emit (events.js:129:20)
</code>

ForEachLine() in node.js

Referring to slide no 35 in ppt on slideshare
When I run this code
var server = my_http.createServer();
server.on("request", function(request,response){
var chunks = [];
output = fs.createWriteStream("./output");
request.on("data",function(chunk){
chunks = forEachLine(chunks.concat(chunk),function(line){
output.write(parseInt(line,10)*2);
output.write("\n");
})
});
request.on("end",function(){
response.writeHeader(200,{"Content-Type":"plain/text"})
response.end("OK\n");
output.end()
server.close()
})
});
server.listen("8080");
I get error as
chunks = forEachLine(chunks.concat(chunk),function(line){
^
ReferenceError: forEachLine is not defined
Of course I unserstand that I need to include some library but when I googled this I found nothing . Since I am complete newbie to this I have absolutely no idea how to resolve it.
Any suggestions will be appreciable.
EDIT
Using the suggested answer I am getting error as
events.js:72
throw er; // Unhandled 'error' event
^
TypeError: Invalid non-string/buffer chunk
at validChunk (_stream_writable.js:150:14)
at WriteStream.Writable.write (_stream_writable.js:179:12)
at /var/www/html/experimentation/nodejs/first.js:18:20
at Array.forEach (native)
at forEachLine (/var/www/html/experimentation/nodejs/first.js:8:60)
at IncomingMessage.<anonymous> (/var/www/html/experimentation/nodejs/first.js:17:18)
at IncomingMessage.EventEmitter.emit (events.js:95:17)
at IncomingMessage.<anonymous> (_stream_readable.js:736:14)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
Thanks
See proxy_stream.js
function forEachLine(chunks, callback) {
var buffer = chunks.join("")
buffer.substr(0, buffer.lastIndexOf("\n")).split("\n").forEach(callback)
return buffer.substr(buffer.lastIndexOf("\n") + 1).split("\n")
}
The link to the repo was on the first slide.
EDIT BY LET's CODE FOR ERROR MESSAGE
Came to know the actual issue now .
I was using nod v0.10 and it is buggy in getting the streams so I was getting the error. Downgraded to v0.8 and same code is working perfect .

Resources