Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions src/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,51 @@ describe('format(obj)', function () {
);
});

it('should not repeat an extended parameter that is already present', function () {
assert.strictEqual(
format({
type: 'attachment',
parameters: {
filename: '€ rates.pdf',
'filename*': "UTF-8''%E2%82%AC%20rates.pdf",
},
}),
"attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf",
);
});
it.each([
{
parameters: {
filename: '€ rates.pdf',
'filename*': "UTF-8''explicit.pdf",
},
expected:
"attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf; filename*=UTF-8''explicit.pdf",
},
{
parameters: {
'filename*': "UTF-8''explicit.pdf",
filename: '€ rates.pdf',
},
expected:
"attachment; filename*=UTF-8''explicit.pdf; filename*=UTF-8''%E2%82%AC%20rates.pdf",
},
])(
'should emit provided and generated extended parameters in order: $expected',
function ({ parameters, expected }) {
assert.strictEqual(format({ type: 'attachment', parameters }), expected);
},
);

it('should not repeat an extended parameter listed before its value', function () {
assert.strictEqual(
format({
type: 'attachment',
parameters: {
'filename*': "UTF-8''%E2%82%AC%20rates.pdf",
filename: '€ rates.pdf',
},
}),
"attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf",
);
});
it.each([
{ options: {}, expected: 'attachment; filename=first; FILENAME=second' },
{
options: { multipart: true },
expected: 'attachment; filename="first"; FILENAME="second"',
},
])(
'should emit provided case-insensitive duplicates: $expected',
function ({ options, expected }) {
assert.strictEqual(
format(
{
type: 'attachment',
parameters: { filename: 'first', FILENAME: 'second' },
},
options,
),
expected,
);
},
);

it('should format the result of parse without repeating a parameter', function () {
assert.strictEqual(
Expand Down
28 changes: 15 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export function parse(
const type =
(typeFlags & CASE_FLAGS) === 0 ? typeValue : typeValue.toLowerCase();
const parameters: Record<string, string> = new NullObject();
const seenKeys: Record<string, boolean> = new NullObject();

parameter: while (index < len) {
index++; // Skip over semicolon.
Expand Down Expand Up @@ -184,7 +185,7 @@ export function parse(
while (index < len) {
const code = header.charCodeAt(index);
if (code === DQUOTE) {
if (parameters[key] === undefined) {
if (parameters[key] === undefined && !seenKeys[key]) {
parameters[key] = encoded
? multipart
? decodeMultipartEscapes(header, quotedStart, index)
Expand All @@ -199,6 +200,7 @@ export function parse(
}
index++;
}
seenKeys[key] = true;
continue parameter;
}

Expand All @@ -220,12 +222,17 @@ export function parse(
index++;
}

seenKeys[key] = true;
continue parameter;
}

const valueStart = index;
const delimiter = header.indexOf(';', index);
index = delimiter === -1 ? len : delimiter;

if (seenKeys[key]) continue parameter;
seenKeys[key] = true;

let valueEnd = index;
while (
valueEnd > valueStart &&
Expand All @@ -235,16 +242,16 @@ export function parse(
}
const value = header.slice(valueStart, valueEnd);

if (parameters[key] === undefined) {
parameters[key] = value;

if (extended && key.charCodeAt(key.length - 1) === ASTERISK) {
const normalizedKey = key.slice(0, -1);
const decoded = decodeExtended(value);
if (decoded !== undefined) parameters[normalizedKey] = decoded;
if (extended && key.charCodeAt(key.length - 1) === ASTERISK) {
const normalizedKey = key.slice(0, -1);
const decoded = decodeExtended(value);
if (decoded !== undefined) {
parameters[normalizedKey] = decoded;
continue parameter;
}
}

if (parameters[key] === undefined) parameters[key] = value;
continue parameter;
}

Expand Down Expand Up @@ -495,11 +502,6 @@ export function format(
throw new TypeError('Invalid extended parameter value: ' + value);
}

// When `param` and `param*` are provided, prefer the already extended form.
if (parameters[param + '*'] !== undefined) {
continue;
}

result += '; ' + param + '*=' + encodeExtended(value);
}
}
Expand Down
Loading
Loading