Esempio n. 1
0
	// Apply logger on the provided connection
	static applyLogger(connection, logger) {

		// Check for enabled logger to log the connection
		if (Config.isEnabled(logger.enabled, connection)) {
			connection.log(logger.format, logger.tokens, logger.log);
		}
	}
Esempio n. 2
0
	// Prepare connection for writing process
	static start(connection) {

		const { compression, cors, timeout } = connection._router._options;

		// Set the default content type if it is not defined
		if (!connection.type()) {
			connection.type('html');
		}

		// Check for CORS requests
		if (connection.headers.origin) {
			HTTPConnection.applyCORS(connection, cors);
		}

		// Check for requests accepting compressed response
		if (connection.headers['accept-encoding']) {
			HTTPConnection.applyCompression(connection, compression);
		} else {

			// In Node 10+ use stream.pipeline()
			connection.pipe(connection.response);
		}

		// Set the connection keep alive timeout from router options
		if (Config.isEnabled(timeout.enabled, connection)) {
			connection.keep(timeout.value);
		}

		// Set the started flag
		connection._started = true;
	}
Esempio n. 3
0
	// Apply compression on the provided connection
	static applyCompression(connection, compression) {

		let responseStream = connection.response;

		// Check for enabled compression and filter connection
		if (Config.isEnabled(compression.enabled, connection)) {

			const header = connection.headers['accept-encoding'];
			const encodings = QValueParser.parse(header, true);

			let encoding = null;

			// Check for accepted encoding
			if (encodings.includes(compression.preferred)) {
				if (compression.preferred === 'deflate') {
					encoding = 'Deflate';
				} else {
					encoding = 'Gzip';
				}
			} else if (encodings.includes('deflate')) {
				encoding = 'Deflate';
			} else if (encodings.includes('gzip')) {
				encoding = 'Gzip';
			}

			// Check for encoding to set the header and prepare the response
			if (encoding) {
				connection.header('Content-Encoding', encoding);
				responseStream = zlib[encoding](compression.options);

				// In Node 10+ use stream.pipeline()
				responseStream.pipe(connection.response);
			}
		}

		// Pipe the connection to the response stream
		// In Node 10+ use stream.pipeline()
		connection.pipe(responseStream);
	}