@atomservice/functions-sdk 0.1.6 → 0.1.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atomservice/functions-sdk",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "函数作者 SDK:定义器(defineFunction 等)、event 类型推导、schema 校验、结构化日志、内置 HTTP 运行时",
5
5
  "type": "module",
6
6
  "author": "openorson",
package/src/sdk.consts.ts CHANGED
@@ -1,4 +1,4 @@
1
- export const INVOKE_PATH_PREFIX = "/__invoke/"
1
+ export const INVOKE_PATH_PREFIX = "/invoke/"
2
2
  export const HEALTH_PATH = "/__health"
3
3
  export const DEFAULT_BUNDLE_PORT = 8080
4
4
  export const REQUEST_ID_HEADER = "x-atomfn-request-id"
@@ -54,7 +54,7 @@ interface ExecutorRouter {
54
54
  dispose: () => Promise<void>
55
55
  }
56
56
 
57
- function buildExecutorRouter(registry: FunctionRegistry, options: ServeOptions): ExecutorRouter {
57
+ function buildExecutorRouter<T extends FunctionRegistry>(registry: T, options: ServeOptions): ExecutorRouter {
58
58
  const inline = new InlineExecutor(registry, makeContextFactory(options))
59
59
  const workerEnv: Record<string, string> = {
60
60
  ...(process.env as Record<string, string>),
@@ -90,8 +90,8 @@ function overloaded(options: ServeOptions): boolean {
90
90
  return rssMb > options.memoryLimitMb * MEMORY_BACKPRESSURE_RATIO
91
91
  }
92
92
 
93
- export function serve(
94
- registry: FunctionRegistry,
93
+ export function serve<T extends FunctionRegistry>(
94
+ registry: T,
95
95
  overrides?: Partial<ServeOptions>,
96
96
  ): Bun.Server<undefined> | undefined {
97
97
  const options = resolveOptions(overrides)
@@ -114,6 +114,13 @@ export function serve(
114
114
  })
115
115
  }
116
116
 
117
+ const fnNames = Object.keys(registry)
118
+ console.log("\n%s", options.bundle)
119
+ for (const name of fnNames) {
120
+ console.log("- %s/%s", options.bundle, name)
121
+ console.log(" ")
122
+ }
123
+
117
124
  return Bun.serve({
118
125
  port: options.port,
119
126
  async fetch(request) {
@@ -124,7 +131,7 @@ export function serve(
124
131
  return Response.json({ ok: true, functions })
125
132
  }
126
133
 
127
- if (request.method === "POST" && url.pathname.startsWith(INVOKE_PATH_PREFIX)) {
134
+ if ((request.method === "GET" || request.method === "POST") && url.pathname.startsWith(INVOKE_PATH_PREFIX)) {
128
135
  const name = url.pathname.slice(INVOKE_PATH_PREFIX.length)
129
136
  const def = registry[name]
130
137
  if (!def) {
@@ -148,16 +155,23 @@ export function serve(
148
155
  }
149
156
 
150
157
  const id = requestId(request)
151
- const body = await readBody(request)
158
+ const queryParams: Record<string, unknown> = {}
159
+ url.searchParams.forEach((v, k) => {
160
+ queryParams[k] = v
161
+ })
162
+ const bodyParams = await readBody(request)
163
+ const event = typeof bodyParams === "object" && bodyParams !== null
164
+ ? { ...queryParams, ...(bodyParams as Record<string, unknown>) }
165
+ : queryParams
152
166
  const deps = { executor: router.pick(def), name, requestId: id, defaultTimeout: options.defaultTimeout }
153
167
 
154
168
  if (isStreamKind(def)) {
155
- const response = invokeStream(def, body, deps)
169
+ const response = invokeStream(def, event, deps)
156
170
  response.headers.set(REQUEST_ID_HEADER, id)
157
171
  return response
158
172
  }
159
173
 
160
- const { status, envelope } = await invokeOneShot(def, body, deps)
174
+ const { status, envelope } = await invokeOneShot(def, event, deps)
161
175
  return Response.json(envelope, { status, headers: { [REQUEST_ID_HEADER]: id } })
162
176
  }
163
177