Skip to content

Commit f2499c3

Browse files
committed
Remove deprecated url dependency
1 parent 48de1d8 commit f2499c3

File tree

6 files changed

+49
-60
lines changed

6 files changed

+49
-60
lines changed

tests/spec/features/sharing_with_others_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
# Need to be logged in to URLO for this link to work
4646
expect(urlo_link).to match(%r{https://users.rust-lang.org/new-topic})
47-
expect(urlo_link).to match(%{automated%20test})
47+
expect(urlo_link).to include('automated+test')
4848
end
4949

5050
def editor

ui/frontend/actions.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fetch from 'isomorphic-fetch';
22
import { ThunkAction as ReduxThunkAction } from 'redux-thunk';
3-
import url, { UrlObject } from 'url';
43
import { z } from 'zod';
54

65
import {
@@ -9,6 +8,7 @@ import {
98
getCrateType,
109
runAsTest,
1110
useWebsocketSelector,
11+
baseUrlSelector,
1212
} from './selectors';
1313
import State from './state';
1414
import {
@@ -35,14 +35,14 @@ import {
3535
} from './types';
3636

3737
const routes = {
38-
compile: { pathname: '/compile' },
39-
execute: { pathname: '/execute' },
40-
format: { pathname: '/format' },
41-
clippy: { pathname: '/clippy' },
42-
miri: { pathname: '/miri' },
43-
macroExpansion: { pathname: '/macro-expansion' },
38+
compile: '/compile',
39+
execute: '/execute',
40+
format: '/format',
41+
clippy: '/clippy',
42+
miri: '/miri',
43+
macroExpansion: '/macro-expansion',
4444
meta: {
45-
crates: { pathname: '/meta/crates' },
45+
crates: '/meta/crates',
4646
version: {
4747
stable: '/meta/version/stable',
4848
beta: '/meta/version/beta',
@@ -51,7 +51,8 @@ const routes = {
5151
clippy: '/meta/version/clippy',
5252
miri: '/meta/version/miri',
5353
},
54-
gist: { pathname: '/meta/gist/' },
54+
gistSave: '/meta/gist',
55+
gistLoad: '/meta/gist/id',
5556
},
5657
};
5758

@@ -231,24 +232,22 @@ const receiveExecuteSuccess = ({ stdout, stderr }: ExecuteResponseBody) =>
231232
const receiveExecuteFailure = ({ error }: { error?: string }) =>
232233
createAction(ActionType.ExecuteFailed, { error });
233234

234-
function jsonGet(urlObj: string | UrlObject) {
235-
const urlStr = url.format(urlObj);
235+
type FetchArg = Parameters<typeof fetch>[0];
236236

237-
return fetchJson(urlStr, {
237+
function jsonGet(url: FetchArg) {
238+
return fetchJson(url, {
238239
method: 'get',
239240
});
240241
}
241242

242-
function jsonPost<T>(urlObj: UrlObject, body: Record<string, any>): Promise<T> {
243-
const urlStr = url.format(urlObj);
244-
245-
return fetchJson(urlStr, {
243+
function jsonPost<T>(url: FetchArg, body: Record<string, any>): Promise<T> {
244+
return fetchJson(url, {
246245
method: 'post',
247246
body: JSON.stringify(body),
248247
});
249248
}
250249

251-
async function fetchJson(url: string, args: RequestInit) {
250+
async function fetchJson(url: FetchArg, args: RequestInit) {
252251
const headers = new Headers(args.headers);
253252
headers.set('Content-Type', 'application/json');
254253

@@ -751,9 +750,14 @@ type PerformGistLoadProps =
751750
Pick<GistSuccessProps, Exclude<keyof GistSuccessProps, 'url' | 'code' | 'stdout' | 'stderr'>>;
752751

753752
export function performGistLoad({ id, channel, mode, edition }: PerformGistLoadProps): ThunkAction {
754-
return function(dispatch, _getState) {
753+
return function(dispatch, getState) {
755754
dispatch(requestGistLoad());
756-
const u = url.resolve(routes.meta.gist.pathname, id);
755+
756+
const state = getState();
757+
const baseUrl = baseUrlSelector(state);
758+
const gistUrl = new URL(routes.meta.gistLoad, baseUrl);
759+
const u = new URL(id, gistUrl);
760+
757761
jsonGet(u)
758762
.then(gist => dispatch(receiveGistLoadSuccess({ channel, mode, edition, ...gist })));
759763
// TODO: Failure case
@@ -792,7 +796,7 @@ export function performGistSave(): ThunkAction {
792796
},
793797
} = getState();
794798

795-
return jsonPost<GistResponseBody>(routes.meta.gist, { code })
799+
return jsonPost<GistResponseBody>(routes.meta.gistSave, { code })
796800
.then(json => dispatch(receiveGistSaveSuccess({ ...json, code, stdout, stderr, channel, mode, edition })));
797801
// TODO: Failure case
798802
};

ui/frontend/configureStore.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { merge } from 'lodash';
22
import { applyMiddleware, compose, createStore } from 'redux';
33
import { useDispatch } from 'react-redux';
44
import thunk, { ThunkDispatch } from 'redux-thunk';
5-
import * as url from 'url';
65

76
import { Action, initializeApplication } from './actions';
87
import initializeLocalStorage from './local_storage';
@@ -11,7 +10,7 @@ import playgroundApp, { State } from './reducers';
1110
import { websocketMiddleware } from './websocketMiddleware';
1211

1312
export default function configureStore(window: Window) {
14-
const baseUrl = url.resolve(window.location.href, '/');
13+
const baseUrl = new URL('/', window.location.href).href;
1514
const websocket = websocketMiddleware(window);
1615

1716
const initialGlobalState = {

ui/frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"reselect": "^4.0.0",
3030
"route-parser": "^0.0.5",
3131
"split-grid": "^1.0.9",
32-
"url": "^0.11.0",
3332
"zod": "^3.20.3"
3433
},
3534
"devDependencies": {

ui/frontend/selectors/index.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { source } from 'common-tags';
22
import { createSelector } from 'reselect';
3-
import * as url from 'url';
43

54
import { State } from '../reducers';
65
import {
@@ -175,7 +174,7 @@ export const getSomethingToShow = createSelector(
175174
a => a.some(hasProperties),
176175
);
177176

178-
const baseUrlSelector = (state: State) =>
177+
export const baseUrlSelector = (state: State) =>
179178
state.globalConfiguration.baseUrl;
180179

181180
const gistSelector = (state: State) =>
@@ -184,11 +183,13 @@ const gistSelector = (state: State) =>
184183
// Selects url.query of build configs.
185184
const urlQuerySelector = createSelector(
186185
gistSelector,
187-
gist => ({
188-
version: gist.channel,
189-
mode: gist.mode,
190-
edition: gist.edition,
191-
}),
186+
gist => {
187+
const res = new URLSearchParams();
188+
if (gist.channel) { res.set('version', gist.channel) }
189+
if (gist.mode) { res.set('mode', gist.mode) }
190+
if (gist.edition) { res.set('edition', gist.edition) }
191+
return res;
192+
},
192193
);
193194

194195
export const showGistLoaderSelector = createSelector(
@@ -198,10 +199,12 @@ export const showGistLoaderSelector = createSelector(
198199

199200
export const permalinkSelector = createSelector(
200201
baseUrlSelector, urlQuerySelector, gistSelector,
201-
(baseUrl, query, gist) => {
202-
const u = url.parse(baseUrl, true);
203-
u.query = { ...query, gist: gist.id };
204-
return url.format(u);
202+
(baseUrl, originalQuery, gist) => {
203+
const u = new URL(baseUrl);
204+
const query = new URLSearchParams(originalQuery);
205+
if (gist.id) { query.set('gist', gist.id) }
206+
u.search = query.toString();
207+
return u.href;
205208
},
206209
);
207210

@@ -257,18 +260,20 @@ const snippetSelector = createSelector(
257260
export const urloUrlSelector = createSelector(
258261
snippetSelector,
259262
snippet => {
260-
const newUsersPostUrl = url.parse('https://users.rust-lang.org/new-topic', true);
261-
newUsersPostUrl.query = { body: snippet };
262-
return url.format(newUsersPostUrl);
263+
const newUsersPostUrl = new URL('https://users.rust-lang.org/new-topic');
264+
newUsersPostUrl.searchParams.set('body', snippet);
265+
return newUsersPostUrl.href;
263266
},
264267
);
265268

266269
export const codeUrlSelector = createSelector(
267270
baseUrlSelector, urlQuerySelector, gistSelector,
268-
(baseUrl, query, gist) => {
269-
const u = url.parse(baseUrl, true);
270-
u.query = { ...query, code: gist.code };
271-
return url.format(u);
271+
(baseUrl, originalQuery, gist) => {
272+
const u = new URL(baseUrl);
273+
const query = new URLSearchParams(originalQuery);
274+
if (gist.code) { query.set('code', gist.code) }
275+
u.search = new URLSearchParams(query).toString();
276+
return u.href;
272277
},
273278
);
274279

ui/frontend/yarn.lock

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5003,11 +5003,6 @@ prr@~1.0.1:
50035003
resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476"
50045004
integrity sha512-yPw4Sng1gWghHQWj0B3ZggWUm4qVbPwPFcRG8KyxiU7J2OHFSoEHKS+EZ3fv5l1t9CyCiop6l/ZYeWbrgoQejw==
50055005

5006-
punycode@1.3.2:
5007-
version "1.3.2"
5008-
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
5009-
integrity sha512-RofWgt/7fL5wP1Y7fxE7/EmTLzQVnB0ycyibJ0OOHIlJqTNzglYFxVwETOcIoJqJmpDXJ9xImDv+Fq34F/d4Dw==
5010-
50115006
punycode@^2.1.0:
50125007
version "2.3.0"
50135008
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.0.tgz#f67fa67c94da8f4d0cfff981aee4118064199b8f"
@@ -5020,11 +5015,6 @@ qs@^6.4.0:
50205015
dependencies:
50215016
side-channel "^1.0.4"
50225017

5023-
querystring@0.2.0:
5024-
version "0.2.0"
5025-
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
5026-
integrity sha512-X/xY82scca2tau62i9mDyU9K+I+djTMUsvwf7xnUX5GLvVzgJybOJf4Y6o9Zx3oJK/LSXg5tTZBjwzqVPaPO2g==
5027-
50285018
queue-microtask@^1.2.2:
50295019
version "1.2.3"
50305020
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"
@@ -6147,14 +6137,6 @@ uri-js@^4.2.2:
61476137
dependencies:
61486138
punycode "^2.1.0"
61496139

6150-
url@^0.11.0:
6151-
version "0.11.0"
6152-
resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1"
6153-
integrity sha512-kbailJa29QrtXnxgq+DdCEGlbTeYM2eJUxsz6vjZavrCYPMIFHMKQmSKYAIuUK2i7hgPm28a8piX5NTUtM/LKQ==
6154-
dependencies:
6155-
punycode "1.3.2"
6156-
querystring "0.2.0"
6157-
61586140
use-sync-external-store@^1.0.0:
61596141
version "1.2.0"
61606142
resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a"

0 commit comments

Comments
 (0)