Browse Source

allow to post body in ajaxGet

Alexander Rose 6 years ago
parent
commit
304b6edd9b
2 changed files with 9 additions and 6 deletions
  1. 2 2
      src/mol-plugin/context.ts
  2. 7 4
      src/mol-util/data-source.ts

+ 2 - 2
src/mol-plugin/context.ts

@@ -103,8 +103,8 @@ export class PluginContext {
      * This should be used in all transform related request so that it could be "spoofed" to allow
      * "static" access to resources.
      */
-    fetch(url: string, type: 'string' | 'binary' = 'string'): Task<string | Uint8Array> {
-        return ajaxGet({ url, type });
+    fetch(url: string, type: 'string' | 'binary' = 'string', body?: string): Task<string | Uint8Array> {
+        return ajaxGet({ url, type, body });
         // const req = await fetch(url, { referrerPolicy: 'origin-when-cross-origin' });
         // return type === 'string' ? await req.text() : new Uint8Array(await req.arrayBuffer());
     }

+ 7 - 4
src/mol-util/data-source.ts

@@ -19,6 +19,7 @@ export interface AjaxGetParams {
     type: 'string' | 'binary',
     title?: string,
     compression?: DataCompressionMethod
+    body?: string
 }
 
 export function readStringFromFile(file: File) {
@@ -42,9 +43,11 @@ export function ajaxGetUint8Array(url: string, title?: string) {
 }
 
 export function ajaxGet(params: AjaxGetParams) {
-    return <Task<string | Uint8Array>>ajaxGetInternal(params.title, params.url, params.type === 'binary', params.compression === DataCompressionMethod.Gzip);
+    return <Task<string | Uint8Array>>ajaxGetInternal(params.title, params.url, params.type === 'binary', params.compression === DataCompressionMethod.Gzip, params.body);
 }
 
+export type AjaxTask = (url: string, type: 'string' | 'binary') => Task<string | Uint8Array>
+
 function decompress(buffer: Uint8Array): Uint8Array {
     // TODO
     throw 'nyi';
@@ -160,7 +163,7 @@ async function processAjax(ctx: RuntimeContext, asUint8Array: boolean, decompres
     }
 }
 
-function ajaxGetInternal(title: string | undefined, url: string, asUint8Array: boolean, decompressGzip: boolean): Task<string | Uint8Array> {
+function ajaxGetInternal(title: string | undefined, url: string, asUint8Array: boolean, decompressGzip: boolean, body?: string): Task<string | Uint8Array> {
     let xhttp: XMLHttpRequest | undefined = void 0;
     return Task.create(title ? title : 'Download', async ctx => {
         try {
@@ -170,9 +173,9 @@ function ajaxGetInternal(title: string | undefined, url: string, asUint8Array: b
 
             xhttp = RequestPool.get();
 
-            xhttp.open('get', url, true);
+            xhttp.open(body ? 'post' : 'get', url, true);
             xhttp.responseType = asUint8Array ? 'arraybuffer' : 'text';
-            xhttp.send();
+            xhttp.send(body);
 
             ctx.update({ message: 'Waiting for server...', canAbort: true });
             const e = await readData(ctx, 'Downloading...', xhttp, asUint8Array);