"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_buffer = require('../primordials/buffer.js');
const require_primordials_json = require('../primordials/json.js');
const require_compression_brotli = require('../compression/brotli.js');
const require_compression_gzip = require('../compression/gzip.js');

//#region src/http-request/response-reader.ts
/**
* @file Read a raw Node `IncomingMessage` into our `HttpResponse` shape. Split
*   out of `http-request/request.ts` for size hygiene. Useful when a caller
*   already has an `IncomingMessage` from code that bypasses `httpRequest()`
*   (e.g., multipart uploads via `http.request()` directly, or third-party HTTP
*   libraries) and wants the same fetch-like body accessors. The body is
*   transparently decompressed when the response carries a `Content-Encoding`
*   of `gzip` or `br` — the two encodings `httpRequest` advertises via
*   `Accept-Encoding`. Node's http client does not decompress on its own, so
*   without this step a compressed Socket API response would reach callers as
*   raw deflated bytes and fail JSON parsing.
*/
/**
* Decompress a response body per its `Content-Encoding`. Returns the input
* unchanged for `identity` or any unrecognized/absent encoding — we only
* decompress what `httpRequest` advertised support for (`gzip`, `br`).
*/
async function decodeBody(body, contentEncoding) {
	if (!contentEncoding || body.length === 0) return body;
	const encoding = (Array.isArray(contentEncoding) ? contentEncoding[0] : contentEncoding).trim().toLowerCase();
	if (encoding === "gzip") return await require_compression_gzip.decompressGzip(body);
	if (encoding === "br") return await require_compression_brotli.decompressBrotli(body);
	return body;
}
/**
* Read and buffer a client-side IncomingResponse into an HttpResponse.
*
* Useful when you have a raw response from code that bypasses `httpRequest()`
* (e.g., multipart form-data uploads via `http.request()`, or responses from
* third-party HTTP libraries) and need to convert it into the standard
* HttpResponse interface.
*
* @example
*   ;```typescript
*   const raw = await makeRawRequest('https://example.com/api')
*   const response = await readIncomingResponse(raw)
*   console.log(response.status, response.body.toString('utf8'))
*   ```
*/
async function readIncomingResponse(msg) {
	const chunks = [];
	for await (const chunk of msg) chunks.push(chunk);
	const body = await decodeBody(require_primordials_buffer.BufferConcat(chunks), msg.headers["content-encoding"]);
	const status = msg.statusCode ?? 0;
	const statusText = msg.statusMessage ?? "";
	return {
		arrayBuffer: () => body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength),
		body,
		headers: msg.headers,
		json: () => require_primordials_json.JSONParse(body.toString("utf8")),
		ok: status >= 200 && status < 300,
		rawResponse: msg,
		status,
		statusText,
		text: () => body.toString("utf8")
	};
}

//#endregion
exports.decodeBody = decodeBody;
exports.readIncomingResponse = readIncomingResponse;