Skip to content

Commit 8557404

Browse files
committed
Don't override pathname in transformUrl
1 parent 2552dbb commit 8557404

File tree

8 files changed

+48
-9
lines changed

8 files changed

+48
-9
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.2.5 (Common, Node.js, Web)
2+
3+
### Bug fixes
4+
5+
- `pathname` segment from `host` client configuration parameter is now handled properly when making requests.
6+
See this [comment](https://github.com/ClickHouse/clickhouse-js/issues/164#issuecomment-1785166626) for more details.
7+
18
## 0.2.4 (Node.js only)
29

310
No changes in web/common modules.

packages/client-common/__tests__/unit/transform_url.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import { transformUrl } from '@clickhouse/client-common'
22

33
describe('transformUrl', () => {
4+
it('only adds the trailing slash to a url without pathname', () => {
5+
const url = new URL('http://clickhouse.com')
6+
const newUrl = transformUrl({
7+
url,
8+
})
9+
expect(newUrl.toString()).toBe('http://clickhouse.com/')
10+
})
11+
12+
it('does nothing with a url with pathname', () => {
13+
const url = new URL('http://clickhouse.com/clickhouse')
14+
const newUrl = transformUrl({
15+
url,
16+
})
17+
expect(newUrl.toString()).toBe('http://clickhouse.com/clickhouse')
18+
})
19+
420
it('attaches pathname and search params to the url', () => {
521
const url = new URL('http://clickhouse.com')
622
const newUrl = transformUrl({
@@ -20,6 +36,15 @@ describe('transformUrl', () => {
2036
expect(newUrl.toString()).toBe('http://clickhouse.com/foo')
2137
})
2238

39+
it('attaches pathname to an existing pathname', () => {
40+
const url = new URL('http://clickhouse.com/clickhouse')
41+
const newUrl = transformUrl({
42+
url,
43+
pathname: '/foobar',
44+
})
45+
expect(newUrl.toString()).toBe('http://clickhouse.com/clickhouse/foobar')
46+
})
47+
2348
it('does not mutate an original url', () => {
2449
const url = new URL('http://clickhouse.com')
2550
const newUrl = transformUrl({

packages/client-common/src/utils/url.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ClickHouseSettings } from '../settings'
21
import { formatQueryParams, formatQuerySettings } from '../data_formatter'
2+
import type { ClickHouseSettings } from '../settings'
33

44
export function transformUrl({
55
url,
@@ -13,7 +13,14 @@ export function transformUrl({
1313
const newUrl = new URL(url)
1414

1515
if (pathname) {
16-
newUrl.pathname = pathname
16+
// See https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname
17+
// > value for such "special scheme" URLs can never be the empty string,
18+
// > but will instead always have at least one / character.
19+
if (newUrl.pathname === '/') {
20+
newUrl.pathname = pathname
21+
} else {
22+
newUrl.pathname += pathname
23+
}
1724
}
1825

1926
if (searchParams) {

packages/client-common/src/version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default '0.2.4'
1+
export default '0.2.5'

packages/client-node/src/connection/node_base_connection.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export abstract class NodeBaseConnection
317317

318318
const stream = await this.request({
319319
method: 'POST',
320-
url: transformUrl({ url: this.params.url, pathname: '/', searchParams }),
320+
url: transformUrl({ url: this.params.url, searchParams }),
321321
body: params.query,
322322
abort_signal: params.abort_signal,
323323
decompress_response: clickhouse_settings.enable_http_compression === 1,
@@ -343,7 +343,7 @@ export abstract class NodeBaseConnection
343343

344344
const stream = await this.request({
345345
method: 'POST',
346-
url: transformUrl({ url: this.params.url, pathname: '/', searchParams }),
346+
url: transformUrl({ url: this.params.url, searchParams }),
347347
body: params.query,
348348
abort_signal: params.abort_signal,
349349
})
@@ -403,7 +403,7 @@ export abstract class NodeBaseConnection
403403

404404
const stream = await this.request({
405405
method: 'POST',
406-
url: transformUrl({ url: this.params.url, pathname: '/', searchParams }),
406+
url: transformUrl({ url: this.params.url, searchParams }),
407407
body: params.values,
408408
abort_signal: params.abort_signal,
409409
compress_request: this.params.compression.compress_request,

packages/client-node/src/version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default '0.2.4'
1+
export default '0.2.5'

packages/client-web/src/connection/web_connection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class WebConnection implements Connection<ReadableStream> {
143143
}): Promise<Response> {
144144
const url = transformUrl({
145145
url: this.params.url,
146-
pathname: pathname ?? '/',
146+
pathname,
147147
searchParams,
148148
}).toString()
149149

packages/client-web/src/version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default '0.2.4'
1+
export default '0.2.5'

0 commit comments

Comments
 (0)