Skip to content

Commit a6c8142

Browse files
authored
Remove dependency on node:path (#68)
1 parent 5879db8 commit a6c8142

3 files changed

Lines changed: 83 additions & 9 deletions

File tree

HISTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
=================
3+
4+
* refactor: use simplified `basename` function and remove dependency on `node:path`
5+
16
1.0.1 / 2025-11-18
27
=================
38

index.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
module.exports = contentDisposition
1515
module.exports.parse = parse
1616

17-
/**
18-
* Module dependencies.
19-
* @private
20-
*/
21-
22-
var basename = require('path').basename
23-
2417
/**
2518
* RegExp to match non attr-char, *after* encodeURIComponent (i.e. not including "%")
2619
* @private
@@ -456,3 +449,29 @@ function ContentDisposition (type, parameters) {
456449
this.type = type
457450
this.parameters = parameters
458451
}
452+
453+
/**
454+
* Return the last portion of a path
455+
*
456+
* @param {string} path
457+
* @returns {string}
458+
*/
459+
function basename (path) {
460+
const normalized = path.replaceAll('\\', '/')
461+
462+
let end = normalized.length
463+
while (end > 0 && normalized[end - 1] === '/') {
464+
end--
465+
}
466+
467+
if (end === 0) {
468+
return ''
469+
}
470+
471+
let start = end - 1
472+
while (start >= 0 && normalized[start] !== '/') {
473+
start--
474+
}
475+
476+
return normalized.slice(start + 1, end)
477+
}

test/test.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,51 @@ describe('contentDisposition(filename)', function () {
1919
'attachment; filename="plans.pdf"')
2020
})
2121

22-
it('should use the basename of the string', function () {
22+
it('should use the basename of a posix path', function () {
2323
assert.strictEqual(contentDisposition('/path/to/plans.pdf'),
2424
'attachment; filename="plans.pdf"')
2525
})
2626

27+
it('should use the basename of a windows path', function () {
28+
assert.strictEqual(contentDisposition('\\path\\to\\plans.pdf'),
29+
'attachment; filename="plans.pdf"')
30+
})
31+
32+
it('should use the basename of a windows path with drive letter', function () {
33+
assert.strictEqual(contentDisposition('C:\\path\\to\\plans.pdf'),
34+
'attachment; filename="plans.pdf"')
35+
})
36+
37+
it('should use the basename of a posix path with trailing slash', function () {
38+
assert.strictEqual(contentDisposition('/path/to/plans.pdf/'),
39+
'attachment; filename="plans.pdf"')
40+
})
41+
42+
it('should use the basename of a windows path with trailing slash', function () {
43+
assert.strictEqual(contentDisposition('\\path\\to\\plans.pdf\\'),
44+
'attachment; filename="plans.pdf"')
45+
})
46+
47+
it('should use the basename of a windows path with drive letter and trailing slash', function () {
48+
assert.strictEqual(contentDisposition('C:\\path\\to\\plans.pdf\\'),
49+
'attachment; filename="plans.pdf"')
50+
})
51+
52+
it('should use the basename of a posix path with trailing slashes', function () {
53+
assert.strictEqual(contentDisposition('/path/to/plans.pdf///'),
54+
'attachment; filename="plans.pdf"')
55+
})
56+
57+
it('should use the basename of a windows path with trailing slashes', function () {
58+
assert.strictEqual(contentDisposition('\\path\\to\\plans.pdf\\\\\\'),
59+
'attachment; filename="plans.pdf"')
60+
})
61+
62+
it('should use the basename of a windows path with drive letter and trailing slashes', function () {
63+
assert.strictEqual(contentDisposition('C:\\path\\to\\plans.pdf\\\\\\'),
64+
'attachment; filename="plans.pdf"')
65+
})
66+
2767
describe('when "filename" is US-ASCII', function () {
2868
it('should only include filename parameter', function () {
2969
assert.strictEqual(contentDisposition('plans.pdf'),
@@ -137,11 +177,21 @@ describe('contentDisposition(filename, options)', function () {
137177
'attachment; filename="plans.pdf"')
138178
})
139179

140-
it('should use the basename of the string', function () {
180+
it('should use the basename of a posix path', function () {
141181
assert.strictEqual(contentDisposition('€ rates.pdf', { fallback: '/path/to/EURO rates.pdf' }),
142182
'attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf')
143183
})
144184

185+
it('should use the basename of a windows path', function () {
186+
assert.strictEqual(contentDisposition('€ rates.pdf', { fallback: '\\path\\to\\EURO rates.pdf' }),
187+
'attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf')
188+
})
189+
190+
it('should use the basename of a windows path with drive letter', function () {
191+
assert.strictEqual(contentDisposition('€ rates.pdf', { fallback: 'C:\\path\\to\\EURO rates.pdf' }),
192+
'attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf')
193+
})
194+
145195
it('should do nothing without filename option', function () {
146196
assert.strictEqual(contentDisposition(undefined, { fallback: 'plans.pdf' }),
147197
'attachment')

0 commit comments

Comments
 (0)