From c7b8c935a6b02dc4f9e5c20c6911b2e59db8fa64 Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Sat, 15 Jan 2022 15:13:29 +0100 Subject: [PATCH 1/3] fix(android): `cancelRequest` fix --- src/https.android.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/https.android.ts b/src/https.android.ts index 8671be7..1a50294 100644 --- a/src/https.android.ts +++ b/src/https.android.ts @@ -346,7 +346,10 @@ function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHtt return Client; } -export function cancelRequest(tag: string, client: okhttp3.OkHttpClient) { +export function cancelRequest(tag: string, client: okhttp3.OkHttpClient = runningClients[tag]) { + if (!client) { + return; + } if (notClosedResponses[tag]) { notClosedResponses[tag].cancel(); return; @@ -382,6 +385,9 @@ let CALL_ID = 0; const notClosedResponses: { [k: string]: com.nativescript.https.OkHttpResponse; } = {}; + +const runningClients: { [k: string]: okhttp3.OkHttpClient } = {}; + let OkHttpResponse: typeof com.nativescript.https.OkHttpResponse; export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsRequest { const client = getClient(false, opts.timeout); @@ -458,7 +464,7 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque } const tag = opts.tag || `okhttp_request_${CALL_ID++}`; const call = client.newCall(request.tag(tag).build()); - + runningClients[tag] = client; // We have to allow networking on the main thread because larger responses will crash the app with an NetworkOnMainThreadException. // Note that it would probably be better to offload it to a Worker or (natively running) AsyncTask. // Also note that once set, this policy remains active until the app is killed. @@ -472,6 +478,7 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque call.enqueue( new okhttp3.Callback({ onResponse(call, response) { + delete runningClients[tag]; const responseBody = response.body(); const message = response.message(); const statusCode = response.code(); @@ -522,6 +529,7 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque } }, onFailure(task, error) { + delete runningClients[tag]; reject(error); }, }) From 451b4fc52a470e700e7cb47703fb8c42f6198ea7 Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Tue, 18 Jan 2022 21:50:10 +0100 Subject: [PATCH 2/3] feat!: `useLegacy` is now default => * Your answer handling needs to be updated. `content` is now of type [HttpsResponseLegacy](https://github.com/farfromrefug/nativescript-https/blob/c7b8c935a6b02dc4f9e5c20c6911b2e59db8fa64/src/https.common.ts#L74) * `useLegacy` is not a request option anymore it is now the second optional parameter to the [request](https://github.com/farfromrefug/nativescript-https/blob/c7b8c935a6b02dc4f9e5c20c6911b2e59db8fa64/src/https.d.ts#L7) method --- src/https.android.ts | 10 +++++----- src/https.common.ts | 7 +++---- src/https.d.ts | 2 +- src/https.ios.ts | 12 +++++------- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/https.android.ts b/src/https.android.ts index 1a50294..8f32c3d 100644 --- a/src/https.android.ts +++ b/src/https.android.ts @@ -389,7 +389,7 @@ const notClosedResponses: { const runningClients: { [k: string]: okhttp3.OkHttpClient } = {}; let OkHttpResponse: typeof com.nativescript.https.OkHttpResponse; -export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsRequest { +export function createRequest(opts: Https.HttpsRequestOptions, useLegacy: boolean = true): Https.HttpsRequest { const client = getClient(false, opts.timeout); const request = new okhttp3.Request.Builder(); @@ -468,7 +468,7 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque // We have to allow networking on the main thread because larger responses will crash the app with an NetworkOnMainThreadException. // Note that it would probably be better to offload it to a Worker or (natively running) AsyncTask. // Also note that once set, this policy remains active until the app is killed. - if (opts.useLegacy === false && opts.allowLargeResponse) { + if (useLegacy === false && opts.allowLargeResponse) { android.os.StrictMode.setThreadPolicy(android.os.StrictMode.ThreadPolicy.LAX); } return { @@ -494,7 +494,7 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque } return headers; }; - if (opts.useLegacy) { + if (useLegacy) { if (!OkHttpResponse) { OkHttpResponse = com.nativescript.https.OkHttpResponse; } @@ -538,10 +538,10 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque }; } -export function request(opts: Https.HttpsRequestOptions) { +export function request(opts: Https.HttpsRequestOptions, useLegacy: boolean = true) { return new Promise((resolve, reject) => { try { - createRequest(opts).run(resolve, reject); + createRequest(opts, useLegacy).run(resolve, reject); } catch (error) { reject(error); } diff --git a/src/https.common.ts b/src/https.common.ts index f951fc0..99e434d 100644 --- a/src/https.common.ts +++ b/src/https.common.ts @@ -52,7 +52,6 @@ export interface HttpsRequestOptions extends HttpRequestOptions { onProgress?: (current: number, total: number) => void; cachePolicy?: CachePolicy; - useLegacy?: boolean; } export interface HttpsResponse { @@ -71,14 +70,14 @@ export interface HttpsRequest { run(success, failure); } -export interface HttpsResponseLegacy { +export interface HttpsResponseLegacy { toArrayBuffer(): ArrayBuffer; toArrayBufferAsync(): Promise; toString(): string; toStringAsync(): Promise; - toJSON(): any; - toJSONAsync(): Promise; + toJSON(): T; + toJSONAsync(): Promise; toImage(): Promise; // toImageAsync(): Promise; toFile(destinationFilePath: string): Promise; diff --git a/src/https.d.ts b/src/https.d.ts index 3bd3362..177c3ba 100644 --- a/src/https.d.ts +++ b/src/https.d.ts @@ -4,7 +4,7 @@ export function enableSSLPinning(options: Https.HttpsSSLPinningOptions); export function disableSSLPinning(); -export function request(options: Https.HttpsRequestOptions): Promise>; +export function request(options: Https.HttpsRequestOptions, useLegacy?: U): U extends true ? Promise> : Promise>; export function setCache(options?: Https.CacheOptions); export function clearCache(); export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsRequest; diff --git a/src/https.ios.ts b/src/https.ios.ts index fb81b90..7e1d236 100644 --- a/src/https.ios.ts +++ b/src/https.ios.ts @@ -306,7 +306,7 @@ export function cancelRequest(tag: string) { } } -export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsRequest { +export function createRequest(opts: Https.HttpsRequestOptions, useLegacy: boolean = true): Https.HttpsRequest { const type = opts.headers && opts.headers['Content-Type'] ? (opts.headers['Content-Type'] as string) : 'application/json'; if (type.startsWith('application/json')) { manager.requestSerializer = AFJSONRequestSerializer.serializer(); @@ -356,12 +356,10 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque manager.requestSerializer.timeoutInterval = opts.timeout ? opts.timeout : 10; - const useLegacy = Utils.isDefined(opts.useLegacy) ? opts.useLegacy : false; - const progress = opts.onProgress ? (progress: NSProgress) => { - opts.onProgress(progress.completedUnitCount, progress.totalUnitCount); - } + opts.onProgress(progress.completedUnitCount, progress.totalUnitCount); + } : null; let task: NSURLSessionDataTask; const tag = opts.tag; @@ -473,10 +471,10 @@ export function createRequest(opts: Https.HttpsRequestOptions): Https.HttpsReque }, }; } -export function request(opts: Https.HttpsRequestOptions) { +export function request(opts: Https.HttpsRequestOptions, useLegacy: boolean = true) { return new Promise((resolve, reject) => { try { - createRequest(opts).run(resolve, reject); + createRequest(opts, useLegacy).run(resolve, reject); } catch (error) { reject(error); } From 29247b1260651e7eb3499e14c0e50461fe42fde4 Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Tue, 18 Jan 2022 21:50:28 +0100 Subject: [PATCH 3/3] v3.3.8 --- CHANGELOG.md | 11 +++++++++++ lerna.json | 2 +- plugin/CHANGELOG.md | 8 ++++++++ plugin/package.json | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ded60..ae735b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.3.8](https://github.com/farfromrefug/nativescript-https/compare/v3.3.7...v3.3.8) (2022-01-18) + + +### Bug Fixes + +* **android:** `cancelRequest` fix ([c7b8c93](https://github.com/farfromrefug/nativescript-https/commit/c7b8c935a6b02dc4f9e5c20c6911b2e59db8fa64)) + + + + + ## [3.3.7](https://github.com/farfromrefug/nativescript-https/compare/v3.3.6...v3.3.7) (2022-01-15) diff --git a/lerna.json b/lerna.json index cd17989..e1f79b0 100644 --- a/lerna.json +++ b/lerna.json @@ -2,7 +2,7 @@ "packages": [ "plugin" ], - "version": "3.3.7", + "version": "3.3.8", "command": { "publish": { "conventionalCommits": true diff --git a/plugin/CHANGELOG.md b/plugin/CHANGELOG.md index 8798440..9a00196 100644 --- a/plugin/CHANGELOG.md +++ b/plugin/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [3.3.8](https://github.com/farfromrefug/nativescript-https/compare/v3.3.7...v3.3.8) (2022-01-18) + +**Note:** Version bump only for package @nativescript-community/https + + + + + ## [3.3.7](https://github.com/farfromrefug/nativescript-https/compare/v3.3.6...v3.3.7) (2022-01-15) **Note:** Version bump only for package @nativescript-community/https diff --git a/plugin/package.json b/plugin/package.json index e865d95..532bde8 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript-community/https", - "version": "3.3.7", + "version": "3.3.8", "description": "Nativescript plugin for gestures", "main": "https", "sideEffects": false,