@neondatabase/functions 0.4.0 → 0.6.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/README.md CHANGED
@@ -1,19 +1,21 @@
1
- # @neondatabase/functions
1
+ # @neon/functions
2
2
 
3
3
  Runtime helpers for [Neon Functions](https://neon.com). Currently provides a `waitUntil` primitive for deferring background work past a response.
4
4
 
5
5
  ## Install
6
6
 
7
7
  ```bash
8
- npm install @neondatabase/functions
8
+ npm install @neon/functions
9
9
  ```
10
10
 
11
+ > **Requirements:** Node.js >= 20.19.
12
+
11
13
  ## Usage
12
14
 
13
15
  The API mirrors [`@vercel/functions`](https://vercel.com/docs/functions/functions-api-reference/vercel-functions-package): import `waitUntil` and call it directly with the promise you want to keep alive.
14
16
 
15
17
  ```ts
16
- import { waitUntil } from "@neondatabase/functions";
18
+ import { waitUntil } from "@neon/functions";
17
19
 
18
20
  export default {
19
21
  async fetch(req: Request): Promise<Response> {
@@ -27,26 +29,13 @@ export default {
27
29
  `waitUntil(promise)` forwards the promise to the Neon Functions runtime, which keeps
28
30
  the invocation alive until the promise settles (up to the 15-minute `waitUntil` limit).
29
31
  When no invocation context is in scope — local dev, tests, or any non-Neon host — it is
30
- a **no-op**: the promise is accepted and ignored, so the same code runs everywhere
31
- without branching. Passing a non-`Promise` throws a `TypeError`.
32
+ a **no-op**: the promise is accepted and ignored (it still runs on its own, it just
33
+ isn't tracked), so the same code runs everywhere without branching. Passing a
34
+ non-`Promise` throws a `TypeError`.
32
35
 
33
36
  ## Runtime integration
34
37
 
35
- The runtime carries the per-invocation context in an `AsyncLocalStorage` and publishes
36
- it at `globalThis.NEON_REQUEST_CONTEXT` (the key exported as `NEON_REQUEST_CONTEXT_KEY`)
37
- as a getter that returns the live context object **directly** — `{ waitUntil }` during
38
- an invocation, `undefined` outside one. `waitUntil` reads that value straight off the
39
- key, so there is no `.get()` provider indirection.
40
-
41
- To make `waitUntil` resolve to a given invocation, wrap the handler with
42
- `runWithRequestContext`. This is intended for the Neon Functions runtime; application
43
- code should not need it.
44
-
45
- ```ts
46
- import { runWithRequestContext } from "@neondatabase/functions";
47
-
48
- runWithRequestContext({ waitUntil: realWaitUntil }, () => handler(req));
49
- ```
50
-
51
- Because the context lives in `AsyncLocalStorage`, concurrent invocations in the same
52
- isolate each see their own context and never clobber one another.
38
+ The runtime publishes the active invocation context on `globalThis.NEON_REQUEST_CONTEXT`
39
+ as a getter that returns the live context object directly — `{ waitUntil }` during an
40
+ invocation, `undefined` outside one. `waitUntil` reads that value straight off the
41
+ global, so there is nothing for application code to wire up.
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { NEON_REQUEST_CONTEXT_KEY, NeonFunctionsContext, WaitUntil, runWithRequestContext, waitUntil } from "./lib/wait-until.js";
2
- export { NEON_REQUEST_CONTEXT_KEY, type NeonFunctionsContext, type WaitUntil, runWithRequestContext, waitUntil };
1
+ import { waitUntil } from "./lib/wait-until.js";
2
+ export { waitUntil };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { NEON_REQUEST_CONTEXT_KEY, runWithRequestContext, waitUntil } from "./lib/wait-until.js";
2
- export { NEON_REQUEST_CONTEXT_KEY, runWithRequestContext, waitUntil };
1
+ import { waitUntil } from "./lib/wait-until.js";
2
+ export { waitUntil };
@@ -1,37 +1,20 @@
1
1
  //#region src/lib/wait-until.d.ts
2
- type WaitUntil = (promise: Promise<unknown>) => void;
3
2
  /**
4
- * The slice of the runtime context this package reads. The runtime may attach
5
- * additional fields; only `waitUntil` is consumed here.
6
- */
7
- type NeonFunctionsContext = {
8
- waitUntil?: WaitUntil;
9
- };
10
- /**
11
- * Well-known `globalThis` key under which the Neon Functions runtime publishes the
12
- * current invocation context. The runtime installs it as a getter that returns the
13
- * live context object DIRECTLY — `globalThis.NEON_REQUEST_CONTEXT` is `{ waitUntil }`
14
- * during an invocation and `undefined` outside one — so it is read as the context
15
- * itself, not via a `.get()`-style provider.
16
- */
17
- declare const NEON_REQUEST_CONTEXT_KEY = "NEON_REQUEST_CONTEXT";
18
- /**
19
- * Defers async work past the response by forwarding the promise to the Neon Functions
20
- * runtime, which keeps the invocation alive until the promise settles.
3
+ * `waitUntil(promise)` defers async work past a Neon Function's response: the runtime
4
+ * keeps the invocation alive until the promise settles (up to the 15-minute limit).
21
5
  *
22
- * The context is resolved at call time from the enclosing invocation, so this stays
23
- * correct under concurrency. When no invocation context is in scope (local dev, tests,
24
- * non-Neon hosts), this is a no-op: the promise is accepted and ignored (it still runs
25
- * on its own the caller already started it it just isn't tracked).
6
+ * The runtime publishes the active invocation context on `globalThis.NEON_REQUEST_CONTEXT`
7
+ * (a getter returning `{ waitUntil }` during an invocation, `undefined` outside one), so we
8
+ * read it directly. Off-platform local dev, tests, non-Neon hosts there is no context
9
+ * and this is a no-op, mirroring `@vercel/functions`: the promise the caller created still
10
+ * runs on its own, it just isn't tracked. Passing a non-Promise throws a `TypeError`.
26
11
  */
12
+ declare global {
13
+ var NEON_REQUEST_CONTEXT: {
14
+ waitUntil?: (promise: Promise<unknown>) => void;
15
+ } | undefined;
16
+ }
27
17
  declare function waitUntil(promise: Promise<unknown>): void;
28
- /**
29
- * Runtime entry point: binds `context` as the current invocation context for the
30
- * duration of `fn` (and any async work it spawns), so calls to `waitUntil` inside it
31
- * forward to `context.waitUntil`. Intended for the Neon Functions runtime to wrap each
32
- * invocation; application code should not need this.
33
- */
34
- declare function runWithRequestContext<T>(context: NeonFunctionsContext, fn: () => T): T;
35
18
  //#endregion
36
- export { NEON_REQUEST_CONTEXT_KEY, NeonFunctionsContext, WaitUntil, runWithRequestContext, waitUntil };
19
+ export { waitUntil };
37
20
  //# sourceMappingURL=wait-until.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wait-until.d.ts","names":[],"sources":["../../src/lib/wait-until.ts"],"mappings":";KAWY,SAAA,aAAsB;AAAlC;AAMA;AAWA;AAkDA;AAegB,KA5EJ,oBAAA,GA4EyB;EAAA,SAAA,CAAA,EA3ExB,SA2EwB;;;;AAGjC;;;;;cApES,wBAAA;;;;;;;;;;iBAkDG,SAAA,UAAmB;;;;;;;iBAenB,kCACN,gCACC,IACR"}
1
+ {"version":3,"file":"wait-until.d.ts","names":[],"sources":["../../src/lib/wait-until.ts"],"mappings":";;;;;;AAgBmC;AAanC;;;;;;0BAb4B;;;iBAaZ,SAAA,UAAmB"}
@@ -1,68 +1,12 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  //#region src/lib/wait-until.ts
3
- /**
4
- * `waitUntil` extends the lifetime of a Neon Function invocation so background work
5
- * (logging, cache writes, analytics, …) can finish after the response has been sent.
6
- *
7
- * The public API mirrors Vercel's `@vercel/functions`: import `waitUntil` and call it
8
- * directly with a promise (`waitUntil(promise)`). The active invocation context is
9
- * published by the runtime on `globalThis`, so it can be read without importing the
10
- * runtime and stays correct under concurrency.
11
- */
12
- /**
13
- * Well-known `globalThis` key under which the Neon Functions runtime publishes the
14
- * current invocation context. The runtime installs it as a getter that returns the
15
- * live context object DIRECTLY — `globalThis.NEON_REQUEST_CONTEXT` is `{ waitUntil }`
16
- * during an invocation and `undefined` outside one — so it is read as the context
17
- * itself, not via a `.get()`-style provider.
18
- */
19
- const NEON_REQUEST_CONTEXT_KEY = "NEON_REQUEST_CONTEXT";
20
- const globalWithContext = globalThis;
21
- /**
22
- * Backs `runWithRequestContext` for local dev and tests. When the runtime is present
23
- * it has already published its own accessor under the same key, so we leave that in
24
- * place and never publish over it.
25
- */
26
- const requestContextStore = new AsyncLocalStorage();
27
- if (!("NEON_REQUEST_CONTEXT" in globalWithContext)) Object.defineProperty(globalWithContext, NEON_REQUEST_CONTEXT_KEY, {
28
- configurable: true,
29
- get() {
30
- return requestContextStore.getStore();
31
- }
32
- });
33
- /**
34
- * Reads the current invocation context off `globalThis.NEON_REQUEST_CONTEXT`, falling
35
- * back to an empty context outside an invocation (local dev, tests, non-Neon hosts).
36
- */
37
- function getContext() {
38
- return globalWithContext["NEON_REQUEST_CONTEXT"] ?? {};
39
- }
40
2
  function isPromise(value) {
41
3
  return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
42
4
  }
43
- /**
44
- * Defers async work past the response by forwarding the promise to the Neon Functions
45
- * runtime, which keeps the invocation alive until the promise settles.
46
- *
47
- * The context is resolved at call time from the enclosing invocation, so this stays
48
- * correct under concurrency. When no invocation context is in scope (local dev, tests,
49
- * non-Neon hosts), this is a no-op: the promise is accepted and ignored (it still runs
50
- * on its own — the caller already started it — it just isn't tracked).
51
- */
52
5
  function waitUntil(promise) {
53
6
  if (!isPromise(promise)) throw new TypeError(`waitUntil can only be called with a Promise, got ${typeof promise}`);
54
- getContext().waitUntil?.(promise);
55
- }
56
- /**
57
- * Runtime entry point: binds `context` as the current invocation context for the
58
- * duration of `fn` (and any async work it spawns), so calls to `waitUntil` inside it
59
- * forward to `context.waitUntil`. Intended for the Neon Functions runtime to wrap each
60
- * invocation; application code should not need this.
61
- */
62
- function runWithRequestContext(context, fn) {
63
- return requestContextStore.run(context, fn);
7
+ globalThis.NEON_REQUEST_CONTEXT?.waitUntil?.(promise);
64
8
  }
65
9
  //#endregion
66
- export { NEON_REQUEST_CONTEXT_KEY, runWithRequestContext, waitUntil };
10
+ export { waitUntil };
67
11
 
68
12
  //# sourceMappingURL=wait-until.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"wait-until.js","names":[],"sources":["../../src/lib/wait-until.ts"],"sourcesContent":["/**\n * `waitUntil` extends the lifetime of a Neon Function invocation so background work\n * (logging, cache writes, analytics, …) can finish after the response has been sent.\n *\n * The public API mirrors Vercel's `@vercel/functions`: import `waitUntil` and call it\n * directly with a promise (`waitUntil(promise)`). The active invocation context is\n * published by the runtime on `globalThis`, so it can be read without importing the\n * runtime and stays correct under concurrency.\n */\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nexport type WaitUntil = (promise: Promise<unknown>) => void;\n\n/**\n * The slice of the runtime context this package reads. The runtime may attach\n * additional fields; only `waitUntil` is consumed here.\n */\nexport type NeonFunctionsContext = {\n\twaitUntil?: WaitUntil;\n};\n\n/**\n * Well-known `globalThis` key under which the Neon Functions runtime publishes the\n * current invocation context. The runtime installs it as a getter that returns the\n * live context object DIRECTLY — `globalThis.NEON_REQUEST_CONTEXT` is `{ waitUntil }`\n * during an invocation and `undefined` outside one — so it is read as the context\n * itself, not via a `.get()`-style provider.\n */\nexport const NEON_REQUEST_CONTEXT_KEY = \"NEON_REQUEST_CONTEXT\";\n\ntype GlobalWithContext = typeof globalThis & {\n\t[NEON_REQUEST_CONTEXT_KEY]?: NeonFunctionsContext;\n};\n\nconst globalWithContext: GlobalWithContext = globalThis;\n\n/**\n * Backs `runWithRequestContext` for local dev and tests. When the runtime is present\n * it has already published its own accessor under the same key, so we leave that in\n * place and never publish over it.\n */\nconst requestContextStore = new AsyncLocalStorage<NeonFunctionsContext>();\n\nif (!(NEON_REQUEST_CONTEXT_KEY in globalWithContext)) {\n\tObject.defineProperty(globalWithContext, NEON_REQUEST_CONTEXT_KEY, {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn requestContextStore.getStore();\n\t\t},\n\t});\n}\n\n/**\n * Reads the current invocation context off `globalThis.NEON_REQUEST_CONTEXT`, falling\n * back to an empty context outside an invocation (local dev, tests, non-Neon hosts).\n */\nfunction getContext(): NeonFunctionsContext {\n\treturn globalWithContext[NEON_REQUEST_CONTEXT_KEY] ?? {};\n}\n\nfunction isPromise(value: unknown): value is Promise<unknown> {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue !== null &&\n\t\t\"then\" in value &&\n\t\ttypeof value.then === \"function\"\n\t);\n}\n\n/**\n * Defers async work past the response by forwarding the promise to the Neon Functions\n * runtime, which keeps the invocation alive until the promise settles.\n *\n * The context is resolved at call time from the enclosing invocation, so this stays\n * correct under concurrency. When no invocation context is in scope (local dev, tests,\n * non-Neon hosts), this is a no-op: the promise is accepted and ignored (it still runs\n * on its own — the caller already started it — it just isn't tracked).\n */\nexport function waitUntil(promise: Promise<unknown>): void {\n\tif (!isPromise(promise)) {\n\t\tthrow new TypeError(\n\t\t\t`waitUntil can only be called with a Promise, got ${typeof promise}`,\n\t\t);\n\t}\n\tgetContext().waitUntil?.(promise);\n}\n\n/**\n * Runtime entry point: binds `context` as the current invocation context for the\n * duration of `fn` (and any async work it spawns), so calls to `waitUntil` inside it\n * forward to `context.waitUntil`. Intended for the Neon Functions runtime to wrap each\n * invocation; application code should not need this.\n */\nexport function runWithRequestContext<T>(\n\tcontext: NeonFunctionsContext,\n\tfn: () => T,\n): T {\n\treturn requestContextStore.run(context, fn);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA4BA,MAAa,2BAA2B;AAMxC,MAAM,oBAAuC;;;;;;AAO7C,MAAM,sBAAsB,IAAI,kBAAwC;AAExE,IAAI,EAAA,0BAA8B,oBACjC,OAAO,eAAe,mBAAmB,0BAA0B;CAClE,cAAc;CACd,MAAM;EACL,OAAO,oBAAoB,SAAS;CACrC;AACD,CAAC;;;;;AAOF,SAAS,aAAmC;CAC3C,OAAO,kBAAA,2BAA+C,CAAC;AACxD;AAEA,SAAS,UAAU,OAA2C;CAC7D,OACC,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,OAAO,MAAM,SAAS;AAExB;;;;;;;;;;AAWA,SAAgB,UAAU,SAAiC;CAC1D,IAAI,CAAC,UAAU,OAAO,GACrB,MAAM,IAAI,UACT,oDAAoD,OAAO,SAC5D;CAED,WAAW,CAAC,CAAC,YAAY,OAAO;AACjC;;;;;;;AAQA,SAAgB,sBACf,SACA,IACI;CACJ,OAAO,oBAAoB,IAAI,SAAS,EAAE;AAC3C"}
1
+ {"version":3,"file":"wait-until.js","names":[],"sources":["../../src/lib/wait-until.ts"],"sourcesContent":["/**\n * `waitUntil(promise)` defers async work past a Neon Function's response: the runtime\n * keeps the invocation alive until the promise settles (up to the 15-minute limit).\n *\n * The runtime publishes the active invocation context on `globalThis.NEON_REQUEST_CONTEXT`\n * (a getter returning `{ waitUntil }` during an invocation, `undefined` outside one), so we\n * read it directly. Off-platform local dev, tests, non-Neon hosts there is no context\n * and this is a no-op, mirroring `@vercel/functions`: the promise the caller created still\n * runs on its own, it just isn't tracked. Passing a non-Promise throws a `TypeError`.\n */\n\ndeclare global {\n\t// Published by the Neon Functions runtime. Declared here (the only way to type an\n\t// augmented global) so it can be read off `globalThis` without a cast. `var` is the\n\t// required form for a global augmentation.\n\tvar NEON_REQUEST_CONTEXT:\n\t\t| { waitUntil?: (promise: Promise<unknown>) => void }\n\t\t| undefined;\n}\n\nfunction isPromise(value: unknown): value is Promise<unknown> {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue !== null &&\n\t\t\"then\" in value &&\n\t\ttypeof value.then === \"function\"\n\t);\n}\n\nexport function waitUntil(promise: Promise<unknown>): void {\n\tif (!isPromise(promise)) {\n\t\tthrow new TypeError(\n\t\t\t`waitUntil can only be called with a Promise, got ${typeof promise}`,\n\t\t);\n\t}\n\tglobalThis.NEON_REQUEST_CONTEXT?.waitUntil?.(promise);\n}\n"],"mappings":";AAoBA,SAAS,UAAU,OAA2C;CAC7D,OACC,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,OAAO,MAAM,SAAS;AAExB;AAEA,SAAgB,UAAU,SAAiC;CAC1D,IAAI,CAAC,UAAU,OAAO,GACrB,MAAM,IAAI,UACT,oDAAoD,OAAO,SAC5D;CAED,WAAW,sBAAsB,YAAY,OAAO;AACrD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neondatabase/functions",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Runtime helpers for Neon Functions. Currently provides a `waitUntil` primitive for deferring async work past a response.",
5
5
  "keywords": [
6
6
  "neon",
@@ -43,7 +43,7 @@
43
43
  "vitest": "^3.0.9"
44
44
  },
45
45
  "engines": {
46
- "node": ">=22"
46
+ "node": ">=20.19.0"
47
47
  },
48
48
  "publishConfig": {
49
49
  "provenance": false