@mercuryworkshop/scramjet 2.0.2-alpha → 2.0.6-alpha

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.
Files changed (52) hide show
  1. package/dist/scramjet-external.mjs +56 -0
  2. package/dist/scramjet.js +7 -9
  3. package/dist/scramjet.js.map +1 -1
  4. package/dist/scramjet.mjs +7 -9
  5. package/dist/scramjet.mjs.map +1 -1
  6. package/dist/scramjet.wasm +0 -0
  7. package/dist/scramjet_bundled.js +7 -9
  8. package/dist/scramjet_bundled.js.map +1 -1
  9. package/dist/scramjet_bundled.mjs +7 -9
  10. package/dist/scramjet_bundled.mjs.map +1 -1
  11. package/dist/temp-types-build/index.js +23 -0
  12. package/dist/temp-types-build/index.js.map +1 -0
  13. package/dist/types/Tap.d.ts +39 -0
  14. package/dist/types/client/client.d.ts +61 -37
  15. package/dist/types/client/events.d.ts +9 -37
  16. package/dist/types/client/helpers.d.ts +1 -1
  17. package/dist/types/client/location.d.ts +2 -2
  18. package/dist/types/client/shared/eval.d.ts +1 -1
  19. package/dist/types/client/shared/sourcemaps.d.ts +1 -1
  20. package/dist/types/client/shared/wrap.d.ts +3 -3
  21. package/dist/types/client/singletonbox.d.ts +2 -0
  22. package/dist/types/fetch/body.d.ts +3 -0
  23. package/dist/types/fetch/fetch.d.ts +7 -0
  24. package/dist/types/fetch/headers.d.ts +19 -0
  25. package/dist/types/fetch/parse.d.ts +23 -0
  26. package/dist/types/fetch/util.d.ts +7 -0
  27. package/dist/types/index.d.ts +2 -0
  28. package/dist/types/shared/cookie.d.ts +13 -5
  29. package/dist/types/shared/htmlRules.d.ts +3 -3
  30. package/dist/types/shared/index.d.ts +33 -5
  31. package/dist/types/shared/mime.d.ts +47 -0
  32. package/dist/types/shared/refresh.d.ts +7 -0
  33. package/dist/types/shared/rewriters/css.d.ts +3 -3
  34. package/dist/types/shared/rewriters/html.d.ts +32 -5
  35. package/dist/types/shared/rewriters/js.d.ts +4 -4
  36. package/dist/types/shared/rewriters/url.d.ts +19 -5
  37. package/dist/types/shared/rewriters/wasm.d.ts +2 -3
  38. package/dist/types/shared/rewriters/worker.d.ts +2 -2
  39. package/dist/types/shared/set-cookie-parser.d.ts +20 -0
  40. package/dist/types/shared/snapshot.d.ts +185 -0
  41. package/dist/types/shared/sniffEncoding.d.ts +65 -0
  42. package/dist/types/shared/util.d.ts +2 -0
  43. package/dist/types/symbols.d.ts +1 -2
  44. package/dist/types/types.d.ts +3 -50
  45. package/package.json +15 -13
  46. package/dist/a68dd7a5344f1722.wasm +0 -0
  47. package/dist/c34a4f083a11eae2.wasm +0 -0
  48. package/dist/types/fetch/index.d.ts +0 -80
  49. package/dist/types/index.js +0 -26
  50. package/dist/types/index.js.map +0 -1
  51. package/dist/types/shared/security/index.d.ts +0 -1
  52. package/dist/types/shared/security/siteTests.d.ts +0 -1
@@ -0,0 +1,39 @@
1
+ type Description = {
2
+ context?: object;
3
+ props?: object;
4
+ };
5
+ type Callback<T extends Description> = (context: T["context"], props: T["props"]) => void | Promise<void>;
6
+ export type TapOrder = {
7
+ /** Run before these plugins. */
8
+ before?: readonly string[];
9
+ /** Run after these plugins. */
10
+ after?: readonly string[];
11
+ };
12
+ type CallbackInfo<T extends Description> = {
13
+ callback: Callback<T>;
14
+ plugin: Plugin;
15
+ order: TapOrder;
16
+ };
17
+ type InternalHookDescription = {
18
+ tap: TapInternal;
19
+ key: string;
20
+ };
21
+ type TapInternal = {
22
+ callbacks: Record<string, CallbackInfo<Description>[]>;
23
+ };
24
+ export type TapInstance<T extends Record<string, Description>> = {
25
+ [K in keyof T]: T[K] & InternalHookDescription;
26
+ };
27
+ export declare class Plugin {
28
+ name: string;
29
+ readonly tapOrder: TapOrder;
30
+ constructor(name: string, tapOrder?: TapOrder);
31
+ tap<T extends Description>(hook: T, callback: Callback<T>, order?: TapOrder): void;
32
+ }
33
+ export declare class Tap {
34
+ static dispatch<T extends Description>(hook: T, context: T["context"], props: T["props"]): Promise<void[]>;
35
+ static tap<T extends Description>(hook: T, callback: Callback<T>, plugin?: Plugin, order?: TapOrder): void;
36
+ static create<T extends Record<string, Description>>(): TapInstance<T>;
37
+ static getTappers<T extends Description>(hook: T): Plugin[];
38
+ }
39
+ export {};
@@ -1,16 +1,20 @@
1
- type ScramjetFrame = any;
2
- import { BareCompatibleClient, ProxyTransport } from "@mercuryworkshop/proxy-transports";
3
- import { type URLMeta } from "../shared/rewriters/url";
4
- import { ScramjetContext } from "../shared";
1
+ import { BareCompatibleClient, ProxyTransport, RawHeaders } from "@mercuryworkshop/proxy-transports";
2
+ import { LifecycleHooks } from "@client/events";
3
+ import { RewriteUrlOptions, type URLMeta } from "@rewriters/url";
4
+ import { HtmlRewriterHooks, ScramjetContext, ScramjetHeaders } from "@/shared";
5
5
  import { SingletonBox } from "./singletonbox";
6
- import { ScramjetConfig } from "../types";
6
+ import { ScramjetConfig } from "@/types";
7
+ import { type CookieSyncEntry, type CookieSyncOptions, TrackedHistoryState } from "@/fetch";
8
+ import { AnyFunction } from "@/types";
9
+ import { _URL } from "@/shared/snapshot";
7
10
  export type ScramjetClientInit = {
8
11
  context: ScramjetContext;
9
12
  transport: ProxyTransport;
10
- sendSetCookie: (url: URL, cookie: string) => Promise<void>;
11
- shouldPassthroughWebsocket?: (url: string | URL) => boolean;
13
+ sendSetCookie: (cookies: CookieSyncEntry[], options?: CookieSyncOptions) => Promise<void>;
12
14
  shouldBlockMessageEvent?: (ev: MessageEvent) => boolean;
13
15
  hookSubcontext: (self: Self, frame?: HTMLIFrameElement) => ScramjetClient;
16
+ initHeaders: RawHeaders;
17
+ history: TrackedHistoryState[];
14
18
  };
15
19
  type NativeStore = {
16
20
  store: Record<string, any>;
@@ -22,40 +26,45 @@ type DescriptorStore = {
22
26
  get: (target: string, that: any) => any;
23
27
  set: (target: string, that: any, value: any) => void;
24
28
  };
25
- export type AnyFunction = Function;
29
+ type Traverse<O extends Record<any, any>, P extends string> = P extends `${infer K}.${infer R}` ? Traverse<O[K], R> : O[P];
30
+ type GlobalTraverse<P extends string> = Traverse<GlobalThis & Record<string, any>, P>;
31
+ type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 ? Y : N;
32
+ type ProxyApplyThis<T extends string> = unknown extends ThisParameterType<Extract<GlobalTraverse<T>, AnyFunction>> ? T extends `${infer ClassName}.prototype.${string}` ? GlobalTraverse<ClassName> extends {
33
+ prototype: infer Proto;
34
+ } ? Proto : unknown : unknown : ThisParameterType<Extract<GlobalTraverse<T>, AnyFunction>>;
26
35
  export type ScramjetModule = {
27
36
  enabled: (client: ScramjetClient) => boolean | undefined;
28
- disabled: (client: ScramjetClient, self: typeof globalThis) => void | undefined;
37
+ disabled: (client: ScramjetClient, self: GlobalThis) => void | undefined;
29
38
  order: number | undefined;
30
- default: (client: ScramjetClient, self: typeof globalThis) => void;
39
+ default: (client: ScramjetClient, self: GlobalThis) => void;
31
40
  };
32
- export type ProxyCtx = {
33
- fn: AnyFunction;
34
- this: any;
35
- args: any[];
36
- newTarget: AnyFunction;
37
- return: (r: any) => void;
38
- call: () => any;
41
+ export type ProxyCtx<T extends string = string, U extends "construct" | "apply" = "apply"> = {
42
+ fn: GlobalTraverse<T>;
43
+ this: IfEquals<U, "construct", null, ProxyApplyThis<T>>;
44
+ args: IfEquals<U, "construct", ConstructorParameters<GlobalTraverse<T>>, Parameters<GlobalTraverse<T>>>;
45
+ newTarget: IfEquals<U, "construct", GlobalTraverse<T>, null>;
46
+ return: (r: IfEquals<U, "construct", InstanceType<GlobalTraverse<T>>, ReturnType<GlobalTraverse<T>>>) => void;
47
+ call: () => IfEquals<U, "construct", InstanceType<GlobalTraverse<T>>, ReturnType<GlobalTraverse<T>>>;
39
48
  };
40
- export type Proxy = {
41
- construct?(ctx: ProxyCtx): any;
42
- apply?(ctx: ProxyCtx): any;
49
+ export type Proxy<T extends string = string> = {
50
+ construct?(ctx: ProxyCtx<T, "construct">): any;
51
+ apply?(ctx: ProxyCtx<T, "apply">): any;
43
52
  };
44
- export type TrapCtx<T> = {
53
+ export type TrapCtx<T extends string> = {
45
54
  this: any;
46
- get: () => T;
47
- set: (v: T) => void;
55
+ get: () => GlobalTraverse<T>;
56
+ set: (v: GlobalTraverse<T>) => void;
48
57
  };
49
- export type Trap<T> = {
58
+ export type Trap<T extends string> = {
50
59
  writable?: boolean;
51
60
  value?: any;
52
61
  enumerable?: boolean;
53
62
  configurable?: boolean;
54
- get?: (ctx: TrapCtx<T>) => T;
55
- set?: (ctx: TrapCtx<T>, v: T) => void;
63
+ get?: (ctx: TrapCtx<T>) => GlobalTraverse<T>;
64
+ set?: (ctx: TrapCtx<T>, v: GlobalTraverse<T>) => void;
56
65
  };
57
66
  export declare class ScramjetClient {
58
- global: typeof globalThis;
67
+ global: GlobalThis;
59
68
  init: ScramjetClientInit;
60
69
  locationProxy: any;
61
70
  serviceWorker: ServiceWorkerContainer;
@@ -73,17 +82,32 @@ export declare class ScramjetClient {
73
82
  meta: URLMeta;
74
83
  box: SingletonBox;
75
84
  context: ScramjetContext;
76
- constructor(global: typeof globalThis, init: ScramjetClientInit);
77
- get frame(): ScramjetFrame | null;
78
- get isSubframe(): boolean;
85
+ initHeaders: ScramjetHeaders;
86
+ history: TrackedHistoryState[];
87
+ private flagCache;
88
+ hooks: {
89
+ rewriter: {
90
+ html: import("@/Tap").TapInstance<HtmlRewriterHooks>;
91
+ };
92
+ lifecycle: import("@/Tap").TapInstance<LifecycleHooks>;
93
+ };
94
+ constructor(global: GlobalThis, init: ScramjetClientInit);
95
+ /** Apply document injection init when a client was already installed (e.g. early contentWindow). */
96
+ syncDocumentInit(init: {
97
+ initHeaders: RawHeaders;
98
+ history: TrackedHistoryState[];
99
+ cookies?: string;
100
+ }): void;
79
101
  hook(): void;
80
- get url(): URL;
81
- set url(url: URL | string);
82
- Proxy(name: string | string[], handler: Proxy): void;
83
- RawProxy(target: any, prop: string, handler: Proxy, debugname?: string): void;
84
- Trap<T>(name: string | string[], descriptor: Trap<T>): PropertyDescriptor;
85
- RawTrap<T>(target: any, prop: string, descriptor: Trap<T>): PropertyDescriptor;
86
- rewriteUrl(url: string | URL): string;
102
+ get url(): _URL;
103
+ set url(url: _URL | string);
104
+ Proxy<T extends string>(name: T, handler: Proxy<T>): void;
105
+ Proxy<const T extends readonly string[]>(name: T, handler: Proxy<T[number]>): void;
106
+ RawProxy(target: any, prop: string, handler: Proxy<any>, debugname?: string): void;
107
+ Trap<T extends string>(name: T, handler: Trap<T>): void;
108
+ Trap<const T extends readonly string[]>(name: T, handler: Trap<T[number]>): void;
109
+ RawTrap(target: any, prop: string, descriptor: Trap<any>): void;
110
+ rewriteUrl(url: string | URL, options?: RewriteUrlOptions): string;
87
111
  unrewriteUrl(url: string | URL): string;
88
112
  flagEnabled(flag: keyof ScramjetConfig["flags"]): boolean;
89
113
  get config(): ScramjetConfig;
@@ -1,38 +1,10 @@
1
- import { ScramjetClient } from "./index";
2
- /**
3
- * Union type for all Scramjet proxified navigation events.
4
- */
5
- export type ScramjetEvent = NavigateEvent | UrlChangeEvent | ScramjetContextEvent;
6
- /**
7
- * Type map for all Scramjet navigation events with their corresponding event types.
8
- */
9
- export type ScramjetEvents = {
10
- navigate: NavigateEvent;
11
- urlchange: UrlChangeEvent;
12
- contextInit: ScramjetContextEvent;
1
+ export type LifecycleHooks = {
2
+ navigate: {
3
+ context: {
4
+ type: "location" | "history" | "hashchange";
5
+ };
6
+ props: {
7
+ url: string;
8
+ };
9
+ };
13
10
  };
14
- /**
15
- * Navigation event class fired when Scramjet frame navigates to a new proxified URL.
16
- */
17
- export declare class NavigateEvent extends Event {
18
- url: string;
19
- type: string;
20
- constructor(url: string);
21
- }
22
- /**
23
- * URL change event class fired when the proxified URL changes in a Scramjet frame.
24
- */
25
- export declare class UrlChangeEvent extends Event {
26
- url: string;
27
- type: string;
28
- constructor(url: string);
29
- }
30
- /**
31
- * Event class fired when Scramjet initializes in a frame.
32
- */
33
- export declare class ScramjetContextEvent extends Event {
34
- window: Self;
35
- client: ScramjetClient;
36
- type: string;
37
- constructor(window: Self, client: ScramjetClient);
38
- }
@@ -1 +1 @@
1
- export declare function getOwnPropertyDescriptorHandler(target: any, prop: any): TypedPropertyDescriptor<any>;
1
+ export declare function getOwnPropertyDescriptorHandler(target: any, prop: any): PropertyDescriptor;
@@ -1,2 +1,2 @@
1
- import { ScramjetClient } from "./index";
2
- export declare function createLocationProxy(client: ScramjetClient, self: typeof globalThis): any;
1
+ import { ScramjetClient } from "@client/index";
2
+ export declare function createLocationProxy(client: ScramjetClient, self: GlobalThis): any;
@@ -1,3 +1,3 @@
1
- import { ScramjetClient } from "../index";
1
+ import { ScramjetClient } from "@client/index";
2
2
  export default function (client: ScramjetClient, self: Self): void;
3
3
  export declare function indirectEval(this: ScramjetClient, strict: boolean, js: any): any;
@@ -1,4 +1,4 @@
1
- import { ScramjetClient } from "../index";
1
+ import { ScramjetClient } from "@client/index";
2
2
  declare enum RewriteType {
3
3
  Insert = 0,
4
4
  Replace = 1
@@ -1,4 +1,4 @@
1
- import { ScramjetClient } from "../index";
2
- export declare function createWrapFn(client: ScramjetClient, self: typeof globalThis): (identifier: any, strict: boolean) => any;
1
+ import { ScramjetClient } from "@client/index";
2
+ export declare function createWrapFn(client: ScramjetClient, self: GlobalThis): (identifier: any, strict: boolean) => any;
3
3
  export declare const order = 4;
4
- export default function (client: ScramjetClient, self: typeof globalThis): void;
4
+ export default function (client: ScramjetClient, self: GlobalThis): void;
@@ -1,3 +1,4 @@
1
+ import { IncrementalHtmlRewriter } from "@/shared";
1
2
  import { ScramjetClient } from "./client";
2
3
  import { SourceMaps } from "./shared/sourcemaps";
3
4
  export declare class SingletonBox {
@@ -6,6 +7,7 @@ export declare class SingletonBox {
6
7
  globals: Map<Self, ScramjetClient>;
7
8
  documents: Map<Document, ScramjetClient>;
8
9
  locations: Map<Location, ScramjetClient>;
10
+ writeRewriters: WeakMap<Document, IncrementalHtmlRewriter>;
9
11
  ctors: Record<string, Function[]>;
10
12
  sourcemaps: SourceMaps;
11
13
  constructor(ownerclient: ScramjetClient);
@@ -0,0 +1,3 @@
1
+ import { BareResponse } from "@mercuryworkshop/proxy-transports";
2
+ import { BodyType, ScramjetFetchHandler, ScramjetFetchParsed, ScramjetFetchRequest } from ".";
3
+ export declare function rewriteBody(handler: ScramjetFetchHandler, request: ScramjetFetchRequest, parsed: ScramjetFetchParsed, response: BareResponse): Promise<BodyType>;
@@ -0,0 +1,7 @@
1
+ import { BareResponse } from "@mercuryworkshop/proxy-transports";
2
+ import { ScramjetFetchHandler, ScramjetFetchParsed, ScramjetFetchRequest, ScramjetFetchResponse } from ".";
3
+ import { ScramjetHeaders } from "@/shared";
4
+ export declare function doHandleFetch(handler: ScramjetFetchHandler, request: ScramjetFetchRequest): Promise<ScramjetFetchResponse>;
5
+ export declare function doNetworkFetch(handler: ScramjetFetchHandler, request: ScramjetFetchRequest, parsed: ScramjetFetchParsed, newheaders: ScramjetHeaders): Promise<BareResponse>;
6
+ /** Simplified registrable-domain check used for cross-site redirect detection. */
7
+ export declare function registrableDomainForRedirect(hostname: string): string;
@@ -0,0 +1,19 @@
1
+ import { ScramjetHeaders } from "@/shared";
2
+ import { ScramjetFetchHandler, ScramjetFetchParsed, ScramjetFetchRequest } from ".";
3
+ import { RawHeaders } from "@mercuryworkshop/proxy-transports";
4
+ export declare function rewriteResponseHeaders(handler: ScramjetFetchHandler, request: ScramjetFetchRequest, parsed: ScramjetFetchParsed, rawHeaders: RawHeaders): Promise<ScramjetHeaders>;
5
+ export declare function rewriteRequestHeaders(request: ScramjetFetchRequest, handler: ScramjetFetchHandler, parsed: ScramjetFetchParsed): ScramjetHeaders;
6
+ /**
7
+ * Compute the immediate Sec-Fetch-Site relation between an initiator origin and
8
+ * a destination URL.
9
+ *
10
+ * - "same-origin" if scheme + host + port match exactly.
11
+ * - "same-site" if scheme matches and the registrable domains match.
12
+ * - "cross-site" otherwise.
13
+ */
14
+ export declare function computeFetchSite(originUrl: URL, destUrl: URL): "same-origin" | "same-site" | "cross-site";
15
+ /**
16
+ * Combine two Sec-Fetch-Site classifications, returning the "worst" (least
17
+ * trusted) of the two. Used when propagating state through redirect chains.
18
+ */
19
+ export declare function worstFetchSite(a: "none" | "same-origin" | "same-site" | "cross-site", b: "none" | "same-origin" | "same-site" | "cross-site"): "none" | "same-origin" | "same-site" | "cross-site";
@@ -0,0 +1,23 @@
1
+ import { ScramjetFetchHandler, ScramjetFetchParsed, ScramjetFetchRequest } from ".";
2
+ export declare const QP: {
3
+ readonly referrerPolicy: "$rfp";
4
+ readonly referrerSource: "$rfs";
5
+ readonly isModule: "$module";
6
+ readonly topFrame: "$tf";
7
+ readonly parentFrame: "$pf";
8
+ readonly isIframe: "$iframe";
9
+ readonly mode: "$mode";
10
+ readonly credentials: "$cred";
11
+ readonly destination: "$dest";
12
+ readonly initiatorOrigin: "$io";
13
+ readonly fetchSite: "$fs";
14
+ readonly crossSiteRedirect: "$csr";
15
+ readonly fakeDataURL: "$fakedataurl";
16
+ };
17
+ export type QueryParamKey = keyof typeof QP;
18
+ export type QueryParams = Partial<Record<QueryParamKey, string>>;
19
+ export declare function parseQueryParams(searchParams: URLSearchParams): {
20
+ params: QueryParams;
21
+ extras: Record<string, string>;
22
+ };
23
+ export declare function parseRequest(request: ScramjetFetchRequest, handler: ScramjetFetchHandler): ScramjetFetchParsed;
@@ -0,0 +1,7 @@
1
+ import { ScramjetHeaders } from "@/shared";
2
+ import { BareResponse } from "@mercuryworkshop/proxy-transports";
3
+ import { ScramjetFetchParsed } from ".";
4
+ export declare function normalizeContentType(parsed: ScramjetFetchParsed, headers: ScramjetHeaders): void;
5
+ export declare function isRedirect(response: BareResponse): boolean;
6
+ export declare function isDocument(parsed: ScramjetFetchParsed): boolean;
7
+ export declare function createReferrerString(clientUrl: URL, resource: URL, policy: string | null): string;
@@ -5,5 +5,7 @@ export * from "./shared";
5
5
  export * from "./symbols";
6
6
  export * from "./types";
7
7
  export * from "./fetch";
8
+ export * from "./Tap";
9
+ export { BareResponse } from "@mercuryworkshop/proxy-transports";
8
10
  export declare const defaultConfig: ScramjetConfig;
9
11
  export declare const defaultConfigDev: ScramjetConfig;
@@ -2,17 +2,25 @@ export type Cookie = {
2
2
  name: string;
3
3
  value: string;
4
4
  path?: string;
5
- expires?: string;
5
+ expires?: number;
6
6
  maxAge?: number;
7
7
  domain?: string;
8
+ hostOnly?: boolean;
8
9
  secure?: boolean;
9
10
  httpOnly?: boolean;
10
- sameSite?: "strict" | "lax" | "none";
11
+ sameSite?: string;
11
12
  };
12
13
  export declare class CookieJar {
13
14
  private cookies;
14
- setCookies(cookies: string[], url: URL): void;
15
- getCookies(url: URL, fromJs: boolean): string;
16
- load(cookies: string): null;
15
+ private byDomain;
16
+ private defaultPath;
17
+ private pathMatches;
18
+ private indexCookie;
19
+ private unindexCookie;
20
+ private removeById;
21
+ setCookies(cookieString: string, url: URL): void;
22
+ getCookies(url: URL, fromJs: boolean, sameSiteContext?: "strict" | "lax" | "cross-site"): string;
23
+ load(cookies: string | Record<string, Cookie>): void;
24
+ clear(): void;
17
25
  dump(): string;
18
26
  }
@@ -1,6 +1,6 @@
1
- import { URLMeta } from "./rewriters/url";
2
- import { ScramjetContext } from "../shared";
1
+ import { URLMeta } from "@rewriters/url";
2
+ import { ScramjetContext } from "@/shared";
3
3
  export declare const htmlRules: {
4
4
  [key: string]: "*" | string[] | ((...any: any[]) => string | null);
5
- fn: (value: string, context: ScramjetContext, meta: URLMeta) => string | null;
5
+ fn: (value: string, context: ScramjetContext, meta: URLMeta, attrs?: Record<string, string | undefined>) => string | null;
6
6
  }[];
@@ -1,23 +1,51 @@
1
- import { ScramjetConfig, ScramjetFlags, ScramjetVersionInfo } from "../types";
1
+ import { ScramjetConfig, ScramjetFlags, ScramjetVersionInfo } from "@/types";
2
2
  import DomHandler, { Element } from "domhandler";
3
- import { URLMeta } from "./rewriters/url";
3
+ import { URLMeta } from "@rewriters/url";
4
4
  import { CookieJar } from "./cookie";
5
+ import { TapInstance } from "@/Tap";
6
+ import { HtmlContext } from "@/shared/rewriters/html";
5
7
  export * from "./cookie";
6
8
  export * from "./headers";
7
9
  export * from "./htmlRules";
10
+ export * from "./mime";
8
11
  export * from "./rewriters";
9
- export * from "./security";
10
12
  export declare function flagEnabled(flag: keyof ScramjetFlags, context: ScramjetContext, url: URL): boolean;
11
13
  export type ScramjetInterface = {
12
14
  codecEncode: (input: string) => string;
13
15
  codecDecode: (input: string) => string;
14
- getInjectScripts(meta: URLMeta, handler: DomHandler, script: (src: string) => Element): Element[];
15
- getWorkerInjectScripts?(meta: URLMeta, type: "module" | "regular", script: (src: string) => string): string;
16
+ getInjectScripts(meta: URLMeta, handler: DomHandler, htmlcontext: HtmlContext, script: (src: string) => Element): Element[];
17
+ getWorkerInjectScripts?(meta: URLMeta, isModule: boolean, script: (src: string) => string): string;
16
18
  };
17
19
  export type ScramjetContext = {
18
20
  config: ScramjetConfig;
19
21
  prefix: URL;
20
22
  interface: ScramjetInterface;
21
23
  cookieJar: CookieJar;
24
+ hooks?: {
25
+ rewriter: {
26
+ html: TapInstance<HtmlRewriterHooks>;
27
+ };
28
+ };
22
29
  };
23
30
  export declare const versionInfo: ScramjetVersionInfo;
31
+ export type HtmlRewriterHooks = {
32
+ pre: {
33
+ context: {
34
+ handler: DomHandler;
35
+ meta: URLMeta;
36
+ origHtml: string;
37
+ htmlcontext: HtmlContext;
38
+ };
39
+ };
40
+ post: {
41
+ context: {
42
+ handler: DomHandler;
43
+ meta: URLMeta;
44
+ origHtml: string;
45
+ htmlcontext: HtmlContext;
46
+ };
47
+ props: {
48
+ setRawHtml?: string;
49
+ };
50
+ };
51
+ };
@@ -0,0 +1,47 @@
1
+ export interface ParsedMimeType {
2
+ type: string;
3
+ subtype: string;
4
+ /** `type`/`subtype` in ASCII lowercase; excludes parameters. */
5
+ essence: string;
6
+ }
7
+ /**
8
+ * Parses a MIME type string (e.g. a Content-Type value) into type, subtype, and essence.
9
+ * Returns null if the input is not a valid MIME type.
10
+ */
11
+ export declare function parseMimeType(input: string): ParsedMimeType | null;
12
+ /** A MIME type whose type is "image". */
13
+ export declare function isImageMimeType(mime: string | ParsedMimeType): boolean;
14
+ /** Audio, video, or essence `application/ogg`. */
15
+ export declare function isAudioOrVideoMimeType(mime: string | ParsedMimeType): boolean;
16
+ /** Type `font` or a registered font essence. */
17
+ export declare function isFontMimeType(mime: string | ParsedMimeType): boolean;
18
+ /** Subtype ends with `+zip` or essence `application/zip`. */
19
+ export declare function isZipBasedMimeType(mime: string | ParsedMimeType): boolean;
20
+ /** One of the archive essences. */
21
+ export declare function isArchiveMimeType(mime: string | ParsedMimeType): boolean;
22
+ /** Subtype ends with `+xml` or essence `text/xml` / `application/xml`. */
23
+ export declare function isXmlMimeType(mime: string | ParsedMimeType): boolean;
24
+ /** Essence `text/html`. */
25
+ export declare function isHtmlMimeType(mime: string | ParsedMimeType): boolean;
26
+ /** XML, HTML, or `application/pdf`. */
27
+ export declare function isScriptableMimeType(mime: string | ParsedMimeType): boolean;
28
+ /** Essence is one of the JavaScript MIME type essences. */
29
+ export declare function isJavascriptMimeType(mime: string | ParsedMimeType): boolean;
30
+ /**
31
+ * True if the string is an ASCII case-insensitive match for one of the
32
+ * JavaScript MIME type essence strings (not necessarily a full parsed MIME type).
33
+ */
34
+ export declare function isJavascriptMimeTypeEssenceMatch(s: string): boolean;
35
+ /**
36
+ * Whether a `<script type="...">` value denotes executable JavaScript
37
+ * (classic or module script), not a data block or import map.
38
+ * @see https://html.spec.whatwg.org/multipage/scripting.html#attr-script-type
39
+ */
40
+ export declare function isScriptType(type: string | null | undefined): boolean;
41
+ /** Whether `type` denotes a JavaScript module script. */
42
+ export declare function isModuleScriptType(type: string | null | undefined): boolean;
43
+ /**
44
+ * MIME types typically shown inline in a browsing context (navigation / iframe),
45
+ * as opposed to triggering a download when Content-Disposition is absent.
46
+ */
47
+ export declare function isInlineDisplayableMimeType(mime: string | ParsedMimeType): boolean;
@@ -0,0 +1,7 @@
1
+ export type ParsedDeclarativeRefresh = {
2
+ time: number;
3
+ urlStart: number;
4
+ urlEnd: number;
5
+ url: string | null;
6
+ };
7
+ export declare function parseDeclarativeRefresh(input: string): ParsedDeclarativeRefresh | null;
@@ -1,4 +1,4 @@
1
- import { URLMeta } from "./url";
2
- import { ScramjetContext } from "../../shared";
1
+ import { URLMeta } from "@rewriters/url";
2
+ import { ScramjetContext } from "@/shared";
3
3
  export declare function rewriteCss(css: string, context: ScramjetContext, meta: URLMeta): string;
4
- export declare function unrewriteCss(css: string): string;
4
+ export declare function unrewriteCss(css: string, context: ScramjetContext): string;
@@ -1,6 +1,33 @@
1
- import { DomHandler } from "domhandler";
2
- import { URLMeta } from "./url";
3
- import { ScramjetContext } from "../../shared";
4
- export declare function rewriteHtml(html: string, context: ScramjetContext, meta: URLMeta, fromTop?: boolean, preRewrite?: (handler: DomHandler) => void, postRewrite?: (handler: DomHandler) => void): string;
5
- export declare function unrewriteHtml(html: string): string;
1
+ import { URLMeta } from "@rewriters/url";
2
+ import { ScramjetContext } from "@/shared";
3
+ import { RawHeaders } from "@mercuryworkshop/proxy-transports";
4
+ import { TrackedHistoryState } from "@/fetch";
5
+ export type ForeignContext = "svg" | "math" | "html";
6
+ export type HtmlContext = {
7
+ loadScripts: boolean;
8
+ inline: boolean;
9
+ source: string;
10
+ apisource?: string;
11
+ headers?: RawHeaders;
12
+ foreignContext?: ForeignContext;
13
+ history?: TrackedHistoryState[];
14
+ };
15
+ export declare class IncrementalHtmlRewriter {
16
+ private readonly context;
17
+ private readonly meta;
18
+ private readonly htmlcontext;
19
+ private readonly handler;
20
+ private readonly parser;
21
+ private readonly completedElements;
22
+ private readonly emittedLengths;
23
+ private readonly rewrittenNodes;
24
+ private ended;
25
+ constructor(context: ScramjetContext, meta: URLMeta, htmlcontext: HtmlContext);
26
+ write(html: string): string;
27
+ end(html?: string): string;
28
+ private flush;
29
+ private getAvailableOutput;
30
+ }
31
+ export declare function rewriteHtml(html: string, context: ScramjetContext, meta: URLMeta, htmlcontext: HtmlContext): string;
32
+ export declare function unrewriteHtml(html: string, foreignContext?: ForeignContext): string;
6
33
  export declare function rewriteSrcset(srcset: string, context: ScramjetContext, meta: URLMeta): string;
@@ -1,11 +1,11 @@
1
- import { ScramjetContext } from "../../shared";
2
- import { URLMeta } from "./url";
1
+ import { ScramjetContext } from "@/shared";
2
+ import { URLMeta } from "@rewriters/url";
3
3
  type RewriterResult = {
4
4
  js: string | Uint8Array;
5
5
  map: Uint8Array | null;
6
6
  tag: string;
7
7
  errors: string[];
8
8
  };
9
- export declare function rewriteJsInner(js: string | Uint8Array, url: string | null, context: ScramjetContext, meta: URLMeta, module?: boolean): RewriterResult;
10
- export declare function rewriteJs(js: string | Uint8Array, url: string | null, context: ScramjetContext, meta: URLMeta, module?: boolean): string | Uint8Array;
9
+ export declare function rewriteJsInner(js: string | Uint8Array, url: string | null, context: ScramjetContext, meta: URLMeta, isModule?: boolean): RewriterResult;
10
+ export declare function rewriteJs(js: string | Uint8Array, url: string | null, context: ScramjetContext, meta: URLMeta, isModule?: boolean): string | Uint8Array;
11
11
  export {};
@@ -1,11 +1,25 @@
1
- import { ScramjetContext } from "../../shared";
1
+ import { ScramjetContext } from "@/shared";
2
+ import { _URL } from "../snapshot";
3
+ export type NavigationType = "user" | "link" | "location";
4
+ export type RewriteUrlOptions = {
5
+ referrerPolicy?: string;
6
+ isModule?: boolean;
7
+ navigateType?: NavigationType;
8
+ topFrame?: string;
9
+ parentFrame?: string;
10
+ isIframe?: string;
11
+ mode?: string;
12
+ credentials?: string;
13
+ destination?: RequestDestination;
14
+ };
2
15
  export type URLMeta = {
3
- origin: URL;
4
- base: URL;
16
+ origin: _URL;
17
+ base: _URL;
5
18
  topFrameName?: string;
6
19
  parentFrameName?: string;
20
+ referrerPolicy?: string;
7
21
  };
8
22
  export declare function rewriteBlob(url: string, context: ScramjetContext, meta: URLMeta): string;
9
- export declare function unrewriteBlob(url: string, context: ScramjetContext, meta: URLMeta): string;
10
- export declare function rewriteUrl(url: string | URL, context: ScramjetContext, meta: URLMeta): string;
23
+ export declare function unrewriteBlob(url: string, context: ScramjetContext, _meta: URLMeta): string;
24
+ export declare function rewriteUrl(url: string | URL, context: ScramjetContext, meta: URLMeta, options?: RewriteUrlOptions): string;
11
25
  export declare function unrewriteUrl(url: string | URL, context: ScramjetContext): string;
@@ -1,8 +1,7 @@
1
1
  import { Rewriter } from "../../../rewriter/wasm/out/wasm.js";
2
2
  import type { JsRewriterOutput } from "../../../rewriter/wasm/out/wasm.js";
3
- import { ScramjetContext } from "../../shared";
3
+ import { ScramjetContext } from "@/shared";
4
4
  export type { JsRewriterOutput, Rewriter };
5
- import { URLMeta } from "./url";
5
+ import { URLMeta } from "@rewriters/url";
6
6
  export declare function setWasm(u8: Uint8Array | ArrayBuffer): void;
7
- export declare const textDecoder: TextDecoder;
8
7
  export declare function getRewriter(context: ScramjetContext, meta: URLMeta): [Rewriter, () => void];
@@ -1,3 +1,3 @@
1
- import { ScramjetContext } from "../../shared";
2
- import { URLMeta } from "./url";
1
+ import { ScramjetContext } from "@/shared";
2
+ import { URLMeta } from "@rewriters/url";
3
3
  export declare function rewriteWorkers(context: ScramjetContext, js: string | Uint8Array, type: "module" | "regular", url: string, meta: URLMeta): string;