|
| 1 | +// The `MAIN` world script cannot use `browser.runtime.id`, but the uniqueness |
| 2 | +// of the event name is not an issue as the event is only dispatched on a |
| 3 | +// specific `script` element which is detached from the document. |
| 4 | +const SCRIPT_RESULT_EVENT_NAME = 'wxt:script-result'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Dispatch a `CustomEvent` for a successful script result which carries a |
| 8 | + * result value. |
| 9 | + */ |
| 10 | +export function dispatchScriptSuccessEvent( |
| 11 | + target: EventTarget, |
| 12 | + value: unknown, |
| 13 | +): boolean { |
| 14 | + return dispatchScriptResultEvent(target, scriptSuccess(value)); |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Dispatch a `CustomEvent` for a failed script result which carries an `Error` |
| 19 | + * value. |
| 20 | + */ |
| 21 | +export function dispatchScriptErrorEvent( |
| 22 | + target: EventTarget, |
| 23 | + error: Error, |
| 24 | +): boolean { |
| 25 | + return dispatchScriptResultEvent(target, scriptError(error)); |
| 26 | +} |
| 27 | + |
| 28 | +function dispatchScriptResultEvent( |
| 29 | + target: EventTarget, |
| 30 | + result: ScriptResult, |
| 31 | +): boolean { |
| 32 | + return target.dispatchEvent( |
| 33 | + new CustomEvent(SCRIPT_RESULT_EVENT_NAME, { detail: result }), |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Add a one-time event listener for a script result `CustomEvent`, and return a |
| 39 | + * `Promise` which either resolves with the result value or rejects with an |
| 40 | + * error depending on whether the result is successful or not. |
| 41 | + */ |
| 42 | +export function waitForScriptResultEvent( |
| 43 | + target: EventTarget, |
| 44 | +): Promise<unknown> { |
| 45 | + return new Promise((resolve, reject) => { |
| 46 | + target.addEventListener( |
| 47 | + SCRIPT_RESULT_EVENT_NAME, |
| 48 | + (event) => { |
| 49 | + try { |
| 50 | + resolve(parseScriptResultEvent(event)); |
| 51 | + } catch (err) { |
| 52 | + reject(err); |
| 53 | + } |
| 54 | + }, |
| 55 | + { once: true }, |
| 56 | + ); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +function parseScriptResultEvent(event: Event): unknown { |
| 61 | + if (!(event instanceof CustomEvent)) { |
| 62 | + throw new Error( |
| 63 | + `Expected a \`CustomEvent\`, got: ${JSON.stringify(event)}`, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + return parseScriptResult(event.detail as unknown); |
| 68 | +} |
| 69 | + |
| 70 | +type ScriptResult = |
| 71 | + | { type: 'success'; value: unknown } |
| 72 | + | { type: 'error'; error: Error }; |
| 73 | + |
| 74 | +function scriptSuccess(value: unknown): ScriptResult { |
| 75 | + return { type: 'success', value }; |
| 76 | +} |
| 77 | + |
| 78 | +function scriptError(error: Error): ScriptResult { |
| 79 | + return { type: 'error', error: deconstructError(error) }; |
| 80 | +} |
| 81 | + |
| 82 | +function parseScriptResult(result: unknown): unknown { |
| 83 | + if (!isScriptResult(result)) { |
| 84 | + throw new Error( |
| 85 | + `Expected a \`ScriptResult\`, got: ${JSON.stringify(result)}`, |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + switch (result.type) { |
| 90 | + case 'success': |
| 91 | + return result.value; |
| 92 | + case 'error': |
| 93 | + throw reconstructError(result.error); |
| 94 | + default: |
| 95 | + throw new Error( |
| 96 | + `Impossible \`ScriptResult\`: ${JSON.stringify(result satisfies never)}`, |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function isScriptResult(result: unknown): result is ScriptResult { |
| 102 | + return ( |
| 103 | + result != null && |
| 104 | + typeof result === 'object' && |
| 105 | + 'type' in result && |
| 106 | + typeof result.type === 'string' && |
| 107 | + ((result.type === 'success' && 'value' in result) || |
| 108 | + (result.type === 'error' && |
| 109 | + 'error' in result && |
| 110 | + isDeconstructedError(result.error))) |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * A representation of an `Error` which can be transmitted within a |
| 116 | + * `CustomEvent`. |
| 117 | + */ |
| 118 | +type DeconstructedError = { |
| 119 | + name: string; |
| 120 | + message: string; |
| 121 | + stack?: string; |
| 122 | +}; |
| 123 | + |
| 124 | +function deconstructError(error: Error): DeconstructedError { |
| 125 | + const result: DeconstructedError = { |
| 126 | + name: error.name, |
| 127 | + message: error.message, |
| 128 | + }; |
| 129 | + |
| 130 | + if ('stack' in error) result.stack = error.stack; |
| 131 | + |
| 132 | + return result; |
| 133 | +} |
| 134 | + |
| 135 | +function reconstructError(deconstructedError: DeconstructedError): Error { |
| 136 | + const error = new Error(deconstructedError.message); |
| 137 | + error.name = deconstructedError.name; |
| 138 | + |
| 139 | + if ('stack' in deconstructedError) { |
| 140 | + error.stack = deconstructedError.stack; |
| 141 | + } else { |
| 142 | + delete error.stack; |
| 143 | + } |
| 144 | + |
| 145 | + return error; |
| 146 | +} |
| 147 | + |
| 148 | +function isDeconstructedError(value: unknown): value is DeconstructedError { |
| 149 | + return ( |
| 150 | + value != null && |
| 151 | + typeof value === 'object' && |
| 152 | + 'name' in value && |
| 153 | + value.name != null && |
| 154 | + typeof value.name === 'string' && |
| 155 | + 'message' in value && |
| 156 | + value.message != null && |
| 157 | + typeof value.message === 'string' && |
| 158 | + (!('stack' in value) || |
| 159 | + (value.stack != null && typeof value.stack === 'string')) |
| 160 | + ); |
| 161 | +} |
0 commit comments