pax_global_header00006660000000000000000000000064126133351200014506gustar00rootroot0000000000000052 comment=dfbda1ac04b03cb694991b93e7f961011403771c deps-sort-2.0.0/000077500000000000000000000000001261333512000134255ustar00rootroot00000000000000deps-sort-2.0.0/.travis.yml000066400000000000000000000000721261333512000155350ustar00rootroot00000000000000language: node_js node_js: - "0.10" - "0.12" - node deps-sort-2.0.0/LICENSE000066400000000000000000000020611261333512000144310ustar00rootroot00000000000000This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. deps-sort-2.0.0/bin/000077500000000000000000000000001261333512000141755ustar00rootroot00000000000000deps-sort-2.0.0/bin/cmd.js000077500000000000000000000004601261333512000153010ustar00rootroot00000000000000#!/usr/bin/env node var argv = require('subarg')(process.argv.slice(2)); var JSONStream = require('JSONStream'); var sort = require('../')(argv); var parse = JSONStream.parse([ true ]); var stringify = JSONStream.stringify(); process.stdin.pipe(parse).pipe(sort).pipe(stringify).pipe(process.stdout); deps-sort-2.0.0/example/000077500000000000000000000000001261333512000150605ustar00rootroot00000000000000deps-sort-2.0.0/example/sort.js000066400000000000000000000003411261333512000164030ustar00rootroot00000000000000var sort = require('../')(); var JSONStream = require('JSONStream'); var parse = JSONStream.parse([ true ]); var stringify = JSONStream.stringify(); process.stdin.pipe(parse).pipe(sort).pipe(stringify).pipe(process.stdout); deps-sort-2.0.0/index.js000066400000000000000000000062431261333512000150770ustar00rootroot00000000000000var through = require('through2'); var shasum = require('shasum'); module.exports = function (opts) { if (!opts) opts = {}; var rows = []; return through.obj(write, end); function write (row, enc, next) { rows.push(row); next() } function end () { var tr = this; rows.sort(cmp); sorter(rows, tr, opts); } }; function sorter (rows, tr, opts) { var expose = opts.expose || {}; if (Array.isArray(expose)) { expose = expose.reduce(function (acc, key) { acc[key] = true; return acc; }, {}); } var hashes = {}, deduped = {}; var sameDeps = depCmp(); if (opts.dedupe) { rows.forEach(function (row) { var h = shasum(row.source); sameDeps.add(row, h); if (hashes[h]) { hashes[h].push(row); } else { hashes[h] = [row]; } }); Object.keys(hashes).forEach(function (h) { var rows = hashes[h]; while (rows.length > 1) { var row = rows.pop(); row.dedupe = rows[0].id; row.sameDeps = sameDeps.cmp(rows[0].deps, row.deps); deduped[row.id] = rows[0].id; } }); } if (opts.index) { var index = {}; var offset = 0; rows.forEach(function (row, ix) { if (has(expose, row.id)) { row.index = row.id; offset ++; if (expose[row.id] !== true) { index[expose[row.id]] = row.index; } } else { row.index = ix + 1 - offset; } index[row.id] = row.index; }); rows.forEach(function (row) { row.indexDeps = {}; Object.keys(row.deps).forEach(function (key) { var id = row.deps[key]; row.indexDeps[key] = index[id]; }); if (row.dedupe) { row.dedupeIndex = index[row.dedupe]; } tr.push(row); }); } else { rows.forEach(function (row) { tr.push(row) }); } tr.push(null); } function cmp (a, b) { return a.id + a.hash < b.id + b.hash ? -1 : 1; } function has (obj, key) { return Object.prototype.hasOwnProperty.call(obj, key); } function depCmp () { var deps = {}, hashes = {}; return { add: add, cmp: cmp } function add (row, hash) { deps[row.id] = row.deps; hashes[row.id] = hash; } function cmp (a, b, limit) { if (!a && !b) return true; if (!a || !b) return false; var keys = Object.keys(a); if (keys.length !== Object.keys(b).length) return false; for (var i = 0; i < keys.length; i++) { var k = keys[i], ka = a[k], kb = b[k]; var ha = hashes[ka]; var hb = hashes[kb]; var da = deps[ka]; var db = deps[kb]; if (ka === kb) continue; if (ha !== hb || (!limit && !cmp(da, db, 1))) { return false; } } return true; } } deps-sort-2.0.0/package.json000066400000000000000000000015351261333512000157170ustar00rootroot00000000000000{ "name": "deps-sort", "version": "2.0.0", "description": "sort module-deps output for deterministic browserify bundles", "main": "index.js", "bin": { "deps-sort": "bin/cmd.js" }, "dependencies": { "JSONStream": "^1.0.3", "shasum": "^1.0.0", "subarg": "^1.0.0", "through2": "^2.0.0" }, "devDependencies": { "tap": "^2.2.0" }, "scripts": { "test": "tap test/*.js" }, "repository": { "type": "git", "url": "git://github.com/substack/deps-sort.git" }, "homepage": "https://github.com/substack/deps-sort", "keywords": [ "dependency", "graph", "browser", "browserify", "module-deps", "browser-pack", "sorted", "determinism" ], "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" }, "license": "MIT" } deps-sort-2.0.0/readme.markdown000066400000000000000000000043621261333512000164330ustar00rootroot00000000000000# deps-sort sort [module-deps](https://npmjs.org/package/module-deps) output for deterministic browserify bundles [![build status](https://secure.travis-ci.org/substack/deps-sort.png)](http://travis-ci.org/substack/deps-sort) # example ## command-line ``` $ for((i=0;i<5;i++)); do module-deps main.js | deps-sort | browser-pack | md5sum; done e9e630de2c62953140357db0444c3c3a - e9e630de2c62953140357db0444c3c3a - e9e630de2c62953140357db0444c3c3a - e9e630de2c62953140357db0444c3c3a - e9e630de2c62953140357db0444c3c3a - ``` or using `browserify --deps` on a [voxeljs](http://voxeljs.com/) project: ``` $ for((i=0;i<5;i++)); do browserify --deps browser.js | deps-sort | browser-pack | md5sum; done fb418c74b53ba2e4cef7d01808b848e6 - fb418c74b53ba2e4cef7d01808b848e6 - fb418c74b53ba2e4cef7d01808b848e6 - fb418c74b53ba2e4cef7d01808b848e6 - fb418c74b53ba2e4cef7d01808b848e6 - ``` ## api To use this module programmatically, write streaming object data and read streaming object data: ``` js var sort = require('../')(); var JSONStream = require('JSONStream'); var parse = JSONStream.parse([ true ]); var stringify = JSONStream.stringify(); process.stdin.pipe(parse).pipe(sort).pipe(stringify).pipe(process.stdout); ``` # methods ``` js var depsSort = require('deps-sort'); ``` ## var stream = depsSort(opts) Return a new through `stream` that should get written [module-deps](https://npmjs.org/package/module-deps) objects and will output sorted objects. `opts` can be: * `opts.index` - when true, for each module-deps row, insert `row.index` with the numeric index and `row.indexDeps` like `row.deps` but mapping require strings to row indices * `opts.expose` - array of names or object mapping names to `true` not to mangle with integer indexes when `opts.index` is turned on. If `opts.expose` maps names to strings, those strings will be used to resolve the indexed references. * `opts.dedupe` - set `row.dedupe` for files that match existing contents. Sets `row.dedupeIndex` when `opts.index` is enabled. When `row.dedupe` is set, `row.sameDeps` will be set to a boolean of whether the dependencies at the dedupe target match (true) or just the source content (false). # install With [npm](https://npmjs.org) do: ``` npm install deps-sort ``` # license MIT deps-sort-2.0.0/test/000077500000000000000000000000001261333512000144045ustar00rootroot00000000000000deps-sort-2.0.0/test/dedupe-deps-of-deps.js000066400000000000000000000033001261333512000204700ustar00rootroot00000000000000var sort = require('../'); var test = require('tap').test; var through = require('through2'); test('dedupe-deps-of-deps', function (t) { t.plan(1); var s = sort({ dedupe: true }); var rows = []; function write (row, enc, next) { rows.push(row); next() } function end () { t.deepEqual(rows, [ { id: '/bar.js', deps: { baz: '/bar/baz.js' }, source: 'TWO' }, { id: '/bar/baz.js', deps: {}, source: 'THREE' }, { id: '/foo.js', deps: { baz: '/foo/baz.js' }, source: 'TWO', dedupe: '/bar.js', sameDeps: true }, { id: '/foo/baz.js', deps: {}, source: 'THREE', dedupe: '/bar/baz.js', sameDeps: true }, { id: '/main.js', deps: { './foo': '/foo.js', './bar': '/bar.js' }, source: 'ONE' } ]); } s.pipe(through.obj(write, end)); s.write({ id: '/main.js', deps: { './foo': '/foo.js', './bar': '/bar.js' }, source: 'ONE' }); s.write({ id: '/foo.js', deps: { baz : '/foo/baz.js' }, source: 'TWO' }); s.write({ id: '/bar.js', deps: { baz : '/bar/baz.js' }, source: 'TWO' }); s.write({ id: '/foo/baz.js', deps: {}, source: 'THREE' }); s.write({ id: '/bar/baz.js', deps: {}, source: 'THREE' }); s.end(); }); deps-sort-2.0.0/test/dedupe.js000066400000000000000000000017301261333512000162110ustar00rootroot00000000000000var sort = require('../'); var test = require('tap').test; var through = require('through2'); test('dedupe', function (t) { t.plan(1); var s = sort({ dedupe: true }); var rows = []; function write (row, enc, next) { rows.push(row); next() } function end () { t.deepEqual(rows, [ { id: '/bar.js', deps: {}, source: 'TWO' }, { id: '/foo.js', deps: {}, source: 'TWO', dedupe: '/bar.js', sameDeps: true }, { id: '/main.js', deps: { './foo': '/foo.js', './bar': '/bar.js' }, source: 'ONE' } ]); } s.pipe(through.obj(write, end)); s.write({ id: '/main.js', deps: { './foo': '/foo.js', './bar': '/bar.js' }, source: 'ONE' }); s.write({ id: '/foo.js', deps: {}, source: 'TWO' }); s.write({ id: '/bar.js', deps: {}, source: 'TWO' }); s.end(); }); deps-sort-2.0.0/test/dedupe_index.js000066400000000000000000000025471261333512000174070ustar00rootroot00000000000000var sort = require('../'); var test = require('tap').test; var through = require('through2'); test('dedupe index', function (t) { t.plan(1); var s = sort({ dedupe: true, index: true }); var rows = []; function write (row, enc, next) { rows.push(row); next() } function end () { t.deepEqual(rows, [ { id: '/bar.js', deps: {}, source: 'TWO', index: 1, indexDeps: {} }, { id: '/foo.js', deps: {}, source: 'TWO', dedupe: '/bar.js', index: 2, indexDeps: {}, dedupeIndex: 1, sameDeps: true }, { id: '/main.js', deps: { './foo': '/foo.js', './bar': '/bar.js' }, source: 'ONE', index: 3, indexDeps: { './foo': 2, './bar': 1 }, } ]); } s.pipe(through.obj(write, end)); s.write({ id: '/main.js', deps: { './foo': '/foo.js', './bar': '/bar.js' }, source: 'ONE' }); s.write({ id: '/foo.js', deps: {}, source: 'TWO' }); s.write({ id: '/bar.js', deps: {}, source: 'TWO' }); s.end(); }); deps-sort-2.0.0/test/dedupe_undef.js000066400000000000000000000016461261333512000174000ustar00rootroot00000000000000var sort = require('../'); var test = require('tap').test; var through = require('through2'); test('dedupe undef', function (t) { t.plan(1); var s = sort({ dedupe: true }); var rows = []; function write (row, enc, next) { rows.push(row); next() } function end () { t.deepEqual(rows, [ { id: '/bar.js', source: 'TWO' }, { id: '/foo.js', source: 'TWO', dedupe: '/bar.js', sameDeps: true }, { id: '/main.js', deps: { './foo': '/foo.js', './bar': '/bar.js' }, source: 'ONE' } ]); } s.pipe(through.obj(write, end)); s.write({ id: '/main.js', deps: { './foo': '/foo.js', './bar': '/bar.js' }, source: 'ONE' }); s.write({ id: '/foo.js', source: 'TWO' }); s.write({ id: '/bar.js', source: 'TWO' }); s.end(); }); deps-sort-2.0.0/test/expose.js000066400000000000000000000021311261333512000162420ustar00rootroot00000000000000var sort = require('../'); var test = require('tap').test; var through = require('through2'); test('expose true', function (t) { t.plan(1); var s = sort({ index: true, expose: [ '/foo.js', '/bar.js' ] }); var rows = []; function write (row, enc, next) { rows.push(row); next() } function end () { t.deepEqual(rows, [ { id: '/bar.js', deps: {}, index: '/bar.js', indexDeps: {} }, { id: '/foo.js', deps: { './bar': '/bar.js' }, index: '/foo.js', indexDeps: { './bar': '/bar.js' } }, { id: '/main.js', deps: { './foo': '/foo.js' }, index: 1, indexDeps: { './foo': '/foo.js' } }, ]); } s.pipe(through.obj(write, end)); s.write({ id: '/main.js', deps: { './foo': '/foo.js' } }); s.write({ id: '/foo.js', deps: { './bar': '/bar.js' } }); s.write({ id: '/bar.js', deps: {} }); s.end(); }); deps-sort-2.0.0/test/expose_str.js000066400000000000000000000021501261333512000171330ustar00rootroot00000000000000var sort = require('../'); var test = require('tap').test; var through = require('through2'); test('expose string', function (t) { t.plan(1); var s = sort({ index: true, expose: { 'f': '/foo.js', 'b': '/bar.js' } }); var rows = []; function write (row, enc, next) { rows.push(row); next() } function end () { t.deepEqual(rows, [ { id: '/main.js', deps: { './foo': '/foo.js' }, index: 1, indexDeps: { './foo': 'f' } }, { id: 'b', deps: {}, index: 'b', indexDeps: {} }, { id: 'f', deps: { './bar': '/bar.js' }, index: 'f', indexDeps: { './bar': 'b' } } ]); } s.pipe(through.obj(write, end)); s.write({ id: '/main.js', deps: { './foo': '/foo.js' } }); s.write({ id: 'f', deps: { './bar': '/bar.js' } }); s.write({ id: 'b', deps: {} }); s.end(); }); deps-sort-2.0.0/test/indexed.js000066400000000000000000000020231261333512000163570ustar00rootroot00000000000000var sort = require('../'); var test = require('tap').test; var through = require('through2'); test('indexed', function (t) { t.plan(1); var s = sort({ index: true }); var rows = []; function write (row, enc, next) { rows.push(row); next() } function end () { t.deepEqual(rows, [ { id: '/bar.js', deps: {}, index: 1, indexDeps: {} }, { id: '/foo.js', deps: { './bar': '/bar.js' }, index: 2, indexDeps: { './bar': 1 } }, { id: '/main.js', deps: { './foo': '/foo.js' }, index: 3, indexDeps: { './foo': 2 } }, ]); } s.pipe(through.obj(write, end)); s.write({ id: '/main.js', deps: { './foo': '/foo.js' } }); s.write({ id: '/foo.js', deps: { './bar': '/bar.js' } }); s.write({ id: '/bar.js', deps: {} }); s.end(); }); deps-sort-2.0.0/test/sort.js000066400000000000000000000012711261333512000157320ustar00rootroot00000000000000var sort = require('../'); var test = require('tap').test; var through = require('through2'); test('sort', function (t) { t.plan(1); var s = sort(); var rows = []; function write (row, enc, next) { rows.push(row); next() } function end () { t.deepEqual(rows, [ { id: '/bar.js', deps: {} }, { id: '/foo.js', deps: { './bar': '/bar.js' } }, { id: '/main.js', deps: { './foo': '/foo.js' } } ]); } s.pipe(through.obj(write, end)); s.write({ id: '/main.js', deps: { './foo': '/foo.js' } }); s.write({ id: '/foo.js', deps: { './bar': '/bar.js' } }); s.write({ id: '/bar.js', deps: {} }); s.end(); });