generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a843df2
commit 7c3de88
Showing
8 changed files
with
72 additions
and
77 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
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 |
---|---|---|
@@ -1,40 +1,10 @@ | ||
import type { Readable } from 'readable-stream'; | ||
import { ReadableWebToNodeStream } from 'readable-web-to-node-stream'; | ||
import { Encoder} from '@tbd54566975/dwn-sdk-js'; | ||
|
||
export function blobToIsomorphicNodeReadable(blob: Blob): Readable { | ||
return webReadableToIsomorphicNodeReadable(blob.stream()); | ||
} | ||
|
||
export function webReadableToIsomorphicNodeReadable(webReadable: ReadableStream) { | ||
return new ReadableWebToNodeStream(webReadable); | ||
} | ||
|
||
export function dataToBlob(data, dataFormat){ | ||
let dataBlob; | ||
// Check for Object or String, and if neither, assume bytes. | ||
const detectedType = toType(data); | ||
if (dataFormat === 'text/plain' || detectedType === 'string') { | ||
dataBlob = new Blob([data], { type: 'text/plain' }); | ||
} | ||
else if (dataFormat === 'application/json' || detectedType === 'object') { | ||
const dataBytes = Encoder.objectToBytes(data); | ||
dataBlob = new Blob([dataBytes], { type: 'application/json' }); | ||
} | ||
else if (data instanceof Uint8Array || data instanceof ArrayBuffer) { | ||
dataBlob = new Blob([data], { type: 'application/octet-stream' }); | ||
} | ||
else if (data instanceof Blob) { | ||
dataBlob = data; | ||
} | ||
else { | ||
throw new Error('data type not supported.'); | ||
} | ||
dataFormat = dataFormat || dataBlob.type || 'application/octet-stream'; | ||
return { dataBlob, dataFormat }; | ||
} | ||
|
||
|
||
function toType(obj){ | ||
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase(); | ||
} |
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