Skip to content

Commit

Permalink
fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-portmen committed Oct 14, 2017
1 parent 8006dba commit 5902493
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions WebExtension/geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,37 @@ var geo = {
},

init4: () => {
const req = new XMLHttpRequest();
req.open('GET', './data/assets/geoip-country4.dat');
req.responseType = 'arraybuffer';
req.onload = () => {
geo.cache4.buffer = req.response;
const size = req.response.byteLength;

geo.cache4.lastLine = (size / geo.cache4.recordSize) - 1;

geo.cache4.lastIP = new DataView(
geo.cache4.buffer,
(geo.cache4.lastLine * geo.cache4.recordSize) + 4, 4
).getUint32(0);
geo.cache4.firstIP = new DataView(geo.cache4.buffer, 0, 4).getUint32(0);
};
req.send();
return fetch('/data/assets/geoip-country4.dat')
.then(r => r.arrayBuffer())
.then(buffer => {
geo.cache4.buffer = buffer;
const size = buffer.byteLength;

geo.cache4.lastLine = (size / geo.cache4.recordSize) - 1;

geo.cache4.lastIP = new DataView(
geo.cache4.buffer,
(geo.cache4.lastLine * geo.cache4.recordSize) + 4, 4
).getUint32(0);
geo.cache4.firstIP = new DataView(geo.cache4.buffer, 0, 4).getUint32(0);

});
},
init6: () => {
const req = new XMLHttpRequest();
req.open('GET', './data/assets/geoip-country6.dat');
req.responseType = 'arraybuffer';
req.onload = () => {
geo.cache6.buffer = req.response;
const size = req.response.byteLength;

geo.cache6.lastLine = (size / geo.cache6.recordSize) - 1;

geo.cache6.lastIP = new DataView(
geo.cache6.buffer,
(geo.cache6.lastLine * geo.cache6.recordSize) + 4, 4
).getUint32(0);
geo.cache6.firstIP = new DataView(geo.cache6.buffer, 0, 4).getUint32(0);
};
req.send();
return fetch('/data/assets/geoip-country6.dat')
.then(r => r.arrayBuffer())
.then(buffer => {
geo.cache6.buffer = buffer;
const size = buffer.byteLength;

geo.cache6.lastLine = (size / geo.cache6.recordSize) - 1;

geo.cache6.lastIP = new DataView(
geo.cache6.buffer,
(geo.cache6.lastLine * geo.cache6.recordSize) + 4, 4
).getUint32(0);
geo.cache6.firstIP = new DataView(geo.cache6.buffer, 0, 4).getUint32(0);
});
},
lookup4: ip => {
if (!geo.cache4.buffer) {
Expand Down Expand Up @@ -162,10 +159,10 @@ var geo = {
}
};

geo.init4();
geo.init6();
var isLoaded = false;
var requests = [];

self.onmessage = function({data}) {
var perform = data => {
try {
if (data.type === 4) {
const ip = utils.aton4(data.ip);
Expand All @@ -189,3 +186,15 @@ self.onmessage = function({data}) {
});
}
};

Promise.all([geo.init4(), geo.init6()]).then(() => {
isLoaded = true;
requests.forEach(r => perform(r));
});

self.onmessage = function({data}) {
if (isLoaded) {
perform(data);
requests.push(data);
}
};

0 comments on commit 5902493

Please sign in to comment.