-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
client.ts
500 lines (383 loc) · 16.4 KB
/
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
Copyright 2016 kanreisa
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import * as fs from "fs";
import * as http from "http";
import * as querystring from "querystring";
import * as yaml from "js-yaml";
import { OpenAPIV2 } from "openapi-types";
import * as apid from "../api";
import { IncomingHttpHeaders } from "http";
const pkg = require("../package.json");
const spec = yaml.safeLoad(fs.readFileSync(__dirname + "/../api.yml", "utf8")) as OpenAPIV2.Document;
export type RequestMethod = "GET" | "POST" | "PUT" | "DELETE";
export interface RequestOption {
/** positive integer */
priority?: number;
/** request headers */
headers?: { [key: string]: string };
/** request query */
query?: { [key: string]: any };
/** request body */
body?: string | object;
}
export interface Response {
status: number;
statusText: string;
contentType: string;
headers: IncomingHttpHeaders;
isSuccess: boolean;
body?: any | string | Buffer;
}
export interface ErrorResponse extends Response {
body?: apid.Error;
}
export interface ChannelsQuery {
type?: apid.ChannelType;
channel?: string;
name?: string;
}
export interface ProgramsQuery {
networkId?: apid.NetworkId;
serviceId?: apid.ServiceId;
eventId?: apid.EventId;
}
export interface EventsQuery {
resource?: apid.EventResource;
type?: apid.EventType;
}
export interface ServicesQuery {
serviceId?: apid.ServiceId;
networkId?: apid.NetworkId;
name?: string;
type?: number;
"channel.type"?: apid.ChannelType;
"channel.channel"?: string;
}
export interface ChannelScanOption {
type?: apid.ChannelType;
min?: number;
max?: number;
}
export default class Client {
basePath = spec.basePath;
docsPath = "/docs";
/** positive integer */
priority = 0;
host = "";
port = 40772;
socketPath = process.platform === "win32" ? "\\\\.\\pipe\\mirakurun" : "/var/run/mirakurun.sock";
agent: http.Agent | boolean;
/** provide User-Agent string to identify client. */
userAgent = "";
private _userAgent = `MirakurunClient/${pkg.version} Node/${process.version} (${process.platform})`;
private _docs: OpenAPIV2.Document;
request(method: RequestMethod, path: string, option: RequestOption = {}): Promise<Response>|Promise<ErrorResponse> {
return new Promise((resolve, reject) => {
this._httpRequest(method, path, option).then(
res => {
const ret: Response = {
status: res.statusCode,
statusText: res.statusMessage,
contentType: res.headers["content-type"].split(";")[0],
headers: res.headers,
isSuccess: (res.statusCode >= 200 && res.statusCode <= 202)
};
const chunks: Buffer[] = [];
res.on("data", chunk => chunks.push(chunk as Buffer));
res.on("end", () => {
const buffer = Buffer.concat(chunks);
if (ret.contentType === "application/json") {
ret.body = JSON.parse(buffer.toString("utf8"));
} else if (ret.contentType === "text/plain") {
ret.body = buffer.toString("utf8");
} else {
ret.body = buffer;
}
if (ret.isSuccess === true) {
resolve(ret);
} else {
reject(ret);
}
});
},
err => {
const ret: Response = {
status: -1,
statusText: "Request Failure",
contentType: "",
headers: {},
isSuccess: false,
body: err
};
reject(ret);
}
);
});
}
async call(operationId: string, param: { [key: string]: any } = {}): Promise<any|http.IncomingMessage> {
if (!this._docs) {
await this._getDocs();
}
let path: string;
let method: RequestMethod;
let parameters: OpenAPIV2.GeneralParameterObject[];
let operation: OpenAPIV2.OperationObject;
for (path in this._docs.paths) {
const p = this._docs.paths[path] as OpenAPIV2.PathItemObject;
if (p.post?.operationId === operationId) {
method = "POST";
parameters = [...p.parameters, ...(p.post.parameters || [])] as any;
operation = p.post;
break;
}
if (p.get?.operationId === operationId) {
method = "GET";
parameters = [...p.parameters, ...(p.get.parameters || [])] as any;
operation = p.get;
break;
}
if (p.put?.operationId === operationId) {
method = "PUT";
parameters = [...p.parameters, ...(p.put.parameters || [])] as any;
operation = p.put;
break;
}
if (p.delete?.operationId === operationId) {
method = "DELETE";
parameters = [...p.parameters, ...(p.delete.parameters || [])] as any;
operation = p.delete;
break;
}
}
if (!operation) {
throw new Error(`operationId "${operationId}" is not found.`);
}
const option: RequestOption = {
headers: {},
query: {}
};
for (const p of parameters) {
if (param[p.name] === undefined || param[p.name] === null) {
if (p.required) {
throw new Error(`Required parameter "${p.name}" is undefined.`);
}
continue;
}
if (p.in === "path") {
path = path.replace(`{${p.name}}`, param[p.name]);
} else if (p.in === "header") {
option.headers[p.name] = param[p.name];
} else if (p.in === "query") {
option.query[p.name] = param[p.name];
} else if (p.in === "body" && p.name === "body") {
option.body = param.body;
}
}
if (operation.tags.indexOf("stream") !== -1) {
return await this._requestStream(method, path, option);
}
return await this.request(method, path, option);
}
async getChannels(query?: ChannelsQuery): Promise<apid.Channel[]> {
const res = await this.call("getChannels", query);
return res.body as apid.Channel[];
}
async getChannelsByType(type: apid.ChannelType, query?: ChannelsQuery): Promise<apid.Channel[]> {
const res = await this.call("getChannelsByType", { type, ...query });
return res.body as apid.Channel[];
}
async getChannel(type: apid.ChannelType, channel: string): Promise<apid.Channel> {
const res = await this.call("getChannel", { type, channel });
return res.body as apid.Channel;
}
async getServicesByChannel(type: apid.ChannelType, channel: string): Promise<apid.Service[]> {
const res = await this.call("getServicesByChannel", { type, channel });
return res.body as apid.Service[];
}
async getServiceByChannel(type: apid.ChannelType, channel: string, sid: apid.ServiceId): Promise<apid.Service> {
const res = await this.call("getServiceByChannel", { type, channel, sid });
return res.body as apid.Service;
}
async getServiceStreamByChannel(type: apid.ChannelType, channel: string, sid: apid.ServiceId, decode?: boolean, priority?: number): Promise<http.IncomingMessage> {
return await this.call("getServiceStreamByChannel", { type, channel, sid, "decode": decode ? 1 : 0, "X-Mirakurun-Priority": priority });
}
async getChannelStream(type: apid.ChannelType, channel: string, decode?: boolean, priority?: number): Promise<http.IncomingMessage> {
return await this.call("getChannelStream", { type, channel, "decode": decode ? 1 : 0, "X-Mirakurun-Priority": priority });
}
async getPrograms(query?: ProgramsQuery): Promise<apid.Program[]> {
const res = await this.call("getPrograms", query);
return res.body as apid.Program[];
}
async getProgram(id: apid.ProgramId): Promise<apid.Program> {
const res = await this.call("getProgram", { id });
return res.body as apid.Program;
}
async getProgramStream(id: apid.ProgramId, decode?: boolean, priority?: number): Promise<http.IncomingMessage> {
return await this.call("getProgramStream", { id, "decode": decode ? 1 : 0, "X-Mirakurun-Priority": priority });
}
async getServices(query?: ServicesQuery): Promise<apid.Service[]> {
const res = await this.call("getServices", query);
return res.body as apid.Service[];
}
async getService(id: apid.ServiceItemId): Promise<apid.Service> {
const res = await this.call("getService", { id });
return res.body as apid.Service;
}
async getLogoImage(id: apid.ServiceItemId): Promise<Buffer> {
const res = await this.call("getLogoImage", { id });
return res.body as Buffer;
}
async getServiceStream(id: apid.ServiceItemId, decode?: boolean, priority?: number): Promise<http.IncomingMessage> {
return await this.call("getServiceStream", { id, "decode": decode ? 1 : 0, "X-Mirakurun-Priority": priority });
}
async getTuners(): Promise<apid.TunerDevice[]> {
const res = await this.call("getTuners");
return res.body as apid.TunerDevice[];
}
async getTuner(index: number): Promise<apid.TunerDevice> {
const res = await this.call("getTuner", { index });
return res.body as apid.TunerDevice;
}
async getTunerProcess(index: number): Promise<apid.TunerProcess> {
const res = await this.call("getTunerProcess", { index });
return res.body as apid.TunerProcess;
}
async killTunerProcess(index: number): Promise<apid.TunerProcess> {
const res = await this.call("killTunerProcess", { index });
return res.body as apid.TunerProcess;
}
async getEvents(): Promise<apid.Event[]> {
const res = await this.call("getEvents");
return res.body as apid.Event[];
}
async getEventsStream(query?: EventsQuery): Promise<http.IncomingMessage> {
return await this.call("getEventsStream", query);
}
async getChannelsConfig(): Promise<apid.ConfigChannels> {
const res = await this.call("getChannelsConfig");
return res.body as apid.ConfigChannels;
}
async updateChannelsConfig(channels: apid.ConfigChannels): Promise<apid.ConfigChannels> {
const res = await this.call("updateChannelsConfig", { body: channels });
return res.body as apid.ConfigChannels;
}
async channelScan(option?: ChannelScanOption): Promise<http.IncomingMessage> {
return await this.call("channelScan", option);
}
async getServerConfig(): Promise<apid.ConfigServer> {
const res = await this.call("getServerConfig");
return res.body as apid.ConfigServer;
}
async updateServerConfig(server: apid.ConfigServer): Promise<apid.ConfigServer> {
const res = await this.call("updateServerConfig", { body: server });
return res.body as apid.ConfigServer;
}
async getTunersConfig(): Promise<apid.ConfigTuners> {
const res = await this.call("getTunersConfig");
return res.body as apid.ConfigTuners;
}
async updateTunersConfig(tuners: apid.ConfigTuners): Promise<apid.ConfigTuners> {
const res = await this.call("updateTunersConfig", { body: tuners });
return res.body as apid.ConfigTuners;
}
async getLog(): Promise<string> {
const res = await this.call("getLog");
return res.body as string;
}
async getLogStream(): Promise<http.IncomingMessage> {
return await this.call("getLogStream");
}
async checkVersion(): Promise<apid.Version> {
const res = await this.call("checkVersion");
return res.body as apid.Version;
}
async updateVersion(force?: boolean): Promise<http.IncomingMessage> {
return await this.call("updateVersion", { force });
}
async getStatus(): Promise<apid.Status> {
const res = await this.call("getStatus");
return res.body as apid.Status;
}
async restart(): Promise<{}> {
const res = await this.call("restart");
return res.body;
}
private _httpRequest(method: RequestMethod, path: string, option: RequestOption = {}): Promise<http.IncomingMessage> {
const opt: http.RequestOptions = {
method: method,
path: this.basePath + path,
headers: option.headers || {},
agent: this.agent
};
if (this.host === "") {
opt.socketPath = this.socketPath;
} else {
opt.host = this.host;
opt.port = this.port;
}
if (this.userAgent === "") {
opt.headers["User-Agent"] = this._userAgent;
} else {
opt.headers["User-Agent"] = this.userAgent + " " + this._userAgent;
}
if (option.priority === undefined) {
option.priority = this.priority;
}
opt.headers["X-Mirakurun-Priority"] = option.priority.toString(10);
if (typeof option.query === "object") {
opt.path += "?" + querystring.stringify(option.query);
}
if (typeof option.body === "object") {
opt.headers["Content-Type"] = "application/json; charset=utf-8";
option.body = JSON.stringify(option.body);
}
return new Promise((resolve, reject) => {
const req = http.request(opt, res => {
if (res.statusCode > 300 && res.statusCode < 400 && res.headers.location) {
if (/^\//.test(res.headers.location) === false) {
reject(new Error(`Redirecting location "${res.headers.location}" isn't supported.`));
return;
}
this._httpRequest(method, res.headers.location, option)
.then(resolve, reject);
return;
}
resolve(res);
});
req.on("error", reject);
// write request body
if (typeof option.body === "string") {
req.write(option.body + "\n");
}
req.end();
});
}
private async _requestStream(method: RequestMethod, path: string, option: RequestOption = {}): Promise<http.IncomingMessage> {
const res = await this._httpRequest(method, path, option);
if (res.statusCode >= 200 && res.statusCode <= 202) {
return res;
} else {
if (res.statusCode) {
throw new Error(`Bad status respond (${res.statusCode} ${res.statusMessage}).`);
}
throw res;
}
}
private async _getDocs() {
const res = await this.request("GET", this.docsPath);
if (res.isSuccess !== true) {
throw new Error(`Failed to get "${this.docsPath}".`);
}
this._docs = res.body;
}
}