From 2b96c634c21c4f6af8135bc4320ac123479f6376 Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 30 Dec 2021 21:02:05 +0800 Subject: [PATCH] feat(App): request with ArrayBuffer --- packages/uni-app-plus/dist/index.v3.js | 99 +++++++++++-------- .../app-plus/service/api/network/request.js | 90 ++++++++++------- 2 files changed, 108 insertions(+), 81 deletions(-) diff --git a/packages/uni-app-plus/dist/index.v3.js b/packages/uni-app-plus/dist/index.v3.js index 023496cbd..1721d575f 100644 --- a/packages/uni-app-plus/dist/index.v3.js +++ b/packages/uni-app-plus/dist/index.v3.js @@ -7543,46 +7543,59 @@ var serviceContext = (function () { firstIpv4: firstIpv4, tls }; + let withArrayBuffer; if (method !== 'GET') { - options.body = typeof data === 'string' ? data : JSON.stringify(data); + if (toString.call(data) === '[object ArrayBuffer]') { + withArrayBuffer = true; + } else { + options.body = typeof data === 'string' ? data : JSON.stringify(data); + } } - try { - stream.fetch(options, ({ - ok, - status, - data, - headers, - errorMsg - }) => { - if (aborted) { - return - } - if (abortTimeout) { - clearTimeout(abortTimeout); - } - const statusCode = status; - if (statusCode > 0) { - publishStateChange$1({ - requestTaskId, - state: 'success', - data: ok && responseType === 'arraybuffer' ? base64ToArrayBuffer$2(data) : data, - statusCode, - header: headers, - cookies: cookiesParse(headers) - }); - } else { - let errMsg = 'abort statusCode:' + statusCode; - if (errorMsg) { - errMsg = errMsg + ' ' + errorMsg; - } - publishStateChange$1({ - requestTaskId, - state: 'fail', - statusCode, - errMsg - }); + const callback = ({ + ok, + status, + data, + headers, + errorMsg + }) => { + if (aborted) { + return + } + if (abortTimeout) { + clearTimeout(abortTimeout); + } + const statusCode = status; + if (statusCode > 0) { + publishStateChange$1({ + requestTaskId, + state: 'success', + data: ok && responseType === 'arraybuffer' ? base64ToArrayBuffer$2(data) : data, + statusCode, + header: headers, + cookies: cookiesParse(headers) + }); + } else { + let errMsg = 'abort statusCode:' + statusCode; + if (errorMsg) { + errMsg = errMsg + ' ' + errorMsg; } - }); + publishStateChange$1({ + requestTaskId, + state: 'fail', + statusCode, + errMsg + }); + } + }; + try { + if (withArrayBuffer) { + stream.fetchWithArrayBuffer({ + '@type': 'binary', + base64: arrayBufferToBase64$2(data) + }, options, callback); + } else { + stream.fetch(options, callback); + } requestTasks[requestTaskId] = { abort () { aborted = true; @@ -15794,7 +15807,7 @@ var serviceContext = (function () { var zstream = ZStream; - var toString = Object.prototype.toString; + var toString$1 = Object.prototype.toString; /* Public constants ==========================================================*/ /* ===========================================================================*/ @@ -15958,7 +15971,7 @@ var serviceContext = (function () { if (typeof opt.dictionary === 'string') { // If we need to compress text, change encoding to utf8. dict = strings.string2buf(opt.dictionary); - } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') { + } else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') { dict = new Uint8Array(opt.dictionary); } else { dict = opt.dictionary; @@ -16016,7 +16029,7 @@ var serviceContext = (function () { if (typeof data === 'string') { // If we need to compress text, change encoding to utf8. strm.input = strings.string2buf(data); - } else if (toString.call(data) === '[object ArrayBuffer]') { + } else if (toString$1.call(data) === '[object ArrayBuffer]') { strm.input = new Uint8Array(data); } else { strm.input = data; @@ -18568,7 +18581,7 @@ var serviceContext = (function () { var gzheader = GZheader; - var toString$1 = Object.prototype.toString; + var toString$2 = Object.prototype.toString; /** * class Inflate @@ -18709,7 +18722,7 @@ var serviceContext = (function () { // Convert data if needed if (typeof opt.dictionary === 'string') { opt.dictionary = strings.string2buf(opt.dictionary); - } else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') { + } else if (toString$2.call(opt.dictionary) === '[object ArrayBuffer]') { opt.dictionary = new Uint8Array(opt.dictionary); } if (opt.raw) { //In raw mode we need to set the dictionary early @@ -18767,7 +18780,7 @@ var serviceContext = (function () { if (typeof data === 'string') { // Only binary strings can be decompressed on practice strm.input = strings.binstring2buf(data); - } else if (toString$1.call(data) === '[object ArrayBuffer]') { + } else if (toString$2.call(data) === '[object ArrayBuffer]') { strm.input = new Uint8Array(data); } else { strm.input = data; diff --git a/src/platforms/app-plus/service/api/network/request.js b/src/platforms/app-plus/service/api/network/request.js index 21df64b39..ba1a450b4 100644 --- a/src/platforms/app-plus/service/api/network/request.js +++ b/src/platforms/app-plus/service/api/network/request.js @@ -5,7 +5,8 @@ import { import { publish, requireNativePlugin, - base64ToArrayBuffer + base64ToArrayBuffer, + arrayBufferToBase64 } from '../../bridge' import { @@ -107,46 +108,59 @@ export function createRequestTaskById (requestTaskId, { firstIpv4: firstIpv4, tls } + let withArrayBuffer if (method !== 'GET') { - options.body = typeof data === 'string' ? data : JSON.stringify(data) + if (toString.call(data) === '[object ArrayBuffer]') { + withArrayBuffer = true + } else { + options.body = typeof data === 'string' ? data : JSON.stringify(data) + } } - try { - stream.fetch(options, ({ - ok, - status, - data, - headers, - errorMsg - }) => { - if (aborted) { - return - } - if (abortTimeout) { - clearTimeout(abortTimeout) - } - const statusCode = status - if (statusCode > 0) { - publishStateChange({ - requestTaskId, - state: 'success', - data: ok && responseType === 'arraybuffer' ? base64ToArrayBuffer(data) : data, - statusCode, - header: headers, - cookies: cookiesParse(headers) - }) - } else { - let errMsg = 'abort statusCode:' + statusCode - if (errorMsg) { - errMsg = errMsg + ' ' + errorMsg - } - publishStateChange({ - requestTaskId, - state: 'fail', - statusCode, - errMsg - }) + const callback = ({ + ok, + status, + data, + headers, + errorMsg + }) => { + if (aborted) { + return + } + if (abortTimeout) { + clearTimeout(abortTimeout) + } + const statusCode = status + if (statusCode > 0) { + publishStateChange({ + requestTaskId, + state: 'success', + data: ok && responseType === 'arraybuffer' ? base64ToArrayBuffer(data) : data, + statusCode, + header: headers, + cookies: cookiesParse(headers) + }) + } else { + let errMsg = 'abort statusCode:' + statusCode + if (errorMsg) { + errMsg = errMsg + ' ' + errorMsg } - }) + publishStateChange({ + requestTaskId, + state: 'fail', + statusCode, + errMsg + }) + } + } + try { + if (withArrayBuffer) { + stream.fetchWithArrayBuffer({ + '@type': 'binary', + base64: arrayBufferToBase64(data) + }, options, callback) + } else { + stream.fetch(options, callback) + } requestTasks[requestTaskId] = { abort () { aborted = true -- GitLab