Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Nov 29, 2024
1 parent 5c3c549 commit 2bb026e
Show file tree
Hide file tree
Showing 21 changed files with 387 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Source/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import { IAuthenticator } from "./types";

export class Authenticator implements IAuthenticator {
constructor(private readonly fetch: SimpleFetch) {}

public async getJupyterAuthInfo(
options: {
baseUrl: string;

authInfo: {
username: string;

password: string;

token: string;
};
},
Expand All @@ -34,6 +38,7 @@ export class Authenticator implements IAuthenticator {
return { tokenId: "", token: options.authInfo.password };
}
}

if (options.authInfo.token) {
const isApiTokenValid = await verifyApiToken(
options.baseUrl,
Expand All @@ -47,6 +52,7 @@ export class Authenticator implements IAuthenticator {
return { tokenId: "", token: options.authInfo.token };
}
}

return generateNewApiToken(
options.baseUrl,
options.authInfo.username,
Expand Down
31 changes: 31 additions & 0 deletions Source/common/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export async function sleep(timeout: number, token?: CancellationToken) {

const promise = new Promise((resolve) => {
const timer = setTimeout(resolve, timeout);

disposables.push(new Disposable(() => clearTimeout(timer)));
});

await raceCancellation(token, promise).finally(() => {
disposables.forEach((d) => d.dispose());
});
Expand Down Expand Up @@ -94,13 +96,16 @@ export async function raceCancellation<T>(

if (isPromiseLike(defaultValue)) {
promises.push(defaultValue as unknown as Promise<T>);

value = undefined;
} else {
value = defaultValue;
}

if (!token) {
return await Promise.race(promises);
}

if (token.isCancellationRequested) {
return value;
}
Expand All @@ -109,10 +114,13 @@ export async function raceCancellation<T>(
if (token.isCancellationRequested) {
return resolve(value);
}

const disposable = token.onCancellationRequested(() => {
disposable.dispose();

resolve(value);
});

Promise.race(promises)
.then(resolve, reject)
.finally(() => disposable.dispose());
Expand All @@ -125,6 +133,7 @@ export async function raceCancellationError<T>(
if (!token) {
return Promise.race(promises);
}

if (token.isCancellationRequested) {
throw new CancellationError();
}
Expand All @@ -133,10 +142,13 @@ export async function raceCancellationError<T>(
if (token.isCancellationRequested) {
return reject(new CancellationError());
}

const disposable = token.onCancellationRequested(() => {
disposable.dispose();

reject(new CancellationError());
});

Promise.race(promises)
.then(resolve, reject)
.finally(() => disposable.dispose());
Expand All @@ -149,10 +161,15 @@ export async function raceCancellationError<T>(
// eslint-disable-next-line @typescript-eslint/naming-convention
export interface Deferred<T> {
readonly promise: Promise<T>;

readonly resolved: boolean;

readonly rejected: boolean;

readonly completed: boolean;

readonly value?: T;

resolve(value?: T | PromiseLike<T>): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reject(reason?: any): void;
Expand All @@ -162,10 +179,15 @@ class DeferredImpl<T> implements Deferred<T> {
private _resolve!: (value: T | PromiseLike<T>) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _reject!: (reason?: any) => void;

private _resolved: boolean = false;

private _rejected: boolean = false;

private _promise: Promise<T>;

private _value: T | undefined;

public get value() {
return this._value;
}
Expand All @@ -174,30 +196,38 @@ class DeferredImpl<T> implements Deferred<T> {
// eslint-disable-next-line
this._promise = new Promise<T>((res, rej) => {
this._resolve = res;

this._reject = rej;
});
}

public resolve(value?: T | PromiseLike<T>) {
this._value = value as T | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._resolve.apply(this.scope ? this.scope : this, arguments as any);

this._resolved = true;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public reject(_reason?: any) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._reject.apply(this.scope ? this.scope : this, arguments as any);

this._rejected = true;
}

get promise(): Promise<T> {
return this._promise;
}

get resolved(): boolean {
return this._resolved;
}

get rejected(): boolean {
return this._rejected;
}

get completed(): boolean {
return this._rejected || this._resolved;
}
Expand All @@ -209,6 +239,7 @@ export function createDeferred<T>(scope: any = null): Deferred<T> {

export function createDeferredFromPromise<T>(promise: Promise<T>): Deferred<T> {
const deferred = createDeferred<T>();

promise
.then(deferred.resolve.bind(deferred))
.catch(deferred.reject.bind(deferred));
Expand Down
2 changes: 2 additions & 0 deletions Source/common/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ export async function computeHash(
if (Object.keys(computedHashes).length > 10_000) {
stopStoringHashes = true;
}

computedHashes[data] = hash;
}

return hash;
}

Expand Down
Loading

0 comments on commit 2bb026e

Please sign in to comment.