-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (68 loc) · 2.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import "./lib/wasm_exec.js";
import "./lib/bridge.js";
export class SuperDB {
/**
* Instantiates a new `SuperDB` instance from a given wasm file URL.
* @static
* @param {string} url - The URL of the wasm file to instantiate.
* @returns {Promise<SuperDB>} A promise that resolves to a new `SuperDB` instance.
*/
static instantiate(url) {
return this.fetchCompressed(url)
.then((resp) => this.createInstance(resp))
.then((instance) => new SuperDB(instance));
}
/**
* @private
*/
static fetchCompressed(url) {
const headers = { "Content-Type": "application/wasm" };
const gzip = new DecompressionStream("gzip");
return fetch(url)
.then((resp) => resp.blob())
.then((blob) => blob.stream().pipeThrough(gzip))
.then((stream) => new Response(stream, { headers }));
}
/**
* @private
*/
static async createInstance(response) {
const go = new Go();
const env = go.importObject;
const wasm = await WebAssembly.instantiateStreaming(response, env);
go.run(wasm.instance);
return __go_wasm__;
}
/**
* @private
*/
constructor(instance) {
this.instance = instance;
}
/**
* Executes a query using the provided options and returns the result.
* @async
* @param {Object} opts - The options for the query.
* @param {string} [opts.query] - The ZQ program to execute.
* @param {string | ReadableStream} [opts.input] - The input data for the query.
* @param {'auto' | 'arrows' | 'csv' | 'json' | 'line' | 'parquet' | 'tsv' | 'vng' | 'zeek' | 'zjson' | 'zng' | 'zson'} [opts.inputFormat] - The format of the input data.
* @param {'auto' | 'arrows' | 'csv' | 'json' | 'line' | 'parquet' | 'tsv' | 'vng' | 'zeek' | 'zjson' | 'zng' | 'zson'} [opts.outputFormat] - The desired format of the output data.
* @returns {Promise<any[]>} A promise that resolves to the processed query result.
*/
run(args) {
return this.instance.zq({
input: args.input,
inputFormat: args.inputFormat,
program: args.query,
outputFormat: args.outputFormat,
});
}
/**
* Parses the given query string and returns the result.
* @param {string} query - The query string to parse.
* @returns {any} The parsed result.
*/
parse(query) {
return this.instance.parse(query);
}
}