Skip to content
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

fixes for safari and firefox #60

Merged
merged 4 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.75.0",
"axios": "^0.26.1",
"browser-fs-access": "^0.30.2",
"buffer-crc32": "^0.2.13",
"events": "^3.3.0",
"jose": "^4.8.1",
Expand Down
31 changes: 16 additions & 15 deletions lib/tdf3/src/client/BrowserTdfSteam.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import streamSaver from 'streamsaver';
import { WritableStream } from 'web-streams-polyfill/ponyfill';
import ReadableStream from './DecoratedReadableStream';
import streamSaver from 'streamsaver';
import { fileSave } from 'browser-fs-access';

class BrowserTdfStream extends ReadableStream {
static convertToWebStream() {
throw new Error('Please use Web Streams in browser environment')
throw new Error('Please use Web Streams in browser environment');
}

/**
* Dump the stream content to a string. This will consume the stream.
* @return {string} - the plaintext in string form.
*/
async toString() {
const results = await this.toBuffer()
const results = await this.toBuffer();
return results.toString('utf8');
}

Expand Down Expand Up @@ -47,23 +47,24 @@ class BrowserTdfStream extends ReadableStream {
* Dump the stream content to a local file. This will consume the stream.
*
* @param {string} filepath - the path of the local file to write plaintext to.
* @param {string} encoding - the charset encoding to use. Defaults to utf-8.
*/
async toFile(filepath, encoding) {
try {
new streamSaver.WritableStream();
} catch (e) {
// Conditionally add ponyfill for Firefox
streamSaver.WritableStream = WritableStream;
}

async toFile(filepath) {
const fileName = filepath || 'download.tdf';

const isFirefox = typeof InstallTrigger !== 'undefined';

if (isFirefox) {
return await fileSave(new Response(this), {
fileName: fileName,
extensions: [`.${filepath.split('.').pop()}`],
});
}

const fileStream = streamSaver.createWriteStream(fileName, {
...(this.contentLength && { size: this.contentLength }),
});

if (WritableStream && this.pipeTo) {
if (WritableStream) {
return this.pipeTo(fileStream);
}

Expand All @@ -78,4 +79,4 @@ class BrowserTdfStream extends ReadableStream {
}
}

export default BrowserTdfStream
export default BrowserTdfStream;
8 changes: 8 additions & 0 deletions lib/tdf3/src/client/DecoratedReadableStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ class DecoratedReadableStream extends ReadableStream {
const ee = new EventEmitter();
this.on = ee.on;
this.emit = ee.emit;

const isSafari =
/constructor/i.test(window.HTMLElement) ||
window?.safari?.pushNotification?.toString() === '[object SafariRemoteNotification]';

if (isSafari) {
Object.setPrototypeOf(this, DecoratedReadableStream.prototype);
}
}

/**
Expand Down
26 changes: 11 additions & 15 deletions lib/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const prodPattern = /^prod/i;

module.exports = (env, argv) => {
const isProd = prodPattern.test(argv.mode) || prodPattern.test(process.env.NODE_ENV);
const mode = (isProd)
? 'production'
: 'development';
const mode = isProd ? 'production' : 'development';
// CRA fails to run build with mode: 'production'

const clientConfig = {
Expand All @@ -27,14 +25,14 @@ module.exports = (env, argv) => {
},
target: 'web',
resolve: {
extensions: [ '.ts', '.tsx', '.js'],
extensions: ['.ts', '.tsx', '.js'],
modules: ['src', 'node_modules'],
fallback: {
crypto: false,
fs: false,
constants: false,
stream: false,
}
},
},
output: {
publicPath: '',
Expand Down Expand Up @@ -63,7 +61,7 @@ module.exports = (env, argv) => {
},
target: 'node',
resolve: {
extensions: [ '.ts', '.tsx', '.js'],
extensions: ['.ts', '.tsx', '.js'],
modules: ['src', 'node_modules'],
},
output: {
Expand All @@ -89,7 +87,10 @@ module.exports = (env, argv) => {
...clientConfig.resolve,
alias: {
'@tdfStream': path.resolve(__dirname, '/tdf3/src/client/BrowserTdfSteam.js'),
"@runtimeConfig": path.resolve(__dirname, '/tdf3/src/utils/aws-lib-storage/runtimeConfig.browser'),
'@runtimeConfig': path.resolve(
__dirname,
'/tdf3/src/utils/aws-lib-storage/runtimeConfig.browser'
),
},
},
output: {
Expand All @@ -105,11 +106,11 @@ module.exports = (env, argv) => {
resolve: {
...serverConfig.resolve,
fallback: {
fetch: require.resolve('node-fetch')
fetch: require.resolve('node-fetch'),
},
alias: {
'@tdfStream': path.resolve(__dirname, '/tdf3/src/client/NodeTdfStream.js'),
"@runtimeConfig": path.resolve(__dirname, '/tdf3/src/utils/aws-lib-storage/runtimeConfig'),
'@runtimeConfig': path.resolve(__dirname, '/tdf3/src/utils/aws-lib-storage/runtimeConfig'),
},
},
output: {
Expand All @@ -118,10 +119,5 @@ module.exports = (env, argv) => {
},
};

return [
clientConfig,
serverConfig,
clientTdf3Config,
serverTdf3Config,
];
return [clientConfig, serverConfig, clientTdf3Config, serverTdf3Config];
};