@php-wasm/util
Advanced tools
Comparing version
242
index.js
@@ -1,8 +0,8 @@ | ||
const h = Symbol("SleepFinished"); | ||
function a(e) { | ||
const a = Symbol("SleepFinished"); | ||
function d(e) { | ||
return new Promise((t) => { | ||
setTimeout(() => t(h), e); | ||
setTimeout(() => t(a), e); | ||
}); | ||
} | ||
class d extends Error { | ||
class g extends Error { | ||
constructor() { | ||
@@ -12,5 +12,5 @@ super("Acquiring lock timed out"); | ||
} | ||
class x { | ||
constructor({ concurrency: t, timeout: s }) { | ||
this._running = 0, this.concurrency = t, this.timeout = s, this.queue = []; | ||
class D { | ||
constructor({ concurrency: t, timeout: i }) { | ||
this._running = 0, this.concurrency = t, this.timeout = i, this.queue = []; | ||
} | ||
@@ -26,9 +26,9 @@ get remaining() { | ||
if (this._running >= this.concurrency) { | ||
const t = new Promise((s) => { | ||
this.queue.push(s); | ||
const t = new Promise((i) => { | ||
this.queue.push(i); | ||
}); | ||
this.timeout !== void 0 ? await Promise.race([t, a(this.timeout)]).then( | ||
(s) => { | ||
if (s === h) | ||
throw new d(); | ||
this.timeout !== void 0 ? await Promise.race([t, d(this.timeout)]).then( | ||
(i) => { | ||
if (i === a) | ||
throw new g(); | ||
} | ||
@@ -45,13 +45,13 @@ ) : await t; | ||
async run(t) { | ||
const s = await this.acquire(); | ||
const i = await this.acquire(); | ||
try { | ||
return await t(); | ||
} finally { | ||
s(); | ||
i(); | ||
} | ||
} | ||
} | ||
class S extends Error { | ||
constructor(t, s) { | ||
super(t), this.userFriendlyMessage = s ?? t; | ||
class _ extends Error { | ||
constructor(t, i) { | ||
super(t), this.userFriendlyMessage = i ?? t; | ||
} | ||
@@ -63,5 +63,5 @@ } | ||
} | ||
let s = e.join("/"); | ||
const i = s[0] === "/", n = t(s); | ||
return s = l(s), !s && !i && (s = "."), s && n && !t(s) && (s += "/"), s; | ||
let i = e.join("/"); | ||
const n = i[0] === "/", r = t(i); | ||
return i = l(i), !i && !n && (i = "."), i && r && !t(i) && (i += "/"), i; | ||
} | ||
@@ -75,3 +75,3 @@ function U(e) { | ||
} | ||
function b(e) { | ||
function A(e) { | ||
if (e === "/") | ||
@@ -85,51 +85,51 @@ return "/"; | ||
const t = e[0] === "/"; | ||
return e = g( | ||
e.split("/").filter((s) => !!s), | ||
return e = y( | ||
e.split("/").filter((i) => !!i), | ||
!t | ||
).join("/"), (t ? "/" : "") + e.replace(/\/$/, ""); | ||
} | ||
function g(e, t) { | ||
let s = 0; | ||
for (let i = e.length - 1; i >= 0; i--) { | ||
const n = e[i]; | ||
n === "." ? e.splice(i, 1) : n === ".." ? (e.splice(i, 1), s++) : s && (e.splice(i, 1), s--); | ||
function y(e, t) { | ||
let i = 0; | ||
for (let n = e.length - 1; n >= 0; n--) { | ||
const r = e[n]; | ||
r === "." ? e.splice(n, 1) : r === ".." ? (e.splice(n, 1), i++) : i && (e.splice(n, 1), i--); | ||
} | ||
if (t) | ||
for (; s; s--) | ||
for (; i; i--) | ||
e.unshift(".."); | ||
return e; | ||
} | ||
function A(e, t) { | ||
function M(e, t) { | ||
return e === "/" ? !0 : (e = l(e), t = l(t), t.startsWith(e + "/") || t === e); | ||
} | ||
function m(e) { | ||
let i = 0, n = ""; | ||
let n = 0, r = ""; | ||
const u = []; | ||
let r = ""; | ||
let s = ""; | ||
for (let o = 0; o < e.length; o++) { | ||
const c = e[o]; | ||
c === "\\" ? ((e[o + 1] === '"' || e[o + 1] === "'") && o++, r += e[o]) : i === 0 ? c === '"' || c === "'" ? (i = 1, n = c) : c.match(/\s/) ? (r.trim().length && u.push(r.trim()), r = c) : u.length && !r ? r = u.pop() + c : r += c : i === 1 && (c === n ? (i = 0, n = "") : r += c); | ||
c === "\\" ? ((e[o + 1] === '"' || e[o + 1] === "'") && o++, s += e[o]) : n === 0 ? c === '"' || c === "'" ? (n = 1, r = c) : c.match(/\s/) ? (s.trim().length && u.push(s.trim()), s = c) : u.length && !s ? s = u.pop() + c : s += c : n === 1 && (c === r ? (n = 0, r = "") : s += c); | ||
} | ||
return r && u.push(r.trim()), u; | ||
return s && u.push(s.trim()), u; | ||
} | ||
function q(e) { | ||
return function(t, s = [], i = {}) { | ||
const n = new y(), u = new E(n); | ||
return function(t, i = [], n = {}) { | ||
const r = new b(), u = new E(r); | ||
return setTimeout(async () => { | ||
let r = []; | ||
if (s.length) | ||
r = [t, ...s]; | ||
let s = []; | ||
if (i.length) | ||
s = [t, ...i]; | ||
else if (typeof t == "string") | ||
r = m(t); | ||
s = m(t); | ||
else if (Array.isArray(t)) | ||
r = t; | ||
s = t; | ||
else | ||
throw new Error("Invalid command ", t); | ||
try { | ||
await e(r, u, i); | ||
await e(s, u, n); | ||
} catch (o) { | ||
n.emit("error", o), typeof o == "object" && o !== null && "message" in o && typeof o.message == "string" && u.stderr(o.message), u.exit(1); | ||
r.emit("error", o), typeof o == "object" && o !== null && "message" in o && typeof o.message == "string" && u.stderr(o.message), u.exit(1); | ||
} | ||
n.emit("spawn", !0); | ||
}), n; | ||
r.emit("spawn", !0); | ||
}), r; | ||
}; | ||
@@ -141,9 +141,9 @@ } | ||
} | ||
emit(t, s) { | ||
this.listeners[t] && this.listeners[t].forEach(function(i) { | ||
i(s); | ||
emit(t, i) { | ||
this.listeners[t] && this.listeners[t].forEach(function(n) { | ||
n(i); | ||
}); | ||
} | ||
on(t, s) { | ||
this.listeners[t] || (this.listeners[t] = []), this.listeners[t].push(s); | ||
on(t, i) { | ||
this.listeners[t] || (this.listeners[t] = []), this.listeners[t].push(i); | ||
} | ||
@@ -153,4 +153,4 @@ } | ||
constructor(t) { | ||
super(), this.exited = !1, this.stdinData = [], this.childProcess = t, t.on("stdin", (s) => { | ||
this.stdinData ? this.stdinData.push(s.slice()) : this.emit("stdin", s); | ||
super(), this.exited = !1, this.stdinData = [], this.childProcess = t, t.on("stdin", (i) => { | ||
this.stdinData ? this.stdinData.push(i.slice()) : this.emit("stdin", i); | ||
}); | ||
@@ -183,10 +183,10 @@ } | ||
} | ||
let w = 9743; | ||
class y extends f { | ||
constructor(t = w++) { | ||
let p = 9743; | ||
class b extends f { | ||
constructor(t = p++) { | ||
super(), this.stdout = new f(), this.stderr = new f(); | ||
const s = this; | ||
const i = this; | ||
this.pid = t, this.stdin = { | ||
write: (i) => { | ||
s.emit("stdin", i); | ||
write: (n) => { | ||
i.emit("stdin", n); | ||
} | ||
@@ -196,59 +196,111 @@ }; | ||
} | ||
function p(e = 36, t = "!@#$%^&*()_+=-[]/.,<>?") { | ||
const s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + t; | ||
let i = ""; | ||
for (let n = e; n > 0; --n) | ||
i += s[Math.floor(Math.random() * s.length)]; | ||
return i; | ||
function w(e = 36, t = "!@#$%^&*()_+=-[]/.,<>?") { | ||
const i = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + t; | ||
let n = ""; | ||
for (let r = e; r > 0; --r) | ||
n += i[Math.floor(Math.random() * i.length)]; | ||
return n; | ||
} | ||
function M() { | ||
return p(36, "-_"); | ||
function N() { | ||
return w(36, "-_"); | ||
} | ||
function P(e) { | ||
return `json_decode(base64_decode('${D( | ||
function S(e) { | ||
return `json_decode(base64_decode('${P( | ||
JSON.stringify(e) | ||
)}'), true)`; | ||
} | ||
function Q(e) { | ||
function k(e) { | ||
const t = {}; | ||
for (const s in e) | ||
t[s] = P(e[s]); | ||
for (const i in e) | ||
t[i] = S(e[i]); | ||
return t; | ||
} | ||
function D(e) { | ||
return O(new TextEncoder().encode(e)); | ||
function P(e) { | ||
return x(new TextEncoder().encode(e)); | ||
} | ||
function O(e) { | ||
function x(e) { | ||
const t = String.fromCodePoint(...e); | ||
return btoa(t); | ||
} | ||
function _(e) { | ||
function I(e, ...t) { | ||
let i = "", n = 0; | ||
for (let r = 0; r < e.length; r++) | ||
if (e[r] === "%" && r + 1 < e.length) { | ||
r++; | ||
const u = e[r]; | ||
switch (u) { | ||
case "s": { | ||
const s = t[n++]; | ||
let o; | ||
if (typeof s == "object") | ||
try { | ||
o = JSON.stringify( | ||
s, | ||
// Represent bigint values as strings in JSON.stringify(). | ||
(c, h) => typeof h == "bigint" ? `0x${h.toString(16)}` : h, | ||
2 | ||
); | ||
} catch { | ||
} | ||
else | ||
o = String(s); | ||
i += o; | ||
break; | ||
} | ||
case "d": { | ||
const s = t[n++]; | ||
typeof s == "bigint" ? i += s.toString() : i += Math.floor(Number(s)); | ||
break; | ||
} | ||
case "f": { | ||
const s = t[n++]; | ||
i += Number(s); | ||
break; | ||
} | ||
case "x": { | ||
const s = t[n++]; | ||
typeof s == "bigint" ? i += s.toString(16) : i += Math.floor(Number(s)).toString(16); | ||
break; | ||
} | ||
case "%": { | ||
i += "%"; | ||
break; | ||
} | ||
default: | ||
i += "%" + u; | ||
} | ||
} else | ||
i += e[r]; | ||
return i; | ||
} | ||
function O(e) { | ||
let t = 0; | ||
e.forEach((n) => t += n.length); | ||
const s = new Uint8Array(t); | ||
let i = 0; | ||
return e.forEach((n) => { | ||
s.set(n, i), i += n.length; | ||
}), s; | ||
e.forEach((r) => t += r.length); | ||
const i = new Uint8Array(t); | ||
let n = 0; | ||
return e.forEach((r) => { | ||
i.set(r, n), n += r.length; | ||
}), i; | ||
} | ||
function I(e) { | ||
return _(e.map((t) => new Uint8Array(t))).buffer; | ||
function Q(e) { | ||
return O(e.map((t) => new Uint8Array(t))).buffer; | ||
} | ||
export { | ||
d as AcquireTimeoutError, | ||
S as PhpWasmError, | ||
x as Semaphore, | ||
b as basename, | ||
I as concatArrayBuffers, | ||
_ as concatUint8Arrays, | ||
g as AcquireTimeoutError, | ||
_ as PhpWasmError, | ||
D as Semaphore, | ||
A as basename, | ||
Q as concatArrayBuffers, | ||
O as concatUint8Arrays, | ||
q as createSpawnHandler, | ||
U as dirname, | ||
A as isParentOf, | ||
M as isParentOf, | ||
T as joinPaths, | ||
l as normalizePath, | ||
P as phpVar, | ||
Q as phpVars, | ||
M as randomFilename, | ||
p as randomString | ||
S as phpVar, | ||
k as phpVars, | ||
N as randomFilename, | ||
w as randomString, | ||
I as sprintf | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -10,3 +10,4 @@ import Semaphore, { AcquireTimeoutError } from './semaphore'; | ||
export * from './php-vars'; | ||
export * from './sprintf'; | ||
export declare function concatUint8Arrays(arrays: Uint8Array[]): Uint8Array; | ||
export declare function concatArrayBuffers(buffers: ArrayBuffer[]): ArrayBuffer; |
{ | ||
"name": "@php-wasm/util", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"type": "module", | ||
@@ -26,3 +26,3 @@ "types": "index.d.ts", | ||
"module": "./index.js", | ||
"gitHead": "977f8e90eabb2c8d4eed75677ddd9fb6c13274ae", | ||
"gitHead": "ed646326d99ba84bad911d65b6634265f508f073", | ||
"engines": { | ||
@@ -29,0 +29,0 @@ "node": ">=20.18.3", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
90199
12.27%19
5.56%512
15.32%