Comparing version 0.3.3 to 0.3.5
@@ -1,2 +0,2 @@ | ||
/*! (C) 2017 by Andrea Giammarchi */ | ||
/*! (C) 2017 Andrea Giammarchi */ | ||
if (typeof module === 'object') { | ||
@@ -12,11 +12,6 @@ module.constructor.prototype.import = function (path) { | ||
var | ||
remotePath = /^[a-zA-Z_-]/, | ||
__filename = info._ || el.getAttribute('data-main').replace(remotePath, './$&'), | ||
npmPath = /^[a-zA-Z_-]/, | ||
__filename = info._ || el.getAttribute('data-main').replace(npmPath, './$&'), | ||
normalize = function (url) { | ||
if (remotePath.test(url)) { | ||
i = url.indexOf('/'); | ||
length = url.length; | ||
url = 'https://unpkg.com/' + url.slice(0, i < 0 ? length : i) + | ||
'@latest' + (i < 0 ? '' : url.slice(i)); | ||
} | ||
if (npmPath.test(url)) url = gModule._path(url); | ||
for (var | ||
@@ -47,22 +42,20 @@ abs = /^(?:[a-z]+:)?\/\//.test(url), | ||
}, | ||
onload = function () { | ||
onload = function (xhr, path, resolve) { | ||
var | ||
path = this._, | ||
resolve = this.$, | ||
html = document.documentElement, | ||
script = document.createElement('script') | ||
; | ||
script.setAttribute('nonce', window.module._nonce); | ||
script.setAttribute('nonce', gModule._nonce); | ||
script.textContent = 'module.$(function(){' + | ||
'var module=' + CommonJS + '(arguments[0]),' + | ||
'var module=' + gModule._cjs + '(arguments[0]),' + | ||
'__filename=module.filename,' + | ||
'__dirname=__filename.slice(0,__filename.lastIndexOf("/")),' + | ||
'require=module.require,' + | ||
'exports=module.exports;(function(){"use strict";' + | ||
this.responseText + | ||
'exports=module.exports;(function(){"use strict";\n' + | ||
xhr.responseText + | ||
';\n}.call(exports));return module.exports;' | ||
+ '}(module));'; | ||
window.module._ = path; | ||
window.module.$ = function (exports) { | ||
resolve(window.module._cache[path] = exports); | ||
gModule._ = path; | ||
gModule.$ = function (exports) { | ||
resolve(gModule._cache[path] = exports); | ||
}; | ||
@@ -74,9 +67,28 @@ // cleanup after, no matter what | ||
}, | ||
error = function (path, xhr) { | ||
throw (gModule._cache[path] = new Error(xhr.statusText)); | ||
}, | ||
load = function (path) { | ||
var xhr = new XMLHttpRequest(), module; | ||
xhr.open('GET', path, false); | ||
xhr._ = path; | ||
xhr.$ = function (exports) { module = exports; }; | ||
xhr.send(null); | ||
onload.call(xhr); | ||
var | ||
remote = path, | ||
m = /^((?:[a-z]+?:\/\/)?[^/]+)\/([^@]+)@latest(\/.*)?$/.exec(path), | ||
resolve = function (exports) { module = exports; }, | ||
xhr = new XMLHttpRequest(), | ||
module | ||
; | ||
if (m) { | ||
// hopefully soon ... | ||
// xhr.open('GET', 'https://registry.npmjs.org/' + m[2] + '/latest', false); | ||
// meanwhile ... | ||
xhr.open('GET', 'http://www.3site.eu/latest/?@=' + m[2], false); | ||
xhr.send(null); | ||
if (xhr.status < 400) module = JSON.parse(xhr.responseText); | ||
else return error(path, xhr); | ||
remote = m[1] + '/' + m[2] + '@' + module.version + (m[3] || ''); | ||
} | ||
xhr = new XMLHttpRequest(); | ||
xhr.open('GET', remote, false); | ||
xhr.send(module = null); | ||
if (xhr.status < 400) onload(xhr, path, resolve); | ||
else error(path, xhr); | ||
return module; | ||
@@ -90,3 +102,3 @@ }, | ||
var path = normalize(url); | ||
return window.module._cache[path] || load(path); | ||
return gModule._cache[path] || load(path); | ||
}, | ||
@@ -96,11 +108,13 @@ import: function (url) { | ||
return Promise.resolve( | ||
window.module._cache[path] || | ||
(window.module._cache[path] = new Promise( | ||
gModule._cache[path] || | ||
(gModule._cache[path] = new Promise( | ||
function (resolve, reject) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', path, true); | ||
xhr._ = path; | ||
xhr.$ = resolve; | ||
xhr.onerror = reject; | ||
xhr.onload = onload; | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState == 4) { | ||
if (xhr.status < 400) onload(xhr, path, resolve); | ||
else reject(new Error(xhr.statusText)); | ||
} | ||
}; | ||
xhr.send(null); | ||
@@ -111,13 +125,21 @@ } | ||
} | ||
} | ||
}, | ||
gModule = window.module || module | ||
; | ||
if (!window.module) { | ||
if (gModule === module) { | ||
window.global = window; | ||
window.module = module; | ||
window.process = {browser: true}; | ||
module._cache = Object.create(null); | ||
module._nonce = el.getAttribute('nonce'); | ||
module._cjs = '' + CommonJS; | ||
module._path = function (url) { | ||
var i = url.indexOf('/'), length = url.length; | ||
return 'https://unpkg.com/' + url.slice(0, i < 0 ? length : i) + | ||
'@latest' + (i < 0 ? '' : url.slice(i)); | ||
}; | ||
module.import('./' + __filename.split('/').pop()); | ||
} | ||
return module; | ||
}({_:null}, document.getElementById('common-js'))); | ||
}({_:''}, document.getElementById('common-js'))); | ||
} |
@@ -1,2 +0,2 @@ | ||
/*! (C) 2017 by Andrea Giammarchi */ | ||
"object"==typeof module?module.constructor.prototype.import=function(e){var n=this;return new Promise(function(t){t(n.require(e))})}:!function e(n,t){var o=/^[a-zA-Z_-]/,i=n._||t.getAttribute("data-main").replace(o,"./$&"),r=function(e){o.test(e)&&(u=e.indexOf("/"),l=e.length,e="https://unpkg.com/"+e.slice(0,u<0?l:u)+"@latest"+(u<0?"":e.slice(u)));for(var n,t=/^(?:[a-z]+:)?\/\//.test(e),r=t?e:i.slice(0,i.lastIndexOf("/")),l=t?0:e.length,u=0,c=0;u<l;c=u+1)if(u=e.indexOf("/",c),u<0)u=l,r+="/"+e.slice(c),/\.js$/i.test(r)||(r+=".js");else if(0===u)r="";else{for(n=c,c=u;c&&"."===e.charAt(c-1);)--c;switch(u-c){case 0:r+="/"+e.slice(n,u);break;case 1:break;case 2:r=r.slice(0,r.lastIndexOf("/"))}}return r},l=function(){var n=this._,t=this.$,o=document.documentElement,i=document.createElement("script");i.setAttribute("nonce",window.module._nonce),i.textContent="module.$(function(){var module="+e+'(arguments[0]),__filename=module.filename,__dirname=__filename.slice(0,__filename.lastIndexOf("/")),require=module.require,exports=module.exports;(function(){"use strict";'+this.responseText+";\n}.call(exports));return module.exports;}(module));",window.module._=n,window.module.$=function(e){t(window.module._cache[n]=e)},setTimeout(function(){o.removeChild(i)},1),o.appendChild(i)},u=function(e){var n,t=new XMLHttpRequest;return t.open("GET",e,!1),t._=e,t.$=function(e){n=e},t.send(null),l.call(t),n},c={},s={filename:i,exports:c,require:function(e){var n=r(e);return window.module._cache[n]||u(n)},import:function(e){var n=r(e);return Promise.resolve(window.module._cache[n]||(window.module._cache[n]=new Promise(function(e,t){var o=new XMLHttpRequest;o.open("GET",n,!0),o._=n,o.$=e,o.onerror=t,o.onload=l,o.send(null)})))}};return window.module||(window.global=window,window.module=s,s._cache=Object.create(null),s._nonce=t.getAttribute("nonce"),s.import("./"+i.split("/").pop())),s}({_:null},document.getElementById("common-js")); | ||
/*! (C) 2017 Andrea Giammarchi */ | ||
"object"==typeof module?module.constructor.prototype["import"]=function(e){var t=this;return new Promise(function(n){n(t.require(e))})}:!function e(t,n){var r=/^[a-zA-Z_-]/,o=t._||n.getAttribute("data-main").replace(r,"./$&"),s=function(e){r.test(e)&&(e=m._path(e));for(var t,n=/^(?:[a-z]+:)?\/\//.test(e),s=n?e:o.slice(0,o.lastIndexOf("/")),c=n?0:e.length,i=0,u=0;i<c;u=i+1)if(i=e.indexOf("/",u),i<0)i=c,s+="/"+e.slice(u),/\.js$/i.test(s)||(s+=".js");else if(0===i)s="";else{for(t=u,u=i;u&&"."===e.charAt(u-1);)--u;switch(i-u){case 0:s+="/"+e.slice(t,i);break;case 1:break;case 2:s=s.slice(0,s.lastIndexOf("/"))}}return s},c=function(e,t,n){var r=document.documentElement,o=document.createElement("script");o.setAttribute("nonce",m._nonce),o.textContent="module.$(function(){var module="+m._cjs+'(arguments[0]),__filename=module.filename,__dirname=__filename.slice(0,__filename.lastIndexOf("/")),require=module.require,exports=module.exports;(function(){"use strict";\n'+e.responseText+";\n}.call(exports));return module.exports;}(module));",m._=t,m.$=function(e){n(m._cache[t]=e)},setTimeout(function(){r.removeChild(o)},1),r.appendChild(o)},i=function(e,t){throw m._cache[e]=new Error(t.statusText)},u=function(e){var t,n=e,r=/^((?:[a-z]+?:\/\/)?[^\/]+)\/([^@]+)@latest(\/.*)?$/.exec(e),o=function(e){t=e},s=new XMLHttpRequest;if(r){if(s.open("GET","http://www.3site.eu/latest/?@="+r[2],!1),s.send(null),!(s.status<400))return i(e,s);t=JSON.parse(s.responseText),n=r[1]+"/"+r[2]+"@"+t.version+(r[3]||"")}return s=new XMLHttpRequest,s.open("GET",n,!1),s.send(t=null),s.status<400?c(s,e,o):i(e,s),t},a={},l={filename:o,exports:a,require:function(e){var t=s(e);return m._cache[t]||u(t)},"import":function(e){var t=s(e);return Promise.resolve(m._cache[t]||(m._cache[t]=new Promise(function(e,n){var r=new XMLHttpRequest;r.open("GET",t,!0),r.onreadystatechange=function(){4==r.readyState&&(r.status<400?c(r,t,e):n(new Error(r.statusText)))},r.send(null)})))}},m=window.module||l;return m===l&&(window.global=window,window.module=l,window.process={browser:!0},l._cache=Object.create(null),l._nonce=n.getAttribute("nonce"),l._cjs=""+e,l._path=function(e){var t=e.indexOf("/"),n=e.length;return"https://unpkg.com/"+e.slice(0,t<0?n:t)+"@latest"+(t<0?"":e.slice(t))},l["import"]("./"+o.split("/").pop())),l}({_:""},document.getElementById("common-js")); |
@@ -1,1 +0,1 @@ | ||
Eop9ZZOXdqXHDKWk8zebBDEZC98wh/cKy+DVef4ISKE= | ||
Dc2Yi71baawxeNAkVCdXfw0xROndLWentxGqnJZsAyY= |
@@ -1,1 +0,1 @@ | ||
cS9HV5fgB+uA6f9pQWLC7ZIrzWMrwkDCgAhGUTxX4ss= | ||
OTkZQBXHHw/9zkezKj8lefsM6OCnZh9YcMxSYbSxwOw= |
{ | ||
"name": "common-js", | ||
"version": "0.3.3", | ||
"version": "0.3.5", | ||
"description": "module.exports and module.import for browsers too", | ||
@@ -10,3 +10,3 @@ "main": "common.js", | ||
"size": "cat common.js | wc -c;cat common.min.js | wc -c;gzip -c common.min.js | wc -c", | ||
"minify": "uglifyjs common.js --comments=/^!/ --compress --mangle -o common.min.js", | ||
"minify": "uglifyjs common.js --support-ie8 --comments=/^!/ --compress --mangle -o common.min.js", | ||
"nonce": "cat common.js | openssl dgst -sha256 -binary | base64 > nonce.txt;cat common.min.js | openssl dgst -sha256 -binary | base64 > nonce.min.txt" | ||
@@ -13,0 +13,0 @@ }, |
@@ -8,3 +8,3 @@ # CommonJS + module.import() [![build status](https://secure.travis-ci.org/WebReflection/common-js.svg)](http://travis-ci.org/WebReflection/common-js) | ||
<sub>Yes, it is secure too, check the [CSP enabled page](https://webreflection.github.io/common-js/)!<sub> | ||
<sub>Yes, it is secure too, check the [CSP enabled page](https://webreflection.github.io/common-js/csp.html)!<sub> | ||
@@ -79,2 +79,14 @@ Don't miss [the introductory blog post](https://medium.com/@WebReflection/asynchronous-module-import-path-b9f56675e109#.8nsdv9571) about this idea! | ||
### Compatibility | ||
You can test your target directly through the [live test page](https://webreflection.github.io/common-js/). | ||
What I could test was the following: | ||
**Mobile** Android 2+, iOS5+, WP7+, BBOS7+, FFOS1+, WebOS2+, Kindle Paper & Fire | ||
**Desktop** Chrome, FF, Safari, Opera, IE9+ (theoretically IE8 too but it needs few polyfills upfront) | ||
### What else? | ||
@@ -95,3 +107,3 @@ The synchronous `require` and both `__filename` and `__dirname` are also exposed, but nothing else from NodeJS core is available. | ||
* **Is there a CDN I can use to test?** | ||
There is always one for npm modules. [https://unpkg.com/common-js@latest](https://unpkg.com/common-js@0.3.3/common.min.js) should be already OK. | ||
There is always one for npm modules. [https://unpkg.com/common-js@latest](https://unpkg.com/common-js@latest/common.min.js) should be already OK. | ||
* **Is this using eval?** | ||
@@ -98,0 +110,0 @@ No. It's using a technique that is even compatible with highest security standards such [Content Security Policy](https://w3c.github.io/webappsec-csp/) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14245
9
139
112
1