gatsby-source-filesystem 2.0.6 → 2.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,32 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ <a name="2.0.10"></a>
7
+
8
+ ## [2.0.10](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem/compare/gatsby-source-filesystem@2.0.9...gatsby-source-filesystem@2.0.10) (2018-11-29)
9
+
10
+ **Note:** Version bump only for package gatsby-source-filesystem
11
+
12
+ <a name="2.0.9"></a>
13
+
14
+ ## [2.0.9](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem/compare/gatsby-source-filesystem@2.0.8...gatsby-source-filesystem@2.0.9) (2018-11-26)
15
+
16
+ ### Bug Fixes
17
+
18
+ - **gatsby-plugin-filesystem:** throw meaningful errors on bad inputs ([#10123](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem/issues/10123)) ([21ebf2c](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem/commit/21ebf2c)), closes [#6643](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem/issues/6643)
19
+
20
+ <a name="2.0.8"></a>
21
+
22
+ ## [2.0.8](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem/compare/gatsby-source-filesystem@2.0.7...gatsby-source-filesystem@2.0.8) (2018-11-08)
23
+
24
+ **Note:** Version bump only for package gatsby-source-filesystem
25
+
26
+ <a name="2.0.7"></a>
27
+
28
+ ## [2.0.7](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem/compare/gatsby-source-filesystem@2.0.6...gatsby-source-filesystem@2.0.7) (2018-11-01)
29
+
30
+ **Note:** Version bump only for package gatsby-source-filesystem
31
+
6
32
  <a name="2.0.6"></a>
7
33
 
8
34
  ## [2.0.6](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem/compare/gatsby-source-filesystem@2.0.5...gatsby-source-filesystem@2.0.6) (2018-10-29)
package/README.md CHANGED
@@ -175,6 +175,10 @@ createRemoteFileNode({
175
175
  // OPTIONAL
176
176
  // Adds htaccess authentication to the download request if passed in.
177
177
  auth: { htaccess_user: `USER`, htaccess_pass: `PASSWORD` },
178
+
179
+ // OPTIONAL
180
+ // Sets the file extension
181
+ ext: ".jpg",
178
182
  })
179
183
  ```
180
184
 
@@ -222,3 +226,20 @@ exports.downloadMediaFiles = ({
222
226
  ```
223
227
 
224
228
  The file node can then be queried using GraphQL. See an example of this in the [gatsby-source-wordpress README](/packages/gatsby-source-wordpress/#image-processing) where downloaded images are queried using [gatsby-transformer-sharp](/packages/gatsby-transformer-sharp/) to use in the component [gatsby-image](/packages/gatsby-image/).
229
+
230
+ #### Retrieving the remote file extension
231
+
232
+ The helper tries first to retrieve the file extension by parsing the url and the path provided (e.g. if the url is https://example.com/image.jpg, the extension will be inferred as `.jpg`). If the url does not contain an extension, we use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the extension _can_ be explicitly passed, like so:
233
+
234
+ ```javascript
235
+ createRemoteFileNode({
236
+ // The source url of the remote file
237
+ url: `https://example.com/a-file-without-an-extension`,
238
+ store,
239
+ cache,
240
+ createNode,
241
+ createNodeId,
242
+ // if necessary!
243
+ ext: ".jpg",
244
+ })
245
+ ```
@@ -17,6 +17,10 @@ const _require = require(`valid-url`),
17
17
 
18
18
  const Queue = require(`better-queue`);
19
19
 
20
+ const readChunk = require(`read-chunk`);
21
+
22
+ const fileType = require(`file-type`);
23
+
20
24
  const _require2 = require(`./create-file-node`),
21
25
  createFileNode = _require2.createFileNode;
22
26
 
@@ -135,7 +139,6 @@ function pushToQueue(_x, _x2) {
135
139
  * @param {String} url
136
140
  * @param {Headers} headers
137
141
  * @param {String} tmpFilename
138
- * @param {String} filename
139
142
  * @return {Promise<Object>} Resolves with the [http Result Object]{@link https://nodejs.org/api/http.html#http_class_http_serverresponse}
140
143
  */
141
144
 
@@ -152,7 +155,7 @@ function _pushToQueue() {
152
155
  return _pushToQueue.apply(this, arguments);
153
156
  }
154
157
 
155
- const requestRemoteNode = (url, headers, tmpFilename, filename) => new Promise((resolve, reject) => {
158
+ const requestRemoteNode = (url, headers, tmpFilename) => new Promise((resolve, reject) => {
156
159
  const responseStream = got.stream(url, Object.assign({}, headers, {
157
160
  timeout: 30000,
158
161
  retries: 5
@@ -199,7 +202,8 @@ function _processRemoteNode() {
199
202
  cache,
200
203
  createNode,
201
204
  auth = {},
202
- createNodeId
205
+ createNodeId,
206
+ ext
203
207
  }) {
204
208
  // Ensure our cache directory exists.
205
209
  const programDir = store.getState().program.directory;
@@ -220,14 +224,28 @@ function _processRemoteNode() {
220
224
 
221
225
 
222
226
  const digest = createHash(url);
223
- const ext = getRemoteFileExtension(url);
224
- const tmpFilename = createFilePath(programDir, `tmp-${digest}`, ext);
225
- const filename = createFilePath(programDir, digest, ext); // Fetch the file.
227
+
228
+ if (!ext) {
229
+ ext = getRemoteFileExtension(url);
230
+ }
231
+
232
+ const tmpFilename = createFilePath(programDir, `tmp-${digest}`, ext); // Fetch the file.
226
233
 
227
234
  try {
228
- const response = yield requestRemoteNode(url, headers, tmpFilename, filename); // Save the response headers for future requests.
235
+ const response = yield requestRemoteNode(url, headers, tmpFilename); // Save the response headers for future requests.
236
+
237
+ yield cache.set(cacheId(url), response.headers); // If the user did not provide an extension and we couldn't get one from remote file, try and guess one
238
+
239
+ if (ext === ``) {
240
+ const buffer = readChunk.sync(tmpFilename, 0, fileType.minimumBytes);
241
+ const filetype = fileType(buffer);
242
+
243
+ if (filetype) {
244
+ ext = `.${filetype.ext}`;
245
+ }
246
+ }
229
247
 
230
- cache.set(cacheId(url), response.headers); // If the status code is 200, move the piped temp file to the real name.
248
+ const filename = createFilePath(programDir, digest, ext); // If the status code is 200, move the piped temp file to the real name.
231
249
 
232
250
  if (response.statusCode === 200) {
233
251
  yield fs.move(tmpFilename, filename, {
@@ -297,10 +315,30 @@ module.exports = ({
297
315
  cache,
298
316
  createNode,
299
317
  auth = {},
300
- createNodeId
318
+ createNodeId,
319
+ ext = null
301
320
  }) => {
302
- // Check if we already requested node for this remote file
321
+ // validation of the input
322
+ // without this it's notoriously easy to pass in the wrong `createNodeId`
323
+ // see gatsbyjs/gatsby#6643
324
+ if (typeof createNodeId !== `function`) {
325
+ throw new Error(`createNodeId must be a function, was ${typeof createNodeId}`);
326
+ }
327
+
328
+ if (typeof createNode !== `function`) {
329
+ throw new Error(`createNode must be a function, was ${typeof createNode}`);
330
+ }
331
+
332
+ if (typeof store !== `object`) {
333
+ throw new Error(`store must be the redux store, was ${typeof store}`);
334
+ }
335
+
336
+ if (typeof cache !== `object`) {
337
+ throw new Error(`cache must be the Gatsby cache, was ${typeof cache}`);
338
+ } // Check if we already requested node for this remote file
303
339
  // and return stored promise if we did.
340
+
341
+
304
342
  if (processingCache[url]) {
305
343
  return processingCache[url];
306
344
  }
@@ -317,6 +355,7 @@ module.exports = ({
317
355
  cache,
318
356
  createNode,
319
357
  createNodeId,
320
- auth
358
+ auth,
359
+ ext
321
360
  });
322
361
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gatsby-source-filesystem",
3
3
  "description": "Gatsby plugin which parses files within a directory for further parsing by other plugins",
4
- "version": "2.0.6",
4
+ "version": "2.0.10",
5
5
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
6
6
  "bugs": {
7
7
  "url": "https://github.com/gatsbyjs/gatsby/issues"
@@ -11,11 +11,13 @@
11
11
  "better-queue": "^3.8.7",
12
12
  "bluebird": "^3.5.0",
13
13
  "chokidar": "^1.7.0",
14
+ "file-type": "^10.2.0",
14
15
  "fs-extra": "^5.0.0",
15
16
  "got": "^7.1.0",
16
17
  "md5-file": "^3.1.1",
17
18
  "mime": "^2.2.0",
18
19
  "pretty-bytes": "^4.0.2",
20
+ "read-chunk": "^3.0.0",
19
21
  "slash": "^1.0.0",
20
22
  "valid-url": "^1.0.9",
21
23
  "xstate": "^3.1.0"
@@ -23,7 +25,7 @@
23
25
  "devDependencies": {
24
26
  "@babel/cli": "^7.0.0",
25
27
  "@babel/core": "^7.0.0",
26
- "babel-preset-gatsby-package": "^0.1.2",
28
+ "babel-preset-gatsby-package": "^0.1.3",
27
29
  "cross-env": "^5.1.4"
28
30
  },
29
31
  "homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-filesystem#readme",
@@ -41,5 +43,5 @@
41
43
  "prepare": "cross-env NODE_ENV=production npm run build",
42
44
  "watch": "babel -w src --out-dir . --ignore **/__tests__"
43
45
  },
44
- "gitHead": "dbd71c33ced0829c5f4bc552127ef80b4a7398a5"
46
+ "gitHead": "a3e548956009fecbcbd5e55f11e7803cbf5253d0"
45
47
  }