@sanity-labs/groq-wasm 1.2.2 → 1.2.4
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/README.md +1 -1
- package/dist/index.browser.d.ts +8 -287
- package/dist/index.browser.js +15 -33
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +9 -2
- package/dist/index.js +13 -29
- package/dist/index.js.map +1 -1
- package/dist/index.lazy.d.ts +16 -22
- package/dist/index.lazy.js +40 -57
- package/dist/index.lazy.js.map +1 -1
- package/dist/public-zXzIJJvd.js +737 -0
- package/dist/public-zXzIJJvd.js.map +1 -0
- package/dist/validate-B-khA-A1.d.ts +307 -0
- package/package.json +12 -7
- package/dist/chunk-ZSHLLLLH.js +0 -571
- package/dist/chunk-ZSHLLLLH.js.map +0 -1
|
@@ -0,0 +1,737 @@
|
|
|
1
|
+
// wasm/groq_wasm.js
|
|
2
|
+
/**
|
|
3
|
+
* List every query parameter a GROQ query references, as JSON.
|
|
4
|
+
*
|
|
5
|
+
* Returns one entry per reference, ordered by `start`. Byte offsets are
|
|
6
|
+
* UTF-8 offsets into `query`; the JavaScript wrapper converts them to UTF-16
|
|
7
|
+
* code unit indices. Parameters bound by a custom function (`fn ns::f($p)`)
|
|
8
|
+
* are omitted inside that function's body.
|
|
9
|
+
* @param {string} query
|
|
10
|
+
* @returns {string}
|
|
11
|
+
*/
|
|
12
|
+
function extractParams$1(query) {
|
|
13
|
+
let deferred3_0;
|
|
14
|
+
let deferred3_1;
|
|
15
|
+
try {
|
|
16
|
+
const ptr0 = passStringToWasm0(query, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
17
|
+
const len0 = WASM_VECTOR_LEN;
|
|
18
|
+
const ret = wasm.extractParams(ptr0, len0);
|
|
19
|
+
var ptr2 = ret[0];
|
|
20
|
+
var len2 = ret[1];
|
|
21
|
+
if (ret[3]) {
|
|
22
|
+
ptr2 = 0;
|
|
23
|
+
len2 = 0;
|
|
24
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
25
|
+
}
|
|
26
|
+
deferred3_0 = ptr2;
|
|
27
|
+
deferred3_1 = len2;
|
|
28
|
+
return getStringFromWasm0(ptr2, len2);
|
|
29
|
+
} finally {
|
|
30
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Format a GROQ query.
|
|
35
|
+
*
|
|
36
|
+
* # Arguments
|
|
37
|
+
* * `query` - The GROQ query string to format
|
|
38
|
+
* * `width` - Maximum line width (default: 80)
|
|
39
|
+
*
|
|
40
|
+
* # Returns
|
|
41
|
+
* The formatted query string
|
|
42
|
+
* @param {string} query
|
|
43
|
+
* @param {number | null} [width]
|
|
44
|
+
* @returns {string}
|
|
45
|
+
*/
|
|
46
|
+
function format$1(query, width) {
|
|
47
|
+
let deferred4_0;
|
|
48
|
+
let deferred4_1;
|
|
49
|
+
try {
|
|
50
|
+
const val2 = +(isLikeNone(width) ? Number.MAX_SAFE_INTEGER : width >>> 0);
|
|
51
|
+
const ptr0 = passStringToWasm0(query, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
52
|
+
const len0 = WASM_VECTOR_LEN;
|
|
53
|
+
const ret = wasm.format(ptr0, len0, val2);
|
|
54
|
+
var ptr3 = ret[0];
|
|
55
|
+
var len3 = ret[1];
|
|
56
|
+
if (ret[3]) {
|
|
57
|
+
ptr3 = 0;
|
|
58
|
+
len3 = 0;
|
|
59
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
60
|
+
}
|
|
61
|
+
deferred4_0 = ptr3;
|
|
62
|
+
deferred4_1 = len3;
|
|
63
|
+
return getStringFromWasm0(ptr3, len3);
|
|
64
|
+
} finally {
|
|
65
|
+
wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Lint a GROQ query and return findings as JSON.
|
|
70
|
+
*
|
|
71
|
+
* # Arguments
|
|
72
|
+
* * `query` - The GROQ query string to lint
|
|
73
|
+
*
|
|
74
|
+
* # Returns
|
|
75
|
+
* A JSON string containing an array of findings
|
|
76
|
+
* @param {string} query
|
|
77
|
+
* @returns {string}
|
|
78
|
+
*/
|
|
79
|
+
function lint$1(query) {
|
|
80
|
+
let deferred3_0;
|
|
81
|
+
let deferred3_1;
|
|
82
|
+
try {
|
|
83
|
+
const ptr0 = passStringToWasm0(query, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
84
|
+
const len0 = WASM_VECTOR_LEN;
|
|
85
|
+
const ret = wasm.lint(ptr0, len0);
|
|
86
|
+
var ptr2 = ret[0];
|
|
87
|
+
var len2 = ret[1];
|
|
88
|
+
if (ret[3]) {
|
|
89
|
+
ptr2 = 0;
|
|
90
|
+
len2 = 0;
|
|
91
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
92
|
+
}
|
|
93
|
+
deferred3_0 = ptr2;
|
|
94
|
+
deferred3_1 = len2;
|
|
95
|
+
return getStringFromWasm0(ptr2, len2);
|
|
96
|
+
} finally {
|
|
97
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Parse a GROQ query and return a JSON validation result.
|
|
102
|
+
*
|
|
103
|
+
* Byte offsets in the JSON are UTF-8 offsets into `query`. The JavaScript
|
|
104
|
+
* wrapper converts them to UTF-16 code unit indices.
|
|
105
|
+
*
|
|
106
|
+
* Success is `{"ok":true}`. A syntax error is
|
|
107
|
+
* `{"ok":false,"message":"...","start":N,"end":M}` where `start` is inclusive,
|
|
108
|
+
* `end` is exclusive, and the two are equal when the parser reports a caret
|
|
109
|
+
* rather than a token span.
|
|
110
|
+
* @param {string} query
|
|
111
|
+
* @returns {string}
|
|
112
|
+
*/
|
|
113
|
+
function validate$1(query) {
|
|
114
|
+
let deferred2_0;
|
|
115
|
+
let deferred2_1;
|
|
116
|
+
try {
|
|
117
|
+
const ptr0 = passStringToWasm0(query, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
118
|
+
const len0 = WASM_VECTOR_LEN;
|
|
119
|
+
const ret = wasm.validate(ptr0, len0);
|
|
120
|
+
deferred2_0 = ret[0];
|
|
121
|
+
deferred2_1 = ret[1];
|
|
122
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
123
|
+
} finally {
|
|
124
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function __wbg_get_imports() {
|
|
128
|
+
return {
|
|
129
|
+
__proto__: null,
|
|
130
|
+
"./groq_wasm_bg.js": {
|
|
131
|
+
__proto__: null,
|
|
132
|
+
__wbg_error_757e9472f8410341: function(arg0, arg1) {
|
|
133
|
+
let deferred0_0;
|
|
134
|
+
let deferred0_1;
|
|
135
|
+
try {
|
|
136
|
+
deferred0_0 = arg0;
|
|
137
|
+
deferred0_1 = arg1;
|
|
138
|
+
console.error(getStringFromWasm0(arg0, arg1));
|
|
139
|
+
} finally {
|
|
140
|
+
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
__wbg_new_bb74001155cb8ca8: function() {
|
|
144
|
+
return /* @__PURE__ */ new Error();
|
|
145
|
+
},
|
|
146
|
+
__wbg_stack_575c4e0918581b9c: function(arg0, arg1) {
|
|
147
|
+
const ret = arg1.stack;
|
|
148
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
149
|
+
const len1 = WASM_VECTOR_LEN;
|
|
150
|
+
getDataViewMemory0().setInt32((arg0 >>> 0) + 4, len1, true);
|
|
151
|
+
getDataViewMemory0().setInt32((arg0 >>> 0) + 0, ptr1, true);
|
|
152
|
+
},
|
|
153
|
+
__wbindgen_generic_0000000000000001: function(arg0, arg1) {
|
|
154
|
+
return getStringFromWasm0(arg0, arg1);
|
|
155
|
+
},
|
|
156
|
+
__wbindgen_init_externref_table: function() {
|
|
157
|
+
const table = wasm.__wbindgen_externrefs;
|
|
158
|
+
table.set(0, void 0);
|
|
159
|
+
table.set(1024, void 0);
|
|
160
|
+
table.set(1025, null);
|
|
161
|
+
table.set(1026, true);
|
|
162
|
+
table.set(1027, false);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
let cachedDataViewMemory0 = null;
|
|
168
|
+
function getDataViewMemory0() {
|
|
169
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || cachedDataViewMemory0.buffer.detached === void 0 && cachedDataViewMemory0.buffer !== wasm.memory.buffer) cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
170
|
+
return cachedDataViewMemory0;
|
|
171
|
+
}
|
|
172
|
+
function getStringFromWasm0(ptr, len) {
|
|
173
|
+
ptr = ptr >>> 0;
|
|
174
|
+
return decodeText(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
175
|
+
}
|
|
176
|
+
let cachedUint8ArrayMemory0 = null;
|
|
177
|
+
function getUint8ArrayMemory0() {
|
|
178
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
179
|
+
return cachedUint8ArrayMemory0;
|
|
180
|
+
}
|
|
181
|
+
function isLikeNone(x) {
|
|
182
|
+
return x === void 0 || x === null;
|
|
183
|
+
}
|
|
184
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
185
|
+
if (realloc === void 0) {
|
|
186
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
187
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
188
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
189
|
+
WASM_VECTOR_LEN = buf.length;
|
|
190
|
+
return ptr;
|
|
191
|
+
}
|
|
192
|
+
let len = arg.length;
|
|
193
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
194
|
+
const mem = getUint8ArrayMemory0();
|
|
195
|
+
let offset = 0;
|
|
196
|
+
for (; offset < len; offset++) {
|
|
197
|
+
const code = arg.charCodeAt(offset);
|
|
198
|
+
if (code > 127) break;
|
|
199
|
+
mem[ptr + offset] = code;
|
|
200
|
+
}
|
|
201
|
+
if (offset !== len) {
|
|
202
|
+
if (offset !== 0) arg = arg.slice(offset);
|
|
203
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
204
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
205
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
206
|
+
offset += ret.written;
|
|
207
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
208
|
+
}
|
|
209
|
+
WASM_VECTOR_LEN = offset;
|
|
210
|
+
return ptr;
|
|
211
|
+
}
|
|
212
|
+
function takeFromExternrefTable0(idx) {
|
|
213
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
214
|
+
wasm.__externref_table_dealloc(idx);
|
|
215
|
+
return value;
|
|
216
|
+
}
|
|
217
|
+
let cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
218
|
+
cachedTextDecoder.decode();
|
|
219
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
220
|
+
let numBytesDecoded = 0;
|
|
221
|
+
function decodeText(bytes) {
|
|
222
|
+
numBytesDecoded += bytes.length;
|
|
223
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
224
|
+
cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
225
|
+
cachedTextDecoder.decode();
|
|
226
|
+
numBytesDecoded = bytes.length;
|
|
227
|
+
}
|
|
228
|
+
return cachedTextDecoder.decode(bytes);
|
|
229
|
+
}
|
|
230
|
+
const cachedTextEncoder = new TextEncoder();
|
|
231
|
+
if (!("encodeInto" in cachedTextEncoder)) cachedTextEncoder.encodeInto = function(arg, view) {
|
|
232
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
233
|
+
view.set(buf);
|
|
234
|
+
return {
|
|
235
|
+
read: arg.length,
|
|
236
|
+
written: buf.length
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
let WASM_VECTOR_LEN = 0;
|
|
240
|
+
let wasm;
|
|
241
|
+
function __wbg_finalize_init(instance, module) {
|
|
242
|
+
wasm = instance.exports;
|
|
243
|
+
cachedDataViewMemory0 = null;
|
|
244
|
+
cachedUint8ArrayMemory0 = null;
|
|
245
|
+
wasm.__wbindgen_start();
|
|
246
|
+
return wasm;
|
|
247
|
+
}
|
|
248
|
+
function initSync(module) {
|
|
249
|
+
if (wasm !== void 0) return wasm;
|
|
250
|
+
if (module !== void 0) {
|
|
251
|
+
if (Object.getPrototypeOf(module) === Object.prototype) ({module} = module);
|
|
252
|
+
else console.warn("using deprecated parameters for `initSync()`; pass a single object instead");
|
|
253
|
+
}
|
|
254
|
+
const imports = __wbg_get_imports();
|
|
255
|
+
if (!(module instanceof WebAssembly.Module)) module = new WebAssembly.Module(module);
|
|
256
|
+
return __wbg_finalize_init(new WebAssembly.Instance(module, imports), module);
|
|
257
|
+
}
|
|
258
|
+
// src/types.ts
|
|
259
|
+
/**
|
|
260
|
+
* Map WASM severity to our standard severity
|
|
261
|
+
*/
|
|
262
|
+
function mapSeverity(wasmSeverity) {
|
|
263
|
+
switch (wasmSeverity) {
|
|
264
|
+
case "high": return "error";
|
|
265
|
+
case "medium": return "warning";
|
|
266
|
+
case "low": return "info";
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Error from WASM operations
|
|
271
|
+
*/
|
|
272
|
+
var WasmError = class extends Error {
|
|
273
|
+
code;
|
|
274
|
+
constructor(message, code) {
|
|
275
|
+
super(message);
|
|
276
|
+
this.code = code;
|
|
277
|
+
this.name = "WasmError";
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Rule ID mapping from Rust (snake_case) to TS (kebab-case)
|
|
282
|
+
*/
|
|
283
|
+
const RULE_ID_MAP = {
|
|
284
|
+
join_in_filter: "join-in-filter",
|
|
285
|
+
join_to_get_id: "join-to-get-id",
|
|
286
|
+
computed_value_in_filter: "computed-value-in-filter",
|
|
287
|
+
match_on_id: "match-on-id",
|
|
288
|
+
order_on_expr: "order-on-expr",
|
|
289
|
+
deep_pagination: "deep-pagination",
|
|
290
|
+
large_pages: "large-pages",
|
|
291
|
+
non_literal_comparison: "non-literal-comparison",
|
|
292
|
+
count_in_correlated_subquery: "count-in-correlated-subquery",
|
|
293
|
+
very_large_query: "very-large-query",
|
|
294
|
+
extremely_large_query: "extremely-large-query",
|
|
295
|
+
many_joins: "many-joins",
|
|
296
|
+
unlimited_query: "unlimited-query"
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* Convert Rust rule ID to TS convention
|
|
300
|
+
*/
|
|
301
|
+
function mapRuleId(rustRuleId) {
|
|
302
|
+
return RULE_ID_MAP[rustRuleId] ?? rustRuleId.replaceAll("_", "-");
|
|
303
|
+
}
|
|
304
|
+
// src/wasm-loader.ts
|
|
305
|
+
/**
|
|
306
|
+
* WASM module runtime.
|
|
307
|
+
*
|
|
308
|
+
* Exposes the WASM-backed lint/format/validate calls and init helpers.
|
|
309
|
+
* Byte loading is platform-specific:
|
|
310
|
+
* - `index.ts` reads the file and calls `initWasm` synchronously (Node).
|
|
311
|
+
* - `index.browser.ts` fetches it with top-level await (browser).
|
|
312
|
+
* - `index.lazy.ts` does neither until `load()` or `initWasm()`.
|
|
313
|
+
*/
|
|
314
|
+
const NOT_INITIALIZED_MESSAGE = "GROQ WASM module is not initialized. Call load() or initWasm() before using this API.";
|
|
315
|
+
let initialized = false;
|
|
316
|
+
function isWasmInitialized() {
|
|
317
|
+
return initialized;
|
|
318
|
+
}
|
|
319
|
+
function initWasm(source) {
|
|
320
|
+
if (isResponse(source)) return initFromResponse(source);
|
|
321
|
+
initFromCompiled(source);
|
|
322
|
+
}
|
|
323
|
+
function callLint(query) {
|
|
324
|
+
assertInitialized();
|
|
325
|
+
return lint$1(query);
|
|
326
|
+
}
|
|
327
|
+
function callExtractParams(query) {
|
|
328
|
+
assertInitialized();
|
|
329
|
+
return extractParams$1(query);
|
|
330
|
+
}
|
|
331
|
+
function callFormat(query, width) {
|
|
332
|
+
assertInitialized();
|
|
333
|
+
return format$1(query, width ?? null);
|
|
334
|
+
}
|
|
335
|
+
function callValidate(query) {
|
|
336
|
+
assertInitialized();
|
|
337
|
+
return validate$1(query);
|
|
338
|
+
}
|
|
339
|
+
function assertInitialized() {
|
|
340
|
+
if (!initialized) throw new WasmError(NOT_INITIALIZED_MESSAGE, "NOT_INITIALIZED");
|
|
341
|
+
}
|
|
342
|
+
function isResponse(source) {
|
|
343
|
+
return "arrayBuffer" in source && typeof source.arrayBuffer === "function";
|
|
344
|
+
}
|
|
345
|
+
function initFromCompiled(source) {
|
|
346
|
+
if (initialized) return;
|
|
347
|
+
try {
|
|
348
|
+
initSync({ module: source });
|
|
349
|
+
} catch (error) {
|
|
350
|
+
throw new WasmError(`Failed to initialize WASM: ${error instanceof Error ? error.message : String(error)}`, "WASM_ERROR");
|
|
351
|
+
}
|
|
352
|
+
initialized = true;
|
|
353
|
+
}
|
|
354
|
+
async function initFromResponse(response) {
|
|
355
|
+
if (initialized) return;
|
|
356
|
+
if (!response.ok) throw new WasmError(`Failed to read GROQ WASM module${response.url ? ` from ${response.url}` : ""}: ${response.status} ${response.statusText}`, "WASM_ERROR");
|
|
357
|
+
initFromCompiled(await response.arrayBuffer());
|
|
358
|
+
}
|
|
359
|
+
// src/offsets.ts
|
|
360
|
+
/**
|
|
361
|
+
* Create a function that maps UTF-8 byte offsets in `text` to
|
|
362
|
+
* {@link TextPosition}s.
|
|
363
|
+
*
|
|
364
|
+
* The mapper resumes from the previous result, so calling it with ascending
|
|
365
|
+
* offsets walks `text` once. A smaller offset restarts from the beginning.
|
|
366
|
+
* An offset inside a code point resolves to the start of that code point, and
|
|
367
|
+
* an offset past the end resolves to the end of `text`.
|
|
368
|
+
*/
|
|
369
|
+
function createUtf8OffsetMapper(text) {
|
|
370
|
+
let index = 0;
|
|
371
|
+
let bytes = 0;
|
|
372
|
+
let line = 1;
|
|
373
|
+
let column = 1;
|
|
374
|
+
return (byteOffset) => {
|
|
375
|
+
if (byteOffset < bytes) {
|
|
376
|
+
index = 0;
|
|
377
|
+
bytes = 0;
|
|
378
|
+
line = 1;
|
|
379
|
+
column = 1;
|
|
380
|
+
}
|
|
381
|
+
while (index < text.length && bytes < byteOffset) {
|
|
382
|
+
const codePoint = text.codePointAt(index) ?? 0;
|
|
383
|
+
const byteWidth = utf8ByteLength(codePoint);
|
|
384
|
+
if (bytes + byteWidth > byteOffset) break;
|
|
385
|
+
const utf16Width = codePoint > 65535 ? 2 : 1;
|
|
386
|
+
bytes += byteWidth;
|
|
387
|
+
index += utf16Width;
|
|
388
|
+
if (codePoint === 10) {
|
|
389
|
+
line += 1;
|
|
390
|
+
column = 1;
|
|
391
|
+
} else column += utf16Width;
|
|
392
|
+
}
|
|
393
|
+
return {
|
|
394
|
+
index,
|
|
395
|
+
line,
|
|
396
|
+
column
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* UTF-8 byte length of one code point, matching `TextEncoder`.
|
|
402
|
+
*
|
|
403
|
+
* Unpaired surrogates fall in the 3-byte range. `TextEncoder` replaces each
|
|
404
|
+
* of them with U+FFFD, which is also 3 bytes.
|
|
405
|
+
*/
|
|
406
|
+
function utf8ByteLength(codePoint) {
|
|
407
|
+
if (codePoint <= 127) return 1;
|
|
408
|
+
if (codePoint <= 2047) return 2;
|
|
409
|
+
if (codePoint <= 65535) return 3;
|
|
410
|
+
return 4;
|
|
411
|
+
}
|
|
412
|
+
// src/params.ts
|
|
413
|
+
/**
|
|
414
|
+
* GROQ query parameter extraction via WASM.
|
|
415
|
+
*
|
|
416
|
+
* Wraps groq-parser's `ParseResult::referenced_params`. The parser reports
|
|
417
|
+
* UTF-8 byte offsets; this module converts them to UTF-16 code unit indices.
|
|
418
|
+
*/
|
|
419
|
+
/**
|
|
420
|
+
* List the query parameters (`$name`) a GROQ query references.
|
|
421
|
+
*
|
|
422
|
+
* Returns one entry per distinct name, in order of first reference. Each entry
|
|
423
|
+
* lists every reference to that name in source order. Names do not include
|
|
424
|
+
* the `$`, so they match the keys of the params object passed to a client.
|
|
425
|
+
*
|
|
426
|
+
* Parameters bound by a custom function (`fn ns::f($p) = ...`) are local to
|
|
427
|
+
* that function's body and are not reported from inside it. `$name` text
|
|
428
|
+
* inside string literals and comments is not a reference.
|
|
429
|
+
*
|
|
430
|
+
* Reference `start` / `end` are 0-based UTF-16 code unit indices into
|
|
431
|
+
* `query` (`end` is exclusive), so `query.slice(start, end)` is `"$name"`.
|
|
432
|
+
*
|
|
433
|
+
* An empty or whitespace-only query returns `[]`, like `lint('')`.
|
|
434
|
+
*
|
|
435
|
+
* @param query - The GROQ query string
|
|
436
|
+
* @returns Referenced parameters, in order of first reference
|
|
437
|
+
* @throws {WasmError} With code `PARSE_ERROR` if the query does not parse
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```typescript
|
|
441
|
+
* import {extractParams} from '@sanity-labs/groq-wasm'
|
|
442
|
+
*
|
|
443
|
+
* extractParams('*[_type == $type][0...$limit]').map((param) => param.name)
|
|
444
|
+
* // ['type', 'limit']
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
447
|
+
function extractParams(query) {
|
|
448
|
+
if (!query.trim()) return [];
|
|
449
|
+
let resultJson;
|
|
450
|
+
try {
|
|
451
|
+
resultJson = callExtractParams(query);
|
|
452
|
+
} catch (error) {
|
|
453
|
+
if (error instanceof WasmError) throw error;
|
|
454
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
455
|
+
if (message.includes("parse") || message.includes("Parse")) throw new WasmError(`Failed to parse query: ${message}`, "PARSE_ERROR");
|
|
456
|
+
throw new WasmError(`Extract params failed: ${message}`, "WASM_ERROR");
|
|
457
|
+
}
|
|
458
|
+
const wasmReferences = JSON.parse(resultJson);
|
|
459
|
+
const toPosition = createUtf8OffsetMapper(query);
|
|
460
|
+
const params = /* @__PURE__ */ new Map();
|
|
461
|
+
for (const reference of wasmReferences) {
|
|
462
|
+
const start = toPosition(reference.start);
|
|
463
|
+
const end = toPosition(reference.end);
|
|
464
|
+
let param = params.get(reference.name);
|
|
465
|
+
if (!param) {
|
|
466
|
+
param = {
|
|
467
|
+
name: reference.name,
|
|
468
|
+
references: []
|
|
469
|
+
};
|
|
470
|
+
params.set(reference.name, param);
|
|
471
|
+
}
|
|
472
|
+
param.references.push({
|
|
473
|
+
start: start.index,
|
|
474
|
+
end: end.index,
|
|
475
|
+
line: start.line,
|
|
476
|
+
column: start.column,
|
|
477
|
+
endLine: end.line,
|
|
478
|
+
endColumn: end.column
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
return [...params.values()];
|
|
482
|
+
}
|
|
483
|
+
// src/lint.ts
|
|
484
|
+
/**
|
|
485
|
+
* Lint a GROQ query using the WASM-based linter.
|
|
486
|
+
*
|
|
487
|
+
* Finding spans are source locations in JavaScript string coordinates.
|
|
488
|
+
* The Rust linter reports UTF-8 byte offsets; `line`, `column`, and `offset`
|
|
489
|
+
* below are converted from those bytes. `column` counts UTF-16 code units.
|
|
490
|
+
*
|
|
491
|
+
* @param query - The GROQ query string to lint
|
|
492
|
+
* @param config - Optional configuration
|
|
493
|
+
* @returns Array of findings
|
|
494
|
+
* @throws {WasmError} If query parsing fails
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* ```typescript
|
|
498
|
+
* import {lint} from '@sanity-labs/groq-wasm'
|
|
499
|
+
*
|
|
500
|
+
* const findings = lint('*[_type == "post"]{ author-> }')
|
|
501
|
+
* // [{ ruleId: 'join-in-filter', message: '...', severity: 'error' }]
|
|
502
|
+
* ```
|
|
503
|
+
*/
|
|
504
|
+
function lint(query, config) {
|
|
505
|
+
if (!query.trim()) return [];
|
|
506
|
+
try {
|
|
507
|
+
const resultJson = callLint(query);
|
|
508
|
+
let findings = JSON.parse(resultJson);
|
|
509
|
+
if (config?.rules) {
|
|
510
|
+
const rules = config.rules;
|
|
511
|
+
findings = findings.filter((f) => {
|
|
512
|
+
return rules[f.ruleId] !== false;
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
return findings.map((wf) => convertFinding(wf, query));
|
|
516
|
+
} catch (error) {
|
|
517
|
+
if (error instanceof WasmError) throw error;
|
|
518
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
519
|
+
if (message.includes("parse") || message.includes("Parse")) throw new WasmError(`Failed to parse query: ${message}`, "PARSE_ERROR");
|
|
520
|
+
throw new WasmError(`Lint failed: ${message}`, "WASM_ERROR");
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Convert WASM finding to our Finding type
|
|
525
|
+
*/
|
|
526
|
+
function convertFinding(wf, query) {
|
|
527
|
+
return {
|
|
528
|
+
ruleId: mapRuleId(wf.ruleId),
|
|
529
|
+
message: wf.message,
|
|
530
|
+
severity: mapSeverity(wf.severity),
|
|
531
|
+
span: byteSpanToSourceSpan(wf.start, wf.end, query)
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Convert UTF-8 byte offsets to a line/column source span.
|
|
536
|
+
*
|
|
537
|
+
* `column` is a 1-based count of UTF-16 code units and `offset` is the
|
|
538
|
+
* matching JavaScript string index. See {@link createUtf8OffsetMapper}.
|
|
539
|
+
*/
|
|
540
|
+
function byteSpanToSourceSpan(startByte, endByte, query) {
|
|
541
|
+
const toPosition = createUtf8OffsetMapper(query);
|
|
542
|
+
const start = toPosition(startByte);
|
|
543
|
+
const end = toPosition(endByte);
|
|
544
|
+
return {
|
|
545
|
+
start: {
|
|
546
|
+
line: start.line,
|
|
547
|
+
column: start.column,
|
|
548
|
+
offset: start.index
|
|
549
|
+
},
|
|
550
|
+
end: {
|
|
551
|
+
line: end.line,
|
|
552
|
+
column: end.column,
|
|
553
|
+
offset: end.index
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
// src/format.ts
|
|
558
|
+
/**
|
|
559
|
+
* GROQ Formatting via WASM
|
|
560
|
+
*
|
|
561
|
+
* Wraps the Rust groq-format library compiled to WASM.
|
|
562
|
+
*/
|
|
563
|
+
/**
|
|
564
|
+
* Default line width for formatting
|
|
565
|
+
* Matches Rust groq-format DEFAULT_WIDTH
|
|
566
|
+
*/
|
|
567
|
+
const DEFAULT_WIDTH = 80;
|
|
568
|
+
/**
|
|
569
|
+
* Format a GROQ query using the WASM-based formatter
|
|
570
|
+
*
|
|
571
|
+
* A document of only `fn` definitions, such as a file of shared custom functions, is formatted
|
|
572
|
+
* too, although GROQ's parser requires a query to end with an expression.
|
|
573
|
+
*
|
|
574
|
+
* @param query - The GROQ query string to format
|
|
575
|
+
* @param config - Optional configuration (width defaults to 80)
|
|
576
|
+
* @returns Formatted query string
|
|
577
|
+
* @throws {WasmError} If query parsing fails
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```typescript
|
|
581
|
+
* import {format} from '@sanity-labs/groq-wasm'
|
|
582
|
+
*
|
|
583
|
+
* const formatted = format('*[_type=="post"]{title,body}')
|
|
584
|
+
* // '*[_type == "post"]{ title, body }'
|
|
585
|
+
* ```
|
|
586
|
+
*/
|
|
587
|
+
function format(query, config) {
|
|
588
|
+
const width = config?.width ?? 80;
|
|
589
|
+
if (!query.trim()) return query;
|
|
590
|
+
try {
|
|
591
|
+
return callFormat(query, width);
|
|
592
|
+
} catch (error) {
|
|
593
|
+
if (error instanceof WasmError) throw error;
|
|
594
|
+
const definitions = formatDefinitionsOnly(query, width);
|
|
595
|
+
if (definitions !== void 0) return definitions;
|
|
596
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
597
|
+
if (message.includes("parse") || message.includes("Parse")) throw new WasmError(`Failed to parse query: ${message}`, "PARSE_ERROR");
|
|
598
|
+
throw new WasmError(`Format failed: ${message}`, "WASM_ERROR");
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
const SENTINEL_HEADER = "fn groqwasm::definitionsonly(";
|
|
602
|
+
const SENTINEL = `\n${SENTINEL_HEADER}$x) = $x{};\nnull`;
|
|
603
|
+
/**
|
|
604
|
+
* `query` formatted, when it is only `fn` definitions (and comments), like a file of shared custom
|
|
605
|
+
* functions. GROQ's parser requires a query to end with an expression, so the definitions are
|
|
606
|
+
* formatted followed by one more definition and `null`, which are then cut off. A definition can
|
|
607
|
+
* only follow definitions, so that parses only when everything before it is complete definitions.
|
|
608
|
+
*/
|
|
609
|
+
function formatDefinitionsOnly(query, width) {
|
|
610
|
+
if (!/^fn\s/.test(query.replace(/^(?:\s|\/\/[^\n]*)*/, ""))) return;
|
|
611
|
+
let formatted;
|
|
612
|
+
try {
|
|
613
|
+
formatted = callFormat(`${query}${SENTINEL}`, width);
|
|
614
|
+
} catch {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
const sentinel = formatted.lastIndexOf(`\n${SENTINEL_HEADER}`);
|
|
618
|
+
return sentinel === -1 ? void 0 : formatted.slice(0, sentinel).trimEnd();
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Check if a query can be parsed (without full formatting)
|
|
622
|
+
*
|
|
623
|
+
* @param query - The GROQ query to validate
|
|
624
|
+
* @returns true if the query is valid GROQ syntax
|
|
625
|
+
*/
|
|
626
|
+
function isValidSyntax(query) {
|
|
627
|
+
try {
|
|
628
|
+
format(query);
|
|
629
|
+
return true;
|
|
630
|
+
} catch (error) {
|
|
631
|
+
if (error instanceof WasmError && error.code === "PARSE_ERROR") return false;
|
|
632
|
+
throw error;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
// src/validate.ts
|
|
636
|
+
/**
|
|
637
|
+
* GROQ syntax validation via WASM.
|
|
638
|
+
*
|
|
639
|
+
* Wraps groq-parser's structured parse errors. The parser reports UTF-8 byte
|
|
640
|
+
* offsets; this module converts them to UTF-16 code unit indices.
|
|
641
|
+
*/
|
|
642
|
+
/**
|
|
643
|
+
* Parse a GROQ query.
|
|
644
|
+
*
|
|
645
|
+
* Returns `{ok: true}` when the query parses. Syntax errors are returned as
|
|
646
|
+
* `{ok: false, error}` and are not thrown. `lint` and `format` still throw
|
|
647
|
+
* {@link WasmError} on invalid syntax; this function does not change that.
|
|
648
|
+
*
|
|
649
|
+
* `error.start` and `error.end` are 0-based UTF-16 code unit indices into
|
|
650
|
+
* `query` (`end` is exclusive), which is what JavaScript strings and editors
|
|
651
|
+
* such as Vision use. groq-parser's `ParseError` positions are UTF-8 byte
|
|
652
|
+
* offsets (`Position.start` / `Position.end` in groq-parser-rs). An empty or
|
|
653
|
+
* whitespace-only query is a syntax error (`"no query"`) at offset 0, unlike
|
|
654
|
+
* `lint('')` and `format('')`, which treat it as nothing to do.
|
|
655
|
+
*
|
|
656
|
+
* `line` / `column` locate `start`, and `endLine` / `endColumn` locate `end`.
|
|
657
|
+
* They are 1-based. Columns count UTF-16 code units, matching `SourceLocation`
|
|
658
|
+
* on lint findings.
|
|
659
|
+
*
|
|
660
|
+
* Parameter references (`$name`) are accepted. This is a syntax check and does
|
|
661
|
+
* not require the parameters to be declared.
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* ```typescript
|
|
665
|
+
* import {validate} from '@sanity-labs/groq-wasm'
|
|
666
|
+
*
|
|
667
|
+
* validate('*[_type == "post"]')
|
|
668
|
+
* // {ok: true}
|
|
669
|
+
*
|
|
670
|
+
* validate('*["é" == ]')
|
|
671
|
+
* // {
|
|
672
|
+
* // ok: false,
|
|
673
|
+
* // error: {
|
|
674
|
+
* // message: 'unexpected token "]", expected expression',
|
|
675
|
+
* // start: 9,
|
|
676
|
+
* // end: 10,
|
|
677
|
+
* // line: 1,
|
|
678
|
+
* // column: 10,
|
|
679
|
+
* // endLine: 1,
|
|
680
|
+
* // endColumn: 11,
|
|
681
|
+
* // },
|
|
682
|
+
* // }
|
|
683
|
+
* ```
|
|
684
|
+
*/
|
|
685
|
+
function validate(query) {
|
|
686
|
+
let parsed;
|
|
687
|
+
try {
|
|
688
|
+
parsed = JSON.parse(callValidate(query));
|
|
689
|
+
} catch (error) {
|
|
690
|
+
if (error instanceof WasmError) throw error;
|
|
691
|
+
throw new WasmError(`Validate failed: ${error instanceof Error ? error.message : String(error)}`, "WASM_ERROR");
|
|
692
|
+
}
|
|
693
|
+
const payload = readValidatePayload(parsed);
|
|
694
|
+
switch (payload.ok) {
|
|
695
|
+
case true: return { ok: true };
|
|
696
|
+
case false: return {
|
|
697
|
+
ok: false,
|
|
698
|
+
error: toValidateError(query, payload)
|
|
699
|
+
};
|
|
700
|
+
default: throw new WasmError(`Unexpected validate result: ${JSON.stringify(payload)}`, "WASM_ERROR");
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
function readValidatePayload(value) {
|
|
704
|
+
if (!isRecord(value)) throw new WasmError("Validate returned an invalid payload", "WASM_ERROR");
|
|
705
|
+
const ok = value["ok"];
|
|
706
|
+
if (ok === true) return { ok: true };
|
|
707
|
+
const message = value["message"];
|
|
708
|
+
const start = value["start"];
|
|
709
|
+
const end = value["end"];
|
|
710
|
+
if (ok === false && typeof message === "string" && typeof start === "number" && typeof end === "number") return {
|
|
711
|
+
ok: false,
|
|
712
|
+
message,
|
|
713
|
+
start,
|
|
714
|
+
end
|
|
715
|
+
};
|
|
716
|
+
throw new WasmError("Validate returned an invalid payload", "WASM_ERROR");
|
|
717
|
+
}
|
|
718
|
+
function toValidateError(query, failure) {
|
|
719
|
+
const toPosition = createUtf8OffsetMapper(query);
|
|
720
|
+
const start = toPosition(Math.max(0, failure.start));
|
|
721
|
+
const end = toPosition(Math.max(failure.start, failure.end));
|
|
722
|
+
return {
|
|
723
|
+
message: failure.message,
|
|
724
|
+
start: start.index,
|
|
725
|
+
end: end.index,
|
|
726
|
+
line: start.line,
|
|
727
|
+
column: start.column,
|
|
728
|
+
endLine: end.line,
|
|
729
|
+
endColumn: end.column
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
function isRecord(value) {
|
|
733
|
+
return typeof value === "object" && value !== null;
|
|
734
|
+
}
|
|
735
|
+
export { lint as a, isWasmInitialized as c, mapRuleId as d, mapSeverity as f, isValidSyntax as i, RULE_ID_MAP as l, DEFAULT_WIDTH as n, extractParams as o, format as r, initWasm as s, validate as t, WasmError as u };
|
|
736
|
+
|
|
737
|
+
//# sourceMappingURL=public-zXzIJJvd.js.map
|