Saturday, 1 April 2017

nodejs - change contents of zip file without re-generating the whole archive

I'm using node-zip (which uses JSZip under the hood). I need to modify the contents of a zip file, and I'd love to be able to modify it without generating the entire zip again, because it can take a long time for large archives. Here's an example:

var zip = new JSZip()

// Add some files to the zip
zip.file('file1', "file 1 contents\n")
zip.file('file2', "file 2 contents\n")
zip.file('file3', "file 3 contents\n")

// Generate the zip file
buffer = zip.generate()

// Make some changes
zip.file('file1', "changed file contents\n")

// Now I have to generate the entire zip again to get the buffer
buffer = zip.generate()

How can I do something like

updatedBuffer = zip.updateFile(buffer, 'file1', 'changed file contents\n')

Where I get an updated archive buffer but I only have to spend CPU cycles updating the one file



via ZECTBynmo

node.js, for-loops, and twitter API

I am trying to only fetch the text of the tweets for a twitter bot I am working on (keep in mind twitter's API docs are worse than Oracles, which I never thought that was possible, anyway). I was using the following link to make my bot https://community.risingstack.com/node-js-twitter-bot-tutorial/

I assume that it is within their randomTweet function where they get the index from.

function ranDom (arr) {  
  var index = Math.floor(Math.random()*arr.length);
  return arr[index];
};

and later in their code they would reference it using

Twitter.post('favorites/create', {id: randomTweet.id_str}

I am trying to simply access the text using a regular foreach loop (seeing as, as far as I am aware, node.js has no concept of an indexed for loop) as follows:

var tweet = data.statuses;



    for(var result in tweet) {
        console.log(result.id_str);
    }  

However, all I am getting is undefined, and I am not certain as to why since for them it worked fine when they passed in a specific index.



via SomeStudent

How to debug firebase web app locally

I need to build a firebase function, so i am trying to set up a developer environment where i can test and debug a firebase function.

From the examples i saw, it all ends up setting up a nodejs node. Because i can't run the node directly, i need to use firebase CLI to start it, so i can't use any local debug.

The best thing i can come with is to put the code in html page and use the browser native debug, which is ok, but database handlers do not work.

Debugging on the server, looks to complicated for short cycles.

Is there a better way to debug it locally?



via ByteArtisan

Stuck in multiline edit mode when importing module in Node.js REPL

I'm on node v7.5.0. I run node and then:

> import moment from 'moment';
... moment();
...

I get stuck in multiline mode as you can see. How do I use import in Node REPL?



via Liron Yahdav

How to combine multiple npm modules?

I like to learn by doing, and I would like to learn to use npm modules for more flexible and rapid coding - not "monolithical.

I have been using a graph visualisation - vivagraph, which can be assembled with npm modules - https://github.com/anvaka/ngraph.

Could you show an example explaining how to compile npm modules into one bundle (as an example, a few ngraph modules into a custom vivagraph bundle) and which tools to use for a "correct" approach in programming and extension for writing new modules?

I have learnt using JS "monolithically" by myself thanks to examples on the graph visualisation library, and I am bit overwelhmed by require, browserify, npm.. and how to combine them to make coding simpler.

My typical need is using JS for front-end only (not as a server).



via user305883

node.js needle post not doing its job

Why doesn't Needle do its job here? It just seems to hang forever waiting to time out. Eventually I will get the

Ready-o, friend-o

var express = require('express');
var router = express.Router();
var needle = require('needle');

router.get('/', function (req, resp) {
    console.log("nee.get");

    var data = { key: 'value' };
    needle.post('http://localhost/nee', data, { json: true })
        .on('done', function () {
            console.log('Ready-o, friend-o.');
            resp.sendStatus(200);
        })
});


router.post('/', function (req, resp, next) {
    console.log(req.body.key);
    res.sendStatus(200);
});


module.exports = router;



via Steve

How to define webpack dynamic context where node.js code try to find a filename with fallback?

I try to webpack a node.js module and stumbling on some code where we try to require a JSON file with a fallback to a file that will exist at all events. The following code is a hard nut for webpack.

var tryRequire = function(filename){
  var result = null;
  if (fs.existsSync(filename)){
    result = require(filename);
  }
  return result;
}

var getI18nStorage = function(culture){
  var cultureParts = culture.toLowerCase().split("-");
  var dir = path.join(__dirname, "../../i18n/address");
  var fn = cultureParts[0] + ".json";
  var filename = path.join(dir, fn);
  var storage = tryRequire(filename);
  if (!storage){
    fn = cultureParts[1] + ".json";
    filename = path.join(dir, fn);
    storage = tryRequire(filename);
    if (!storage){
      fn = "us.json";
      filename = path.join(dir, fn);
      storage = tryRequire(filename);
/*      if (!storage){
        console.log(culture, "nothing found");
        process.exit(1)
      }*/
    }
  }

  return storage;
}

Currently packing var c = getI18nStorage("en-GB"); results in !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; which is understandable :o)

I tried to learn from https://github.com/webpack/webpack/tree/master/examples/require.context but could not transfer the technique to my problem. I think I need to know 2 things:

  • How to manualy add requiered modules to webpack if they cannot be resolved programaticaly?
    • I tried to add context: path.join(__dirname, "../lib/i18n"), to webpack config but this does not include data to bundled output.
  • Is there a way to help webpack to understand this node.js filesystem strategy to try to find a required module?


via Stephan Ahlf