@php-wasm/util
Advanced tools
Comparing version 0.6.4 to 0.6.5
302
index.js
@@ -1,301 +0,1 @@ | ||
// packages/php-wasm/util/src/lib/semaphore.ts | ||
var Semaphore = class { | ||
constructor({ concurrency }) { | ||
this._running = 0; | ||
this.concurrency = concurrency; | ||
this.queue = []; | ||
} | ||
get running() { | ||
return this._running; | ||
} | ||
async acquire() { | ||
while (true) { | ||
if (this._running >= this.concurrency) { | ||
await new Promise((resolve) => this.queue.push(resolve)); | ||
} else { | ||
this._running++; | ||
let released = false; | ||
return () => { | ||
if (released) { | ||
return; | ||
} | ||
released = true; | ||
this._running--; | ||
if (this.queue.length > 0) { | ||
this.queue.shift()(); | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
async run(fn) { | ||
const release = await this.acquire(); | ||
try { | ||
return await fn(); | ||
} finally { | ||
release(); | ||
} | ||
} | ||
}; | ||
// packages/php-wasm/util/src/lib/paths.ts | ||
function joinPaths(...paths) { | ||
let path = paths.join("/"); | ||
const isAbsolute = path[0] === "/"; | ||
const trailingSlash = path.substring(path.length - 1) === "/"; | ||
path = normalizePath(path); | ||
if (!path && !isAbsolute) { | ||
path = "."; | ||
} | ||
if (path && trailingSlash) { | ||
path += "/"; | ||
} | ||
return path; | ||
} | ||
function dirname(path) { | ||
if (path === "/") { | ||
return "/"; | ||
} | ||
path = normalizePath(path); | ||
const lastSlash = path.lastIndexOf("/"); | ||
if (lastSlash === -1) { | ||
return ""; | ||
} else if (lastSlash === 0) { | ||
return "/"; | ||
} | ||
return path.substr(0, lastSlash); | ||
} | ||
function basename(path) { | ||
if (path === "/") { | ||
return "/"; | ||
} | ||
path = normalizePath(path); | ||
const lastSlash = path.lastIndexOf("/"); | ||
if (lastSlash === -1) { | ||
return path; | ||
} | ||
return path.substr(lastSlash + 1); | ||
} | ||
function normalizePath(path) { | ||
const isAbsolute = path[0] === "/"; | ||
path = normalizePathsArray( | ||
path.split("/").filter((p) => !!p), | ||
!isAbsolute | ||
).join("/"); | ||
return (isAbsolute ? "/" : "") + path.replace(/\/$/, ""); | ||
} | ||
function normalizePathsArray(parts, allowAboveRoot) { | ||
let up = 0; | ||
for (let i = parts.length - 1; i >= 0; i--) { | ||
const last = parts[i]; | ||
if (last === ".") { | ||
parts.splice(i, 1); | ||
} else if (last === "..") { | ||
parts.splice(i, 1); | ||
up++; | ||
} else if (up) { | ||
parts.splice(i, 1); | ||
up--; | ||
} | ||
} | ||
if (allowAboveRoot) { | ||
for (; up; up--) { | ||
parts.unshift(".."); | ||
} | ||
} | ||
return parts; | ||
} | ||
// packages/php-wasm/util/src/lib/split-shell-command.ts | ||
function splitShellCommand(command) { | ||
const MODE_UNQUOTED = 0; | ||
const MODE_IN_QUOTE = 1; | ||
let mode = MODE_UNQUOTED; | ||
let quote = ""; | ||
const parts = []; | ||
let currentPart = ""; | ||
for (let i = 0; i < command.length; i++) { | ||
const char = command[i]; | ||
if (char === "\\") { | ||
if (command[i + 1] === '"' || command[i + 1] === "'") { | ||
i++; | ||
} | ||
currentPart += command[i]; | ||
} else if (mode === MODE_UNQUOTED) { | ||
if (char === '"' || char === "'") { | ||
mode = MODE_IN_QUOTE; | ||
quote = char; | ||
} else if (char.match(/\s/)) { | ||
if (currentPart.trim().length) { | ||
parts.push(currentPart.trim()); | ||
} | ||
currentPart = char; | ||
} else if (parts.length && !currentPart) { | ||
currentPart = parts.pop() + char; | ||
} else { | ||
currentPart += char; | ||
} | ||
} else if (mode === MODE_IN_QUOTE) { | ||
if (char === quote) { | ||
mode = MODE_UNQUOTED; | ||
quote = ""; | ||
} else { | ||
currentPart += char; | ||
} | ||
} | ||
} | ||
if (currentPart) { | ||
parts.push(currentPart.trim()); | ||
} | ||
return parts; | ||
} | ||
// packages/php-wasm/util/src/lib/create-spawn-handler.ts | ||
function createSpawnHandler(program) { | ||
return function(command, argsArray = [], options = {}) { | ||
const childProcess = new ChildProcess(); | ||
const processApi = new ProcessApi(childProcess); | ||
setTimeout(async () => { | ||
let commandArray = []; | ||
if (argsArray.length) { | ||
commandArray = [command, ...argsArray]; | ||
} else if (typeof command === "string") { | ||
commandArray = splitShellCommand(command); | ||
} else if (Array.isArray(command)) { | ||
commandArray = command; | ||
} else { | ||
throw new Error("Invalid command ", command); | ||
} | ||
await program(commandArray, processApi, options); | ||
childProcess.emit("spawn", true); | ||
}); | ||
return childProcess; | ||
}; | ||
} | ||
var EventEmitter = class { | ||
constructor() { | ||
this.listeners = {}; | ||
} | ||
emit(eventName, data) { | ||
if (this.listeners[eventName]) { | ||
this.listeners[eventName].forEach(function(listener) { | ||
listener(data); | ||
}); | ||
} | ||
} | ||
on(eventName, listener) { | ||
if (!this.listeners[eventName]) { | ||
this.listeners[eventName] = []; | ||
} | ||
this.listeners[eventName].push(listener); | ||
} | ||
}; | ||
var ProcessApi = class extends EventEmitter { | ||
constructor(childProcess) { | ||
super(); | ||
this.childProcess = childProcess; | ||
this.exited = false; | ||
this.stdinData = []; | ||
childProcess.on("stdin", (data) => { | ||
if (this.stdinData) { | ||
this.stdinData.push(data.slice()); | ||
} else { | ||
this.emit("stdin", data); | ||
} | ||
}); | ||
} | ||
stdout(data) { | ||
if (typeof data === "string") { | ||
data = new TextEncoder().encode(data); | ||
} | ||
this.childProcess.stdout.emit("data", data); | ||
} | ||
stdoutEnd() { | ||
this.childProcess.stdout.emit("end", {}); | ||
} | ||
stderr(data) { | ||
if (typeof data === "string") { | ||
data = new TextEncoder().encode(data); | ||
} | ||
this.childProcess.stderr.emit("data", data); | ||
} | ||
stderrEnd() { | ||
this.childProcess.stderr.emit("end", {}); | ||
} | ||
exit(code) { | ||
if (!this.exited) { | ||
this.exited = true; | ||
this.childProcess.emit("exit", code); | ||
} | ||
} | ||
flushStdin() { | ||
if (this.stdinData) { | ||
for (let i = 0; i < this.stdinData.length; i++) { | ||
this.emit("stdin", this.stdinData[i]); | ||
} | ||
} | ||
this.stdinData = null; | ||
} | ||
}; | ||
var lastPid = 9743; | ||
var ChildProcess = class extends EventEmitter { | ||
constructor(pid = lastPid++) { | ||
super(); | ||
this.pid = pid; | ||
this.stdout = new EventEmitter(); | ||
this.stderr = new EventEmitter(); | ||
const self = this; | ||
this.stdin = { | ||
write: (data) => { | ||
self.emit("stdin", data); | ||
} | ||
}; | ||
} | ||
}; | ||
// packages/php-wasm/util/src/lib/random-string.ts | ||
function randomString(length = 36, specialChars = "!@#$%^&*()_+=-[]/.,<>?") { | ||
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + specialChars; | ||
let result = ""; | ||
for (let i = length; i > 0; --i) | ||
result += chars[Math.floor(Math.random() * chars.length)]; | ||
return result; | ||
} | ||
// packages/php-wasm/util/src/lib/random-filename.ts | ||
function randomFilename() { | ||
return randomString(36, "-_"); | ||
} | ||
// packages/php-wasm/util/src/lib/php-vars.ts | ||
function phpVar(value) { | ||
return `json_decode(base64_decode('${stringToBase64( | ||
JSON.stringify(value) | ||
)}'), true)`; | ||
} | ||
function phpVars(vars) { | ||
const result = {}; | ||
for (const key in vars) { | ||
result[key] = phpVar(vars[key]); | ||
} | ||
return result; | ||
} | ||
function stringToBase64(str) { | ||
return bytesToBase64(new TextEncoder().encode(str)); | ||
} | ||
function bytesToBase64(bytes) { | ||
const binString = String.fromCodePoint(...bytes); | ||
return btoa(binString); | ||
} | ||
export { | ||
Semaphore, | ||
basename, | ||
createSpawnHandler, | ||
dirname, | ||
joinPaths, | ||
normalizePath, | ||
phpVar, | ||
phpVars, | ||
randomFilename, | ||
randomString | ||
}; | ||
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class d{constructor({concurrency:t}){this._running=0,this.concurrency=t,this.queue=[]}get running(){return this._running}async acquire(){for(;;)if(this._running>=this.concurrency)await new Promise(t=>this.queue.push(t));else{this._running++;let t=!1;return()=>{t||(t=!0,this._running--,this.queue.length>0&&this.queue.shift()())}}}async run(t){const i=await this.acquire();try{return await t()}finally{i()}}}function g(...e){let t=e.join("/");const i=t[0]==="/",s=t.substring(t.length-1)==="/";return t=h(t),!t&&!i&&(t="."),t&&s&&(t+="/"),t}function E(e){if(e==="/")return"/";e=h(e);const t=e.lastIndexOf("/");return t===-1?"":t===0?"/":e.substr(0,t)}function m(e){if(e==="/")return"/";e=h(e);const t=e.lastIndexOf("/");return t===-1?e:e.substr(t+1)}function h(e){const t=e[0]==="/";return e=p(e.split("/").filter(i=>!!i),!t).join("/"),(t?"/":"")+e.replace(/\/$/,"")}function p(e,t){let i=0;for(let s=e.length-1;s>=0;s--){const r=e[s];r==="."?e.splice(s,1):r===".."?(e.splice(s,1),i++):i&&(e.splice(s,1),i--)}if(t)for(;i;i--)e.unshift("..");return e}function w(e){let s=0,r="";const l=[];let n="";for(let u=0;u<e.length;u++){const o=e[u];o==="\\"?((e[u+1]==='"'||e[u+1]==="'")&&u++,n+=e[u]):s===0?o==='"'||o==="'"?(s=1,r=o):o.match(/\s/)?(n.trim().length&&l.push(n.trim()),n=o):l.length&&!n?n=l.pop()+o:n+=o:s===1&&(o===r?(s=0,r=""):n+=o)}return n&&l.push(n.trim()),l}function y(e){return function(t,i=[],s={}){const r=new P,l=new D(r);return setTimeout(async()=>{let n=[];if(i.length)n=[t,...i];else if(typeof t=="string")n=w(t);else if(Array.isArray(t))n=t;else throw new Error("Invalid command ",t);await e(n,l,s),r.emit("spawn",!0)}),r}}class c{constructor(){this.listeners={}}emit(t,i){this.listeners[t]&&this.listeners[t].forEach(function(s){s(i)})}on(t,i){this.listeners[t]||(this.listeners[t]=[]),this.listeners[t].push(i)}}class D extends c{constructor(t){super(),this.childProcess=t,this.exited=!1,this.stdinData=[],t.on("stdin",i=>{this.stdinData?this.stdinData.push(i.slice()):this.emit("stdin",i)})}stdout(t){typeof t=="string"&&(t=new TextEncoder().encode(t)),this.childProcess.stdout.emit("data",t)}stdoutEnd(){this.childProcess.stdout.emit("end",{})}stderr(t){typeof t=="string"&&(t=new TextEncoder().encode(t)),this.childProcess.stderr.emit("data",t)}stderrEnd(){this.childProcess.stderr.emit("end",{})}exit(t){this.exited||(this.exited=!0,this.childProcess.emit("exit",t))}flushStdin(){if(this.stdinData)for(let t=0;t<this.stdinData.length;t++)this.emit("stdin",this.stdinData[t]);this.stdinData=null}}let O=9743;class P extends c{constructor(t=O++){super(),this.pid=t,this.stdout=new c,this.stderr=new c;const i=this;this.stdin={write:s=>{i.emit("stdin",s)}}}}function f(e=36,t="!@#$%^&*()_+=-[]/.,<>?"){const i="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+t;let s="";for(let r=e;r>0;--r)s+=i[Math.floor(Math.random()*i.length)];return s}function _(){return f(36,"-_")}function a(e){return`json_decode(base64_decode('${T(JSON.stringify(e))}'), true)`}function S(e){const t={};for(const i in e)t[i]=a(e[i]);return t}function T(e){return b(new TextEncoder().encode(e))}function b(e){const t=String.fromCodePoint(...e);return btoa(t)}exports.Semaphore=d;exports.basename=m;exports.createSpawnHandler=y;exports.dirname=E;exports.joinPaths=g;exports.normalizePath=h;exports.phpVar=a;exports.phpVars=S;exports.randomFilename=_;exports.randomString=f; |
{ | ||
"name": "@php-wasm/util", | ||
"version": "0.6.4", | ||
"version": "0.6.5", | ||
"type": "commonjs", | ||
@@ -15,3 +15,3 @@ "typedoc": { | ||
}, | ||
"gitHead": "8b74852e9701f5083b367f9a294f34200230ce79", | ||
"gitHead": "eb45486a61d06a8a9e62cbd65348bfb274ff9e7c", | ||
"engines": { | ||
@@ -18,0 +18,0 @@ "node": ">=18.18.2", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
8447
199
1
0
1