現在chrome-extensionを作っておりましてサイドバーを使う機能になっています。
サイドバー内のUI(iframeで出来ています)から制限なく通信を行う必要がありました。そこでbackground(event page)経由でfetchを行う
background-fetch(proxy-fetch)を実装し運用しておりました。
ところが特定の通信でエラーが発生していることがわかり、書き換えを試みたのですが動作が悪化してしまいました。
お手数ですが添削をお願いできませんでしょうか?
もしくはnpm packageで同じ目的のfetch代替のライブラリがありましたら教えていただけないでしょうか?
以下が既存の実装となります。
- 変更前
ts
1// frontend側のfetchを代行する TODO 要リファクタリング 2export interface FetchInterface { 3 type: "fetch"; 4 input: RequestInfo; 5 init?: RequestInit; 6} 7 8export interface ResultInterface { 9 type: "result" | "error"; 10 result: Object; 11} 12 13// EventPageで本関数実行することでProxy側の準備を行う 14export function backgroundFetchReady() { 15 // eventページ以外で実行したら例外を発生 16 expectBackground(); 17 chrome.runtime.onMessage.addListener((inFetchData, sender, sendResponse) => { 18 let fetchData: FetchInterface = <FetchInterface>inFetchData; 19 if (fetchData.type === undefined) { 20 return; 21 } 22 if (fetchData.type !== "fetch") { 23 return; 24 } 25 26 fetch(fetchData.input, fetchData.init).then(async result => { 27 let json = {}; 28 try { 29 json = await result.json(); 30 } catch (e) { 31 // do nothing 32 } 33 sendResponse(<ResultInterface>{ 34 type: "result", 35 result: { 36 status: result.status, 37 statusText: result.statusText, 38 json: json 39 } 40 }); 41 }); 42 return true; 43 }); 44} 45 46// fetch代替関数 fetchに置き換えることで通信を行う 47export async function backgroundFetch(input: RequestInfo, init?: RequestInit) { 48 return new Promise<Response>((resolve, reject) => { 49 chrome.runtime.sendMessage( 50 <FetchInterface>{ 51 type: "fetch", 52 input, 53 init 54 }, 55 (response: ResultInterface) => { 56 if (response === undefined || response.type === undefined) { 57 return; 58 } 59 // console.log('response', response); 60 if (response.type === "result") { 61 // console.log('in response.type is result', response.result); 62 const res = <{ 63 status: number; 64 statusText: string; 65 json: Object; 66 }>response.result; 67 resolve(<Response>{ 68 status: res.status, 69 statusText: res.statusText, 70 json: () => res.json 71 }); 72 } else { 73 // console.log('in response.type is error', response.result); 74 reject(response.result); 75 } 76 } 77 ); 78 }); 79}
上記が暫く使っていたコードです。
fetch(fetchData.input, fetchData.init).then(async result
の箇所でresultに値が入らないパターンがあったため想定外のエラーが出ていました。
それと asyncを使う箇所が気持ち悪かったのでbackgroundFetchReadyのみ以下のように書き換えました。
- 変更後
export function backgroundFetchReady() { expectBackground(); chrome.runtime.onMessage.addListener( async (inFetchData, sender, sendResponse) => { let fetchData: FetchInterface = <FetchInterface>inFetchData; if (fetchData.type !== "fetch") { return; } const response = await fetch(fetchData.input, fetchData.init).catch( e => logger.error(e) ); if (response) { const json = await response.json(); console.log(json) // debug用 sendResponse(<ResultInterface>{ type: "result", result: { status: response.status, statusText: response.statusText, json: json } }); } return true; } ); } ``` こちらだと``console.log(json)`` でレスポンスが返ってるのが見えるのに backgroundFetchのResponseが戻らなくなるパターンが生じました まず、代替ライブラリがパッケージなり標準であるようならそちらを使いたいので教えてください。 無けれは上記のコードの添削をおねがいしたいのです。 勝手なお願いで申し訳ございません。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/06 11:10