Skip to content
This repository was archived by the owner on Oct 4, 2025. It is now read-only.

Commit bded247

Browse files
committed
ts conversion, first pass
1 parent 5082ccb commit bded247

File tree

11 files changed

+384
-319
lines changed

11 files changed

+384
-319
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616
"src/*"
1717
],
1818
"dependencies": {
19-
"@ipld/car": "^5.3.1",
20-
"@ipld/unixfs": "^3.0.0",
21-
"@webrecorder/wabac": "^2.18.1",
19+
"@webrecorder/wabac": "^2.20.0-beta.1",
2220
"auto-js-ipfs": "^2.3.0",
2321
"client-zip": "^2.3.0",
2422
"hash-wasm": "^4.9.0",
2523
"idb": "^7.1.1",
26-
"p-queue": "^7.3.4",
2724
"uuid": "^9.0.0",
28-
"warcio": "^2.2.1"
25+
"warcio": "^2.3.1"
2926
},
3027
"scripts": {
3128
"build": "webpack --mode production",
@@ -34,8 +31,12 @@
3431
"lint": "eslint ./src webpack.config.cjs"
3532
},
3633
"devDependencies": {
34+
"@types/uuid": "^10.0.0",
3735
"eslint": "^8.28.0",
3836
"raw-loader": "^4.0.2",
37+
"ts-loader": "^9.5.1",
38+
"tsconfig-paths-webpack-plugin": "^4.1.0",
39+
"typescript": "^5.5.4",
3940
"webpack": "^5.91.0",
4041
"webpack-cli": "^5.1.4"
4142
},

src/api.js renamed to src/api.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { API } from "@webrecorder/wabac/src/api.js";
2-
import { tsToDate } from "@webrecorder/wabac/src/utils.js";
1+
import { API, SWCollections, tsToDate } from "@webrecorder/wabac/swlib";
32

43
import { Downloader } from "./downloader.js";
54
import { Signer } from "./keystore.js";
@@ -12,14 +11,17 @@ const DEFAULT_SOFTWARE_STRING = `Webrecorder ArchiveWeb.page ${__AWP_VERSION__},
1211
// ===========================================================================
1312
class ExtAPI extends API
1413
{
15-
constructor(collections, {softwareString = "", replaceSoftwareString = false} = {}) {
14+
softwareString = "";
15+
uploading = new Map();
16+
17+
constructor(collections: SWCollections, {softwareString = "", replaceSoftwareString = false} = {}) {
1618
super(collections);
1719
this.softwareString = replaceSoftwareString ? softwareString : softwareString + DEFAULT_SOFTWARE_STRING;
1820

1921
this.uploading = new Map();
2022
}
2123

22-
get routes() {
24+
override get routes(): Record<string, string | [string, string]> {
2325
return {
2426
...super.routes,
2527
"downloadPages": "c/:coll/dl",
@@ -43,7 +45,7 @@ class ExtAPI extends API
4345
return {softwareString, signer};
4446
}
4547

46-
async handleApi(request, params, event) {
48+
override async handleApi(request: Request, params: any, event: FetchEvent){
4749
switch (params._route) {
4850
case "downloadPages":
4951
return await this.handleDownload(params);
@@ -67,10 +69,12 @@ class ExtAPI extends API
6769
return await this.getPublicKey();
6870

6971
case "ipfsAdd":
70-
return await this.startIpfsAdd(event, request, params.coll);
72+
//return await this.startIpfsAdd(event, request, params.coll);
73+
return {};
7174

7275
case "ipfsRemove":
73-
return await this.ipfsRemove(request, params.coll);
76+
//return await this.ipfsRemove(request, params.coll);
77+
return {};
7478

7579
case "ipfsDaemonUrl":
7680
return await this.setIPFSDaemonUrlFromBody(request);

src/downloader.js renamed to src/downloader.ts

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import { Deflate } from "pako";
55
import { v5 as uuidv5 } from "uuid";
66

77
import { createSHA256 } from "hash-wasm";
8+
import { type IHasher } from "hash-wasm/dist/lib/WASMInterface.js";
89

910
import { getSurt, WARCRecord, WARCSerializer } from "warcio";
1011

11-
import { getTSMillis, getStatusText, digestMessage } from "@webrecorder/wabac/src/utils.js";
12+
import { getTSMillis, getStatusText, digestMessage } from "@webrecorder/wabac";
13+
import { ArchiveDB, ResourceEntry } from "@webrecorder/wabac/swlib";
14+
import { Signer } from "./keystore";
15+
import { Collection } from "../../wabac.js/dist/types/collection";
1216

1317

1418
// ===========================================================================
@@ -28,11 +32,11 @@ const encoder = new TextEncoder();
2832

2933
const EMPTY = new Uint8Array([]);
3034

31-
async function* getPayload(payload) {
35+
async function* getPayload(payload: Uint8Array) {
3236
yield payload;
3337
}
3438

35-
async function* hashingGen(gen, stats, hasher, sizeCallback, zipMarker) {
39+
async function* hashingGen(gen: AsyncIterable<Uint8Array | string>, stats: any, hasher: IHasher, sizeCallback: any, zipMarker: Uint8Array) {
3640
stats.size = 0;
3741

3842
hasher.init();
@@ -61,31 +65,89 @@ async function* hashingGen(gen, stats, hasher, sizeCallback, zipMarker) {
6165
stats.hash = hasher.digest("hex");
6266
}
6367

68+
type DownloaderOpts = {
69+
coll: Collection;
70+
format: string;
71+
filename?: string;
72+
pageList?: string[];
73+
signer?: Signer;
74+
softwareString?: string;
75+
gzip?: boolean;
76+
uuidNamespace?: string;
77+
markers?: Record<string, Uint8Array>;
78+
};
79+
6480
// ===========================================================================
6581
class Downloader
6682
{
67-
constructor({coll, format = "wacz", filename = null, pageList = null, signer = null,
68-
softwareString = null, gzip = true, uuidNamespace = null, markers = null}) {
83+
db: ArchiveDB;
84+
pageList: string[] | null;
85+
collId: string;
86+
metadata: Record<string, string>;
87+
gzip: boolean;
88+
89+
markers: Record<string, Uint8Array>;
90+
warcName: string;
91+
alreadyDecoded: boolean;
92+
93+
softwareString: string;
94+
uuidNamespace: string;
95+
96+
createdDateDt: Date;
97+
createdDate: string;
98+
modifiedDate: string | null;
99+
100+
format: string;
101+
warcVersion: string;
102+
103+
digestOpts: {
104+
algo: string;
105+
prefix: string;
106+
base32?: boolean;
107+
};
108+
109+
filename: string;
110+
111+
signer: Signer | null;
112+
113+
offset = 0;
114+
firstResources = [];
115+
textResources = [];
116+
cdxjLines = [];
117+
118+
// compressed index (idx) entries
119+
indexLines = [];
120+
121+
digestsVisted = {};
122+
fileHasher = null;
123+
recordHasher = null;
124+
125+
datapackageDigest = null;
126+
127+
fileStats = [];
128+
129+
constructor({coll, format = "wacz", filename, pageList, signer,
130+
softwareString, gzip = true, uuidNamespace, markers} : DownloaderOpts) {
69131

70132
this.db = coll.store;
71-
this.pageList = pageList;
133+
this.pageList = pageList || [];
72134
this.collId = coll.name;
73-
this.metadata = coll.config.metadata;
135+
this.metadata = coll.config["metadata"];
74136
this.gzip = gzip;
75137

76138
this.markers = markers || {};
77139

78140
this.warcName = this.gzip ? "data.warc.gz" : "data.warc";
79141

80-
this.alreadyDecoded = !coll.config.decode && !coll.config.loadUrl;
142+
this.alreadyDecoded = !coll.config["decode"] && !coll.config["loadUrl"];
81143

82144
this.softwareString = softwareString || "ArchiveWeb.page";
83145

84146
this.uuidNamespace = uuidNamespace || DEFAULT_UUID_NAMESPACE;
85147

86-
this.createdDateDt = new Date(coll.config.ctime);
148+
this.createdDateDt = new Date(coll.config["ctime"]);
87149
this.createdDate = this.createdDateDt.toISOString();
88-
this.modifiedDate = coll.config.metadata.mtime ? new Date(coll.config.metadata.mtime).toISOString() : null;
150+
this.modifiedDate = coll.config["metadata"].mtime ? new Date(coll.config["metadata"].mtime).toISOString() : null;
89151

90152
this.format = format;
91153
this.warcVersion = (format === "warc1.0") ? "WARC/1.0" : "WARC/1.1";
@@ -96,33 +158,17 @@ class Downloader
96158
this.digestOpts = {algo: "sha-256", prefix: "sha256:"};
97159
}
98160

99-
this.filename = filename;
100-
101161
// determine filename from title, if it exists
102-
if (!this.filename && coll.config.metadata.title) {
103-
this.filename = encodeURIComponent(coll.config.metadata.title.toLowerCase().replace(/\s/g, "-"));
162+
if (!filename && coll.config["metadata"].title) {
163+
filename = encodeURIComponent(coll.config["metadata"].title.toLowerCase().replace(/\s/g, "-"));
104164
}
105165

106-
if (!this.filename) {
107-
this.filename = "webarchive";
166+
if (!filename) {
167+
filename = "webarchive";
108168
}
169+
this.filename = filename;
109170

110-
this.offset = 0;
111-
this.firstResources = [];
112-
this.textResources = [];
113-
this.cdxjLines = [];
114-
115-
// compressed index (idx) entries
116-
this.indexLines = [];
117-
118-
this.digestsVisted = {};
119-
this.fileHasher = null;
120-
this.recordHasher = null;
121-
122-
this.datapackageDigest = null;
123-
this.signer = signer;
124-
125-
this.fileStats = [];
171+
this.signer = signer || null;
126172
}
127173

128174
download(sizeCallback = null) {
@@ -139,7 +185,7 @@ class Downloader
139185
}
140186
}
141187

142-
downloadWARC(filename, sizeCallback = null) {
188+
downloadWARC(filename: string, sizeCallback = null) {
143189
filename = (filename || "webarchive").split(".")[0] + ".warc";
144190

145191
const dl = this;
@@ -160,19 +206,19 @@ class Downloader
160206
return resp;
161207
}
162208

163-
async loadResourcesBlock(start = []) {
164-
return await this.db.db.getAll("resources", IDBKeyRange.lowerBound(start, true), RESOURCE_BATCH_SIZE);
209+
async loadResourcesBlock(start : [string, number] | [] = []) {
210+
return await this.db.db!.getAll("resources", IDBKeyRange.lowerBound(start, true), RESOURCE_BATCH_SIZE);
165211
}
166212

167-
async* iterResources(resources) {
168-
let start = [];
213+
async* iterResources(resources: ResourceEntry[]) {
214+
let start : [string, number] | [] = [];
169215
//let count = 0;
170216

171217
while (resources.length) {
172-
const last = resources[resources.length - 1];
218+
const last : ResourceEntry = resources[resources.length - 1] as ResourceEntry;
173219

174220
if (this.pageList) {
175-
resources = resources.filter((res) => this.pageList.includes(res.pageId));
221+
resources = resources.filter((res) => this.pageList!.includes(res.pageId || ""));
176222
}
177223
//count += resources.length;
178224
yield* resources;
@@ -185,7 +231,7 @@ class Downloader
185231
// }
186232
}
187233

188-
async queueWARC(controller, filename, sizeCallback) {
234+
async queueWARC(controller, filename: string, sizeCallback: any) {
189235
this.firstResources = await this.loadResourcesBlock();
190236

191237
for await (const chunk of this.generateWARC(filename)) {
File renamed without changes.

src/ipfsutils.js renamed to src/ipfsutils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Downloader } from "./downloader.js";
22

33
import { create as createAutoIPFS } from "auto-js-ipfs";
44

5-
import * as UnixFS from "@ipld/unixfs";
6-
import { CarWriter } from "@ipld/car/writer";
7-
import Queue from "p-queue";
5+
//import * as UnixFS from "@ipld/unixfs";
6+
//import { CarWriter } from "@ipld/car/writer";
7+
//import Queue from "p-queue";
88

99
// eslint-disable-next-line no-undef
1010
const autoipfsOpts = {web3StorageToken: __WEB3_STORAGE_TOKEN__};

0 commit comments

Comments
 (0)