Skip to content

Commit 92c575f

Browse files
committed
fix: allow baseUrl to be optional for HTTP Accessor
1 parent 7d36627 commit 92c575f

7 files changed

Lines changed: 47 additions & 14 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { rebaseUrl } from '../pathJoin'
2+
test('rebaseUrl', () => {
3+
expect(rebaseUrl('https://a', 'b')).toBe('https://a/b')
4+
expect(rebaseUrl('https://a/', 'b')).toBe('https://a/b')
5+
expect(rebaseUrl('https://a/', '/b')).toBe('https://a/b')
6+
expect(rebaseUrl('file://a/', '/b/')).toBe('file://a/b/')
7+
expect(rebaseUrl('https:////a/b/', 'c')).toBe('https://a/b/c')
8+
expect(rebaseUrl('https://a/', '//b/')).toBe('https://a/b/')
9+
10+
expect(rebaseUrl('https://a/b//c/', '/d/e//f/')).toBe('https://a/b/c/d/e/f/')
11+
})

shared/packages/api/src/inputApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export namespace Accessor {
326326
allowWrite: false
327327

328328
/** Base url (url to the host), for example http://myhost.com/fileShare/ */
329-
baseUrl: string
329+
baseUrl?: string
330330

331331
/** Name/Id of the network the share exists on. Used to differ between different local networks. Leave empty if globally accessible. */
332332
networkId?: string
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export function removeBasePath(basePath: string, addPath: string): string {
2+
addPath = addPath.replace(/\\/g, '/')
3+
basePath = basePath.replace(/\\/g, '/')
4+
5+
return addPath.replace(new RegExp('^' + escapeRegExp(basePath)), '')
6+
}
7+
function escapeRegExp(text: string): string {
8+
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
9+
}
10+
export function rebaseUrl(baseUrl: string, relativeUrl: string): string {
11+
try {
12+
if (!baseUrl) return new URL(relativeUrl).toString()
13+
const base = new URL(baseUrl)
14+
const relative = new URL(baseUrl)
15+
// relative path may contain URL-unsafe characters, this will encode them but leave any path elements intact
16+
relative.pathname = relativeUrl
17+
// at this point, relativeUrl.pathname will already include the leading `/`
18+
base.pathname = base.pathname + relative.pathname
19+
base.pathname = base.pathname.replace(/\/{2,}/g, '/') // Remove double slashes
20+
return base.toString()
21+
} catch (e) {
22+
// Probably a malformed URL
23+
if (e instanceof Error) {
24+
e.message = `URL: "${baseUrl}" + "${relativeUrl}": ${e.message}`
25+
}
26+
throw e
27+
}
28+
}

shared/packages/worker/src/worker/accessorHandlers/http.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,9 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
215215
},
216216
}
217217
}
218-
if (!this.accessor.baseUrl && this.accessor.baseUrl !== '')
219-
return {
220-
success: false,
221-
reason: {
222-
user: `Accessor baseUrl not set`,
223-
tech: `Accessor baseUrl not set`,
224-
},
225-
}
218+
// Note: For the HTTP-accessor, we allow this.accessor.baseUrl to be empty/falsy
219+
// (which means that the content path needs to be a full URL)
220+
226221
if (!this.content.onlyContainerAccess) {
227222
if (!this.path)
228223
return {
@@ -236,8 +231,7 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
236231
return { success: true }
237232
}
238233
private get baseUrl(): string {
239-
if (!this.accessor.baseUrl) throw new Error(`HTTPAccessorHandle: accessor.baseUrl not set!`)
240-
return this.accessor.baseUrl
234+
return this.accessor.baseUrl ?? ''
241235
}
242236
get path(): string {
243237
if (this.content.onlyContainerAccess) throw new Error('onlyContainerAccess is set!')

shared/packages/worker/src/worker/accessorHandlers/httpProxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import {
2121
Reason,
2222
AccessorId,
2323
MonitorId,
24+
rebaseUrl,
2425
} from '@sofie-package-manager/api'
2526
import { BaseWorker } from '../worker'
2627
import FormData from 'form-data'
2728
import { MonitorInProgress } from '../lib/monitorInProgress'
2829
import { fetchWithController, fetchWithTimeout } from './lib/fetch'
29-
import { rebaseUrl } from './lib/pathJoin'
3030
import { defaultCheckHandleRead, defaultCheckHandleWrite } from './lib/lib'
3131

3232
/** Accessor handle for accessing files in HTTP- */

shared/packages/worker/src/worker/accessorHandlers/lib/FileHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import {
1717
protectString,
1818
MonitorId,
1919
AccessorId,
20+
removeBasePath,
2021
} from '@sofie-package-manager/api'
2122

2223
import { BaseWorker } from '../../worker'
2324

2425
import { GenericAccessorHandle } from '../genericHandle'
2526
import { MonitorInProgress } from '../../lib/monitorInProgress'
26-
import { removeBasePath } from './pathJoin'
2727
import { FileEvent, FileWatcher, IFileWatcher } from './FileWatcher'
2828
import { updateJSONFileBatch } from './json-write-file'
2929

shared/packages/worker/src/worker/accessorHandlers/quantel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import {
2323
Reason,
2424
INNER_ACTION_TIMEOUT,
2525
AccessorId,
26+
rebaseUrl,
2627
} from '@sofie-package-manager/api'
2728
import { BaseWorker } from '../worker'
2829
import { ClipData, ClipDataSummary, ServerInfo, ZoneInfo } from 'tv-automation-quantel-gateway-client/dist/quantelTypes'
29-
import { rebaseUrl } from './lib/pathJoin'
3030
import { defaultCheckHandleRead, defaultCheckHandleWrite } from './lib/lib'
3131

3232
/** The minimum amount of frames where a clip is minimumly playable */

0 commit comments

Comments
 (0)