Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed May 23, 2023
1 parent 9ee6aa2 commit 66041f3
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 201 deletions.
9 changes: 2 additions & 7 deletions modules/3d-tiles/test/lib/utils/load-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@

import {fetchFile, load} from '@loaders.gl/core';
import {Tiles3DLoader} from '@loaders.gl/3d-tiles';
import {Tileset3D} from '@loaders.gl/tiles';

/** @typedef {import('@loaders.gl/tiles').Tile3D} Tile3D */
import {Tileset3D, Tile3D} from '@loaders.gl/tiles';

/**
* @returns {Promise<Tile3D>}
*/
export async function loadRootTile(t, tilesetUrl) {
export async function loadRootTile(t, tilesetUrl): Promise<Tile3D> {
try {
// Load tileset
const tilesetJson = await load(tilesetUrl, Tiles3DLoader);
const tileset = new Tileset3D(tilesetJson, tilesetUrl);

// Load root tile
/** @type {Tile3D} */
// @ts-ignore
const sourceRootTile = tileset.root as Tile3D;
await tileset._loadTile(sourceRootTile);
return sourceRootTile;
Expand Down
14 changes: 6 additions & 8 deletions modules/compression/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export {Compression} from './lib/compression';

export {NoCompression} from './lib/no-compression';

export {DeflateCompression} from './lib/deflate-compression';
export {DeflateCompression as _DeflateCompressionZlib} from './lib/deflate-compression-zlib';
export {GZipCompression} from './lib/gzip-compression';
export {GZipCompression as _GZipCompressionZlib} from './lib/gzip-compression-zlib';
export {DeflateCompression} from './lib/deflate-compression-pako';
export {DeflateCompressionZlib} from './lib/deflate-compression-zlib';
export {GZipCompression} from './lib/gzip-compression-pako';
export {GZipCompressionZlib} from './lib/gzip-compression-zlib';

export {BrotliCompression} from './lib/brotli-compression';
export {BrotliCompressionZlib} from './lib/brotli-compression-zlib';
Expand All @@ -24,10 +24,8 @@ export type {CompressionWorkerOptions} from './compression-worker';
export {CompressionWorker, compressOnWorker} from './compression-worker';

// Versions
export {DeflateCompression as _DeflateCompressionFflate} from './lib/deflate-compression';
export {GZipCompression as _GZipCompressionFflate} from './lib/gzip-compression';
export {DeflateCompression as _DeflateCompressionFflate} from './lib/deflate-compression-fflate';
export {GZipCompression as _GZipCompressionFflate} from './lib/gzip-compression-fflate';

export {DeflateCompression as _DeflateCompressionPako} from './lib/deflate-compression-pako';
export {GZipCompression as _GZipCompressionPako} from './lib/gzip-compression-pako';


37 changes: 23 additions & 14 deletions modules/compression/src/lib/brotli-compression-zlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@
import type {CompressionOptions} from './compression';
import {Compression} from './compression';
import {isBrowser, toArrayBuffer} from '@loaders.gl/loader-utils';
import zlib from 'zlib';
import zlib, {BrotliOptions} from 'zlib';
import {promisify1, promisify2} from '@loaders.gl/loader-utils';

export type BrotliCompressionOptions = CompressionOptions & {
brotli?: {
mode?: number;
quality?: number;
lgwin?: number;
useZlib?: boolean;
};
export type BrotliCompressionZlibOptions = CompressionOptions & {
brotliZlib?: BrotliOptions;
};

/**
* brotli compression / decompression using zlib
* brotli compression / decompression
* zlib implementation
* @note Node uses compression level 11 by default which is 100x slower!!
*/
export class BrotliCompressionZlib extends Compression {
readonly name: string = 'brotli';
readonly extensions = ['br'];
readonly contentEncodings = ['br'];
readonly isSupported = true;
readonly options: BrotliCompressionOptions;
readonly options: BrotliCompressionZlibOptions;

constructor(options: BrotliCompressionOptions = {}) {
constructor(options: BrotliCompressionZlibOptions = {}) {
super(options);
this.options = options;
if (isBrowser) {
Expand All @@ -33,13 +30,15 @@ export class BrotliCompressionZlib extends Compression {
}

async compress(input: ArrayBuffer): Promise<ArrayBuffer> {
// @ts-expect-error overloads - Node uses compression level 11 by default which is 100x slower
const buffer = await promisify2(zlib.brotliCompress)(input, {params: {[zlib.constants.BROTLI_PARAM_QUALITY]: 4}});
const options = this._getBrotliZlibOptions();
// @ts-expect-error promisify type failure on overload
const buffer = await promisify2(zlib.brotliCompress)(input, options);
return toArrayBuffer(buffer);
}

compressSync(input: ArrayBuffer): ArrayBuffer {
const buffer = zlib.brotliCompressSync(input, {params: {[zlib.constants.BROTLI_PARAM_QUALITY]: 4}});
const options = this._getBrotliZlibOptions();
const buffer = zlib.brotliCompressSync(input, options);
return toArrayBuffer(buffer);
}

Expand All @@ -52,4 +51,14 @@ export class BrotliCompressionZlib extends Compression {
const buffer = zlib.brotliDecompressSync(input);
return toArrayBuffer(buffer);
}

private _getBrotliZlibOptions(): BrotliOptions {
// {params: {[zlib.constants.BROTLI_PARAM_QUALITY]: 4}}
return {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: Compression.DEFAULT_COMPRESSION_LEVEL,
...this.options?.brotliZlib
}
};
}
}
45 changes: 20 additions & 25 deletions modules/compression/src/lib/brotli-compression.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
// BROTLI
import {isBrowser} from '@loaders.gl/loader-utils';
import type {CompressionOptions} from './compression';
import {Compression} from './compression';
import {BrotliCompressionZlib} from './brotli-compression-zlib';
import {BrotliCompressionZlib, BrotliCompressionZlibOptions} from './brotli-compression-zlib';
import type brotliNamespace from 'brotli';
// import brotli from 'brotli'; // https://bundlephobia.com/package/brotli
import type {BrotliOptions} from 'brotli';
// import brotli from 'brotli';
// import {BrotliDecode} from '../brotli/decode';

export type BrotliCompressionOptions = CompressionOptions & {
brotli?: {
mode?: number;
quality?: number;
lgwin?: number;
useZlib?: boolean;
};
};

const DEFAULT_BROTLI_OPTIONS = {
brotli: {
mode: 0,
quality: 8,
lgwin: 22
}
export type BrotliCompressionOptions = BrotliCompressionZlibOptions & {
brotli?: {};
};

let brotli: typeof brotliNamespace;

/**
* brotli compression / decompression
* Implemented with brotli package
* @see https://bundlephobia.com/package/brotli
*/
export class BrotliCompression extends Compression {
readonly name: string = 'brotli';
Expand All @@ -50,7 +39,8 @@ export class BrotliCompression extends Compression {
// dependency injection
brotli = brotli || this.options?.modules?.brotli || Compression.modules.brotli;

if (!isBrowser && this.options.brotli?.useZlib) {
if (!isBrowser && this.options.useZlib) {
// @ts-ignore public API is equivalent
return new BrotliCompressionZlib(options);
}
}
Expand All @@ -71,11 +61,9 @@ export class BrotliCompression extends Compression {
if (!brotli) {
throw new Error('brotli compression: brotli module not installed');
}

const brotliOptions = {...DEFAULT_BROTLI_OPTIONS.brotli, ...this.options?.brotli};
const options = this._getBrotliOptions();
const inputArray = new Uint8Array(input);

const outputArray = brotli.compress(inputArray, {quality: 5, brotliOptions});
const outputArray = brotli.compress(inputArray, options);
return outputArray.buffer;
}

Expand All @@ -84,13 +72,20 @@ export class BrotliCompression extends Compression {
throw new Error('brotli compression: brotli module not installed');
}

const brotliOptions = {...DEFAULT_BROTLI_OPTIONS.brotli, ...this.options?.brotli};
const options = this._getBrotliOptions();
const inputArray = new Uint8Array(input);

// @ts-ignore brotli types state that only Buffers are accepted...
const outputArray = brotli.decompress(inputArray, brotliOptions);
const outputArray = brotli.decompress(inputArray, options);
return outputArray.buffer;
// const outputArray = BrotliDecode(inputArray, undefined);
// return outputArray.buffer;
}

private _getBrotliOptions(): BrotliOptions {
return {
level: this.options.quality || Compression.DEFAULT_COMPRESSION_LEVEL,
...this.options?.brotli
};
}
}
31 changes: 26 additions & 5 deletions modules/compression/src/lib/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,41 @@ import {concatenateArrayBuffersAsync} from '@loaders.gl/loader-utils';

/** Compression options */
export type CompressionOptions = {
/** Compression quality typically goes from 1-11 (higher values better but slower) */
/**
* Compression quality (higher values better compression but exponentially slower)
* brotli goes from 1-11
* zlib goes from 1-9
* 5 or 6 is usually a good compromise
*/
quality?: number;
/** Injection of npm modules for large libraries */

/**
* Whether to use built-in Zlib on node.js for max performance (doesn't handle incremental compression)
* Currently only deflate, gzip and brotli are supported.
*/
useZlib?: boolean;

/**
* Injection of npm modules - keeps large compression libraries out of standard bundle
*/
modules?: CompressionModules;
};

/**
* Injection of npm modules - keeps large compression libraries out of standard bundle
*/
export type CompressionModules = {
brotli?: any;
lz4js?: any;
lzo?: any;
'zstd-codec'?: any;
}

};

/** Compression */
export abstract class Compression {
/** Default compression level for gzip, brotli etc */
static DEFAULT_COMPRESSION_LEVEL = 5;

/** Name of the compression */
abstract readonly name: string;
/** File extensions used for this */
Expand All @@ -28,7 +47,9 @@ export abstract class Compression {
/** Whether decompression is supported */
abstract readonly isSupported: boolean;
/** Whether compression is supported */
get isCompressionSupported(): boolean { return this.isSupported; };
get isCompressionSupported(): boolean {
return this.isSupported;
}

static modules: CompressionModules = {};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
// loaders.gl, MIT license
import type {CompressionOptions} from './compression';
import {isBrowser} from '@loaders.gl/loader-utils';
import {DeflateCompressionZlib, DeflateCompressionZlibOptions} from './deflate-compression-zlib';
import {Compression} from './compression';

import type {DeflateOptions} from 'fflate';
import {deflateSync, inflateSync} from 'fflate';
import type {DeflateOptions} from 'fflate'; // https://bundlephobia.com/package/pako

export type DeflateCompressionOptions = CompressionOptions & {
export type DeflateCompressionOptions = DeflateCompressionZlibOptions & {
deflate?: DeflateOptions;
};

/**
* DEFLATE compression / decompression
* Implementation using fflate
* @see https://bundlephobia.com/package/fflate
*/
export class DeflateCompression extends Compression {
readonly name: string = 'fflate';
readonly extensions: string[] = [];
readonly contentEncodings = ['fflate', 'gzip, zlib'];
readonly isSupported = true;
readonly isSupported: boolean = true;

readonly options: DeflateCompressionOptions;

constructor(options: DeflateCompressionOptions = {}) {
super(options);
this.options = options;
if (!isBrowser && this.options.useZlib) {
// @ts-ignore public API is equivalent
return new DeflateCompressionZlib(options);
}
}

// Async fflate uses Workers which interferes with loaders.gl
Expand Down
24 changes: 15 additions & 9 deletions modules/compression/src/lib/deflate-compression-pako.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// loaders.gl, MIT license
import type {CompressionOptions} from './compression';
import {isBrowser} from '@loaders.gl/loader-utils';
import {DeflateCompressionZlib, DeflateCompressionZlibOptions} from './deflate-compression-zlib';
import {Compression} from './compression';
import {getPakoError} from './utils/pako-utils';
import pako from 'pako'; // https://bundlephobia.com/package/pako
import pako from 'pako';

export type DeflateCompressionOptions = CompressionOptions & {
deflate?: pako.InflateOptions & pako.DeflateOptions & {useZlib?: boolean};
export type DeflateCompressionOptions = DeflateCompressionZlibOptions & {
deflate?: pako.InflateOptions & pako.DeflateOptions;
};

/**
* DEFLATE compression / decompression
* Using PAKO library
* Implementation using pako
* @see https://bundlephobia.com/package/pako
*/
export class DeflateCompression extends Compression {
readonly name: string = 'deflate';
Expand All @@ -25,6 +27,10 @@ export class DeflateCompression extends Compression {
constructor(options: DeflateCompressionOptions = {}) {
super(options);
this.options = options;
if (!isBrowser && this.options.useZlib) {
// @ts-ignore public API is equivalent
return new DeflateCompressionZlib(options);
}
}

async compress(input: ArrayBuffer): Promise<ArrayBuffer> {
Expand Down Expand Up @@ -63,7 +69,7 @@ export class DeflateCompression extends Compression {
yield* this.transformBatches(pakoProcessor, asyncIterator);
}

async *transformBatches(
private async *transformBatches(
pakoProcessor: pako.Inflate | pako.Deflate,
asyncIterator: AsyncIterable<ArrayBuffer> | Iterable<ArrayBuffer>
): AsyncIterable<ArrayBuffer> {
Expand All @@ -90,17 +96,17 @@ export class DeflateCompression extends Compression {
yield* chunks;
}

_onData(chunk) {
private _onData(chunk) {
this._chunks.push(chunk);
}

_onEnd(status) {
private _onEnd(status) {
if (status !== 0) {
throw new Error(getPakoError(status) + this._chunks.length);
}
}

_getChunks(): ArrayBuffer[] {
private _getChunks(): ArrayBuffer[] {
const chunks = this._chunks;
this._chunks = [];
return chunks;
Expand Down
Loading

0 comments on commit 66041f3

Please sign in to comment.