wooks 0.6.5 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,4 @@
1
- # Wooks
2
-
3
- **!!! This is work-in-progress library, breaking changes are expected !!!**
1
+ # wooks
4
2
 
5
3
  <p align="center">
6
4
  <img src="../../wooks-logo.png" width="450px"><br>
@@ -9,46 +7,18 @@
9
7
  </a>
10
8
  </p>
11
9
 
12
- Wooks is a Event Processing Framework based on composable (hooks) functions.
13
-
14
- `wooks` + `@wooksjs/event-http` = Web Application Framework with hooks.
15
-
16
- As an alternative for `express` and `fastify`, `wooks` brings the whole different approach for processing http requests.
17
- It utilizes such a technique as you can see in React Hooks or Vue Composables. It has only a dependency on [@prostojs/router](https://github.com/prostojs/router) (an alternative to `find-my-way` used by `fastify`) which is a very fast (see benchmarks [here](https://github.com/prostojs/router-benchmark)) and robust URI router.
18
-
19
- ### HTTP Composables packs:
20
-
21
- - [@wooksjs/event-http](https://github.com/wooksjs/wooksjs/tree/main/packages/event-http) - HTTP event package with core functionality
22
- - [@wooksjs/http-body](https://github.com/wooksjs/wooksjs/tree/main/packages/http-body) - to parse body
23
- - [@wooksjs/http-static](https://github.com/wooksjs/wooksjs/tree/main/packages/http-static) - to serve static files
24
- - [@wooksjs/http-proxy](https://github.com/wooksjs/wooksjs/tree/main/packages/http-proxy) - to proxy requests
10
+ Wooks is an event processing framework based on composable functions. It provides a typed, per-event context with lazy caching and a fast URI router, serving as the foundation for HTTP, CLI, and workflow applications.
25
11
 
26
12
  ## Installation
27
13
 
28
- `npm install wooks`
29
-
30
- ## Quick Start with a Web App
31
-
32
- `npm install wooks @wooksjs/event-http`
33
-
34
- ```js
35
- import { useRouteParams } from 'wooks'
36
- import { createHttpApp } from '@wooksjs/event-http'
37
-
38
- const app = createHttpApp()
39
-
40
- app.on('GET', 'hello/:name', () => `Hello ${useRouteParams().get('name')}!`)
41
-
42
- // shortcuts for some methods are supported:
43
- // app.get('hello/:name', () => `Hello ${ useRouteParams().get('name') }!`)
44
-
45
- app.listen(3000, () => {
46
- console.log('Wooks Server is up on port 3000')
47
- })
14
+ ```sh
15
+ npm install wooks
48
16
  ```
49
17
 
50
- See full documentation for http event [here](https://github.com/wooksjs/wooksjs/tree/main/packages/event-http)
51
-
52
18
  ## Documentation
53
19
 
54
- To check out docs, visit [wooks.moost.org](https://wooks.moost.org/).
20
+ For full documentation, visit [wooks.moost.org](https://wooks.moost.org/).
21
+
22
+ ## License
23
+
24
+ MIT
package/dist/index.cjs CHANGED
@@ -74,9 +74,9 @@ var Wooks = class {
74
74
  * @param method - HTTP method (e.g., "GET", "POST").
75
75
  * @param path - URL path to match against registered routes.
76
76
  */
77
- lookup(method, path) {
77
+ lookup(method, path, ctx = (0, __wooksjs_event_core.current)()) {
78
78
  const found = this.getRouter().lookup(method, path || "");
79
- (0, __wooksjs_event_core.useAsyncEventContext)().store("routeParams").value = found?.ctx?.params || {};
79
+ ctx.set(__wooksjs_event_core.routeParamsKey, found?.ctx?.params || {});
80
80
  if (found?.route?.handlers.length) (0, __wooksjs_event_core.getContextInjector)().hook(method, "Handler:routed", found.route.path);
81
81
  else (0, __wooksjs_event_core.getContextInjector)().hook(method, "Handler:not_found");
82
82
  return {
@@ -87,6 +87,20 @@ var Wooks = class {
87
87
  };
88
88
  }
89
89
  /**
90
+ * Fast lookup that returns only the handlers array (or null).
91
+ * Avoids allocating a result object on each request.
92
+ */
93
+ lookupHandlers(method, path, ctx = (0, __wooksjs_event_core.current)()) {
94
+ const found = this.getRouter().lookup(method, path || "");
95
+ ctx.set(__wooksjs_event_core.routeParamsKey, found?.ctx?.params || {});
96
+ if (found?.route?.handlers.length) {
97
+ (0, __wooksjs_event_core.getContextInjector)().hook(method, "Handler:routed", found.route.path);
98
+ return found.route.handlers;
99
+ }
100
+ (0, __wooksjs_event_core.getContextInjector)().hook(method, "Handler:not_found");
101
+ return null;
102
+ }
103
+ /**
90
104
  * Registers a route handler for the given method and path.
91
105
  * @param method - HTTP method (e.g., "GET", "POST").
92
106
  * @param path - URL path pattern (supports parameters like `/users/:id`).
@@ -158,18 +172,9 @@ var WooksAdapterBase = class WooksAdapterBase {
158
172
  getLogger(topic) {
159
173
  return this.getWooks().getLogger(topic);
160
174
  }
161
- getLoggerOptions() {
162
- return this.getWooks().getLoggerOptions();
163
- }
164
- /** Merges the given event options with the current logger configuration. */
165
- mergeEventOptions(opts) {
166
- return {
167
- ...opts,
168
- eventLogger: {
169
- ...this.getLoggerOptions(),
170
- ...opts?.eventLogger
171
- }
172
- };
175
+ /** Returns event context options with a logger derived from the Wooks instance. */
176
+ getEventContextOptions() {
177
+ return { logger: this.getWooks().getLogger("event") };
173
178
  }
174
179
  /**
175
180
  * Registers a route handler for the given method and path.
@@ -193,10 +198,10 @@ Object.defineProperty(exports, 'useEventId', {
193
198
  return __wooksjs_event_core.useEventId;
194
199
  }
195
200
  });
196
- Object.defineProperty(exports, 'useEventLogger', {
201
+ Object.defineProperty(exports, 'useLogger', {
197
202
  enumerable: true,
198
203
  get: function () {
199
- return __wooksjs_event_core.useEventLogger;
204
+ return __wooksjs_event_core.useLogger;
200
205
  }
201
206
  });
202
207
  Object.defineProperty(exports, 'useRouteParams', {
package/dist/index.d.ts CHANGED
@@ -1,11 +1,30 @@
1
+ import { Key, EventContext, EventContextOptions } from '@wooksjs/event-core';
2
+ export { EventContextOptions, Logger, useEventId, useLogger, useRouteParams } from '@wooksjs/event-core';
3
+ import { IncomingMessage } from 'http';
4
+ import { Duplex } from 'stream';
1
5
  import { TConsoleBase, TProstoLoggerOptions } from '@prostojs/logger';
2
6
  import { ProstoRouter, TParsedSegment, TProstoRouterPathHandle } from '@prostojs/router';
3
7
  export { TProstoRouterPathHandle } from '@prostojs/router';
4
- import { TEventOptions } from '@wooksjs/event-core';
5
- export { useEventId, useEventLogger, useRouteParams } from '@wooksjs/event-core';
6
8
 
7
9
  /** A route handler function that returns a response synchronously or asynchronously. */
8
10
  type TWooksHandler<ResType = unknown> = () => Promise<ResType> | ResType;
11
+ /**
12
+ * Contract interface for integrating a WebSocket adapter with the HTTP adapter.
13
+ *
14
+ * The WS adapter implements this interface and provides context keys.
15
+ * The HTTP adapter accepts it via `httpApp.ws(handler)`, sets the keys
16
+ * on the HTTP context during upgrade events, and routes as method 'UPGRADE'.
17
+ */
18
+ interface WooksUpgradeHandler {
19
+ /** Key for the upgrade request (HTTP sets this alongside its own httpKind.keys.req). */
20
+ readonly reqKey: Key<IncomingMessage>;
21
+ /** Key for the raw TCP socket from the upgrade event. */
22
+ readonly socketKey: Key<Duplex>;
23
+ /** Key for the initial data chunk from the upgrade event. */
24
+ readonly headKey: Key<Buffer>;
25
+ /** Fallback handler called by HTTP when no UPGRADE route matches. */
26
+ handleUpgrade(req: IncomingMessage, socket: Duplex, head: Buffer): void;
27
+ }
9
28
 
10
29
  /** Configuration options for the Wooks instance. */
11
30
  interface TWooksOptions {
@@ -49,12 +68,17 @@ declare class Wooks {
49
68
  * @param method - HTTP method (e.g., "GET", "POST").
50
69
  * @param path - URL path to match against registered routes.
51
70
  */
52
- lookup(method: string, path: string): {
71
+ lookup(method: string, path: string, ctx?: EventContext): {
53
72
  handlers: TWooksHandler[] | null;
54
73
  segments: TParsedSegment[] | null;
55
74
  firstStatic: string | null;
56
75
  path: string | null;
57
76
  };
77
+ /**
78
+ * Fast lookup that returns only the handlers array (or null).
79
+ * Avoids allocating a result object on each request.
80
+ */
81
+ lookupHandlers(method: string, path: string, ctx?: EventContext): TWooksHandler[] | null;
58
82
  /**
59
83
  * Registers a route handler for the given method and path.
60
84
  * @param method - HTTP method (e.g., "GET", "POST").
@@ -108,9 +132,8 @@ declare class WooksAdapterBase {
108
132
  * @returns logger instance
109
133
  */
110
134
  getLogger(topic: string): TConsoleBase;
111
- protected getLoggerOptions(): TProstoLoggerOptions;
112
- /** Merges the given event options with the current logger configuration. */
113
- protected mergeEventOptions(opts?: TEventOptions): TEventOptions;
135
+ /** Returns event context options with a logger derived from the Wooks instance. */
136
+ protected getEventContextOptions(): EventContextOptions;
114
137
  /**
115
138
  * Registers a route handler for the given method and path.
116
139
  * @param method - HTTP method (e.g., "GET", "POST").
@@ -121,4 +144,4 @@ declare class WooksAdapterBase {
121
144
  }
122
145
 
123
146
  export { Wooks, WooksAdapterBase, clearGlobalWooks, getGlobalWooks };
124
- export type { TWooksHandler, TWooksOptions };
147
+ export type { TWooksHandler, TWooksOptions, WooksUpgradeHandler };
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ProstoLogger, coloredConsole, createConsoleTransort } from "@prostojs/logger";
2
2
  import { ProstoRouter } from "@prostojs/router";
3
- import { getContextInjector, useAsyncEventContext, useEventId, useEventLogger, useRouteParams } from "@wooksjs/event-core";
3
+ import { current, getContextInjector, routeParamsKey, useEventId, useLogger, useRouteParams } from "@wooksjs/event-core";
4
4
 
5
5
  //#region packages/wooks/src/wooks.ts
6
6
  function getDefaultLogger(topic) {
@@ -51,9 +51,9 @@ var Wooks = class {
51
51
  * @param method - HTTP method (e.g., "GET", "POST").
52
52
  * @param path - URL path to match against registered routes.
53
53
  */
54
- lookup(method, path) {
54
+ lookup(method, path, ctx = current()) {
55
55
  const found = this.getRouter().lookup(method, path || "");
56
- useAsyncEventContext().store("routeParams").value = found?.ctx?.params || {};
56
+ ctx.set(routeParamsKey, found?.ctx?.params || {});
57
57
  if (found?.route?.handlers.length) getContextInjector().hook(method, "Handler:routed", found.route.path);
58
58
  else getContextInjector().hook(method, "Handler:not_found");
59
59
  return {
@@ -64,6 +64,20 @@ var Wooks = class {
64
64
  };
65
65
  }
66
66
  /**
67
+ * Fast lookup that returns only the handlers array (or null).
68
+ * Avoids allocating a result object on each request.
69
+ */
70
+ lookupHandlers(method, path, ctx = current()) {
71
+ const found = this.getRouter().lookup(method, path || "");
72
+ ctx.set(routeParamsKey, found?.ctx?.params || {});
73
+ if (found?.route?.handlers.length) {
74
+ getContextInjector().hook(method, "Handler:routed", found.route.path);
75
+ return found.route.handlers;
76
+ }
77
+ getContextInjector().hook(method, "Handler:not_found");
78
+ return null;
79
+ }
80
+ /**
67
81
  * Registers a route handler for the given method and path.
68
82
  * @param method - HTTP method (e.g., "GET", "POST").
69
83
  * @param path - URL path pattern (supports parameters like `/users/:id`).
@@ -135,18 +149,9 @@ var WooksAdapterBase = class WooksAdapterBase {
135
149
  getLogger(topic) {
136
150
  return this.getWooks().getLogger(topic);
137
151
  }
138
- getLoggerOptions() {
139
- return this.getWooks().getLoggerOptions();
140
- }
141
- /** Merges the given event options with the current logger configuration. */
142
- mergeEventOptions(opts) {
143
- return {
144
- ...opts,
145
- eventLogger: {
146
- ...this.getLoggerOptions(),
147
- ...opts?.eventLogger
148
- }
149
- };
152
+ /** Returns event context options with a logger derived from the Wooks instance. */
153
+ getEventContextOptions() {
154
+ return { logger: this.getWooks().getLogger("event") };
150
155
  }
151
156
  /**
152
157
  * Registers a route handler for the given method and path.
@@ -160,4 +165,4 @@ var WooksAdapterBase = class WooksAdapterBase {
160
165
  };
161
166
 
162
167
  //#endregion
163
- export { Wooks, WooksAdapterBase, clearGlobalWooks, getGlobalWooks, useEventId, useEventLogger, useRouteParams };
168
+ export { Wooks, WooksAdapterBase, clearGlobalWooks, getGlobalWooks, useEventId, useLogger, useRouteParams };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wooks",
3
- "version": "0.6.5",
3
+ "version": "0.7.0",
4
4
  "description": "wooks",
5
5
  "keywords": [
6
6
  "api",
@@ -41,14 +41,14 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@prostojs/logger": "^0.4.3",
44
- "@prostojs/router": "^0.3.0",
45
- "@wooksjs/event-core": "^0.6.5"
44
+ "@prostojs/router": "^0.3.2",
45
+ "@wooksjs/event-core": "^0.7.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "typescript": "^5.9.3",
49
49
  "vitest": "^3.2.4",
50
- "@wooksjs/event-http": "^0.6.5",
51
- "@wooksjs/http-body": "^0.6.5"
50
+ "@wooksjs/http-body": "^0.7.0",
51
+ "@wooksjs/event-http": "^0.7.0"
52
52
  },
53
53
  "scripts": {
54
54
  "build": "rolldown -c ../../rolldown.config.mjs"