@faasjs/func 5.0.1 → 6.0.0-beta.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
@@ -19,6 +19,8 @@ npm install @faasjs/func
19
19
 
20
20
  ## Functions
21
21
 
22
+ - [detectNodeRuntime](functions/detectNodeRuntime.md)
23
+ - [loadPackage](functions/loadPackage.md)
22
24
  - [nameFunc](functions/nameFunc.md)
23
25
  - [useFunc](functions/useFunc.md)
24
26
  - [usePlugin](functions/usePlugin.md)
package/dist/index.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  var crypto = require('crypto');
4
4
  var logger = require('@faasjs/logger');
5
5
 
6
+ var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
6
7
  // src/index.ts
7
8
 
8
9
  // src/plugins/run_handler/index.ts
@@ -31,6 +32,30 @@ var RunHandler = class {
31
32
  }
32
33
  };
33
34
 
35
+ // src/utils.ts
36
+ function nameFunc(name, handler) {
37
+ Object.defineProperty(handler, "name", { value: name });
38
+ return handler;
39
+ }
40
+ function detectNodeRuntime() {
41
+ if (typeof globalThis.require === "function" && typeof module !== "undefined")
42
+ return "commonjs";
43
+ if (typeof ({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)) }) !== "undefined") return "module";
44
+ throw Error("Unknown runtime");
45
+ }
46
+ async function loadPackage(name) {
47
+ const runtime = detectNodeRuntime();
48
+ if (runtime === "module") {
49
+ const module2 = await import(name);
50
+ return module2.default ? module2.default : module2;
51
+ }
52
+ if (runtime === "commonjs") {
53
+ const module2 = globalThis.require(name);
54
+ return module2.default ? module2.default : module2;
55
+ }
56
+ throw Error("Unknown runtime");
57
+ }
58
+
34
59
  // src/index.ts
35
60
  var Func = class {
36
61
  plugins;
@@ -154,7 +179,7 @@ var Func = class {
154
179
  event,
155
180
  context,
156
181
  callback,
157
- response: void 0,
182
+ response: undefined,
158
183
  handler: this.handler,
159
184
  logger: logger$1,
160
185
  config: this.config
@@ -195,12 +220,10 @@ function useFunc(handler) {
195
220
  plugins = [];
196
221
  return func;
197
222
  }
198
- function nameFunc(name, handler) {
199
- Object.defineProperty(handler, "name", { value: name });
200
- return handler;
201
- }
202
223
 
203
224
  exports.Func = Func;
225
+ exports.detectNodeRuntime = detectNodeRuntime;
226
+ exports.loadPackage = loadPackage;
204
227
  exports.nameFunc = nameFunc;
205
228
  exports.useFunc = useFunc;
206
229
  exports.usePlugin = usePlugin;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,48 @@
1
1
  import { Logger } from '@faasjs/logger';
2
2
 
3
+ /**
4
+ * Assigns a name to a given function handler, which will be displayed in logs and error messages.
5
+ *
6
+ * @template T - The type of the function handler.
7
+ * @param {string} name - The name to assign to the function handler.
8
+ * @param {T} handler - The function handler to which the name will be assigned.
9
+ * @returns {T} - The original function handler with the assigned name.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { nameFunc } from '@faasjs/func'
14
+ *
15
+ * const handler = nameFunc('myHandler', () => {
16
+ * return 'Hello World'
17
+ * })
18
+ *
19
+ * console.log(handler.name) // => 'myHandler'
20
+ * ```
21
+ */
22
+ declare function nameFunc<T extends (...args: any[]) => any>(name: string, handler: T): T;
23
+ /**
24
+ * Detect the current JavaScript runtime environment.
25
+ *
26
+ * This function checks for the presence of `import.meta` and `require` to determine
27
+ * whether the runtime is using ECMAScript modules (ESM) or CommonJS modules (CJS).
28
+ *
29
+ * @returns {'commonjs' | 'module'} - Returns 'module' if the runtime is using ECMAScript modules,
30
+ * and 'cjs' if the runtime is using CommonJS modules.
31
+ * @throws {Error} - Throws an error if the runtime cannot be determined.
32
+ */
33
+ declare function detectNodeRuntime(): 'commonjs' | 'module';
34
+ /**
35
+ * Load a package dynamically based on the Node.js runtime environment.
36
+ *
37
+ * This function detects the current Node.js runtime (either 'module' or 'commonjs') and loads the specified package accordingly.
38
+ *
39
+ * @template T - The type of the module to be loaded.
40
+ * @param {string} name - The name of the package to load.
41
+ * @returns {Promise<T>} A promise that resolves to the loaded package.
42
+ * @throws {Error} If the runtime is neither 'module' nor 'commonjs'.
43
+ */
44
+ declare function loadPackage<T = unknown>(name: string): Promise<T>;
45
+
3
46
  type Handler<TEvent = any, TContext = any, TResult = any> = (data: InvokeData<TEvent, TContext>) => Promise<TResult>;
4
47
  type Next = () => Promise<void>;
5
48
  type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event?: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
@@ -139,25 +182,5 @@ declare function usePlugin<T extends Plugin>(plugin: T & {
139
182
  * ```
140
183
  */
141
184
  declare function useFunc<TEvent = any, TContext = any, TResult = any>(handler: () => Handler<TEvent, TContext, TResult>): Func<TEvent, TContext, TResult>;
142
- /**
143
- * Assigns a name to a given function handler, which will be displayed in logs and error messages.
144
- *
145
- * @template T - The type of the function handler.
146
- * @param {string} name - The name to assign to the function handler.
147
- * @param {T} handler - The function handler to which the name will be assigned.
148
- * @returns {T} - The original function handler with the assigned name.
149
- *
150
- * @example
151
- * ```ts
152
- * import { nameFunc } from '@faasjs/func'
153
- *
154
- * const handler = nameFunc('myHandler', () => {
155
- * return 'Hello World'
156
- * })
157
- *
158
- * console.log(handler.name) // => 'myHandler'
159
- * ```
160
- */
161
- declare function nameFunc<T extends (...args: any[]) => any>(name: string, handler: T): T;
162
185
 
163
- export { type Config, type ExportedHandler, Func, type FuncConfig, type FuncEventType, type FuncReturnType, type Handler, type InvokeData, type LifeCycleKey, type MountData, type Next, type Plugin, type UseifyPlugin, nameFunc, useFunc, usePlugin };
186
+ export { type Config, type ExportedHandler, Func, type FuncConfig, type FuncEventType, type FuncReturnType, type Handler, type InvokeData, type LifeCycleKey, type MountData, type Next, type Plugin, type UseifyPlugin, detectNodeRuntime, loadPackage, nameFunc, useFunc, usePlugin };
package/dist/index.mjs CHANGED
@@ -29,6 +29,30 @@ var RunHandler = class {
29
29
  }
30
30
  };
31
31
 
32
+ // src/utils.ts
33
+ function nameFunc(name, handler) {
34
+ Object.defineProperty(handler, "name", { value: name });
35
+ return handler;
36
+ }
37
+ function detectNodeRuntime() {
38
+ if (typeof globalThis.require === "function" && typeof module !== "undefined")
39
+ return "commonjs";
40
+ if (typeof import.meta !== "undefined") return "module";
41
+ throw Error("Unknown runtime");
42
+ }
43
+ async function loadPackage(name) {
44
+ const runtime = detectNodeRuntime();
45
+ if (runtime === "module") {
46
+ const module2 = await import(name);
47
+ return module2.default ? module2.default : module2;
48
+ }
49
+ if (runtime === "commonjs") {
50
+ const module2 = globalThis.require(name);
51
+ return module2.default ? module2.default : module2;
52
+ }
53
+ throw Error("Unknown runtime");
54
+ }
55
+
32
56
  // src/index.ts
33
57
  var Func = class {
34
58
  plugins;
@@ -152,7 +176,7 @@ var Func = class {
152
176
  event,
153
177
  context,
154
178
  callback,
155
- response: void 0,
179
+ response: undefined,
156
180
  handler: this.handler,
157
181
  logger,
158
182
  config: this.config
@@ -193,9 +217,5 @@ function useFunc(handler) {
193
217
  plugins = [];
194
218
  return func;
195
219
  }
196
- function nameFunc(name, handler) {
197
- Object.defineProperty(handler, "name", { value: name });
198
- return handler;
199
- }
200
220
 
201
- export { Func, nameFunc, useFunc, usePlugin };
221
+ export { Func, detectNodeRuntime, loadPackage, nameFunc, useFunc, usePlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/func",
3
- "version": "5.0.1",
3
+ "version": "6.0.0-beta.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -30,12 +30,12 @@
30
30
  "dist"
31
31
  ],
32
32
  "peerDependencies": {
33
- "@faasjs/deep_merge": "5.0.1",
34
- "@faasjs/logger": "5.0.1"
33
+ "@faasjs/deep_merge": "6.0.0-beta.0",
34
+ "@faasjs/logger": "6.0.0-beta.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@faasjs/deep_merge": "5.0.1",
38
- "@faasjs/logger": "5.0.1"
37
+ "@faasjs/deep_merge": "6.0.0-beta.0",
38
+ "@faasjs/logger": "6.0.0-beta.0"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=22.0.0",