@bluelibs/runner 4.5.5 → 4.5.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/AI.md CHANGED
@@ -11,8 +11,8 @@ npm install @bluelibs/runner
11
11
  - Auto‑detects platform at runtime. In browsers, `exit()` is unsupported and throws. Env reads use `globalThis.__ENV__`, `process.env`, or `globalThis.env`.
12
12
 
13
13
  ```ts
14
- import { setPlatform, BrowserPlatformAdapter } from "@bluelibs/runner/platform";
15
- setPlatform(new BrowserPlatformAdapter());
14
+ import { setPlatform, PlatformAdapter } from "@bluelibs/runner/platform";
15
+ setPlatform(new PlatformAdapter("browser"));
16
16
  //
17
17
  globalThis.__ENV__ = { API_URL: "https://example.test" };
18
18
  ```
@@ -184,11 +184,9 @@ import { globals, task } from "@bluelibs/runner";
184
184
 
185
185
  const critical = task({
186
186
  id: "app.tasks.critical",
187
- meta: {
188
- tags: [
189
- globals.tags.debug.with({ logTaskInput: true, logTaskResult: true }),
190
- ],
191
- },
187
+ tags: [
188
+ globals.tags.debug.with({ logTaskInput: true, logTaskOutput: true }),
189
+ ],
192
190
  run: async () => "ok",
193
191
  });
194
192
  ```
@@ -202,7 +200,7 @@ const logsExtension = resource({
202
200
  id: "app.logs",
203
201
  dependencies: { logger: globals.resources.logger },
204
202
  init: async (_, { logger }) => {
205
- logger.info("test", { data }); // "trace", "debug", "info", "warn", "error", "critical"
203
+ logger.info("test", { example: 123 }); // "trace", "debug", "info", "warn", "error", "critical"
206
204
  const sublogger = logger.with({
207
205
  source: "app.logs",
208
206
  context: {},
@@ -214,6 +212,47 @@ const logsExtension = resource({
214
212
  });
215
213
  ```
216
214
 
215
+ ## AWS Lambda
216
+
217
+ - Cache the runner between warm invocations; do not dispose on each call.
218
+ - Disable shutdown hooks (`shutdownHooks: false`) and enable the error boundary.
219
+ - Provide a request-scoped context per invocation via `createContext`.
220
+ - Parse API Gateway v1/v2 events (handle `requestContext.http.method`/`rawPath` and `httpMethod`/`path`) and base64 bodies.
221
+ - Optionally set `context.callbackWaitsForEmptyEventLoop = false` when using long‑lived connections.
222
+
223
+ Example outline:
224
+
225
+ ```ts
226
+ // bootstrap.ts
227
+ import { resource, task, run, createContext } from "@bluelibs/runner";
228
+ export const RequestCtx: any = createContext("app.http.request");
229
+ // define resources & tasks...
230
+ let rrPromise: Promise<any> | null = null;
231
+ export async function getRunner() {
232
+ if (!rrPromise) {
233
+ rrPromise = run(app, { shutdownHooks: false, errorBoundary: true });
234
+ }
235
+ return rrPromise;
236
+ }
237
+
238
+ // handler.ts
239
+ export const handler = async (event: any, context: any) => {
240
+ const rr: any = await getRunner();
241
+ const method = event?.requestContext?.http?.method ?? event?.httpMethod ?? "GET";
242
+ const path = event?.rawPath || event?.path || "/";
243
+ const rawBody = event?.body
244
+ ? event.isBase64Encoded
245
+ ? Buffer.from(event.body, "base64").toString("utf8")
246
+ : event.body
247
+ : undefined;
248
+ const body = rawBody ? JSON.parse(rawBody) : undefined;
249
+
250
+ return RequestCtx.provide({ requestId: context?.awsRequestId ?? "local", method, path }, async () => {
251
+ // route and call rr.runTask(...)
252
+ });
253
+ };
254
+ ```
255
+
217
256
  ## Middleware (global or local)
218
257
 
219
258
  Middleware now supports type contracts with `<Config, Input, Output>` signature:
package/README.md CHANGED
@@ -19,6 +19,7 @@ _Or: How I Learned to Stop Worrying and Love Dependency Injection_
19
19
  | [Migrate from 3.x.x to 4.x.x](https://github.com/bluelibs/runner/blob/main/readmes/MIGRATION.md) | Guide | Step-by-step upgrade from v3 to v4 |
20
20
  | [Runner Lore](https://github.com/bluelibs/runner/blob/main/readmes) | Docs | Design notes, deep dives, and context |
21
21
  | [Example: Express + OpenAPI + SQLite](https://github.com/bluelibs/runner/tree/main/examples/express-openapi-sqlite) | Example | Full Express + OpenAPI + SQLite demo |
22
+ | [Example: Fastify + MikroORM + PostgreSQL](https://github.com/bluelibs/runner/tree/main/examples/fastify-mikroorm) | Example | Full Fastify + MikroORM + PostgreSQL demo |
22
23
  | [OpenAI Runner Chatbot](https://chatgpt.com/g/g-68b756abec648191aa43eaa1ea7a7945-runner?model=gpt-5-thinking) | Chatbot | Ask questions interactively, or feed README.md to your own AI |
23
24
 
24
25
  Welcome to BlueLibs Runner, where we've taken the chaos of modern application architecture and turned it into something that won't make you question your life choices at 3am. This isn't just another framework – it's your new best friend who actually understands that code should be readable, testable, and not require a PhD in abstract nonsense to maintain.
@@ -94,8 +95,6 @@ const { dispose, getResourceValue, runTask, emitEvent } = await run(app);
94
95
  const { dispose } = await run(app, { debug: "verbose" });
95
96
  ```
96
97
 
97
- > **runtime:** "'Less lines than Hello World.' Incredible. All you had to do was externalize 90% of the work into `express`, Node, and me. But please, bask in the brevity. I’ll be over here negotiating a peace treaty between your dependency tree and reality."
98
-
99
98
  ### Platform & Async Context
100
99
 
101
100
  Runner auto-detects the platform and adapts behavior at runtime. The only feature present only in Node.js is the use of `AsyncLocalStorage` for managing async context.
@@ -142,8 +141,6 @@ Look, we get it. You could turn every function into a task, but that's like usin
142
141
 
143
142
  Think of tasks as the "main characters" in your application story, not every single line of dialogue.
144
143
 
145
- > **runtime:** "'Pure-ish.' Like diet chaos. Zero calories, full aftertaste. You stapled dependencies to a function and called it virtuous. It's fine. I’ll keep the receipts while you roleplay purity with side effects in a trench coat."
146
-
147
144
  ### Resources
148
145
 
149
146
  Resources are the singletons, the services, configs, and connections that live throughout your app's lifecycle. They initialize once and stick around until cleanup time. They have to be registered (via `register: []`) only once before they can be used.
@@ -235,8 +232,6 @@ const dbResource = resource({
235
232
  });
236
233
  ```
237
234
 
238
- > **runtime:** "Singletons: global variables with a nicer haircut. You ban globals, then create 'resources' that live forever and hold the keys to everything. At least there's a `dispose()`. I’ll believe you use it when I stop finding zombie sockets haunting the process."
239
-
240
235
  ### Events
241
236
 
242
237
  Events let different parts of your app talk to each other without tight coupling. It's like having a really good office messenger who never forgets anything.
@@ -579,7 +574,7 @@ const loggedTask = task({
579
574
  });
580
575
  ```
581
576
 
582
- > **runtime:** "Ah, the onion pattern. A matryoshka doll made of promises. Every peel reveals… another logger. Another tracer. Another 'just a tiny wrapper'. I'll keep unwrapping until we hit the single lonely `return` you were hiding like state secrets."
577
+ > **runtime:** "Ah, the onion pattern. A matryoshka doll made of promises. Every peel reveals… another logger. Another tracer. Another 'just a tiny wrapper'."
583
578
 
584
579
  ### Tags
585
580
 
@@ -3205,6 +3200,7 @@ This is part of the [BlueLibs](https://www.bluelibs.com) ecosystem. We're not tr
3205
3200
  - [GitHub Repository](https://github.com/bluelibs/runner) - ⭐ if you find this useful
3206
3201
  - [Documentation](https://bluelibs.github.io/runner/) - When you need the full details
3207
3202
  - [Issues](https://github.com/bluelibs/runner/issues) - When something breaks (or you want to make it better)
3203
+ - [Contributing](./CONTRIBUTING.md) - How to file great issues and PRs
3208
3204
 
3209
3205
  _P.S. - Yes, we know there are 47 other JavaScript frameworks. This one's still different._
3210
3206
 
package/dist/index.cjs CHANGED
@@ -237,6 +237,211 @@ __export(errors_exports, {
237
237
  UnknownItemTypeError: () => UnknownItemTypeError,
238
238
  ValidationError: () => ValidationError
239
239
  });
240
+
241
+ // src/platform/index.ts
242
+ function detectEnvironment() {
243
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
244
+ return "browser";
245
+ }
246
+ if (typeof process !== "undefined" && process.versions && process.versions.node) {
247
+ return "node";
248
+ }
249
+ return "universal";
250
+ }
251
+ __name(detectEnvironment, "detectEnvironment");
252
+ var _PlatformAdapter = class _PlatformAdapter {
253
+ constructor(env) {
254
+ this.isInitialized = false;
255
+ this.nodeALSClass = null;
256
+ // timers
257
+ this.setTimeout = globalThis.setTimeout;
258
+ this.clearTimeout = globalThis.clearTimeout;
259
+ this.env = env ?? detectEnvironment();
260
+ }
261
+ async init() {
262
+ if (this.env === "node") {
263
+ {
264
+ {
265
+ const mod = __require("async_hooks");
266
+ this.nodeALSClass = mod.AsyncLocalStorage;
267
+ }
268
+ }
269
+ }
270
+ }
271
+ onUncaughtException(handler) {
272
+ switch (this.env) {
273
+ case "node": {
274
+ process.on("uncaughtException", handler);
275
+ return () => process.off("uncaughtException", handler);
276
+ }
277
+ case "browser": {
278
+ const target = globalThis.window ?? globalThis;
279
+ const h = /* @__PURE__ */ __name((e) => handler(e?.error ?? e), "h");
280
+ target.addEventListener?.("error", h);
281
+ return () => target.removeEventListener?.("error", h);
282
+ }
283
+ default: {
284
+ const tgt = globalThis;
285
+ if (tgt.addEventListener) {
286
+ const h = /* @__PURE__ */ __name((e) => handler(e?.error ?? e), "h");
287
+ tgt.addEventListener("error", h);
288
+ return () => tgt.removeEventListener("error", h);
289
+ }
290
+ return () => {
291
+ };
292
+ }
293
+ }
294
+ }
295
+ onUnhandledRejection(handler) {
296
+ switch (this.env) {
297
+ case "node": {
298
+ const h = /* @__PURE__ */ __name((reason) => handler(reason), "h");
299
+ process.on("unhandledRejection", h);
300
+ return () => process.off("unhandledRejection", h);
301
+ }
302
+ case "browser": {
303
+ const target = globalThis.window;
304
+ const wrap = /* @__PURE__ */ __name((e) => handler(e.reason), "wrap");
305
+ target.addEventListener?.("unhandledrejection", wrap);
306
+ return () => target.removeEventListener?.("unhandledrejection", wrap);
307
+ }
308
+ default: {
309
+ const tgt = globalThis;
310
+ if (tgt.addEventListener) {
311
+ const wrap = /* @__PURE__ */ __name((e) => handler(e.reason ?? e), "wrap");
312
+ tgt.addEventListener("unhandledrejection", wrap);
313
+ return () => tgt.removeEventListener("unhandledrejection", wrap);
314
+ }
315
+ return () => {
316
+ };
317
+ }
318
+ }
319
+ }
320
+ onShutdownSignal(handler) {
321
+ switch (this.env) {
322
+ case "node": {
323
+ process.on("SIGINT", handler);
324
+ process.on("SIGTERM", handler);
325
+ return () => {
326
+ process.off("SIGINT", handler);
327
+ process.off("SIGTERM", handler);
328
+ };
329
+ }
330
+ case "browser": {
331
+ const win = window;
332
+ win.addEventListener?.("beforeunload", handler);
333
+ return () => {
334
+ win.removeEventListener?.("beforeunload", handler);
335
+ };
336
+ }
337
+ default: {
338
+ const tgt = globalThis;
339
+ const cleanup = [];
340
+ if (tgt.addEventListener) {
341
+ tgt.addEventListener("beforeunload", handler);
342
+ cleanup.push(
343
+ () => tgt.removeEventListener?.("beforeunload", handler)
344
+ );
345
+ const vis = /* @__PURE__ */ __name(() => {
346
+ const doc = globalThis.document;
347
+ if (doc && doc.visibilityState === "hidden") handler();
348
+ }, "vis");
349
+ tgt.addEventListener("visibilitychange", vis);
350
+ cleanup.push(
351
+ () => tgt.removeEventListener?.("visibilitychange", vis)
352
+ );
353
+ }
354
+ if (typeof process !== "undefined" && process.on) {
355
+ process.on("SIGINT", handler);
356
+ process.on("SIGTERM", handler);
357
+ cleanup.push(() => {
358
+ process.off?.("SIGINT", handler);
359
+ process.off?.("SIGTERM", handler);
360
+ });
361
+ }
362
+ return () => cleanup.forEach((fn) => fn());
363
+ }
364
+ }
365
+ }
366
+ exit(code) {
367
+ switch (this.env) {
368
+ case "node":
369
+ process.exit(code);
370
+ return;
371
+ default:
372
+ throw new PlatformUnsupportedFunction("exit");
373
+ }
374
+ }
375
+ getEnv(key) {
376
+ switch (this.env) {
377
+ case "node":
378
+ return process.env[key];
379
+ default: {
380
+ const g = globalThis;
381
+ if (g.__ENV__ && typeof g.__ENV__ === "object") return g.__ENV__[key];
382
+ if (typeof process !== "undefined" && process.env)
383
+ return process.env[key];
384
+ if (g.env && typeof g.env === "object") return g.env[key];
385
+ return void 0;
386
+ }
387
+ }
388
+ }
389
+ hasAsyncLocalStorage() {
390
+ switch (this.env) {
391
+ case "node":
392
+ return true;
393
+ // We'll try native, else polyfill
394
+ case "browser":
395
+ default:
396
+ return false;
397
+ }
398
+ }
399
+ createAsyncLocalStorage() {
400
+ switch (this.env) {
401
+ case "node": {
402
+ let instance;
403
+ const get = /* @__PURE__ */ __name(() => {
404
+ if (!instance) {
405
+ if (!this.nodeALSClass) {
406
+ throw new PlatformUnsupportedFunction(
407
+ "createAsyncLocalStorage: Platform not initialized"
408
+ );
409
+ }
410
+ instance = new this.nodeALSClass();
411
+ }
412
+ return instance;
413
+ }, "get");
414
+ return {
415
+ getStore: /* @__PURE__ */ __name(() => get().getStore(), "getStore"),
416
+ run: /* @__PURE__ */ __name((store2, callback) => get().run(store2, callback), "run")
417
+ };
418
+ }
419
+ case "browser":
420
+ default:
421
+ return {
422
+ getStore: /* @__PURE__ */ __name(() => {
423
+ throw new PlatformUnsupportedFunction("createAsyncLocalStorage");
424
+ }, "getStore"),
425
+ run: /* @__PURE__ */ __name(() => {
426
+ throw new PlatformUnsupportedFunction("createAsyncLocalStorage");
427
+ }, "run")
428
+ };
429
+ }
430
+ }
431
+ };
432
+ __name(_PlatformAdapter, "PlatformAdapter");
433
+ var PlatformAdapter = _PlatformAdapter;
434
+ var platformInstance = null;
435
+ function getPlatform() {
436
+ if (!platformInstance) {
437
+ const env = detectEnvironment();
438
+ platformInstance = new PlatformAdapter(env);
439
+ }
440
+ return platformInstance;
441
+ }
442
+ __name(getPlatform, "getPlatform");
443
+
444
+ // src/errors.ts
240
445
  var _RuntimeError = class _RuntimeError extends Error {
241
446
  constructor(message) {
242
447
  super(message);
@@ -395,7 +600,7 @@ var EventEmissionCycleError = _EventEmissionCycleError;
395
600
  var _PlatformUnsupportedFunction = class _PlatformUnsupportedFunction extends RuntimeError {
396
601
  constructor(functionName) {
397
602
  super(
398
- `Platform function not supported in this environment: ${functionName}`
603
+ `Platform function not supported in this environment: ${functionName}. Detected platform: ${detectEnvironment()}.`
399
604
  );
400
605
  this.name = "PlatformUnsupportedFunction";
401
606
  }
@@ -693,209 +898,6 @@ function isOptional(definition) {
693
898
  }
694
899
  __name(isOptional, "isOptional");
695
900
 
696
- // src/platform/index.ts
697
- function detectEnvironment() {
698
- if (typeof window !== "undefined" && typeof document !== "undefined") {
699
- return "browser";
700
- }
701
- if (typeof process !== "undefined" && process.versions && process.versions.node) {
702
- return "node";
703
- }
704
- return "universal";
705
- }
706
- __name(detectEnvironment, "detectEnvironment");
707
- var _PlatformAdapter = class _PlatformAdapter {
708
- constructor(env) {
709
- this.isInitialized = false;
710
- this.nodeALSClass = null;
711
- // timers
712
- this.setTimeout = globalThis.setTimeout;
713
- this.clearTimeout = globalThis.clearTimeout;
714
- this.env = env ?? detectEnvironment();
715
- }
716
- async init() {
717
- if (this.env === "node") {
718
- {
719
- {
720
- const mod = __require("async_hooks");
721
- this.nodeALSClass = mod.AsyncLocalStorage;
722
- }
723
- }
724
- }
725
- }
726
- onUncaughtException(handler) {
727
- switch (this.env) {
728
- case "node": {
729
- process.on("uncaughtException", handler);
730
- return () => process.off("uncaughtException", handler);
731
- }
732
- case "browser": {
733
- const target = globalThis.window ?? globalThis;
734
- const h = /* @__PURE__ */ __name((e) => handler(e?.error ?? e), "h");
735
- target.addEventListener?.("error", h);
736
- return () => target.removeEventListener?.("error", h);
737
- }
738
- default: {
739
- const tgt = globalThis;
740
- if (tgt.addEventListener) {
741
- const h = /* @__PURE__ */ __name((e) => handler(e?.error ?? e), "h");
742
- tgt.addEventListener("error", h);
743
- return () => tgt.removeEventListener("error", h);
744
- }
745
- return () => {
746
- };
747
- }
748
- }
749
- }
750
- onUnhandledRejection(handler) {
751
- switch (this.env) {
752
- case "node": {
753
- const h = /* @__PURE__ */ __name((reason) => handler(reason), "h");
754
- process.on("unhandledRejection", h);
755
- return () => process.off("unhandledRejection", h);
756
- }
757
- case "browser": {
758
- const target = globalThis.window;
759
- const wrap = /* @__PURE__ */ __name((e) => handler(e.reason), "wrap");
760
- target.addEventListener?.("unhandledrejection", wrap);
761
- return () => target.removeEventListener?.("unhandledrejection", wrap);
762
- }
763
- default: {
764
- const tgt = globalThis;
765
- if (tgt.addEventListener) {
766
- const wrap = /* @__PURE__ */ __name((e) => handler(e.reason ?? e), "wrap");
767
- tgt.addEventListener("unhandledrejection", wrap);
768
- return () => tgt.removeEventListener("unhandledrejection", wrap);
769
- }
770
- return () => {
771
- };
772
- }
773
- }
774
- }
775
- onShutdownSignal(handler) {
776
- switch (this.env) {
777
- case "node": {
778
- process.on("SIGINT", handler);
779
- process.on("SIGTERM", handler);
780
- return () => {
781
- process.off("SIGINT", handler);
782
- process.off("SIGTERM", handler);
783
- };
784
- }
785
- case "browser": {
786
- const win = window;
787
- win.addEventListener?.("beforeunload", handler);
788
- return () => {
789
- win.removeEventListener?.("beforeunload", handler);
790
- };
791
- }
792
- default: {
793
- const tgt = globalThis;
794
- const cleanup = [];
795
- if (tgt.addEventListener) {
796
- tgt.addEventListener("beforeunload", handler);
797
- cleanup.push(
798
- () => tgt.removeEventListener?.("beforeunload", handler)
799
- );
800
- const vis = /* @__PURE__ */ __name(() => {
801
- const doc = globalThis.document;
802
- if (doc && doc.visibilityState === "hidden") handler();
803
- }, "vis");
804
- tgt.addEventListener("visibilitychange", vis);
805
- cleanup.push(
806
- () => tgt.removeEventListener?.("visibilitychange", vis)
807
- );
808
- }
809
- if (typeof process !== "undefined" && process.on) {
810
- process.on("SIGINT", handler);
811
- process.on("SIGTERM", handler);
812
- cleanup.push(() => {
813
- process.off?.("SIGINT", handler);
814
- process.off?.("SIGTERM", handler);
815
- });
816
- }
817
- return () => cleanup.forEach((fn) => fn());
818
- }
819
- }
820
- }
821
- exit(code) {
822
- switch (this.env) {
823
- case "node":
824
- process.exit(code);
825
- return;
826
- default:
827
- throw new PlatformUnsupportedFunction("exit");
828
- }
829
- }
830
- getEnv(key) {
831
- switch (this.env) {
832
- case "node":
833
- return process.env[key];
834
- default: {
835
- const g = globalThis;
836
- if (g.__ENV__ && typeof g.__ENV__ === "object") return g.__ENV__[key];
837
- if (typeof process !== "undefined" && process.env)
838
- return process.env[key];
839
- if (g.env && typeof g.env === "object") return g.env[key];
840
- return void 0;
841
- }
842
- }
843
- }
844
- hasAsyncLocalStorage() {
845
- switch (this.env) {
846
- case "node":
847
- return true;
848
- // We'll try native, else polyfill
849
- case "browser":
850
- default:
851
- return false;
852
- }
853
- }
854
- createAsyncLocalStorage() {
855
- switch (this.env) {
856
- case "node": {
857
- let instance;
858
- const get = /* @__PURE__ */ __name(() => {
859
- if (!instance) {
860
- if (!this.nodeALSClass) {
861
- throw new PlatformUnsupportedFunction(
862
- "createAsyncLocalStorage: Platform not initialized"
863
- );
864
- }
865
- instance = new this.nodeALSClass();
866
- }
867
- return instance;
868
- }, "get");
869
- return {
870
- getStore: /* @__PURE__ */ __name(() => get().getStore(), "getStore"),
871
- run: /* @__PURE__ */ __name((store2, callback) => get().run(store2, callback), "run")
872
- };
873
- }
874
- case "browser":
875
- default:
876
- return {
877
- getStore: /* @__PURE__ */ __name(() => {
878
- throw new PlatformUnsupportedFunction("createAsyncLocalStorage");
879
- }, "getStore"),
880
- run: /* @__PURE__ */ __name(() => {
881
- throw new PlatformUnsupportedFunction("createAsyncLocalStorage");
882
- }, "run")
883
- };
884
- }
885
- }
886
- };
887
- __name(_PlatformAdapter, "PlatformAdapter");
888
- var PlatformAdapter = _PlatformAdapter;
889
- var platformInstance = null;
890
- function getPlatform() {
891
- if (!platformInstance) {
892
- const env = detectEnvironment();
893
- platformInstance = new PlatformAdapter(env);
894
- }
895
- return platformInstance;
896
- }
897
- __name(getPlatform, "getPlatform");
898
-
899
901
  // src/globals/middleware/requireContext.middleware.ts
900
902
  var requireContextTaskMiddleware = defineTaskMiddleware({
901
903
  id: "globals.middleware.requireContext",