Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Translating the Header object for globalFetch's RPC call. #25

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions web-apis/bg/experimental/global-fetch.js
Expand Up @@ -17,7 +17,7 @@ const LAB_API_ID = 'globalFetch'
module.exports = {
async fetch (reqOptions, reqBody) {
// parse url
var urlp = new URL(reqOptions.url)
let urlp = new URL(reqOptions.url)
reqOptions.protocol = urlp.protocol
reqOptions.host = urlp.host
reqOptions.path = urlp.pathname + urlp.search + urlp.hash
Expand All @@ -36,15 +36,15 @@ module.exports = {

return new Promise((resolve, reject) => {
// start request
var proto = urlp.protocol === 'https:' ? https : http
var reqStream = proto.request(reqOptions, resStream => {
let proto = urlp.protocol === 'https:' ? https : http
let reqStream = proto.request(reqOptions, resStream => {
resStream.pipe(concat(resStream, resBody => {
// resolve with response
resolve({
status: resStream.statusCode,
statusText: resStream.statusMessage,
headers: resStream.headers,
body: resBody
body: (resStream.statusCode != 204 && resStream.statusCode != 304 ? resBody : null)
})
}))

Expand Down
11 changes: 7 additions & 4 deletions web-apis/fg/experimental.js
Expand Up @@ -31,15 +31,18 @@ exports.setup = function (rpc) {

// experimental.globalFetch
experimental.globalFetch = async function globalFetch (input, init) {
var request = new Request(input, init)
let request = new Request(input, init)
if (request.method !== 'HEAD' && request.method !== 'GET') {
throw new Error('Only HEAD and GET requests are currently supported by globalFetch()')
}
try {
var responseData = await globalFetchRPC.fetch({
if (request.compress)
request.headers.set('accept-encoding', 'gzip,deflate')
let headers = {}
request.headers.forEach((val, name) => headers[name] = val)
let responseData = await globalFetchRPC.fetch({headers,
method: request.method,
url: request.url,
headers: request.headers
url: request.url
})
return new Response(responseData.body, responseData)
} catch (e) {
Expand Down