Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Upcoming release

-
- Add `GtfsSqlJs.fromZipData(zipData, options?)` static method for loading from pre-loaded ZIP data (`ArrayBuffer` or `Uint8Array`)
- Keep `GtfsSqlJs.fromZip(zipPath, options?)` strictly typed as `string` for paths/URLs

## 0.2.2

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ Full API documentation: [API Reference](https://sysdevrun.github.io/gtfs-sqljs/d

### Static Methods

- `GtfsSqlJs.fromZip(zipPath, options?)` - Create instance from GTFS ZIP file
- `GtfsSqlJs.fromZip(zipPath, options?)` - Create instance from GTFS ZIP file path or URL
- `GtfsSqlJs.fromZipData(zipData, options?)` - Create instance from pre-loaded GTFS ZIP data (`ArrayBuffer` or `Uint8Array`)
- `GtfsSqlJs.fromDatabase(database, options?)` - Create instance from existing database

### Instance Methods
Expand Down
50 changes: 23 additions & 27 deletions src/gtfs-sqljs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,27 @@ export class GtfsSqlJs {
private constructor() {}

/**
* Create GtfsSqlJs instance from GTFS ZIP file
* Create GtfsSqlJs instance from GTFS ZIP file path or URL
*/
static async fromZip(
zipPath: string,
options: Omit<GtfsSqlJsOptions, 'zipPath' | 'database'> = {}
): Promise<GtfsSqlJs> {
const zipData = await fetchZip(zipPath, options.onProgress);
return GtfsSqlJs.fromZipData(zipData, options, zipPath);
}

/**
* Create GtfsSqlJs instance from pre-loaded GTFS ZIP data
* @param source - Optional original path/URL, used for cache key generation and metadata
*/
static async fromZipData(
zipData: ArrayBuffer | Uint8Array,
options: Omit<GtfsSqlJsOptions, 'zipPath' | 'database'> = {},
source?: string
): Promise<GtfsSqlJs> {
const instance = new GtfsSqlJs();
await instance.initFromZip(zipPath, options);
await instance.initFromZipData(zipData, options, source);
return instance;
}

Expand All @@ -171,9 +184,10 @@ export class GtfsSqlJs {
}

/**
* Initialize from ZIP file
* Initialize from pre-loaded ZIP data
* @param source - Optional original path/URL, used for cache key generation and metadata
*/
private async initFromZip(zipPath: string, options: Omit<GtfsSqlJsOptions, 'zipPath' | 'database'>): Promise<void> {
private async initFromZipData(zipData: ArrayBuffer | Uint8Array, options: Omit<GtfsSqlJsOptions, 'zipPath' | 'database'>, source?: string): Promise<void> {
const onProgress = options.onProgress;
const {
cache: userCache,
Expand Down Expand Up @@ -203,15 +217,6 @@ export class GtfsSqlJs {
message: 'Checking cache...',
});

// Fetch raw zip data for checksum (only if zipPath is a string)
let zipData: ArrayBuffer;
if (typeof zipPath === 'string') {
zipData = await fetchZip(zipPath, onProgress);
} else {
// zipPath is already ArrayBuffer or Uint8Array
zipData = zipPath as ArrayBuffer;
}

// Calculate filesize
const filesize = zipData.byteLength;

Expand All @@ -224,7 +229,7 @@ export class GtfsSqlJs {
LIB_VERSION,
cacheVersion,
filesize,
typeof zipPath === 'string' ? zipPath : undefined,
source,
skipFiles
);

Expand Down Expand Up @@ -307,7 +312,7 @@ export class GtfsSqlJs {
checksum,
version: cacheVersion,
timestamp: Date.now(),
source: typeof zipPath === 'string' ? zipPath : undefined,
source,
size: dbBuffer.byteLength,
skipFiles,
});
Expand All @@ -326,16 +331,7 @@ export class GtfsSqlJs {
return;
}

// No cache - use normal loading flow
// Fetch zip data
let zipData: ArrayBuffer;
if (typeof zipPath === 'string') {
zipData = await fetchZip(zipPath, onProgress);
} else {
zipData = zipPath as ArrayBuffer;
}

// Load from zip data
// No cache - load directly from zip data
await this.loadFromZipData(zipData, options, onProgress);

onProgress?.({
Expand All @@ -351,11 +347,11 @@ export class GtfsSqlJs {
}

/**
* Helper method to load GTFS data from zip data (ArrayBuffer)
* Helper method to load GTFS data from zip data
* Used by both cache-enabled and cache-disabled paths
*/
private async loadFromZipData(
zipData: ArrayBuffer,
zipData: ArrayBuffer | Uint8Array,
options: Omit<GtfsSqlJsOptions, 'zipPath' | 'database'>,
onProgress?: ProgressCallback
): Promise<void> {
Expand Down