Skip to content

Commit

Permalink
Add createExtractorFromFile support in Webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
YuJianrong committed Mar 17, 2022
1 parent f4b6c8a commit 35b02b5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ npm install node-unrar-js

- `data: ArrayBuffer` : ArrayBuffer object of the RAR archive file
- `password?: string` : _Optional_ password
- `wasmBinary? ArrayBuffer;` : _Optional_ Use in browser, the wasm binary must be loaded in the code and send to this function to load the wasm code
- `wasmBinary? ArrayBuffer;` : _Optional_ Use in browser/webpack, the wasm binary must be loaded in the code and send to this function to load the wasm code

- `async function createExtractorFromFile(options: ExtractorFromFileOptions): Promise<Extractor>` - Get the File Extractor

Expand All @@ -45,6 +45,7 @@ npm install node-unrar-js
- `targetPath?: string` : _Optional_ target folder
- `password?: string` : _Optional_ password
- `filenameTransform?: (filename: string) => string`: _Optional_ transform the file name before it's created on file system
- `wasmBinary? ArrayBuffer;` : _Optional_ Use in nodejs/webpack, the wasm binary must be loaded in the code and send to this function to load the wasm code in webpack based nodejs project (please read [Used in Webpack-bundled NodeJS Project](#use-in-webpack-bundled-nodejs-project) for more details).

_Node_: This function is not available in EM2015 Module since the EM2015 Module is used for webpack in Browser.

Expand Down Expand Up @@ -176,10 +177,27 @@ async function main() {
main();
```
## Demo in webpack
## Demo in Webpack
This package can also be used in browser by Webpack, please visit the [demo project](https://github.com/YuJianrong/node-unrar.js/tree/master/demo/web) to see how to use it in webpack.
## Use in Webpack-bundled NodeJS Project
In most cases the exported ESModule is used in browser by Webpack, but in case if the NodeJs project (or an Electron project) is bundled by Webpack, the `wasmBinary` data must be loaded manually just like the browser by Webpack, it can be loaded like this:
```Typescript
import fs from 'fs';
import { createExtractorFromFile } from 'node-unrar-js/esm';

const wasmBinary = fs.readFileSync(require.resolve('node-unrar-js/esm/js/unrar.wasm'));

const extractor = await createExtractorFromFile({ wasmBinary, filepath: './WithComment.rar' });

// const list = extractor.getFileList();
```
Note: the package must be loaded from `'node-unrar-js/esm'` instead of `'node-unrar-js'` to enable the function `createExtractorFromFile` in ES Module.
## TypeScript
This module is written in [TypeScript](https://www.typescriptlang.org/), you can import it directly in TypeScript and get the benefit of static type checking and auto-complete of IDE.
Expand Down Expand Up @@ -212,6 +230,10 @@ This module is licensed under MIT.
### Changelog
#### 1.0.6 (2022-03-16)
- Add `createExtractorFromFile` support in Webpack
#### 1.0.5 (2022-02-28)
- Fix an issue on file extracting to current working directory ('./')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-unrar-js",
"version": "1.0.5",
"version": "1.0.6",
"description": "Pure JavaScript RAR archive extractor by compile the official unrar lib by Emscripten.",
"homepage": "https://github.com/YuJianrong/node-unrar.js",
"bugs": {
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { getUnrar } from './js/unrar.singleton';
export * from './index.esm';

export interface ExtractorFromFileOptions {
wasmBinary?: ArrayBuffer;
filepath: string;
targetPath?: string;
password?: string;
filenameTransform?: (filename: string) => string;
}

export async function createExtractorFromFile({
wasmBinary,
filepath,
targetPath = '',
password = '',
filenameTransform = (filename: string) => filename,
}: ExtractorFromFileOptions): Promise<Extractor> {
const unrar = await getUnrar();
const unrar = await getUnrar(wasmBinary && { wasmBinary });
const extractor = new ExtractorFile(unrar, filepath, targetPath, password, filenameTransform);
unrar.extractor = extractor;
return extractor;
Expand Down

0 comments on commit 35b02b5

Please sign in to comment.