Last active
October 23, 2017 23:09
-
-
Save ksm2/890bf989629e1afe6b704fdfae3f97aa to your computer and use it in GitHub Desktop.
Testing support of Streams API
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
<script> | |
var setResult = setResult || function setResult(tag, bool) { | |
console.log(tag + ' supported? ' + (bool ? 'yes' : 'no')); | |
}; | |
// Basic support | |
try { | |
var result0 = true; | |
// Test exposed globals | |
result0 = result0 && ('ReadableStream' in window); | |
result0 = result0 && ('ByteLengthQueuingStrategy' in window); | |
result0 = result0 && ('CountQueuingStrategy' in window); | |
// Test accessibility from fetch API | |
if ('Response' in window) { | |
result0 = result0 && (Object.getOwnPropertyNames(window.Response.prototype).indexOf('body') > -1); | |
} | |
// Test ReadableStreamDefaultReader | |
result0 = result0 && Object.getPrototypeOf((new window.ReadableStream).getReader({ mode: undefined })).constructor.name === 'ReadableStreamDefaultReader'; | |
// Test ReadableStreamDefaultController | |
new window.ReadableStream({ | |
start(controller) { | |
result0 = result0 && Object.getPrototypeOf(controller).constructor.name === 'ReadableStreamDefaultController'; | |
} | |
}); | |
setResult('streams0', result0); | |
} catch (e) { | |
setResult('streams0', false); | |
} | |
// Test WritableStream support | |
try { | |
var result1 = true; | |
// Test exposed globals | |
result1 = result1 && ('WritableStream' in window); | |
setResult('streams1', result1); | |
} catch (e) { | |
setResult('streams1', false); | |
} | |
// Test pipeTo / pipeThrough support (note #2) | |
try { | |
var result2 = true; | |
result2 = result2 && typeof window.ReadableStream.prototype.pipeThrough === 'function'; | |
result2 = result2 && typeof window.ReadableStream.prototype.pipeTo === 'function'; | |
const rs1 = new ReadableStream(); | |
rs1.pipeTo(new WritableStream()); | |
const rs2 = new ReadableStream(); | |
rs2.pipeThrough({ writable: new WritableStream(), readable: new ReadableStream() }); | |
setResult('streams2', result2); | |
} catch (e) { | |
setResult('streams2', false); | |
} | |
// Test BYOB support (note #3) | |
try { | |
var result3 = true; | |
// Test ReadableByteStreamController | |
var readableByteStreamCtrl = new window.ReadableStream({ | |
type: 'bytes', | |
start(controller) { | |
result3 = result3 && Object.getPrototypeOf(controller).constructor.name === 'ReadableByteStreamController'; | |
} | |
}); | |
// Test ReadableStreamBYOBReader | |
result3 = result3 && Object.getPrototypeOf(readableByteStreamCtrl.getReader({ mode: 'byob' })).constructor.name === 'ReadableStreamBYOBReader'; | |
setResult('streams3', result3); | |
} catch (e) { | |
setResult('streams3', false); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment