@faasjs/func 0.0.2-beta.398 → 0.0.2-beta.401

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.ts CHANGED
@@ -83,7 +83,6 @@ declare class Func<TEvent = any, TContext = any, TResult = any> {
83
83
  [key: string]: any;
84
84
  plugins: Plugin[];
85
85
  handler?: Handler<TEvent, TContext, TResult>;
86
- logger: Logger;
87
86
  config: Config;
88
87
  mounted: boolean;
89
88
  filename?: string;
@@ -111,6 +110,7 @@ declare class Func<TEvent = any, TContext = any, TResult = any> {
111
110
  event: TEvent;
112
111
  context: TContext;
113
112
  config?: Config;
113
+ logger?: Logger;
114
114
  }): Promise<void>;
115
115
  /**
116
116
  * Invoke the function
package/dist/index.js CHANGED
@@ -58,10 +58,9 @@ var RunHandler = class {
58
58
  };
59
59
 
60
60
  // src/index.ts
61
- var startedAt = Date.now();
61
+ var import_crypto = require("crypto");
62
62
  var Func = class {
63
63
  constructor(config) {
64
- this.logger = new import_logger.Logger("Func");
65
64
  this.handler = config.handler;
66
65
  this.plugins = config.plugins || [];
67
66
  this.plugins.push(new RunHandler());
@@ -74,11 +73,10 @@ var Func = class {
74
73
  try {
75
74
  this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
76
75
  } catch (error) {
77
- this.logger.debug(error.message);
76
+ new import_logger.Logger("Func").warn(error.message);
78
77
  }
79
78
  }
80
79
  compose(key) {
81
- const logger = new import_logger.Logger(key);
82
80
  let list = [];
83
81
  if (this.cachedFunctions[key])
84
82
  list = this.cachedFunctions[key];
@@ -95,6 +93,7 @@ var Func = class {
95
93
  }
96
94
  return async function(data, next) {
97
95
  let index = -1;
96
+ const logger = (data == null ? void 0 : data.logger) || new import_logger.Logger();
98
97
  const dispatch = async function(i) {
99
98
  if (i <= index)
100
99
  return Promise.reject(Error("next() called multiple times"));
@@ -106,15 +105,15 @@ var Func = class {
106
105
  return Promise.resolve();
107
106
  if (typeof fn.key === "undefined")
108
107
  fn.key = `UnNamedPlugin#${i}`;
109
- logger.debug(`[${fn.key}] begin`);
108
+ logger.debug("[%s][%s] begin", fn.key, key);
110
109
  logger.time(fn.key);
111
110
  try {
112
111
  const res = await Promise.resolve(fn.handler(data, dispatch.bind(null, i + 1)));
113
- logger.timeEnd(fn.key, `[${fn.key}] end`);
112
+ logger.timeEnd(fn.key, "[%s][%s] end", fn.key, key);
114
113
  return res;
115
114
  } catch (err) {
116
- logger.timeEnd(fn.key, `[${fn.key}] failed`);
117
- console.error(err);
115
+ logger.timeEnd(fn.key, "[%s][%s] failed", fn.key, key);
116
+ logger.error(err);
118
117
  return Promise.reject(err);
119
118
  }
120
119
  };
@@ -122,24 +121,29 @@ var Func = class {
122
121
  };
123
122
  }
124
123
  deploy(data) {
125
- this.logger.debug("onDeploy");
126
- this.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
124
+ if (!data.logger)
125
+ data.logger = new import_logger.Logger("Func");
126
+ data.logger.debug("onDeploy");
127
+ data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
127
128
  return this.compose("onDeploy")(data);
128
129
  }
129
130
  async mount(data) {
130
- this.logger.debug("onMount");
131
+ if (!data.logger)
132
+ data.logger = new import_logger.Logger("Func");
133
+ data.logger.debug("onMount");
131
134
  if (this.mounted) {
132
- this.logger.warn("mount() has been called, skipped.");
135
+ data.logger.warn("mount() has been called, skipped.");
133
136
  return;
134
137
  }
135
- data.config = this.config;
138
+ if (!data.config)
139
+ data.config = this.config;
136
140
  try {
137
- this.logger.time("mount");
138
- this.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
141
+ data.logger.time("mount");
142
+ data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
139
143
  await this.compose("onMount")(data);
140
144
  this.mounted = true;
141
145
  } finally {
142
- this.logger.timeEnd("mount", "mounted");
146
+ data.logger.timeEnd("mount", "mounted");
143
147
  }
144
148
  }
145
149
  async invoke(data) {
@@ -147,31 +151,28 @@ var Func = class {
147
151
  await this.mount({
148
152
  event: data.event,
149
153
  context: data.context,
150
- config: data.config
154
+ config: data.config,
155
+ logger: data.logger
151
156
  });
152
157
  try {
153
158
  await this.compose("onInvoke")(data);
154
159
  } catch (error) {
155
- this.logger.error(error);
160
+ data.logger.error(error);
156
161
  data.response = error;
157
162
  }
158
163
  }
159
164
  export() {
160
165
  const handler = async (event, context, callback) => {
161
- const logger = new import_logger.Logger();
162
- if (startedAt) {
163
- logger.debug(`Container started +${Date.now() - startedAt}ms`);
164
- startedAt = 0;
165
- }
166
- logger.debug("event: %j", event);
167
- logger.debug("context: %j", context);
168
166
  if (typeof context === "undefined")
169
167
  context = {};
170
168
  if (!context.request_id)
171
- context.request_id = new Date().getTime().toString();
169
+ context.request_id = (0, import_crypto.randomBytes)(16).toString("hex");
172
170
  if (!context.request_at)
173
171
  context.request_at = Math.round(new Date().getTime() / 1e3);
174
172
  context.callbackWaitsForEmptyEventLoop = false;
173
+ const logger = new import_logger.Logger(context.request_id);
174
+ logger.debug("event: %j", event);
175
+ logger.debug("context: %j", context);
175
176
  const data = {
176
177
  event,
177
178
  context,
package/dist/index.mjs CHANGED
@@ -32,10 +32,9 @@ var RunHandler = class {
32
32
  };
33
33
 
34
34
  // src/index.ts
35
- var startedAt = Date.now();
35
+ import { randomBytes } from "crypto";
36
36
  var Func = class {
37
37
  constructor(config) {
38
- this.logger = new Logger("Func");
39
38
  this.handler = config.handler;
40
39
  this.plugins = config.plugins || [];
41
40
  this.plugins.push(new RunHandler());
@@ -48,11 +47,10 @@ var Func = class {
48
47
  try {
49
48
  this.filename = new Error().stack.split("\n").find((s) => /[^/]\.func\.ts/.test(s)).match(/\((.*\.func\.ts).*\)/)[1];
50
49
  } catch (error) {
51
- this.logger.debug(error.message);
50
+ new Logger("Func").warn(error.message);
52
51
  }
53
52
  }
54
53
  compose(key) {
55
- const logger = new Logger(key);
56
54
  let list = [];
57
55
  if (this.cachedFunctions[key])
58
56
  list = this.cachedFunctions[key];
@@ -69,6 +67,7 @@ var Func = class {
69
67
  }
70
68
  return async function(data, next) {
71
69
  let index = -1;
70
+ const logger = (data == null ? void 0 : data.logger) || new Logger();
72
71
  const dispatch = async function(i) {
73
72
  if (i <= index)
74
73
  return Promise.reject(Error("next() called multiple times"));
@@ -80,15 +79,15 @@ var Func = class {
80
79
  return Promise.resolve();
81
80
  if (typeof fn.key === "undefined")
82
81
  fn.key = `UnNamedPlugin#${i}`;
83
- logger.debug(`[${fn.key}] begin`);
82
+ logger.debug("[%s][%s] begin", fn.key, key);
84
83
  logger.time(fn.key);
85
84
  try {
86
85
  const res = await Promise.resolve(fn.handler(data, dispatch.bind(null, i + 1)));
87
- logger.timeEnd(fn.key, `[${fn.key}] end`);
86
+ logger.timeEnd(fn.key, "[%s][%s] end", fn.key, key);
88
87
  return res;
89
88
  } catch (err) {
90
- logger.timeEnd(fn.key, `[${fn.key}] failed`);
91
- console.error(err);
89
+ logger.timeEnd(fn.key, "[%s][%s] failed", fn.key, key);
90
+ logger.error(err);
92
91
  return Promise.reject(err);
93
92
  }
94
93
  };
@@ -96,24 +95,29 @@ var Func = class {
96
95
  };
97
96
  }
98
97
  deploy(data) {
99
- this.logger.debug("onDeploy");
100
- this.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
98
+ if (!data.logger)
99
+ data.logger = new Logger("Func");
100
+ data.logger.debug("onDeploy");
101
+ data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
101
102
  return this.compose("onDeploy")(data);
102
103
  }
103
104
  async mount(data) {
104
- this.logger.debug("onMount");
105
+ if (!data.logger)
106
+ data.logger = new Logger("Func");
107
+ data.logger.debug("onMount");
105
108
  if (this.mounted) {
106
- this.logger.warn("mount() has been called, skipped.");
109
+ data.logger.warn("mount() has been called, skipped.");
107
110
  return;
108
111
  }
109
- data.config = this.config;
112
+ if (!data.config)
113
+ data.config = this.config;
110
114
  try {
111
- this.logger.time("mount");
112
- this.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
115
+ data.logger.time("mount");
116
+ data.logger.debug("Plugins: " + this.plugins.map((p) => `${p.type}#${p.name}`).join(","));
113
117
  await this.compose("onMount")(data);
114
118
  this.mounted = true;
115
119
  } finally {
116
- this.logger.timeEnd("mount", "mounted");
120
+ data.logger.timeEnd("mount", "mounted");
117
121
  }
118
122
  }
119
123
  async invoke(data) {
@@ -121,31 +125,28 @@ var Func = class {
121
125
  await this.mount({
122
126
  event: data.event,
123
127
  context: data.context,
124
- config: data.config
128
+ config: data.config,
129
+ logger: data.logger
125
130
  });
126
131
  try {
127
132
  await this.compose("onInvoke")(data);
128
133
  } catch (error) {
129
- this.logger.error(error);
134
+ data.logger.error(error);
130
135
  data.response = error;
131
136
  }
132
137
  }
133
138
  export() {
134
139
  const handler = async (event, context, callback) => {
135
- const logger = new Logger();
136
- if (startedAt) {
137
- logger.debug(`Container started +${Date.now() - startedAt}ms`);
138
- startedAt = 0;
139
- }
140
- logger.debug("event: %j", event);
141
- logger.debug("context: %j", context);
142
140
  if (typeof context === "undefined")
143
141
  context = {};
144
142
  if (!context.request_id)
145
- context.request_id = new Date().getTime().toString();
143
+ context.request_id = randomBytes(16).toString("hex");
146
144
  if (!context.request_at)
147
145
  context.request_at = Math.round(new Date().getTime() / 1e3);
148
146
  context.callbackWaitsForEmptyEventLoop = false;
147
+ const logger = new Logger(context.request_id);
148
+ logger.debug("event: %j", event);
149
+ logger.debug("context: %j", context);
149
150
  const data = {
150
151
  event,
151
152
  context,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/func",
3
- "version": "0.0.2-beta.398",
3
+ "version": "0.0.2-beta.401",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,14 +22,15 @@
22
22
  "dist"
23
23
  ],
24
24
  "peerDependencies": {
25
- "@faasjs/deep_merge": "^0.0.2-beta.398",
26
- "@faasjs/logger": "^0.0.2-beta.398"
25
+ "@faasjs/deep_merge": "^0.0.2-beta.401",
26
+ "@faasjs/logger": "^0.0.2-beta.401"
27
27
  },
28
28
  "devDependencies": {
29
29
  "tsup": "*",
30
30
  "typescript": "*"
31
31
  },
32
32
  "engines": {
33
- "npm": ">=8.0.0"
33
+ "npm": ">=8.0.0",
34
+ "node": ">=16.0.0"
34
35
  }
35
36
  }