Skip to content

Commit 63a71f2

Browse files
authored
fix: remove extraneous mime-types package in favor of mime (#2435)
* fix: remove extraneous mime-types package in favor of mime * remove compressible dependency * fix merge conflicts, fix tests * add comment
1 parent fe1ac65 commit 63a71f2

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,12 @@
7777
"@google-cloud/promisify": "^4.0.0",
7878
"abort-controller": "^3.0.0",
7979
"async-retry": "^1.3.3",
80-
"compressible": "^2.0.12",
8180
"duplexify": "^4.1.3",
8281
"ent": "^2.2.0",
8382
"fast-xml-parser": "^4.3.0",
8483
"gaxios": "^6.0.2",
8584
"google-auth-library": "^9.6.3",
8685
"mime": "^3.0.0",
87-
"mime-types": "^2.0.8",
8886
"p-limit": "^3.0.1",
8987
"retry-request": "^7.0.0",
9088
"teeny-request": "^9.0.0",
@@ -97,10 +95,8 @@
9795
"@grpc/grpc-js": "^1.0.3",
9896
"@grpc/proto-loader": "^0.7.0",
9997
"@types/async-retry": "^1.4.3",
100-
"@types/compressible": "^2.0.0",
10198
"@types/ent": "^2.2.1",
10299
"@types/mime": "^3.0.0",
103-
"@types/mime-types": "^2.1.0",
104100
"@types/mocha": "^9.1.1",
105101
"@types/mockery": "^1.4.29",
106102
"@types/node": "^20.4.4",

src/bucket.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {paginator} from '@google-cloud/paginator';
2929
import {promisifyAll} from '@google-cloud/promisify';
3030
import * as fs from 'fs';
3131
import * as http from 'http';
32-
import * as mime from 'mime-types';
32+
import mime from 'mime';
3333
import * as path from 'path';
3434
import pLimit from 'p-limit';
3535
import {promisify} from 'util';
@@ -1625,7 +1625,8 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
16251625
callback = callback || util.noop;
16261626

16271627
if (!destinationFile.metadata.contentType) {
1628-
const destinationContentType = mime.contentType(destinationFile.name);
1628+
const destinationContentType =
1629+
mime.getType(destinationFile.name) || undefined;
16291630

16301631
if (destinationContentType) {
16311632
destinationFile.metadata.contentType = destinationContentType;

src/file.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
} from './nodejs-common/index.js';
2525
import {promisifyAll} from '@google-cloud/promisify';
2626

27-
import compressible from 'compressible';
2827
import * as crypto from 'crypto';
2928
import * as fs from 'fs';
3029
import mime from 'mime';
@@ -326,6 +325,27 @@ export const STORAGE_POST_POLICY_BASE_URL = 'https://storage.googleapis.com';
326325
*/
327326
const GS_URL_REGEXP = /^gs:\/\/([a-z0-9_.-]+)\/(.+)$/;
328327

328+
/**
329+
* @private
330+
* This regex will match compressible content types. These are primarily text/*, +json, +text, +xml content types.
331+
* This was based off of mime-db and may periodically need to be updated if new compressible content types become
332+
* standards.
333+
*/
334+
const COMPRESSIBLE_MIME_REGEX = new RegExp(
335+
[
336+
/^text\/|application\/ecmascript|application\/javascript|application\/json/,
337+
/|application\/postscript|application\/rtf|application\/toml|application\/vnd.dart/,
338+
/|application\/vnd.ms-fontobject|application\/wasm|application\/x-httpd-php|application\/x-ns-proxy-autoconfig/,
339+
/|application\/x-sh(?!ockwave-flash)|application\/x-tar|application\/x-virtualbox-hdd|application\/x-virtualbox-ova|application\/x-virtualbox-ovf/,
340+
/|^application\/x-virtualbox-vbox$|application\/x-virtualbox-vdi|application\/x-virtualbox-vhd|application\/x-virtualbox-vmdk/,
341+
/|application\/xml|application\/xml-dtd|font\/otf|font\/ttf|image\/bmp|image\/vnd.adobe.photoshop|image\/vnd.microsoft.icon/,
342+
/|image\/vnd.ms-dds|image\/x-icon|image\/x-ms-bmp|message\/rfc822|model\/gltf-binary|\+json|\+text|\+xml|\+yaml/,
343+
]
344+
.map(r => r.source)
345+
.join(''),
346+
'i'
347+
);
348+
329349
export interface FileOptions {
330350
crc32cGenerator?: CRC32CValidatorGenerator;
331351
encryptionKey?: string | Buffer;
@@ -1980,7 +2000,7 @@ class File extends ServiceObject<File, FileMetadata> {
19802000
let gzip = options.gzip;
19812001

19822002
if (gzip === 'auto') {
1983-
gzip = compressible(options!.metadata!.contentType || '');
2003+
gzip = COMPRESSIBLE_MIME_REGEX.test(options!.metadata!.contentType || '');
19842004
}
19852005

19862006
if (gzip) {

test/bucket.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import assert from 'assert';
2323
import * as fs from 'fs';
2424
import {describe, it, before, beforeEach, after, afterEach} from 'mocha';
25-
import * as mime from 'mime-types';
25+
import mime from 'mime';
2626
import pLimit from 'p-limit';
2727
import * as path from 'path';
2828
import proxyquire from 'proxyquire';
@@ -704,7 +704,7 @@ describe('Bucket', () => {
704704
destination.request = (reqOpts: DecorateRequestOptions) => {
705705
assert.strictEqual(
706706
reqOpts.json.destination.contentType,
707-
mime.contentType(destination.name)
707+
mime.getType(destination.name)
708708
);
709709

710710
done();
@@ -735,7 +735,7 @@ describe('Bucket', () => {
735735
destination.request = (reqOpts: DecorateRequestOptions) => {
736736
assert.strictEqual(
737737
reqOpts.json.destination.contentType,
738-
mime.contentType(destination.name)
738+
mime.getType(destination.name)
739739
);
740740

741741
done();
@@ -751,7 +751,10 @@ describe('Bucket', () => {
751751
destination.request = (reqOpts: DecorateRequestOptions) => {
752752
assert.strictEqual(reqOpts.uri, '/compose');
753753
assert.deepStrictEqual(reqOpts.json, {
754-
destination: {contentType: undefined, contentEncoding: undefined},
754+
destination: {
755+
contentType: mime.getType(destination.name) || undefined,
756+
contentEncoding: undefined,
757+
},
755758
sourceObjects: [{name: sources[0].name}, {name: sources[1].name}],
756759
});
757760

0 commit comments

Comments
 (0)