Skip to content

Instantly share code, notes, and snippets.

@RubenVerborgh
Created July 24, 2017 18:54
Show Gist options
  • Save RubenVerborgh/7fdcc5d7d6b182ee7c533c289632ed32 to your computer and use it in GitHub Desktop.
Save RubenVerborgh/7fdcc5d7d6b182ee7c533c289632ed32 to your computer and use it in GitHub Desktop.
<!doctype html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="data:text/css;base64,LyogVmVuZG9yZWQgZnJvbSBodHRwczovL2dpdGh1Yi5jb20vcm1taC93ZWJ0cmVlbWFwL2Jsb2IvOWZhMGMwNjZhMTBlYTQ0MDJkOTYwYjBjNmMxYTQzMjg0NmFjN2ZjNC93ZWJ0cmVlbWFwLmNzcyAqLwoKLndlYnRyZWVtYXAtbm9kZSB7CiAgLyogUmVxdWlyZWQgYXR0cmlidXRlcy4gKi8KICBwb3NpdGlvbjogYWJzb2x1dGU7CiAgb3ZlcmZsb3c6IGhpZGRlbjsgICAvKiBUbyBoaWRlIG92ZXJsb25nIGNhcHRpb25zLiAqLwogIGJhY2tncm91bmQ6IHdoaXRlOyAgLyogTm9kZXMgbXVzdCBiZSBvcGFxdWUgZm9yIHpJbmRleCBsYXllcmluZy4gKi8KICBib3JkZXI6IHNvbGlkIDFweCBibGFjazsgIC8qIENhbGN1bGF0aW9ucyBhc3N1bWUgMXB4IGJvcmRlci4gKi8KCiAgLyogT3B0aW9uYWw6IENTUyBhbmltYXRpb24uICovCiAgdHJhbnNpdGlvbjogdG9wICAgIDAuM3MsCiAgICAgICAgICAgICAgbGVmdCAgIDAuM3MsCiAgICAgICAgICAgICAgd2lkdGggIDAuM3MsCiAgICAgICAgICAgICAgaGVpZ2h0IDAuM3M7Cn0KCi8qIE9wdGlvbmFsOiBoaWdobGlnaHQgbm9kZXMgb24gbW91c2VvdmVyLiAqLwoud2VidHJlZW1hcC1ub2RlOmhvdmVyIHsKICBiYWNrZ3JvdW5kOiAjZWVlOwp9CgovKiBPcHRpb25hbDogRGlmZmVyZW50IGJhY2tncm91bm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C1sZXZlbDAgewogIGJvcmRlcjogc29saWQgMXB4ICM0NDQ7Cn0KLndlYnRyZWVtYXAtbGV2ZWwxIHsKICBib3JkZXI6IHNvbGlkIDFweCAjNjY2Owp9Ci53ZWJ0cmVlbWFwLWxldmVsMiB7CiAgYm9yZGVyOiBzb2xpZCAxcHggIzg4ODsKfQoud2VidHJlZW1hcC1sZXZlbDMgewogIGJvcmRlcjogc29saWQgMXB4ICNhYWE7Cn0KLndlYnRyZWVtYXAtbGV2ZWw0IHsKICBib3JkZXI6IHNvbGlkIDFweCAjY2NjOwp9CgovKiBPcHRpb25hbDogc3R5bGluZyBvbiBub2RlIGNhcHRpb25zLiAqLwoud2VidHJlZW1hcC1jYXB0aW9uIHsKICBmb250LWZhbWlseTogc2Fucy1zZXJpZjsKICBmb250LXNpemU6IDExcHg7CiAgcGFkZGluZzogMnB4OwogIHRleHQtYWxpZ246IGNlbnRlcjsKfQoKLyogT3B0aW9uYWw6IHN0eWxpbmcgb24gY2FwdGlvbnMgb24gbW91c2UgaG92ZXIuICovCi8qLndlYnRyZWVtYXAtbm9kZTpob3ZlciA+IC53ZWJ0cmVlbWFwLWNhcHRpb24gewogIHRleHQtZGVjb3JhdGlvbjogdW5kZXJsaW5lOwp9Ki8K">
<title>solid-auth-client.min.js - Source Map Explorer</title>
<style>
html, body {
height: 100%;
}
body {
font-family: sans-serif;
font-size: 0.8em;
margin: 0;
}
#map {
top: 10px;
bottom: 10px;
left: 10px;
right: 10px;
position: absolute;
cursor: pointer;
-webkit-user-select: none;
}
</style>
</head>
<body>
<div id='map'></div>
</body>
<script>
// https://stackoverflow.com/a/18650828/388951
function formatBytes(bytes, decimals) {
if (bytes == 0) return '0 B';
var k = 1000,
dm = decimals || 2,
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
</script>
<script>
var tree = {
"//webpack/universalModuleDefinition": 455,
"//solid-auth-client.min.js": 13,
"webpack/bootstrap faa6bd79d58dfe98b320": 482,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/index.js": 150,
"//Users/ruben/oidc-rp/~/base64url/index.js": 54,
"//~/buffer/index.js": 19961,
"//(webpack)/buildin/global.js": 145,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/schemas/JWKSchema.js": 517,
"external \"crypto\"": 13,
"//~/assert/assert.js": 4380,
"//src/storage.js": 618,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/index.js": 234,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/jose/JWA.js": 814,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/JSONPointer.js": 2457,
"external \"URL\"": 13,
"//~/auth-header/dist/util.js": 322,
"//Users/ruben/oidc-rp/src/FormUrlEncoded.js": 398,
"//Users/ruben/oidc-rp/src/onHttpError.js": 259,
"//src/browser-util.js": 326,
"//src/session.js": 271,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/errors/DataError.js": 967,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/errors/NotSupportedError.js": 1017,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/errors/index.js": 54,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/jose/JWK.js": 1365,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/jose/JWS.js": 1451,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/schemas/Base64URLSchema.js": 84,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/schemas/JOSEHeaderSchema.js": 475,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/schemas/JWKSetSchema.js": 113,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/schemas/JWTClaimsSetSchema.js": 369,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/schemas/JWTSchema.js": 594,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/text-encoder/index.js": 66,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/Formats.js": 2555,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/Initializer.js": 7607,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/JSONPatch.js": 2288,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/Validator.js": 16410,
"//~/isomorphic-fetch/fetch-npm-browserify.js": 39,
"external \"fetch\"": 13,
"//src/api.js": 1183,
"//~/auth-header/dist/format.js": 1572,
"//~/auth-header/dist/index.js": 197,
"//~/auth-header/dist/parse.js": 676,
"//Users/ruben/oidc-rp/src/AuthenticationRequest.js": 1444,
"//Users/ruben/oidc-rp/src/AuthenticationResponse.js": 5733,
"//Users/ruben/oidc-rp/src/IDToken.js": 220,
"//Users/ruben/oidc-rp/src/IDTokenSchema.js": 372,
"//Users/ruben/oidc-rp/src/RelyingParty.js": 3399,
"//Users/ruben/oidc-rp/src/RelyingPartySchema.js": 616,
"//Users/ruben/oidc-rp/src/index.js": 18,
"//src/authn-fetch.js": 610,
"//src/index.js": 270,
"//src/webid-oidc.js": 1642,
"//src/webid-tls.js": 179,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/algorithms/HMAC.js": 893,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/algorithms/RSASSA-PKCS1-v1_5.js": 1087,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/algorithms/SupportedAlgorithms.js": 724,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/algorithms/index.js": 1270,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/formats/index.js": 259,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/jose/JWKSet.js": 1737,
"//Users/ruben/oidc-rp/~/@trust/jose/lib/jose/JWT.js": 3042,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/JSONDocument.js": 1308,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/JSONMapping.js": 769,
"//Users/ruben/oidc-rp/~/@trust/json-document/lib/JSONSchema.js": 1478,
"//Users/ruben/oidc-rp/~/base64url/dist/base64url.js": 541,
"//Users/ruben/oidc-rp/~/base64url/dist/pad-string.js": 201,
"//~/base64-js/index.js": 1338,
"//~/ieee754/index.js": 868,
"//~/isarray/index.js": 91,
"//~/process/browser.js": 1671,
"//~/util/~/inherits/inherits_browser.js": 298,
"//~/util/support/isBufferBrowser.js": 139,
"//~/util/util.js": 6051,
"//~/whatwg-fetch/fetch.js": 7237,
"external \"TextEncoder\"": 17
};
</script>
<script src="data:application/javascript;base64,Ly8gICAgIFVuZGVyc2NvcmUuanMgMS44LjMKLy8gICAgIGh0dHA6Ly91bmRlcnNjb3JlanMub3JnCi8vICAgICAoYykgMjAwOS0yMDE1IEplcmVteSBBc2hrZW5hcywgRG9jdW1lbnRDbG91ZCBhbmQgSW52ZXN0aWdhdGl2ZSBSZXBvcnRlcnMgJiBFZGl0b3JzCi8vICAgICBVbmRlcnNjb3JlIG1heSBiZSBmcmVlbHkgZGlzdHJpYnV0ZWQgdW5kZXIgdGhlIE1JVCBsaWNlbnNlLgoKKGZ1bmN0aW9uKCkgewoKICAvLyBCYXNlbGluZSBzZXR1cAogIC8vIC0tLS0tLS0tLS0tLS0tCgogIC8vIEVzdGFibGlzaCB0aGUgcm9vdCBvYmplY3QsIGB3aW5kb3dgIGluIHRoZSBicm93c2VyLCBvciBgZXhwb3J0c2Agb24gdGhlIHNlcnZlci4KICB2YXIgcm9vdCA9IHRoaXM7CgogIC8vIFNhdmUgdGhlIHByZXZpb3VzIHZhbHVlIG9mIHRoZSBgX2AgdmFyaWFibGUuCiAgdmFyIHByZXZpb3VzVW5kZXJzY29yZSA9IHJvb3QuXzsKCiAgLy8gU2F2ZSBieXRlcyBpbiB0aGUgbWluaWZpZWQgKGJ1dCBub3QgZ3ppcHBlZCkgdmVyc2lvbjoKICB2YXIgQXJyYXlQcm90byA9IEFycmF5LnByb3RvdHlwZSwgT2JqUHJvdG8gPSBPYmplY3QucHJvdG90eXBlLCBGdW5jUHJvdG8gPSBGdW5jdGlvbi5wcm90b3R5cGU7CgogIC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WFkaW5nID09PSBmYWxzZSA/IDAgOiBfLm5vdygpOwogICAgICB0aW1lb3V0ID0gbnVsbDsKICAgICAgcmVzdWx0ID0gZnVuYy5hcHBseShjb250ZXh0LCBhcmdzKTsKICAgICAgaWYgKCF0aW1lb3V0KSBjb250ZXh0ID0gYXJncyA9IG51bGw7CiAgICB9OwogICAgcmV0dXJuIGZ1bmN0aW9uKCkgewogICAgICB2YXIgbm93ID0gXy5ub3coKTsKICAgICAgaWYgKCFwcmV2aW91cyAmJiBvcHRpb25zLmxlYWRpbmcgPT09IGZhbHNlKSBwcmV2aW91cyA9IG5vdzsKICAgICAgdmFyIHJlbWFpbmluZyA9IHdhaXQgLSAobm93IC0gcHJldmlvdXMpOwogICAgICBjb250ZXh0ID0gdGhpczsKICAgICAgYXJncyA9IGFyZ3VtZW50czsKICAgICAgaWYgKHJlbWFpbmluZyA8PSAwIHx8IHJlbWFpbmluZyA+IHdhaXQpIHsKICAgICAgICBpZiAodGltZW91dCkgewogICAgICAgICAgY2xlYXJUaW1lb3V0KHRpbWVvdXQpOwogICAgICAgICAgdGltZW91dCA9IG51bGw7CiAgICAgICAgfQogICAgICAgIHByZXZpb3VzID0gbm93OwogICAgICAgIHJlc3VsdCA9IGZ1bmMuYXBwbHkoY29udGV4dCwgYXJncyk7CiAgICAgICAgaWYgKCF0aW1lb3V0KSBjb250ZXh0ID0gYXJncyA9IG51bGw7CiAgICAgIH0gZWxzZSBpZiAoIXRpbWVvdXQgJiYgb3B0aW9ucy50cmFpbGluZyAhPT0gZmFsc2UpIHsKICAgICAgICB0aW1lb3V0ID0gc2V0VGltZW91dChsYXRlciwgcmVtYWluaW5nKTsKICAgICAgfQogICAgICByZXR1cm4gcmVzdWx0OwogICAgfTsKICB9OwoKICAvLyBSZXR1cm5zIGEgZnVuY3Rpb24sIHRoYXQsIGFzIGxvbmcgYXMgaXQgY29udGludWVzIHRvIGJlIGludm9rZWQsIHdpbGwgbm90CiAgLy8gYmUgdHJpZ2dlcmVkL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JsZVByb3BzW25vbkVudW1JZHhdOwogICAgICBpZiAocHJvcCBpbiBvYmogJiYgb2JqW3Byb3BdICE9PSBwcm90b1twcm9wXSAmJiAhXy5jb250YWlucyhrZXlzLCBwcm9wKSkgewogICAgICAgIGtleXMucHVzaChwcm9wKTsKICAgICAgfQogICAgfQogIH0KCiAgLy8gUmV0cmlldmUgdGhlIG5hbWVzIG9mIGFuIG9iamVjdCdzIG93biBwcm9wZXJ0aWVzLgogIC8vIERlbGVnYXRlcyB0byAqKkVDTUFTY3JpcHQgNSoqJ3MgbmF0aXZlIGBPYmplY3Qua2V5c2AKICBfLmtleXMgPSBmdW5jdGlvbihvYmopIHsKICAgIGlmICghXy5pc09iamVjdChvYmopKSByZXR1cm4gW107CiAgICBpZiAobmF0aXZlS2V5cykgcmV0dXJuIG5hdGl2ZUtleXMob2JqKTsKICAgIHZhciBrZXlzID0gW107CiAgICBmb3IgKHZhciBrZXkgaW4gb2JqKSBpZiAoXy5oYXMob2JqLCBrZXkpKSBrZXlzLnB1c2goa2V5KTsKICAgIC8vIEFoZW0sIElFIDwgOS4KICAgIGlmIChoYXNFbnVtQnVnKSBjb2xsZWN0Tm9uRW51bVByb3BzKG9iaiwga2V5cyk7CiAgICByZXR1cm4ga2V5czsKICB9OwoKICAvLyBSZXRyaWV2ZSBhbGwgdGhlIHByb3BlcnR5IG5hbWVzIG9mIGFuIG9iamVjdC4KICBfLmFsbEtleXMgPSBmdW5jdGlvbihvYmopIHsKICAgIGlmICghXy5pc09iamVjdChvYmopKSByZXR1cm4gW107CiAgICB2YXIga2V5cyA9IFtdOwogICAgZm9yICh2YXIga2V5IGluIG9iaikga2V5cy5wdXNoKGtleSk7CiAgICAvLyBBaGVtLCBJRSA8IDkuCiAgICBpZiAoaGFzRW51bUJ1ZykgY29sbGVjdE5vbkVudW1Qcm9wcyhvYmosIGtleXMpOwogICAgcmV0dXJuIGtleXM7CiAgfTsKCiAgLy8gUmV0cmlldmUgdGhlIHZhbHVlcyBvZi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SArICIpKT09bnVsbD8nJzpfLmVzY2FwZShfX3QpKStcbiciOwogICAgICB9IGVsc2UgaWYgKGludGVycG9sYXRlKSB7CiAgICAgICAgc291cmNlICs9ICInK1xuKChfX3Q9KCIgKyBpbnRlcnBvbGF0ZSArICIpKT09bnVsbD8nJzpfX3QpK1xuJyI7CiAgICAgIH0gZWxzZSBpZiAoZXZhbHVhdGUpIHsKICAgICAgICBzb3VyY2UgKz0gIic7XG4iICsgZXZhbHVhdGUgKyAiXG5fX3ArPSciOwogICAgICB9CgogICAgICAvLyBBZG9iZSBWTXMgbmVlZCB0aGUgbWF0Y2ggcmV0dXJuZWQgdG8gcHJvZHVjZSB0aGUgY29ycmVjdCBvZmZlc3QuCiAgICAgIHJldHVybiBtYXRjaDsKICAgIH0pOwogICAgc291cmNlICs9ICInO1xuIjsKCiAgICAvLyBJZiBhIHZhcmlhYmxlIGlzIG5vdCBzcGVjaWZpZWQsIHBsYWNlIGRhdGEgdmFsdWVzIGluIGxvY2FsIHNjb3BlLgogICAgaWYgKCFzZXR0aW5ncy52YXJpYWJsZSkgc291cmNlID0gJ3dpdGgob2JqfHx7fSl7XG4nICsgc291cmNlICsgJ31cbic7CgogICAgc291cmNlID0gInZhciBfX3QsX19wPScnLF9faj1BcnJheS5wcm90b3R5cGUuam9pbiwiICsKICAgICAgInByaW50PWZ1bmN0aW9uKCl7X19wKz1fX2ouY2FsbChhcmd1bWVudHMsJycpO307XG4iICsKICAgICAgc291cmNlICsgJ3JldHVybiBfX3A7XG4nOwoKICAgIHRyeSB7CiAgICAgIHZhciByZW5kZXIgPSBuZXcgRnVuY3Rpb24oc2V0dGluZ3MudmFyaWFibGUgfHwgJ29iaicsICdfJywgc291cmNlKTsKICAgIH0gY2F0Y2ggKGUpIHsKICAgICAgZS5zb3VyY2UgPSBzb3VyY2U7CiAgICAgIHRocm93IGU7CiAgICB9CgogICAgdmFyIHRlbXBsYXRlID0gZnVuY3Rpb24oZGF0YSkgewogICAgICByZXR1cm4gc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QgPSBlbmQgLSAxOwogIH0KfQoKZnVuY3Rpb24gYXBwZW5kVHJlZW1hcChkb20sIGRhdGEpIHsKICB2YXIgc3R5bGUgPSBnZXRDb21wdXRlZFN0eWxlKGRvbSwgbnVsbCk7CiAgdmFyIHdpZHRoID0gcGFyc2VJbnQoc3R5bGUud2lkdGgpOwogIHZhciBoZWlnaHQgPSBwYXJzZUludChzdHlsZS5oZWlnaHQpOwogIGlmICghZGF0YS5kb20pCiAgICBtYWtlRG9tKGRhdGEsIDApOwogIGRvbS5hcHBlbmRDaGlsZChkYXRhLmRvbSk7CiAgcG9zaXRpb24oZGF0YS5kb20sIDAsIDAsIHdpZHRoLCBoZWlnaHQpOwogIGxheW91dChkYXRhLCAwLCB3aWR0aCwgaGVpZ2h0KTsKfQoKd2luZG93LmFwcGVuZFRyZWVtYXAgPSBhcHBlbmRUcmVlbWFwOwp9KSh3aW5kb3cpOwo="></script>
<script>
function newNode(name) {
return {
name: name,
data: {
'$area': 0
},
children: []
};
}
var treeData = newNode('/');
function addNode(path, size) {
var parts = path.split('/');
var node = treeData;
node.data['$area'] += size;
parts.forEach(function(part) {
var child = _.find(node.children, function(child) { return child.name == part; });
if (!child) {
var child = newNode(part);
node.children.push(child);
}
node = child;
node.data['$area'] += size;
});
}
function addSizeToTitle(node, total) {
var size = node.data['$area'],
pct = 100.0 * size / total;
node.name += ' • ' + formatBytes(size) + ' • ' + pct.toFixed(1) + '%';
node.children.forEach(function(x) { addSizeToTitle(x, total) });
}
for (var source in tree) {
addNode(source, tree[source]);
}
addSizeToTitle(treeData, treeData.data['$area']);
var map = document.getElementById('map');
appendTreemap(map, treeData);
window.addEventListener('resize', function() {
appendTreemap(map, treeData);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment