@jsb188/app 1.1.11 → 1.1.13
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/LICENSE +21 -0
- package/package.json +17 -6
- package/src/constants/agent.ts +44 -57
- package/src/constants/app.ts +43 -0
- package/src/constants/wsActions.ts +1 -15
- package/src/i18n/account.ts +1 -1
- package/src/i18n/agent.ts +321 -96
- package/src/i18n/agentToolBusiness.ts +340 -0
- package/src/i18n/agentToolGrid.ts +121 -0
- package/src/i18n/agentToolWorkflow.ts +101 -0
- package/src/i18n/agent_note.ts +110 -0
- package/src/i18n/app.ts +23 -17
- package/src/i18n/auth.ts +25 -30
- package/src/i18n/billing.ts +3 -85
- package/src/i18n/c.ts +953 -0
- package/src/i18n/campaign.ts +164 -211
- package/src/i18n/datetime.ts +6 -0
- package/src/i18n/editor.ts +56 -0
- package/src/i18n/email_outreach_editor.ts +526 -0
- package/src/i18n/error.ts +430 -202
- package/src/i18n/field_doc_editor.ts +21 -0
- package/src/i18n/field_doc_programs.ts +51 -0
- package/src/i18n/field_docs.ts +171 -8
- package/src/i18n/form.ts +7 -491
- package/src/i18n/index.ts +23 -3
- package/src/i18n/log.ts +234 -39
- package/src/i18n/memory.ts +18 -5
- package/src/i18n/message.ts +1 -0
- package/src/i18n/note.ts +5 -3
- package/src/i18n/org.ts +97 -45
- package/src/i18n/schedule.ts +133 -0
- package/src/i18n/sheet.ts +105 -161
- package/src/i18n/sheet_formula.ts +165 -0
- package/src/i18n/workflow.ts +112 -11
- package/src/main.ts +2 -2
- package/src/platform.ts +89 -49
- package/src/types/ai.d.ts +16 -1
- package/src/types/app.d.ts +29 -73
- package/src/types/auth.d.ts +1 -1
- package/src/types/other.d.ts +8 -1
- package/src/utils/api.ts +60 -10
- package/src/utils/clientIP.ts +213 -0
- package/src/utils/color.ts +95 -0
- package/src/utils/csv.ts +19 -0
- package/src/utils/datetime.ts +207 -31
- package/src/utils/deferredSave.ts +89 -0
- package/src/utils/email.ts +50 -0
- package/src/utils/expression.helpers.ts +470 -0
- package/src/utils/expression.ts +1633 -239
- package/src/utils/language.ts +9 -0
- package/src/utils/logic.ts +9 -162
- package/src/utils/number.ts +30 -11
- package/src/utils/object.ts +69 -127
- package/src/utils/promise.ts +78 -7
- package/src/utils/queryReset.ts +110 -61
- package/src/utils/string.ts +80 -143
- package/src/utils/url_client.ts +115 -15
- package/src/constants/billing.ts +0 -146
- package/src/i18n/agentDoc.ts +0 -110
- package/src/types/form.d.ts +0 -101
- package/src/utils/billing.ts +0 -141
- package/src/utils/managedSessionBff.test.ts +0 -60
- package/src/utils/string.test.ts +0 -293
- package/tsconfig.json +0 -12
package/src/utils/api.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { getENVVariable } from '../main.ts';
|
|
|
3
3
|
import type { ServerErrorObj } from '../types/app.d.ts';
|
|
4
4
|
|
|
5
5
|
let AUTH_TOKEN: string | null = null;
|
|
6
|
+
let STORAGE_TOKEN: string | null = null;
|
|
6
7
|
|
|
7
8
|
export type ManagedAccessTokenBinding = {
|
|
8
9
|
accountId: string;
|
|
@@ -202,14 +203,61 @@ export function getRealtimeCapabilityOwnerKey(
|
|
|
202
203
|
return binding ? JSON.stringify([binding.accountId, binding.sessionId]) : '';
|
|
203
204
|
}
|
|
204
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Return the candidate realtime capability only when it provably renews the
|
|
208
|
+
* current one: same resource instance, organization, and owner, with a strictly
|
|
209
|
+
* later server issue time. Otherwise the current token stays. This only picks
|
|
210
|
+
* which signed token to present; the server still verifies everything.
|
|
211
|
+
*/
|
|
212
|
+
export function getNewerRealtimeCapability(
|
|
213
|
+
current: string | null | undefined,
|
|
214
|
+
candidate: string | null | undefined,
|
|
215
|
+
expectedResource: RealtimeCapabilityResource,
|
|
216
|
+
): string | null {
|
|
217
|
+
const currentToken = current || null;
|
|
218
|
+
if (!currentToken || !candidate || candidate === currentToken) {
|
|
219
|
+
return currentToken;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const resourceIdKey = {
|
|
223
|
+
DATA_TABLE: 'dataTableId',
|
|
224
|
+
SHEET: 'sheetId',
|
|
225
|
+
}[expectedResource];
|
|
226
|
+
const [currentPayload, candidatePayload] = [currentToken, candidate].map(
|
|
227
|
+
(token) => {
|
|
228
|
+
const payload = getUnverifiedJWTPayload(token);
|
|
229
|
+
return payload?.__name === 'type:RealtimeCapability' &&
|
|
230
|
+
Number(payload.realtimeCapabilityVersion || 0) === 2 &&
|
|
231
|
+
payload.resource === expectedResource &&
|
|
232
|
+
Number.isSafeInteger(payload.issuedAtMs)
|
|
233
|
+
? payload
|
|
234
|
+
: null;
|
|
235
|
+
},
|
|
236
|
+
);
|
|
237
|
+
const sameIdentity = Boolean(
|
|
238
|
+
currentPayload &&
|
|
239
|
+
candidatePayload &&
|
|
240
|
+
['organizationId', resourceIdKey, 'accountId', 'sessionId'].every(
|
|
241
|
+
(key) =>
|
|
242
|
+
String(currentPayload[key] ?? '') ===
|
|
243
|
+
String(candidatePayload[key] ?? ''),
|
|
244
|
+
),
|
|
245
|
+
);
|
|
246
|
+
return sameIdentity &&
|
|
247
|
+
candidatePayload!.issuedAtMs > currentPayload!.issuedAtMs
|
|
248
|
+
? candidate
|
|
249
|
+
: currentToken;
|
|
250
|
+
}
|
|
251
|
+
|
|
205
252
|
/**
|
|
206
253
|
* Global; save auth token to memory
|
|
207
254
|
*/
|
|
208
255
|
|
|
209
|
-
export function setAuthToken(token: string | null) {
|
|
256
|
+
export function setAuthToken(token: string | null, storageToken?: string | null) {
|
|
210
257
|
if (getENVVariable('IS_BROWSER')) {
|
|
211
258
|
// Only set auth token if it's browser; we don't want to share it across users
|
|
212
259
|
AUTH_TOKEN = token || null;
|
|
260
|
+
STORAGE_TOKEN = token && storageToken ? storageToken : null;
|
|
213
261
|
}
|
|
214
262
|
}
|
|
215
263
|
|
|
@@ -221,6 +269,13 @@ export function getAuthToken(): string | null {
|
|
|
221
269
|
return getENVVariable('IS_BROWSER') ? AUTH_TOKEN : null;
|
|
222
270
|
}
|
|
223
271
|
|
|
272
|
+
/*
|
|
273
|
+
* Read the asset credential paired with the current browser access token.
|
|
274
|
+
*/
|
|
275
|
+
export function getStorageToken(): string | null {
|
|
276
|
+
return getENVVariable('IS_BROWSER') ? STORAGE_TOKEN : null;
|
|
277
|
+
}
|
|
278
|
+
|
|
224
279
|
/**
|
|
225
280
|
* Helper; check if i18n exists for this error
|
|
226
281
|
*/
|
|
@@ -254,15 +309,10 @@ export function normalizeServerError(error?: any): ServerErrorObj {
|
|
|
254
309
|
gErr.errorCode && i18nErrCodeExists(gErr.errorCode),
|
|
255
310
|
);
|
|
256
311
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
} else {
|
|
262
|
-
statusCode = error.errors[0].statusCode;
|
|
263
|
-
errorCode = error.errors[0].errorCode;
|
|
264
|
-
errorValue = error.errors[0].variableWord;
|
|
265
|
-
}
|
|
312
|
+
const serverError = loggedError || error.errors[0];
|
|
313
|
+
statusCode = serverError.statusCode;
|
|
314
|
+
errorCode = serverError.errorCode == null ? undefined : String(serverError.errorCode);
|
|
315
|
+
errorValue = serverError.variableWord;
|
|
266
316
|
}
|
|
267
317
|
|
|
268
318
|
// Keep the returned error contract total even for malformed server payloads.
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The visitor's IP address behind our proxies, and the proof the app server
|
|
3
|
+
* sends with the visitor IP it forwards to the API.
|
|
4
|
+
*
|
|
5
|
+
* Production traffic reaches the servers through a Google Cloud external
|
|
6
|
+
* Application Load Balancer, then nginx on each instance. The load balancer
|
|
7
|
+
* appends two addresses to `X-Forwarded-For`: the client it accepted the
|
|
8
|
+
* connection from, then its own; nginx may append the Google front end it
|
|
9
|
+
* heard from. Everything left of the client's entry is whatever the client
|
|
10
|
+
* sent, so only the entries our own proxies appended can be trusted.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const CLIENT_IP = {
|
|
14
|
+
/** Addresses of our own hops: Google front ends (the load balancer's proxies), private networks and loopback. */
|
|
15
|
+
proxyRanges: [
|
|
16
|
+
['35.191.0.0', 16],
|
|
17
|
+
['130.211.0.0', 22],
|
|
18
|
+
['10.0.0.0', 8],
|
|
19
|
+
['172.16.0.0', 12],
|
|
20
|
+
['192.168.0.0', 16],
|
|
21
|
+
['169.254.0.0', 16],
|
|
22
|
+
['127.0.0.0', 8],
|
|
23
|
+
] as const,
|
|
24
|
+
proof: {
|
|
25
|
+
version: '1',
|
|
26
|
+
purpose: 'ssr-client-ip',
|
|
27
|
+
// App server and API clocks are both NTP-synced; this only has to cover the request's flight
|
|
28
|
+
maxAgeMs: 30_000,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* An IPv4 address as a 32-bit number, or null when the text is not one.
|
|
34
|
+
*/
|
|
35
|
+
function parseIPv4(value: string): number | null {
|
|
36
|
+
const parts = value.split('.');
|
|
37
|
+
if (parts.length !== 4) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let address = 0;
|
|
42
|
+
for (const part of parts) {
|
|
43
|
+
if (!/^\d{1,3}$/.test(part) || Number(part) > 255) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
address = address * 256 + Number(part);
|
|
47
|
+
}
|
|
48
|
+
return address;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* Whether the text is an IPv6 address, as the URL parser reads a bracketed host.
|
|
53
|
+
*/
|
|
54
|
+
function isIPv6(value: string): boolean {
|
|
55
|
+
if (!value.includes(':') || !/^[0-9a-f:.]+$/i.test(value)) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
new URL(`http://[${value}]/`);
|
|
61
|
+
return true;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
* Whether the text is an IPv4 or IPv6 address.
|
|
69
|
+
*/
|
|
70
|
+
export function isIPAddress(value: string): boolean {
|
|
71
|
+
return parseIPv4(value) !== null || isIPv6(value);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/*
|
|
75
|
+
* Whether an address belongs to one of our own hops (a Google front end, a
|
|
76
|
+
* private network, loopback) rather than to a client on the internet.
|
|
77
|
+
*/
|
|
78
|
+
function isProxyAddress(value: string): boolean {
|
|
79
|
+
const lower = value.toLowerCase();
|
|
80
|
+
// IPv4-mapped IPv6 (::ffff:10.0.0.1) is judged by the IPv4 address inside it
|
|
81
|
+
const ipv4 = parseIPv4(lower.startsWith('::ffff:') ? lower.slice(7) : lower);
|
|
82
|
+
if (ipv4 !== null) {
|
|
83
|
+
return CLIENT_IP.proxyRanges.some(([base, bits]) => {
|
|
84
|
+
const size = 2 ** (32 - bits);
|
|
85
|
+
return Math.floor(ipv4 / size) === Math.floor((parseIPv4(base) as number) / size);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Loopback, unique local (fc00::/7) and link-local (fe80::/10) IPv6
|
|
90
|
+
return lower === '::1' || /^f[cd][0-9a-f]{0,2}:/.test(lower) || /^fe[89ab][0-9a-f]?:/.test(lower);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* The client address our load balancer saw connect, read from the right of an
|
|
95
|
+
* `X-Forwarded-For` chain: past our own trailing hops (nginx's Google front
|
|
96
|
+
* end, private networks), past the load balancer's own address (once, or
|
|
97
|
+
* repeated if a hop copied it), to the address the load balancer appended for
|
|
98
|
+
* the connection. Null when the chain cannot have come through our load
|
|
99
|
+
* balancer (fewer than two addresses, or nothing valid where the client should
|
|
100
|
+
* be), so the caller decides what an unproxied request means.
|
|
101
|
+
*/
|
|
102
|
+
export function getForwardedClientIP(forwardedFor: string | null | undefined): string | null {
|
|
103
|
+
const hops = (forwardedFor || '')
|
|
104
|
+
.split(',')
|
|
105
|
+
.map((hop) => hop.trim())
|
|
106
|
+
.filter(Boolean);
|
|
107
|
+
let index = hops.length - 1;
|
|
108
|
+
|
|
109
|
+
while (index >= 0 && isProxyAddress(hops[index])) {
|
|
110
|
+
index--;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const balancer = hops[index];
|
|
114
|
+
while (index >= 0 && hops[index] === balancer) {
|
|
115
|
+
index--;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const client = hops[index];
|
|
119
|
+
return client && isIPAddress(client) && !isProxyAddress(client) ? client : null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
* Convert bytes into lowercase hexadecimal.
|
|
124
|
+
*/
|
|
125
|
+
function bytesToHex(bytes: Uint8Array): string {
|
|
126
|
+
return Array.from(bytes)
|
|
127
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
128
|
+
.join('');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/*
|
|
132
|
+
* Compare two equal-length signatures without data-dependent early exits.
|
|
133
|
+
*/
|
|
134
|
+
function signaturesMatch(left: string, right: string): boolean {
|
|
135
|
+
if (left.length !== right.length) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let difference = 0;
|
|
140
|
+
for (let index = 0; index < left.length; index++) {
|
|
141
|
+
difference |= left.charCodeAt(index) ^ right.charCodeAt(index);
|
|
142
|
+
}
|
|
143
|
+
return difference === 0;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/*
|
|
147
|
+
* Sign one forwarded client IP and its timestamp under the SSR client IP key domain.
|
|
148
|
+
*/
|
|
149
|
+
async function signClientIPProof(secret: string, timestamp: number, ipAddress: string): Promise<string> {
|
|
150
|
+
const { purpose, version } = CLIENT_IP.proof;
|
|
151
|
+
const key = await crypto.subtle.importKey(
|
|
152
|
+
'raw',
|
|
153
|
+
new TextEncoder().encode(`${purpose}:${secret}`),
|
|
154
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
155
|
+
false,
|
|
156
|
+
['sign'],
|
|
157
|
+
);
|
|
158
|
+
const signature = await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(`${version}:${timestamp}:${ipAddress}`));
|
|
159
|
+
return bytesToHex(new Uint8Array(signature));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
* Create the short-lived proof the app server sends with the visitor IP it
|
|
164
|
+
* forwards to the API (`x-ssr-ip-proof` beside `x-ssr-ip`), signed with the
|
|
165
|
+
* deployment secret the two share. Null without a secret or a valid address.
|
|
166
|
+
*/
|
|
167
|
+
export async function createClientIPProof(secret: string, ipAddress: string, now: number = Date.now()): Promise<string | null> {
|
|
168
|
+
if (!secret || !isIPAddress(ipAddress) || !Number.isSafeInteger(now) || now <= 0) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return `${CLIENT_IP.proof.version}.${now}.${await signClientIPProof(secret, now, ipAddress)}`;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/*
|
|
176
|
+
* When a client IP proof claims it was signed, or null when it is not shaped
|
|
177
|
+
* like one; read without checking the signature, to diagnose a rejected proof
|
|
178
|
+
* (a stale one points to clock drift, a fresh one to a secret mismatch).
|
|
179
|
+
*/
|
|
180
|
+
export function getClientIPProofTimestamp(proof: string | null | undefined): number | null {
|
|
181
|
+
const timestamp = Number(proof?.split('.')[1]);
|
|
182
|
+
return Number.isSafeInteger(timestamp) && timestamp > 0 ? timestamp : null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/*
|
|
186
|
+
* Verify that a forwarded client IP was signed by the app server within the
|
|
187
|
+
* last `maxAgeMs`, for exactly this address.
|
|
188
|
+
*/
|
|
189
|
+
export async function verifyClientIPProof(
|
|
190
|
+
proof: string | null | undefined,
|
|
191
|
+
ipAddress: string | null | undefined,
|
|
192
|
+
secret: string,
|
|
193
|
+
now: number = Date.now(),
|
|
194
|
+
): Promise<boolean> {
|
|
195
|
+
if (!proof || !ipAddress || !secret || !isIPAddress(ipAddress)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const [version, timestampText, signature, ...extra] = proof.split('.');
|
|
200
|
+
const timestamp = Number(timestampText);
|
|
201
|
+
if (
|
|
202
|
+
extra.length ||
|
|
203
|
+
version !== CLIENT_IP.proof.version ||
|
|
204
|
+
!Number.isSafeInteger(timestamp) ||
|
|
205
|
+
timestamp <= 0 ||
|
|
206
|
+
Math.abs(now - timestamp) > CLIENT_IP.proof.maxAgeMs ||
|
|
207
|
+
!/^[0-9a-f]{64}$/.test(signature || '')
|
|
208
|
+
) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return signaturesMatch(signature, await signClientIPProof(secret, timestamp, ipAddress));
|
|
213
|
+
}
|
package/src/utils/color.ts
CHANGED
|
@@ -32,6 +32,30 @@ const APP_COLOR_HEX_VALUES: Record<ColorEnum, string> = {
|
|
|
32
32
|
zinc: '#71717a',
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Return an app palette name only when the supplied value belongs to the palette.
|
|
37
|
+
*/
|
|
38
|
+
export function getAppColorName(
|
|
39
|
+
color: string | null | undefined,
|
|
40
|
+
): ColorEnum | undefined {
|
|
41
|
+
if (!color || !Object.hasOwn(APP_COLOR_HEX_VALUES, color)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return color as ColorEnum;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Resolve an app palette name to its CSS hex value while preserving custom colors.
|
|
50
|
+
*/
|
|
51
|
+
export function getAppColorValue(
|
|
52
|
+
color: string | null | undefined,
|
|
53
|
+
): string | undefined {
|
|
54
|
+
return color && Object.hasOwn(APP_COLOR_HEX_VALUES, color)
|
|
55
|
+
? APP_COLOR_HEX_VALUES[color as ColorEnum]
|
|
56
|
+
: color || undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
35
59
|
/**
|
|
36
60
|
* Return one app palette hex color picked deterministically by index.
|
|
37
61
|
*/
|
|
@@ -65,6 +89,24 @@ export function getValidColor(
|
|
|
65
89
|
return COLORS[(colorIndexHash >>> 0) % COLORS.length] as ColorEnum;
|
|
66
90
|
}
|
|
67
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Return one hex color in the six-digit form a native color input accepts, expanding #rgb, or the fallback.
|
|
94
|
+
*/
|
|
95
|
+
export function getSixDigitHexColor(
|
|
96
|
+
value: string | null | undefined,
|
|
97
|
+
fallback: string,
|
|
98
|
+
): string {
|
|
99
|
+
const hex = String(value || '')
|
|
100
|
+
.trim()
|
|
101
|
+
.toLowerCase();
|
|
102
|
+
if (/^#[0-9a-f]{6}$/.test(hex)) {
|
|
103
|
+
return hex;
|
|
104
|
+
}
|
|
105
|
+
return /^#[0-9a-f]{3}$/.test(hex)
|
|
106
|
+
? `#${hex[1]}${hex[1]}${hex[2]}${hex[2]}${hex[3]}${hex[3]}`
|
|
107
|
+
: fallback;
|
|
108
|
+
}
|
|
109
|
+
|
|
68
110
|
const RGB_COLOR_REGEX = /^rgba?\(([^)]+)\)$/i;
|
|
69
111
|
const HEX_COLOR_VALUE_REGEX = /^#[0-9a-f]{6}$/i;
|
|
70
112
|
const HEX_COLOR_DRAFT_VALUE_REGEX = /^#[0-9a-f]{0,6}$/i;
|
|
@@ -200,3 +242,56 @@ export function isDarkColor(
|
|
|
200
242
|
|
|
201
243
|
return brightness < 128;
|
|
202
244
|
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Normalize one numeric RGB channel to an integer within the supported range.
|
|
248
|
+
*/
|
|
249
|
+
function getNormalizedRGBChannel(channel: number): number {
|
|
250
|
+
return Math.max(0, Math.min(255, Math.round(channel)));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Convert one numeric RGB channel into a two-digit hex value.
|
|
255
|
+
*/
|
|
256
|
+
function getHexColorChannel(channel: number): string {
|
|
257
|
+
return getNormalizedRGBChannel(channel).toString(16).padStart(2, '0');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Convert numeric RGB channels into a six-digit CSS hex color.
|
|
262
|
+
*/
|
|
263
|
+
export function getHexColorFromRGB(
|
|
264
|
+
red: number,
|
|
265
|
+
green: number,
|
|
266
|
+
blue: number,
|
|
267
|
+
): string {
|
|
268
|
+
return `#${getHexColorChannel(red)}${getHexColorChannel(green)}${getHexColorChannel(blue)}`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Convert one sRGB channel into its linear-light value.
|
|
273
|
+
*/
|
|
274
|
+
function getLinearRGBChannel(channel: number): number {
|
|
275
|
+
const normalizedChannel = channel / 255;
|
|
276
|
+
return normalizedChannel <= 0.04045
|
|
277
|
+
? normalizedChannel / 12.92
|
|
278
|
+
: Math.pow((normalizedChannel + 0.055) / 1.055, 2.4);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Calculate WCAG relative luminance for a supported sRGB color.
|
|
283
|
+
*/
|
|
284
|
+
export function getRelativeLuminance(
|
|
285
|
+
color: string | null | undefined,
|
|
286
|
+
): number | null {
|
|
287
|
+
const rgb = getRGBColor(color);
|
|
288
|
+
if (!rgb) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return (
|
|
293
|
+
0.2126 * getLinearRGBChannel(rgb.red) +
|
|
294
|
+
0.7152 * getLinearRGBChannel(rgb.green) +
|
|
295
|
+
0.0722 * getLinearRGBChannel(rgb.blue)
|
|
296
|
+
);
|
|
297
|
+
}
|
package/src/utils/csv.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Identify spreadsheet formulas while preserving plain signed numbers and
|
|
3
|
+
* phone-like text. CSV quoting alone does not prevent formula execution.
|
|
4
|
+
*/
|
|
5
|
+
export function isCSVFormulaLikeValue(text: string) {
|
|
6
|
+
if (/^[=@\t\r\n]/.test(text)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
return /^[+-]/.test(text) && !/^[+-]?[\d\s().-]+$/.test(text) && !/^[+-]?(?:\d+\.?\d*|\.\d+)[eE][+-]?\d+$/.test(text);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/* Escape one CSV cell and neutralize spreadsheet formulas with an apostrophe. */
|
|
13
|
+
export function escapeCSVValue(value: unknown) {
|
|
14
|
+
let text = String(value ?? '');
|
|
15
|
+
if (isCSVFormulaLikeValue(text)) {
|
|
16
|
+
text = `'${text}`;
|
|
17
|
+
}
|
|
18
|
+
return /[",\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
19
|
+
}
|