@tidegap/core 0.25.0 → 0.26.0
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.
|
@@ -13,7 +13,12 @@ export declare const NTFY_DEFAULT_SERVER = "https://ntfy.sh";
|
|
|
13
13
|
export declare const NTFY_MAX_BODY_BYTES = 4000;
|
|
14
14
|
/** The header value of `text`: as is when printable ASCII, else an RFC 2047 base64 encoded word. */
|
|
15
15
|
export declare function headerText(text: string): string;
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* At most `max` UTF-8 bytes, with an ellipsis when cut (empty when even the
|
|
18
|
+
* ellipsis does not fit). The cut falls between grapheme clusters
|
|
19
|
+
* ({@link headWithin}): a cluster that would cross the cap is left out whole,
|
|
20
|
+
* and a first cluster longer than the cap is cut between code points.
|
|
21
|
+
*/
|
|
17
22
|
export declare function clipBytes(text: string, max: number): string;
|
|
18
23
|
/**
|
|
19
24
|
* The publish URL: the server (any path prefix kept, any query or fragment
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* ntfy decodes. The optional access token is a Bearer header; it belongs to
|
|
7
7
|
* its server (`keepWith`), so a new server never gets the old token.
|
|
8
8
|
*/
|
|
9
|
+
import { headWithin } from "../notice.js";
|
|
9
10
|
import { optString } from "./shared.js";
|
|
10
11
|
export const NTFY_DEFAULT_SERVER = "https://ntfy.sh";
|
|
11
12
|
/** ntfy turns a longer body into an attachment; stay under its 4,096-byte message limit. */
|
|
@@ -29,19 +30,28 @@ export function headerText(text) {
|
|
|
29
30
|
return `=?UTF-8?B?${Buffer.from(text, "utf8").toString("base64")}?=`;
|
|
30
31
|
}
|
|
31
32
|
const ELLIPSIS_BYTES = Buffer.byteLength("…", "utf8");
|
|
32
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* At most `max` UTF-8 bytes, with an ellipsis when cut (empty when even the
|
|
35
|
+
* ellipsis does not fit). The cut falls between grapheme clusters
|
|
36
|
+
* ({@link headWithin}): a cluster that would cross the cap is left out whole,
|
|
37
|
+
* and a first cluster longer than the cap is cut between code points.
|
|
38
|
+
*/
|
|
33
39
|
export function clipBytes(text, max) {
|
|
34
40
|
if (Buffer.byteLength(text, "utf8") <= max)
|
|
35
41
|
return text;
|
|
36
|
-
|
|
42
|
+
if (max < ELLIPSIS_BYTES)
|
|
43
|
+
return "";
|
|
44
|
+
// The most code points whose bytes fit before the ellipsis. Every head of at most that many code points fits too,
|
|
45
|
+
// so the longest one that ends between clusters is the cut.
|
|
46
|
+
let fit = 0;
|
|
37
47
|
let size = ELLIPSIS_BYTES;
|
|
38
48
|
for (const ch of text) {
|
|
39
49
|
size += Buffer.byteLength(ch, "utf8");
|
|
40
50
|
if (size > max)
|
|
41
51
|
break;
|
|
42
|
-
|
|
52
|
+
fit++;
|
|
43
53
|
}
|
|
44
|
-
return `${
|
|
54
|
+
return `${headWithin(text, fit)}…`;
|
|
45
55
|
}
|
|
46
56
|
/**
|
|
47
57
|
* The publish URL: the server (any path prefix kept, any query or fragment
|