-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowed the back-end to select a port as needed, marked the kind of d…
…rivers, and added some security to the tRPC ch annel.
- Loading branch information
Showing
29 changed files
with
182 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,48 @@ | ||
import { initTRPC } from '@trpc/server' | ||
import { randomBytes } from 'crypto' | ||
import { initTRPC, TRPCError } from '@trpc/server' | ||
import { memo } from 'radash' | ||
import type { CreateHTTPContextOptions } from '@trpc/server/adapters/standalone' | ||
import type { CreateWSSContextFnOptions } from '@trpc/server/adapters/ws' | ||
import type { TRPC_ERROR_CODE_KEY } from '@trpc/server/rpc' | ||
import useSuperJson from '@/rpc' | ||
|
||
const t = initTRPC.create({ | ||
export type Context = Awaited<ReturnType<typeof createContext>> | ||
|
||
function createContext(path: string | null) { | ||
if (path == null) return { auth: undefined } | ||
const url = new URL(`ws://127.0.0.1${path}`) | ||
const auth = url.searchParams.get('auth') ?? undefined | ||
|
||
return { auth } | ||
} | ||
|
||
export function createStandaloneContext(opts: CreateHTTPContextOptions | CreateWSSContextFnOptions) { | ||
return createContext(opts.req.url ?? null) | ||
} | ||
|
||
const t = initTRPC.context<Context>().create({ | ||
transformer: useSuperJson() | ||
}) | ||
|
||
export const { router, procedure, createCallerFactory } = t | ||
export const getAuthToken = memo(function getAuthToken() { | ||
return randomBytes(16).toString('base64url') | ||
}) | ||
|
||
function error(code: TRPC_ERROR_CODE_KEY, message?: string, cause?: unknown): never { | ||
throw new TRPCError({ | ||
code, | ||
...(message ? { message } : {}), | ||
...(cause != null ? { cause } : {}) | ||
}) | ||
} | ||
|
||
export const { router, createCallerFactory } = t | ||
export const procedure = t.procedure.use(async function checkAuth(opts) { | ||
const { ctx } = opts | ||
const { auth } = ctx | ||
|
||
if (auth == null) error('UNAUTHORIZED') | ||
if (auth !== getAuthToken()) error('UNAUTHORIZED') | ||
|
||
return await opts.next() | ||
}) |
Oops, something went wrong.