Skip to content

Commit

Permalink
feat: add address remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Nov 20, 2023
1 parent 128f0a7 commit 95b34a5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/bzz-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { logger } from './logger'

export class NotEnabledError extends Error {}

export function subdomainToBzz(subdomain: string, isCidEnabled: boolean, isEnsEnabled: boolean): string {
export function subdomainToBzz(
subdomain: string,
isCidEnabled: boolean,
isEnsEnabled: boolean,
remap: Record<string, string>,
): string {
if (subdomain in remap) {
return remap[subdomain]
}
try {
const result = swarmCid.decodeCid(subdomain)

Expand Down
3 changes: 3 additions & 0 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface Options {
hostname?: string
cidSubdomains?: boolean
ensSubdomains?: boolean
remap: Record<string, string>
}

export function createProxyEndpoints(app: Application, options: Options) {
Expand All @@ -32,11 +33,13 @@ export function createProxyEndpoints(app: Application, options: Options) {

return
}

try {
const newUrl = subdomainToBzz(
subdomain.slice(0, -1), // remove trailing dot
options.cidSubdomains ?? false,
options.ensSubdomains ?? false,
options.remap,
)
await fetchAndRespond(
'GET',
Expand Down
4 changes: 4 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Bee, BeeDebug } from '@ethersphere/bee-js'
import bodyParser from 'body-parser'
import { Arrays } from 'cafe-utility'
import express, { Application } from 'express'
import { AppConfig, DEFAULT_HOSTNAME } from './config'
import { HASHED_IDENTITY_HEADER, fetchBeeIdentity, getHashedIdentity } from './identity'
Expand Down Expand Up @@ -113,6 +114,9 @@ export const createApp = (
cidSubdomains,
ensSubdomains,
hostname,
remap: Object.fromEntries(
(Arrays.getArgument(process.argv, 'remap', process.env as any, 'REMAP') || '').split(';').map(x => x.split('=')),

Check warning on line 118 in src/server.ts

View workflow job for this annotation

GitHub Actions / check (14.x)

Unexpected any. Specify a different type
),
})

app.use(express.static('public'))
Expand Down

0 comments on commit 95b34a5

Please sign in to comment.