@php-wasm/util 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +1 -1
  2. package/index.mjs +102 -96
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=Symbol("SleepFinished");function g(t){return new Promise(e=>{setTimeout(()=>e(f),t)})}class m extends Error{constructor(){super("Acquiring lock timed out")}}class p{constructor({concurrency:e,timeout:i}){this._running=0,this.concurrency=e,this.timeout=i,this.queue=[]}get remaining(){return this.concurrency-this.running}get running(){return this._running}async acquire(){for(;;)if(this._running>=this.concurrency){const e=new Promise(i=>{this.queue.push(i)});this.timeout!==void 0?await Promise.race([e,g(this.timeout)]).then(i=>{if(i===f)throw new m}):await e}else{this._running++;let e=!1;return()=>{e||(e=!0,this._running--,this.queue.length>0&&this.queue.shift()())}}}async run(e){const i=await this.acquire();try{return await e()}finally{i()}}}class E extends Error{constructor(e,i){super(e),this.userFriendlyMessage=i,this.userFriendlyMessage||(this.userFriendlyMessage=e)}}function w(...t){let e=t.join("/");const i=e[0]==="/",s=e.substring(e.length-1)==="/";return e=h(e),!e&&!i&&(e="."),e&&s&&(e+="/"),e}function y(t){if(t==="/")return"/";t=h(t);const e=t.lastIndexOf("/");return e===-1?"":e===0?"/":t.substr(0,e)}function P(t){if(t==="/")return"/";t=h(t);const e=t.lastIndexOf("/");return e===-1?t:t.substr(e+1)}function h(t){const e=t[0]==="/";return t=S(t.split("/").filter(i=>!!i),!e).join("/"),(e?"/":"")+t.replace(/\/$/,"")}function S(t,e){let i=0;for(let s=t.length-1;s>=0;s--){const r=t[s];r==="."?t.splice(s,1):r===".."?(t.splice(s,1),i++):i&&(t.splice(s,1),i--)}if(e)for(;i;i--)t.unshift("..");return t}function D(t){let s=0,r="";const u=[];let n="";for(let l=0;l<t.length;l++){const o=t[l];o==="\\"?((t[l+1]==='"'||t[l+1]==="'")&&l++,n+=t[l]):s===0?o==='"'||o==="'"?(s=1,r=o):o.match(/\s/)?(n.trim().length&&u.push(n.trim()),n=o):u.length&&!n?n=u.pop()+o:n+=o:s===1&&(o===r?(s=0,r=""):n+=o)}return n&&u.push(n.trim()),u}function O(t){return function(e,i=[],s={}){const r=new b,u=new _(r);return setTimeout(async()=>{let n=[];if(i.length)n=[e,...i];else if(typeof e=="string")n=D(e);else if(Array.isArray(e))n=e;else throw new Error("Invalid command ",e);await t(n,u,s),r.emit("spawn",!0)}),r}}class c{constructor(){this.listeners={}}emit(e,i){this.listeners[e]&&this.listeners[e].forEach(function(s){s(i)})}on(e,i){this.listeners[e]||(this.listeners[e]=[]),this.listeners[e].push(i)}}class _ extends c{constructor(e){super(),this.childProcess=e,this.exited=!1,this.stdinData=[],e.on("stdin",i=>{this.stdinData?this.stdinData.push(i.slice()):this.emit("stdin",i)})}stdout(e){typeof e=="string"&&(e=new TextEncoder().encode(e)),this.childProcess.stdout.emit("data",e)}stdoutEnd(){this.childProcess.stdout.emit("end",{})}stderr(e){typeof e=="string"&&(e=new TextEncoder().encode(e)),this.childProcess.stderr.emit("data",e)}stderrEnd(){this.childProcess.stderr.emit("end",{})}exit(e){this.exited||(this.exited=!0,this.childProcess.emit("exit",e))}flushStdin(){if(this.stdinData)for(let e=0;e<this.stdinData.length;e++)this.emit("stdin",this.stdinData[e]);this.stdinData=null}}let T=9743;class b extends c{constructor(e=T++){super(),this.pid=e,this.stdout=new c,this.stderr=new c;const i=this;this.stdin={write:s=>{i.emit("stdin",s)}}}}function a(t=36,e="!@#$%^&*()_+=-[]/.,<>?"){const i="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+e;let s="";for(let r=t;r>0;--r)s+=i[Math.floor(Math.random()*i.length)];return s}function x(){return a(36,"-_")}function d(t){return`json_decode(base64_decode('${U(JSON.stringify(t))}'), true)`}function M(t){const e={};for(const i in t)e[i]=d(t[i]);return e}function U(t){return q(new TextEncoder().encode(t))}function q(t){const e=String.fromCodePoint(...t);return btoa(e)}exports.PhpWasmError=E;exports.Semaphore=p;exports.basename=P;exports.createSpawnHandler=O;exports.dirname=y;exports.joinPaths=w;exports.normalizePath=h;exports.phpVar=d;exports.phpVars=M;exports.randomFilename=x;exports.randomString=a;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=Symbol("SleepFinished");function m(t){return new Promise(e=>{setTimeout(()=>e(f),t)})}class a extends Error{constructor(){super("Acquiring lock timed out")}}class p{constructor({concurrency:e,timeout:i}){this._running=0,this.concurrency=e,this.timeout=i,this.queue=[]}get remaining(){return this.concurrency-this.running}get running(){return this._running}async acquire(){for(;;)if(this._running>=this.concurrency){const e=new Promise(i=>{this.queue.push(i)});this.timeout!==void 0?await Promise.race([e,m(this.timeout)]).then(i=>{if(i===f)throw new a}):await e}else{this._running++;let e=!1;return()=>{e||(e=!0,this._running--,this.queue.length>0&&this.queue.shift()())}}}async run(e){const i=await this.acquire();try{return await e()}finally{i()}}}class E extends Error{constructor(e,i){super(e),this.userFriendlyMessage=i,this.userFriendlyMessage||(this.userFriendlyMessage=e)}}function y(...t){let e=t.join("/");const i=e[0]==="/",s=e.substring(e.length-1)==="/";return e=h(e),!e&&!i&&(e="."),e&&s&&(e+="/"),e}function w(t){if(t==="/")return"/";t=h(t);const e=t.lastIndexOf("/");return e===-1?"":e===0?"/":t.substr(0,e)}function P(t){if(t==="/")return"/";t=h(t);const e=t.lastIndexOf("/");return e===-1?t:t.substr(e+1)}function h(t){const e=t[0]==="/";return t=S(t.split("/").filter(i=>!!i),!e).join("/"),(e?"/":"")+t.replace(/\/$/,"")}function S(t,e){let i=0;for(let s=t.length-1;s>=0;s--){const n=t[s];n==="."?t.splice(s,1):n===".."?(t.splice(s,1),i++):i&&(t.splice(s,1),i--)}if(e)for(;i;i--)t.unshift("..");return t}function D(t){let s=0,n="";const l=[];let r="";for(let o=0;o<t.length;o++){const u=t[o];u==="\\"?((t[o+1]==='"'||t[o+1]==="'")&&o++,r+=t[o]):s===0?u==='"'||u==="'"?(s=1,n=u):u.match(/\s/)?(r.trim().length&&l.push(r.trim()),r=u):l.length&&!r?r=l.pop()+u:r+=u:s===1&&(u===n?(s=0,n=""):r+=u)}return r&&l.push(r.trim()),l}function O(t){return function(e,i=[],s={}){const n=new b,l=new _(n);return setTimeout(async()=>{let r=[];if(i.length)r=[e,...i];else if(typeof e=="string")r=D(e);else if(Array.isArray(e))r=e;else throw new Error("Invalid command ",e);try{await t(r,l,s)}catch(o){n.emit("error",o),typeof o=="object"&&o!==null&&"message"in o&&typeof o.message=="string"&&l.stderr(o.message),l.exit(1)}n.emit("spawn",!0)}),n}}class c{constructor(){this.listeners={}}emit(e,i){this.listeners[e]&&this.listeners[e].forEach(function(s){s(i)})}on(e,i){this.listeners[e]||(this.listeners[e]=[]),this.listeners[e].push(i)}}class _ extends c{constructor(e){super(),this.childProcess=e,this.exited=!1,this.stdinData=[],e.on("stdin",i=>{this.stdinData?this.stdinData.push(i.slice()):this.emit("stdin",i)})}stdout(e){typeof e=="string"&&(e=new TextEncoder().encode(e)),this.childProcess.stdout.emit("data",e)}stdoutEnd(){this.childProcess.stdout.emit("end",{})}stderr(e){typeof e=="string"&&(e=new TextEncoder().encode(e)),this.childProcess.stderr.emit("data",e)}stderrEnd(){this.childProcess.stderr.emit("end",{})}exit(e){this.exited||(this.exited=!0,this.childProcess.emit("exit",e))}flushStdin(){if(this.stdinData)for(let e=0;e<this.stdinData.length;e++)this.emit("stdin",this.stdinData[e]);this.stdinData=null}}let T=9743;class b extends c{constructor(e=T++){super(),this.pid=e,this.stdout=new c,this.stderr=new c;const i=this;this.stdin={write:s=>{i.emit("stdin",s)}}}}function d(t=36,e="!@#$%^&*()_+=-[]/.,<>?"){const i="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+e;let s="";for(let n=t;n>0;--n)s+=i[Math.floor(Math.random()*i.length)];return s}function x(){return d(36,"-_")}function g(t){return`json_decode(base64_decode('${q(JSON.stringify(t))}'), true)`}function M(t){const e={};for(const i in t)e[i]=g(t[i]);return e}function q(t){return U(new TextEncoder().encode(t))}function U(t){const e=String.fromCodePoint(...t);return btoa(e)}exports.AcquireTimeoutError=a;exports.PhpWasmError=E;exports.Semaphore=p;exports.basename=P;exports.createSpawnHandler=O;exports.dirname=w;exports.joinPaths=y;exports.normalizePath=h;exports.phpVar=g;exports.phpVars=M;exports.randomFilename=x;exports.randomString=d;
package/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const f = Symbol("SleepFinished");
2
- function d(e) {
3
- return new Promise((t) => {
4
- setTimeout(() => t(f), e);
2
+ function d(t) {
3
+ return new Promise((e) => {
4
+ setTimeout(() => e(f), t);
5
5
  });
6
6
  }
7
7
  class a extends Error {
@@ -10,8 +10,8 @@ class a extends Error {
10
10
  }
11
11
  }
12
12
  class O {
13
- constructor({ concurrency: t, timeout: s }) {
14
- this._running = 0, this.concurrency = t, this.timeout = s, this.queue = [];
13
+ constructor({ concurrency: e, timeout: s }) {
14
+ this._running = 0, this.concurrency = e, this.timeout = s, this.queue = [];
15
15
  }
16
16
  get remaining() {
17
17
  return this.concurrency - this.running;
@@ -22,98 +22,103 @@ class O {
22
22
  async acquire() {
23
23
  for (; ; )
24
24
  if (this._running >= this.concurrency) {
25
- const t = new Promise((s) => {
25
+ const e = new Promise((s) => {
26
26
  this.queue.push(s);
27
27
  });
28
- this.timeout !== void 0 ? await Promise.race([t, d(this.timeout)]).then(
28
+ this.timeout !== void 0 ? await Promise.race([e, d(this.timeout)]).then(
29
29
  (s) => {
30
30
  if (s === f)
31
31
  throw new a();
32
32
  }
33
- ) : await t;
33
+ ) : await e;
34
34
  } else {
35
35
  this._running++;
36
- let t = !1;
36
+ let e = !1;
37
37
  return () => {
38
- t || (t = !0, this._running--, this.queue.length > 0 && this.queue.shift()());
38
+ e || (e = !0, this._running--, this.queue.length > 0 && this.queue.shift()());
39
39
  };
40
40
  }
41
41
  }
42
- async run(t) {
42
+ async run(e) {
43
43
  const s = await this.acquire();
44
44
  try {
45
- return await t();
45
+ return await e();
46
46
  } finally {
47
47
  s();
48
48
  }
49
49
  }
50
50
  }
51
51
  class x extends Error {
52
- constructor(t, s) {
53
- super(t), this.userFriendlyMessage = s, this.userFriendlyMessage || (this.userFriendlyMessage = t);
52
+ constructor(e, s) {
53
+ super(e), this.userFriendlyMessage = s, this.userFriendlyMessage || (this.userFriendlyMessage = e);
54
54
  }
55
55
  }
56
- function T(...e) {
57
- let t = e.join("/");
58
- const s = t[0] === "/", i = t.substring(t.length - 1) === "/";
59
- return t = h(t), !t && !s && (t = "."), t && i && (t += "/"), t;
56
+ function T(...t) {
57
+ let e = t.join("/");
58
+ const s = e[0] === "/", i = e.substring(e.length - 1) === "/";
59
+ return e = h(e), !e && !s && (e = "."), e && i && (e += "/"), e;
60
60
  }
61
- function S(e) {
62
- if (e === "/")
61
+ function S(t) {
62
+ if (t === "/")
63
63
  return "/";
64
- e = h(e);
65
- const t = e.lastIndexOf("/");
66
- return t === -1 ? "" : t === 0 ? "/" : e.substr(0, t);
64
+ t = h(t);
65
+ const e = t.lastIndexOf("/");
66
+ return e === -1 ? "" : e === 0 ? "/" : t.substr(0, e);
67
67
  }
68
- function M(e) {
69
- if (e === "/")
68
+ function b(t) {
69
+ if (t === "/")
70
70
  return "/";
71
- e = h(e);
72
- const t = e.lastIndexOf("/");
73
- return t === -1 ? e : e.substr(t + 1);
74
- }
75
- function h(e) {
76
- const t = e[0] === "/";
77
- return e = g(
78
- e.split("/").filter((s) => !!s),
79
- !t
80
- ).join("/"), (t ? "/" : "") + e.replace(/\/$/, "");
81
- }
82
- function g(e, t) {
71
+ t = h(t);
72
+ const e = t.lastIndexOf("/");
73
+ return e === -1 ? t : t.substr(e + 1);
74
+ }
75
+ function h(t) {
76
+ const e = t[0] === "/";
77
+ return t = g(
78
+ t.split("/").filter((s) => !!s),
79
+ !e
80
+ ).join("/"), (e ? "/" : "") + t.replace(/\/$/, "");
81
+ }
82
+ function g(t, e) {
83
83
  let s = 0;
84
- for (let i = e.length - 1; i >= 0; i--) {
85
- const r = e[i];
86
- r === "." ? e.splice(i, 1) : r === ".." ? (e.splice(i, 1), s++) : s && (e.splice(i, 1), s--);
84
+ for (let i = t.length - 1; i >= 0; i--) {
85
+ const r = t[i];
86
+ r === "." ? t.splice(i, 1) : r === ".." ? (t.splice(i, 1), s++) : s && (t.splice(i, 1), s--);
87
87
  }
88
- if (t)
88
+ if (e)
89
89
  for (; s; s--)
90
- e.unshift("..");
91
- return e;
90
+ t.unshift("..");
91
+ return t;
92
92
  }
93
- function E(e) {
93
+ function m(t) {
94
94
  let i = 0, r = "";
95
- const u = [];
95
+ const l = [];
96
96
  let n = "";
97
- for (let l = 0; l < e.length; l++) {
98
- const o = e[l];
99
- o === "\\" ? ((e[l + 1] === '"' || e[l + 1] === "'") && l++, n += e[l]) : i === 0 ? o === '"' || o === "'" ? (i = 1, r = o) : o.match(/\s/) ? (n.trim().length && u.push(n.trim()), n = o) : u.length && !n ? n = u.pop() + o : n += o : i === 1 && (o === r ? (i = 0, r = "") : n += o);
97
+ for (let o = 0; o < t.length; o++) {
98
+ const u = t[o];
99
+ u === "\\" ? ((t[o + 1] === '"' || t[o + 1] === "'") && o++, n += t[o]) : i === 0 ? u === '"' || u === "'" ? (i = 1, r = u) : u.match(/\s/) ? (n.trim().length && l.push(n.trim()), n = u) : l.length && !n ? n = l.pop() + u : n += u : i === 1 && (u === r ? (i = 0, r = "") : n += u);
100
100
  }
101
- return n && u.push(n.trim()), u;
101
+ return n && l.push(n.trim()), l;
102
102
  }
103
- function b(e) {
104
- return function(t, s = [], i = {}) {
105
- const r = new p(), u = new m(r);
103
+ function M(t) {
104
+ return function(e, s = [], i = {}) {
105
+ const r = new p(), l = new E(r);
106
106
  return setTimeout(async () => {
107
107
  let n = [];
108
108
  if (s.length)
109
- n = [t, ...s];
110
- else if (typeof t == "string")
111
- n = E(t);
112
- else if (Array.isArray(t))
113
- n = t;
109
+ n = [e, ...s];
110
+ else if (typeof e == "string")
111
+ n = m(e);
112
+ else if (Array.isArray(e))
113
+ n = e;
114
114
  else
115
- throw new Error("Invalid command ", t);
116
- await e(n, u, i), r.emit("spawn", !0);
115
+ throw new Error("Invalid command ", e);
116
+ try {
117
+ await t(n, l, i);
118
+ } catch (o) {
119
+ r.emit("error", o), typeof o == "object" && o !== null && "message" in o && typeof o.message == "string" && l.stderr(o.message), l.exit(1);
120
+ }
121
+ r.emit("spawn", !0);
117
122
  }), r;
118
123
  };
119
124
  }
@@ -121,47 +126,47 @@ class c {
121
126
  constructor() {
122
127
  this.listeners = {};
123
128
  }
124
- emit(t, s) {
125
- this.listeners[t] && this.listeners[t].forEach(function(i) {
129
+ emit(e, s) {
130
+ this.listeners[e] && this.listeners[e].forEach(function(i) {
126
131
  i(s);
127
132
  });
128
133
  }
129
- on(t, s) {
130
- this.listeners[t] || (this.listeners[t] = []), this.listeners[t].push(s);
134
+ on(e, s) {
135
+ this.listeners[e] || (this.listeners[e] = []), this.listeners[e].push(s);
131
136
  }
132
137
  }
133
- class m extends c {
134
- constructor(t) {
135
- super(), this.childProcess = t, this.exited = !1, this.stdinData = [], t.on("stdin", (s) => {
138
+ class E extends c {
139
+ constructor(e) {
140
+ super(), this.childProcess = e, this.exited = !1, this.stdinData = [], e.on("stdin", (s) => {
136
141
  this.stdinData ? this.stdinData.push(s.slice()) : this.emit("stdin", s);
137
142
  });
138
143
  }
139
- stdout(t) {
140
- typeof t == "string" && (t = new TextEncoder().encode(t)), this.childProcess.stdout.emit("data", t);
144
+ stdout(e) {
145
+ typeof e == "string" && (e = new TextEncoder().encode(e)), this.childProcess.stdout.emit("data", e);
141
146
  }
142
147
  stdoutEnd() {
143
148
  this.childProcess.stdout.emit("end", {});
144
149
  }
145
- stderr(t) {
146
- typeof t == "string" && (t = new TextEncoder().encode(t)), this.childProcess.stderr.emit("data", t);
150
+ stderr(e) {
151
+ typeof e == "string" && (e = new TextEncoder().encode(e)), this.childProcess.stderr.emit("data", e);
147
152
  }
148
153
  stderrEnd() {
149
154
  this.childProcess.stderr.emit("end", {});
150
155
  }
151
- exit(t) {
152
- this.exited || (this.exited = !0, this.childProcess.emit("exit", t));
156
+ exit(e) {
157
+ this.exited || (this.exited = !0, this.childProcess.emit("exit", e));
153
158
  }
154
159
  flushStdin() {
155
160
  if (this.stdinData)
156
- for (let t = 0; t < this.stdinData.length; t++)
157
- this.emit("stdin", this.stdinData[t]);
161
+ for (let e = 0; e < this.stdinData.length; e++)
162
+ this.emit("stdin", this.stdinData[e]);
158
163
  this.stdinData = null;
159
164
  }
160
165
  }
161
- let w = 9743;
166
+ let y = 9743;
162
167
  class p extends c {
163
- constructor(t = w++) {
164
- super(), this.pid = t, this.stdout = new c(), this.stderr = new c();
168
+ constructor(e = y++) {
169
+ super(), this.pid = e, this.stdout = new c(), this.stderr = new c();
165
170
  const s = this;
166
171
  this.stdin = {
167
172
  write: (i) => {
@@ -170,44 +175,45 @@ class p extends c {
170
175
  };
171
176
  }
172
177
  }
173
- function y(e = 36, t = "!@#$%^&*()_+=-[]/.,<>?") {
174
- const s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + t;
178
+ function w(t = 36, e = "!@#$%^&*()_+=-[]/.,<>?") {
179
+ const s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + e;
175
180
  let i = "";
176
- for (let r = e; r > 0; --r)
181
+ for (let r = t; r > 0; --r)
177
182
  i += s[Math.floor(Math.random() * s.length)];
178
183
  return i;
179
184
  }
180
185
  function U() {
181
- return y(36, "-_");
186
+ return w(36, "-_");
182
187
  }
183
- function D(e) {
188
+ function D(t) {
184
189
  return `json_decode(base64_decode('${P(
185
- JSON.stringify(e)
190
+ JSON.stringify(t)
186
191
  )}'), true)`;
187
192
  }
188
- function q(e) {
189
- const t = {};
190
- for (const s in e)
191
- t[s] = D(e[s]);
192
- return t;
193
+ function q(t) {
194
+ const e = {};
195
+ for (const s in t)
196
+ e[s] = D(t[s]);
197
+ return e;
193
198
  }
194
- function P(e) {
195
- return _(new TextEncoder().encode(e));
199
+ function P(t) {
200
+ return _(new TextEncoder().encode(t));
196
201
  }
197
- function _(e) {
198
- const t = String.fromCodePoint(...e);
199
- return btoa(t);
202
+ function _(t) {
203
+ const e = String.fromCodePoint(...t);
204
+ return btoa(e);
200
205
  }
201
206
  export {
207
+ a as AcquireTimeoutError,
202
208
  x as PhpWasmError,
203
209
  O as Semaphore,
204
- M as basename,
205
- b as createSpawnHandler,
210
+ b as basename,
211
+ M as createSpawnHandler,
206
212
  S as dirname,
207
213
  T as joinPaths,
208
214
  h as normalizePath,
209
215
  D as phpVar,
210
216
  q as phpVars,
211
217
  U as randomFilename,
212
- y as randomString
218
+ w as randomString
213
219
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/util",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "type": "commonjs",
5
5
  "typedoc": {
6
6
  "entryPoint": "./src/index.ts",
@@ -12,7 +12,7 @@
12
12
  "access": "public",
13
13
  "directory": "../../../dist/packages/php-wasm/util"
14
14
  },
15
- "gitHead": "ed0ec39040ab4cd69dc48a8eac157436b30f9d55",
15
+ "gitHead": "e8fedf92bbead5cec963c8d6976e7143d8e68721",
16
16
  "engines": {
17
17
  "node": ">=18.18.0",
18
18
  "npm": ">=8.11.0"