Skip to content

Commit

Permalink
more work on #18
Browse files Browse the repository at this point in the history
  • Loading branch information
abschill committed Dec 7, 2022
1 parent 55dcba1 commit b41ec54
Show file tree
Hide file tree
Showing 25 changed files with 412 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
root = true

[*{.js,.ts,.cjs,.mjs}]
[*{.js,.ts,.cjs,.mjs, *.json}]
tab_width = 2
indent_size = 2
indent_style = tab
Expand Down
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* text=auto
*.html linguist-detectable=false
autocrlf=false
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ tmp
*.log
.DS_STORE
log
.yarn/cache
.yarn/cache
.yarn/install-state.gz
Binary file modified .yarn/install-state.gz
Binary file not shown.
18 changes: 18 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useServer } from 'httpuppy';
import { useRouter } from 'httpuppy-router';

const server = useServer({
port: 3001,
log_level: 'verbose',
clustered: true
});

const router = useRouter(server);

server.static('/static', './static-server-test');

router.get('/', (req, res) => res.json({ msg: 'hello' }));

router.get('/:route', (req, res) => res.json({ msg: req.params.route }));

server.start();
4 changes: 4 additions & 0 deletions packages/httpuppy-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"test": "ts-mocha test/**/*.test.ts",
"test:watch": "ts-mocha -w test/**/*.test.ts"
},
"peerDependencies": {
"httpuppy": "*"
},
"devDependencies": {
"@types/mime-types": "^2.1.1",
"@types/mocha": "^9.1.1",
Expand All @@ -22,6 +25,7 @@
"@typescript-eslint/parser": "^5.22.0",
"eslint": "^8.14.0",
"expect": "^28.1.0",
"httpuppy": "workspace:*",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"ts-mocha": "^10.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/httpuppy-router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
HTTPRouterCallback,
HTTPRouterOptions,
HTTPServer
} from 'httpuppy-types';
} from 'httpuppy';

/**
* @internal
Expand Down
8 changes: 4 additions & 4 deletions packages/httpuppy-types/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @module Router
* @description Router Types
* @remarks Router Types
*/

import { HTTPuppyRequest, HTTPuppyResponse } from '.';

/**
* @internal
* @private
* @internal
*/
export type HTTPRouterCallback = (
req: HTTPuppyRequest,
Expand All @@ -20,13 +20,13 @@ export type HTTPRouterOptions = {
};
/**
* @internal
* @private
* @internal
*/
export type HTTPRouterBindMethod = (url: string, cb: HTTPRouterCallback) => any;

/**
* @internal
* @private
* @internal
*/
export interface HTTPRouter {
url: string;
Expand Down
5 changes: 2 additions & 3 deletions packages/httpuppy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "httpuppy",
"version": "0.5.7",
"version": "0.6.0",
"description": "speedy abstraction layer for node web servers with automatic clustering",
"types": "lib/index.d.ts",
"main": "lib/index.js",
Expand Down Expand Up @@ -36,7 +36,7 @@
"license": "AGPL-3.0",
"devDependencies": {
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.31",
"@types/node": "^18.11.11",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"eslint": "^8.14.0",
Expand All @@ -51,7 +51,6 @@
"dependencies": {
"http-graceful-shutdown": "^3.1.7",
"httpuppy-router": "^0.1.3",
"httpuppy-types": "^0.1.3",
"httpuppy-vfs": "^0.1.4",
"terminal-color": "^0.2.0",
"winston": "^3.7.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/httpuppy/src/internal/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import cluster from 'cluster';
import { cpus } from 'os';
import { HTTPServer, HTTPServerOptions } from 'httpuppy-types';
import { HTTPServer, HTTPServerOptions } from '../internal/types';

export default function apply_clustered(server: HTTPServer): boolean {
if (cluster.isPrimary) {
Expand Down
25 changes: 13 additions & 12 deletions packages/httpuppy/src/internal/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import apply_clustered from './cluster';
import {
defaultCacheSettings,
shutdown,
create_logger,
$cleanShutdown,
useLogger,
ENV_DEFAULT_ERROR_FILE,
ENV_DEFAULT_EVENT_FILE,
ENV_DEFAULT_HOST,
ENV_TTL_DEFAULT,
ENV_PORT_DEFAULT
} from '.';
import { DiagnosticLog, HTTPServer, HTTPServerOptions } from 'httpuppy-types';
import { DiagnosticLog, HTTPServer, HTTPServerOptions } from './types';

export const default_http_config: HTTPServerOptions = {
port: ENV_PORT_DEFAULT,
Expand All @@ -25,9 +25,10 @@ export const default_http_config: HTTPServerOptions = {
};
/**
* @internal useConfig
* @description hook for applying default config settings against given user input
* @param conf the submitted user input
* @param diagnostics diagnostic log of the top level
* @remarks
* hook for applying default config settings against given user input
* @param conf - the submitted user input
* @param diagnostics - diagnostic log of the top level
* @returns cleaned user config
*/
export function use_config(
Expand All @@ -47,10 +48,10 @@ export function use_config(
}
/**
* @internal _useServer
* @description an internal startup process for the `useServer` hook
* @param config config from user for runtime
* @param server server generated from node standard http library
* @param diagnostics diagnostic list from the prestartup process
* @remarks an internal startup process for the `useServer` hook
* @param config - config from user for runtime
* @param server - server generated from node standard http library
* @param diagnostics - diagnostic list from the prestartup process
* @returns the http server object
*/
export function _use_server(
Expand All @@ -63,7 +64,7 @@ export function _use_server(
ss.diagnostics = diagnostics;
ss.config = config;
ss.routers = [];
ss.logger = create_logger(
ss.logger = useLogger(
config.log_level ?? 'base',
ENV_DEFAULT_ERROR_FILE,
ENV_DEFAULT_EVENT_FILE
Expand All @@ -86,6 +87,6 @@ export function _use_server(
}
};
// bind safe shutdown to the server for callability on the end user side
ss.stop = () => shutdown(server);
ss.stop = () => $cleanShutdown(server);
return ss;
}
68 changes: 34 additions & 34 deletions packages/httpuppy/src/internal/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// import { color_tag } from './include';
import { color } from 'terminal-color';
import { ENV_DEFAULT_ERROR_FILE, ENV_DEFAULT_EVENT_FILE, ENV_LOG_PREFIX } from '.';
import { LogLevel, LogEventFile, LogErrorFile } from 'httpuppy-types';
import { LogLevel, LogEventFile, LogErrorFile } from './types';
import { Logger, format, transports, createLogger } from 'winston';
export function create_logger(

export function useLogger(
level: LogLevel,
error_file?: LogErrorFile,
event_file?: LogEventFile
Expand All @@ -15,38 +16,37 @@ export function create_logger(
const _transports =
level === 'verbose'
? [
new transports.Console({
format: format.combine(
format.label({ label: ENV_LOG_PREFIX }),
format.timestamp(),
format.printf(({ level, message, label, timestamp }) => {
return `[${color.fg.blue(label)}] ${level}: ${message} @ ${color.fg.green(
timestamp
)}`;
})
)
}),
new transports.File({
filename: error_file,
level: 'error',
format: format.colorize()
}),
new transports.File({
filename: event_file,
format: format.colorize()
})
]
: [
new transports.File({
filename: error_file,
level: 'error',
format: format.colorize()
}),
new transports.File({
filename: event_file,
format: format.colorize()
})
];
new transports.Console({
format: format.combine(
format.label({ label: ENV_LOG_PREFIX }),
format.timestamp(),
format.printf(({ level, message, label, timestamp }) => {
return `[${color.fg.blue(label)}] ${level}: ${message} @ ${color.fg.green(
timestamp
)}`;
})
)
}),
new transports.File({
filename: error_file,
level: 'error',
format: format.colorize()
}),
new transports.File({
filename: event_file,
format: format.colorize()
})
] : [
new transports.File({
filename: error_file,
level: 'error',
format: format.colorize()
}),
new transports.File({
filename: event_file,
format: format.colorize()
})
];
const logger = createLogger({
level: 'info',
silent: level === 'silent',
Expand Down
9 changes: 5 additions & 4 deletions packages/httpuppy/src/internal/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
HTTPServer,
HTTPServerOptions,
HTTPHeaders,
HTTPuppyResponse
} from 'httpuppy-types';
HTTPuppyResponse,
HTTPuppySleep
} from './types';
/**
*
* @param options - the writer options to apply the headers against
Expand Down Expand Up @@ -51,13 +52,13 @@ export function apply_404(res: HTTPuppyResponse) {
* @param s - http server to shut down
* @returns void promise to gracefully shut down
*/
export function shutdown(s: HTTPServer): void {
export function $cleanShutdown(s: HTTPServer): HTTPuppySleep {
try {
if (s.onClose) s.onClose();
s.removeAllListeners();
s.close();
// eslint-disable-next-line new-cap
GracefulShutdown(s);
return GracefulShutdown(s);
} catch (e) {
throw new Error('invalid shutdown, nothing submitted/already shut down');
}
Expand Down
8 changes: 5 additions & 3 deletions packages/httpuppy/src/internal/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
HTTPuppyResponse,
HTTPServer,
MountedVFS
} from 'httpuppy-types';
} from './types';

export type UserStaticConfig = {
href?: string; // prefix path to access the directory on router
Expand All @@ -22,8 +22,10 @@ export type UserStaticConfig = {
/**
* @internal
*
* mounts virtual file system to the server
*
*/
export async function mount_vfs(
export async function $$vmount(
server: HTTPServer,
staticOptions?: UserStaticConfig
): Promise<MountedVFS> {
Expand All @@ -43,7 +45,7 @@ export async function apply_static_callback(
static_path: string
) {
const sConfig = { href: _url, path: static_path };
const vfs = await mount_vfs(server, sConfig);
const vfs = await $$vmount(server, sConfig);
server.vfs = vfs;
server.on(ENV_REQUEST_SIGNATURE, (req: HTTPuppyRequest, res: HTTPuppyResponse) => {
const url = req.url ?? '/';
Expand Down
Loading

0 comments on commit b41ec54

Please sign in to comment.