270 lines
6.3 KiB
JavaScript
Executable File
270 lines
6.3 KiB
JavaScript
Executable File
var bricks = window.bricks || {};
|
|
function url_params(data) {
|
|
return Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&');
|
|
}
|
|
/*
|
|
async function doit() {
|
|
const response = await fetch("http://localhost:3000")
|
|
const reader = response.body.getReader();
|
|
const decoder = new TextDecoder('utf-8');
|
|
let result = await reader.read();
|
|
var buffer = '';
|
|
while (!result.done) {
|
|
const text = decoder.decode(result.value);
|
|
buffer += text;
|
|
const newline = /\r?\n/gm;
|
|
var rez = newline.exec(buffer);
|
|
while (rez){
|
|
yield buffer.substring(0, rez.index);
|
|
buffer = buffer.substring(newline.lastIndex);
|
|
rez = newline.exec(buffer);
|
|
}
|
|
result = await reader.read()
|
|
}
|
|
if (buffer != ''){
|
|
yield bufffer;
|
|
}
|
|
}
|
|
// chunked response handle
|
|
*/
|
|
bricks.HttpText = class {
|
|
constructor(headers){
|
|
/*
|
|
var _headers = {
|
|
"Accept":"text/html",
|
|
}
|
|
_headers = {
|
|
"Accept": "application/json",
|
|
};
|
|
*/
|
|
if (!headers)
|
|
headers = {};
|
|
this.headers = headers || {
|
|
"Accept":"text/html",
|
|
};
|
|
bricks.extend(this.headers, headers);
|
|
var width=0, height=0;
|
|
var is_mobile = 0
|
|
if (bricks.is_mobile()){
|
|
is_mobile = 1;
|
|
}
|
|
if (bricks.app) {
|
|
width = bricks.app.screenWidth();
|
|
height = bricks.app.screenHeight();
|
|
}
|
|
|
|
this.params = {
|
|
"_webbricks_":1,
|
|
"width":width,
|
|
"height":height,
|
|
"_is_mobile":is_mobile
|
|
}
|
|
}
|
|
url_parse(url){
|
|
var a = url.split('?');
|
|
if (a.length == 1) return url;
|
|
url = a[0];
|
|
var a = a[1].split('&');
|
|
for (var i=0;i<a.length;i++){
|
|
var b;
|
|
b = a[i].split('=');
|
|
this.params[b[0]] = b[1];
|
|
}
|
|
return url;
|
|
}
|
|
|
|
async get_result_data(resp){
|
|
return await resp.text();
|
|
}
|
|
add_own_params(params){
|
|
if (! params)
|
|
params = {};
|
|
var session = bricks.app.get_session();
|
|
if (params instanceof FormData){
|
|
for ( const [key, value] of Object.entries(this.params)){
|
|
params.append(key, value);
|
|
}
|
|
if(session){
|
|
params.append('session', session);
|
|
}
|
|
return params;
|
|
}
|
|
var p = bricks.extend({}, this.params);
|
|
p = bricks.extend(p, params);
|
|
if (session){
|
|
bricks.extend(p,{session:session});
|
|
}
|
|
return p;
|
|
}
|
|
add_own_headers(headers){
|
|
if (! headers){
|
|
headers = {};
|
|
}
|
|
return Object.assign(this.headers, headers);
|
|
}
|
|
async httpcall(url, {method='GET', headers=null, params=null}={}){
|
|
url = this.url_parse(url);
|
|
var data = this.add_own_params(params);
|
|
var header = this.add_own_headers(headers);
|
|
var _params = {
|
|
method:method
|
|
}
|
|
// _params.headers = headers;
|
|
if (method == 'GET' || method == 'HEAD') {
|
|
let pstr = url_params(data);
|
|
url = url + '?' + pstr;
|
|
} else {
|
|
if (data instanceof FormData){
|
|
_params.body = data;
|
|
} else {
|
|
_params.body = JSON.stringify(data);
|
|
}
|
|
}
|
|
const fetchResult = await fetch(url, _params);
|
|
var result=null;
|
|
result = await this.get_result_data(fetchResult);
|
|
if (fetchResult.ok){
|
|
var ck = objget(fetchResult.headers, 'Set-Cookie');
|
|
if (ck){
|
|
var session = ck.split(';')[0];
|
|
bricks.app.save_session(session);
|
|
}
|
|
return result;
|
|
}
|
|
if (fetchResult.status == 401 && bricks.app.login_url){
|
|
return await this.withLoginInfo(url, _params);
|
|
}
|
|
const resp_error = {
|
|
"type":"Error",
|
|
"message":result.message || 'Something went wrong',
|
|
"data":result.data || '',
|
|
"code":result.code || ''
|
|
};
|
|
error.info = resp_error;
|
|
return error;
|
|
}
|
|
async withLoginInfo(url, params){
|
|
var get_login_info = function(e){
|
|
bricks.debug('login info:', e.target.getValue());
|
|
return e.target.getValue();
|
|
}
|
|
var w = await bricks.widgetBuild({
|
|
"id":"login_form",
|
|
"widgettype":"urlwidget",
|
|
"options":{
|
|
"url":bricks.app.login_url
|
|
}
|
|
});
|
|
var login_info = await new Promise((resolve, reject, w) => {
|
|
w.bind('submit', (event) => {
|
|
resolve(event.target.getValue());
|
|
event.target.dismiss();
|
|
});
|
|
w.bind('discard', (event) => {
|
|
resolve(null);
|
|
event.target.dismiss()
|
|
});
|
|
});
|
|
if (login_info){
|
|
this.set_authorization_header(params, lgin_info);
|
|
const fetchResult = await fetch(url, params);
|
|
var result=null;
|
|
result = await this.get_result_data(fetchResult);
|
|
if (fetchResult.ok){
|
|
return result;
|
|
}
|
|
if (fetchResult.status == 401){
|
|
return await this.withLoginInfo(url, params);
|
|
}
|
|
}
|
|
const resp_error = {
|
|
"type":"Error",
|
|
"message":result.message || 'Something went wrong',
|
|
"data":result.data || '',
|
|
"code":result.code || ''
|
|
};
|
|
const error = new Error();
|
|
error.info = resp_error;
|
|
return error;
|
|
}
|
|
set_authorization_header(params, lgin_info){
|
|
var auth = 'password' + '::' + login_info.user + '::' + login_info.password;
|
|
var rsa = bricks.app.rsa;
|
|
var code = rsa.encrypt(auth);
|
|
self.header.authorization = btoa(code)
|
|
}
|
|
async get(url, {headers=null, params=null}={}){
|
|
return await this.httpcall(url, {
|
|
method:'GET',
|
|
headers:headers,
|
|
params:params
|
|
});
|
|
}
|
|
async post(url, {headers=null, params=null}={}){
|
|
return await this.httpcall(url, {
|
|
method:'POST',
|
|
headers:headers,
|
|
params:params
|
|
});
|
|
}
|
|
}
|
|
|
|
bricks.HttpResponse = class extends bricks.HttpText {
|
|
async get_result_data(resp){
|
|
return resp;
|
|
}
|
|
}
|
|
|
|
bricks.HttpResponseStream = class extends bricks.HttpResponse {
|
|
async handle_chunk(resp, handler){
|
|
const reader = resp.body.getReader();
|
|
const decoder = new TextDecoder('utf-8');
|
|
let result = await reader.read();
|
|
var buff_ = '';
|
|
while (!result.done) {
|
|
const text = decoder.decode(result.value);
|
|
buff_ += text;
|
|
const lines = buff_.split('\n');
|
|
for (var i=0;i<lines.length - 1; i++){
|
|
// console.log('line=', lines[i]);
|
|
handler(lines[i]);
|
|
}
|
|
buff_ = lines[lines.length - 1];
|
|
result = await reader.read()
|
|
}
|
|
if (buff_ != ''){
|
|
handler(buff_);
|
|
}
|
|
}
|
|
}
|
|
|
|
bricks.HttpRaw = class extends bricks.HttpText {
|
|
async get_result_data(resp){
|
|
return await resp.blob();
|
|
}
|
|
}
|
|
|
|
bricks.HttpJson = class extends bricks.HttpText {
|
|
constructor(headers){
|
|
if (!headers)
|
|
headers = {};
|
|
super(headers);
|
|
this.headers = {
|
|
"Accept": "application/json",
|
|
}
|
|
bricks.extend(this.headers, headers);
|
|
}
|
|
async get_result_data(resp) {
|
|
return await resp.json()
|
|
}
|
|
}
|
|
|
|
bricks.hc = new bricks.HttpText();
|
|
bricks.tget = bricks.hc.get.bind(bricks.hc);
|
|
bricks.tpost = bricks.hc.post.bind(bricks.hc);
|
|
bricks.jc = new bricks.HttpJson();
|
|
bricks.jcall = bricks.jc.httpcall.bind(bricks.jc);
|
|
bricks.jget = bricks.jc.get.bind(bricks.jc);
|
|
bricks.jpost = bricks.jc.post.bind(bricks.jc);
|
|
|