ensloppify 1.0.0-beta.1
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/LICENSE +7 -0
- package/README.md +2 -0
- package/dist/esm/index.mjs +16 -0
- package/dist/esm/index.mjs.map +7 -0
- package/dist/types/data.d.ts +73 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/patcher.d.ts +22 -0
- package/dist/types/patchers/module.d.ts +2 -0
- package/dist/types/patchers/module.internal.d.ts +11 -0
- package/package.json +36 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as acorn from "acorn";
|
|
2
|
+
import { SourceDownloadCallback } from "./patcher";
|
|
3
|
+
export type SourceType = "module" | "script" | "commonjs";
|
|
4
|
+
export type SourcePatchScriptDataKey = string | HTMLScriptElement;
|
|
5
|
+
export declare class SourcePatchScriptData {
|
|
6
|
+
private static CLEAN_SOURCE_AST_CACHE;
|
|
7
|
+
private _key;
|
|
8
|
+
private _source;
|
|
9
|
+
private _sourceType;
|
|
10
|
+
private _sourceOriginCanonicalUrl;
|
|
11
|
+
private _isSourceClean;
|
|
12
|
+
private _ast;
|
|
13
|
+
private _isAstOutdated;
|
|
14
|
+
/**
|
|
15
|
+
* A key that can identify a {@link SourcePatchScriptData} by the unique script it refers to.
|
|
16
|
+
*
|
|
17
|
+
* This must compare equal in two {@link SourcePatchScriptData} instances that point to the same script.
|
|
18
|
+
*/
|
|
19
|
+
get key(): SourcePatchScriptDataKey;
|
|
20
|
+
/**
|
|
21
|
+
* The source code of this script. Patcher functions should apply their changes here.
|
|
22
|
+
*
|
|
23
|
+
* NOTE: To avoid unnecessary string comparison, setting this value will mark the source dirty ({@link isSourceClean} == `false`)
|
|
24
|
+
* and the AST outdated ({@link isAstOutdated}), regardless of whether the value actually changed at all.
|
|
25
|
+
*/
|
|
26
|
+
get source(): string;
|
|
27
|
+
get sourceType(): SourceType;
|
|
28
|
+
/**
|
|
29
|
+
* The canonical/absolute URL from where this script originates from.
|
|
30
|
+
* This value should correspond to that of `import.meta.url` as called from this script.
|
|
31
|
+
*
|
|
32
|
+
* In the case of a script whose source is embedded into a HTML document, this refers to the URL of the HTML document;
|
|
33
|
+
* As such, this must not be used distinguishing scripts from each other, as there may be ambiquity across scripts;
|
|
34
|
+
* Use {@link key} instead, as that can distinguish between scripts embedded into the same document.
|
|
35
|
+
*/
|
|
36
|
+
get sourceOriginCanonicalUrl(): string;
|
|
37
|
+
/**
|
|
38
|
+
* Whether {@link source} has not had any mutations after this {@link SourcePatchScriptData} was constructed.
|
|
39
|
+
*/
|
|
40
|
+
get isSourceClean(): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* The AST of this script as parsed by {@link acorn} or {@link acorn_loose} when {@link parseAstIfOutdated} was last called, or `null` if never parsed.
|
|
43
|
+
*/
|
|
44
|
+
get ast(): acorn.Program | null;
|
|
45
|
+
/**
|
|
46
|
+
* Whether {@link source} has been mutated since {@link ast} was last parsed by {@link parseAstIfOutdated}.
|
|
47
|
+
*/
|
|
48
|
+
get isAstOutdated(): boolean;
|
|
49
|
+
set source(value: string);
|
|
50
|
+
/**
|
|
51
|
+
* Enables caching AST parsed from clean sources, for when a new {@link SourcePatchScriptData} is instantiated with the same script origin.
|
|
52
|
+
*
|
|
53
|
+
* NOTE: This caching behavior assumes that a script origin will always resolve to the same script content.
|
|
54
|
+
* This assumption may not hold in some cases (unit tests, undeterministic server, etc), which is why caching is disabled by default.
|
|
55
|
+
*/
|
|
56
|
+
static enableAstCache(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Clears and disables the clean source AST cache. See {@link enableAstCache} for more information.
|
|
59
|
+
*/
|
|
60
|
+
static disableAstCache(): void;
|
|
61
|
+
static ofModuleSource(source: string, sourceOriginCanonicalUrl: string): SourcePatchScriptData;
|
|
62
|
+
static ofScriptElement(scriptElement: HTMLScriptElement, srcDownloadCallback: SourceDownloadCallback): Promise<SourcePatchScriptData>;
|
|
63
|
+
constructor(key: SourcePatchScriptDataKey, source: string, sourceType: SourceType, sourceOriginCanonicalUrl: string);
|
|
64
|
+
/**
|
|
65
|
+
* Parses an AST of {@link source} using {@link acorn} or {@link acorn_loose} if {@link ast} is outdated (including `null`).
|
|
66
|
+
*
|
|
67
|
+
* The AST can later on be accessed as {@link ast} without reparsing, though you will then have to check {@link isAstOutdated} manually,
|
|
68
|
+
* if you want to make sure that the AST is consistent with the current {@link source}.
|
|
69
|
+
*
|
|
70
|
+
* @returns the parsed AST, or the previously parsed value in {@link ast} if not outdated.
|
|
71
|
+
*/
|
|
72
|
+
parseAstIfOutdated(): acorn.Program;
|
|
73
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { SourcePatchScriptData, SourcePatchScriptDataKey, SourceType } from "./data";
|
|
2
|
+
export { SourcePatcher, SourcePatchCondition, SourcePatchFunction, SourcePatchImplementation, SourceDownloadCallback } from "./patcher";
|
|
3
|
+
export { patchModuleImportsRecursively } from "./patchers/module";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { SourcePatchScriptData } from "./data";
|
|
2
|
+
export type SourcePatchCondition = string | RegExp | ((scriptData: SourcePatchScriptData, sourcePatcher: SourcePatcher) => boolean | Promise<boolean>);
|
|
3
|
+
export type SourcePatchFunction = (scriptData: SourcePatchScriptData, sourcePatcher: SourcePatcher) => void | Promise<void>;
|
|
4
|
+
export type SourcePatchImplementation = [SourcePatchCondition, SourcePatchFunction];
|
|
5
|
+
export type SourceDownloadCallback = (sourceOriginUrl: string) => string | Promise<string>;
|
|
6
|
+
export declare class SourcePatcher {
|
|
7
|
+
private patchImplementations;
|
|
8
|
+
private patchedBlobUrls;
|
|
9
|
+
private matchingPatchFunctionsCache;
|
|
10
|
+
constructor(patchImplementations: SourcePatchImplementation[]);
|
|
11
|
+
patchScriptElementToBlobUrl(scriptElement: HTMLScriptElement, srcDownloadCallback: SourceDownloadCallback): Promise<URL | null>;
|
|
12
|
+
patchDataToBlobUrl(scriptData: SourcePatchScriptData): Promise<URL | null>;
|
|
13
|
+
revokePatchedBlobUrls(): void;
|
|
14
|
+
needsPatching(scriptData: SourcePatchScriptData, options?: {
|
|
15
|
+
exceptPatchFunctions: SourcePatchFunction[];
|
|
16
|
+
}): Promise<boolean>;
|
|
17
|
+
getMatchingPatchFunctions(scriptData: SourcePatchScriptData, options?: {
|
|
18
|
+
exceptPatchFunctions: SourcePatchFunction[];
|
|
19
|
+
}): Promise<SourcePatchFunction[]>;
|
|
20
|
+
private getAllMatchingPatchFunctions;
|
|
21
|
+
private getPatchedSource;
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SourcePatchScriptData } from "../data";
|
|
2
|
+
import { SourceDownloadCallback } from "../patcher";
|
|
3
|
+
export type FoundImport = {
|
|
4
|
+
resolvedSourceUrl: string;
|
|
5
|
+
sourceValue: string;
|
|
6
|
+
sourceValueStartIndex: number;
|
|
7
|
+
sourceValueEndIndex: number;
|
|
8
|
+
};
|
|
9
|
+
export declare function findImportsRecursive(scriptData: SourcePatchScriptData, importedSourceDownloadCallback: SourceDownloadCallback, dontRecurseIntoUrls?: string[]): Promise<FoundImport[]>;
|
|
10
|
+
export declare function findImports(scriptData: SourcePatchScriptData): FoundImport[];
|
|
11
|
+
export declare function resolveImportSourceUrl(importSourceValue: string, importerScriptData: SourcePatchScriptData): string | null;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ensloppify",
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"./LICENSE",
|
|
7
|
+
"./dist/esm",
|
|
8
|
+
"./dist/types"
|
|
9
|
+
],
|
|
10
|
+
"module": "./dist/esm/index.mjs",
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/esm/index.mjs",
|
|
15
|
+
"types": "./dist/types/index.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "node build.mts && tsc --project tsconfig.build.json",
|
|
20
|
+
"test": "vitest --ui"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"acorn": "^8.18.0",
|
|
24
|
+
"acorn-loose": "^8.5.2",
|
|
25
|
+
"acorn-walk": "^8.3.5",
|
|
26
|
+
"eval-estree-expression": "^4.0.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"typescript": "^7.0.0",
|
|
30
|
+
"@types/node": "^26.4.1",
|
|
31
|
+
"@vitest/browser-playwright": "^4.1.11",
|
|
32
|
+
"@vitest/ui": "^4.1.11",
|
|
33
|
+
"esbuild": "^0.28.2",
|
|
34
|
+
"vitest": "^4.1.11"
|
|
35
|
+
}
|
|
36
|
+
}
|