From 4cd25170757c23be3513f315d926189aaec4ed3d Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 7 Nov 2024 18:02:55 +0000 Subject: [PATCH] chore: do not explode in browsers --- .../connection-encrypter-tls/package.json | 3 ++ .../src/tls.browser.ts | 27 +++++++++++++++++ packages/transport-tcp/src/tcp.browser.ts | 29 ------------------- 3 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 packages/connection-encrypter-tls/src/tls.browser.ts diff --git a/packages/connection-encrypter-tls/package.json b/packages/connection-encrypter-tls/package.json index 96da7eb540..40da424ec3 100644 --- a/packages/connection-encrypter-tls/package.json +++ b/packages/connection-encrypter-tls/package.json @@ -69,5 +69,8 @@ "protons": "^7.5.0", "sinon": "^18.0.0" }, + "browser": { + "./dist/src/tls.js": "./dist/src/tls.browser.js" + }, "sideEffects": false } diff --git a/packages/connection-encrypter-tls/src/tls.browser.ts b/packages/connection-encrypter-tls/src/tls.browser.ts new file mode 100644 index 0000000000..a7078170cf --- /dev/null +++ b/packages/connection-encrypter-tls/src/tls.browser.ts @@ -0,0 +1,27 @@ +import { serviceCapabilities } from '@libp2p/interface' +import { PROTOCOL } from './index.js' +import type { MultiaddrConnection, ConnectionEncrypter, SecuredConnection, SecureConnectionOptions } from '@libp2p/interface' +import type { Duplex } from 'it-stream-types' +import type { Uint8ArrayList } from 'uint8arraylist' + +export class TLS implements ConnectionEncrypter { + public protocol: string = PROTOCOL + + constructor () { + throw new Error('TLS encryption is not possible in browsers') + } + + readonly [Symbol.toStringTag] = '@libp2p/tls' + + readonly [serviceCapabilities]: string[] = [ + '@libp2p/connection-encryption' + ] + + async secureInbound > = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise> { + throw new Error('TLS encryption is not possible in browsers') + } + + async secureOutbound > = MultiaddrConnection> (conn: Stream, options?: SecureConnectionOptions): Promise> { + throw new Error('TLS encryption is not possible in browsers') + } +} diff --git a/packages/transport-tcp/src/tcp.browser.ts b/packages/transport-tcp/src/tcp.browser.ts index 079fda23e4..85da1cac58 100644 --- a/packages/transport-tcp/src/tcp.browser.ts +++ b/packages/transport-tcp/src/tcp.browser.ts @@ -1,32 +1,3 @@ -/** - * @packageDocumentation - * - * A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/) based on the TCP networking stack. - * - * @example - * - * ```TypeScript - * import { createLibp2p } from 'libp2p' - * import { tcp } from '@libp2p/tcp' - * import { multiaddr } from '@multiformats/multiaddr' - * - * const node = await createLibp2p({ - * transports: [ - * tcp() - * ] - * }) - * - * const ma = multiaddr('/ip4/123.123.123.123/tcp/1234') - * - * // dial a TCP connection, timing out after 10 seconds - * const connection = await node.dial(ma, { - * signal: AbortSignal.timeout(10_000) - * }) - * - * // use connection... - * ``` - */ - import { serviceCapabilities, transportSymbol } from '@libp2p/interface' import type { TCPComponents, TCPDialEvents, TCPMetrics, TCPOptions } from './index.js' import type { Logger, Connection, Transport, Listener } from '@libp2p/interface'