2018-01-30 18:00:02 +01:00
|
|
|
module.exports = {
|
|
|
|
REGEXP_INVALID_CLAIM : /[^A-Za-z0-9-]/g,
|
|
|
|
REGEXP_INVALID_CHANNEL: /[^A-Za-z0-9-@]/g,
|
|
|
|
REGEXP_ADDRESS : /^b(?=[^0OIl]{32,33})[0-9A-Za-z]{32,33}$/,
|
|
|
|
CHANNEL_CHAR : '@',
|
|
|
|
parseIdentifier : function (identifier) {
|
|
|
|
const componentsRegex = new RegExp(
|
|
|
|
'([^:$#/]*)' + // value (stops at the first separator or end)
|
|
|
|
'([:$#]?)([^/]*)' // modifier separator, modifier (stops at the first path separator or end)
|
|
|
|
);
|
|
|
|
const [proto, value, modifierSeperator, modifier] = componentsRegex
|
|
|
|
.exec(identifier)
|
|
|
|
.map(match => match || null);
|
|
|
|
|
|
|
|
// Validate and process name
|
|
|
|
if (!value) {
|
2018-02-01 04:12:54 +01:00
|
|
|
throw new Error(`Check your URL. No channel name provided before "${modifierSeperator}"`);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
const isChannel = value.startsWith(module.exports.CHANNEL_CHAR);
|
|
|
|
const channelName = isChannel ? value : null;
|
|
|
|
let claimId;
|
|
|
|
if (isChannel) {
|
|
|
|
if (!channelName) {
|
2018-02-01 04:12:54 +01:00
|
|
|
throw new Error('Check your URL. No channel name after "@".');
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
const nameBadChars = (channelName).match(module.exports.REGEXP_INVALID_CHANNEL);
|
|
|
|
if (nameBadChars) {
|
2018-02-01 04:12:54 +01:00
|
|
|
throw new Error(`Check your URL. Invalid characters in channel name: "${nameBadChars.join(', ')}".`);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
claimId = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate and process modifier
|
|
|
|
let channelClaimId;
|
|
|
|
if (modifierSeperator) {
|
|
|
|
if (!modifier) {
|
2018-02-01 04:12:54 +01:00
|
|
|
throw new Error(`Check your URL. No modifier provided after separator "${modifierSeperator}"`);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modifierSeperator === ':') {
|
|
|
|
channelClaimId = modifier;
|
|
|
|
} else {
|
2018-02-01 04:12:54 +01:00
|
|
|
throw new Error(`Check your URL. The "${modifierSeperator}" modifier is not currently supported`);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
isChannel,
|
|
|
|
channelName,
|
2018-02-01 23:29:33 +01:00
|
|
|
channelClaimId: channelClaimId || null,
|
|
|
|
claimId : claimId || null,
|
2018-01-30 18:00:02 +01:00
|
|
|
};
|
|
|
|
},
|
2018-02-01 04:12:54 +01:00
|
|
|
parseClaim: function (name) {
|
2018-01-30 18:00:02 +01:00
|
|
|
const componentsRegex = new RegExp(
|
2018-02-01 23:29:33 +01:00
|
|
|
'([^:$#/.]*)' + // name (stops at the first extension)
|
|
|
|
'([:$#.]?)([^/]*)' // extension separator, extension (stops at the first path separator or end)
|
2018-01-30 18:00:02 +01:00
|
|
|
);
|
2018-02-01 23:29:33 +01:00
|
|
|
const [proto, claimName, extensionSeperator, extension] = componentsRegex
|
2018-01-30 18:00:02 +01:00
|
|
|
.exec(name)
|
|
|
|
.map(match => match || null);
|
|
|
|
|
|
|
|
// Validate and process name
|
|
|
|
if (!claimName) {
|
2018-02-01 04:12:54 +01:00
|
|
|
throw new Error('Check your URL. No claim name provided before "."');
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
const nameBadChars = (claimName).match(module.exports.REGEXP_INVALID_CLAIM);
|
|
|
|
if (nameBadChars) {
|
2018-02-01 04:12:54 +01:00
|
|
|
throw new Error(`Check your URL. Invalid characters in claim name: "${nameBadChars.join(', ')}".`);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
2018-02-01 23:29:33 +01:00
|
|
|
// Validate and process extension
|
|
|
|
if (extensionSeperator) {
|
|
|
|
if (!extension) {
|
|
|
|
throw new Error(`Check your URL. No file extension provided after separator "${extensionSeperator}".`);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
2018-02-01 23:29:33 +01:00
|
|
|
if (extensionSeperator !== '.') {
|
|
|
|
throw new Error(`Check your URL. The "${extensionSeperator}" separator is not supported in the claim name.`);
|
2018-01-30 18:00:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
claimName,
|
2018-02-01 23:29:33 +01:00
|
|
|
extension: extension || null,
|
2018-01-30 18:00:02 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|