@wordpress/blob 4.54.1-next.v.202609031004.0 → 4.55.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/CHANGELOG.md +2 -0
- package/package.json +5 -6
- package/src/index.ts +0 -113
- package/src/test/index.jsdom.test.ts +0 -92
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/blob",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.55.0",
|
|
4
4
|
"description": "Blob utilities for WordPress.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"npm": ">=8.19.2"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
|
-
"src",
|
|
27
26
|
"build",
|
|
28
27
|
"build-module",
|
|
29
28
|
"build-types",
|
|
@@ -43,13 +42,13 @@
|
|
|
43
42
|
"types": "build-types",
|
|
44
43
|
"sideEffects": false,
|
|
45
44
|
"devDependencies": {
|
|
46
|
-
"@testing-library/jest-dom": "^
|
|
45
|
+
"@testing-library/jest-dom": "^7.0.1",
|
|
47
46
|
"@types/jest": "^30.0.0",
|
|
48
|
-
"@types/node": "^
|
|
49
|
-
"vitest": "^
|
|
47
|
+
"@types/node": "^24.13.3",
|
|
48
|
+
"vitest": "^5.0.0"
|
|
50
49
|
},
|
|
51
50
|
"publishConfig": {
|
|
52
51
|
"access": "public"
|
|
53
52
|
},
|
|
54
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "485f42ae8a1c58ceea18371a507fd4acfa86fbd8"
|
|
55
54
|
}
|
package/src/index.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
const cache: Record< string, File > = {};
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Create a blob URL from a file.
|
|
5
|
-
*
|
|
6
|
-
* @param file The file to create a blob URL for.
|
|
7
|
-
*
|
|
8
|
-
* @return The blob URL.
|
|
9
|
-
*/
|
|
10
|
-
export function createBlobURL( file: File ): string {
|
|
11
|
-
const url = window.URL.createObjectURL( file );
|
|
12
|
-
|
|
13
|
-
cache[ url ] = file;
|
|
14
|
-
|
|
15
|
-
return url;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Retrieve a file based on a blob URL. The file must have been created by
|
|
20
|
-
* `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
|
|
21
|
-
* `undefined`.
|
|
22
|
-
*
|
|
23
|
-
* @param url The blob URL.
|
|
24
|
-
*
|
|
25
|
-
* @return The file for the blob URL.
|
|
26
|
-
*/
|
|
27
|
-
export function getBlobByURL( url: string ): File | undefined {
|
|
28
|
-
return cache[ url ];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Retrieve a blob type based on URL. The file must have been created by
|
|
33
|
-
* `createBlobURL` and not removed by `revokeBlobURL`, otherwise it will return
|
|
34
|
-
* `undefined`.
|
|
35
|
-
*
|
|
36
|
-
* @param url The blob URL.
|
|
37
|
-
*
|
|
38
|
-
* @return The blob type.
|
|
39
|
-
*/
|
|
40
|
-
export function getBlobTypeByURL( url: string ): string | undefined {
|
|
41
|
-
return getBlobByURL( url )?.type.split( '/' )[ 0 ]; // 0: media type , 1: file extension eg ( type: 'image/jpeg' ).
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Remove the resource and file cache from memory.
|
|
46
|
-
*
|
|
47
|
-
* @param url The blob URL.
|
|
48
|
-
*/
|
|
49
|
-
export function revokeBlobURL( url: string ): void {
|
|
50
|
-
if ( cache[ url ] ) {
|
|
51
|
-
window.URL.revokeObjectURL( url );
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
delete cache[ url ];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Check whether a url is a blob url.
|
|
59
|
-
*
|
|
60
|
-
* @param url The URL.
|
|
61
|
-
*
|
|
62
|
-
* @return Is the url a blob url?
|
|
63
|
-
*/
|
|
64
|
-
export function isBlobURL( url: string | undefined ): boolean {
|
|
65
|
-
if ( ! url || ! url.indexOf ) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
return url.indexOf( 'blob:' ) === 0;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Downloads a file, e.g., a text or readable stream, in the browser.
|
|
73
|
-
* Appropriate for downloading smaller file sizes, e.g., < 5 MB.
|
|
74
|
-
*
|
|
75
|
-
* Example usage:
|
|
76
|
-
*
|
|
77
|
-
* ```js
|
|
78
|
-
* const fileContent = JSON.stringify(
|
|
79
|
-
* {
|
|
80
|
-
* "title": "My Post",
|
|
81
|
-
* },
|
|
82
|
-
* null,
|
|
83
|
-
* 2
|
|
84
|
-
* );
|
|
85
|
-
* const filename = 'file.json';
|
|
86
|
-
*
|
|
87
|
-
* downloadBlob( filename, fileContent, 'application/json' );
|
|
88
|
-
* ```
|
|
89
|
-
*
|
|
90
|
-
* @param filename File name.
|
|
91
|
-
* @param content File content (BufferSource | Blob | string).
|
|
92
|
-
* @param contentType (Optional) File mime type. Default is `''`.
|
|
93
|
-
*/
|
|
94
|
-
export function downloadBlob(
|
|
95
|
-
filename: string,
|
|
96
|
-
content: BlobPart,
|
|
97
|
-
contentType: string = ''
|
|
98
|
-
): void {
|
|
99
|
-
if ( ! filename || ! content ) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const file = new window.Blob( [ content ], { type: contentType } );
|
|
104
|
-
const url = window.URL.createObjectURL( file );
|
|
105
|
-
const anchorElement = document.createElement( 'a' );
|
|
106
|
-
anchorElement.href = url;
|
|
107
|
-
anchorElement.download = filename;
|
|
108
|
-
anchorElement.style.display = 'none';
|
|
109
|
-
document.body.appendChild( anchorElement );
|
|
110
|
-
anchorElement.click();
|
|
111
|
-
document.body.removeChild( anchorElement );
|
|
112
|
-
window.URL.revokeObjectURL( url );
|
|
113
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { isBlobURL, getBlobTypeByURL, downloadBlob } from '..';
|
|
2
|
-
|
|
3
|
-
describe( 'isBlobURL', () => {
|
|
4
|
-
it( 'returns true if the url starts with "blob:"', () => {
|
|
5
|
-
expect( isBlobURL( 'blob:thisbitdoesnotmatter' ) ).toBe( true );
|
|
6
|
-
} );
|
|
7
|
-
|
|
8
|
-
it( 'returns false if the url does not start with "blob:"', () => {
|
|
9
|
-
expect( isBlobURL( 'https://www.example.com' ) ).toBe( false );
|
|
10
|
-
} );
|
|
11
|
-
|
|
12
|
-
it( 'returns false if the url is not defined', () => {
|
|
13
|
-
expect(
|
|
14
|
-
// @ts-expect-error This is not a valid call according to types.
|
|
15
|
-
isBlobURL()
|
|
16
|
-
).toBe( false );
|
|
17
|
-
} );
|
|
18
|
-
} );
|
|
19
|
-
|
|
20
|
-
describe( 'getBlobTypeByURL', () => {
|
|
21
|
-
it( 'returns undefined if the blob is not found', () => {
|
|
22
|
-
expect( getBlobTypeByURL( 'blob:notexisting' ) ).toBeUndefined();
|
|
23
|
-
} );
|
|
24
|
-
|
|
25
|
-
it( 'returns undefined if the url is not defined', () => {
|
|
26
|
-
expect(
|
|
27
|
-
// @ts-expect-error This is not a valid call according to types.
|
|
28
|
-
getBlobTypeByURL()
|
|
29
|
-
).toBeUndefined();
|
|
30
|
-
} );
|
|
31
|
-
} );
|
|
32
|
-
|
|
33
|
-
describe( 'downloadBlob', () => {
|
|
34
|
-
const originalURL = window.URL;
|
|
35
|
-
const createObjectURL = jest.fn().mockReturnValue( 'blob:pannacotta' );
|
|
36
|
-
const revokeObjectURL = jest.fn().mockReturnValue( false );
|
|
37
|
-
const mockAnchorElement = document.createElement( 'a' );
|
|
38
|
-
mockAnchorElement.click = jest.fn();
|
|
39
|
-
const createElementSpy = jest
|
|
40
|
-
.spyOn( global.document, 'createElement' )
|
|
41
|
-
.mockReturnValue( mockAnchorElement );
|
|
42
|
-
|
|
43
|
-
const mockBlob = jest.fn();
|
|
44
|
-
const blobSpy = jest
|
|
45
|
-
.spyOn( window, 'Blob' )
|
|
46
|
-
.mockReturnValue( mockBlob as unknown as Blob );
|
|
47
|
-
jest.spyOn( document.body, 'appendChild' );
|
|
48
|
-
jest.spyOn( document.body, 'removeChild' );
|
|
49
|
-
beforeEach( () => {
|
|
50
|
-
// Can't seem to spy on these static methods. They are `undefined`.
|
|
51
|
-
// Possibly overwritten: https://github.com/WordPress/gutenberg/blob/trunk/packages/jest-preset-default/scripts/setup-globals.js#L5
|
|
52
|
-
// @ts-expect-error This is not a valid URL object.
|
|
53
|
-
window.URL = {
|
|
54
|
-
createObjectURL,
|
|
55
|
-
revokeObjectURL,
|
|
56
|
-
};
|
|
57
|
-
} );
|
|
58
|
-
|
|
59
|
-
afterAll( () => {
|
|
60
|
-
window.URL = originalURL;
|
|
61
|
-
} );
|
|
62
|
-
|
|
63
|
-
it( 'requires a filename argument', () => {
|
|
64
|
-
downloadBlob( '', '{}', 'application/json' );
|
|
65
|
-
expect( blobSpy ).not.toHaveBeenCalled();
|
|
66
|
-
} );
|
|
67
|
-
|
|
68
|
-
it( 'requires a content argument', () => {
|
|
69
|
-
downloadBlob( 'text.txt', '', 'text/plain' );
|
|
70
|
-
expect( blobSpy ).not.toHaveBeenCalled();
|
|
71
|
-
} );
|
|
72
|
-
|
|
73
|
-
it( 'constructs an anchor element with attributes and removes it', () => {
|
|
74
|
-
downloadBlob( 'filename.json', '{}', 'application/json' );
|
|
75
|
-
expect( blobSpy ).toHaveBeenCalledWith( [ '{}' ], {
|
|
76
|
-
type: 'application/json',
|
|
77
|
-
} );
|
|
78
|
-
expect( createObjectURL ).toHaveBeenCalledWith( mockBlob );
|
|
79
|
-
expect( createElementSpy ).toHaveBeenCalledWith( 'a' );
|
|
80
|
-
expect( mockAnchorElement.download ).toBe( 'filename.json' );
|
|
81
|
-
expect( mockAnchorElement.href ).toBe( 'blob:pannacotta' );
|
|
82
|
-
expect( mockAnchorElement ).toHaveStyle( 'display:none' );
|
|
83
|
-
expect( document.body.appendChild ).toHaveBeenCalledWith(
|
|
84
|
-
mockAnchorElement
|
|
85
|
-
);
|
|
86
|
-
expect( mockAnchorElement.click ).toHaveBeenCalledTimes( 1 );
|
|
87
|
-
expect( document.body.removeChild ).toHaveBeenCalledWith(
|
|
88
|
-
mockAnchorElement
|
|
89
|
-
);
|
|
90
|
-
expect( revokeObjectURL ).toHaveBeenCalled();
|
|
91
|
-
} );
|
|
92
|
-
} );
|