From fb14b880d1b1b278e1e826bb0d9939db358e6ccc Mon Sep 17 00:00:00 2001 From: Chinmay Kousik Date: Tue, 3 Jan 2023 22:12:56 +0530 Subject: [PATCH] fix: remove uuid dependency (#68) Removes the dependency on uuid. Use a simpler random string generator instead. --- package.json | 4 +--- src/transport.ts | 5 ++--- src/util.ts | 3 +++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index b7325aa..88a2480 100644 --- a/package.json +++ b/package.json @@ -154,15 +154,13 @@ "multihashes": "^4.0.3", "p-defer": "^4.0.0", "uint8arraylist": "^2.3.3", - "uint8arrays": "^4.0.2", - "uuid": "^9.0.0" + "uint8arrays": "^4.0.2" }, "devDependencies": { "@libp2p/interface-mocks": "^8.0.1", "@libp2p/peer-id-factory": "^1.0.19", "@protobuf-ts/plugin": "^2.8.0", "@protobuf-ts/protoc": "^2.8.0", - "@types/uuid": "^8.3.4", "aegir": "^37.6.6", "it-first": "^2.0.0", "libp2p": "^0.40.0" diff --git a/src/transport.ts b/src/transport.ts index 6fc10bd..897f9ca 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -7,16 +7,15 @@ import * as p from '@libp2p/peer-id' import type { Multiaddr } from '@multiformats/multiaddr' import * as multihashes from 'multihashes' import defer from 'p-defer' -import { v4 as genUuid } from 'uuid' import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' import { concat } from 'uint8arrays/concat' - import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument } from './error.js' import { WebRTCMultiaddrConnection } from './maconn.js' import { DataChannelMuxerFactory } from './muxer.js' import type { WebRTCDialOptions } from './options.js' import * as sdp from './sdp.js' import { WebRTCStream } from './stream.js' +import { genUfrag } from './util.js' const log = logger('libp2p:webrtc:transport') @@ -142,7 +141,7 @@ export class WebRTCTransport implements Transport { dataChannelOpenPromise.reject(dataChannelError('data', error)) } - const ufrag = 'libp2p+webrtc+v1/' + genUuid().replaceAll('-', '') + const ufrag = 'libp2p+webrtc+v1/' + genUfrag(32) // Create offer and munge sdp with ufrag = pwd. This allows the remote to // respond to STUN messages without performing an actual SDP exchange. diff --git a/src/util.ts b/src/util.ts index 91fbfcc..0270d36 100644 --- a/src/util.ts +++ b/src/util.ts @@ -3,3 +3,6 @@ export const nopSource = { } export const nopSink = async (_: any) => {} + +const charset = Array.from('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') +export const genUfrag = (len: number): string => [...Array(len)].map(() => charset.at(Math.floor(Math.random() * charset.length))).join('')