Skip to content

Commit b948e53

Browse files
refactor: generate unique names/urls locally (#1375)
* refactor: generate unique names/urls locally * refactor: add counter to differentiate instead of infinite underscores
1 parent 54bc0ff commit b948e53

4 files changed

Lines changed: 23 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,16 @@ export default class CoapServer implements ProtocolServer {
158158
}
159159

160160
private createThingUrlPath(thing: ExposedThing) {
161-
const urlPath = slugify(thing.title, { lower: true });
161+
let urlPath = slugify(thing.title, { lower: true });
162162

163+
// avoid URL clashes
163164
if (this.things.has(urlPath)) {
164-
return Helpers.generateUniqueName(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: 7 additions & 1 deletion
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+
// avoid URL clashes
272273
if (this.things.has(urlPath)) {
273-
urlPath = Helpers.generateUniqueName(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: 7 additions & 1 deletion
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+
// avoid name clashes
161162
if (this.thingNames.has(urlPath)) {
162-
urlPath = Helpers.generateUniqueName(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) {

packages/core/src/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export default class Helpers implements Resolver {
124124
return address;
125125
}
126126

127+
/** @deprecated see https://github.com/eclipse-thingweb/node-wot/issues/1351 */
127128
public static generateUniqueName(name: string): string {
128129
const suffix = name.match(/.+_([0-9]+)$/);
129130
if (suffix !== null) {

0 commit comments

Comments
 (0)