text: // Copyright (c) Microsoft Corporation.
  // Licensed under the MIT license.
  import { __awaiter, __extends, __generator } from "tslib";
  import { Readable } from "stream";
  import { AvroReadableFromStream, AvroReader } from "../../../storage-internal-avro/src";
  /**
   * ONLY AVAILABLE IN NODE.JS RUNTIME.
   *
   * A Node.js BlobQuickQueryStream will internally parse avro data stream for blob query.
   */
  var BlobQuickQueryStream = /** @class */ (function (_super) {
      __extends(BlobQuickQueryStream, _super);
      /**
       * Creates an instance of BlobQuickQueryStream.
       *
       * @param source - The current ReadableStream returned from getter
       * @param options -
       */
      function BlobQuickQueryStream(source, options) {
          if (options === void 0) { options = {}; }
          var _this = _super.call(this) || this;
          _this.avroPaused = true;
          _this.source = source;
          _this.onProgress = options.onProgress;
          _this.onError = options.onError;
          _this.avroReader = new AvroReader(new AvroReadableFromStream(_this.source));
          _this.avroIter = _this.avroReader.parseObjects({ abortSignal: options.abortSignal });
          return _this;
      }
      BlobQuickQueryStream.prototype._read = function () {
          var _this = this;
          if (this.avroPaused) {
              this.readInternal().catch(function (err) {
                  _this.emit("error", err);
              });
          }
      };
      BlobQuickQueryStream.prototype.readInternal = function () {
          return __awaiter(this, void 0, void 0, function () {
              var avroNext, obj, schema, data, bytesScanned, totalBytes, fatal, name_1, description, position;
              return __generator(this, function (_a) {
                  switch (_a.label) {
                      case 0:
                          this.avroPaused = false;
                          _a.label = 1;
                      case 1: return [4 /*yield*/, this.avroIter.next()];
                      case 2:
                          avroNext = _a.sent();
                          if (avroNext.done) {
                              return [3 /*break*/, 4];
                          }
                          obj = avroNext.value;
                          schema = obj.$schema;
                          if (typeof schema !== "string") {
                              throw Error("Missing schema in avro record.");
                          }
                          switch (schema) {
                              case "com.microsoft.azure.storage.queryBlobContents.resultData":
                                  data = obj.data;
                                  if (data instanceof Uint8Array === false) {
                                      throw Error("Invalid data in avro result record.");
                                  }
                                  if (!this.push(Buffer.from(data))) {
                                      this.avroPaused = true;
                                  }
                                  break;
                              case "com.microsoft.azure.storage.queryBlobContents.progress":
                                  bytesScanned = obj.bytesScanned;
                                  if (typeof bytesScanned !== "number") {
                                      throw Error("Invalid bytesScanned in avro progress record.");
                                  }
                                  if (this.onProgress) {
                                      this.onProgress({ loadedBytes: bytesScanned });
                                  }
                                  break;
                              case "com.microsoft.azure.storage.queryBlobContents.end":
                                  if (this.onProgress) {
                                      totalBytes = obj.totalBytes;
                                      if (typeof totalBytes !== "number") {
                                          throw Error("Invalid totalBytes in avro end record.");
                                      }
                                      this.onProgress({ loadedBytes: totalBytes });
                                  }
                                  this.push(null);
                                  break;
                              case "com.microsoft.azure.storage.queryBlobContents.error":
                                  if (this.onError) {
                                      fatal = obj.fatal;
                                      if (typeof fatal !== "boolean") {
                                          throw Error("Invalid fatal in avro error record.");
                                      }
                                      name_1 = obj.name;
                                      if (typeof name_1 !== "string") {
                                          throw Error("Invalid name in avro error record.");
                                      }
                                      description = obj.description;
                                      if (typeof description !== "string") {
                                          throw Error("Invalid description in avro error record.");
                                      }
                                      position = obj.position;
                                      if (typeof position !== "number") {
                                          throw Error("Invalid position in avro error record.");
                                      }
                                      this.onError({
                                          position: position,
                                          name: name_1,
                                          isFatal: fatal,
                                          description: description
                                      });
                                  }
                                  break;
                              default:
                                  throw Error("Unknown schema " + schema + " in avro progress record.");
                          }
                          _a.label = 3;
                      case 3:
                          if (!avroNext.done && !this.avroPaused) return [3 /*break*/, 1];
                          _a.label = 4;
                      case 4: return [2 /*return*/];
                  }
              });
          });
      };
      return BlobQuickQueryStream;
  }(Readable));
  export { BlobQuickQueryStream };
  //# sourceMappingURL=BlobQuickQueryStream.js.map