### Initialize CurlTransport with Wisp Source: https://github.com/mercuryworkshop/curltransport/blob/master/README.md Demonstrates the basic setup for CurlTransport using Wisp as the proxy protocol. It involves importing BareMuxConnection and setting the transport path along with the Wisp websocket endpoint. ```javascript import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; const conn = new BareMuxConnection("/path/to/baremux/worker.js"); await conn.setTransport("/path/to/curltransport/index.mjs", [{ websocket: "wss://example.com/wisp/" }]); ``` -------------------------------- ### Complete CurlTransport Integration with BareMux (JavaScript) Source: https://context7.com/mercuryworkshop/curltransport/llms.txt Provides a full example of setting up and using the CurlTransport with BareMux. This includes initializing the connection, configuring the transport with various options, making HTTP requests using the fetch API, and handling potential errors. It demonstrates concurrent request execution and error checking. ```javascript import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; async function setupProxyTransport() { try { // Initialize bare-mux connection const conn = new BareMuxConnection("/baremux/worker.js"); // Configure transport with all options await conn.setTransport("/curltransport/index.mjs", [{ websocket: "wss://proxy.example.com/wisp/", transport: "wisp", // or "wsproxy" connections: [60, 50, 6], // Connection pool settings proxy: undefined // Optional: "socks5h://127.0.0.1:1080" }]); console.log("Transport configured successfully"); // Make an HTTP request const response = await fetch("https://api.example.com/data", { method: "GET", headers: { "User-Agent": "Mozilla/5.0", "Accept": "application/json" } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); console.log("API data:", data); // Make multiple concurrent requests const urls = [ "https://example.com/page1", "https://example.com/page2", "https://example.com/page3" ]; const results = await Promise.allSettled( urls.map(url => fetch(url).then(r => r.text())) ); results.forEach((result, index) => { if (result.status === "fulfilled") { console.log(`Page ${index + 1} loaded: ${result.value.length} bytes`); } else { console.error(`Page ${index + 1} failed:`, result.reason); } }); } catch (error) { console.error("Transport setup failed:", error.message); // Handle specific error cases if (error.message.includes("trailing forward slash")) { console.error("WebSocket URL must end with /"); } else if (error.message.includes("ws:// or wss://")) { console.error("WebSocket URL must use ws:// or wss:// protocol"); } throw error; } } // Run the setup setupProxyTransport(); ``` -------------------------------- ### Setup CurlTransport with Wisp for Multiplexed WebSockets Source: https://context7.com/mercuryworkshop/curltransport/llms.txt This snippet demonstrates how to initialize a BareMuxConnection and configure CurlTransport to use the Wisp protocol for multiplexed WebSocket connections. It then shows how to make a basic HTTPS request through the established encrypted transport. Dependencies include '@mercuryworkshop/bare-mux'. ```javascript import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; // Initialize bare-mux connection const conn = new BareMuxConnection("/baremux/worker.js"); // Configure CurlTransport with Wisp endpoint await conn.setTransport("/curltransport/index.mjs", [{ websocket: "wss://proxy.example.com/wisp/" }]); // Now make requests through the encrypted transport fetch("https://example.com") .then(response => response.text()) .then(data => console.log(data)) .catch(error => console.error("Request failed:", error)); ``` -------------------------------- ### Configure CurlTransport Proxy Chaining Source: https://context7.com/mercuryworkshop/curltransport/llms.txt This example illustrates how to configure CurlTransport to route traffic through an additional proxy server, such as SOCKS5, SOCKS4a, or HTTP. This is useful for enhancing anonymity or accessing restricted networks. Dependencies include '@mercuryworkshop/bare-mux'. ```javascript import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; const conn = new BareMuxConnection("/baremux/worker.js"); // Chain through a SOCKS5 proxy await conn.setTransport("/curltransport/index.mjs", [{ websocket: "wss://proxy.example.com/wisp/", proxy: "socks5h://socks-proxy.example.com:1080" }]); // Supported proxy types: socks5h, socks4a, http // Traffic flow: Client -> Wisp -> SOCKS5 Proxy -> Destination fetch("https://restricted-site.example.com") .then(response => response.text()) .then(data => console.log("Response through chained proxies:", data)) .catch(error => console.error("Proxy chain failed:", error)); ``` -------------------------------- ### Setup CurlTransport with wsproxy for Separate WebSockets Source: https://context7.com/mercuryworkshop/curltransport/llms.txt This code configures CurlTransport to utilize the wsproxy protocol, establishing a separate WebSocket connection for each TCP stream instead of multiplexing. It's useful for scenarios where non-multiplexed connections are preferred. Dependencies include '@mercuryworkshop/bare-mux'. ```javascript import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; const conn = new BareMuxConnection("/baremux/worker.js"); // Use wsproxy transport (non-multiplexed) await conn.setTransport("/curltransport/index.mjs", [{ websocket: "wss://proxy.example.com/wsproxy/", transport: "wsproxy" }]); // Requests now use separate WebSocket connections fetch("https://api.example.com/data") .then(response => response.json()) .then(json => console.log("API Response:", json)) .catch(error => console.error("API call failed:", error)); ``` -------------------------------- ### Establish WebSocket Connections with LibcurlClient (JavaScript) Source: https://context7.com/mercuryworkshop/curltransport/llms.txt Illustrates how to establish WebSocket connections using the internal LibcurlClient via the BareTransport interface. This function shows how to initiate a connection, send messages, and handle incoming data and closure events. It's designed for internal use by bare-mux. ```javascript function exampleWebSocketUsage() { const transport = new LibcurlClient({ websocket: "wss://proxy.example.com/wisp/" }); transport.init().then(() => { // Connect to a WebSocket server through the transport const [sendMessage, closeConnection] = transport.connect( new URL("wss://echo.example.com/socket"), ["chat", "binary"], // Subprotocols { "authorization": ["Bearer token123"] }, // Request headers (protocol) => { console.log("WebSocket opened with protocol:", protocol); sendMessage("Hello, server!"); }, (data) => { console.log("Received message:", data); if (data instanceof ArrayBuffer) { console.log("Binary data length:", data.byteLength); } }, (code, reason) => { console.log("WebSocket closed:", code, reason); }, (error) => { console.error("WebSocket error:", error); } ); // Send messages setTimeout(() => { sendMessage(new Uint8Array([1, 2, 3, 4, 5]).buffer); }, 1000); // Close connection after 5 seconds setTimeout(() => { closeConnection(1000, "Normal closure"); }, 5000); }); } ``` -------------------------------- ### Handle HTTP Requests with LibcurlClient (JavaScript) Source: https://context7.com/mercuryworkshop/curltransport/llms.txt Demonstrates how to make HTTP POST requests using the internal LibcurlClient, which is part of the BareTransport interface. It includes setting headers, sending a JSON body, and processing the response stream. This is intended for internal use and not direct user invocation. ```javascript async function exampleRequestUsage() { const transport = new LibcurlClient({ websocket: "wss://proxy.example.com/wisp/" }); await transport.init(); // Make a POST request with headers and body const response = await transport.request( new URL("https://api.example.com/users"), "POST", JSON.stringify({ name: "Alice", email: "alice@example.com" }), { "content-type": ["application/json"], "authorization": ["Bearer token123"] }, undefined // AbortSignal ); console.log("Status:", response.status); console.log("Headers:", response.headers); // Response body is a ReadableStream const reader = response.body.getReader(); const chunks = []; while (true) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); } const bodyText = new TextDecoder().decode( new Uint8Array( chunks.reduce((acc, chunk) => [...acc, ...chunk], []) ) ); console.log("Response body:", bodyText); } ``` -------------------------------- ### Initialize CurlTransport with wsproxy Source: https://github.com/mercuryworkshop/curltransport/blob/master/README.md Illustrates how to configure CurlTransport to use wsproxy instead of Wisp. This is achieved by specifying the 'wsproxy' transport type in the options, which handles each TCP connection as a separate WebSocket. ```javascript await conn.setTransport("/path/to/curltransport/index.mjs", [{ websocket: "wss://example.com/wsproxy/", transport: "wsproxy" }]); ``` -------------------------------- ### Configure CurlTransport Connection Limits Source: https://github.com/mercuryworkshop/curltransport/blob/master/README.md Shows how to set the maximum number of open connections for libcurl.js within CurlTransport. The 'connections' option accepts an array of three integers to control the hard limit, cache limit, and per-host connection limit. ```javascript conn.setTransport("/libcurl/index.mjs", [{ websocket: "wss://example.com/wsproxy/", connections: [30, 20, 1] } ]) ``` -------------------------------- ### Configure CurlTransport Connection Pooling Source: https://context7.com/mercuryworkshop/curltransport/llms.txt This snippet shows how to customize connection pooling settings for libcurl.js within CurlTransport. You can set limits for active connections, cache size, and connections per host to optimize performance and resource usage. Dependencies include '@mercuryworkshop/bare-mux'. ```javascript import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; const conn = new BareMuxConnection("/baremux/worker.js"); // Configure connection limits: [max_active, cache_size, per_host] // Example: 30 max active, 20 cache size, 1 connection per host await conn.setTransport("/curltransport/index.mjs", [{ websocket: "wss://proxy.example.com/wisp/", connections: [30, 20, 1] }]); // Useful for limiting resource usage // Default values: [60, 50, 6] // Make requests with custom connection pooling Promise.all([ fetch("https://site1.example.com"), fetch("https://site2.example.com"), fetch("https://site3.example.com") ]) .then(responses => Promise.all(responses.map(r => r.text()))) .then(results => console.log("All requests completed:", results.length)) .catch(error => console.error("Batch request failed:", error)); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.