diff --git a/index.js b/index.js index ab89f0d..bb3d570 100644 --- a/index.js +++ b/index.js @@ -5,161 +5,159 @@ 'use strict'; var ASN1 = require('./asn1'), - Base64 = require('./base64'), - Hex = require('./hex'), - maxLength = 10240, - reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/, - tree = id('tree'), - dump = id('dump'), - wantHex = id('wantHex'), - area = id('area'), - file = id('file'), - examples = id('examples'), - selectTheme = id('theme-select'), - selectTag = id('tags'), - hash = null; + Base64 = require('./base64'), + Hex = require('./hex'), + tags = require('./tags'), + maxLength = 10240, + reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/, + tree = id('tree'), + dump = id('dump'), + wantHex = id('wantHex'), + area = id('area'), + file = id('file'), + examples = id('examples'), + selectTheme = id('theme-select'), + selectTag = id('tags'), + hash = null; require('./dom'); // side effect: augment ASN1 -if (!window.console || !window.console.log) - // IE8 with closed developer tools - window.console = { log: function () {} }; +if (!window.console || !window.console.log) // IE8 with closed developer tools + window.console = { log: function () {} }; function id(elem) { - return document.getElementById(elem); + return document.getElementById(elem); } function text(el, string) { - if ('textContent' in el) el.textContent = string; - else el.innerText = string; + if ('textContent' in el) el.textContent = string; + else el.innerText = string; } function decode(der, offset) { - offset = offset || 0; - tree.innerHTML = ''; - dump.innerHTML = ''; - try { - var asn1 = ASN1.decode(der, offset); - tree.appendChild(asn1.toDOM()); - if (wantHex.checked) dump.appendChild(asn1.toHexDOM()); - var b64 = der.length < maxLength ? asn1.toB64String() : ''; - if (area.value === '') area.value = Base64.pretty(b64); + offset = offset || 0; + tree.innerHTML = ''; + dump.innerHTML = ''; try { - window.location.hash = hash = '#' + b64; + var asn1 = ASN1.decode(der, offset); + tree.appendChild(asn1.toDOM()); + if (wantHex.checked) dump.appendChild(asn1.toHexDOM()); + var b64 = der.length < maxLength ? asn1.toB64String() : ''; + if (area.value === '') area.value = Base64.pretty(b64); + try { + window.location.hash = hash = '#' + b64; + } catch (e) { + // fails with "Access Denied" on IE with URLs longer than ~2048 chars + window.location.hash = hash = '#'; + } + var endOffset = asn1.posEnd(); + if (endOffset < der.length) { + var p = document.createElement('p'); + p.innerText = 'Input contains ' + (der.length - endOffset) + ' more bytes to decode.'; + var button = document.createElement('button'); + button.innerText = 'try to decode'; + button.onclick = function () { + decode(der, endOffset); + }; + p.appendChild(button); + tree.insertBefore(p, tree.firstChild); + } } catch (e) { - // fails with "Access Denied" on IE with URLs longer than ~2048 chars - window.location.hash = hash = '#'; - } - var endOffset = asn1.posEnd(); - if (endOffset < der.length) { - var p = document.createElement('p'); - p.innerText = 'Input contains ' + (der.length - endOffset) + ' more bytes to decode.'; - var button = document.createElement('button'); - button.innerText = 'try to decode'; - button.onclick = function () { - decode(der, endOffset); - }; - p.appendChild(button); - tree.insertBefore(p, tree.firstChild); + text(tree, e); } - } catch (e) { - text(tree, e); - } } function decodeText(val) { - try { - var der = reHex.test(val) ? Hex.decode(val) : Base64.unarmor(val); - decode(der); - } catch (e) { - text(tree, e); - dump.innerHTML = ''; - } + try { + var der = reHex.test(val) ? Hex.decode(val) : Base64.unarmor(val); + decode(der); + } catch (e) { + text(tree, e); + dump.innerHTML = ''; + } } function decodeBinaryString(str) { - var der; - try { - if (reHex.test(str)) der = Hex.decode(str); - else if (Base64.re.test(str)) der = Base64.unarmor(str); - else der = str; - decode(der); - } catch (e) { - text(tree, 'Cannot decode file.'); - dump.innerHTML = ''; - } + var der; + try { + if (reHex.test(str)) der = Hex.decode(str); + else if (Base64.re.test(str)) der = Base64.unarmor(str); + else der = str; + decode(der); + } catch (e) { + text(tree, 'Cannot decode file.'); + dump.innerHTML = ''; + } } // set up buttons id('butDecode').onclick = function () { - decodeText(area.value); + decodeText(area.value); }; id('butClear').onclick = function () { - area.value = ''; - file.value = ''; - tree.innerHTML = ''; - dump.innerHTML = ''; - hash = window.location.hash = ''; + area.value = ''; + file.value = ''; + tree.innerHTML = ''; + dump.innerHTML = ''; + hash = window.location.hash = ''; }; id('butExample').onclick = function () { - console.log('Loading example:', examples.value); - var request = new XMLHttpRequest(); - request.open('GET', 'examples/' + examples.value, true); - request.onreadystatechange = function () { - if (this.readyState !== 4) return; - if (this.status >= 200 && this.status < 400) { - area.value = this.responseText; - decodeText(this.responseText); - } else { - console.log('Error loading example.'); - } - }; - request.send(); + console.log('Loading example:', examples.value); + var request = new XMLHttpRequest(); + request.open('GET', 'examples/' + examples.value, true); + request.onreadystatechange = function () { + if (this.readyState !== 4) return; + if (this.status >= 200 && this.status < 400) { + area.value = this.responseText; + decodeText(this.responseText); + } else { + console.log('Error loading example.'); + } + }; + request.send(); }; // set dark theme depending on OS settings function setTheme() { - const storedTheme = localStorage.getItem('theme'); - let theme = 'os'; - if (storedTheme) - theme = storedTheme; - selectTheme.value = theme; - if (theme == 'os') { - const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)'); - theme = prefersDarkScheme.matches ? 'dark': 'light'; - } - const themeLink = document.querySelector('#theme-link'); - themeLink.href = (theme == 'dark') ? 'index-dark.css' : 'index.css'; + var storedTheme = localStorage.getItem('theme'); + var theme = 'os'; + if (storedTheme) + theme = storedTheme; + selectTheme.value = theme; + if (theme == 'os') { + var prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)'); + theme = prefersDarkScheme.matches ? 'dark': 'light'; + } + id('theme-link').href = (theme == 'dark') ? 'index-dark.css' : 'index.css'; } setTheme(); selectTheme.addEventListener('change', function () { - const selectedTheme = selectTheme.value; - localStorage.setItem('theme', selectedTheme); - setTheme(); + localStorage.setItem('theme', selectTheme.value); + setTheme(); }); // this is only used if window.FileReader function read(f) { - area.value = ''; // clear text area, will get b64 content - var r = new FileReader(); - r.onloadend = function () { - if (r.error) alert("Your browser couldn't read the specified file (error code " + r.error.code + ').'); - else decodeBinaryString(r.result); - }; - r.readAsBinaryString(f); + area.value = ''; // clear text area, will get b64 content + var r = new FileReader(); + r.onloadend = function () { + if (r.error) alert("Your browser couldn't read the specified file (error code " + r.error.code + ').'); + else decodeBinaryString(r.result); + }; + r.readAsBinaryString(f); } function load() { - if (file.files.length === 0) alert('Select a file to load first.'); - else read(file.files[0]); + if (file.files.length === 0) alert('Select a file to load first.'); + else read(file.files[0]); } function loadFromHash() { - if (window.location.hash && window.location.hash != hash) { - hash = window.location.hash; - // Firefox is not consistent with other browsers and returns an - // already-decoded hash string so we risk double-decoding here, - // but since % is not allowed in base64 nor hexadecimal, it's ok - var val = decodeURIComponent(hash.substr(1)); - if (val.length) decodeText(val); - } + if (window.location.hash && window.location.hash != hash) { + hash = window.location.hash; + // Firefox is not consistent with other browsers and returns an + // already-decoded hash string so we risk double-decoding here, + // but since % is not allowed in base64 nor hexadecimal, it's ok + var val = decodeURIComponent(hash.substr(1)); + if (val.length) decodeText(val); + } } function stop(e) { - e.stopPropagation(); - e.preventDefault(); + e.stopPropagation(); + e.preventDefault(); } function dragAccept(e) { - stop(e); - if (e.dataTransfer.files.length > 0) read(e.dataTransfer.files[0]); + stop(e); + if (e.dataTransfer.files.length > 0) read(e.dataTransfer.files[0]); } // main if ('onhashchange' in window) window.onhashchange = loadFromHash; @@ -167,20 +165,20 @@ loadFromHash(); document.ondragover = stop; document.ondragleave = stop; if ('FileReader' in window && 'readAsBinaryString' in new FileReader()) { - file.style.display = 'block'; - file.onchange = load; - document.ondrop = dragAccept; + file.style.display = 'block'; + file.onchange = load; + document.ondrop = dragAccept; } for (var tag in tags) { - var date = tags[tag]; - var el = document.createElement('option'); - el.value = tag; - el.innerText = date + " " + tag; - selectTag.appendChild(el); + var date = tags[tag]; + var el = document.createElement('option'); + el.value = tag; + el.innerText = date + " " + tag; + selectTag.appendChild(el); } selectTag.onchange = function (ev) { - var tag = ev.target.selectedOptions[0].value; - window.location.href = 'https://rawcdn.githack.com/lapo-luchini/asn1js/' + tag + '/index.html' -} + var tag = ev.target.selectedOptions[0].value; + window.location.href = 'https://rawcdn.githack.com/lapo-luchini/asn1js/' + tag + '/index.html'; +}; }); diff --git a/package.json b/package.json index 570b94c..ac33eb4 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "homepage": "https://lapo.it/asn1js/", "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "oids.js" ], "scripts" : { - "lint" : "npx eslint asn1.js base64.js hex.js int10.js oids.js index.js parseRFC.js dumpASN1.js", + "lint" : "npx eslint asn1.js base64.js hex.js int10.js oids.js tags.js index.js parseRFC.js dumpASN1.js", "test" : "node test" }, "devDependencies": { @@ -52,6 +52,16 @@ "rules": { "indent": "off" } + }, { + "files": [ "oids.js" ], + "rules": { + "indent": "off" + } + }, { + "files": [ "tags.js" ], + "rules": { + "comma-dangle": [ "error", "always-multiline" ] + } } ] } diff --git a/release.sh b/release.sh index e2664bc..4994f70 100755 --- a/release.sh +++ b/release.sh @@ -18,9 +18,16 @@ mtn automate tags it.lapo.asn1js | \ END { print date " " tag } ' done | sort -r | awk -v q='"' ' - BEGIN { print "var tags = {" } - { print " " q $2 q ":" q $1 q "," } - END { print "};" } + BEGIN { + print "(typeof define != " q "undefined" q " ? define : function (factory) { " q "use strict" q ";"; + print " if (typeof module == " q "object" q ") module.exports = factory();"; + print " else window.tags = factory();"; + print "})(function () {"; + print q "use strict" q ";"; + print "return {" + } + { print " " q $2 q ":" q $1 q "," } + END { print "};});" } ' > tags.js type gsha256sum >/dev/null && SHA256=gsha256sum || SHA256=sha256sum $SHA256 -t $FILES | gpg --clearsign > sha256sums.asc diff --git a/tags.js b/tags.js index c7aa6a6..e63004d 100644 --- a/tags.js +++ b/tags.js @@ -1,11 +1,16 @@ -var tags = { - "1.2.4":"2022-11-14", - "1.2.3":"2021-10-21", - "1.2.2":"2021-10-21", - "1.2.1":"2020-09-06", - "1.2.0":"2020-07-20", - "1.1.0":"2019-07-13", - "1.0.2":"2018-08-23", - "1.0.1":"2018-08-14", - "1.0.0":"2018-08-14", -}; +(typeof define != "undefined" ? define : function (factory) { "use strict"; + if (typeof module == "object") module.exports = factory(); + else window.tags = factory(); +})(function () { +"use strict"; +return{ + "1.2.4":"2022-11-14", + "1.2.3":"2021-10-21", + "1.2.2":"2021-10-21", + "1.2.1":"2020-09-06", + "1.2.0":"2020-07-20", + "1.1.0":"2019-07-13", + "1.0.2":"2018-08-23", + "1.0.1":"2018-08-14", + "1.0.0":"2018-08-14", +};});