diff --git a/packages/vite/LICENSE.md b/packages/vite/LICENSE.md index 107329e4f89e81..0690d0a7730557 100644 --- a/packages/vite/LICENSE.md +++ b/packages/vite/LICENSE.md @@ -570,37 +570,6 @@ Repository: git+https://github.com/vuejs/core.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 @@ -774,37 +743,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 @@ -895,69 +833,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 @@ -2188,66 +2063,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 @@ -2328,38 +2143,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 @@ -2816,36 +2599,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 f790184bf765f2..8bc61c2e19698c 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -79,7 +79,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 c07fd41ba9060d..bfc2ddbe32a302 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..7a97044985ac0e --- /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() + } +} 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 9982e48276c096..7260844b0200ba 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -177,6 +177,9 @@ importers: devDependencies: cross-env: 7.0.3 + packages/playground/env-nested: + specifiers: {} + packages/playground/extensions: specifiers: vue: ^3.2.25 @@ -786,7 +789,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 @@ -864,7 +866,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