UNPKG

@itwin/core-common

Version:

iTwin.js components common to frontend and backend

95 lines 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BlobReader = exports.Uint8Chunks = void 0; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module iModels */ const core_bentley_1 = require("@itwin/core-bentley"); const ConcurrentQuery_1 = require("./ConcurrentQuery"); /** @beta */ class Uint8Chunks { _chunks = []; append(chunk) { this._chunks.push(chunk); } at(idx) { return this._chunks[idx]; } get length() { return this._chunks.length; } [Symbol.iterator]() { return this._chunks[Symbol.iterator]; } combine() { const totalChunkLength = this._chunks.reduce((acc, v) => acc + v.length, 0); const combineChunk = new Uint8Array(totalChunkLength); let offset = 0; for (const array of this._chunks) { combineChunk.set(array, offset); offset += array.length; } return combineChunk; } } exports.Uint8Chunks = Uint8Chunks; /** @beta */ class BlobReader { _executor; className; accessString; instanceId; _chunks = new Uint8Chunks(); _lengthToRead = -1; _options = new ConcurrentQuery_1.BlobOptionsBuilder().getOptions(); constructor(_executor, className, accessString, instanceId, options) { this._executor = _executor; this.className = className; this.accessString = accessString; this.instanceId = instanceId; this.reset(options); } reset(options) { if (options) { this._options = options; } this._chunks = new Uint8Chunks(); this._lengthToRead = (0, core_bentley_1.expectDefined)(this.range.count); } get range() { return (0, core_bentley_1.expectDefined)(this._options.range); } async step() { if (this._lengthToRead === this._chunks.length) { return false; } const request = { kind: ConcurrentQuery_1.DbRequestKind.BlobIO, className: this.className, accessString: this.accessString, instanceId: this.instanceId, ...this._options, }; request.range = { offset: this._chunks.length, count: this.range ? this._lengthToRead - this._chunks.length : 0 }; const resp = await this._executor.execute(request); ConcurrentQuery_1.DbQueryError.throwIfError(resp, request); if (this._lengthToRead === -1) { this._lengthToRead = resp.rawBlobSize; } if (resp.data && resp.data.length > 0) { this._chunks.append(resp.data); } return true; } async readToEnd() { while (await this.step()) { } return this._chunks.combine(); } get current() { if (this._chunks.length === 0) { throw new Error("there is no current buffer"); } return this._chunks.at(this._chunks.length); } get chunks() { return this._chunks; } } exports.BlobReader = BlobReader; //# sourceMappingURL=BlobReader.js.map