Skip to content

Commit

Permalink
v6.0.20211015
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed Oct 15, 2021
1 parent 8d4536f commit 4cbed56
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
61 changes: 34 additions & 27 deletions Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
var strTag = global.Symbol && global.Symbol.toStringTag;
var blobSupported = false;
var blobSupportsArrayBufferView = false;
var arrayBufferSupported = !!global.ArrayBuffer;
var blobBuilderSupported = BlobBuilder
&& BlobBuilder.prototype.append
&& BlobBuilder.prototype.getBlob;
Expand Down Expand Up @@ -255,9 +254,6 @@
: stringDecode;

function FakeBlobBuilder () {
function isDataView (obj) {
return obj && Object.prototype.isPrototypeOf.call(DataView.prototype, obj);
}
function bufferClone (buf) {
var view = new Array(buf.byteLength);
var array = new Uint8Array(buf);
Expand Down Expand Up @@ -307,22 +303,37 @@
return new c();
};

if (arrayBufferSupported) {
var viewClasses = [
"[object Int8Array]",
"[object Uint8Array]",
"[object Uint8ClampedArray]",
"[object Int16Array]",
"[object Uint16Array]",
"[object Int32Array]",
"[object Uint32Array]",
"[object Float32Array]",
"[object Float64Array]"
];

var isArrayBufferView = ArrayBuffer.isView || function (obj) {
return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1;
};
function getObjectTypeName (o) {
return Object.prototype.toString.call(o).slice(8, -1);
}

function isPrototypeOf(c, o) {
return typeof c === "object" && Object.prototype.isPrototypeOf.call(c.prototype, o);
}

function isDataView (o) {
return getObjectTypeName(o) === "DataView" || isPrototypeOf(global.DataView, o);
}

var arrayBufferClassNames = [
"Int8Array",
"Uint8Array",
"Uint8ClampedArray",
"Int16Array",
"Uint16Array",
"Int32Array",
"Uint32Array",
"Float32Array",
"Float64Array",
"ArrayBuffer"
];

function includes(a, v) {
return a.indexOf(v) !== -1;
}

function isArrayBuffer(o) {
return includes(arrayBufferClassNames, getObjectTypeName(o)) || isPrototypeOf(global.ArrayBuffer, o);
}

function concatTypedarrays (chunks) {
Expand Down Expand Up @@ -352,14 +363,10 @@
chunks[i] = chunk._buffer;
} else if (typeof chunk === "string") {
chunks[i] = textEncode(chunk);
} else if (
arrayBufferSupported && (
Object.prototype.isPrototypeOf.call(ArrayBuffer.prototype, chunk)
|| isArrayBufferView(chunk)
|| toString.call(chunk) === "[object ArrayBuffer]")) {
chunks[i] = bufferClone(chunk);
} else if (arrayBufferSupported && isDataView(chunk)) {
} else if (isDataView(chunk)) {
chunks[i] = bufferClone(chunk.buffer);
} else if (isArrayBuffer(chunk)) {
chunks[i] = bufferClone(chunk);
} else {
chunks[i] = textEncode(String(chunk));
}
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# `blob-polyfill` CHANGELOG

## v6.0.20211015
* [Blob.js] Check object class names when determining Object types (@coclauso)
* [Blob.js] Reduce redundancies in getting class names and prototype checks (@bjornstar)
* [test] Add a test for round tripping data in ArrayBuffers (@coclauso)

## v5.0.20210201
* [Blob.js] Blob.arrayBuffer() should return a promise that resolves with an ArrayBuffer (@bjornstar)
* [test] Add a test for Blob.arrayBuffer (@bjornstar)
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blob-polyfill",
"version": "5.0.20210201",
"version": "6.0.20211015",
"homepage": "https://github.com/bjornstar/blob-polyfill",
"authors": [
"Eli Grey <me@eligrey.com>"
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": "blob-polyfill",
"version": "5.0.20210201",
"version": "6.0.20211015",
"description": "Blob.js implements the W3C Blob interface in browsers that do not natively support it.",
"main": "Blob.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ describe("blob-polyfill", function () {


function stringToArrayBuffer(string) {
const buf = new ArrayBuffer(string.length * 2); // 2 bytes for each char
const bufView = new Uint16Array(buf);
for (let i = 0, strLen = string.length; i < strLen; i++) {
var buf = new ArrayBuffer(string.length * 2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i = 0; i < string.length; i+= 1) {
bufView[i] = string.charCodeAt(i);
}
return buf;
}

function arrayBufferToString(buffer) {
const array = new Uint16Array(buffer);
var array = new Uint16Array(buffer);
return String.fromCharCode.apply(null, array);
}

0 comments on commit 4cbed56

Please sign in to comment.