@faasjs/load 7.0.4 → 8.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
@@ -15,6 +15,7 @@ npm install @faasjs/load
15
15
 
16
16
  - [detectNodeRuntime](functions/detectNodeRuntime.md)
17
17
  - [loadConfig](functions/loadConfig.md)
18
+ - [loadFunc](functions/loadFunc.md)
18
19
  - [loadPackage](functions/loadPackage.md)
19
20
  - [resetRuntime](functions/resetRuntime.md)
20
21
 
package/dist/index.cjs CHANGED
@@ -3,7 +3,7 @@
3
3
  var fs = require('fs');
4
4
  var path = require('path');
5
5
  var deep_merge = require('@faasjs/deep_merge');
6
- var logger$1 = require('@faasjs/logger');
6
+ var logger = require('@faasjs/logger');
7
7
  var jsYaml = require('js-yaml');
8
8
 
9
9
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
@@ -14,9 +14,9 @@ var Config = class {
14
14
  origin;
15
15
  defaults;
16
16
  logger;
17
- constructor(root, filename, logger2) {
18
- this.logger = new logger$1.Logger(
19
- logger2?.label ? `${logger2.label}] [config` : "config"
17
+ constructor(root, filename, logger$1) {
18
+ this.logger = new logger.Logger(
19
+ logger$1?.label ? `${logger$1.label}] [config` : "config"
20
20
  );
21
21
  this.root = root;
22
22
  if (!this.root.endsWith(path.sep)) this.root += path.sep;
@@ -53,9 +53,11 @@ var Config = class {
53
53
  return this[key] || this.defaults || /* @__PURE__ */ Object.create(null);
54
54
  }
55
55
  };
56
- function loadConfig(root, filename, staging, logger2) {
57
- return new Config(root, filename, logger2).get(staging);
56
+ function loadConfig(root, filename, staging, logger) {
57
+ return new Config(root, filename, logger).get(staging);
58
58
  }
59
+
60
+ // src/load_package.ts
59
61
  var _runtime = null;
60
62
  function resetRuntime() {
61
63
  _runtime = null;
@@ -67,23 +69,10 @@ function detectNodeRuntime() {
67
69
  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 _runtime = "module";
68
70
  throw Error("Unknown runtime");
69
71
  }
70
- var tsxLoaded = null;
71
- var logger = new logger$1.Logger("loadPackage");
72
72
  async function loadPackage(name, defaultNames = "default") {
73
73
  const runtime = detectNodeRuntime();
74
74
  let module2;
75
75
  if (runtime === "module") {
76
- if (tsxLoaded === null) {
77
- try {
78
- await import('tsx');
79
- tsxLoaded = true;
80
- } catch (_) {
81
- tsxLoaded = false;
82
- logger.warn(
83
- 'Recommend installing the "tsx" package for loading ts/tsx files'
84
- );
85
- }
86
- }
87
76
  module2 = await import(name);
88
77
  if (typeof defaultNames === "string")
89
78
  return defaultNames in module2 ? module2[defaultNames] : module2;
@@ -91,17 +80,6 @@ async function loadPackage(name, defaultNames = "default") {
91
80
  return module2;
92
81
  }
93
82
  if (runtime === "commonjs") {
94
- if (tsxLoaded === null) {
95
- try {
96
- globalThis.require("tsx");
97
- tsxLoaded = true;
98
- } catch (_) {
99
- tsxLoaded = false;
100
- logger.warn(
101
- 'Recommend installing the "tsx" package for loading ts/tsx files'
102
- );
103
- }
104
- }
105
83
  module2 = globalThis.require(name);
106
84
  if (typeof defaultNames === "string")
107
85
  return defaultNames in module2 ? module2[defaultNames] : module2;
@@ -111,7 +89,15 @@ async function loadPackage(name, defaultNames = "default") {
111
89
  throw Error("Unknown runtime");
112
90
  }
113
91
 
92
+ // src/load_func.ts
93
+ async function loadFunc(root, filename, staging) {
94
+ const func = await loadPackage(filename);
95
+ func.config = await loadConfig(root, filename, staging);
96
+ return func.export().handler;
97
+ }
98
+
114
99
  exports.detectNodeRuntime = detectNodeRuntime;
115
100
  exports.loadConfig = loadConfig;
101
+ exports.loadFunc = loadFunc;
116
102
  exports.loadPackage = loadPackage;
117
103
  exports.resetRuntime = resetRuntime;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Config } from '@faasjs/func';
1
+ import { Config, ExportedHandler } from '@faasjs/func';
2
2
  import { Logger } from '@faasjs/logger';
3
3
 
4
4
  /**
@@ -6,28 +6,51 @@ import { Logger } from '@faasjs/logger';
6
6
  */
7
7
  declare function loadConfig(root: string, filename: string, staging: string, logger?: Logger): Config;
8
8
 
9
+ /**
10
+ * Load a FaasJS function and its configuration, returning the handler.
11
+ *
12
+ * @param root - Project root directory used to resolve configuration
13
+ * @param filename - Path to the packaged FaasJS function file to load
14
+ * @param staging - Staging directory name (used when locating config)
15
+ *
16
+ * @returns A Promise that resolves to the function handler
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { loadFunc } from '@faasjs/load'
21
+ *
22
+ * const handler = await loadFunc(
23
+ * process.cwd(),
24
+ * __dirname + '/example.func.ts',
25
+ * 'development'
26
+ * )
27
+ *
28
+ * const result = await handler(event, context)
29
+ * console.log(result)
30
+ * ```
31
+ */
32
+ declare function loadFunc<TEvent = any, TContext = any, TResult = any>(root: string, filename: string, staging: string): Promise<ExportedHandler<TEvent, TContext, TResult>>;
33
+
9
34
  type NodeRuntime = 'commonjs' | 'module';
10
35
  declare function resetRuntime(): void;
11
36
  /**
12
- * Detect the current JavaScript runtime environment.
37
+ * Detect current JavaScript runtime environment.
13
38
  *
14
- * This function checks for the presence of `import.meta` and `require` to determine
15
- * whether the runtime is using ECMAScript modules (ESM) or CommonJS modules (CJS).
39
+ * This function checks for presence of `import.meta` and `require` to determine
40
+ * whether runtime is using ECMAScript modules (ESM) or CommonJS modules (CJS).
16
41
  *
17
- * @returns {NodeRuntime} - Returns 'module' if the runtime is using ECMAScript modules,
42
+ * @returns {NodeRuntime} - Returns 'module' if runtime is using ECMAScript modules,
18
43
  * and 'cjs' if the runtime is using CommonJS modules.
19
- * @throws {Error} - Throws an error if the runtime cannot be determined.
44
+ * @throws {Error} - Throws an error if runtime cannot be determined.
20
45
  */
21
46
  declare function detectNodeRuntime(): NodeRuntime;
22
47
  /**
23
48
  * Asynchronously loads a package by its name, supporting both ESM and CommonJS runtimes.
24
49
  *
25
- * Also supports loading TypeScript and TSX files by checking for the presence of the "tsx" package.
26
- *
27
- * @template T - The type of the module to be loaded.
28
- * @param {string} name - The name of the package to load.
29
- * @returns {Promise<T>} A promise that resolves to the loaded module.
30
- * @throws {Error} If the runtime is unknown.
50
+ * @template T - The type of module to be loaded.
51
+ * @param {string} name - The name of package to load.
52
+ * @returns {Promise<T>} A promise that resolves to loaded module.
53
+ * @throws {Error} If runtime is unknown.
31
54
  *
32
55
  * @example
33
56
  * ```typescript
@@ -36,4 +59,4 @@ declare function detectNodeRuntime(): NodeRuntime;
36
59
  */
37
60
  declare function loadPackage<T = unknown>(name: string, defaultNames?: string | string[]): Promise<T>;
38
61
 
39
- export { type NodeRuntime, detectNodeRuntime, loadConfig, loadPackage, resetRuntime };
62
+ export { type NodeRuntime, detectNodeRuntime, loadConfig, loadFunc, loadPackage, resetRuntime };
package/dist/index.mjs CHANGED
@@ -11,9 +11,9 @@ var Config = class {
11
11
  origin;
12
12
  defaults;
13
13
  logger;
14
- constructor(root, filename, logger2) {
14
+ constructor(root, filename, logger) {
15
15
  this.logger = new Logger(
16
- logger2?.label ? `${logger2.label}] [config` : "config"
16
+ logger?.label ? `${logger.label}] [config` : "config"
17
17
  );
18
18
  this.root = root;
19
19
  if (!this.root.endsWith(sep)) this.root += sep;
@@ -50,9 +50,11 @@ var Config = class {
50
50
  return this[key] || this.defaults || /* @__PURE__ */ Object.create(null);
51
51
  }
52
52
  };
53
- function loadConfig(root, filename, staging, logger2) {
54
- return new Config(root, filename, logger2).get(staging);
53
+ function loadConfig(root, filename, staging, logger) {
54
+ return new Config(root, filename, logger).get(staging);
55
55
  }
56
+
57
+ // src/load_package.ts
56
58
  var _runtime = null;
57
59
  function resetRuntime() {
58
60
  _runtime = null;
@@ -64,23 +66,10 @@ function detectNodeRuntime() {
64
66
  if (typeof import.meta !== "undefined") return _runtime = "module";
65
67
  throw Error("Unknown runtime");
66
68
  }
67
- var tsxLoaded = null;
68
- var logger = new Logger("loadPackage");
69
69
  async function loadPackage(name, defaultNames = "default") {
70
70
  const runtime = detectNodeRuntime();
71
71
  let module2;
72
72
  if (runtime === "module") {
73
- if (tsxLoaded === null) {
74
- try {
75
- await import('tsx');
76
- tsxLoaded = true;
77
- } catch (_) {
78
- tsxLoaded = false;
79
- logger.warn(
80
- 'Recommend installing the "tsx" package for loading ts/tsx files'
81
- );
82
- }
83
- }
84
73
  module2 = await import(name);
85
74
  if (typeof defaultNames === "string")
86
75
  return defaultNames in module2 ? module2[defaultNames] : module2;
@@ -88,17 +77,6 @@ async function loadPackage(name, defaultNames = "default") {
88
77
  return module2;
89
78
  }
90
79
  if (runtime === "commonjs") {
91
- if (tsxLoaded === null) {
92
- try {
93
- globalThis.require("tsx");
94
- tsxLoaded = true;
95
- } catch (_) {
96
- tsxLoaded = false;
97
- logger.warn(
98
- 'Recommend installing the "tsx" package for loading ts/tsx files'
99
- );
100
- }
101
- }
102
80
  module2 = globalThis.require(name);
103
81
  if (typeof defaultNames === "string")
104
82
  return defaultNames in module2 ? module2[defaultNames] : module2;
@@ -108,4 +86,11 @@ async function loadPackage(name, defaultNames = "default") {
108
86
  throw Error("Unknown runtime");
109
87
  }
110
88
 
111
- export { detectNodeRuntime, loadConfig, loadPackage, resetRuntime };
89
+ // src/load_func.ts
90
+ async function loadFunc(root, filename, staging) {
91
+ const func = await loadPackage(filename);
92
+ func.config = await loadConfig(root, filename, staging);
93
+ return func.export().handler;
94
+ }
95
+
96
+ export { detectNodeRuntime, loadConfig, loadFunc, loadPackage, resetRuntime };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/load",
3
- "version": "7.0.4",
3
+ "version": "v8.0.0-beta.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -31,16 +31,14 @@
31
31
  ],
32
32
  "peerDependencies": {
33
33
  "js-yaml": "*",
34
- "tsx": "*",
35
- "@faasjs/deep_merge": ">=7.0.4",
36
- "@faasjs/func": ">=7.0.4"
34
+ "@faasjs/deep_merge": ">=v8.0.0-beta.0",
35
+ "@faasjs/func": ">=v8.0.0-beta.0"
37
36
  },
38
37
  "devDependencies": {
39
38
  "js-yaml": "*",
40
- "tsx": "*",
41
39
  "@types/js-yaml": "*",
42
- "@faasjs/deep_merge": ">=7.0.4",
43
- "@faasjs/func": ">=7.0.4"
40
+ "@faasjs/deep_merge": ">=v8.0.0-beta.0",
41
+ "@faasjs/func": ">=v8.0.0-beta.0"
44
42
  },
45
43
  "engines": {
46
44
  "node": ">=24.0.0",