@go-go-scope/adapter-sveltekit 2.5.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/dist/index.d.mts +114 -0
- package/dist/index.mjs +124 -0
- package/package.json +47 -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/dist/index.d.mts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { RequestEvent, Handle, ServerLoadEvent } from '@sveltejs/kit';
|
|
2
|
+
import { Scope } from 'go-go-scope';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* SvelteKit adapter for go-go-scope
|
|
6
|
+
*
|
|
7
|
+
* Provides structured concurrency for SvelteKit server hooks,
|
|
8
|
+
* API routes, and form actions with automatic cleanup.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // src/hooks.server.ts
|
|
13
|
+
* import { createScopeHandle } from '@go-go-scope/adapter-sveltekit'
|
|
14
|
+
*
|
|
15
|
+
* export const handle = createScopeHandle({
|
|
16
|
+
* timeout: 30000,
|
|
17
|
+
* services: { db: createDatabase() }
|
|
18
|
+
* })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* SvelteKit context with scope
|
|
24
|
+
*/
|
|
25
|
+
interface SvelteKitContext<T extends Record<string, unknown> = Record<string, never>> {
|
|
26
|
+
/** The scope for this request */
|
|
27
|
+
scope: Scope<T>;
|
|
28
|
+
/** Request signal for cancellation */
|
|
29
|
+
signal: AbortSignal;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Options for scope configuration
|
|
33
|
+
*/
|
|
34
|
+
interface SvelteKitScopeOptions<T extends Record<string, unknown> = Record<string, never>> {
|
|
35
|
+
/** Scope timeout in milliseconds */
|
|
36
|
+
timeout?: number;
|
|
37
|
+
/** Initial services to provide */
|
|
38
|
+
services?: T;
|
|
39
|
+
/** Error handler */
|
|
40
|
+
onError?: (error: unknown, event: RequestEvent) => Response | Promise<Response>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create a SvelteKit handle with scope integration
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // src/hooks.server.ts
|
|
48
|
+
* import { createScopeHandle } from '@go-go-scope/adapter-sveltekit'
|
|
49
|
+
* import { sequence } from '@sveltejs/kit/hooks'
|
|
50
|
+
*
|
|
51
|
+
* export const handle = sequence(
|
|
52
|
+
* createScopeHandle({ timeout: 30000 }),
|
|
53
|
+
* async ({ event, resolve }) => resolve(event)
|
|
54
|
+
* )
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function createScopeHandle<T extends Record<string, unknown>>(options?: SvelteKitScopeOptions<T>): Handle;
|
|
58
|
+
/**
|
|
59
|
+
* Wrap a server load function with a scope
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* // src/routes/+page.server.ts
|
|
64
|
+
* import { withScopeLoad } from '@go-go-scope/adapter-sveltekit'
|
|
65
|
+
*
|
|
66
|
+
* export const load = withScopeLoad(async (event) => {
|
|
67
|
+
* const { scope } = event.locals as { scope: Scope }
|
|
68
|
+
* const [err, data] = await scope.task(() => fetchData())
|
|
69
|
+
* if (err) throw error(500, err.message)
|
|
70
|
+
* return { data }
|
|
71
|
+
* })
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function withScopeLoad<T extends Record<string, unknown>, R extends Record<string, unknown>>(loader: (event: ServerLoadEvent & {
|
|
75
|
+
scope: Scope<T>;
|
|
76
|
+
}) => Promise<R> | R, options?: Omit<SvelteKitScopeOptions<T>, "services">): (event: ServerLoadEvent) => Promise<R>;
|
|
77
|
+
/**
|
|
78
|
+
* Wrap a form action with a scope
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // src/routes/+page.server.ts
|
|
83
|
+
* import { withScopeAction } from '@go-go-scope/adapter-sveltekit'
|
|
84
|
+
* import { fail } from '@sveltejs/kit'
|
|
85
|
+
*
|
|
86
|
+
* export const actions = {
|
|
87
|
+
* create: withScopeAction(async (event) => {
|
|
88
|
+
* const { scope } = event.locals as { scope: Scope }
|
|
89
|
+
* const data = await event.request.formData()
|
|
90
|
+
*
|
|
91
|
+
* const [err, result] = await scope.task(() => createItem(data))
|
|
92
|
+
* if (err) return fail(400, { error: err.message })
|
|
93
|
+
* return { success: true, result }
|
|
94
|
+
* })
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare function withScopeAction<T extends Record<string, unknown>, R extends Record<string, unknown>>(action: (event: RequestEvent & {
|
|
99
|
+
scope: Scope<T>;
|
|
100
|
+
}) => Promise<R> | R, options?: Omit<SvelteKitScopeOptions<T>, "services">): (event: RequestEvent) => Promise<R>;
|
|
101
|
+
/**
|
|
102
|
+
* Type augmentation for SvelteKit locals
|
|
103
|
+
*/
|
|
104
|
+
declare global {
|
|
105
|
+
namespace App {
|
|
106
|
+
interface Locals {
|
|
107
|
+
scope?: Scope;
|
|
108
|
+
signal?: AbortSignal;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { createScopeHandle, withScopeAction, withScopeLoad };
|
|
114
|
+
export type { SvelteKitContext, SvelteKitScopeOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { scope } from 'go-go-scope';
|
|
2
|
+
|
|
3
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
4
|
+
var __typeError = (msg) => {
|
|
5
|
+
throw TypeError(msg);
|
|
6
|
+
};
|
|
7
|
+
var __using = (stack, value, async) => {
|
|
8
|
+
if (value != null) {
|
|
9
|
+
if (typeof value !== "object" && typeof value !== "function") __typeError("Object expected");
|
|
10
|
+
var dispose, inner;
|
|
11
|
+
dispose = value[__knownSymbol("asyncDispose")];
|
|
12
|
+
if (dispose === void 0) {
|
|
13
|
+
dispose = value[__knownSymbol("dispose")];
|
|
14
|
+
inner = dispose;
|
|
15
|
+
}
|
|
16
|
+
if (typeof dispose !== "function") __typeError("Object not disposable");
|
|
17
|
+
if (inner) dispose = function() {
|
|
18
|
+
try {
|
|
19
|
+
inner.call(this);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
return Promise.reject(e);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
stack.push([async, dispose, value]);
|
|
25
|
+
} else {
|
|
26
|
+
stack.push([async]);
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
var __callDispose = (stack, error, hasError) => {
|
|
31
|
+
var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
|
|
32
|
+
return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
|
|
33
|
+
};
|
|
34
|
+
var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
|
|
35
|
+
var next = (it) => {
|
|
36
|
+
while (it = stack.pop()) {
|
|
37
|
+
try {
|
|
38
|
+
var result = it[1] && it[1].call(it[2]);
|
|
39
|
+
if (it[0]) return Promise.resolve(result).then(next, (e) => (fail(e), next()));
|
|
40
|
+
} catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (hasError) throw error;
|
|
45
|
+
};
|
|
46
|
+
return next();
|
|
47
|
+
};
|
|
48
|
+
function createScopeHandle(options = {}) {
|
|
49
|
+
return async ({ event, resolve }) => {
|
|
50
|
+
var _stack = [];
|
|
51
|
+
try {
|
|
52
|
+
const abortController = new AbortController();
|
|
53
|
+
const s = __using(_stack, scope({
|
|
54
|
+
timeout: options.timeout,
|
|
55
|
+
signal: abortController.signal,
|
|
56
|
+
name: `sveltekit-${event.request.method}-${event.url.pathname}`
|
|
57
|
+
}), true);
|
|
58
|
+
if (options.services) {
|
|
59
|
+
for (const [key, value] of Object.entries(options.services)) {
|
|
60
|
+
s.provide(key, value);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
event.locals.scope = s;
|
|
64
|
+
event.locals.signal = abortController.signal;
|
|
65
|
+
try {
|
|
66
|
+
return await resolve(event);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (options.onError) {
|
|
69
|
+
return await options.onError(error, event);
|
|
70
|
+
}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
} catch (_) {
|
|
74
|
+
var _error = _, _hasError = true;
|
|
75
|
+
} finally {
|
|
76
|
+
var _promise = __callDispose(_stack, _error, _hasError);
|
|
77
|
+
_promise && await _promise;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function withScopeLoad(loader, options = {}) {
|
|
82
|
+
return async (event) => {
|
|
83
|
+
var _stack = [];
|
|
84
|
+
try {
|
|
85
|
+
const existingScope = event.locals.scope;
|
|
86
|
+
if (existingScope) {
|
|
87
|
+
return await loader({ ...event, scope: existingScope });
|
|
88
|
+
}
|
|
89
|
+
const s = __using(_stack, scope({
|
|
90
|
+
timeout: options.timeout,
|
|
91
|
+
name: `sveltekit-load-${event.url.pathname}`
|
|
92
|
+
}), true);
|
|
93
|
+
return await loader({ ...event, scope: s });
|
|
94
|
+
} catch (_) {
|
|
95
|
+
var _error = _, _hasError = true;
|
|
96
|
+
} finally {
|
|
97
|
+
var _promise = __callDispose(_stack, _error, _hasError);
|
|
98
|
+
_promise && await _promise;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function withScopeAction(action, options = {}) {
|
|
103
|
+
return async (event) => {
|
|
104
|
+
var _stack = [];
|
|
105
|
+
try {
|
|
106
|
+
const existingScope = event.locals.scope;
|
|
107
|
+
if (existingScope) {
|
|
108
|
+
return await action({ ...event, scope: existingScope });
|
|
109
|
+
}
|
|
110
|
+
const s = __using(_stack, scope({
|
|
111
|
+
timeout: options.timeout,
|
|
112
|
+
name: `sveltekit-action-${event.url.pathname}`
|
|
113
|
+
}), true);
|
|
114
|
+
return await action({ ...event, scope: s });
|
|
115
|
+
} catch (_) {
|
|
116
|
+
var _error = _, _hasError = true;
|
|
117
|
+
} finally {
|
|
118
|
+
var _promise = __callDispose(_stack, _error, _hasError);
|
|
119
|
+
_promise && await _promise;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export { createScopeHandle, withScopeAction, withScopeLoad };
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@go-go-scope/adapter-sveltekit",
|
|
3
|
+
"version": "2.5.0",
|
|
4
|
+
"description": "SvelteKit adapter for go-go-scope",
|
|
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
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"structured-concurrency",
|
|
19
|
+
"sveltekit",
|
|
20
|
+
"svelte",
|
|
21
|
+
"server-hooks",
|
|
22
|
+
"typescript"
|
|
23
|
+
],
|
|
24
|
+
"author": "Your Name",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"go-go-scope": "^2.4.0",
|
|
28
|
+
"@sveltejs/kit": "^2.0.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@sveltejs/kit": "^2.0.0",
|
|
32
|
+
"@types/node": "^22.13.15",
|
|
33
|
+
"pkgroll": "^2.6.1",
|
|
34
|
+
"svelte": "^5.0.0",
|
|
35
|
+
"typescript": "^5.8.3",
|
|
36
|
+
"vitest": "^3.1.1",
|
|
37
|
+
"go-go-scope": "2.5.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "pkgroll",
|
|
41
|
+
"typecheck": "tsc --noEmit",
|
|
42
|
+
"lint": "biome check --fix src",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"test:watch": "vitest",
|
|
45
|
+
"clean": "rm -rf dist"
|
|
46
|
+
}
|
|
47
|
+
}
|