{"library":"popsicle-content-encoding","title":"Popsicle Content Encoding Middleware","description":"This package, `popsicle-content-encoding`, provides a specialized middleware for the `Popsicle` HTTP client library, designed to transparently manage HTTP `Content-Encoding` compression and decompression. Currently at its initial stable release, version `1.0.0`, it emerged from the core `popsicle` project to offer a modular solution for handling content negotiation. The middleware automatically detects and adds appropriate `Accept-Encoding` headers to outgoing requests based on the Node.js runtime's supported compression algorithms (e.g., gzip, deflate, brotli). Upon receiving a response, it automatically decodes the body if a matching `Content-Encoding` header is present, simplifying client-side data handling. A key differentiator is its seamless integration within the `servie` and `popsicle` middleware composition pattern, abstracting away the complexities of manual compression negotiation and decompression. It operates passively if `Accept-Encoding` is already defined, allowing for manual override when necessary. Its release cadence is currently tied to user feedback and the broader `serviejs` ecosystem, with `1.0.0` marking its first standalone stable version.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install popsicle-content-encoding"],"cli":null},"imports":["import { contentEncoding } from 'popsicle-content-encoding';","import type { Options } from 'popsicle-content-encoding';","import type { Middleware } from 'servie';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { compose } from 'servie';\nimport popsicle from 'popsicle'; // popsicle is typically a default export\nimport { transport } from 'servie-http'; // Common transport for servie/popsicle\nimport { contentEncoding, Options } from 'popsicle-content-encoding'; // Import Options\n\nasync function fetchDataWithCompression() {\n  const API_URL = 'https://jsonplaceholder.typicode.com/posts/1'; // A public API for demonstration\n\n  // Configure contentEncoding middleware with specific options\n  // For example, to explicitly enable gzip and brotli, or disable certain types.\n  const encodingOptions: Options = {\n    gzip: true,\n    brotli: true,\n    deflate: false, // Explicitly disable deflate for this request\n  };\n\n  // Compose the middleware pipeline: contentEncoding first to handle headers, then transport\n  const requestMiddleware = compose([\n    contentEncoding(encodingOptions), // Pass the configuration options\n    transport()\n  ]);\n\n  const request = popsicle(API_URL, {\n    method: 'GET',\n    middleware: requestMiddleware, // Apply the composed middleware\n  });\n\n  try {\n    const response = await request.send();\n    console.log(`Status: ${response.status}`);\n    const contentEncodingHeader = response.headers.get('Content-Encoding');\n    console.log(`Content-Encoding Header (from response): ${contentEncodingHeader || 'N/A'}`);\n    console.log(`Response URL: ${response.url}`);\n\n    const data = await response.json(); // Body is automatically decoded by the middleware\n    console.log('Decoded data snippet (first 100 chars):', JSON.stringify(data).substring(0, 100) + '...');\n\n    if (typeof data === 'object' && data !== null) {\n      console.log('Successfully decoded JSON response and processed.');\n    } else {\n      console.warn('Response body might not be JSON or was not correctly decoded by the middleware.');\n    }\n  } catch (error: any) {\n    console.error('Error fetching data:', error.message);\n  }\n}\n\nfetchDataWithCompression();","lang":"typescript","description":"This quickstart demonstrates how to integrate `popsicle-content-encoding` into a Popsicle request pipeline, showing how to configure its `Options` to control `Content-Encoding` negotiation and decompression for HTTP requests.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}