@capacitor/ios
Advanced tools
Comparing version 4.1.0 to 4.1.1-dev-6756a744-20220907T2202.0
@@ -244,2 +244,226 @@ | ||
}; | ||
/** | ||
* Safely web decode a string value (inspired by js-cookie) | ||
* @param str The string value to decode | ||
*/ | ||
const decode = (str) => str.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent); | ||
const platform = getPlatformId(win); | ||
if (platform == 'android' || platform == 'ios') { | ||
// patch document.cookie on Android/iOS | ||
win.CapacitorCookiesDescriptor = | ||
Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') || | ||
Object.getOwnPropertyDescriptor(HTMLDocument.prototype, 'cookie'); | ||
Object.defineProperty(document, 'cookie', { | ||
get: function () { | ||
if (platform === 'ios') { | ||
// Use prompt to synchronously get cookies. | ||
// https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323 | ||
const payload = { | ||
type: 'CapacitorCookies', | ||
}; | ||
const res = prompt(JSON.stringify(payload)); | ||
return res; | ||
} | ||
else if (typeof win.CapacitorCookiesAndroidInterface !== 'undefined') { | ||
return win.CapacitorCookiesAndroidInterface.getCookies(); | ||
} | ||
}, | ||
set: function (val) { | ||
const cookiePairs = val.split(';'); | ||
for (const cookiePair of cookiePairs) { | ||
const cookieKey = cookiePair.split('=')[0]; | ||
const cookieValue = cookiePair.split('=')[1]; | ||
if (null == cookieValue) { | ||
continue; | ||
} | ||
cap.toNative('CapacitorCookies', 'setCookie', { | ||
key: cookieKey, | ||
value: decode(cookieValue), | ||
}); | ||
} | ||
}, | ||
}); | ||
// patch fetch / XHR on Android/iOS | ||
// store original fetch & XHR functions | ||
win.CapacitorWebFetch = window.fetch; | ||
win.CapacitorWebXMLHttpRequest = { | ||
abort: window.XMLHttpRequest.prototype.abort, | ||
open: window.XMLHttpRequest.prototype.open, | ||
send: window.XMLHttpRequest.prototype.send, | ||
setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader, | ||
}; | ||
let doPatchHttp = true; | ||
// check if capacitor http is disabled before patching | ||
if (platform === 'ios') { | ||
// Use prompt to synchronously get capacitor http config. | ||
// https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323 | ||
const payload = { | ||
type: 'CapacitorHttp', | ||
}; | ||
const isDisabled = prompt(JSON.stringify(payload)); | ||
if (isDisabled === 'true') { | ||
doPatchHttp = false; | ||
} | ||
} | ||
else if (typeof win.CapacitorHttpAndroidInterface !== 'undefined') { | ||
const isDisabled = win.CapacitorHttpAndroidInterface.isDisabled(); | ||
if (isDisabled === true) { | ||
doPatchHttp = false; | ||
} | ||
} | ||
if (doPatchHttp) { | ||
// fetch patch | ||
window.fetch = async (resource, options) => { | ||
if (resource.toString().startsWith('data:')) { | ||
return win.CapacitorWebFetch(resource, options); | ||
} | ||
try { | ||
// intercept request & pass to the bridge | ||
const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', { | ||
url: resource, | ||
method: (options === null || options === void 0 ? void 0 : options.method) ? options.method : undefined, | ||
data: (options === null || options === void 0 ? void 0 : options.body) ? options.body : undefined, | ||
headers: (options === null || options === void 0 ? void 0 : options.headers) ? options.headers : undefined, | ||
}); | ||
// intercept & parse response before returning | ||
const response = new Response(JSON.stringify(nativeResponse.data), { | ||
headers: nativeResponse.headers, | ||
status: nativeResponse.status, | ||
}); | ||
return response; | ||
} | ||
catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}; | ||
// XHR event listeners | ||
const addEventListeners = function () { | ||
this.addEventListener('abort', function () { | ||
if (typeof this.onabort === 'function') | ||
this.onabort(); | ||
}); | ||
this.addEventListener('error', function () { | ||
if (typeof this.onerror === 'function') | ||
this.onerror(); | ||
}); | ||
this.addEventListener('load', function () { | ||
if (typeof this.onload === 'function') | ||
this.onload(); | ||
}); | ||
this.addEventListener('loadend', function () { | ||
if (typeof this.onloadend === 'function') | ||
this.onloadend(); | ||
}); | ||
this.addEventListener('loadstart', function () { | ||
if (typeof this.onloadstart === 'function') | ||
this.onloadstart(); | ||
}); | ||
this.addEventListener('readystatechange', function () { | ||
if (typeof this.onreadystatechange === 'function') | ||
this.onreadystatechange(); | ||
}); | ||
this.addEventListener('timeout', function () { | ||
if (typeof this.ontimeout === 'function') | ||
this.ontimeout(); | ||
}); | ||
}; | ||
// XHR patch abort | ||
window.XMLHttpRequest.prototype.abort = function () { | ||
Object.defineProperties(this, { | ||
_headers: { | ||
value: {}, | ||
writable: true, | ||
}, | ||
readyState: { | ||
get: function () { | ||
var _a; | ||
return (_a = this._readyState) !== null && _a !== void 0 ? _a : 0; | ||
}, | ||
set: function (val) { | ||
this._readyState = val; | ||
this.dispatchEvent(new Event('readystatechange')); | ||
}, | ||
}, | ||
response: { | ||
value: '', | ||
writable: true, | ||
}, | ||
responseText: { | ||
value: '', | ||
writable: true, | ||
}, | ||
responseURL: { | ||
value: '', | ||
writable: true, | ||
}, | ||
status: { | ||
value: 0, | ||
writable: true, | ||
}, | ||
}); | ||
this.readyState = 0; | ||
this.dispatchEvent(new Event('abort')); | ||
this.dispatchEvent(new Event('loadend')); | ||
}; | ||
// XHR patch open | ||
window.XMLHttpRequest.prototype.open = function (method, url) { | ||
this.abort(); | ||
addEventListeners.call(this); | ||
this._method = method; | ||
this._url = url; | ||
this.readyState = 1; | ||
}; | ||
// XHR patch set request header | ||
window.XMLHttpRequest.prototype.setRequestHeader = function (header, value) { | ||
this._headers[header] = value; | ||
}; | ||
// XHR patch send | ||
window.XMLHttpRequest.prototype.send = function (body) { | ||
try { | ||
this.readyState = 2; | ||
// intercept request & pass to the bridge | ||
cap | ||
.nativePromise('CapacitorHttp', 'request', { | ||
url: this._url, | ||
method: this._method, | ||
data: body !== null ? body : undefined, | ||
headers: this._headers, | ||
}) | ||
.then((nativeResponse) => { | ||
// intercept & parse response before returning | ||
if (this.readyState == 2) { | ||
this.dispatchEvent(new Event('loadstart')); | ||
this.status = nativeResponse.status; | ||
this.response = nativeResponse.data; | ||
this.responseText = JSON.stringify(nativeResponse.data); | ||
this.responseURL = nativeResponse.url; | ||
this.readyState = 4; | ||
this.dispatchEvent(new Event('load')); | ||
this.dispatchEvent(new Event('loadend')); | ||
} | ||
}) | ||
.catch((error) => { | ||
this.dispatchEvent(new Event('loadstart')); | ||
this.status = error.status; | ||
this.response = error.data; | ||
this.responseText = JSON.stringify(error.data); | ||
this.responseURL = error.url; | ||
this.readyState = 4; | ||
this.dispatchEvent(new Event('error')); | ||
this.dispatchEvent(new Event('loadend')); | ||
}); | ||
} | ||
catch (error) { | ||
this.dispatchEvent(new Event('loadstart')); | ||
this.status = 500; | ||
this.response = error; | ||
this.responseText = error.toString(); | ||
this.responseURL = this._url; | ||
this.readyState = 4; | ||
this.dispatchEvent(new Event('error')); | ||
this.dispatchEvent(new Event('loadend')); | ||
} | ||
}; | ||
} | ||
} | ||
// patch window.console on iOS and store original console fns | ||
@@ -246,0 +470,0 @@ const isIos = getPlatformId(win) === 'ios'; |
{ | ||
"name": "@capacitor/ios", | ||
"version": "4.1.0", | ||
"version": "4.1.1-dev-6756a744-20220907T2202.0", | ||
"description": "Capacitor: Cross-platform apps with JavaScript and the web", | ||
@@ -28,3 +28,3 @@ "homepage": "https://capacitorjs.com", | ||
"peerDependencies": { | ||
"@capacitor/core": "^4.0.0" | ||
"@capacitor/core": "^4.1.0" | ||
}, | ||
@@ -34,3 +34,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "0b5c2b740e9613512a100051b8822fd1361f4aeb" | ||
"gitHead": "6756a744466b66243d5ef7211f4842e6d4ab2caa" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
332176
96
711
1