-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add URL-safe Base64 Encoding/Decoding for Dynamic Sample Redirection #141
Open
janvandenschilden
wants to merge
8
commits into
hms-dbmi:main
Choose a base branch
from
CenterForMedicalGeneticsGhent:feature/base64-url-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7e6571
Add URL-safe codec and improve external URL handling
janvandenschilden f98be8e
Reduce code duplication
janvandenschilden 35c79d1
Refactor encode method to support optional headerCheck parameter
janvandenschilden b2be486
feat: Add JsonBase64Converter component
janvandenschilden ca185f2
fix: update PTEN position based on hg38 (#140)
sehilyi 8b8b944
feat: make pe_support optional in sv bedpe data (#137)
sehilyi f38a1ba
Update driver.custom.json
dominikglodzikhms 7675e5d
Remove JsonBase64Converter component from App.tsx
janvandenschilden 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 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 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 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 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 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,72 @@ | ||
import pako from 'pako'; | ||
import base64Js from 'base64-js'; | ||
|
||
/** | ||
* UrlsafeCodec provides static methods to encode and decode samples | ||
* to and from a URL-safe base64 encoded string. It uses JSON for serialization, | ||
* pako for compression, and base64-js for handling base64 encoding. | ||
*/ | ||
class UrlsafeCodec { | ||
/** | ||
* Encodes a sample object into a URL-safe base64 string. | ||
* | ||
* The method serializes the sample to a JSON string, compresses it using pako, | ||
* By default, the header check is turned off. If headerCheck is set to true, the method | ||
* will perform zlib/gzip header and checksum verification during decompression using pako. | ||
* converts the compressed data to a base64 string, and then modifies the base64 string | ||
* to make it URL-safe by replacing '+' with '.', '/' with '_', and '=' with '-'. | ||
* | ||
* @param {Object} sample - The sample object to encode. | ||
* @param {boolean} headerCheck - Optional parameter to enable header and checksum verification. | ||
* @returns {string} A URL-safe base64 encoded string representing the sample. | ||
*/ | ||
static encode(sample, headerCheck = false) { | ||
try { | ||
const string = JSON.stringify(sample); | ||
const encoder = new TextEncoder(); | ||
const stringAsUint8Array = encoder.encode(string); | ||
// Set 'raw' to true if headerCheck is false | ||
const compressedUint8Array = pako.deflate(stringAsUint8Array, {raw: !headerCheck }); | ||
const base64Bytes = base64Js.fromByteArray(compressedUint8Array); | ||
const base64Blob = base64Bytes.toString(); | ||
const base64UrlsafeBlob = base64Blob.replace(/\+/g, '.').replace(/\//g, '_').replace(/=/g, '-'); | ||
return base64UrlsafeBlob; | ||
} catch (error) { | ||
console.error('Error encoding sample:', error); | ||
// Handle the error or rethrow, depending on your needs | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Decodes a URL-safe base64 string back into a sample object. | ||
* | ||
* The method reverses the URL-safe transformation by replacing '.', '_', and '-' | ||
* with '+', '/', and '=' respectively. It then converts the base64 string back to bytes. | ||
* By default, the header check is turned off. If headerCheck is set to true, the method | ||
* will perform zlib/gzip header and checksum verification during decompression using pako. | ||
* Finally, it parses the JSON string to reconstruct the original sample object. | ||
* | ||
* @param {string} encodedString - The URL-safe base64 encoded string to decode. | ||
* @param {boolean} headerCheck - Optional parameter to enable header and checksum verification. | ||
* @returns {Object} The original sample object. | ||
*/ | ||
static decode(encodedString, headerCheck = false) { | ||
try { | ||
const base64Blob = encodedString.replace(/\./g, '+').replace(/_/g, '/').replace(/-/g, '='); | ||
const compressedUint8Array = base64Js.toByteArray(base64Blob); | ||
// Set 'raw' to true if headerCheck is false | ||
const bytes = pako.inflate(compressedUint8Array, {raw: !headerCheck }); | ||
const decoder = new TextDecoder(); | ||
const string = decoder.decode(bytes); | ||
const sample = JSON.parse(string); | ||
return sample; | ||
} catch (error) { | ||
console.error('Error decoding string:', error); | ||
// Handle the error or rethrow, depending on your needs | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
export default UrlsafeCodec; |
This file contains 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 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 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,60 @@ | ||
import React, { useState } from 'react'; | ||
import UrlsafeCodec from '../lib/urlsafe-codec'; | ||
|
||
const JsonBase64Converter = () => { | ||
const [jsonText, setJsonText] = useState(''); | ||
const [base64Text, setBase64Text] = useState(''); | ||
const [error, setError] = useState(''); | ||
|
||
const handleEncode = () => { | ||
try { | ||
const jsonObject = JSON.parse(jsonText); | ||
const encoded = UrlsafeCodec.encode(jsonObject); | ||
setBase64Text(encoded); | ||
setError(''); | ||
} catch (error) { | ||
setError('Error parsing JSON. Please enter valid JSON.'); | ||
} | ||
}; | ||
|
||
const handleDecode = () => { | ||
try { | ||
const decoded = UrlsafeCodec.decode(base64Text); | ||
const jsonString = JSON.stringify(decoded, null, 4); | ||
setJsonText(jsonString); | ||
setError(''); | ||
} catch (error) { | ||
setError('Error decoding base64 string. Please enter a valid base64-encoded string.'); | ||
} | ||
}; | ||
|
||
return ( | ||
<div style={{ display: 'flex', flexDirection: 'row' }}> | ||
<div style={{ flex: 1, marginRight: '8px', marginLeft: '8px' }}> | ||
<h2>JSON Text</h2> | ||
<textarea | ||
value={jsonText} | ||
onChange={(e) => setJsonText(e.target.value)} | ||
placeholder="Enter JSON text" | ||
rows={48} | ||
style={{ width: '99%', fontSize: 'large' }} | ||
/> | ||
<button onClick={handleEncode}>Encode</button> | ||
{error && <p style={{ color: 'red' }}>{error}</p>} | ||
</div> | ||
<div style={{ flex: 1, marginRight: '8px', marginLeft: '8px' }}> | ||
<h2>Base64 Encoded Text</h2> | ||
<textarea | ||
value={base64Text} | ||
onChange={(e) => setBase64Text(e.target.value)} | ||
placeholder="Base64 url safe encoded string" | ||
rows={48} | ||
style={{ width: '99%', fontSize: 'large' }} | ||
/> | ||
<button onClick={handleDecode}>Decode</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default JsonBase64Converter; |
This file contains 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.
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.
I realized this change makes the demo of the chromoscope inaccessible via
chromoscope.bio/app
. I tested this by running the following commands.How the document and demo are organized under
\build
:By setting
base: '/app/'
, theindex.html
file for the demo (i.e.,app/index.html
) created byvite
has proper relative paths (e.g.,app/index.js
instead ofindex.js
). So, without setting thebase
option,chromoscope.bio/app
(app/index.html
) ends up having the wrong relative paths, making the demo not work.I did not find a simple way to walk around this and would need to experiment more for a better alternative, but we can simply create an inlined HTML file for the webpage that you created (instead of using React) and put that under a
/dev/codec/
folder.In this way, users should be able to access the webpage via "chromoscope.bio/dev/codec/'.