1
- import lib from './index'
2
- import compress from './image-compression'
3
- import { cleanupCanvasMemory , getNewCanvasAndCtx } from './utils'
4
- import UPNG from './UPNG'
5
- import * as UZIP from 'uzip'
1
+ import * as UZIP from 'uzip' ;
2
+ // eslint-disable-next-line import/no-cycle
3
+ import lib from './index' ;
4
+ import compress from './image-compression' ;
5
+ import { getNewCanvasAndCtx } from './utils' ;
6
+ import UPNG from './UPNG' ;
7
+ import MaxCanvasSize from './config/max-canvas-size' ;
6
8
7
- let cnt = 0
8
- let imageCompressionLibUrl
9
- let worker
9
+ let cnt = 0 ;
10
+ let imageCompressionLibUrl ;
11
+ let worker ;
10
12
11
- function createWorker ( script ) {
13
+ function createWorker ( script ) {
12
14
if ( typeof script === 'function' ) {
13
- script = `(${ script } )()`
15
+ script = `(${ script } )()` ;
14
16
}
15
- return new Worker ( URL . createObjectURL ( new Blob ( [ script ] ) ) )
17
+ return new Worker ( URL . createObjectURL ( new Blob ( [ script ] ) ) ) ;
16
18
}
17
19
18
- function createSourceObject ( str ) {
20
+ function createSourceObject ( str ) {
19
21
// console.log('createSourceObject', str)
20
- return URL . createObjectURL ( new Blob ( [ str ] , { type : 'application/javascript' } ) )
22
+ return URL . createObjectURL ( new Blob ( [ str ] , { type : 'application/javascript' } ) ) ;
21
23
}
22
24
23
25
function stringify ( o ) {
24
- return JSON . stringify ( o , ( key , value ) => {
25
- return ( typeof value === 'function' ) ? `BIC_FN:::(() => ${ value . toString ( ) } )()` : value ;
26
- } )
26
+ return JSON . stringify ( o , ( key , value ) => ( ( typeof value === 'function' ) ? `BIC_FN:::(function () { return ${ value . toString ( ) } })()` : value ) ) ;
27
27
}
28
28
29
29
function parse ( o ) {
30
- if ( typeof o === 'string' ) return o
31
- const result = { }
30
+ if ( typeof o === 'string' ) return o ;
31
+ const result = { } ;
32
32
Object . entries ( o ) . forEach ( ( [ key , value ] ) => {
33
33
if ( typeof value === 'string' && value . startsWith ( 'BIC_FN:::' ) ) {
34
34
try {
35
- result [ key ] = eval ( value . replace ( / ^ B I C _ F N : : : / , '' ) )
35
+ result [ key ] = eval ( value . replace ( / ^ B I C _ F N : : : / , '' ) ) ;
36
36
} catch ( e ) {
37
- console . log ( key , e )
38
- throw e
37
+ // console.log(key, e);
38
+ throw e ;
39
39
}
40
40
} else {
41
- result [ key ] = parse ( value )
41
+ result [ key ] = parse ( value ) ;
42
42
}
43
- } )
44
- return result
43
+ } ) ;
44
+ return result ;
45
45
}
46
46
47
- function generateLib ( ) {
47
+ function generateLib ( ) {
48
48
// prepare the lib to be used inside WebWorker
49
49
return createSourceObject ( `
50
50
// reconstruct library
@@ -61,6 +61,8 @@ function generateLib () {
61
61
imageCompression.followExifOrientation = ${ lib . followExifOrientation }
62
62
imageCompression.cleanupCanvasMemory = ${ lib . cleanupCanvasMemory }
63
63
imageCompression.isAutoOrientationInBrowser = ${ lib . isAutoOrientationInBrowser }
64
+ imageCompression.approximateBelowMaximumCanvasSizeOfBrowser = ${ lib . approximateBelowMaximumCanvasSizeOfBrowser }
65
+ imageCompression.getBrowserName = ${ lib . getBrowserName }
64
66
65
67
// functions / objects
66
68
getDataUrlFromFile = imageCompression.getDataUrlFromFile
@@ -74,13 +76,27 @@ function generateLib () {
74
76
followExifOrientation = imageCompression.followExifOrientation
75
77
cleanupCanvasMemory = imageCompression.cleanupCanvasMemory
76
78
isAutoOrientationInBrowser = imageCompression.isAutoOrientationInBrowser
79
+ approximateBelowMaximumCanvasSizeOfBrowser = imageCompression.approximateBelowMaximumCanvasSizeOfBrowser
80
+ getBrowserName = imageCompression.getBrowserName
77
81
78
82
getNewCanvasAndCtx = ${ getNewCanvasAndCtx }
79
83
CustomFileReader = FileReader
80
84
CustomFile = File
85
+ MaxCanvasSize = ${ JSON . stringify ( MaxCanvasSize ) }
86
+ function compress (){return (${ compress } ).apply(null, arguments)}
87
+
88
+ // core-js
81
89
function _slicedToArray(arr, n) { return arr }
82
90
function _typeof(a) { return typeof a }
83
- function compress (){return (${ compress } ).apply(null, arguments)}
91
+ function _objectSpread2(target) {
92
+ for (var i = 1; i < arguments.length; i++) {
93
+ var source = arguments[i] != null ? arguments[i] : {};
94
+
95
+ Object.assign(target, source)
96
+ }
97
+
98
+ return target;
99
+ }
84
100
85
101
// Libraries
86
102
const parse = ${ parse }
@@ -164,7 +180,7 @@ function generateLib () {
164
180
n(N.D,30,0);n(N.v,320,0)}());return H.H.N}()
165
181
166
182
const UZIP = {}
167
- UZIP["parse"] = ${ UZIP [ " parse" ] }
183
+ UZIP["parse"] = ${ UZIP . parse }
168
184
UZIP._readLocal = ${ UZIP . _readLocal }
169
185
UZIP.inflateRaw = ${ UZIP . inflateRaw }
170
186
UZIP.inflate = ${ UZIP . inflate }
@@ -291,10 +307,10 @@ function generateLib () {
291
307
for(var i=0; i<320; i++) U.ttree.push(0,0);
292
308
*/
293
309
})()
294
- ` )
310
+ ` ) ;
295
311
}
296
312
297
- function generateWorkerScript ( ) {
313
+ function generateWorkerScript ( ) {
298
314
// code to be run in the WebWorker
299
315
return createWorker ( `
300
316
let scriptImported = false
@@ -315,43 +331,43 @@ function generateWorkerScript () {
315
331
self.postMessage({ error: e.message + '\\n' + e.stack, id })
316
332
}
317
333
})
318
- ` )
334
+ ` ) ;
319
335
}
320
336
321
- export function compressOnWebWorker ( file , options ) {
337
+ export function compressOnWebWorker ( file , options ) {
322
338
return new Promise ( async ( resolve , reject ) => {
323
- let id = cnt ++
339
+ const id = cnt ++ ;
324
340
325
341
if ( ! imageCompressionLibUrl ) {
326
- imageCompressionLibUrl = generateLib ( )
342
+ imageCompressionLibUrl = generateLib ( ) ;
327
343
}
328
344
329
345
if ( ! worker ) {
330
- worker = generateWorkerScript ( )
346
+ worker = generateWorkerScript ( ) ;
331
347
}
332
348
333
- function handler ( e ) {
349
+ function handler ( e ) {
334
350
if ( e . data . id === id ) {
335
351
if ( e . data . progress !== undefined ) {
336
- options . onProgress ( e . data . progress )
337
- return
352
+ options . onProgress ( e . data . progress ) ;
353
+ return ;
338
354
}
339
- worker . removeEventListener ( 'message' , handler )
355
+ worker . removeEventListener ( 'message' , handler ) ;
340
356
if ( e . data . error ) {
341
- reject ( new Error ( e . data . error ) )
357
+ reject ( new Error ( e . data . error ) ) ;
342
358
}
343
- resolve ( e . data . file )
359
+ resolve ( e . data . file ) ;
344
360
}
345
361
}
346
362
347
- worker . addEventListener ( 'message' , handler )
348
- worker . addEventListener ( 'error' , reject )
363
+ worker . addEventListener ( 'message' , handler ) ;
364
+ worker . addEventListener ( 'error' , reject ) ;
349
365
350
366
worker . postMessage ( {
351
367
file,
352
368
id,
353
369
imageCompressionLibUrl,
354
- options : { ...options , onProgress : undefined }
355
- } )
356
- } )
370
+ options : { ...options , onProgress : undefined } ,
371
+ } ) ;
372
+ } ) ;
357
373
}
0 commit comments