@budibase/backend-core 2.13.0 → 2.13.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.js +93 -25
- package/dist/index.js.map +4 -4
- package/dist/index.js.meta.json +1 -1
- package/dist/package.json +4 -4
- package/dist/plugins.js.meta.json +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/queue/inMemoryQueue.d.ts +1 -1
- package/dist/src/queue/inMemoryQueue.js +1 -1
- package/dist/src/queue/inMemoryQueue.js.map +1 -1
- package/dist/src/queue/queue.js +16 -3
- package/dist/src/queue/queue.js.map +1 -1
- package/dist/src/redis/redis.js +2 -4
- package/dist/src/redis/redis.js.map +1 -1
- package/dist/src/redis/utils.d.ts +5 -4
- package/dist/src/redis/utils.js +24 -20
- package/dist/src/redis/utils.js.map +1 -1
- package/dist/src/utils/Duration.d.ts +30 -0
- package/dist/src/utils/Duration.js +48 -0
- package/dist/src/utils/Duration.js.map +1 -0
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +1 -0
- package/src/queue/inMemoryQueue.ts +1 -1
- package/src/queue/queue.ts +17 -4
- package/src/redis/redis.ts +3 -3
- package/src/redis/utils.ts +25 -18
- package/src/utils/Duration.ts +49 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/tests/Duration.spec.ts +19 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/redis/utils.ts"],"names":[],"mappings":";;;;;;AAAA,iEAAgC;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/redis/utils.ts"],"names":[],"mappings":";;;;;;AAAA,iEAAgC;AAGhC,MAAM,eAAe,GAAG,IAAI,CAAA;AAC5B,MAAM,kBAAkB,GAAG,KA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BAAS,GAAG,GAAG,EAAE,CAAA;AAClC,CAAC;AALD,kCAKC;AAED,SAAgB,cAAc,CAAC,GAAW;IACxC,IAAI,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,iBAAS,CAAC,CAAA;IAChC,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,EAAE;QACrB,KAAK,CAAC,KAAK,EAAE,CAAA;QACb,OAAO,KAAK,CAAC,IAAI,CAAC,iBAAS,CAAC,CAAA;KAC7B;SAAM;QACL,uBAAuB;QACvB,OAAO,KAAK,CAAC,CAAC,CAAC,CAAA;KAChB;AACH,CAAC;AATD,wCASC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare enum DurationType {
|
|
2
|
+
MILLISECONDS = "milliseconds",
|
|
3
|
+
SECONDS = "seconds",
|
|
4
|
+
MINUTES = "minutes",
|
|
5
|
+
HOURS = "hours",
|
|
6
|
+
DAYS = "days"
|
|
7
|
+
}
|
|
8
|
+
export declare class Duration {
|
|
9
|
+
static convert(from: DurationType, to: DurationType, duration: number): number;
|
|
10
|
+
static from(from: DurationType, duration: number): {
|
|
11
|
+
to: (to: DurationType) => number;
|
|
12
|
+
toMs: () => number;
|
|
13
|
+
};
|
|
14
|
+
static fromSeconds(duration: number): {
|
|
15
|
+
to: (to: DurationType) => number;
|
|
16
|
+
toMs: () => number;
|
|
17
|
+
};
|
|
18
|
+
static fromMinutes(duration: number): {
|
|
19
|
+
to: (to: DurationType) => number;
|
|
20
|
+
toMs: () => number;
|
|
21
|
+
};
|
|
22
|
+
static fromHours(duration: number): {
|
|
23
|
+
to: (to: DurationType) => number;
|
|
24
|
+
toMs: () => number;
|
|
25
|
+
};
|
|
26
|
+
static fromDays(duration: number): {
|
|
27
|
+
to: (to: DurationType) => number;
|
|
28
|
+
toMs: () => number;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Duration = exports.DurationType = void 0;
|
|
4
|
+
var DurationType;
|
|
5
|
+
(function (DurationType) {
|
|
6
|
+
DurationType["MILLISECONDS"] = "milliseconds";
|
|
7
|
+
DurationType["SECONDS"] = "seconds";
|
|
8
|
+
DurationType["MINUTES"] = "minutes";
|
|
9
|
+
DurationType["HOURS"] = "hours";
|
|
10
|
+
DurationType["DAYS"] = "days";
|
|
11
|
+
})(DurationType || (exports.DurationType = DurationType = {}));
|
|
12
|
+
const conversion = {
|
|
13
|
+
milliseconds: 1,
|
|
14
|
+
seconds: 1000,
|
|
15
|
+
minutes: 60 * 1000,
|
|
16
|
+
hours: 60 * 60 * 1000,
|
|
17
|
+
days: 24 * 60 * 60 * 1000,
|
|
18
|
+
};
|
|
19
|
+
class Duration {
|
|
20
|
+
static convert(from, to, duration) {
|
|
21
|
+
const milliseconds = duration * conversion[from];
|
|
22
|
+
return milliseconds / conversion[to];
|
|
23
|
+
}
|
|
24
|
+
static from(from, duration) {
|
|
25
|
+
return {
|
|
26
|
+
to: (to) => {
|
|
27
|
+
return Duration.convert(from, to, duration);
|
|
28
|
+
},
|
|
29
|
+
toMs: () => {
|
|
30
|
+
return Duration.convert(from, DurationType.MILLISECONDS, duration);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
static fromSeconds(duration) {
|
|
35
|
+
return Duration.from(DurationType.SECONDS, duration);
|
|
36
|
+
}
|
|
37
|
+
static fromMinutes(duration) {
|
|
38
|
+
return Duration.from(DurationType.MINUTES, duration);
|
|
39
|
+
}
|
|
40
|
+
static fromHours(duration) {
|
|
41
|
+
return Duration.from(DurationType.HOURS, duration);
|
|
42
|
+
}
|
|
43
|
+
static fromDays(duration) {
|
|
44
|
+
return Duration.from(DurationType.DAYS, duration);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.Duration = Duration;
|
|
48
|
+
//# sourceMappingURL=Duration.js.map
|
|
@@ -0,0 +1 @@
|
|
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,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;IACtD,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,QAAgB;QAC/B,OAAO,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;IACpD,CAAC;IAED,MAAM,CAAC,QAAQ,CAAC,QAAgB;QAC9B,OAAO,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;IACnD,CAAC;CACF;AAhCD,4BAgCC"}
|
package/dist/src/utils/index.js
CHANGED
|
@@ -17,4 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./hashing"), exports);
|
|
18
18
|
__exportStar(require("./utils"), exports);
|
|
19
19
|
__exportStar(require("./stringUtils"), exports);
|
|
20
|
+
__exportStar(require("./Duration"), exports);
|
|
20
21
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAyB;AACzB,0CAAuB;AACvB,gDAA6B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAAyB;AACzB,0CAAuB;AACvB,gDAA6B;AAC7B,6CAA0B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@budibase/backend-core",
|
|
3
|
-
"version": "2.13.
|
|
3
|
+
"version": "2.13.2",
|
|
4
4
|
"description": "Budibase backend core libraries used in server and worker",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/src/index.d.ts",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@budibase/nano": "10.1.3",
|
|
25
25
|
"@budibase/pouchdb-replication-stream": "1.2.10",
|
|
26
|
-
"@budibase/shared-core": "2.13.
|
|
27
|
-
"@budibase/types": "2.13.
|
|
26
|
+
"@budibase/shared-core": "2.13.2",
|
|
27
|
+
"@budibase/types": "2.13.2",
|
|
28
28
|
"@techpass/passport-openidconnect": "0.3.2",
|
|
29
29
|
"aws-cloudfront-sign": "3.0.2",
|
|
30
30
|
"aws-sdk": "2.1030.0",
|
|
@@ -96,5 +96,5 @@
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
},
|
|
99
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "06a7f67e1f9d52ae3e4cf981468d45d107e3c6de"
|
|
100
100
|
}
|
package/src/index.ts
CHANGED
|
@@ -30,6 +30,7 @@ export * as timers from "./timers"
|
|
|
30
30
|
export { default as env } from "./environment"
|
|
31
31
|
export * as blacklist from "./blacklist"
|
|
32
32
|
export * as docUpdates from "./docUpdates"
|
|
33
|
+
export * from "./utils/Duration"
|
|
33
34
|
export { SearchParams } from "./db"
|
|
34
35
|
// Add context to tenancy for backwards compatibility
|
|
35
36
|
// only do this for external usages to prevent internal
|
|
@@ -36,7 +36,7 @@ class InMemoryQueue {
|
|
|
36
36
|
* @param opts This is not used by the in memory queue as there is no real use
|
|
37
37
|
* case when in memory, but is the same API as Bull
|
|
38
38
|
*/
|
|
39
|
-
constructor(name: string, opts
|
|
39
|
+
constructor(name: string, opts?: any) {
|
|
40
40
|
this._name = name
|
|
41
41
|
this._opts = opts
|
|
42
42
|
this._messages = []
|
package/src/queue/queue.ts
CHANGED
|
@@ -2,11 +2,17 @@ import env from "../environment"
|
|
|
2
2
|
import { getRedisOptions } from "../redis/utils"
|
|
3
3
|
import { JobQueue } from "./constants"
|
|
4
4
|
import InMemoryQueue from "./inMemoryQueue"
|
|
5
|
-
import BullQueue from "bull"
|
|
5
|
+
import BullQueue, { QueueOptions } from "bull"
|
|
6
6
|
import { addListeners, StalledFn } from "./listeners"
|
|
7
|
+
import { Duration } from "../utils"
|
|
7
8
|
import * as timers from "../timers"
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
// the queue lock is held for 5 minutes
|
|
11
|
+
const QUEUE_LOCK_MS = Duration.fromMinutes(5).toMs()
|
|
12
|
+
// queue lock is refreshed every 30 seconds
|
|
13
|
+
const QUEUE_LOCK_RENEW_INTERNAL_MS = Duration.fromSeconds(30).toMs()
|
|
14
|
+
// cleanup the queue every 60 seconds
|
|
15
|
+
const CLEANUP_PERIOD_MS = Duration.fromSeconds(60).toMs()
|
|
10
16
|
let QUEUES: BullQueue.Queue[] | InMemoryQueue[] = []
|
|
11
17
|
let cleanupInterval: NodeJS.Timeout
|
|
12
18
|
|
|
@@ -20,8 +26,15 @@ export function createQueue<T>(
|
|
|
20
26
|
jobQueue: JobQueue,
|
|
21
27
|
opts: { removeStalledCb?: StalledFn } = {}
|
|
22
28
|
): BullQueue.Queue<T> {
|
|
23
|
-
const
|
|
24
|
-
const queueConfig:
|
|
29
|
+
const redisOpts = getRedisOptions()
|
|
30
|
+
const queueConfig: QueueOptions = {
|
|
31
|
+
redis: redisOpts,
|
|
32
|
+
settings: {
|
|
33
|
+
maxStalledCount: 0,
|
|
34
|
+
lockDuration: QUEUE_LOCK_MS,
|
|
35
|
+
lockRenewTime: QUEUE_LOCK_RENEW_INTERNAL_MS,
|
|
36
|
+
},
|
|
37
|
+
}
|
|
25
38
|
let queue: any
|
|
26
39
|
if (!env.isTest()) {
|
|
27
40
|
queue = new BullQueue(jobQueue, queueConfig)
|
package/src/redis/redis.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
getRedisOptions,
|
|
17
17
|
SEPARATOR,
|
|
18
18
|
SelectableDatabase,
|
|
19
|
+
getRedisConnectionDetails,
|
|
19
20
|
} from "./utils"
|
|
20
21
|
import * as timers from "../timers"
|
|
21
22
|
|
|
@@ -91,12 +92,11 @@ function init(selectDb = DEFAULT_SELECT_DB) {
|
|
|
91
92
|
if (client) {
|
|
92
93
|
client.disconnect()
|
|
93
94
|
}
|
|
94
|
-
const {
|
|
95
|
+
const { host, port } = getRedisConnectionDetails()
|
|
96
|
+
const opts = getRedisOptions()
|
|
95
97
|
|
|
96
98
|
if (CLUSTERED) {
|
|
97
99
|
client = new RedisCore.Cluster([{ host, port }], opts)
|
|
98
|
-
} else if (redisProtocolUrl) {
|
|
99
|
-
client = new RedisCore(redisProtocolUrl)
|
|
100
100
|
} else {
|
|
101
101
|
client = new RedisCore(opts)
|
|
102
102
|
}
|
package/src/redis/utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import env from "../environment"
|
|
2
|
+
import * as Redis from "ioredis"
|
|
2
3
|
|
|
3
4
|
const SLOT_REFRESH_MS = 2000
|
|
4
5
|
const CONNECT_TIMEOUT_MS = 10000
|
|
@@ -42,7 +43,7 @@ export enum Databases {
|
|
|
42
43
|
export enum SelectableDatabase {
|
|
43
44
|
DEFAULT = 0,
|
|
44
45
|
SOCKET_IO = 1,
|
|
45
|
-
|
|
46
|
+
RATE_LIMITING = 2,
|
|
46
47
|
UNUSED_2 = 3,
|
|
47
48
|
UNUSED_3 = 4,
|
|
48
49
|
UNUSED_4 = 5,
|
|
@@ -58,7 +59,7 @@ export enum SelectableDatabase {
|
|
|
58
59
|
UNUSED_14 = 15,
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
export function
|
|
62
|
+
export function getRedisConnectionDetails() {
|
|
62
63
|
let password = env.REDIS_PASSWORD
|
|
63
64
|
let url: string[] | string = env.REDIS_URL.split("//")
|
|
64
65
|
// get rid of the protocol
|
|
@@ -74,28 +75,34 @@ export function getRedisOptions() {
|
|
|
74
75
|
}
|
|
75
76
|
const [host, port] = url.split(":")
|
|
76
77
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
redisProtocolUrl = env.REDIS_URL
|
|
78
|
+
return {
|
|
79
|
+
host,
|
|
80
|
+
password,
|
|
81
|
+
port: parseInt(port),
|
|
82
82
|
}
|
|
83
|
+
}
|
|
83
84
|
|
|
84
|
-
|
|
85
|
+
export function getRedisOptions() {
|
|
86
|
+
const { host, password, port } = getRedisConnectionDetails()
|
|
87
|
+
let redisOpts: Redis.RedisOptions = {
|
|
85
88
|
connectTimeout: CONNECT_TIMEOUT_MS,
|
|
89
|
+
port: port,
|
|
90
|
+
host,
|
|
91
|
+
password,
|
|
86
92
|
}
|
|
93
|
+
let opts: Redis.ClusterOptions | Redis.RedisOptions = redisOpts
|
|
87
94
|
if (env.REDIS_CLUSTERED) {
|
|
88
|
-
opts
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
opts = {
|
|
96
|
+
connectTimeout: CONNECT_TIMEOUT_MS,
|
|
97
|
+
redisOptions: {
|
|
98
|
+
...redisOpts,
|
|
99
|
+
tls: {},
|
|
100
|
+
},
|
|
101
|
+
slotsRefreshTimeout: SLOT_REFRESH_MS,
|
|
102
|
+
dnsLookup: (address: string, callback: any) => callback(null, address),
|
|
103
|
+
} as Redis.ClusterOptions
|
|
97
104
|
}
|
|
98
|
-
return
|
|
105
|
+
return opts
|
|
99
106
|
}
|
|
100
107
|
|
|
101
108
|
export function addDbPrefix(db: string, key: string) {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export enum DurationType {
|
|
2
|
+
MILLISECONDS = "milliseconds",
|
|
3
|
+
SECONDS = "seconds",
|
|
4
|
+
MINUTES = "minutes",
|
|
5
|
+
HOURS = "hours",
|
|
6
|
+
DAYS = "days",
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const conversion: Record<DurationType, number> = {
|
|
10
|
+
milliseconds: 1,
|
|
11
|
+
seconds: 1000,
|
|
12
|
+
minutes: 60 * 1000,
|
|
13
|
+
hours: 60 * 60 * 1000,
|
|
14
|
+
days: 24 * 60 * 60 * 1000,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class Duration {
|
|
18
|
+
static convert(from: DurationType, to: DurationType, duration: number) {
|
|
19
|
+
const milliseconds = duration * conversion[from]
|
|
20
|
+
return milliseconds / conversion[to]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static from(from: DurationType, duration: number) {
|
|
24
|
+
return {
|
|
25
|
+
to: (to: DurationType) => {
|
|
26
|
+
return Duration.convert(from, to, duration)
|
|
27
|
+
},
|
|
28
|
+
toMs: () => {
|
|
29
|
+
return Duration.convert(from, DurationType.MILLISECONDS, duration)
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static fromSeconds(duration: number) {
|
|
35
|
+
return Duration.from(DurationType.SECONDS, duration)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static fromMinutes(duration: number) {
|
|
39
|
+
return Duration.from(DurationType.MINUTES, duration)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static fromHours(duration: number) {
|
|
43
|
+
return Duration.from(DurationType.HOURS, duration)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static fromDays(duration: number) {
|
|
47
|
+
return Duration.from(DurationType.DAYS, duration)
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Duration, DurationType } from "../Duration"
|
|
2
|
+
|
|
3
|
+
describe("duration", () => {
|
|
4
|
+
it("should convert minutes to milliseconds", () => {
|
|
5
|
+
expect(Duration.fromMinutes(5).toMs()).toBe(300000)
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
it("should convert seconds to milliseconds", () => {
|
|
9
|
+
expect(Duration.fromSeconds(30).toMs()).toBe(30000)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it("should convert days to milliseconds", () => {
|
|
13
|
+
expect(Duration.fromDays(1).toMs()).toBe(86400000)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it("should convert minutes to days", () => {
|
|
17
|
+
expect(Duration.fromMinutes(1440).to(DurationType.DAYS)).toBe(1)
|
|
18
|
+
})
|
|
19
|
+
})
|