js you should

know about

npm + browserify

  • npm
  • patterns
  • browserify
  • write some code
  • answer questions
npm

npm

everything is a package, not just js for node

*not to be confused with www.npm.org

the registry

  • versioned metadata + tarball
  • couchdb

npm -h

  • command-line tool
  • package manager and runner

→ package.json

  • basics
    • name, version, description
  • special
    • dependencies, bin, scripts

virtualenv by default

  • globals = bad
  • each dep has it's own node_modules

npm home express

  • → look up the express package, open browser

npm ls

  • → list installed packages

npm install -s toTitlecase

  • → install mocha, add it to deps
alias yay="npm test &&\
npm version patch &&\
npm publish &&\
git push origin --tags"
patterns

you'll see a lot of

  • require('thing')
  • module.exports = function(){};
  • function(err, res)

require('thing')

  • synchronous
  • require('thing'); →
    • stdlib? node module?
  • require('./thing'); →
    • ./thing.js? ./thing/index.js?

require('thing')

  • run in a sandbox, `module`
  • define the public api

magic

module.exports = function(){};

  • how do i use this thing?
  • require('thing').toString()

function(err, res)

  • always be async'ing.
  • in your face if something fails

example

  var fs = require('fs');
  // doublin` - read a README with double vision
  module.exports = function(opts, fn){
    fs.readFile(opts.path || './README.md', function(err, buf){
      if(err) return fn(err);
      fn(null, Buffer.concat([buf, buf]);
    });
  };

but you ugly

  var fs = require('fs')
  module.exports = *function(opts){
    var buf = yield read(opts.path || './README.md');
    return Buffer.concat([buf, buf]);
  };

--harmony

  • soon we'll have generators with jetpacks
  • npm home co

browserify

  • made by wizards/hobbits
  • just need 1 code style
  • lots of hammers already on npm

browserify.bundle

  1. parse ast of entry point
  2. build dag
  3. source dependencies
  4. serialize dag + sources

hi front-end developers.

  • scope enforced for you
  • dependencyhelllollloll
  • it's ok to split your code into lots of files!

pre-bundled deps must die

npm install --save jquery\
  https://github.com/twbs/bootstrap/archive/v3.1.1.tar.gz;
require('bootstrap/js/dropdown.js');
window.jQuery = require('jquery');

too much to cover in one sitting

  • transforms jadeify, es6ify
  • shims → node stdlib api's for browsers
    • (fs, crypto, etc)
modules

Why modules?

  • business in one place
  • test and forget
  • reuse all over the place
  • weird and interesting things will happen!

two cases

  • want to use that code over there
  • someone else could use this

over there

  • namespace_string.cpp
  • want logic in mongoscope
  • workflow: copy paste + find all and replace
// npm install mongodb-ns
var ns = require('mongodb-ns);
ns('local.oplog.rs').oplog // true
ns('local.oplog.$main').oplog // true
ns('$Dataface$.users').validDatabase // false

console.log(ns('canadian-things.songs-aboot-bacon'));
// {ns: 'canadian-things.songs-aboot-bacon',
//   dotIndex: 15,
//   database: 'canadian-things',
//   collection: 'songs-aboot-bacon',
//   system: false,
//   oplog: false,
//   command: false,
//   special: false,
//   normal: true,
//   validDatabaseName: true,
//   validCollectionName: true,
//   databaseHash: 23620443216}

"here use this"

  • made some business for security
  • scattered across models and jade mixins
  • "hey we're adding this to mms"
  • workflow: README first then populate API blocks

mongodb-security.md

treasure hunt

  • not enough time to run in kernel today :(
  • MongoRunner....
  • drivers, mtools, everywhere
thanks
 
⇧⌘F
⌘F1