-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add plugin api for manipulating the ast
- Loading branch information
Showing
4 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { parse } from '@babel/parser'; | ||
import template from '@babel/template'; | ||
import traverse, { visitors, type Visitor } from '@babel/traverse'; | ||
import * as t from '@babel/types'; | ||
import * as m from '@codemod/matchers'; | ||
|
||
const stages = [ | ||
'parse', | ||
'prepare', | ||
'deobfuscate', | ||
'unminify', | ||
'unpack', | ||
] as const; | ||
|
||
export type Stage = (typeof stages)[number]; | ||
|
||
export type PluginState = { opts: Record<string, unknown> }; | ||
|
||
export interface PluginObject { | ||
name?: string; | ||
runAfter: Stage; | ||
pre?: (this: PluginState, state: PluginState) => Promise<void> | void; | ||
post?: (this: PluginState, state: PluginState) => Promise<void> | void; | ||
visitor?: Visitor<PluginState>; | ||
} | ||
|
||
export interface PluginAPI { | ||
parse: typeof parse; | ||
types: typeof t; | ||
traverse: typeof traverse; | ||
template: typeof template; | ||
matchers: typeof m; | ||
} | ||
|
||
export type Plugin = (api: PluginAPI) => PluginObject; | ||
|
||
export function loadPlugins(plugins: Plugin[]) { | ||
const groups = new Map<Stage, PluginObject[]>( | ||
stages.map((stage) => [stage, []]), | ||
); | ||
for (const plugin of plugins) { | ||
const obj = plugin({ | ||
parse, | ||
types: t, | ||
traverse, | ||
template, | ||
matchers: m, | ||
}); | ||
groups.get(obj.runAfter)?.push(obj); | ||
} | ||
return Object.fromEntries( | ||
[...groups].map(([stage, plugins]) => [ | ||
stage, | ||
plugins.length | ||
? async (ast: t.File) => { | ||
const state: PluginState = { opts: {} }; | ||
for (const transform of plugins) { | ||
await transform.pre?.call(state, state); | ||
} | ||
|
||
const pluginVisitors = plugins.flatMap( | ||
(plugin) => plugin.visitor ?? [], | ||
); | ||
if (pluginVisitors.length > 0) { | ||
const mergedVisitor = visitors.merge(pluginVisitors); | ||
traverse(ast, mergedVisitor, undefined, state); | ||
} | ||
|
||
for (const plugin of plugins) { | ||
await plugin.post?.call(state, state); | ||
} | ||
} | ||
: undefined, | ||
]), | ||
) as Record<Stage, (ast: t.File) => Promise<void>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect, test, vi } from 'vitest'; | ||
import { webcrack } from '../src'; | ||
import type { Plugin } from '../src/plugin'; | ||
|
||
test('run plugin after parse', async () => { | ||
const pre = vi.fn(); | ||
const post = vi.fn(); | ||
|
||
const plugin: Plugin = ({ types: t }) => ({ | ||
runAfter: 'parse', | ||
pre, | ||
post, | ||
visitor: { | ||
NumericLiteral(path) { | ||
path.replaceWith(t.stringLiteral(path.node.value.toString())); | ||
}, | ||
}, | ||
}); | ||
const result = await webcrack('1 + 1;', { plugins: [plugin] }); | ||
|
||
expect(pre).toHaveBeenCalledOnce(); | ||
expect(post).toHaveBeenCalledOnce(); | ||
expect(result.code).toBe('"11";'); | ||
}); |