@eduardo191labs/watcher 0.0.0-stage → 2.6.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/CMakeLists.txt +82 -0
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/binding.gyp +125 -0
- package/index.d.ts +49 -0
- package/index.js +55 -0
- package/index.js.flow +45 -0
- package/package.json +61 -3
- package/scripts/build-from-source.js +16 -0
- package/src/Backend.cc +186 -0
- package/src/Backend.hh +37 -0
- package/src/Debounce.cc +113 -0
- package/src/Debounce.hh +49 -0
- package/src/DirTree.cc +164 -0
- package/src/DirTree.hh +50 -0
- package/src/Event.hh +109 -0
- package/src/Glob.cc +22 -0
- package/src/Glob.hh +34 -0
- package/src/PromiseRunner.hh +101 -0
- package/src/Signal.hh +46 -0
- package/src/Watcher.cc +241 -0
- package/src/Watcher.hh +73 -0
- package/src/binding.cc +268 -0
- package/src/kqueue/KqueueBackend.cc +306 -0
- package/src/kqueue/KqueueBackend.hh +35 -0
- package/src/linux/InotifyBackend.cc +236 -0
- package/src/linux/InotifyBackend.hh +34 -0
- package/src/macos/FSEventsBackend.cc +338 -0
- package/src/macos/FSEventsBackend.hh +20 -0
- package/src/shared/BruteForceBackend.cc +41 -0
- package/src/shared/BruteForceBackend.hh +25 -0
- package/src/unix/fts.cc +50 -0
- package/src/unix/legacy.cc +77 -0
- package/src/wasm/WasmBackend.cc +132 -0
- package/src/wasm/WasmBackend.hh +34 -0
- package/src/wasm/include.h +74 -0
- package/src/watchman/BSER.cc +302 -0
- package/src/watchman/BSER.hh +69 -0
- package/src/watchman/IPC.hh +175 -0
- package/src/watchman/WatchmanBackend.cc +342 -0
- package/src/watchman/WatchmanBackend.hh +35 -0
- package/src/windows/WindowsBackend.cc +282 -0
- package/src/windows/WindowsBackend.hh +18 -0
- package/src/windows/win_utils.cc +44 -0
- package/src/windows/win_utils.hh +11 -0
- package/wrapper.js +88 -0
package/src/unix/fts.cc
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#include <string>
|
|
2
|
+
|
|
3
|
+
// weird error on linux
|
|
4
|
+
#ifdef __THROW
|
|
5
|
+
#undef __THROW
|
|
6
|
+
#endif
|
|
7
|
+
#define __THROW
|
|
8
|
+
|
|
9
|
+
#include <fts.h>
|
|
10
|
+
#include <sys/stat.h>
|
|
11
|
+
#include "../DirTree.hh"
|
|
12
|
+
#include "../shared/BruteForceBackend.hh"
|
|
13
|
+
|
|
14
|
+
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
15
|
+
#if __APPLE__
|
|
16
|
+
#define st_mtim st_mtimespec
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
void BruteForceBackend::readTree(WatcherRef watcher, std::shared_ptr<DirTree> tree) {
|
|
20
|
+
char *paths[2] {(char *)watcher->mDir.c_str(), NULL};
|
|
21
|
+
FTS *fts = fts_open(paths, FTS_NOCHDIR | FTS_PHYSICAL, NULL);
|
|
22
|
+
if (!fts) {
|
|
23
|
+
throw WatcherError(strerror(errno), watcher);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
FTSENT *node;
|
|
27
|
+
bool isRoot = true;
|
|
28
|
+
|
|
29
|
+
while ((node = fts_read(fts)) != NULL) {
|
|
30
|
+
if (node->fts_errno) {
|
|
31
|
+
fts_close(fts);
|
|
32
|
+
throw WatcherError(strerror(node->fts_errno), watcher);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (isRoot && !(node->fts_info & FTS_D)) {
|
|
36
|
+
fts_close(fts);
|
|
37
|
+
throw WatcherError(strerror(ENOTDIR), watcher);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (watcher->isIgnored(std::string(node->fts_path))) {
|
|
41
|
+
fts_set(fts, node, FTS_SKIP);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
tree->add(node->fts_path, CONVERT_TIME(node->fts_statp->st_mtim), (node->fts_info & FTS_D) == FTS_D);
|
|
46
|
+
isRoot = false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fts_close(fts);
|
|
50
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#include <string>
|
|
2
|
+
|
|
3
|
+
// weird error on linux
|
|
4
|
+
#ifdef __THROW
|
|
5
|
+
#undef __THROW
|
|
6
|
+
#endif
|
|
7
|
+
#define __THROW
|
|
8
|
+
|
|
9
|
+
#ifdef _LIBC
|
|
10
|
+
# include <include/sys/stat.h>
|
|
11
|
+
#else
|
|
12
|
+
# include <sys/stat.h>
|
|
13
|
+
#endif
|
|
14
|
+
#include <dirent.h>
|
|
15
|
+
#include <unistd.h>
|
|
16
|
+
#include <fcntl.h>
|
|
17
|
+
|
|
18
|
+
#include "../DirTree.hh"
|
|
19
|
+
#include "../shared/BruteForceBackend.hh"
|
|
20
|
+
|
|
21
|
+
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
22
|
+
#if __APPLE__
|
|
23
|
+
#define st_mtim st_mtimespec
|
|
24
|
+
#endif
|
|
25
|
+
#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
|
|
26
|
+
|
|
27
|
+
void iterateDir(WatcherRef watcher, const std::shared_ptr <DirTree> tree, const char *relative, int parent_fd, const std::string &dirname) {
|
|
28
|
+
int open_flags = (O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
|
|
29
|
+
int new_fd = openat(parent_fd, relative, open_flags);
|
|
30
|
+
if (new_fd == -1) {
|
|
31
|
+
if (errno == EACCES) {
|
|
32
|
+
return; // ignore insufficient permissions
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
throw WatcherError(strerror(errno), watcher);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
struct stat rootAttributes;
|
|
39
|
+
fstatat(new_fd, ".", &rootAttributes, AT_SYMLINK_NOFOLLOW);
|
|
40
|
+
tree->add(dirname, CONVERT_TIME(rootAttributes.st_mtim), true);
|
|
41
|
+
|
|
42
|
+
if (DIR *dir = fdopendir(new_fd)) {
|
|
43
|
+
while (struct dirent *ent = (errno = 0, readdir(dir))) {
|
|
44
|
+
if (ISDOT(ent->d_name)) continue;
|
|
45
|
+
|
|
46
|
+
std::string fullPath = dirname + "/" + ent->d_name;
|
|
47
|
+
|
|
48
|
+
if (!watcher->isIgnored(fullPath)) {
|
|
49
|
+
struct stat attrib;
|
|
50
|
+
fstatat(new_fd, ent->d_name, &attrib, AT_SYMLINK_NOFOLLOW);
|
|
51
|
+
bool isDir = ent->d_type == DT_DIR;
|
|
52
|
+
|
|
53
|
+
if (isDir) {
|
|
54
|
+
iterateDir(watcher, tree, ent->d_name, new_fd, fullPath);
|
|
55
|
+
} else {
|
|
56
|
+
tree->add(fullPath, CONVERT_TIME(attrib.st_mtim), isDir);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
closedir(dir);
|
|
62
|
+
} else {
|
|
63
|
+
close(new_fd);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (errno) {
|
|
67
|
+
throw WatcherError(strerror(errno), watcher);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
void BruteForceBackend::readTree(WatcherRef watcher, std::shared_ptr <DirTree> tree) {
|
|
72
|
+
int fd = open(watcher->mDir.c_str(), O_RDONLY);
|
|
73
|
+
if (fd) {
|
|
74
|
+
iterateDir(watcher, tree, ".", fd, watcher->mDir);
|
|
75
|
+
close(fd);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
#include <sys/stat.h>
|
|
2
|
+
#include "WasmBackend.hh"
|
|
3
|
+
|
|
4
|
+
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
5
|
+
|
|
6
|
+
void WasmBackend::start() {
|
|
7
|
+
notifyStarted();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
void WasmBackend::subscribe(WatcherRef watcher) {
|
|
11
|
+
// Build a full directory tree recursively, and watch each directory.
|
|
12
|
+
std::shared_ptr<DirTree> tree = getTree(watcher);
|
|
13
|
+
|
|
14
|
+
for (auto it = tree->entries.begin(); it != tree->entries.end(); it++) {
|
|
15
|
+
if (it->second.isDir) {
|
|
16
|
+
watchDir(watcher, it->second.path, tree);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
void WasmBackend::watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree) {
|
|
22
|
+
int wd = wasm_backend_add_watch(path.c_str(), (void *)this);
|
|
23
|
+
std::shared_ptr<WasmSubscription> sub = std::make_shared<WasmSubscription>();
|
|
24
|
+
sub->tree = tree;
|
|
25
|
+
sub->path = path;
|
|
26
|
+
sub->watcher = watcher;
|
|
27
|
+
mSubscriptions.emplace(wd, sub);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
extern "C" void wasm_backend_event_handler(void *backend, int wd, int type, char *filename) {
|
|
31
|
+
WasmBackend *b = (WasmBackend *)(backend);
|
|
32
|
+
b->handleEvent(wd, type, filename);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
void WasmBackend::handleEvent(int wd, int type, char *filename) {
|
|
36
|
+
// Find the subscriptions for this watch descriptor
|
|
37
|
+
auto range = mSubscriptions.equal_range(wd);
|
|
38
|
+
std::unordered_set<std::shared_ptr<WasmSubscription>> set;
|
|
39
|
+
for (auto it = range.first; it != range.second; it++) {
|
|
40
|
+
set.insert(it->second);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (auto it = set.begin(); it != set.end(); it++) {
|
|
44
|
+
if (handleSubscription(type, filename, *it)) {
|
|
45
|
+
(*it)->watcher->notify();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
bool WasmBackend::handleSubscription(int type, char *filename, std::shared_ptr<WasmSubscription> sub) {
|
|
51
|
+
// Build full path and check if its in our ignore list.
|
|
52
|
+
WatcherRef watcher = sub->watcher;
|
|
53
|
+
std::string path = std::string(sub->path);
|
|
54
|
+
|
|
55
|
+
if (filename[0] != '\0') {
|
|
56
|
+
path += "/" + std::string(filename);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (watcher->isIgnored(path)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (type == 1) {
|
|
64
|
+
struct stat st;
|
|
65
|
+
stat(path.c_str(), &st);
|
|
66
|
+
sub->tree->update(path, CONVERT_TIME(st.st_mtim));
|
|
67
|
+
watcher->mEvents.update(path);
|
|
68
|
+
} else if (type == 2) {
|
|
69
|
+
// Determine if this is a create or delete depending on if the file exists or not.
|
|
70
|
+
struct stat st;
|
|
71
|
+
if (lstat(path.c_str(), &st)) {
|
|
72
|
+
// If the entry being deleted/moved is a directory, remove it from the list of subscriptions
|
|
73
|
+
DirEntry *entry = sub->tree->find(path);
|
|
74
|
+
if (!entry) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (entry->isDir) {
|
|
79
|
+
std::string pathStart = path + DIR_SEP;
|
|
80
|
+
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
|
81
|
+
if (it->second->path == path || it->second->path.rfind(pathStart, 0) == 0) {
|
|
82
|
+
wasm_backend_remove_watch(it->first);
|
|
83
|
+
it = mSubscriptions.erase(it);
|
|
84
|
+
} else {
|
|
85
|
+
++it;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Remove all sub-entries
|
|
90
|
+
for (auto it = sub->tree->entries.begin(); it != sub->tree->entries.end();) {
|
|
91
|
+
if (it->first.rfind(pathStart, 0) == 0) {
|
|
92
|
+
watcher->mEvents.remove(it->first);
|
|
93
|
+
it = sub->tree->entries.erase(it);
|
|
94
|
+
} else {
|
|
95
|
+
it++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
watcher->mEvents.remove(path);
|
|
101
|
+
sub->tree->remove(path);
|
|
102
|
+
} else if (sub->tree->find(path)) {
|
|
103
|
+
sub->tree->update(path, CONVERT_TIME(st.st_mtim));
|
|
104
|
+
watcher->mEvents.update(path);
|
|
105
|
+
} else {
|
|
106
|
+
watcher->mEvents.create(path);
|
|
107
|
+
|
|
108
|
+
// If this is a create, check if it's a directory and start watching if it is.
|
|
109
|
+
DirEntry *entry = sub->tree->add(path, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
|
|
110
|
+
if (entry->isDir) {
|
|
111
|
+
watchDir(watcher, path, sub->tree);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
void WasmBackend::unsubscribe(WatcherRef watcher) {
|
|
120
|
+
// Find any subscriptions pointing to this watcher, and remove them.
|
|
121
|
+
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
|
122
|
+
if (it->second->watcher.get() == watcher.get()) {
|
|
123
|
+
if (mSubscriptions.count(it->first) == 1) {
|
|
124
|
+
wasm_backend_remove_watch(it->first);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
it = mSubscriptions.erase(it);
|
|
128
|
+
} else {
|
|
129
|
+
it++;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#ifndef WASM_H
|
|
2
|
+
#define WASM_H
|
|
3
|
+
|
|
4
|
+
#include <unordered_map>
|
|
5
|
+
#include "../shared/BruteForceBackend.hh"
|
|
6
|
+
#include "../DirTree.hh"
|
|
7
|
+
|
|
8
|
+
extern "C" {
|
|
9
|
+
int wasm_backend_add_watch(const char *filename, void *backend);
|
|
10
|
+
void wasm_backend_remove_watch(int wd);
|
|
11
|
+
void wasm_backend_event_handler(void *backend, int wd, int type, char *filename);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
struct WasmSubscription {
|
|
15
|
+
std::shared_ptr<DirTree> tree;
|
|
16
|
+
std::string path;
|
|
17
|
+
WatcherRef watcher;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
class WasmBackend : public BruteForceBackend {
|
|
21
|
+
public:
|
|
22
|
+
void start() override;
|
|
23
|
+
void subscribe(WatcherRef watcher) override;
|
|
24
|
+
void unsubscribe(WatcherRef watcher) override;
|
|
25
|
+
void handleEvent(int wd, int type, char *filename);
|
|
26
|
+
private:
|
|
27
|
+
int mWasm;
|
|
28
|
+
std::unordered_multimap<int, std::shared_ptr<WasmSubscription>> mSubscriptions;
|
|
29
|
+
|
|
30
|
+
void watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree);
|
|
31
|
+
bool handleSubscription(int type, char *filename, std::shared_ptr<WasmSubscription> sub);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
#endif
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright Node.js contributors. All rights reserved.
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to
|
|
6
|
+
deal in the Software without restriction, including without limitation the
|
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
8
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
|
12
|
+
all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
19
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
20
|
+
IN THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// Node does not include the headers for these functions when compiling for WASM, so add them here.
|
|
24
|
+
#ifdef __wasm32__
|
|
25
|
+
extern "C" {
|
|
26
|
+
NAPI_EXTERN napi_status NAPI_CDECL
|
|
27
|
+
napi_create_threadsafe_function(napi_env env,
|
|
28
|
+
napi_value func,
|
|
29
|
+
napi_value async_resource,
|
|
30
|
+
napi_value async_resource_name,
|
|
31
|
+
size_t max_queue_size,
|
|
32
|
+
size_t initial_thread_count,
|
|
33
|
+
void* thread_finalize_data,
|
|
34
|
+
napi_finalize thread_finalize_cb,
|
|
35
|
+
void* context,
|
|
36
|
+
napi_threadsafe_function_call_js call_js_cb,
|
|
37
|
+
napi_threadsafe_function* result);
|
|
38
|
+
|
|
39
|
+
NAPI_EXTERN napi_status NAPI_CDECL napi_get_threadsafe_function_context(
|
|
40
|
+
napi_threadsafe_function func, void** result);
|
|
41
|
+
|
|
42
|
+
NAPI_EXTERN napi_status NAPI_CDECL
|
|
43
|
+
napi_call_threadsafe_function(napi_threadsafe_function func,
|
|
44
|
+
void* data,
|
|
45
|
+
napi_threadsafe_function_call_mode is_blocking);
|
|
46
|
+
|
|
47
|
+
NAPI_EXTERN napi_status NAPI_CDECL
|
|
48
|
+
napi_acquire_threadsafe_function(napi_threadsafe_function func);
|
|
49
|
+
|
|
50
|
+
NAPI_EXTERN napi_status NAPI_CDECL napi_release_threadsafe_function(
|
|
51
|
+
napi_threadsafe_function func, napi_threadsafe_function_release_mode mode);
|
|
52
|
+
|
|
53
|
+
NAPI_EXTERN napi_status NAPI_CDECL
|
|
54
|
+
napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func);
|
|
55
|
+
|
|
56
|
+
NAPI_EXTERN napi_status NAPI_CDECL
|
|
57
|
+
napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func);
|
|
58
|
+
|
|
59
|
+
NAPI_EXTERN napi_status NAPI_CDECL
|
|
60
|
+
napi_create_async_work(napi_env env,
|
|
61
|
+
napi_value async_resource,
|
|
62
|
+
napi_value async_resource_name,
|
|
63
|
+
napi_async_execute_callback execute,
|
|
64
|
+
napi_async_complete_callback complete,
|
|
65
|
+
void* data,
|
|
66
|
+
napi_async_work* result);
|
|
67
|
+
NAPI_EXTERN napi_status NAPI_CDECL napi_delete_async_work(napi_env env,
|
|
68
|
+
napi_async_work work);
|
|
69
|
+
NAPI_EXTERN napi_status NAPI_CDECL napi_queue_async_work(napi_env env,
|
|
70
|
+
napi_async_work work);
|
|
71
|
+
NAPI_EXTERN napi_status NAPI_CDECL napi_cancel_async_work(napi_env env,
|
|
72
|
+
napi_async_work work);
|
|
73
|
+
}
|
|
74
|
+
#endif
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
#include <stdint.h>
|
|
2
|
+
#include "./BSER.hh"
|
|
3
|
+
|
|
4
|
+
BSERType decodeType(std::istream &iss) {
|
|
5
|
+
int8_t type;
|
|
6
|
+
iss.read(reinterpret_cast<char*>(&type), sizeof(type));
|
|
7
|
+
return (BSERType) type;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
void expectType(std::istream &iss, BSERType expected) {
|
|
11
|
+
BSERType got = decodeType(iss);
|
|
12
|
+
if (got != expected) {
|
|
13
|
+
throw std::runtime_error("Unexpected BSER type");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
void encodeType(std::ostream &oss, BSERType type) {
|
|
18
|
+
int8_t t = (int8_t)type;
|
|
19
|
+
oss.write(reinterpret_cast<char*>(&t), sizeof(t));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
template<typename T>
|
|
23
|
+
class Value : public BSERValue {
|
|
24
|
+
public:
|
|
25
|
+
T value;
|
|
26
|
+
Value(T val) {
|
|
27
|
+
value = val;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
Value() {}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
class BSERInteger : public Value<int64_t> {
|
|
34
|
+
public:
|
|
35
|
+
BSERInteger(int64_t value) : Value(value) {}
|
|
36
|
+
BSERInteger(std::istream &iss) {
|
|
37
|
+
int8_t int8;
|
|
38
|
+
int16_t int16;
|
|
39
|
+
int32_t int32;
|
|
40
|
+
int64_t int64;
|
|
41
|
+
|
|
42
|
+
BSERType type = decodeType(iss);
|
|
43
|
+
|
|
44
|
+
switch (type) {
|
|
45
|
+
case BSER_INT8:
|
|
46
|
+
iss.read(reinterpret_cast<char*>(&int8), sizeof(int8));
|
|
47
|
+
value = int8;
|
|
48
|
+
break;
|
|
49
|
+
case BSER_INT16:
|
|
50
|
+
iss.read(reinterpret_cast<char*>(&int16), sizeof(int16));
|
|
51
|
+
value = int16;
|
|
52
|
+
break;
|
|
53
|
+
case BSER_INT32:
|
|
54
|
+
iss.read(reinterpret_cast<char*>(&int32), sizeof(int32));
|
|
55
|
+
value = int32;
|
|
56
|
+
break;
|
|
57
|
+
case BSER_INT64:
|
|
58
|
+
iss.read(reinterpret_cast<char*>(&int64), sizeof(int64));
|
|
59
|
+
value = int64;
|
|
60
|
+
break;
|
|
61
|
+
default:
|
|
62
|
+
throw std::runtime_error("Invalid BSER int type");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
int64_t intValue() override {
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
void encode(std::ostream &oss) override {
|
|
71
|
+
if (value <= INT8_MAX) {
|
|
72
|
+
encodeType(oss, BSER_INT8);
|
|
73
|
+
int8_t v = (int8_t)value;
|
|
74
|
+
oss.write(reinterpret_cast<char*>(&v), sizeof(v));
|
|
75
|
+
} else if (value <= INT16_MAX) {
|
|
76
|
+
encodeType(oss, BSER_INT16);
|
|
77
|
+
int16_t v = (int16_t)value;
|
|
78
|
+
oss.write(reinterpret_cast<char*>(&v), sizeof(v));
|
|
79
|
+
} else if (value <= INT32_MAX) {
|
|
80
|
+
encodeType(oss, BSER_INT32);
|
|
81
|
+
int32_t v = (int32_t)value;
|
|
82
|
+
oss.write(reinterpret_cast<char*>(&v), sizeof(v));
|
|
83
|
+
} else {
|
|
84
|
+
encodeType(oss, BSER_INT64);
|
|
85
|
+
oss.write(reinterpret_cast<char*>(&value), sizeof(value));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
class BSERArray : public Value<BSER::Array> {
|
|
91
|
+
public:
|
|
92
|
+
BSERArray() : Value() {}
|
|
93
|
+
BSERArray(BSER::Array value) : Value(value) {}
|
|
94
|
+
BSERArray(std::istream &iss) {
|
|
95
|
+
expectType(iss, BSER_ARRAY);
|
|
96
|
+
int64_t len = BSERInteger(iss).intValue();
|
|
97
|
+
for (int64_t i = 0; i < len; i++) {
|
|
98
|
+
value.push_back(BSER(iss));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
BSER::Array arrayValue() override {
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
void encode(std::ostream &oss) override {
|
|
107
|
+
encodeType(oss, BSER_ARRAY);
|
|
108
|
+
BSERInteger(value.size()).encode(oss);
|
|
109
|
+
for (auto it = value.begin(); it != value.end(); it++) {
|
|
110
|
+
it->encode(oss);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
class BSERString : public Value<std::string> {
|
|
116
|
+
public:
|
|
117
|
+
BSERString(std::string value) : Value(value) {}
|
|
118
|
+
BSERString(std::istream &iss) {
|
|
119
|
+
expectType(iss, BSER_STRING);
|
|
120
|
+
int64_t len = BSERInteger(iss).intValue();
|
|
121
|
+
value.resize(len);
|
|
122
|
+
iss.read(&value[0], len);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
std::string stringValue() override {
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
void encode(std::ostream &oss) override {
|
|
130
|
+
encodeType(oss, BSER_STRING);
|
|
131
|
+
BSERInteger(value.size()).encode(oss);
|
|
132
|
+
oss << value;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
class BSERObject : public Value<BSER::Object> {
|
|
137
|
+
public:
|
|
138
|
+
BSERObject() : Value() {}
|
|
139
|
+
BSERObject(BSER::Object value) : Value(value) {}
|
|
140
|
+
BSERObject(std::istream &iss) {
|
|
141
|
+
expectType(iss, BSER_OBJECT);
|
|
142
|
+
int64_t len = BSERInteger(iss).intValue();
|
|
143
|
+
for (int64_t i = 0; i < len; i++) {
|
|
144
|
+
auto key = BSERString(iss).stringValue();
|
|
145
|
+
auto val = BSER(iss);
|
|
146
|
+
value.emplace(key, val);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
BSER::Object objectValue() override {
|
|
151
|
+
return value;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
void encode(std::ostream &oss) override {
|
|
155
|
+
encodeType(oss, BSER_OBJECT);
|
|
156
|
+
BSERInteger(value.size()).encode(oss);
|
|
157
|
+
for (auto it = value.begin(); it != value.end(); it++) {
|
|
158
|
+
BSERString(it->first).encode(oss);
|
|
159
|
+
it->second.encode(oss);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
class BSERDouble : public Value<double> {
|
|
165
|
+
public:
|
|
166
|
+
BSERDouble(double value) : Value(value) {}
|
|
167
|
+
BSERDouble(std::istream &iss) {
|
|
168
|
+
expectType(iss, BSER_REAL);
|
|
169
|
+
iss.read(reinterpret_cast<char*>(&value), sizeof(value));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
double doubleValue() override {
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
void encode(std::ostream &oss) override {
|
|
177
|
+
encodeType(oss, BSER_REAL);
|
|
178
|
+
oss.write(reinterpret_cast<char*>(&value), sizeof(value));
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
class BSERBoolean : public Value<bool> {
|
|
183
|
+
public:
|
|
184
|
+
BSERBoolean(bool value) : Value(value) {}
|
|
185
|
+
bool boolValue() override { return value; }
|
|
186
|
+
void encode(std::ostream &oss) override {
|
|
187
|
+
int8_t t = value == true ? static_cast<int8_t>(BSER_BOOL_TRUE) : static_cast<int8_t>(BSER_BOOL_FALSE);
|
|
188
|
+
oss.write(reinterpret_cast<char*>(&t), sizeof(t));
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
class BSERNull : public Value<bool> {
|
|
193
|
+
public:
|
|
194
|
+
BSERNull() : Value(false) {}
|
|
195
|
+
void encode(std::ostream &oss) override {
|
|
196
|
+
encodeType(oss, BSER_NULL);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
std::shared_ptr<BSERArray> decodeTemplate(std::istream &iss) {
|
|
201
|
+
expectType(iss, BSER_TEMPLATE);
|
|
202
|
+
auto keys = BSERArray(iss).arrayValue();
|
|
203
|
+
auto len = BSERInteger(iss).intValue();
|
|
204
|
+
std::shared_ptr<BSERArray> arr = std::make_shared<BSERArray>();
|
|
205
|
+
for (int64_t i = 0; i < len; i++) {
|
|
206
|
+
BSER::Object obj;
|
|
207
|
+
for (auto it = keys.begin(); it != keys.end(); it++) {
|
|
208
|
+
if (iss.peek() == 0x0c) {
|
|
209
|
+
iss.ignore(1);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
auto val = BSER(iss);
|
|
214
|
+
obj.emplace(it->stringValue(), val);
|
|
215
|
+
}
|
|
216
|
+
arr->value.push_back(obj);
|
|
217
|
+
}
|
|
218
|
+
return arr;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
BSER::BSER(std::istream &iss) {
|
|
222
|
+
BSERType type = decodeType(iss);
|
|
223
|
+
iss.unget();
|
|
224
|
+
|
|
225
|
+
switch (type) {
|
|
226
|
+
case BSER_ARRAY:
|
|
227
|
+
m_ptr = std::make_shared<BSERArray>(iss);
|
|
228
|
+
break;
|
|
229
|
+
case BSER_OBJECT:
|
|
230
|
+
m_ptr = std::make_shared<BSERObject>(iss);
|
|
231
|
+
break;
|
|
232
|
+
case BSER_STRING:
|
|
233
|
+
m_ptr = std::make_shared<BSERString>(iss);
|
|
234
|
+
break;
|
|
235
|
+
case BSER_INT8:
|
|
236
|
+
case BSER_INT16:
|
|
237
|
+
case BSER_INT32:
|
|
238
|
+
case BSER_INT64:
|
|
239
|
+
m_ptr = std::make_shared<BSERInteger>(iss);
|
|
240
|
+
break;
|
|
241
|
+
case BSER_REAL:
|
|
242
|
+
m_ptr = std::make_shared<BSERDouble>(iss);
|
|
243
|
+
break;
|
|
244
|
+
case BSER_BOOL_TRUE:
|
|
245
|
+
iss.ignore(1);
|
|
246
|
+
m_ptr = std::make_shared<BSERBoolean>(true);
|
|
247
|
+
break;
|
|
248
|
+
case BSER_BOOL_FALSE:
|
|
249
|
+
iss.ignore(1);
|
|
250
|
+
m_ptr = std::make_shared<BSERBoolean>(false);
|
|
251
|
+
break;
|
|
252
|
+
case BSER_NULL:
|
|
253
|
+
iss.ignore(1);
|
|
254
|
+
m_ptr = std::make_shared<BSERNull>();
|
|
255
|
+
break;
|
|
256
|
+
case BSER_TEMPLATE:
|
|
257
|
+
m_ptr = decodeTemplate(iss);
|
|
258
|
+
break;
|
|
259
|
+
default:
|
|
260
|
+
throw std::runtime_error("unknown BSER type");
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
BSER::BSER() : m_ptr(std::make_shared<BSERNull>()) {}
|
|
265
|
+
BSER::BSER(BSER::Array value) : m_ptr(std::make_shared<BSERArray>(value)) {}
|
|
266
|
+
BSER::BSER(BSER::Object value) : m_ptr(std::make_shared<BSERObject>(value)) {}
|
|
267
|
+
BSER::BSER(const char *value) : m_ptr(std::make_shared<BSERString>(value)) {}
|
|
268
|
+
BSER::BSER(std::string value) : m_ptr(std::make_shared<BSERString>(value)) {}
|
|
269
|
+
BSER::BSER(int64_t value) : m_ptr(std::make_shared<BSERInteger>(value)) {}
|
|
270
|
+
BSER::BSER(double value) : m_ptr(std::make_shared<BSERDouble>(value)) {}
|
|
271
|
+
BSER::BSER(bool value) : m_ptr(std::make_shared<BSERBoolean>(value)) {}
|
|
272
|
+
|
|
273
|
+
BSER::Array BSER::arrayValue() { return m_ptr->arrayValue(); }
|
|
274
|
+
BSER::Object BSER::objectValue() { return m_ptr->objectValue(); }
|
|
275
|
+
std::string BSER::stringValue() { return m_ptr->stringValue(); }
|
|
276
|
+
int64_t BSER::intValue() { return m_ptr->intValue(); }
|
|
277
|
+
double BSER::doubleValue() { return m_ptr->doubleValue(); }
|
|
278
|
+
bool BSER::boolValue() { return m_ptr->boolValue(); }
|
|
279
|
+
void BSER::encode(std::ostream &oss) {
|
|
280
|
+
m_ptr->encode(oss);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
int64_t BSER::decodeLength(std::istream &iss) {
|
|
284
|
+
char pdu[2];
|
|
285
|
+
if (!iss.read(pdu, 2) || pdu[0] != 0 || pdu[1] != 1) {
|
|
286
|
+
throw std::runtime_error("Invalid BSER");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return BSERInteger(iss).intValue();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
std::string BSER::encode() {
|
|
293
|
+
std::ostringstream oss(std::ios_base::binary);
|
|
294
|
+
encode(oss);
|
|
295
|
+
|
|
296
|
+
std::ostringstream res(std::ios_base::binary);
|
|
297
|
+
res.write("\x00\x01", 2);
|
|
298
|
+
|
|
299
|
+
BSERInteger(oss.str().size()).encode(res);
|
|
300
|
+
res << oss.str();
|
|
301
|
+
return res.str();
|
|
302
|
+
}
|