Skip to content

Commit

Permalink
Update more encoding uses
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Mar 4, 2020
1 parent f574dd3 commit 945990e
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import { format as formatUrl } from 'url';
// import { stringify } from 'querystring';
import { createBrowserHistory, History } from 'history';
import { decodeState, encodeState } from '../state_encoder';
import { getCurrentUrl, parseUrl, parseUrlHash } from './parse';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { parse, stringify } from 'querystring';
import { parse } from 'querystring';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { LocalUIFilterName } from '../../../../../../../plugins/apm/server/lib/ui_filters/local_ui_filters/config';
import { url } from '../../../../../../../../src/plugins/kibana_utils/public';
Expand All @@ -18,7 +18,7 @@ export function fromQuery(query: Record<string, any>) {
encodeURIComponent(value).replace(/%3A/g, ':')
);

return stringify(encodedQuery);
return url.makeUrlFromQuery(encodedQuery);
}

export type APMQueryParams = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const queryParamsFromObject = (params, encodeParams = false) => {

const paramStr = stringify(
params,
'&',
'=',
undefined,
undefined,
encodeParams
? {}
: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const analyticsJobExplorationRoute: MlRoute = {

const PageWrapper: FC<PageProps> = ({ location, deps }) => {
const { context } = useResolver('', undefined, deps.config, basicResolvers(deps));
const { _g }: Record<string, any> = parse(location.search);
const { _g }: Record<string, any> = parse(location.search.substring(1));

let globalState: any = null;
try {
Expand Down
5 changes: 3 additions & 2 deletions x-pack/legacy/plugins/ml/public/application/util/url_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useCallback } from 'react';
import { isEqual } from 'lodash';
import { decode, encode } from 'rison-node';
import { useHistory, useLocation } from 'react-router-dom';
import { url } from '../../../../../../../src/plugins/kibana_utils/common/url';

import { Dictionary } from '../../../common/types/common';

Expand Down Expand Up @@ -83,7 +84,7 @@ export const useUrlState = (accessor: string): UrlState => {
}

try {
const oldLocationSearch = stringify(parsedQueryString);
const oldLocationSearch = url.makeUrlFromQuery(parsedQueryString);

Object.keys(urlState).forEach(a => {
if (isRisonSerializationRequired(a)) {
Expand All @@ -92,7 +93,7 @@ export const useUrlState = (accessor: string): UrlState => {
parsedQueryString[a] = urlState[a];
}
});
const newLocationSearch = stringify(parsedQueryString);
const newLocationSearch = url.makeUrlFromQuery(parsedQueryString);

if (oldLocationSearch !== newLocationSearch) {
history.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const MlHostConditionalContainer = React.memo<MlHostConditionalProps>(({
if (queryStringDecoded.query != null) {
queryStringDecoded.query = replaceKQLParts(queryStringDecoded.query);
}
const reEncoded = stringify(urlUtils.encodeQuery(queryStringDecoded));
const reEncoded = urlUtils.makeUrlFromQuery(queryStringDecoded);
return <Redirect to={`/${SiemPageName.hosts}?${reEncoded}`} />;
}}
/>
Expand All @@ -49,15 +49,13 @@ export const MlHostConditionalContainer = React.memo<MlHostConditionalProps>(({
params: { hostName },
},
}) => {
const queryStringDecoded = parse(location.search.substring(1), {
sort: false,
}) as Required<QueryStringType>;
const queryStringDecoded = parse(location.search.substring(1)) as Required<QueryStringType>;

if (queryStringDecoded.query != null) {
queryStringDecoded.query = replaceKQLParts(queryStringDecoded.query);
}
if (emptyEntity(hostName)) {
const reEncoded = stringify(urlUtils.encodeQuery(queryStringDecoded));
const reEncoded = urlUtils.makeUrlFromQuery(queryStringDecoded);

return (
<Redirect to={`/${SiemPageName.hosts}/${HostsTableType.anomalies}?${reEncoded}`} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

// eslint-disable-next-line import/no-nodejs-modules
import { parse, stringify } from 'querystring';
import { parse } from 'querystring';
import React from 'react';

import { Redirect, Route, Switch, RouteComponentProps } from 'react-router-dom';
Expand Down Expand Up @@ -37,7 +37,7 @@ export const MlNetworkConditionalContainer = React.memo<MlNetworkConditionalProp
queryStringDecoded.query = replaceKQLParts(queryStringDecoded.query);
}

const reEncoded = stringify(urlUtils.encodeQuery(queryStringDecoded));
const reEncoded = urlUtils.makeUrlFromQuery(queryStringDecoded);

return <Redirect to={`/${SiemPageName.network}?${reEncoded}`} />;
}}
Expand All @@ -57,7 +57,7 @@ export const MlNetworkConditionalContainer = React.memo<MlNetworkConditionalProp
}

if (emptyEntity(ip)) {
const reEncoded = stringify(urlUtils.encodeQuery(queryStringDecoded));
const reEncoded = urlUtils.makeUrlFromQuery(queryStringDecoded);

return <Redirect to={`/${SiemPageName.network}?${reEncoded}`} />;
} else if (multipleEntities(ip)) {
Expand All @@ -67,10 +67,10 @@ export const MlNetworkConditionalContainer = React.memo<MlNetworkConditionalProp
ips,
queryStringDecoded.query || ''
);
const reEncoded = stringify(urlUtils.encodeQuery(queryStringDecoded));
const reEncoded = urlUtils.makeUrlFromQuery(queryStringDecoded);
return <Redirect to={`/${SiemPageName.network}?${reEncoded}`} />;
} else {
const reEncoded = stringify(urlUtils.encodeQuery(queryStringDecoded));
const reEncoded = urlUtils.makeUrlFromQuery(queryStringDecoded);
return <Redirect to={`/${SiemPageName.network}/ip/${ip}?${reEncoded}`} />;
}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,18 @@ export const replaceStateKeyInQueryString = <T>(stateKey: string, urlState: T) =
if (urlState == null || (typeof urlState === 'string' && urlState === '')) {
delete previousQueryValues[stateKey];

return stringify(url.encodeQuery(previousQueryValues));
return url.makeUrlFromQuery(previousQueryValues);
}

// ಠ_ಠ Code was copied from x-pack/legacy/plugins/infra/public/utils/url_state.tsx ಠ_ಠ
// Remove this if these utilities are promoted to kibana core
const encodedUrlState =
typeof urlState !== 'undefined' ? encodeRisonUrlState(urlState) : undefined;

return stringify(
url.encodeQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
}),
{ sort: false, encode: false }
);
return url.makeUrlFromQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
});
};

export const replaceQueryStringInLocation = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { FormattedMessage } from '@kbn/i18n/react';
import React from 'react';
import { encode } from 'rison-node';
import url from 'url';
import { stringify } from 'querystring';
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public';
import { TimeRange } from '../../../../common/http_api/shared/time_range';
import { url as urlUtils } from '../../../../../../../src/plugins/kibana_utils/public';
Expand Down Expand Up @@ -62,7 +61,7 @@ const getOverallAnomalyExplorerLink = (pathname: string, jobId: string, timeRang
},
});

const hash = `/explorer?${stringify(urlUtils.encodeQuery({ _g }))}`;
const hash = `/explorer?${urlUtils.makeUrlFromQuery({ _g })}`;

return url.format({
pathname,
Expand Down Expand Up @@ -95,7 +94,7 @@ const getPartitionSpecificSingleMetricViewerLink = (
},
});

const hash = `/timeseriesexplorer?${stringify(urlUtils.encodeQuery({ _g, _a }))}`;
const hash = `/timeseriesexplorer?${urlUtils.makeUrlFromQuery({ _g, _a })}`;

return url.format({
pathname,
Expand Down
10 changes: 4 additions & 6 deletions x-pack/plugins/infra/public/utils/url_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,10 @@ export const replaceStateKeyInQueryString = <UrlState extends any>(
const encodedUrlState =
typeof urlState !== 'undefined' ? encodeRisonUrlState(urlState) : undefined;

return stringify(
url.encodeQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
})
);
return url.makeUrlFromQuery({
...previousQueryValues,
[stateKey]: encodedUrlState,
});
};

const replaceQueryStringInLocation = (location: Location, queryString: string): Location => {
Expand Down

0 comments on commit 945990e

Please sign in to comment.