@@ -16,11 +16,23 @@ var Module = typeof Module != 'undefined' ? Module : {};
16
16
17
17
// --pre-jses are emitted after the Module integration code, so that they can
18
18
// refer to Module (if they choose; they can also define Module)
19
+ /**
20
+ * Web Worker API for libzim JavaScript bindings
21
+ *
22
+ * This file provides the pre-JS portion of the web worker that handles ZIM file operations.
23
+ * It is concatenated with postjs_file_api.js during the Emscripten build process (see Makefile)
24
+ * to create a complete web worker script that can be used with WebAssembly or asm.js builds.
25
+ *
26
+ * Supported actions: getEntryByPath, search, suggest, getArticleCount, init
27
+ */
28
+
19
29
self . addEventListener ( 'message' , function ( e ) {
20
30
var action = e . data . action ;
21
31
var path = e . data . path ;
22
32
var outgoingMessagePort = e . ports [ 0 ] ;
23
33
console . debug ( 'WebWorker called with action=' + action ) ;
34
+
35
+ // Retrieve content from ZIM archive by path
24
36
if ( action === 'getEntryByPath' ) {
25
37
var follow = e . data . follow ;
26
38
var entry = Module [ action ] ( path ) ;
@@ -43,7 +55,8 @@ self.addEventListener('message', function(e) {
43
55
else {
44
56
outgoingMessagePort . postMessage ( { content : new Uint8Array ( ) , mimetype : 'unknown' , isRedirect : false } ) ;
45
57
}
46
- }
58
+ }
59
+ // Full-text search across ZIM archive content
47
60
else if ( action === 'search' ) {
48
61
var text = e . data . text ;
49
62
var numResults = e . data . numResults || 50 ;
@@ -55,11 +68,26 @@ self.addEventListener('message', function(e) {
55
68
serializedEntries . push ( { path : entry . getPath ( ) } ) ;
56
69
}
57
70
outgoingMessagePort . postMessage ( { entries : serializedEntries } ) ;
58
- }
71
+ }
72
+ // Title-based suggestions for autocomplete (faster than full-text search)
73
+ else if ( action === 'suggest' ) {
74
+ var text = e . data . text ;
75
+ var numResults = e . data . numResults || 10 ;
76
+ var suggestions = Module [ action ] ( text , numResults ) ;
77
+ console . debug ( 'Found nb suggestions = ' + suggestions . size ( ) , suggestions ) ;
78
+ var serializedSuggestions = [ ] ;
79
+ for ( var i = 0 ; i < suggestions . size ( ) ; i ++ ) {
80
+ var entry = suggestions . get ( i ) ;
81
+ serializedSuggestions . push ( { path : entry . getPath ( ) , title : entry . getTitle ( ) } ) ;
82
+ }
83
+ outgoingMessagePort . postMessage ( { suggestions : serializedSuggestions } ) ;
84
+ }
85
+ // Get total number of articles in the ZIM archive
59
86
else if ( action === 'getArticleCount' ) {
60
87
var articleCount = Module [ action ] ( ) ;
61
88
outgoingMessagePort . postMessage ( articleCount ) ;
62
- }
89
+ }
90
+ // Initialize the ZIM archive with file system mounting
63
91
else if ( action === 'init' ) {
64
92
var files = e . data . files ;
65
93
var assemblerType = e . data . assemblerType || 'runtime' ;
@@ -75,6 +103,7 @@ self.addEventListener('message', function(e) {
75
103
for ( let i = 0 ; i < files . length ; i ++ ) {
76
104
Module [ 'arguments' ] . push ( '/work/' + files [ i ] . name ) ;
77
105
}
106
+ // Mount file system for ZIM file access (Electron vs browser environments)
78
107
Module [ 'preRun' ] = function ( ) {
79
108
FS . mkdir ( '/work' ) ;
80
109
if ( files [ 0 ] . readMode === 'electron' ) {
@@ -91,6 +120,9 @@ self.addEventListener('message', function(e) {
91
120
console . debug ( 'baseZimFileName = ' + baseZimFileName ) ;
92
121
console . debug ( "Module['arguments'] = " + Module [ 'arguments' ] ) ;
93
122
123
+ // File continues in postjs_file_api.js - handles invalid actions and closes the event listener
124
+ // Between prejs and postjs: Emscripten injects the compiled WebAssembly/asm.js Module code and bindings
125
+
94
126
95
127
// Sometimes an existing Module object exists with properties
96
128
// meant to overwrite the default module functionality. Here
@@ -7445,21 +7477,21 @@ function invoke_viii(index,a1,a2,a3) {
7445
7477
}
7446
7478
}
7447
7479
7448
- function invoke_ii ( index , a1 ) {
7480
+ function invoke_v ( index ) {
7449
7481
var sp = stackSave ( ) ;
7450
7482
try {
7451
- return getWasmTableEntry ( index ) ( a1 ) ;
7483
+ getWasmTableEntry ( index ) ( ) ;
7452
7484
} catch ( e ) {
7453
7485
stackRestore ( sp ) ;
7454
7486
if ( ! ( e instanceof EmscriptenEH ) ) throw e ;
7455
7487
_setThrew ( 1 , 0 ) ;
7456
7488
}
7457
7489
}
7458
7490
7459
- function invoke_v ( index ) {
7491
+ function invoke_ii ( index , a1 ) {
7460
7492
var sp = stackSave ( ) ;
7461
7493
try {
7462
- getWasmTableEntry ( index ) ( ) ;
7494
+ return getWasmTableEntry ( index ) ( a1 ) ;
7463
7495
} catch ( e ) {
7464
7496
stackRestore ( sp ) ;
7465
7497
if ( ! ( e instanceof EmscriptenEH ) ) throw e ;
@@ -8611,12 +8643,10 @@ run();
8611
8643
8612
8644
// end include: postamble.js
8613
8645
// include: /src/postjs_file_api.js
8614
- }
8615
- else {
8646
+ } else {
8616
8647
console . error ( 'Invalid action: ' + action ) ;
8617
8648
outgoingMessagePort . postMessage ( 'invalid action' ) ;
8618
8649
}
8619
- } , false ) ;
8620
-
8650
+ } , false ) ;
8621
8651
8622
8652
// end include: /src/postjs_file_api.js
0 commit comments