UNPKG

casualos

Version:
132 lines (129 loc) 4.51 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var casualos_cli_exports = {}; __export(casualos_cli_exports, { RecordsClient: () => RecordsClient }); module.exports = __toCommonJS(casualos_cli_exports); // ../aux-records/RecordsClient.js var RecordsClient = class { get sessionKey() { return this._sessionKey; } set sessionKey(value) { this._sessionKey = value; } get endpoint() { return this._endpoint; } constructor(endpoint) { this._endpoint = endpoint; this._sessionKey = null; Object.defineProperties(this, { then: { value: void 0, configurable: false, writable: false }, catch: { value: void 0, configurable: false, writable: false } }); } /** * Calls the procedure with the given name, using the given argument. * @param name The name of the procedure to call. * @param input The input to the procedure. * @param options The options to use for the procedure. * @param query The query to use for the procedure. */ async callProcedure(name, input, options, query) { var _a, _b, _c, _d; const response = await fetch(`${(_a = options === null || options === void 0 ? void 0 : options.endpoint) !== null && _a !== void 0 ? _a : this._endpoint}/api/v3/callProcedure`, { method: "POST", body: JSON.stringify({ procedure: name, input, query }), headers: { "Content-Type": "application/json;charset=UTF-8", Accept: "application/json,application/x-ndjson", ...(_b = options === null || options === void 0 ? void 0 : options.headers) !== null && _b !== void 0 ? _b : {}, ...this._authenticationHeaders(options) } }); if (((_d = (_c = response.headers) === null || _c === void 0 ? void 0 : _c.get("Content-Type")) === null || _d === void 0 ? void 0 : _d.indexOf("application/x-ndjson")) >= 0) { return streamJsonLines(response.body, new TextDecoder()); } else { return await response.json(); } } _authenticationHeaders(options) { var _a; const key = (_a = options === null || options === void 0 ? void 0 : options.sessionKey) !== null && _a !== void 0 ? _a : this._sessionKey; if (key) { return { Authorization: `Bearer ${key}` }; } else { return {}; } } }; async function* streamJsonLines(stream, decoder) { let buffer = ""; const reader = stream.getReader(); while (true) { const { done, value } = await reader.read(); if (done) { if (buffer.length > 0) { return JSON.parse(buffer); } break; } let chunk = decoder.decode(value, { stream: true }); let newlinePosition; while ((newlinePosition = chunk.indexOf("\n")) >= 0) { let carriageReturnPosition = chunk.indexOf("\r", newlinePosition - 1); if (carriageReturnPosition >= 0) { const beforeCarriageReturn = chunk.substring(0, carriageReturnPosition); if (buffer.length + beforeCarriageReturn.length > 0) { const jsonLine = buffer + beforeCarriageReturn; yield JSON.parse(jsonLine); } const afterNewline = chunk.substring(carriageReturnPosition + 2); chunk = afterNewline; buffer = ""; } else { const beforeNewline = chunk.substring(0, newlinePosition); if (buffer.length + beforeNewline.length > 0) { const jsonLine = buffer + beforeNewline; yield JSON.parse(jsonLine); } const afterNewline = chunk.substring(newlinePosition + 1); chunk = afterNewline; buffer = ""; } } buffer += chunk; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RecordsClient });