diff --git a/package.json b/package.json index f6b1c06a35ccdd..19108d99c07b65 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,8 @@ "ts-node": "^10.4.0", "typescript": "~4.5.4", "vite": "workspace:*", - "vitepress": "^0.21.6" + "vitepress": "^0.21.6", + "compression" : "^1.7.4" }, "simple-git-hooks": { "pre-commit": "pnpm exec lint-staged --concurrent false", diff --git a/packages/vite/LICENSE.md b/packages/vite/LICENSE.md index a4169a00039134..3f3d644087c1c8 100644 --- a/packages/vite/LICENSE.md +++ b/packages/vite/LICENSE.md @@ -237,6 +237,33 @@ Repository: git+https://github.com/ampproject/remapping.git --------------------------------------- +## @jridgewell/resolve-uri +License: MIT +By: Justin Ridgewell +Repository: https://github.com/jridgewell/resolve-uri + +> Copyright 2019 Justin Ridgewell +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +> SOFTWARE. + +--------------------------------------- + ## @nodelib/fs.scandir License: MIT Repository: https://github.com/nodelib/nodelib/tree/master/packages/fs/fs.scandir @@ -403,7 +430,7 @@ Repository: rollup/plugins ## @vue/compiler-core License: MIT By: Evan You -Repository: git+https://github.com/vuejs/vue-next.git +Repository: git+https://github.com/vuejs/core.git > The MIT License (MIT) > @@ -432,7 +459,7 @@ Repository: git+https://github.com/vuejs/vue-next.git ## @vue/compiler-dom License: MIT By: Evan You -Repository: git+https://github.com/vuejs/vue-next.git +Repository: git+https://github.com/vuejs/core.git > The MIT License (MIT) > @@ -461,7 +488,7 @@ Repository: git+https://github.com/vuejs/vue-next.git ## @vue/shared License: MIT By: Evan You -Repository: git+https://github.com/vuejs/vue-next.git +Repository: git+https://github.com/vuejs/core.git > The MIT License (MIT) > @@ -487,37 +514,6 @@ Repository: git+https://github.com/vuejs/vue-next.git --------------------------------------- -## accepts -License: MIT -By: Douglas Christopher Wilson, Jonathan Ong -Repository: jshttp/accepts - -> (The MIT License) -> -> Copyright (c) 2014 Jonathan Ong -> Copyright (c) 2015 Douglas Christopher Wilson -> -> Permission is hereby granted, free of charge, to any person obtaining -> a copy of this software and associated documentation files (the -> 'Software'), to deal in the Software without restriction, including -> without limitation the rights to use, copy, modify, merge, publish, -> distribute, sublicense, and/or sell copies of the Software, and to -> permit persons to whom the Software is furnished to do so, subject to -> the following conditions: -> -> The above copyright notice and this permission notice shall be -> included in all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ---------------------------------------- - ## acorn License: MIT By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine @@ -691,37 +687,6 @@ Repository: micromatch/braces --------------------------------------- -## bytes -License: MIT -By: TJ Holowaychuk, Jed Watson, Théo FIDRY -Repository: visionmedia/bytes.js - -> (The MIT License) -> -> Copyright (c) 2012-2014 TJ Holowaychuk -> Copyright (c) 2015 Jed Watson -> -> Permission is hereby granted, free of charge, to any person obtaining -> a copy of this software and associated documentation files (the -> 'Software'), to deal in the Software without restriction, including -> without limitation the rights to use, copy, modify, merge, publish, -> distribute, sublicense, and/or sell copies of the Software, and to -> permit persons to whom the Software is furnished to do so, subject to -> the following conditions: -> -> The above copyright notice and this permission notice shall be -> included in all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ---------------------------------------- - ## cac License: MIT By: egoist @@ -812,69 +777,6 @@ Repository: http://github.com/substack/node-commondir.git --------------------------------------- -## compressible -License: MIT -By: Douglas Christopher Wilson, Jonathan Ong, Jeremiah Senkpiel -Repository: jshttp/compressible - -> (The MIT License) -> -> Copyright (c) 2013 Jonathan Ong -> Copyright (c) 2014 Jeremiah Senkpiel -> Copyright (c) 2015 Douglas Christopher Wilson -> -> Permission is hereby granted, free of charge, to any person obtaining -> a copy of this software and associated documentation files (the -> 'Software'), to deal in the Software without restriction, including -> without limitation the rights to use, copy, modify, merge, publish, -> distribute, sublicense, and/or sell copies of the Software, and to -> permit persons to whom the Software is furnished to do so, subject to -> the following conditions: -> -> The above copyright notice and this permission notice shall be -> included in all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ---------------------------------------- - -## compression -License: MIT -By: Douglas Christopher Wilson, Jonathan Ong -Repository: expressjs/compression - -> (The MIT License) -> -> Copyright (c) 2014 Jonathan Ong -> Copyright (c) 2014-2015 Douglas Christopher Wilson -> -> Permission is hereby granted, free of charge, to any person obtaining -> a copy of this software and associated documentation files (the -> 'Software'), to deal in the Software without restriction, including -> without limitation the rights to use, copy, modify, merge, publish, -> distribute, sublicense, and/or sell copies of the Software, and to -> permit persons to whom the Software is furnished to do so, subject to -> the following conditions: -> -> The above copyright notice and this permission notice shall be -> included in all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ---------------------------------------- - ## concat-map License: MIT By: James Halliday @@ -1158,6 +1060,7 @@ Repository: git://github.com/motdotla/dotenv.git ## dotenv-expand License: BSD-2-Clause By: motdotla +Repository: https://github.com/motdotla/dotenv-expand > Copyright (c) 2016, Scott Motte > All rights reserved. @@ -2105,66 +2008,6 @@ Repository: micromatch/micromatch --------------------------------------- -## mime-db -License: MIT -By: Douglas Christopher Wilson, Jonathan Ong, Robert Kieffer -Repository: jshttp/mime-db - -> The MIT License (MIT) -> -> Copyright (c) 2014 Jonathan Ong me@jongleberry.com -> -> Permission is hereby granted, free of charge, to any person obtaining a copy -> of this software and associated documentation files (the "Software"), to deal -> in the Software without restriction, including without limitation the rights -> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -> copies of the Software, and to permit persons to whom the Software is -> furnished to do so, subject to the following conditions: -> -> The above copyright notice and this permission notice shall be included in -> all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -> THE SOFTWARE. - ---------------------------------------- - -## mime-types -License: MIT -By: Douglas Christopher Wilson, Jeremiah Senkpiel, Jonathan Ong -Repository: jshttp/mime-types - -> (The MIT License) -> -> Copyright (c) 2014 Jonathan Ong -> Copyright (c) 2015 Douglas Christopher Wilson -> -> Permission is hereby granted, free of charge, to any person obtaining -> a copy of this software and associated documentation files (the -> 'Software'), to deal in the Software without restriction, including -> without limitation the rights to use, copy, modify, merge, publish, -> distribute, sublicense, and/or sell copies of the Software, and to -> permit persons to whom the Software is furnished to do so, subject to -> the following conditions: -> -> The above copyright notice and this permission notice shall be -> included in all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ---------------------------------------- - ## minimatch License: ISC By: Isaac Z. Schlueter @@ -2245,38 +2088,6 @@ Repository: zeit/ms --------------------------------------- -## negotiator -License: MIT -By: Douglas Christopher Wilson, Federico Romero, Isaac Z. Schlueter -Repository: jshttp/negotiator - -> (The MIT License) -> -> Copyright (c) 2012-2014 Federico Romero -> Copyright (c) 2012-2014 Isaac Z. Schlueter -> Copyright (c) 2014-2015 Douglas Christopher Wilson -> -> Permission is hereby granted, free of charge, to any person obtaining -> a copy of this software and associated documentation files (the -> 'Software'), to deal in the Software without restriction, including -> without limitation the rights to use, copy, modify, merge, publish, -> distribute, sublicense, and/or sell copies of the Software, and to -> permit persons to whom the Software is furnished to do so, subject to -> the following conditions: -> -> The above copyright notice and this permission notice shall be -> included in all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ---------------------------------------- - ## node-forge License: (BSD-3-Clause OR GPL-2.0) By: Digital Bazaar, Inc., Dave Longley, David I. Lehn, Stefan Siegl, Christoph Dorn @@ -2733,36 +2544,6 @@ Repository: jshttp/on-finished --------------------------------------- -## on-headers -License: MIT -By: Douglas Christopher Wilson -Repository: jshttp/on-headers - -> (The MIT License) -> -> Copyright (c) 2014 Douglas Christopher Wilson -> -> Permission is hereby granted, free of charge, to any person obtaining -> a copy of this software and associated documentation files (the -> 'Software'), to deal in the Software without restriction, including -> without limitation the rights to use, copy, modify, merge, publish, -> distribute, sublicense, and/or sell copies of the Software, and to -> permit persons to whom the Software is furnished to do so, subject to -> the following conditions: -> -> The above copyright notice and this permission notice shall be -> included in all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - ---------------------------------------- - ## once License: ISC By: Isaac Z. Schlueter diff --git a/packages/vite/package.json b/packages/vite/package.json index c55a4d99c3d365..0f89efa772c26d 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -80,7 +80,6 @@ "acorn": "^8.7.0", "cac": "6.7.9", "chokidar": "^3.5.3", - "compression": "^1.7.4", "connect": "^3.7.0", "connect-history-api-fallback": "^1.6.0", "convert-source-map": "^1.8.0", diff --git a/packages/vite/src/node/http.ts b/packages/vite/src/node/http.ts index fd45907ce0b723..37aef92335cab5 100644 --- a/packages/vite/src/node/http.ts +++ b/packages/vite/src/node/http.ts @@ -94,14 +94,6 @@ export async function resolveHttpServer( app: Connect.Server, httpsOptions?: HttpsServerOptions ): Promise { - /* - * Some Node.js packages are known to be using this undocumented function, - * notably "compression" middleware. - */ - app.prototype._implicitHeader = function _implicitHeader() { - this.writeHead(this.statusCode) - } - if (!httpsOptions) { return require('http').createServer(app) } diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index 5f4fe480c4f90f..56dee04e2e2e07 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -1,7 +1,7 @@ import path from 'path' import sirv from 'sirv' import connect from 'connect' -import compression from 'compression' +import compression from './server/middlewares/compression' import type { Server } from 'http' import type { InlineConfig, ResolvedConfig } from '.' import { resolveConfig } from '.' diff --git a/packages/vite/src/node/server/middlewares/compression.ts b/packages/vite/src/node/server/middlewares/compression.ts new file mode 100644 index 00000000000000..399da50ea38f25 --- /dev/null +++ b/packages/vite/src/node/server/middlewares/compression.ts @@ -0,0 +1,118 @@ +//@ts-nocheck +/* eslint-disable */ +//TODO: replace this code with https://github.com/lukeed/polka/pull/148 once it's released + +// This is based on https://github.com/preactjs/wmr/blob/main/packages/wmr/src/lib/polkompress.js +// MIT Licensed https://github.com/preactjs/wmr/blob/main/LICENSE +import zlib from 'zlib' + +/* global Buffer */ + +const noop = () => {} + +const mimes = /text|javascript|\/json|xml/i +const threshold = 1024 +const level = -1 +const brotli = false +const gzip = true + +const getChunkSize = (chunk, enc) => (chunk ? Buffer.byteLength(chunk, enc) : 0) + +export default function compression() { + const brotliOpts = (typeof brotli === 'object' && brotli) || {} + const gzipOpts = (typeof gzip === 'object' && gzip) || {} + + // disable Brotli on Node<12.7 where it is unsupported: + if (!zlib.createBrotliCompress) brotli = false + + return (req, res, next = noop) => { + const accept = req.headers['accept-encoding'] + '' + const encoding = ((brotli && accept.match(/\bbr\b/)) || + (gzip && accept.match(/\bgzip\b/)) || + [])[0] + + // skip if no response body or no supported encoding: + if (req.method === 'HEAD' || !encoding) return next() + + /** @type {zlib.Gzip | zlib.BrotliCompress} */ + let compress + let pendingStatus + /** @type {[string, function][]?} */ + let pendingListeners = [] + let started = false + let size = 0 + + function start() { + started = true + // @ts-ignore + size = res.getHeader('Content-Length') | 0 || size + const compressible = mimes.test( + String(res.getHeader('Content-Type') || 'text/plain') + ) + const cleartext = !res.getHeader('Content-Encoding') + const listeners = pendingListeners || [] + if (compressible && cleartext && size >= threshold) { + res.setHeader('Content-Encoding', encoding) + res.removeHeader('Content-Length') + if (encoding === 'br') { + const params = { + [zlib.constants.BROTLI_PARAM_QUALITY]: level, + [zlib.constants.BROTLI_PARAM_SIZE_HINT]: size + } + compress = zlib.createBrotliCompress({ + params: Object.assign(params, brotliOpts) + }) + } else { + compress = zlib.createGzip(Object.assign({ level }, gzipOpts)) + } + // backpressure + compress.on( + 'data', + (chunk) => write.call(res, chunk) === false && compress.pause() + ) + on.call(res, 'drain', () => compress.resume()) + compress.on('end', () => end.call(res)) + listeners.forEach((p) => compress.on.apply(compress, p)) + } else { + pendingListeners = null + listeners.forEach((p) => on.apply(res, p)) + } + + writeHead.call(res, pendingStatus || res.statusCode) + } + + const { end, write, on, writeHead } = res + + res.writeHead = function (status, reason, headers) { + if (typeof reason !== 'string') [headers, reason] = [reason, headers] + if (headers) for (let i in headers) res.setHeader(i, headers[i]) + pendingStatus = status + return this + } + + res.write = function (chunk, enc, cb) { + size += getChunkSize(chunk, enc) + if (!started) start() + if (!compress) return write.apply(this, arguments) + return compress.write.apply(compress, arguments) + } + + res.end = function (chunk, enc, cb) { + if (arguments.length > 0 && typeof chunk !== 'function') { + size += getChunkSize(chunk, enc) + } + if (!started) start() + if (!compress) return end.apply(this, arguments) + return compress.end.apply(compress, arguments) + } + + res.on = function (type, listener) { + if (!pendingListeners || type !== 'drain') on.call(this, type, listener) + else if (compress) compress.on(type, listener) + else pendingListeners.push([type, listener]) + return this + } + + next() + } +} \ No newline at end of file diff --git a/packages/vite/types/shims.d.ts b/packages/vite/types/shims.d.ts index 9c2905cfb76dec..68aa799cb6a863 100644 --- a/packages/vite/types/shims.d.ts +++ b/packages/vite/types/shims.d.ts @@ -93,11 +93,6 @@ declare module 'micromatch' { ): boolean } -declare module 'compression' { - function compression(): any - export default compression -} - // LESS' types somewhat references this which doesn't make sense in Node, // so we have to shim it declare interface HTMLLinkElement {} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e73bdd147602b1..358df1ed44b981 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,6 +16,7 @@ importers: '@types/semver': ^7.3.9 '@typescript-eslint/eslint-plugin': ^5.10.2 '@typescript-eslint/parser': ^5.10.2 + compression: ^1.7.4 conventional-changelog-cli: ^2.2.2 cross-env: ^7.0.3 esbuild: ^0.14.14 @@ -52,6 +53,7 @@ importers: '@types/semver': 7.3.9 '@typescript-eslint/eslint-plugin': 5.10.2_bd728c0845bbc9cf5062f079c5f7bc3e '@typescript-eslint/parser': 5.10.2_eslint@8.8.0+typescript@4.5.4 + compression: 1.7.4 conventional-changelog-cli: 2.2.2 cross-env: 7.0.3 esbuild: 0.14.14 @@ -764,7 +766,6 @@ importers: acorn: ^8.7.0 cac: 6.7.9 chokidar: ^3.5.3 - compression: ^1.7.4 connect: ^3.7.0 connect-history-api-fallback: ^1.6.0 convert-source-map: ^1.8.0 @@ -842,7 +843,6 @@ importers: acorn: 8.7.0 cac: 6.7.9 chokidar: 3.5.3 - compression: 1.7.4 connect: 3.7.0 connect-history-api-fallback: 1.6.0 convert-source-map: 1.8.0