-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👩💻 dx: Define Blob from/to DataURL conversion utilities.
- Loading branch information
1 parent
d030af1
commit c462c58
Showing
3 changed files
with
71 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const blobFromDataURL = async (url: string): Promise<Blob> => { | ||
const response = await fetch(url); | ||
return response.blob(); | ||
}; | ||
|
||
export default blobFromDataURL; |
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,39 @@ | ||
const _blobToDataURLClient = async (blob: Blob): Promise<string> => | ||
new Promise<string>((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', (_e) => { | ||
resolve(reader.result as string); | ||
}); | ||
|
||
reader.addEventListener('error', (_e) => { | ||
reject(reader.error); | ||
}); | ||
|
||
reader.addEventListener('abort', (_e) => { | ||
reject(new Error('Read aborted')); | ||
}); | ||
reader.readAsDataURL(blob); | ||
}); | ||
|
||
const _blobToDataURLServer = async (blob: Blob): Promise<string> => { | ||
const mimeType = blob.type; | ||
if (mimeType === '') { | ||
throw new Error('unknown mime-type'); | ||
} | ||
|
||
const arrayBuffer = await blob.arrayBuffer(); | ||
|
||
const {Buffer} = await import('buffer'); | ||
const buffer = Buffer.from(arrayBuffer); | ||
|
||
const base64 = buffer.toString('base64'); | ||
|
||
const {default: dataURL} = await import('../dataURL'); | ||
return dataURL(mimeType, base64); | ||
}; | ||
|
||
const blobToDataURL = Meteor.isServer | ||
? _blobToDataURLServer | ||
: _blobToDataURLClient; | ||
|
||
export default blobToDataURL; |
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,26 @@ | ||
// eslint-disable-next-line import/no-unassigned-import | ||
import 'regenerator-runtime/runtime.js'; | ||
|
||
import {assert} from 'chai'; | ||
|
||
import {isomorphic} from '../../_test/fixtures'; | ||
import {randomPNGDataURI} from '../../_test/png'; | ||
|
||
import blobFromDataURL from './blobFromDataURL'; | ||
import blobToDataURL from './blobToDataURL'; | ||
|
||
isomorphic(__filename, () => { | ||
it('should allow to convert back and forth from a dataURL', async () => { | ||
if (Meteor.isServer) { | ||
await import('../../../server/polyfill/fetch'); | ||
} | ||
|
||
const url = randomPNGDataURI(); | ||
|
||
const blob = await blobFromDataURL(url); | ||
assert.instanceOf(blob, Blob); | ||
|
||
const result = await blobToDataURL(blob); | ||
assert.equal(result, url); | ||
}); | ||
}); |