internetdata-web 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +282 -2
- package/dist/bogon.d.ts +31 -0
- package/dist/bogon.js +214 -0
- package/dist/bogons.gen.d.ts +2 -0
- package/dist/bogons.gen.js +87 -0
- package/dist/client.d.ts +183 -0
- package/dist/client.js +446 -0
- package/dist/errors.d.ts +66 -0
- package/dist/errors.js +185 -0
- package/dist/generated/client/client.gen.d.ts +2 -0
- package/dist/generated/client/client.gen.js +216 -0
- package/dist/generated/client/index.d.ts +10 -0
- package/dist/generated/client/index.js +6 -0
- package/dist/generated/client/types.gen.d.ts +120 -0
- package/dist/generated/client/types.gen.js +2 -0
- package/dist/generated/client/utils.gen.d.ts +37 -0
- package/dist/generated/client/utils.gen.js +228 -0
- package/dist/generated/client.gen.d.ts +12 -0
- package/dist/generated/client.gen.js +3 -0
- package/dist/generated/core/auth.gen.d.ts +25 -0
- package/dist/generated/core/auth.gen.js +14 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +25 -0
- package/dist/generated/core/bodySerializer.gen.js +57 -0
- package/dist/generated/core/params.gen.d.ts +43 -0
- package/dist/generated/core/params.gen.js +109 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +33 -0
- package/dist/generated/core/pathSerializer.gen.js +106 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/generated/core/queryKeySerializer.gen.js +92 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/generated/core/serverSentEvents.gen.js +132 -0
- package/dist/generated/core/types.gen.d.ts +83 -0
- package/dist/generated/core/types.gen.js +2 -0
- package/dist/generated/core/utils.gen.d.ts +19 -0
- package/dist/generated/core/utils.gen.js +87 -0
- package/dist/generated/index.d.ts +2 -0
- package/dist/generated/index.js +2 -0
- package/dist/generated/sdk.gen.d.ts +117 -0
- package/dist/generated/sdk.gen.js +251 -0
- package/dist/generated/types.gen.d.ts +1527 -0
- package/dist/generated/types.gen.js +2 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +4 -0
- package/dist/oauth.d.ts +119 -0
- package/dist/oauth.js +247 -0
- package/dist/shapes.d.ts +68 -0
- package/dist/shapes.js +284 -0
- package/dist/store.d.ts +39 -0
- package/dist/store.js +64 -0
- package/dist/transport.d.ts +51 -0
- package/dist/transport.js +147 -0
- package/dist/types.d.ts +115 -0
- package/dist/types.js +46 -0
- package/package.json +65 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { InternetData, DEFAULT_BASE_URL } from './client.js';
|
|
2
|
+
export type { Options, CacheOptions, LookupOptions, BatchOptions } from './client.js';
|
|
3
|
+
export type { CacheStore } from './store.js';
|
|
4
|
+
export { OauthApi } from './oauth.js';
|
|
5
|
+
export type { OauthOptions, AuthorizationUrlOptions, OauthMetadata, TokenResponse, Pkce } from './oauth.js';
|
|
6
|
+
export { isBogon } from './bogon.js';
|
|
7
|
+
export { InternetDataError, OauthError, OauthAccessDeniedError, OauthExpiredTokenError, } from './errors.js';
|
|
8
|
+
export type { ErrorKind } from './errors.js';
|
|
9
|
+
export type { Result, AsnResult, DomainResult, LookupResponse, AsnLookupResponse, DomainLookupResponse, ClassDetail, ProxyDetail, PubproxyDetail, CarrierDetail, SatelliteDetail, SshDetail, SmtpDetail, WebDetail, DnsResolverDetail, BitTorrentDetail, Location, Asn, Company, AbuseContact, Whois, Rwhois, HostedDomains, AsnPrefix, DomainDns, DnsAddressRecord, DnsMxRecord, DnsNsRecord, DnsCnameRecord, DomainIpRanges, DomainIpRange, DisposableDetail, Entitlement, EntitlementApikey, EntitlementPlan, EntitlementUsage, } from './types.js';
|
package/dist/index.js
ADDED
package/dist/oauth.d.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Client } from './generated/client/index.js';
|
|
2
|
+
/** Per-call overrides for one OAuth request. Anything omitted falls back to the client's setting. */
|
|
3
|
+
export interface OauthOptions {
|
|
4
|
+
/** How long one attempt may take before it is abandoned, in milliseconds. */
|
|
5
|
+
timeoutMs?: number;
|
|
6
|
+
}
|
|
7
|
+
/** What an authorization URL asks for beyond what every one carries. A member left out is left out. */
|
|
8
|
+
export interface AuthorizationUrlOptions {
|
|
9
|
+
/** Space-delimited scopes, sent as given. The server narrows them to what the client may ask for. */
|
|
10
|
+
scope?: string;
|
|
11
|
+
/** Comes back on the redirect unchanged, so the caller can tell the answer is to its own request. */
|
|
12
|
+
state?: string;
|
|
13
|
+
/** The API the tokens are meant for. */
|
|
14
|
+
resource?: string;
|
|
15
|
+
}
|
|
16
|
+
/** One sign-in's PKCE pair: `challenge` goes in the authorization URL, `verifier` to the exchange. */
|
|
17
|
+
export interface Pkce {
|
|
18
|
+
/** 32 random bytes as 43 characters of unpadded base64url, sent only to the exchange. */
|
|
19
|
+
verifier: string;
|
|
20
|
+
/** The verifier's SHA-256, as unpadded base64url. */
|
|
21
|
+
challenge: string;
|
|
22
|
+
/** `S256`, the only method the server accepts. */
|
|
23
|
+
method: 'S256';
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The authorization server's discovery document. No call here needs it: each builds on the apex
|
|
27
|
+
* of the client's base URL.
|
|
28
|
+
*/
|
|
29
|
+
export interface OauthMetadata {
|
|
30
|
+
issuer: string;
|
|
31
|
+
authorization_endpoint: string;
|
|
32
|
+
token_endpoint: string;
|
|
33
|
+
device_authorization_endpoint?: string;
|
|
34
|
+
revocation_endpoint?: string;
|
|
35
|
+
scopes_supported?: string[];
|
|
36
|
+
response_types_supported?: string[];
|
|
37
|
+
grant_types_supported?: string[];
|
|
38
|
+
code_challenge_methods_supported?: string[];
|
|
39
|
+
token_endpoint_auth_methods_supported?: string[];
|
|
40
|
+
authorization_response_iss_parameter_supported?: boolean;
|
|
41
|
+
client_id_metadata_document_supported?: boolean;
|
|
42
|
+
registration_endpoint?: string;
|
|
43
|
+
service_documentation?: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* What a completed sign-in or a refresh hands back.
|
|
47
|
+
*
|
|
48
|
+
* `apikey_id` is set when the person picked one of their API keys and may still
|
|
49
|
+
* read it back. `apikey`, the key itself, also needs a sign-in rather than a
|
|
50
|
+
* refresh and a key whose secret can be shown again, so `apikey_id` without
|
|
51
|
+
* `apikey` is normal. An app signing in with a client metadata document is never
|
|
52
|
+
* handed a key. An empty `scope` is present.
|
|
53
|
+
*/
|
|
54
|
+
export interface TokenResponse {
|
|
55
|
+
access_token: string;
|
|
56
|
+
token_type: string;
|
|
57
|
+
expires_in: number;
|
|
58
|
+
/** Spent by the refresh that presents it, so keep the one each refresh returns. */
|
|
59
|
+
refresh_token?: string;
|
|
60
|
+
scope?: string;
|
|
61
|
+
apikey_id?: string;
|
|
62
|
+
apikey?: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Signs a person in through their browser with the OAuth authorization code flow
|
|
66
|
+
* and PKCE, so an app or an extension can act for them. Reached through
|
|
67
|
+
* `client.oauth`, whose requests go to the apex of the client's base URL, where the
|
|
68
|
+
* authorization server is: `https://api.internetdata.io` signs in at
|
|
69
|
+
* `https://internetdata.io`.
|
|
70
|
+
*
|
|
71
|
+
* Every call takes a client ID: one issued on request from
|
|
72
|
+
* support@internetdata.io, or the https URL of a client metadata document the app
|
|
73
|
+
* serves. None of these requests carries the client's API key, and none needs one.
|
|
74
|
+
*/
|
|
75
|
+
export declare class OauthApi {
|
|
76
|
+
private readonly client;
|
|
77
|
+
private readonly retries;
|
|
78
|
+
private readonly timeoutMs;
|
|
79
|
+
constructor(client: Client, retries: number, timeoutMs: number);
|
|
80
|
+
metadata(options?: OauthOptions): Promise<OauthMetadata>;
|
|
81
|
+
/**
|
|
82
|
+
* The URL to send the person's browser to. Makes no request. Once they
|
|
83
|
+
* decide, the server redirects to `redirectUri` with a `code` for
|
|
84
|
+
* `exchangeAuthorizationCode` (and `state`, when one was given), or with an
|
|
85
|
+
* `error`.
|
|
86
|
+
*/
|
|
87
|
+
authorizationUrl(clientId: string, redirectUri: string, codeChallenge: string, options?: AuthorizationUrlOptions): string;
|
|
88
|
+
/**
|
|
89
|
+
* Trade the `code` a sign-in's redirect brought back for tokens.
|
|
90
|
+
* `codeVerifier` is the PKCE verifier whose challenge went into the
|
|
91
|
+
* authorization URL, and `redirectUri` that URL's, exactly.
|
|
92
|
+
*
|
|
93
|
+
* Never retried: the server spends the code on first read, before it checks
|
|
94
|
+
* the verifier, so a retry could only be refused.
|
|
95
|
+
*/
|
|
96
|
+
exchangeAuthorizationCode(clientId: string, code: string, codeVerifier: string, redirectUri: string, options?: OauthOptions): Promise<TokenResponse>;
|
|
97
|
+
/**
|
|
98
|
+
* Trade a refresh token for a new pair. The old one is spent whatever happens
|
|
99
|
+
* next, so this is never retried. A refresh names the key the person picked
|
|
100
|
+
* (`apikey_id`) but never reveals it again (`apikey`).
|
|
101
|
+
*/
|
|
102
|
+
exchangeRefreshToken(clientId: string, refreshToken: string, options?: OauthOptions): Promise<TokenResponse>;
|
|
103
|
+
/**
|
|
104
|
+
* A fresh PKCE pair for one sign-in, from the browser's secure random source.
|
|
105
|
+
*
|
|
106
|
+
* Async, as Web Crypto's digest is. A page served over plain http has no Web
|
|
107
|
+
* Crypto digest at all, so there it rejects as `bad_request`.
|
|
108
|
+
*/
|
|
109
|
+
createPkce(): Promise<Pkce>;
|
|
110
|
+
/** The `S256` challenge for a PKCE verifier: its SHA-256, as unpadded base64url. */
|
|
111
|
+
pkceChallenge(verifier: string): Promise<string>;
|
|
112
|
+
/**
|
|
113
|
+
* End a token. A refresh token ends the whole sign-in and every token it
|
|
114
|
+
* issued, which is how an app signs the person out; an access token ends
|
|
115
|
+
* only itself. The server answers the same for any token, known or not.
|
|
116
|
+
*/
|
|
117
|
+
revoke(clientId: string, token: string, options?: OauthOptions): Promise<void>;
|
|
118
|
+
private exchange;
|
|
119
|
+
}
|
package/dist/oauth.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { oauthMetadata, oauthRevoke, oauthToken } from './generated/sdk.gen.js';
|
|
2
|
+
import { errorFromResponse, InternetDataError, messageFromBody, oauthErrorFrom } from './errors.js';
|
|
3
|
+
import { checkTimeout, deadline, noResponse, withRetry } from './transport.js';
|
|
4
|
+
/**
|
|
5
|
+
* Signs a person in through their browser with the OAuth authorization code flow
|
|
6
|
+
* and PKCE, so an app or an extension can act for them. Reached through
|
|
7
|
+
* `client.oauth`, whose requests go to the apex of the client's base URL, where the
|
|
8
|
+
* authorization server is: `https://api.internetdata.io` signs in at
|
|
9
|
+
* `https://internetdata.io`.
|
|
10
|
+
*
|
|
11
|
+
* Every call takes a client ID: one issued on request from
|
|
12
|
+
* support@internetdata.io, or the https URL of a client metadata document the app
|
|
13
|
+
* serves. None of these requests carries the client's API key, and none needs one.
|
|
14
|
+
*/
|
|
15
|
+
export class OauthApi {
|
|
16
|
+
client;
|
|
17
|
+
retries;
|
|
18
|
+
timeoutMs;
|
|
19
|
+
constructor(client, retries, timeoutMs) {
|
|
20
|
+
this.client = client;
|
|
21
|
+
this.retries = retries;
|
|
22
|
+
this.timeoutMs = timeoutMs;
|
|
23
|
+
}
|
|
24
|
+
async metadata(options = {}) {
|
|
25
|
+
checkTimeout(options.timeoutMs);
|
|
26
|
+
return withRetry(this.retries, async () => {
|
|
27
|
+
const res = await deadline(options.timeoutMs ?? this.timeoutMs, (signal) => oauthMetadata({
|
|
28
|
+
client: this.client, signal: signal,
|
|
29
|
+
}));
|
|
30
|
+
return decode(res, METADATA);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The URL to send the person's browser to. Makes no request. Once they
|
|
35
|
+
* decide, the server redirects to `redirectUri` with a `code` for
|
|
36
|
+
* `exchangeAuthorizationCode` (and `state`, when one was given), or with an
|
|
37
|
+
* `error`.
|
|
38
|
+
*/
|
|
39
|
+
authorizationUrl(clientId, redirectUri, codeChallenge, options = {}) {
|
|
40
|
+
const params = [
|
|
41
|
+
['response_type', 'code'],
|
|
42
|
+
['client_id', clientId],
|
|
43
|
+
['redirect_uri', redirectUri],
|
|
44
|
+
['code_challenge', codeChallenge],
|
|
45
|
+
['code_challenge_method', 'S256'],
|
|
46
|
+
['scope', options.scope || undefined],
|
|
47
|
+
['state', options.state || undefined],
|
|
48
|
+
['resource', options.resource || undefined],
|
|
49
|
+
];
|
|
50
|
+
const query = params
|
|
51
|
+
.filter((param) => param[1] !== undefined)
|
|
52
|
+
.map(([name, value]) => `${name}=${percentEncode(name, value)}`)
|
|
53
|
+
.join('&');
|
|
54
|
+
return `${this.client.getConfig().baseUrl ?? ''}/oauth/authorize?${query}`;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Trade the `code` a sign-in's redirect brought back for tokens.
|
|
58
|
+
* `codeVerifier` is the PKCE verifier whose challenge went into the
|
|
59
|
+
* authorization URL, and `redirectUri` that URL's, exactly.
|
|
60
|
+
*
|
|
61
|
+
* Never retried: the server spends the code on first read, before it checks
|
|
62
|
+
* the verifier, so a retry could only be refused.
|
|
63
|
+
*/
|
|
64
|
+
async exchangeAuthorizationCode(clientId, code, codeVerifier, redirectUri, options = {}) {
|
|
65
|
+
return this.exchange({
|
|
66
|
+
grant_type: 'authorization_code', code: code, redirect_uri: redirectUri,
|
|
67
|
+
client_id: clientId, code_verifier: codeVerifier,
|
|
68
|
+
}, options.timeoutMs);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Trade a refresh token for a new pair. The old one is spent whatever happens
|
|
72
|
+
* next, so this is never retried. A refresh names the key the person picked
|
|
73
|
+
* (`apikey_id`) but never reveals it again (`apikey`).
|
|
74
|
+
*/
|
|
75
|
+
async exchangeRefreshToken(clientId, refreshToken, options = {}) {
|
|
76
|
+
return this.exchange({
|
|
77
|
+
grant_type: 'refresh_token', refresh_token: refreshToken, client_id: clientId,
|
|
78
|
+
}, options.timeoutMs);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* A fresh PKCE pair for one sign-in, from the browser's secure random source.
|
|
82
|
+
*
|
|
83
|
+
* Async, as Web Crypto's digest is. A page served over plain http has no Web
|
|
84
|
+
* Crypto digest at all, so there it rejects as `bad_request`.
|
|
85
|
+
*/
|
|
86
|
+
async createPkce() {
|
|
87
|
+
const verifier = base64url(webCrypto().getRandomValues(new Uint8Array(32)));
|
|
88
|
+
return { verifier: verifier, challenge: await challengeFor(verifier), method: 'S256' };
|
|
89
|
+
}
|
|
90
|
+
/** The `S256` challenge for a PKCE verifier: its SHA-256, as unpadded base64url. */
|
|
91
|
+
async pkceChallenge(verifier) {
|
|
92
|
+
return challengeFor(verifier);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* End a token. A refresh token ends the whole sign-in and every token it
|
|
96
|
+
* issued, which is how an app signs the person out; an access token ends
|
|
97
|
+
* only itself. The server answers the same for any token, known or not.
|
|
98
|
+
*/
|
|
99
|
+
async revoke(clientId, token, options = {}) {
|
|
100
|
+
checkTimeout(options.timeoutMs);
|
|
101
|
+
await withRetry(this.retries, async () => {
|
|
102
|
+
const res = await deadline(options.timeoutMs ?? this.timeoutMs, (signal) => oauthRevoke({
|
|
103
|
+
client: this.client,
|
|
104
|
+
body: { token: token, client_id: clientId },
|
|
105
|
+
// The answer says nothing, so it is read as text and dropped rather than parsed.
|
|
106
|
+
parseAs: 'text',
|
|
107
|
+
signal: signal,
|
|
108
|
+
}));
|
|
109
|
+
throwIfFailed(res);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async exchange(form, timeoutMs) {
|
|
113
|
+
checkTimeout(timeoutMs);
|
|
114
|
+
const res = await deadline(timeoutMs ?? this.timeoutMs, (signal) => oauthToken({
|
|
115
|
+
client: this.client, body: form, signal: signal,
|
|
116
|
+
}));
|
|
117
|
+
return decode(res, TOKEN_RESPONSE);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async function challengeFor(verifier) {
|
|
121
|
+
if (typeof verifier !== 'string') {
|
|
122
|
+
throw new InternetDataError('bad_request', `verifier must be a string, got ${typeof verifier}`);
|
|
123
|
+
}
|
|
124
|
+
const digest = await webCrypto().subtle.digest('SHA-256', new TextEncoder().encode(verifier));
|
|
125
|
+
return base64url(new Uint8Array(digest));
|
|
126
|
+
}
|
|
127
|
+
// A browser offers Web Crypto's digest only to a secure context: https, or a
|
|
128
|
+
// page on the loopback address. Refused by name here rather than failing as a
|
|
129
|
+
// TypeError on `undefined`.
|
|
130
|
+
function webCrypto() {
|
|
131
|
+
const crypto = globalThis.crypto;
|
|
132
|
+
if (crypto?.subtle === undefined || typeof crypto.getRandomValues !== 'function') {
|
|
133
|
+
throw new InternetDataError('bad_request', 'PKCE needs Web Crypto, which this context lacks: serve the page over https');
|
|
134
|
+
}
|
|
135
|
+
return crypto;
|
|
136
|
+
}
|
|
137
|
+
function base64url(bytes) {
|
|
138
|
+
let binary = '';
|
|
139
|
+
for (const byte of bytes) {
|
|
140
|
+
binary += String.fromCharCode(byte);
|
|
141
|
+
}
|
|
142
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
143
|
+
}
|
|
144
|
+
// Every byte of the value's UTF-8 as %XX but A-Z a-z 0-9 - . _ ~, so a space is
|
|
145
|
+
// %20 and never +. encodeURIComponent alone leaves ! ' ( ) * as they are, and
|
|
146
|
+
// throws a URIError on a lone surrogate, which has no UTF-8 at all.
|
|
147
|
+
function percentEncode(name, value) {
|
|
148
|
+
if (typeof value !== 'string' || value === '') {
|
|
149
|
+
throw new InternetDataError('bad_request', `${name} must be a non-empty string`);
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
return encodeURIComponent(value)
|
|
153
|
+
.replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
throw new InternetDataError('bad_request', `${name} is not valid UTF-16, so it has no UTF-8 to send`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const METADATA = {
|
|
160
|
+
issuer: { type: 'string', required: true },
|
|
161
|
+
authorization_endpoint: { type: 'string', required: true },
|
|
162
|
+
token_endpoint: { type: 'string', required: true },
|
|
163
|
+
device_authorization_endpoint: { type: 'string' },
|
|
164
|
+
revocation_endpoint: { type: 'string' },
|
|
165
|
+
scopes_supported: { type: 'string[]' },
|
|
166
|
+
response_types_supported: { type: 'string[]' },
|
|
167
|
+
grant_types_supported: { type: 'string[]' },
|
|
168
|
+
code_challenge_methods_supported: { type: 'string[]' },
|
|
169
|
+
token_endpoint_auth_methods_supported: { type: 'string[]' },
|
|
170
|
+
authorization_response_iss_parameter_supported: { type: 'boolean' },
|
|
171
|
+
client_id_metadata_document_supported: { type: 'boolean' },
|
|
172
|
+
registration_endpoint: { type: 'string' },
|
|
173
|
+
service_documentation: { type: 'string' },
|
|
174
|
+
};
|
|
175
|
+
const TOKEN_RESPONSE = {
|
|
176
|
+
access_token: { type: 'string', required: true },
|
|
177
|
+
token_type: { type: 'string', required: true },
|
|
178
|
+
expires_in: { type: 'integer', required: true },
|
|
179
|
+
refresh_token: { type: 'string' },
|
|
180
|
+
scope: { type: 'string' },
|
|
181
|
+
apikey_id: { type: 'string', wire: 'mslm:apikey_id' },
|
|
182
|
+
apikey: { type: 'string', wire: 'mslm:apikey' },
|
|
183
|
+
};
|
|
184
|
+
// Only the declared members are copied, on PRESENCE, so an absent one stays
|
|
185
|
+
// absent and an empty `scope` stays present. Anything else the server sends is
|
|
186
|
+
// dropped rather than surfaced untyped.
|
|
187
|
+
function decode(res, members) {
|
|
188
|
+
const status = throwIfFailed(res).status;
|
|
189
|
+
if (res.error !== undefined) {
|
|
190
|
+
// A 2xx the generated client could not parse, which reports it here: as
|
|
191
|
+
// unreadable as an object missing a member, so the same retried error.
|
|
192
|
+
throw new InternetDataError('server_error', 'the answer was not JSON', status);
|
|
193
|
+
}
|
|
194
|
+
const body = res.data;
|
|
195
|
+
if (!isObject(body)) {
|
|
196
|
+
throw new InternetDataError('server_error', 'the answer was not a JSON object', status);
|
|
197
|
+
}
|
|
198
|
+
const out = {};
|
|
199
|
+
for (const [name, member] of Object.entries(members)) {
|
|
200
|
+
const wire = member.wire ?? name;
|
|
201
|
+
const value = body[wire];
|
|
202
|
+
if (value === undefined || value === null) {
|
|
203
|
+
if (member.required) {
|
|
204
|
+
throw new InternetDataError('server_error', `the answer carried no ${wire}`, status);
|
|
205
|
+
}
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (!hasType(value, member.type)) {
|
|
209
|
+
throw new InternetDataError('server_error', `the answer's ${wire} is not ${TYPE_NAMES[member.type]}`, status);
|
|
210
|
+
}
|
|
211
|
+
out[name] = value;
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
// Only a 4xx whose body is a JSON object with a STRING `error` is an OAuth
|
|
216
|
+
// refusal. Every 5xx, whatever it says, is the server failing, and is retried
|
|
217
|
+
// wherever the operation retries.
|
|
218
|
+
function throwIfFailed(res) {
|
|
219
|
+
if (res.response === undefined) {
|
|
220
|
+
throw noResponse(res.error);
|
|
221
|
+
}
|
|
222
|
+
const { status, ok, headers } = res.response;
|
|
223
|
+
if (ok) {
|
|
224
|
+
return res.response;
|
|
225
|
+
}
|
|
226
|
+
const body = res.error;
|
|
227
|
+
if (status >= 400 && status < 500 && isObject(body) && typeof body.error === 'string') {
|
|
228
|
+
const description = typeof body.error_description === 'string' ? body.error_description : undefined;
|
|
229
|
+
throw oauthErrorFrom(body.error, description, status);
|
|
230
|
+
}
|
|
231
|
+
throw errorFromResponse(status, headers, messageFromBody(body));
|
|
232
|
+
}
|
|
233
|
+
function isObject(value) {
|
|
234
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
235
|
+
}
|
|
236
|
+
const TYPE_NAMES = {
|
|
237
|
+
'string': 'a string', 'integer': 'an integer', 'boolean': 'a boolean', 'string[]': 'a list of strings',
|
|
238
|
+
};
|
|
239
|
+
function hasType(value, type) {
|
|
240
|
+
if (type === 'string[]') {
|
|
241
|
+
return Array.isArray(value) && value.every((item) => typeof item === 'string');
|
|
242
|
+
}
|
|
243
|
+
if (type === 'integer') {
|
|
244
|
+
return Number.isInteger(value);
|
|
245
|
+
}
|
|
246
|
+
return typeof value === type;
|
|
247
|
+
}
|
package/dist/shapes.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export declare const LOOKUP_MEMBERS: readonly ["ip", "is_bogon", "is_tor", "is_relay", "is_anycast", "is_carrier", "is_satellite", "is_ssh", "is_smtp", "is_web", "is_dns_resolver", "is_bittorrent", "tor", "relay", "location", "asn", "company", "hostname", "abuse"];
|
|
2
|
+
export declare const WEB_DETAIL_MEMBERS: readonly ["port", "tls_ok", "session_ok", "status_code", "server", "location", "title", "cert_subject_cn", "cert_subject_o", "cert_issuer_dn", "cert_browser_trusted", "tls_version", "first_seen", "last_seen"];
|
|
3
|
+
export declare const LOCATION_MEMBERS: readonly ["continent", "continent_name", "country", "country_name", "timezone", "currency_code", "currency_name", "currency_symbol", "flag_emoji", "flag_unicode"];
|
|
4
|
+
export declare const ASN_MEMBERS: readonly ["asn", "name", "domain", "type", "prefix"];
|
|
5
|
+
export declare const COMPANY_MEMBERS: readonly ["name", "domain"];
|
|
6
|
+
export declare const ABUSE_CONTACT_MEMBERS: readonly ["email"];
|
|
7
|
+
export declare const WHOIS_MEMBERS: readonly ["id", "name", "org", "country", "status", "source", "domain", "abuse_email", "asn"];
|
|
8
|
+
export declare const RWHOIS_MEMBERS: readonly ["id", "name", "country", "source", "domain", "abuse_email"];
|
|
9
|
+
export declare const HOSTED_DOMAINS_MEMBERS: readonly ["total"];
|
|
10
|
+
export declare const ASN_LOOKUP_MEMBERS: readonly ["asn", "name", "country", "registry", "allocated", "domain"];
|
|
11
|
+
export declare const ASN_PREFIX_MEMBERS: readonly ["prefix", "peer_count", "rpki_status", "first_seen", "country", "size", "other_origins"];
|
|
12
|
+
export declare const DOMAIN_LOOKUP_MEMBERS: readonly ["domain", "is_registered", "has_a", "has_aaaa", "has_mx", "has_ns", "has_cname"];
|
|
13
|
+
export declare const DOMAIN_DNS_MEMBERS: readonly ["a", "aaaa", "mx", "ns", "cname"];
|
|
14
|
+
export declare const DNS_ADDRESS_RECORD_MEMBERS: readonly ["ip", "last_seen"];
|
|
15
|
+
export declare const DNS_MX_RECORD_MEMBERS: readonly ["priority", "host", "last_seen"];
|
|
16
|
+
export declare const DNS_NS_RECORD_MEMBERS: readonly ["host", "last_seen"];
|
|
17
|
+
export declare const DNS_CNAME_RECORD_MEMBERS: readonly ["target", "last_seen"];
|
|
18
|
+
export declare const DOMAIN_IP_RANGES_MEMBERS: readonly ["domain", "redirects_to", "total", "ipv4", "ranges"];
|
|
19
|
+
export declare const DOMAIN_IP_RANGE_MEMBERS: readonly ["range", "name", "country"];
|
|
20
|
+
export declare const BATCH_MEMBERS: readonly ["results", "errors"];
|
|
21
|
+
export declare const BATCH_ERROR_MEMBERS: readonly ["status", "error"];
|
|
22
|
+
export declare const ENTITLEMENT_MEMBERS: readonly ["org_id", "apikey", "plan", "usage"];
|
|
23
|
+
export declare const ENTITLEMENT_APIKEY_MEMBERS: readonly ["id", "allowed_cidrs"];
|
|
24
|
+
export declare const ENTITLEMENT_PLAN_MEMBERS: readonly ["key", "tier"];
|
|
25
|
+
export declare const ENTITLEMENT_USAGE_MEMBERS: readonly ["requests", "quota", "window_start", "window_end"];
|
|
26
|
+
type Kind = 'string' | 'integer' | 'number' | 'boolean' | 'object' | 'array' | 'date' | 'date-time';
|
|
27
|
+
export declare const LOOKUP_TYPES: Record<string, Kind>;
|
|
28
|
+
export declare const CLASS_DETAIL_TYPES: Record<string, Kind>;
|
|
29
|
+
export declare const PROXY_DETAIL_TYPES: Record<string, Kind>;
|
|
30
|
+
export declare const PUBPROXY_DETAIL_TYPES: Record<string, Kind>;
|
|
31
|
+
export declare const CARRIER_DETAIL_TYPES: Record<string, Kind>;
|
|
32
|
+
export declare const SATELLITE_DETAIL_TYPES: Record<string, Kind>;
|
|
33
|
+
export declare const SSH_DETAIL_TYPES: Record<string, Kind>;
|
|
34
|
+
export declare const SMTP_DETAIL_TYPES: Record<string, Kind>;
|
|
35
|
+
export declare const WEB_DETAIL_TYPES: Record<string, Kind>;
|
|
36
|
+
export declare const DNS_RESOLVER_DETAIL_TYPES: Record<string, Kind>;
|
|
37
|
+
export declare const BITTORRENT_DETAIL_TYPES: Record<string, Kind>;
|
|
38
|
+
export declare const LOCATION_TYPES: Record<string, Kind>;
|
|
39
|
+
export declare const ASN_TYPES: Record<string, Kind>;
|
|
40
|
+
export declare const COMPANY_TYPES: Record<string, Kind>;
|
|
41
|
+
export declare const ABUSE_CONTACT_TYPES: Record<string, Kind>;
|
|
42
|
+
export declare const WHOIS_TYPES: Record<string, Kind>;
|
|
43
|
+
export declare const RWHOIS_TYPES: Record<string, Kind>;
|
|
44
|
+
export declare const HOSTED_DOMAINS_TYPES: Record<string, Kind>;
|
|
45
|
+
export declare const ASN_LOOKUP_TYPES: Record<string, Kind>;
|
|
46
|
+
export declare const ASN_PREFIX_TYPES: Record<string, Kind>;
|
|
47
|
+
export declare const DOMAIN_LOOKUP_TYPES: Record<string, Kind>;
|
|
48
|
+
export declare const DOMAIN_DNS_TYPES: Record<string, Kind>;
|
|
49
|
+
export declare const DNS_ADDRESS_RECORD_TYPES: Record<string, Kind>;
|
|
50
|
+
export declare const DNS_MX_RECORD_TYPES: Record<string, Kind>;
|
|
51
|
+
export declare const DNS_NS_RECORD_TYPES: Record<string, Kind>;
|
|
52
|
+
export declare const DNS_CNAME_RECORD_TYPES: Record<string, Kind>;
|
|
53
|
+
export declare const DOMAIN_IP_RANGES_TYPES: Record<string, Kind>;
|
|
54
|
+
export declare const DOMAIN_IP_RANGE_TYPES: Record<string, Kind>;
|
|
55
|
+
export declare const DISPOSABLE_DETAIL_TYPES: Record<string, Kind>;
|
|
56
|
+
export declare const BATCH_ERROR_TYPES: Record<string, Kind>;
|
|
57
|
+
export declare const ENTITLEMENT_TYPES: Record<string, Kind>;
|
|
58
|
+
export declare const ENTITLEMENT_APIKEY_TYPES: Record<string, Kind>;
|
|
59
|
+
export declare const ENTITLEMENT_PLAN_TYPES: Record<string, Kind>;
|
|
60
|
+
export declare const ENTITLEMENT_USAGE_TYPES: Record<string, Kind>;
|
|
61
|
+
export declare function isLookup(data: unknown): boolean;
|
|
62
|
+
export declare function isAsnLookup(data: unknown): boolean;
|
|
63
|
+
export declare function isDomainLookup(data: unknown): boolean;
|
|
64
|
+
/** Both maps present. An entry in either is judged on its own, as that address's answer. */
|
|
65
|
+
export declare function isBatch(data: unknown): boolean;
|
|
66
|
+
export declare function isBatchError(entry: unknown): boolean;
|
|
67
|
+
export declare function isEntitlement(data: unknown): boolean;
|
|
68
|
+
export {};
|