From 4c157cfa24216f47c0069c9fc0053e9a117bacb3 Mon Sep 17 00:00:00 2001
From: Chris McCormick <chris@mccormick.cx>
Date: Mon, 27 Nov 2017 16:11:16 +0800
Subject: [PATCH 1/2] Fixed browser building.

Was getting the following error message:

$ make browser
node_modules/.bin/cjsify --export Bencode src/index.coffee --no-node > bencode.js
bencode-js/node_modules/commonjs-everywhere/lib/command.js:56
throw new Error('wrong number of entry points given; expected 1');
---
 Makefile     | 10 ++++++----
 package.json |  2 ++
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index 3c1183b..8735840 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,8 @@ BIN=./node_modules/.bin
 MOCHA=$(BIN)/mocha
 COFFEE=$(BIN)/coffee --js
 CJSIFY=$(BIN)/cjsify --export Bencode src/index.coffee
+BROWSERIFY=$(BIN)/browserify
+UGLIFY=$(BIN)/uglifyjs
 
 SRCS = $(shell find src -name "*.coffee" -type f | sort)
 LIBS = $(SRCS:src/%.coffee=lib/%.js)
@@ -14,10 +16,10 @@ lib/%.js: src/%.coffee
 	$(COFFEE) <"$<" >"$@"
 
 browser: bencode.js bencode-min.js
-bencode.js: $(SRCS)
-	$(CJSIFY) --no-node > bencode.js
-bencode-min.js: $(SRCS)
-	$(CJSIFY) --no-node --minify > bencode-min.js
+bencode.js: $(LIBS)
+	$(BROWSERIFY) lib/index.js --standalone Bencode > $@
+bencode-min.js: bencode.js
+	$(UGLIFY) $< > $@
 
 .PHONY: test
 
diff --git a/package.json b/package.json
index 59735f1..f4d19c1 100644
--- a/package.json
+++ b/package.json
@@ -20,6 +20,8 @@
       , "mocha"               : ">=1.8.2"
       , "coffee-script-redux" : "2.0.0-beta8"
       , "commonjs-everywhere" : "~0.6.1"
+      , "browserify"          : "^14.5.0"
+      , "uglify-js"           : "^3.2.0"
     }
 
   , "repository":

From 71cc5ad1cc8d6ac5ba8ba14763450cb26b3f58c4 Mon Sep 17 00:00:00 2001
From: Chris McCormick <chris@mccormick.cx>
Date: Mon, 27 Nov 2017 16:13:08 +0800
Subject: [PATCH 2/2] New build.

---
 bencode-min.js |   2 +-
 bencode.js     | 292 ++++++++++++++++++++++---------------------------
 2 files changed, 133 insertions(+), 161 deletions(-)

diff --git a/bencode-min.js b/bencode-min.js
index 69d6380..b22562e 100644
--- a/bencode-min.js
+++ b/bencode-min.js
@@ -1 +1 @@
-(function(b){function a(b,d){if({}.hasOwnProperty.call(a.cache,b))return a.cache[b];var e=a.resolve(b);if(!e)throw new Error('Failed to resolve module '+b);var c={id:b,require:a,filename:b,exports:{},loaded:!1,parent:d,children:[]};d&&d.children.push(c);var f=b.slice(0,b.lastIndexOf('/')+1);return a.cache[b]=c.exports,e.call(c.exports,c,c.exports,f,b),c.loaded=!0,a.cache[b]=c.exports}a.modules={},a.cache={},a.resolve=function(b){return{}.hasOwnProperty.call(a.modules,b)?a.modules[b]:void 0},a.define=function(b,c){a.modules[b]=c},a.define('/src/index.coffee',function(b,c,d,e){b.exports={encode:a('/src/encode.coffee',b),decode:a('/src/decode.coffee',b)}}),a.define('/src/decode.coffee',function(b,c,d,e){var a;a=function(c){var l,d,e,f,b,k,g,h,i,j,n,m;switch(c[0]){case'i':d=c.match(/^i-?\d+e/)[0];return[d.length,+d.slice(1,-1)];case'0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':j=c.match(/^\d+/)[0];i=j.length+1;d=c.slice(0,i+ +j);return[d.length,d.slice(i)];case'l':b=1;l=function(e){var d,f,g;while(c[b]!=='e')d=a(c.slice(b)),g=d[0],f=d[1],b+=g,e.push(f);return e}.call(this,[]);return[b+1,l];case'd':b=1;h={};while(c[b]!=='e')e=a(c.slice(b)),g=e[0],k=e[1],f=a(c.slice(b+g)),m=f[0],n=f[1],b+=g+m,h[k]=n;return[b+1,h]}},b.exports=function(b){return a(b)[1]}}),a.define('/src/encode.coffee',function(f,j,i,h){function g(a,b){return{}.hasOwnProperty.call(a,b)}var a,e,d,c,b;b=function(a){return''+a.length+':'+a},d=function(a){return'i'+a+'e'},c=function(b){var c;return c=function(d){var e;for(var c=0,f=b.length;c<f;++c)e=b[c],d.push(a(e));return d}.call(this,[]).join(''),'l'+c+'e'},e=function(b){var d,c;return c=function(c){var a;for(a in b){if(!g(b,a))continue;c.push(a)}return c}.call(this,[]).sort(),d=function(f){var e;for(var d=0,g=c.length;d<g;++d)e=c[d],f.push(''+a(e)+a(b[e]));return f}.call(this,[]).join(''),'d'+d+'e'},a=function(a){switch(!1){case!(typeof a==='string'):return b(a);case!(typeof a==='number'):return d(0|a);case!('[object Array]'==={}.toString.call(a)):return c(a);default:return e(a)}},f.exports=a}),b.Bencode=a('/src/index.coffee')}.call(this,this))
+(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Bencode=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){void function(){var read;read=function(str){var arr,bencoded,cache$,cache$1,cursor,key,keyLength,obj,startPos,stringLength,value,valueLength;switch(str[0]){case"i":bencoded=str.match(/^i-?\d+e/)[0];return[bencoded.length,+bencoded.slice(1,-1)];case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":case"8":case"9":stringLength=str.match(/^\d+/)[0];startPos=stringLength.length+1;bencoded=str.slice(0,startPos+ +stringLength);return[bencoded.length,bencoded.slice(startPos)];case"l":cursor=1;arr=function(accum$){var cache$,entry,entryLength;while(str[cursor]!=="e"){cache$=read(str.slice(cursor));entryLength=cache$[0];entry=cache$[1];cursor+=entryLength;accum$.push(entry)}return accum$}.call(this,[]);return[cursor+1,arr];case"d":cursor=1;obj={};while(str[cursor]!=="e"){cache$=read(str.slice(cursor));keyLength=cache$[0];key=cache$[1];cache$1=read(str.slice(cursor+keyLength));valueLength=cache$1[0];value=cache$1[1];cursor+=keyLength+valueLength;obj[key]=value}return[cursor+1,obj]}};module.exports=function(str){return read(str)[1]}}.call(this)},{}],2:[function(require,module,exports){void function(){var encode,encodeDictionary,encodeInteger,encodeList,encodeString;encodeString=function(s){return""+s.length+":"+s};encodeInteger=function(i){return"i"+i+"e"};encodeList=function(array){var encodedContents,object;encodedContents=function(accum$){for(var i$=0,length$=array.length;i$<length$;++i$){object=array[i$];accum$.push(encode(object))}return accum$}.call(this,[]).join("");return"l"+encodedContents+"e"};encodeDictionary=function(object){var encodedContents,key,keys;keys=function(accum$){for(key in object){if(!isOwn$(object,key))continue;accum$.push(key)}return accum$}.call(this,[]).sort();encodedContents=function(accum$1){for(var i$=0,length$=keys.length;i$<length$;++i$){key=keys[i$];accum$1.push(""+encode(key)+encode(object[key]))}return accum$1}.call(this,[]).join("");return"d"+encodedContents+"e"};encode=function(object){switch(false){case!(typeof object==="string"):return encodeString(object);case!(typeof object==="number"):return encodeInteger(Math.floor(object));case!("[object Array]"==={}.toString.call(object)):return encodeList(object);default:return encodeDictionary(object)}};module.exports=encode;function isOwn$(o,p){return{}.hasOwnProperty.call(o,p)}}.call(this)},{}],3:[function(require,module,exports){module.exports={encode:require("./encode"),decode:require("./decode")}},{"./decode":1,"./encode":2}]},{},[3])(3)});
diff --git a/bencode.js b/bencode.js
index abd76d1..6432a35 100644
--- a/bencode.js
+++ b/bencode.js
@@ -1,166 +1,138 @@
-(function (global) {
-  function require(file, parentModule) {
-    if ({}.hasOwnProperty.call(require.cache, file))
-      return require.cache[file];
-    var resolved = require.resolve(file);
-    if (!resolved)
-      throw new Error('Failed to resolve module ' + file);
-    var module$ = {
-        id: file,
-        require: require,
-        filename: file,
-        exports: {},
-        loaded: false,
-        parent: parentModule,
-        children: []
-      };
-    if (parentModule)
-      parentModule.children.push(module$);
-    var dirname = file.slice(0, file.lastIndexOf('/') + 1);
-    require.cache[file] = module$.exports;
-    resolved.call(module$.exports, module$, module$.exports, dirname, file);
-    module$.loaded = true;
-    return require.cache[file] = module$.exports;
-  }
-  require.modules = {};
-  require.cache = {};
-  require.resolve = function (file) {
-    return {}.hasOwnProperty.call(require.modules, file) ? require.modules[file] : void 0;
-  };
-  require.define = function (file, fn) {
-    require.modules[file] = fn;
-  };
-  require.define('/src/index.coffee', function (module, exports, __dirname, __filename) {
-    module.exports = {
-      encode: require('/src/encode.coffee', module),
-      decode: require('/src/decode.coffee', module)
-    };
-  });
-  require.define('/src/decode.coffee', function (module, exports, __dirname, __filename) {
-    var read;
-    read = function (str) {
-      var arr, bencoded, cache$, cache$1, cursor, key, keyLength, obj, startPos, stringLength, value, valueLength;
-      switch (str[0]) {
-      case 'i':
-        bencoded = str.match(/^i-?\d+e/)[0];
-        return [
-          bencoded.length,
-          +bencoded.slice(1, -1)
-        ];
-      case '0':
-      case '1':
-      case '2':
-      case '3':
-      case '4':
-      case '5':
-      case '6':
-      case '7':
-      case '8':
-      case '9':
-        stringLength = str.match(/^\d+/)[0];
-        startPos = stringLength.length + 1;
-        bencoded = str.slice(0, startPos + +stringLength);
-        return [
-          bencoded.length,
-          bencoded.slice(startPos)
-        ];
-      case 'l':
-        cursor = 1;
-        arr = function (accum$) {
-          var cache$, entry, entryLength;
-          while (str[cursor] !== 'e') {
-            cache$ = read(str.slice(cursor));
-            entryLength = cache$[0];
-            entry = cache$[1];
-            cursor += entryLength;
-            accum$.push(entry);
-          }
-          return accum$;
-        }.call(this, []);
-        return [
-          cursor + 1,
-          arr
-        ];
-      case 'd':
-        cursor = 1;
-        obj = {};
+(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Bencode = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
+// Generated by CoffeeScript 2.0.0-beta4
+void function () {
+  var read;
+  read = function (str) {
+    var arr, bencoded, cache$, cache$1, cursor, key, keyLength, obj, startPos, stringLength, value, valueLength;
+    switch (str[0]) {
+    case 'i':
+      bencoded = str.match(/^i-?\d+e/)[0];
+      return [
+        bencoded.length,
+        +bencoded.slice(1, -1)
+      ];
+    case '0':
+    case '1':
+    case '2':
+    case '3':
+    case '4':
+    case '5':
+    case '6':
+    case '7':
+    case '8':
+    case '9':
+      stringLength = str.match(/^\d+/)[0];
+      startPos = stringLength.length + 1;
+      bencoded = str.slice(0, startPos + +stringLength);
+      return [
+        bencoded.length,
+        bencoded.slice(startPos)
+      ];
+    case 'l':
+      cursor = 1;
+      arr = function (accum$) {
+        var cache$, entry, entryLength;
         while (str[cursor] !== 'e') {
           cache$ = read(str.slice(cursor));
-          keyLength = cache$[0];
-          key = cache$[1];
-          cache$1 = read(str.slice(cursor + keyLength));
-          valueLength = cache$1[0];
-          value = cache$1[1];
-          cursor += keyLength + valueLength;
-          obj[key] = value;
-        }
-        return [
-          cursor + 1,
-          obj
-        ];
-      }
-    };
-    module.exports = function (str) {
-      return read(str)[1];
-    };
-  });
-  require.define('/src/encode.coffee', function (module, exports, __dirname, __filename) {
-    var encode, encodeDictionary, encodeInteger, encodeList, encodeString;
-    encodeString = function (s) {
-      return '' + s.length + ':' + s;
-    };
-    encodeInteger = function (i) {
-      return 'i' + i + 'e';
-    };
-    encodeList = function (array) {
-      var encodedContents;
-      encodedContents = function (accum$) {
-        var object;
-        for (var i$ = 0, length$ = array.length; i$ < length$; ++i$) {
-          object = array[i$];
-          accum$.push(encode(object));
+          entryLength = cache$[0];
+          entry = cache$[1];
+          cursor += entryLength;
+          accum$.push(entry);
         }
         return accum$;
-      }.call(this, []).join('');
-      return 'l' + encodedContents + 'e';
-    };
-    encodeDictionary = function (object) {
-      var encodedContents, keys;
-      keys = function (accum$) {
-        var key;
-        for (key in object) {
-          if (!isOwn$(object, key))
-            continue;
-          accum$.push(key);
-        }
-        return accum$;
-      }.call(this, []).sort();
-      encodedContents = function (accum$) {
-        var key;
-        for (var i$ = 0, length$ = keys.length; i$ < length$; ++i$) {
-          key = keys[i$];
-          accum$.push('' + encode(key) + encode(object[key]));
-        }
-        return accum$;
-      }.call(this, []).join('');
-      return 'd' + encodedContents + 'e';
-    };
-    encode = function (object) {
-      switch (false) {
-      case !(typeof object === 'string'):
-        return encodeString(object);
-      case !(typeof object === 'number'):
-        return encodeInteger(0 | object);
-      case !('[object Array]' === {}.toString.call(object)):
-        return encodeList(object);
-      default:
-        return encodeDictionary(object);
+      }.call(this, []);
+      return [
+        cursor + 1,
+        arr
+      ];
+    case 'd':
+      cursor = 1;
+      obj = {};
+      while (str[cursor] !== 'e') {
+        cache$ = read(str.slice(cursor));
+        keyLength = cache$[0];
+        key = cache$[1];
+        cache$1 = read(str.slice(cursor + keyLength));
+        valueLength = cache$1[0];
+        value = cache$1[1];
+        cursor += keyLength + valueLength;
+        obj[key] = value;
       }
-    };
-    module.exports = encode;
-    function isOwn$(o, p) {
-      return {}.hasOwnProperty.call(o, p);
+      return [
+        cursor + 1,
+        obj
+      ];
     }
-  });
-  global.Bencode = require('/src/index.coffee');
-}.call(this, this));
+  };
+  module.exports = function (str) {
+    return read(str)[1];
+  };
+}.call(this);
+
+},{}],2:[function(require,module,exports){
+// Generated by CoffeeScript 2.0.0-beta8
+void function () {
+  var encode, encodeDictionary, encodeInteger, encodeList, encodeString;
+  encodeString = function (s) {
+    return '' + s.length + ':' + s;
+  };
+  encodeInteger = function (i) {
+    return 'i' + i + 'e';
+  };
+  encodeList = function (array) {
+    var encodedContents, object;
+    encodedContents = function (accum$) {
+      for (var i$ = 0, length$ = array.length; i$ < length$; ++i$) {
+        object = array[i$];
+        accum$.push(encode(object));
+      }
+      return accum$;
+    }.call(this, []).join('');
+    return 'l' + encodedContents + 'e';
+  };
+  encodeDictionary = function (object) {
+    var encodedContents, key, keys;
+    keys = function (accum$) {
+      for (key in object) {
+        if (!isOwn$(object, key))
+          continue;
+        accum$.push(key);
+      }
+      return accum$;
+    }.call(this, []).sort();
+    encodedContents = function (accum$1) {
+      for (var i$ = 0, length$ = keys.length; i$ < length$; ++i$) {
+        key = keys[i$];
+        accum$1.push('' + encode(key) + encode(object[key]));
+      }
+      return accum$1;
+    }.call(this, []).join('');
+    return 'd' + encodedContents + 'e';
+  };
+  encode = function (object) {
+    switch (false) {
+    case !(typeof object === 'string'):
+      return encodeString(object);
+    case !(typeof object === 'number'):
+      return encodeInteger(Math.floor(object));
+    case !('[object Array]' === {}.toString.call(object)):
+      return encodeList(object);
+    default:
+      return encodeDictionary(object);
+    }
+  };
+  module.exports = encode;
+  function isOwn$(o, p) {
+    return {}.hasOwnProperty.call(o, p);
+  }
+}.call(this);
+
+},{}],3:[function(require,module,exports){
+// Generated by CoffeeScript 2.0.0-beta4
+module.exports = {
+  encode: require('./encode'),
+  decode: require('./decode')
+};
+
+},{"./decode":1,"./encode":2}]},{},[3])(3)
+});
\ No newline at end of file