paseo-plugin-genui 0.0.0-stage → 0.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 +202 -0
- package/OVERVIEW.md +21 -0
- package/README.md +77 -2
- package/assets/fonts/Excalifont-OFL.txt +109 -0
- package/assets/fonts/Excalifont-Regular-latin.woff2 +0 -0
- package/assets/icon.png +0 -0
- package/assets/icon.svg +1 -0
- package/assets/screenshot-chat.png +0 -0
- package/assets/screenshot-gallery.png +0 -0
- package/assets/screenshot-playground.png +0 -0
- package/client/components/content.tsx +468 -0
- package/client/components/data.tsx +967 -0
- package/client/components/dev.tsx +998 -0
- package/client/components/diagram.tsx +513 -0
- package/client/components/inputs.tsx +994 -0
- package/client/components/layout.tsx +239 -0
- package/client/components/media.tsx +428 -0
- package/client/components/project.tsx +641 -0
- package/client/components/widgets.tsx +836 -0
- package/client/gallery.tsx +449 -0
- package/client/live-ui.tsx +525 -0
- package/client/loaders.ts +342 -0
- package/client/markdown.tsx +172 -0
- package/client/node-renderer.tsx +149 -0
- package/client/render.tsx +186 -0
- package/client/runtime.ts +103 -0
- package/client/settings.tsx +57 -0
- package/client/web.ts +186 -0
- package/gallery/Acceptance-baseline.ui +1 -0
- package/gallery/Acceptance-checklist.ui +14 -0
- package/gallery/Acceptance-evidence.ui +9 -0
- package/gallery/Acceptance.ui +1 -0
- package/gallery/Badge.ui +3 -0
- package/gallery/Benchmark-context.ui +8 -0
- package/gallery/Benchmark-hyperfine.ui +1 -0
- package/gallery/Benchmark-runs-list.ui +9 -0
- package/gallery/Benchmark-runs.ui +1 -0
- package/gallery/Benchmark-vitest.ui +1 -0
- package/gallery/Benchmark.ui +9 -0
- package/gallery/Board-bb-tasks.ui +1 -0
- package/gallery/Board-gh-issues.ui +1 -0
- package/gallery/Board-gh-prs.ui +1 -0
- package/gallery/Board.ui +12 -0
- package/gallery/Button-destructive.ui +4 -0
- package/gallery/Button.ui +5 -0
- package/gallery/Calendar.ui +15 -0
- package/gallery/CalendarHeatmap-short.ui +11 -0
- package/gallery/CalendarHeatmap.ui +4 -0
- package/gallery/Callout.ui +4 -0
- package/gallery/Card.ui +4 -0
- package/gallery/Carousel.ui +8 -0
- package/gallery/Changes-paths.ui +1 -0
- package/gallery/Changes-uncommitted.ui +1 -0
- package/gallery/Changes.ui +1 -0
- package/gallery/Chart-area.ui +3 -0
- package/gallery/Chart-funnel.ui +3 -0
- package/gallery/Chart-heatmap.ui +11 -0
- package/gallery/Chart-pie.ui +3 -0
- package/gallery/Chart-radar.ui +3 -0
- package/gallery/Chart-scatter.ui +4 -0
- package/gallery/Chart-stacked.ui +5 -0
- package/gallery/Chart.ui +3 -0
- package/gallery/Checklist.ui +6 -0
- package/gallery/Choice.ui +8 -0
- package/gallery/Code.ui +3 -0
- package/gallery/Commits-limit.ui +1 -0
- package/gallery/Commits.ui +1 -0
- package/gallery/Compare-conditions.ui +4 -0
- package/gallery/Compare-three.ui +7 -0
- package/gallery/Compare.ui +4 -0
- package/gallery/Coverage-go.ui +1 -0
- package/gallery/Coverage-lcov.ui +1 -0
- package/gallery/Coverage-python.ui +1 -0
- package/gallery/Coverage.ui +1 -0
- package/gallery/DateInput.ui +5 -0
- package/gallery/Decision-percent.ui +10 -0
- package/gallery/Decision.ui +12 -0
- package/gallery/Dependencies-audit.ui +1 -0
- package/gallery/Dependencies-both.ui +1 -0
- package/gallery/Dependencies-list.ui +7 -0
- package/gallery/Dependencies-pip.ui +1 -0
- package/gallery/Dependencies-pnpm.ui +1 -0
- package/gallery/Dependencies.ui +1 -0
- package/gallery/Details.ui +4 -0
- package/gallery/Diagram-flow.ui +16 -0
- package/gallery/Diagram-sequence.ui +15 -0
- package/gallery/Diagram.ui +16 -0
- package/gallery/Diff.ui +3 -0
- package/gallery/Divider.ui +5 -0
- package/gallery/EditableTable.ui +9 -0
- package/gallery/Entity-list.ui +6 -0
- package/gallery/Entity.ui +8 -0
- package/gallery/Environments.ui +1 -0
- package/gallery/Estimate-points.ui +6 -0
- package/gallery/Estimate.ui +8 -0
- package/gallery/File.ui +4 -0
- package/gallery/Findings-biome.ui +1 -0
- package/gallery/Findings-eslint.ui +1 -0
- package/gallery/Findings-oxlint.ui +1 -0
- package/gallery/Findings-review.ui +1 -0
- package/gallery/Findings-ruff.ui +1 -0
- package/gallery/Findings-sarif.ui +1 -0
- package/gallery/Findings-ship.ui +11 -0
- package/gallery/Findings-tsc.ui +1 -0
- package/gallery/Findings.ui +7 -0
- package/gallery/Flashcards.ui +3 -0
- package/gallery/FollowUps.ui +4 -0
- package/gallery/Form.ui +8 -0
- package/gallery/Gallery-links.ui +8 -0
- package/gallery/Gallery-strip.ui +14 -0
- package/gallery/Gallery.ui +10 -0
- package/gallery/Gauge.ui +7 -0
- package/gallery/Grid.ui +8 -0
- package/gallery/Hotspots-photo.ui +8 -0
- package/gallery/Hotspots.ui +11 -0
- package/gallery/Icon.ui +5 -0
- package/gallery/Image.ui +4 -0
- package/gallery/ImageSearch-aspects.ui +6 -0
- package/gallery/ImageSearch.ui +4 -0
- package/gallery/Input.ui +5 -0
- package/gallery/Json-api.ui +1 -0
- package/gallery/Json.ui +8 -0
- package/gallery/KeyValue.ui +3 -0
- package/gallery/List-dots.ui +8 -0
- package/gallery/List.ui +8 -0
- package/gallery/ListEditor.ui +5 -0
- package/gallery/Logs-jsonl.ui +1 -0
- package/gallery/Logs-lines.ui +11 -0
- package/gallery/Logs-python.ui +1 -0
- package/gallery/Logs.ui +1 -0
- package/gallery/Map-city.ui +8 -0
- package/gallery/Map.ui +8 -0
- package/gallery/Metric.ui +3 -0
- package/gallery/OptionCards-multi.ui +10 -0
- package/gallery/OptionCards.ui +10 -0
- package/gallery/Pipeline-checks.ui +1 -0
- package/gallery/Pipeline-passed.ui +1 -0
- package/gallery/Pipeline-running.ui +1 -0
- package/gallery/Pipeline-stages.ui +13 -0
- package/gallery/Pipeline.ui +1 -0
- package/gallery/Plan.ui +9 -0
- package/gallery/Progress.ui +4 -0
- package/gallery/Provenance-deploy.ui +6 -0
- package/gallery/Provenance-no-receipt.ui +1 -0
- package/gallery/Provenance.ui +1 -0
- package/gallery/PullRequest.ui +1 -0
- package/gallery/Quiz.ui +6 -0
- package/gallery/Radio.ui +5 -0
- package/gallery/Rating.ui +5 -0
- package/gallery/Roadmap-sprint.ui +8 -0
- package/gallery/Roadmap-year.ui +9 -0
- package/gallery/Roadmap.ui +12 -0
- package/gallery/Row.ui +3 -0
- package/gallery/SegmentedBar-status.ui +8 -0
- package/gallery/SegmentedBar.ui +8 -0
- package/gallery/Select.ui +5 -0
- package/gallery/Slider.ui +7 -0
- package/gallery/Snippet.ui +3 -0
- package/gallery/Sources-chips.ui +4 -0
- package/gallery/Sources.ui +8 -0
- package/gallery/Sparkline.ui +6 -0
- package/gallery/Stack.ui +7 -0
- package/gallery/StackTrace-aggregate.ui +1 -0
- package/gallery/StackTrace-browser.ui +1 -0
- package/gallery/StackTrace-go.ui +1 -0
- package/gallery/StackTrace-java.ui +1 -0
- package/gallery/StackTrace-python.ui +1 -0
- package/gallery/StackTrace-rust.ui +1 -0
- package/gallery/StackTrace.ui +1 -0
- package/gallery/Status.ui +9 -0
- package/gallery/Stepper.ui +5 -0
- package/gallery/Steps.ui +8 -0
- package/gallery/Swatches-long-names.ui +4 -0
- package/gallery/Swatches-picker.ui +5 -0
- package/gallery/Swatches.ui +10 -0
- package/gallery/Table.ui +7 -0
- package/gallery/Tabs.ui +8 -0
- package/gallery/Tests-junit.ui +1 -0
- package/gallery/Tests-vitest-junit.ui +1 -0
- package/gallery/Tests-vitest.ui +1 -0
- package/gallery/Tests.ui +9 -0
- package/gallery/Text.ui +4 -0
- package/gallery/TextArea.ui +5 -0
- package/gallery/Threads-ids.ui +1 -0
- package/gallery/Threads.ui +1 -0
- package/gallery/Timeline.ui +8 -0
- package/gallery/Timer.ui +4 -0
- package/gallery/Toggle.ui +5 -0
- package/gallery/Transcript-text.ui +1 -0
- package/gallery/Transcript-untimed.ui +1 -0
- package/gallery/Transcript-whisper.ui +1 -0
- package/gallery/Transcript.ui +1 -0
- package/gallery/Tree.ui +3 -0
- package/gallery/Treemap-du.ui +1 -0
- package/gallery/Treemap-esbuild.ui +1 -0
- package/gallery/Treemap-tree.ui +7 -0
- package/gallery/Treemap.ui +18 -0
- package/gallery/Walkthrough.ui +9 -0
- package/gallery/Waterfall-har.ui +1 -0
- package/gallery/Waterfall-trace.ui +1 -0
- package/gallery/Waterfall.ui +11 -0
- package/gallery/data/acceptance/coil/viewport-lifecycle.json +25 -0
- package/gallery/data/acceptance/coil-gate.json +153 -0
- package/gallery/data/acceptance/orbit-baseline.json +163 -0
- package/gallery/data/acceptance/shots/gate-cell-opened.png +0 -0
- package/gallery/data/acceptance/shots/gate-light.png +0 -0
- package/gallery/data/acceptance/shots/gate-phone-dark.png +0 -0
- package/gallery/data/activity.json +1 -0
- package/gallery/data/bb-tasks-orb.json +420 -0
- package/gallery/data/biome.json +229 -0
- package/gallery/data/compare/status-after.png +0 -0
- package/gallery/data/compare/status-before.png +0 -0
- package/gallery/data/compare/status-dark-2x.png +0 -0
- package/gallery/data/compare/status-rerender.png +0 -0
- package/gallery/data/coverage-py.json +1 -0
- package/gallery/data/coverage-summary.json +1 -0
- package/gallery/data/coverage.out +23 -0
- package/gallery/data/deploys.json +1 -0
- package/gallery/data/device-runs.json +285 -0
- package/gallery/data/du-node-modules.txt +359 -0
- package/gallery/data/esbuild-meta.json +292 -0
- package/gallery/data/eslint.json +266 -0
- package/gallery/data/eslint.sarif +343 -0
- package/gallery/data/findings-review.json +76 -0
- package/gallery/data/gh-issues-nextjs.json +293 -0
- package/gallery/data/gh-pr-checks.json +272 -0
- package/gallery/data/gh-prs-nextjs.json +419 -0
- package/gallery/data/gh-run-failed.json +1 -0
- package/gallery/data/gh-run-passed.json +1 -0
- package/gallery/data/gh-run-running.json +1 -0
- package/gallery/data/github-release.json +57 -0
- package/gallery/data/hyperfine-startup.json +187 -0
- package/gallery/data/lcov.info +234 -0
- package/gallery/data/logs/media-sync.log +127 -0
- package/gallery/data/logs/thumbnailer.jsonl +17 -0
- package/gallery/data/logs/vitest-run.log +89 -0
- package/gallery/data/npm-audit.json +672 -0
- package/gallery/data/npm-outdated.json +179 -0
- package/gallery/data/oxlint.json +156 -0
- package/gallery/data/pip-outdated.json +68 -0
- package/gallery/data/pnpm-outdated.json +58 -0
- package/gallery/data/provenance/devicectl-install.json +35 -0
- package/gallery/data/provenance/install-timeout.log +4 -0
- package/gallery/data/provenance/iphone-install.json +72 -0
- package/gallery/data/provenance/iphone-no-receipt.json +57 -0
- package/gallery/data/provenance/live-home.jpg +0 -0
- package/gallery/data/provenance/live-probe.txt +10 -0
- package/gallery/data/provenance/xcodebuild-2811.log +19 -0
- package/gallery/data/provenance/xcodebuild-4712.log +19 -0
- package/gallery/data/pytest-junit.xml +64 -0
- package/gallery/data/python-org-trace.json +35 -0
- package/gallery/data/python-org.har +31 -0
- package/gallery/data/retry.ts +13 -0
- package/gallery/data/ruff.json +261 -0
- package/gallery/data/src/durations.py +63 -0
- package/gallery/data/src/json-logic.ts +156 -0
- package/gallery/data/src/mapMath.ts +135 -0
- package/gallery/data/src/semver/bump.go +73 -0
- package/gallery/data/traces/browser-error.json +5 -0
- package/gallery/data/traces/go/main.go +27 -0
- package/gallery/data/traces/go-panic.txt +16 -0
- package/gallery/data/traces/java-caused-by.txt +15 -0
- package/gallery/data/traces/node-aggregate.txt +30 -0
- package/gallery/data/traces/node-cause.txt +21 -0
- package/gallery/data/traces/py/deploy.json +5 -0
- package/gallery/data/traces/py/deploy.py +17 -0
- package/gallery/data/traces/py/settings.py +19 -0
- package/gallery/data/traces/python-chain.txt +30 -0
- package/gallery/data/traces/rust/main.rs +11 -0
- package/gallery/data/traces/rust-panic.txt +16 -0
- package/gallery/data/traces/shop/cart.mjs +19 -0
- package/gallery/data/traces/shop/pricing.mjs +12 -0
- package/gallery/data/traces/shop/status.mjs +9 -0
- package/gallery/data/traces/web/app.js +11 -0
- package/gallery/data/traces/web/index.html +1 -0
- package/gallery/data/traces/web/search.js +10 -0
- package/gallery/data/transcript/call-noisy.wav +0 -0
- package/gallery/data/transcript/call.json +362 -0
- package/gallery/data/transcript/call.wav +0 -0
- package/gallery/data/transcript/mlx-whisper-tiny.en-noisy.json +1 -0
- package/gallery/data/transcript/whisper-cpp-base.en-noisy.json +658 -0
- package/gallery/data/transcript/whisper-cpp-base.en.json +658 -0
- package/gallery/data/transcript/whisper-cpp-base.en.srt +16 -0
- package/gallery/data/transcript/whisper-cpp-tiny.en-noisy.json +619 -0
- package/gallery/data/tsc.txt +4 -0
- package/gallery/data/vitest-bench.json +119 -0
- package/gallery/data/vitest-junit.xml +133 -0
- package/gallery/data/vitest-report.json +612 -0
- package/gallery/images/bell.svg +10 -0
- package/gallery/images/bike.svg +47 -0
- package/gallery/images/footer-planet.webp +0 -0
- package/gallery/images/front-light.svg +13 -0
- package/gallery/images/golden-hour.webp +0 -0
- package/gallery/images/option-flowers.webp +0 -0
- package/gallery/images/option-hilltop.webp +0 -0
- package/gallery/images/option-meadow.webp +0 -0
- package/gallery/images/option-sky.webp +0 -0
- package/gallery/images/u-lock.svg +10 -0
- package/index.client.tsx +164 -0
- package/index.server.ts +216 -0
- package/package.json +50 -4
- package/paseo-plugin.json +14 -0
- package/server/data-source.ts +407 -0
- package/server/project.ts +981 -0
- package/server/web/address.ts +57 -0
- package/server/web/cache.ts +85 -0
- package/server/web/commons.ts +126 -0
- package/server/web/geocode.ts +40 -0
- package/server/web/image.ts +35 -0
- package/server/web/lookups.ts +248 -0
- package/server/web/net.ts +210 -0
- package/shared/core/acceptance.ts +744 -0
- package/shared/core/ast.ts +76 -0
- package/shared/core/benchmark.ts +743 -0
- package/shared/core/catalog.ts +733 -0
- package/shared/core/compare.ts +225 -0
- package/shared/core/coverage.ts +456 -0
- package/shared/core/data.ts +334 -0
- package/shared/core/decisions.ts +527 -0
- package/shared/core/dependencies.ts +604 -0
- package/shared/core/diagnostics.ts +24 -0
- package/shared/core/environments.ts +212 -0
- package/shared/core/evaluate.ts +737 -0
- package/shared/core/fields.ts +66 -0
- package/shared/core/findings.ts +1172 -0
- package/shared/core/form.ts +69 -0
- package/shared/core/index.ts +44 -0
- package/shared/core/junit.ts +252 -0
- package/shared/core/lexer.ts +275 -0
- package/shared/core/logs.ts +496 -0
- package/shared/core/markdown.ts +2374 -0
- package/shared/core/parser.ts +355 -0
- package/shared/core/perf-units.ts +228 -0
- package/shared/core/pipeline.ts +679 -0
- package/shared/core/project.ts +472 -0
- package/shared/core/provenance.ts +725 -0
- package/shared/core/stack-trace.ts +718 -0
- package/shared/core/streaming.ts +136 -0
- package/shared/core/test-results.ts +562 -0
- package/shared/core/thread-activity.ts +203 -0
- package/shared/core/tracking.ts +887 -0
- package/shared/core/transcript.ts +1364 -0
- package/shared/core/treemap.ts +446 -0
- package/shared/core/types.ts +70 -0
- package/shared/core/validate.ts +574 -0
- package/shared/core/waterfall.ts +536 -0
- package/shared/diagram/flowchart.ts +205 -0
- package/shared/diagram/layout.ts +440 -0
- package/shared/diagram/markdown.ts +135 -0
- package/shared/diagram/sequence.ts +122 -0
- package/shared/diagram/zoom.ts +114 -0
- package/shared/fallback.ts +120 -0
- package/shared/genui.test.ts +122 -0
- package/shared/instructions.ts +144 -0
- package/shared/logic/calendarMath.ts +130 -0
- package/shared/logic/chartMath.ts +289 -0
- package/shared/logic/choose-logic.ts +196 -0
- package/shared/logic/compare-logic.ts +270 -0
- package/shared/logic/content-model.ts +227 -0
- package/shared/logic/environments-logic.ts +53 -0
- package/shared/logic/explain-logic.ts +315 -0
- package/shared/logic/fieldLogic.ts +138 -0
- package/shared/logic/gallery-search.ts +71 -0
- package/shared/logic/icons.ts +322 -0
- package/shared/logic/json-logic.ts +153 -0
- package/shared/logic/mapMath.ts +133 -0
- package/shared/logic/project-logic.ts +39 -0
- package/shared/logic/references-model.ts +201 -0
- package/shared/logic/sketch.ts +165 -0
- package/shared/logic/supersession.ts +77 -0
- package/shared/logic/values.ts +133 -0
- package/shared/logic/widget-logic.ts +163 -0
- package/shared/rpc.ts +252 -0
- package/shared/segment.ts +81 -0
- package/shared/settings.ts +12 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// The plugin's one way onto the network: GET over https to public addresses
|
|
2
|
+
// only. Every hop of a redirect is checked again, and the address check runs
|
|
3
|
+
// inside the connection's own DNS lookup, so the address that was checked is
|
|
4
|
+
// the address connected to (no DNS-rebinding window). Bodies are capped and
|
|
5
|
+
// requests time out; failures are values with a short reason, never throws.
|
|
6
|
+
import { lookup as dnsLookup, type LookupAddress } from "node:dns";
|
|
7
|
+
import type { IncomingHttpHeaders } from "node:http";
|
|
8
|
+
import { Agent, request } from "node:https";
|
|
9
|
+
import { isIP, type LookupFunction } from "node:net";
|
|
10
|
+
import { brotliDecompressSync, gunzipSync, inflateSync } from "node:zlib";
|
|
11
|
+
import { isPublicAddress } from "./address";
|
|
12
|
+
|
|
13
|
+
/** Names this plugin to every service, as their usage policies require. */
|
|
14
|
+
export const USER_AGENT = "bb-plugin-genui/0.1 (Generative UI plugin for bb; https://getbb.app)";
|
|
15
|
+
|
|
16
|
+
const MAX_REDIRECTS = 5;
|
|
17
|
+
/** A connection that goes quiet this long is dropped, whatever the overall deadline. */
|
|
18
|
+
const IDLE_MS = 8_000;
|
|
19
|
+
|
|
20
|
+
export interface GetOptions {
|
|
21
|
+
/** The Accept header. */
|
|
22
|
+
readonly accept: string;
|
|
23
|
+
readonly maxBytes: number;
|
|
24
|
+
/** Deadline for the whole request, redirects included. */
|
|
25
|
+
readonly timeoutMs: number;
|
|
26
|
+
/** Extra request headers (conditional-request validators). */
|
|
27
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type Fetched =
|
|
31
|
+
| {
|
|
32
|
+
readonly ok: true;
|
|
33
|
+
/** 200, or 304 when validators were sent and still hold (empty body). */
|
|
34
|
+
readonly status: 200 | 304;
|
|
35
|
+
/** The final URL after redirects. */
|
|
36
|
+
readonly url: string;
|
|
37
|
+
readonly headers: IncomingHttpHeaders;
|
|
38
|
+
readonly body: Buffer;
|
|
39
|
+
}
|
|
40
|
+
| { readonly ok: false; readonly error: string };
|
|
41
|
+
|
|
42
|
+
class Refused extends Error {}
|
|
43
|
+
|
|
44
|
+
/** Resolves like dns.lookup, but fails unless every address is public. */
|
|
45
|
+
const publicLookup = ((hostname: string, options: { all?: boolean }, callback: (...args: unknown[]) => void) => {
|
|
46
|
+
dnsLookup(hostname, { all: true, verbatim: true }, (error, addresses: LookupAddress[]) => {
|
|
47
|
+
if (error) {
|
|
48
|
+
callback(error);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (addresses.length === 0 || addresses.some((entry) => !isPublicAddress(entry.address))) {
|
|
52
|
+
callback(new Refused(`${hostname} is not on the public internet`));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (options.all) callback(null, addresses);
|
|
56
|
+
else callback(null, addresses[0].address, addresses[0].family);
|
|
57
|
+
});
|
|
58
|
+
}) as unknown as LookupFunction;
|
|
59
|
+
|
|
60
|
+
const agent = new Agent({ keepAlive: true, maxSockets: 6, lookup: publicLookup });
|
|
61
|
+
|
|
62
|
+
/** Why `url` may not be fetched, or null when it may. */
|
|
63
|
+
export function refusal(url: URL): string | null {
|
|
64
|
+
if (url.protocol !== "https:") return "only https links can be fetched";
|
|
65
|
+
if (url.username !== "" || url.password !== "") return "links with credentials can't be fetched";
|
|
66
|
+
if (url.port !== "") return "only the standard https port is allowed";
|
|
67
|
+
const host = url.hostname.startsWith("[") ? url.hostname.slice(1, -1) : url.hostname;
|
|
68
|
+
if (host === "") return "the link has no host";
|
|
69
|
+
if (isIP(host) !== 0 && !isPublicAddress(host)) return `${host} is not on the public internet`;
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
type Hop =
|
|
74
|
+
| { readonly kind: "redirect"; readonly location: string | undefined }
|
|
75
|
+
| { readonly kind: "done"; readonly status: 200 | 304; readonly headers: IncomingHttpHeaders; readonly body: Buffer }
|
|
76
|
+
| { readonly kind: "failed"; readonly error: string };
|
|
77
|
+
|
|
78
|
+
function failure(cause: unknown, deadline: AbortSignal): string {
|
|
79
|
+
if (deadline.aborted) return "the request timed out";
|
|
80
|
+
if (cause instanceof Refused) return cause.message;
|
|
81
|
+
const code = typeof cause === "object" && cause !== null && "code" in cause ? String(cause.code) : "";
|
|
82
|
+
if (code === "ENOTFOUND" || code === "EAI_AGAIN") return "the host could not be found";
|
|
83
|
+
if (code === "ECONNREFUSED" || code === "ECONNRESET" || code === "EHOSTUNREACH") return "the host could not be reached";
|
|
84
|
+
if (code.startsWith("ERR_TLS") || code.includes("CERT")) return "the host's certificate was not accepted";
|
|
85
|
+
return cause instanceof Error ? cause.message : String(cause);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Undoes a Content-Encoding a server applied despite `Accept-Encoding: identity`, within the size cap. */
|
|
89
|
+
function decode(body: Buffer, encoding: string | undefined, maxBytes: number): Buffer {
|
|
90
|
+
const options = { maxOutputLength: maxBytes };
|
|
91
|
+
switch ((encoding ?? "identity").trim().toLowerCase()) {
|
|
92
|
+
case "identity":
|
|
93
|
+
case "":
|
|
94
|
+
return body;
|
|
95
|
+
case "gzip":
|
|
96
|
+
case "x-gzip":
|
|
97
|
+
return gunzipSync(body, options);
|
|
98
|
+
case "deflate":
|
|
99
|
+
return inflateSync(body, options);
|
|
100
|
+
case "br":
|
|
101
|
+
return brotliDecompressSync(body, options);
|
|
102
|
+
default:
|
|
103
|
+
throw new Error(`unsupported content encoding ${encoding}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function hop(url: URL, options: GetOptions, deadline: AbortSignal): Promise<Hop> {
|
|
108
|
+
return new Promise((resolve) => {
|
|
109
|
+
let settled = false;
|
|
110
|
+
const settle = (result: Hop) => {
|
|
111
|
+
if (!settled) {
|
|
112
|
+
settled = true;
|
|
113
|
+
resolve(result);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const req = request(
|
|
117
|
+
url.href,
|
|
118
|
+
{
|
|
119
|
+
method: "GET",
|
|
120
|
+
agent,
|
|
121
|
+
lookup: publicLookup,
|
|
122
|
+
signal: deadline,
|
|
123
|
+
timeout: IDLE_MS,
|
|
124
|
+
headers: {
|
|
125
|
+
"user-agent": USER_AGENT,
|
|
126
|
+
accept: options.accept,
|
|
127
|
+
"accept-encoding": "identity",
|
|
128
|
+
...options.headers,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
(res) => {
|
|
132
|
+
const status = res.statusCode ?? 0;
|
|
133
|
+
if (status >= 300 && status < 400 && status !== 304) {
|
|
134
|
+
res.resume();
|
|
135
|
+
settle({ kind: "redirect", location: res.headers.location });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (status !== 200 && status !== 304) {
|
|
139
|
+
res.resume();
|
|
140
|
+
settle({ kind: "failed", error: `the server answered ${status}` });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const declared = Number(res.headers["content-length"]);
|
|
144
|
+
const tooLarge = `the response is over the ${Math.round(options.maxBytes / 1024 / 1024)} MB limit`;
|
|
145
|
+
if (Number.isFinite(declared) && declared > options.maxBytes) {
|
|
146
|
+
res.destroy();
|
|
147
|
+
settle({ kind: "failed", error: tooLarge });
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const chunks: Buffer[] = [];
|
|
151
|
+
let size = 0;
|
|
152
|
+
res.on("data", (chunk: Buffer) => {
|
|
153
|
+
size += chunk.length;
|
|
154
|
+
if (size > options.maxBytes) {
|
|
155
|
+
res.destroy();
|
|
156
|
+
settle({ kind: "failed", error: tooLarge });
|
|
157
|
+
} else {
|
|
158
|
+
chunks.push(chunk);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
res.on("end", () => {
|
|
162
|
+
try {
|
|
163
|
+
const body = decode(Buffer.concat(chunks), res.headers["content-encoding"], options.maxBytes);
|
|
164
|
+
settle({ kind: "done", status: status === 304 ? 304 : 200, headers: res.headers, body });
|
|
165
|
+
} catch (cause) {
|
|
166
|
+
settle({ kind: "failed", error: failure(cause, deadline) });
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
res.on("error", (cause) => settle({ kind: "failed", error: failure(cause, deadline) }));
|
|
170
|
+
res.on("close", () => settle({ kind: "failed", error: "the connection closed early" }));
|
|
171
|
+
},
|
|
172
|
+
);
|
|
173
|
+
req.on("timeout", () => req.destroy(new Error("the host stopped responding")));
|
|
174
|
+
req.on("error", (cause) => settle({ kind: "failed", error: failure(cause, deadline) }));
|
|
175
|
+
req.end();
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** GETs `input` under the rules above. */
|
|
180
|
+
export async function safeGet(input: string, options: GetOptions): Promise<Fetched> {
|
|
181
|
+
let url: URL;
|
|
182
|
+
try {
|
|
183
|
+
url = new URL(input);
|
|
184
|
+
} catch {
|
|
185
|
+
return { ok: false, error: "the link is not a valid URL" };
|
|
186
|
+
}
|
|
187
|
+
const controller = new AbortController();
|
|
188
|
+
const timer = setTimeout(() => controller.abort(), options.timeoutMs);
|
|
189
|
+
const deadline = controller.signal;
|
|
190
|
+
try {
|
|
191
|
+
for (let redirects = 0; redirects <= MAX_REDIRECTS; redirects += 1) {
|
|
192
|
+
const refused = refusal(url);
|
|
193
|
+
if (refused !== null) return { ok: false, error: refused };
|
|
194
|
+
const result = await hop(url, options, deadline);
|
|
195
|
+
if (result.kind === "failed") return { ok: false, error: result.error };
|
|
196
|
+
if (result.kind === "done") {
|
|
197
|
+
return { ok: true, status: result.status, url: url.href, headers: result.headers, body: result.body };
|
|
198
|
+
}
|
|
199
|
+
if (result.location === undefined) return { ok: false, error: "a redirect had no destination" };
|
|
200
|
+
try {
|
|
201
|
+
url = new URL(result.location, url);
|
|
202
|
+
} catch {
|
|
203
|
+
return { ok: false, error: "a redirect pointed at an invalid URL" };
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return { ok: false, error: "too many redirects" };
|
|
207
|
+
} finally {
|
|
208
|
+
clearTimeout(timer);
|
|
209
|
+
}
|
|
210
|
+
}
|