-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
120 additions
and
190 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {Transformer} from "./transformer.js"; | ||
|
||
/** | ||
* Removes comments and whitespace from a PHP script, by calling a Web service. | ||
*/ | ||
export class FastTransformer implements Transformer { | ||
|
||
/** | ||
* Creates a new fast transformer. | ||
* @param executable The path to the PHP executable. | ||
*/ | ||
constructor(executable?: string); | ||
|
||
/** | ||
* Closes this transformer and releases any resources associated with it. | ||
* @returns Resolves when the transformer is finally disposed. | ||
*/ | ||
close(): Promise<void>; | ||
|
||
/** | ||
* Processes a PHP script. | ||
* @param file The path to the PHP script. | ||
* @returns The transformed script. | ||
*/ | ||
transform(file: string): Promise<string>; | ||
} |
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
Empty file.
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,52 @@ | ||
import {spawn} from "node:child_process" | ||
import {createServer} from "node:net" | ||
import {join, normalize, resolve} from "node:path" | ||
import {setTimeout} from "node:timers" | ||
|
||
# Removes comments and whitespace from a PHP script, by calling a Web service. | ||
export class FastTransformer | ||
|
||
# Creates a new fast transformer. | ||
constructor: (executable = "php") -> | ||
|
||
# The path to the PHP executable. | ||
@_executable = normalize executable | ||
|
||
# The port that the PHP process is listening on. | ||
@_port = -1 | ||
|
||
# The underlying PHP process. | ||
@_process = null | ||
|
||
# Closes this transformer and releases any resources associated with it. | ||
close: -> | ||
@_process?.kill() | ||
@_process = null | ||
Promise.resolve() | ||
|
||
# Starts the underlying PHP process and begins accepting connections. | ||
listen: -> if @_process? then Promise.resolve @_port else | ||
@_port = await @_getPort() | ||
args = ["-S", "127.0.0.1:#{@_port}", "-t", join(import.meta.dirname, "../www")] | ||
new Promise (fulfill, reject) => | ||
spawn @_executable, args, stdio: ["ignore", "pipe", "ignore"] | ||
.on "error", reject | ||
.on "spawn", => setTimeout (=> fulfill @_port), 1000 | ||
|
||
# Processes a PHP script. | ||
transform: (file) -> | ||
await @listen() | ||
url = new URL "http://127.0.0.1:#{@_port}/index.php" | ||
url.searchParams.set "file", resolve(file) | ||
|
||
response = await fetch url | ||
throw Error("An error occurred while processing the script: #{file}") unless response.ok | ||
response.text() | ||
|
||
# Gets an ephemeral TCP port chosen by the system. | ||
_getPort: -> new Promise (fulfill, reject) -> | ||
socket = createServer().on "error", reject | ||
socket.unref() | ||
socket.listen host: "127.0.0.1", port: 0, -> | ||
{port} = socket.address() | ||
socket.close -> fulfill port |
Empty file.
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./fast_transformer.js" | ||
export * from "./safe_transformer.js" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,34 @@ | ||
import {FastTransformer} from "@cedx/php-minifier" | ||
import {doesNotReject, ok} from "node:assert/strict" | ||
import {after, describe, it} from "node:test" | ||
|
||
# Tests the features of the `FastTransformer` class. | ||
describe "FastTransformer", -> | ||
describe "close()", -> | ||
it "should not reject, even if called several times", -> | ||
transformer = new FastTransformer | ||
await doesNotReject transformer.listen() | ||
await doesNotReject transformer.close() | ||
await doesNotReject transformer.close() | ||
|
||
describe "listen()", -> | ||
it "should not reject, even if called several times", -> | ||
transformer = new FastTransformer | ||
await doesNotReject transformer.listen() | ||
await doesNotReject transformer.listen() | ||
await doesNotReject transformer.close() | ||
|
||
describe "transform()", -> | ||
map = new Map [ | ||
["should remove the inline comments", "<?= 'Hello World!' ?>"] | ||
["should remove the multi-line comments", "namespace dummy; class Dummy"] | ||
["should remove the single-line comments", "$className = get_class($this); return $className;"] | ||
["should remove the whitespace", "__construct() { $this->property"] | ||
] | ||
|
||
transformer = new FastTransformer | ||
after -> transformer.close() | ||
|
||
for [key, value] from map then it key, -> | ||
output = await transformer.transform "res/sample.php" | ||
ok output.includes value |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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