@faasjs/func 2.8.1 → 3.0.0-canary.1

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
@@ -18,7 +18,6 @@ npm install @faasjs/func
18
18
  ## Type Aliases
19
19
 
20
20
  - [Config](type-aliases/Config.md)
21
- - [DeployData](type-aliases/DeployData.md)
22
21
  - [ExportedHandler](type-aliases/ExportedHandler.md)
23
22
  - [FuncConfig](type-aliases/FuncConfig.md)
24
23
  - [FuncEventType](type-aliases/FuncEventType.md)
@@ -29,7 +28,6 @@ npm install @faasjs/func
29
28
  - [MountData](type-aliases/MountData.md)
30
29
  - [Next](type-aliases/Next.md)
31
30
  - [Plugin](type-aliases/Plugin.md)
32
- - [ProviderConfig](type-aliases/ProviderConfig.md)
33
31
  - [UseifyPlugin](type-aliases/UseifyPlugin.md)
34
32
 
35
33
  ## Functions
package/dist/index.d.mts CHANGED
@@ -16,30 +16,19 @@ import { Logger } from '@faasjs/logger';
16
16
 
17
17
  type Handler<TEvent = any, TContext = any, TResult = any> = (data: InvokeData<TEvent, TContext>) => Promise<TResult>;
18
18
  type Next = () => Promise<void>;
19
- type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
19
+ type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event?: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
20
20
  type Plugin = {
21
21
  [key: string]: any;
22
22
  readonly type: string;
23
23
  readonly name: string;
24
- onDeploy?: (data: DeployData, next: Next) => Promise<void>;
25
24
  onMount?: (data: MountData, next: Next) => Promise<void>;
26
25
  onInvoke?: (data: InvokeData, next: Next) => Promise<void>;
27
26
  };
28
- type ProviderConfig = {
29
- type: string;
30
- config: {
31
- [key: string]: any;
32
- };
33
- };
34
27
  type Config = {
35
28
  [key: string]: any;
36
- providers?: {
37
- [key: string]: ProviderConfig;
38
- };
39
29
  plugins?: {
40
30
  [key: string]: {
41
31
  [key: string]: any;
42
- provider?: string | ProviderConfig;
43
32
  type: string;
44
33
  config?: {
45
34
  [key: string]: any;
@@ -47,31 +36,6 @@ type Config = {
47
36
  };
48
37
  };
49
38
  };
50
- type DeployData = {
51
- [key: string]: any;
52
- root: string;
53
- filename: string;
54
- env?: string;
55
- name?: string;
56
- config?: Config;
57
- version?: string;
58
- dependencies: {
59
- [name: string]: string;
60
- };
61
- plugins?: {
62
- [name: string]: {
63
- [key: string]: any;
64
- name?: string;
65
- type: string;
66
- provider?: string;
67
- config: {
68
- [key: string]: any;
69
- };
70
- plugin: Plugin;
71
- };
72
- };
73
- logger?: Logger;
74
- };
75
39
  type MountData = {
76
40
  [key: string]: any;
77
41
  config: Config;
@@ -88,7 +52,7 @@ type InvokeData<TEvent = any, TContext = any, TResult = any> = {
88
52
  handler?: Handler<TEvent, TContext, TResult>;
89
53
  config: Config;
90
54
  };
91
- type LifeCycleKey = 'onDeploy' | 'onMount' | 'onInvoke';
55
+ type LifeCycleKey = 'onMount' | 'onInvoke';
92
56
  type FuncConfig<TEvent = any, TContext = any, TResult = any> = {
93
57
  plugins?: Plugin[];
94
58
  handler?: Handler<TEvent, TContext, TResult>;
@@ -135,14 +99,6 @@ declare class Func<TEvent = any, TContext = any, TResult = any> {
135
99
  */
136
100
  constructor(config: FuncConfig<TEvent, TContext>);
137
101
  private compose;
138
- /**
139
- * Deploy the function
140
- * @param data {object} data
141
- * @param data.root {string} root path
142
- * @param data.filename {string} filename
143
- * @param data.env {string} environment
144
- */
145
- deploy(data: DeployData): any;
146
102
  /**
147
103
  * First time mount the function
148
104
  */
@@ -165,11 +121,15 @@ declare class Func<TEvent = any, TContext = any, TResult = any> {
165
121
  };
166
122
  }
167
123
  type UseifyPlugin<T> = T & {
168
- mount?: (data?: {
124
+ mount: (data?: {
169
125
  config?: Config;
170
126
  }) => Promise<T>;
171
127
  };
172
- declare function usePlugin<T extends Plugin>(plugin: UseifyPlugin<T>): UseifyPlugin<T>;
128
+ declare function usePlugin<T extends Plugin>(plugin: T & {
129
+ mount?: (data?: {
130
+ config?: Config;
131
+ }) => Promise<T>;
132
+ }): UseifyPlugin<T>;
173
133
  /**
174
134
  * ```ts
175
135
  * // pure function
@@ -193,4 +153,4 @@ declare function usePlugin<T extends Plugin>(plugin: UseifyPlugin<T>): UseifyPlu
193
153
  */
194
154
  declare function useFunc<TEvent = any, TContext = any, TResult = any>(handler: () => Handler<TEvent, TContext, TResult>): Func<TEvent, TContext, TResult>;
195
155
 
196
- export { type Config, type DeployData, type ExportedHandler, Func, type FuncConfig, type FuncEventType, type FuncReturnType, type Handler, type InvokeData, type LifeCycleKey, type MountData, type Next, type Plugin, type ProviderConfig, type UseifyPlugin, useFunc, usePlugin };
156
+ 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, useFunc, usePlugin };
package/dist/index.d.ts CHANGED
@@ -16,30 +16,19 @@ import { Logger } from '@faasjs/logger';
16
16
 
17
17
  type Handler<TEvent = any, TContext = any, TResult = any> = (data: InvokeData<TEvent, TContext>) => Promise<TResult>;
18
18
  type Next = () => Promise<void>;
19
- type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
19
+ type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event?: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
20
20
  type Plugin = {
21
21
  [key: string]: any;
22
22
  readonly type: string;
23
23
  readonly name: string;
24
- onDeploy?: (data: DeployData, next: Next) => Promise<void>;
25
24
  onMount?: (data: MountData, next: Next) => Promise<void>;
26
25
  onInvoke?: (data: InvokeData, next: Next) => Promise<void>;
27
26
  };
28
- type ProviderConfig = {
29
- type: string;
30
- config: {
31
- [key: string]: any;
32
- };
33
- };
34
27
  type Config = {
35
28
  [key: string]: any;
36
- providers?: {
37
- [key: string]: ProviderConfig;
38
- };
39
29
  plugins?: {
40
30
  [key: string]: {
41
31
  [key: string]: any;
42
- provider?: string | ProviderConfig;
43
32
  type: string;
44
33
  config?: {
45
34
  [key: string]: any;
@@ -47,31 +36,6 @@ type Config = {
47
36
  };
48
37
  };
49
38
  };
50
- type DeployData = {
51
- [key: string]: any;
52
- root: string;
53
- filename: string;
54
- env?: string;
55
- name?: string;
56
- config?: Config;
57
- version?: string;
58
- dependencies: {
59
- [name: string]: string;
60
- };
61
- plugins?: {
62
- [name: string]: {
63
- [key: string]: any;
64
- name?: string;
65
- type: string;
66
- provider?: string;
67
- config: {
68
- [key: string]: any;
69
- };
70
- plugin: Plugin;
71
- };
72
- };
73
- logger?: Logger;
74
- };
75
39
  type MountData = {
76
40
  [key: string]: any;
77
41
  config: Config;
@@ -88,7 +52,7 @@ type InvokeData<TEvent = any, TContext = any, TResult = any> = {
88
52
  handler?: Handler<TEvent, TContext, TResult>;
89
53
  config: Config;
90
54
  };
91
- type LifeCycleKey = 'onDeploy' | 'onMount' | 'onInvoke';
55
+ type LifeCycleKey = 'onMount' | 'onInvoke';
92
56
  type FuncConfig<TEvent = any, TContext = any, TResult = any> = {
93
57
  plugins?: Plugin[];
94
58
  handler?: Handler<TEvent, TContext, TResult>;
@@ -135,14 +99,6 @@ declare class Func<TEvent = any, TContext = any, TResult = any> {
135
99
  */
136
100
  constructor(config: FuncConfig<TEvent, TContext>);
137
101
  private compose;
138
- /**
139
- * Deploy the function
140
- * @param data {object} data
141
- * @param data.root {string} root path
142
- * @param data.filename {string} filename
143
- * @param data.env {string} environment
144
- */
145
- deploy(data: DeployData): any;
146
102
  /**
147
103
  * First time mount the function
148
104
  */
@@ -165,11 +121,15 @@ declare class Func<TEvent = any, TContext = any, TResult = any> {
165
121
  };
166
122
  }
167
123
  type UseifyPlugin<T> = T & {
168
- mount?: (data?: {
124
+ mount: (data?: {
169
125
  config?: Config;
170
126
  }) => Promise<T>;
171
127
  };
172
- declare function usePlugin<T extends Plugin>(plugin: UseifyPlugin<T>): UseifyPlugin<T>;
128
+ declare function usePlugin<T extends Plugin>(plugin: T & {
129
+ mount?: (data?: {
130
+ config?: Config;
131
+ }) => Promise<T>;
132
+ }): UseifyPlugin<T>;
173
133
  /**
174
134
  * ```ts
175
135
  * // pure function
@@ -193,4 +153,4 @@ declare function usePlugin<T extends Plugin>(plugin: UseifyPlugin<T>): UseifyPlu
193
153
  */
194
154
  declare function useFunc<TEvent = any, TContext = any, TResult = any>(handler: () => Handler<TEvent, TContext, TResult>): Func<TEvent, TContext, TResult>;
195
155
 
196
- export { type Config, type DeployData, type ExportedHandler, Func, type FuncConfig, type FuncEventType, type FuncReturnType, type Handler, type InvokeData, type LifeCycleKey, type MountData, type Next, type Plugin, type ProviderConfig, type UseifyPlugin, useFunc, usePlugin };
156
+ 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, useFunc, usePlugin };
package/dist/index.js CHANGED
@@ -7,10 +7,8 @@ var crypto = require('crypto');
7
7
 
8
8
  // src/plugins/run_handler/index.ts
9
9
  var RunHandler = class {
10
- constructor() {
11
- this.type = "handler";
12
- this.name = "handler";
13
- }
10
+ type = "handler";
11
+ name = "handler";
14
12
  async onInvoke(data, next) {
15
13
  if (data.handler)
16
14
  if (!data.runHandler) {
@@ -32,6 +30,12 @@ var RunHandler = class {
32
30
  }
33
31
  };
34
32
  var Func = class {
33
+ plugins;
34
+ handler;
35
+ config;
36
+ mounted;
37
+ filename;
38
+ cachedFunctions;
35
39
  /**
36
40
  * Create a cloud function
37
41
  * @param config {object} config
@@ -43,7 +47,6 @@ var Func = class {
43
47
  this.plugins = config.plugins || [];
44
48
  this.plugins.push(new RunHandler());
45
49
  this.config = {
46
- providers: /* @__PURE__ */ Object.create(null),
47
50
  plugins: /* @__PURE__ */ Object.create(null)
48
51
  };
49
52
  this.mounted = false;
@@ -69,7 +72,7 @@ var Func = class {
69
72
  }
70
73
  return async (data, next) => {
71
74
  let index = -1;
72
- const logger$1 = (data == null ? void 0 : data.logger) || new logger.Logger();
75
+ const logger$1 = data?.logger || new logger.Logger();
73
76
  const dispatch = async (i) => {
74
77
  if (i <= index)
75
78
  return Promise.reject(Error("next() called multiple times"));
@@ -95,28 +98,12 @@ var Func = class {
95
98
  return await dispatch(0);
96
99
  };
97
100
  }
98
- /**
99
- * Deploy the function
100
- * @param data {object} data
101
- * @param data.root {string} root path
102
- * @param data.filename {string} filename
103
- * @param data.env {string} environment
104
- */
105
- deploy(data) {
106
- if (!data.logger) data.logger = new logger.Logger("Func");
107
- data.logger.debug("onDeploy");
108
- data.logger.debug(
109
- `Plugins: ${this.plugins.map((p) => `${p.type}#${p.name}`).join(",")}`
110
- );
111
- return this.compose("onDeploy")(data);
112
- }
113
101
  /**
114
102
  * First time mount the function
115
103
  */
116
104
  async mount(data) {
117
- var _a;
118
105
  if (!data.logger) data.logger = new logger.Logger("Func");
119
- const logger$1 = new logger.Logger(((_a = data.logger) == null ? void 0 : _a.label) || "Func");
106
+ const logger$1 = new logger.Logger(data.logger?.label || "Func");
120
107
  if (!logger$1.label.endsWith("Func")) logger$1.label = `${logger$1.label}] [Func`;
121
108
  logger$1.debug("onMount");
122
109
  if (this.mounted) {
@@ -159,10 +146,9 @@ var Func = class {
159
146
  */
160
147
  export() {
161
148
  const handler = async (event, context, callback) => {
162
- var _a;
163
149
  if (typeof context === "undefined") context = {};
164
150
  if (!context.request_id)
165
- context.request_id = ((_a = event == null ? void 0 : event.headers) == null ? void 0 : _a["x-faasjs-request-id"]) || crypto.randomBytes(16).toString("hex");
151
+ context.request_id = event?.headers?.["x-faasjs-request-id"] || crypto.randomBytes(16).toString("hex");
166
152
  if (!context.request_at)
167
153
  context.request_at = Math.round(Date.now() / 1e3);
168
154
  context.callbackWaitsForEmptyEventLoop = false;
@@ -195,7 +181,7 @@ function usePlugin(plugin) {
195
181
  if (plugin.onMount)
196
182
  await plugin.onMount(
197
183
  {
198
- config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
184
+ config: data?.config || /* @__PURE__ */ Object.create(null),
199
185
  event: /* @__PURE__ */ Object.create(null),
200
186
  context: /* @__PURE__ */ Object.create(null),
201
187
  logger: new logger.Logger(plugin.name)
package/dist/index.mjs CHANGED
@@ -5,10 +5,8 @@ import { randomBytes } from 'node:crypto';
5
5
 
6
6
  // src/plugins/run_handler/index.ts
7
7
  var RunHandler = class {
8
- constructor() {
9
- this.type = "handler";
10
- this.name = "handler";
11
- }
8
+ type = "handler";
9
+ name = "handler";
12
10
  async onInvoke(data, next) {
13
11
  if (data.handler)
14
12
  if (!data.runHandler) {
@@ -30,6 +28,12 @@ var RunHandler = class {
30
28
  }
31
29
  };
32
30
  var Func = class {
31
+ plugins;
32
+ handler;
33
+ config;
34
+ mounted;
35
+ filename;
36
+ cachedFunctions;
33
37
  /**
34
38
  * Create a cloud function
35
39
  * @param config {object} config
@@ -41,7 +45,6 @@ var Func = class {
41
45
  this.plugins = config.plugins || [];
42
46
  this.plugins.push(new RunHandler());
43
47
  this.config = {
44
- providers: /* @__PURE__ */ Object.create(null),
45
48
  plugins: /* @__PURE__ */ Object.create(null)
46
49
  };
47
50
  this.mounted = false;
@@ -67,7 +70,7 @@ var Func = class {
67
70
  }
68
71
  return async (data, next) => {
69
72
  let index = -1;
70
- const logger = (data == null ? void 0 : data.logger) || new Logger();
73
+ const logger = data?.logger || new Logger();
71
74
  const dispatch = async (i) => {
72
75
  if (i <= index)
73
76
  return Promise.reject(Error("next() called multiple times"));
@@ -93,28 +96,12 @@ var Func = class {
93
96
  return await dispatch(0);
94
97
  };
95
98
  }
96
- /**
97
- * Deploy the function
98
- * @param data {object} data
99
- * @param data.root {string} root path
100
- * @param data.filename {string} filename
101
- * @param data.env {string} environment
102
- */
103
- deploy(data) {
104
- if (!data.logger) data.logger = new Logger("Func");
105
- data.logger.debug("onDeploy");
106
- data.logger.debug(
107
- `Plugins: ${this.plugins.map((p) => `${p.type}#${p.name}`).join(",")}`
108
- );
109
- return this.compose("onDeploy")(data);
110
- }
111
99
  /**
112
100
  * First time mount the function
113
101
  */
114
102
  async mount(data) {
115
- var _a;
116
103
  if (!data.logger) data.logger = new Logger("Func");
117
- const logger = new Logger(((_a = data.logger) == null ? void 0 : _a.label) || "Func");
104
+ const logger = new Logger(data.logger?.label || "Func");
118
105
  if (!logger.label.endsWith("Func")) logger.label = `${logger.label}] [Func`;
119
106
  logger.debug("onMount");
120
107
  if (this.mounted) {
@@ -157,10 +144,9 @@ var Func = class {
157
144
  */
158
145
  export() {
159
146
  const handler = async (event, context, callback) => {
160
- var _a;
161
147
  if (typeof context === "undefined") context = {};
162
148
  if (!context.request_id)
163
- context.request_id = ((_a = event == null ? void 0 : event.headers) == null ? void 0 : _a["x-faasjs-request-id"]) || randomBytes(16).toString("hex");
149
+ context.request_id = event?.headers?.["x-faasjs-request-id"] || randomBytes(16).toString("hex");
164
150
  if (!context.request_at)
165
151
  context.request_at = Math.round(Date.now() / 1e3);
166
152
  context.callbackWaitsForEmptyEventLoop = false;
@@ -193,7 +179,7 @@ function usePlugin(plugin) {
193
179
  if (plugin.onMount)
194
180
  await plugin.onMount(
195
181
  {
196
- config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
182
+ config: data?.config || /* @__PURE__ */ Object.create(null),
197
183
  event: /* @__PURE__ */ Object.create(null),
198
184
  context: /* @__PURE__ */ Object.create(null),
199
185
  logger: new Logger(plugin.name)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/func",
3
- "version": "2.8.1",
3
+ "version": "3.0.0-canary.1",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -34,11 +34,11 @@
34
34
  "dist"
35
35
  ],
36
36
  "dependencies": {
37
- "@faasjs/deep_merge": "2.8.1",
38
- "@faasjs/logger": "2.8.1"
37
+ "@faasjs/deep_merge": "3.0.0-canary.1",
38
+ "@faasjs/logger": "3.0.0-canary.1"
39
39
  },
40
40
  "engines": {
41
- "npm": ">=9.0.0",
42
- "node": ">=18.0.0"
41
+ "node": ">=22.0.0",
42
+ "npm": ">=10.0.0"
43
43
  }
44
44
  }