-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocation.js
222 lines (200 loc) · 5.03 KB
/
location.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const { createContentDigest } = require(`gatsby-core-utils`);
const Queue = require(`better-queue`);
const ProgressBar = require(`progress`);
const axios = require("axios");
const rateLimit = require("axios-rate-limit");
const http = rateLimit(axios.create(), {
maxRequests: 1,
perMilliseconds: 1010,
});
async function loadLocation(place) {
const url = `https://nominatim.openstreetmap.org/search/${encodeURIComponent(
place
)}?format=json`;
const res = await http(url, {
headers: {
"User-Agent": "family.jamesst.one - A personal family tree",
},
});
if (res.status !== 200) return { lat: null, lng: null };
const [location] = res.data;
return {
lat: location && location.hasOwnProperty("lat") ? location.lat : null,
lng: location && location.hasOwnProperty("lon") ? location.lon : null,
};
}
function createProgress(message, reporter) {
if (reporter && reporter.createProgress) {
return reporter.createProgress(message);
}
const bar = new ProgressBar(
` [:bar] :current/:total :elapsed s :percent ${message}`,
{
total: 0,
width: 30,
clear: true,
}
);
return {
start() {},
tick() {
bar.tick();
},
done() {},
set total(value) {
bar.total = value;
},
};
}
const cacheId = (location) => `create-remote-location-node-${location}`;
let bar;
// Keep track of the total number of jobs we push in the queue
let totalJobs = 0;
/**
* Queue
* Use the task's location as the id
* When pushing a task with a similar id, prefer the original task
* as it's already in the processing cache
*/
const queue = new Queue(pushToQueue, {
id: `location`,
merge: (old, _, cb) => cb(old),
concurrent: 1,
});
// when the queue is empty we stop the progressbar
queue.on(`drain`, () => {
if (bar) {
bar.done();
}
totalJobs = 0;
});
async function pushToQueue(task, cb) {
try {
const node = await processRemoteNode(task);
return cb(null, node);
} catch (e) {
console.error("pushToQueue - Error", { task, e });
return cb(e);
}
}
async function processRemoteNode({
location,
cache,
createNode,
parentNodeId,
auth = {},
httpHeaders = {},
createNodeId,
ext,
name,
}) {
// See if there's response headers for this url
// from a previous request.
const cachedLocation = await cache.get(cacheId(location));
const place = cachedLocation || (await loadLocation(location));
await cache.set(cacheId(location), place);
return place;
}
/**
* Index of promises resolving to File node from remote url
*/
const processingCache = {};
/**
* pushTask
* --
* pushes a task in to the Queue and the processing cache
*
* Promisfy a task in queue
* @param {CreateRemoteFileNodePayload} task
* @return {Promise<Object>}
*/
const pushTask = (task) =>
new Promise((resolve, reject) => {
queue
.push(task)
.on(`finish`, (task) => {
resolve(task);
})
.on(`failed`, (err) => {
reject(`failed to process ${task.location}\n${err}`);
});
});
/***************
* Entry Point *
***************/
/**
* createRemoteFileNode
* --
*
* Download a remote location
* First checks cache to ensure duplicate requests aren't processed
* Then pushes to a queue
*
* @param {CreateRemoteFileNodePayload} options
* @return {Promise<Object>} Returns the created node
*/
module.exports = ({
location: l,
cache,
createNode,
getCache,
parentNodeId = null,
auth = {},
httpHeaders = {},
createNodeId,
ext = null,
name = null,
reporter,
}) => {
// validation of the input
// without this it's notoriously easy to pass in the wrong `createNodeId`
// see gatsbyjs/gatsby#6643
if (typeof createNodeId !== `function`) {
throw new Error(
`createNodeId must be a function, was ${typeof createNodeId}`
);
}
if (typeof createNode !== `function`) {
throw new Error(`createNode must be a function, was ${typeof createNode}`);
}
if (typeof getCache === `function`) {
// use cache of this plugin and not cache of function caller
cache = getCache(`gatsby-source-filesystem`);
}
if (typeof cache !== `object`) {
throw new Error(
`Neither "cache" or "getCache" was passed. getCache must be function that return Gatsby cache, "cache" must be the Gatsby cache, was ${typeof cache}`
);
}
if (!l) {
return Promise.reject(`missing location: ${l}`);
}
const location = l.trim();
// Check if we already requested node for this remote file
// and return stored promise if we did.
if (processingCache[location]) {
return processingCache[location];
}
if (totalJobs === 0) {
bar = createProgress(`Downloading remote locations`, reporter);
bar.start();
}
totalJobs += 1;
bar.total = totalJobs;
const locationDownloadPromise = pushTask({
location,
cache,
createNode,
parentNodeId,
createNodeId,
auth,
httpHeaders,
ext,
name,
});
processingCache[location] = locationDownloadPromise.then((location) => {
bar.tick();
return location;
});
return processingCache[location];
};