mime-types 3.6.0 → 3.7.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.
data/README.md ADDED
@@ -0,0 +1,200 @@
1
+ # mime-types for Ruby
2
+
3
+ - home :: https://github.com/mime-types/ruby-mime-types/
4
+ - issues :: https://github.com/mime-types/ruby-mime-types/issues
5
+ - code :: https://github.com/mime-types/ruby-mime-types/
6
+ - rdoc :: http://rdoc.info/gems/mime-types/
7
+ - changelog ::
8
+ https://github.com/mime-types/ruby-mime-types/blob/main/CHANGELOG.md
9
+ - continuous integration :: [![Build Status][ci-badge]][ci-workflow]
10
+ - test coverage :: [![Coverage][coveralls-badge]][coveralls]
11
+
12
+ ## Description
13
+
14
+ The mime-types library provides a library and registry for information about
15
+ MIME content type definitions. It can be used to determine defined filename
16
+ extensions for MIME types, or to use filename extensions to look up the likely
17
+ MIME type definitions.
18
+
19
+ Version 3.0 is a major release that requires Ruby 2.0 compatibility and removes
20
+ deprecated functions. The columnar registry format introduced in 2.6 has been
21
+ made the primary format; the registry data has been extracted from this library
22
+ and put into [mime-types-data][data]. Additionally, mime-types is now licensed
23
+ exclusively under the MIT licence and there is a code of conduct in effect.
24
+ There are a number of other smaller changes described in the History file.
25
+
26
+ ### About MIME Media Types
27
+
28
+ MIME content types are used in MIME-compliant communications, as in e-mail or
29
+ HTTP traffic, to indicate the type of content which is transmitted. The
30
+ mime-types library provides the ability for detailed information about MIME
31
+ entities (provided as an enumerable collection of MIME::Type objects) to be
32
+ determined and used. There are many types defined by RFCs and vendors, so the
33
+ list is long but by definition incomplete; don't hesitate to add additional type
34
+ definitions. MIME type definitions found in mime-types are from RFCs, W3C
35
+ recommendations, the [IANA Media Types registry][registry], and user
36
+ contributions. It conforms to RFCs 2045 and 2231.
37
+
38
+ ### mime-types 3.x
39
+
40
+ Users are encouraged to upgrade to mime-types 3.x as soon as is practical.
41
+ mime-types 3.x requires Ruby 2.0 compatibility and a simpler licensing scheme.
42
+
43
+ ## Synopsis
44
+
45
+ MIME types are used in MIME entities, as in email or HTTP traffic. It is useful
46
+ at times to have information available about MIME types (or, inversely, about
47
+ files). A MIME::Type stores the known information about one MIME type.
48
+
49
+ ```ruby
50
+ require 'mime/types'
51
+
52
+ plaintext = MIME::Types['text/plain'] # => [ text/plain ]
53
+ text = plaintext.first
54
+ puts text.media_type # => 'text'
55
+ puts text.sub_type # => 'plain'
56
+
57
+ puts text.extensions.join(' ') # => 'txt asc c cc h hh cpp hpp dat hlp'
58
+ puts text.preferred_extension # => 'txt'
59
+ puts text.friendly # => 'Text Document'
60
+ puts text.i18n_key # => 'text.plain'
61
+
62
+ puts text.encoding # => quoted-printable
63
+ puts text.default_encoding # => quoted-printable
64
+ puts text.binary? # => false
65
+ puts text.ascii? # => true
66
+ puts text.obsolete? # => false
67
+ puts text.registered? # => true
68
+ puts text.provisional? # => false
69
+ puts text.complete? # => true
70
+
71
+ puts text # => 'text/plain'
72
+
73
+ puts text == 'text/plain' # => true
74
+ puts 'text/plain' == text # => true
75
+ puts text == 'text/x-plain' # => false
76
+ puts 'text/x-plain' == text # => false
77
+
78
+ puts MIME::Type.simplified('x-appl/x-zip') # => 'x-appl/x-zip'
79
+ puts MIME::Type.i18n_key('x-appl/x-zip') # => 'x-appl.x-zip'
80
+
81
+ puts text.like?('text/x-plain') # => true
82
+ puts text.like?(MIME::Type.new('x-text/x-plain')) # => true
83
+
84
+ puts text.xrefs.inspect # => { "rfc" => [ "rfc2046", "rfc3676", "rfc5147" ] }
85
+ puts text.xref_urls # => [ "http://www.iana.org/go/rfc2046",
86
+ # "http://www.iana.org/go/rfc3676",
87
+ # "http://www.iana.org/go/rfc5147" ]
88
+
89
+ xtext = MIME::Type.new('x-text/x-plain')
90
+ puts xtext.media_type # => 'text'
91
+ puts xtext.raw_media_type # => 'x-text'
92
+ puts xtext.sub_type # => 'plain'
93
+ puts xtext.raw_sub_type # => 'x-plain'
94
+ puts xtext.complete? # => false
95
+
96
+ puts MIME::Types.any? { |type| type.content_type == 'text/plain' } # => true
97
+ puts MIME::Types.all?(&:registered?) # => false
98
+
99
+ # Various string representations of MIME types
100
+ qcelp = MIME::Types['audio/QCELP'].first # => audio/QCELP
101
+ puts qcelp.content_type # => 'audio/QCELP'
102
+ puts qcelp.simplified # => 'audio/qcelp'
103
+
104
+ xwingz = MIME::Types['application/x-Wingz'].first # => application/x-Wingz
105
+ puts xwingz.content_type # => 'application/x-Wingz'
106
+ puts xwingz.simplified # => 'application/x-wingz'
107
+ ```
108
+
109
+ ### Columnar Store
110
+
111
+ mime-types uses as its primary registry storage format a columnar storage format
112
+ reducing the default memory footprint. This is done by selectively loading the
113
+ data on a per-attribute basis. When the registry is first loaded from the
114
+ columnar store, only the canonical MIME content type and known extensions and
115
+ the MIME type will be connected to its loading registry. When other data about
116
+ the type is required (including `preferred_extension`, `obsolete?`, and
117
+ `registered?`) that data is loaded from its own column file for all types in the
118
+ registry.
119
+
120
+ The load of any column data is performed with a Mutex to ensure that types are
121
+ updated safely in a multithreaded environment. Benchmarks show that while
122
+ columnar data loading is slower than the JSON store, it cuts the memory use by a
123
+ third over the JSON store.
124
+
125
+ If you prefer to load all the data at once, this can be specified in your
126
+ application Gemfile as:
127
+
128
+ ```ruby
129
+ gem 'mime-types', require: 'mime/types/full'
130
+ ```
131
+
132
+ Projects that do not use Bundler should `require` the same:
133
+
134
+ ```ruby
135
+ require 'mime/types/full'
136
+ ```
137
+
138
+ Libraries that use mime-types are discouraged from choosing the JSON store.
139
+
140
+ For applications and clients that used mime-types 2.6 when the columnar store
141
+ was introduced, the require used previously will still work through at least
142
+ [version 4][pull-96-comment] and possibly beyond; it is effectively an empty
143
+ operation. You are recommended to change your Gemfile as soon as is practical.
144
+
145
+ ```ruby
146
+ require 'mime/types/columnar'
147
+ ```
148
+
149
+ Note that MIME::Type::Columnar and MIME::Types::Columnar are considered private
150
+ variant implementations of MIME::Type and MIME::Types and the specific
151
+ implementation should not be relied upon by consumers of the mime-types library.
152
+ Instead, depend on the public implementations (MIME::Type and MIME::Types) only.
153
+
154
+ ### Cached Storage
155
+
156
+ mime-types supports a cache of MIME types using `Marshal.dump`. The cache is
157
+ invalidated for each version of the mime-types-data gem so that data version
158
+ 3.2015.1201 will not be reused with data version 3.2016.0101. If the environment
159
+ variable `RUBY_MIME_TYPES_CACHE` is set to a cache file, mime-types will attempt
160
+ to load the MIME type registry from the cache file. If it cannot, it will load
161
+ the types normally and then saves the registry to the cache file.
162
+
163
+ The caching works with both full stores and columnar stores. Only the data that
164
+ has been loaded prior to saving the cache will be stored.
165
+
166
+ ## mime-types Modified Semantic Versioning
167
+
168
+ The mime-types library has one version number, but this single version number
169
+ tracks both API changes and registry data changes; this is not wholly compatible
170
+ with all aspects of [Semantic Versioning][semver]; removing a MIME type from the
171
+ registry _could_ be considered a breaking change under some interpretations of
172
+ semantic versioning (as lookups for that particular type would no longer work by
173
+ default).
174
+
175
+ mime-types itself uses a modified semantic versioning scheme. Given the version
176
+ `MAJOR.MINOR`:
177
+
178
+ 1. If an incompatible API (code) change is made, the `MAJOR` version will be
179
+ incremented and both `MINOR` and `PATCH` will be set to zero. Major version
180
+ updates will also generally break Ruby version compatibility guarantees.
181
+
182
+ 2. If an API (code) feature is added that does not break compatibility, the
183
+ `MINOR` version will be incremented and `PATCH` will be set to zero.
184
+
185
+ 3. If there is a bug fix to a feature added in the most recent `MAJOR.MINOR`
186
+ release, the `PATCH` value will be incremented.
187
+
188
+ In practical terms, there will be fewer releases of mime-types focussing on
189
+ features because of the existence of the [mime-types-data][data] gem, and if
190
+ features are marked deprecated in the course of mime-types 3.x, they will not be
191
+ removed until mime-types 4.x or possibly later.
192
+
193
+ [pull-96-comment]: https://github.com/mime-types/ruby-mime-types/pull/96#issuecomment-100725400
194
+ [semver]: https://semver.org
195
+ [data]: https://github.com/mime-types/mime-types-data
196
+ [ci-badge]: https://github.com/mime-types/ruby-mime-types/actions/workflows/ci.yml/badge.svg
197
+ [ci-workflow]: https://github.com/mime-types/ruby-mime-types/actions/workflows/ci.yml
198
+ [coveralls-badge]: https://coveralls.io/repos/mime-types/ruby-mime-types/badge.svg?branch=main&service=github
199
+ [coveralls]: https://coveralls.io/github/mime-types/ruby-mime-types?branch=main
200
+ [registry]: https://www.iana.org/assignments/media-types/media-types.xhtml
data/Rakefile CHANGED
@@ -1,42 +1,74 @@
1
1
  require "rubygems"
2
2
  require "hoe"
3
3
  require "rake/clean"
4
+ require "minitest"
5
+ require "minitest/test_task"
4
6
 
5
- Hoe.plugin :cov
6
- Hoe.plugin :doofus
7
- Hoe.plugin :gemspec2
8
- Hoe.plugin :git2
9
- Hoe.plugin :minitest
7
+ Hoe.plugin :halostatue
10
8
  Hoe.plugin :rubygems
11
9
 
10
+ Hoe.plugins.delete :debug
11
+ Hoe.plugins.delete :newb
12
+ Hoe.plugins.delete :publish
13
+ Hoe.plugins.delete :signing
14
+ Hoe.plugins.delete :test
15
+
12
16
  spec = Hoe.spec "mime-types" do
13
17
  developer("Austin Ziegler", "halostatue@gmail.com")
14
18
 
15
- self.history_file = "History.md"
16
- self.readme_file = "README.rdoc"
17
-
18
- license "MIT"
19
+ self.trusted_release = ENV["rubygems_release_gem"] == "true"
19
20
 
20
21
  require_ruby_version ">= 2.0"
21
22
 
22
- spec_extras[:metadata] = ->(val) { val["rubygems_mfa_required"] = "true" }
23
+ license "MIT"
24
+
25
+ spec_extras[:metadata] = ->(val) {
26
+ val.merge!({"rubygems_mfa_required" => "true"})
27
+ }
23
28
 
24
- extra_deps << ["mime-types-data", "~> 3.2015"]
29
+ extra_deps << ["mime-types-data", "~> 3.2025", ">= 3.2025.0507"]
25
30
  extra_deps << ["logger", ">= 0"]
26
31
 
27
- extra_dev_deps << ["hoe", ">= 3.0", "< 5"]
28
- extra_dev_deps << ["hoe-doofus", "~> 1.0"]
29
- extra_dev_deps << ["hoe-gemspec2", "~> 1.1"]
30
- extra_dev_deps << ["hoe-git2", "~> 1.7"]
32
+ extra_dev_deps << ["hoe", "~> 4.0"]
33
+ extra_dev_deps << ["hoe-halostatue", "~> 2.0"]
31
34
  extra_dev_deps << ["hoe-rubygems", "~> 1.0"]
32
35
  extra_dev_deps << ["minitest", "~> 5.0"]
33
36
  extra_dev_deps << ["minitest-autotest", "~> 1.0"]
34
37
  extra_dev_deps << ["minitest-focus", "~> 1.0"]
35
38
  extra_dev_deps << ["minitest-hooks", "~> 1.4"]
36
- extra_dev_deps << ["rake", ">= 10.0", "< 14.0"]
39
+ extra_dev_deps << ["rake", ">= 10.0", "< 14"]
40
+ extra_dev_deps << ["rdoc", ">= 0.0"]
37
41
  extra_dev_deps << ["standard", "~> 1.0"]
38
42
  end
39
43
 
44
+ Minitest::TestTask.create :test
45
+ Minitest::TestTask.create :coverage do |t|
46
+ formatters = <<-RUBY.split($/).join(" ")
47
+ SimpleCov::Formatter::MultiFormatter.new([
48
+ SimpleCov::Formatter::HTMLFormatter,
49
+ SimpleCov::Formatter::LcovFormatter,
50
+ SimpleCov::Formatter::SimpleFormatter
51
+ ])
52
+ RUBY
53
+ t.test_prelude = <<-RUBY.split($/).join("; ")
54
+ require "simplecov"
55
+ require "simplecov-lcov"
56
+
57
+ SimpleCov::Formatter::LcovFormatter.config do |config|
58
+ config.report_with_single_file = true
59
+ config.lcov_file_name = "lcov.info"
60
+ end
61
+
62
+ SimpleCov.start "test_frameworks" do
63
+ enable_coverage :branch
64
+ primary_coverage :branch
65
+ formatter #{formatters}
66
+ end
67
+ RUBY
68
+ end
69
+
70
+ task default: :test
71
+
40
72
  namespace :benchmark do
41
73
  task :support do
42
74
  %w[lib support].each { |path|
@@ -53,39 +85,9 @@ namespace :benchmark do
53
85
  )
54
86
  end
55
87
 
56
- desc "Allocation counts"
57
- task :allocations, [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
58
- require "benchmarks/load_allocations"
59
- Benchmarks::LoadAllocations.report(
60
- top_x: args.top_x,
61
- mime_types_only: args.mime_types_only
62
- )
63
- end
64
-
65
- desc "Columnar allocation counts"
66
- task "allocations:columnar", [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
67
- require "benchmarks/load_allocations"
68
- Benchmarks::LoadAllocations.report(
69
- columnar: true,
70
- top_x: args.top_x,
71
- mime_types_only: args.mime_types_only
72
- )
73
- end
74
-
75
- desc "Columnar allocation counts (full load)"
76
- task "allocations:columnar:full", [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
77
- require "benchmarks/load_allocations"
78
- Benchmarks::LoadAllocations.report(
79
- columnar: true,
80
- top_x: args.top_x,
81
- mime_types_only: args.mime_types_only,
82
- full: true
83
- )
84
- end
85
-
86
88
  desc "Memory profiler"
87
89
  task :memory, [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
88
- require "benchmarks/memory_profiler"
90
+ require "benchmarks/profile_memory"
89
91
  Benchmarks::ProfileMemory.report(
90
92
  mime_types_only: args.mime_types_only,
91
93
  top_x: args.top_x
@@ -94,7 +96,7 @@ namespace :benchmark do
94
96
 
95
97
  desc "Columnar memory profiler"
96
98
  task "memory:columnar", [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
97
- require "benchmarks/memory_profiler"
99
+ require "benchmarks/profile_memory"
98
100
  Benchmarks::ProfileMemory.report(
99
101
  columnar: true,
100
102
  mime_types_only: args.mime_types_only,
@@ -104,7 +106,7 @@ namespace :benchmark do
104
106
 
105
107
  desc "Columnar allocation counts (full load)"
106
108
  task "memory:columnar:full", [:top_x, :mime_types_only] => "benchmark:support" do |_, args|
107
- require "benchmarks/memory_profiler"
109
+ require "benchmarks/profile_memory"
108
110
  Benchmarks::ProfileMemory.report(
109
111
  columnar: true,
110
112
  full: true,
@@ -133,53 +135,19 @@ namespace :benchmark do
133
135
  end
134
136
 
135
137
  namespace :profile do
136
- directory "tmp/profile"
137
-
138
- CLEAN.add "tmp"
139
-
140
- def ruby_prof(script)
141
- require "pathname"
142
- output = Pathname("tmp/profile").join(script)
143
- output.mkpath
144
- script = Pathname("support/profile").join("#{script}.rb")
145
-
146
- args = [
147
- "-W0",
148
- "-Ilib",
149
- "-S", "ruby-prof",
150
- "-R", "mime/types",
151
- "-s", "self",
152
- "-p", "multi",
153
- "-f", output.to_s,
154
- script.to_s
155
- ]
156
- ruby args.join(" ")
138
+ task full: "benchmark:support" do
139
+ require "profile"
140
+ profile_full
157
141
  end
158
142
 
159
- task full: "tmp/profile" do
160
- ruby_prof "full"
143
+ task columnar: "benchmark:support" do
144
+ require "profile"
145
+ profile_columnar
161
146
  end
162
147
 
163
- task columnar: :support do
164
- ruby_prof "columnar"
165
- end
166
-
167
- task "columnar:full" => :support do
168
- ruby_prof "columnar_full"
169
- end
170
- end
171
-
172
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.0")
173
- namespace :test do
174
- desc "Run test coverage"
175
- task :coverage do
176
- spec.test_prelude = [
177
- 'require "simplecov"',
178
- 'SimpleCov.start("test_frameworks") { command_name "Minitest" }',
179
- 'gem "minitest"'
180
- ].join("; ")
181
- Rake::Task["test"].execute
182
- end
148
+ task "columnar:full" => "benchmark:support" do
149
+ require "profile"
150
+ profile_columnar_full
183
151
  end
184
152
  end
185
153
 
@@ -210,6 +178,11 @@ namespace :convert do
210
178
  task docs: "convert:docs:run"
211
179
  end
212
180
 
181
+ task :version do
182
+ require "mime/types/version"
183
+ puts MIME::Types::VERSION
184
+ end
185
+
213
186
  namespace :deps do
214
187
  task :top, [:number] => "benchmark:support" do |_, args|
215
188
  require "deps"
@@ -226,5 +199,3 @@ task :console do
226
199
  abort
227
200
  end
228
201
  end
229
-
230
- # vim: syntax=ruby
data/SECURITY.md ADDED
@@ -0,0 +1,7 @@
1
+ # mime-types Security
2
+
3
+ ## Security contact information
4
+
5
+ To report a security vulnerability, please use the
6
+ [Tidelift security contact](https://tidelift.com/security). Tidelift will
7
+ coordinate the fix and disclosure.
@@ -15,8 +15,10 @@ require "mime/type"
15
15
  class MIME::Type::Columnar < MIME::Type
16
16
  def initialize(container, content_type, extensions) # :nodoc:
17
17
  @container = container
18
+ @__priority_penalty = nil
18
19
  self.content_type = content_type
19
- self.extensions = extensions
20
+ @extensions = Set[*Array(extensions).flatten.compact].freeze
21
+ clear_sort_priority
20
22
  end
21
23
 
22
24
  def self.column(*methods, file: nil) # :nodoc:
@@ -51,6 +53,17 @@ class MIME::Type::Columnar < MIME::Type
51
53
  super
52
54
  end
53
55
 
56
+ def update_sort_priority
57
+ if @container.__fully_loaded?
58
+ super
59
+ else
60
+ obsolete = (@__sort_priority & (1 << 7)) != 0
61
+ registered = (@__sort_priority & (1 << 5)) == 0
62
+
63
+ @__priority_penalty = (obsolete ? 3 : 0) + (registered ? 0 : 2)
64
+ end
65
+ end
66
+
54
67
  class << self
55
68
  undef column
56
69
  end