@openclaw/whatsapp 2026.5.1-beta.1
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/action-runtime-api.ts +1 -0
- package/action-runtime.runtime.ts +1 -0
- package/api.ts +213 -0
- package/auth-presence.ts +80 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +3 -0
- package/config-api.ts +4 -0
- package/constants.ts +1 -0
- package/contract-api.ts +29 -0
- package/directory-contract-api.ts +4 -0
- package/dist/.boundary-tsc.stamp +1 -0
- package/dist/.boundary-tsc.tsbuildinfo +1 -0
- package/doctor-contract-api.ts +8 -0
- package/index.test.ts +13 -0
- package/index.ts +16 -0
- package/legacy-session-surface-api.ts +6 -0
- package/legacy-state-migrations-api.ts +1 -0
- package/light-runtime-api.ts +12 -0
- package/login-qr-api.ts +1 -0
- package/login-qr-runtime.ts +23 -0
- package/openclaw.plugin.json +742 -0
- package/outbound-payload-test-api.ts +1 -0
- package/package.json +74 -0
- package/runtime-api.ts +86 -0
- package/secret-contract-api.ts +4 -0
- package/security-contract-api.ts +4 -0
- package/setup-entry.test.ts +21 -0
- package/setup-entry.ts +21 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-config.ts +77 -0
- package/src/account-ids.ts +13 -0
- package/src/account-types.ts +5 -0
- package/src/accounts.test.ts +182 -0
- package/src/accounts.ts +176 -0
- package/src/accounts.whatsapp-auth.test.ts +59 -0
- package/src/action-runtime-target-auth.ts +27 -0
- package/src/action-runtime.test.ts +330 -0
- package/src/action-runtime.ts +76 -0
- package/src/active-listener.test.ts +65 -0
- package/src/active-listener.ts +17 -0
- package/src/agent-tools-login.test.ts +81 -0
- package/src/agent-tools-login.ts +113 -0
- package/src/approval-auth.test.ts +24 -0
- package/src/approval-auth.ts +27 -0
- package/src/auth-store.runtime.ts +1 -0
- package/src/auth-store.test.ts +311 -0
- package/src/auth-store.ts +502 -0
- package/src/auto-reply/config.runtime.ts +16 -0
- package/src/auto-reply/constants.ts +1 -0
- package/src/auto-reply/deliver-reply.test.ts +843 -0
- package/src/auto-reply/deliver-reply.ts +279 -0
- package/src/auto-reply/heartbeat-runner.runtime.ts +33 -0
- package/src/auto-reply/heartbeat-runner.test.ts +214 -0
- package/src/auto-reply/heartbeat-runner.ts +330 -0
- package/src/auto-reply/loggers.ts +6 -0
- package/src/auto-reply/mentions.ts +131 -0
- package/src/auto-reply/monitor/ack-reaction.test.ts +170 -0
- package/src/auto-reply/monitor/ack-reaction.ts +99 -0
- package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
- package/src/auto-reply/monitor/broadcast.ts +153 -0
- package/src/auto-reply/monitor/commands.ts +19 -0
- package/src/auto-reply/monitor/echo.ts +64 -0
- package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
- package/src/auto-reply/monitor/group-activation.test.ts +189 -0
- package/src/auto-reply/monitor/group-activation.ts +73 -0
- package/src/auto-reply/monitor/group-gating.audio-preflight.test.ts +103 -0
- package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
- package/src/auto-reply/monitor/group-gating.ts +217 -0
- package/src/auto-reply/monitor/group-members.test.ts +56 -0
- package/src/auto-reply/monitor/group-members.ts +65 -0
- package/src/auto-reply/monitor/inbound-context.test.ts +97 -0
- package/src/auto-reply/monitor/inbound-context.ts +92 -0
- package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +21 -0
- package/src/auto-reply/monitor/inbound-dispatch.test.ts +895 -0
- package/src/auto-reply/monitor/inbound-dispatch.ts +422 -0
- package/src/auto-reply/monitor/last-route.test.ts +37 -0
- package/src/auto-reply/monitor/last-route.ts +61 -0
- package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
- package/src/auto-reply/monitor/message-line.ts +54 -0
- package/src/auto-reply/monitor/on-message.audio-preflight.test.ts +354 -0
- package/src/auto-reply/monitor/on-message.ts +291 -0
- package/src/auto-reply/monitor/peer.ts +17 -0
- package/src/auto-reply/monitor/process-message.audio-preflight.test.ts +420 -0
- package/src/auto-reply/monitor/process-message.test.ts +355 -0
- package/src/auto-reply/monitor/process-message.ts +531 -0
- package/src/auto-reply/monitor/runtime-api.ts +30 -0
- package/src/auto-reply/monitor-state.test.ts +64 -0
- package/src/auto-reply/monitor-state.ts +106 -0
- package/src/auto-reply/monitor.ts +600 -0
- package/src/auto-reply/reply-resolver.runtime.ts +1 -0
- package/src/auto-reply/session-snapshot.ts +69 -0
- package/src/auto-reply/types.ts +48 -0
- package/src/auto-reply/util.ts +63 -0
- package/src/auto-reply/web-auto-reply-monitor.test.ts +687 -0
- package/src/auto-reply/web-auto-reply-utils.test.ts +327 -0
- package/src/auto-reply.broadcast-groups.combined.test.ts +246 -0
- package/src/auto-reply.broadcast-groups.test-harness.ts +47 -0
- package/src/auto-reply.impl.ts +7 -0
- package/src/auto-reply.test-harness.ts +421 -0
- package/src/auto-reply.ts +1 -0
- package/src/auto-reply.web-auto-reply.compresses-common-formats-jpeg-cap.test.ts +427 -0
- package/src/auto-reply.web-auto-reply.connection-and-logging.e2e.test.ts +908 -0
- package/src/auto-reply.web-auto-reply.last-route.test.ts +184 -0
- package/src/channel-actions.runtime.ts +7 -0
- package/src/channel-actions.test.ts +216 -0
- package/src/channel-actions.ts +84 -0
- package/src/channel-outbound.test.ts +131 -0
- package/src/channel-outbound.ts +36 -0
- package/src/channel-react-action.runtime.ts +7 -0
- package/src/channel-react-action.test.ts +294 -0
- package/src/channel-react-action.ts +84 -0
- package/src/channel.runtime.ts +117 -0
- package/src/channel.setup.test.ts +461 -0
- package/src/channel.setup.ts +28 -0
- package/src/channel.ts +336 -0
- package/src/command-policy.ts +7 -0
- package/src/config-accessors.test.ts +34 -0
- package/src/config-accessors.ts +22 -0
- package/src/config-schema.test.ts +121 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +24 -0
- package/src/connection-controller-registry.test.ts +27 -0
- package/src/connection-controller-registry.ts +49 -0
- package/src/connection-controller.test.ts +282 -0
- package/src/connection-controller.ts +679 -0
- package/src/creds-files.ts +19 -0
- package/src/creds-persistence.ts +102 -0
- package/src/directory-config.test.ts +60 -0
- package/src/directory-config.ts +40 -0
- package/src/directory-contract.test.ts +56 -0
- package/src/doctor-contract.ts +11 -0
- package/src/doctor.test.ts +74 -0
- package/src/doctor.ts +56 -0
- package/src/group-intro.ts +15 -0
- package/src/group-policy.test.ts +36 -0
- package/src/group-policy.ts +40 -0
- package/src/group-session-contract.ts +20 -0
- package/src/group-session-key.test.ts +53 -0
- package/src/group-session-key.ts +41 -0
- package/src/heartbeat-recipients.runtime.ts +6 -0
- package/src/heartbeat-recipients.test.ts +203 -0
- package/src/heartbeat-recipients.ts +104 -0
- package/src/heartbeat.ts +34 -0
- package/src/identity.ts +164 -0
- package/src/inbound/access-control.test-harness.ts +37 -0
- package/src/inbound/access-control.test.ts +375 -0
- package/src/inbound/access-control.ts +237 -0
- package/src/inbound/dedupe.ts +132 -0
- package/src/inbound/extract.test.ts +282 -0
- package/src/inbound/extract.ts +488 -0
- package/src/inbound/lifecycle.ts +39 -0
- package/src/inbound/media.node.test.ts +83 -0
- package/src/inbound/media.ts +102 -0
- package/src/inbound/monitor.ts +890 -0
- package/src/inbound/runtime-api.ts +7 -0
- package/src/inbound/save-media.runtime.ts +1 -0
- package/src/inbound/send-api.test.ts +307 -0
- package/src/inbound/send-api.ts +141 -0
- package/src/inbound/send-result.ts +61 -0
- package/src/inbound/types.ts +106 -0
- package/src/inbound-context.contract.test.ts +32 -0
- package/src/inbound-policy.ts +231 -0
- package/src/inbound.media.test.ts +320 -0
- package/src/inbound.test.ts +290 -0
- package/src/inbound.ts +9 -0
- package/src/login-qr.test.ts +483 -0
- package/src/login-qr.ts +542 -0
- package/src/login.coverage.test.ts +133 -0
- package/src/login.test.ts +89 -0
- package/src/login.ts +70 -0
- package/src/logout.test.ts +162 -0
- package/src/media.test.ts +480 -0
- package/src/media.ts +10 -0
- package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +400 -0
- package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
- package/src/monitor-inbox.behavior.test.ts +5 -0
- package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +362 -0
- package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +306 -0
- package/src/monitor-inbox.streams-inbound-messages.test-support.ts +729 -0
- package/src/monitor-inbox.test-harness.ts +304 -0
- package/src/normalize-target.ts +112 -0
- package/src/normalize.ts +6 -0
- package/src/outbound-adapter.poll.test.ts +62 -0
- package/src/outbound-adapter.sendpayload.test.ts +195 -0
- package/src/outbound-adapter.ts +36 -0
- package/src/outbound-base.test.ts +560 -0
- package/src/outbound-base.ts +244 -0
- package/src/outbound-media-contract.ts +278 -0
- package/src/outbound-media.runtime.ts +36 -0
- package/src/outbound-payload.contract.test.ts +61 -0
- package/src/outbound-send-deps.ts +1 -0
- package/src/outbound-test-support.ts +16 -0
- package/src/pairing-security.test-harness.ts +59 -0
- package/src/qr-image.ts +1 -0
- package/src/qr-terminal.ts +1 -0
- package/src/quoted-message.test.ts +83 -0
- package/src/quoted-message.ts +184 -0
- package/src/reaction-level.test.ts +94 -0
- package/src/reaction-level.ts +21 -0
- package/src/reconnect.test.ts +51 -0
- package/src/reconnect.ts +55 -0
- package/src/resolve-outbound-target.test.ts +265 -0
- package/src/resolve-outbound-target.ts +54 -0
- package/src/resolve-target.test.ts +97 -0
- package/src/runtime-api.ts +60 -0
- package/src/runtime-group-policy.ts +16 -0
- package/src/runtime.ts +9 -0
- package/src/security-contract.ts +47 -0
- package/src/security-fix.ts +71 -0
- package/src/send.test.ts +515 -0
- package/src/send.ts +287 -0
- package/src/session-contract.test.ts +31 -0
- package/src/session-contract.ts +43 -0
- package/src/session-errors.ts +125 -0
- package/src/session-route.ts +26 -0
- package/src/session.runtime.ts +8 -0
- package/src/session.test.ts +559 -0
- package/src/session.ts +330 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-finalize.ts +446 -0
- package/src/setup-surface.test.ts +367 -0
- package/src/setup-surface.ts +69 -0
- package/src/setup-test-helpers.ts +216 -0
- package/src/shared.ts +291 -0
- package/src/socket-timing.test.ts +49 -0
- package/src/socket-timing.ts +38 -0
- package/src/state-migrations.ts +54 -0
- package/src/status-issues.test.ts +137 -0
- package/src/status-issues.ts +185 -0
- package/src/system-prompt.test.ts +199 -0
- package/src/system-prompt.ts +31 -0
- package/src/targets-runtime.ts +180 -0
- package/src/test-helpers.ts +690 -0
- package/src/text-runtime.test.ts +162 -0
- package/src/text-runtime.ts +11 -0
- package/src/vcard.ts +84 -0
- package/targets.ts +5 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
import {
|
|
6
|
+
assertWebChannel,
|
|
7
|
+
jidToE164,
|
|
8
|
+
markdownToWhatsApp,
|
|
9
|
+
resolveJidToE164,
|
|
10
|
+
toWhatsappJid,
|
|
11
|
+
} from "./text-runtime.js";
|
|
12
|
+
|
|
13
|
+
async function withTempDir<T>(
|
|
14
|
+
prefix: string,
|
|
15
|
+
run: (dir: string) => T | Promise<T>,
|
|
16
|
+
): Promise<Awaited<T>> {
|
|
17
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
|
18
|
+
try {
|
|
19
|
+
return await run(dir);
|
|
20
|
+
} finally {
|
|
21
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe("markdownToWhatsApp", () => {
|
|
26
|
+
it.each([
|
|
27
|
+
["converts **bold** to *bold*", "**SOD Blast:**", "*SOD Blast:*"],
|
|
28
|
+
["converts __bold__ to *bold*", "__important__", "*important*"],
|
|
29
|
+
["converts ~~strikethrough~~ to ~strikethrough~", "~~deleted~~", "~deleted~"],
|
|
30
|
+
["leaves single *italic* unchanged (already WhatsApp bold)", "*text*", "*text*"],
|
|
31
|
+
["leaves _italic_ unchanged (already WhatsApp italic)", "_text_", "_text_"],
|
|
32
|
+
["preserves inline code", "Use `**not bold**` here", "Use `**not bold**` here"],
|
|
33
|
+
[
|
|
34
|
+
"handles mixed formatting",
|
|
35
|
+
"**bold** and ~~strike~~ and _italic_",
|
|
36
|
+
"*bold* and ~strike~ and _italic_",
|
|
37
|
+
],
|
|
38
|
+
["handles multiple bold segments", "**one** then **two**", "*one* then *two*"],
|
|
39
|
+
["returns empty string for empty input", "", ""],
|
|
40
|
+
["returns plain text unchanged", "no formatting here", "no formatting here"],
|
|
41
|
+
["handles bold inside a sentence", "This is **very** important", "This is *very* important"],
|
|
42
|
+
] as const)("handles markdown-to-whatsapp conversion: %s", (_name, input, expected) => {
|
|
43
|
+
expect(markdownToWhatsApp(input)).toBe(expected);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("preserves fenced code blocks", () => {
|
|
47
|
+
const input = "```\nconst x = **bold**;\n```";
|
|
48
|
+
expect(markdownToWhatsApp(input)).toBe(input);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("preserves code block with formatting inside", () => {
|
|
52
|
+
const input = "Before ```**bold** and ~~strike~~``` after **real bold**";
|
|
53
|
+
expect(markdownToWhatsApp(input)).toBe(
|
|
54
|
+
"Before ```**bold** and ~~strike~~``` after *real bold*",
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("assertWebChannel", () => {
|
|
60
|
+
it("accepts valid channel", () => {
|
|
61
|
+
expect(() => assertWebChannel("web")).not.toThrow();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("throws for invalid channel", () => {
|
|
65
|
+
expect(() => assertWebChannel("bad" as string)).toThrow();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("toWhatsappJid", () => {
|
|
70
|
+
it("strips formatting and prefixes", () => {
|
|
71
|
+
expect(toWhatsappJid("whatsapp:+555 123 4567")).toBe("5551234567@s.whatsapp.net");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("preserves existing JIDs", () => {
|
|
75
|
+
expect(toWhatsappJid("123456789-987654321@g.us")).toBe("123456789-987654321@g.us");
|
|
76
|
+
expect(toWhatsappJid("whatsapp:123456789-987654321@g.us")).toBe("123456789-987654321@g.us");
|
|
77
|
+
expect(toWhatsappJid("1555123@s.whatsapp.net")).toBe("1555123@s.whatsapp.net");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe("jidToE164", () => {
|
|
82
|
+
it("maps @lid using reverse mapping file", async () => {
|
|
83
|
+
await withTempDir("openclaw-state-", async (stateDir) => {
|
|
84
|
+
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
|
|
85
|
+
const credentialsDir = path.join(stateDir, "credentials");
|
|
86
|
+
fs.mkdirSync(credentialsDir, { recursive: true });
|
|
87
|
+
fs.writeFileSync(
|
|
88
|
+
path.join(credentialsDir, "lid-mapping-123_reverse.json"),
|
|
89
|
+
JSON.stringify("5551234"),
|
|
90
|
+
);
|
|
91
|
+
process.env.OPENCLAW_STATE_DIR = stateDir;
|
|
92
|
+
vi.resetModules();
|
|
93
|
+
try {
|
|
94
|
+
const { jidToE164: freshJidToE164 } = await import("./text-runtime.js");
|
|
95
|
+
expect(freshJidToE164("123@lid")).toBe("+5551234");
|
|
96
|
+
} finally {
|
|
97
|
+
if (previousStateDir) {
|
|
98
|
+
process.env.OPENCLAW_STATE_DIR = previousStateDir;
|
|
99
|
+
} else {
|
|
100
|
+
delete process.env.OPENCLAW_STATE_DIR;
|
|
101
|
+
}
|
|
102
|
+
vi.resetModules();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("maps @lid from authDir mapping files", async () => {
|
|
108
|
+
await withTempDir("openclaw-auth-", (authDir) => {
|
|
109
|
+
const mappingPath = path.join(authDir, "lid-mapping-456_reverse.json");
|
|
110
|
+
fs.writeFileSync(mappingPath, JSON.stringify("5559876"));
|
|
111
|
+
expect(jidToE164("456@lid", { authDir })).toBe("+5559876");
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("maps @hosted.lid from authDir mapping files", async () => {
|
|
116
|
+
await withTempDir("openclaw-auth-", (authDir) => {
|
|
117
|
+
const mappingPath = path.join(authDir, "lid-mapping-789_reverse.json");
|
|
118
|
+
fs.writeFileSync(mappingPath, JSON.stringify(4440001));
|
|
119
|
+
expect(jidToE164("789@hosted.lid", { authDir })).toBe("+4440001");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("accepts hosted PN JIDs", () => {
|
|
124
|
+
expect(jidToE164("1555000:2@hosted")).toBe("+1555000");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("falls back through lidMappingDirs in order", async () => {
|
|
128
|
+
await withTempDir("openclaw-lid-a-", async (first) => {
|
|
129
|
+
await withTempDir("openclaw-lid-b-", (second) => {
|
|
130
|
+
const mappingPath = path.join(second, "lid-mapping-321_reverse.json");
|
|
131
|
+
fs.writeFileSync(mappingPath, JSON.stringify("123321"));
|
|
132
|
+
expect(jidToE164("321@lid", { lidMappingDirs: [first, second] })).toBe("+123321");
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
describe("resolveJidToE164", () => {
|
|
139
|
+
it("resolves @lid via lidLookup when mapping file is missing", async () => {
|
|
140
|
+
const lidLookup = {
|
|
141
|
+
getPNForLID: vi.fn().mockResolvedValue("777:0@s.whatsapp.net"),
|
|
142
|
+
};
|
|
143
|
+
await expect(resolveJidToE164("777@lid", { lidLookup })).resolves.toBe("+777");
|
|
144
|
+
expect(lidLookup.getPNForLID).toHaveBeenCalledWith("777@lid");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("skips lidLookup for non-lid JIDs", async () => {
|
|
148
|
+
const lidLookup = {
|
|
149
|
+
getPNForLID: vi.fn().mockResolvedValue("888:0@s.whatsapp.net"),
|
|
150
|
+
};
|
|
151
|
+
await expect(resolveJidToE164("888@s.whatsapp.net", { lidLookup })).resolves.toBe("+888");
|
|
152
|
+
expect(lidLookup.getPNForLID).not.toHaveBeenCalled();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("returns null when lidLookup throws", async () => {
|
|
156
|
+
const lidLookup = {
|
|
157
|
+
getPNForLID: vi.fn().mockRejectedValue(new Error("lookup failed")),
|
|
158
|
+
};
|
|
159
|
+
await expect(resolveJidToE164("777@lid", { lidLookup })).resolves.toBeNull();
|
|
160
|
+
expect(lidLookup.getPNForLID).toHaveBeenCalledWith("777@lid");
|
|
161
|
+
});
|
|
162
|
+
});
|
package/src/vcard.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
|
|
2
|
+
|
|
3
|
+
type ParsedVcard = {
|
|
4
|
+
name?: string;
|
|
5
|
+
phones: string[];
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const ALLOWED_VCARD_KEYS = new Set(["FN", "N", "TEL"]);
|
|
9
|
+
|
|
10
|
+
export function parseVcard(vcard?: string): ParsedVcard {
|
|
11
|
+
if (!vcard) {
|
|
12
|
+
return { phones: [] };
|
|
13
|
+
}
|
|
14
|
+
const lines = vcard.split(/\r?\n/);
|
|
15
|
+
let nameFromN: string | undefined;
|
|
16
|
+
let nameFromFn: string | undefined;
|
|
17
|
+
const phones: string[] = [];
|
|
18
|
+
for (const rawLine of lines) {
|
|
19
|
+
const line = rawLine.trim();
|
|
20
|
+
if (!line) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const colonIndex = line.indexOf(":");
|
|
24
|
+
if (colonIndex === -1) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const key = line.slice(0, colonIndex).toUpperCase();
|
|
28
|
+
const rawValue = line.slice(colonIndex + 1).trim();
|
|
29
|
+
if (!rawValue) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const baseKey = normalizeVcardKey(key);
|
|
33
|
+
if (!baseKey || !ALLOWED_VCARD_KEYS.has(baseKey)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const value = cleanVcardValue(rawValue);
|
|
37
|
+
if (!value) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (baseKey === "FN" && !nameFromFn) {
|
|
41
|
+
nameFromFn = normalizeVcardName(value);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (baseKey === "N" && !nameFromN) {
|
|
45
|
+
nameFromN = normalizeVcardName(value);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (baseKey === "TEL") {
|
|
49
|
+
const phone = normalizeVcardPhone(value);
|
|
50
|
+
if (phone) {
|
|
51
|
+
phones.push(phone);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { name: nameFromFn ?? nameFromN, phones };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function normalizeVcardKey(key: string): string | undefined {
|
|
59
|
+
const [primary] = key.split(";");
|
|
60
|
+
if (!primary) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const segments = primary.split(".");
|
|
64
|
+
return segments[segments.length - 1] || undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function cleanVcardValue(value: string): string {
|
|
68
|
+
return value.replace(/\\n/gi, " ").replace(/\\,/g, ",").replace(/\\;/g, ";").trim();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function normalizeVcardName(value: string): string {
|
|
72
|
+
return value.replace(/;/g, " ").replace(/\s+/g, " ").trim();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function normalizeVcardPhone(value: string): string {
|
|
76
|
+
const trimmed = value.trim();
|
|
77
|
+
if (!trimmed) {
|
|
78
|
+
return "";
|
|
79
|
+
}
|
|
80
|
+
if (normalizeLowercaseStringOrEmpty(trimmed).startsWith("tel:")) {
|
|
81
|
+
return trimmed.slice(4).trim();
|
|
82
|
+
}
|
|
83
|
+
return trimmed;
|
|
84
|
+
}
|
package/targets.ts
ADDED
package/test-api.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../tsconfig.package-boundary.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "."
|
|
5
|
+
},
|
|
6
|
+
"include": ["./*.ts", "./src/**/*.ts"],
|
|
7
|
+
"exclude": [
|
|
8
|
+
"./**/*.test.ts",
|
|
9
|
+
"./dist/**",
|
|
10
|
+
"./node_modules/**",
|
|
11
|
+
"./src/test-support/**",
|
|
12
|
+
"./src/**/*test-helpers.ts",
|
|
13
|
+
"./src/**/*test-harness.ts",
|
|
14
|
+
"./src/**/*test-support.ts"
|
|
15
|
+
]
|
|
16
|
+
}
|