Debian Codegraph @

resources/src/mediawiki.deflate/mw.deflate.js

raw symbols SCIP
1 const pako = require( '../../lib/pako/pako_deflate.js' );
2
3 /**
4 * Convert a byte stream to base64 text.
5 * Before using load the mediawiki.deflate ResourceLoader module.
6 *
7 * @example
8 * return mw.loader.using( 'mediawiki.deflate' ).then( function () {
9 * return mw.deflate( html );
10 * } );
11 * @param {string} data
12 * @return {string}
13 */
14 mw.deflate = function ( data ) {
15 return 'rawdeflate,' + bytesToBase64( pako.deflateRaw( data, { level: 5 } ) );
16 };
17
18 /*
19 * Convert a byte stream to base64 text.
20 *
21 * As suggested in https://github.com/nodeca/pako/issues/206#issuecomment-744264726
22 *
23 * Code from https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727.
24 * MIT License
25 * Copyright (c) 2020 Egor Nepomnyaschih
26 *
27 * @type {Array}
28 */
29 const base64abc = [
30 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
31 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
32 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
33 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
34 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
35 ];
36
37 function bytesToBase64( bytes ) {
38 /* eslint-disable no-bitwise */
39 let result = '', i;
40 const l = bytes.length;
41 for ( i = 2; i < l; i += 3 ) {
42 result += base64abc[ bytes[ i - 2 ] >> 2 ];
43 result += base64abc[ ( ( bytes[ i - 2 ] & 0x03 ) << 4 ) | ( bytes[ i - 1 ] >> 4 ) ];
44 result += base64abc[ ( ( bytes[ i - 1 ] & 0x0F ) << 2 ) | ( bytes[ i ] >> 6 ) ];
45 result += base64abc[ bytes[ i ] & 0x3F ];
46 }
47 if ( i === l + 1 ) { // 1 octet yet to write
48 result += base64abc[ bytes[ i - 2 ] >> 2 ];
49 result += base64abc[ ( bytes[ i - 2 ] & 0x03 ) << 4 ];
50 result += '==';
51 }
52 if ( i === l ) { // 2 octets yet to write
53 result += base64abc[ bytes[ i - 2 ] >> 2 ];
54 result += base64abc[ ( ( bytes[ i - 2 ] & 0x03 ) << 4 ) | ( bytes[ i - 1 ] >> 4 ) ];
55 result += base64abc[ ( bytes[ i - 1 ] & 0x0F ) << 2 ];
56 result += '=';
57 }
58 return result;
59 /* eslint-enable no-bitwise */
60 }