Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/conat/util.ts
1452 views
1
import jsonStableStringify from "json-stable-stringify";
2
import { encode as encodeBase64, decode as decodeBase64 } from "js-base64";
3
export { encodeBase64, decodeBase64 };
4
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
5
6
export function handleErrorMessage(mesg) {
7
if (mesg?.error) {
8
if (mesg.error.startsWith("Error: ")) {
9
throw Error(mesg.error.slice("Error: ".length));
10
} else {
11
throw Error(mesg.error);
12
}
13
}
14
return mesg;
15
}
16
17
// Returns true if the subject matches the NATS pattern.
18
export function matchesPattern({
19
pattern,
20
subject,
21
}: {
22
pattern: string;
23
subject: string;
24
}): boolean {
25
const subParts = subject.split(".");
26
const patParts = pattern.split(".");
27
let i = 0,
28
j = 0;
29
while (i < subParts.length && j < patParts.length) {
30
if (patParts[j] === ">") return true;
31
if (patParts[j] !== "*" && patParts[j] !== subParts[i]) return false;
32
i++;
33
j++;
34
}
35
36
return i === subParts.length && j === patParts.length;
37
}
38
39
// Return true if the subject is a valid NATS subject.
40
// Returns true if the subject is a valid NATS subject (UTF-8 aware)
41
export function isValidSubject(subject: string): boolean {
42
if (typeof subject !== "string" || subject.length === 0) return false;
43
if (subject.startsWith(".") || subject.endsWith(".")) return false;
44
const tokens = subject.split(".");
45
// No empty tokens
46
if (tokens.some((t) => t.length === 0)) return false;
47
for (let i = 0; i < tokens.length; ++i) {
48
const tok = tokens[i];
49
// ">" is only allowed as last token
50
if (tok === ">" && i !== tokens.length - 1) return false;
51
// "*" and ">" are allowed as sole tokens
52
if (tok !== "*" && tok !== ">") {
53
// Must not contain "." or any whitespace Unicode code point
54
if (/[.\s]/u.test(tok)) {
55
return false;
56
}
57
}
58
// All tokens: must not contain whitespace (unicode aware)
59
if (/\s/u.test(tok)) {
60
return false;
61
}
62
// Allow any UTF-8 (unicode) chars except dot and whitespace in tokens.
63
}
64
return true;
65
}
66
67
export function isValidSubjectWithoutWildcards(subject: string): boolean {
68
return (
69
isValidSubject(subject) && !subject.includes("*") && !subject.endsWith(">")
70
);
71
}
72
73
export function toKey(x): string | undefined {
74
if (x === undefined) {
75
return undefined;
76
} else if (typeof x === "object") {
77
return jsonStableStringify(x);
78
} else {
79
return `${x}`;
80
}
81
}
82
83
// Returns the max payload size for messages for the NATS server
84
// that we are connected to. This is used for chunking by the kv
85
// and stream to support arbitrarily large values.
86
export const getMaxPayload = reuseInFlight(async () => {
87
// [ ] TODO
88
return 1e6;
89
});
90
91
export const waitUntilConnected = reuseInFlight(async () => {});
92
93