Skip to content

Commit

Permalink
fix: add missing return
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Oct 30, 2023
1 parent 784119e commit 5b6a0ad
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 29 deletions.
5 changes: 0 additions & 5 deletions src/bzz-link.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import * as swarmCid from '@ethersphere/swarm-cid'
import { Strings } from 'cafe-utility'
import { Request } from 'express'
import { logger } from './logger'

export class NotEnabledError extends Error {}

export function requestFilter(req: Request): boolean {
return req.method === 'GET' && req.subdomains.length >= 1
}

export function subdomainToBzz(
requestHostname: string,
appHostname: string,
Expand Down
6 changes: 4 additions & 2 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios'
import { Dates, Objects, Strings } from 'cafe-utility'
import { Application, Response } from 'express'
import { IncomingHttpHeaders } from 'http'
import { requestFilter, subdomainToBzz } from './bzz-link'
import { subdomainToBzz } from './bzz-link'
import { logger } from './logger'
import { StampsManager } from './stamps'
import { getErrorMessage } from './utils'
Expand All @@ -25,8 +25,10 @@ interface Options {

export function createProxyEndpoints(app: Application, options: Options) {
app.use(async (req, res, next) => {
if (!options.hostname || !requestFilter(req)) {
if (!options.hostname || !Strings.before(req.hostname, options.hostname) || req.method !== 'GET') {
next()

return
}
const newUrl = subdomainToBzz(
req.hostname,
Expand Down
23 changes: 1 addition & 22 deletions test/bzz-link.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import { Request } from 'express'
import { NotEnabledError, requestFilter, subdomainToBzz } from '../src/bzz-link'
import { NotEnabledError, subdomainToBzz } from '../src/bzz-link'

describe('bzz.link', () => {
describe('requestFilter', () => {
it('should return true for subdomain', async () => {
const req = { subdomains: ['someEnsName'] } as Request

expect(requestFilter(req)).toStrictEqual(true)
})

it('should return true for multilevel subdomain', async () => {
const req = { subdomains: ['swarm', 'book'] } as Request

expect(requestFilter(req)).toStrictEqual(true)
})

it('should return false for no subdomain', async () => {
const req = { subdomains: [] as string[] } as Request

expect(requestFilter(req)).toStrictEqual(false)
})
})

describe('routerClosure', () => {
const MANIFEST = {
cid: 'bah5acgzamh5fl7emnrazttpy7sag6utq5myidv3venspn6l5sevr4lko2n3q',
Expand Down

0 comments on commit 5b6a0ad

Please sign in to comment.