提交 5de1ba98 编写于 作者: Q qiang

feat(App): request with ArrayBuffer

上级 496de782
......@@ -6397,7 +6397,7 @@ var serviceContext = (function (vue) {
var ZStream = zstream;
var toString$1 = Object.prototype.toString;
var toString$2 = Object.prototype.toString;
/* Public constants ==========================================================*/
/* ===========================================================================*/
......@@ -6561,7 +6561,7 @@ var serviceContext = (function (vue) {
if (typeof opt.dictionary === 'string') {
// If we need to compress text, change encoding to utf8.
dict = strings.string2buf(opt.dictionary);
} else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') {
} else if (toString$2.call(opt.dictionary) === '[object ArrayBuffer]') {
dict = new Uint8Array(opt.dictionary);
} else {
dict = opt.dictionary;
......@@ -6619,7 +6619,7 @@ var serviceContext = (function (vue) {
if (typeof data === 'string') {
// If we need to compress text, change encoding to utf8.
strm.input = strings.string2buf(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;
......@@ -9181,7 +9181,7 @@ var serviceContext = (function (vue) {
var GZheader = gzheader;
var toString = Object.prototype.toString;
var toString$1 = Object.prototype.toString;
/**
* class Inflate
......@@ -9322,7 +9322,7 @@ var serviceContext = (function (vue) {
// Convert data if needed
if (typeof opt.dictionary === 'string') {
opt.dictionary = strings.string2buf(opt.dictionary);
} else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
} else if (toString$1.call(opt.dictionary) === '[object ArrayBuffer]') {
opt.dictionary = new Uint8Array(opt.dictionary);
}
if (opt.raw) { //In raw mode we need to set the dictionary early
......@@ -9380,7 +9380,7 @@ var serviceContext = (function (vue) {
if (typeof data === 'string') {
// Only binary strings can be decompressed on practice
strm.input = strings.binstring2buf(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;
......@@ -14920,20 +14920,24 @@ var serviceContext = (function (vue) {
const options = {
method,
url: url.trim(),
// weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应
headers,
type: responseType === 'arraybuffer' ? 'base64' : 'text',
// weex 官方文档未说明实际支持 timeout,单位:ms
timeout: timeout || 6e5,
// 配置和weex模块内相反
sslVerify: !sslVerify,
firstIpv4: firstIpv4,
tls,
};
let withArrayBuffer = false;
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);
}
}
stream.fetch(options, ({ ok, status, data, headers, errorMsg, }) => {
const callback = ({ ok, status, data, headers, errorMsg, }) => {
if (aborted) {
return;
}
......@@ -14958,7 +14962,16 @@ var serviceContext = (function (vue) {
}
reject(errMsg);
}
});
};
if (withArrayBuffer) {
stream.fetchWithArrayBuffer({
'@type': 'binary',
base64: arrayBufferToBase64(data)
}, options, callback);
}
else {
stream.fetch(options, callback);
}
return new RequestTask({
abort() {
aborted = true;
......
......@@ -11,17 +11,10 @@ import {
ConfigMTLSOptions,
ConfigMTLSProtocol,
} from '@dcloudio/uni-api'
import { base64ToArrayBuffer } from '@dcloudio/uni-api'
import { base64ToArrayBuffer, arrayBufferToBase64 } from '@dcloudio/uni-api'
import { requireNativePlugin } from '../plugin/requireNativePlugin'
import { Stream, FetchOptions, FetchCallback, FetchHeaders } from './stream'
type Type = 'base64' | 'text'
type Headers = Record<string, string>
type Options = UniApp.RequestOptions & {
tls: any
headers: Headers
type: Type
body?: string | Data
}
interface RequestTasks {
abort: Function
}
......@@ -139,8 +132,8 @@ export const request = defineTaskApi<API_TYPE_REQUEST>(
data = JSON.stringify(data)
}
const stream = requireNativePlugin('stream')
const headers: Headers = {}
const stream: Stream = requireNativePlugin('stream')
const headers: FetchHeaders = {}
let abortTimeout: ReturnType<typeof setTimeout>
let aborted: boolean
let hasContentType = false
......@@ -183,70 +176,74 @@ export const request = defineTaskApi<API_TYPE_REQUEST>(
}, timeout + 200) // TODO +200 发消息到原生层有时间开销,以后考虑由原生层回调超时
}
const options: Options = {
const options: FetchOptions = {
method,
url: url.trim(),
// weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应
headers,
type: responseType === 'arraybuffer' ? 'base64' : 'text',
// weex 官方文档未说明实际支持 timeout,单位:ms
timeout: timeout || 6e5,
// 配置和weex模块内相反
sslVerify: !sslVerify,
firstIpv4: firstIpv4,
tls,
}
let withArrayBuffer: boolean = false
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)
}
}
stream.fetch(
options,
({
ok,
status,
data,
headers,
errorMsg,
}: {
ok: boolean
status: number
data: string
headers: Headers
errorMsg: string
}) => {
if (aborted) {
return
}
if (abortTimeout) {
clearTimeout(abortTimeout)
}
const statusCode = status
if (statusCode > 0) {
resolve(
formatResponse(
{
data:
ok && responseType === 'arraybuffer'
? base64ToArrayBuffer(data)
: data,
statusCode,
header: headers,
cookies: cookiesParse(headers),
},
args
)
const callback: FetchCallback = ({
ok,
status,
data,
headers,
errorMsg,
}) => {
if (aborted) {
return
}
if (abortTimeout) {
clearTimeout(abortTimeout)
}
const statusCode = status
if (statusCode > 0) {
resolve(
formatResponse(
{
data:
ok && responseType === 'arraybuffer'
? base64ToArrayBuffer(data)
: data,
statusCode,
header: headers,
cookies: cookiesParse(headers),
},
args
)
} else {
let errMsg = 'abort statusCode:' + statusCode
if (errorMsg) {
errMsg = errMsg + ' ' + errorMsg
}
reject(errMsg)
)
} else {
let errMsg = 'abort statusCode:' + statusCode
if (errorMsg) {
errMsg = errMsg + ' ' + errorMsg
}
reject(errMsg)
}
)
}
if (withArrayBuffer) {
stream.fetchWithArrayBuffer(
{
'@type': 'binary',
base64: arrayBufferToBase64(data as ArrayBuffer),
},
options,
callback
)
} else {
stream.fetch(options, callback)
}
return new RequestTask({
abort() {
......
export type FetchHeaders = Record<string, string>
export interface FetchOptions {
method?: string
url: string
// weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应
headers?: FetchHeaders
type?: 'base64' | 'text'
// weex 官方文档未说明实际支持 timeout,单位:ms
timeout?: number
sslVerify?: boolean
firstIpv4?: boolean
tls?: any
body?: string | Data
}
export interface FetchResult {
ok: boolean
status: number
data: string
headers: FetchHeaders
errorMsg: string
}
export type FetchCallback = (result: FetchResult) => void
export interface FetchArrayBuffer {
'@type': 'binary'
base64: string
}
export interface Stream {
fetch: (options: FetchOptions, callback: FetchCallback) => void
fetchWithArrayBuffer: (
body: FetchArrayBuffer,
options: FetchOptions,
callback: FetchCallback
) => void
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册