forked from bitfoundation/bitplatform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit-bup.ts
136 lines (102 loc) · 4.79 KB
/
bit-bup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var BitBup = BitBup || {};
BitBup.version = window['bit-bup version'] = '9.6.0-pre-10';
declare const Blazor: any;
; (function () {
const bitBupScript = document.currentScript;
if (!Blazor) {
console.warn('BitBup: no Blazor found!');
return;
}
window.addEventListener('DOMContentLoaded', runBup);
function runBup() {
let counter = 0;
const fetchPromises = [];
const options = extract();
info('starting...');
startBlazor();
function startBlazor() {
const scriptTags = [].slice.call(document.scripts);
const blazorWasmScriptTag = scriptTags.find(s => s.src && s.src.indexOf(options.blazorScript) !== -1);
if (!blazorWasmScriptTag) {
warn(`blazor script (${options.blazorScript}) not found!`);
return;
}
const autostart = blazorWasmScriptTag.attributes['autostart'];
if (!autostart || autostart.value !== 'false') {
warn('no "autostart=false" found on the blazor script tag!');
return;
}
Blazor.start({ loadBootResource });
}
function extract(): BupOptions {
const defaultoptions = {
log: 'info',
handler: (...args: any[]) => { },
blazorScript: '_framework/blazor.webassembly.js',
integrityCheck: false,
}
const optionsAttribute = (bitBupScript.attributes)['options'];
const optionsName = (optionsAttribute || {}).value || 'bitBup';
const options = (window[optionsName] || defaultoptions) as BupOptions;
const logAttribute = bitBupScript.attributes['log'];
options.log = (logAttribute && logAttribute.value) || options.log;
const handlerAttribute = bitBupScript.attributes['handler'];
const handlerName = (handlerAttribute && handlerAttribute.value) || 'bitBupHandler';
options.handler = (window[handlerName] || options.handler) as (...args: any[]) => void;
const blazorScriptAttribute = bitBupScript.attributes['blazorScript'];
options.blazorScript = (blazorScriptAttribute && blazorScriptAttribute.value) || options.blazorScript;
const integrityCheckAttribute = bitBupScript.attributes['integrityCheck'];
options.integrityCheck = ((integrityCheckAttribute && integrityCheckAttribute.value) === 'true') || options.integrityCheck;
if (!options.handler || typeof options.handler !== 'function') {
console.warn('BitBup: progress handler not found or is not a function!');
options.handler = undefined;
}
return options;
}
function loadBootResource(type, name, url, integrity) {
if (type === 'manifest') return url; // since this is the file containing the resources list lets the blazor itself handle it
if (type === 'dotnetjs') return url; // blazor itself handles this specific resource and needs to have its url
const resourceUrl = `${url}?v=${integrity}`;
const requestInit: RequestInit = options.integrityCheck
? { cache: 'no-store', integrity: integrity, headers: [['cache-control', 'public, max-age=3153600']] }
: { cache: 'no-store', headers: [['cache-control', 'public, max-age=3153600']] };
const response = fetch(resourceUrl, requestInit);
fetchPromises.push(response);
response.then(_ => {
if (counter === 0) {
handle('start');
}
//if (fetchPromises.length < 10) return;
const percent = 100 * (++counter) / fetchPromises.length;
handle('progress', { percent, type, name, url, integrity, index: counter });
if (percent >= 100) {
handle('end');
}
});
return response;
}
function handle(...args: any[]) {
options.handler && options.handler(...args);
}
// TODO: apply log options: info, verbode, debug, error, ...
//function info(...texts: string[]) {
// console.log(`%cBitBup: ${texts.join('\n')}`, 'color:lightblue');
//}
function info(...args: any[]) {
if (options.log === 'none') return;
console.info(...['BitBup:', ...args]);
}
function warn(text: string) {
console.warn(`BitBup: ${text}`);
}
function err(e: any) {
console.error('BitBup:', e);
}
}
}());
interface BupOptions {
log: 'none' | 'info' | 'verbose' | 'debug' | 'error'
handler(...args: any[]): void
blazorScript: string
integrityCheck: boolean
}