user 6 yıl önce
ebeveyn
işleme
5a830b734c

+ 2 - 2
lib/index.js

@@ -13,14 +13,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
 Object.defineProperty(exports, "__esModule", { value: true });
 const crypto = require("libp2p-crypto");
 const util = require("util");
-const storage_1 = require("./storage");
+const storage_node_1 = require("./storage-node");
 const TextEncoder = util.TextEncoder;
 const ws_1 = __importDefault(require("ws"));
 const content_item_1 = require("./content-item");
 const util_1 = require("./util");
 const webclient_node_1 = __importDefault(require("./webclient-node"));
 class BankClient {
-    constructor(urlBase, ipfsUrlBase, storage = new storage_1.Storage('bankClient'), webClient = new webclient_node_1.default()) {
+    constructor(urlBase, ipfsUrlBase, storage = new storage_node_1.StorageNode('bankClient'), webClient = new webclient_node_1.default()) {
         this.urlBase = urlBase;
         this.ipfsUrlBase = ipfsUrlBase;
         this.storage = storage;

Dosya farkı çok büyük olduğundan ihmal edildi
+ 1 - 1
lib/index.js.map


+ 10 - 0
lib/storage-node.d.ts

@@ -0,0 +1,10 @@
+import { Storage } from './storage';
+export declare class StorageNode implements Storage {
+    private prefix;
+    constructor(id: string);
+    getValues(): Promise<unknown[]>;
+    get(key: string): Promise<any>;
+    put(key: string, value: any): Promise<void>;
+    set(key: string, value: any): Promise<void>;
+    private getMap;
+}

+ 68 - 0
lib/storage-node.js

@@ -0,0 +1,68 @@
+"use strict";
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
+    return new (P || (P = Promise))(function (resolve, reject) {
+        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
+        step((generator = generator.apply(thisArg, _arguments || [])).next());
+    });
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+const fs = require("fs");
+class StorageNode {
+    constructor(id) {
+        try {
+            fs.mkdirSync("data");
+        }
+        catch (e) {
+            // ignore
+        }
+        const safeId = id.replace(/[^A-Za-z0-9]/g, "");
+        if (safeId === '') {
+            throw new Error("must have non-empty id");
+        }
+        this.prefix = safeId;
+    }
+    getValues() {
+        return __awaiter(this, void 0, void 0, function* () {
+            return Object.values(this.getMap());
+        });
+    }
+    get(key) {
+        return __awaiter(this, void 0, void 0, function* () {
+            return this.getMap()[key];
+        });
+    }
+    put(key, value) {
+        return __awaiter(this, void 0, void 0, function* () {
+            const filename = "data/storage_" + this.prefix;
+            let map;
+            try {
+                const contents = fs.readFileSync(filename, { encoding: 'utf-8' });
+                map = JSON.parse(contents);
+            }
+            catch (e) {
+                map = {};
+            }
+            map[key] = value;
+            fs.writeFileSync(filename, JSON.stringify(map));
+        });
+    }
+    set(key, value) {
+        return __awaiter(this, void 0, void 0, function* () {
+            return yield this.put(key, value);
+        });
+    }
+    getMap() {
+        try {
+            const filename = "data/storage_" + this.prefix;
+            const contents = fs.readFileSync(filename, { encoding: 'utf-8' });
+            return JSON.parse(contents);
+        }
+        catch (e) {
+            return {};
+        }
+    }
+}
+exports.StorageNode = StorageNode;
+//# sourceMappingURL=storage-node.js.map

Dosya farkı çok büyük olduğundan ihmal edildi
+ 1 - 0
lib/storage-node.js.map


+ 1 - 5
lib/storage.d.ts

@@ -1,9 +1,5 @@
-export declare class Storage {
-    private prefix;
-    constructor(id: string);
-    getValues(): Promise<unknown[]>;
+export interface Storage {
     get(key: string): Promise<any>;
     put(key: string, value: any): Promise<void>;
     set(key: string, value: any): Promise<void>;
-    private getMap;
 }

+ 0 - 65
lib/storage.js

@@ -1,68 +1,3 @@
 "use strict";
-var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
-    return new (P || (P = Promise))(function (resolve, reject) {
-        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
-        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
-        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
-        step((generator = generator.apply(thisArg, _arguments || [])).next());
-    });
-};
 Object.defineProperty(exports, "__esModule", { value: true });
-const fs = require("fs");
-class Storage {
-    constructor(id) {
-        try {
-            fs.mkdirSync("data");
-        }
-        catch (e) {
-            // ignore
-        }
-        const safeId = id.replace(/[^A-Za-z0-9]/g, "");
-        if (safeId === '') {
-            throw new Error("must have non-empty id");
-        }
-        this.prefix = safeId;
-    }
-    getValues() {
-        return __awaiter(this, void 0, void 0, function* () {
-            return Object.values(this.getMap());
-        });
-    }
-    get(key) {
-        return __awaiter(this, void 0, void 0, function* () {
-            return this.getMap()[key];
-        });
-    }
-    put(key, value) {
-        return __awaiter(this, void 0, void 0, function* () {
-            const filename = "data/storage_" + this.prefix;
-            let map;
-            try {
-                const contents = fs.readFileSync(filename, { encoding: 'utf-8' });
-                map = JSON.parse(contents);
-            }
-            catch (e) {
-                map = {};
-            }
-            map[key] = value;
-            fs.writeFileSync(filename, JSON.stringify(map));
-        });
-    }
-    set(key, value) {
-        return __awaiter(this, void 0, void 0, function* () {
-            return yield this.put(key, value);
-        });
-    }
-    getMap() {
-        try {
-            const filename = "data/storage_" + this.prefix;
-            const contents = fs.readFileSync(filename, { encoding: 'utf-8' });
-            return JSON.parse(contents);
-        }
-        catch (e) {
-            return {};
-        }
-    }
-}
-exports.Storage = Storage;
 //# sourceMappingURL=storage.js.map

Dosya farkı çok büyük olduğundan ihmal edildi
+ 1 - 1
lib/storage.js.map


+ 6 - 0
lib/webclient-fetch.d.ts

@@ -0,0 +1,6 @@
+import { IWebClient } from "./webclient";
+import { IWebClientOptions } from "./webclient-options";
+export default class WebClientFetch implements IWebClient {
+    request(options: IWebClientOptions): Promise<string>;
+    requestJSON(options: IWebClientOptions): Promise<object>;
+}

+ 55 - 0
lib/webclient-fetch.js

@@ -0,0 +1,55 @@
+"use strict";
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
+    return new (P || (P = Promise))(function (resolve, reject) {
+        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
+        step((generator = generator.apply(thisArg, _arguments || [])).next());
+    });
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+class WebClientFetch {
+    request(options) {
+        return __awaiter(this, void 0, void 0, function* () {
+            const fetchOptions = {
+                headers: options.headers,
+                method: options.method
+            };
+            if (options.body) {
+                fetchOptions.body = options.body;
+            }
+            const res = yield fetch(options.url, fetchOptions);
+            console.log('res', res);
+            return res.text();
+        });
+    }
+    requestJSON(options) {
+        return __awaiter(this, void 0, void 0, function* () {
+            if (!options.headers) {
+                options.headers = {};
+            }
+            if (options.body) {
+                options.headers['content-type'] = 'application/json';
+                if (typeof options.body === 'object') {
+                    options.body = JSON.stringify(options.body);
+                }
+            }
+            options.headers.accept = 'application/json';
+            const fetchOptions = {
+                headers: options.headers,
+                method: options.method
+            };
+            if (options.formData) {
+                fetchOptions.formData = options.formData;
+            }
+            if (options.body) {
+                fetchOptions.body = options.body;
+            }
+            const res = yield fetch(options.url, fetchOptions);
+            console.log('res', res);
+            return res.json();
+        });
+    }
+}
+exports.default = WebClientFetch;
+//# sourceMappingURL=webclient-fetch.js.map

Dosya farkı çok büyük olduğundan ihmal edildi
+ 1 - 0
lib/webclient-fetch.js.map


+ 2 - 1
src/index.ts

@@ -2,6 +2,7 @@ import crypto = require('libp2p-crypto');
 import util = require('util');
 import RsaPrivateKey = crypto.keys;
 import { Storage } from './storage';
+import { StorageNode } from './storage-node';
 import { IWebClient } from './webclient';
 const TextEncoder = util.TextEncoder;
 import WebSocket from 'ws';
@@ -37,7 +38,7 @@ export class BankClient {
     private bootstrapPromise: any;
     private bootstrapResult: any;
     
-    constructor(private urlBase: string, private ipfsUrlBase: string, private storage: Storage = new Storage('bankClient'), private webClient: IWebClient = new WebClientNode()) {
+    constructor(private urlBase: string, private ipfsUrlBase: string, private storage: Storage = new StorageNode('bankClient'), private webClient: IWebClient = new WebClientNode()) {
       this.wsUrlBase = urlBase.replace(/^http/i, 'ws');
     }
 

+ 56 - 0
src/storage-node.ts

@@ -0,0 +1,56 @@
+import fs = require('fs');
+
+import { Storage } from './storage';
+
+export class StorageNode implements Storage {
+
+    private prefix: string;
+
+    constructor(id:string) {
+        try {
+            fs.mkdirSync("data");
+        } catch (e) {
+            // ignore
+        }
+        const safeId = id.replace(/[^A-Za-z0-9]/g, "");
+        if (safeId === '') {
+            throw new Error("must have non-empty id");
+        }
+        this.prefix = safeId;
+    }
+
+    public async getValues() {
+        return Object.values(this.getMap());
+    }
+
+    public async get(key:string) {
+        return this.getMap()[key];
+    }
+
+    public async put(key:string, value:any) {
+        const filename = "data/storage_" + this.prefix;
+        let map;
+        try {
+            const contents = fs.readFileSync(filename, {encoding: 'utf-8'});
+            map = JSON.parse(contents);
+        } catch(e) {
+            map = {};
+        }
+        map[key] = value;
+        fs.writeFileSync(filename, JSON.stringify(map));
+    }
+
+    public async set(key:string, value:any) {
+        return await this.put(key, value);
+    }
+
+    private getMap() {
+        try {
+            const filename = "data/storage_" + this.prefix;
+            const contents = fs.readFileSync(filename, {encoding: 'utf-8'});
+            return JSON.parse(contents);
+        } catch (e) {
+            return {};
+        }
+    }
+}

+ 5 - 48
src/storage.ts

@@ -1,54 +1,11 @@
 import fs = require('fs');
 
-export class Storage {
+export interface Storage {
 
-    private prefix: string;
+    get(key:string): Promise<any>;
 
-    constructor(id:string) {
-        try {
-            fs.mkdirSync("data");
-        } catch (e) {
-            // ignore
-        }
-        const safeId = id.replace(/[^A-Za-z0-9]/g, "");
-        if (safeId === '') {
-            throw new Error("must have non-empty id");
-        }
-        this.prefix = safeId;
-    }
+    put(key:string, value:any): Promise<void>;
 
-    public async getValues() {
-        return Object.values(this.getMap());
-    }
+    set(key:string, value:any): Promise<void>;
 
-    public async get(key:string) {
-        return this.getMap()[key];
-    }
-
-    public async put(key:string, value:any) {
-        const filename = "data/storage_" + this.prefix;
-        let map;
-        try {
-            const contents = fs.readFileSync(filename, {encoding: 'utf-8'});
-            map = JSON.parse(contents);
-        } catch(e) {
-            map = {};
-        }
-        map[key] = value;
-        fs.writeFileSync(filename, JSON.stringify(map));
-    }
-
-    public async set(key:string, value:any) {
-        return await this.put(key, value);
-    }
-
-    private getMap() {
-        try {
-            const filename = "data/storage_" + this.prefix;
-            const contents = fs.readFileSync(filename, {encoding: 'utf-8'});
-            return JSON.parse(contents);
-        } catch (e) {
-            return {};
-        }
-    }
-}
+}

+ 45 - 0
src/webclient-fetch.ts

@@ -0,0 +1,45 @@
+import { IWebClient } from "./webclient";
+import { IWebClientOptions } from "./webclient-options";
+
+export default class WebClientFetch implements IWebClient {
+
+    public async request(options: IWebClientOptions): Promise<string> {
+        const fetchOptions: any = {
+            headers: options.headers,
+            method: options.method
+        };
+        if (options.body) {
+            fetchOptions.body = options.body;
+        }
+
+        const res = await fetch(options.url, fetchOptions);
+        console.log('res', res);
+        return res.text();
+    }    
+    
+    public async requestJSON(options: IWebClientOptions): Promise<object> {
+        if (!options.headers) {
+            options.headers = {};
+        }
+        if (options.body) {
+            options.headers['content-type'] = 'application/json';
+            if (typeof options.body === 'object') {
+                options.body = JSON.stringify(options.body);
+            }
+        }
+        options.headers.accept = 'application/json';
+        const fetchOptions: any = {
+            headers: options.headers,
+            method: options.method
+        };
+        if (options.formData) {
+            fetchOptions.formData = options.formData;
+        }
+        if (options.body) {
+            fetchOptions.body = options.body;
+        }
+        const res = await fetch(options.url, fetchOptions);
+        console.log('res', res);
+        return res.json();
+    }
+}

+ 0 - 2
src/webclient-options.ts

@@ -1,5 +1,3 @@
-import { request } from "https";
-
 export interface IWebClientOptions {
     method: string;
     url: string;