Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 5ed444c

Browse files
committed
quick refactor
1 parent 9dc8c99 commit 5ed444c

File tree

5 files changed

+54
-32
lines changed

5 files changed

+54
-32
lines changed

src/http/gateway/utils/html.js renamed to src/http/gateway/dir-view/index.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
const filesize = require('filesize')
44

5-
const HTML_PAGE_STYLE = require('./style')
6-
const PathUtil = require('./path')
5+
const mainStyle = require('./style')
6+
const pathUtil = require('../utils/path')
77

8-
const getParentDirectoryURL = (originalParts) => {
8+
function getParentDirectoryURL (originalParts) {
99
const parts = originalParts.splice()
1010

1111
if (parts.length > 1) {
@@ -15,11 +15,11 @@ const getParentDirectoryURL = (originalParts) => {
1515
return [ '', 'ipfs' ].concat(parts).join('/')
1616
}
1717

18-
const buildFilesList = (path, links) => {
18+
function buildFilesList (path, links) {
1919
const rows = links.map((link) => {
2020
let row = [
2121
`<div class="ipfs-icon ipfs-_blank">&nbsp;</div>`,
22-
`<a href="${PathUtil.joinURLParts(path, link.name)}">${link.name}</a>`,
22+
`<a href="${pathUtil.joinURLParts(path, link.name)}">${link.name}</a>`,
2323
filesize(link.size)
2424
]
2525

@@ -31,9 +31,9 @@ const buildFilesList = (path, links) => {
3131
return rows.join('')
3232
}
3333

34-
const buildTable = (path, links) => {
35-
const parts = PathUtil.splitPath(path)
36-
let parentDirectoryURL = getParentDirectoryURL(parts)
34+
function buildTable (path, links) {
35+
const parts = pathUtil.splitPath(path)
36+
const parentDirectoryURL = getParentDirectoryURL(parts)
3737

3838
return `
3939
<table class="table table-striped">
@@ -53,14 +53,14 @@ const buildTable = (path, links) => {
5353
`
5454
}
5555

56-
module.exports.build = (path, links) => {
56+
function render (path, links) {
5757
return `
5858
<!DOCTYPE html>
5959
<html>
6060
<head>
6161
<meta charset="utf-8">
6262
<title>${path}</title>
63-
<style>${HTML_PAGE_STYLE}</style>
63+
<style>${mainStyle}</style>
6464
</head>
6565
<body>
6666
<div id="header" class="row">
@@ -81,3 +81,6 @@ module.exports.build = (path, links) => {
8181
</html>
8282
`
8383
}
84+
85+
exports = module.exports
86+
exports.render = render
File renamed without changes.

src/http/gateway/resolver.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ const Unixfs = require('ipfs-unixfs')
88
const debug = require('debug')
99
const log = debug('jsipfs:http-gateway:resolver')
1010
log.error = debug('jsipfs:http-gateway:resolver:error')
11-
const html = require('./utils/html')
12-
const PathUtil = require('./utils/path')
1311

14-
const INDEX_HTML_FILES = [ 'index.html', 'index.htm', 'index.shtml' ]
12+
const dirView = require('./dir-view')
13+
const pathUtil = require('./utils/path')
14+
15+
function getIndexFiles (links) {
16+
const INDEX_HTML_FILES = [
17+
'index.html',
18+
'index.htm',
19+
'index.shtml'
20+
]
21+
22+
return links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)
23+
}
1524

1625
function noop () {}
1726

@@ -23,22 +32,20 @@ const resolveDirectory = promisify((ipfs, path, multihash, callback) => {
2332
ipfs.object.get(multihash, { enc: 'base58' }, (err, dagNode) => {
2433
if (err) { return callback(err) }
2534

26-
const links = dagNode.links
27-
const indexFiles = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)
35+
const indexFiles = getIndexFiles(dagNode.links)
2836

29-
// found index file in links
3037
if (indexFiles.length > 0) {
3138
return callback(null, indexFiles)
3239
}
3340

34-
return callback(null, html.build(path, links))
41+
return callback(null, dirView.build(path, dagNode.links))
3542
})
3643
})
3744

3845
const resolveMultihash = promisify((ipfs, path, callback) => {
3946
callback = callback || noop
4047

41-
const parts = PathUtil.splitPath(path)
48+
const parts = pathUtil.splitPath(path)
4249
const partsLength = parts.length
4350

4451
let currentMultihash = parts[0]
@@ -47,8 +54,8 @@ const resolveMultihash = promisify((ipfs, path, callback) => {
4754
// throws error when invalid CID is passed
4855
try {
4956
currentCid = new CID(mh.fromB58String(currentMultihash))
50-
} catch (e) {
51-
if (e) throw e
57+
} catch (err) {
58+
return next(err)
5259
}
5360

5461
log('currentMultihash: ', currentMultihash)
@@ -94,6 +101,7 @@ const resolveMultihash = promisify((ipfs, path, callback) => {
94101
})
95102
}, (err) => {
96103
if (err) { return callback(err) }
104+
97105
callback(null, { multihash: currentMultihash })
98106
})
99107
})

src/http/gateway/resources/gateway.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const pull = require('pull-stream')
77
const toPull = require('stream-to-pull-stream')
88
const fileType = require('file-type')
99
const mime = require('mime-types')
10+
const Stream = require('readable-stream')
11+
1012
const GatewayResolver = require('../resolver')
1113
const PathUtils = require('../utils/path')
12-
const Stream = require('stream')
1314

1415
module.exports = {
1516
checkCID: (request, reply) => {

src/http/gateway/utils/path.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
11
'use strict'
22

3-
const splitPath = (path) => {
4-
if (path[path.length - 1] === '/') path = path.substring(0, path.length - 1)
3+
function splitPath (path) {
4+
if (path[path.length - 1] === '/') {
5+
path = path.substring(0, path.length - 1)
6+
}
7+
58
return path.substring(6).split('/')
69
}
710

8-
const removeLeadingSlash = (url) => {
9-
if (url[0] === '/') url = url.substring(1)
11+
function removeLeadingSlash (url) {
12+
if (url[0] === '/') {
13+
url = url.substring(1)
14+
}
15+
1016
return url
1117
}
1218

13-
const removeTrailingSlash = (url) => {
14-
if (url.endsWith('/')) url = url.substring(0, url.length - 1)
19+
function removeTrailingSlash (url) {
20+
if (url.endsWith('/')) {
21+
url = url.substring(0, url.length - 1)
22+
}
23+
1524
return url
1625
}
1726

18-
const removeSlashFromBothEnds = (url) => {
27+
function removeSlashFromBothEnds (url) {
1928
url = removeLeadingSlash(url)
2029
url = removeTrailingSlash(url)
30+
2131
return url
2232
}
2333

24-
const joinURLParts = (...urls) => {
34+
function joinURLParts (...urls) {
2535
urls = urls.filter((url) => url.length > 0)
2636
urls = [ '' ].concat(urls.map((url) => removeSlashFromBothEnds(url)))
2737

2838
return urls.join('/')
2939
}
3040

3141
module.exports = {
32-
splitPath,
33-
removeTrailingSlash,
34-
joinURLParts
42+
splitPath: splitPath,
43+
removeTrailingSlash: removeTrailingSlash,
44+
joinURLParts: joinURLParts
3545
}

0 commit comments

Comments
 (0)