Repository Public
1#!/usr/bin/env node2/**3 * =============================================================================4 * SP BOUCHER | SOFTWARE & RESEARCH5 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING6 * =============================================================================7 *8 * Project : SPIDER — Universal Web Intelligence Engine9 * File : sdk/typescript/scripts/live.mjs10 * Module : Sdk11 * Repository : https://git.spboucher.ai/spider12 * Project Website : Not specified13 * Created : 2026-12-0414 * Last Modified : 2026-12-0415 *16 * Author : Simon-Pierre Boucher17 * Position : Professor of Finance18 * Institution : Université du Québec en Outaouais (UQO)19 * Organization : SP Boucher20 * Website : https://spboucher.ai21 * Git : https://git.spboucher.ai22 *23 * Preferred Contact: contact@spboucher.ai24 * Development : dev@spboucher.ai25 * Support : support@spboucher.ai26 * Security/Admin : admin@spboucher.ai27 *28 * Copyright (c) 202629 * Simon-Pierre Boucher30 * All rights reserved where applicable. This file is governed by the31 * applicable project license, copyright notices and relevant law.32 * Existing third-party rights and notices remain unaffected.33 *34 * License : ISC35 *36 * Do not embed credentials, private keys, access tokens,37 * certificates or confidential personal information.38 *39 * =============================================================================40 * END OF SP BOUCHER SOURCE HEADER41 * =============================================================================42 */4344// Live check of the compiled SDK against a running SPIDER gateway. Prints one JSON document.45// SPIDER_URL=… SPIDER_API_KEY=… node scripts/live.mjs <page URL> [<crawl URL> <include regex>]46// Collect only sources you are authorised to collect.47import { createHash } from "node:crypto";4849import { MandatoryPolicyError, NotFoundError, Spider, USER_AGENT } from "../dist/index.js";5051const [pageUrl, crawlUrl, include] = process.argv.slice(2);52const { SPIDER_URL, SPIDER_API_KEY } = process.env;53if (!SPIDER_URL || !SPIDER_API_KEY || !pageUrl) {54 console.error("usage: SPIDER_URL=… SPIDER_API_KEY=… node scripts/live.mjs <page URL> [<crawl URL> <include regex>]");55 process.exit(2);56}5758const spider = new Spider({ baseUrl: SPIDER_URL, apiKey: SPIDER_API_KEY, userAgent: "live-check/1.0" });59const out = { user_agent: USER_AGENT, health: (await spider.health()).status };6061// scrape and wait, following the event stream62const scraped = await spider.scrapeAndWait({ url: pageUrl, render: "http" }, { useEvents: true, timeoutMs: 60_000 });63const item = scraped.results[0];64out.scrape = {65 job_id: scraped.job.job_id, status: scraped.job.status, results: scraped.results.length,66 url: item?.url, http_status: item?.http_status, markdown_chars: item?.content?.markdown?.length ?? 0,67};6869// artifacts, verified against X-Spider-Sha256 and against the hash listed in the result70out.artifacts = [];71for (const a of item?.artifacts ?? []) {72 const bytes = await spider.getArtifact(a.artifact_id, { expectedSha256: a.sha256 });73 out.artifacts.push({ kind: a.kind, bytes: bytes.length, sha256_ok: createHash("sha256").update(bytes).digest("hex") === a.sha256 });74}7576// events of a second job, read explicitly77const second = await spider.scrape({ url: pageUrl, render: "http" });78out.events = [];79for await (const ev of spider.streamEvents(second.job_id)) out.events.push(ev.type);8081// the same Idempotency-Key and body twice: one job82const key = `live-${scraped.job.job_id}`;83const first = await spider.scrape({ url: pageUrl, render: "http", metadata: { run: "idempotency" } }, { idempotencyKey: key });84const replay = await spider.scrape({ url: pageUrl, render: "http", metadata: { run: "idempotency" } }, { idempotencyKey: key });85out.idempotency = { same_job: first.job_id === replay.job_id, replayed: replay.idempotent_replay === true };8687// typed errors88out.errors = {};89try {90 await spider.request("POST", "/v1/scrape", { body: { url: pageUrl, policy: { respect_robots_txt: false } } });91} catch (e) {92 out.errors.mandatory_policy = { typed: e instanceof MandatoryPolicyError, status: e.status, code: e.code, request_id: Boolean(e.requestId) };93}94try {95 await spider.getJob("00000000-0000-4000-8000-000000000000");96} catch (e) {97 out.errors.not_found = { typed: e instanceof NotFoundError, status: e.status, code: e.code };98}99100// a small crawl, iterated two results at a time101if (crawlUrl) {102 const job = await spider.crawl({103 url: crawlUrl, render: "http", use_sitemaps: false, budgets: { max_pages: 6, max_depth: 1 },104 policy: include ? { include_paths: [include] } : {}, extract: { formats: ["markdown", "text", "json"] },105 });106 const final = await spider.waitForJob(job.job_id, { timeoutMs: 90_000, pollIntervalMs: 100 });107 const urls = [];108 for await (const r of spider.iterResults(job.job_id, { pageSize: 2, includeContent: false })) urls.push(r.url);109 out.crawl = { job_id: job.job_id, status: final.status, urls };110}111112console.log(JSON.stringify(out));113