@finos/legend-lego 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/index.css +1 -1
- package/package.json +4 -6
- package/tsconfig.json +0 -2
- package/lib/download-helper/__test-utils__/MockedDownloadHelper.d.ts +0 -18
- package/lib/download-helper/__test-utils__/MockedDownloadHelper.d.ts.map +0 -1
- package/lib/download-helper/__test-utils__/MockedDownloadHelper.js +0 -18
- package/lib/download-helper/__test-utils__/MockedDownloadHelper.js.map +0 -1
- package/lib/download-helper/index.d.ts +0 -18
- package/lib/download-helper/index.d.ts.map +0 -1
- package/lib/download-helper/index.js +0 -122
- package/lib/download-helper/index.js.map +0 -1
- package/src/download-helper/__test-utils__/MockedDownloadHelper.ts +0 -18
- package/src/download-helper/index.ts +0 -158
package/lib/index.css
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@finos/legend-lego",
|
3
|
-
"version": "1.2.
|
3
|
+
"version": "1.2.3",
|
4
4
|
"description": "Legend shared advanced application components and building blocks",
|
5
5
|
"keywords": [
|
6
6
|
"legend",
|
@@ -26,8 +26,6 @@
|
|
26
26
|
"./code-editor/test/MockedMonacoEditor.js": "./lib/code-editor/__test-utils__/MockedMonacoEditor.js",
|
27
27
|
"./data-grid": "./lib/data-grid/index.js",
|
28
28
|
"./graph-editor": "./lib/graph-editor/index.js",
|
29
|
-
"./download-helper": "./lib/download-helper/index.js",
|
30
|
-
"./download-helper/test/MockedDownloadHelper.js": "./lib/download-helper/__test-utils__/MockedDownloadHelper.js",
|
31
29
|
"./lib/index.css": "./lib/index.css"
|
32
30
|
},
|
33
31
|
"module": "lib/index.js",
|
@@ -65,9 +63,9 @@
|
|
65
63
|
"@ag-grid-enterprise/server-side-row-model": "30.2.0",
|
66
64
|
"@ag-grid-enterprise/side-bar": "30.2.0",
|
67
65
|
"@ag-grid-enterprise/status-bar": "30.2.0",
|
68
|
-
"@finos/legend-application": "15.0.
|
66
|
+
"@finos/legend-application": "15.0.67",
|
69
67
|
"@finos/legend-art": "7.1.19",
|
70
|
-
"@finos/legend-graph": "31.7.
|
68
|
+
"@finos/legend-graph": "31.7.3",
|
71
69
|
"@finos/legend-shared": "10.0.32",
|
72
70
|
"@types/css-font-loading-module": "0.0.10",
|
73
71
|
"@types/react": "18.2.31",
|
@@ -80,7 +78,7 @@
|
|
80
78
|
"react-dom": "18.2.0"
|
81
79
|
},
|
82
80
|
"devDependencies": {
|
83
|
-
"@finos/legend-dev-utils": "2.1.
|
81
|
+
"@finos/legend-dev-utils": "2.1.3",
|
84
82
|
"@jest/globals": "29.7.0",
|
85
83
|
"cross-env": "7.0.3",
|
86
84
|
"eslint": "8.52.0",
|
package/tsconfig.json
CHANGED
@@ -39,8 +39,6 @@
|
|
39
39
|
"./src/code-editor/__test-utils__/MockedMonacoEditor.ts",
|
40
40
|
"./src/code-editor/themes/MonacoEditorThemeUtils.ts",
|
41
41
|
"./src/data-grid/index.ts",
|
42
|
-
"./src/download-helper/index.ts",
|
43
|
-
"./src/download-helper/__test-utils__/MockedDownloadHelper.ts",
|
44
42
|
"./src/graph-editor/index.ts",
|
45
43
|
"./src/application/ActivityBar.tsx",
|
46
44
|
"./src/application/DocumentationLink.tsx",
|
@@ -1,18 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) 2020-present, Goldman Sachs
|
3
|
-
*
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
7
|
-
*
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
|
-
*/
|
16
|
-
export declare function downloadStream(): Promise<void>;
|
17
|
-
export declare function registerDownloadHelperServiceWorker(): void;
|
18
|
-
//# sourceMappingURL=MockedDownloadHelper.d.ts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"MockedDownloadHelper.d.ts","sourceRoot":"","sources":["../../../src/download-helper/__test-utils__/MockedDownloadHelper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAAG;AACxD,wBAAgB,mCAAmC,IAAI,IAAI,CAAG"}
|
@@ -1,18 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) 2020-present, Goldman Sachs
|
3
|
-
*
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
7
|
-
*
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
|
-
*/
|
16
|
-
export async function downloadStream() { }
|
17
|
-
export function registerDownloadHelperServiceWorker() { }
|
18
|
-
//# sourceMappingURL=MockedDownloadHelper.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"MockedDownloadHelper.js","sourceRoot":"","sources":["../../../src/download-helper/__test-utils__/MockedDownloadHelper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,MAAM,CAAC,KAAK,UAAU,cAAc,KAAmB,CAAC;AACxD,MAAM,UAAU,mCAAmC,KAAU,CAAC"}
|
@@ -1,18 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) 2020-present, Goldman Sachs
|
3
|
-
*
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
7
|
-
*
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
|
-
*/
|
16
|
-
export declare function downloadStream(responseBody: ReadableStream, filename: string): Promise<void>;
|
17
|
-
export declare function registerDownloadHelperServiceWorker(): void;
|
18
|
-
//# sourceMappingURL=index.d.ts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/download-helper/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAqGH,wBAAsB,cAAc,CAClC,YAAY,EAAE,cAAc,EAC5B,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CAsBf;AAED,wBAAgB,mCAAmC,IAAI,IAAI,CAe1D"}
|
@@ -1,122 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) 2020-present, Goldman Sachs
|
3
|
-
*
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
7
|
-
*
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
|
-
*/
|
16
|
-
var DOWNLOAD_EVENTS;
|
17
|
-
(function (DOWNLOAD_EVENTS) {
|
18
|
-
DOWNLOAD_EVENTS["TAG_REQUEST"] = "download-request";
|
19
|
-
DOWNLOAD_EVENTS["TAG_RESPONSE"] = "download-response";
|
20
|
-
DOWNLOAD_EVENTS["STREAM_CLOSED"] = "#stream-closed";
|
21
|
-
DOWNLOAD_EVENTS["STREAM_ABORTED"] = "#stream-aborted";
|
22
|
-
})(DOWNLOAD_EVENTS || (DOWNLOAD_EVENTS = {}));
|
23
|
-
function createWritableStreamFromMessageChannel(channel) {
|
24
|
-
return new WritableStream({
|
25
|
-
write(chunk) {
|
26
|
-
channel.port1.postMessage(chunk);
|
27
|
-
},
|
28
|
-
close() {
|
29
|
-
channel.port1.postMessage(DOWNLOAD_EVENTS.STREAM_CLOSED);
|
30
|
-
},
|
31
|
-
abort() {
|
32
|
-
channel.port1.postMessage(DOWNLOAD_EVENTS.STREAM_ABORTED);
|
33
|
-
closeMessagePort(channel.port1);
|
34
|
-
closeMessagePort(channel.port2);
|
35
|
-
},
|
36
|
-
});
|
37
|
-
}
|
38
|
-
function closeMessagePort(port) {
|
39
|
-
port.onmessage = null;
|
40
|
-
port.close();
|
41
|
-
}
|
42
|
-
async function getServiceWorker() {
|
43
|
-
if (!('serviceWorker' in navigator)) {
|
44
|
-
return Promise.reject(new Error('Service worker is not available. Service Worker requires HTTPS protocol'));
|
45
|
-
}
|
46
|
-
return navigator.serviceWorker
|
47
|
-
.getRegistration()
|
48
|
-
.then((workerRegistration) => {
|
49
|
-
if (workerRegistration === undefined) {
|
50
|
-
return undefined;
|
51
|
-
}
|
52
|
-
const pending = workerRegistration.installing ?? workerRegistration.waiting;
|
53
|
-
return (workerRegistration.active ??
|
54
|
-
new Promise((resolve) => {
|
55
|
-
// if not activated, add listener to waiting or installing registration
|
56
|
-
const listener = () => {
|
57
|
-
if (pending?.state === 'activated') {
|
58
|
-
pending.removeEventListener('statechange', listener);
|
59
|
-
resolve(workerRegistration.active ?? undefined);
|
60
|
-
}
|
61
|
-
};
|
62
|
-
pending?.addEventListener('statechange', listener);
|
63
|
-
}));
|
64
|
-
});
|
65
|
-
}
|
66
|
-
function createDownloadRequest(filename) {
|
67
|
-
const PREFIX = 6;
|
68
|
-
const prefix = String(Math.random()).slice(-PREFIX);
|
69
|
-
const url = new URL(`${prefix}/${filename}`, window.location.href).toString();
|
70
|
-
return { tag: DOWNLOAD_EVENTS.TAG_REQUEST, filename, url };
|
71
|
-
}
|
72
|
-
function handleServiceWorkerDownloadResponse(event) {
|
73
|
-
const data = event.data;
|
74
|
-
if (data?.tag === DOWNLOAD_EVENTS.TAG_RESPONSE && data.downloadUrl.length) {
|
75
|
-
openInIframe(data.downloadUrl);
|
76
|
-
}
|
77
|
-
}
|
78
|
-
function openInIframe(src) {
|
79
|
-
const iframe = document.createElement('iframe');
|
80
|
-
iframe.hidden = true;
|
81
|
-
iframe.src = src;
|
82
|
-
document.body.appendChild(iframe);
|
83
|
-
return iframe;
|
84
|
-
}
|
85
|
-
export async function downloadStream(responseBody, filename) {
|
86
|
-
// creates communication channel with service worker with response handler
|
87
|
-
const channel = new MessageChannel();
|
88
|
-
channel.port1.onmessage = handleServiceWorkerDownloadResponse;
|
89
|
-
// grabs service worker and handles it download along with response channel port
|
90
|
-
const serviceWorker = await getServiceWorker();
|
91
|
-
if (!serviceWorker) {
|
92
|
-
return;
|
93
|
-
}
|
94
|
-
const downloadRequest = createDownloadRequest(filename);
|
95
|
-
serviceWorker.postMessage(downloadRequest, [channel.port2]);
|
96
|
-
// creates new data stream over communication channel and pipes given stream in it
|
97
|
-
responseBody
|
98
|
-
.pipeTo(createWritableStreamFromMessageChannel(channel))
|
99
|
-
.then(() => {
|
100
|
-
// TODO: trace success
|
101
|
-
})
|
102
|
-
.catch(() => {
|
103
|
-
// TODO: fail
|
104
|
-
});
|
105
|
-
}
|
106
|
-
export function registerDownloadHelperServiceWorker() {
|
107
|
-
if ('serviceWorker' in navigator) {
|
108
|
-
navigator.serviceWorker
|
109
|
-
.register('ServiceWorker.js')
|
110
|
-
.then((reg) => {
|
111
|
-
// TODO: add trace
|
112
|
-
// eslint-disable-next-line no-console
|
113
|
-
console.debug('register service worker success', reg);
|
114
|
-
})
|
115
|
-
.catch((error) => {
|
116
|
-
// TODO: add trace
|
117
|
-
// eslint-disable-next-line no-console
|
118
|
-
console.debug('register service worker error', error);
|
119
|
-
});
|
120
|
-
}
|
121
|
-
}
|
122
|
-
//# sourceMappingURL=index.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/download-helper/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,IAAK,eAKJ;AALD,WAAK,eAAe;IAClB,mDAAgC,CAAA;IAChC,qDAAkC,CAAA;IAClC,mDAAgC,CAAA;IAChC,qDAAkC,CAAA;AACpC,CAAC,EALI,eAAe,KAAf,eAAe,QAKnB;AAED,SAAS,sCAAsC,CAC7C,OAAuB;IAEvB,OAAO,IAAI,cAAc,CAAC;QACxB,KAAK,CAAC,KAAK;YACT,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QACnC,CAAC;QACD,KAAK;YACH,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,eAAe,CAAC,aAAa,CAAC,CAAC;QAC3D,CAAC;QACD,KAAK;YACH,OAAO,CAAC,KAAK,CAAC,WAAW,CAAC,eAAe,CAAC,cAAc,CAAC,CAAC;YAC1D,gBAAgB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YAChC,gBAAgB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAClC,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED,SAAS,gBAAgB,CAAC,IAAiB;IACzC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;IACtB,IAAI,CAAC,KAAK,EAAE,CAAC;AACf,CAAC;AAaD,KAAK,UAAU,gBAAgB;IAC7B,IAAI,CAAC,CAAC,eAAe,IAAI,SAAS,CAAC,EAAE;QACnC,OAAO,OAAO,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QAAQ,CAAC,kBAAkB,CAAC;aAC5B,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;YACZ,kBAAkB;YAClB,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,GAAG,CAAC,CAAC;QACxD,CAAC,CAAC;aACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACf,kBAAkB;YAClB,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,+BAA+B,EAAE,KAAK,CAAC,CAAC;QACxD,CAAC,CAAC,CAAC;KACN;AACH,CAAC"}
|
@@ -1,18 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) 2020-present, Goldman Sachs
|
3
|
-
*
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
7
|
-
*
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
|
-
*/
|
16
|
-
|
17
|
-
export async function downloadStream(): Promise<void> {}
|
18
|
-
export function registerDownloadHelperServiceWorker(): void {}
|
@@ -1,158 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) 2020-present, Goldman Sachs
|
3
|
-
*
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
7
|
-
*
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
|
-
*/
|
16
|
-
|
17
|
-
enum DOWNLOAD_EVENTS {
|
18
|
-
TAG_REQUEST = 'download-request',
|
19
|
-
TAG_RESPONSE = 'download-response',
|
20
|
-
STREAM_CLOSED = '#stream-closed',
|
21
|
-
STREAM_ABORTED = '#stream-aborted',
|
22
|
-
}
|
23
|
-
|
24
|
-
function createWritableStreamFromMessageChannel(
|
25
|
-
channel: MessageChannel,
|
26
|
-
): WritableStream {
|
27
|
-
return new WritableStream({
|
28
|
-
write(chunk) {
|
29
|
-
channel.port1.postMessage(chunk);
|
30
|
-
},
|
31
|
-
close() {
|
32
|
-
channel.port1.postMessage(DOWNLOAD_EVENTS.STREAM_CLOSED);
|
33
|
-
},
|
34
|
-
abort() {
|
35
|
-
channel.port1.postMessage(DOWNLOAD_EVENTS.STREAM_ABORTED);
|
36
|
-
closeMessagePort(channel.port1);
|
37
|
-
closeMessagePort(channel.port2);
|
38
|
-
},
|
39
|
-
});
|
40
|
-
}
|
41
|
-
|
42
|
-
function closeMessagePort(port: MessagePort): void {
|
43
|
-
port.onmessage = null;
|
44
|
-
port.close();
|
45
|
-
}
|
46
|
-
|
47
|
-
interface LegendApplicationDownloadRequest {
|
48
|
-
tag: string;
|
49
|
-
filename: string;
|
50
|
-
url: string;
|
51
|
-
}
|
52
|
-
|
53
|
-
interface LegendApplicationTagResponse {
|
54
|
-
tag: string;
|
55
|
-
downloadUrl: string;
|
56
|
-
}
|
57
|
-
|
58
|
-
async function getServiceWorker(): Promise<ServiceWorker | undefined> {
|
59
|
-
if (!('serviceWorker' in navigator)) {
|
60
|
-
return Promise.reject(
|
61
|
-
new Error(
|
62
|
-
'Service worker is not available. Service Worker requires HTTPS protocol',
|
63
|
-
),
|
64
|
-
);
|
65
|
-
}
|
66
|
-
return navigator.serviceWorker
|
67
|
-
.getRegistration()
|
68
|
-
.then((workerRegistration) => {
|
69
|
-
if (workerRegistration === undefined) {
|
70
|
-
return undefined;
|
71
|
-
}
|
72
|
-
const pending =
|
73
|
-
workerRegistration.installing ?? workerRegistration.waiting;
|
74
|
-
return (
|
75
|
-
workerRegistration.active ??
|
76
|
-
new Promise((resolve) => {
|
77
|
-
// if not activated, add listener to waiting or installing registration
|
78
|
-
const listener = (): void => {
|
79
|
-
if (pending?.state === 'activated') {
|
80
|
-
pending.removeEventListener('statechange', listener);
|
81
|
-
resolve(workerRegistration.active ?? undefined);
|
82
|
-
}
|
83
|
-
};
|
84
|
-
pending?.addEventListener('statechange', listener);
|
85
|
-
})
|
86
|
-
);
|
87
|
-
});
|
88
|
-
}
|
89
|
-
|
90
|
-
function createDownloadRequest(
|
91
|
-
filename: string,
|
92
|
-
): LegendApplicationDownloadRequest {
|
93
|
-
const PREFIX = 6;
|
94
|
-
const prefix = String(Math.random()).slice(-PREFIX);
|
95
|
-
const url = new URL(`${prefix}/${filename}`, window.location.href).toString();
|
96
|
-
return { tag: DOWNLOAD_EVENTS.TAG_REQUEST, filename, url };
|
97
|
-
}
|
98
|
-
|
99
|
-
function handleServiceWorkerDownloadResponse(
|
100
|
-
event: MessageEvent<LegendApplicationTagResponse | undefined>,
|
101
|
-
): void {
|
102
|
-
const data = event.data;
|
103
|
-
if (data?.tag === DOWNLOAD_EVENTS.TAG_RESPONSE && data.downloadUrl.length) {
|
104
|
-
openInIframe(data.downloadUrl);
|
105
|
-
}
|
106
|
-
}
|
107
|
-
|
108
|
-
function openInIframe(src: string): HTMLIFrameElement {
|
109
|
-
const iframe = document.createElement('iframe');
|
110
|
-
iframe.hidden = true;
|
111
|
-
iframe.src = src;
|
112
|
-
document.body.appendChild(iframe);
|
113
|
-
return iframe;
|
114
|
-
}
|
115
|
-
|
116
|
-
export async function downloadStream(
|
117
|
-
responseBody: ReadableStream,
|
118
|
-
filename: string,
|
119
|
-
): Promise<void> {
|
120
|
-
// creates communication channel with service worker with response handler
|
121
|
-
const channel = new MessageChannel();
|
122
|
-
channel.port1.onmessage = handleServiceWorkerDownloadResponse;
|
123
|
-
|
124
|
-
// grabs service worker and handles it download along with response channel port
|
125
|
-
const serviceWorker = await getServiceWorker();
|
126
|
-
if (!serviceWorker) {
|
127
|
-
return;
|
128
|
-
}
|
129
|
-
const downloadRequest = createDownloadRequest(filename);
|
130
|
-
serviceWorker.postMessage(downloadRequest, [channel.port2]);
|
131
|
-
|
132
|
-
// creates new data stream over communication channel and pipes given stream in it
|
133
|
-
responseBody
|
134
|
-
.pipeTo(createWritableStreamFromMessageChannel(channel))
|
135
|
-
.then(() => {
|
136
|
-
// TODO: trace success
|
137
|
-
})
|
138
|
-
.catch(() => {
|
139
|
-
// TODO: fail
|
140
|
-
});
|
141
|
-
}
|
142
|
-
|
143
|
-
export function registerDownloadHelperServiceWorker(): void {
|
144
|
-
if ('serviceWorker' in navigator) {
|
145
|
-
navigator.serviceWorker
|
146
|
-
.register('ServiceWorker.js')
|
147
|
-
.then((reg) => {
|
148
|
-
// TODO: add trace
|
149
|
-
// eslint-disable-next-line no-console
|
150
|
-
console.debug('register service worker success', reg);
|
151
|
-
})
|
152
|
-
.catch((error) => {
|
153
|
-
// TODO: add trace
|
154
|
-
// eslint-disable-next-line no-console
|
155
|
-
console.debug('register service worker error', error);
|
156
|
-
});
|
157
|
-
}
|
158
|
-
}
|