1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Add consume support for browsers.

This commit is contained in:
Pedro Gutiérrez 2019-04-08 16:09:35 +02:00 committed by Pedro Gutiérrez
parent 9a252d7731
commit dde934543c
3 changed files with 21 additions and 13 deletions

View File

@ -131,20 +131,24 @@ export class Brizo extends Instantiable {
} }
private async downloadFile(url: string, destination?: string): Promise<string> { private async downloadFile(url: string, destination?: string): Promise<string> {
const response = await WebServiceConnectorProvider if (destination) {
.getConnector() const response = await WebServiceConnectorProvider
.get(url) .getConnector()
.get(url)
const filename = response.headers.get('content-disposition').match(/attachment;filename=(.+)/)[1] const filename = response.headers.get('content-disposition').match(/attachment;filename=(.+)/)[1]
await new Promise(async (resolve, reject) => { await new Promise(async (resolve, reject) => {
fs.mkdirSync(destination, {recursive: true}) fs.mkdirSync(destination, {recursive: true})
const fileStream = fs.createWriteStream(`${destination}${filename}`); const fileStream = fs.createWriteStream(`${destination}${filename}`);
response.body.pipe(fileStream); response.body.pipe(fileStream);
response.body.on("error", reject); response.body.on("error", reject);
fileStream.on("finish", resolve); fileStream.on("finish", resolve);
}); });
return destination return destination
} else {
window.open(url, '_blank')
}
} }
} }

View File

@ -5,7 +5,8 @@
"lib": [ "lib": [
"es2017", "es2017",
"es6", "es6",
"es7" "es7",
"DOM"
], ],
"declaration": true, "declaration": true,
"module": "commonjs", "module": "commonjs",

View File

@ -9,6 +9,9 @@ module.exports = {
minimize: true, minimize: true,
noEmitOnErrors: true noEmitOnErrors: true
}, },
node: {
fs: 'empty',
},
resolve: { resolve: {
extensions: ['.js'], extensions: ['.js'],
modules: ['node_modules'], modules: ['node_modules'],