From 63a71f2e81181ad976e982ef79d6148913a87c1f Mon Sep 17 00:00:00 2001 From: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com> Date: Mon, 15 Apr 2024 13:03:12 -0400 Subject: [PATCH] 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 --- package.json | 4 ---- src/bucket.ts | 5 +++-- src/file.ts | 24 ++++++++++++++++++++++-- test/bucket.ts | 11 +++++++---- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 01462e990..a49ee9b65 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/bucket.ts b/src/bucket.ts index be792435f..d4321e072 100644 --- a/src/bucket.ts +++ b/src/bucket.ts @@ -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'; @@ -1625,7 +1625,8 @@ class Bucket extends ServiceObject { 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; diff --git a/src/file.ts b/src/file.ts index 45e2d9f7f..8edb2c602 100644 --- a/src/file.ts +++ b/src/file.ts @@ -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'; @@ -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; @@ -1980,7 +2000,7 @@ class File extends ServiceObject { let gzip = options.gzip; if (gzip === 'auto') { - gzip = compressible(options!.metadata!.contentType || ''); + gzip = COMPRESSIBLE_MIME_REGEX.test(options!.metadata!.contentType || ''); } if (gzip) { diff --git a/test/bucket.ts b/test/bucket.ts index 1b38abf1f..951cef220 100644 --- a/test/bucket.ts +++ b/test/bucket.ts @@ -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'; @@ -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(); @@ -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(); @@ -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}], });