-
Notifications
You must be signed in to change notification settings - Fork 3
/
service-worker.js
153 lines (138 loc) · 4.3 KB
/
service-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
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
/* Note: not yet supported for type module ;( */
/* self.importScripts("./src/index.js"); */
/* The service worker is put at the root of the repo, and the site; so
it has browser API access to the entire "root" of the site. */
self.symbols = {};
self.userSymbols = {};
self.allSymbols = {};
self.allEngines = [];
self.pathname = "/";
/* mock find/osd until we can import it */
const Find = {
suggestSymbols(searchQuery) {
const symbol = searchQuery.slice(0, 1);
const symbolData = self.allSymbols[symbol];
if (symbolData) {
const symbolEngines = self.allSymbols[symbol].engines;
const symbolName = self.allSymbols[symbol].name;
return _createSuggestions({
searchQuery,
symbol,
symbolEngines,
symbolName,
});
} else {
return _createSuggestions({ searchQuery });
}
},
};
const _createSuggestions = ({
searchQuery,
symbol,
symbolEngines,
symbolName,
}) => {
/* filter for matching results */
const matchingEngines = self.allEngines.filter((engineData) => {
const [eSymbol, eId, eUrl, eSearch] = engineData;
return eSearch.includes(searchQuery);
});
/* map the result to a same sized array,
but open-search suggestion format */
const suggestedEngines = matchingEngines.reduce(
(acc, engineData) => {
const [eSymbol, eId, eUrl, eSearch] = engineData;
const term = `${eSymbol + eId}`;
const desc = `[${eSymbol}${self.symbols[eSymbol].name}::${eUrl}]`;
const newAcc = [
[...acc[0], term],
[...acc[1], desc],
[...acc[2], eUrl],
];
return newAcc;
},
[[], [], []]
);
return [searchQuery, suggestedEngines];
};
const _mergeSymbols = (symbols, userSymbols) => {
return Object.keys(symbols).reduce((acc, currKey) => {
acc[currKey] = {
engines: { ...symbols[currKey].engines },
};
acc[currKey] = {
engines: { ...acc[currKey]?.engines, ...userSymbols[currKey]?.engines },
};
acc[currKey].name = symbols[currKey].name;
return acc;
}, {});
};
const _mergeEngines = (allSymbols) => {
return Object.keys(allSymbols).reduce((acc, symbol) => {
const symbolData = allSymbols[symbol];
const symbolEngines = Object.entries(symbolData.engines);
const engines = [
...acc,
...symbolEngines.map(([engine, url]) => {
return [symbol, engine, url, `${symbol + engine + " " + url}`];
}),
];
return engines;
}, []);
};
const _handleFetch = (event) => {
const url = new URL(event.request.url);
const localApiUrl = self.pathname + "api";
if (url.pathname === localApiUrl) {
return event.respondWith(_respondWithApiRoot(event.request));
} else if (url.pathname.startsWith(localApiUrl + "/suggestions")) {
return event.respondWith(_respondWithSuggestions(event.request));
} else {
return self.fetch(event.request.url);
}
};
const _respondWithSuggestions = (request) => {
// Parse the hash query param from the request URL
const url = new URL(request.url);
const params = new URLSearchParams(url.hash.slice(1));
const searchQuery = params.get("q");
let suggestionBody;
try {
suggestionBody = Find.suggestSymbols(searchQuery);
} catch (e) {
console.info("Could not fetch any suggestion", e);
}
/* https://stackoverflow.com/questions/36535642/serving-an-opensearch-application-x-suggestionsjson-through-a-service-worker */
return new Response(JSON.stringify(suggestionBody), {
status: 200,
headers: { "Content-Type": "application/x-suggestions+json" },
});
};
const _respondWithApiRoot = (request) => {
const body = {
message: "Welcome to the client side Find API",
suggestions: "api/sugestions#q={searchTerms}",
};
return new Response(JSON.stringify(body), {
status: 200,
headers: { "Content-Type": "application/x-suggestions+json" },
});
};
const _handleMessage = ({ data }) => {
const { symbols, userSymbols, pathname } = JSON.parse(data);
/* assign the data on the worker self global object;
so we can re-use these values in the suggestions */
self.symbols = symbols;
self.userSymbols = userSymbols;
self.allSymbols = _mergeSymbols(symbols, userSymbols);
self.allEngines = _mergeEngines(self.allSymbols);
self.pathname = pathname;
};
self.addEventListener("install", (event) => {
console.info("I4kFind Service Worker installed");
});
self.addEventListener("activate", (event) => {
console.info("Service worker activated");
});
self.addEventListener("fetch", _handleFetch);
self.addEventListener("message", _handleMessage);