2022-11-22 14:56:01 +01:00
|
|
|
import env from "../environment"
|
2022-12-15 12:35:22 +01:00
|
|
|
import { context } from "@budibase/backend-core"
|
2022-11-22 14:56:01 +01:00
|
|
|
import { generateMetadataID } from "../db/utils"
|
|
|
|
import { Document } from "@budibase/types"
|
|
|
|
import stream from "stream"
|
|
|
|
const Readable = stream.Readable
|
|
|
|
|
|
|
|
export function wait(ms: number) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
|
|
}
|
2020-10-26 18:49:33 +01:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export const isDev = env.isDev
|
2021-03-26 00:42:50 +01:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export const NUMBER_REGEX = /^[+-]?([0-9]*[.])?[0-9]+$/g
|
2022-04-07 11:26:39 +02:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export function removeFromArray(array: any[], element: any) {
|
2021-09-23 20:04:53 +02:00
|
|
|
const index = array.indexOf(element)
|
|
|
|
if (index !== -1) {
|
|
|
|
array.splice(index, 1)
|
|
|
|
}
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:11:58 +02:00
|
|
|
/**
|
|
|
|
* Makes sure that a URL has the correct number of slashes, while maintaining the
|
|
|
|
* http(s):// double slashes.
|
2023-10-17 17:46:32 +02:00
|
|
|
* @param url The URL to test and remove any extra double slashes.
|
|
|
|
* @return The updated url.
|
2021-04-01 15:11:58 +02:00
|
|
|
*/
|
2022-11-22 14:56:01 +01:00
|
|
|
export function checkSlashesInUrl(url: string) {
|
2021-03-26 00:42:50 +01:00
|
|
|
return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
|
|
|
|
}
|
2021-04-01 13:48:38 +02:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export async function updateEntityMetadata(
|
|
|
|
type: string,
|
|
|
|
entityId: string,
|
|
|
|
updateFn: any
|
|
|
|
) {
|
|
|
|
const db = context.getAppDB()
|
2021-09-08 20:29:28 +02:00
|
|
|
const id = generateMetadataID(type, entityId)
|
|
|
|
// read it to see if it exists, we'll overwrite it no matter what
|
2022-11-22 14:56:01 +01:00
|
|
|
let rev, metadata: Document
|
2021-09-08 20:29:28 +02:00
|
|
|
try {
|
2023-07-18 11:41:51 +02:00
|
|
|
const oldMetadata = await db.get<any>(id)
|
2021-09-08 20:29:28 +02:00
|
|
|
rev = oldMetadata._rev
|
2021-09-10 14:52:41 +02:00
|
|
|
metadata = updateFn(oldMetadata)
|
2021-09-08 20:29:28 +02:00
|
|
|
} catch (err) {
|
|
|
|
rev = null
|
2021-09-10 14:52:41 +02:00
|
|
|
metadata = updateFn({})
|
2021-09-08 20:29:28 +02:00
|
|
|
}
|
|
|
|
metadata._id = id
|
|
|
|
if (rev) {
|
|
|
|
metadata._rev = rev
|
|
|
|
}
|
|
|
|
const response = await db.put(metadata)
|
|
|
|
return {
|
|
|
|
...metadata,
|
|
|
|
_id: id,
|
|
|
|
_rev: response.rev,
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 14:52:41 +02:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export async function saveEntityMetadata(
|
|
|
|
type: string,
|
|
|
|
entityId: string,
|
|
|
|
metadata: Document
|
|
|
|
) {
|
|
|
|
return updateEntityMetadata(type, entityId, () => {
|
2021-09-10 14:52:41 +02:00
|
|
|
return metadata
|
|
|
|
})
|
|
|
|
}
|
2021-09-13 19:03:09 +02:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export async function deleteEntityMetadata(type: string, entityId: string) {
|
|
|
|
const db = context.getAppDB()
|
2021-09-13 19:03:09 +02:00
|
|
|
const id = generateMetadataID(type, entityId)
|
|
|
|
let rev
|
|
|
|
try {
|
2023-07-18 11:41:51 +02:00
|
|
|
const metadata = await db.get<any>(id)
|
2021-09-13 19:03:09 +02:00
|
|
|
if (metadata) {
|
|
|
|
rev = metadata._rev
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// don't need to error if it doesn't exist
|
|
|
|
}
|
|
|
|
if (id && rev) {
|
|
|
|
await db.remove(id, rev)
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 19:10:30 +02:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export function escapeDangerousCharacters(string: string) {
|
2021-09-24 19:10:30 +02:00
|
|
|
return string
|
|
|
|
.replace(/[\\]/g, "\\\\")
|
|
|
|
.replace(/[\b]/g, "\\b")
|
|
|
|
.replace(/[\f]/g, "\\f")
|
|
|
|
.replace(/[\n]/g, "\\n")
|
|
|
|
.replace(/[\r]/g, "\\r")
|
|
|
|
.replace(/[\t]/g, "\\t")
|
|
|
|
}
|
2021-09-28 19:05:52 +02:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export function stringToReadStream(string: string) {
|
2021-09-28 19:05:52 +02:00
|
|
|
return new Readable({
|
|
|
|
read() {
|
|
|
|
this.push(string)
|
|
|
|
this.push(null)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2021-11-04 00:15:38 +01:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export function formatBytes(bytes: string) {
|
2021-12-06 19:23:18 +01:00
|
|
|
const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
|
|
|
|
const byteIncrements = 1024
|
|
|
|
let unit = 0
|
|
|
|
let size = parseInt(bytes, 10) || 0
|
|
|
|
while (size >= byteIncrements && ++unit) {
|
|
|
|
size /= byteIncrements
|
|
|
|
}
|
|
|
|
return `${size.toFixed(size < 10 && unit > 0 ? 1 : 0)}${units[unit]}`
|
|
|
|
}
|
2022-03-09 11:12:26 +01:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export function convertBookmark(bookmark: string) {
|
2022-03-09 11:12:26 +01:00
|
|
|
const IS_NUMBER = /^\d+\.?\d*$/
|
|
|
|
if (typeof bookmark === "string" && bookmark.match(IS_NUMBER)) {
|
|
|
|
return parseFloat(bookmark)
|
|
|
|
}
|
|
|
|
return bookmark
|
|
|
|
}
|
2022-09-09 21:06:29 +02:00
|
|
|
|
2022-11-22 14:56:01 +01:00
|
|
|
export function isQsTrue(param: string) {
|
2022-09-09 21:06:29 +02:00
|
|
|
if (typeof param === "string") {
|
|
|
|
return param.toLowerCase() === "true"
|
|
|
|
} else {
|
|
|
|
return param === true
|
|
|
|
}
|
|
|
|
}
|