Skip to content

Commit

Permalink
Merge pull request #1524 from silx-kit/more-plugins
Browse files Browse the repository at this point in the history
Support Blosc2 and Bitshuffle plugins
  • Loading branch information
axelboc authored Feb 8, 2024
2 parents 20ded3e + 3d59fa1 commit 7fe42d7
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@h5web/h5wasm": "workspace:*",
"axios": "1.6.2",
"axios-hooks": "5.0.2",
"h5wasm-plugins": "0.0.1",
"h5wasm-plugins": "0.0.3",
"normalize.css": "8.0.1",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
23 changes: 14 additions & 9 deletions apps/demo/src/h5wasm/plugin-utils.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
// Import compression plugins as static assets (i.e. as URLs)
// cf. `vite.config.ts` and `src/vite-env.d.ts
import { Plugin } from '@h5web/h5wasm';
import blosc from 'h5wasm-plugins/plugins/libH5Zblosc.so';
import blosc2 from 'h5wasm-plugins/plugins/libH5Zblosc2.so';
import bshuf from 'h5wasm-plugins/plugins/libH5Zbshuf.so';
import bz2 from 'h5wasm-plugins/plugins/libH5Zbz2.so';
import lz4 from 'h5wasm-plugins/plugins/libH5Zlz4.so';
import lzf from 'h5wasm-plugins/plugins/libH5Zlzf.so';
import szf from 'h5wasm-plugins/plugins/libH5Zszf.so';
import zfp from 'h5wasm-plugins/plugins/libH5Zzfp.so';
import zstd from 'h5wasm-plugins/plugins/libH5Zzstd.so';

const PLUGINS: Record<string, string> = {
blosc,
bz2,
lz4,
lzf,
szf,
zfp,
zstd,
const PLUGINS: Record<Plugin, string> = {
[Plugin.Blosc]: blosc,
[Plugin.Blosc2]: blosc2,
[Plugin.Bitshuffle]: bshuf,
[Plugin.BZIP2]: bz2,
[Plugin.LZ4]: lz4,
[Plugin.LZF]: lzf,
[Plugin.SZ]: szf,
[Plugin.ZFP]: zfp,
[Plugin.Zstandard]: zstd,
};

export async function getPlugin(
name: string,
name: Plugin,
): Promise<ArrayBuffer | undefined> {
if (!PLUGINS[name]) {
return undefined;
Expand Down
12 changes: 9 additions & 3 deletions packages/h5wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ See
this time, so if you don't provide your own, the export menu will remain
disabled in the toolbar.

#### `getPlugin?: (name: string) => Promise<ArrayBuffer | undefined>`
#### `getPlugin?: (name: Plugin) => Promise<ArrayBuffer | undefined>`

If provided, this aysnchronous function is invoked when loading a compressed
dataset. It receives the name of a compression plugin as parameter and should
Expand All @@ -184,6 +184,8 @@ A typical implementation of `getPlugin` in a bundled front-end application might
look like this:

```ts
import type { Plugin } from '@h5web/h5wasm';

/*
* Import the plugins' source files as static assets (i.e. as URLs).
* The exact syntax may vary depending on your bundler (Vite, webpack ...)
Expand All @@ -193,9 +195,13 @@ import blosc from 'h5wasm-plugins/plugins/libH5Zblosc.so';
import bz2 from 'h5wasm-plugins/plugins/libH5Zbz2.so';
// ...

const PLUGINS = { blosc, bz2 /* ... */ };
const PLUGINS: Record<Plugin, string> = {
[Plugin.Blosc]: blosc,
[Plugin.BZIP2]: bz2,
// ...
};

async function getPlugin(name: string): Promise<ArrayBuffer | undefined> {
async function getPlugin(name: Plugin): Promise<ArrayBuffer | undefined> {
if (!PLUGINS[name]) {
return undefined;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/h5wasm/src/H5WasmProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import type { PropsWithChildren } from 'react';
import { useEffect, useState } from 'react';

import { H5WasmApi } from './h5wasm-api';
import type { Plugin } from './utils';

interface Props {
filename: string;
buffer: ArrayBuffer;
getExportURL?: DataProviderApi['getExportURL'];
getPlugin?: (name: string) => Promise<ArrayBuffer | undefined>;
getPlugin?: (name: Plugin) => Promise<ArrayBuffer | undefined>;
}

function H5WasmProvider(props: PropsWithChildren<Props>) {
Expand Down
3 changes: 2 additions & 1 deletion packages/h5wasm/src/h5wasm-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { nanoid } from 'nanoid';

import { assertH5WasmDataset, hasInt64Type } from './guards';
import type { H5WasmEntity } from './models';
import type { Plugin } from './utils';
import {
convertSelectionToRanges,
parseEntity,
Expand All @@ -40,7 +41,7 @@ export class H5WasmApi extends DataProviderApi {
buffer: ArrayBuffer,
private readonly _getExportURL?: DataProviderApi['getExportURL'],
private readonly getPlugin?: (
name: string,
name: Plugin,
) => Promise<ArrayBuffer | undefined>,
) {
super(filename);
Expand Down
1 change: 1 addition & 0 deletions packages/h5wasm/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as H5WasmProvider } from './H5WasmProvider';
export { Plugin } from './utils';
30 changes: 22 additions & 8 deletions packages/h5wasm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,29 @@ import {
import type { H5WasmAttributes, H5WasmEntity } from './models';

// https://github.com/h5wasm/h5wasm-plugins#included-plugins
export enum Plugin {
Bitshuffle = 'bshuf',
Blosc = 'blosc',
Blosc2 = 'blosc2',
BZIP2 = 'bz2',
LZ4 = 'lz4',
LZF = 'lzf',
SZ = 'szf',
ZFP = 'zfp',
Zstandard = 'zstd',
}

// https://support.hdfgroup.org/services/contributions.html
export const PLUGINS_BY_FILTER_ID: Record<number, string> = {
307: 'bz2',
32_000: 'lzf',
32_001: 'blosc',
32_004: 'lz4',
32_013: 'zfp',
32_015: 'zstd',
32_017: 'szf',
export const PLUGINS_BY_FILTER_ID: Record<number, Plugin> = {
307: Plugin.BZIP2,
32_000: Plugin.LZF,
32_001: Plugin.Blosc,
32_004: Plugin.LZ4,
32_008: Plugin.Bitshuffle,
32_013: Plugin.ZFP,
32_015: Plugin.Zstandard,
32_017: Plugin.SZ,
32_026: Plugin.Blosc2,
};

export function parseEntity(
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7fe42d7

Please sign in to comment.