@autonoma-ai/server-node 0.1.0 → 0.1.3
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 +59 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @autonoma-ai/server-node
|
|
2
|
+
|
|
3
|
+
Node.js `http` server adapter for the Autonoma SDK.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @autonoma-ai/sdk @autonoma-ai/sdk-prisma @autonoma-ai/server-node
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import http from 'node:http'
|
|
15
|
+
import { createNodeHandler } from '@autonoma-ai/server-node'
|
|
16
|
+
import { prismaAdapter } from '@autonoma-ai/sdk-prisma'
|
|
17
|
+
import { prisma } from './db'
|
|
18
|
+
|
|
19
|
+
const handler = createNodeHandler({
|
|
20
|
+
adapter: prismaAdapter(prisma, { scopeField: 'organizationId' }),
|
|
21
|
+
sharedSecret: process.env.AUTONOMA_SHARED_SECRET!,
|
|
22
|
+
signingSecret: process.env.AUTONOMA_SIGNING_SECRET!,
|
|
23
|
+
auth: async (user) => {
|
|
24
|
+
const session = await createSession(user.id as string)
|
|
25
|
+
return { token: session.token }
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
http.createServer((req, res) => {
|
|
30
|
+
if (req.method === 'POST' && req.url === '/api/autonoma') {
|
|
31
|
+
return handler(req, res)
|
|
32
|
+
}
|
|
33
|
+
res.writeHead(404).end()
|
|
34
|
+
}).listen(3000)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Auth callback
|
|
38
|
+
|
|
39
|
+
The `auth` callback receives the first `User` created during setup and must return real credentials that the test runner can use to log in:
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
// Session cookie
|
|
43
|
+
auth: async (user) => {
|
|
44
|
+
const session = await createSession(user.id as string)
|
|
45
|
+
return {
|
|
46
|
+
cookies: [{ name: 'session', value: session.token, httpOnly: true, sameSite: 'lax', path: '/' }],
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Bearer token
|
|
51
|
+
auth: async (user) => {
|
|
52
|
+
const token = jwt.sign({ sub: user.id }, SECRET)
|
|
53
|
+
return { token }
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Documentation
|
|
58
|
+
|
|
59
|
+
Full docs: [docs/](../../docs/) — see [setup guide](../../docs/setup.txt).
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { handleRequest } from "@autonoma-ai/sdk";
|
|
3
3
|
function createNodeHandler(config) {
|
|
4
|
+
const enrichedConfig = { ...config, sdk: { ...config.sdk, server: "node" } };
|
|
4
5
|
return async (req, res) => {
|
|
5
6
|
const body = await readBody(req);
|
|
6
7
|
const headers = {};
|
|
@@ -9,7 +10,7 @@ function createNodeHandler(config) {
|
|
|
9
10
|
else if (Array.isArray(val)) headers[key] = val[0] ?? "";
|
|
10
11
|
}
|
|
11
12
|
const handlerReq = { body, headers };
|
|
12
|
-
const result = await handleRequest(
|
|
13
|
+
const result = await handleRequest(enrichedConfig, handlerReq);
|
|
13
14
|
res.writeHead(result.status, { "Content-Type": "application/json" });
|
|
14
15
|
res.end(JSON.stringify(result.body));
|
|
15
16
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from 'node:http'\nimport { handleRequest } from '@autonoma-ai/sdk'\nimport type { HandlerConfig, HandlerRequest } from '@autonoma-ai/sdk'\n\n/**\n * Create a Node.js http handler.\n *\n * @example\n * ```ts\n * import { createNodeHandler } from '@autonoma-ai/server-node'\n * import http from 'node:http'\n * http.createServer(createNodeHandler(config)).listen(3000)\n * ```\n */\nexport function createNodeHandler(config: HandlerConfig) {\n return async (req: IncomingMessage, res: ServerResponse): Promise<void> => {\n const body = await readBody(req)\n const headers: Record<string, string> = {}\n for (const [key, val] of Object.entries(req.headers)) {\n if (typeof val === 'string') headers[key] = val\n else if (Array.isArray(val)) headers[key] = val[0] ?? ''\n }\n\n const handlerReq: HandlerRequest = { body, headers }\n const result = await handleRequest(
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from 'node:http'\nimport { handleRequest } from '@autonoma-ai/sdk'\nimport type { HandlerConfig, HandlerRequest } from '@autonoma-ai/sdk'\n\n/**\n * Create a Node.js http handler.\n *\n * @example\n * ```ts\n * import { createNodeHandler } from '@autonoma-ai/server-node'\n * import http from 'node:http'\n * http.createServer(createNodeHandler(config)).listen(3000)\n * ```\n */\nexport function createNodeHandler(config: HandlerConfig) {\n const enrichedConfig = { ...config, sdk: { ...config.sdk, server: 'node' } }\n return async (req: IncomingMessage, res: ServerResponse): Promise<void> => {\n const body = await readBody(req)\n const headers: Record<string, string> = {}\n for (const [key, val] of Object.entries(req.headers)) {\n if (typeof val === 'string') headers[key] = val\n else if (Array.isArray(val)) headers[key] = val[0] ?? ''\n }\n\n const handlerReq: HandlerRequest = { body, headers }\n const result = await handleRequest(enrichedConfig, handlerReq)\n\n res.writeHead(result.status, { 'Content-Type': 'application/json' })\n res.end(JSON.stringify(result.body))\n }\n}\n\nfunction readBody(req: IncomingMessage): Promise<string> {\n return new Promise((resolve, reject) => {\n const chunks: Buffer[] = []\n req.on('data', (chunk: Buffer) => chunks.push(chunk))\n req.on('end', () => resolve(Buffer.concat(chunks).toString()))\n req.on('error', reject)\n })\n}\n"],"mappings":";AACA,SAAS,qBAAqB;AAavB,SAAS,kBAAkB,QAAuB;AACvD,QAAM,iBAAiB,EAAE,GAAG,QAAQ,KAAK,EAAE,GAAG,OAAO,KAAK,QAAQ,OAAO,EAAE;AAC3E,SAAO,OAAO,KAAsB,QAAuC;AACzE,UAAM,OAAO,MAAM,SAAS,GAAG;AAC/B,UAAM,UAAkC,CAAC;AACzC,eAAW,CAAC,KAAK,GAAG,KAAK,OAAO,QAAQ,IAAI,OAAO,GAAG;AACpD,UAAI,OAAO,QAAQ,SAAU,SAAQ,GAAG,IAAI;AAAA,eACnC,MAAM,QAAQ,GAAG,EAAG,SAAQ,GAAG,IAAI,IAAI,CAAC,KAAK;AAAA,IACxD;AAEA,UAAM,aAA6B,EAAE,MAAM,QAAQ;AACnD,UAAM,SAAS,MAAM,cAAc,gBAAgB,UAAU;AAE7D,QAAI,UAAU,OAAO,QAAQ,EAAE,gBAAgB,mBAAmB,CAAC;AACnE,QAAI,IAAI,KAAK,UAAU,OAAO,IAAI,CAAC;AAAA,EACrC;AACF;AAEA,SAAS,SAAS,KAAuC;AACvD,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,SAAmB,CAAC;AAC1B,QAAI,GAAG,QAAQ,CAAC,UAAkB,OAAO,KAAK,KAAK,CAAC;AACpD,QAAI,GAAG,OAAO,MAAM,QAAQ,OAAO,OAAO,MAAM,EAAE,SAAS,CAAC,CAAC;AAC7D,QAAI,GAAG,SAAS,MAAM;AAAA,EACxB,CAAC;AACH;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autonoma-ai/server-node",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Node.js http server adapter for Autonoma SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@autonoma-ai/sdk": "0.1.
|
|
19
|
+
"@autonoma-ai/sdk": "0.1.3"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/node": "^22.0.0",
|