#StackBounty: #node.js #http #dump Dump the whole HTTP communication as raw data in nodejs

Bounty: 50

I wonder if it’s possible to dump the whole HTTP request+response as it goes over the wire.

I don’t want to get the method, the path info, the query string, the headers, the cookies, the body and whatever. I could in theory assemble the raw data myself, but then I’d need no HTTP library, right?

Moreover, I want to dump exactly the bytes that go over the wire.

I want the raw data like in this image

http raw data

taken from this page.

I’m using current node.js as a HTTP client with request. It’s plain HTTP (no HTTPS).

Installing a proxy in node.js would be an option, but I don’t insist on a library. I could imagine to wrap the socket read and write functions, but I can’t see how to get to the socket used.


Get this bounty!!!

#StackBounty: #node.js #npm Npm global not being used?

Bounty: 50

So I’ve previously had some npm issues and followed some stackoverflow/github issues commands to attempt to fix them. Which worked… Kinda. Everything was fine until recently when I realised that my global npm packages are not actually being read/used.

So whenever I did a npm -g install xxx, or an update. It updates the global folder alright, but it’s just not being used. So I went to dig a little and found this.

Where npm is reading/using from

usr/local/lib/node_modules

Where the global is installing to

Users/Me/.npm-global/lib/node_modules

So whenever I did a -g check for my modules and stuff, everything is good and updated, but when I actually try to run anything, the npm command uses the one in the older folders. So basically I can’t update or install anything using -g. And nothing goes into the old folder which is the one being actually used.

How do I resolve this? Was some linking or profile got screwed?


Get this bounty!!!

#StackBounty: #node.js #artillery Artillery NodeJS Cluster Performance Benchmarking Issue

Bounty: 50

I am using Artillery.IO with NodeJS to measure performance. NodeJS is running at my localhost and I am also running Artillery from same machine.

When I run Artillery with this script.

{
  "config": {
      "target": "http://localhost:3000/",
      "phases": [
          {
             "duration": 10,
             "arrivalRate": 2
          }
      ]
  },
  "scenarios": [
    {
       "flow": [
           {"get": {"url": "/"}}
       ]
    }
  ]
} 

CASE 1: NodeJS running with single cluster.

RESULT :
==============================

Scenarios launched: 20

Scenarios completed: 20

Requests completed: 20

RPS sent: 2

Request latency:

min: 1.1

max: 4.6

median: 1.2

p95: 3.3

p99: 4.6

Scenario counts:

0: 20 (100%)

Codes:

404: 20

==============================

CASE 2 : NodeJS running with 4 clusters.

RESULT :
=============================

Scenarios launched: 20

Scenarios completed: 20

Requests completed: 20

RPS sent: 2

Request latency:

min: 1.2

max: 4.7

median: 1.4

p95: 3.2

p99: 4.7

Scenario counts:

0: 20 (100%)

Codes:

404: 20

==========================

My Node Server.js code is :

var http    = require('http');
var express = require('express');
var os = require('os');
var app     = express();
var server  = http.createServer(app);
var io = require('socket.io')(server);

app.get('/', function(req, res,next) {
    for(let i = 0; i < 10000000000; i++) {

    }
    console.log("Get Request received");
    res.send(new Date());
});

server.listen(3000, function() {
    console.log("Server is running in Port 3000");
});

My Question is why there is no improvement when I run NodeJS in cluster mode ?

Does it have to do something with the fact that both NodeJS and Artillery running on same system or It is server.js code that is not correctly written to measure performance.

Please help.


Get this bounty!!!

#StackBounty: #javascript #performance #node.js #interview-questions #web-services Find currency exchange rates

Bounty: 50

Description:

Design a service to fetch exchange rate from a remote resource and
then calculate the exchange rate for each currency pair.

The remote resource contains the exchange rates of each currency in
Euro.

This is an interview assignment and I came up with an easy solution.

index.js

'use strict';

const joi = require('joi');

const api = require('./api');
const Exchange = require('./exchange');
const xmlParser = require('./parse-xml');

const schema = joi
  .object({
    source: joi.string().required().min(3).max(3).example('EUR'),
    target: joi.string().required().min(3).max(3).example('GBP')
  })
  .unknown()
  .required();

const defaults = {
  timeout: 1000 // 1 sec
};

const exchange = async (pair, options = {}) => {
  options = Object.assign({}, defaults, options);
  const {source, target} = joi.attempt(pair, schema);

  const {requestApi = api, parser = xmlParser} = options;

  const exchange = new Exchange(requestApi, parser, options);
  const rate = await exchange.convert({source, target});
  return {source, target, rate};
};

module.exports = exchange;

exchange.js

'use strict';

const URL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';

class Exchange {
  constructor(api, parser, options = {}) {
    this.api = api;
    this.options = options;
    this.parser = parser;
  }

  async convert({source, target}) {
    if (!this.xml) {
      await this.fetch();
      this.euroToAll = this.parser(this.xml);
    }
    const euroToSource = this.euroToAll[source];
    const euroToTarget = this.euroToAll[target];
    return exchange(euroToSource, euroToTarget);
  }

  async fetch() {
    const response = await this.api.fetch(URL, this.options);
    this.xml = response.body || '';
  }
}

function exchange(from, to) {
  return round(parseFloat(to) / parseFloat(from));
}

function round(result, digits = 4) {
  return Math.round(result * (10 ** digits)) / (10 ** digits);
}

module.exports = Exchange;

parse-xml.js

'use strict';

const xmldoc = require('xmldoc');
const debug = require('debug')('exchange-rate:parse');

const currencies = require('./currencies');

const parse = xml => {
  const doc = new xmldoc.XmlDocument(xml);
  const cube = doc.childNamed('Cube').childNamed('Cube');

  const rates = currencies.reduce(
    (accumulator, currency) => {
      const exchange = cube.childWithAttribute('currency', currency);
      if (exchange) {
        const {rate} = exchange.attr;
        accumulator[currency] = rate;
      } else {
        debug(`Node not found for currency: ${currency}`);
      }
      return accumulator;
    },
    {}
  );
  // Add EUR rate to make it consistent
  rates.EUR = '1.0';
  return rates;
};

module.exports = parse;

api.js

'use strict';

const got = require('got');

module.exports = {
  async fetch(url, options = {}) {
    return got(url, options);
  }
};

Questions:

  1. What if in future we need to add different providers with different representation? How can I make it more flexible and keep the core logic decoupled?
  2. I am also curious to know if the design of the api from the client perspective is good or it can be improved.
  3. In NodeJs we can define dependency via require but I found it difficult to mock them for testing so, I have tried at couple of places to pass dependencies via arguments, is this fine?


Get this bounty!!!

#StackBounty: #node.js #npm #progress #child-process #spawn Trigger a parallel node progress bar on spawn sync child process

Bounty: 50

I am basically trying to build my own wrapper around npm install package functionality.

I have successfully written the install function wrapper but now I want to trigger a progress bar when I am sync spawning a child process which basically installs npm modules, here is my code below

const spawn = require('cross-spawn');
const ProgressBar = require('progress');
const chalk = require('chalk');

public static installPkgs =
  (des: any, packages: string[], opts: any) => {
    const args: string[] = [];
    if (opts.save) {
      args.push('-S');
    }
    if (opts.saveDev) {
      args.push('-D');
    }
    if (opts.global) {
      args.push('-g');
    }
    const cliArgs: string[] = ['i'].concat(args, packages);

    if (typeof des === 'string') {
      process.stdout.write('n' + chalk.yellow(des) + 'n');
    } else {
      process.stdout.write('');
    }

    const barOpts =  { 
                      width: 20, 
                      total: packages.length, 
                      clear: true
                     };

    const bar = new ProgressBar(' uploading [:bar] :percent :etas', barOpts);

    bar.tick(0);

    const bat = spawn.sync('npm', cliArgs, {stdio:'pipe'});

    bar.tick(bat.stdout.toString().length);
}

Now my problems are –

  • I am not sure whether to use packages.length for the total property of progress bar tick completion
  • Since the spawning is happening in synchronous way, the progress bar is triggered after that process how can I trigger it as soon as the npm modules are being downloaded?

Please help me guide in the right direction on how to achieve the above!


Get this bounty!!!

#StackBounty: #node.js #mongodb #mongoose #mongodb-query Validate a mongodb query syntax programmatically

Bounty: 50

I have an API method where the user can pass in their own query. The field in the collection is simply ns, so the user might pass something like:

v.search = function(query: Object){
   // query => {ns:{$in:['foo','bar',baz]}}  // valid!

   // query => {ns:{$in:{}}}  // invalid!

   // query => {ns:/foo/}     // valid!

});

is there some way to do this, like a smoke test that can fail queries that are obviously wrong?

most likely MongoDB libraries would export this functionality, but they probably just use their DB to do it. I am looking to validate the query before sending it to the DB.


Get this bounty!!!

#StackBounty: #node.js #sockets #websocket #socket.io Connect to a socket.io server from a Node.js server using the 'ws' package

Bounty: 100

I have a Node.js server which utilizes the popular ws package for using web sockets. I’d like to use this library to connect to an third party server which is running socket.io.

If I were to use socket.io on my server, the connection code would be something like this:

const socket = socketIo('https://api.example.com/1.0/scores')

I’ve attempted to connect to the same service using the ws package, and modifying the url:

const wsClient = new WebSocket('wss://api.example.com/1.0/scores');

but this results in the following:

Error: Unexpected server response: 200

Question:
What needs to be done to connect to a third party server running socket.io from a server running the ws package?


Get this bounty!!!

#StackBounty: #node.js #sockets #websocket #socket.io Connect to a socket.io server from a Node.js server using the 'ws' package

Bounty: 100

I have a Node.js server which utilizes the popular ws package for using web sockets. I’d like to use this library to connect to an third party server which is running socket.io.

If I were to use socket.io on my server, the connection code would be something like this:

const socket = socketIo('https://example.com/1.0/scores')

I’ve attempted to connect to the same service using the ws package, and modifying the url:

const wsClient = new WebSocket('wss://example.com/1.0/scores');

but this results in the following:

Error: Unexpected server response: 200

Question:
What needs to be done to connect to a third party server running socket.io from a server running the ws package?


Get this bounty!!!

#StackBounty: #javascript #node.js #firebase #express #google-cloud-functions Firebase Functions : How to store simple cookies to remem…

Bounty: 50

I’m trying to just remeber a user returning to the site and count views, only after 5 minutes. I did this, works when using Firebase Serve, but the cookies are not being stored after deploy.

Somewhere up in the app.

app.use(cookieSession({ name: 'session', keys: ['utl__key_s1', 'utl__key_s2'] }));

Trying to check if session exists and isn’t more than 5 min old.

function sessionExists(req) {
    const t = req.session.viewTime;

    if (t == null) {
        req.session.viewTime = + new Date();
        return false;
    }

    const fiveMinutes = ((1000) * 60) * 5;
    if (((+new Date()) - t) > fiveMinutes) {
        req.session = null;
        return false;
    }

    return true;
}

Then I find out the issue is that we have to use __session. That I don’t really understand. Can I get an example with context to the above code examples?


Get this bounty!!!

#StackBounty: #node.js #macos #stdin #pid #file-descriptor Pass handle down pipeline

Bounty: 50

Say I have

node foo.js | node bar.js

is there a way to pass a handle on foo’s stdin to bar.js?

I have a rare case where I’d like to communicate backwards in the pipeline.

At the least I know that I could send node bar.js the pid of node foo.js. Given that pid, on *nix, I should be able to write to foo’s stdin using:

/proc/<pid>/fd/0

but is there a way to do the same on MacOS?


Get this bounty!!!