Skip to content

Commit

Permalink
fix: remove extraneous mime-types package in favor of mime (#2435)
Browse files Browse the repository at this point in the history
* fix: remove extraneous mime-types package in favor of mime

* remove compressible dependency

* fix merge conflicts, fix tests

* add comment
  • Loading branch information
ddelgrosso1 authored Apr 15, 2024
1 parent fe1ac65 commit 63a71f2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@
"@google-cloud/promisify": "^4.0.0",
"abort-controller": "^3.0.0",
"async-retry": "^1.3.3",
"compressible": "^2.0.12",
"duplexify": "^4.1.3",
"ent": "^2.2.0",
"fast-xml-parser": "^4.3.0",
"gaxios": "^6.0.2",
"google-auth-library": "^9.6.3",
"mime": "^3.0.0",
"mime-types": "^2.0.8",
"p-limit": "^3.0.1",
"retry-request": "^7.0.0",
"teeny-request": "^9.0.0",
Expand All @@ -97,10 +95,8 @@
"@grpc/grpc-js": "^1.0.3",
"@grpc/proto-loader": "^0.7.0",
"@types/async-retry": "^1.4.3",
"@types/compressible": "^2.0.0",
"@types/ent": "^2.2.1",
"@types/mime": "^3.0.0",
"@types/mime-types": "^2.1.0",
"@types/mocha": "^9.1.1",
"@types/mockery": "^1.4.29",
"@types/node": "^20.4.4",
Expand Down
5 changes: 3 additions & 2 deletions src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {paginator} from '@google-cloud/paginator';
import {promisifyAll} from '@google-cloud/promisify';
import * as fs from 'fs';
import * as http from 'http';
import * as mime from 'mime-types';
import mime from 'mime';
import * as path from 'path';
import pLimit from 'p-limit';
import {promisify} from 'util';
Expand Down Expand Up @@ -1625,7 +1625,8 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
callback = callback || util.noop;

if (!destinationFile.metadata.contentType) {
const destinationContentType = mime.contentType(destinationFile.name);
const destinationContentType =
mime.getType(destinationFile.name) || undefined;

if (destinationContentType) {
destinationFile.metadata.contentType = destinationContentType;
Expand Down
24 changes: 22 additions & 2 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
} from './nodejs-common/index.js';
import {promisifyAll} from '@google-cloud/promisify';

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

/**
* @private
* This regex will match compressible content types. These are primarily text/*, +json, +text, +xml content types.
* This was based off of mime-db and may periodically need to be updated if new compressible content types become
* standards.
*/
const COMPRESSIBLE_MIME_REGEX = new RegExp(
[
/^text\/|application\/ecmascript|application\/javascript|application\/json/,
/|application\/postscript|application\/rtf|application\/toml|application\/vnd.dart/,
/|application\/vnd.ms-fontobject|application\/wasm|application\/x-httpd-php|application\/x-ns-proxy-autoconfig/,
/|application\/x-sh(?!ockwave-flash)|application\/x-tar|application\/x-virtualbox-hdd|application\/x-virtualbox-ova|application\/x-virtualbox-ovf/,
/|^application\/x-virtualbox-vbox$|application\/x-virtualbox-vdi|application\/x-virtualbox-vhd|application\/x-virtualbox-vmdk/,
/|application\/xml|application\/xml-dtd|font\/otf|font\/ttf|image\/bmp|image\/vnd.adobe.photoshop|image\/vnd.microsoft.icon/,
/|image\/vnd.ms-dds|image\/x-icon|image\/x-ms-bmp|message\/rfc822|model\/gltf-binary|\+json|\+text|\+xml|\+yaml/,
]
.map(r => r.source)
.join(''),
'i'
);

export interface FileOptions {
crc32cGenerator?: CRC32CValidatorGenerator;
encryptionKey?: string | Buffer;
Expand Down Expand Up @@ -1980,7 +2000,7 @@ class File extends ServiceObject<File, FileMetadata> {
let gzip = options.gzip;

if (gzip === 'auto') {
gzip = compressible(options!.metadata!.contentType || '');
gzip = COMPRESSIBLE_MIME_REGEX.test(options!.metadata!.contentType || '');
}

if (gzip) {
Expand Down
11 changes: 7 additions & 4 deletions test/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import assert from 'assert';
import * as fs from 'fs';
import {describe, it, before, beforeEach, after, afterEach} from 'mocha';
import * as mime from 'mime-types';
import mime from 'mime';
import pLimit from 'p-limit';
import * as path from 'path';
import proxyquire from 'proxyquire';
Expand Down Expand Up @@ -704,7 +704,7 @@ describe('Bucket', () => {
destination.request = (reqOpts: DecorateRequestOptions) => {
assert.strictEqual(
reqOpts.json.destination.contentType,
mime.contentType(destination.name)
mime.getType(destination.name)
);

done();
Expand Down Expand Up @@ -735,7 +735,7 @@ describe('Bucket', () => {
destination.request = (reqOpts: DecorateRequestOptions) => {
assert.strictEqual(
reqOpts.json.destination.contentType,
mime.contentType(destination.name)
mime.getType(destination.name)
);

done();
Expand All @@ -751,7 +751,10 @@ describe('Bucket', () => {
destination.request = (reqOpts: DecorateRequestOptions) => {
assert.strictEqual(reqOpts.uri, '/compose');
assert.deepStrictEqual(reqOpts.json, {
destination: {contentType: undefined, contentEncoding: undefined},
destination: {
contentType: mime.getType(destination.name) || undefined,
contentEncoding: undefined,
},
sourceObjects: [{name: sources[0].name}, {name: sources[1].name}],
});

Expand Down

0 comments on commit 63a71f2

Please sign in to comment.