@faasjs/func 0.0.3-beta.86 → 0.0.3-beta.88
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/dist/index.d.mts +156 -0
- package/dist/index.js +47 -51
- package/dist/index.mjs +29 -10
- package/package.json +4 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Logger } from '@faasjs/logger';
|
|
2
|
+
|
|
3
|
+
type Handler<TEvent = any, TContext = any, TResult = any> = (data: InvokeData<TEvent, TContext>) => Promise<TResult>;
|
|
4
|
+
type Next = () => Promise<void>;
|
|
5
|
+
type ExportedHandler<TEvent = any, TContext = any, TResult = any> = (event: TEvent, context?: TContext, callback?: (...args: any) => any) => Promise<TResult>;
|
|
6
|
+
type Plugin = {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
readonly type: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
onDeploy?: (data: DeployData, next: Next) => void | Promise<void>;
|
|
11
|
+
onMount?: (data: MountData, next: Next) => void | Promise<void>;
|
|
12
|
+
onInvoke?: (data: InvokeData, next: Next) => void | Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
type ProviderConfig = {
|
|
15
|
+
type: string;
|
|
16
|
+
config: {
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type Config = {
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
providers?: {
|
|
23
|
+
[key: string]: ProviderConfig;
|
|
24
|
+
};
|
|
25
|
+
plugins?: {
|
|
26
|
+
[key: string]: {
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
provider?: string | ProviderConfig;
|
|
29
|
+
type: string;
|
|
30
|
+
config?: {
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
type DeployData = {
|
|
37
|
+
[key: string]: any;
|
|
38
|
+
root: string;
|
|
39
|
+
filename: string;
|
|
40
|
+
env?: string;
|
|
41
|
+
name?: string;
|
|
42
|
+
config?: Config;
|
|
43
|
+
version?: string;
|
|
44
|
+
dependencies: {
|
|
45
|
+
[name: string]: string;
|
|
46
|
+
};
|
|
47
|
+
plugins?: {
|
|
48
|
+
[name: string]: {
|
|
49
|
+
[key: string]: any;
|
|
50
|
+
name?: string;
|
|
51
|
+
type: string;
|
|
52
|
+
provider?: string;
|
|
53
|
+
config: {
|
|
54
|
+
[key: string]: any;
|
|
55
|
+
};
|
|
56
|
+
plugin: Plugin;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
logger?: Logger;
|
|
60
|
+
};
|
|
61
|
+
type MountData = {
|
|
62
|
+
[key: string]: any;
|
|
63
|
+
config: Config;
|
|
64
|
+
event: any;
|
|
65
|
+
context: any;
|
|
66
|
+
};
|
|
67
|
+
type InvokeData<TEvent = any, TContext = any, TResult = any> = {
|
|
68
|
+
[key: string]: any;
|
|
69
|
+
event: TEvent;
|
|
70
|
+
context: TContext;
|
|
71
|
+
callback: any;
|
|
72
|
+
response: any;
|
|
73
|
+
logger: Logger;
|
|
74
|
+
handler?: Handler<TEvent, TContext, TResult>;
|
|
75
|
+
config: Config;
|
|
76
|
+
};
|
|
77
|
+
type LifeCycleKey = 'onDeploy' | 'onMount' | 'onInvoke';
|
|
78
|
+
type FuncConfig<TEvent = any, TContext = any, TResult = any> = {
|
|
79
|
+
plugins?: Plugin[];
|
|
80
|
+
handler?: Handler<TEvent, TContext, TResult>;
|
|
81
|
+
};
|
|
82
|
+
declare class Func<TEvent = any, TContext = any, TResult = any> {
|
|
83
|
+
[key: string]: any;
|
|
84
|
+
plugins: Plugin[];
|
|
85
|
+
handler?: Handler<TEvent, TContext, TResult>;
|
|
86
|
+
config: Config;
|
|
87
|
+
mounted: boolean;
|
|
88
|
+
filename?: string;
|
|
89
|
+
private cachedFunctions;
|
|
90
|
+
/**
|
|
91
|
+
* Create a cloud function
|
|
92
|
+
* @param config {object} config
|
|
93
|
+
* @param config.plugins {Plugin[]} plugins list
|
|
94
|
+
* @param config.handler {Handler} business logic
|
|
95
|
+
*/
|
|
96
|
+
constructor(config: FuncConfig<TEvent, TContext>);
|
|
97
|
+
private compose;
|
|
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: DeployData): any;
|
|
106
|
+
/**
|
|
107
|
+
* First time mount the function
|
|
108
|
+
*/
|
|
109
|
+
mount(data: {
|
|
110
|
+
event: TEvent;
|
|
111
|
+
context: TContext;
|
|
112
|
+
config?: Config;
|
|
113
|
+
logger?: Logger;
|
|
114
|
+
}): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Invoke the function
|
|
117
|
+
* @param data {object} data
|
|
118
|
+
*/
|
|
119
|
+
invoke(data: InvokeData<TEvent, TContext, TResult>): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* Export the function
|
|
122
|
+
*/
|
|
123
|
+
export(): {
|
|
124
|
+
handler: ExportedHandler<TEvent, TContext, TResult>;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
type UseifyPlugin<T> = T & {
|
|
128
|
+
mount?: (data?: {
|
|
129
|
+
config?: Config;
|
|
130
|
+
}) => Promise<T>;
|
|
131
|
+
};
|
|
132
|
+
declare function usePlugin<T extends Plugin>(plugin: UseifyPlugin<T>): UseifyPlugin<T>;
|
|
133
|
+
/**
|
|
134
|
+
* ```ts
|
|
135
|
+
* // pure function
|
|
136
|
+
* export default useFunc(() => {
|
|
137
|
+
* return () => {
|
|
138
|
+
* return 'Hello World'
|
|
139
|
+
* }
|
|
140
|
+
* })
|
|
141
|
+
*
|
|
142
|
+
* // with http
|
|
143
|
+
* import { useHttp } from '@faasjs/http'
|
|
144
|
+
*
|
|
145
|
+
* export default useFunc(() => {
|
|
146
|
+
* const http = useHttp<{ name: string }>()
|
|
147
|
+
*
|
|
148
|
+
* return () => {
|
|
149
|
+
* return `Hello ${http.params.name}`
|
|
150
|
+
* }
|
|
151
|
+
* })
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
declare function useFunc<TEvent = any, TContext = any, TResult = any>(handler: () => Handler<TEvent, TContext, TResult>): Func<TEvent, TContext, TResult>;
|
|
155
|
+
|
|
156
|
+
export { Config, DeployData, ExportedHandler, Func, FuncConfig, Handler, InvokeData, LifeCycleKey, MountData, Next, Plugin, ProviderConfig, UseifyPlugin, useFunc, usePlugin };
|
package/dist/index.js
CHANGED
|
@@ -1,31 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var logger = require('@faasjs/logger');
|
|
4
|
+
var crypto = require('crypto');
|
|
19
5
|
|
|
20
6
|
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
Func: () => Func,
|
|
24
|
-
useFunc: () => useFunc,
|
|
25
|
-
usePlugin: () => usePlugin
|
|
26
|
-
});
|
|
27
|
-
module.exports = __toCommonJS(src_exports);
|
|
28
|
-
var import_logger = require("@faasjs/logger");
|
|
29
7
|
|
|
30
8
|
// src/plugins/run_handler/index.ts
|
|
31
9
|
var RunHandler = class {
|
|
@@ -56,10 +34,13 @@ var RunHandler = class {
|
|
|
56
34
|
await next();
|
|
57
35
|
}
|
|
58
36
|
};
|
|
59
|
-
|
|
60
|
-
// src/index.ts
|
|
61
|
-
var import_crypto = require("crypto");
|
|
62
37
|
var Func = class {
|
|
38
|
+
/**
|
|
39
|
+
* Create a cloud function
|
|
40
|
+
* @param config {object} config
|
|
41
|
+
* @param config.plugins {Plugin[]} plugins list
|
|
42
|
+
* @param config.handler {Handler} business logic
|
|
43
|
+
*/
|
|
63
44
|
constructor(config) {
|
|
64
45
|
this.handler = config.handler;
|
|
65
46
|
this.plugins = config.plugins || [];
|
|
@@ -73,7 +54,7 @@ var Func = class {
|
|
|
73
54
|
try {
|
|
74
55
|
this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
|
|
75
56
|
} catch (error) {
|
|
76
|
-
new
|
|
57
|
+
new logger.Logger("Func").warn(error.message);
|
|
77
58
|
}
|
|
78
59
|
}
|
|
79
60
|
compose(key) {
|
|
@@ -93,7 +74,7 @@ var Func = class {
|
|
|
93
74
|
}
|
|
94
75
|
return async function(data, next) {
|
|
95
76
|
let index = -1;
|
|
96
|
-
const logger = (data == null ? void 0 : data.logger) || new
|
|
77
|
+
const logger$1 = (data == null ? void 0 : data.logger) || new logger.Logger();
|
|
97
78
|
const dispatch = async function(i) {
|
|
98
79
|
if (i <= index)
|
|
99
80
|
return Promise.reject(Error("next() called multiple times"));
|
|
@@ -105,31 +86,41 @@ var Func = class {
|
|
|
105
86
|
return Promise.resolve();
|
|
106
87
|
if (typeof fn.key === "undefined")
|
|
107
88
|
fn.key = `UnNamedPlugin#${i}`;
|
|
108
|
-
logger.debug("[%s][%s] begin", fn.key, key);
|
|
109
|
-
logger.time(fn.key);
|
|
89
|
+
logger$1.debug("[%s][%s] begin", fn.key, key);
|
|
90
|
+
logger$1.time(fn.key);
|
|
110
91
|
try {
|
|
111
92
|
const res = await Promise.resolve(fn.handler(data, dispatch.bind(null, i + 1)));
|
|
112
|
-
logger.timeEnd(fn.key, "[%s][%s] end", fn.key, key);
|
|
93
|
+
logger$1.timeEnd(fn.key, "[%s][%s] end", fn.key, key);
|
|
113
94
|
return res;
|
|
114
95
|
} catch (err) {
|
|
115
|
-
logger.timeEnd(fn.key, "[%s][%s] failed", fn.key, key);
|
|
116
|
-
logger.error(err);
|
|
96
|
+
logger$1.timeEnd(fn.key, "[%s][%s] failed", fn.key, key);
|
|
97
|
+
logger$1.error(err);
|
|
117
98
|
return Promise.reject(err);
|
|
118
99
|
}
|
|
119
100
|
};
|
|
120
101
|
return await dispatch(0);
|
|
121
102
|
};
|
|
122
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Deploy the function
|
|
106
|
+
* @param data {object} data
|
|
107
|
+
* @param data.root {string} root path
|
|
108
|
+
* @param data.filename {string} filename
|
|
109
|
+
* @param data.env {string} environment
|
|
110
|
+
*/
|
|
123
111
|
deploy(data) {
|
|
124
112
|
if (!data.logger)
|
|
125
|
-
data.logger = new
|
|
113
|
+
data.logger = new logger.Logger("Func");
|
|
126
114
|
data.logger.debug("onDeploy");
|
|
127
115
|
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
128
116
|
return this.compose("onDeploy")(data);
|
|
129
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* First time mount the function
|
|
120
|
+
*/
|
|
130
121
|
async mount(data) {
|
|
131
122
|
if (!data.logger)
|
|
132
|
-
data.logger = new
|
|
123
|
+
data.logger = new logger.Logger("Func");
|
|
133
124
|
data.logger.debug("onMount");
|
|
134
125
|
if (this.mounted) {
|
|
135
126
|
data.logger.warn("mount() has been called, skipped.");
|
|
@@ -146,6 +137,10 @@ var Func = class {
|
|
|
146
137
|
data.logger.timeEnd("mount", "mounted");
|
|
147
138
|
}
|
|
148
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Invoke the function
|
|
142
|
+
* @param data {object} data
|
|
143
|
+
*/
|
|
149
144
|
async invoke(data) {
|
|
150
145
|
if (!this.mounted)
|
|
151
146
|
await this.mount({
|
|
@@ -161,25 +156,28 @@ var Func = class {
|
|
|
161
156
|
data.response = error;
|
|
162
157
|
}
|
|
163
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Export the function
|
|
161
|
+
*/
|
|
164
162
|
export() {
|
|
165
163
|
const handler = async (event, context, callback) => {
|
|
166
164
|
if (typeof context === "undefined")
|
|
167
165
|
context = {};
|
|
168
166
|
if (!context.request_id)
|
|
169
|
-
context.request_id =
|
|
167
|
+
context.request_id = crypto.randomBytes(16).toString("hex");
|
|
170
168
|
if (!context.request_at)
|
|
171
|
-
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
169
|
+
context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
172
170
|
context.callbackWaitsForEmptyEventLoop = false;
|
|
173
|
-
const logger = new
|
|
174
|
-
logger.debug("event: %j", event);
|
|
175
|
-
logger.debug("context: %j", context);
|
|
171
|
+
const logger$1 = new logger.Logger(context.request_id);
|
|
172
|
+
logger$1.debug("event: %j", event);
|
|
173
|
+
logger$1.debug("context: %j", context);
|
|
176
174
|
const data = {
|
|
177
175
|
event,
|
|
178
176
|
context,
|
|
179
177
|
callback,
|
|
180
178
|
response: void 0,
|
|
181
179
|
handler: this.handler,
|
|
182
|
-
logger,
|
|
180
|
+
logger: logger$1,
|
|
183
181
|
config: this.config
|
|
184
182
|
};
|
|
185
183
|
await this.invoke(data);
|
|
@@ -202,7 +200,7 @@ function usePlugin(plugin) {
|
|
|
202
200
|
config: (data == null ? void 0 : data.config) || /* @__PURE__ */ Object.create(null),
|
|
203
201
|
event: /* @__PURE__ */ Object.create(null),
|
|
204
202
|
context: /* @__PURE__ */ Object.create(null),
|
|
205
|
-
logger: new
|
|
203
|
+
logger: new logger.Logger(plugin.name)
|
|
206
204
|
}, async () => Promise.resolve());
|
|
207
205
|
return plugin;
|
|
208
206
|
};
|
|
@@ -218,9 +216,7 @@ function useFunc(handler) {
|
|
|
218
216
|
plugins = [];
|
|
219
217
|
return func;
|
|
220
218
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
usePlugin
|
|
226
|
-
});
|
|
219
|
+
|
|
220
|
+
exports.Func = Func;
|
|
221
|
+
exports.useFunc = useFunc;
|
|
222
|
+
exports.usePlugin = usePlugin;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { Logger } from '@faasjs/logger';
|
|
2
|
+
import { randomBytes } from 'crypto';
|
|
3
|
+
|
|
1
4
|
// src/index.ts
|
|
2
|
-
import { Logger } from "@faasjs/logger";
|
|
3
5
|
|
|
4
6
|
// src/plugins/run_handler/index.ts
|
|
5
7
|
var RunHandler = class {
|
|
@@ -30,10 +32,13 @@ var RunHandler = class {
|
|
|
30
32
|
await next();
|
|
31
33
|
}
|
|
32
34
|
};
|
|
33
|
-
|
|
34
|
-
// src/index.ts
|
|
35
|
-
import { randomBytes } from "crypto";
|
|
36
35
|
var Func = class {
|
|
36
|
+
/**
|
|
37
|
+
* Create a cloud function
|
|
38
|
+
* @param config {object} config
|
|
39
|
+
* @param config.plugins {Plugin[]} plugins list
|
|
40
|
+
* @param config.handler {Handler} business logic
|
|
41
|
+
*/
|
|
37
42
|
constructor(config) {
|
|
38
43
|
this.handler = config.handler;
|
|
39
44
|
this.plugins = config.plugins || [];
|
|
@@ -94,6 +99,13 @@ var Func = class {
|
|
|
94
99
|
return await dispatch(0);
|
|
95
100
|
};
|
|
96
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Deploy the function
|
|
104
|
+
* @param data {object} data
|
|
105
|
+
* @param data.root {string} root path
|
|
106
|
+
* @param data.filename {string} filename
|
|
107
|
+
* @param data.env {string} environment
|
|
108
|
+
*/
|
|
97
109
|
deploy(data) {
|
|
98
110
|
if (!data.logger)
|
|
99
111
|
data.logger = new Logger("Func");
|
|
@@ -101,6 +113,9 @@ var Func = class {
|
|
|
101
113
|
data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
|
|
102
114
|
return this.compose("onDeploy")(data);
|
|
103
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* First time mount the function
|
|
118
|
+
*/
|
|
104
119
|
async mount(data) {
|
|
105
120
|
if (!data.logger)
|
|
106
121
|
data.logger = new Logger("Func");
|
|
@@ -120,6 +135,10 @@ var Func = class {
|
|
|
120
135
|
data.logger.timeEnd("mount", "mounted");
|
|
121
136
|
}
|
|
122
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Invoke the function
|
|
140
|
+
* @param data {object} data
|
|
141
|
+
*/
|
|
123
142
|
async invoke(data) {
|
|
124
143
|
if (!this.mounted)
|
|
125
144
|
await this.mount({
|
|
@@ -135,6 +154,9 @@ var Func = class {
|
|
|
135
154
|
data.response = error;
|
|
136
155
|
}
|
|
137
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Export the function
|
|
159
|
+
*/
|
|
138
160
|
export() {
|
|
139
161
|
const handler = async (event, context, callback) => {
|
|
140
162
|
if (typeof context === "undefined")
|
|
@@ -142,7 +164,7 @@ var Func = class {
|
|
|
142
164
|
if (!context.request_id)
|
|
143
165
|
context.request_id = randomBytes(16).toString("hex");
|
|
144
166
|
if (!context.request_at)
|
|
145
|
-
context.request_at = Math.round(new Date().getTime() / 1e3);
|
|
167
|
+
context.request_at = Math.round((/* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
146
168
|
context.callbackWaitsForEmptyEventLoop = false;
|
|
147
169
|
const logger = new Logger(context.request_id);
|
|
148
170
|
logger.debug("event: %j", event);
|
|
@@ -192,8 +214,5 @@ function useFunc(handler) {
|
|
|
192
214
|
plugins = [];
|
|
193
215
|
return func;
|
|
194
216
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
useFunc,
|
|
198
|
-
usePlugin
|
|
199
|
-
};
|
|
217
|
+
|
|
218
|
+
export { Func, useFunc, usePlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/func",
|
|
3
|
-
"version": "0.0.3-beta.
|
|
3
|
+
"version": "0.0.3-beta.88",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
},
|
|
16
16
|
"funding": "https://github.com/sponsors/faasjs",
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "tsup-node src/index.ts --
|
|
18
|
+
"build": "tsup-node src/index.ts --config ../../tsup.config.json"
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@faasjs/deep_merge": "^0.0.3-beta.
|
|
25
|
-
"@faasjs/logger": "^0.0.3-beta.
|
|
24
|
+
"@faasjs/deep_merge": "^0.0.3-beta.88",
|
|
25
|
+
"@faasjs/logger": "^0.0.3-beta.88"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
28
|
"npm": ">=8.0.0",
|