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: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

- 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
- Remove `as unknown as Crypto` double cast in checksum module; call `webcrypto.subtle.digest()` directly
- Remove `as BufferSource` cast in checksum module; pass `ArrayBuffer` directly to `sha256Digest()`
- Simplify checksum module to use global `crypto.subtle` directly (available in both browsers and Node.js 18+, which is the minimum engine version); remove multi-branch environment detection, dynamic `import('crypto')` fallback, and empty `catch` block
- Extract shared `isNodeEnvironment()` helper into `utils/env.ts`, replacing inline `typeof process` checks in zip-loader and gtfs-rt-loader
- Narrow `loadGTFSZip()` parameter from `string | ArrayBuffer | Uint8Array` to `ArrayBuffer | Uint8Array` (string path was dead code)
- Replace `unknown[]` with proper `ProtobufTimeRange[]` and `ProtobufEntitySelector[]` types in gtfs-rt-loader
Expand Down
31 changes: 3 additions & 28 deletions src/cache/checksum.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
/**
* Compute a SHA-256 digest using the Web Crypto API (browser or Node.js 18+).
* Returns the raw hash as an ArrayBuffer.
*/
async function sha256Digest(data: ArrayBuffer): Promise<ArrayBuffer> {
// Browser environment
if (typeof crypto !== 'undefined' && crypto.subtle) {
return crypto.subtle.digest('SHA-256', data);
}

// Node.js environment
if (typeof globalThis !== 'undefined') {
try {
const { webcrypto } = await import('crypto');
if (webcrypto?.subtle) {
return webcrypto.subtle.digest('SHA-256', data);
}
} catch {
// Fallback for older Node.js versions
}
}

throw new Error('Web Crypto API not available in this environment');
}

/**
* Compute SHA-256 checksum of data
* Uses Web Crypto API (available in both browser and Node.js 18+)
* Compute SHA-256 checksum of data.
* Uses the global Web Crypto API (available in browsers and Node.js 18+).
*/
export async function computeChecksum(data: ArrayBuffer | Uint8Array): Promise<string> {
let buffer: ArrayBuffer;
Expand All @@ -37,7 +12,7 @@ export async function computeChecksum(data: ArrayBuffer | Uint8Array): Promise<s
new Uint8Array(copy).set(data);
buffer = copy;
}
const hashBuffer = await sha256Digest(buffer);
const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
Expand Down