Skip to content

Commit

Permalink
support for wx mini program
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterXuan committed Nov 29, 2018
1 parent 04011e5 commit 92003dd
Show file tree
Hide file tree
Showing 5 changed files with 2,855 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ function getGlobalNamespace(): {ENV: Environment} {
} else if (typeof (process) !== 'undefined') {
ns = process;
} else {
throw new Error('Could not find a global object');
ns = global;
}
return ns;
}
Expand Down
1 change: 1 addition & 0 deletions src/io/browser_http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {concatenateArrayBuffers, getModelArtifactsInfoForJSON} from './io_utils'
import {IORouter, IORouterRegistry} from './router_registry';
import {IOHandler, ModelArtifacts, SaveResult, WeightsManifestConfig, WeightsManifestEntry} from './types';
import {loadWeightsAsArrayBuffer} from './weights_loader';
import {fetch} from '../mp_util';

export class BrowserHTTPRequest implements IOHandler {
protected readonly path: string|string[];
Expand Down
1 change: 1 addition & 0 deletions src/io/weights_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import {NamedTensorMap} from '../tensor_types';
import * as util from '../util';
import {fetch} from '../mp_util';

import {decodeWeights} from './io_utils';
import {DTYPE_VALUE_SIZE_MAP, WeightsManifestConfig, WeightsManifestEntry} from './types';
Expand Down
63 changes: 63 additions & 0 deletions src/mp_util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/// <reference path="./wx.d.ts">
function generateResponse(res:any) {
let header = res.header || {};
return {
ok: ((res.statusCode / 200) | 0) === 1, // 200-299
status: res.statusCode,
statusText: res.errMsg,
url: '',
clone: () => {
return generateResponse(res);
},
text: () => Promise.resolve(String.fromCharCode.apply(null, new Uint8Array(res.data))),
json: () => {
var json = {};
try {
json = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(res.data)));
}
catch (err) {
console.error(err);
}
return Promise.resolve(json);
},
arrayBuffer: () => Promise.resolve(res.data),
blob: () => Promise.resolve(res.data),
headers: {
keys: () => Object.keys(header),
entries: () => {
let all = [];
for (let key in header) {
if (header.hasOwnProperty(key)) {
all.push([key, header[key]]);
}
}
return all;
},
get: (n:string) => header[n.toLowerCase()],
has: (n:string) => n.toLowerCase() in header
}
};
}

async function fetch(input:any, init:any): Promise<any> {
return new Promise(function(resolve, reject) {
wx.request({
url: input,
header: init.headers || {},
data: init.body || {},
method: init.method || 'GET',
dataType: '',
responseType: 'arraybuffer',
success: function(res:any) {
resolve(generateResponse(res))
},
fail: function(res:any) {
reject(generateResponse(res))
}
})
})
}

export {
fetch,
};
Loading

0 comments on commit 92003dd

Please sign in to comment.