@port-labs/jq-node-bindings 0.0.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/.editorconfig +5 -0
- package/.jshintignore +1 -0
- package/.jshintrc +23 -0
- package/binding.gyp +56 -0
- package/configure +26 -0
- package/deps/jq/.gitattributes +2 -0
- package/deps/jq/.travis.yml +53 -0
- package/deps/jq/AUTHORS +73 -0
- package/deps/jq/COPYING +70 -0
- package/deps/jq/ChangeLog +1349 -0
- package/deps/jq/Makefile.am +198 -0
- package/deps/jq/NEWS +88 -0
- package/deps/jq/README.md +64 -0
- package/deps/jq/builtin.c +1684 -0
- package/deps/jq/builtin.h +10 -0
- package/deps/jq/bytecode.c +161 -0
- package/deps/jq/bytecode.h +92 -0
- package/deps/jq/compile-ios.sh +102 -0
- package/deps/jq/compile.c +1210 -0
- package/deps/jq/compile.h +101 -0
- package/deps/jq/config/m4/check-math-func.m4 +4 -0
- package/deps/jq/config/m4/find-func-no-libs.m4 +8 -0
- package/deps/jq/config/m4/find-func-no-libs2.m4 +62 -0
- package/deps/jq/config/m4/find-func.m4 +9 -0
- package/deps/jq/config/m4/misc.m4 +3 -0
- package/deps/jq/configure.ac +221 -0
- package/deps/jq/docs/Gemfile +7 -0
- package/deps/jq/docs/Gemfile.lock +63 -0
- package/deps/jq/docs/README.md +25 -0
- package/deps/jq/docs/Rakefile +145 -0
- package/deps/jq/docs/content/1.tutorial/default.yml +327 -0
- package/deps/jq/docs/content/2.download/default.yml +117 -0
- package/deps/jq/docs/content/3.manual/manual.yml +2878 -0
- package/deps/jq/docs/content/3.manual/v1.3/manual.yml +1270 -0
- package/deps/jq/docs/content/3.manual/v1.4/manual.yml +1672 -0
- package/deps/jq/docs/content/index/index.yml +51 -0
- package/deps/jq/docs/default_manpage.md +22 -0
- package/deps/jq/docs/public/.htaccess +28 -0
- package/deps/jq/docs/public/bootstrap/css/bootstrap-responsive.css +1058 -0
- package/deps/jq/docs/public/bootstrap/css/bootstrap-responsive.min.css +9 -0
- package/deps/jq/docs/public/bootstrap/css/bootstrap.css +5224 -0
- package/deps/jq/docs/public/bootstrap/css/bootstrap.min.css +9 -0
- package/deps/jq/docs/public/bootstrap/img/glyphicons-halflings-white.png +0 -0
- package/deps/jq/docs/public/bootstrap/img/glyphicons-halflings.png +0 -0
- package/deps/jq/docs/public/bootstrap/js/bootstrap.js +2027 -0
- package/deps/jq/docs/public/bootstrap/js/bootstrap.min.js +6 -0
- package/deps/jq/docs/public/css/base.scss +99 -0
- package/deps/jq/docs/public/jq.png +0 -0
- package/deps/jq/docs/public/robots.txt +2 -0
- package/deps/jq/docs/site.yml +18 -0
- package/deps/jq/docs/templates/default.liquid +34 -0
- package/deps/jq/docs/templates/index.liquid +60 -0
- package/deps/jq/docs/templates/manual.liquid +122 -0
- package/deps/jq/docs/templates/shared/_footer.liquid +5 -0
- package/deps/jq/docs/templates/shared/_head.liquid +12 -0
- package/deps/jq/docs/templates/shared/_header.liquid +26 -0
- package/deps/jq/exec_stack.h +112 -0
- package/deps/jq/execute.c +1155 -0
- package/deps/jq/inject_errors.c +112 -0
- package/deps/jq/jq.1.default +39 -0
- package/deps/jq/jq.1.prebuilt +3075 -0
- package/deps/jq/jq.h +60 -0
- package/deps/jq/jq.spec +70 -0
- package/deps/jq/jq_parser.h +9 -0
- package/deps/jq/jq_test.c +346 -0
- package/deps/jq/jv.c +1333 -0
- package/deps/jq/jv.h +240 -0
- package/deps/jq/jv_alloc.c +179 -0
- package/deps/jq/jv_alloc.h +27 -0
- package/deps/jq/jv_aux.c +619 -0
- package/deps/jq/jv_dtoa.c +4275 -0
- package/deps/jq/jv_dtoa.h +22 -0
- package/deps/jq/jv_file.c +49 -0
- package/deps/jq/jv_parse.c +852 -0
- package/deps/jq/jv_print.c +348 -0
- package/deps/jq/jv_unicode.c +96 -0
- package/deps/jq/jv_unicode.h +11 -0
- package/deps/jq/jv_utf8_tables.h +37 -0
- package/deps/jq/lexer.c +2442 -0
- package/deps/jq/lexer.h +362 -0
- package/deps/jq/lexer.l +184 -0
- package/deps/jq/libm.h +160 -0
- package/deps/jq/linker.c +393 -0
- package/deps/jq/linker.h +7 -0
- package/deps/jq/locfile.c +91 -0
- package/deps/jq/locfile.h +29 -0
- package/deps/jq/m4/ax_compare_version.m4 +177 -0
- package/deps/jq/m4/ax_prog_bison_version.m4 +68 -0
- package/deps/jq/main.c +566 -0
- package/deps/jq/opcode_list.h +44 -0
- package/deps/jq/parser.c +3914 -0
- package/deps/jq/parser.h +193 -0
- package/deps/jq/parser.y +923 -0
- package/deps/jq/scripts/crosscompile +42 -0
- package/deps/jq/scripts/gen_utf8_tables.py +32 -0
- package/deps/jq/scripts/version +5 -0
- package/deps/jq/setup.sh +33 -0
- package/deps/jq/tests/jq.test +1235 -0
- package/deps/jq/tests/jqtest +5 -0
- package/deps/jq/tests/mantest +7 -0
- package/deps/jq/tests/modules/.jq +5 -0
- package/deps/jq/tests/modules/a.jq +2 -0
- package/deps/jq/tests/modules/b/b.jq +2 -0
- package/deps/jq/tests/modules/c/c.jq +16 -0
- package/deps/jq/tests/modules/c/d.jq +1 -0
- package/deps/jq/tests/modules/data.json +4 -0
- package/deps/jq/tests/modules/lib/jq/e/e.jq +1 -0
- package/deps/jq/tests/modules/lib/jq/f.jq +1 -0
- package/deps/jq/tests/modules/syntaxerror/syntaxerror.jq +1 -0
- package/deps/jq/tests/modules/test_bind_order.jq +4 -0
- package/deps/jq/tests/modules/test_bind_order0.jq +1 -0
- package/deps/jq/tests/modules/test_bind_order1.jq +2 -0
- package/deps/jq/tests/modules/test_bind_order2.jq +2 -0
- package/deps/jq/tests/onig.supp +21 -0
- package/deps/jq/tests/onig.test +85 -0
- package/deps/jq/tests/onigtest +5 -0
- package/deps/jq/tests/setup +36 -0
- package/deps/jq/tests/shtest +205 -0
- package/deps/jq/tests/torture/input0.json +7 -0
- package/deps/jq/util.c +462 -0
- package/deps/jq/util.h +64 -0
- package/deps/jq.gyp +35 -0
- package/index.d.ts +3 -0
- package/jest.config.js +10 -0
- package/lib/index.js +14 -0
- package/package.json +48 -0
- package/reports/jest-port-api.xml +35 -0
- package/src/binding.cc +177 -0
- package/src/binding.h +13 -0
- package/test/santiy.test.js +122 -0
- package/util/configure.js +27 -0
package/deps/jq/util.c
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
|
|
2
|
+
#ifdef HAVE_MEMMEM
|
|
3
|
+
#define _GNU_SOURCE
|
|
4
|
+
#endif
|
|
5
|
+
|
|
6
|
+
#include <sys/types.h>
|
|
7
|
+
#include <sys/stat.h>
|
|
8
|
+
#include <assert.h>
|
|
9
|
+
#include <errno.h>
|
|
10
|
+
#include <fcntl.h>
|
|
11
|
+
#include <limits.h>
|
|
12
|
+
#include <string.h>
|
|
13
|
+
#include <unistd.h>
|
|
14
|
+
#include <stdlib.h>
|
|
15
|
+
#include <stddef.h>
|
|
16
|
+
#ifdef HAVE_ALLOCA_H
|
|
17
|
+
# include <alloca.h>
|
|
18
|
+
#elif !defined alloca
|
|
19
|
+
# ifdef __GNUC__
|
|
20
|
+
# define alloca __builtin_alloca
|
|
21
|
+
# elif defined _MSC_VER
|
|
22
|
+
# include <malloc.h>
|
|
23
|
+
# define alloca _alloca
|
|
24
|
+
# elif !defined HAVE_ALLOCA
|
|
25
|
+
# ifdef __cplusplus
|
|
26
|
+
extern "C"
|
|
27
|
+
# endif
|
|
28
|
+
void *alloca (size_t);
|
|
29
|
+
# endif
|
|
30
|
+
#endif
|
|
31
|
+
#ifndef WIN32
|
|
32
|
+
#include <pwd.h>
|
|
33
|
+
#endif
|
|
34
|
+
|
|
35
|
+
#ifdef WIN32
|
|
36
|
+
#include <windows.h>
|
|
37
|
+
#include <processenv.h>
|
|
38
|
+
#include <shellapi.h>
|
|
39
|
+
#include <wchar.h>
|
|
40
|
+
#include <wtypes.h>
|
|
41
|
+
#endif
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
#include "util.h"
|
|
45
|
+
#include "jq.h"
|
|
46
|
+
#include "jv_alloc.h"
|
|
47
|
+
|
|
48
|
+
#ifdef WIN32
|
|
49
|
+
FILE *fopen(const char *fname, const char *mode) {
|
|
50
|
+
size_t sz = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
|
|
51
|
+
wchar_t *wfname = alloca(sz);
|
|
52
|
+
MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname, sz);
|
|
53
|
+
|
|
54
|
+
sz = MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0);
|
|
55
|
+
wchar_t *wmode = alloca(sz);
|
|
56
|
+
MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, sz);
|
|
57
|
+
return _wfopen(wfname, wmode);
|
|
58
|
+
}
|
|
59
|
+
#endif
|
|
60
|
+
|
|
61
|
+
#ifndef HAVE_MKSTEMP
|
|
62
|
+
int mkstemp(char *template) {
|
|
63
|
+
size_t len = strlen(template);
|
|
64
|
+
int tries=5;
|
|
65
|
+
int fd;
|
|
66
|
+
|
|
67
|
+
// mktemp() truncates template when it fails
|
|
68
|
+
char *s = alloca(len + 1);
|
|
69
|
+
assert(s != NULL);
|
|
70
|
+
strcpy(s, template);
|
|
71
|
+
|
|
72
|
+
do {
|
|
73
|
+
// Restore template
|
|
74
|
+
strcpy(template, s);
|
|
75
|
+
(void) mktemp(template);
|
|
76
|
+
fd = open(template, O_CREAT | O_EXCL | O_RDWR, 0600);
|
|
77
|
+
} while (fd == -1 && tries-- > 0);
|
|
78
|
+
return fd;
|
|
79
|
+
}
|
|
80
|
+
#endif
|
|
81
|
+
|
|
82
|
+
jv expand_path(jv path) {
|
|
83
|
+
assert(jv_get_kind(path) == JV_KIND_STRING);
|
|
84
|
+
const char *pstr = jv_string_value(path);
|
|
85
|
+
jv ret = path;
|
|
86
|
+
if (jv_string_length_bytes(jv_copy(path)) > 1 && pstr[0] == '~' && pstr[1] == '/') {
|
|
87
|
+
jv home = get_home();
|
|
88
|
+
if (jv_is_valid(home)) {
|
|
89
|
+
ret = jv_string_fmt("%s/%s",jv_string_value(home),pstr+2);
|
|
90
|
+
jv_free(home);
|
|
91
|
+
} else {
|
|
92
|
+
jv emsg = jv_invalid_get_msg(home);
|
|
93
|
+
ret = jv_invalid_with_msg(jv_string_fmt("Could not expand %s. (%s)", pstr, jv_string_value(emsg)));
|
|
94
|
+
jv_free(emsg);
|
|
95
|
+
}
|
|
96
|
+
jv_free(path);
|
|
97
|
+
}
|
|
98
|
+
return ret;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
jv get_home() {
|
|
102
|
+
jv ret;
|
|
103
|
+
char *home = getenv("HOME");
|
|
104
|
+
if (!home) {
|
|
105
|
+
#ifndef WIN32
|
|
106
|
+
struct passwd* pwd = getpwuid(getuid());
|
|
107
|
+
if (pwd)
|
|
108
|
+
ret = jv_string(pwd->pw_dir);
|
|
109
|
+
else
|
|
110
|
+
ret = jv_invalid_with_msg(jv_string("Could not find home directory."));
|
|
111
|
+
#else
|
|
112
|
+
home = getenv("USERPROFILE");
|
|
113
|
+
if (!home) {
|
|
114
|
+
char *hd = getenv("HOMEDRIVE");
|
|
115
|
+
if (!hd) hd = "";
|
|
116
|
+
home = getenv("HOMEPATH");
|
|
117
|
+
if (!home) {
|
|
118
|
+
ret = jv_invalid_with_msg(jv_string("Could not find home directory."));
|
|
119
|
+
} else {
|
|
120
|
+
ret = jv_string_fmt("%s%s",hd,home);
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
ret = jv_string(home);
|
|
124
|
+
}
|
|
125
|
+
#endif
|
|
126
|
+
} else {
|
|
127
|
+
ret = jv_string(home);
|
|
128
|
+
}
|
|
129
|
+
return ret;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
jv jq_realpath(jv path) {
|
|
134
|
+
int path_max;
|
|
135
|
+
char *buf = NULL;
|
|
136
|
+
#ifdef _PC_PATH_MAX
|
|
137
|
+
path_max = pathconf(jv_string_value(path),_PC_PATH_MAX);
|
|
138
|
+
#else
|
|
139
|
+
path_max = PATH_MAX;
|
|
140
|
+
#endif
|
|
141
|
+
if (path_max > 0) {
|
|
142
|
+
buf = malloc(sizeof(char) * path_max);
|
|
143
|
+
}
|
|
144
|
+
#ifdef WIN32
|
|
145
|
+
char *tmp = _fullpath(buf, jv_string_value(path), path_max);
|
|
146
|
+
#else
|
|
147
|
+
char *tmp = realpath(jv_string_value(path), buf);
|
|
148
|
+
#endif
|
|
149
|
+
if (tmp == NULL) {
|
|
150
|
+
free(buf);
|
|
151
|
+
return path;
|
|
152
|
+
}
|
|
153
|
+
jv_free(path);
|
|
154
|
+
path = jv_string(tmp);
|
|
155
|
+
free(tmp);
|
|
156
|
+
return path;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const void *_jq_memmem(const void *haystack, size_t haystacklen,
|
|
160
|
+
const void *needle, size_t needlelen) {
|
|
161
|
+
#ifdef HAVE_MEMMEM
|
|
162
|
+
return (const void*)memmem(haystack, haystacklen, needle, needlelen);
|
|
163
|
+
#else
|
|
164
|
+
const char *h = haystack;
|
|
165
|
+
const char *n = needle;
|
|
166
|
+
size_t hi, hi2, ni;
|
|
167
|
+
|
|
168
|
+
if (haystacklen < needlelen || haystacklen == 0)
|
|
169
|
+
return NULL;
|
|
170
|
+
for (hi = 0; hi < (haystacklen - needlelen + 1); hi++) {
|
|
171
|
+
for (ni = 0, hi2 = hi; ni < needlelen; ni++, hi2++) {
|
|
172
|
+
if (h[hi2] != n[ni])
|
|
173
|
+
goto not_this;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return &h[hi];
|
|
177
|
+
|
|
178
|
+
not_this:
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
return NULL;
|
|
182
|
+
#endif /* !HAVE_MEMMEM */
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
struct jq_util_input_state {
|
|
186
|
+
jq_util_msg_cb err_cb;
|
|
187
|
+
void *err_cb_data;
|
|
188
|
+
jv_parser *parser;
|
|
189
|
+
FILE* current_input;
|
|
190
|
+
char **files;
|
|
191
|
+
int nfiles;
|
|
192
|
+
int curr_file;
|
|
193
|
+
int failures;
|
|
194
|
+
jv slurped;
|
|
195
|
+
char buf[4096];
|
|
196
|
+
size_t buf_valid_len;
|
|
197
|
+
jv current_filename;
|
|
198
|
+
size_t current_line;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
static void fprinter(void *data, const char *fname) {
|
|
202
|
+
fprintf((FILE *)data, "jq: error: Could not open file %s: %s\n", fname, strerror(errno));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// If parser == NULL -> RAW
|
|
206
|
+
jq_util_input_state *jq_util_input_init(jq_util_msg_cb err_cb, void *err_cb_data) {
|
|
207
|
+
if (err_cb == NULL) {
|
|
208
|
+
err_cb = fprinter;
|
|
209
|
+
err_cb_data = stderr;
|
|
210
|
+
}
|
|
211
|
+
jq_util_input_state *new_state = jv_mem_alloc(sizeof(*new_state));
|
|
212
|
+
memset(new_state, 0, sizeof(*new_state));
|
|
213
|
+
new_state->err_cb = err_cb;
|
|
214
|
+
new_state->err_cb_data = err_cb_data;
|
|
215
|
+
new_state->parser = NULL;
|
|
216
|
+
new_state->current_input = NULL;
|
|
217
|
+
new_state->files = NULL;
|
|
218
|
+
new_state->nfiles = 0;
|
|
219
|
+
new_state->curr_file = 0;
|
|
220
|
+
new_state->slurped = jv_invalid();
|
|
221
|
+
new_state->buf[0] = 0;
|
|
222
|
+
new_state->buf_valid_len = 0;
|
|
223
|
+
new_state->current_filename = jv_invalid();
|
|
224
|
+
new_state->current_line = 0;
|
|
225
|
+
|
|
226
|
+
return new_state;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
void jq_util_input_set_parser(jq_util_input_state *state, jv_parser *parser, int slurp) {
|
|
230
|
+
assert(!jv_is_valid(state->slurped));
|
|
231
|
+
state->parser = parser;
|
|
232
|
+
|
|
233
|
+
if (parser == NULL && slurp)
|
|
234
|
+
state->slurped = jv_string("");
|
|
235
|
+
else if (slurp)
|
|
236
|
+
state->slurped = jv_array();
|
|
237
|
+
else
|
|
238
|
+
state->slurped = jv_invalid();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
void jq_util_input_free(jq_util_input_state **state) {
|
|
242
|
+
jq_util_input_state *old_state = *state;
|
|
243
|
+
*state = NULL;
|
|
244
|
+
if (old_state == NULL)
|
|
245
|
+
return;
|
|
246
|
+
|
|
247
|
+
if (old_state->parser != NULL)
|
|
248
|
+
jv_parser_free(old_state->parser);
|
|
249
|
+
for (int i = 0; i < old_state->nfiles; i++)
|
|
250
|
+
free(old_state->files[i]);
|
|
251
|
+
free(old_state->files);
|
|
252
|
+
jv_free(old_state->slurped);
|
|
253
|
+
jv_free(old_state->current_filename);
|
|
254
|
+
jv_mem_free(old_state);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
void jq_util_input_add_input(jq_util_input_state *state, const char *fname) {
|
|
258
|
+
state->files = jv_mem_realloc(state->files, (state->nfiles + 1) * sizeof(state->files[0]));
|
|
259
|
+
state->files[state->nfiles++] = jv_mem_strdup(fname);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
int jq_util_input_errors(jq_util_input_state *state) {
|
|
263
|
+
return state->failures;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
static const char *next_file(jq_util_input_state *state) {
|
|
267
|
+
if (state->curr_file < state->nfiles)
|
|
268
|
+
return state->files[state->curr_file++];
|
|
269
|
+
return NULL;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
static int jq_util_input_read_more(jq_util_input_state *state) {
|
|
273
|
+
if (!state->current_input || feof(state->current_input) || ferror(state->current_input)) {
|
|
274
|
+
if (state->current_input && ferror(state->current_input)) {
|
|
275
|
+
// System-level input error on the stream. It will be closed (below).
|
|
276
|
+
// TODO: report it. Can't use 'state->err_cb()' as it is hard-coded for
|
|
277
|
+
// 'open' related problems.
|
|
278
|
+
fprintf(stderr,"Input error: %s\n", strerror(errno));
|
|
279
|
+
}
|
|
280
|
+
if (state->current_input) {
|
|
281
|
+
if (state->current_input == stdin) {
|
|
282
|
+
clearerr(stdin); // perhaps we can read again; anyways, we don't fclose(stdin)
|
|
283
|
+
} else {
|
|
284
|
+
fclose(state->current_input);
|
|
285
|
+
}
|
|
286
|
+
state->current_input = NULL;
|
|
287
|
+
jv_free(state->current_filename);
|
|
288
|
+
state->current_filename = jv_invalid();
|
|
289
|
+
state->current_line = 0 ;
|
|
290
|
+
}
|
|
291
|
+
const char *f = next_file(state);
|
|
292
|
+
if (f != NULL) {
|
|
293
|
+
if (!strcmp(f, "-")) {
|
|
294
|
+
state->current_input = stdin;
|
|
295
|
+
state->current_filename = jv_string("<stdin>");
|
|
296
|
+
} else {
|
|
297
|
+
state->current_input = fopen(f, "r");
|
|
298
|
+
state->current_filename = jv_string(f);
|
|
299
|
+
if (!state->current_input) {
|
|
300
|
+
state->err_cb(state->err_cb_data, f);
|
|
301
|
+
state->failures++;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
state->current_line = 0;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
state->buf[0] = 0;
|
|
309
|
+
state->buf_valid_len = 0;
|
|
310
|
+
if (state->current_input) {
|
|
311
|
+
char *res;
|
|
312
|
+
memset(state->buf, 0, sizeof(state->buf));
|
|
313
|
+
|
|
314
|
+
while (!(res = fgets(state->buf, sizeof(state->buf), state->current_input)) &&
|
|
315
|
+
ferror(state->current_input) && errno == EINTR)
|
|
316
|
+
clearerr(state->current_input);
|
|
317
|
+
if (res == NULL) {
|
|
318
|
+
state->buf[0] = 0;
|
|
319
|
+
if (ferror(state->current_input))
|
|
320
|
+
state->failures++;
|
|
321
|
+
} else {
|
|
322
|
+
const char *p = memchr(state->buf, '\n', sizeof(state->buf));
|
|
323
|
+
|
|
324
|
+
if (p != NULL)
|
|
325
|
+
state->current_line++;
|
|
326
|
+
|
|
327
|
+
if (p == NULL && state->parser != NULL) {
|
|
328
|
+
/*
|
|
329
|
+
* There should be no NULs in JSON texts (but JSON text
|
|
330
|
+
* sequences are another story).
|
|
331
|
+
*/
|
|
332
|
+
state->buf_valid_len = strlen(state->buf);
|
|
333
|
+
} else if (p == NULL && feof(state->current_input)) {
|
|
334
|
+
size_t i;
|
|
335
|
+
|
|
336
|
+
/*
|
|
337
|
+
* XXX We can't know how many bytes we've read!
|
|
338
|
+
*
|
|
339
|
+
* We can't use getline() because there need not be any newlines
|
|
340
|
+
* in the input. The only entirely correct choices are: use
|
|
341
|
+
* fgetc() or fread(). Using fread() will complicate buffer
|
|
342
|
+
* management here.
|
|
343
|
+
*
|
|
344
|
+
* For now we guess how much fgets() read.
|
|
345
|
+
*/
|
|
346
|
+
for (p = state->buf, i = 0; i < sizeof(state->buf); i++) {
|
|
347
|
+
if (state->buf[i] != '\0')
|
|
348
|
+
p = &state->buf[i];
|
|
349
|
+
}
|
|
350
|
+
state->buf_valid_len = p - state->buf + 1;
|
|
351
|
+
} else if (p == NULL) {
|
|
352
|
+
state->buf_valid_len = sizeof(state->buf) - 1;
|
|
353
|
+
} else {
|
|
354
|
+
state->buf_valid_len = (p - state->buf) + 1;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return state->curr_file == state->nfiles &&
|
|
359
|
+
(!state->current_input || feof(state->current_input) || ferror(state->current_input));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
jv jq_util_input_next_input_cb(jq_state *jq, void *data) {
|
|
363
|
+
return jq_util_input_next_input((jq_util_input_state *)data);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Return the current_filename:current_line
|
|
367
|
+
jv jq_util_input_get_position(jq_state *jq) {
|
|
368
|
+
jq_input_cb cb = NULL;
|
|
369
|
+
void *cb_data = NULL;
|
|
370
|
+
jq_get_input_cb(jq, &cb, &cb_data);
|
|
371
|
+
assert(cb == jq_util_input_next_input_cb);
|
|
372
|
+
if (cb != jq_util_input_next_input_cb)
|
|
373
|
+
return jv_invalid_with_msg(jv_string("Invalid jq_util_input API usage"));
|
|
374
|
+
jq_util_input_state *s = (jq_util_input_state *)cb_data;
|
|
375
|
+
|
|
376
|
+
// We can't assert that current_filename is a string because if
|
|
377
|
+
// the error was a JSON parser error then we may not have set
|
|
378
|
+
// current_filename yet.
|
|
379
|
+
if (jv_get_kind(s->current_filename) != JV_KIND_STRING)
|
|
380
|
+
return jv_string("<unknown>");
|
|
381
|
+
|
|
382
|
+
jv v = jv_string_fmt("%s:%lu", jv_string_value(s->current_filename), (unsigned long)s->current_line);
|
|
383
|
+
return v;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
jv jq_util_input_get_current_filename(jq_state* jq) {
|
|
387
|
+
jq_input_cb cb=NULL;
|
|
388
|
+
void *cb_data=NULL;
|
|
389
|
+
jq_get_input_cb(jq, &cb, &cb_data);
|
|
390
|
+
if (cb != jq_util_input_next_input_cb)
|
|
391
|
+
return jv_invalid_with_msg(jv_string("Unknown input filename"));
|
|
392
|
+
jq_util_input_state *s = (jq_util_input_state *)cb_data;
|
|
393
|
+
jv v = jv_copy(s->current_filename);
|
|
394
|
+
return v;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
jv jq_util_input_get_current_line(jq_state* jq) {
|
|
398
|
+
jq_input_cb cb=NULL;
|
|
399
|
+
void *cb_data=NULL;
|
|
400
|
+
jq_get_input_cb(jq, &cb, &cb_data);
|
|
401
|
+
if (cb != jq_util_input_next_input_cb)
|
|
402
|
+
return jv_invalid_with_msg(jv_string("Unknown input line number"));
|
|
403
|
+
jq_util_input_state *s = (jq_util_input_state *)cb_data;
|
|
404
|
+
jv v = jv_number(s->current_line);
|
|
405
|
+
return v;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
// Blocks to read one more input from stdin and/or given files
|
|
410
|
+
// When slurping, it returns just one value
|
|
411
|
+
jv jq_util_input_next_input(jq_util_input_state *state) {
|
|
412
|
+
int is_last = 0;
|
|
413
|
+
jv value = jv_invalid(); // need more input
|
|
414
|
+
do {
|
|
415
|
+
if (state->parser == NULL) {
|
|
416
|
+
// Raw input
|
|
417
|
+
is_last = jq_util_input_read_more(state);
|
|
418
|
+
if (state->buf_valid_len == 0)
|
|
419
|
+
continue;
|
|
420
|
+
if (jv_is_valid(state->slurped)) {
|
|
421
|
+
// Slurped raw input
|
|
422
|
+
state->slurped = jv_string_concat(state->slurped, jv_string_sized(state->buf, state->buf_valid_len));
|
|
423
|
+
} else {
|
|
424
|
+
if (!jv_is_valid(value))
|
|
425
|
+
value = jv_string("");
|
|
426
|
+
if (state->buf[state->buf_valid_len-1] == '\n') {
|
|
427
|
+
// whole line
|
|
428
|
+
state->buf[state->buf_valid_len-1] = 0;
|
|
429
|
+
return jv_string_concat(value, jv_string_sized(state->buf, state->buf_valid_len-1));
|
|
430
|
+
}
|
|
431
|
+
value = jv_string_concat(value, jv_string_sized(state->buf, state->buf_valid_len));
|
|
432
|
+
state->buf[0] = '\0';
|
|
433
|
+
state->buf_valid_len = 0;
|
|
434
|
+
}
|
|
435
|
+
} else {
|
|
436
|
+
if (jv_parser_remaining(state->parser) == 0) {
|
|
437
|
+
is_last = jq_util_input_read_more(state);
|
|
438
|
+
if (is_last && state->buf_valid_len == 0) {
|
|
439
|
+
value = jv_invalid();
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
jv_parser_set_buf(state->parser, state->buf, state->buf_valid_len, !is_last);
|
|
443
|
+
}
|
|
444
|
+
value = jv_parser_next(state->parser);
|
|
445
|
+
if (jv_is_valid(state->slurped)) {
|
|
446
|
+
if (jv_is_valid(value)) {
|
|
447
|
+
state->slurped = jv_array_append(state->slurped, value);
|
|
448
|
+
value = jv_invalid();
|
|
449
|
+
} else if (jv_invalid_has_msg(jv_copy(value)))
|
|
450
|
+
return value; // Not slurped parsed input
|
|
451
|
+
} else if (jv_is_valid(value) || jv_invalid_has_msg(jv_copy(value))) {
|
|
452
|
+
return value;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
} while (!is_last);
|
|
456
|
+
|
|
457
|
+
if (jv_is_valid(state->slurped)) {
|
|
458
|
+
value = state->slurped;
|
|
459
|
+
state->slurped = jv_invalid();
|
|
460
|
+
}
|
|
461
|
+
return value;
|
|
462
|
+
}
|
package/deps/jq/util.h
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#ifndef UTIL_H
|
|
2
|
+
#define UTIL_H
|
|
3
|
+
|
|
4
|
+
#ifdef WIN32
|
|
5
|
+
/* For WriteFile() below */
|
|
6
|
+
#include <windows.h>
|
|
7
|
+
#include <io.h>
|
|
8
|
+
#include <processenv.h>
|
|
9
|
+
#include <shellapi.h>
|
|
10
|
+
#include <wchar.h>
|
|
11
|
+
#include <wtypes.h>
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
#include "jv.h"
|
|
15
|
+
|
|
16
|
+
#ifndef HAVE_MKSTEMP
|
|
17
|
+
int mkstemp(char *template);
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
jv expand_path(jv);
|
|
21
|
+
jv get_home(void);
|
|
22
|
+
jv jq_realpath(jv);
|
|
23
|
+
|
|
24
|
+
/*
|
|
25
|
+
* The Windows CRT and console are something else. In order for the
|
|
26
|
+
* console to get UTF-8 written to it correctly we have to bypass stdio
|
|
27
|
+
* completely. No amount of fflush()ing helps. If the first byte of a
|
|
28
|
+
* buffer being written with fwrite() is non-ASCII UTF-8 then the
|
|
29
|
+
* console misinterprets the byte sequence. But one must not
|
|
30
|
+
* WriteFile() if stdout is a file!1!!
|
|
31
|
+
*
|
|
32
|
+
* We carry knowledge of whether the FILE * is a tty everywhere we
|
|
33
|
+
* output to it just so we can write with WriteFile() if stdout is a
|
|
34
|
+
* console on WIN32.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
static void priv_fwrite(const char *s, size_t len, FILE *fout, int is_tty) {
|
|
38
|
+
#ifdef WIN32
|
|
39
|
+
if (is_tty)
|
|
40
|
+
WriteFile((HANDLE)_get_osfhandle(fileno(fout)), s, len, NULL, NULL);
|
|
41
|
+
else
|
|
42
|
+
fwrite(s, 1, len, fout);
|
|
43
|
+
#else
|
|
44
|
+
fwrite(s, 1, len, fout);
|
|
45
|
+
#endif
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const void *_jq_memmem(const void *haystack, size_t haystacklen,
|
|
49
|
+
const void *needle, size_t needlelen);
|
|
50
|
+
|
|
51
|
+
#ifndef MIN
|
|
52
|
+
#define MIN(a,b) \
|
|
53
|
+
({ __typeof__ (a) _a = (a); \
|
|
54
|
+
__typeof__ (b) _b = (b); \
|
|
55
|
+
_a < _b ? _a : _b; })
|
|
56
|
+
#endif
|
|
57
|
+
#ifndef MAX
|
|
58
|
+
#define MAX(a,b) \
|
|
59
|
+
({ __typeof__ (a) _a = (a); \
|
|
60
|
+
__typeof__ (b) _b = (b); \
|
|
61
|
+
_a > _b ? _a : _b; })
|
|
62
|
+
#endif
|
|
63
|
+
|
|
64
|
+
#endif /* UTIL_H */
|
package/deps/jq.gyp
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
'targets': [
|
|
3
|
+
{
|
|
4
|
+
"target_name": "jq",
|
|
5
|
+
"type": "none",
|
|
6
|
+
"actions": [
|
|
7
|
+
{
|
|
8
|
+
"action_name": "configure",
|
|
9
|
+
"inputs": [],
|
|
10
|
+
"action": [
|
|
11
|
+
"node", "../util/configure"
|
|
12
|
+
],
|
|
13
|
+
"conditions": [
|
|
14
|
+
[
|
|
15
|
+
'OS=="mac"',
|
|
16
|
+
{
|
|
17
|
+
'outputs': [
|
|
18
|
+
'deps/libjq/src/libjq.dylib',
|
|
19
|
+
'deps/libjq/src/libjq.1.dylib'
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
'outputs': [
|
|
24
|
+
'deps/jq/src/libjq.so',
|
|
25
|
+
'deps/jq/src/libjq.so.1',
|
|
26
|
+
'deps/jq/src/libjq.a'
|
|
27
|
+
],
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
package/index.d.ts
ADDED
package/jest.config.js
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const nativeJq = require('bindings')('jq-node-bindings')
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
exec: (object, filter) => {
|
|
5
|
+
try {
|
|
6
|
+
const data = nativeJq.exec(JSON.stringify(object), filter)
|
|
7
|
+
|
|
8
|
+
return data?.value;
|
|
9
|
+
} catch (err) {
|
|
10
|
+
return null
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@port-labs/jq-node-bindings",
|
|
3
|
+
"version": "v0.0.1",
|
|
4
|
+
"description": "Node.js bindings for JQ",
|
|
5
|
+
"jq-node-bindings": "0.0.1",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"configure": "node-gyp configure",
|
|
9
|
+
"build": "node-gyp build",
|
|
10
|
+
"install": "node-gyp rebuild",
|
|
11
|
+
"prepack": "npm run test",
|
|
12
|
+
"test": "jest"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"jq",
|
|
16
|
+
"binding"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git@github.com:port-labs/jq-node-bindings.git"
|
|
21
|
+
},
|
|
22
|
+
"contributors": [
|
|
23
|
+
{
|
|
24
|
+
"name": "Daniel Sinai",
|
|
25
|
+
"email": "daniel.sinai.main@gmail.com"
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"bluebird": "^3.5.3",
|
|
31
|
+
"jest": "^29.0.5",
|
|
32
|
+
"jest-junit": "^15.0.0",
|
|
33
|
+
"jsdoc": "^3.4.0",
|
|
34
|
+
"jshint": "^2.10.1",
|
|
35
|
+
"mocha": "^10.2.0",
|
|
36
|
+
"node-gyp": "^9.3.1",
|
|
37
|
+
"prettier": "^2.5.1",
|
|
38
|
+
"toolkit-jsdoc": "^1.0.0"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"bindings": "^1.3.1",
|
|
42
|
+
"jqts": "^0.0.8",
|
|
43
|
+
"nan": "^2.17.0"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=6.0.0"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<testsuites name="jest tests" tests="15" failures="0" errors="0" time="0.356">
|
|
3
|
+
<testsuite name="jq" errors="0" failures="0" skipped="0" timestamp="2023-02-15T20:09:42" time="0.316" tests="15">
|
|
4
|
+
<testcase classname="jq should break" name="jq should break" time="0.003">
|
|
5
|
+
</testcase>
|
|
6
|
+
<testcase classname="jq should break for invalid input" name="jq should break for invalid input" time="0.002">
|
|
7
|
+
</testcase>
|
|
8
|
+
<testcase classname="jq should break for invalid input" name="jq should break for invalid input" time="0.002">
|
|
9
|
+
</testcase>
|
|
10
|
+
<testcase classname="jq should break for invalid input" name="jq should break for invalid input" time="0.001">
|
|
11
|
+
</testcase>
|
|
12
|
+
<testcase classname="jq string should work" name="jq string should work" time="0">
|
|
13
|
+
</testcase>
|
|
14
|
+
<testcase classname="jq number should work" name="jq number should work" time="0.001">
|
|
15
|
+
</testcase>
|
|
16
|
+
<testcase classname="jq should return null" name="jq should return null" time="0.002">
|
|
17
|
+
</testcase>
|
|
18
|
+
<testcase classname="jq should return array with object" name="jq should return array with object" time="0">
|
|
19
|
+
</testcase>
|
|
20
|
+
<testcase classname="jq should return an item of an array" name="jq should return an item of an array" time="0.002">
|
|
21
|
+
</testcase>
|
|
22
|
+
<testcase classname="jq should return array with objects" name="jq should return array with objects" time="0">
|
|
23
|
+
</testcase>
|
|
24
|
+
<testcase classname="jq should return boolean" name="jq should return boolean" time="0.001">
|
|
25
|
+
</testcase>
|
|
26
|
+
<testcase classname="jq should return object" name="jq should return object" time="0">
|
|
27
|
+
</testcase>
|
|
28
|
+
<testcase classname="jq should return recursed obj" name="jq should return recursed obj" time="0.001">
|
|
29
|
+
</testcase>
|
|
30
|
+
<testcase classname="jq should return recursed obj" name="jq should return recursed obj" time="0">
|
|
31
|
+
</testcase>
|
|
32
|
+
<testcase classname="jq should return null on invalid json" name="jq should return null on invalid json" time="0">
|
|
33
|
+
</testcase>
|
|
34
|
+
</testsuite>
|
|
35
|
+
</testsuites>
|