Skip to content

Commit

Permalink
type config on Collection
Browse files Browse the repository at this point in the history
  • Loading branch information
ikreymer committed Sep 8, 2024
1 parent 327fece commit 2480042
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 28 deletions.
19 changes: 5 additions & 14 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getAdBlockCSSResponse } from "./adblockcss";
import { notFoundByTypeResponse } from "./notfound";
import { type ArchiveDB } from "./archivedb";
import { type ArchiveRequest } from "./request";
import { type CollConfig } from "./types";

const DEFAULT_CSP =
"default-src 'unsafe-eval' 'unsafe-inline' 'self' data: blob: mediastream: ws: wss: ; form-action 'self'";
Expand All @@ -30,9 +31,7 @@ export class Collection {
name: string;
store: ArchiveDB;

// [TODO]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: Record<string, any>;
config: CollConfig;
// [TODO]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
metadata: Record<string, any>;
Expand Down Expand Up @@ -75,11 +74,11 @@ export class Collection {
this.name = name;
this.store = store;
this.config = config;
// @ts-expect-error [TODO] - TS4111 - Property 'metadata' comes from an index signature, so it must be accessed with ['metadata']. | TS4111 - Property 'metadata' comes from an index signature, so it must be accessed with ['metadata'].
this.metadata = this.config.metadata ? this.config.metadata : {};

// @ts-expect-error [TODO] - TS4111 - Property 'extraConfig' comes from an index signature, so it must be accessed with ['extraConfig'].
const extraConfig = { ...defaultConfig, ...this.config.extraConfig };
// [TODO]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const extraConfig: any = { ...defaultConfig, ...this.config.extraConfig };

this.injectScripts = extraConfig.injectScripts || [];
this.noRewritePrefixes = extraConfig.noRewritePrefixes || null;
Expand Down Expand Up @@ -107,7 +106,6 @@ export class Collection {
this.prefix = prefixes.main;

// support root collection hashtag nav
// @ts-expect-error [TODO] - TS4111 - Property 'root' comes from an index signature, so it must be accessed with ['root'].
if (this.config.root) {
this.isRoot = true;
} else {
Expand Down Expand Up @@ -254,7 +252,6 @@ export class Collection {
workerInsertFunc,
urlRewrite: !noRewrite,
contentRewrite: !noRewrite,
// @ts-expect-error [TODO] - TS4111 - Property 'decode' comes from an index signature, so it must be accessed with ['decode'].
decode: this.config.decode,
};

Expand Down Expand Up @@ -405,10 +402,8 @@ export class Collection {

if (this.baseFrameUrl && !this.baseFramePrefix) {
baseUrl = this.baseFrameUrl;
// @ts-expect-error [TODO] - TS4111 - Property 'sourceUrl' comes from an index signature, so it must be accessed with ['sourceUrl'].
} else if (!this.isRoot && this.config.sourceUrl) {
baseUrl = this.baseFramePrefix || "./";
// @ts-expect-error [TODO] - TS4111 - Property 'sourceUrl' comes from an index signature, so it must be accessed with ['sourceUrl'].
baseUrl += `?source=${this.config.sourceUrl}`;
}

Expand All @@ -429,11 +424,7 @@ export class Collection {

let content = "";

// @ts-expect-error [TODO] - TS4111 - Property 'topTemplateUrl' comes from an index signature, so it must be accessed with ['topTemplateUrl'].
if (this.config.topTemplateUrl) {
// @ts-expect-error [TODO] - TS4111 - Property 'topTemplateUrl' comes from an index signature, so it must be accessed with ['topTemplateUrl'].
// [TODO]
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const resp = await fetch(this.config.topTemplateUrl);
const topTemplate = await resp.text();
content = topTemplate
Expand Down
18 changes: 10 additions & 8 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ import {
AuthNeededError,
} from "./utils";
import { detectFileType, getKnownFileExtension } from "./detectfiletype";
import { type CollConfig, type ArchiveLoader, type DBStore, type WACZCollConfig } from "./types";
import {
type CollConfig,
type ArchiveLoader,
type DBStore,
type WACZCollConfig,
} from "./types";

// [TODO]
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
Expand All @@ -45,7 +50,6 @@ const interruptLoads: Record<string, () => void> = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(self as any).interruptLoads = interruptLoads;


export type LoadColl = {
name?: string;
type: string;
Expand Down Expand Up @@ -139,7 +143,7 @@ export class CollectionLoader {

async deleteColl(name: string) {
await this._init_db;
const data = await this.colldb!.get("colls", name) as LoadColl | null;
const data = (await this.colldb!.get("colls", name)) as LoadColl | null;
if (!data) {
return false;
}
Expand Down Expand Up @@ -233,7 +237,7 @@ export class CollectionLoader {
const decode = false;
const ctime = new Date().getTime();

const data : LoadColl = {
const data: LoadColl = {
name: id,
type,
config: {
Expand Down Expand Up @@ -640,7 +644,7 @@ export class WorkerLoader extends CollectionLoader {
config.loadUrl = loadUrl;
config.sourceUrl = file.sourceUrl;

let sourceName : string = file.name || file.sourceUrl;
let sourceName: string = file.name || file.sourceUrl;

// parse to strip out query, keep hash/fragment (if any)
try {
Expand All @@ -653,9 +657,7 @@ export class WorkerLoader extends CollectionLoader {
} catch (e) {
// ignore, keep sourceName as is
}
config.sourceName = sourceName.slice(
sourceName.lastIndexOf("/") + 1,
);
config.sourceName = sourceName.slice(sourceName.lastIndexOf("/") + 1);

config.size = typeof file.size === "number" ? file.size : null;
config.extra = file.extra;
Expand Down
4 changes: 0 additions & 4 deletions src/swmain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export class SWCollections extends WorkerLoader {
// @ts-expect-error [TODO] - TS2532 - Object is possibly 'undefined'. | TS4111 - Property 'extra' comes from an index signature, so it must be accessed with ['extra'].
this.colls[name].config.extra?.fileHandle
) {
// @ts-expect-error [TODO] - TS2532 - Object is possibly 'undefined'. | TS4111 - Property 'sourceUrl' comes from an index signature, so it must be accessed with ['sourceUrl'].
this._fileHandles[this.colls[name].config.sourceUrl] =
// @ts-expect-error [TODO] - TS2532 - Object is possibly 'undefined'. | TS4111 - Property 'extra' comes from an index signature, so it must be accessed with ['extra'].
this.colls[name].config.extra.fileHandle;
Expand Down Expand Up @@ -150,7 +149,6 @@ export class SWCollections extends WorkerLoader {
async updateMetadata(name: string, newMetadata: Record<string, string>) {
const metadata = await super.updateMetadata(name, newMetadata);
if (this.colls[name] && metadata) {
// @ts-expect-error [TODO] - TS2532 - Object is possibly 'undefined'. | TS4111 - Property 'metadata' comes from an index signature, so it must be accessed with ['metadata'].
this.colls[name].config.metadata = metadata;
this.colls[name].metadata = metadata;
}
Expand All @@ -173,12 +171,10 @@ export class SWCollections extends WorkerLoader {
updateDecode,
);
if (this.colls[name] && metadata) {
// @ts-expect-error [TODO] - TS2532 - Object is possibly 'undefined'. | TS4111 - Property 'metadata' comes from an index signature, so it must be accessed with ['metadata'].
this.colls[name].config.metadata = metadata;
this.colls[name].metadata = metadata;
}
if (updateDecode !== undefined && this.colls[name]) {
// @ts-expect-error [TODO] - TS2532 - Object is possibly 'undefined'. | TS4111 - Property 'decode' comes from an index signature, so it must be accessed with ['decode'].
this.colls[name].config.decode = updateDecode;
}
// [TODO]
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export type CollConfig = {
topTemplateUrl?: string;
// [TODO]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
metadata: Record<string, any>;
metadata?: Record<string, any>;

loadUrl?: string;

Expand All @@ -137,7 +137,6 @@ export type CollConfig = {
onDemand?: boolean;
};


export type WACZCollConfig = CollConfig & {
dbname: string;
noCache?: boolean;
Expand Down

0 comments on commit 2480042

Please sign in to comment.