Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type {IMeta, Adapter} from '@feasibleone/blong/types';
import {adapter, type Errors, type IErrorMap} from '@feasibleone/blong/types';
import got, {type HttpsOptions, type Options} from 'got';
import tls from '../../tls.ts';
export interface IConfig {
tls?: {
key?: string;
cert?: string;
ca?: string | string[];
};
url?: string;
}
const errorMap: IErrorMap = {
'http.generic': 'HTTP Error',
};
let _errors: Errors<typeof errorMap>;
export default adapter<IConfig>(({utError}) => {
_errors ||= utError.register(errorMap);
let https: HttpsOptions;
return {
activation: {
default: {
type: 'http',
},
},
async init(...configs: object[]) {
await super.init(...configs);
https = tls(this.config, true) as HttpsOptions;
},
start() {
super.connect();
return super.start();
},
/**
* configChanged hook: only recreate TLS options when the `tls` or `url`
* sub-key changed. Unrelated config changes are ignored.
*/
async configChanged(diff: Map<string, {prev: unknown; next: unknown}>, next: unknown) {
const tlsOrUrlChanged = Array.from(diff.keys()).some(
(key: string) =>
key === this.config.id + '.tls' ||
key.startsWith(this.config.id + '.tls.') ||
key === this.config.id + '.url',
);
if (!tlsOrUrlChanged) return;
const newAdapterConfig = (next as Record<string, unknown>)?.[this.config.id] as
| Partial<IConfig>
| undefined;
if (newAdapterConfig) {
this.config.tls = newAdapterConfig.tls ?? this.config.tls;
this.config.url = newAdapterConfig.url ?? this.config.url;
}
https = tls(this.config, true) as HttpsOptions;
},
async exec(
this: Adapter<IConfig>,
{
path,
query: searchParams,
host,
port,
url = new URL(path ?? '', this.config.url || 'http://localhost'),
responseType,
method,
headers,
body,
form,
json,
}: {
path?: string;
query?: string;
host?: string;
port?: number;
url?: URL;
responseType?: Options['responseType'];
method?: Options['method'];
headers?: Options['headers'];
body?: Options['body'];
form?: Options['form'];
json?: Options['json'];
},
$meta: IMeta,
) {
const {stream} = $meta;
try {
if (host) url.hostname = host;
if (port) url.port = String(port);
return got({
url,
searchParams,
https,
method: method || 'POST',
headers,
responseType,
body,
form,
json,
throwHttpErrors: false,
followRedirect: false,
isStream: !!stream,
});
} catch (error) {
throw this.error(_errors['http.generic'](error), $meta);
}
},
};
});
|