SHA.js: Streamable SHA Hashes in Pure Javascript

sha.js

Node style SHA on pure JavaScript.

var shajs = require('sha.js')

console.log(shajs('sha256').update('42').digest('hex'))
// => 73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049
console.log(new shajs.sha256().update('42').digest('hex'))
// => 73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049

var sha256stream = shajs('sha256')
sha256stream.end('42')
console.log(sha256stream.read().toString('hex'))
// => 73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049

supported hashes

sha.js currently implements:

  • SHA (SHA-0) -- legacy, do not use in new systems
  • SHA-1 -- legacy, do not use in new systems
  • SHA-224
  • SHA-256
  • SHA-384
  • SHA-512

Not an actual stream

Note, this doesn't actually implement a stream, but wrapping this in a stream is trivial. It does update incrementally, so you can hash things larger than RAM, as it uses a constant amount of memory (except when using base64 or utf8 encoding, see code comments).

Acknowledgements

This work is derived from Paul Johnston's A JavaScript implementation of the Secure Hash Algorithm.

Download Details:

Author: Crypto-browserify
Source Code: https://github.com/crypto-browserify/sha.js 
License: View license

#javascript #browserify #crypto #node 

SHA.js: Streamable SHA Hashes in Pure Javascript
Lawrence  Lesch

Lawrence Lesch

1664391000

Drag-drop: HTML5 Drag & Drop for Humans

drag-drop

HTML5 drag & drop for humans

In case you didn't know, the HTML5 drag and drop API is a total disaster! This is an attempt to make the API usable by mere mortals.

Features

  • simple API
  • supports files and directories
  • excellent browser support (Chrome, Firefox, Safari, Edge)
  • adds a drag class to the drop target on hover, for easy styling!
  • optionally, get the file(s) as a Buffer (see buffer)

install

npm install drag-drop

This package works in the browser with browserify. If you do not use a bundler, you can use the standalone script directly in a <script> tag.

usage

const dragDrop = require('drag-drop')

dragDrop('#dropTarget', (files, pos, fileList, directories) => {
  console.log('Here are the dropped files', files) // Array of File objects
  console.log('Dropped at coordinates', pos.x, pos.y)
  console.log('Here is the raw FileList object if you need it:', fileList)
  console.log('Here is the list of directories:', directories)
})

Another handy thing this does is add a drag class to the drop target when the user is dragging a file over the drop target. Useful for styling the drop target to make it obvious that this is a drop target!

complete example

const dragDrop = require('drag-drop')

// You can pass in a DOM node or a selector string!
dragDrop('#dropTarget', (files, pos, fileList, directories) => {
  console.log('Here are the dropped files', files)
  console.log('Dropped at coordinates', pos.x, pos.y)
  console.log('Here is the raw FileList object if you need it:', fileList)
  console.log('Here is the list of directories:', directories)

  // `files` is an Array!
  files.forEach(file => {
    console.log(file.name)
    console.log(file.size)
    console.log(file.type)
    console.log(file.lastModifiedDate)
    console.log(file.fullPath) // not real full path due to browser security restrictions
    console.log(file.path) // in Electron, this contains the actual full path

    // convert the file to a Buffer that we can use!
    const reader = new FileReader()
    reader.addEventListener('load', e => {
      // e.target.result is an ArrayBuffer
      const arr = new Uint8Array(e.target.result)
      const buffer = new Buffer(arr)

      // do something with the buffer!
    })
    reader.addEventListener('error', err => {
      console.error('FileReader error' + err)
    })
    reader.readAsArrayBuffer(file)
  })
})

get files as buffers

If you prefer to access file data as Buffers, then just require drag-drop like this:

const dragDrop = require('drag-drop/buffer')

dragDrop('#dropTarget', files => {
  files.forEach(file => {
    // file is actually a buffer!
    console.log(file.readUInt32LE(0))
    console.log(file.toJSON())
    console.log(file.toString('hex')) // etc...

    // but it still has all the normal file properties!
    console.log(file.name)
    console.log(file.size)
    console.log(file.type)
    console.log(file.lastModifiedDate)
  })
})

detect drag-and-dropped text

If the user highlights text and drags it, we capture that as a separate event. Listen for it like this:

const dragDrop = require('drag-drop')

dragDrop('#dropTarget', {
  onDropText: (text, pos) => {
    console.log('Here is the dropped text:', text)
    console.log('Dropped at coordinates', pos.x, pos.y)
  }
})

detect dragenter, dragover and dragleave events

Instead of passing just an ondrop function as the second argument, instead pass an object with all the events you want to listen for:

const dragDrop = require('drag-drop')

dragDrop('#dropTarget', {
  onDrop: (files, pos, fileList, directories) => {
    console.log('Here are the dropped files', files)
    console.log('Dropped at coordinates', pos.x, pos.y)
    console.log('Here is the raw FileList object if you need it:', fileList)
    console.log('Here is the list of directories:', directories)
  },
  onDropText: (text, pos) => {
    console.log('Here is the dropped text:', text)
    console.log('Dropped at coordinates', pos.x, pos.y)
  },
  onDragEnter: (event) => {},
  onDragOver: (event) => {},
  onDragLeave: (event) => {}
})

You can rely on the onDragEnter and onDragLeave events to fire only for the drop target you specified. Events which bubble up from child nodes are ignored so that you can expect a single onDragEnter and then a single onDragLeave event to fire.

Furthermore, neither onDragEnter, onDragLeave, nor onDragOver will fire for drags which cannot be handled by the registered drop listeners. For example, if you only listen for onDrop (files) but not onDropText (text) and the user is dragging text over the drop target, then none of the listed events will fire.

remove listeners

To stop listening for drag & drop events and remove the event listeners, just use the cleanup function returned by the dragDrop function.

const dragDrop = require('drag-drop')

const cleanup = dragDrop('#dropTarget', files => {
  // ...
})

// ... at some point in the future, stop listening for drag & drop events
cleanup()

support pasting files from the clipboard

To support users pasting files from their clipboard, use the provided processItems() function to process the DataTransferItemList from the browser's native 'paste' event.

document.addEventListener('paste', event => {
  dragDrop.processItems(event.clipboardData.items, (err, files) => {
    // ...
  })
})

a note about file:// urls

Don't run your app from file://. For security reasons, browsers do not allow you to run your app from file://. In fact, many of the powerful storage APIs throw errors if you run the app locally from file://.

Instead, start a local server and visit your site at http://localhost:port.

Live demo

See https://instant.io.

Download Details:

Author: Feross
Source Code: https://github.com/feross/drag-drop 
License: MIT license

#javascript #browserify #browser #html5 

Drag-drop: HTML5 Drag & Drop for Humans

Isomorphic-fetch: Isomorphic WHATWG Fetch API, for Node & Browserify

isomorphic-fetch

Fetch for node and Browserify. Built on top of GitHub's WHATWG Fetch polyfill.

Warnings

  • This adds fetch as a global so that its API is consistent between client and server.

For ease-of-maintenance and backward-compatibility reasons, this library will always be a polyfill. As a "safe" alternative, which does not modify the global, consider fetch-ponyfill.

Why Use Isomorphic Fetch

The Fetch API is currently not implemented consistently across browsers. This module will enable you to use fetch in your Node code in a cross-browser compliant fashion. The Fetch API is part of the Web platform API defined by the standards bodies WHATWG and W3C.

Installation

NPM

npm install --save isomorphic-fetch

Bower

bower install --save isomorphic-fetch

Usage

require('isomorphic-fetch');

fetch('//offline-news-api.herokuapp.com/stories')
    .then(function(response) {
        if (response.status >= 400) {
            throw new Error("Bad response from server");
        }
        return response.json();
    })
    .then(function(stories) {
        console.log(stories);
    });

Alternatives

Download Details:

Author: Matthew-andrews
Source Code: https://github.com/matthew-andrews/isomorphic-fetch 
License: MIT license

#javascript #node #browserify 

Isomorphic-fetch: Isomorphic WHATWG Fetch API, for Node & Browserify
Gordon  Taylor

Gordon Taylor

1660682340

Proxyquireify: Browserify >= V2 Version Of Proxyquire

proxyquireify 

browserify >= v2 version of proxyquire.

Proxies browserify's require in order to make overriding dependencies during testing easy while staying totally unobstrusive. To run your tests in both Node and the browser, use proxyquire-universal.

Features

  • no changes to your code are necessary
  • non overriden methods of a module behave like the original
  • mocking framework agnostic, if it can stub a function then it works with proxyquireify
  • "use strict" compliant
  • automatic injection of require calls to ensure the module you are testing gets bundled

Installation

npm install proxyquireify

To use with browserify < 5.1 please npm install proxyquireify@0.5 instead. To run your tests in PhantomJS, you may need to use a shim.

Example

foo.js:

var bar = require('./bar');

module.exports = function () {
  return bar.kinder() + ' ist ' + bar.wunder();
};

foo.test.js:

var proxyquire = require('proxyquireify')(require);

var stubs = { 
  './bar': { 
      wunder: function () { return 'wirklich wunderbar'; }
    , kinder: function () { return 'schokolade'; }
  }
};

var foo = proxyquire('./src/foo', stubs);

console.log(foo()); 

browserify.build.js:

var browserify = require('browserify');
var proxyquire = require('proxyquireify');

browserify()
  .plugin(proxyquire.plugin)
  .require(require.resolve('./foo.test'), { entry: true })
  .bundle()
  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));

load it in the browser and see:

schokolade ist wirklich wunderbar

With Other Transforms

If you're transforming your source code to JavaScript, you must apply those transforms before applying the proxyquireify plugin:

browserify()
  .transform('coffeeify')
  .plugin(proxyquire.plugin)
  .require(require.resolve('./test.coffee'), { entry: true })
  .bundle()
  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));

proxyquireify needs to parse your code looking for require statements. If you require anything that's not valid JavaScript that acorn can parse (e.g. CoffeeScript, TypeScript), you need to make sure the relevant transform runs before proxyquireify.

API

proxyquire.plugin()

proxyquireify functions as a browserify plugin and needs to be registered with browserify like so:

var browserify = require('browserify');
var proxyquire = require('proxyquireify');

browserify()
  .plugin(proxyquire.plugin)
  .require(require.resolve('./test'), { entry: true })
  .bundle()
  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));

Alternatively you can register proxyquireify as a plugin from the command line like so:

browserify -p proxyquireify/plugin test.js > bundle.js

proxyquire.browserify()

Deprecation Warning

This API to setup proxyquireify was used prior to browserify plugin support.

It has not been removed yet to make upgrading proxyquireify easier for now, but it will be deprecated in future versions. Please consider using the plugin API (above) instead.


To be used in build script instead of browserify(), autmatically adapts browserify to work for tests and injects require overrides into all modules via a browserify transform.

proxyquire.browserify()
  .require(require.resolve('./test'), { entry: true })
  .bundle()
  .pipe(fs.createWriteStream(__dirname + '/bundle.js'));

proxyquire(request: String, stubs: Object)

  • request: path to the module to be tested e.g., ../lib/foo
  • stubs: key/value pairs of the form { modulePath: stub, ... }
    • module paths are relative to the tested module not the test file
    • therefore specify it exactly as in the require statement inside the tested file
    • values themselves are key/value pairs of functions/properties and the appropriate override
var proxyquire =  require('proxyquireify')(require);
var barStub    =  { wunder: function () { 'really wonderful'; } };

var foo = proxyquire('./foo', { './bar': barStub })

Important Magic

In order for browserify to include the module you are testing in the bundle, proxyquireify will inject a require() call for every module you are proxyquireing. So in the above example require('./foo') will be injected at the top of your test file.

noCallThru

By default proxyquireify calls the function defined on the original dependency whenever it is not found on the stub.

If you prefer a more strict behavior you can prevent callThru on a per module or per stub basis.

If callThru is disabled, you can stub out modules that weren't even included in the bundle. Note, that unlike in proxquire, there is no option to prevent call thru globally.

// Prevent callThru for path module only
var foo = proxyquire('./foo', {
    path: {
      extname: function (file) { ... }
    , '@noCallThru': true
    }
  , fs: { readdir: function (..) { .. } }
});

// Prevent call thru for all contained stubs (path and fs)
var foo = proxyquire('./foo', {
    path: {
      extname: function (file) { ... }
    }
  , fs: { readdir: function (..) { .. } }
  , '@noCallThru': true
});

// Prevent call thru for all stubs except path
var foo = proxyquire('./foo', {
    path: {
      extname: function (file) { ... }
    , '@noCallThru': false
    }
  , fs: { readdir: function (..) { .. } }
  , '@noCallThru': true
});

More Examples

Download Details:

Author: Thlorenz
Source Code: https://github.com/thlorenz/proxyquireify 
License: MIT license

#javascript #browserify 

Proxyquireify: Browserify >= V2 Version Of Proxyquire
Gordon  Taylor

Gordon Taylor

1657873620

Browserify/wzrd.in: Browserify As A Service

browserify-as-a-service

What just happened?

Well, in this case, since someone has visited this link before you, the file was cached with leveldb. But if you were to try and grab a bundle that nobody else has tried to grab before, what would happen is this:

  • The module gets pulled down from npm and installed
  • The module gets browserified as a standalone bundle
  • The module gets sent to you, piping hot
  • The module gets cached so that you don't have to wait later on

API

There are a few API endpoints:

GET /bundle/:module

Get the latest version of :module.

GET /bundle/:module@:version

Get a version of :module which satisfies the given :version semver range. Defaults to latest.

GET /debug-bundle/:module

GET /debug-bundle/:module@:version

The same as the prior two, except with --debug passed to browserify.

GET /standalone/:module

GET /standalone/:module@:version

In this case, --standalone is passed to browserify.

GET /debug-standalone/:module

GET /debug-standalone/:module@:version

Both --debug and --standalone are passed to browserify!

POST /multi

POST a body that looks something like this:

{
  "options": {
    "debug": true
  },
  "dependencies": {
    "concat-stream": "0.1.x",
    "hyperstream": "0.2.x"
  }
}

"options" is where you get to set "debug", "standalone", and "fullPaths". Usually, in this case, you'll probably only really care about debug. If you don't define "options", it will default to { "debug": false, "standalone": false, "fullPaths": false }.

What you get in return looks something like this:

HTTP/1.1 200 OK
X-Powered-By: Express
Location: /multi/48GOmL0XvnRZn32bkpz75A==
content-type: application/json
Date: Sat, 22 Jun 2013 22:36:32 GMT
Connection: keep-alive
Transfer-Encoding: chunked

{
  "concat-stream": {
    "package": /* the concat-stream package.json */,
    "bundle": /* the concat-stream bundle */
  },
  "hyperstream": {
    "package": /* the hyperstream package.json */,
    "bundle": /* the hyperstream bundle */
  }
}

The bundle gets permanently cached at /multi/48GOmL0XvnRZn32bkpz75A== for future GETs.

GET /multi/:existing-bundle

If you saved the Location url from the POST earlier, you can just GET it instead of POSTing again.

GET /status/:module

GET /status/:module@:version

Get information on the build status of a module. Returns build information for all versions which satisfy the given semver (or latest in the event of a missing semver).

Blobs generally look something like this:

HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Content-Length: 109
ETag: "-9450086"
Date: Sun, 26 Jan 2014 08:05:59 GMT
Connection: keep-alive

{
  "module": "concat-stream",
  "builds": {
    "1.4.1": {
      "ok": true
    }
  }
}

The "module" and "builds" fields should both exist. Keys for "builds" are the versions. Properties:

  • "ok": Whether the package has last built or not
  • "error": If the package was built insuccessfully ("ok" is false), this property will contain information about the error

Versions which have not been built will not be keyed onto "builds".

Heroku Installation

browserify-cdn is ready to run on Heroku:

heroku create my-browserify-cdn
git push heroku master
heroku ps:scale web=1

Docker Installation

You can build and run an image doing the following:

docker build -t "wzrd.in" /path/to/wzrd.in
docker run -p 8080:8080 wzrd.in

Keep in mind that a new deploy will wipe the cache.

Places

Quick Start

Try visiting this link:

/standalone/concat-stream@latest

Also, wzrd.in has a nice url generating form.

Author: Browserify
Source Code: https://github.com/browserify/wzrd.in 
License: MIT license

#javascript #service #browserify 

Browserify/wzrd.in: Browserify As A Service
Reid  Rohan

Reid Rohan

1657614308

Browserify-fs: Fs for The Browser using Level-filesystem & Browserify

browserify-fs

fs for the browser using level-filesystem, level.js and browserify

npm install browserify-fs

Usage

To use simply require it and use it as you would fs

var fs = require('browserify-fs');

fs.mkdir('/home', function() {
    fs.writeFile('/home/hello-world.txt', 'Hello world!\n', function() {
        fs.readFile('/home/hello-world.txt', 'utf-8', function(err, data) {
            console.log(data);
        });
    });
});

You can also make browserify replace require('fs') with browserify-fs using

browserify -r fs:browserify-fs

Using the replacement you can browserify modules like tar-fs and mkdirp!

Checkout level-filesystem and level.js to see which browsers are supported

Author: Mafintosh
Source Code: https://github.com/mafintosh/browserify-fs 
License: MIT

#javascript #node #browserify 

Browserify-fs: Fs for The Browser using Level-filesystem & Browserify
Hermann  Frami

Hermann Frami

1656103020

Serverless Optimize Plugin

Serverless Optimize Plugin

Bundle with Browserify, transpile and minify with Babel automatically to your NodeJS runtime compatible JavaScript.

This plugin is a child of the great serverless-optimizer-plugin. Kudos!

Requirements:

  • Serverless v1.12.x or higher.
  • AWS provider and nodejs4.3/6.10/8.10/10.x/12.x/14.x runtimes

Setup

Install via npm in the root of your Serverless service:

npm install serverless-plugin-optimize --save-dev
  • Add the plugin to the plugins array in your Serverless serverless.yml:
plugins:
  - serverless-plugin-optimize
  • Set your packages to be built individually to have smaller packages:
package:
  individually: true
  • All done! Optimize will run on SLS deploy and invoke local commands

Options

Configuration options can be set globally in custom property and inside each function in optimize property. Function options overwrite global options.

Global

  • debug (default false) - When debug is set to true it won't remove prefix folder and will generate debug output at the end of package creation.
  • exclude (default ['aws-sdk']) - Array of modules or paths that will be excluded.
  • extensions (default ['.js', '.json']) - Array of optional extra extensions modules that will be included.
  • external Array of modules to be copied into node_modules instead of being loaded into browserify bundle. Note that external modules will require that its dependencies are within its directory and this plugin will not do this for you. e.g. you should execute the following: (cd external_modules/some-module && npm i --prod)
  • externalPaths Optional object key value pair of external module name and path. If not set, external modules will look for reference path in node_modules.
  • global (default false) - When global is set to true transforms will run inside node_modules.
  • ignore - Array of modules or paths that won't be transformed with Babelify.
  • includePaths - Array of file paths that will be included in the bundle package. Read here how to call these files.
  • minify (default true) - When minify is set to false Babili preset won't be added.
  • plugins - Array of Babel plugins.
  • prefix (default _optimize) - Folder to output bundle.
  • presets (default ['env']) - Array of Babel presets.
custom:
  optimize:
    debug: true
    exclude: ['ajv']
    extensions: ['.extension']
    external: ['sharp']
    externalPaths:
      sharp: 'external_modules/sharp'
    global: true
    ignore: ['ajv']
    includePaths: ['bin/some-binary-file']
    minify: false
    prefix: 'dist'
    plugins: ['transform-decorators-legacy']
    presets: ['es2017']

Function

  • optimize (default true) - When optimize is set to false the function won't be optimized.
functions:
  hello:
    optimize: false
  • exclude - Array of modules or paths that will be excluded.
  • extensions - Array of optional extra extensions modules that will be included.
  • external Array of modules to be copied into node_modules instead of being loaded into browserify bundle. Note that external modules will require it's dependencies within it's directory. (cd external_modules/some-module && npm i --prod)
  • externalPaths Optional object key value pair of external module name and path. If not set, external modules will look for reference path in node_modules.
  • global - When global is set to true transforms will run inside node_modules.
  • ignore - Array of modules or paths that won't be transformed with Babelify.
  • includePaths - Array of file paths that will be included in the bundle package. Read here how to call these files.
  • minify - When minify is set to false Babili preset won't be added.
  • plugins - Array of Babel plugins.
  • presets - Array of Babel presets.
functions:
  hello:
    optimize:
      exclude: ['ajv']
      extensions: ['.extension']
      external: ['sharp']
      externalPaths:
        sharp: 'external_modules/sharp'
      global: false
      ignore: ['ajv']
      includePaths: ['bin/some-binary-file']
      minify: false
      plugins: ['transform-decorators-legacy']
      presets: ['es2017']

includePaths Files

There is a difference you must know between calling files locally and after optimization with includePaths.

When Optimize packages your functions, it bundles them inside /${prefix}/${functionName}/... and when your lambda function runs in AWS it will run from root /var/task/${prefix}/${functionName}/... and your CWD will be /var/task/.

Solution in #32 by @hlegendre. path.resolve(process.env.LAMBDA_TASK_ROOT, ${prefix}, process.env.AWS_LAMBDA_FUNCTION_NAME, ${includePathFile}).

Contribute

Help us making this plugin better and future proof.

  • Clone the code
  • Install the dependencies with npm install
  • Create a feature branch git checkout -b new_feature
  • Lint with standard npm run lint

Author: FidelLimited
Source Code: https://github.com/FidelLimited/serverless-plugin-optimize 
License: MIT license

#serverless #node #browserify 

Serverless Optimize Plugin
Sheldon  Grant

Sheldon Grant

1654917540

Local Development Server That Aims to Make using Browserify Fast & Fun

beefy

a local development server designed to work with browserify.

it:

  • can live reload your browser when your code changes (if you want)
  • works with whatever version of browserify or watchify; globally installed or locally installed to node_modules/.
  • will spit compile errors out into the browser so you don't have that 1-2 seconds of cognitive dissonance and profound ennui that follows refreshing the page only to get a blank screen.
  • will spit out a default index.html for missing routes so you don't need to even muck about with HTML to get started
  • serves up static files with grace and aplomb (and also appropriate mimetypes)
  • is designed to fall away gracefully, as your project gets bigger.
  • loves you, unconditionally

how do I get it?

npm install -g beefy; and if you want to always have a browserify available for beefy to use, npm install -g browserify.

usage

$ cd directory/you/want/served
$ beefy path/to/thing/you/want/browserified.js [PORT] [-- browserify args]

what bundler does it use?

Beefy searches for bundlers in the following order:

  • First, it checks your local project's node_modules for watchify.
  • Then it checks locally for browserify.
  • Failing that, it checks for a global watchify.
  • Then falls back to a global browserify.

path/to/file.js

the path to the file you want browserified. can be just a normal node module. you can also alias it: path/to/file.js:bundle.js if you want -- so all requests to bundle.js will browserify path/to/file.js. this is helpful for when you're writing gh-pages-style sites that already have an index.html, and expect the bundle to be pregenerated and available at a certain path.

You may provide multiple entry points, if you desire!

--browserify command

--bundler command

use command instead of browserify or ./node_modules/.bin/browserify.

in theory, you could even get this working with r.js, but that would probably be scary and bats would fly out of it. but it's there if you need it! if you want to use r.js with beefy, you'll need a config that can write the resulting bundle to stdout, and you can run beefy with beefy :output-url.js --bundler r.js -- -o config.js.

NB: This will not work in Windows.

--live

Enable live reloading. this'll start up a sideband server and an fs watch on the current working directory -- if you save a file, your browser will refresh.

if you're not using the generated index file, beefy has your back -- it'll still automatically inject the appropriate script tag.

    <script src="/-/live-reload.js"></script>

--cwd dir

serve files as if running from dir.

--debug=false

turn off browserify source map output. by default, beefy automatically inserts -d into the browserify args -- this turns that behavior off.

--open

automatically discover a port and open it using your default browser.

--index=path/to/file

Provide your own default index! This works great for single page apps, as every URL on your site will be redirected to the same HTML file. Every instance of {{entry}} will be replaced with the entry point of your app.

api

var beefy = require('beefy')
  , http = require('http')

var handler = beefy('entry.js')

http.createServer(handler).listen(8124)

Beefy defaults the cwd to the directory of the file requiring it, so it's easy to switch from CLI mode to building a server.

As your server grows, you may want to expand on the information you're giving beefy:

var beefy = require('beefy')
  , http = require('http')

http.createServer(beefy({
    entries: ['entry.js']
  , cwd: __dirname
  , live: true
  , quiet: false
  , bundlerFlags: ['-t', 'brfs']
  , unhandled: on404
})).listen(8124)

function on404(req, resp) {
  resp.writeHead(404, {})
  resp.end('sorry folks!')
}

beefy(opts: BeefyOptions, ready: (err: Error) => void)

Create a request handler suitable for providing to http.createServer. Calls ready once the appropriate bundler has been located. If ready is not provided and a bundler isn't located, an error is thrown.

BeefyOptions

Beefy's options are a simple object, which may contain the following attributes:

  • cwd: String. The base directory that beefy is serving. Defaults to the directory of the module that first required beefy.
  • quiet: Boolean. Whether or not to output request information to the console. Defaults to true.
  • live: Boolean. Whether to enable live reloading. Defaults to false.
  • bundler: null, String, or Function. If a string is given, beefy will attempt to run that string as a child process whenever the path is given. If a function is given, it is expected to accept a path and return an object comprised of {stdout: ReadableStream, stderr: ReadableStream}. If not given, beefy will search for an appropriate bundler.
  • bundlerFlags: Flags to be passed to the bundler. Ignored if bundler is a function.
  • entries: String, Array, or Object. The canonical form is that of an object mapping URL pathnames to paths on disk relative to cwd. If given as an array or string, entries will be mapped like so: index.js will map /index.js to <cwd>/index.js.
  • unhandled: Function accepting req and resp. Called for 404s. If not given, a default 404 handler will be used.
  • watchify: defaults to true -- when true, beefy will prefer using watchify to browserify. If false, beefy will prefer browserify.

Beefy may accept, as a shorthand, beefy("file.js") or beefy(["file.js"]).

Author: Chrisdickinson
Source Code: https://github.com/chrisdickinson/beefy 
License: MIT license

#node #nodejs #javascript #browserify 

Local Development Server That Aims to Make using Browserify Fast & Fun

BitcoinJS: A Javascript Bitcoin Library for Node.js and Browsers

BitcoinJS (bitcoinjs-lib)  

A javascript Bitcoin library for node.js and browsers. Written in TypeScript, but committing the JS files to verify.

Should I use this in production?

If you are thinking of using the master branch of this library in production, stop. Master is not stable; it is our development branch, and only tagged releases may be classified as stable.

Can I trust this code?

Don't trust. Verify.

We recommend every user of this library and the bitcoinjs ecosystem audit and verify any underlying code for its validity and suitability, including reviewing any and all of your project's dependencies.

Mistakes and bugs happen, but with your help in resolving and reporting issues, together we can produce open source software that is:

  • Easy to audit and verify,
  • Tested, with test coverage >95%,
  • Advanced and feature rich,
  • Standardized, using prettier and Node Buffer's throughout, and
  • Friendly, with a strong and helpful community, ready to answer questions.

Documentation

Presently, we do not have any formal documentation other than our examples, please ask for help if our examples aren't enough to guide you.

You can find a Web UI that covers most of the psbt.ts, transaction.ts and p2*.ts APIs here.

Installation

npm install bitcoinjs-lib
# optionally, install a key derivation library as well
npm install ecpair bip32
# ecpair is the ECPair class for single keys
# bip32 is for generating HD keys

Previous versions of the library included classes for key management (ECPair, HDNode(->"bip32")) but now these have been separated into different libraries. This lowers the bundle size significantly if you don't need to perform any crypto functions (converting private to public keys and deriving HD keys).

Typically we support the Node Maintenance LTS version. TypeScript target will be set to the ECMAScript version in which all features are fully supported by current Active Node LTS. However, depending on adoption among other environments (browsers etc.) we may keep the target back a year or two. If in doubt, see the main_ci.yml for what versions are used by our continuous integration tests.

WARNING: We presently don't provide any tooling to verify that the release on npm matches GitHub. As such, you should verify anything downloaded by npm against your own verified copy.

Usage

Crypto is hard.

When working with private keys, the random number generator is fundamentally one of the most important parts of any software you write. For random number generation, we default to the randombytes module, which uses window.crypto.getRandomValues in the browser, or Node js' crypto.randomBytes, depending on your build system. Although this default is ~OK, there is no simple way to detect if the underlying RNG provided is good enough, or if it is catastrophically bad. You should always verify this yourself to your own standards.

This library uses tiny-secp256k1, which uses RFC6979 to help prevent k re-use and exploitation. Unfortunately, this isn't a silver bullet. Often, Javascript itself is working against us by bypassing these counter-measures.

Problems in Buffer (UInt8Array), for example, can trivially result in catastrophic fund loss without any warning. It can do this through undermining your random number generation, accidentally producing a duplicate k value, sending Bitcoin to a malformed output script, or any of a million different ways. Running tests in your target environment is important and a recommended step to verify continuously.

Finally, adhere to best practice. We are not an authorative source of best practice, but, at the very least:

Browser

The recommended method of using bitcoinjs-lib in your browser is through Browserify. If you're familiar with how to use browserify, ignore this and carry on, otherwise, it is recommended to read the tutorial at https://browserify.org/.

NOTE: We use Node Maintenance LTS features, if you need strict ES5, use --transform babelify in conjunction with your browserify step (using an es2015 preset).

WARNING: iOS devices have problems, use at least buffer@5.0.5 or greater, and enforce the test suites (for Buffer, and any other dependency) pass before use.

Typescript or VSCode users

Type declarations for Typescript are included in this library. Normal installation should include all the needed type information.

Examples

The below examples are implemented as integration tests, they should be very easy to understand. Otherwise, pull requests are appreciated. Some examples interact (via HTTPS) with a 3rd Party Blockchain Provider (3PBP).

Taproot Key Spend

Generate a random address

Import an address via WIF

Generate a 2-of-3 P2SH multisig address

Generate a SegWit address

Generate a SegWit P2SH address

Generate a SegWit 3-of-4 multisig address

Generate a SegWit 2-of-2 P2SH multisig address

Support the retrieval of transactions for an address (3rd party blockchain)

Generate a Testnet address

Generate a Litecoin address

Create a 1-to-1 Transaction

Create (and broadcast via 3PBP) a typical Transaction

Create (and broadcast via 3PBP) a Transaction with an OP_RETURN output

Create (and broadcast via 3PBP) a Transaction with a 2-of-4 P2SH(multisig) input

Create (and broadcast via 3PBP) a Transaction with a SegWit P2SH(P2WPKH) input

Create (and broadcast via 3PBP) a Transaction with a SegWit P2WPKH input

Create (and broadcast via 3PBP) a Transaction with a SegWit P2PK input

Create (and broadcast via 3PBP) a Transaction with a SegWit 3-of-4 P2SH(P2WSH(multisig)) input

Create (and broadcast via 3PBP) a Transaction and sign with an HDSigner interface (bip32)

Import a BIP32 testnet xpriv and export to WIF

Export a BIP32 xpriv, then import it

Export a BIP32 xpub

Create a BIP32, bitcoin, account 0, external address

Create a BIP44, bitcoin, account 0, external address

Create a BIP49, bitcoin testnet, account 0, external address

Use BIP39 to generate BIP32 addresses

Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the past)

Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future)

Create (and broadcast via 3PBP) a Transaction where Alice and Bob can redeem the output at any time

Create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry

Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future) (simple CHECKSEQUENCEVERIFY)

Create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry (simple CHECKSEQUENCEVERIFY)

Create (and broadcast via 3PBP) a Transaction where Bob and Charles can send (complex CHECKSEQUENCEVERIFY)

Create (and broadcast via 3PBP) a Transaction where Alice (mediator) and Bob can send after 2 blocks (complex CHECKSEQUENCEVERIFY)

Create (and broadcast via 3PBP) a Transaction where Alice (mediator) can send after 5 blocks (complex CHECKSEQUENCEVERIFY)

If you have a use case that you feel could be listed here, please ask for it!

Contributing

See CONTRIBUTING.md.

Running the test suite

npm test
npm run-script coverage

Complementing Libraries

  • BIP21 - A BIP21 compatible URL encoding library
  • BIP38 - Passphrase-protected private keys
  • BIP39 - Mnemonic generation for deterministic keys
  • BIP32-Utils - A set of utilities for working with BIP32
  • BIP66 - Strict DER signature decoding
  • BIP68 - Relative lock-time encoding library
  • BIP69 - Lexicographical Indexing of Transaction Inputs and Outputs
  • Base58 - Base58 encoding/decoding
  • Base58 Check - Base58 check encoding/decoding
  • Bech32 - A BIP173/BIP350 compliant Bech32/Bech32m encoding library
  • coinselect - A fee-optimizing, transaction input selection module for bitcoinjs-lib.
  • merkle-lib - A performance conscious library for merkle root and tree calculations.
  • minimaldata - A module to check bitcoin policy: SCRIPT_VERIFY_MINIMALDATA

Alternatives

Author: Bitcoinjs
Source Code: https://github.com/bitcoinjs/bitcoinjs-lib 
License: MIT license

#node #javascript #typescript #bitcoin #browserify 

BitcoinJS: A Javascript Bitcoin Library for Node.js and Browsers
Hermann  Frami

Hermann Frami

1652952060

Speed Up Your Node Based Lambda's

Serverless Browserify Plugin

A Serverless v1.0 plugin that uses Browserify to bundle your NodeJS Lambda functions.

Why? Lambda's with smaller code start and run faster. Lambda also has an account wide deployment package size limit.

aws-sdk-js now officially supports browserify. Read more about why this kicks ass on my blog.

With the example package.json and javascript code below, the default packaging for NodeJs lambdas in Serverless produces a zip file that is 11.3 MB, because it blindly includes all of node_modules in the zip.

This plugin with 2 lines of configuration produces a zip file that is 400KB!

...
  "dependencies": {
    "aws-sdk": "^2.6.12",
    "moment": "^2.15.2",
    "request": "^2.75.0",
    "rxjs": "^5.0.0-rc.1"
  },
...
const Rx      = require('rxjs/Rx');
const request = require('request');
...

Install

From your serverless project run:

npm install serverless-plugin-browserify --save-dev

Add the plugin to your serverless.yml file and set package.individually to true:

plugins:
  - serverless-plugin-browserify
package:
  individually: true

package.individually is required because it makes configuration more straight forward, and if you are not packaging individually size is not a concern of yours in the 1st place.

Configure

For most use cases you should NOT need to do any configuration. If you are a code ninja, read on.

The base config for browserify is read from the custom.browserify section of serverless.yml. All browserify options are supported (most are auto configured by this plugin). This plugin adds one special option disable which if true will bypass this plugin.

The base config can be over-ridden on a function by function basis. Again custom.browserify is not required and should not even need to be defined in most cases.

custom:
  browserify:
    #any option defined in https://github.com/substack/node-browserify#browserifyfiles--opts

functions:
    usersGet:
      name: ${self:provider.stage}-${self:service}-pageGet
      description: get user
      handler: users/handler.hello      
      browserify:
        noParse:
          - ./someBig.json  #browserify can't optimize json, will take long time to parse for nothing      

Note: package.include can be used with this plugin. All other options can be handled by leveraging browserify options in your serverless.yml custom browserify section.

Usage

When this plugin is enabled, and package.individually is true, running serverless deploy and serverless deploy -f <funcName> will automatically browserify your node lambda code.

If you want to see output of bundled file or zip simply set SLS_DEBUG. Ex (using Fish Shell): env SLS_DEBUG=true sls deploy function -v -f usersGet

Also check out the examples directory

Bundle only

Run serverless browserify -f <functionName>. You can optionally dictate where the bundling output dir is by using the -o flag. Ex: sls browserify -o /tmp/test -f pageUpdate.

FAQ

  • Should I use Webpack instead of this plugin? I prefer Browserify over webpack because I have found it supports more modules, optimizes better, and requires less configuration.
  • Why is UglifyJS not built-in? No ES6 support. Issue been open since 2014.
  • My code is not bundling correctly The bundled code is always stored in a tmp dir on your computer. Set SLS_DEBUG=true then re-run your command to output the directory. Fish Shell ex: env SLS_DEBUG=true sls browserify

Author: Doapp-ryanp
Source Code: https://github.com/doapp-ryanp/serverless-plugin-browserify 
License: MIT license

#serverless #aws #lambda #node 

Speed Up Your Node Based Lambda's