2017-12-07 03:49:05 +01:00
|
|
|
module.exports = {
|
2017-12-07 18:28:44 +01:00
|
|
|
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) {
|
2017-12-07 03:49:05 +01:00
|
|
|
const componentsRegex = new RegExp(
|
|
|
|
'([^:$#/]*)' + // value (stops at the first separator or end)
|
|
|
|
'([:$#]?)([^/]*)' // modifier separator, modifier (stops at the first path separator or end)
|
|
|
|
);
|
2018-08-02 02:31:20 +02:00
|
|
|
const [, value, modifierSeperator, modifier] = componentsRegex
|
2017-12-07 03:49:05 +01:00
|
|
|
.exec(identifier)
|
|
|
|
.map(match => match || null);
|
|
|
|
|
|
|
|
// Validate and process name
|
2017-12-19 02:57:31 +01:00
|
|
|
if (!value) {
|
|
|
|
throw new Error(`Check your url. No channel name provided before "${modifierSeperator}"`);
|
|
|
|
}
|
2017-12-07 03:49:05 +01:00
|
|
|
const isChannel = value.startsWith(module.exports.CHANNEL_CHAR);
|
|
|
|
const channelName = isChannel ? value : null;
|
|
|
|
let claimId;
|
|
|
|
if (isChannel) {
|
|
|
|
if (!channelName) {
|
|
|
|
throw new Error('No channel name after @.');
|
|
|
|
}
|
2017-12-07 18:28:44 +01:00
|
|
|
const nameBadChars = (channelName).match(module.exports.REGEXP_INVALID_CHANNEL);
|
2017-12-07 03:49:05 +01:00
|
|
|
if (nameBadChars) {
|
|
|
|
throw new Error(`Invalid characters in channel name: ${nameBadChars.join(', ')}.`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
claimId = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate and process modifier
|
|
|
|
let channelClaimId;
|
|
|
|
if (modifierSeperator) {
|
|
|
|
if (!modifier) {
|
2017-12-19 02:57:31 +01:00
|
|
|
throw new Error(`No modifier provided after separator "${modifierSeperator}"`);
|
2017-12-07 03:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modifierSeperator === ':') {
|
|
|
|
channelClaimId = modifier;
|
|
|
|
} else {
|
2017-12-19 02:57:31 +01:00
|
|
|
throw new Error(`The "${modifierSeperator}" modifier is not currently supported`);
|
2017-12-07 03:49:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
isChannel,
|
|
|
|
channelName,
|
|
|
|
channelClaimId,
|
|
|
|
claimId,
|
|
|
|
};
|
|
|
|
},
|
2018-02-01 04:12:54 +01:00
|
|
|
parseClaim: function (claim) {
|
2017-12-07 03:49:05 +01:00
|
|
|
const componentsRegex = new RegExp(
|
|
|
|
'([^:$#/.]*)' + // name (stops at the first modifier)
|
|
|
|
'([:$#.]?)([^/]*)' // modifier separator, modifier (stops at the first path separator or end)
|
|
|
|
);
|
2018-08-02 02:31:20 +02:00
|
|
|
const [, claimName, modifierSeperator, modifier] = componentsRegex
|
2018-02-01 04:12:54 +01:00
|
|
|
.exec(claim)
|
2017-12-07 03:49:05 +01:00
|
|
|
.map(match => match || null);
|
|
|
|
|
|
|
|
// Validate and process name
|
|
|
|
if (!claimName) {
|
|
|
|
throw new Error('No claim name provided before .');
|
|
|
|
}
|
2017-12-07 18:28:44 +01:00
|
|
|
const nameBadChars = (claimName).match(module.exports.REGEXP_INVALID_CLAIM);
|
2017-12-07 03:49:05 +01:00
|
|
|
if (nameBadChars) {
|
|
|
|
throw new Error(`Invalid characters in claim name: ${nameBadChars.join(', ')}.`);
|
|
|
|
}
|
|
|
|
// Validate and process modifier
|
|
|
|
if (modifierSeperator) {
|
|
|
|
if (!modifier) {
|
|
|
|
throw new Error(`No file extension provided after separator ${modifierSeperator}.`);
|
|
|
|
}
|
|
|
|
if (modifierSeperator !== '.') {
|
|
|
|
throw new Error(`The ${modifierSeperator} modifier is not supported in the claim name`);
|
|
|
|
}
|
|
|
|
}
|
2018-02-01 04:12:54 +01:00
|
|
|
// return results
|
2017-12-07 03:49:05 +01:00
|
|
|
return {
|
|
|
|
claimName,
|
2018-08-02 02:31:20 +02:00
|
|
|
extension: modifier || null,
|
2018-02-01 04:12:54 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
parseModifier: function (claim) {
|
|
|
|
const componentsRegex = new RegExp(
|
|
|
|
'([^:$#/.]*)' + // name (stops at the first modifier)
|
|
|
|
'([:$#.]?)([^/]*)' // modifier separator, modifier (stops at the first path separator or end)
|
|
|
|
);
|
2018-08-02 02:31:20 +02:00
|
|
|
const [ , , modifierSeperator ] = componentsRegex
|
2018-02-01 04:12:54 +01:00
|
|
|
.exec(claim)
|
|
|
|
.map(match => match || null);
|
2018-08-02 02:31:20 +02:00
|
|
|
|
2018-02-01 04:12:54 +01:00
|
|
|
// Validate and process modifier
|
|
|
|
let hasFileExtension = false;
|
|
|
|
if (modifierSeperator) {
|
|
|
|
hasFileExtension = true;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
hasFileExtension,
|
2017-12-07 03:49:05 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|