@atom-forge/rpc 0.3.2
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 +28 -0
- package/README.en.md +612 -0
- package/README.hu.md +613 -0
- package/README.llm.md +343 -0
- package/README.md +25 -0
- package/dist/client/client-context.d.ts +45 -0
- package/dist/client/client-context.js +48 -0
- package/dist/client/create-client.d.ts +9 -0
- package/dist/client/create-client.js +277 -0
- package/dist/client/logger.d.ts +6 -0
- package/dist/client/logger.js +41 -0
- package/dist/client/middleware.d.ts +6 -0
- package/dist/client/middleware.js +7 -0
- package/dist/client/rpc-response.d.ts +27 -0
- package/dist/client/rpc-response.js +46 -0
- package/dist/client/types.d.ts +151 -0
- package/dist/client/types.js +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/server/create-handler.d.ts +18 -0
- package/dist/server/create-handler.js +210 -0
- package/dist/server/errors.d.ts +10 -0
- package/dist/server/errors.js +14 -0
- package/dist/server/middleware.d.ts +22 -0
- package/dist/server/middleware.js +39 -0
- package/dist/server/rpc.d.ts +65 -0
- package/dist/server/rpc.js +49 -0
- package/dist/server/server-context.d.ts +79 -0
- package/dist/server/server-context.js +86 -0
- package/dist/server/types.d.ts +30 -0
- package/dist/server/types.js +1 -0
- package/dist/util/constants.d.ts +1 -0
- package/dist/util/constants.js +1 -0
- package/dist/util/cookies.d.ts +22 -0
- package/dist/util/cookies.js +54 -0
- package/dist/util/pipeline.d.ts +23 -0
- package/dist/util/pipeline.js +22 -0
- package/dist/util/string.d.ts +6 -0
- package/dist/util/string.js +11 -0
- package/dist/util/types.d.ts +5 -0
- package/dist/util/types.js +1 -0
- package/package.json +38 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
function parseCookieHeader(header) {
|
|
2
|
+
const map = new Map();
|
|
3
|
+
for (const pair of header.split(";")) {
|
|
4
|
+
const idx = pair.indexOf("=");
|
|
5
|
+
if (idx === -1)
|
|
6
|
+
continue;
|
|
7
|
+
const name = pair.slice(0, idx).trim();
|
|
8
|
+
const value = pair.slice(idx + 1).trim();
|
|
9
|
+
if (name)
|
|
10
|
+
map.set(name, decodeURIComponent(value));
|
|
11
|
+
}
|
|
12
|
+
return map;
|
|
13
|
+
}
|
|
14
|
+
function serializeCookie(name, value, options = {}) {
|
|
15
|
+
let str = `${name}=${encodeURIComponent(value)}`;
|
|
16
|
+
if (options.maxAge != null)
|
|
17
|
+
str += `; Max-Age=${Math.floor(options.maxAge)}`;
|
|
18
|
+
if (options.expires)
|
|
19
|
+
str += `; Expires=${options.expires.toUTCString()}`;
|
|
20
|
+
if (options.path != null)
|
|
21
|
+
str += `; Path=${options.path}`;
|
|
22
|
+
else
|
|
23
|
+
str += "; Path=/";
|
|
24
|
+
if (options.domain)
|
|
25
|
+
str += `; Domain=${options.domain}`;
|
|
26
|
+
if (options.secure)
|
|
27
|
+
str += "; Secure";
|
|
28
|
+
if (options.httpOnly)
|
|
29
|
+
str += "; HttpOnly";
|
|
30
|
+
if (options.sameSite)
|
|
31
|
+
str += `; SameSite=${options.sameSite}`;
|
|
32
|
+
return str;
|
|
33
|
+
}
|
|
34
|
+
export class CookieManager {
|
|
35
|
+
constructor(requestHeaders, responseHeaders) {
|
|
36
|
+
this.requestHeaders = requestHeaders;
|
|
37
|
+
this.responseHeaders = responseHeaders;
|
|
38
|
+
this.parsed = parseCookieHeader(requestHeaders.get("Cookie") || "");
|
|
39
|
+
}
|
|
40
|
+
get(name) {
|
|
41
|
+
return this.parsed.get(name);
|
|
42
|
+
}
|
|
43
|
+
getAll() {
|
|
44
|
+
return Array.from(this.parsed.entries()).map(([name, value]) => ({ name, value }));
|
|
45
|
+
}
|
|
46
|
+
set(name, value, options) {
|
|
47
|
+
this.parsed.set(name, value);
|
|
48
|
+
this.responseHeaders.append("Set-Cookie", serializeCookie(name, value, options));
|
|
49
|
+
}
|
|
50
|
+
delete(name, options) {
|
|
51
|
+
this.parsed.delete(name);
|
|
52
|
+
this.responseHeaders.append("Set-Cookie", serializeCookie(name, "", { ...options, maxAge: 0 }));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a middleware function.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the state object.
|
|
5
|
+
* @param state - The state object to pass through the middleware.
|
|
6
|
+
* @param next - The next middleware function in the pipeline.
|
|
7
|
+
* @returns A promise that resolves after executing the middleware.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export type Middleware<STATE = any, RESULT = any> = (state: STATE, next: () => Promise<RESULT>) => Promise<RESULT>;
|
|
12
|
+
/**
|
|
13
|
+
* Executes a pipeline of middlewares in a specific order.
|
|
14
|
+
*
|
|
15
|
+
* @param {STATE} state - The state object to pass through the pipeline.
|
|
16
|
+
* @param {Array<Middleware<STATE, RESULT>>} middlewares - The middlewares to execute in the pipeline.
|
|
17
|
+
* @returns {Promise<any>} A promise that resolves after executing all middlewares in the pipeline.
|
|
18
|
+
* @template STATE - The type of the state object.
|
|
19
|
+
* @template RESULT - The type of the result object.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export declare function pipeline<STATE = any, RESULT = any>(state: STATE, ...middlewares: Array<Middleware<STATE, RESULT>>): Promise<RESULT>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Executes a pipeline of middlewares in a specific order.
|
|
3
|
+
*
|
|
4
|
+
* @param {STATE} state - The state object to pass through the pipeline.
|
|
5
|
+
* @param {Array<Middleware<STATE, RESULT>>} middlewares - The middlewares to execute in the pipeline.
|
|
6
|
+
* @returns {Promise<any>} A promise that resolves after executing all middlewares in the pipeline.
|
|
7
|
+
* @template STATE - The type of the state object.
|
|
8
|
+
* @template RESULT - The type of the result object.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export async function pipeline(state, ...middlewares) {
|
|
13
|
+
return await execute(state, middlewares, 0);
|
|
14
|
+
}
|
|
15
|
+
async function execute(state, middlewares, index) {
|
|
16
|
+
const middleware = middlewares[index];
|
|
17
|
+
if (!middleware) {
|
|
18
|
+
throw new Error("Pipeline exhausted. Make sure the last middleware in the chain returns a result without calling next().");
|
|
19
|
+
}
|
|
20
|
+
const next = () => execute(state, middlewares, index + 1);
|
|
21
|
+
return await middleware(state, next);
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a camelCase string to kebab-case.
|
|
3
|
+
* Handles acronyms correctly: getUserID → get-user-id
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export function camelToKebabCase(str) {
|
|
7
|
+
return str
|
|
8
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
9
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2")
|
|
10
|
+
.toLowerCase();
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@atom-forge/rpc",
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "Type-safe RPC framework for TypeScript with Zod validation and middleware support",
|
|
5
|
+
"keywords": ["rpc", "typescript", "zod", "type-safe", "middleware", "api"],
|
|
6
|
+
"author": "Gergely Laborci",
|
|
7
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"url": "git://github.com/atom-forge/rpc.git"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"msgpackr": "^1.11.8"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"zod": ">=4.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"typescript": "^5.0.0",
|
|
35
|
+
"@types/node": "^25.3.1",
|
|
36
|
+
"zod": "^4.3.6"
|
|
37
|
+
}
|
|
38
|
+
}
|