@d1g1tal/media-type 5.0.2 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,153 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [6.0.0] - 2024-06-15
9
+
10
+ ### Changed
11
+ - **Complete TypeScript conversion**: Converted all source and test files from JavaScript to TypeScript
12
+ - **Test framework**: Migrated from Jest to Vitest with coverage support
13
+ - Updated ESLint to modern flat config format
14
+ - Enhanced README with badges, features, and migration guide
15
+ - Updated package.json with proper TypeScript exports
16
+
17
+ ### Added
18
+ - Native TypeScript type definitions with strict compiler options
19
+ - TypeScript interfaces: `MediaTypeComponent`, `ParsedMediaType`
20
+ - tsconfig.json with isolated declarations
21
+ - Vitest configuration with UI and coverage (`@vitest/coverage-v8`, `@vitest/ui`)
22
+ - `@types/node` dev dependency
23
+ - Comprehensive CHANGELOG
24
+ - Automated version script for releases
25
+
26
+ ## [5.0.2] - 2023-XX-XX
27
+
28
+ ### Fixed
29
+ - Maintenance release with dependency updates
30
+
31
+ ## [5.0.0] - 2023-11-11
32
+
33
+ ### Changed
34
+ - Switched to pnpm for package management
35
+ - Switched license from MIT to ISC
36
+ - Refactored `MediaTypeParameters` to extend Map directly (instead of wrapping)
37
+ - Refactored parser to use class with static methods
38
+ - Moved serializer functionality into `MediaType.toString()`
39
+ - Changed package manager from npm/yarn to pnpm
40
+ - Removed `esbuild-library` dependency in favor of simpler TypeScript compilation
41
+
42
+ #### API Changes - BREAKING CHANGES
43
+
44
+ ##### Renamed Classes
45
+ - **`MIMEType` → `MediaType`**: Main class renamed to reflect WHATWG terminology
46
+ - **`MIMETypeParameters` → `MediaTypeParameters`**: Parameters class renamed accordingly
47
+
48
+ ##### Removed Methods
49
+ The following methods have been **removed** from the `MediaType` class:
50
+ - `isHTML()` - Removed (was marked as speculative in original package)
51
+ - `isXML()` - Removed (was marked as speculative in original package)
52
+ - `isJavaScript({ prohibitParameters })` - Removed (was marked as speculative in original package)
53
+
54
+ **Migration Guide**: If you were using these methods, you'll need to implement your own checks:
55
+
56
+ ```typescript
57
+ // Old (v4.x with whatwg-mimetype)
58
+ if (mimeType.isHTML()) { /* ... */ }
59
+
60
+ // New (v5.x with @d1g1tal/media-type)
61
+ if (mediaType.essence === 'text/html') { /* ... */ }
62
+ // or for more comprehensive HTML MIME type checking:
63
+ const htmlTypes = ['text/html', 'application/xhtml+xml'];
64
+ if (htmlTypes.includes(mediaType.essence)) { /* ... */ }
65
+ ```
66
+
67
+ ##### New Methods
68
+ - **`matches(mediaType: MediaType | string): boolean`**: New method to check if media type matches a specified type
69
+ - Accepts either a `MediaType` instance or a string
70
+ - For strings: checks if the essence includes the string (e.g., `mediaType.matches('html')`)
71
+ - For `MediaType` instances: performs exact type/subtype comparison
72
+
73
+ ##### Constructor Changes
74
+ - **Constructor signature change**: Now accepts optional second parameter for overriding parameters
75
+ ```typescript
76
+ // New in v5.0
77
+ const mediaType = new MediaType('text/html;charset=iso-8859-1', { charset: 'utf-8' });
78
+ // Results in: text/html;charset=utf-8
79
+ ```
80
+ - **Removed object-only constructor**: Can no longer pass just an object to constructor
81
+ - Old: `new MIMEType({ type: 'text', subtype: 'html' })` ❌
82
+ - New: Must pass string as first parameter ✅
83
+
84
+ ##### Internal Architecture Changes
85
+ - **`MediaTypeParameters` now extends `Map`**: Previously wrapped a Map, now directly extends it
86
+ - Reduces file size and complexity
87
+ - All Map methods directly available
88
+ - Still maintains media type-specific validation (case-insensitive parameter names, HTTP token validation)
89
+
90
+ - **Parser refactored to class**: Module with exported functions converted to `MediaTypeParser` class with static methods
91
+ - **Serializer merged**: Separate serializer module removed, `serialize` functionality moved into `MediaType.toString()`
92
+ - **Utility optimizations**: Util functions moved directly into classes where used only once
93
+
94
+ #### Module System Changes
95
+ - Pure ES Modules (ESM) - no CommonJS support
96
+ - Updated exports structure in package.json
97
+ - Import extensions required: `.js` extensions must be used in TypeScript imports
98
+
99
+ #### Properties Made Private
100
+ - Internal `_type`, `_subtype`, and `_parameters` properties are now properly private
101
+ - Access only through public getters
102
+ - `type` and `subtype` are now read-only (getter-only)
103
+
104
+ #### Development Experience
105
+ - Added comprehensive ESLint configuration with TypeScript support
106
+ - Added `eslint-plugin-jsdoc` for JSDoc validation
107
+ - Improved test coverage with Vitest
108
+ - Added coverage reporting with `@vitest/coverage-v8`
109
+ - Web Platform Tests integration maintained and updated
110
+
111
+ #### License Change
112
+ - **License changed from MIT to ISC**
113
+ - Maintainer changed to Jason DiMeo
114
+
115
+ ### Added
116
+ - `matches()` method for media type comparison
117
+ - Second constructor parameter for parameter overrides
118
+ - Full TypeScript type definitions
119
+ - Vitest UI support for interactive testing
120
+
121
+ ### Changed
122
+ - Package name: `whatwg-mimetype` → `@d1g1tal/media-type`
123
+ - License: MIT → ISC
124
+ - Build system: esbuild-library → tsbuild
125
+ - Test framework: Jest → Vitest
126
+ - Package manager: npm/yarn → pnpm
127
+ - Module system: CommonJS → Pure ESM
128
+
129
+ ### Removed
130
+ - `isHTML()` method
131
+ - `isXML()` method
132
+ - `isJavaScript()` method
133
+ - Object-only constructor signature
134
+ - CommonJS support
135
+ - Separate serializer module
136
+ - `esbuild-library` dependency
137
+ - `minipass-fetch` dev dependency (replaced with built-in fetch API)
138
+ - `rimraf` dev dependency (tsbuild handles cleaning)
139
+
140
+ ## [4.x.x] - Previous versions
141
+
142
+ See the original [whatwg-mimetype](https://github.com/jsdom/whatwg-mimetype) repository for version 4.x.x and earlier history.
143
+
144
+ ---
145
+
146
+ ## Original Package Information
147
+
148
+ This package is a fork of [`whatwg-mimetype`](https://github.com/jsdom/whatwg-mimetype) by Domenic Denicola and contributors. The original package was licensed under MIT. This fork has been substantially rewritten with breaking changes and is licensed under ISC.
149
+
150
+ **Original Author**: Domenic Denicola <d@domenic.me> (https://domenic.me/)
151
+ **Current Maintainer**: Jason DiMeo <jason.dimeo@gmail.com>
152
+
153
+ The original package contributors and their excellent work are acknowledged with gratitude.
package/README.md CHANGED
@@ -1,11 +1,41 @@
1
1
  # Parse, serialize, and manipulate media types
2
2
 
3
- This package will parse [MIME types](https://mimesniff.spec.whatwg.org/#understanding-mime-types) into a structured format, which can then be manipulated and serialized:
3
+ [![npm version](https://img.shields.io/npm/v/@d1g1tal/media-type.svg)](https://www.npmjs.com/package/@d1g1tal/media-type)
4
+ [![License: ISC](https://img.shields.io/badge/License-ISC-blue.svg)](https://opensource.org/licenses/ISC)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue.svg)](https://www.typescriptlang.org/)
6
+ [![Node Version](https://img.shields.io/node/v/@d1g1tal/media-type.svg)](https://nodejs.org)
4
7
 
5
- This version is using ES Modules instead of commonJS.
8
+ > **Note**: This is a TypeScript rewrite and fork of the original [`whatwg-mimetype`](https://github.com/jsdom/whatwg-mimetype) package. See [CHANGELOG.md](./CHANGELOG.md) for migration information.
9
+
10
+ This package will parse [MIME types](https://mimesniff.spec.whatwg.org/#understanding-mime-types) (also known as media types) into a structured format, which can then be manipulated and serialized according to the WHATWG MIME Sniffing Standard.
11
+
12
+ ## Features
13
+
14
+ - ✅ **WHATWG Standard Compliant**: Implements the [WHATWG MIME Sniffing Standard](https://mimesniff.spec.whatwg.org/)
15
+ - ✅ **Full TypeScript Support**: Written in TypeScript with complete type definitions
16
+ - ✅ **Pure ES Modules**: Modern ESM-only package
17
+ - ✅ **Zero Dependencies**: No runtime dependencies
18
+ - ✅ **Extensive Testing**: Includes WHATWG web platform tests
19
+ - ✅ **Lightweight**: Minimal bundle size
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install @d1g1tal/media-type
25
+ ```
26
+
27
+ ```bash
28
+ pnpm add @d1g1tal/media-type
29
+ ```
30
+
31
+ ```bash
32
+ yarn add @d1g1tal/media-type
33
+ ```
34
+
35
+ ## Usage
6
36
 
7
37
  ```js
8
- import MediaType from '@d1g1tal/media-type';
38
+ import { MediaType } from '@d1g1tal/media-type';
9
39
 
10
40
  const mediaType = new MediaType('Text/HTML;Charset="utf-8"');
11
41
 
@@ -28,6 +58,31 @@ Parsing is a fairly complex process; see [the specification](https://mimesniff.s
28
58
 
29
59
  This package's algorithms conform to those of the WHATWG [MIME Sniffing Standard](https://mimesniff.spec.whatwg.org/), and is aligned up to commit [8e9a7dd](https://github.com/whatwg/mimesniff/commit/8e9a7dd90717c595a4e4d982cd216e4411d33736).
30
60
 
61
+ ## Migration from whatwg-mimetype
62
+
63
+ If you're migrating from the original `whatwg-mimetype` package, please review the [CHANGELOG.md](./CHANGELOG.md) for breaking changes. Key changes include:
64
+
65
+ - **Class renamed**: `MIMEType` → `MediaType`
66
+ - **Removed methods**: `isHTML()`, `isXML()`, `isJavaScript()` - implement your own checks using the `essence` property
67
+ - **New method**: `matches(mediaType)` for flexible type matching
68
+ - **Properties now read-only**: `type` and `subtype` can only be read via getters
69
+ - **Constructor change**: Optional second parameter to override parsed parameters
70
+ - **Pure ESM**: No CommonJS support
71
+
72
+ ### Quick Migration Example
73
+
74
+ ```typescript
75
+ // Old (whatwg-mimetype v4.x)
76
+ import MIMEType from 'whatwg-mimetype';
77
+ const mimeType = new MIMEType('text/html');
78
+ if (mimeType.isHTML()) { /* ... */ }
79
+
80
+ // New (@d1g1tal/media-type v5.x)
81
+ import { MediaType } from '@d1g1tal/media-type';
82
+ const mediaType = new MediaType('text/html');
83
+ if (mediaType.essence === 'text/html' || mediaType.matches('html')) { /* ... */ }
84
+ ```
85
+
31
86
  ## `MediaType` API
32
87
 
33
88
  This package's main module's export is a class, `MediaType`. Its constructor takes a string which it will attempt to parse into a media type; if parsing fails, an `Error` will be thrown.
@@ -51,8 +106,10 @@ As an alternative to the constructor, you can use `MediaType.parse(string)`. The
51
106
 
52
107
  ### Methods
53
108
 
54
- - `toString()` serializes the media type to a string
55
- - `matches(MediaType|string)`: Checks if the media type matches the specified type.
109
+ - **`toString()`**: Serializes the media type to a string
110
+ - **`matches(mediaType: MediaType | string)`**: Checks if the media type matches the specified type
111
+ - When passed a string: checks if the essence includes that string (e.g., `mediaType.matches('html')` returns `true` for `text/html`)
112
+ - When passed a `MediaType` instance: performs exact type/subtype comparison
56
113
 
57
114
  ## `MediaTypeParameters` API
58
115
 
@@ -83,4 +140,88 @@ console.assert(mediaType.toString() === 'x/x;a=b;c=d;e=F;q=X');
83
140
 
84
141
  // Throws:
85
142
  mediaType.parameters.set('@', 'x');
86
- ```
143
+ ```
144
+
145
+ ## Additional Exports
146
+
147
+ In addition to the main `MediaType` class, this package also exports:
148
+
149
+ - **`MediaTypeParameters`**: The parameters class (extends `Map<string, string>`)
150
+ - **`MediaTypeParser`**: Low-level parser with static methods
151
+ - **Type exports**: `MediaTypeComponent`, `ParsedMediaType` (TypeScript types)
152
+
153
+ ```typescript
154
+ import {
155
+ MediaType,
156
+ MediaTypeParameters,
157
+ MediaTypeParser,
158
+ type MediaTypeComponent,
159
+ type ParsedMediaType
160
+ } from '@d1g1tal/media-type';
161
+ ```
162
+
163
+ ## Browser Support
164
+
165
+ This package uses modern ES features and is designed for:
166
+ - Modern browsers with ES6 module support
167
+ - Maintained Node.js versions (see `browserslist` in package.json)
168
+
169
+ ## Development
170
+
171
+ ```bash
172
+ # Install dependencies
173
+ pnpm install
174
+
175
+ # Run tests
176
+ pnpm test
177
+
178
+ # Run tests with coverage
179
+ pnpm test:coverage
180
+
181
+ # Run tests in watch mode
182
+ pnpm test:watch
183
+
184
+ # Run tests with UI
185
+ pnpm test:ui
186
+
187
+ # Build the package
188
+ pnpm build
189
+
190
+ # Lint
191
+ pnpm lint
192
+ ```
193
+
194
+ ## About This Fork
195
+
196
+ This project is a fork and substantial rewrite of the excellent [`whatwg-mimetype`](https://github.com/jsdom/whatwg-mimetype) package by Domenic Denicola and contributors.
197
+
198
+ **Why fork?**
199
+ - Complete TypeScript rewrite for better type safety
200
+ - Modernize build tooling and dependencies
201
+ - Optimize bundle size and performance
202
+ - Maintain WHATWG standard compliance
203
+
204
+ **Original Author**: Domenic Denicola
205
+ **Current Maintainer**: Jason DiMeo
206
+
207
+ The original contributors' excellent work is acknowledged with gratitude. This fork maintains the same commitment to WHATWG standards compliance while providing a modern TypeScript-first experience.
208
+
209
+ ## License
210
+
211
+ ISC License - see [LICENSE](./LICENSE) file for details.
212
+
213
+ The original `whatwg-mimetype` package was licensed under MIT.
214
+
215
+ ## Contributing
216
+
217
+ Contributions are welcome! Please ensure:
218
+ - All tests pass (`pnpm test`)
219
+ - Code follows the ESLint configuration
220
+ - TypeScript types are properly defined
221
+ - Changes are documented in the CHANGELOG
222
+
223
+ ## Resources
224
+
225
+ - [WHATWG MIME Sniffing Standard](https://mimesniff.spec.whatwg.org/)
226
+ - [Original whatwg-mimetype package](https://github.com/jsdom/whatwg-mimetype)
227
+ - [Issue Tracker](https://github.com/D1g1talEntr0py/media-type/issues)
@@ -0,0 +1,184 @@
1
+ /**
2
+ * Class representing the parameters for a media type record.
3
+ * This class extends a JavaScript Map<string, string>.
4
+ *
5
+ * However, MediaTypeParameters methods will always interpret their arguments
6
+ * as appropriate for media types, so parameter names will be lowercased,
7
+ * and attempting to set invalid characters will throw an Error.
8
+ *
9
+ * @see https://mimesniff.spec.whatwg.org
10
+ * @author D1g1talEntr0py <jason.dimeo@gmail.com>
11
+ */
12
+ declare class MediaTypeParameters extends Map<string, string> {
13
+ /**
14
+ * Create a new MediaTypeParameters instance.
15
+ *
16
+ * @param entries An array of [ name, value ] tuples.
17
+ */
18
+ constructor(entries?: Iterable<[string, string]>);
19
+ /**
20
+ * Indicates whether the supplied name and value are valid media type parameters.
21
+ *
22
+ * @param name The name of the media type parameter to validate.
23
+ * @param value The media type parameter value to validate.
24
+ * @returns true if the media type parameter is valid, false otherwise.
25
+ */
26
+ static isValid(name: string, value: string): boolean;
27
+ /**
28
+ * Gets the media type parameter value for the supplied name.
29
+ *
30
+ * @param name The name of the media type parameter to retrieve.
31
+ * @returns The media type parameter value.
32
+ */
33
+ get(name: string): string | undefined;
34
+ /**
35
+ * Indicates whether the media type parameter with the specified name exists or not.
36
+ *
37
+ * @param name The name of the media type parameter to check.
38
+ * @returns true if the media type parameter exists, false otherwise.
39
+ */
40
+ has(name: string): boolean;
41
+ /**
42
+ * Adds a new media type parameter using the specified name and value to the MediaTypeParameters.
43
+ * If an parameter with the same name already exists, the parameter will be updated.
44
+ *
45
+ * @param name The name of the media type parameter to set.
46
+ * @param value The media type parameter value.
47
+ * @returns This instance.
48
+ */
49
+ set(name: string, value: string): this;
50
+ /**
51
+ * Removes the media type parameter using the specified name.
52
+ *
53
+ * @param name The name of the media type parameter to delete.
54
+ * @returns true if the parameter existed and has been removed, or false if the parameter does not exist.
55
+ */
56
+ delete(name: string): boolean;
57
+ /**
58
+ * Returns a string representation of the media type parameters.
59
+ *
60
+ * @returns The string representation of the media type parameters.
61
+ */
62
+ toString(): string;
63
+ /**
64
+ * Returns the name of this class.
65
+ *
66
+ * @returns The name of this class.
67
+ */
68
+ get [Symbol.toStringTag](): string;
69
+ }
70
+
71
+ /**
72
+ * Class used to parse media types.
73
+ * @see https://mimesniff.spec.whatwg.org/#understanding-mime-types
74
+ */
75
+ declare class MediaType {
76
+ private readonly _type;
77
+ private readonly _subtype;
78
+ private readonly _parameters;
79
+ /**
80
+ * Create a new MediaType instance from a string representation.
81
+ * @param mediaType The media type to parse.
82
+ * @param parameters Optional parameters.
83
+ */
84
+ constructor(mediaType: string, parameters?: Record<string, string>);
85
+ /**
86
+ * Parses a media type string.
87
+ * @param mediaType The media type to parse.
88
+ * @returns The parsed media type or null if the mediaType cannot be parsed.
89
+ */
90
+ static parse(mediaType: string): MediaType | null;
91
+ /**
92
+ * Gets the type.
93
+ * @returns The type.
94
+ */
95
+ get type(): string;
96
+ /**
97
+ * Gets the subtype.
98
+ * @returns The subtype.
99
+ */
100
+ get subtype(): string;
101
+ /**
102
+ * Gets the media type essence (type/subtype).
103
+ * @returns The media type without any parameters
104
+ */
105
+ get essence(): string;
106
+ /**
107
+ * Gets the parameters.
108
+ * @returns The media type parameters.
109
+ */
110
+ get parameters(): MediaTypeParameters;
111
+ /**
112
+ * Checks if the media type matches the specified type.
113
+ *
114
+ * @param mediaType The media type to check.
115
+ * @returns true if the media type matches the specified type, false otherwise.
116
+ */
117
+ matches(mediaType: MediaType | string): boolean;
118
+ /**
119
+ * Gets the serialized version of the media type.
120
+ *
121
+ * @returns The serialized media type.
122
+ */
123
+ toString(): string;
124
+ /**
125
+ * Gets the name of the class.
126
+ * @returns The class name
127
+ */
128
+ get [Symbol.toStringTag](): string;
129
+ }
130
+
131
+ interface MediaTypeComponent {
132
+ position?: number;
133
+ input: string;
134
+ lowerCase?: boolean;
135
+ trim?: boolean;
136
+ }
137
+ interface ParsedMediaType {
138
+ type: string;
139
+ subtype: string;
140
+ parameters: MediaTypeParameters;
141
+ }
142
+ /**
143
+ * Parser for media types.
144
+ * @see https://mimesniff.spec.whatwg.org/#parsing-a-mime-type
145
+ * @author D1g1talEntr0py <jason.dimeo@gmail.com>
146
+ */
147
+ declare class MediaTypeParser {
148
+ /**
149
+ * Function to parse a media type.
150
+ * @param input The media type to parse
151
+ * @returns An object populated with the parsed media type properties and any parameters.
152
+ */
153
+ static parse(input: string): ParsedMediaType;
154
+ /**
155
+ * Gets the name of this class.
156
+ * @returns The string tag of this class.
157
+ */
158
+ get [Symbol.toStringTag](): string;
159
+ /**
160
+ * Filters a component from the input string.
161
+ * @param options The options.
162
+ * @param charactersToFilter The characters to filter.
163
+ * @returns An object that includes the resulting string and updated position.
164
+ */
165
+ private static filterComponent;
166
+ /**
167
+ * Collects all the HTTP quoted strings.
168
+ * This variant only implements it with the extract-value flag set.
169
+ * @param input The string to process.
170
+ * @param position The starting position.
171
+ * @returns An array that includes the resulting string and updated position.
172
+ */
173
+ private static collectHttpQuotedString;
174
+ /**
175
+ * Generates an error message.
176
+ * @param component The component name.
177
+ * @param value The component value.
178
+ * @returns The error message.
179
+ */
180
+ private static generateErrorMessage;
181
+ }
182
+
183
+ export { MediaType, MediaTypeParameters, MediaTypeParser };
184
+ export type { MediaTypeComponent, ParsedMediaType };