-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commonize on port-connected behavior for compressors and decompressor…
…s (part of the refactor for issue #44)
- Loading branch information
Showing
7 changed files
with
114 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* common.js | ||
* | ||
* Provides common functionality for compressing and decompressing. | ||
* | ||
* Licensed under the MIT License | ||
* | ||
* Copyright(c) 2023 Google Inc. | ||
*/ | ||
|
||
// Requires the following JavaScript features: MessageChannel, MessagePort, and dynamic imports. | ||
|
||
/** | ||
* Connects a host to a compress/decompress implementation via MessagePorts. The implementation must | ||
* have an exported connect() function that accepts a MessagePort. If the runtime support Workers | ||
* (e.g. web browsers, deno), imports the implementation inside a Web Worker. Otherwise, it | ||
* dynamically imports the implementation inside the current JS context (node, bun). | ||
* @param {string} implFilename The compressor/decompressor implementation filename relative to this | ||
* path (e.g. './unzip.js'). | ||
* @returns {Promise<MessagePort>} The Promise resolves to the MessagePort connected to the | ||
* implementation that the host should use. | ||
*/ | ||
export async function getConnectedPort(implFilename) { | ||
const messageChannel = new MessageChannel(); | ||
const hostPort = messageChannel.port1; | ||
const implPort = messageChannel.port2; | ||
|
||
if (typeof Worker === 'undefined') { | ||
const implModule = await import(`${implFilename}`); | ||
await implModule.connect(implPort); | ||
return hostPort; | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const workerScriptPath = new URL(`./webworker-wrapper.js`, import.meta.url).href; | ||
const worker = new Worker(workerScriptPath, { type: 'module' }); | ||
worker.postMessage({ implSrc: implFilename }, [implPort]); | ||
resolve(hostPort); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* common.js | ||
* | ||
* Provides common functionality for compressing and decompressing. | ||
* | ||
* Licensed under the MIT License | ||
* | ||
* Copyright(c) 2023 Google Inc. | ||
*/ | ||
/** | ||
* Connects a host to a compress/decompress implementation via MessagePorts. The implementation must | ||
* have an exported connect() function that accepts a MessagePort. If the runtime support Workers | ||
* (e.g. web browsers, deno), imports the implementation inside a Web Worker. Otherwise, it | ||
* dynamically imports the implementation inside the current JS context (node, bun). | ||
* @param {string} implFilename The compressor/decompressor implementation filename relative to this | ||
* path (e.g. './unzip.js'). | ||
* @returns {Promise<MessagePort>} The Promise resolves to the MessagePort connected to the | ||
* implementation that the host should use. | ||
*/ | ||
export function getConnectedPort(implFilename: string): Promise<MessagePort>; | ||
//# sourceMappingURL=common.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.