Skip to content

Commit f68d40b

Browse files
author
pooya parsa
committed
use HEAD for exists
1 parent 78bac43 commit f68d40b

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ fs.readFile('/test/file.txt').then((contents) => {
7171

7272
### Options
7373

74-
- `endpoint`: Address endpoint of server
75-
- `maxAge`: Time to keep requests in cache
74+
#### `endpoint`
75+
76+
Required. HTTP URL of server.
7677

7778
## License
7879

lib/client.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const fetch = require('node-fetch')
2-
const pMemoize = require('p-memoize')
32

43
module.exports = class HTTPFSClient {
5-
constructor ({ endpoint, maxAge = 1000 }) {
6-
this.endpoint = endpoint.replace(/\/$/, '')
7-
this._request = pMemoize(this._request, { maxAge })
4+
constructor (options = {}) {
5+
if (!options.endpoint) {
6+
throw new Error('Missing required option `endpoint`')
7+
}
8+
this.endpoint = options.endpoint.replace(/\/$/, '')
89
}
910

1011
_url (path) {
@@ -18,11 +19,11 @@ module.exports = class HTTPFSClient {
1819
}
1920

2021
async exists (path) {
21-
const res = await this._request(path)
22+
const res = await this._request(path, { method: 'HEAD' })
2223
switch (res.status) {
2324
case 200: return true
2425
case 404: return false
25-
default: throw new Error('Invalid status ' + res.status)
26+
default: throw new Error('Unexpected status ' + res.status)
2627
}
2728
}
2829

lib/middleware.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ module.exports = function HTTPFSMiddleware (mfs) {
1212
return res.end('No such a file or directory: ' + resourcePath)
1313
}
1414

15+
// End response for HEAD requests
16+
if (req.method === 'HEAD') {
17+
return res.end()
18+
}
19+
1520
// Directory listing
1621
if (stat.isDirectory()) {
1722
return res.end(`

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
},
3434
"dependencies": {
3535
"errno": "^0.1.7",
36-
"node-fetch": "^2.3.0",
37-
"p-memoize": "^2.1.0"
36+
"node-fetch": "^2.3.0"
3837
}
3938
}

yarn.lock

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3633,14 +3633,6 @@ p-locate@^3.0.0:
36333633
dependencies:
36343634
p-limit "^2.0.0"
36353635

3636-
p-memoize@^2.1.0:
3637-
version "2.1.0"
3638-
resolved "https://registry.yarnpkg.com/p-memoize/-/p-memoize-2.1.0.tgz#9ac80c8cf9373c52dfece6aae1fd2e300602898a"
3639-
integrity sha512-c6+a2iV4JyX0r4+i2IBJYO0r6LZAT2fg/tcB6GQbv1uzZsfsmKT7Ej5DRT1G6Wi7XUJSV2ZiP9+YEtluvhCmkg==
3640-
dependencies:
3641-
mem "^4.0.0"
3642-
mimic-fn "^1.0.0"
3643-
36443636
p-reduce@^1.0.0:
36453637
version "1.0.0"
36463638
resolved "https://registry.yarnpkg.com/p-reduce/-/p-reduce-1.0.0.tgz#18c2b0dd936a4690a529f8231f58a0fdb6a47dfa"

0 commit comments

Comments
 (0)