-
Notifications
You must be signed in to change notification settings - Fork 79
/
file-client.ts
91 lines (79 loc) · 2.86 KB
/
file-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/********************************************************************************
* Copyright (c) 2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
********************************************************************************/
/**
* File protocol binding
*/
import { Form } from "@node-wot/td-tools";
import { ProtocolClient, Content } from "@node-wot/core"
import fs = require('fs');
import path = require('path');
export default class FileClient implements ProtocolClient {
constructor() { }
public toString() {
return "[FileClient]";
}
public readResource(form: Form): Promise<Content> {
return new Promise<Content>((resolve, reject) => {
let filepath = form.href.split('//');
let resource = fs.readFileSync(filepath[1], 'utf8');
let extension = path.extname(filepath[1]);
console.debug(`FileClient found '${extension}' extension`);
let mediaType = "application/octet-stream";
switch (extension) {
case ".txt":
case ".log":
case ".ini":
case ".cfg":
mediaType = "text/plain";
break;
case ".json":
mediaType = "application/json";
break;
case ".jsonld":
mediaType = "application/ld+json";
break;
default:
console.warn(`FileClient cannot determine media type of '${form.href}'`);
}
resolve({ mediaType: mediaType, body: new Buffer(resource) });
});
}
public writeResource(form: Form, content: Content): Promise<any> {
return new Promise<Object>((resolve, reject) => {
reject(new Error(`FileClient does not implement write`));
});
}
public invokeResource(form: Form, payload: Object): Promise<any> {
return new Promise<Object>((resolve, reject) => {
reject(new Error(`FileClient does not implement invoke`));
});
}
public unlinkResource(form: Form): Promise<any> {
return new Promise<Object>((resolve, reject) => {
reject(new Error(`FileClient does not implement unlink`));
});
}
public subscribeResource(form: Form, next: ((value: any) => void), error?: (error: any) => void, complete?: () => void): any {
error(new Error(`FileClient does not implement subscribe`));
return null;
}
public start(): boolean {
return true;
}
public stop(): boolean {
return true;
}
public setSecurity = (metadata : any) => false;
}