@loaders.gl/zip
Advanced tools
Comparing version 4.2.0-alpha.1 to 4.2.0-alpha.2
import { FileSystem } from '@loaders.gl/loader-utils'; | ||
import { FileProvider } from '@loaders.gl/loader-utils'; | ||
import { ZipCDFileHeader } from '../parse-zip/cd-file-header'; | ||
import { IndexedArchive } from './IndexedArchive'; | ||
export type CompressionHandler = (compressedFile: ArrayBuffer) => Promise<ArrayBuffer>; | ||
/** Handling different compression types in zip */ | ||
export declare const ZIP_COMPRESSION_HANDLERS: { | ||
[key: number]: CompressionHandler; | ||
}; | ||
/** | ||
@@ -12,2 +18,3 @@ * FileSystem adapter for a ZIP file | ||
fileName?: string; | ||
archive: IndexedArchive | null; | ||
/** | ||
@@ -17,3 +24,3 @@ * Constructor | ||
*/ | ||
constructor(file: FileProvider | string); | ||
constructor(file: FileProvider | IndexedArchive | string); | ||
/** Clean up resources */ | ||
@@ -20,0 +27,0 @@ destroy(): Promise<void>; |
@@ -7,3 +7,4 @@ import { isBrowser } from '@loaders.gl/loader-utils'; | ||
import { DeflateCompression } from '@loaders.gl/compression'; | ||
const COMPRESSION_METHODS = { | ||
import { IndexedArchive } from "./IndexedArchive.js"; | ||
export const ZIP_COMPRESSION_HANDLERS = { | ||
0: async compressedFile => compressedFile, | ||
@@ -22,2 +23,3 @@ 8: async compressedFile => { | ||
this.fileName = void 0; | ||
this.archive = null; | ||
if (typeof file === 'string') { | ||
@@ -30,2 +32,6 @@ this.fileName = file; | ||
} | ||
} else if (file instanceof IndexedArchive) { | ||
this.fileProvider = file.fileProvider; | ||
this.archive = file; | ||
this.fileName = file.fileName; | ||
} else if (isFileProvider(file)) { | ||
@@ -59,19 +65,24 @@ this.fileProvider = file; | ||
async fetch(filename) { | ||
if (!this.fileProvider) { | ||
throw new Error('No data detected in the zip archive'); | ||
let uncompressedFile; | ||
if (this.archive) { | ||
uncompressedFile = await this.archive.getFile(filename, 'http'); | ||
} else { | ||
if (!this.fileProvider) { | ||
throw new Error('No data detected in the zip archive'); | ||
} | ||
const cdFileHeader = await this.getCDFileHeader(filename); | ||
const localFileHeader = await parseZipLocalFileHeader(cdFileHeader.localHeaderOffset, this.fileProvider); | ||
if (!localFileHeader) { | ||
throw new Error('Local file header has not been found in the zip archive`'); | ||
} | ||
const compressionHandler = ZIP_COMPRESSION_HANDLERS[localFileHeader.compressionMethod.toString()]; | ||
if (!compressionHandler) { | ||
throw Error('Only Deflation compression is supported'); | ||
} | ||
const compressedFile = await this.fileProvider.slice(localFileHeader.fileDataOffset, localFileHeader.fileDataOffset + localFileHeader.compressedSize); | ||
uncompressedFile = await compressionHandler(compressedFile); | ||
} | ||
const cdFileHeader = await this.getCDFileHeader(filename); | ||
const localFileHeader = await parseZipLocalFileHeader(cdFileHeader.localHeaderOffset, this.fileProvider); | ||
if (!localFileHeader) { | ||
throw new Error('Local file header has not been found in the zip archive`'); | ||
} | ||
const compressionHandler = COMPRESSION_METHODS[localFileHeader.compressionMethod.toString()]; | ||
if (!compressionHandler) { | ||
throw Error('Only Deflation compression is supported'); | ||
} | ||
const compressedFile = await this.fileProvider.slice(localFileHeader.fileDataOffset, localFileHeader.fileDataOffset + localFileHeader.compressedSize); | ||
const uncompressedFile = await compressionHandler(compressedFile); | ||
const response = new Response(uncompressedFile); | ||
Object.defineProperty(response, 'url', { | ||
value: `${this.fileName || ''}/${filename}` | ||
value: filename ? `${this.fileName || ''}/${filename}` : this.fileName || '' | ||
}); | ||
@@ -78,0 +89,0 @@ return response; |
@@ -9,4 +9,6 @@ export { ZipLoader } from './zip-loader'; | ||
export { addOneFile, createZip } from './parse-zip/zip-composition'; | ||
export { IndexedArchive } from './filesystems/IndexedArchive'; | ||
export { parseHashTable, makeHashTableFromZipHeaders, composeHashFile } from './hash-file-utility'; | ||
export { ZipFileSystem } from './filesystems/zip-filesystem'; | ||
export { ZipFileSystem, ZIP_COMPRESSION_HANDLERS } from './filesystems/zip-filesystem'; | ||
export type { CompressionHandler } from './filesystems/zip-filesystem'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -9,4 +9,5 @@ export { ZipLoader } from "./zip-loader.js"; | ||
export { addOneFile, createZip } from "./parse-zip/zip-composition.js"; | ||
export { IndexedArchive } from "./filesystems/IndexedArchive.js"; | ||
export { parseHashTable, makeHashTableFromZipHeaders, composeHashFile } from "./hash-file-utility.js"; | ||
export { ZipFileSystem } from "./filesystems/zip-filesystem.js"; | ||
export { ZipFileSystem, ZIP_COMPRESSION_HANDLERS } from "./filesystems/zip-filesystem.js"; | ||
//# sourceMappingURL=index.js.map |
import JSZip from 'jszip'; | ||
const VERSION = typeof "4.2.0-alpha.1" !== 'undefined' ? "4.2.0-alpha.1" : 'latest'; | ||
const VERSION = typeof "4.2.0-alpha.2" !== 'undefined' ? "4.2.0-alpha.2" : 'latest'; | ||
export const ZipLoader = { | ||
@@ -4,0 +4,0 @@ id: 'zip', |
import JSZip from 'jszip'; | ||
const VERSION = typeof "4.2.0-alpha.1" !== 'undefined' ? "4.2.0-alpha.1" : 'latest'; | ||
const VERSION = typeof "4.2.0-alpha.2" !== 'undefined' ? "4.2.0-alpha.2" : 'latest'; | ||
export const ZipWriter = { | ||
@@ -4,0 +4,0 @@ name: 'Zip Archive', |
{ | ||
"name": "@loaders.gl/zip", | ||
"version": "4.2.0-alpha.1", | ||
"version": "4.2.0-alpha.2", | ||
"description": "Zip Archive Loader", | ||
@@ -41,9 +41,9 @@ "license": "MIT", | ||
"dependencies": { | ||
"@loaders.gl/compression": "4.2.0-alpha.1", | ||
"@loaders.gl/crypto": "4.2.0-alpha.1", | ||
"@loaders.gl/loader-utils": "4.2.0-alpha.1", | ||
"@loaders.gl/compression": "4.2.0-alpha.2", | ||
"@loaders.gl/crypto": "4.2.0-alpha.2", | ||
"@loaders.gl/loader-utils": "4.2.0-alpha.2", | ||
"jszip": "^3.1.5", | ||
"md5": "^2.3.0" | ||
}, | ||
"gitHead": "d4da81f4d8fb2a3b43b0e025109cf7ccfb317d4c" | ||
"gitHead": "d66a6a4626ea84c5f2cad5fa5cf7ebb6943c57c8" | ||
} |
@@ -11,6 +11,7 @@ // loaders.gl | ||
import {DeflateCompression} from '@loaders.gl/compression'; | ||
import {IndexedArchive} from './IndexedArchive'; | ||
type CompressionHandler = (compressedFile: ArrayBuffer) => Promise<ArrayBuffer>; | ||
export type CompressionHandler = (compressedFile: ArrayBuffer) => Promise<ArrayBuffer>; | ||
/** Handling different compression types in zip */ | ||
const COMPRESSION_METHODS: {[key: number]: CompressionHandler} = { | ||
export const ZIP_COMPRESSION_HANDLERS: {[key: number]: CompressionHandler} = { | ||
/** No compression */ | ||
@@ -34,2 +35,3 @@ 0: async (compressedFile) => compressedFile, | ||
public fileName?: string; | ||
public archive: IndexedArchive | null = null; | ||
@@ -40,3 +42,3 @@ /** | ||
*/ | ||
constructor(file: FileProvider | string) { | ||
constructor(file: FileProvider | IndexedArchive | string) { | ||
// Try to open file in NodeJS | ||
@@ -50,2 +52,6 @@ if (typeof file === 'string') { | ||
} | ||
} else if (file instanceof IndexedArchive) { | ||
this.fileProvider = file.fileProvider; | ||
this.archive = file; | ||
this.fileName = file.fileName; | ||
} else if (isFileProvider(file)) { | ||
@@ -95,28 +101,36 @@ this.fileProvider = file; | ||
async fetch(filename: string): Promise<Response> { | ||
if (!this.fileProvider) { | ||
throw new Error('No data detected in the zip archive'); | ||
} | ||
const cdFileHeader = await this.getCDFileHeader(filename); | ||
const localFileHeader = await parseZipLocalFileHeader( | ||
cdFileHeader.localHeaderOffset, | ||
this.fileProvider | ||
); | ||
if (!localFileHeader) { | ||
throw new Error('Local file header has not been found in the zip archive`'); | ||
} | ||
let uncompressedFile: ArrayBuffer; | ||
if (this.archive) { | ||
uncompressedFile = await this.archive.getFile(filename, 'http'); | ||
} else { | ||
if (!this.fileProvider) { | ||
throw new Error('No data detected in the zip archive'); | ||
} | ||
const cdFileHeader = await this.getCDFileHeader(filename); | ||
const localFileHeader = await parseZipLocalFileHeader( | ||
cdFileHeader.localHeaderOffset, | ||
this.fileProvider | ||
); | ||
if (!localFileHeader) { | ||
throw new Error('Local file header has not been found in the zip archive`'); | ||
} | ||
const compressionHandler = COMPRESSION_METHODS[localFileHeader.compressionMethod.toString()]; | ||
if (!compressionHandler) { | ||
throw Error('Only Deflation compression is supported'); | ||
} | ||
const compressionHandler = | ||
ZIP_COMPRESSION_HANDLERS[localFileHeader.compressionMethod.toString()]; | ||
if (!compressionHandler) { | ||
throw Error('Only Deflation compression is supported'); | ||
} | ||
const compressedFile = await this.fileProvider.slice( | ||
localFileHeader.fileDataOffset, | ||
localFileHeader.fileDataOffset + localFileHeader.compressedSize | ||
); | ||
const compressedFile = await this.fileProvider.slice( | ||
localFileHeader.fileDataOffset, | ||
localFileHeader.fileDataOffset + localFileHeader.compressedSize | ||
); | ||
const uncompressedFile = await compressionHandler(compressedFile); | ||
uncompressedFile = await compressionHandler(compressedFile); | ||
} | ||
const response = new Response(uncompressedFile); | ||
Object.defineProperty(response, 'url', {value: `${this.fileName || ''}/${filename}`}); | ||
Object.defineProperty(response, 'url', { | ||
value: filename ? `${this.fileName || ''}/${filename}` : this.fileName || '' | ||
}); | ||
return response; | ||
@@ -123,0 +137,0 @@ } |
@@ -25,4 +25,6 @@ // loaders.gl | ||
// export type {HashElement} from './hash-file-utility'; | ||
export {IndexedArchive} from './filesystems/IndexedArchive'; | ||
export {parseHashTable, makeHashTableFromZipHeaders, composeHashFile} from './hash-file-utility'; | ||
export {ZipFileSystem} from './filesystems/zip-filesystem'; | ||
export {ZipFileSystem, ZIP_COMPRESSION_HANDLERS} from './filesystems/zip-filesystem'; | ||
export type {CompressionHandler} from './filesystems/zip-filesystem'; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
686349
90
14429
+ Added@loaders.gl/compression@4.2.0-alpha.2(transitive)
+ Added@loaders.gl/crypto@4.2.0-alpha.2(transitive)
+ Added@loaders.gl/loader-utils@4.2.0-alpha.2(transitive)
+ Added@loaders.gl/worker-utils@4.2.0-alpha.2(transitive)
- Removed@loaders.gl/compression@4.2.0-alpha.1(transitive)
- Removed@loaders.gl/crypto@4.2.0-alpha.1(transitive)
- Removed@loaders.gl/loader-utils@4.2.0-alpha.1(transitive)
- Removed@loaders.gl/worker-utils@4.2.0-alpha.1(transitive)