Skip to content

Commit

Permalink
Merge pull request #887 from permaweb/jrdn91/trim-trailing-slash-sche…
Browse files Browse the repository at this point in the history
…duler-utils

fix(scheduler-utils): trim trailing slash from returned URLs #246
  • Loading branch information
jrdn91 authored Jul 17, 2024
2 parents 8394fdd + ecb97a6 commit 496b5e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion scheduler-utils/src/locate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { checkForRedirectSchema, getByOwnerSchema, getByProcessSchema, loadProcessSchedulerSchema, loadSchedulerSchema, setByOwnerSchema, setByProcessSchema } from './dal.js'
import { trimSlash } from './utils.js'

export function locateWith ({ loadProcessScheduler, loadScheduler, cache, followRedirects, checkForRedirect }) {
loadProcessScheduler = loadProcessSchedulerSchema.implement(loadProcessScheduler)
Expand Down Expand Up @@ -53,7 +54,7 @@ export function locateWith ({ loadProcessScheduler, loadScheduler, cache, follow
*/
if (followRedirects) finalUrl = await checkForRedirect(scheduler.url, process)

const byProcess = { url: finalUrl, address: scheduler.address }
const byProcess = { url: trimSlash(finalUrl), address: scheduler.address }
await setByProcess(process, byProcess, scheduler.ttl)
return byProcess
})
Expand Down
3 changes: 2 additions & 1 deletion scheduler-utils/src/raw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getByOwnerSchema, loadSchedulerSchema, setByOwnerSchema } from './dal.js'
import { InvalidSchedulerLocationError } from './err.js'
import { trimSlash } from './utils.js'

export function rawWith ({ loadScheduler, cache }) {
loadScheduler = loadSchedulerSchema.implement(loadScheduler)
Expand All @@ -19,7 +20,7 @@ export function rawWith ({ loadScheduler, cache }) {
return loadScheduler(address)
.then((scheduler) =>
setByOwner(address, scheduler.url, scheduler.ttl)
.then(() => ({ url: scheduler.url }))
.then(() => ({ url: trimSlash(scheduler.url) }))
)
.catch((err) => {
if (err instanceof InvalidSchedulerLocationError) return undefined
Expand Down
4 changes: 4 additions & 0 deletions scheduler-utils/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function trimSlash (str = '') {
str = str.trim()
return str.endsWith('/') ? trimSlash(str.slice(0, -1)) : str
}
12 changes: 12 additions & 0 deletions scheduler-utils/src/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, test } from 'node:test'
import * as assert from 'node:assert'
import { trimSlash } from './utils.js'

describe('trimSlash', () => {
test('should remove trailing slash from url', () => {
const resultWithTrailingSlash = trimSlash('https://foo.bar/')
assert.equal(resultWithTrailingSlash, 'https://foo.bar')
const resultWithoutTrailingSlash = trimSlash('https://foo.bar')
assert.equal(resultWithoutTrailingSlash, 'https://foo.bar')
})
})

0 comments on commit 496b5e6

Please sign in to comment.