zlib#gunzipSync TypeScript Examples

The following examples show how to use zlib#gunzipSync. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: s3-queue.ts    From posthog-foss with MIT License 6 votes vote down vote up
// consumer

    async readState(): Promise<boolean> {
        if (!this.s3Wrapper) {
            throw new Error('S3 object not initialized')
        }
        const response = await this.listObjects()
        if (response.length > 0) {
            for (const filename of response) {
                const object = await this.s3Wrapper?.getObject({
                    Bucket: this.serverConfig.JOB_QUEUE_S3_BUCKET_NAME,
                    Key: filename,
                })
                if (object?.Body) {
                    const job: EnqueuedJob = JSON.parse(gunzipSync(object.Body as Buffer).toString('utf8'))
                    await this.onJob?.([job])
                    await this.s3Wrapper?.deleteObject({
                        Bucket: this.serverConfig.JOB_QUEUE_S3_BUCKET_NAME,
                        Key: filename,
                    })
                }
            }
        }
        return response.length > 0
    }
Example #2
Source File: gzip.ts    From mtcute with GNU Lesser General Public License v3.0 5 votes vote down vote up
export function gzipInflate(buf: Buffer): Buffer {
    return gunzipSync(buf)
}
Example #3
Source File: lambda.ts    From office-booker with MIT License 5 votes vote down vote up
processEvent = async (
  fetchMethod: typeof fetch,
  env: string,
  event: LogGroupEvent | undefined
) => {
  const postNotification = async (message: string) => {
    // TODO: implement logging
  };

  const parseEvents = (data?: string) => {
    if (typeof data !== 'string') {
      throw new Error('Unexpected type of data');
    }
    try {
      const payload = Buffer.from(data, 'base64');
      const parsed = JSON.parse(gunzipSync(payload).toString());
      return parsed;
    } catch (err) {
      throw new Error(`Failed reading event: deserializing\n${err.message}`);
    }
  };

  const getMessage = (event: LogGroupEvent | undefined): string => {
    try {
      const events = parseEvents(event?.awslogs?.data);
      const logEvents = events?.logEvents;
      if (!Array.isArray(logEvents)) {
        throw new Error('Log events is not an array');
      }
      const messages = logEvents.map((item, i) => {
        const message = item?.message;
        if (typeof message !== 'string') {
          throw new Error(`logEvent[${i}].message is not a string: ${JSON.stringify(item)}`);
        }
        return message;
      });

      return `Issue on office-booker-${env}: ${messages.join('\n')}`;
    } catch (err) {
      console.error(err);
      return `Unparsable issue office-booker-${env}: ${err.message}`;
    }
  };

  try {
    const message = getMessage(event);
    await postNotification(message);
  } catch (error) {
    console.error(error);
  }
}
Example #4
Source File: MacUpdater.ts    From electron-differential-updater with MIT License 4 votes vote down vote up
private async differentialDownloadInstaller(
    fileInfo: ResolvedUpdateFileInfo,
    downloadUpdateOptions: DownloadUpdateOptions,
    installerPath: string,
    provider: Provider<any>
  ): Promise<boolean> {
    try {
      if (
        this._testOnlyOptions != null &&
        !this._testOnlyOptions.isUseDifferentialDownload
      ) {
        return true;
      }

      const newBlockMapUrl = newUrlFromBase(
        `${fileInfo.url.pathname}.blockmap`,
        fileInfo.url
      );
      const oldBlockMapUrl = newUrlFromBase(
        `${fileInfo.url.pathname.replace(
          new RegExp(
            downloadUpdateOptions.updateInfoAndProvider.info.version,
            "g"
          ),
          this.app.version
        )}.blockmap`,
        fileInfo.url
      );
      this._logger.info(
        `Download block maps (old: "${oldBlockMapUrl.href}", new: ${newBlockMapUrl.href})`
      );

      const downloadBlockMap = async (url: URL): Promise<BlockMap> => {
        const data = await this.httpExecutor.downloadToBuffer(url, {
          headers: downloadUpdateOptions.requestHeaders,
          cancellationToken: downloadUpdateOptions.cancellationToken
        });

        if (data == null || data.length === 0) {
          throw new Error(`Blockmap "${url.href}" is empty`);
        }

        try {
          return JSON.parse(gunzipSync(data).toString());
        } catch (e) {
          throw new Error(
            `Cannot parse blockmap "${url.href}", error: ${e}, raw data: ${data}`
          );
        }
      };

      const blockMapDataList = await Promise.all([
        downloadBlockMap(oldBlockMapUrl),
        downloadBlockMap(newBlockMapUrl)
      ]);
      await new GenericDifferentialDownloader(
        fileInfo.info,
        this.httpExecutor,
        {
          newUrl: fileInfo.url,
          oldFile: path.join(
            this.downloadedUpdateHelper!!.cacheDir,
            process.platform === "darwin"
              ? `${this.app.name}-${this.app.version}-mac.zip`
              : CURRENT_APP_INSTALLER_FILE_NAME
          ),
          logger: this._logger,
          newFile: installerPath,
          isUseMultipleRangeRequest: provider.isUseMultipleRangeRequest,
          requestHeaders: downloadUpdateOptions.requestHeaders
        }
      ).download(
        blockMapDataList[0],
        blockMapDataList[1],
        this.emit.bind(this)
      );
      return false;
    } catch (e) {
      this._logger.error(
        `Cannot download differentially, fallback to full download: ${e.stack ||
          e}`
      );
      if (this._testOnlyOptions != null) {
        // test mode
        throw e;
      }
      return true;
    }
  }
Example #5
Source File: NsisUpdater.ts    From electron-differential-updater with MIT License 4 votes vote down vote up
private async differentialDownloadInstaller(
    fileInfo: ResolvedUpdateFileInfo,
    downloadUpdateOptions: DownloadUpdateOptions,
    installerPath: string,
    provider: Provider<any>
  ): Promise<boolean> {
    try {
      if (
        this._testOnlyOptions != null &&
        !this._testOnlyOptions.isUseDifferentialDownload
      ) {
        return true;
      }
      const getBlockMapUrl = () => {
        if (
          fileInfo.info.url.includes(
            `${downloadUpdateOptions.updateInfoAndProvider.info.version}`
          )
        ) {
          return `${fileInfo.url.pathname}.blockmap`;
        } else {
          const { app } = this.app;
          return `${fileInfo.url.pathname.replace(
            new RegExp(fileInfo.info.url, "g"),
            `${app.productName || app.name} Setup ${
              downloadUpdateOptions.updateInfoAndProvider.info.version
            }.exe`
          )}.blockmap`;
        }
      };
      const blockMapUrl = getBlockMapUrl();
      const newBlockMapUrl = newUrlFromBase(blockMapUrl, fileInfo.url);
      const oldBlockMapUrl = newUrlFromBase(
        `${blockMapUrl.replace(
          new RegExp(
            downloadUpdateOptions.updateInfoAndProvider.info.version,
            "g"
          ),
          this.app.version
        )}`,
        fileInfo.url
      );
      this._logger.info(
        `Download block maps (old: "${oldBlockMapUrl.href}", new: ${newBlockMapUrl.href})`
      );

      const downloadBlockMap = async (url: URL): Promise<BlockMap> => {
        const data = await this.httpExecutor.downloadToBuffer(url, {
          headers: downloadUpdateOptions.requestHeaders,
          cancellationToken: downloadUpdateOptions.cancellationToken
        });

        if (data == null || data.length === 0) {
          throw new Error(`Blockmap "${url.href}" is empty`);
        }

        try {
          return JSON.parse(gunzipSync(data).toString());
        } catch (e) {
          throw new Error(
            `Cannot parse blockmap "${url.href}", error: ${e}, raw data: ${data}`
          );
        }
      };

      const blockMapDataList = await Promise.all([
        downloadBlockMap(oldBlockMapUrl),
        downloadBlockMap(newBlockMapUrl)
      ]);
      await new GenericDifferentialDownloader(
        fileInfo.info,
        this.httpExecutor,
        {
          newUrl: fileInfo.url,
          oldFile: path.join(
            this.downloadedUpdateHelper!!.cacheDir,
            CURRENT_APP_INSTALLER_FILE_NAME
          ),
          logger: this._logger,
          newFile: installerPath,
          isUseMultipleRangeRequest: provider.isUseMultipleRangeRequest,
          requestHeaders: downloadUpdateOptions.requestHeaders
        }
      ).download(
        blockMapDataList[0],
        blockMapDataList[1],
        this.emit.bind(this)
      );
      return false;
    } catch (e) {
      this._logger.error(
        `Cannot download differentially, fallback to full download: ${e.stack ||
          e}`
      );
      if (this._testOnlyOptions != null) {
        // test mode
        throw e;
      }
      return true;
    }
  }
Example #6
Source File: api.spec.ts    From code-client with MIT License 4 votes vote down vote up
describe('Base64 encoded operations', () => {
  it('encodes a payload to base64', async () => {
    // Create a bundle
    const files: BundleFiles = (await singleBundleFull).reduce((r, d) => {
      r[d.bundlePath] = pick(d, ['hash', 'content']);
      return r;
    }, {});
    const makeRequestSpy = jest.spyOn(needle, 'makeRequest');

    const bundleResponse = await createBundle({
      baseURL,
      sessionToken,
      source,
      files,
      base64Encoding: true,
    });

    const request = makeRequestSpy.mock.calls[0][0];
    const requestBody = request.body as string;
    const requestHeaders = request.headers;
    const decompressedBody = gunzipSync(Buffer.from(requestBody)).toString();
    expect(requestHeaders).toEqual(
      expect.objectContaining({
        'content-type': 'application/octet-stream',
        'content-encoding': 'gzip',
      }),
    );
    expect(request.isJson).toBe(false);
    expect(JSON.parse(Buffer.from(decompressedBody, 'base64').toString())).toEqual(files);
  }),
    it('extends a base64-encoded bundle', async () => {
      const makeRequestSpy = jest.spyOn(needle, 'makeRequest');
      const bundleResponse = await extendBundle({
        baseURL,
        sessionToken,
        source,
        bundleHash: fakeBundleHashFull,
        files: {
          'new.js': 'new123',
        },
        removedFiles: [
          `AnnotatorTest.cpp`,
          `app.js`,
          `GitHubAccessTokenScrambler12.java`,
          `db.js`,
          `main.js`,
          'big-file.js',
          `not/ignored/this_should_be_ignored.jsx`,
          `not/ignored/this_should_not_be_ignored.java`,
          `routes/index.js`,
          `routes/sharks.js`,
        ],
        base64Encoding: true,
      });
      const request = makeRequestSpy.mock.calls[0][0];
      const requestBody = request.body as string;
      const requestHeaders = request.headers;
      const decompressedBody = gunzipSync(Buffer.from(requestBody)).toString();
      expect(requestHeaders).toEqual(
        expect.objectContaining({
          'content-type': 'application/octet-stream',
          'content-encoding': 'gzip',
        }),
      );
      expect(request.isJson).toBe(false);
      expect(JSON.parse(Buffer.from(decompressedBody, 'base64').toString())).toEqual({
        files: {
          'new.js': 'new123',
        },
        removedFiles: [
          `AnnotatorTest.cpp`,
          `app.js`,
          `GitHubAccessTokenScrambler12.java`,
          `db.js`,
          `main.js`,
          'big-file.js',
          `not/ignored/this_should_be_ignored.jsx`,
          `not/ignored/this_should_not_be_ignored.java`,
          `routes/index.js`,
          `routes/sharks.js`,
        ],
      });
    });
});