Browse Source

Updated the generated assets

master
Cédric Belin 6 months ago
parent
commit
ac190ce4db
3 changed files with 114 additions and 105 deletions
  1. +78
    -74
      lib/fast_transformer.js
  2. +1
    -0
      lib/index.js
  3. +35
    -31
      lib/safe_transformer.js

+ 78
- 74
lib/fast_transformer.js View File

@@ -11,86 +11,90 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
}
return privateMap.get(receiver);
};
var _executable, _port, _process;
import { spawn } from 'child_process';
import { createServer } from 'net';
import fetch from 'node-fetch';
import { dirname, normalize, join, resolve } from 'path';
import { fileURLToPath } from 'url';
/** Removes comments and whitespace from a PHP script, by calling a Web service. */
export class FastTransformer {
/**
* Creates a new fast transformer.
* @param executable The path to the PHP executable.
*/
constructor(executable = 'php') {
/** The path to the PHP executable. */
_executable.set(this, void 0);
/** The port that the PHP process is listening on. */
_port.set(this, -1);
/** The underlying PHP process. */
_process.set(this, void 0);
__classPrivateFieldSet(this, _executable, executable);
}
/** Value indicating whether the PHP process is currently listening. */
get listening() {
return Boolean(__classPrivateFieldGet(this, _process));
}
/**
* Closes this transformer and releases any resources associated with it.
* @return Completes when the transformer is finally disposed.
*/
close() {
if (this.listening) {
__classPrivateFieldGet(this, _process).kill();
__classPrivateFieldSet(this, _process, undefined);
let FastTransformer = /** @class */ (() => {
var _executable, _port, _process;
class FastTransformer {
/**
* Creates a new fast transformer.
* @param executable The path to the PHP executable.
*/
constructor(executable = 'php') {
/** The path to the PHP executable. */
_executable.set(this, void 0);
/** The port that the PHP process is listening on. */
_port.set(this, -1);
/** The underlying PHP process. */
_process.set(this, void 0);
__classPrivateFieldSet(this, _executable, executable);
}
return Promise.resolve();
}
/**
* Starts the underlying PHP process: begins accepting connections. It does nothing if the server is already started.
* @return The port used by the PHP process.
*/
async listen() {
if (this.listening)
return __classPrivateFieldGet(this, _port);
__classPrivateFieldSet(this, _port, await this._getPort());
return new Promise((fulfill, reject) => {
const libDir = dirname(fileURLToPath(import.meta.url));
const args = ['-S', `${FastTransformer.address}:${__classPrivateFieldGet(this, _port)}`, '-t', join(libDir, 'php')];
__classPrivateFieldSet(this, _process, spawn(normalize(__classPrivateFieldGet(this, _executable)), args));
__classPrivateFieldGet(this, _process).on('error', err => reject(err));
setTimeout(() => fulfill(__classPrivateFieldGet(this, _port)), 1000);
});
}
/**
* Processes a PHP script.
* @param script The path to the PHP script.
* @return The transformed script.
*/
async transform(script) {
const file = encodeURIComponent(resolve(script));
const port = await this.listen();
const res = await fetch(`http://${FastTransformer.address}:${port}/?file=${file}`);
if (!res.ok)
throw new Error('An error occurred while transforming the script.');
return res.text();
}
/**
* Gets an ephemeral port chosen by the system.
* @return A port that the server can listen on.
*/
_getPort() {
return new Promise((fulfill, reject) => {
const server = createServer().unref();
server.on('error', err => reject(err));
server.listen(0, FastTransformer.address, () => {
const { port } = server.address();
server.close(() => fulfill(port));
/** Value indicating whether the PHP process is currently listening. */
get listening() {
return Boolean(__classPrivateFieldGet(this, _process));
}
/**
* Closes this transformer and releases any resources associated with it.
* @return Completes when the transformer is finally disposed.
*/
close() {
if (this.listening) {
__classPrivateFieldGet(this, _process).kill();
__classPrivateFieldSet(this, _process, undefined);
}
return Promise.resolve();
}
/**
* Starts the underlying PHP process: begins accepting connections. It does nothing if the server is already started.
* @return The port used by the PHP process.
*/
async listen() {
if (this.listening)
return __classPrivateFieldGet(this, _port);
__classPrivateFieldSet(this, _port, await this._getPort());
return new Promise((fulfill, reject) => {
const libDir = dirname(fileURLToPath(import.meta.url));
const args = ['-S', `${FastTransformer.address}:${__classPrivateFieldGet(this, _port)}`, '-t', join(libDir, 'php')];
__classPrivateFieldSet(this, _process, spawn(normalize(__classPrivateFieldGet(this, _executable)), args));
__classPrivateFieldGet(this, _process).on('error', err => reject(err));
setTimeout(() => fulfill(__classPrivateFieldGet(this, _port)), 1000);
});
});
}
/**
* Processes a PHP script.
* @param script The path to the PHP script.
* @return The transformed script.
*/
async transform(script) {
const file = encodeURIComponent(resolve(script));
const port = await this.listen();
const res = await fetch(`http://${FastTransformer.address}:${port}/?file=${file}`);
if (!res.ok)
throw new Error('An error occurred while transforming the script.');
return res.text();
}
/**
* Gets an ephemeral port chosen by the system.
* @return A port that the server can listen on.
*/
_getPort() {
return new Promise((fulfill, reject) => {
const server = createServer().unref();
server.on('error', err => reject(err));
server.listen(0, FastTransformer.address, () => {
const { port } = server.address();
server.close(() => fulfill(port));
});
});
}
}
}
_executable = new WeakMap(), _port = new WeakMap(), _process = new WeakMap();
/** The address that the server is listening on. */
FastTransformer.address = '127.0.0.1';
_executable = new WeakMap(), _port = new WeakMap(), _process = new WeakMap();
/** The address that the server is listening on. */
FastTransformer.address = '127.0.0.1';
return FastTransformer;
})();
export { FastTransformer };

+ 1
- 0
lib/index.js View File

@@ -2,6 +2,7 @@ import { Minifier } from './minifier.js';
export * from './fast_transformer.js';
export * from './minifier.js';
export * from './safe_transformer.js';
export * from './transformer.js';
/**
* Creates a new minifier.
* @param options The minifier options.


+ 35
- 31
lib/safe_transformer.js View File

@@ -11,39 +11,43 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
}
return privateMap.get(receiver);
};
var _executable;
import { execFile } from 'child_process';
import { normalize, resolve } from 'path';
import { promisify } from 'util';
/** Removes comments and whitespace from a PHP script, by calling a PHP process. */
export class SafeTransformer {
/**
* Creates a new safe transformer.
* @param executable The path to the PHP executable.
*/
constructor(executable = 'php') {
/** The path to the PHP executable. */
_executable.set(this, void 0);
__classPrivateFieldSet(this, _executable, executable);
let SafeTransformer = /** @class */ (() => {
var _executable;
class SafeTransformer {
/**
* Creates a new safe transformer.
* @param executable The path to the PHP executable.
*/
constructor(executable = 'php') {
/** The path to the PHP executable. */
_executable.set(this, void 0);
__classPrivateFieldSet(this, _executable, executable);
}
/**
* Closes this transformer and releases any resources associated with it.
* @return Completes when the transformer is finally disposed.
*/
close() {
return Promise.resolve();
}
/**
* Processes a PHP script.
* @param script The path to the PHP script.
* @return The transformed script.
*/
async transform(script) {
const spawn = promisify(execFile);
const { stdout } = await spawn(normalize(__classPrivateFieldGet(this, _executable)), ['-w', resolve(script)], { maxBuffer: SafeTransformer.bufferSize });
return stdout;
}
}
/**
* Closes this transformer and releases any resources associated with it.
* @return Completes when the transformer is finally disposed.
*/
close() {
return Promise.resolve();
}
/**
* Processes a PHP script.
* @param script The path to the PHP script.
* @return The transformed script.
*/
async transform(script) {
const spawn = promisify(execFile);
const { stdout } = await spawn(normalize(__classPrivateFieldGet(this, _executable)), ['-w', resolve(script)], { maxBuffer: SafeTransformer.bufferSize });
return stdout;
}
}
_executable = new WeakMap();
/** The largest amount of data in bytes allowed on `stdout` or `stderr`. */
SafeTransformer.bufferSize = 10 * 1024 * 1024;
_executable = new WeakMap();
/** The largest amount of data in bytes allowed on `stdout` or `stderr`. */
SafeTransformer.bufferSize = 10 * 1024 * 1024;
return SafeTransformer;
})();
export { SafeTransformer };

Loading…
Cancel
Save