@zip.js/zip.js
    Preparing search index...

    Function initWorker

    • Initializes a custom web worker script. This function is exposed by the @zip.js/zip.js/worker entry point and must be called in the worker script created by Configuration#createWorker or referenced by Configuration#workerURI.

      Here is a complete example of a worker script using fflate as the compression engine, e.g. to reduce the bundle size:

      import { initWorker } from "@zip.js/zip.js/worker";
      import { Deflate, Inflate } from "fflate";

      const FORMAT_DEFLATE_RAW = "deflate-raw";

      class FflateStream extends TransformStream {
      constructor(codec) {
      super({
      start(controller) {
      codec.ondata = chunk => {
      if (chunk.length) {
      controller.enqueue(chunk);
      }
      };
      },
      transform(chunk) {
      codec.push(chunk);
      },
      flush() {
      codec.push(new Uint8Array(0), true);
      }
      });
      }
      }

      class CompressionStreamFallback extends FflateStream {
      constructor(format, { level } = {}) {
      checkFormat(format);
      super(new Deflate(level === undefined ? {} : { level }));
      }
      }

      class DecompressionStreamFallback extends FflateStream {
      constructor(format) {
      checkFormat(format);
      super(new Inflate());
      }
      }

      function checkFormat(format) {
      if (format != FORMAT_DEFLATE_RAW) {
      throw new TypeError("Unsupported compression format: " + format);
      }
      }

      initWorker({ CompressionStreamFallback, DecompressionStreamFallback });

      Parameters

      • Optionaloptions: {
            CompressionStreamFallback?: typeof CompressionStreamLike;
            DecompressionStreamFallback?: typeof DecompressionStreamLike;
            init?(config: Configuration): unknown;
        }
        • OptionalCompressionStreamFallback?: typeof CompressionStreamLike

          The stream implementation used to compress data when useCompressionStream is set to false or when CompressionStream is unsupported.

        • OptionalDecompressionStreamFallback?: typeof DecompressionStreamLike

          The stream implementation used to decompress data when useCompressionStream is set to false or when DecompressionStream is unsupported.

        • init?: function
          • The function called before resolving the stream implementations, e.g. to load a WebAssembly module.

            Parameters

            Returns unknown

      Returns void