import type { CloudlyApiClient } from './classes.cloudlyapiclient.js'; import * as plugins from './plugins.js'; export class Image implements plugins.servezoneInterfaces.data.IImage { public static async getImages(cloudlyClientRef: CloudlyApiClient) { const getAllImagesTR = cloudlyClientRef.typedsocketClient.createTypedRequest( 'getAllImages' ); const response = await getAllImagesTR.fire({ jwt: cloudlyClientRef.identity.jwt, }); const resultImages: Image[] = []; for (const image of response.images) { const newImage = new Image(cloudlyClientRef); Object.assign(newImage, image); resultImages.push(newImage); } return resultImages; } // INSTANCE cloudlyClientRef: CloudlyApiClient; id: plugins.servezoneInterfaces.data.IImage['id']; data: plugins.servezoneInterfaces.data.IImage['data']; constructor(cloudlyClientRef: CloudlyApiClient) { this.cloudlyClientRef = cloudlyClientRef; } /** * updates the image data */ public async update() { const getVersionsTR = this.cloudlyClientRef.typedsocketClient.createTypedRequest( 'getImageMetadata' ); const response = await getVersionsTR.fire({ jwt: this.cloudlyClientRef.identity.jwt, imageId: this.id, }); Object.assign(this, response.image); } /** * pushes a new version of the image * @param imageVersion * @param imageReadableArg */ public async pushImageVersion(imageVersion: string, imageReadableArg: ReadableStream): Promise { const done = plugins.smartpromise.defer(); const pullImageTR = this.cloudlyClientRef.typedsocketClient.createTypedRequest( 'pushImageVersion' ); const virtualStream = new plugins.typedrequest.VirtualStream(); const response = await pullImageTR.fire({ jwt: this.cloudlyClientRef.identity.jwt, imageId: this.id, versionString: '', imageStream: virtualStream, }); await virtualStream.readFromWebstream(imageReadableArg); await done.promise; await this.update(); }; /** * pulls a version of the image */ public async pullImageVersion(versionStringArg: string): Promise> { const pullImageTR = this.cloudlyClientRef.typedsocketClient.createTypedRequest( 'pullImageVersion' ); const response = await pullImageTR.fire({ jwt: this.cloudlyClientRef.identity.jwt, imageId: this.id, versionString: versionStringArg, }); const imageStream = response.imageStream; const webduplexStream = new plugins.webstream.WebDuplexStream({}); imageStream.writeToWebstream(webduplexStream.writable); return webduplexStream.readable; }; }