/
ActorDereference.ts
106 lines (99 loc) 路 3.18 KB
/
ActorDereference.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
import type { IAction, IActorArgs, IActorOutput, IActorTest, Mediate } from '@comunica/core';
import { ActorDereferenceBase } from './ActorDereferenceBase';
/**
* A base actor for dereferencing URLs to (generic) streams.
*
* Actor types:
* * Input: IActionDereference: A URL.
* * Test: <none>
* * Output: IActorDereferenceOutput: A Readable stream
*
* @see IActionDereference
* @see IActorDereferenceOutput
*/
export abstract class ActorDereference extends
ActorDereferenceBase<IActionDereference, IActorTest, IActorDereferenceOutput> {
/**
* @param args - @defaultNested {<default_bus> a <cc:components/Bus.jsonld#Bus>} bus
*/
public constructor(args: IActorDereferenceArgs) {
super(args);
}
/**
* Handle the given error as a rejection or delegate it to the logger,
* depending on whether or not hard errors are enabled.
* @param {IActionDereference} action A dereference action.
* @param {Error} error An error that has occurred.
* @param headers Optional HTTP headers to pass.
* @param {number} requestTime The time it took to request the page in milliseconds.
* @return {Promise<IActorDereferenceOutput>} A promise that rejects or resolves to an empty output.
*/
protected async handleDereferenceErrors(
action: IActionDereference,
error: unknown,
headers?: Headers | undefined,
requestTime = 0,
): Promise<IActorDereferenceOutput> {
return this.dereferenceErrorHandler(action, error, { url: action.url, exists: false, headers, requestTime });
}
}
export interface IActionDereference extends IAction {
/**
* The URL to dereference
*/
url: string;
/**
* By default, actors will reject upon receiving non-200 HTTP responses.
* If this option is true, then all HTTP responses will cause the action to resolve,
* but some outputs may therefore contain empty quad streams.
*/
acceptErrors?: boolean;
/**
* Optional HTTP method to use.
* Defaults to GET.
*/
method?: string;
/**
* Optional HTTP headers to pass.
*/
headers?: Headers;
/**
* An optional callback to retrieve the mediaType mappings
*/
mediaTypes?: () => Promise<Record<string, number> | undefined>;
}
interface IReadableClose extends NodeJS.ReadableStream {
close?: () => void | Promise<void>;
}
export interface IActorDereferenceOutput extends IActorOutput {
/**
* The page on which the output was found.
*
* This is not necessarily the same as the original input url,
* as this may have changed due to redirects.
*/
url: string;
/**
* The resulting stream.
*/
data: IReadableClose;
/**
* This will always be true, unless `acceptErrors` was set to true in the action and the dereferencing failed.
*/
exists: boolean;
/**
* The time it took to request the page in milliseconds.
* This is the time until the first byte arrives.
*/
requestTime: number;
/**
* The returned headers of the final URL.
*/
headers?: Headers;
/**
* The mediatype of the source
*/
mediaType?: string;
}
export type IActorDereferenceArgs = IActorArgs<IActionDereference, IActorTest, IActorDereferenceOutput>;
export type MediatorDereference = Mediate<IActionDereference, IActorDereferenceOutput>;