@atomservice/functions-sdk 0.1.6 → 0.1.7

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.7",
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,12 @@ 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
+ }
122
+
117
123
  return Bun.serve({
118
124
  port: options.port,
119
125
  async fetch(request) {
@@ -124,7 +130,7 @@ export function serve(
124
130
  return Response.json({ ok: true, functions })
125
131
  }
126
132
 
127
- if (request.method === "POST" && url.pathname.startsWith(INVOKE_PATH_PREFIX)) {
133
+ if ((request.method === "GET" || request.method === "POST") && url.pathname.startsWith(INVOKE_PATH_PREFIX)) {
128
134
  const name = url.pathname.slice(INVOKE_PATH_PREFIX.length)
129
135
  const def = registry[name]
130
136
  if (!def) {
@@ -148,16 +154,23 @@ export function serve(
148
154
  }
149
155
 
150
156
  const id = requestId(request)
151
- const body = await readBody(request)
157
+ const queryParams: Record<string, unknown> = {}
158
+ url.searchParams.forEach((v, k) => {
159
+ queryParams[k] = v
160
+ })
161
+ const bodyParams = await readBody(request)
162
+ const event = typeof bodyParams === "object" && bodyParams !== null
163
+ ? { ...queryParams, ...(bodyParams as Record<string, unknown>) }
164
+ : queryParams
152
165
  const deps = { executor: router.pick(def), name, requestId: id, defaultTimeout: options.defaultTimeout }
153
166
 
154
167
  if (isStreamKind(def)) {
155
- const response = invokeStream(def, body, deps)
168
+ const response = invokeStream(def, event, deps)
156
169
  response.headers.set(REQUEST_ID_HEADER, id)
157
170
  return response
158
171
  }
159
172
 
160
- const { status, envelope } = await invokeOneShot(def, body, deps)
173
+ const { status, envelope } = await invokeOneShot(def, event, deps)
161
174
  return Response.json(envelope, { status, headers: { [REQUEST_ID_HEADER]: id } })
162
175
  }
163
176