Skip to content

Commit 85ca726

Browse files
refactor: add counter to differentiate instead of infinite underscores
1 parent a85e54e commit 85ca726

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

packages/binding-coap/src/coap-server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,14 @@ export default class CoapServer implements ProtocolServer {
160160
private createThingUrlPath(thing: ExposedThing) {
161161
let urlPath = slugify(thing.title, { lower: true });
162162

163-
while (this.things.has(urlPath)) {
164-
urlPath += "_";
163+
// avoid URL clashes
164+
if (this.things.has(urlPath)) {
165+
let uniqueUrlPath;
166+
let nameClashCnt = 2;
167+
do {
168+
uniqueUrlPath = urlPath + "_" + nameClashCnt++;
169+
} while (this.things.has(uniqueUrlPath));
170+
urlPath = uniqueUrlPath;
165171
}
166172

167173
return urlPath;

packages/binding-http/src/http-server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,14 @@ export default class HttpServer implements ProtocolServer {
269269
public async expose(thing: ExposedThing, tdTemplate: WoT.ExposedThingInit = {}): Promise<void> {
270270
let urlPath = slugify(thing.title, { lower: true });
271271

272-
while (this.things.has(urlPath)) {
273-
urlPath += "_";
272+
// avoid URL clashes
273+
if (this.things.has(urlPath)) {
274+
let uniqueUrlPath;
275+
let nameClashCnt = 2;
276+
do {
277+
uniqueUrlPath = urlPath + "_" + nameClashCnt++;
278+
} while (this.things.has(uniqueUrlPath));
279+
urlPath = uniqueUrlPath;
274280
}
275281

276282
if (this.getPort() !== -1) {

packages/binding-websockets/src/ws-server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,14 @@ export default class WebSocketServer implements ProtocolServer {
158158
public expose(thing: ExposedThing): Promise<void> {
159159
let urlPath = slugify(thing.title, { lower: true });
160160

161-
while (this.thingNames.has(urlPath)) {
162-
urlPath += "_";
161+
// avoid name clashes
162+
if (this.thingNames.has(urlPath)) {
163+
let uniqueUrlPath;
164+
let nameClashCnt = 2;
165+
do {
166+
uniqueUrlPath = urlPath + "_" + nameClashCnt++;
167+
} while (this.thingNames.has(uniqueUrlPath));
168+
urlPath = uniqueUrlPath;
163169
}
164170

165171
if (this.getPort() !== -1) {

0 commit comments

Comments
 (0)