@graphistry/js-upload-api
Version:
Graphistry upload client for reuse by node and browser clients
266 lines • 15.7 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { AbstractClient } from './AbstractClient.js';
var AUTH_API_ENDPOINT = 'api/v2/auth/token/generate';
/**
* # Client examples
*
* Authenticate against a Graphistry server using a username and password, and manage communications with it.
*
* Different authentication modes may be desirable depending on the type of Graphistry server.
*
* <br>
*
* ---
*
* <br>
*
* @example **Authenticate against Graphistry Hub for a personal account**
* ```javascript
* import { Client } from '@graphistry/node-api';
* const client = new Client('my_username', 'my_password');
* ```
*
* <br>
*
* @example **Authenticate against an org in Graphistry Hub**
* ```javascript
* import { Client } from '@graphistry/node-api';
* const client = new Client('my_username', 'my_password', 'my_org');
* ```
*
* <br>
*
* @example **Authenticate against a private Graphistry server**
* ```javascript
* import { Client } from '@graphistry/node-api';
* const client = new Client('my_username', 'my_password', '', 'http', 'my-ec2.aws.com:8080');
* ```
*
* <br>
*
* @example **Upload via internal IP but publish urls via a public domain**
* ```javascript
* import { Client } from '@graphistry/node-api';
* const client = new Client(
* 'my_username', 'my_password', '',
* 'http', '10.20.0.1:8080',
* 'https://www.my-site.com'
* );
* ```
*
* <br>
*
* @example **Upload through the local docker network but publish urls via a public domain**
* ```javascript
* import { Client } from '@graphistry/node-api';
* const client = new Client(
* 'my_username', 'my_password', '',
* 'http', 'nginx',
* 'https://www.my-site.com'
* );
* ```
*
* <br>
*
* @example **Create a client with an externally-provided JWT token**
* ```javascript
* import { Client } from '@graphistry/node-api';
* const client = new Client();
* client.setToken('Bearer 123abc');
* ```
*/
var Client = /** @class */ (function (_super) {
__extends(Client, _super);
/**
*
* See examples at top of file
*
* @param username The username to authenticate with.
* @param password The password to authenticate with.
* @param org The organization to use (optional)
* @param protocol The protocol to use for the server during uploads: 'http' or 'https'.
* @param host The hostname of the server during uploads: defaults to 'hub.graphistry.com'
* @param clientProtocolHostname Base path to use inside the browser and when sharing public URLs: defaults to '{protocol}://{host}'
* @param fetch The fetch implementation to use
* @param version The version of the client library
* @param agent The agent name to use when communicating with the server
*/
function Client(username, password, org, protocol, host, clientProtocolHostname, fetch, // eslint-disable-line @typescript-eslint/no-explicit-any
version, agent) {
if (protocol === void 0) { protocol = 'https'; }
if (host === void 0) { host = 'hub.graphistry.com'; }
if (agent === void 0) { agent = '@graphistry/js-upload-api'; }
var _this = _super.call(this, org, protocol, host, clientProtocolHostname, fetch, version, agent) || this;
_this.username = username;
_this._password = password;
if (_this.isServerConfigured()) {
_this._getAuthTokenPromise = _this.getAuthToken();
}
return _this;
}
Client.prototype.isServerConfigured = function () {
return (this.username || '') !== '' && (this._password || '') !== '' && (this.host || '') !== '';
};
Client.prototype.checkStale = function (username, password, protocol, host, clientProtocolHostname) {
// username changed
if (this.username !== username) {
return true;
}
// password changed
if (this._password !== password) {
return true;
}
// protocol changed
if (this.protocol !== protocol) {
return true;
}
// host changed
if (this.host !== host) {
return true;
}
// clientProtocolHostname changed
if (this.clientProtocolHostname !== clientProtocolHostname) {
return true;
}
return false;
};
/**
* Get the authentication token for the current user.
* By default, reuses current token if available.
*
* @param force If true, forces a new token to be generated; defaults to false
*
* @returns The authentication token
*
*/
Client.prototype.getAuthToken = function (force) {
if (force === void 0) { force = false; }
return __awaiter(this, void 0, void 0, function () {
var response, tok;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!force && this.authTokenValid()) {
return [2 /*return*/, this._token || '']; // workaround ts not recognizing that _token is set
}
//Throw exception if invalid username or password
if (!this.isServerConfigured()) {
throw new Error('Invalid username or password');
}
if (!(!force && this._getAuthTokenPromise)) return [3 /*break*/, 2];
return [4 /*yield*/, this._getAuthTokenPromise];
case 1:
// reusing outstanding auth promise
return [2 /*return*/, _a.sent()];
case 2: return [4 /*yield*/, this.postToApi(AUTH_API_ENDPOINT, __assign({ username: this.username, password: this._password }, (this.org ? { org_name: this.org } : {})), this.getBaseHeaders())];
case 3:
response = _a.sent();
return [4 /*yield*/, response.json()];
case 4:
response = _a.sent();
tok = response.token;
this._token = tok;
if (!this.authTokenValid()) {
console.error('auth token failure', { response: response, username: this.username, host: this.host });
throw new Error(__assign({ 'error': 'Auth token failure' }, (response || {})));
}
return [2 /*return*/, tok];
}
});
});
};
/**
*
* @param username
* @param password
* @param org
* @param protocol
* @param host
* @returns Promise for the authentication token
*
* Helper to fetch a token for a given user
*
*/
Client.prototype.fetchToken = function (username, password, org, protocol, host) {
if (protocol === void 0) { protocol = 'https'; }
if (host === void 0) { host = 'hub.graphistry.com'; }
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.postToApi(AUTH_API_ENDPOINT, __assign({ username: username, password: password }, (org ? { org_name: org } : {})), this.getBaseHeaders(), "".concat(protocol, "://").concat(host, "/"))];
case 1:
response = _a.sent();
return [4 /*yield*/, response.json()];
case 2:
response = _a.sent();
return [2 /*return*/, response.token];
}
});
});
};
return Client;
}(AbstractClient));
export { Client };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ2xpZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL0NsaWVudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs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