fix(ziptools,gziptools): Use fflate synchronous APIs for ZIP and GZIP operations for Deno compatibility; add TEntryFilter type and small docs/tests cleanup
This commit is contained in:
@@ -32,20 +32,209 @@ tap.preTask('should prepare downloads', async (tools) => {
|
||||
);
|
||||
});
|
||||
|
||||
tap.test('should extract existing files on disk', async () => {
|
||||
const testSmartarchive = await smartarchive.SmartArchive.fromUrl(
|
||||
'https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz',
|
||||
);
|
||||
await testSmartarchive.extractToDirectory(testPaths.nogitDir);
|
||||
tap.test('should extract existing files on disk using fluent API', async () => {
|
||||
await smartarchive.SmartArchive.create()
|
||||
.url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz')
|
||||
.extract(testPaths.nogitDir);
|
||||
});
|
||||
|
||||
tap.test('should extract from file using fluent API', async () => {
|
||||
const extractPath = plugins.path.join(testPaths.nogitDir, 'from-file-test');
|
||||
await plugins.fsPromises.mkdir(extractPath, { recursive: true });
|
||||
|
||||
await smartarchive.SmartArchive.create()
|
||||
.file(plugins.path.join(testPaths.nogitDir, 'test.tgz'))
|
||||
.extract(extractPath);
|
||||
|
||||
const files = await plugins.listFileTree(extractPath, '**/*');
|
||||
expect(files.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
tap.test('should extract with stripComponents using fluent API', async () => {
|
||||
const extractPath = plugins.path.join(testPaths.nogitDir, 'strip-test');
|
||||
await plugins.fsPromises.mkdir(extractPath, { recursive: true });
|
||||
|
||||
await smartarchive.SmartArchive.create()
|
||||
.url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz')
|
||||
.stripComponents(1)
|
||||
.extract(extractPath);
|
||||
|
||||
const files = await plugins.listFileTree(extractPath, '**/*');
|
||||
expect(files.length).toBeGreaterThan(0);
|
||||
// Files should not have 'package/' prefix
|
||||
const hasPackagePrefix = files.some(f => f.startsWith('package/'));
|
||||
expect(hasPackagePrefix).toBeFalse();
|
||||
});
|
||||
|
||||
tap.test('should extract with filter using fluent API', async () => {
|
||||
const extractPath = plugins.path.join(testPaths.nogitDir, 'filter-test');
|
||||
await plugins.fsPromises.mkdir(extractPath, { recursive: true });
|
||||
|
||||
await smartarchive.SmartArchive.create()
|
||||
.url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz')
|
||||
.filter(entry => entry.path.endsWith('.json'))
|
||||
.extract(extractPath);
|
||||
|
||||
const files = await plugins.listFileTree(extractPath, '**/*');
|
||||
// All extracted files should be JSON
|
||||
for (const file of files) {
|
||||
expect(file.endsWith('.json')).toBeTrue();
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should list archive entries using fluent API', async () => {
|
||||
const entries = await smartarchive.SmartArchive.create()
|
||||
.url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz')
|
||||
.list();
|
||||
|
||||
expect(entries.length).toBeGreaterThan(0);
|
||||
const hasPackageJson = entries.some(e => e.path.includes('package.json'));
|
||||
expect(hasPackageJson).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should create archive using fluent API', async () => {
|
||||
const archive = await smartarchive.SmartArchive.create()
|
||||
.format('tar.gz')
|
||||
.compression(9)
|
||||
.entry('hello.txt', 'Hello World!')
|
||||
.entry('config.json', JSON.stringify({ name: 'test', version: '1.0.0' }));
|
||||
|
||||
expect(archive).toBeInstanceOf(smartarchive.SmartArchive);
|
||||
|
||||
const buffer = await archive.toBuffer();
|
||||
expect(buffer.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
tap.test('should create and write archive to file using fluent API', async () => {
|
||||
const outputPath = plugins.path.join(testPaths.nogitDir, 'created-archive.tar.gz');
|
||||
|
||||
await smartarchive.SmartArchive.create()
|
||||
.format('tar.gz')
|
||||
.entry('readme.txt', 'This is a test archive')
|
||||
.entry('data/info.json', JSON.stringify({ created: new Date().toISOString() }))
|
||||
.toFile(outputPath);
|
||||
|
||||
// Verify file was created
|
||||
const stats = await plugins.fsPromises.stat(outputPath);
|
||||
expect(stats.size).toBeGreaterThan(0);
|
||||
|
||||
// Verify we can extract it
|
||||
const extractPath = plugins.path.join(testPaths.nogitDir, 'verify-created');
|
||||
await smartarchive.SmartArchive.create()
|
||||
.file(outputPath)
|
||||
.extract(extractPath);
|
||||
|
||||
const files = await plugins.listFileTree(extractPath, '**/*');
|
||||
expect(files).toContain('readme.txt');
|
||||
});
|
||||
|
||||
tap.test('should create ZIP archive using fluent API', async () => {
|
||||
const outputPath = plugins.path.join(testPaths.nogitDir, 'created-archive.zip');
|
||||
|
||||
await smartarchive.SmartArchive.create()
|
||||
.format('zip')
|
||||
.entry('file1.txt', 'Content 1')
|
||||
.entry('file2.txt', 'Content 2')
|
||||
.toFile(outputPath);
|
||||
|
||||
// Verify file was created
|
||||
const stats = await plugins.fsPromises.stat(outputPath);
|
||||
expect(stats.size).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
tap.test('should extract to SmartFiles using fluent API', async () => {
|
||||
const smartFiles = await smartarchive.SmartArchive.create()
|
||||
.url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz')
|
||||
.toSmartFiles();
|
||||
|
||||
expect(smartFiles.length).toBeGreaterThan(0);
|
||||
|
||||
const packageJson = smartFiles.find(f => f.relative.includes('package.json'));
|
||||
expect(packageJson).toBeDefined();
|
||||
});
|
||||
|
||||
tap.test('should analyze archive using fluent API', async () => {
|
||||
const info = await smartarchive.SmartArchive.create()
|
||||
.file(plugins.path.join(testPaths.nogitDir, 'test.tgz'))
|
||||
.analyze();
|
||||
|
||||
expect(info.isArchive).toBeTrue();
|
||||
expect(info.isCompressed).toBeTrue();
|
||||
expect(info.format).toEqual('gz');
|
||||
});
|
||||
|
||||
tap.test('should check if file exists in archive using fluent API', async () => {
|
||||
const hasPackageJson = await smartarchive.SmartArchive.create()
|
||||
.url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz')
|
||||
.hasFile('package.json');
|
||||
|
||||
expect(hasPackageJson).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should extract single file using fluent API', async () => {
|
||||
const packageJson = await smartarchive.SmartArchive.create()
|
||||
.url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz')
|
||||
.extractFile('package.json');
|
||||
|
||||
expect(packageJson).toBeDefined();
|
||||
expect(packageJson!.contents.toString()).toContain('websetup');
|
||||
});
|
||||
|
||||
tap.test('should handle include/exclude patterns', async () => {
|
||||
const smartFiles = await smartarchive.SmartArchive.create()
|
||||
.url('https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz')
|
||||
.include(/\.json$/)
|
||||
.toSmartFiles();
|
||||
|
||||
expect(smartFiles.length).toBeGreaterThan(0);
|
||||
for (const file of smartFiles) {
|
||||
expect(file.relative.endsWith('.json')).toBeTrue();
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should throw error when mixing modes', async () => {
|
||||
let threw = false;
|
||||
try {
|
||||
smartarchive.SmartArchive.create()
|
||||
.url('https://example.com/archive.tgz')
|
||||
.entry('file.txt', 'content'); // This should throw
|
||||
} catch (e) {
|
||||
threw = true;
|
||||
expect((e as Error).message).toContain('extraction mode');
|
||||
}
|
||||
expect(threw).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should throw error when no source configured', async () => {
|
||||
let threw = false;
|
||||
try {
|
||||
await smartarchive.SmartArchive.create().extract('./output');
|
||||
} catch (e) {
|
||||
threw = true;
|
||||
expect((e as Error).message).toContain('No source configured');
|
||||
}
|
||||
expect(threw).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should throw error when no format configured', async () => {
|
||||
let threw = false;
|
||||
try {
|
||||
await smartarchive.SmartArchive.create()
|
||||
.entry('file.txt', 'content')
|
||||
.toBuffer();
|
||||
} catch (e) {
|
||||
threw = true;
|
||||
expect((e as Error).message).toContain('No format specified');
|
||||
}
|
||||
expect(threw).toBeTrue();
|
||||
});
|
||||
|
||||
tap.skip.test('should extract a b2zip', async () => {
|
||||
const dataUrl =
|
||||
'https://daten.offeneregister.de/de_companies_ocdata.jsonl.bz2';
|
||||
const testArchive = await smartarchive.SmartArchive.fromUrl(dataUrl);
|
||||
await testArchive.extractToDirectory(
|
||||
plugins.path.join(testPaths.nogitDir, 'de_companies_ocdata.jsonl'),
|
||||
);
|
||||
await smartarchive.SmartArchive.create()
|
||||
.url(dataUrl)
|
||||
.extract(plugins.path.join(testPaths.nogitDir, 'de_companies_ocdata.jsonl'));
|
||||
});
|
||||
|
||||
await tap.start();
|
||||
export default tap.start();
|
||||
|
||||
Reference in New Issue
Block a user