-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
ActorHttp.ts
85 lines (74 loc) 路 2.5 KB
/
ActorHttp.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
import type { IAction, IActorArgs, IActorOutput, IActorTest, Mediate } from '@comunica/core';
import { Actor } from '@comunica/core';
import { readableFromWeb } from 'readable-from-web';
/* istanbul ignore next */
if (!globalThis.ReadableStream) {
globalThis.ReadableStream = require('web-streams-ponyfill').ReadableStream;
}
const isStream = require('is-stream');
const toWebReadableStream = require('readable-stream-node-to-web');
/**
* A base actor for listening to HTTP events.
*
* Actor types:
* * Input: IActionHttp: The HTTP request.
* * Test: IActorHttpTest: An estimate for the response time.
* * Output: IActorHttpOutput: The HTTP response.
*
* @see IActionHttp
* @see IActorHttpTest
* @see IActorHttpOutput
*/
export abstract class ActorHttp extends Actor<IActionHttp, IActorTest, IActorHttpOutput> {
/**
* @param args - @defaultNested {<default_bus> a <cc:components/Bus.jsonld#Bus>} bus
*/
public constructor(args: IActorHttpArgs) {
super(args);
}
/**
* Converts WhatWG streams to Node streams if required.
* Returns the input in case the stream already is a Node stream.
* @param {ReadableStream} body
* @returns {NodeJS.ReadableStream} A node stream.
*/
public static toNodeReadable(body: ReadableStream | null): NodeJS.ReadableStream {
return isStream(body) || body === null ?
<NodeJS.ReadableStream> <any> body :
<NodeJS.ReadableStream> <any> readableFromWeb(body);
}
/**
* Converts Node streams to WhatWG streams.
* @param {NodeJS.ReadableStream} body
* @returns {ReadableStream} A web stream.
*/
public static toWebReadableStream(body: NodeJS.ReadableStream | null): ReadableStream {
return toWebReadableStream(body);
}
/**
* Convert the given headers object into a raw hash.
* @param headers A headers object.
*/
public static headersToHash(headers: Headers): Record<string, string> {
const hash: Record<string, string> = {};
// eslint-disable-next-line unicorn/no-array-for-each
headers.forEach((value, key) => {
hash[key] = value;
});
return hash;
}
}
/**
* The HTTP input, which contains the HTTP request.
*/
export interface IActionHttp extends IAction {
input: RequestInfo;
init?: RequestInit;
}
/**
* The HTTP output, which contains the HTTP response.
*/
export interface IActorHttpOutput extends IActorOutput, Response {
}
export type IActorHttpArgs = IActorArgs<IActionHttp, IActorTest, IActorHttpOutput>;
export type MediatorHttp = Mediate<IActionHttp, IActorHttpOutput>;