fb-messenger-e2ee 0.1.1 → 0.1.3

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.
Files changed (111) hide show
  1. package/DOCS.md +41 -3
  2. package/README.md +27 -2
  3. package/dist/index.cjs +9017 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +910 -0
  6. package/dist/index.d.ts +910 -0
  7. package/dist/index.js +9049 -0
  8. package/dist/index.js.map +1 -0
  9. package/package.json +19 -4
  10. package/bun.lock +0 -1014
  11. package/examples/basic-usage.ts +0 -52
  12. package/jest.config.ts +0 -18
  13. package/src/config/env.ts +0 -15
  14. package/src/controllers/client.controller.ts +0 -805
  15. package/src/controllers/dgw-handler.ts +0 -171
  16. package/src/controllers/e2ee-handler.ts +0 -832
  17. package/src/controllers/event-mapper.ts +0 -327
  18. package/src/core/client.ts +0 -151
  19. package/src/e2ee/application/e2ee-client.ts +0 -376
  20. package/src/e2ee/application/fanout-planner.ts +0 -79
  21. package/src/e2ee/application/outbound-message-cache.ts +0 -58
  22. package/src/e2ee/application/prekey-maintenance.ts +0 -55
  23. package/src/e2ee/application/retry-manager.ts +0 -156
  24. package/src/e2ee/facebook/facebook-protocol-utils.ts +0 -230
  25. package/src/e2ee/facebook/icdc-payload.ts +0 -31
  26. package/src/e2ee/index.ts +0 -76
  27. package/src/e2ee/internal.ts +0 -27
  28. package/src/e2ee/media/media-crypto.ts +0 -147
  29. package/src/e2ee/media/media-upload.ts +0 -90
  30. package/src/e2ee/message/builders/client-payload.ts +0 -54
  31. package/src/e2ee/message/builders/consumer-application.ts +0 -362
  32. package/src/e2ee/message/builders/message-application.ts +0 -64
  33. package/src/e2ee/message/builders/message-transport.ts +0 -84
  34. package/src/e2ee/message/codecs/protobuf-codecs.ts +0 -101
  35. package/src/e2ee/message/constants.ts +0 -4
  36. package/src/e2ee/message/message-builder.ts +0 -15
  37. package/src/e2ee/signal/prekey-manager.ts +0 -140
  38. package/src/e2ee/signal/signal-manager.ts +0 -365
  39. package/src/e2ee/store/device-json.ts +0 -47
  40. package/src/e2ee/store/device-repository.ts +0 -12
  41. package/src/e2ee/store/device-store.ts +0 -538
  42. package/src/e2ee/transport/binary/decoder.ts +0 -180
  43. package/src/e2ee/transport/binary/encoder.ts +0 -143
  44. package/src/e2ee/transport/binary/stanzas.ts +0 -97
  45. package/src/e2ee/transport/binary/tokens.ts +0 -64
  46. package/src/e2ee/transport/binary/wa-binary.ts +0 -8
  47. package/src/e2ee/transport/dgw/dgw-socket.ts +0 -345
  48. package/src/e2ee/transport/noise/noise-handshake.ts +0 -417
  49. package/src/e2ee/transport/noise/noise-socket.ts +0 -230
  50. package/src/index.ts +0 -34
  51. package/src/models/client.ts +0 -26
  52. package/src/models/config.ts +0 -14
  53. package/src/models/domain.ts +0 -299
  54. package/src/models/e2ee.ts +0 -295
  55. package/src/models/media.ts +0 -41
  56. package/src/models/messaging.ts +0 -88
  57. package/src/models/thread.ts +0 -69
  58. package/src/repositories/session.repository.ts +0 -20
  59. package/src/services/auth.service.ts +0 -55
  60. package/src/services/e2ee.service.ts +0 -174
  61. package/src/services/facebook-gateway.service.ts +0 -245
  62. package/src/services/icdc.service.ts +0 -177
  63. package/src/services/media.service.ts +0 -304
  64. package/src/services/messaging.service.ts +0 -28
  65. package/src/services/thread.service.ts +0 -199
  66. package/src/types/advanced-types.ts +0 -61
  67. package/src/types/fca-unofficial.d.ts +0 -212
  68. package/src/types/protocol-types.ts +0 -43
  69. package/src/utils/fca-utils.ts +0 -30
  70. package/src/utils/logger.ts +0 -24
  71. package/src/utils/mime.ts +0 -51
  72. package/tests/.env.example +0 -87
  73. package/tests/data/1x1.png +0 -0
  74. package/tests/data/example.txt +0 -1
  75. package/tests/data/file_example.mp3 +0 -0
  76. package/tests/data/file_example.mp4 +0 -0
  77. package/tests/integration.test.ts +0 -498
  78. package/tests/script/echo-e2ee.ts +0 -174
  79. package/tests/script/send-e2ee-media.ts +0 -227
  80. package/tests/script/send-e2ee-reaction.ts +0 -108
  81. package/tests/script/send-e2ee-unsend.ts +0 -115
  82. package/tests/script/send-typing.ts +0 -107
  83. package/tests/script/test-group-send.ts +0 -105
  84. package/tests/setup.ts +0 -3
  85. package/tests/types.test.ts +0 -57
  86. package/tests/unit/controllers/dgw-handler.test.ts +0 -60
  87. package/tests/unit/controllers/e2ee-handler.test.ts +0 -293
  88. package/tests/unit/controllers/event-mapper.test.ts +0 -252
  89. package/tests/unit/e2ee/application-helpers.test.ts +0 -418
  90. package/tests/unit/e2ee/device-store.test.ts +0 -126
  91. package/tests/unit/e2ee/facebook-protocol-utils.test.ts +0 -134
  92. package/tests/unit/e2ee/media-crypto.test.ts +0 -55
  93. package/tests/unit/e2ee/media-upload.test.ts +0 -60
  94. package/tests/unit/e2ee/message-builder.test.ts +0 -42
  95. package/tests/unit/e2ee/message-builders.test.ts +0 -230
  96. package/tests/unit/e2ee/noise-handshake.test.ts +0 -260
  97. package/tests/unit/e2ee/prekey-manager.test.ts +0 -55
  98. package/tests/unit/e2ee/wa-binary.test.ts +0 -127
  99. package/tests/unit/services/e2ee.service.test.ts +0 -101
  100. package/tests/unit/services/facebook-gateway.service.test.ts +0 -138
  101. package/tests/unit/services/media.service.test.ts +0 -169
  102. package/tests/unit/utils/fca-utils.test.ts +0 -48
  103. package/tsconfig.json +0 -19
  104. /package/{src/e2ee/message/proto → proto}/ArmadilloApplication.proto +0 -0
  105. /package/{src/e2ee/message/proto → proto}/ArmadilloICDC.proto +0 -0
  106. /package/{src/e2ee/message/proto → proto}/ConsumerApplication.proto +0 -0
  107. /package/{src/e2ee/message/proto → proto}/MessageApplication.proto +0 -0
  108. /package/{src/e2ee/message/proto → proto}/MessageTransport.proto +0 -0
  109. /package/{src/e2ee/message/proto → proto}/WACommon.proto +0 -0
  110. /package/{src/e2ee/message/proto → proto}/WAMediaTransport.proto +0 -0
  111. /package/{src/e2ee/message/proto → proto}/proto-writer.ts +0 -0
@@ -1,171 +0,0 @@
1
- import { str, num, now } from "../utils/fca-utils.ts";
2
- import type { EventMapper } from "./event-mapper.ts";
3
-
4
- export class DGWHandler {
5
- private readonly seenDGWMessageIds = new Set<string>();
6
-
7
- constructor(private readonly eventMapper: EventMapper) {}
8
-
9
- public handleDGWFrame(frame: Record<string, unknown>): void {
10
- const payloadJson = frame.payloadJson;
11
- const root = this.unwrapDGWPayloadRoot(payloadJson);
12
- if (!root) return;
13
-
14
- const operations: Array<{ name: string; args: unknown[] }> = [];
15
- this.collectDGWStoredProcedures(root, operations);
16
-
17
- for (const op of operations) {
18
- const normalized = this.normalizeDGWStoredProcedureMessage(op.name, op.args);
19
- if (!normalized) continue;
20
-
21
- if (normalized.messageId && this.seenDGWMessageIds.has(normalized.messageId)) {
22
- continue;
23
- }
24
-
25
- if (normalized.messageId) {
26
- if (this.seenDGWMessageIds.size > 5000) {
27
- this.seenDGWMessageIds.clear();
28
- }
29
- this.seenDGWMessageIds.add(normalized.messageId);
30
- }
31
-
32
- this.eventMapper.emitMappedEvent({ type: "e2ee_message", data: normalized });
33
- }
34
- }
35
-
36
- public buildDGWBootstrapDataPayload(userId: string, deviceId: string): Buffer | undefined {
37
- const explicitHex = process.env.FB_DGW_BOOTSTRAP_DATA_PAYLOAD_HEX;
38
- if (explicitHex && explicitHex.trim().length > 0) {
39
- const clean = explicitHex.trim().replace(/^0x/i, "").replace(/\s+/g, "");
40
- try {
41
- return Buffer.from(clean, "hex");
42
- } catch {
43
- return undefined;
44
- }
45
- }
46
-
47
- const jsonStr = process.env.FB_DGW_BOOTSTRAP_DATA_JSON;
48
- const autoEnabled = process.env.FB_DGW_BOOTSTRAP_DATA_AUTO !== "0";
49
-
50
- const defaultJson = JSON.stringify({
51
- input_data: {
52
- user_id: userId,
53
- device_id: deviceId || process.env.FB_DGW_DEVICE_ID || "31c42901-eb7a-417b-9969-ef3bcc71b1fc",
54
- entity_fbid: userId,
55
- sync_params: JSON.stringify({
56
- filter: ["lightspeed"],
57
- force_full_sync: true,
58
- }),
59
- database: 1,
60
- client_capabilities: 7,
61
- },
62
- batch_id: 1,
63
- terminate_at_indices: [],
64
- request_id: 1,
65
- "%options": {
66
- useOSSResponseFormat: true,
67
- client_has_ods_usecase_counters: true,
68
- }
69
- });
70
-
71
- const effectiveJson = (jsonStr && jsonStr.trim().length > 0)
72
- ? jsonStr
73
- : (autoEnabled ? defaultJson : "");
74
-
75
- if (!effectiveJson) return undefined;
76
-
77
- const prefixHex = process.env.FB_DGW_BOOTSTRAP_DATA_PREFIX_HEX ?? "2c1878";
78
- const suffixHex = process.env.FB_DGW_BOOTSTRAP_DATA_SUFFIX_HEX ?? "0000";
79
-
80
- try {
81
- const normalizedJson = JSON.stringify(JSON.parse(effectiveJson));
82
- return Buffer.concat([
83
- Buffer.from(prefixHex, "hex"),
84
- Buffer.from(normalizedJson, "utf8"),
85
- Buffer.from(suffixHex, "hex"),
86
- ]);
87
- } catch {
88
- return undefined;
89
- }
90
- }
91
-
92
- public unwrapDGWPayloadRoot(payloadJson: unknown): any | null {
93
- if (payloadJson && typeof payloadJson === "object") {
94
- const obj = payloadJson as Record<string, unknown>;
95
- if (typeof obj.payload === "string") {
96
- try {
97
- const parsed = JSON.parse(obj.payload);
98
- if (parsed && typeof parsed === "object") {
99
- return this.unwrapDGWPayloadRoot(parsed);
100
- }
101
- } catch { }
102
- }
103
- return obj;
104
- }
105
-
106
- if (typeof payloadJson === "string") {
107
- try {
108
- const parsed = JSON.parse(payloadJson);
109
- if (parsed && typeof parsed === "object") {
110
- return this.unwrapDGWPayloadRoot(parsed);
111
- }
112
- } catch { }
113
- }
114
-
115
- return null;
116
- }
117
-
118
- private collectDGWStoredProcedures(node: unknown, out: Array<{ name: string; args: unknown[] }>): void {
119
- if (Array.isArray(node)) {
120
- if (node[0] === 5 && typeof node[1] === "string") {
121
- const decodedArgs = node.slice(2).map(value => this.decodeDGWStepValue(value));
122
- out.push({ name: node[1], args: decodedArgs });
123
- }
124
- for (const child of node) {
125
- this.collectDGWStoredProcedures(child, out);
126
- }
127
- return;
128
- }
129
-
130
- if (node && typeof node === "object") {
131
- for (const value of Object.values(node as Record<string, unknown>)) {
132
- this.collectDGWStoredProcedures(value, out);
133
- }
134
- }
135
- }
136
-
137
- private decodeDGWStepValue(value: unknown): unknown {
138
- if (!Array.isArray(value)) return value;
139
- if (value.length === 0) return value;
140
- const op = value[0];
141
- if (op === 9) return undefined;
142
- if (op === 19) return value[1];
143
- return value.map(item => this.decodeDGWStepValue(item));
144
- }
145
-
146
- private normalizeDGWStoredProcedureMessage(name: string, args: unknown[]): {
147
- text: string;
148
- chatJid: string;
149
- senderJid: string;
150
- messageId: string;
151
- timestampMs: number;
152
- } | null {
153
- if (name !== "insertMessage" && name !== "upsertMessage") return null;
154
-
155
- const messageId = str(args[8]);
156
- const chatJid = str(args[3]);
157
- const senderJid = str(args[10]);
158
- const isUnsent = Boolean(args[17]);
159
-
160
- if (!messageId || !chatJid || isUnsent) {
161
- return null;
162
- }
163
-
164
- const timestampMs = num(args[5]) || now();
165
- const rawText = str(args[0]);
166
- const translatedText = str(args[74]);
167
- const text = rawText || translatedText || "[non-text message]";
168
-
169
- return { text, chatJid, senderJid, messageId, timestampMs };
170
- }
171
- }