-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathworker.js
123 lines (113 loc) · 3.43 KB
/
worker.js
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
/* global initSqlJs */
/* eslint-env worker */
/* eslint no-restricted-globals: ["error"] */
"use strict";
var db;
function onModuleReady(SQL) {
function createDb(data) {
if (db != null) db.close();
db = new SQL.Database(data);
return db;
}
var buff; var data; var result;
data = this["data"];
var config = data["config"] ? data["config"] : {};
switch (data && data["action"]) {
case "open":
buff = data["buffer"];
createDb(buff && new Uint8Array(buff));
return postMessage({
id: data["id"],
ready: true
});
case "exec":
if (db === null) {
createDb();
}
if (!data["sql"]) {
throw "exec: Missing query string";
}
return postMessage({
id: data["id"],
results: db.exec(data["sql"], data["params"], config)
});
case "getRowsModified":
return postMessage({
id: data["id"],
rowsModified: db.getRowsModified()
});
case "each":
if (db === null) {
createDb();
}
var callback = function callback(row) {
return postMessage({
id: data["id"],
row: row,
finished: false
});
};
var done = function done() {
return postMessage({
id: data["id"],
finished: true
});
};
return db.each(data["sql"], data["params"], callback, done, config);
case "export":
buff = db["export"]();
result = {
id: data["id"],
buffer: buff
};
try {
return postMessage(result, [result]);
} catch (error) {
return postMessage(result);
}
case "close":
if (db) {
db.close();
}
return postMessage({
id: data["id"]
});
default:
throw new Error("Invalid action : " + (data && data["action"]));
}
}
function onError(err) {
return postMessage({
id: this["data"]["id"],
error: err["message"]
});
}
db = null;
var sqlModuleReady = initSqlJs();
function global_sqljs_message_handler(event) {
return sqlModuleReady
.then(onModuleReady.bind(event))
.catch(onError.bind(event));
}
if (typeof importScripts === "function") {
self.onmessage = global_sqljs_message_handler;
}
if (typeof require === "function") {
// eslint-disable-next-line global-require
var worker_threads = require("worker_threads");
var parentPort = worker_threads.parentPort;
// eslint-disable-next-line no-undef
globalThis.postMessage = parentPort.postMessage.bind(parentPort);
parentPort.on("message", function onmessage(data) {
var event = { data: data };
global_sqljs_message_handler(event);
});
if (typeof process !== "undefined") {
process.on("uncaughtException", function uncaughtException(err) {
postMessage({ error: err.message });
});
process.on("unhandledRejection", function unhandledRejection(err) {
postMessage({ error: err.message });
});
}
}