Copilot commented on code in PR #891: URL: https://github.com/apache/rocketmq-dashboard/pull/891#discussion_r3708489583
########## web/src/utils/download.test.ts: ########## @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { downloadBlob } from './download'; + +describe('downloadBlob', () => { + afterEach(() => { + document.body.innerHTML = ''; + vi.restoreAllMocks(); + }); + + it('clicks an attached temporary anchor and removes it after download', () => { + const createObjectURL = vi.fn(() => 'blob:download'); + const revokeObjectURL = vi.fn(); + Object.defineProperty(URL, 'createObjectURL', { + writable: true, + value: createObjectURL, + }); + Object.defineProperty(URL, 'revokeObjectURL', { + writable: true, + value: revokeObjectURL, + }); Review Comment: This test overrides `URL.createObjectURL` / `URL.revokeObjectURL` via `Object.defineProperty`, but the overrides are not restored, which can leak global state into other test files running in the same worker. Prefer capturing the original functions, assigning typed mocks, and restoring the originals in `afterEach`. ########## web/src/utils/download.ts: ########## @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const downloadBlob = (blob: Blob, filename: string) => { + const url = URL.createObjectURL(blob); + const anchor = document.createElement('a'); + anchor.href = url; + anchor.download = filename; + anchor.style.display = 'none'; + document.body.appendChild(anchor); + anchor.click(); + anchor.remove(); + URL.revokeObjectURL(url); +}; Review Comment: `downloadBlob` performs DOM cleanup and `URL.revokeObjectURL` after `anchor.click()`, but without a `try/finally`. If `appendChild`/`click` throws (e.g., browser blocks programmatic download), the temporary anchor and object URL can be leaked. Wrap the click path in `try/finally` so cleanup is guaranteed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
