-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added topic validations for unsubscribe.
- Loading branch information
Showing
4 changed files
with
58 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
function validateSubscribeTopic (topic) { | ||
var end = topic.length - 1 | ||
var endMinus = end - 1 | ||
var slashInPreEnd = endMinus > 0 && topic.charCodeAt(endMinus) !== 47 | ||
if (topic.length === 0) { | ||
return new Error('impossible to subscribe to an empty topic') | ||
} | ||
for (var i = 0; i < topic.length; i++) { | ||
switch (topic.charCodeAt(i)) { | ||
case 35: | ||
var notAtTheEnd = i !== end | ||
if (notAtTheEnd || slashInPreEnd) { | ||
return new Error('# is only allowed in SUBSCRIBE in the last position') | ||
} | ||
break | ||
case 43: | ||
var pastChar = i < end - 1 && topic.charCodeAt(i + 1) !== 47 | ||
var preChar = i > 1 && topic.charCodeAt(i - 1) !== 47 | ||
if (pastChar || preChar) { | ||
return new Error('+ is only allowed in SUBSCRIBE between /') | ||
} | ||
break | ||
} | ||
} | ||
} | ||
|
||
module.exports.validateSubscribeTopic = validateSubscribeTopic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters