git ssb

0+

Dominic / web-bootloader



Commit 0a6ed44106dda70fdaeda1032dd46186c5406562

static

Dominic Tarr committed on 7/26/2016, 1:28:11 AM
Parent: dec8839bab146f6a8b8caa881c2edab80cfa8e4f

Files changed

index.htmlchanged
index.htmlView
@@ -1,23 +1,45 @@
11 <!DOCTYPE html>
2-<html>
3-<head>
2 +<html manifest="./manifest.appcache"><head>
43 <title>---</title>
54 <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
65 <meta charset=utf-8></head>
76 <body></body>
87 <script>
98 (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){
9 +;(function () {
10 +
1011 var BinaryXHR = require('binary-xhr')
1112 var toBase64 = require('arraybuffer-base64')
1213
14 +var Progress = require('hyperprogress')
15 +
16 +var prog = Progress()
17 +var running = false
18 +
19 +window.WebBoot = {
20 + scorchedEarth: scorchedEarth,
21 + reinitialize: reinitialize,
22 + add: add,
23 + run: run
24 +}
25 +
26 +function parse (str) {
27 + try {
28 + return JSON.parse(str)
29 + } catch (_) { }
30 +}
31 +
32 +document.body.appendChild(prog)
33 +
1334 //split the hash.
1435
1536 var parts = window.location.hash.split('#').slice(1)
1637 var hasHash = /([A-Za-z0-9\/+]{43}=)\.sha256/
1738 var isUrl = /^https?:\/\//
1839
1940 var APPNAME = 'SWB'
41 +var match = new RegExp('^'+APPNAME)
2042
2143 function log (msg) {
2244 console.log(APPNAME, msg)
2345 }
@@ -34,15 +56,33 @@
3456 cb(err)
3557 })
3658 }
3759
38-function run (id) {
60 +function reinitialize () {
61 + for(var k in localStorage) {
62 + if(match.test(k))
63 + delete localStorage[k]
64 + }
65 +}
66 +
67 +//destroy everything
68 +function scorchedEarth () {
69 + for(var k in localStorage) {
70 + delete localStorage[k]
71 + }
72 +}
73 +
74 +function run (id, cb) {
75 + //if we are already running, restart
76 + if(running) {
77 + //leave a note in local storage, and restart.
78 + location.hash = '#run:'+id
79 + }
3980 var text = localStorage[APPNAME+'_version_'+id]
40- var buffer = new TextEncoder('utf8').encode(text)
41- hash(buffer, function (err, hash) {
42- if(localStorage.SWB_current !== hash) {
81 + hash(new TextEncoder('utf8').encode(text), function (err, _id) {
82 + if(id !== _id) {
4383 //LOCAL STORAGE CORRUPTED
44- alert('localStorage is corrupted')
84 + cb(new Error('localStorage is corrupted'))
4585 }
4686 else {
4787 var script = document.createElement('script')
4888 script.textContent = text
@@ -51,48 +91,80 @@
5191 })
5292
5393 }
5494
95 +function add (secure_url, cb) {
96 + if(!(isUrl.test(secure_url) && hasHash.test(secure_url)))
97 + return cb(new Error('is not a secure url:'+secure_url))
98 + var id = hasHash.exec(parts[0])[1]
99 + BinaryXHR(secure_url, function (err, buf) {
100 + if(err)
101 + return prog.fail(err, 'could not retrive secure url')
102 + if(!buf)
103 + return prog.fail(new Error('could not retrive:'+secure_url))
104 +
105 + hash(buf, function (err, _id) {
106 + if(err)
107 + return prog.fail(err, 'hash failed')
108 +
109 + if(id !== _id) {
110 + return prog.fail(new Error('secure url is invalid'))
111 + }
112 + cb(null, _id)
113 + })
114 + })
115 +}
116 +
55117 if(parts.length && isUrl.test(parts[0]) && hasHash.test(parts[0])) {
56118 var id = hasHash.exec(parts[0])[1]
57- log('detected secure url:'+ parts[0])
119 + prog.next('detected secure url:'+ parts[0])
120 + window.location.hash = '#'+parts.slice(1).join('#')
121 +
122 + var current = localStorage[APPNAME+'_current']
123 + if(current && current !== id)
124 + if(!confirm("this action updates code to:"+id + "\nclick 'cancel' to continue with current version"))
125 + return run(current)
126 +
58127 //check if we already have this data.
59128 if(localStorage[APPNAME+'_version_'+id]) {
129 + prog.next('loading local version')
60130 run(id)
61- } else
62- log('GET:'+parts[0])
63- BinaryXHR(parts[0], function (err, buf) {
64- console.log(err, buf)
65- if(err) {
66- log('did not retrive new version')
67- throw err
68- }
69- hash(buf, function (err, hash_buf) {
70- if(id !== hash_buf) {
71- //ERROR. requested data was corrupt.
72- }
73- else {
74- log('received correct hash:'+id)
75- //returned data is correct. save, then run.
76- localStorage[APPNAME+'_version_'+id] = new TextDecoder('utf8').decode(buf)
77- //reload to run app
78- localStorage[APPNAME+'_current'] = id
79- window.location = '#' + parts.slice(1).join('#')
80- window.location.reload()
81- }
82- })
131 + } else {
132 + prog.next('retriving secure url:'+parts[0])
133 + add(parts[0], function (err, id) {
134 + if(err) return prog.fail(err)
135 +
136 + prog.next('loading secure javascript')
137 + //returned data is correct. save, then run.
138 + localStorage[APPNAME+'_version_'+id] = new TextDecoder('utf8').decode(buf)
139 + localStorage[APPNAME+'_current'] = id
140 +
141 + var versions = parse(localStorage[APPNAME+'_versions']) || {}
142 + versions[Date.now()] = id
143 + localStorage[APPNAME+'_versions'] = JSON.stringify(versions)
144 +
145 + window.location = '#' + parts.slice(1).join('#')
146 + window.location.reload()
147 +
83148 })
149 + }
84150 }
85151 else if(localStorage[APPNAME+'_current']) {
86152 run(localStorage[APPNAME+'_current'])
87153 }
88154 else {
89- alert('nothing to run')
155 + prog.next('no code to run: paste #{secure_url} to a javascript file.')
90156 }
91157
158 +})();
92159
93160
94-},{"arraybuffer-base64":2,"binary-xhr":3}],2:[function(require,module,exports){
161 +
162 +
163 +
164 +
165 +
166 +},{"arraybuffer-base64":2,"binary-xhr":3,"hyperprogress":4}],2:[function(require,module,exports){
95167 module.exports = function ToBase64(buf) {
96168 buf = new Uint8Array(buf)
97169 var s = ''
98170 for(var i = 0; i < buf.byteLength; i++)
@@ -128,9 +200,78 @@
128200 }
129201 xhr.send(null)
130202 }
131203
132-},{"inherits":4}],4:[function(require,module,exports){
204 +},{"inherits":5}],4:[function(require,module,exports){
205 +
206 +function create (tag, classname, children) {
207 + var el = document.createElement(tag)
208 + classname && el.classList.add(classname)
209 + children && children.forEach(function (e) {
210 + console.log('append', e)
211 + el.appendChild(
212 + 'string' === typeof e ? document.createTextNode(e) : e
213 + )
214 + })
215 + return el
216 +}
217 +
218 +module.exports = function (steps) {
219 + var list = create('ul', 'hyperprogress__list')
220 + var error = create('pre', 'hyperprogress__error')
221 + var liquid = create('div', 'hyperprogress__liquid', ['.'])
222 + var bar = create('div', 'hyperprogress__bar', [liquid])
223 + liquid.style.width = '0%'
224 +
225 + var n = 0
226 +
227 + var prog = create('div', 'hyperprogress', [
228 + steps ? bar : '',
229 + list,
230 + //only show bar if a number of steps is provided.
231 + error
232 + ])
233 +
234 + prog.complete = function () {
235 + liquid.style.width = '100%'
236 + prog.classList.add('hyperprogress--complete')
237 + }
238 +
239 + prog.next = function (name) {
240 + n = Math.min(n+1, steps)
241 + if(list.lastChild)
242 + list.lastChild.classList.add('hyperprogress--okay')
243 +
244 + if(name)
245 + list.appendChild(create('li', 'hyperprogress__started', [name]))
246 +
247 + liquid.style.width = Math.round((n/steps)*100)+'%'
248 +
249 + if(n === steps)
250 + prog.complete()
251 + }
252 +
253 + prog.fail = function (err) {
254 + prog.classList.add('hyperprogress--failed')
255 + list.lastChild.classList.add('hyperprogress--error')
256 + if(err && err.stack)
257 + error.textContent = err.stack
258 + }
259 +
260 + prog.reset = function () {
261 + n = 0
262 + error.innerHTML = list.innerHTML = ''
263 + liquid.style.width = '0%'
264 + return prog
265 + }
266 +
267 + return prog
268 +}
269 +
270 +
271 +
272 +
273 +},{}],5:[function(require,module,exports){
133274 module.exports = inherits
134275
135276 function inherits (c, p, proto) {
136277 proto = proto || {}

Built with git-ssb-web