@go-go-scope/adapter-hapi 2.2.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/LICENSE +21 -0
- package/README.md +115 -0
- package/dist/index.d.mts +81 -0
- package/dist/index.mjs +60 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 thelinuxlich (Alisson Cavalcante Agiani)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# @go-go-scope/adapter-hapi
|
|
2
|
+
|
|
3
|
+
Hapi adapter for go-go-scope - request-scoped structured concurrency with automatic cleanup.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @go-go-scope/adapter-hapi @hapi/hapi
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import Hapi from '@hapi/hapi'
|
|
17
|
+
import { hapiGoGoScope, getScope, getRootScope, closeHapiScope } from '@go-go-scope/adapter-hapi'
|
|
18
|
+
|
|
19
|
+
const server = Hapi.server({ port: 3000 })
|
|
20
|
+
|
|
21
|
+
// Register plugin
|
|
22
|
+
await server.register({
|
|
23
|
+
plugin: hapiGoGoScope,
|
|
24
|
+
options: { name: 'my-api', metrics: true }
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
// Use scope in routes
|
|
28
|
+
server.route({
|
|
29
|
+
method: 'GET',
|
|
30
|
+
path: '/users/{id}',
|
|
31
|
+
handler: async (request) => {
|
|
32
|
+
const [err, user] = await request.scope.task(
|
|
33
|
+
() => fetchUser(request.params.id),
|
|
34
|
+
{ retry: 'exponential', timeout: 5000 }
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
if (err) {
|
|
38
|
+
return { error: err.message }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return user
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
await server.start()
|
|
46
|
+
console.log('Server running at:', server.info.uri)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Options
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
await server.register({
|
|
53
|
+
plugin: hapiGoGoScope,
|
|
54
|
+
options: {
|
|
55
|
+
name: 'my-api', // Root scope name
|
|
56
|
+
metrics: true, // Enable metrics collection
|
|
57
|
+
timeout: 30000 // Default request timeout
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Accessing Root Scope
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// From server instance
|
|
66
|
+
const rootScope = getRootScope(server)
|
|
67
|
+
const metrics = rootScope.metrics()
|
|
68
|
+
console.log('App metrics:', metrics)
|
|
69
|
+
|
|
70
|
+
// Or directly from request
|
|
71
|
+
server.route({
|
|
72
|
+
method: 'GET',
|
|
73
|
+
path: '/health',
|
|
74
|
+
handler: async (request) => {
|
|
75
|
+
const rootScope = request.rootScope
|
|
76
|
+
return { status: 'ok', metrics: rootScope.metrics() }
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Graceful Shutdown
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
process.on('SIGTERM', async () => {
|
|
85
|
+
await closeHapiScope(server)
|
|
86
|
+
await server.stop()
|
|
87
|
+
})
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## API
|
|
91
|
+
|
|
92
|
+
### `hapiGoGoScope`
|
|
93
|
+
|
|
94
|
+
Hapi plugin that provides request-scoped structured concurrency.
|
|
95
|
+
|
|
96
|
+
**Options:**
|
|
97
|
+
- `name`: Root scope name (default: 'hapi-app')
|
|
98
|
+
- `metrics`: Enable metrics collection (default: false)
|
|
99
|
+
- `timeout`: Default request timeout in ms
|
|
100
|
+
|
|
101
|
+
### `getScope(request)`
|
|
102
|
+
|
|
103
|
+
Get the request-scoped scope from Hapi request.
|
|
104
|
+
|
|
105
|
+
### `getRootScope(server)`
|
|
106
|
+
|
|
107
|
+
Get the root application scope from Hapi server.
|
|
108
|
+
|
|
109
|
+
### `closeHapiScope(server)`
|
|
110
|
+
|
|
111
|
+
Gracefully shutdown the root scope (for use in SIGTERM handlers).
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Server, Request, Plugin } from '@hapi/hapi';
|
|
2
|
+
import { Scope } from 'go-go-scope';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hapi adapter for go-go-scope
|
|
6
|
+
* Provides request-scoped structured concurrency for Hapi applications
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare module "@hapi/hapi" {
|
|
10
|
+
interface Request {
|
|
11
|
+
scope: Scope<Record<string, unknown>>;
|
|
12
|
+
rootScope: Scope<Record<string, unknown>>;
|
|
13
|
+
}
|
|
14
|
+
interface Server {
|
|
15
|
+
rootScope: Scope<Record<string, unknown>>;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
interface HapiGoGoScopeOptions {
|
|
19
|
+
/** Root scope name */
|
|
20
|
+
name?: string;
|
|
21
|
+
/** Enable metrics collection */
|
|
22
|
+
metrics?: boolean;
|
|
23
|
+
/** Default timeout for all requests */
|
|
24
|
+
timeout?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Hapi plugin for go-go-scope integration
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import Hapi from '@hapi/hapi'
|
|
32
|
+
* import { hapiGoGoScope } from '@go-go-scope/adapter-hapi'
|
|
33
|
+
*
|
|
34
|
+
* const server = Hapi.server({ port: 3000 })
|
|
35
|
+
* await server.register({
|
|
36
|
+
* plugin: hapiGoGoScope,
|
|
37
|
+
* options: { name: 'my-api', metrics: true }
|
|
38
|
+
* })
|
|
39
|
+
*
|
|
40
|
+
* server.route({
|
|
41
|
+
* method: 'GET',
|
|
42
|
+
* path: '/users/{id}',
|
|
43
|
+
* handler: async (request) => {
|
|
44
|
+
* const [err, user] = await request.scope.task(
|
|
45
|
+
* () => fetchUser(request.params.id),
|
|
46
|
+
* { retry: 'exponential' }
|
|
47
|
+
* )
|
|
48
|
+
*
|
|
49
|
+
* if (err) {
|
|
50
|
+
* return { error: err.message }
|
|
51
|
+
* }
|
|
52
|
+
* return user
|
|
53
|
+
* }
|
|
54
|
+
* })
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare const hapiGoGoScope: Plugin<HapiGoGoScopeOptions>;
|
|
58
|
+
/**
|
|
59
|
+
* Get the request-scoped scope from Hapi request
|
|
60
|
+
*/
|
|
61
|
+
declare function getScope(request: Request): Scope<Record<string, unknown>>;
|
|
62
|
+
/**
|
|
63
|
+
* Get the root application scope from Hapi server
|
|
64
|
+
*/
|
|
65
|
+
declare function getRootScope(server: Server): Scope<Record<string, unknown>>;
|
|
66
|
+
/**
|
|
67
|
+
* Graceful shutdown helper for Hapi applications
|
|
68
|
+
* Disposes the root scope when the server is closing
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* process.on('SIGTERM', async () => {
|
|
73
|
+
* await closeHapiScope(server)
|
|
74
|
+
* await server.stop()
|
|
75
|
+
* })
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
declare function closeHapiScope(server: Server): Promise<void>;
|
|
79
|
+
|
|
80
|
+
export { closeHapiScope, getRootScope, getScope, hapiGoGoScope };
|
|
81
|
+
export type { HapiGoGoScopeOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { scope } from 'go-go-scope';
|
|
2
|
+
|
|
3
|
+
const hapiGoGoScope = {
|
|
4
|
+
name: "hapi-go-go-scope",
|
|
5
|
+
version: "2.1.0",
|
|
6
|
+
register: async (server, options) => {
|
|
7
|
+
const { name = "hapi-app", metrics = false, timeout } = options;
|
|
8
|
+
const rootScope = scope({ name, metrics });
|
|
9
|
+
server.rootScope = rootScope;
|
|
10
|
+
server.decorate("request", "scope", null);
|
|
11
|
+
server.decorate("request", "rootScope", rootScope);
|
|
12
|
+
server.ext("onRequest", (request, h) => {
|
|
13
|
+
const scopeOptions = {
|
|
14
|
+
parent: rootScope,
|
|
15
|
+
// biome-ignore lint/suspicious/noExplicitAny: Hapi request info access
|
|
16
|
+
name: `request-${request.id || request.info.id}`
|
|
17
|
+
};
|
|
18
|
+
if (timeout) scopeOptions.timeout = timeout;
|
|
19
|
+
request.scope = scope(scopeOptions);
|
|
20
|
+
return h.continue;
|
|
21
|
+
});
|
|
22
|
+
server.ext("onPostResponse", async (request, h) => {
|
|
23
|
+
const requestScope = request.scope;
|
|
24
|
+
if (requestScope) {
|
|
25
|
+
await requestScope[Symbol.asyncDispose]().catch(() => {
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return h.continue;
|
|
29
|
+
});
|
|
30
|
+
server.ext("onPostStop", async () => {
|
|
31
|
+
await rootScope[Symbol.asyncDispose]().catch(() => {
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
function getScope(request) {
|
|
37
|
+
const requestScope = request.scope;
|
|
38
|
+
if (!requestScope) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
"Scope not available. Ensure hapiGoGoScope plugin is registered."
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
return requestScope;
|
|
44
|
+
}
|
|
45
|
+
function getRootScope(server) {
|
|
46
|
+
if (!server.rootScope) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"Root scope not available. Ensure hapiGoGoScope plugin is registered."
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return server.rootScope;
|
|
52
|
+
}
|
|
53
|
+
async function closeHapiScope(server) {
|
|
54
|
+
if (server.rootScope) {
|
|
55
|
+
await server.rootScope[Symbol.asyncDispose]().catch(() => {
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { closeHapiScope, getRootScope, getScope, hapiGoGoScope };
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@go-go-scope/adapter-hapi",
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"description": "Hapi adapter for go-go-scope - request-scoped structured concurrency",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.mts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"hapi",
|
|
16
|
+
"adapter",
|
|
17
|
+
"go-go-scope",
|
|
18
|
+
"structured-concurrency",
|
|
19
|
+
"plugin"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=24.0.0"
|
|
23
|
+
},
|
|
24
|
+
"author": "thelinuxlich",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/thelinuxlich/go-go-scope.git",
|
|
29
|
+
"directory": "packages/adapter-hapi"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist/",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"go-go-scope": "2.2.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@hapi/hapi": "^21.0.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@biomejs/biome": "^2.4.4",
|
|
46
|
+
"@hapi/hapi": "^21.4.6",
|
|
47
|
+
"@types/node": "^24",
|
|
48
|
+
"pkgroll": "^2.26.3",
|
|
49
|
+
"typescript": "^5.9.3",
|
|
50
|
+
"vitest": "^4.0.18"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "pkgroll --clean-dist",
|
|
54
|
+
"lint": "biome check --write src/",
|
|
55
|
+
"test": "vitest run --passWithNoTests",
|
|
56
|
+
"clean": "rm -rf dist",
|
|
57
|
+
"typecheck": "tsc --noEmit"
|
|
58
|
+
}
|
|
59
|
+
}
|