@faasjs/http 2.9.0 → 3.0.0-canary.2
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 +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +41 -47
- package/dist/index.mjs +41 -47
- package/package.json +7 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Plugin,
|
|
1
|
+
import { Plugin, MountData, Next, InvokeData, UseifyPlugin } from '@faasjs/func';
|
|
2
2
|
import { Logger } from '@faasjs/logger';
|
|
3
3
|
|
|
4
4
|
type SessionOptions = {
|
|
@@ -201,7 +201,6 @@ declare class Http<TParams extends Record<string, any> = any, TCookie extends Re
|
|
|
201
201
|
private response?;
|
|
202
202
|
private validator?;
|
|
203
203
|
constructor(config?: HttpConfig<TParams, TCookie, TSession>);
|
|
204
|
-
onDeploy(data: DeployData, next: Next): Promise<void>;
|
|
205
204
|
onMount(data: MountData, next: Next): Promise<void>;
|
|
206
205
|
onInvoke(data: InvokeData, next: Next): Promise<void>;
|
|
207
206
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Plugin,
|
|
1
|
+
import { Plugin, MountData, Next, InvokeData, UseifyPlugin } from '@faasjs/func';
|
|
2
2
|
import { Logger } from '@faasjs/logger';
|
|
3
3
|
|
|
4
4
|
type SessionOptions = {
|
|
@@ -201,7 +201,6 @@ declare class Http<TParams extends Record<string, any> = any, TCookie extends Re
|
|
|
201
201
|
private response?;
|
|
202
202
|
private validator?;
|
|
203
203
|
constructor(config?: HttpConfig<TParams, TCookie, TSession>);
|
|
204
|
-
onDeploy(data: DeployData, next: Next): Promise<void>;
|
|
205
204
|
onMount(data: MountData, next: Next): Promise<void>;
|
|
206
205
|
onInvoke(data: InvokeData, next: Next): Promise<void>;
|
|
207
206
|
/**
|
package/dist/index.js
CHANGED
|
@@ -6,16 +6,17 @@ var logger = require('@faasjs/logger');
|
|
|
6
6
|
var crypto = require('crypto');
|
|
7
7
|
var zlib = require('zlib');
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
11
|
-
}) : x)(function(x) {
|
|
12
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
13
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
14
|
-
});
|
|
9
|
+
// src/index.ts
|
|
15
10
|
var Session = class {
|
|
11
|
+
content;
|
|
12
|
+
config;
|
|
13
|
+
secret;
|
|
14
|
+
signedSecret;
|
|
15
|
+
cookie;
|
|
16
|
+
changed;
|
|
16
17
|
constructor(cookie, config) {
|
|
17
18
|
this.cookie = cookie;
|
|
18
|
-
if (!
|
|
19
|
+
if (!config?.secret) cookie.logger.warn("Session's secret is missing.");
|
|
19
20
|
this.config = Object.assign(
|
|
20
21
|
{
|
|
21
22
|
key: "key",
|
|
@@ -49,7 +50,7 @@ var Session = class {
|
|
|
49
50
|
try {
|
|
50
51
|
this.content = cookie ? this.decode(cookie) : /* @__PURE__ */ Object.create(null);
|
|
51
52
|
} catch (error) {
|
|
52
|
-
logger
|
|
53
|
+
logger?.error(error);
|
|
53
54
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
54
55
|
}
|
|
55
56
|
this.changed = false;
|
|
@@ -108,6 +109,11 @@ var Session = class {
|
|
|
108
109
|
}
|
|
109
110
|
};
|
|
110
111
|
var Cookie = class {
|
|
112
|
+
session;
|
|
113
|
+
content;
|
|
114
|
+
config;
|
|
115
|
+
logger;
|
|
116
|
+
setCookie;
|
|
111
117
|
constructor(config, logger) {
|
|
112
118
|
this.logger = logger;
|
|
113
119
|
this.config = deep_merge.deepMerge(
|
|
@@ -172,6 +178,11 @@ var Cookie = class {
|
|
|
172
178
|
|
|
173
179
|
// src/validator.ts
|
|
174
180
|
var Validator = class {
|
|
181
|
+
before;
|
|
182
|
+
paramsConfig;
|
|
183
|
+
cookieConfig;
|
|
184
|
+
sessionConfig;
|
|
185
|
+
request;
|
|
175
186
|
constructor(config) {
|
|
176
187
|
this.paramsConfig = config.params;
|
|
177
188
|
this.cookieConfig = config.cookie;
|
|
@@ -352,6 +363,8 @@ var ContentType = {
|
|
|
352
363
|
jsonp: "application/javascript"
|
|
353
364
|
};
|
|
354
365
|
var HttpError = class _HttpError extends Error {
|
|
366
|
+
statusCode;
|
|
367
|
+
message;
|
|
355
368
|
constructor({
|
|
356
369
|
statusCode,
|
|
357
370
|
message
|
|
@@ -378,45 +391,27 @@ function deepClone(obj) {
|
|
|
378
391
|
return clone;
|
|
379
392
|
}
|
|
380
393
|
var Http = class {
|
|
394
|
+
type = Name;
|
|
395
|
+
name = Name;
|
|
396
|
+
headers;
|
|
397
|
+
body;
|
|
398
|
+
params;
|
|
399
|
+
cookie;
|
|
400
|
+
session;
|
|
401
|
+
config;
|
|
402
|
+
validatorOptions;
|
|
403
|
+
response;
|
|
404
|
+
validator;
|
|
381
405
|
constructor(config) {
|
|
382
|
-
this.
|
|
383
|
-
this.
|
|
384
|
-
|
|
385
|
-
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
386
|
-
if (config == null ? void 0 : config.validator) {
|
|
406
|
+
this.name = config?.name || this.type;
|
|
407
|
+
this.config = config?.config || /* @__PURE__ */ Object.create(null);
|
|
408
|
+
if (config?.validator) {
|
|
387
409
|
console.warn("Validator will deprecated in the v3.");
|
|
388
410
|
this.validatorOptions = config.validator;
|
|
389
411
|
}
|
|
390
412
|
}
|
|
391
|
-
async onDeploy(data, next) {
|
|
392
|
-
var _a;
|
|
393
|
-
data.dependencies["@faasjs/http"] = "*";
|
|
394
|
-
await next();
|
|
395
|
-
const logger$1 = new logger.Logger(this.name);
|
|
396
|
-
logger$1.debug("Generate api gateway's config");
|
|
397
|
-
logger$1.debug("%j", data);
|
|
398
|
-
const config = data.config.plugins ? deep_merge.deepMerge(data.config.plugins[this.name || this.type], {
|
|
399
|
-
config: this.config
|
|
400
|
-
}) : { config: this.config };
|
|
401
|
-
if (!config.config.path) {
|
|
402
|
-
config.config.path = `/${(_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, "")}`;
|
|
403
|
-
if (config.config.path === "/index") config.config.path = "/";
|
|
404
|
-
if (config.config.ignorePathPrefix) {
|
|
405
|
-
config.config.path = config.config.path.replace(
|
|
406
|
-
new RegExp(`^${config.config.ignorePathPrefix}`),
|
|
407
|
-
""
|
|
408
|
-
);
|
|
409
|
-
if (config.config.path === "") config.config.path = "/";
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
logger$1.debug("Api gateway's config: %j", config);
|
|
413
|
-
const Provider = __require(config.provider.type).Provider;
|
|
414
|
-
const provider = new Provider(config.provider.config);
|
|
415
|
-
await provider.deploy(this.type, data, config);
|
|
416
|
-
}
|
|
417
413
|
async onMount(data, next) {
|
|
418
|
-
|
|
419
|
-
const logger$1 = new logger.Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
|
|
414
|
+
const logger$1 = new logger.Logger(data.logger?.label || this.name);
|
|
420
415
|
if (!logger$1.label.endsWith(this.name))
|
|
421
416
|
logger$1.label = `${logger$1.label}] [${this.name}`;
|
|
422
417
|
logger$1.debug("[onMount] merge config");
|
|
@@ -436,7 +431,7 @@ var Http = class {
|
|
|
436
431
|
} else this.config[key] = value;
|
|
437
432
|
}
|
|
438
433
|
if (!data.config) throw Error(`[${this.name}] Config not found.`);
|
|
439
|
-
if (
|
|
434
|
+
if (data.config.plugins?.[this.name || this.type])
|
|
440
435
|
this.config = deep_merge.deepMerge(
|
|
441
436
|
this.config,
|
|
442
437
|
data.config.plugins[this.name || this.type].config
|
|
@@ -453,16 +448,15 @@ var Http = class {
|
|
|
453
448
|
await next();
|
|
454
449
|
}
|
|
455
450
|
async onInvoke(data, next) {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
if (!((_b = logger$1.label) == null ? void 0 : _b.endsWith(this.name)))
|
|
451
|
+
const logger$1 = new logger.Logger(data.logger?.label || this.name);
|
|
452
|
+
if (!logger$1.label?.endsWith(this.name))
|
|
459
453
|
logger$1.label = `${logger$1.label}] [${this.name}`;
|
|
460
454
|
this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
|
|
461
455
|
this.body = data.event.body;
|
|
462
456
|
this.params = data.event.queryString || /* @__PURE__ */ Object.create(null);
|
|
463
457
|
this.response = { headers: /* @__PURE__ */ Object.create(null) };
|
|
464
458
|
if (data.event.body) {
|
|
465
|
-
if (
|
|
459
|
+
if (this.headers["content-type"]?.includes("application/json") && typeof data.event.body === "string" && data.event.body.length > 1) {
|
|
466
460
|
logger$1.debug("[onInvoke] Parse params from json body");
|
|
467
461
|
try {
|
|
468
462
|
this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
|
|
@@ -509,7 +503,7 @@ var Http = class {
|
|
|
509
503
|
}
|
|
510
504
|
this.session.update();
|
|
511
505
|
if (data.response)
|
|
512
|
-
if (data.response instanceof Error ||
|
|
506
|
+
if (data.response instanceof Error || data.response.constructor?.name === "Error") {
|
|
513
507
|
logger$1.error(data.response);
|
|
514
508
|
this.response.body = JSON.stringify({
|
|
515
509
|
error: { message: data.response.message }
|
package/dist/index.mjs
CHANGED
|
@@ -4,16 +4,17 @@ import { Logger } from '@faasjs/logger';
|
|
|
4
4
|
import { randomBytes, pbkdf2Sync, createCipheriv, createHmac, createDecipheriv } from 'node:crypto';
|
|
5
5
|
import { brotliCompressSync, gzipSync, deflateSync } from 'node:zlib';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
-
}) : x)(function(x) {
|
|
10
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
-
});
|
|
7
|
+
// src/index.ts
|
|
13
8
|
var Session = class {
|
|
9
|
+
content;
|
|
10
|
+
config;
|
|
11
|
+
secret;
|
|
12
|
+
signedSecret;
|
|
13
|
+
cookie;
|
|
14
|
+
changed;
|
|
14
15
|
constructor(cookie, config) {
|
|
15
16
|
this.cookie = cookie;
|
|
16
|
-
if (!
|
|
17
|
+
if (!config?.secret) cookie.logger.warn("Session's secret is missing.");
|
|
17
18
|
this.config = Object.assign(
|
|
18
19
|
{
|
|
19
20
|
key: "key",
|
|
@@ -47,7 +48,7 @@ var Session = class {
|
|
|
47
48
|
try {
|
|
48
49
|
this.content = cookie ? this.decode(cookie) : /* @__PURE__ */ Object.create(null);
|
|
49
50
|
} catch (error) {
|
|
50
|
-
logger
|
|
51
|
+
logger?.error(error);
|
|
51
52
|
this.content = /* @__PURE__ */ Object.create(null);
|
|
52
53
|
}
|
|
53
54
|
this.changed = false;
|
|
@@ -106,6 +107,11 @@ var Session = class {
|
|
|
106
107
|
}
|
|
107
108
|
};
|
|
108
109
|
var Cookie = class {
|
|
110
|
+
session;
|
|
111
|
+
content;
|
|
112
|
+
config;
|
|
113
|
+
logger;
|
|
114
|
+
setCookie;
|
|
109
115
|
constructor(config, logger) {
|
|
110
116
|
this.logger = logger;
|
|
111
117
|
this.config = deepMerge(
|
|
@@ -170,6 +176,11 @@ var Cookie = class {
|
|
|
170
176
|
|
|
171
177
|
// src/validator.ts
|
|
172
178
|
var Validator = class {
|
|
179
|
+
before;
|
|
180
|
+
paramsConfig;
|
|
181
|
+
cookieConfig;
|
|
182
|
+
sessionConfig;
|
|
183
|
+
request;
|
|
173
184
|
constructor(config) {
|
|
174
185
|
this.paramsConfig = config.params;
|
|
175
186
|
this.cookieConfig = config.cookie;
|
|
@@ -350,6 +361,8 @@ var ContentType = {
|
|
|
350
361
|
jsonp: "application/javascript"
|
|
351
362
|
};
|
|
352
363
|
var HttpError = class _HttpError extends Error {
|
|
364
|
+
statusCode;
|
|
365
|
+
message;
|
|
353
366
|
constructor({
|
|
354
367
|
statusCode,
|
|
355
368
|
message
|
|
@@ -376,45 +389,27 @@ function deepClone(obj) {
|
|
|
376
389
|
return clone;
|
|
377
390
|
}
|
|
378
391
|
var Http = class {
|
|
392
|
+
type = Name;
|
|
393
|
+
name = Name;
|
|
394
|
+
headers;
|
|
395
|
+
body;
|
|
396
|
+
params;
|
|
397
|
+
cookie;
|
|
398
|
+
session;
|
|
399
|
+
config;
|
|
400
|
+
validatorOptions;
|
|
401
|
+
response;
|
|
402
|
+
validator;
|
|
379
403
|
constructor(config) {
|
|
380
|
-
this.
|
|
381
|
-
this.
|
|
382
|
-
|
|
383
|
-
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
384
|
-
if (config == null ? void 0 : config.validator) {
|
|
404
|
+
this.name = config?.name || this.type;
|
|
405
|
+
this.config = config?.config || /* @__PURE__ */ Object.create(null);
|
|
406
|
+
if (config?.validator) {
|
|
385
407
|
console.warn("Validator will deprecated in the v3.");
|
|
386
408
|
this.validatorOptions = config.validator;
|
|
387
409
|
}
|
|
388
410
|
}
|
|
389
|
-
async onDeploy(data, next) {
|
|
390
|
-
var _a;
|
|
391
|
-
data.dependencies["@faasjs/http"] = "*";
|
|
392
|
-
await next();
|
|
393
|
-
const logger = new Logger(this.name);
|
|
394
|
-
logger.debug("Generate api gateway's config");
|
|
395
|
-
logger.debug("%j", data);
|
|
396
|
-
const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], {
|
|
397
|
-
config: this.config
|
|
398
|
-
}) : { config: this.config };
|
|
399
|
-
if (!config.config.path) {
|
|
400
|
-
config.config.path = `/${(_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, "")}`;
|
|
401
|
-
if (config.config.path === "/index") config.config.path = "/";
|
|
402
|
-
if (config.config.ignorePathPrefix) {
|
|
403
|
-
config.config.path = config.config.path.replace(
|
|
404
|
-
new RegExp(`^${config.config.ignorePathPrefix}`),
|
|
405
|
-
""
|
|
406
|
-
);
|
|
407
|
-
if (config.config.path === "") config.config.path = "/";
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
logger.debug("Api gateway's config: %j", config);
|
|
411
|
-
const Provider = __require(config.provider.type).Provider;
|
|
412
|
-
const provider = new Provider(config.provider.config);
|
|
413
|
-
await provider.deploy(this.type, data, config);
|
|
414
|
-
}
|
|
415
411
|
async onMount(data, next) {
|
|
416
|
-
|
|
417
|
-
const logger = new Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
|
|
412
|
+
const logger = new Logger(data.logger?.label || this.name);
|
|
418
413
|
if (!logger.label.endsWith(this.name))
|
|
419
414
|
logger.label = `${logger.label}] [${this.name}`;
|
|
420
415
|
logger.debug("[onMount] merge config");
|
|
@@ -434,7 +429,7 @@ var Http = class {
|
|
|
434
429
|
} else this.config[key] = value;
|
|
435
430
|
}
|
|
436
431
|
if (!data.config) throw Error(`[${this.name}] Config not found.`);
|
|
437
|
-
if (
|
|
432
|
+
if (data.config.plugins?.[this.name || this.type])
|
|
438
433
|
this.config = deepMerge(
|
|
439
434
|
this.config,
|
|
440
435
|
data.config.plugins[this.name || this.type].config
|
|
@@ -451,16 +446,15 @@ var Http = class {
|
|
|
451
446
|
await next();
|
|
452
447
|
}
|
|
453
448
|
async onInvoke(data, next) {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
if (!((_b = logger.label) == null ? void 0 : _b.endsWith(this.name)))
|
|
449
|
+
const logger = new Logger(data.logger?.label || this.name);
|
|
450
|
+
if (!logger.label?.endsWith(this.name))
|
|
457
451
|
logger.label = `${logger.label}] [${this.name}`;
|
|
458
452
|
this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
|
|
459
453
|
this.body = data.event.body;
|
|
460
454
|
this.params = data.event.queryString || /* @__PURE__ */ Object.create(null);
|
|
461
455
|
this.response = { headers: /* @__PURE__ */ Object.create(null) };
|
|
462
456
|
if (data.event.body) {
|
|
463
|
-
if (
|
|
457
|
+
if (this.headers["content-type"]?.includes("application/json") && typeof data.event.body === "string" && data.event.body.length > 1) {
|
|
464
458
|
logger.debug("[onInvoke] Parse params from json body");
|
|
465
459
|
try {
|
|
466
460
|
this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
|
|
@@ -507,7 +501,7 @@ var Http = class {
|
|
|
507
501
|
}
|
|
508
502
|
this.session.update();
|
|
509
503
|
if (data.response)
|
|
510
|
-
if (data.response instanceof Error ||
|
|
504
|
+
if (data.response instanceof Error || data.response.constructor?.name === "Error") {
|
|
511
505
|
logger.error(data.response);
|
|
512
506
|
this.response.body = JSON.stringify({
|
|
513
507
|
error: { message: data.response.message }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/http",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-canary.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -34,15 +34,15 @@
|
|
|
34
34
|
"dist"
|
|
35
35
|
],
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@faasjs/func": "
|
|
38
|
-
"@faasjs/logger": "
|
|
37
|
+
"@faasjs/func": "3.0.0-canary.2",
|
|
38
|
+
"@faasjs/logger": "3.0.0-canary.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@faasjs/func": "
|
|
42
|
-
"@faasjs/logger": "
|
|
41
|
+
"@faasjs/func": "3.0.0-canary.2",
|
|
42
|
+
"@faasjs/logger": "3.0.0-canary.2"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
|
-
"
|
|
46
|
-
"
|
|
45
|
+
"node": ">=22.0.0",
|
|
46
|
+
"npm": ">=10.0.0"
|
|
47
47
|
}
|
|
48
48
|
}
|