Skip to content

Commit 521d013

Browse files
author
Cédric Belin
committed
Port the FastTransformer class
1 parent fa341ac commit 521d013

17 files changed

+120
-190
lines changed

example/gulpfile.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export compressPhp = ->
1010
mode: if isWindows then "safe" else "fast"
1111
silent: process.stdout.isTTY
1212

13-
return gulp.src "path/to/**/*.php", read: false
13+
gulp.src "path/to/**/*.php", read: false
1414
.pipe phpMinify options
1515
.pipe gulp.dest "path/to/out"

lib/fast_transformer.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Transformer} from "./transformer.js";
2+
3+
/**
4+
* Removes comments and whitespace from a PHP script, by calling a Web service.
5+
*/
6+
export class FastTransformer implements Transformer {
7+
8+
/**
9+
* Creates a new fast transformer.
10+
* @param executable The path to the PHP executable.
11+
*/
12+
constructor(executable?: string);
13+
14+
/**
15+
* Closes this transformer and releases any resources associated with it.
16+
* @returns Resolves when the transformer is finally disposed.
17+
*/
18+
close(): Promise<void>;
19+
20+
/**
21+
* Processes a PHP script.
22+
* @param file The path to the PHP script.
23+
* @returns The transformed script.
24+
*/
25+
transform(file: string): Promise<string>;
26+
}

lib/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Transform} from "node:stream";
2+
export * from "./fast_transformer.js";
23
export * from "./safe_transformer.js";
34
export * from "./transformer.js";
45

lib/safe_transformer.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import {Transformer} from "./transformer.js";
55
*/
66
export class SafeTransformer implements Transformer {
77

8-
/**
9-
* The path to the PHP executable.
10-
*/
11-
executable: string;
12-
138
/**
149
* Creates a new safe transformer.
1510
* @param executable The path to the PHP executable.

src/cli.coffee

Whitespace-only changes.

src/fast_transformer.coffee

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {spawn} from "node:child_process"
2+
import {createServer} from "node:net"
3+
import {join, normalize, resolve} from "node:path"
4+
import {setTimeout} from "node:timers"
5+
6+
# Removes comments and whitespace from a PHP script, by calling a Web service.
7+
export class FastTransformer
8+
9+
# Creates a new fast transformer.
10+
constructor: (executable = "php") ->
11+
12+
# The path to the PHP executable.
13+
@_executable = normalize executable
14+
15+
# The port that the PHP process is listening on.
16+
@_port = -1
17+
18+
# The underlying PHP process.
19+
@_process = null
20+
21+
# Closes this transformer and releases any resources associated with it.
22+
close: ->
23+
@_process?.kill()
24+
@_process = null
25+
Promise.resolve()
26+
27+
# Starts the underlying PHP process and begins accepting connections.
28+
listen: -> if @_process? then Promise.resolve @_port else
29+
@_port = await @_getPort()
30+
args = ["-S", "127.0.0.1:#{@_port}", "-t", join(import.meta.dirname, "../www")]
31+
new Promise (fulfill, reject) =>
32+
spawn @_executable, args, stdio: ["ignore", "pipe", "ignore"]
33+
.on "error", reject
34+
.on "spawn", => setTimeout (=> fulfill @_port), 1000
35+
36+
# Processes a PHP script.
37+
transform: (file) ->
38+
await @listen()
39+
url = new URL "http://127.0.0.1:#{@_port}/index.php"
40+
url.searchParams.set "file", resolve(file)
41+
42+
response = await fetch url
43+
throw Error("An error occurred while processing the script: #{file}") unless response.ok
44+
response.text()
45+
46+
# Gets an ephemeral TCP port chosen by the system.
47+
_getPort: -> new Promise (fulfill, reject) ->
48+
socket = createServer().on "error", reject
49+
socket.unref()
50+
socket.listen host: "127.0.0.1", port: 0, ->
51+
{port} = socket.address()
52+
socket.close -> fulfill port

src/gulp_plugin.coffee

Whitespace-only changes.

src/index.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./fast_transformer.js"
12
export * from "./safe_transformer.js"

src/php_minifier/FastTransformer.hx

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/php_minifier/SafeTransformer.hx

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)