Skip to content

Commit

Permalink
Merge branch 'master' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed Jun 12, 2019
2 parents c02cddc + c92eaf7 commit 82ac326
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 40 deletions.
42 changes: 13 additions & 29 deletions StreamSaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
let supportsTransferable = false
const test = fn => { try { fn() } catch (e) {} }
const ponyfill = window.WebStreamsPolyfill || {}
const once = { once: true }
const isSecureContext = window.isSecureContext
let useBlobFallback = /constructor/i.test(window.HTMLElement) || !!window.safari
const downloadStrategy = isSecureContext || 'MozAppearance' in document.documentElement.style
Expand Down Expand Up @@ -45,7 +44,7 @@
iframe.postMessage = (...args) => iframe.contentWindow.postMessage(...args)
iframe.addEventListener('load', () => {
iframe.loaded = true
}, once)
}, { once: true })
document.body.appendChild(iframe)
return iframe
}
Expand Down Expand Up @@ -85,22 +84,12 @@
return popup
}

/**
* Destroys a channel and return null
*
* @param {MessageChannel} channel [description]
* @return {null} [description]
*/
function destroyChannel (channel) {
channel.port1.onmessage = null
channel.port1.close()
channel.port2.close()
return null
}

try {
// We can't look for service worker since it may still work on http
!!new Response(new ReadableStream())
new Response(new ReadableStream())
if (isSecureContext && !('serviceWorker' in navigator)) {
useBlobFallback = true
}
} catch (err) {
useBlobFallback = true
}
Expand Down Expand Up @@ -232,7 +221,7 @@
}
}

// We never remove this iframes b/c it can interrupt saveAs
// We never remove this iframes b/c it can interrupt saving
makeIframe(evt.data.download)
}
}
Expand All @@ -243,20 +232,13 @@
} else {
mitmTransporter.addEventListener('load', () => {
mitmTransporter.postMessage(...args)
}, once)
}, { once: true })
}
}

let chunks = []

return (!useBlobFallback && ts && ts.writable) || new streamSaver.WritableStream({
start () {
// is called immediately, and should perform any actions
// necessary to acquire access to the underlying sink.
// If this process is asynchronous, it can return a promise
// to signal success or failure.
// return setupChannel()
},
write (chunk) {
if (useBlobFallback) {
// Safari... The new IE6
Expand Down Expand Up @@ -293,15 +275,17 @@
link.href = URL.createObjectURL(blob)
link.download = filename
link.click()
return
} else {
channel.port1.postMessage('end')
}
channel.port1.postMessage('end')
channel = destroyChannel(channel)
},
abort () {
chunks = []
channel.port1.postMessage('abort')
channel = destroyChannel(channel)
channel.port1.onmessage = null
channel.port1.close()
channel.port2.close()
channel = null
}
}, opts.writableStrategy)
}
Expand Down
3 changes: 2 additions & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ <h2>Example of saving a stream directly to the filesystem</h2>
<li><a href="examples/fetch.html">Piping a fetch response to StreamSaver</a>
<li><a href="examples/plain-text.html">Write as you type</a>
<li><a href="examples/torrent.html">Saving a file using webtorrent</a>
<li><a href="examples/saving-a-blob.html">Saving one Blob (File)</a>
<li><a href="examples/saving-multiple-files.html">Saving multiple files as a zip</a>
<li><a href="examples/write-slowly.html">slowly write 1 byte / sec</a>
<li><a href="examples/write-slowly.html">Slowly write 1 byte / sec</a>
</ul>
</body>
</html>
18 changes: 10 additions & 8 deletions examples/saving-a-blob.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
<!-- includes blob.stream() polyfill -->
<script src="https://cdn.jsdelivr.net/gh/eligrey/Blob.js/Blob.js"></script>
<script src="../StreamSaver.js"></script>
<script src="zip-stream.js"></script>
<script>
// Saving a blob is as simple as the fetch example, you just get the
// readableStream from the blob by calling blob.stream() to get a
// readableStream and then pipe it

// One alternetive way of getting the stream is by doing:
// const readableStream = new Response(blob).body

$start.onclick = () => {
const fileStream = streamSaver.createWriteStream('sample.txt')
const blob = new Blob(['StreamSaver is awesome'])
const fileStream = streamSaver.createWriteStream('sample.txt', {
size: blob.size // Makes the procentage visiable in the download
})

// One quick alternetive way if you don't want the hole blob.js thing:
// const readableStream = new Response(
// Blob || String || ArrayBuffer || ArrayBufferView
// ).body
const readableStream = blob.stream()

// more optimized pipe version
Expand All @@ -32,10 +34,10 @@
.then(() => console.log('done writing'))
}

// Write manually
// Write (pipe) manually
window.writer = fileStream.getWriter()

const reader = readableZipStream.getReader()
const reader = readableStream.getReader()
const pump = () => reader.read()
.then(res => res.done
? writer.close()
Expand Down
1 change: 0 additions & 1 deletion examples/saving-multiple-files.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
while Also making File constructor work in some browser that don't support it
-->
<script src="https://cdn.jsdelivr.net/gh/eligrey/Blob.js/Blob.js"></script>

<script src="../StreamSaver.js"></script>
<script src="zip-stream.js"></script>
<script>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "streamsaver",
"version": "2.0.2",
"version": "2.0.3",
"description": "StreamSaver writes stream to the filesystem directly - asynchronous",
"main": "StreamSaver.js",
"scripts": {
Expand Down

0 comments on commit 82ac326

Please sign in to comment.