Skip to content

Commit 8a82213

Browse files
authored
Merge pull request emscripten-forge#31 from martinRenou/change_file_data_type
Change data structure for output filesdata
2 parents 6aba817 + 56c9e1e commit 8a82213

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

src/index.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import initializeWasm from './helper';
2-
import { IFileData, IUnpackJSAPI } from './types';
2+
import { FilesData, IUnpackJSAPI } from './types';
33

44
const fetchByteArray = async (url: string): Promise<Uint8Array> => {
55
const response = await fetch(url);
@@ -13,7 +13,7 @@ const fetchByteArray = async (url: string): Promise<Uint8Array> => {
1313
export const initUntarJS = async (): Promise<IUnpackJSAPI> => {
1414
const wasmModule = await initializeWasm();
1515

16-
const extractData = async (data: Uint8Array): Promise<IFileData[]> => {
16+
const extractData = async (data: Uint8Array): Promise<FilesData> => {
1717
/**Since WebAssembly, memory is accessed using pointers
1818
and the first parameter of extract_archive method from unpack.c, which is Uint8Array of file data, should be a pointer
1919
so we have to allocate memory for file data
@@ -60,12 +60,12 @@ export const initUntarJS = async (): Promise<IUnpackJSAPI> => {
6060
'Error:',
6161
errorMessage
6262
);
63-
return [];
63+
return {};
6464
}
6565
const filesPtr = wasmModule.getValue(resultPtr, 'i32');
6666
const fileCount = wasmModule.getValue(resultPtr + 4, 'i32');
6767

68-
const files: IFileData[] = [];
68+
const files: FilesData = {};
6969

7070
/**
7171
* FilesPtr is a pointer that refers to an instance of the FileData in unpack.c
@@ -97,10 +97,7 @@ export const initUntarJS = async (): Promise<IUnpackJSAPI> => {
9797
dataSize
9898
);
9999

100-
files.push({
101-
filename: filename,
102-
data: fileData
103-
});
100+
files[filename] = fileData;
104101
}
105102

106103
wasmModule._free(inputPtr);
@@ -111,17 +108,17 @@ export const initUntarJS = async (): Promise<IUnpackJSAPI> => {
111108
return files;
112109
} catch (error) {
113110
console.error('Error during extraction:', error);
114-
return [];
111+
return {};
115112
}
116113
};
117114

118-
const extract = async (url: string): Promise<IFileData[]> => {
115+
const extract = async (url: string): Promise<FilesData> => {
119116
try {
120117
const data = await fetchByteArray(url);
121118
return await extractData(data);
122119
} catch (error) {
123120
console.error('Error during extracting:', error);
124-
return [];
121+
return {};
125122
}
126123
}
127124

src/types.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
export interface IFileData {
2-
filename: string;
3-
data: Uint8Array;
4-
}
1+
export type FilesData = {[filename: string]: Uint8Array};
52

63
export interface IUnpackJSAPI {
7-
extractData: (data: Uint8Array) => Promise<IFileData[]>;
8-
extract: (url: string) => Promise<IFileData[]>;
4+
extractData: (data: Uint8Array) => Promise<FilesData>;
5+
extract: (url: string) => Promise<FilesData>;
96
}

0 commit comments

Comments
 (0)