Skip to content

Commit f0db02c

Browse files
authored
feat(core): make it easier to create a cotar from tar and tar index (#542)
#### Description It is sometimes very useful to have two seperate files for a cotar, `.tar` and `.tar.index` but its somewhat hard to make the cotar from the two seperate files #### Intention Add a builder `Cotar.fromTarIndex(tar, index)` to make it easier to create them. #### Checklist *If not applicable, provide explanation of why.* - [ ] Tests updated - [ ] Docs updated - [ ] Issue linked in Title
1 parent 89139c2 commit f0db02c

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

packages/core/README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,28 @@ To fetch a single tile, the index has to be loaded into memory then the cotar ob
1010

1111
```typescript
1212
import { Cotar } from '@cotar/core';
13-
import { SourceUrl } from '@chunkd/source-url';
13+
import { SourceHttp } from '@chunkd/source-http';
1414

15-
const source = new SourceUrl('s3://linz-basemaps/topographic.tar.co');
16-
const cotar = Cotar.fromTar(source);
15+
const source = new SourceHttp('s3://linz-basemaps/topographic.tar.co');
16+
const cotar = await Cotar.fromTar(source);
1717

18-
// Fetch a gzipped PBF file from a tar
19-
const bytes = await cotar.get(`tiles/z10/5/5.pbf.gz`);
18+
// Fetch a gzipped PBF file from a tar
19+
const bytes = await cotar.get(`tiles/10/5/5.pbf.gz`);
20+
```
21+
22+
Index files can also be stored as separate files
23+
24+
```typescript
25+
import { Cotar } from '@cotar/core';
26+
import { SourceHttp } from '@chunkd/source-http';
27+
28+
const source = new SourceHttp('s3://linz-basemaps/topographic.tar');
29+
const sourceIndex = new SourceHttp('s3://linz-basemaps/topographic.tar.index');
30+
31+
const cotar = await Cotar.fromTarIndex(source, index);
32+
33+
// Fetch a gzipped PBF file from a tar
34+
const bytes = await cotar.get(`tiles/10/5/5.pbf.gz`);
2035
```
2136

2237
### Creating indexes

packages/core/src/cotar.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ export class Cotar {
2121
this.index = index;
2222
}
2323

24+
/**
25+
* Create a cotar using two sources, one for the tar, one for the index
26+
*
27+
* @param source the tar asset file generally a file ending with ".tar"
28+
* @param index the tar index generally a file ending with ".tar.index"
29+
*
30+
* @returns a Cotar instance
31+
*
32+
*/
33+
static async fromTarIndex(source: Source, index: Source): Promise<Cotar> {
34+
const metadata = await CotarIndex.create(index);
35+
return new Cotar(source, metadata);
36+
}
37+
38+
/**
39+
* Create a cotar from a single archive, generally a ".tar.co"
40+
*
41+
* @param source asset containing both the tar and the tar index.
42+
*
43+
* @returns a Cotar instance
44+
*/
2445
static async fromTar(source: Source): Promise<Cotar> {
2546
// Load the last file in the tar archive
2647
const metadata = await CotarIndex.getMetadata(source, 0, false);

0 commit comments

Comments
 (0)