-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: harmonize print address infos util api
- Loading branch information
1 parent
74e9673
commit 315ed00
Showing
8 changed files
with
34 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
/// <reference types="@gracile/gracile/ambient" /> | ||
/// <reference types="@gracile/markdown/ambient" /> | ||
/// <reference types="@gracile/svg/ambient" /> | ||
|
||
declare namespace Gracile { | ||
interface Locals { | ||
requestId: import('node:crypto').UUID; | ||
userEmail: string | null; | ||
} | ||
} | ||
declare namespace Express { | ||
interface Locals extends Gracile.Locals {} | ||
} |
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,111 +1,37 @@ | ||
// NOTE: Util. to pretty print for user provided server. | ||
|
||
import type { IncomingMessage, Server, ServerResponse } from 'node:http'; | ||
import type { AddressInfo } from 'node:net'; | ||
|
||
// import type { AddressInfo } from 'node:net'; | ||
import { logger } from '@gracile/internal-utils/logger'; | ||
import { DEV } from 'esm-env'; | ||
import c from 'picocolors'; | ||
|
||
import { IP_EXPOSED } from './env.js'; | ||
|
||
export function printAddressInfos(options: { | ||
server?: Server; | ||
address?: string; | ||
port?: number; | ||
}) { | ||
export function printAddressInfos(server: string | AddressInfo | null) { | ||
let address: null | string = null; | ||
|
||
if (options.server) { | ||
const infos = options.server.address(); | ||
|
||
if (typeof infos === 'object' && infos && infos.port && infos.address) { | ||
address = `http://${infos.address}:${infos.port}/`; | ||
} | ||
} else if (options.address) { | ||
address = `http://${options.address}${options.port ? `:${String(options.port)}` : ''}`; | ||
if (!server) throw new Error('Incorrect address infos'); | ||
if (typeof server === 'string') { | ||
address = server; | ||
} else { | ||
address = `http://${server.address}${server.port ? `:${String(server.port)}` : ''}`; | ||
} | ||
if (!address) throw new Error('Incorrect options'); | ||
|
||
logger.info(c.green(`${DEV ? 'development' : 'production'} server started`), { | ||
timestamp: true, | ||
}); | ||
|
||
if (address?.includes(IP_EXPOSED)) | ||
if (address.includes(IP_EXPOSED)) | ||
logger.info( | ||
`${ | ||
address?.includes(IP_EXPOSED) | ||
? `\n${c.dim('┃')} Network ${c.cyan(address)}` | ||
address.includes(IP_EXPOSED) | ||
? `\n${c.dim('┃')} Network ${c.cyan(address)}\n` | ||
: '' | ||
}`, | ||
); | ||
else | ||
logger.info( | ||
` | ||
${c.dim('┃')} Local ${c.cyan(address.replace(/::1?/, 'localhost'))}`, | ||
${c.dim('┃')} Local ${c.cyan(address.replace(/::1?/, 'localhost'))}\n`, | ||
); | ||
} | ||
|
||
// NOTE: UNUSED (keep?) | ||
function sendHtml(res: ServerResponse, payload: unknown) { | ||
res.setHeader('content/type', 'text/html'); | ||
res.end(payload); | ||
} | ||
function fallback404(res: ServerResponse) { | ||
return sendHtml(res, '404 — Not found!'); | ||
} | ||
export function notFoundHandler(req: IncomingMessage, res: ServerResponse) { | ||
const host = req.headers.host; | ||
if (!host) { | ||
fallback404(res); | ||
return; | ||
} | ||
|
||
const url = new URL('/404/', `http://${host}`); | ||
|
||
fetch(url) | ||
.then((t) => t.text()) | ||
.then((r) => sendHtml(res, r)) | ||
.catch(() => fallback404(res)); | ||
} | ||
|
||
export type LocalMiddlewareContext = { | ||
request: Request; | ||
response: ResponseInit; | ||
}; | ||
|
||
// NOTE: NOT EXPOSED FOR NOW. Maybe just put in docs. | ||
export type BasicAuthUser = { userName: string | null }; | ||
|
||
/** | ||
* @param context | ||
* @returns | ||
*/ | ||
export const authenticateBasic = ( | ||
context: LocalMiddlewareContext, | ||
validate: (user: { name: string; pass: string }) => boolean, | ||
): BasicAuthUser => { | ||
const b64 = context.request.headers.get('authorization')?.split(' ')?.[1]; | ||
|
||
if (b64) { | ||
const [name, pass] = Buffer.from(b64, 'base64').toString().split(':'); | ||
if (name && pass) { | ||
validate({ name, pass }); | ||
|
||
return { userName: name }; | ||
} | ||
} | ||
|
||
/** | ||
* Authentication failed | ||
*/ | ||
context.response.headers = new Headers({ | ||
...context.response.headers, | ||
'WWW-Authenticate': 'Basic', | ||
}); | ||
|
||
context.response.status = 401; | ||
context.response.statusText = 'You are not authenticated!'; | ||
|
||
return { userName: null }; | ||
}; |