browser-module-sandbox
Advanced tools
Comparing version 1.2.1 to 1.3.0
69
index.js
@@ -6,2 +6,3 @@ var inherits = require('inherits') | ||
var detective = require('detective') | ||
var createCache = require('browser-module-cache') | ||
@@ -24,2 +25,3 @@ module.exports = function(opts) { | ||
"</style>" | ||
this.cache = createCache() | ||
} | ||
@@ -33,19 +35,10 @@ | ||
var body = { | ||
"options": { | ||
"debug": true | ||
}, | ||
"dependencies": {} | ||
} | ||
modules.map(function(module) { | ||
var version = preferredVersions[module] || 'latest' | ||
body.dependencies[module] = version | ||
}) | ||
self.emit('bundleStart') | ||
if (modules.length === 0) return makeIframe(entry) | ||
request({method: "POST", body: body, url: this.cdn + '/multi', json: true}, function(err, resp, json) { | ||
if (modules.length === 0) return makeIframe() | ||
var allBundles = '' | ||
var packages = [] | ||
self.cache.get(function(err, cached) { | ||
if (err) { | ||
@@ -55,5 +48,33 @@ self.emit('bundleEnd') | ||
} | ||
var allBundles = '' | ||
var packages = [] | ||
var download = [] | ||
modules.forEach(function(module) { | ||
if (cached[module]) { | ||
allBundles += cached[module]['bundle'] | ||
packages.push(cached[module]['package']) | ||
} else { | ||
download.push(module) | ||
} | ||
}) | ||
if (download.length === 0) return makeIframe(allBundles) | ||
var body = { | ||
"options": { | ||
"debug": true | ||
}, | ||
"dependencies": {} | ||
} | ||
download.map(function(module) { | ||
var version = preferredVersions[module] || 'latest' | ||
body.dependencies[module] = version | ||
}) | ||
request({method: "POST", body: body, url: self.cdn + '/multi', json: true}, downloadedModules) | ||
}) | ||
function downloadedModules(err, resp, json) { | ||
if (err) { | ||
self.emit('bundleEnd') | ||
return err | ||
} | ||
Object.keys(json).map(function(module) { | ||
@@ -63,9 +84,11 @@ allBundles += json[module]['bundle'] | ||
}) | ||
self.emit('modules', packages) | ||
makeIframe(allBundles + entry) | ||
}) | ||
self.cache.put(json, function() { | ||
self.emit('modules', packages) | ||
makeIframe(allBundles) | ||
}) | ||
} | ||
function makeIframe(script) { | ||
script = script + entry | ||
// setTimeout is because iframes report inaccurate window.innerWidth/innerHeight, even after DOMContentLoaded! | ||
@@ -72,0 +95,0 @@ var body = self.iframeBody + |
{ | ||
"name": "browser-module-sandbox", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "uses browserify-cdn to run node code in an iframe", | ||
@@ -13,3 +13,4 @@ "repository": { | ||
"inherits": "1.0.0", | ||
"detective": "~2.1.2" | ||
"detective": "~2.1.2", | ||
"browser-module-cache": "0.1.1" | ||
}, | ||
@@ -16,0 +17,0 @@ "engines": { |
4726
85
5
+ Addedbrowser-module-cache@0.1.1
+ Addedabstract-leveldown@0.7.2(transitive)
+ Addedbrowser-module-cache@0.1.1(transitive)
+ Addedidb-wrapper@1.2.0(transitive)
+ Addedisbuffer@0.0.0(transitive)
+ Addedlevel-js@1.0.8(transitive)