mirror of
				https://github.com/karl0ss/homepage.git
				synced 2025-11-04 00:10:57 +00:00 
			
		
		
		
	feat: support compressed responses
This commit is contained in:
		
							parent
							
								
									c1a68a79ee
								
							
						
					
					
						commit
						3a29262256
					
				@ -1,5 +1,7 @@
 | 
				
			|||||||
/* eslint-disable prefer-promise-reject-errors */
 | 
					/* eslint-disable prefer-promise-reject-errors */
 | 
				
			||||||
/* eslint-disable no-param-reassign */
 | 
					/* eslint-disable no-param-reassign */
 | 
				
			||||||
 | 
					import { createUnzip } from "node:zlib";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { http, https } from "follow-redirects";
 | 
					import { http, https } from "follow-redirects";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { addCookieToJar, setCookieHeader } from "./cookie-jar";
 | 
					import { addCookieToJar, setCookieHeader } from "./cookie-jar";
 | 
				
			||||||
@ -28,12 +30,19 @@ function handleRequest(requestor, url, params) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const request = requestor.request(url, params, (response) => {
 | 
					    const request = requestor.request(url, params, (response) => {
 | 
				
			||||||
      const data = [];
 | 
					      const data = [];
 | 
				
			||||||
 | 
					      const contentEncoding = response.headers['content-encoding']?.trim().toLowerCase();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      response.on("data", (chunk) => {
 | 
					      let responseContent = response;
 | 
				
			||||||
 | 
					      if (contentEncoding === 'gzip' || contentEncoding === 'deflate') {
 | 
				
			||||||
 | 
					        responseContent = createUnzip();
 | 
				
			||||||
 | 
					        response.pipe(responseContent);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      responseContent.on("data", (chunk) => {
 | 
				
			||||||
        data.push(chunk);
 | 
					        data.push(chunk);
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      response.on("end", () => {
 | 
					      responseContent.on("end", () => {
 | 
				
			||||||
        addCookieToJar(url, response.headers);
 | 
					        addCookieToJar(url, response.headers);
 | 
				
			||||||
        resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
 | 
					        resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user