@eyepop.ai/eyepop 0.9.1 → 0.9.2
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/dist/index.d.mts +41 -31
- package/dist/index.d.ts +41 -31
- package/dist/index.js +298 -84
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +297 -83
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -8584,9 +8584,9 @@ var require_mime_types = __commonJS({
|
|
|
8584
8584
|
return false;
|
|
8585
8585
|
}
|
|
8586
8586
|
var match = EXTRACT_TYPE_REGEXP.exec(type);
|
|
8587
|
-
var
|
|
8588
|
-
if (
|
|
8589
|
-
return
|
|
8587
|
+
var mime = match && db[match[1].toLowerCase()];
|
|
8588
|
+
if (mime && mime.charset) {
|
|
8589
|
+
return mime.charset;
|
|
8590
8590
|
}
|
|
8591
8591
|
if (match && TEXT_TYPE_REGEXP.test(match[1])) {
|
|
8592
8592
|
return "UTF-8";
|
|
@@ -8597,16 +8597,16 @@ var require_mime_types = __commonJS({
|
|
|
8597
8597
|
if (!str || typeof str !== "string") {
|
|
8598
8598
|
return false;
|
|
8599
8599
|
}
|
|
8600
|
-
var
|
|
8601
|
-
if (!
|
|
8600
|
+
var mime = str.indexOf("/") === -1 ? exports2.lookup(str) : str;
|
|
8601
|
+
if (!mime) {
|
|
8602
8602
|
return false;
|
|
8603
8603
|
}
|
|
8604
|
-
if (
|
|
8605
|
-
var charset2 = exports2.charset(
|
|
8604
|
+
if (mime.indexOf("charset") === -1) {
|
|
8605
|
+
var charset2 = exports2.charset(mime);
|
|
8606
8606
|
if (charset2)
|
|
8607
|
-
|
|
8607
|
+
mime += "; charset=" + charset2.toLowerCase();
|
|
8608
8608
|
}
|
|
8609
|
-
return
|
|
8609
|
+
return mime;
|
|
8610
8610
|
}
|
|
8611
8611
|
function extension(type) {
|
|
8612
8612
|
if (!type || typeof type !== "string") {
|
|
@@ -8632,8 +8632,8 @@ var require_mime_types = __commonJS({
|
|
|
8632
8632
|
function populateMaps(extensions, types) {
|
|
8633
8633
|
var preference = ["nginx", "apache", void 0, "iana"];
|
|
8634
8634
|
Object.keys(db).forEach(function forEachMimeType(type) {
|
|
8635
|
-
var
|
|
8636
|
-
var exts =
|
|
8635
|
+
var mime = db[type];
|
|
8636
|
+
var exts = mime.extensions;
|
|
8637
8637
|
if (!exts || !exts.length) {
|
|
8638
8638
|
return;
|
|
8639
8639
|
}
|
|
@@ -8642,7 +8642,7 @@ var require_mime_types = __commonJS({
|
|
|
8642
8642
|
var extension2 = exts[i];
|
|
8643
8643
|
if (types[extension2]) {
|
|
8644
8644
|
var from = preference.indexOf(db[types[extension2]].source);
|
|
8645
|
-
var to = preference.indexOf(
|
|
8645
|
+
var to = preference.indexOf(mime.source);
|
|
8646
8646
|
if (types[extension2] !== "application/octet-stream" && (from > to || from === to && types[extension2].substr(0, 12) === "application/")) {
|
|
8647
8647
|
continue;
|
|
8648
8648
|
}
|
|
@@ -8914,13 +8914,13 @@ function readableStreamAsyncIterable(stream) {
|
|
|
8914
8914
|
|
|
8915
8915
|
// src/jobs.ts
|
|
8916
8916
|
var _AbstractJob = class _AbstractJob {
|
|
8917
|
-
constructor(baseUrl, pipelineId, authorizationHeader,
|
|
8917
|
+
constructor(baseUrl, pipelineId, authorizationHeader, client, requestLogger) {
|
|
8918
8918
|
this._responseStream = null;
|
|
8919
8919
|
this._controller = new AbortController();
|
|
8920
8920
|
this._baseUrl = baseUrl.replace(/\/+$/, "");
|
|
8921
8921
|
this._pipelineId = pipelineId;
|
|
8922
8922
|
this._authorizationHeader = authorizationHeader;
|
|
8923
|
-
this.
|
|
8923
|
+
this._client = client;
|
|
8924
8924
|
this._requestLogger = requestLogger;
|
|
8925
8925
|
this.i = _AbstractJob.n++;
|
|
8926
8926
|
}
|
|
@@ -8930,19 +8930,19 @@ var _AbstractJob = class _AbstractJob {
|
|
|
8930
8930
|
}
|
|
8931
8931
|
return Stream.iterFromReadableStream(this._responseStream, this._controller);
|
|
8932
8932
|
}
|
|
8933
|
-
async
|
|
8933
|
+
async startJob() {
|
|
8934
8934
|
return Promise.reject("abstract class");
|
|
8935
8935
|
}
|
|
8936
8936
|
start(done) {
|
|
8937
|
-
this._responseStream = new Promise((
|
|
8938
|
-
const request = this.
|
|
8937
|
+
this._responseStream = new Promise((resolve2, reject) => {
|
|
8938
|
+
const request = this.startJob();
|
|
8939
8939
|
request.then((response) => {
|
|
8940
8940
|
if (response.status != 200) {
|
|
8941
8941
|
reject(`upload error ${response.status}: ${response.text()}`);
|
|
8942
8942
|
} else if (!response.body) {
|
|
8943
8943
|
reject(`response body is empty`);
|
|
8944
8944
|
} else {
|
|
8945
|
-
|
|
8945
|
+
resolve2(response.body);
|
|
8946
8946
|
}
|
|
8947
8947
|
}).catch((reason) => {
|
|
8948
8948
|
reject(reason);
|
|
@@ -8962,12 +8962,12 @@ var UploadJob = class extends AbstractJob {
|
|
|
8962
8962
|
get [Symbol.toStringTag]() {
|
|
8963
8963
|
return "uploadJob";
|
|
8964
8964
|
}
|
|
8965
|
-
constructor(stream, mimeType, baseUrl, pipelineId, authorizationHeader,
|
|
8966
|
-
super(baseUrl, pipelineId, authorizationHeader,
|
|
8965
|
+
constructor(stream, mimeType, baseUrl, pipelineId, authorizationHeader, client, requestLogger) {
|
|
8966
|
+
super(baseUrl, pipelineId, authorizationHeader, client, requestLogger);
|
|
8967
8967
|
this._uploadStream = stream;
|
|
8968
8968
|
this._mimeType = mimeType;
|
|
8969
8969
|
}
|
|
8970
|
-
async
|
|
8970
|
+
async startJob() {
|
|
8971
8971
|
const headers = {
|
|
8972
8972
|
"Authorization": this._authorizationHeader,
|
|
8973
8973
|
"Accept": "application/jsonl",
|
|
@@ -8975,7 +8975,7 @@ var UploadJob = class extends AbstractJob {
|
|
|
8975
8975
|
};
|
|
8976
8976
|
const postUrl = `${this._baseUrl}/pipelines/${this._pipelineId}/source?mode=queue&processing=sync`;
|
|
8977
8977
|
this._requestLogger.debug("before POST %s with stream as body");
|
|
8978
|
-
const response = await fetch(postUrl, {
|
|
8978
|
+
const response = await this._client.fetch(postUrl, {
|
|
8979
8979
|
headers,
|
|
8980
8980
|
method: "POST",
|
|
8981
8981
|
body: this._uploadStream,
|
|
@@ -8989,14 +8989,14 @@ var UploadJob = class extends AbstractJob {
|
|
|
8989
8989
|
}
|
|
8990
8990
|
};
|
|
8991
8991
|
var LoadFromJob = class extends AbstractJob {
|
|
8992
|
-
constructor(location, baseUrl, pipelineId, authorizationHeader,
|
|
8993
|
-
super(baseUrl, pipelineId, authorizationHeader,
|
|
8992
|
+
constructor(location, baseUrl, pipelineId, authorizationHeader, client, requestLogger) {
|
|
8993
|
+
super(baseUrl, pipelineId, authorizationHeader, client, requestLogger);
|
|
8994
8994
|
this._location = location;
|
|
8995
8995
|
}
|
|
8996
8996
|
get [Symbol.toStringTag]() {
|
|
8997
8997
|
return "loadFromJob";
|
|
8998
8998
|
}
|
|
8999
|
-
async
|
|
8999
|
+
async startJob() {
|
|
9000
9000
|
const headers = {
|
|
9001
9001
|
"Authorization": this._authorizationHeader,
|
|
9002
9002
|
"Accept": "application/jsonl",
|
|
@@ -9008,7 +9008,7 @@ var LoadFromJob = class extends AbstractJob {
|
|
|
9008
9008
|
};
|
|
9009
9009
|
const postUrl = `${this._baseUrl}/pipelines/${this._pipelineId}/source?mode=queue&processing=sync`;
|
|
9010
9010
|
this._requestLogger.debug("before PATCH %s with url %s as source", postUrl, this._location);
|
|
9011
|
-
const response = await fetch(postUrl, {
|
|
9011
|
+
const response = await this._client.fetch(postUrl, {
|
|
9012
9012
|
headers,
|
|
9013
9013
|
method: "PATCH",
|
|
9014
9014
|
body: JSON.stringify(body),
|
|
@@ -9021,20 +9021,226 @@ var LoadFromJob = class extends AbstractJob {
|
|
|
9021
9021
|
}
|
|
9022
9022
|
};
|
|
9023
9023
|
|
|
9024
|
+
// src/shims/http_client.ts
|
|
9025
|
+
var createHttpClient;
|
|
9026
|
+
if ("document" in globalThis && "implementation" in globalThis.document) {
|
|
9027
|
+
class HttpClient2 {
|
|
9028
|
+
constructor() {
|
|
9029
|
+
}
|
|
9030
|
+
async fetch(input, init) {
|
|
9031
|
+
return fetch(input, init);
|
|
9032
|
+
}
|
|
9033
|
+
async close() {
|
|
9034
|
+
}
|
|
9035
|
+
}
|
|
9036
|
+
createHttpClient = async () => {
|
|
9037
|
+
return new HttpClient2();
|
|
9038
|
+
};
|
|
9039
|
+
} else {
|
|
9040
|
+
class HttpClient2 {
|
|
9041
|
+
constructor(agent) {
|
|
9042
|
+
this.agent = agent;
|
|
9043
|
+
}
|
|
9044
|
+
async fetch(input, init) {
|
|
9045
|
+
init || (init = {});
|
|
9046
|
+
init.dispatcher = this.agent;
|
|
9047
|
+
return fetch(input, init);
|
|
9048
|
+
}
|
|
9049
|
+
async close() {
|
|
9050
|
+
return this.agent.close();
|
|
9051
|
+
}
|
|
9052
|
+
}
|
|
9053
|
+
createHttpClient = async () => {
|
|
9054
|
+
const undici = await import("undici");
|
|
9055
|
+
const agent = new undici.Agent({
|
|
9056
|
+
keepAliveTimeout: 1e4,
|
|
9057
|
+
connections: 5
|
|
9058
|
+
});
|
|
9059
|
+
return new HttpClient2(agent);
|
|
9060
|
+
};
|
|
9061
|
+
}
|
|
9062
|
+
|
|
9063
|
+
// src/semaphore.ts
|
|
9064
|
+
var Semaphore = class {
|
|
9065
|
+
/**
|
|
9066
|
+
* Creates a semaphore.
|
|
9067
|
+
* @param permits The number of permits, i.e. strands of execution being allowed
|
|
9068
|
+
* to run in parallel.
|
|
9069
|
+
* This number can be initialized with a negative integer.
|
|
9070
|
+
*/
|
|
9071
|
+
constructor(permits) {
|
|
9072
|
+
this.promiseResolverQueue = [];
|
|
9073
|
+
this.permits = permits;
|
|
9074
|
+
}
|
|
9075
|
+
/**
|
|
9076
|
+
* Returns the number of available permits.
|
|
9077
|
+
* @returns The number of available permits.
|
|
9078
|
+
*/
|
|
9079
|
+
getPermits() {
|
|
9080
|
+
return this.permits;
|
|
9081
|
+
}
|
|
9082
|
+
/**
|
|
9083
|
+
* Returns a promise used to wait for a permit to become available. This method should be awaited on.
|
|
9084
|
+
* @returns A promise that gets resolved when execution is allowed to proceed.
|
|
9085
|
+
*/
|
|
9086
|
+
async wait() {
|
|
9087
|
+
if (this.permits > 0) {
|
|
9088
|
+
this.permits -= 1;
|
|
9089
|
+
return Promise.resolve(true);
|
|
9090
|
+
}
|
|
9091
|
+
return new Promise((resolver) => this.promiseResolverQueue.push(resolver));
|
|
9092
|
+
}
|
|
9093
|
+
/**
|
|
9094
|
+
* Alias for {@linkcode Semaphore.wait}.
|
|
9095
|
+
* @returns A promise that gets resolved when execution is allowed to proceed.
|
|
9096
|
+
*/
|
|
9097
|
+
async acquire() {
|
|
9098
|
+
return this.wait();
|
|
9099
|
+
}
|
|
9100
|
+
/**
|
|
9101
|
+
* Same as {@linkcode Semaphore.wait} except the promise returned gets resolved with false if no
|
|
9102
|
+
* permit becomes available in time.
|
|
9103
|
+
* @param milliseconds The time spent waiting before the wait is aborted. This is a lower bound,
|
|
9104
|
+
* you shouldn't rely on it being precise.
|
|
9105
|
+
* @returns A promise that gets resolved to true when execution is allowed to proceed or
|
|
9106
|
+
* false if the time given elapses before a permit becomes available.
|
|
9107
|
+
*/
|
|
9108
|
+
async waitFor(milliseconds) {
|
|
9109
|
+
if (this.permits > 0) {
|
|
9110
|
+
this.permits -= 1;
|
|
9111
|
+
return Promise.resolve(true);
|
|
9112
|
+
}
|
|
9113
|
+
let resolver = (b) => void 0;
|
|
9114
|
+
const promise = new Promise((r) => {
|
|
9115
|
+
resolver = r;
|
|
9116
|
+
});
|
|
9117
|
+
this.promiseResolverQueue.push(resolver);
|
|
9118
|
+
setTimeout(() => {
|
|
9119
|
+
const index = this.promiseResolverQueue.indexOf(resolver);
|
|
9120
|
+
if (index !== -1) {
|
|
9121
|
+
this.promiseResolverQueue.splice(index, 1);
|
|
9122
|
+
} else {
|
|
9123
|
+
console.warn(`Semaphore.waitFor couldn't find its promise resolver in the queue`);
|
|
9124
|
+
}
|
|
9125
|
+
resolver(false);
|
|
9126
|
+
}, milliseconds);
|
|
9127
|
+
return promise;
|
|
9128
|
+
}
|
|
9129
|
+
/**
|
|
9130
|
+
* Synchronous function that tries to acquire a permit and returns true if successful, false otherwise.
|
|
9131
|
+
* @returns Whether a permit could be acquired.
|
|
9132
|
+
*/
|
|
9133
|
+
tryAcquire() {
|
|
9134
|
+
if (this.permits > 0) {
|
|
9135
|
+
this.permits -= 1;
|
|
9136
|
+
return true;
|
|
9137
|
+
}
|
|
9138
|
+
return false;
|
|
9139
|
+
}
|
|
9140
|
+
/**
|
|
9141
|
+
* Acquires all permits that are currently available and returns the number of acquired permits.
|
|
9142
|
+
* @returns Number of acquired permits.
|
|
9143
|
+
*/
|
|
9144
|
+
drainPermits() {
|
|
9145
|
+
if (this.permits > 0) {
|
|
9146
|
+
const permitCount = this.permits;
|
|
9147
|
+
this.permits = 0;
|
|
9148
|
+
return permitCount;
|
|
9149
|
+
}
|
|
9150
|
+
return 0;
|
|
9151
|
+
}
|
|
9152
|
+
/**
|
|
9153
|
+
* Increases the number of permits by one. If there are other functions waiting, one of them will
|
|
9154
|
+
* continue to execute in a future iteration of the event loop.
|
|
9155
|
+
*/
|
|
9156
|
+
signal() {
|
|
9157
|
+
this.permits += 1;
|
|
9158
|
+
if (this.permits > 1 && this.promiseResolverQueue.length > 0) {
|
|
9159
|
+
console.warn("Semaphore.permits should never be > 0 when there is someone waiting.");
|
|
9160
|
+
} else if (this.permits === 1 && this.promiseResolverQueue.length > 0) {
|
|
9161
|
+
this.permits -= 1;
|
|
9162
|
+
const nextResolver = this.promiseResolverQueue.shift();
|
|
9163
|
+
if (nextResolver) {
|
|
9164
|
+
nextResolver(true);
|
|
9165
|
+
}
|
|
9166
|
+
}
|
|
9167
|
+
}
|
|
9168
|
+
/**
|
|
9169
|
+
* Alias for {@linkcode Semaphore.signal}.
|
|
9170
|
+
*/
|
|
9171
|
+
release() {
|
|
9172
|
+
this.signal();
|
|
9173
|
+
}
|
|
9174
|
+
/**
|
|
9175
|
+
* Schedules func to be called once a permit becomes available.
|
|
9176
|
+
* Returns a promise that resolves to the return value of func.
|
|
9177
|
+
* @typeparam T The return type of func.
|
|
9178
|
+
* @param func The function to be executed.
|
|
9179
|
+
* @return A promise that gets resolved with the return value of the function.
|
|
9180
|
+
*/
|
|
9181
|
+
async execute(func) {
|
|
9182
|
+
await this.wait();
|
|
9183
|
+
try {
|
|
9184
|
+
return await func();
|
|
9185
|
+
} finally {
|
|
9186
|
+
this.signal();
|
|
9187
|
+
}
|
|
9188
|
+
}
|
|
9189
|
+
};
|
|
9190
|
+
|
|
9191
|
+
// src/shims/local_file.ts
|
|
9192
|
+
var resolve;
|
|
9193
|
+
if ("document" in globalThis && "implementation" in globalThis.document) {
|
|
9194
|
+
const isFileLike = (value) => value != null && typeof value === "object" && typeof value.name === "string" && typeof value.lastModified === "number";
|
|
9195
|
+
resolve = async (params) => {
|
|
9196
|
+
if (params.file) {
|
|
9197
|
+
if (isFileLike(params.file)) {
|
|
9198
|
+
params = {
|
|
9199
|
+
mimeType: params.file.type,
|
|
9200
|
+
file: params.file
|
|
9201
|
+
};
|
|
9202
|
+
return params;
|
|
9203
|
+
}
|
|
9204
|
+
}
|
|
9205
|
+
throw new DOMException("resolving a path to a file is not supported in browser");
|
|
9206
|
+
};
|
|
9207
|
+
} else {
|
|
9208
|
+
resolve = async (params) => {
|
|
9209
|
+
if (params.file) {
|
|
9210
|
+
return params;
|
|
9211
|
+
}
|
|
9212
|
+
const mime = await Promise.resolve().then(() => __toESM(require_mime_types()));
|
|
9213
|
+
const fs = await import("fs");
|
|
9214
|
+
let mimeType;
|
|
9215
|
+
let stream;
|
|
9216
|
+
if (params.filePath) {
|
|
9217
|
+
if (params.mimeType) {
|
|
9218
|
+
mimeType = params.mimeType;
|
|
9219
|
+
} else {
|
|
9220
|
+
mimeType = mime.lookup(params.filePath) || void 0;
|
|
9221
|
+
}
|
|
9222
|
+
stream = fs.createReadStream(params.filePath);
|
|
9223
|
+
}
|
|
9224
|
+
params = {
|
|
9225
|
+
filePath: params.filePath,
|
|
9226
|
+
mimeType,
|
|
9227
|
+
file: stream
|
|
9228
|
+
};
|
|
9229
|
+
return params;
|
|
9230
|
+
};
|
|
9231
|
+
}
|
|
9232
|
+
|
|
9024
9233
|
// src/endpoint.ts
|
|
9025
|
-
var fs = __toESM(require("fs"));
|
|
9026
|
-
var import_mime_types = __toESM(require_mime_types());
|
|
9027
|
-
var import_undici = require("undici");
|
|
9028
|
-
var import_await_semaphore = require("await-semaphore");
|
|
9029
9234
|
var import_pino = require("pino");
|
|
9030
|
-
var
|
|
9235
|
+
var Endpoint = class {
|
|
9031
9236
|
constructor(options) {
|
|
9032
9237
|
this._options = options;
|
|
9033
9238
|
this._baseUrl = null;
|
|
9034
9239
|
this._pipelineId = null;
|
|
9240
|
+
this._popName = null;
|
|
9035
9241
|
this._token = null;
|
|
9036
9242
|
this._expire_token_time = null;
|
|
9037
|
-
this.
|
|
9243
|
+
this._client = null;
|
|
9038
9244
|
this._limit = null;
|
|
9039
9245
|
let rootLogger;
|
|
9040
9246
|
if (options.logger) {
|
|
@@ -9045,56 +9251,62 @@ var _Endpoint = class _Endpoint {
|
|
|
9045
9251
|
this._logger = rootLogger.child({ module: "eyepop" });
|
|
9046
9252
|
this._requestLogger = this._logger.child({ module: "requests" });
|
|
9047
9253
|
}
|
|
9254
|
+
popName() {
|
|
9255
|
+
return this._popName;
|
|
9256
|
+
}
|
|
9257
|
+
async session() {
|
|
9258
|
+
await this.currentAccessToken();
|
|
9259
|
+
if (this._token == null || this._expire_token_time == null) {
|
|
9260
|
+
return Promise.reject("endpoint not connected");
|
|
9261
|
+
}
|
|
9262
|
+
const token = {
|
|
9263
|
+
accessToken: this._token,
|
|
9264
|
+
validUntil: new Date(this._expire_token_time * 1e3)
|
|
9265
|
+
};
|
|
9266
|
+
return token;
|
|
9267
|
+
}
|
|
9048
9268
|
async upload(params) {
|
|
9049
|
-
if (!this._baseUrl || !this._pipelineId || !this.
|
|
9269
|
+
if (!this._baseUrl || !this._pipelineId || !this._client || !this._limit) {
|
|
9050
9270
|
return Promise.reject("endpoint not connected, use open() before upload()");
|
|
9051
9271
|
}
|
|
9052
|
-
|
|
9053
|
-
|
|
9054
|
-
if (params.filePath) {
|
|
9055
|
-
if (params.mimeType) {
|
|
9056
|
-
mimeType = params.mimeType;
|
|
9057
|
-
} else {
|
|
9058
|
-
mimeType = import_mime_types.default.lookup(params.filePath);
|
|
9059
|
-
}
|
|
9060
|
-
stream = fs.createReadStream(params.filePath);
|
|
9061
|
-
} else if (params.stream) {
|
|
9062
|
-
stream = params.stream;
|
|
9063
|
-
mimeType = params.mimeType;
|
|
9064
|
-
}
|
|
9065
|
-
if (!mimeType) {
|
|
9272
|
+
params = await resolve(params);
|
|
9273
|
+
if (!params.mimeType) {
|
|
9066
9274
|
return Promise.reject("upload for streams requires a mimeType");
|
|
9067
9275
|
}
|
|
9068
|
-
|
|
9276
|
+
await this._limit.acquire();
|
|
9069
9277
|
const job = new UploadJob(
|
|
9070
|
-
|
|
9071
|
-
mimeType,
|
|
9278
|
+
params.file,
|
|
9279
|
+
params.mimeType,
|
|
9072
9280
|
this._baseUrl,
|
|
9073
9281
|
this._pipelineId,
|
|
9074
9282
|
await this.authorizationHeader(),
|
|
9075
|
-
this.
|
|
9283
|
+
this._client,
|
|
9076
9284
|
this._requestLogger
|
|
9077
9285
|
);
|
|
9078
|
-
return job.start(
|
|
9286
|
+
return job.start(() => {
|
|
9287
|
+
this._limit?.release();
|
|
9288
|
+
});
|
|
9079
9289
|
}
|
|
9080
9290
|
async loadFrom(location) {
|
|
9081
|
-
if (!this._baseUrl || !this._pipelineId || !this.
|
|
9291
|
+
if (!this._baseUrl || !this._pipelineId || !this._client || !this._limit) {
|
|
9082
9292
|
throw new Error("endpoint not connected, use open() before loadFrom()");
|
|
9083
9293
|
}
|
|
9084
|
-
|
|
9294
|
+
await this._limit.acquire();
|
|
9085
9295
|
const job = new LoadFromJob(
|
|
9086
9296
|
location,
|
|
9087
9297
|
this._baseUrl,
|
|
9088
9298
|
this._pipelineId,
|
|
9089
9299
|
await this.authorizationHeader(),
|
|
9090
|
-
this.
|
|
9300
|
+
this._client,
|
|
9091
9301
|
this._requestLogger
|
|
9092
9302
|
);
|
|
9093
|
-
return job.start(
|
|
9303
|
+
return job.start(() => {
|
|
9304
|
+
this._limit?.release();
|
|
9305
|
+
});
|
|
9094
9306
|
}
|
|
9095
9307
|
// noinspection TypeScriptValidateTypes
|
|
9096
9308
|
async connect() {
|
|
9097
|
-
if (this.
|
|
9309
|
+
if (this._client) {
|
|
9098
9310
|
console.info("endpoint already connected");
|
|
9099
9311
|
return this;
|
|
9100
9312
|
}
|
|
@@ -9104,33 +9316,31 @@ var _Endpoint = class _Endpoint {
|
|
|
9104
9316
|
if (!this._options.popId) {
|
|
9105
9317
|
return Promise.reject("option popId or environment variable EYEPOP_POP_ID is required");
|
|
9106
9318
|
}
|
|
9107
|
-
if (
|
|
9108
|
-
|
|
9319
|
+
if (this._options.session) {
|
|
9320
|
+
this._token = this._options.session.accessToken;
|
|
9321
|
+
this._expire_token_time = this._options.session.validUntil.getTime() / 1e3;
|
|
9322
|
+
} else {
|
|
9323
|
+
if (!this._options.secretKey) {
|
|
9324
|
+
return Promise.reject("option secretKey or environment variable EYEPOP_SECRET_KEY is required");
|
|
9325
|
+
}
|
|
9109
9326
|
}
|
|
9110
|
-
this.
|
|
9111
|
-
|
|
9112
|
-
pipelining: 100,
|
|
9113
|
-
maxConcurrentStreams: 100,
|
|
9114
|
-
maxCachedSessions: 100,
|
|
9115
|
-
connections: _Endpoint.MAX_CONNECTIONS
|
|
9116
|
-
});
|
|
9117
|
-
this._limit = new import_await_semaphore.Semaphore(this._options.jobQueueLength);
|
|
9327
|
+
this._client = await createHttpClient();
|
|
9328
|
+
this._limit = new Semaphore(this._options.jobQueueLength);
|
|
9118
9329
|
const config_url = `${this.eyepopUrl()}/pops/${this._options.popId}/config?auto_start=${this._options.autoStart}`;
|
|
9119
9330
|
const headers = {
|
|
9120
9331
|
"Authorization": await this.authorizationHeader()
|
|
9121
9332
|
};
|
|
9122
9333
|
this._requestLogger.debug("before GET %s", config_url);
|
|
9123
|
-
let response = await fetch(config_url, { headers });
|
|
9334
|
+
let response = await this._client.fetch(config_url, { headers });
|
|
9124
9335
|
if (response.status == 401) {
|
|
9125
9336
|
this._requestLogger.debug("after GET %s: 401, about to retry with fresh access token", config_url);
|
|
9126
9337
|
this._token = null;
|
|
9127
9338
|
const headers2 = {
|
|
9128
9339
|
"Authorization": await this.authorizationHeader()
|
|
9129
9340
|
};
|
|
9130
|
-
response = await fetch(config_url, {
|
|
9131
|
-
headers: headers2
|
|
9341
|
+
response = await this._client.fetch(config_url, {
|
|
9342
|
+
headers: headers2
|
|
9132
9343
|
// @ts-ignore
|
|
9133
|
-
dispatcher: this._dispatcher
|
|
9134
9344
|
});
|
|
9135
9345
|
}
|
|
9136
9346
|
if (response.status != 200) {
|
|
@@ -9141,6 +9351,7 @@ var _Endpoint = class _Endpoint {
|
|
|
9141
9351
|
const config = data;
|
|
9142
9352
|
this._baseUrl = config.base_url;
|
|
9143
9353
|
this._pipelineId = config.pipeline_id;
|
|
9354
|
+
this._popName = config.name;
|
|
9144
9355
|
this._requestLogger.debug("after GET %s: %s / %s", config_url, this._baseUrl, this._pipelineId);
|
|
9145
9356
|
if (!this._pipelineId || !this._baseUrl) {
|
|
9146
9357
|
return Promise.reject(`Pop not started`);
|
|
@@ -9154,7 +9365,7 @@ var _Endpoint = class _Endpoint {
|
|
|
9154
9365
|
"Content-Type": "application/json"
|
|
9155
9366
|
};
|
|
9156
9367
|
this._requestLogger.debug("before PATCH %s", stop_url);
|
|
9157
|
-
let response2 = await fetch(stop_url, { method: "PATCH", headers: headers2, body: JSON.stringify(body) });
|
|
9368
|
+
let response2 = await this._client.fetch(stop_url, { method: "PATCH", headers: headers2, body: JSON.stringify(body) });
|
|
9158
9369
|
if (response2.status >= 300) {
|
|
9159
9370
|
const message = await response2.text();
|
|
9160
9371
|
return Promise.reject(`Unexpected status ${response2.status}: ${message}`);
|
|
@@ -9165,34 +9376,37 @@ var _Endpoint = class _Endpoint {
|
|
|
9165
9376
|
}
|
|
9166
9377
|
async disconnect(wait = true) {
|
|
9167
9378
|
if (wait && this._limit) {
|
|
9168
|
-
for (let
|
|
9379
|
+
for (let j = 0; j < this._options.jobQueueLength; j++) {
|
|
9169
9380
|
await this._limit.acquire();
|
|
9170
9381
|
}
|
|
9171
9382
|
}
|
|
9172
|
-
if (this.
|
|
9173
|
-
await this.
|
|
9174
|
-
this.
|
|
9383
|
+
if (this._client) {
|
|
9384
|
+
await this._client.close();
|
|
9385
|
+
this._client = null;
|
|
9175
9386
|
}
|
|
9176
9387
|
this._baseUrl = null;
|
|
9177
9388
|
this._pipelineId = null;
|
|
9178
9389
|
this._limit = null;
|
|
9179
9390
|
}
|
|
9180
9391
|
async authorizationHeader() {
|
|
9181
|
-
return `Bearer ${await this.
|
|
9392
|
+
return `Bearer ${await this.currentAccessToken()}`;
|
|
9182
9393
|
}
|
|
9183
|
-
async
|
|
9394
|
+
async currentAccessToken() {
|
|
9184
9395
|
const now = Date.now() / 1e3;
|
|
9185
9396
|
if (!this._token || this._expire_token_time < now) {
|
|
9397
|
+
if (!this._client) {
|
|
9398
|
+
return Promise.reject("endpoint not connected");
|
|
9399
|
+
} else if (!this._options.secretKey) {
|
|
9400
|
+
return Promise.reject("temporary access token expired");
|
|
9401
|
+
}
|
|
9186
9402
|
const body = { "secret_key": this._options.secretKey };
|
|
9187
9403
|
const headers = { "Content-Type": "application/json" };
|
|
9188
9404
|
const post_url = `${this.eyepopUrl()}/authentication/token`;
|
|
9189
9405
|
this._requestLogger.debug("before POST %s", post_url);
|
|
9190
|
-
const response = await fetch(post_url, {
|
|
9406
|
+
const response = await this._client.fetch(post_url, {
|
|
9191
9407
|
method: "POST",
|
|
9192
9408
|
headers,
|
|
9193
|
-
body: JSON.stringify(body)
|
|
9194
|
-
// @ts-ignore
|
|
9195
|
-
dispatcher: this._dispatcher
|
|
9409
|
+
body: JSON.stringify(body)
|
|
9196
9410
|
});
|
|
9197
9411
|
if (response.status != 200) {
|
|
9198
9412
|
const message = await response.text();
|
|
@@ -9215,8 +9429,6 @@ var _Endpoint = class _Endpoint {
|
|
|
9215
9429
|
}
|
|
9216
9430
|
}
|
|
9217
9431
|
};
|
|
9218
|
-
_Endpoint.MAX_CONNECTIONS = 5;
|
|
9219
|
-
var Endpoint = _Endpoint;
|
|
9220
9432
|
|
|
9221
9433
|
// src/visualize.ts
|
|
9222
9434
|
var _EyePopPlot = class _EyePopPlot {
|
|
@@ -9333,6 +9545,7 @@ var EyePopPlot = _EyePopPlot;
|
|
|
9333
9545
|
var EyePopSdk = class {
|
|
9334
9546
|
static endpoint({
|
|
9335
9547
|
secretKey = readEnv("EYEPOP_SECRET_KEY"),
|
|
9548
|
+
session = void 0,
|
|
9336
9549
|
popId = readEnv("EYEPOP_POP_ID"),
|
|
9337
9550
|
eyepopUrl = readEnv("EYEPOP_URL"),
|
|
9338
9551
|
autoStart = true,
|
|
@@ -9343,6 +9556,7 @@ var EyePopSdk = class {
|
|
|
9343
9556
|
} = {}) {
|
|
9344
9557
|
const options = {
|
|
9345
9558
|
secretKey,
|
|
9559
|
+
session,
|
|
9346
9560
|
popId,
|
|
9347
9561
|
eyepopUrl: eyepopUrl ?? "https://api.eyepop.ai",
|
|
9348
9562
|
autoStart,
|