@leancodepl/api-binary-blob 7.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.d.ts +1 -0
- package/index.cjs.js +60 -0
- package/index.esm.js +50 -0
- package/package.json +13 -0
- package/src/index.d.ts +3 -0
- package/src/lib/base64ToBlob.d.ts +1 -0
- package/src/lib/fromBlob.d.ts +3 -0
- package/src/lib/toBlob.d.ts +3 -0
package/index.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var apiBinary = require('@leancodepl/api-binary');
|
|
6
|
+
|
|
7
|
+
function fromBlob(blob) {
|
|
8
|
+
if (!blob) return Promise.resolve(undefined);
|
|
9
|
+
return new Promise((resolve, reject)=>{
|
|
10
|
+
try {
|
|
11
|
+
const reader = new FileReader();
|
|
12
|
+
reader.onloadend = ()=>{
|
|
13
|
+
const result = reader.result;
|
|
14
|
+
if (typeof result === "string") {
|
|
15
|
+
// we need to strip the `data:*/*;base64,` from the beginning
|
|
16
|
+
resolve(apiBinary.fromRaw(result.substring(1 + result.indexOf(",", result.indexOf(";")))));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
throw new Error("Unknown blob result received for ApiBinary creation");
|
|
20
|
+
};
|
|
21
|
+
reader.onerror = reject;
|
|
22
|
+
reader.readAsDataURL(blob);
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function base64toBlob(base64Data, contentType, sliceSize = 512) {
|
|
30
|
+
const byteCharacters = atob(base64Data);
|
|
31
|
+
const byteArrays = [];
|
|
32
|
+
for(let offset = 0; offset < byteCharacters.length; offset += sliceSize){
|
|
33
|
+
const slice = byteCharacters.slice(offset, offset + sliceSize);
|
|
34
|
+
const byteNumbers = new Array(slice.length);
|
|
35
|
+
for(let i = 0; i < slice.length; i++){
|
|
36
|
+
byteNumbers[i] = slice.charCodeAt(i);
|
|
37
|
+
}
|
|
38
|
+
byteArrays.push(new Uint8Array(byteNumbers));
|
|
39
|
+
}
|
|
40
|
+
const blob = new Blob(byteArrays, {
|
|
41
|
+
type: contentType
|
|
42
|
+
});
|
|
43
|
+
return blob;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function toBlob(apiBinary$1, contentType) {
|
|
47
|
+
if (apiBinary$1 === undefined || apiBinary$1 === null) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
return base64toBlob(apiBinary.toRaw(apiBinary$1), contentType);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
exports.fromBlob = fromBlob;
|
|
54
|
+
exports.toBlob = toBlob;
|
|
55
|
+
Object.keys(apiBinary).forEach(function (k) {
|
|
56
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
get: function () { return apiBinary[k]; }
|
|
59
|
+
});
|
|
60
|
+
});
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { fromRaw, toRaw } from '@leancodepl/api-binary';
|
|
2
|
+
export * from '@leancodepl/api-binary';
|
|
3
|
+
|
|
4
|
+
function fromBlob(blob) {
|
|
5
|
+
if (!blob) return Promise.resolve(undefined);
|
|
6
|
+
return new Promise((resolve, reject)=>{
|
|
7
|
+
try {
|
|
8
|
+
const reader = new FileReader();
|
|
9
|
+
reader.onloadend = ()=>{
|
|
10
|
+
const result = reader.result;
|
|
11
|
+
if (typeof result === "string") {
|
|
12
|
+
// we need to strip the `data:*/*;base64,` from the beginning
|
|
13
|
+
resolve(fromRaw(result.substring(1 + result.indexOf(",", result.indexOf(";")))));
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
throw new Error("Unknown blob result received for ApiBinary creation");
|
|
17
|
+
};
|
|
18
|
+
reader.onerror = reject;
|
|
19
|
+
reader.readAsDataURL(blob);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
reject(e);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function base64toBlob(base64Data, contentType, sliceSize = 512) {
|
|
27
|
+
const byteCharacters = atob(base64Data);
|
|
28
|
+
const byteArrays = [];
|
|
29
|
+
for(let offset = 0; offset < byteCharacters.length; offset += sliceSize){
|
|
30
|
+
const slice = byteCharacters.slice(offset, offset + sliceSize);
|
|
31
|
+
const byteNumbers = new Array(slice.length);
|
|
32
|
+
for(let i = 0; i < slice.length; i++){
|
|
33
|
+
byteNumbers[i] = slice.charCodeAt(i);
|
|
34
|
+
}
|
|
35
|
+
byteArrays.push(new Uint8Array(byteNumbers));
|
|
36
|
+
}
|
|
37
|
+
const blob = new Blob(byteArrays, {
|
|
38
|
+
type: contentType
|
|
39
|
+
});
|
|
40
|
+
return blob;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function toBlob(apiBinary, contentType) {
|
|
44
|
+
if (apiBinary === undefined || apiBinary === null) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
return base64toBlob(toRaw(apiBinary), contentType);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { fromBlob, toBlob };
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leancodepl/api-binary-blob",
|
|
3
|
+
"version": "7.1.0",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@leancodepl/api-binary": "7.1.0"
|
|
6
|
+
},
|
|
7
|
+
"devDependencies": {
|
|
8
|
+
"blob-polyfill": "^7.0.0"
|
|
9
|
+
},
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"main": "./index.cjs.js",
|
|
12
|
+
"module": "./index.esm.js"
|
|
13
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function base64toBlob(base64Data: string, contentType?: string, sliceSize?: number): Blob;
|