Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const listRelations = async (relationType, relationFilter, args) => {
const searchFields = ['name', 'description'];
const { first = 200, after, orderBy, orderMode = 'asc', withCache = true, inferred = false } = args;
const { search, fromRole, fromId, toRole, toId, fromTypes = [], toTypes = [] } = args;
const { firstSeenStart, firstSeenStop, lastSeenStart, lastSeenStop, weights = [] } = args;
const offset = after ? cursorToOffset(after) : 0;
const isRelationOrderBy = orderBy && includes('.', orderBy);
// Handle relation type(s)
const relationToGet = relationType || 'stix_relation';
// 0 - Check if we can support the query by Elastic
const unsupportedOrdering = isRelationOrderBy && last(orderBy.split('.')) !== 'internal_id_key';
const supportedByCache = !unsupportedOrdering && !relationFilter && !inferred;
const useCache = !forceNoCache() && withCache && supportedByCache;
if (useCache) {
const filters = [];
const relationsMap = new Map();
if (fromId) {
filters.push({ key: 'connections.internal_id_key', values: [fromId] });
relationsMap.set(fromId, { alias: 'from', internalIdKey: fromId });
}
if (toId) {
filters.push({ key: 'connections.internal_id_key', values: [toId] });
export const elPaginate = async (indexName, options) => {
const {
first = 200,
after,
types = null,
filters = [],
isUser = null, // TODO @Sam refactor this to use filter
search = null,
orderBy = null,
orderMode = 'asc',
relationsMap = null,
connectionFormat = true // TODO @Julien Refactor that
} = options;
const offset = after ? cursorToOffset(after) : 0;
let must = [];
let mustnot = [];
let ordering = [];
if (search !== null && search.length > 0) {
// Try to decode before search
let decodedSearch;
try {
decodedSearch = decodeURIComponent(search);
} catch (e) {
decodedSearch = search;
}
const trimedSearch = decodedSearch.trim();
let finalSearch;
if (trimedSearch.startsWith('http://')) {
finalSearch = `"*${trimedSearch.replace('http://', '')}*"`;
} else if (trimedSearch.startsWith('https://')) {
export const paginate = (
query,
options,
ordered = true,
relationOrderingKey = null,
infer = false
) => {
try {
const { first = 200, after, orderBy = null, orderMode = 'asc' } = options;
const offset = after ? cursorToOffset(after) : 0;
const instanceKey = /match\s(?:\$|{\s\$)(\w+)[\s]/i.exec(query)[1]; // We need to resolve the key instance used in query.
const findRelationVariable = /\$(\w+)\((\w+):\$(\w+),[\s\w:$]+\)/i.exec(
query
);
const relationKey = findRelationVariable && findRelationVariable[1]; // Could be setup to get relation info
const orderingKey = relationOrderingKey
? `$${relationOrderingKey} has ${orderBy} $o;`
: `$${instanceKey} has ${orderBy} $o;`;
const count = getSingleValueNumber(
`${query}; ${ordered && orderBy ? orderingKey : ''} get $${instanceKey}${
relationKey ? `, $${relationKey}` : ''
}${ordered && orderBy ? ', $o' : ''}; count;`,
infer
);
const elements = getObjects(
`${query}; ${ordered && orderBy ? orderingKey : ''} get $${instanceKey}${
([result, via, viaRelation]) => {
const { first = 200, after } = args;
const offset = after ? cursorToOffset(after) : 0;
const globalCount =
result.globalCount +
sum(pluck('globalCount', via)) +
sum(pluck('globalCount', viaRelation));
let viaInstances = [];
forEach(n => {
viaInstances = concat(viaInstances, n.instances);
}, via);
let viaRelationInstances = [];
forEach(n => {
viaRelationInstances = concat(viaRelationInstances, n.instances);
}, viaRelation);
const instances = concat(viaInstances, viaRelationInstances);
const finalInstances = concat(result.instances, instances);
return buildPagination(first, offset, finalInstances, globalCount);
}
export const paginate = (indexName, options) => {
const {
first = 200,
after,
type = null,
types = null,
reportClass = null,
search = null,
orderBy = null,
orderMode = 'asc'
} = options;
const offset = after ? cursorToOffset(after) : 0;
let must = [];
let ordering = [];
if (search !== null && search.length > 0) {
let finalSearch;
if (search.includes('http://') || search.includes('https://')) {
finalSearch = `"*${search
.replace('http://', '')
.replace('https://', '')}*"`;
} else if (!search.startsWith('"')) {
finalSearch = `*${search}*`;
} else {
finalSearch = `${search}`;
}
must = append(
{
query_string: {