-
-
Notifications
You must be signed in to change notification settings - Fork 16
Replace Node.js built-ins with Web APIs for browser compatibility #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * Pure JavaScript ASCII85 (Adobe variant) decoder. | ||
| * Replaces the `base85` npm package to eliminate Node.js Buffer dependency. | ||
| * Works in browsers, Node.js 18+, Deno, Bun, and edge runtimes. | ||
| */ | ||
| export function ascii85Decode(input: string): Uint8Array | null { | ||
| let str = input; | ||
| if (str.startsWith('<~')) str = str.slice(2); | ||
| if (str.endsWith('~>')) str = str.slice(0, -2); | ||
| str = str.replace(/\s/g, ''); | ||
| if (str.length === 0) return new Uint8Array(0); | ||
|
|
||
| const output: number[] = []; | ||
| let i = 0; | ||
|
|
||
| while (i < str.length) { | ||
| if (str[i] === 'z') { | ||
| output.push(0, 0, 0, 0); | ||
| i++; | ||
| continue; | ||
| } | ||
|
|
||
| const remaining = str.length - i; | ||
| const chunkLen = Math.min(5, remaining); | ||
| if (chunkLen === 1) break; // trailing single char from truncated input; ignore it | ||
|
|
||
| let padded = str.slice(i, i + chunkLen); | ||
| while (padded.length < 5) padded += 'u'; | ||
|
|
||
| let value = 0; | ||
| for (let j = 0; j < 5; j++) { | ||
| const digit = padded.charCodeAt(j) - 33; | ||
| if (digit < 0 || digit > 84) return null; | ||
| value = value * 85 + digit; | ||
| } | ||
|
|
||
| if (chunkLen === 5 && value > 0xffffffff) return null; | ||
|
|
||
| // Use >>> 0 to simulate uint32 overflow for padded groups | ||
| const v = value >>> 0; | ||
| const numBytes = chunkLen === 5 ? 4 : chunkLen - 1; | ||
| if (numBytes >= 1) output.push((v >>> 24) & 0xff); | ||
| if (numBytes >= 2) output.push((v >>> 16) & 0xff); | ||
| if (numBytes >= 3) output.push((v >>> 8) & 0xff); | ||
| if (numBytes >= 4) output.push(v & 0xff); | ||
|
|
||
| i += chunkLen; | ||
| } | ||
|
|
||
| return new Uint8Array(output); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import * as pako from 'pako'; | ||
|
|
||
| /** | ||
| * Inflate compressed data with support for partial/truncated streams. | ||
| * Uses Z_SYNC_FLUSH to extract as much data as possible, even from | ||
| * incomplete deflate streams. Captures output via onData callback. | ||
| * | ||
| * @param data - The compressed input bytes | ||
| * @param raw - If true, use raw deflate (no zlib header); if false, expect zlib header | ||
| * @returns The decompressed bytes, or undefined if no output was produced | ||
| */ | ||
| export function inflateData( | ||
| data: Uint8Array, | ||
| raw: boolean, | ||
| ): Uint8Array | undefined { | ||
| const chunks: Uint8Array[] = []; | ||
| const inflator = new pako.Inflate({ windowBits: raw ? -15 : 15 }); | ||
| inflator.onData = (chunk: Uint8Array) => { | ||
| chunks.push(chunk); | ||
| }; | ||
| inflator.push(data, 2); // Z_SYNC_FLUSH | ||
|
|
||
| if (chunks.length === 0) return undefined; | ||
| if (chunks.length === 1) return chunks[0]; | ||
|
|
||
| const totalLen = chunks.reduce((sum, c) => sum + c.length, 0); | ||
| const result = new Uint8Array(totalLen); | ||
| let offset = 0; | ||
| for (const chunk of chunks) { | ||
| result.set(chunk, offset); | ||
| offset += chunk.length; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Decode a base64 or base64url string to Uint8Array. | ||
| * Handles missing padding and converts base64url chars (- and _) to | ||
| * standard base64 chars (+ and /), matching Buffer.from(str, 'base64') behavior. | ||
| * | ||
| * @param base64 - The base64 or base64url encoded string | ||
| * @returns The decoded bytes | ||
| */ | ||
| export function base64ToUint8Array(base64: string): Uint8Array { | ||
| const cleaned = base64 | ||
| .replace(/-/g, '+') | ||
| .replace(/_/g, '/') | ||
| .replace(/[^A-Za-z0-9+/]/g, ''); | ||
| const padded = cleaned + '='.repeat((4 - (cleaned.length % 4)) % 4); | ||
| const binary = atob(padded); | ||
| const bytes = new Uint8Array(binary.length); | ||
| for (let i = 0; i < binary.length; i++) { | ||
| bytes[i] = binary.charCodeAt(i); | ||
| } | ||
| return bytes; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can probably be it's own lib, but i guess works for now. I won't even pretend to understand it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed it could be its own lib. For now it's a single function (~40 lines) with no dependencies, so keeping it in-tree seemed simplest. Happy to extract it if it ends up being useful elsewhere.