Repository: jshttp/content-disposition Branch: master Commit: 70862364176c Files: 14 Total size: 67.2 KB Directory structure: gitextract_wsx8uper/ ├── .editorconfig ├── .github/ │ └── workflows/ │ ├── ci.yml │ ├── codeql.yml │ └── scorecard.yml ├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── src/ │ ├── create.spec.ts │ ├── index.bench.ts │ ├── index.ts │ └── parse.spec.ts ├── tsconfig.build.json └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*] charset = utf-8 end_of_line = lf indent_size = 2 indent_style = space insert_final_newline = true trim_trailing_whitespace = true quote_type = single ================================================ FILE: .github/workflows/ci.yml ================================================ name: CI on: - push - pull_request permissions: contents: read jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: matrix: node-version: - 18 - '*' steps: - uses: actions/checkout@v6 - uses: actions/setup-node@v6 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test - uses: codecov/codecov-action@v5 with: name: Node.js ${{ matrix.node-version }} token: ${{ secrets.CODECOV_TOKEN }} ================================================ FILE: .github/workflows/codeql.yml ================================================ # For most projects, this workflow file will not need changing; you simply need # to commit it to your repository. # # You may wish to alter this file to override the set of languages analyzed, # or to provide custom queries or build logic. # # ******** NOTE ******** # We have attempted to detect the languages in your repository. Please check # the `language` matrix defined below to confirm you have the correct set of # supported CodeQL languages. # name: 'CodeQL' on: push: branches: ['master'] pull_request: # The branches below must be a subset of the branches above branches: ['master'] schedule: - cron: '0 0 * * 1' permissions: contents: read jobs: analyze: name: Analyze runs-on: ubuntu-latest permissions: actions: read contents: read security-events: write steps: - name: Checkout repository uses: actions/checkout@v6 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@v4 with: languages: javascript # If you wish to specify custom queries, you can do so here or in a config file. # By default, queries listed here will override any specified in a config file. # Prefix the list here with "+" to use these queries and those in the config file. # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) # - name: Autobuild # uses: github/codeql-action/autobuild@1b549b9259bda1cb5ddde3b41741a82a2d15a841 # v3.28.13 # ℹ️ Command-line programs to run using the OS shell. # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun # If the Autobuild fails above, remove it and uncomment the following three lines. # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. # - run: | # echo "Run, Build Application using script" # ./location_of_script_within_repo/buildscript.sh - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v4 with: category: '/language:javascript' ================================================ FILE: .github/workflows/scorecard.yml ================================================ # This workflow uses actions that are not certified by GitHub. They are provided # by a third-party and are governed by separate terms of service, privacy # policy, and support documentation. name: Scorecard supply-chain security on: # For Branch-Protection check. Only the default branch is supported. See # https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection branch_protection_rule: # To guarantee Maintained check is occasionally updated. See # https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained schedule: - cron: '16 21 * * 1' push: branches: ['master'] # Declare default permissions as read only. permissions: read-all jobs: analysis: name: Scorecard analysis runs-on: ubuntu-latest permissions: # Needed to upload the results to code-scanning dashboard. security-events: write # Needed to publish results and get a badge (see publish_results below). id-token: write # Uncomment the permissions below if installing in a private repository. # contents: read # actions: read steps: - name: 'Checkout code' uses: actions/checkout@v6 with: persist-credentials: false - name: 'Run analysis' uses: ossf/scorecard-action@v2.4.3 with: results_file: results.sarif results_format: sarif # (Optional) "write" PAT token. Uncomment the `repo_token` line below if: # - you want to enable the Branch-Protection check on a *public* repository, or # - you are installing Scorecard on a *private* repository # To create the PAT, follow the steps in https://github.com/ossf/scorecard-action#authentication-with-pat. # repo_token: ${{ secrets.SCORECARD_TOKEN }} # Public repositories: # - Publish results to OpenSSF REST API for easy access by consumers # - Allows the repository to include the Scorecard badge. # - See https://github.com/ossf/scorecard-action#publishing-results. # For private repositories: # - `publish_results` will always be set to `false`, regardless # of the value entered here. publish_results: true # Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF # format to the repository Actions tab. - name: 'Upload artifact' uses: actions/upload-artifact@v6 with: name: SARIF file path: results.sarif retention-days: 5 # Upload the results to GitHub's code scanning dashboard. - name: 'Upload to code-scanning' uses: github/codeql-action/upload-sarif@v4 with: sarif_file: results.sarif ================================================ FILE: .gitignore ================================================ coverage/ node_modules/ npm-debug.log package-lock.json dist/ *.tsbuildinfo ================================================ FILE: LICENSE ================================================ (The MIT License) Copyright (c) 2014-2017 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # content-disposition [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Node.js Version][node-version-image]][node-version-url] [![Build Status][github-actions-ci-image]][github-actions-ci-url] [![Test Coverage][coveralls-image]][coveralls-url] Create and parse HTTP `Content-Disposition` header ## Installation ```sh $ npm install content-disposition ``` ## API ```js import { create, parse } from 'content-disposition'; ``` ### create(filename, options) Create an attachment `Content-Disposition` header value using the given file name, if supplied. The `filename` is optional and if no file name is desired, but you want to specify `options`, set `filename` to `undefined`. ```js res.setHeader('Content-Disposition', create('∫ maths.pdf')); ``` **note** HTTP headers are of the ISO-8859-1 character set. If you are writing this header through a means different from `setHeader` in Node.js, you'll want to specify the `'binary'` encoding in Node.js. #### Options `contentDisposition` accepts these properties in the options object. ##### fallback If the `filename` option is outside ISO-8859-1, then the file name is actually stored in a supplemental field for clients that support Unicode file names and a ISO-8859-1 version of the file name is automatically generated. This specifies the ISO-8859-1 file name to override the automatic generation or disables the generation all together, defaults to `true`. - A string will specify the ISO-8859-1 file name to use in place of automatic generation. - `false` will disable including a ISO-8859-1 file name and only include the Unicode version (unless the file name is already ISO-8859-1). - `true` will enable automatic generation if the file name is outside ISO-8859-1. If the `filename` option is ISO-8859-1 and this option is specified and has a different value, then the `filename` option is encoded in the extended field and this set as the fallback field, even though they are both ISO-8859-1. ##### type Specifies the disposition type, defaults to `"attachment"`. This can also be `"inline"`, or any other value (all values except inline are treated like `attachment`, but can convey additional information if both parties agree to it). The type is normalized to lower-case. ### parse(string) ```js const disposition = contentDisposition.parse( 'attachment; filename="EURO rates.txt"; filename*=UTF-8\'\'%e2%82%ac%20rates.txt', ); ``` Parse a `Content-Disposition` header string. This automatically handles extended ("Unicode") parameters by decoding them and providing them under the standard parameter name. This will return an object with the following properties (examples are shown for the string `'attachment; filename="EURO rates.txt"; filename*=UTF-8\'\'%e2%82%ac%20rates.txt'`): - `type`: The disposition type (always lower case). Example: `'attachment'` - `parameters`: An object of the parameters in the disposition (name of parameter always lower case and extended versions replace non-extended versions). Example: `{filename: "€ rates.txt"}` ## Examples ### Send a file for download ```js const contentDisposition = require('content-disposition'); const fs = require('fs'); const http = require('http'); const onFinished = require('on-finished'); const filePath = '/path/to/public/plans.pdf'; http.createServer(function onRequest(req, res) { // set headers res.setHeader('Content-Type', 'application/pdf'); res.setHeader('Content-Disposition', contentDisposition(filePath)); // send file const stream = fs.createReadStream(filePath); stream.pipe(res); onFinished(res, function () { stream.destroy(); }); }); ``` ## Testing ```sh $ npm test ``` ## References - [RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1][rfc-2616] - [RFC 5987: Character Set and Language Encoding for Hypertext Transfer Protocol (HTTP) Header Field Parameters][rfc-5987] - [RFC 6266: Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)][rfc-6266] - [Test Cases for HTTP Content-Disposition header field (RFC 6266) and the Encodings defined in RFCs 2047, 2231 and 5987][tc-2231] [rfc-2616]: https://datatracker.ietf.org/doc/html/rfc2616 [rfc-5987]: https://datatracker.ietf.org/doc/html/rfc5987 [rfc-6266]: https://datatracker.ietf.org/doc/html/rfc6266 [tc-2231]: http://greenbytes.de/tech/tc2231/ ## License [MIT](LICENSE) [npm-image]: https://img.shields.io/npm/v/content-disposition [npm-url]: https://www.npmjs.com/package/content-disposition [node-version-image]: https://img.shields.io/node/v/content-disposition [node-version-url]: https://nodejs.org/en/download [coveralls-image]: https://img.shields.io/coverallsCoverage/github/jshttp/content-disposition [coveralls-url]: https://coveralls.io/github/jshttp/content-disposition?branch=master [downloads-image]: https://img.shields.io/npm/dm/content-disposition [downloads-url]: https://www.npmjs.com/package/content-disposition [github-actions-ci-image]: https://img.shields.io/github/actions/workflow/status/jshttp/content-disposition/ci.yml [github-actions-ci-url]: https://github.com/jshttp/content-disposition/actions/workflows/ci.yml ================================================ FILE: package.json ================================================ { "name": "content-disposition", "version": "1.1.0", "description": "Create and parse Content-Disposition header", "keywords": [ "content-disposition", "http", "rfc6266", "res" ], "repository": "jshttp/content-disposition", "funding": { "type": "opencollective", "url": "https://opencollective.com/express" }, "license": "MIT", "author": "Douglas Christopher Wilson ", "type": "commonjs", "exports": "./dist/index.js", "main": "./dist/index.js", "types": "./dist/index.d.ts", "files": [ "dist/" ], "scripts": { "bench": "vitest bench", "build": "ts-scripts build", "format": "ts-scripts format", "lint": "ts-scripts lint", "prepare": "ts-scripts install && npm run build", "specs": "ts-scripts specs", "test": "ts-scripts test" }, "devDependencies": { "@borderless/ts-scripts": "^0.15.0", "@vitest/coverage-v8": "^3.2.4", "typescript": "^5.9.3", "vitest": "^3.2.4" }, "engines": { "node": ">=18" }, "ts-scripts": { "dist": [ "dist" ], "project": [ "tsconfig.build.json" ] } } ================================================ FILE: src/create.spec.ts ================================================ import { describe, it, assert } from 'vitest'; import { create } from './index'; describe('create()', function () { it('should create an attachment header', function () { assert.strictEqual(create(), 'attachment'); }); }); describe('create(filename)', function () { it('should require a string', function () { assert.throws(create.bind(null, 42 as any), /filename.*string/); }); it('should create a header with file name', function () { assert.strictEqual(create('plans.pdf'), 'attachment; filename="plans.pdf"'); }); it('should use the basename of a posix path', function () { assert.strictEqual( create('/path/to/plans.pdf'), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a windows path', function () { assert.strictEqual( create('\\path\\to\\plans.pdf'), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a windows path with drive letter', function () { assert.strictEqual( create('C:\\path\\to\\plans.pdf'), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a posix path with trailing slash', function () { assert.strictEqual( create('/path/to/plans.pdf/'), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a windows path with trailing slash', function () { assert.strictEqual( create('\\path\\to\\plans.pdf\\'), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a windows path with drive letter and trailing slash', function () { assert.strictEqual( create('C:\\path\\to\\plans.pdf\\'), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a posix path with trailing slashes', function () { assert.strictEqual( create('/path/to/plans.pdf///'), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a windows path with trailing slashes', function () { assert.strictEqual( create('\\path\\to\\plans.pdf\\\\\\'), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a windows path with drive letter and trailing slashes', function () { assert.strictEqual( create('C:\\path\\to\\plans.pdf\\\\\\'), 'attachment; filename="plans.pdf"', ); }); describe('when "filename" is US-ASCII', function () { it('should only include filename parameter', function () { assert.strictEqual( create('plans.pdf'), 'attachment; filename="plans.pdf"', ); }); it('should escape quotes', function () { assert.strictEqual( create('the "plans".pdf'), 'attachment; filename="the \\"plans\\".pdf"', ); }); }); describe('when "filename" is ISO-8859-1', function () { it('should only include filename parameter', function () { assert.strictEqual( create('«plans».pdf'), 'attachment; filename="«plans».pdf"', ); }); it('should escape quotes', function () { assert.strictEqual( create('the "plans" (1µ).pdf'), 'attachment; filename="the \\"plans\\" (1µ).pdf"', ); }); }); describe('when "filename" is Unicode', function () { it('should include filename* parameter', function () { assert.strictEqual( create('планы.pdf'), 'attachment; filename="?????.pdf"; filename*=UTF-8\'\'%D0%BF%D0%BB%D0%B0%D0%BD%D1%8B.pdf', ); }); it('should include filename fallback', function () { assert.strictEqual( create('£ and € rates.pdf'), 'attachment; filename="£ and ? rates.pdf"; filename*=UTF-8\'\'%C2%A3%20and%20%E2%82%AC%20rates.pdf', ); assert.strictEqual( create('€ rates.pdf'), 'attachment; filename="? rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf', ); }); it('should encode special characters', function () { assert.strictEqual( create("€'*%().pdf"), "attachment; filename=\"?'*%().pdf\"; filename*=UTF-8''%E2%82%AC%27%2A%25%28%29.pdf", ); }); }); describe('when "filename" contains hex escape', function () { it('should include filename* parameter', function () { assert.strictEqual( create('the%20plans.pdf'), 'attachment; filename="the%20plans.pdf"; filename*=UTF-8\'\'the%2520plans.pdf', ); }); it('should handle Unicode', function () { assert.strictEqual( create('€%20£.pdf'), 'attachment; filename="?%20£.pdf"; filename*=UTF-8\'\'%E2%82%AC%2520%C2%A3.pdf', ); }); }); }); describe('create(filename, options)', function () { describe('with "fallback" option', function () { it('should require a string or Boolean', function () { assert.throws( create.bind(null, 'plans.pdf', { fallback: 42 } as any), /fallback.*string/, ); }); it('should default to true', function () { assert.strictEqual( create('€ rates.pdf'), 'attachment; filename="? rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf', ); }); describe('when "false"', function () { it('should not generate ISO-8859-1 fallback', function () { assert.strictEqual( create('£ and € rates.pdf', { fallback: false }), "attachment; filename*=UTF-8''%C2%A3%20and%20%E2%82%AC%20rates.pdf", ); }); it('should keep ISO-8859-1 filename', function () { assert.strictEqual( create('£ rates.pdf', { fallback: false }), 'attachment; filename="£ rates.pdf"', ); }); }); describe('when "true"', function () { it('should generate ISO-8859-1 fallback', function () { assert.strictEqual( create('£ and € rates.pdf', { fallback: true }), 'attachment; filename="£ and ? rates.pdf"; filename*=UTF-8\'\'%C2%A3%20and%20%E2%82%AC%20rates.pdf', ); }); it('should pass through ISO-8859-1 filename', function () { assert.strictEqual( create('£ rates.pdf', { fallback: true }), 'attachment; filename="£ rates.pdf"', ); }); }); describe('when a string', function () { it('should require an ISO-8859-1 string', function () { assert.throws( create.bind(null, '€ rates.pdf', { fallback: '€ rates.pdf', }), /fallback.*iso-8859-1/i, ); }); it('should use as ISO-8859-1 fallback', function () { assert.strictEqual( create('£ and € rates.pdf', { fallback: '£ and EURO rates.pdf', }), 'attachment; filename="£ and EURO rates.pdf"; filename*=UTF-8\'\'%C2%A3%20and%20%E2%82%AC%20rates.pdf', ); }); it('should use as fallback even when filename is ISO-8859-1', function () { assert.strictEqual( create('"£ rates".pdf', { fallback: '£ rates.pdf' }), 'attachment; filename="£ rates.pdf"; filename*=UTF-8\'\'%22%C2%A3%20rates%22.pdf', ); }); it('should do nothing if equal to filename', function () { assert.strictEqual( create('plans.pdf', { fallback: 'plans.pdf' }), 'attachment; filename="plans.pdf"', ); }); it('should use the basename of a posix path', function () { assert.strictEqual( create('€ rates.pdf', { fallback: '/path/to/EURO rates.pdf', }), 'attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf', ); }); it('should use the basename of a windows path', function () { assert.strictEqual( create('€ rates.pdf', { fallback: '\\path\\to\\EURO rates.pdf', }), 'attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf', ); }); it('should use the basename of a windows path with drive letter', function () { assert.strictEqual( create('€ rates.pdf', { fallback: 'C:\\path\\to\\EURO rates.pdf', }), 'attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf', ); }); it('should do nothing without filename option', function () { assert.strictEqual( create(undefined, { fallback: 'plans.pdf' }), 'attachment', ); }); }); }); describe('with "type" option', function () { it('should default to attachment', function () { assert.strictEqual(create(), 'attachment'); }); it('should require a string', function () { assert.throws( create.bind(null, undefined, { type: 42 } as any), /invalid type/, ); }); it('should require a valid type', function () { assert.throws( create.bind(null, undefined, { type: 'invalid;type' }), /invalid type/, ); }); it('should create a header with inline type', function () { assert.strictEqual(create(undefined, { type: 'inline' }), 'inline'); }); it('should create a header with inline type & filename', function () { assert.strictEqual( create('plans.pdf', { type: 'inline' }), 'inline; filename="plans.pdf"', ); }); it('should normalize type', function () { assert.strictEqual(create(undefined, { type: 'INLINE' }), 'inline'); }); }); }); ================================================ FILE: src/index.bench.ts ================================================ import { bench, describe } from 'vitest'; import { create, parse } from './index.js'; describe('create', () => { bench('create()', () => { create(); }); bench('create(filename)', () => { create('plans.pdf'); }); }); describe('parse', () => { bench('parse(header)', () => { parse('attachment; filename="plans.pdf"'); }); bench('parse(header) with UTF-8 extended parameter', () => { parse("attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf"); }); }); ================================================ FILE: src/index.ts ================================================ /*! * content-disposition * Copyright(c) 2014-2017 Douglas Christopher Wilson * MIT Licensed */ export interface ContentDisposition { /** * Content-Disposition type, such as "attachment" or "inline" */ type: string; /** * Content-Disposition parameters, such as "filename" */ parameters: Record; } export interface CreateOptions { /** * Content-Disposition type, defaults to "attachment" * @default "attachment" */ type?: string; /** * Fallback filename for non-ISO-8859-1 strings. If true, a fallback will be generated by replacing non-latin1 characters with "?". If false, no fallback will be generated. * @default true */ fallback?: string | boolean; } /** * Null object perf optimization. Faster than `Object.create(null)` and `{ __proto__: null }`. */ const NullObject = /* @__PURE__ */ (() => { const C = function () {}; C.prototype = Object.create(null); return C; })() as unknown as { new (): any }; /** * Create an attachment Content-Disposition header. */ export function create(filename?: string, options?: CreateOptions): string { const type = options?.type || 'attachment'; const parameters = createparams(filename, options?.fallback); return format({ type, parameters }); } const SP = 32; // " " const HTAB = 9; // "\t" const SEMI = 59; // ";" const EQ = 61; // "=" const DQUOTE = 34; // '"' const BSLASH = 92; // "\\" /** * Parse Content-Disposition header string. */ export function parse(header: string): ContentDisposition { const len = header.length; let index = skipOWS(header, 0, header.length); const typeStart = index; index = parseToken(header, index, len); const typeEnd = trailingOWS(header, typeStart, index); const type = header.slice(typeStart, typeEnd).toLowerCase(); const parameters: Record = new NullObject(); parameter: while (index < len) { index = skipOWS(header, index + 1, len); // Skip over semicolon. const keyStart = index; while (index < len) { const char = header.charCodeAt(index); if (char === SEMI) continue parameter; if (char === EQ) { const keyEnd = trailingOWS(header, keyStart, index); const key = header.slice(keyStart, keyEnd).toLowerCase(); index = skipOWS(header, index + 1, len); if (index < len && header.charCodeAt(index) === DQUOTE) { index++; let value = ''; while (index < len) { const code = header.charCodeAt(index++); if (code === DQUOTE) { index = parseToken(header, index, len); if (parameters[key] === undefined) parameters[key] = value; continue parameter; } if (code === BSLASH && index < len) { value += header[index++]; continue; } value += String.fromCharCode(code); } } const valueStart = index; index = parseToken(header, index, len); const valueEnd = trailingOWS(header, valueStart, index); const value = header.slice(valueStart, valueEnd); if (key.charCodeAt(key.length - 1) === 42 /* "*" */) { const normalizedKey = key.slice(0, -1); const decoded = decodeRFC8187(value); if (decoded !== undefined) { parameters[normalizedKey] = decoded; continue parameter; } } if (parameters[key] === undefined) parameters[key] = value; continue parameter; } index++; } } return { type, parameters }; } /** * RegExp to match non attr-char, *after* encodeURIComponent (i.e. not including "%") */ const ENCODE_URL_ATTR_CHAR_REGEXP = /[\x00-\x20"'()*,/:;<=>?@[\\\]{}\x7f]/g; // eslint-disable-line no-control-regex /** * RegExp to match non-latin1 characters. */ const NON_LATIN1_REGEXP = /[^\x20-\x7e\xa0-\xff]/g; /** * RegExp to match chars that must be quoted-pair in RFC 2616 */ const QUOTE_REGEXP = /([\\"])/g; /** * RegExp for various RFC 2616 grammar * * parameter = token "=" ( token | quoted-string ) * token = 1* * separators = "(" | ")" | "<" | ">" | "@" * | "," | ";" | ":" | "\" | <"> * | "/" | "[" | "]" | "?" | "=" * | "{" | "}" | SP | HT * quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) * qdtext = > * quoted-pair = "\" CHAR * CHAR = * TEXT = * LWS = [CRLF] 1*( SP | HT ) * CRLF = CR LF * CR = * LF = * SP = * HT = * CTL = * OCTET = */ const TEXT_REGEXP = /^[\x20-\x7e\x80-\xff]+$/; const TOKEN_REGEXP = /^[!#$%&'*+.0-9A-Z^_`a-z|~-]+$/; /** * Create parameters object from filename and fallback. */ function createparams( filename?: string, fallback: string | boolean = true, ): Record | undefined { if (filename === undefined) { return; } if (typeof filename !== 'string') { throw new TypeError('filename must be a string'); } if (typeof fallback !== 'string' && typeof fallback !== 'boolean') { throw new TypeError('fallback must be a string or boolean'); } if (typeof fallback === 'string' && NON_LATIN1_REGEXP.test(fallback)) { throw new TypeError('fallback must be ISO-8859-1 string'); } const params: Record = new NullObject(); // restrict to file base name const name = basename(filename); // determine if name is suitable for quoted string const isQuotedString = TEXT_REGEXP.test(name); // generate fallback name const fallbackName = typeof fallback !== 'string' ? fallback && getlatin1(name) : basename(fallback); const hasFallback = typeof fallbackName === 'string' && fallbackName !== name; // set extended filename parameter if (hasFallback || !isQuotedString || hasHexEscape(name)) { params['filename*'] = name; } // set filename parameter if (isQuotedString || hasFallback) { params.filename = hasFallback ? fallbackName : name; } return params; } /** * Decode a RFC 8187 field value (gracefully). */ function decodeRFC8187(str: string): string | undefined { const charsetEnd = str.indexOf("'"); if (charsetEnd <= 0) { return undefined; } const languageEnd = str.indexOf("'", charsetEnd + 1); if (languageEnd === -1) { return undefined; } const charset = str.slice(0, charsetEnd).toLowerCase(); const encoded = str.slice(languageEnd + 1); switch (charset) { case 'iso-8859-1': { return decodeHexEscapes(encoded); } case 'utf-8': case 'utf8': { return tryDecodeURIComponent(encoded); } } return undefined; } /** * Decode URI component but return `undefined` on error. */ function tryDecodeURIComponent(str: string): string | undefined { try { return decodeURIComponent(str); } catch { return undefined; } } /** * Parse a token starting at the provided index. */ function parseToken(str: string, index: number, len: number): number { while (index < len) { const char = str.charCodeAt(index); if (char === SEMI) break; index++; } return index; } /** * Skip RFC 2616 linear whitespace (space / tab). */ function skipOWS(str: string, index: number, len: number): number { while (index < len) { const char = str.charCodeAt(index); if (char !== SP && char !== HTAB) break; index++; } return index; } /** * Skip RFC 2616 linear whitespace (space / tab) from the end of a string. */ function trailingOWS(str: string, start: number, end: number): number { while (end > start) { const char = str.charCodeAt(end - 1); if (char !== SP && char !== HTAB) break; end--; } return end; } /** * Format object to Content-Disposition header. */ function format(obj: Partial): string { if (!obj || typeof obj !== 'object') { throw new TypeError('argument obj is required'); } if ( !obj.type || typeof obj.type !== 'string' || !TOKEN_REGEXP.test(obj.type) ) { throw new TypeError('invalid type'); } // start with normalized type let string = obj.type.toLowerCase(); // append parameters if (obj.parameters && typeof obj.parameters === 'object') { const params = Object.keys(obj.parameters).sort(); for (let i = 0; i < params.length; i++) { const param = params[i]; const val = param.slice(-1) === '*' ? ustring(obj.parameters[param]) : qstring(obj.parameters[param]); string += `; ${param}=${val}`; } } return string; } /** * Get ISO-8859-1 version of string. */ function getlatin1(val: string): string { // simple Unicode -> ISO-8859-1 transformation return val.replace(NON_LATIN1_REGEXP, '?'); } /** * Percent encode a single character. */ function pencode(char: string): string { return '%' + char.charCodeAt(0).toString(16).toUpperCase(); } /** * Quote a string for HTTP. */ function qstring(val: unknown): string { const str = String(val); return '"' + str.replace(QUOTE_REGEXP, '\\$1') + '"'; } /** * Encode a Unicode string for HTTP (RFC 5987). */ function ustring(val: unknown): string { const str = String(val); // percent encode as UTF-8 const encoded = encodeURIComponent(str).replace( ENCODE_URL_ATTR_CHAR_REGEXP, pencode, ); return "UTF-8''" + encoded; } /** * Return the last portion of a path */ function basename(path: string): string { const normalized = path.replaceAll('\\', '/'); let end = normalized.length; while (end > 0 && normalized[end - 1] === '/') { end--; } if (end === 0) { return ''; } let start = end - 1; while (start >= 0 && normalized[start] !== '/') { start--; } return normalized.slice(start + 1, end); } /** * Check if a character is a hex digit [0-9A-Fa-f] */ function isHexDigit(char: string): boolean { const code = char.charCodeAt(0); return ( (code >= 48 && code <= 57) || // 0-9 (code >= 65 && code <= 70) || // A-F (code >= 97 && code <= 102) // a-f ); } /** * Check if a string contains percent encoding escapes. */ function hasHexEscape(str: string): boolean { const maxIndex = str.length - 3; let lastIndex = -1; while ( (lastIndex = str.indexOf('%', lastIndex + 1)) !== -1 && lastIndex <= maxIndex ) { if (isHexDigit(str[lastIndex + 1]) && isHexDigit(str[lastIndex + 2])) { return true; } } return false; } /** * Decode hex escapes in a string (e.g., %20 -> space) */ function decodeHexEscapes(str: string): string { const firstEscape = str.indexOf('%'); if (firstEscape === -1) return str; let result = str.slice(0, firstEscape); for (let idx = firstEscape; idx < str.length; idx++) { if ( str[idx] === '%' && idx + 2 < str.length && isHexDigit(str[idx + 1]) && isHexDigit(str[idx + 2]) ) { result += String.fromCharCode( Number.parseInt(str[idx + 1] + str[idx + 2], 16), ); idx += 2; } else { result += str[idx]; } } return result; } ================================================ FILE: src/parse.spec.ts ================================================ import { describe, it, assert } from 'vitest'; import { parse } from './index'; describe('parse(string)', function () { describe('with only type', function () { it('should parse quoted value leniently', function () { assert.deepEqual(parse('"attachment"'), { type: '"attachment"', parameters: {}, }); }); it('should ignore trailing semicolon', function () { assert.deepEqual(parse('attachment;'), { type: 'attachment', parameters: {}, }); }); it('should parse "attachment"', function () { assert.deepEqual(parse('attachment'), { type: 'attachment', parameters: {}, }); }); it('should parse "inline"', function () { assert.deepEqual(parse('inline'), { type: 'inline', parameters: {}, }); }); it('should parse "form-data"', function () { assert.deepEqual(parse('form-data'), { type: 'form-data', parameters: {}, }); }); it('should parse with trailing LWS', function () { assert.deepEqual(parse('attachment \t '), { type: 'attachment', parameters: {}, }); }); it('should normalize to lower-case', function () { assert.deepEqual(parse('ATTACHMENT'), { type: 'attachment', parameters: {}, }); }); }); describe('with parameters', function () { it('should ignore trailing semicolon', function () { assert.deepEqual(parse('attachment; filename="rates.pdf";'), { type: 'attachment', parameters: { filename: 'rates.pdf' }, }); }); it('should preserve invalid parameter name', function () { assert.deepEqual(parse('attachment; filename@="rates.pdf"'), { type: 'attachment', parameters: { 'filename@': 'rates.pdf' }, }); }); it('should treat missing parameter value as empty', function () { assert.deepEqual(parse('attachment; filename='), { type: 'attachment', parameters: { filename: '' }, }); }); it('should preserve invalid parameter value', function () { assert.deepEqual(parse('attachment; filename=trolly,trains'), { type: 'attachment', parameters: { filename: 'trolly,trains' }, }); }); it('should preserve otherwise invalid parameters', function () { assert.deepEqual(parse('attachment; filename=total/; foo=bar'), { type: 'attachment', parameters: { filename: 'total/', foo: 'bar' }, }); }); it('should keep the first duplicate parameter', function () { assert.deepEqual(parse('attachment; filename=foo; filename=bar'), { type: 'attachment', parameters: { filename: 'foo' }, }); }); it('should parse missing type leniently', function () { assert.deepEqual(parse('filename="plans.pdf"'), { type: 'filename="plans.pdf"', parameters: {}, }); assert.deepEqual(parse('; filename="plans.pdf"'), { type: '', parameters: { filename: 'plans.pdf' }, }); }); it('should lower-case parameter name', function () { assert.deepEqual(parse('attachment; FILENAME="plans.pdf"'), { type: 'attachment', parameters: { filename: 'plans.pdf' }, }); }); it('should parse quoted parameter value', function () { assert.deepEqual(parse('attachment; filename="plans.pdf"'), { type: 'attachment', parameters: { filename: 'plans.pdf' }, }); }); it('should parse & unescape quoted value', function () { assert.deepEqual(parse('attachment; filename="the \\"plans\\".pdf"'), { type: 'attachment', parameters: { filename: 'the "plans".pdf' }, }); }); it('should include all parameters', function () { assert.deepEqual(parse('attachment; filename="plans.pdf"; foo=bar'), { type: 'attachment', parameters: { filename: 'plans.pdf', foo: 'bar' }, }); }); it('should parse parameters separated with any LWS', function () { assert.deepEqual( parse('attachment;filename="plans.pdf" \t; \t\t foo=bar'), { type: 'attachment', parameters: { filename: 'plans.pdf', foo: 'bar' }, }, ); }); it('should parse token filename', function () { assert.deepEqual(parse('attachment; filename=plans.pdf'), { type: 'attachment', parameters: { filename: 'plans.pdf' }, }); }); it('should parse ISO-8859-1 filename', function () { assert.deepEqual(parse('attachment; filename="£ rates.pdf"'), { type: 'attachment', parameters: { filename: '£ rates.pdf' }, }); }); }); describe('with extended parameters', function () { it('should preserve quoted extended parameter value', function () { assert.deepEqual( parse('attachment; filename*="UTF-8\'\'%E2%82%AC%20rates.pdf"'), { type: 'attachment', parameters: { 'filename*': "UTF-8''%E2%82%AC%20rates.pdf" }, }, ); }); it('should parse UTF-8 extended parameter value', function () { assert.deepEqual( parse("attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf"), { type: 'attachment', parameters: { filename: '€ rates.pdf' }, }, ); }); it('should parse UTF8 extended parameter value', function () { assert.deepEqual( parse("attachment; filename*=utf8''%E2%82%AC%20rates.pdf"), { type: 'attachment', parameters: { filename: '€ rates.pdf' }, }, ); }); it('should parse UTF-8 extended parameter value', function () { assert.deepEqual( parse("attachment; filename*=UTF-8''%E2%82%AC%20rates.pdf"), { type: 'attachment', parameters: { filename: '€ rates.pdf' }, }, ); }); it('should ignore invalid percent-encodings in UTF-8 extended parameter value', function () { assert.deepEqual(parse("attachment; filename*=UTF-8''%E4%20rates.pdf"), { type: 'attachment', parameters: { 'filename*': "UTF-8''%E4%20rates.pdf" }, }); }); it('should parse ISO-8859-1 extended parameter value', function () { assert.deepEqual( parse("attachment; filename*=ISO-8859-1''%A3%20rates.pdf"), { type: 'attachment', parameters: { filename: '£ rates.pdf' }, }, ); }); it('should retain invalid latin1 bytes in ISO-8859-1 extended parameter value', function () { assert.deepEqual( parse("attachment; filename*=ISO-8859-1''%82%20rates.pdf"), { type: 'attachment', parameters: { filename: '\x82 rates.pdf' }, }, ); }); it('should not be case-sensitive for charset', function () { assert.deepEqual( parse("attachment; filename*=utf-8''%E2%82%AC%20rates.pdf"), { type: 'attachment', parameters: { filename: '€ rates.pdf' }, }, ); }); it('should preserve unsupported charset as the original parameter', function () { assert.deepEqual( parse("attachment; filename*=ISO-8859-2''%A4%20rates.pdf"), { type: 'attachment', parameters: { 'filename*': "ISO-8859-2''%A4%20rates.pdf" }, }, ); }); it('should parse with embedded language', function () { assert.deepEqual( parse("attachment; filename*=UTF-8'en'%E2%82%AC%20rates.pdf"), { type: 'attachment', parameters: { filename: '€ rates.pdf' }, }, ); }); it('should prefer extended parameter value', function () { assert.deepEqual( parse( 'attachment; filename="EURO rates.pdf"; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf', ), { type: 'attachment', parameters: { filename: '€ rates.pdf' }, }, ); assert.deepEqual( parse( 'attachment; filename*=UTF-8\'\'%E2%82%AC%20rates.pdf; filename="EURO rates.pdf"', ), { type: 'attachment', parameters: { filename: '€ rates.pdf' }, }, ); }); it('should keep fallback filename when extended parameter cannot be decoded', function () { assert.deepEqual( parse( 'attachment; filename="EURO rates.pdf"; filename*=ISO-8859-2\'\'%A4%20rates.pdf', ), { type: 'attachment', parameters: { filename: 'EURO rates.pdf', 'filename*': "ISO-8859-2''%A4%20rates.pdf", }, }, ); }); }); describe('from TC 2231', function () { describe('Disposition-Type Inline', function () { it('should parse "inline"', function () { assert.deepEqual(parse('inline'), { type: 'inline', parameters: {}, }); }); it('should parse ""inline"" leniently', function () { assert.deepEqual(parse('"inline"'), { type: '"inline"', parameters: {}, }); }); it('should parse "inline; filename="foo.html""', function () { assert.deepEqual(parse('inline; filename="foo.html"'), { type: 'inline', parameters: { filename: 'foo.html' }, }); }); it('should parse "inline; filename="Not an attachment!""', function () { assert.deepEqual(parse('inline; filename="Not an attachment!"'), { type: 'inline', parameters: { filename: 'Not an attachment!' }, }); }); it('should parse "inline; filename="foo.pdf""', function () { assert.deepEqual(parse('inline; filename="foo.pdf"'), { type: 'inline', parameters: { filename: 'foo.pdf' }, }); }); }); describe('Disposition-Type Attachment', function () { it('should parse "attachment"', function () { assert.deepEqual(parse('attachment'), { type: 'attachment', parameters: {}, }); }); it('should parse ""attachment"" leniently', function () { assert.deepEqual(parse('"attachment"'), { type: '"attachment"', parameters: {}, }); }); it('should parse "ATTACHMENT"', function () { assert.deepEqual(parse('ATTACHMENT'), { type: 'attachment', parameters: {}, }); }); it('should parse "attachment; filename="foo.html""', function () { assert.deepEqual(parse('attachment; filename="foo.html"'), { type: 'attachment', parameters: { filename: 'foo.html' }, }); }); it('should parse "attachment; filename="0000000000111111111122222""', function () { assert.deepEqual( parse('attachment; filename="0000000000111111111122222"'), { type: 'attachment', parameters: { filename: '0000000000111111111122222' }, }, ); }); it('should parse "attachment; filename="00000000001111111111222222222233333""', function () { assert.deepEqual( parse('attachment; filename="00000000001111111111222222222233333"'), { type: 'attachment', parameters: { filename: '00000000001111111111222222222233333' }, }, ); }); it('should parse "attachment; filename="f\\oo.html""', function () { assert.deepEqual(parse('attachment; filename="f\\oo.html"'), { type: 'attachment', parameters: { filename: 'foo.html' }, }); }); it('should parse "attachment; filename="\\"quoting\\" tested.html""', function () { assert.deepEqual( parse('attachment; filename="\\"quoting\\" tested.html"'), { type: 'attachment', parameters: { filename: '"quoting" tested.html' }, }, ); }); it('should parse "attachment; filename="Here\'s a semicolon;.html""', function () { assert.deepEqual( parse('attachment; filename="Here\'s a semicolon;.html"'), { type: 'attachment', parameters: { filename: "Here's a semicolon;.html" }, }, ); }); it('should parse "attachment; foo="bar"; filename="foo.html""', function () { assert.deepEqual(parse('attachment; foo="bar"; filename="foo.html"'), { type: 'attachment', parameters: { filename: 'foo.html', foo: 'bar' }, }); }); it('should parse "attachment; foo="\\"\\\\";filename="foo.html""', function () { assert.deepEqual( parse('attachment; foo="\\"\\\\";filename="foo.html"'), { type: 'attachment', parameters: { filename: 'foo.html', foo: '"\\' }, }, ); }); it('should parse "attachment; FILENAME="foo.html""', function () { assert.deepEqual(parse('attachment; FILENAME="foo.html"'), { type: 'attachment', parameters: { filename: 'foo.html' }, }); }); it('should parse "attachment; filename=foo.html"', function () { assert.deepEqual(parse('attachment; filename=foo.html'), { type: 'attachment', parameters: { filename: 'foo.html' }, }); }); it('should preserve commas in token values', function () { assert.deepEqual(parse('attachment; filename=foo,bar.html'), { type: 'attachment', parameters: { filename: 'foo,bar.html' }, }); }); it('should ignore trailing semicolon after value', function () { assert.deepEqual(parse('attachment; filename=foo.html ;'), { type: 'attachment', parameters: { filename: 'foo.html' }, }); }); it('should skip empty parameter slots', function () { assert.deepEqual(parse('attachment; ;filename=foo'), { type: 'attachment', parameters: { filename: 'foo' }, }); }); it('should preserve spaces in token values', function () { assert.deepEqual(parse('attachment; filename=foo bar.html'), { type: 'attachment', parameters: { filename: 'foo bar.html' }, }); }); it("should parse \"attachment; filename='foo.bar'", function () { assert.deepEqual(parse("attachment; filename='foo.bar'"), { type: 'attachment', parameters: { filename: "'foo.bar'" }, }); }); it('should parse "attachment; filename="foo-ä.html""', function () { assert.deepEqual(parse('attachment; filename="foo-ä.html"'), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }); }); it('should parse "attachment; filename="foo-ä.html""', function () { assert.deepEqual(parse('attachment; filename="foo-ä.html"'), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }); }); it('should parse "attachment; filename="foo-%41.html""', function () { assert.deepEqual(parse('attachment; filename="foo-%41.html"'), { type: 'attachment', parameters: { filename: 'foo-%41.html' }, }); }); it('should parse "attachment; filename="50%.html""', function () { assert.deepEqual(parse('attachment; filename="50%.html"'), { type: 'attachment', parameters: { filename: '50%.html' }, }); }); it('should parse "attachment; filename="foo-%\\41.html""', function () { assert.deepEqual(parse('attachment; filename="foo-%\\41.html"'), { type: 'attachment', parameters: { filename: 'foo-%41.html' }, }); }); it('should parse "attachment; name="foo-%41.html""', function () { assert.deepEqual(parse('attachment; name="foo-%41.html"'), { type: 'attachment', parameters: { name: 'foo-%41.html' }, }); }); it('should parse "attachment; filename="ä-%41.html""', function () { assert.deepEqual(parse('attachment; filename="ä-%41.html"'), { type: 'attachment', parameters: { filename: 'ä-%41.html' }, }); }); it('should parse "attachment; filename="foo-%c3%a4-%e2%82%ac.html""', function () { assert.deepEqual( parse('attachment; filename="foo-%c3%a4-%e2%82%ac.html"'), { type: 'attachment', parameters: { filename: 'foo-%c3%a4-%e2%82%ac.html' }, }, ); }); it('should parse "attachment; filename ="foo.html""', function () { assert.deepEqual(parse('attachment; filename ="foo.html"'), { type: 'attachment', parameters: { filename: 'foo.html' }, }); }); it('should keep the first duplicate quoted filename', function () { assert.deepEqual( parse('attachment; filename="foo.html"; filename="bar.html"'), { type: 'attachment', parameters: { filename: 'foo.html' }, }, ); }); it('should preserve bracket characters in token values', function () { assert.deepEqual(parse('attachment; filename=foo[1](2).html'), { type: 'attachment', parameters: { filename: 'foo[1](2).html' }, }); }); it('should preserve latin1 token values', function () { assert.deepEqual(parse('attachment; filename=foo-ä.html'), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }); }); it('should preserve mojibake token values', function () { assert.deepEqual(parse('attachment; filename=foo-ä.html'), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }); }); it('should treat a bare parameter as the type', function () { assert.deepEqual(parse('filename=foo.html'), { type: 'filename=foo.html', parameters: {}, }); }); it('should preserve invalid type token with parameters', function () { assert.deepEqual(parse('x=y; filename=foo.html'), { type: 'x=y', parameters: { filename: 'foo.html' }, }); }); it('should stop at the first recoverable parameter after a quoted type', function () { assert.deepEqual(parse('"foo; filename=bar;baz"; filename=qux'), { type: '"foo', parameters: { filename: 'bar' }, }); }); it('should preserve commas in a malformed type token', function () { assert.deepEqual(parse('filename=foo.html, filename=bar.html'), { type: 'filename=foo.html, filename=bar.html', parameters: {}, }); }); it('should allow an empty type when parameters follow', function () { assert.deepEqual(parse('; filename=foo.html'), { type: '', parameters: { filename: 'foo.html' }, }); }); it('should preserve leading punctuation in the type', function () { assert.deepEqual(parse(': inline; attachment; filename=foo.html'), { type: ': inline', parameters: { filename: 'foo.html' }, }); }); it('should skip bare parameters without values', function () { assert.deepEqual(parse('inline; attachment; filename=foo.html'), { type: 'inline', parameters: { filename: 'foo.html' }, }); }); it('should skip bare attachment parameters without values', function () { assert.deepEqual(parse('attachment; inline; filename=foo.html'), { type: 'attachment', parameters: { filename: 'foo.html' }, }); }); it('should ignore a suffix after a quoted filename', function () { assert.deepEqual(parse('attachment; filename="foo.html".txt'), { type: 'attachment', parameters: { filename: 'foo.html' }, }); }); it('should treat an unterminated quoted filename as empty', function () { assert.deepEqual(parse('attachment; filename="bar'), { type: 'attachment', parameters: { filename: '' }, }); }); it('should stop a token value at the next semicolon', function () { assert.deepEqual(parse('attachment; filename=foo"bar;baz"qux'), { type: 'attachment', parameters: { filename: 'foo"bar' }, }); }); it('should preserve a comma-separated header fragment in the first value', function () { assert.deepEqual( parse('attachment; filename=foo.html, attachment; filename=bar.html'), { type: 'attachment', parameters: { filename: 'foo.html, attachment' }, }, ); }); it('should keep an unseparated parameter assignment inside the value', function () { assert.deepEqual(parse('attachment; foo=foo filename=bar'), { type: 'attachment', parameters: { foo: 'foo filename=bar' }, }); }); it('should keep trailing assignments inside the filename value', function () { assert.deepEqual(parse('attachment; filename=bar foo=foo'), { type: 'attachment', parameters: { filename: 'bar foo=foo' }, }); }); it('should treat missing semicolon after the type as part of the type', function () { assert.deepEqual(parse('attachment filename=bar'), { type: 'attachment filename=bar', parameters: {}, }); }); it('should keep the first malformed type segment', function () { assert.deepEqual(parse('filename=foo.html; attachment'), { type: 'filename=foo.html', parameters: {}, }); }); it('should parse "attachment; xfilename=foo.html"', function () { assert.deepEqual(parse('attachment; xfilename=foo.html'), { type: 'attachment', parameters: { xfilename: 'foo.html' }, }); }); it('should parse "attachment; filename="/foo.html""', function () { assert.deepEqual(parse('attachment; filename="/foo.html"'), { type: 'attachment', parameters: { filename: '/foo.html' }, }); }); it('should parse "attachment; filename="\\\\foo.html""', function () { assert.deepEqual(parse('attachment; filename="\\\\foo.html"'), { type: 'attachment', parameters: { filename: '\\foo.html' }, }); }); }); describe('Additional Parameters', function () { it('should parse "attachment; creation-date="Wed, 12 Feb 1997 16:29:51 -0500""', function () { assert.deepEqual( parse('attachment; creation-date="Wed, 12 Feb 1997 16:29:51 -0500"'), { type: 'attachment', parameters: { 'creation-date': 'Wed, 12 Feb 1997 16:29:51 -0500' }, }, ); }); it('should parse "attachment; modification-date="Wed, 12 Feb 1997 16:29:51 -0500""', function () { assert.deepEqual( parse( 'attachment; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"', ), { type: 'attachment', parameters: { 'modification-date': 'Wed, 12 Feb 1997 16:29:51 -0500', }, }, ); }); }); describe('Disposition-Type Extension', function () { it('should parse "foobar"', function () { assert.deepEqual(parse('foobar'), { type: 'foobar', parameters: {}, }); }); it('should parse "attachment; example="filename=example.txt""', function () { assert.deepEqual(parse('attachment; example="filename=example.txt"'), { type: 'attachment', parameters: { example: 'filename=example.txt' }, }); }); }); describe('RFC 2231/5987 Encoding: Character Sets', function () { it('should parse "attachment; filename*=iso-8859-1\'\'foo-%E4.html"', function () { assert.deepEqual( parse("attachment; filename*=iso-8859-1''foo-%E4.html"), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }, ); }); it('should parse "attachment; filename*=UTF-8\'\'foo-%c3%a4-%e2%82%ac.html"', function () { assert.deepEqual( parse("attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html"), { type: 'attachment', parameters: { filename: 'foo-ä-€.html' }, }, ); }); it('should preserve extended values without a charset', function () { assert.deepEqual( parse("attachment; filename*=''foo-%c3%a4-%e2%82%ac.html"), { type: 'attachment', parameters: { 'filename*': "''foo-%c3%a4-%e2%82%ac.html" }, }, ); }); it('should parse "attachment; filename*=UTF-8\'\'foo-a%cc%88.html"', function () { assert.deepEqual( parse("attachment; filename*=UTF-8''foo-a%cc%88.html"), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }, ); }); it('should parse iso-8859-1 extended parameter value with invalid bytes', function () { assert.deepEqual( parse("attachment; filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html"), { type: 'attachment', parameters: { filename: 'foo-ä-â\x82¬.html' }, }, ); }); it('should preserve spaces before the star in the parameter name', function () { assert.deepEqual( parse("attachment; filename *=UTF-8''foo-%c3%a4.html"), { type: 'attachment', parameters: { 'filename ': 'foo-ä.html' }, }, ); }); it('should parse "attachment; filename*= UTF-8\'\'foo-%c3%a4.html"', function () { assert.deepEqual( parse("attachment; filename*= UTF-8''foo-%c3%a4.html"), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }, ); }); it('should parse "attachment; filename* =UTF-8\'\'foo-%c3%a4.html"', function () { assert.deepEqual( parse("attachment; filename* =UTF-8''foo-%c3%a4.html"), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }, ); }); it('should preserve quoted UTF-8 extended values verbatim', function () { assert.deepEqual( parse('attachment; filename*="UTF-8\'\'foo-%c3%a4.html"'), { type: 'attachment', parameters: { 'filename*': "UTF-8''foo-%c3%a4.html" }, }, ); }); it('should preserve quoted extended values without charset verbatim', function () { assert.deepEqual(parse('attachment; filename*="foo%20bar.html"'), { type: 'attachment', parameters: { 'filename*': 'foo%20bar.html' }, }); }); it('should preserve extended values without both apostrophes', function () { assert.deepEqual(parse("attachment; filename*=UTF-8'foo-%c3%a4.html"), { type: 'attachment', parameters: { 'filename*': "UTF-8'foo-%c3%a4.html" }, }); }); it('should ignore malformed trailing percent escapes', function () { assert.deepEqual(parse("attachment; filename*=UTF-8''foo%"), { type: 'attachment', parameters: { 'filename*': "UTF-8''foo%" }, }); }); it('should ignore malformed percent escapes inside the value', function () { assert.deepEqual(parse("attachment; filename*=UTF-8''f%oo.html"), { type: 'attachment', parameters: { 'filename*': "UTF-8''f%oo.html" }, }); }); it('should parse "attachment; filename*=UTF-8\'\'A-%2541.html"', function () { assert.deepEqual(parse("attachment; filename*=UTF-8''A-%2541.html"), { type: 'attachment', parameters: { filename: 'A-%41.html' }, }); }); it('should parse "attachment; filename*=UTF-8\'\'%5cfoo.html"', function () { assert.deepEqual(parse("attachment; filename*=UTF-8''%5cfoo.html"), { type: 'attachment', parameters: { filename: '\\foo.html' }, }); }); }); describe('RFC2231 Encoding: Continuations', function () { it('should parse "attachment; filename*0="foo."; filename*1="html""', function () { assert.deepEqual( parse('attachment; filename*0="foo."; filename*1="html"'), { type: 'attachment', parameters: { 'filename*0': 'foo.', 'filename*1': 'html' }, }, ); }); it('should parse "attachment; filename*0="foo"; filename*1="\\b\\a\\r.html""', function () { assert.deepEqual( parse('attachment; filename*0="foo"; filename*1="\\b\\a\\r.html"'), { type: 'attachment', parameters: { 'filename*0': 'foo', 'filename*1': 'bar.html' }, }, ); }); it('should parse "attachment; filename*0*=UTF-8\'\'foo-%c3%a4; filename*1=".html""', function () { assert.deepEqual( parse( 'attachment; filename*0*=UTF-8\'\'foo-%c3%a4; filename*1=".html"', ), { type: 'attachment', parameters: { 'filename*0': 'foo-ä', 'filename*1': '.html', }, }, ); }); it('should parse "attachment; filename*0="foo"; filename*01="bar""', function () { assert.deepEqual( parse('attachment; filename*0="foo"; filename*01="bar"'), { type: 'attachment', parameters: { 'filename*0': 'foo', 'filename*01': 'bar' }, }, ); }); it('should parse "attachment; filename*0="foo"; filename*2="bar""', function () { assert.deepEqual( parse('attachment; filename*0="foo"; filename*2="bar"'), { type: 'attachment', parameters: { 'filename*0': 'foo', 'filename*2': 'bar' }, }, ); }); it('should parse "attachment; filename*1="foo."; filename*2="html""', function () { assert.deepEqual( parse('attachment; filename*1="foo."; filename*2="html"'), { type: 'attachment', parameters: { 'filename*1': 'foo.', 'filename*2': 'html' }, }, ); }); it('should parse "attachment; filename*1="bar"; filename*0="foo""', function () { assert.deepEqual( parse('attachment; filename*1="bar"; filename*0="foo"'), { type: 'attachment', parameters: { 'filename*1': 'bar', 'filename*0': 'foo' }, }, ); }); }); describe('RFC2231 Encoding: Fallback Behaviour', function () { it('should parse "attachment; filename="foo-ae.html"; filename*=UTF-8\'\'foo-%c3%a4.html"', function () { assert.deepEqual( parse( 'attachment; filename="foo-ae.html"; filename*=UTF-8\'\'foo-%c3%a4.html', ), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }, ); }); it('should parse "attachment; filename*=UTF-8\'\'foo-%c3%a4.html; filename="foo-ae.html"', function () { assert.deepEqual( parse( 'attachment; filename*=UTF-8\'\'foo-%c3%a4.html; filename="foo-ae.html"', ), { type: 'attachment', parameters: { filename: 'foo-ä.html' }, }, ); }); it("should parse \"attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4", function () { assert.deepEqual( parse( "attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4; filename*=ISO-8859-1''currency-sign%3d%a4", ), { type: 'attachment', parameters: { filename: 'currency-sign=¤', 'filename*0*': "ISO-8859-15''euro-sign%3d%a4", }, }, ); }); it('should parse "attachment; foobar=x; filename="foo.html"', function () { assert.deepEqual(parse('attachment; foobar=x; filename="foo.html"'), { type: 'attachment', parameters: { filename: 'foo.html', foobar: 'x' }, }); }); }); describe('RFC2047 Encoding', function () { it('should preserve RFC2047-looking token values', function () { assert.deepEqual( parse('attachment; filename==?ISO-8859-1?Q?foo-=E4.html?='), { type: 'attachment', parameters: { filename: '=?ISO-8859-1?Q?foo-=E4.html?=' }, }, ); }); it('should parse "attachment; filename="=?ISO-8859-1?Q?foo-=E4.html?=""', function () { assert.deepEqual( parse('attachment; filename="=?ISO-8859-1?Q?foo-=E4.html?="'), { type: 'attachment', parameters: { filename: '=?ISO-8859-1?Q?foo-=E4.html?=' }, }, ); }); }); }); }); ================================================ FILE: tsconfig.build.json ================================================ { "extends": "./tsconfig.json", "compilerOptions": {}, "exclude": ["src/**/*.spec.ts", "src/**/*.bench.ts"] } ================================================ FILE: tsconfig.json ================================================ { "extends": "@borderless/ts-scripts/configs/tsconfig.json", "compilerOptions": { "target": "ES2022", "lib": ["ES2023"], "rootDir": "src", "outDir": "dist", "module": "nodenext", "moduleResolution": "nodenext", "types": [] }, "include": ["src/**/*"] }