Skip to content

One-file Scrapers

A Scraper whose Definition, Implementation and Release fit in one TypeScript file needs no project. scwrap check typechecks it on the instance and scwrap deploy builds and deploys it there.

A Scraper that only needs effect and the @scwrap SDKs can live in one .ts file: its Definition, its Implementation and its Release as the default export. The instance typechecks and builds that file itself (Builds), so the file and a deployer token are all you need. No package.json, workspace, tsconfig.json or local toolchain.

Example (hn-top.ts, from hn-top.ts)

ts
import { Deployment } from "@scwrap/deploy";
import {
  Blocked,
  Download,
  RateLimited,
  Scraper,
  SchemaDrift,
  UpstreamDown,
} from "@scwrap/domain";
import { Effect, Schema } from "effect";
import { FetchHttpClient } from "effect/http";

const Story = Schema.Struct({
  id: Schema.Int,
  title: Schema.NonEmptyString,
  url: Schema.optionalKey(Schema.String),
});

const HnTop = Scraper.make("hn-top", {
  error: Schema.Union([Blocked, RateLimited, SchemaDrift, UpstreamDown]),
  input: Schema.Struct({
    limit: Schema.Int.check(Schema.isBetween({ maximum: 30, minimum: 1 })),
  }),
  output: Story,
});

const api = "https://hacker-news.firebaseio.com/v0";

const HnTopLive = Scraper.layer(HnTop, ({ limit }) =>
  Download.json(`${api}/topstories.json`, Schema.Array(Schema.Int)).pipe(
    Effect.flatMap((ids) =>
      Effect.forEach(
        ids.slice(0, limit),
        (id) => Download.json(`${api}/item/${id}.json`, Story),
        { concurrency: 5 }
      )
    )
  )
);

export default Deployment.worker({
  implementation: HnTopLive,
  module: import.meta.url,
  scraper: HnTop,
  services: FetchHttpClient.layer,
});

Check it ​

sh
export SCWRAP_INSTANCE_URL=https://scwrap.example.com
export SCWRAP_INSTANCE_DEPLOYER_TOKEN=…
pnpm dlx @scwrap/deploy@<version> check hn-top.ts

<version> is the SDK version your instance runs (Versions an instance accepts). check sends the file to the instance's POST /builds and waits for the build. The instance typechecks it against its own SDKs with the same Effect diagnostics a project gets from tsconfig.base.json, so the compile-time checks still hold:

  • An Implementation that can fail with an error its Definition does not declare does not compile.
  • A Release whose services miss a Capability, or whose provider does not cover a location, does not compile (The instance supplies nothing).

It prints hn-top.ts typechecks and builds as hn-top <revision>, or fails with each diagnostic on the file's own lines:

text
hn-top.ts failed to build:
hn-top.ts(42,3): error TS377034: Missing 'HttpClient' in the expected Layer context. effect(missingLayerContext)

A cold build takes up to a minute, a warm one about 10 seconds. check deploys nothing.

Deploy it ​

sh
pnpm dlx @scwrap/deploy@<version> deploy hn-top.ts

deploy <file> sends the same build with deploy: true: the instance deploys the Release it built, and the CLI keeps the request in a journal for retry. A named deployer token limited to the Scraper's ID is enough. Grants and settings are keyed by Scraper ID on the instance, as for any other Release (Grants).

npx @scwrap/deploy@<version> works the same way as pnpm dlx.

What the file may import ​

effect, its subpaths, and the seven SDKs: @scwrap/domain, @scwrap/deploy, @scwrap/browser, @scwrap/platform-apify, @scwrap/captcha-templates, @scwrap/ai-cloudflare and @scwrap/client, with their subpaths. Beyond those, the instance installs a curated list of npm packages at one pinned version each, today cheerio 1.2.0 for parsing HTML (What a build checks); pin the same version in your own package.json so your tests run against it. The file may also import other .ts files of your own by relative path, and packages of your workspace whose types point at .ts source: the CLI sends those files along and rewrites a workspace package import to a relative one (What the CLI sends). Any other package fails the build:

text
hn-top.ts failed to build:
Imports jpeg-js. A Scraper built on the instance imports only effect, the @scwrap SDKs (…), the curated packages (cheerio 1.2.0) and its own files.

The Worker release rules apply as well: an Effect subpath that scwrap-sdk.mjs does not share fails the build (Supported Effect imports).

Testing it ​

Tests need Vitest, so they live in a project. To test a one-file Scraper with FakeSite, export its Definition and Implementation from the file and import them from a test file next to it.

Source ​

  • client.ts: collectSource, the files the CLI sends, and the build polling
  • cli.ts: check and deploy <file>
  • build-check.ts: the import check, the typecheck and the bundle on the instance