mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* Convert runMany and runManySync to use injectManyWithMeta Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup type errors due to new Runnable requirements Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add documentation for verifyRunnablesAreDAG Signed-off-by: Sebastian Malton <sebastian@malton.name> * Simplify convertToWithIdWith Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move all utility functions to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move testing utilities to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move run-many and run-many-sync to separate package Signed-off-by: Sebastian Malton <sebastian@malton.name> * Replace all internal uses of utilities with new packages Signed-off-by: Sebastian Malton <sebastian@malton.name> * Use new @k8slens/run-many package in core Signed-off-by: Sebastian Malton <sebastian@malton.name> * Add dep to open-lens Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup uses of @k8slens/test-utils Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup getGlobalOverride Signed-off-by: Sebastian Malton <sebastian@malton.name> * Move tests to new package too Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix type errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup uses of AsyncResult and autoBind Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fixup remaining import issues Signed-off-by: Sebastian Malton <sebastian@malton.name> * Finial fixups to fix build Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix lint Signed-off-by: Sebastian Malton <sebastian@malton.name> * Revert moving "testUsingFakeTime" to separate package - This fixes tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix integration tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix unit test failing due to spelling fix Signed-off-by: Sebastian Malton <sebastian@malton.name> --------- Signed-off-by: Sebastian Malton <sebastian@malton.name>
94 lines
2.4 KiB
TypeScript
94 lines
2.4 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
|
|
import { Readable } from "readable-stream";
|
|
import type { TypedArray } from "type-fest";
|
|
|
|
/**
|
|
* ReadableWebToNodeStream
|
|
*
|
|
* Copied from https://github.com/Borewit/readable-web-to-node-stream
|
|
*
|
|
* Adds read error handler
|
|
*
|
|
* */
|
|
export class ReadableWebToNodeStream<T extends TypedArray> extends Readable {
|
|
|
|
public bytesRead = 0;
|
|
public released = false;
|
|
|
|
/**
|
|
* Default web API stream reader
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader
|
|
*/
|
|
private reader: ReadableStreamDefaultReader<T>;
|
|
private pendingRead?: Promise<ReadableStreamReadResult<T>>;
|
|
|
|
/**
|
|
*
|
|
* @param stream ReadableStream: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream
|
|
*/
|
|
constructor(stream: ReadableStream<T>) {
|
|
super();
|
|
this.reader = stream.getReader();
|
|
}
|
|
|
|
/**
|
|
* Implementation of readable._read(size).
|
|
* When readable._read() is called, if data is available from the resource,
|
|
* the implementation should begin pushing that data into the read queue
|
|
* https://nodejs.org/api/stream.html#stream_readable_read_size_1
|
|
*/
|
|
public async _read() {
|
|
// Should start pushing data into the queue
|
|
// Read data from the underlying Web-API-readable-stream
|
|
if (this.released) {
|
|
this.push(null); // Signal EOF
|
|
|
|
return;
|
|
}
|
|
|
|
try {
|
|
this.pendingRead = this.reader.read();
|
|
const data = await this.pendingRead;
|
|
|
|
// clear the promise before pushing pushing new data to the queue and allow sequential calls to _read()
|
|
delete this.pendingRead;
|
|
|
|
if (data.done || this.released) {
|
|
this.push(null); // Signal EOF
|
|
} else {
|
|
this.bytesRead += data.value.length;
|
|
this.push(data.value); // Push new data to the queue
|
|
}
|
|
} catch (error) {
|
|
this.push(null); // Signal EOF
|
|
}
|
|
}
|
|
|
|
/**
|
|
* If there is no unresolved read call to Web-API ReadableStream immediately returns;
|
|
* otherwise will wait until the read is resolved.
|
|
*/
|
|
public async waitForReadToComplete() {
|
|
if (this.pendingRead) {
|
|
await this.pendingRead;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Close wrapper
|
|
*/
|
|
public async close(): Promise<void> {
|
|
await this.syncAndRelease();
|
|
}
|
|
|
|
private async syncAndRelease() {
|
|
this.released = true;
|
|
await this.waitForReadToComplete();
|
|
await this.reader.releaseLock();
|
|
}
|
|
}
|