Skip to content

Commit eaa63d4

Browse files
committed
docs: cut comments that restate the code
1 parent 4b27ab9 commit eaa63d4

14 files changed

Lines changed: 34 additions & 158 deletions

‎examples/stream.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ while (!controller.signal.aborted) {
2626
throw error;
2727
}
2828
console.error("stream:", error);
29-
// Avoid reconnecting in a tight loop.
29+
// Stream connections are limited to 30 per minute per team.
3030
await sleep(2000);
3131
}
3232
}

‎src/client.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ import {
2525
import type { Account, DomainInfo, IPInfo } from "./types.ts";
2626
import { VERSION } from "./version.ts";
2727

28-
/** The base URL of the Synthient HTTP API. */
2928
export const DEFAULT_BASE_URL = "https://api.synthient.com/api/v4";
3029

31-
/** Configures a {@link Synthient} client. */
3230
export interface ClientOptions {
3331
/** The API key. Defaults to the SYNTHIENT_API_KEY environment variable. */
3432
apiKey?: string | undefined;
@@ -59,7 +57,6 @@ export interface ClientOptions {
5957
onResponse?: ((metadata: ResponseMetadata) => void) | undefined;
6058
}
6159

62-
/** Options for {@link Synthient.lookupDomain}. */
6360
export interface LookupDomainOptions extends RequestOptions {
6461
/** Caps the number of recent events returned. The API default is 50, and it clamps values above 1000. */
6562
limit?: number | undefined;

‎src/errors.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export class APITimeoutError extends APIConnectionError {
3030
override name = "APITimeoutError";
3131
}
3232

33-
/** The fields of an {@link APIError}. */
3433
export interface APIErrorInit {
3534
status: number;
3635
type?: string | undefined;
@@ -81,8 +80,8 @@ export class APIError extends SynthientError {
8180
}
8281

8382
/**
84-
* Builds an APIError from a response and its bounded body. Plain text, HTML, and
85-
* malformed problem details still keep the status, request ID, and Retry-After.
83+
* Plain text, HTML, and malformed problem details still keep the status, request ID, and
84+
* Retry-After.
8685
*/
8786
export function apiError(response: Response, body: Uint8Array, cause?: unknown): APIError {
8887
const decoded = new TextDecoder().decode(body);

‎src/events.ts‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { UnixTime } from "./types.ts";
22

3-
/** The category of a proxy, VPN, or other service seen on an address. New values may be added. */
3+
/** New values may be added. */
44
export type ServiceCategory =
55
| "FREE_VPN"
66
| "COMMERCIAL_VPN"
@@ -25,7 +25,6 @@ export type ServiceCategory =
2525
| "INTERNET_SCANNER"
2626
| (string & {});
2727

28-
/** A proxy sighting from the proxies feed. */
2928
export interface ProxyEvent {
3029
/** The proxy exit address. */
3130
ip?: string;
@@ -38,7 +37,6 @@ export interface ProxyEvent {
3837
timestamp?: UnixTime;
3938
}
4039

41-
/** A VPN or anonymizer IP range from the anonymizers feed. */
4240
export interface AnonymizerEvent {
4341
/** The first address of the range. */
4442
range_start?: string;
@@ -71,15 +69,12 @@ export interface TorrentEvent {
7169
timestamp?: UnixTime;
7270
}
7371

74-
/** A file in a torrent. */
7572
export interface TorrentFile {
76-
/** The file path within the torrent. */
7773
path?: string;
7874
/** The file size in bytes. */
7975
length?: number;
8076
}
8177

82-
/** A peer seen sharing a torrent. */
8378
export interface TorrentPeer {
8479
ip?: string;
8580
port?: number;
@@ -89,9 +84,7 @@ export interface TorrentPeer {
8984
encrypted?: boolean;
9085
}
9186

92-
/** A JA4T TCP fingerprint sighting. */
9387
export interface JA4TEvent {
94-
/** The address the fingerprint was seen from. */
9588
ip?: string;
9689
ja4t?: string;
9790
/** The service tag of the address. */
@@ -129,15 +122,12 @@ export interface HeliosHTTPEvent {
129122
/** The destination port. */
130123
port: number;
131124
protocol: "http";
132-
/** The capturing sensor. */
133125
meta: HeliosMeta;
134-
/** The parsed request. */
135126
details?: HTTPRequestDetails;
136127
/** The raw request bytes, base64-encoded. */
137128
raw?: string;
138129
}
139130

140-
/** A parsed HTTP request. */
141131
export interface HTTPRequestDetails {
142132
method?: string;
143133
/** The request target. */
@@ -161,7 +151,6 @@ export interface HeliosHTTPSEvent {
161151
/** The destination port. */
162152
port: number;
163153
protocol: "https";
164-
/** The capturing sensor. */
165154
meta: HeliosMeta;
166155
/** The parsed ClientHello, or null when the sensor could not parse the handshake. */
167156
details?: TLSClientHello | null;
@@ -225,7 +214,6 @@ export interface TLSCodeName {
225214
name?: string;
226215
}
227216

228-
/** A TLS extension in a ClientHello. */
229217
export interface TLSExtension {
230218
code?: number;
231219
name?: string;
@@ -244,7 +232,6 @@ export interface HeliosDNSEvent {
244232
/** The destination port. */
245233
port: number;
246234
protocol: "dns";
247-
/** The capturing sensor. */
248235
meta: HeliosMeta;
249236
}
250237

‎src/feed.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ type Reviver = (key: string, value: unknown, context?: { source: string }) => un
1515
export const exactTunnelID: Reviver = (key, value, context) =>
1616
key === "tunnel_id" && typeof value === "number" && context ? context.source : value;
1717

18-
/** Options for listing exports. */
1918
export interface ListExportsOptions extends RequestOptions {
2019
/** The page size. The API default is 100, and it clamps values above 500. */
2120
limit?: number | undefined;
@@ -189,10 +188,7 @@ function exportPath(id: string): string {
189188
throw new TypeError("invalid export ID");
190189
}
191190

192-
/**
193-
* Parses an NDJSON body into its JSON object events, skipping blank lines. Canceling
194-
* the iteration cancels the body.
195-
*/
191+
/** Blank lines are keepalives. A final line without a newline is still an event. */
196192
async function* ndjson<T>(
197193
body: ReadableStream<Uint8Array>,
198194
signal: AbortSignal | undefined,

‎src/grpcschema.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { Client, Metadata, credentials, status, type ServiceError } from "@grpc/
1616
import { SynthientError } from "./errors.ts";
1717
import { checkTimeout, resolveAPIKey } from "./options.ts";
1818

19-
/** The address of the Synthient gRPC API. */
2019
export const DEFAULT_ENDPOINT = "grpc.synthient.com:443";
2120

2221
const MAX_MESSAGE_SIZE = 32 << 20;
@@ -37,7 +36,6 @@ const EXPLANATIONS: Partial<Record<number, string>> = {
3736
"Timed out reading protobuf schemas from gRPC reflection. Use a longer timeout.",
3837
};
3938

40-
/** Configures {@link fetchSchema}. */
4139
export interface FetchSchemaOptions {
4240
/** Sent as x-api-key metadata when not empty. Defaults to the SYNTHIENT_API_KEY environment variable. */
4341
apiKey?: string | undefined;
@@ -59,7 +57,6 @@ export interface FetchSchemaOptions {
5957
timeout?: number | undefined;
6058
}
6159

62-
/** The protobuf schema returned by {@link fetchSchema}. */
6360
export interface Schema {
6461
/** The normalized address that was queried. */
6562
endpoint: string;
@@ -236,8 +233,8 @@ export async function fetchSchema(options: FetchSchemaOptions = {}): Promise<Sch
236233
}
237234

238235
/**
239-
* Converts `raw`, in any form {@link FetchSchemaOptions.endpoint} accepts, to host:port
240-
* and returns it with the host name used for TLS.
236+
* Converts `raw`, in any form {@link FetchSchemaOptions.endpoint} accepts, to host:port.
237+
* `host` is the bare host name, without brackets or a port.
241238
*/
242239
export function normalizeEndpoint(raw: string): { endpoint: string; host: string } {
243240
let value = raw.trim() || DEFAULT_ENDPOINT;
@@ -313,7 +310,7 @@ function decodeReply(bytes: Uint8Array): Reply {
313310
return reply;
314311
}
315312

316-
// Reads a protobuf message field by field. Fields the visitor leaves unread are skipped.
313+
// Fields the visitor leaves unread are skipped.
317314
function decode(bytes: Uint8Array, visit: (field: number, reader: BinaryReader) => void): void {
318315
const reader = new BinaryReader(bytes);
319316
while (reader.pos < reader.len) {

‎src/options.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
/**
2-
* Returns the trimmed API key, falling back to the SYNTHIENT_API_KEY environment variable.
3-
* It throws a TypeError, without the key in its message, if the key has characters a
4-
* header cannot carry.
5-
*/
1+
/** Rejects a key that a header cannot carry, without putting the key in the error message. */
62
export function resolveAPIKey(apiKey: string | undefined): string {
73
const key = (apiKey ?? process.env.SYNTHIENT_API_KEY ?? "").trim();
84
if (/[^\x20-\x7e]/.test(key)) {
@@ -11,14 +7,13 @@ export function resolveAPIKey(apiKey: string | undefined): string {
117
return key;
128
}
139

14-
/** Throws a RangeError unless `timeout` is a whole number of milliseconds setTimeout accepts. */
10+
/** The upper bound is the longest delay setTimeout accepts. */
1511
export function checkTimeout(timeout: number): void {
1612
if (!Number.isInteger(timeout) || timeout < 1 || timeout > 2 ** 31 - 1) {
1713
throw new RangeError("timeout must be an integer number of milliseconds from 1 to 2147483647");
1814
}
1915
}
2016

21-
/** Returns the query parameters for an optional page or result limit. */
2217
export function limitQuery(limit: number | undefined): Record<string, string> {
2318
if (limit === undefined) {
2419
return {};

‎src/transport.ts‎

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ const TRANSIENT_CODES = new Set([
3333

3434
const RETRY_STATUSES = new Set([500, 502, 503, 504]);
3535

36-
/** The subset of `fetch` the client uses. */
3736
export type Fetch = (input: string, init: RequestInit) => Promise<Response>;
3837

39-
/** Describes one HTTP response from the Synthient API. */
4038
export interface ResponseMetadata {
41-
/** The HTTP status code. */
4239
status: number;
4340
/** The X-Request-Id header, or "". */
4441
requestID: string;
@@ -50,7 +47,6 @@ export interface ResponseMetadata {
5047
headers: Headers;
5148
}
5249

53-
/** Options for a single call. */
5450
export interface RequestOptions {
5551
/**
5652
* Cancels the call, which rejects with the signal's reason. Use `AbortSignal.timeout(ms)`
@@ -88,8 +84,8 @@ export interface Request {
8884
}
8985

9086
/**
91-
* One attempt at a request. Its signal follows the caller's signal and an idle timer
92-
* that runs only while the attempt waits on the network.
87+
* Its signal follows the caller's signal and an idle timer that runs only while the
88+
* attempt waits on the network.
9389
*/
9490
export class Attempt {
9591
readonly caller: AbortSignal | undefined;
@@ -110,9 +106,8 @@ export class Attempt {
110106
}
111107

112108
/**
113-
* Waits at most the timeout for work that reads from the network. A caller abort
114-
* rejects with the caller's reason, the timeout with APITimeoutError, and any other
115-
* failure with APIConnectionError.
109+
* Storage failures are rebuilt without their message or cause, either of which can
110+
* contain the presigned URL.
116111
*/
117112
async watch<T>(work: Promise<T>): Promise<T> {
118113
const timer = setTimeout(() => {
@@ -156,16 +151,12 @@ export class Transport {
156151
this.#headers = new Headers({ "x-api-key": config.apiKey, "user-agent": config.userAgent });
157152
}
158153

159-
/** Returns the absolute URL of an API request. */
160154
url(request: Request): string {
161155
const search = new URLSearchParams(request.query).toString();
162156
return search ? `${this.#base}${request.path}?${search}` : this.#base + request.path;
163157
}
164158

165-
/**
166-
* Sends an API request, retrying as its policy allows, and returns the final
167-
* response with its body unread.
168-
*/
159+
/** The caller must read or cancel the returned response's body. */
169160
send(request: Request, signal?: AbortSignal): Promise<[Response, Attempt]> {
170161
return this.#request<[Response, Attempt]>(request, signal, (response, attempt) => [
171162
response,
@@ -223,7 +214,6 @@ export class Transport {
223214
}
224215
}
225216

226-
/** Sends an API request and returns its JSON object body. */
227217
json<T extends object>(request: Request, signal?: AbortSignal): Promise<T> {
228218
return this.#request(request, signal, async (response, attempt) => {
229219
if (!response.ok) {
@@ -247,10 +237,6 @@ export class Transport {
247237
});
248238
}
249239

250-
/**
251-
* Sends an API request that must answer 200 and returns its body, with each read
252-
* bounded by the client's timeout.
253-
*/
254240
async stream(request: Request, signal?: AbortSignal): Promise<ReadableStream<Uint8Array>> {
255241
const [response, attempt] = await this.send(request, signal);
256242
if (response.status !== 200) {
@@ -259,10 +245,6 @@ export class Transport {
259245
return watchedBody(response, attempt);
260246
}
261247

262-
/**
263-
* Reads a failed response's bounded body into an APIError. If reading fails, the
264-
* error keeps the status and has the read failure as its cause.
265-
*/
266248
async error(response: Response, attempt: Attempt): Promise<APIError> {
267249
try {
268250
const [body] = await read(response, attempt, MAX_ERROR_SIZE);
@@ -276,9 +258,8 @@ export class Transport {
276258
}
277259

278260
/**
279-
* Fetches a presigned storage URL without the API key, following at most 10
280-
* redirects that stay on http(s) and never downgrade from https. Errors never
281-
* include the URL, and storage error bodies are discarded.
261+
* The API key is never sent to the storage host. Errors never include the presigned
262+
* URL, and storage error bodies are discarded.
282263
*/
283264
async download(start: URL, signal?: AbortSignal): Promise<ReadableStream<Uint8Array>> {
284265
let url = start;
@@ -336,10 +317,6 @@ export function isHTTP(url: URL | null): url is URL {
336317
return url?.protocol === "https:" || url?.protocol === "http:";
337318
}
338319

339-
/**
340-
* Resolves a redirect's location against `from`, allowing only an http(s) URL without
341-
* credentials that does not downgrade from https.
342-
*/
343320
export function redirectTarget(from: URL, response: Response): URL {
344321
const location = response.headers.get("location");
345322
const target = location === null ? null : URL.parse(location, from.href);
@@ -352,16 +329,15 @@ export function redirectTarget(from: URL, response: Response): URL {
352329
return target;
353330
}
354331

355-
/** Waits for a backoff delay, rejecting with the signal's reason if it aborts. */
356332
export async function wait(ms: number, signal: AbortSignal | undefined): Promise<void> {
357333
try {
358334
await sleep(ms, undefined, { signal });
359335
} catch (error) {
336+
// timers/promises rejects with an AbortError, not the signal's reason.
360337
throw signal?.aborted ? signal.reason : error;
361338
}
362339
}
363340

364-
/** Returns the jittered delay in milliseconds before retry `attempt`, counted from zero. */
365341
export function backoff(attempt: number): number {
366342
return Math.min(1000 * 2 ** Math.min(attempt, 5), MAX_BACKOFF) * (0.75 + Math.random() / 2);
367343
}
@@ -386,10 +362,6 @@ function transient(error: unknown, signal: AbortSignal | undefined): boolean {
386362
return false;
387363
}
388364

389-
/**
390-
* Wraps a response body so each read is bounded by the attempt's timeout. Canceling the
391-
* returned stream cancels the body.
392-
*/
393365
function watchedBody(response: Response, attempt: Attempt): ReadableStream<Uint8Array> {
394366
if (!response.body) {
395367
return new ReadableStream({
@@ -417,10 +389,7 @@ function watchedBody(response: Response, attempt: Attempt): ReadableStream<Uint8
417389
);
418390
}
419391

420-
/**
421-
* Reads at most `limit` bytes of a body. It returns the bytes and whether the body
422-
* ended within the limit; a longer body is canceled after the limit.
423-
*/
392+
/** Returns false with the first `limit` bytes when the body is longer than `limit`. */
424393
async function read(
425394
response: Response,
426395
attempt: Attempt,
@@ -450,7 +419,7 @@ async function read(
450419
}
451420
}
452421

453-
/** Discards a body so its connection can be reused. Failures other than caller cancellation are ignored. */
422+
/** Discards a body so its connection can be reused. */
454423
export async function drain(response: Response, attempt: Attempt): Promise<void> {
455424
await read(response, attempt, MAX_ERROR_SIZE).catch(() => undefined);
456425
attempt.caller?.throwIfAborted();

0 commit comments

Comments
 (0)