UNPKG

4.11 kBPlain TextView Raw
1import {Connection, Session} from 'autobahn';
2import {Correlation, FileSystem, fileSystemEventNames} from './api';
3import {InternalEventsEmitter, makeEventsEmitter} from "./utils";
4import {timeoutPromise} from './promise-utils';
5import {Directory, File, ShallowDirectory} from "./model";
6
7export const noConnectionError = `WampClientFileSystem hasn't opened connection yet (forgot to init()?).`
8
9export class WampClientFileSystem implements FileSystem {
10 public readonly events: InternalEventsEmitter = makeEventsEmitter();
11 private connection: Connection;
12 private session: Session;
13 private realmPrefix: string;
14
15 constructor(public baseUrl: string, private realm: string, private initTimeout: number = 5000) {
16 this.realm = realm;
17 this.connection = new Connection({url: baseUrl, realm});
18 }
19
20 init(): Promise<WampClientFileSystem> {
21 const {baseUrl, initTimeout, connection} = this
22 return timeoutPromise(new Promise<WampClientFileSystem>(resolve => {
23 connection.open();
24 connection.onopen = (session: Session) => {
25 this.session = session;
26 this.realmPrefix = this.realm.replace(/(.*\..*)(\..*)$/, '$1.'); // 'xxx.yyy.zzz' => 'xxx.yyy.'
27 fileSystemEventNames.forEach(fsEvent => {
28 this.session.subscribe(
29 this.realmPrefix + fsEvent,
30 res => this.events.emit(fsEvent, res && res[0])
31 )
32 });
33 resolve(this)
34 };
35 }), initTimeout, `Cant't open connection to the WAMP server at ${baseUrl} for ${initTimeout}ms.`);
36 }
37
38 async saveFile(fullPath: string, newContent: string): Promise<Correlation> {
39 this.throwIfDisconnected();
40
41 try {
42 return await this.session.call<Correlation>(`${this.realmPrefix}saveFile`, [fullPath, newContent]);
43 } catch (error) {
44 throw new Error(error);
45 }
46 }
47
48 async deleteFile(fullPath: string): Promise<Correlation> {
49 this.throwIfDisconnected();
50
51 try {
52 return await this.session.call<Correlation>(`${this.realmPrefix}deleteFile`, [fullPath]);
53 } catch (error) {
54 throw new Error(error);
55 }
56 }
57
58 async deleteDirectory(fullPath: string, recursive?: boolean): Promise<Correlation> {
59 this.throwIfDisconnected();
60
61 try {
62 return await this.session.call<Correlation>(`${this.realmPrefix}deleteDirectory`, [fullPath, recursive]);
63 } catch (error) {
64 throw new Error(error);
65 }
66 }
67
68 async ensureDirectory(fullPath: string): Promise<Correlation> {
69 this.throwIfDisconnected();
70
71 try {
72 return await this.session.call<Correlation>(`${this.realmPrefix}ensureDirectory`, [fullPath]);
73 } catch (error) {
74 throw new Error(error);
75 }
76 }
77
78 async loadTextFile(fullPath: string): Promise<string> {
79 this.throwIfDisconnected();
80
81 try {
82 return await this.session.call<string>(`${this.realmPrefix}loadTextFile`, [fullPath]);
83 } catch (error) {
84 throw new Error(error);
85 }
86 }
87
88 async loadDirectoryTree(fullPath: string = ''): Promise<Directory> {
89 this.throwIfDisconnected();
90
91 try {
92 return await this.session.call<Directory>(`${this.realmPrefix}loadDirectoryTree`, [fullPath]);
93 } catch (error) {
94 throw new Error(error);
95 }
96 }
97
98 async loadDirectoryChildren(fullPath: string): Promise<(File | ShallowDirectory)[]> {
99 this.throwIfDisconnected();
100
101 try {
102 return await this.session.call<(File | ShallowDirectory)[]>(`${this.realmPrefix}loadDirectoryChildren`, [fullPath]);
103 } catch (error) {
104 throw new Error(error);
105 }
106 }
107
108 dispose() {
109 this.connection && this.connection.close();
110 }
111
112 private throwIfDisconnected() {
113 if (!this.session || !this.session.isOpen) {
114 throw new Error(noConnectionError);
115 }
116 }
117}
118