node-bible-sync 0.0.0-stage → 0.3.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.
- package/.github/workflows/build.yml +39 -0
- package/API.md +81 -0
- package/COPYING +339 -0
- package/README.md +113 -2
- package/biblesync.js +234 -0
- package/binding.gyp +62 -0
- package/examples/biblesync.js +53 -0
- package/index.js +31 -0
- package/package.json +26 -4
- package/scripts/fetch_biblesync.js +80 -0
- package/scripts/ios_framework_postbuild.sh +85 -0
- package/scripts/prepare_biblesync.js +189 -0
- package/src/biblesync_compat/biblesync-version.hh +29 -0
- package/src/biblesync_compat/libintl.h +24 -0
- package/src/biblesync_compat/unistd.h +29 -0
- package/src/biblesync_compat/uuid/uuid.h +61 -0
- package/src/napi_module/biblesync_session.cpp +433 -0
- package/src/napi_module/biblesync_session.hpp +90 -0
- package/src/napi_module/binding.cpp +26 -0
- package/test/biblesync.test.js +142 -0
- package/test/biblesync_example.test.js +59 -0
- package/test/biblesync_fetch.test.js +86 -0
- package/test/biblesync_integration.test.js +267 -0
- package/test/biblesync_native.test.js +162 -0
- package/test/biblesync_prepare.test.js +81 -0
- package/test/fixtures/biblesync_fail_send.c +61 -0
- package/test/fixtures/biblesync_peer.js +53 -0
- package/test/fixtures/biblesync_system_peer.cpp +62 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/* This file is part of node-bible-sync.
|
|
4
|
+
|
|
5
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
6
|
+
|
|
7
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
8
|
+
it under the terms of the GNU General Public License as published by
|
|
9
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
10
|
+
(at your option) any later version.
|
|
11
|
+
|
|
12
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
15
|
+
See the GNU General Public License for more details.
|
|
16
|
+
|
|
17
|
+
You should have received a copy of the GNU General Public License
|
|
18
|
+
along with node-bible-sync. See the file COPYING.
|
|
19
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
20
|
+
|
|
21
|
+
const fs = require('fs');
|
|
22
|
+
const path = require('path');
|
|
23
|
+
const { fetchBibleSync } = require('./fetch_biblesync');
|
|
24
|
+
|
|
25
|
+
const output = process.argv[2];
|
|
26
|
+
if (!output) throw new Error('BibleSync generated source path is required');
|
|
27
|
+
fetchBibleSync();
|
|
28
|
+
|
|
29
|
+
const sourcePath = path.join(__dirname, '..', 'biblesync', 'src', 'biblesync.cc');
|
|
30
|
+
let source = fs.readFileSync(sourcePath, 'utf8').replace(/\r\n/g, '\n');
|
|
31
|
+
const headerPath = path.join(__dirname, '..', 'biblesync', 'include', 'biblesync.hh');
|
|
32
|
+
let header = fs.readFileSync(headerPath, 'utf8').replace(/\r\n/g, '\n');
|
|
33
|
+
|
|
34
|
+
function replaceOnce(from, to) {
|
|
35
|
+
const first = source.indexOf(from);
|
|
36
|
+
if (first < 0 || source.indexOf(from, first + from.length) !== -1) {
|
|
37
|
+
throw new Error(`Unexpected BibleSync 2.2.0 source; cannot apply portability change: ${from.slice(0, 60)}`);
|
|
38
|
+
}
|
|
39
|
+
source = source.slice(0, first) + to + source.slice(first + from.length);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Avoid std::bind being selected instead of the socket function by libc++.
|
|
43
|
+
replaceOnce('if (bind(server_fd, (struct sockaddr*)&server,',
|
|
44
|
+
'if (::bind(server_fd, (struct sockaddr*)&server,');
|
|
45
|
+
|
|
46
|
+
// Make the intentional assignment and validated array index explicit.
|
|
47
|
+
replaceOnce("chase = strchr(chase, '\\n'); ++chase)",
|
|
48
|
+
"(chase = strchr(chase, '\\n')); ++chase)");
|
|
49
|
+
replaceOnce('outbound_fill_count[message_type]', 'outbound_fill_count[static_cast<int>(message_type)]');
|
|
50
|
+
|
|
51
|
+
// Report failures of Windows UUID generation rather than using an invalid UUID.
|
|
52
|
+
replaceOnce('#include <biblesync.hh>', '#include <biblesync.hh>\nextern thread_local int node_bible_sync_uuid_error;');
|
|
53
|
+
replaceOnce(' UuidCreate(&u);', ` RPC_STATUS status = UuidCreate(&u);
|
|
54
|
+
node_bible_sync_uuid_error = (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY) ? 0 : status;`);
|
|
55
|
+
|
|
56
|
+
// Balance Winsock initialization and close the interface-discovery socket.
|
|
57
|
+
replaceOnce(' server_fd = client_fd = -1;\n\n // internal shutdown.',
|
|
58
|
+
` server_fd = client_fd = -1;
|
|
59
|
+
#ifdef WIN32
|
|
60
|
+
if (winsock_initialized) {
|
|
61
|
+
WSACleanup();
|
|
62
|
+
winsock_initialized = false;
|
|
63
|
+
}
|
|
64
|
+
#endif
|
|
65
|
+
|
|
66
|
+
// internal shutdown.`);
|
|
67
|
+
replaceOnce(' SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);',
|
|
68
|
+
' winsock_initialized = true;\n SOCKET sd = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);');
|
|
69
|
+
replaceOnce(`\t== SOCKET_ERROR) {
|
|
70
|
+
\treturn;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
int nNumInterfaces`, `\t== SOCKET_ERROR) {
|
|
74
|
+
\tclosesocket(sd);
|
|
75
|
+
\treturn;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
int nNumInterfaces`);
|
|
79
|
+
replaceOnce(` return;
|
|
80
|
+
}
|
|
81
|
+
#endif /* WIN32 */`, ` closesocket(sd);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
#endif /* WIN32 */`);
|
|
85
|
+
|
|
86
|
+
// Bound one receive batch so a busy network cannot prevent shutdown forever.
|
|
87
|
+
replaceOnce('while ((recv_size = InitSelectRead(dump, &source, &bsp)) > 0)',
|
|
88
|
+
'for (unsigned packets = 0; packets < 64 && (recv_size = InitSelectRead(dump, &source, &bsp)) > 0; ++packets)');
|
|
89
|
+
|
|
90
|
+
// Do not announce a session when socket setup or the initial beacon failed.
|
|
91
|
+
replaceOnce('if ((mode == BSP_MODE_PERSONAL) || (mode == BSP_MODE_SPEAKER))\n\t{\n\t TransmitInternal(BSP_BEACON);',
|
|
92
|
+
'if (ok_so_far && ((mode == BSP_MODE_PERSONAL) || (mode == BSP_MODE_SPEAKER)))\n\t{\n\t TransmitInternal(BSP_BEACON);');
|
|
93
|
+
replaceOnce('if (retval == "")\n\t TransmitInternal(BSP_ANNOUNCE);',
|
|
94
|
+
'if (retval == "" && mode != BSP_MODE_DISABLE)\n\t TransmitInternal(BSP_ANNOUNCE);');
|
|
95
|
+
|
|
96
|
+
// socklen_t differs between Android ABIs.
|
|
97
|
+
replaceOnce(`#ifndef WIN32
|
|
98
|
+
// yes, really:
|
|
99
|
+
// linux insists on unsigned int, win32 insists on int.
|
|
100
|
+
// each complains bitterly of invalid conversion if wrongly used.
|
|
101
|
+
unsigned
|
|
102
|
+
#endif
|
|
103
|
+
int source_length = sizeof(*source);`, `#ifndef WIN32
|
|
104
|
+
socklen_t source_length = sizeof(*source);
|
|
105
|
+
#else
|
|
106
|
+
int source_length = sizeof(*source);
|
|
107
|
+
#endif`);
|
|
108
|
+
|
|
109
|
+
// Mobile systems do not reliably expose /proc/net/route or shell utilities.
|
|
110
|
+
const begin = source.indexOf('#ifndef WIN32\n\n#ifdef linux\n\n// in order to do multicast setup');
|
|
111
|
+
const endMarker = '#endif /* linux */\n\n#else\t/* WIN32 */';
|
|
112
|
+
const end = source.indexOf(endMarker, begin);
|
|
113
|
+
if (begin < 0 || end < 0) throw new Error('Unexpected BibleSync interface discovery implementation');
|
|
114
|
+
source = source.slice(0, begin) + `#ifndef WIN32
|
|
115
|
+
#include <ifaddrs.h>
|
|
116
|
+
#include <net/if.h>
|
|
117
|
+
#include <sys/ioctl.h>
|
|
118
|
+
|
|
119
|
+
void BibleSync::InterfaceAddress()
|
|
120
|
+
{
|
|
121
|
+
interface_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
122
|
+
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
123
|
+
if (fd >= 0) {
|
|
124
|
+
struct sockaddr_in destination = {};
|
|
125
|
+
destination.sin_family = AF_INET;
|
|
126
|
+
destination.sin_port = htons(BSP_PORT);
|
|
127
|
+
destination.sin_addr.s_addr = inet_addr(BSP_MULTICAST);
|
|
128
|
+
if (connect(fd, (struct sockaddr *)&destination, sizeof(destination)) == 0) {
|
|
129
|
+
struct sockaddr_in local = {};
|
|
130
|
+
socklen_t length = sizeof(local);
|
|
131
|
+
if (getsockname(fd, (struct sockaddr *)&local, &length) == 0 &&
|
|
132
|
+
local.sin_addr.s_addr != htonl(INADDR_ANY)) {
|
|
133
|
+
interface_addr = local.sin_addr;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
close(fd);
|
|
137
|
+
}
|
|
138
|
+
if (interface_addr.s_addr != htonl(INADDR_LOOPBACK)) return;
|
|
139
|
+
#if defined(__ANDROID__) && __ANDROID_API__ < 24
|
|
140
|
+
int probe = socket(AF_INET, SOCK_DGRAM, 0);
|
|
141
|
+
if (probe < 0) return;
|
|
142
|
+
struct ifreq entries[64] = {};
|
|
143
|
+
struct ifconf list = {};
|
|
144
|
+
list.ifc_len = sizeof(entries);
|
|
145
|
+
list.ifc_req = entries;
|
|
146
|
+
if (ioctl(probe, SIOCGIFCONF, &list) == 0) {
|
|
147
|
+
unsigned count = list.ifc_len / sizeof(struct ifreq);
|
|
148
|
+
for (unsigned i = 0; i < count; ++i) {
|
|
149
|
+
struct ifreq flags = entries[i];
|
|
150
|
+
if (entries[i].ifr_addr.sa_family == AF_INET &&
|
|
151
|
+
ioctl(probe, SIOCGIFFLAGS, &flags) == 0 &&
|
|
152
|
+
(flags.ifr_flags & IFF_UP) && (flags.ifr_flags & IFF_MULTICAST) &&
|
|
153
|
+
!(flags.ifr_flags & IFF_LOOPBACK)) {
|
|
154
|
+
interface_addr = ((struct sockaddr_in *)&entries[i].ifr_addr)->sin_addr;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
close(probe);
|
|
160
|
+
#else
|
|
161
|
+
struct ifaddrs *interfaces = NULL;
|
|
162
|
+
if (getifaddrs(&interfaces) != 0) return;
|
|
163
|
+
for (struct ifaddrs *item = interfaces; item; item = item->ifa_next) {
|
|
164
|
+
if (item->ifa_addr && item->ifa_addr->sa_family == AF_INET &&
|
|
165
|
+
(item->ifa_flags & IFF_UP) && (item->ifa_flags & IFF_MULTICAST) &&
|
|
166
|
+
!(item->ifa_flags & IFF_LOOPBACK)) {
|
|
167
|
+
interface_addr = ((struct sockaddr_in *)item->ifa_addr)->sin_addr;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
freeifaddrs(interfaces);
|
|
172
|
+
#endif
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
` + source.slice(end + '#endif /* linux */\n\n'.length);
|
|
176
|
+
|
|
177
|
+
fs.mkdirSync(path.dirname(output), { recursive: true });
|
|
178
|
+
fs.writeFileSync(output, source);
|
|
179
|
+
const passphraseGetter = ' inline string getPassphrase(void) { return passphrase; };';
|
|
180
|
+
if (header.split(passphraseGetter).length !== 2) throw new Error('Unexpected BibleSync 2.2.0 header');
|
|
181
|
+
header = header.replace(passphraseGetter, `${passphraseGetter}
|
|
182
|
+
inline string getDevice(void) { return device; };`);
|
|
183
|
+
const socketFields = ' int server_fd, client_fd;';
|
|
184
|
+
if (header.split(socketFields).length !== 2) throw new Error('Unexpected BibleSync 2.2.0 socket fields');
|
|
185
|
+
header = header.replace(socketFields, `${socketFields}
|
|
186
|
+
#ifdef WIN32
|
|
187
|
+
bool winsock_initialized = false;
|
|
188
|
+
#endif`);
|
|
189
|
+
fs.writeFileSync(path.join(path.dirname(output), 'biblesync.hh'), header);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
#ifndef NODE_BIBLE_SYNC_VERSION_HH
|
|
20
|
+
#define NODE_BIBLE_SYNC_VERSION_HH
|
|
21
|
+
|
|
22
|
+
#define BIBLESYNC_VERSION_NUM 2002000000
|
|
23
|
+
#define BIBLESYNC_VERSION_STR "2.2.0"
|
|
24
|
+
#define BIBLESYNC_VERSION_MAJOR 2
|
|
25
|
+
#define BIBLESYNC_VERSION_MINOR 2
|
|
26
|
+
#define BIBLESYNC_VERSION_MICRO 0
|
|
27
|
+
#define BIBLESYNC_VERSION_NANO 0
|
|
28
|
+
|
|
29
|
+
#endif
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
#ifndef NODE_BIBLE_SYNC_LIBINTL_H
|
|
20
|
+
#define NODE_BIBLE_SYNC_LIBINTL_H
|
|
21
|
+
|
|
22
|
+
static inline const char *gettext(const char *text) { return text; }
|
|
23
|
+
|
|
24
|
+
#endif
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
#ifndef NODE_BIBLE_SYNC_UNISTD_H
|
|
20
|
+
#define NODE_BIBLE_SYNC_UNISTD_H
|
|
21
|
+
|
|
22
|
+
#ifdef _WIN32
|
|
23
|
+
#include <winsock2.h>
|
|
24
|
+
#define close closesocket
|
|
25
|
+
#else
|
|
26
|
+
#include_next <unistd.h>
|
|
27
|
+
#endif
|
|
28
|
+
|
|
29
|
+
#endif
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
#ifndef NODE_BIBLE_SYNC_UUID_H
|
|
20
|
+
#define NODE_BIBLE_SYNC_UUID_H
|
|
21
|
+
|
|
22
|
+
#include <errno.h>
|
|
23
|
+
#include <fcntl.h>
|
|
24
|
+
#include <stdlib.h>
|
|
25
|
+
#include <string.h>
|
|
26
|
+
#include <unistd.h>
|
|
27
|
+
|
|
28
|
+
typedef unsigned char uuid_t[16];
|
|
29
|
+
extern thread_local int node_bible_sync_uuid_error;
|
|
30
|
+
|
|
31
|
+
static inline void uuid_generate(uuid_t uuid)
|
|
32
|
+
{
|
|
33
|
+
node_bible_sync_uuid_error = 0;
|
|
34
|
+
#ifdef __APPLE__
|
|
35
|
+
arc4random_buf(uuid, sizeof(uuid_t));
|
|
36
|
+
#else
|
|
37
|
+
int fd = open("/dev/urandom", O_RDONLY);
|
|
38
|
+
if (fd < 0) {
|
|
39
|
+
node_bible_sync_uuid_error = errno;
|
|
40
|
+
memset(uuid, 0, sizeof(uuid_t));
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
size_t received = 0;
|
|
44
|
+
while (received < sizeof(uuid_t)) {
|
|
45
|
+
ssize_t count = read(fd, uuid + received, sizeof(uuid_t) - received);
|
|
46
|
+
if (count < 0 && errno == EINTR) continue;
|
|
47
|
+
if (count <= 0) {
|
|
48
|
+
node_bible_sync_uuid_error = count == 0 ? EIO : errno;
|
|
49
|
+
memset(uuid, 0, sizeof(uuid_t));
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
received += static_cast<size_t>(count);
|
|
53
|
+
}
|
|
54
|
+
close(fd);
|
|
55
|
+
if (node_bible_sync_uuid_error != 0) return;
|
|
56
|
+
#endif
|
|
57
|
+
uuid[6] = (uuid[6] & 0x0f) | 0x40;
|
|
58
|
+
uuid[8] = (uuid[8] & 0x3f) | 0x80;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#endif
|