@blazecrawl/sdk 0.1.2
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/README.md +30 -0
- package/package.json +31 -0
- package/src/index.js +90 -0
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @blazecrawl/sdk
|
|
2
|
+
|
|
3
|
+
Node.js API client for a running BlazeCrawl Core server. Requires Node.js 18+.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @blazecrawl/sdk@0.1.2
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Authenticate and scrape
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { BlazeCrawl } from "@blazecrawl/sdk";
|
|
15
|
+
|
|
16
|
+
const client = new BlazeCrawl({
|
|
17
|
+
apiKey: "blz_local_...",
|
|
18
|
+
baseUrl: "http://127.0.0.1:8000",
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
const document = await client.scrape("https://example.com");
|
|
23
|
+
console.log(document.markdown);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error("BlazeCrawl request failed:", error);
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Use `map()` to discover URLs. `crawl()` starts a job; poll the returned job
|
|
30
|
+
reference through the client until completion.
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blazecrawl/sdk",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Node.js SDK for BlazeCrawl Core (self-hosted web-data engine).",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/danishxsethi/blazecrawl.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/danishxsethi/blazecrawl#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/danishxsethi/blazecrawl/issues"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"provenance": true
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "./src/index.js",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./src/index.js"
|
|
22
|
+
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=18"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "node --test test/*.test.mjs"
|
|
28
|
+
},
|
|
29
|
+
"keywords": ["web-scraping", "crawler", "markdown", "llm"],
|
|
30
|
+
"files": ["src"]
|
|
31
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BlazeCrawl Node.js SDK (OSS).
|
|
3
|
+
*
|
|
4
|
+
* Zero-dependency client using the global fetch (Node 18+).
|
|
5
|
+
*
|
|
6
|
+
* import { BlazeCrawl } from "@blazecrawl/sdk";
|
|
7
|
+
* const bc = new BlazeCrawl({ apiKey: "blz_local_..." });
|
|
8
|
+
* const doc = await bc.scrape("https://example.com");
|
|
9
|
+
* console.log(doc.markdown);
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export class BlazeCrawlError extends Error {
|
|
13
|
+
constructor(message, statusCode, payload) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "BlazeCrawlError";
|
|
16
|
+
this.statusCode = statusCode;
|
|
17
|
+
this.payload = payload;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class BlazeCrawl {
|
|
22
|
+
constructor({ apiKey, baseUrl, timeout } = {}) {
|
|
23
|
+
this.apiKey = apiKey || process.env.BLAZECRAWL_API_KEY || null;
|
|
24
|
+
this.baseUrl = (baseUrl || process.env.BLAZECRAWL_API_URL || "http://127.0.0.1:8000").replace(/\/+$/, "");
|
|
25
|
+
this.timeout = timeout ?? 120000;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async _request(method, path, body) {
|
|
29
|
+
const headers = { "Content-Type": "application/json" };
|
|
30
|
+
if (this.apiKey) headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
31
|
+
const controller = new AbortController();
|
|
32
|
+
const timer = setTimeout(() => controller.abort(), this.timeout);
|
|
33
|
+
let resp;
|
|
34
|
+
try {
|
|
35
|
+
resp = await fetch(`${this.baseUrl}${path}`, {
|
|
36
|
+
method,
|
|
37
|
+
headers,
|
|
38
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
39
|
+
signal: controller.signal,
|
|
40
|
+
});
|
|
41
|
+
} finally {
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
}
|
|
44
|
+
const text = await resp.text();
|
|
45
|
+
let payload;
|
|
46
|
+
try {
|
|
47
|
+
payload = text ? JSON.parse(text) : {};
|
|
48
|
+
} catch {
|
|
49
|
+
payload = { error: text };
|
|
50
|
+
}
|
|
51
|
+
if (resp.status >= 400) {
|
|
52
|
+
const msg = payload?.detail?.message || payload?.error || resp.statusText;
|
|
53
|
+
throw new BlazeCrawlError(msg, resp.status, payload);
|
|
54
|
+
}
|
|
55
|
+
return payload;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Scrape a URL. Returns the `data` object (markdown, html, links, ...). */
|
|
59
|
+
async scrape(url, options = {}) {
|
|
60
|
+
const res = await this._request("POST", "/v1/scrape", { url, ...options });
|
|
61
|
+
return res.data ?? res;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Map a site. Returns { success, urls, count, ... }. */
|
|
65
|
+
async map(url, options = {}) {
|
|
66
|
+
return this._request("POST", "/v1/map", { url, ...options });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Start a crawl; returns the job (with job_id) or polls to completion. */
|
|
70
|
+
async crawl(url, { wait = true, pollInterval = 1000, ...options } = {}) {
|
|
71
|
+
const job = await this._request("POST", "/v1/crawl", { url, ...options });
|
|
72
|
+
if (!wait) return job;
|
|
73
|
+
const id = job.job_id;
|
|
74
|
+
for (;;) {
|
|
75
|
+
const st = await this._request("GET", `/v1/crawl/${id}`);
|
|
76
|
+
if (["completed", "failed", "cancelled"].includes(st.status)) return st;
|
|
77
|
+
await new Promise((r) => setTimeout(r, pollInterval));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async crawlStatus(jobId) {
|
|
82
|
+
return this._request("GET", `/v1/crawl/${jobId}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async health() {
|
|
86
|
+
return this._request("GET", "/health");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default BlazeCrawl;
|