2020-10-09 19:35:46 +02:00
|
|
|
const shim = require('lib/shim').default;
|
2017-07-10 20:09:58 +02:00
|
|
|
|
|
|
|
class GeolocationNode {
|
|
|
|
static async currentPosition(options = null) {
|
|
|
|
if (!options) options = {};
|
|
|
|
|
2020-02-27 11:48:23 +02:00
|
|
|
let response = await shim.fetch('https://freegeoip.app/json/');
|
2019-09-19 23:51:18 +02:00
|
|
|
if (!response.ok) throw new Error(`Could not get geolocation: ${await response.text()}`);
|
2017-07-10 20:09:58 +02:00
|
|
|
|
|
|
|
response = await response.json();
|
|
|
|
|
2020-02-27 11:48:23 +02:00
|
|
|
if (!('latitude' in response) || !('longitude' in response)) throw new Error(`Invalid geolocation response: ${response ? JSON.stringify(response) : '<null>'}`);
|
2018-09-23 20:45:13 +02:00
|
|
|
|
2017-07-10 20:09:58 +02:00
|
|
|
return {
|
2019-07-29 15:43:53 +02:00
|
|
|
timestamp: new Date().getTime(),
|
2017-07-10 20:09:58 +02:00
|
|
|
coords: {
|
2020-02-27 11:48:23 +02:00
|
|
|
longitude: response.longitude,
|
2017-07-10 20:09:58 +02:00
|
|
|
altitude: 0,
|
2020-02-27 11:48:23 +02:00
|
|
|
latitude: response.latitude,
|
2019-07-29 15:43:53 +02:00
|
|
|
},
|
|
|
|
};
|
2017-07-10 20:09:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { GeolocationNode };
|