USAGE EXAMPLES
// Example 1: HTTPS Server with Pipeconst { Context, Pipe } = import.meta.use('ssl');const { TCP } = import.meta.use('streams');const Context = new Context({ mode: "server", cert: certPem, key: keyPem, alpn: ["h2", "http/1.1"]});const server = new TCP();server.bind("0.0.0.0", 8443);server.listen(128);server.accept().then(conn => { const pipe = new Pipe(Context); conn.read().then(data => { pipe.feed(data); pipe.handshake(); const output = pipe.getOutput(); if (output) conn.write(output); });});// Example 2: HTTPS Clientconst { Context, Pipe } = import.meta.use('ssl');const { TCP } = import.meta.use('streams');const Context2 = new Context({ mode: "client", verify: true, ca: caPem});const conn2 = new TCP();await conn2.connect("example.com", 443);const pipe2 = new Pipe(Context2, { servername: "example.com" });// Start handshakepipe2.handshake();const handshake2 = pipe2.getOutput();if (handshake2) await conn2.write(handshake2);// Complete handshakewhile (!pipe2.handshakeComplete) { const response2 = await conn2.read(); pipe2.feed(response2); pipe2.handshake(); const output2 = pipe2.getOutput(); if (output2) await conn2.write(output2);}// Send HTTP requestconst request2 = new TextEncoder().encode("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");pipe2.write(request2);const encrypted2 = pipe2.getOutput();await conn2.write(encrypted2);// Example 3: Generate Self-Signed Certificateconst { Context, Pipe } = import.meta.use('ssl');const { TCP } = import.meta.use('streams');const { cert, key } = createSelfSignedCert({ commonName: "localhost", days: 365});const context3 = new Context({ mode: "server", cert, key});// Example 4: Inspect Peer Certificateconst pipe4 = new Pipe(clientContext, { servername: "example.com" });// ... perform handshake ...if (pipe4.handshakeComplete) { const cert4 = pipe4.certificate; console.log("Subject:", cert4.subject); console.log("Issuer:", cert4.issuer); console.log("Valid:", cert4.validFrom, "to", cert4.validTo); console.log("SANs:", cert4.subjectAltNames); console.log("Fingerprint:", cert4.fingerprint256); const verify4 = pipe4.verifyResult; if (!verify4.ok) { console.error("Certificate verification failed:", verify4.error); }}// Example 5: Advanced SSL Context Configurationimport { Context } from "@tjs/ssl";const context5 = new Context({ mode: "server", cert: certPem, key: keyPem, minVersion: "TLSv1.2", maxVersion: "TLSv1.3", ciphers: "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384", alpn: ["h2", "http/1.1"], sessionTickets: true, sessionCache: true, compression: false, dhparam: "dhparam.pem", ecdhCurve: "prime256v1"}); Copy
// Example 1: HTTPS Server with Pipeconst { Context, Pipe } = import.meta.use('ssl');const { TCP } = import.meta.use('streams');const Context = new Context({ mode: "server", cert: certPem, key: keyPem, alpn: ["h2", "http/1.1"]});const server = new TCP();server.bind("0.0.0.0", 8443);server.listen(128);server.accept().then(conn => { const pipe = new Pipe(Context); conn.read().then(data => { pipe.feed(data); pipe.handshake(); const output = pipe.getOutput(); if (output) conn.write(output); });});// Example 2: HTTPS Clientconst { Context, Pipe } = import.meta.use('ssl');const { TCP } = import.meta.use('streams');const Context2 = new Context({ mode: "client", verify: true, ca: caPem});const conn2 = new TCP();await conn2.connect("example.com", 443);const pipe2 = new Pipe(Context2, { servername: "example.com" });// Start handshakepipe2.handshake();const handshake2 = pipe2.getOutput();if (handshake2) await conn2.write(handshake2);// Complete handshakewhile (!pipe2.handshakeComplete) { const response2 = await conn2.read(); pipe2.feed(response2); pipe2.handshake(); const output2 = pipe2.getOutput(); if (output2) await conn2.write(output2);}// Send HTTP requestconst request2 = new TextEncoder().encode("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");pipe2.write(request2);const encrypted2 = pipe2.getOutput();await conn2.write(encrypted2);// Example 3: Generate Self-Signed Certificateconst { Context, Pipe } = import.meta.use('ssl');const { TCP } = import.meta.use('streams');const { cert, key } = createSelfSignedCert({ commonName: "localhost", days: 365});const context3 = new Context({ mode: "server", cert, key});// Example 4: Inspect Peer Certificateconst pipe4 = new Pipe(clientContext, { servername: "example.com" });// ... perform handshake ...if (pipe4.handshakeComplete) { const cert4 = pipe4.certificate; console.log("Subject:", cert4.subject); console.log("Issuer:", cert4.issuer); console.log("Valid:", cert4.validFrom, "to", cert4.validTo); console.log("SANs:", cert4.subjectAltNames); console.log("Fingerprint:", cert4.fingerprint256); const verify4 = pipe4.verifyResult; if (!verify4.ok) { console.error("Certificate verification failed:", verify4.error); }}// Example 5: Advanced SSL Context Configurationimport { Context } from "@tjs/ssl";const context5 = new Context({ mode: "server", cert: certPem, key: keyPem, minVersion: "TLSv1.2", maxVersion: "TLSv1.3", ciphers: "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384", alpn: ["h2", "http/1.1"], sessionTickets: true, sessionCache: true, compression: false, dhparam: "dhparam.pem", ecdhCurve: "prime256v1"});
USAGE EXAMPLES
Example