summaryrefslogtreecommitdiffstats
path: root/src/js/diff-updater.js
blob: 4e6ece1556b1cbda428f04a21b58e4715618413e (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*******************************************************************************

    uBlock Origin - a comprehensive, efficient content blocker
    Copyright (C) 2014-present Raymond Hill

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see {http://www.gnu.org/licenses/}.

    Home: https://github.com/gorhill/uBlock
*/

'use strict';

// This module can be dynamically loaded or spun off as a worker.

/******************************************************************************/

const patches = new Map();
const encoder = new TextEncoder();
const reFileName = /([^\/]+?)(?:#.+)?$/;
const EMPTYLINE = '';

/******************************************************************************/

const suffleArray = arr => {
    const out = arr.slice();
    for ( let i = 0, n = out.length; i < n; i++ ) {
        const j = Math.floor(Math.random() * n);
        if ( j === i ) { continue; }
        [ out[j], out[i] ] = [ out[i], out[j] ];
    }
    return out;
};

const basename = url => {
    const match = reFileName.exec(url);
    return match && match[1] || '';
};

const resolveURL = (path, url) => {
    try {
        return new URL(path, url);
    }
    catch(_) {
    }
};

const expectedTimeFromPatch = assetDetails => {
    const match = /(\d+)\.(\d+)\.(\d+)\.(\d+)/.exec(assetDetails.patchPath);
    if ( match === null ) { return 0; }
    const date = new Date();
    date.setUTCFullYear(
        parseInt(match[1], 10),
        parseInt(match[2], 10) - 1,
        parseInt(match[3], 10)
    );
    date.setUTCHours(0, parseInt(match[4], 10), 0, 0);
    return date.getTime() + assetDetails.diffExpires;
};

function parsePatch(patch) {
    const patchDetails = new Map();
    const diffLines = patch.split('\n');
    let i = 0, n = diffLines.length;
    while ( i < n ) {
        const line = diffLines[i++];
        if ( line.startsWith('diff ') === false ) { continue; }
        const fields = line.split(/\s+/);
        const diffBlock = {};
        for ( let j = 0; j < fields.length; j++ ) {
            const field = fields[j];
            const pos = field.indexOf(':');
            if ( pos === -1 ) { continue; }
            const name = field.slice(0, pos);
            if ( name === '' ) { continue; }
            const value = field.slice(pos+1);
            switch ( name ) {
            case 'name':
            case 'checksum':
                diffBlock[name] = value;
                break;
            case 'lines':
                diffBlock.lines = parseInt(value, 10);
                break;
            default:
                break;
            }
        }
        if ( diffBlock.name === undefined ) { return; }
        if ( isNaN(diffBlock.lines) || diffBlock.lines <= 0 ) { return; }
        if ( diffBlock.checksum === undefined ) { return; }
        patchDetails.set(diffBlock.name, diffBlock);
        diffBlock.diff = diffLines.slice(i, i + diffBlock.lines).join('\n');
        i += diffBlock.lines;
    }
    if ( patchDetails.size === 0 ) { return; }
    return patchDetails;
}

function applyPatch(text, diff) {
    // Inspired from (Perl) "sub _patch" at:
    // https://twiki.org/p/pub/Codev/RcsLite/RcsLite.pm
    // Apparently authored by John Talintyre in Jan. 2002
    // https://twiki.org/cgi-bin/view/Codev/RcsLite
    const lines = text.split('\n');
    const diffLines = diff.split('\n');
    let iAdjust = 0;
    let iDiff = 0, nDiff = diffLines.length;
    while ( iDiff < nDiff ) {
        const diffLine = diffLines[iDiff++];
        if ( diffLine === '' ) { break; }
        const diffParsed = /^([ad])(\d+) (\d+)$/.exec(diffLine);
        if ( diffParsed === null ) { return; }
        const op = diffParsed[1];
        const iOp = parseInt(diffParsed[2], 10);
        const nOp = parseInt(diffParsed[3], 10);
        const iOpAdj = iOp + iAdjust;
        if ( iOpAdj > lines.length ) { return; }
        // Delete lines
        if ( op === 'd' ) {
            lines.splice(iOpAdj-1, nOp);
            iAdjust -= nOp;
            continue;
        }
        // Add lines: Don't use splice() to avoid stack limit issues
        for ( let i = 0; i < nOp; i++ ) {
            lines.push(EMPTYLINE);
        }
        lines.copyWithin(iOpAdj+nOp, iOpAdj);
        for ( let i = 0; i < nOp; i++ ) {
            lines[iOpAdj+i] = diffLines[iDiff+i];
        }
        iAdjust += nOp;
        iDiff += nOp;
    }
    return lines.join('\n');
}

function hasPatchDetails(assetDetails) {
    const { patchPath } = assetDetails;
    const patchFile = basename(patchPath);
    return patchFile !== '' && patches.has(patchFile);
}

/******************************************************************************/

// Async

async function applyPatchAndValidate(assetDetails, diffDetails) {
    const { text } = assetDetails;
    const { diff, checksum } = diffDetails;
    const textAfter = applyPatch(text, diff);
    if ( typeof textAfter !== 'string' ) {
        assetDetails.error = 'baddiff';
        return false;
    }
    const crypto = globalThis.crypto;
    if ( typeof crypto !== 'object' ) {
        assetDetails.error = 'nocrypto';
        return false;
    }
    const arrayin = encoder.encode(textAfter);
    const arraybuffer = await crypto.subtle.digest('SHA-1', arrayin);
    const arrayout = new Uint8Array(arraybuffer);
    const sha1Full = Array.from(arrayout).map(i =>
        i.toString(16).padStart(2, '0')
    ).join('');
    if ( sha1Full.startsWith(checksum) === false ) {
        assetDetails.error = `badchecksum: expected ${checksum}, computed ${sha1Full.slice(0, checksum.length)}`;
        return false;
    }
    assetDetails.text = textAfter;
    return true;
}

async function fetchPatchDetailsFromCDNs(assetDetails) {
    const { patchPath, cdnURLs } = assetDetails;
    if ( Array.isArray(cdnURLs) === false ) { return null; }
    if ( cdnURLs.length === 0 ) { return null; }
    for ( const cdnURL of suffleArray(cdnURLs) ) {
        const patchURL = resolveURL(patchPath, cdnURL);
        if ( patchURL === undefined ) { continue; }
        const response = await fetch(patchURL).catch(reason => {
            console.error(reason, patchURL);
        });
        if ( response === undefined ) { continue; }
        if ( response.status === 404 ) { break; }
        if ( response.ok !== true ) { continue; }
        const patchText = await response.text();
        const patchDetails = parsePatch(patchText);
        if ( patchURL.hash.length > 1 ) {
            assetDetails.diffName = patchURL.hash.slice(1);
            patchURL.hash = '';
        }
        return {
            patchURL: patchURL.href,
            patchSize: `${(patchText.length / 1000).toFixed(1)} KB`,
            patchDetails,
        };
    }
    return null;
}

async function fetchPatchDetails(assetDetails) {
    const { patchPath } = assetDetails;
    const patchFile = basename(patchPath);
    if ( patchFile === '' ) { return null; }
    if ( patches.has(patchFile) ) {
        return patches.get(patchFile);
    }
    const patchDetailsPromise = fetchPatchDetailsFromCDNs(assetDetails);
    patches.set(patchFile, patchDetailsPromise);
    return patchDetailsPromise;
}

async function fetchAndApplyAllPatches(assetDetails) {
    if ( assetDetails.fetch === false ) {
        if ( hasPatchDetails(assetDetails) === false ) {
            assetDetails.status = 'nodiff';
            return assetDetails;
        }
    }
    // uBO-specific, to avoid pointless fetches which are likely to fail
    // because the patch has not yet been created
    const patchTime = expectedTimeFromPatch(assetDetails);
    if ( patchTime > Date.now() ) {
        assetDetails.status = 'nopatch-yet';
        return assetDetails;
    }
    const patchData = await fetchPatchDetails(assetDetails);
    if ( patchData === null ) {
        assetDetails.status = (Date.now() - patchTime) < (4 * assetDetails.diffExpires)
            ? 'nopatch-yet'
            : 'nopatch';
        return assetDetails;
    }
    const { patchDetails } = patchData;
    if ( patchDetails instanceof Map === false ) {
        assetDetails.status = 'nodiff';
        return assetDetails;
    }
    const diffDetails = patchDetails.get(assetDetails.diffName);
    if ( diffDetails === undefined ) {
        assetDetails.status = 'nodiff';
        return assetDetails;
    }
    if ( assetDetails.text === undefined ) {
        assetDetails.status = 'needtext';
        return assetDetails;
    }
    const outcome = await applyPatchAndValidate(assetDetails, diffDetails);
    if ( outcome !== true ) { return assetDetails; }
    assetDetails.status = 'updated';
    assetDetails.patchURL = patchData.patchURL;
    assetDetails.patchSize = patchData.patchSize;
    return assetDetails;
}

/******************************************************************************/

const bc = new globalThis.BroadcastChannel('diffUpdater');

bc.onmessage = ev => {
    const message = ev.data || {};
    switch ( message.what ) {
    case 'update':
        fetchAndApplyAllPatches(message).then(response => {
            bc.postMessage(response);
        }).catch(error => {
            bc.postMessage({ what: 'broken', error });
        });
        break;
    }
};

bc.postMessage({ what: 'ready' });

/******************************************************************************/