@go-go-scope/adapter-koa 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 +114 -0
- package/dist/index.d.mts +76 -0
- package/dist/index.mjs +51 -0
- package/package.json +60 -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,114 @@
|
|
|
1
|
+
# @go-go-scope/adapter-koa
|
|
2
|
+
|
|
3
|
+
Koa 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-koa koa
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import Koa from 'koa'
|
|
17
|
+
import Router from '@koa/router'
|
|
18
|
+
import { koaGoGoScope, getScope, getRootScope } from '@go-go-scope/adapter-koa'
|
|
19
|
+
|
|
20
|
+
const app = new Koa()
|
|
21
|
+
const router = new Router()
|
|
22
|
+
|
|
23
|
+
// Apply middleware
|
|
24
|
+
app.use(koaGoGoScope({ name: 'my-api', metrics: true }))
|
|
25
|
+
|
|
26
|
+
// Use scope in routes
|
|
27
|
+
router.get('/users/:id', async (ctx) => {
|
|
28
|
+
const scope = getScope(ctx)
|
|
29
|
+
|
|
30
|
+
const [err, user] = await scope.task(
|
|
31
|
+
() => fetchUser(ctx.params.id),
|
|
32
|
+
{ retry: 'exponential', timeout: 5000 }
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
if (err) {
|
|
36
|
+
ctx.status = 500
|
|
37
|
+
ctx.body = { error: err.message }
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
ctx.body = user
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
app.use(router.routes())
|
|
45
|
+
app.listen(3000)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Options
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
app.use(koaGoGoScope({
|
|
52
|
+
name: 'my-api', // Root scope name
|
|
53
|
+
metrics: true, // Enable metrics collection
|
|
54
|
+
timeout: 30000, // Default request timeout
|
|
55
|
+
onError: (err, ctx) => { // Optional error handler
|
|
56
|
+
console.error('Scope error:', err)
|
|
57
|
+
}
|
|
58
|
+
}))
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Accessing Root Scope
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
// Access root scope from any context
|
|
65
|
+
app.use(async (ctx, next) => {
|
|
66
|
+
const rootScope = getRootScope(ctx)
|
|
67
|
+
const metrics = rootScope.metrics()
|
|
68
|
+
console.log('App metrics:', metrics)
|
|
69
|
+
await next()
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Graceful Shutdown
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { closeKoaScope } from '@go-go-scope/adapter-koa'
|
|
77
|
+
|
|
78
|
+
const server = app.listen(3000)
|
|
79
|
+
|
|
80
|
+
process.on('SIGTERM', async () => {
|
|
81
|
+
await closeKoaScope()
|
|
82
|
+
server.close(() => {
|
|
83
|
+
process.exit(0)
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## API
|
|
89
|
+
|
|
90
|
+
### `koaGoGoScope(options?)`
|
|
91
|
+
|
|
92
|
+
Creates Koa middleware that provides request-scoped structured concurrency.
|
|
93
|
+
|
|
94
|
+
**Options:**
|
|
95
|
+
- `name`: Root scope name (default: 'koa-app')
|
|
96
|
+
- `metrics`: Enable metrics collection (default: false)
|
|
97
|
+
- `timeout`: Default request timeout in ms
|
|
98
|
+
- `onError`: Error handler callback
|
|
99
|
+
|
|
100
|
+
### `getScope(ctx)`
|
|
101
|
+
|
|
102
|
+
Get the request-scoped scope from Koa context.
|
|
103
|
+
|
|
104
|
+
### `getRootScope(ctx)`
|
|
105
|
+
|
|
106
|
+
Get the root application scope from Koa context.
|
|
107
|
+
|
|
108
|
+
### `closeKoaScope()`
|
|
109
|
+
|
|
110
|
+
Gracefully shutdown the root scope (for use in SIGTERM handlers).
|
|
111
|
+
|
|
112
|
+
## License
|
|
113
|
+
|
|
114
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Scope } from 'go-go-scope';
|
|
2
|
+
import { Context, Middleware } from 'koa';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Koa adapter for go-go-scope
|
|
6
|
+
* Provides request-scoped structured concurrency for Koa applications
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare module "koa" {
|
|
10
|
+
interface DefaultState {
|
|
11
|
+
scope: Scope<Record<string, unknown>>;
|
|
12
|
+
rootScope: Scope<Record<string, unknown>>;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
interface KoaGoGoScopeOptions {
|
|
16
|
+
/** Root scope name */
|
|
17
|
+
name?: string;
|
|
18
|
+
/** Enable metrics collection */
|
|
19
|
+
metrics?: boolean;
|
|
20
|
+
/** Default timeout for all requests */
|
|
21
|
+
timeout?: number;
|
|
22
|
+
/** Optional error handler */
|
|
23
|
+
onError?: (error: Error, ctx: Context) => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Koa middleware for go-go-scope integration
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import Koa from 'koa'
|
|
31
|
+
* import { koaGoGoScope } from '@go-go-scope/adapter-koa'
|
|
32
|
+
*
|
|
33
|
+
* const app = new Koa()
|
|
34
|
+
* app.use(koaGoGoScope({ name: 'my-api', metrics: true }))
|
|
35
|
+
*
|
|
36
|
+
* app.use(async (ctx) => {
|
|
37
|
+
* const scope = ctx.state.scope
|
|
38
|
+
* const [err, user] = await scope.task(
|
|
39
|
+
* () => fetchUser(ctx.params.id),
|
|
40
|
+
* { retry: 'exponential' }
|
|
41
|
+
* )
|
|
42
|
+
*
|
|
43
|
+
* if (err) {
|
|
44
|
+
* ctx.status = 500
|
|
45
|
+
* ctx.body = { error: err.message }
|
|
46
|
+
* return
|
|
47
|
+
* }
|
|
48
|
+
* ctx.body = user
|
|
49
|
+
* })
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function koaGoGoScope(options?: KoaGoGoScopeOptions): Middleware;
|
|
53
|
+
/**
|
|
54
|
+
* Get the request-scoped scope from Koa context
|
|
55
|
+
*/
|
|
56
|
+
declare function getScope(ctx: Context): Scope<Record<string, unknown>>;
|
|
57
|
+
/**
|
|
58
|
+
* Get the root application scope from Koa context
|
|
59
|
+
*/
|
|
60
|
+
declare function getRootScope(ctx: Context): Scope<Record<string, unknown>>;
|
|
61
|
+
/**
|
|
62
|
+
* Graceful shutdown helper for Koa applications
|
|
63
|
+
* Disposes the root scope when the server is closing
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* process.on('SIGTERM', async () => {
|
|
68
|
+
* await closeKoaScope()
|
|
69
|
+
* server.close()
|
|
70
|
+
* })
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare function closeKoaScope(): Promise<void>;
|
|
74
|
+
|
|
75
|
+
export { closeKoaScope, getRootScope, getScope, koaGoGoScope };
|
|
76
|
+
export type { KoaGoGoScopeOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { scope } from 'go-go-scope';
|
|
2
|
+
|
|
3
|
+
let rootScope = null;
|
|
4
|
+
function koaGoGoScope(options = {}) {
|
|
5
|
+
const { name = "koa-app", metrics = false, timeout, onError } = options;
|
|
6
|
+
if (!rootScope) {
|
|
7
|
+
rootScope = scope({ name, metrics });
|
|
8
|
+
}
|
|
9
|
+
return async (ctx, next) => {
|
|
10
|
+
if (!rootScope) {
|
|
11
|
+
throw new Error("Root scope not initialized");
|
|
12
|
+
}
|
|
13
|
+
const scopeOptions = {
|
|
14
|
+
parent: rootScope,
|
|
15
|
+
name: `request-${ctx.request.url}`
|
|
16
|
+
};
|
|
17
|
+
if (timeout) scopeOptions.timeout = timeout;
|
|
18
|
+
const requestScope = scope(scopeOptions);
|
|
19
|
+
ctx.state.scope = requestScope;
|
|
20
|
+
ctx.state.rootScope = rootScope;
|
|
21
|
+
try {
|
|
22
|
+
await next();
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if (onError) {
|
|
25
|
+
onError(error, ctx);
|
|
26
|
+
}
|
|
27
|
+
throw error;
|
|
28
|
+
} finally {
|
|
29
|
+
await requestScope[Symbol.asyncDispose]().catch((err) => {
|
|
30
|
+
if (onError) {
|
|
31
|
+
onError(err, ctx);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function getScope(ctx) {
|
|
38
|
+
return ctx.state.scope;
|
|
39
|
+
}
|
|
40
|
+
function getRootScope(ctx) {
|
|
41
|
+
return ctx.state.rootScope;
|
|
42
|
+
}
|
|
43
|
+
async function closeKoaScope() {
|
|
44
|
+
if (rootScope) {
|
|
45
|
+
await rootScope[Symbol.asyncDispose]().catch(() => {
|
|
46
|
+
});
|
|
47
|
+
rootScope = null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { closeKoaScope, getRootScope, getScope, koaGoGoScope };
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@go-go-scope/adapter-koa",
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"description": "Koa 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
|
+
"koa",
|
|
16
|
+
"adapter",
|
|
17
|
+
"go-go-scope",
|
|
18
|
+
"structured-concurrency",
|
|
19
|
+
"middleware"
|
|
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-koa"
|
|
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
|
+
"koa": "^3.1.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@biomejs/biome": "^2.4.4",
|
|
46
|
+
"@types/koa": "^2.15.0",
|
|
47
|
+
"@types/node": "^24",
|
|
48
|
+
"koa": "^3.1.0",
|
|
49
|
+
"pkgroll": "^2.26.3",
|
|
50
|
+
"typescript": "^5.9.3",
|
|
51
|
+
"vitest": "^4.0.18"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "pkgroll --clean-dist",
|
|
55
|
+
"lint": "biome check --write src/",
|
|
56
|
+
"test": "vitest run --passWithNoTests",
|
|
57
|
+
"clean": "rm -rf dist",
|
|
58
|
+
"typecheck": "tsc --noEmit"
|
|
59
|
+
}
|
|
60
|
+
}
|