Skip to content

Commit

Permalink
Absolute URLs (#9)
Browse files Browse the repository at this point in the history
* [CI] Add test workflow

* [CI] use npm ci instead of install

* [CI] Run on a matrix. OS=[Win,Linux,Mac]; Node=[12,14,16]

* [README.md] add badge for test workflow

* [CI] Rename job from build to test

* [README.md] Add npm badge

* [Tests] use t.same instead of deprecated t.deepEqual

Run npm test

> [email protected] test /home/runner/work/parse-apache-directory-index/parse-apache-directory-index
> tap --no-check-coverage test/*.js && semistandard

TAP version 13
(node:1830) DeprecationWarning: deepEqual() is deprecated, use same() instead

* Handle absolute URLs
  • Loading branch information
ViliusSutkus89 authored Jul 18, 2021
1 parent 147ff07 commit c22cd11
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,22 @@ module.exports = src => {
const $tds = $(tr).find('td');
const getCol = label => fieldCols[label] === undefined ? null : $tds.eq(fieldCols[label]);
const getColText = label => getCol(label) && getCol(label).text().trim();

const path = getCol('Name').children().eq(0).attr('href');
const name = getColText('Name');

// Ignore 'Parent Directory' row
if (name === 'Parent Directory' || !name) return;

let path = getCol('Name').children().eq(0).attr('href');
if (!path.startsWith('http://') && !path.startsWith('https://')) {
path = join(dir, path);
}

files.push({
type: path.endsWith('/')
? 'directory'
: 'file',
name: name,
path: join(dir, path),
path: path,
lastModified: getCol('Last modified') && new Date(getColText('Last modified')),
size: getCol('Size') && bytes(getColText('Size')),
description: getColText('Description')
Expand Down
48 changes: 48 additions & 0 deletions test/fixture/apache-index-absolute-and-relative-urls.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<html>
<head>
<title>Index of /foo/bar</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1>Index of /foo/bar</h1>
<table cellspacing="10">
<tr>
<th align="left">Name</th>
<th>Last modified</th>
<th>Size</th>
<th>Description</th>
</tr>
<tr>
<td><a href="../">Parent Directory</a></td>
</tr>
<tr>
<td><a href="https://www.example.org/subdir-absolute/">subdir-absolute/</a></td>
<td>Wed Jun 30 22:24:50 UTC 2021</td>
<td align="right"></td>
<td> </td>
</tr>
<tr>
<td><a href="subdir-relative/">subdir-relative/</a></td>
<td>Wed Jun 30 22:24:50 UTC 2021</td>
<td align="right"></td>
<td> </td>
</tr>
<tr>
<td><a href="https://www.example.org/subfile-absolute.xml">subfile-absolute.xml</a></td>
<td>Sun Jul 04 16:22:15 UTC 2021</td>
<td align="right">
25
</td>
<td> </td>
</tr>
<tr>
<td><a href="subfile-relative.xml">subfile-relative.xml</a></td>
<td>Sun Jul 04 16:22:15 UTC 2021</td>
<td align="right">
25
</td>
<td></td>
</tr>
</table>
</body>
</html>
43 changes: 43 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,46 @@ test('should parse apache directory index files (alt format)', t => {
});
t.end();
});

test('Should parse both absolute and relative URLs', t => {
const index = fs.readFileSync(join(__dirname, '/fixture/apache-index-absolute-and-relative-urls.html'), 'utf8');

t.same(parse(index), {
dir: '/foo/bar',
files: [
{
name: 'subdir-absolute/',
type: 'directory',
path: 'https://www.example.org/subdir-absolute/',
description: '',
size: null,
lastModified: new Date('2021-06-30T22:24:50.000Z')
},
{
name: 'subdir-relative/',
type: 'directory',
path: '/foo/bar/subdir-relative/',
size: null,
lastModified: new Date('2021-06-30T22:24:50.000Z'),
description: ''
},
{
name: 'subfile-absolute.xml',
type: 'file',
path: 'https://www.example.org/subfile-absolute.xml',
size: 25,
lastModified: new Date('2021-07-04T16:22:15.000Z'),
description: ''
},
{
name: 'subfile-relative.xml',
type: 'file',
path: '/foo/bar/subfile-relative.xml',
size: 25,
lastModified: new Date('2021-07-04T16:22:15.000Z'),
description: ''
}
]
});
t.end();
});

0 comments on commit c22cd11

Please sign in to comment.