@brimdata/zealot
Version:
The Javascript Client for Zed Lakes
206 lines (205 loc) • 6.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Client", {
enumerable: true,
get: ()=>Client
});
const _eventSourcePolyfill = require("event-source-polyfill");
const _resultStream = require("../query/result-stream");
const _error = require("../util/error");
const _utils = require("./utils");
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class Client {
async version() {
const r = await this.send({
method: "GET",
path: "/version"
});
return await r.json();
}
async authMethod() {
const r = await this.send({
method: "GET",
path: "/auth/method"
});
return (0, _utils.toJS)(r);
}
async load(data, opts = {}) {
const { pool } = opts;
if (!pool) throw new Error("Missing required option 'pool'");
const poolId = typeof pool === "string" ? pool : pool.id;
const branch = opts.branch || "main";
let headers = {};
if (opts.message) headers["Zed-Commit"] = (0, _utils.json)(opts.message);
const res = await this.send({
path: `/pool/${poolId}/branch/${encodeURIComponent(branch)}`,
method: "POST",
body: data,
headers,
contentType: (0, _utils.getLoadContentType)(opts.format) ?? "",
signal: opts.signal,
timeout: Infinity
});
return (0, _utils.toJS)(res);
}
async query(query, opts = {}) {
const options = (0, _utils.defaults)(opts, {
format: "zjson",
controlMessages: true
});
const abortCtl = (0, _utils.wrapAbort)(options.signal);
const result = await this.send({
method: "POST",
path: `/query?ctrl=${options.controlMessages}`,
body: (0, _utils.json)({
query
}),
contentType: "application/json",
format: options.format,
signal: abortCtl.signal,
timeout: options.timeout
});
return new _resultStream.ResultStream(result, abortCtl);
}
async createPool(name, opts = {}) {
const options = (0, _utils.defaults)(opts, {
order: "desc",
key: "ts"
});
// @ts-ignore
const keys = [
[].concat(options.key)
];
const layout = {
order: options.order,
keys
};
return this.send({
method: "POST",
path: "/pool",
body: (0, _utils.json)({
name,
layout
}),
contentType: "application/json"
}).then(_utils.toJS);
}
async deletePool(poolId) {
await this.send({
method: "DELETE",
path: `/pool/${poolId}`
});
return true;
}
async getPools() {
const resp = await this.query("from :pools");
return resp.js();
}
async getPool(nameOrId) {
const res = await this.query(`from :pools | id == ${nameOrId} or name == "${nameOrId}"`);
const values = await res.js();
if (!values || values.length == 0) throw new Error(`Pool Not Found: ${nameOrId}`);
return values[0];
}
async getPoolStats(poolId) {
const res = await this.send({
method: "GET",
path: `/pool/${poolId}/stats`
});
return (0, _utils.toJS)(res);
}
async updatePool(poolId, args) {
await this.send({
method: "PUT",
path: `/pool/${poolId}`,
body: (0, _utils.json)(args),
contentType: "application/json"
});
return true;
}
subscribe() {
return new _eventSourcePolyfill.EventSourcePolyfill(this.baseURL + "/events", {
headers: {
Accept: "application/json"
}
});
}
curl(query, opts = {}) {
const options = (0, _utils.defaults)(opts, {
format: "zjson",
controlMessages: true
});
return `curl -X POST -d '${JSON.stringify({
query
})}' \\
-H "Accept: ${(0, _utils.accept)(options.format)}" \\
-H "Content-Type: application/json" \\
${this.baseURL}/query`;
}
async send(opts) {
const abortCtl = (0, _utils.wrapAbort)(opts.signal);
const clearTimer = this.setTimeout(()=>{
console.error("request timed out:", opts);
abortCtl.abort();
}, opts.timeout);
const headers = {
...opts.headers
};
headers["Accept"] = (0, _utils.accept)(opts.format || "zjson");
if (opts.contentType !== undefined) {
headers["Content-Type"] = opts.contentType;
}
if (this.auth) {
headers["Authorization"] = `Bearer ${this.auth}`;
}
const resp = await Client.fetch(this.baseURL + opts.path, {
method: opts.method,
signal: abortCtl.signal,
headers: headers,
// @ts-ignore
body: opts.body
});
clearTimer();
if (resp.ok) {
return resp;
} else {
return Promise.reject((0, _error.createError)(await (0, _utils.parseContent)(resp)));
}
}
setTimeout(fn, ms) {
if (ms === Infinity) return ()=>{};
if (ms === undefined) ms = this.timeout;
const id = setTimeout(fn, ms);
return ()=>clearTimeout(id);
}
constructor(baseURL, opts = {}){
_defineProperty(this, "baseURL", void 0);
_defineProperty(this, "auth", void 0);
_defineProperty(this, "timeout", void 0);
this.baseURL = baseURL;
this.timeout = 60000;
const defaults = {
auth: null
};
const options = {
...defaults,
...opts
};
this.auth = options.auth || null;
}
}
_defineProperty(Client, "fetch", void 0);