@php-wasm/util 0.6.3 → 0.6.5

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 (4) hide show
  1. package/index.js +1 -241
  2. package/index.mjs +185 -0
  3. package/package.json +2 -2
  4. package/README.md +0 -11
package/index.js CHANGED
@@ -1,241 +1 @@
1
- // packages/php-wasm/util/src/lib/semaphore.ts
2
- var Semaphore = class {
3
- constructor({ concurrency }) {
4
- this._running = 0;
5
- this.concurrency = concurrency;
6
- this.queue = [];
7
- }
8
- get running() {
9
- return this._running;
10
- }
11
- async acquire() {
12
- while (true) {
13
- if (this._running >= this.concurrency) {
14
- await new Promise((resolve) => this.queue.push(resolve));
15
- } else {
16
- this._running++;
17
- let released = false;
18
- return () => {
19
- if (released) {
20
- return;
21
- }
22
- released = true;
23
- this._running--;
24
- if (this.queue.length > 0) {
25
- this.queue.shift()();
26
- }
27
- };
28
- }
29
- }
30
- }
31
- async run(fn) {
32
- const release = await this.acquire();
33
- try {
34
- return await fn();
35
- } finally {
36
- release();
37
- }
38
- }
39
- };
40
-
41
- // packages/php-wasm/util/src/lib/paths.ts
42
- function joinPaths(...paths) {
43
- let path = paths.join("/");
44
- const isAbsolute = path[0] === "/";
45
- const trailingSlash = path.substring(path.length - 1) === "/";
46
- path = normalizePath(path);
47
- if (!path && !isAbsolute) {
48
- path = ".";
49
- }
50
- if (path && trailingSlash) {
51
- path += "/";
52
- }
53
- return path;
54
- }
55
- function dirname(path) {
56
- if (path === "/") {
57
- return "/";
58
- }
59
- path = normalizePath(path);
60
- const lastSlash = path.lastIndexOf("/");
61
- if (lastSlash === -1) {
62
- return "";
63
- } else if (lastSlash === 0) {
64
- return "/";
65
- }
66
- return path.substr(0, lastSlash);
67
- }
68
- function basename(path) {
69
- if (path === "/") {
70
- return "/";
71
- }
72
- path = normalizePath(path);
73
- const lastSlash = path.lastIndexOf("/");
74
- if (lastSlash === -1) {
75
- return path;
76
- }
77
- return path.substr(lastSlash + 1);
78
- }
79
- function normalizePath(path) {
80
- const isAbsolute = path[0] === "/";
81
- path = normalizePathsArray(
82
- path.split("/").filter((p) => !!p),
83
- !isAbsolute
84
- ).join("/");
85
- return (isAbsolute ? "/" : "") + path.replace(/\/$/, "");
86
- }
87
- function normalizePathsArray(parts, allowAboveRoot) {
88
- let up = 0;
89
- for (let i = parts.length - 1; i >= 0; i--) {
90
- const last = parts[i];
91
- if (last === ".") {
92
- parts.splice(i, 1);
93
- } else if (last === "..") {
94
- parts.splice(i, 1);
95
- up++;
96
- } else if (up) {
97
- parts.splice(i, 1);
98
- up--;
99
- }
100
- }
101
- if (allowAboveRoot) {
102
- for (; up; up--) {
103
- parts.unshift("..");
104
- }
105
- }
106
- return parts;
107
- }
108
-
109
- // packages/php-wasm/util/src/lib/create-spawn-handler.ts
110
- function createSpawnHandler(program) {
111
- return function(command) {
112
- const childProcess = new ChildProcess();
113
- const processApi = new ProcessApi(childProcess);
114
- setTimeout(async () => {
115
- await program(command, processApi);
116
- childProcess.emit("spawn", true);
117
- });
118
- return childProcess;
119
- };
120
- }
121
- var EventEmitter = class {
122
- constructor() {
123
- this.listeners = {};
124
- }
125
- emit(eventName, data) {
126
- if (this.listeners[eventName]) {
127
- this.listeners[eventName].forEach(function(listener) {
128
- listener(data);
129
- });
130
- }
131
- }
132
- on(eventName, listener) {
133
- if (!this.listeners[eventName]) {
134
- this.listeners[eventName] = [];
135
- }
136
- this.listeners[eventName].push(listener);
137
- }
138
- };
139
- var ProcessApi = class extends EventEmitter {
140
- constructor(childProcess) {
141
- super();
142
- this.childProcess = childProcess;
143
- this.exited = false;
144
- this.stdinData = [];
145
- childProcess.on("stdin", (data) => {
146
- if (this.stdinData) {
147
- this.stdinData.push(data.slice());
148
- } else {
149
- this.emit("stdin", data);
150
- }
151
- });
152
- }
153
- stdout(data) {
154
- if (typeof data === "string") {
155
- data = new TextEncoder().encode(data);
156
- }
157
- this.childProcess.stdout.emit("data", data);
158
- }
159
- stderr(data) {
160
- if (typeof data === "string") {
161
- data = new TextEncoder().encode(data);
162
- }
163
- this.childProcess.stderr.emit("data", data);
164
- }
165
- exit(code) {
166
- if (!this.exited) {
167
- this.exited = true;
168
- this.childProcess.emit("exit", code);
169
- }
170
- }
171
- flushStdin() {
172
- if (this.stdinData) {
173
- for (let i = 0; i < this.stdinData.length; i++) {
174
- this.emit("stdin", this.stdinData[i]);
175
- }
176
- }
177
- this.stdinData = null;
178
- }
179
- };
180
- var lastPid = 9743;
181
- var ChildProcess = class extends EventEmitter {
182
- constructor(pid = lastPid++) {
183
- super();
184
- this.pid = pid;
185
- this.stdout = new EventEmitter();
186
- this.stderr = new EventEmitter();
187
- const self = this;
188
- this.stdin = {
189
- write: (data) => {
190
- self.emit("stdin", data);
191
- }
192
- };
193
- }
194
- };
195
-
196
- // packages/php-wasm/util/src/lib/random-string.ts
197
- function randomString(length = 36, specialChars = "!@#$%^&*()_+=-[]/.,<>?") {
198
- const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + specialChars;
199
- let result = "";
200
- for (let i = length; i > 0; --i)
201
- result += chars[Math.floor(Math.random() * chars.length)];
202
- return result;
203
- }
204
-
205
- // packages/php-wasm/util/src/lib/random-filename.ts
206
- function randomFilename() {
207
- return randomString(36, "-_");
208
- }
209
-
210
- // packages/php-wasm/util/src/lib/php-vars.ts
211
- function phpVar(value) {
212
- return `json_decode(base64_decode('${stringToBase64(
213
- JSON.stringify(value)
214
- )}'), true)`;
215
- }
216
- function phpVars(vars) {
217
- const result = {};
218
- for (const key in vars) {
219
- result[key] = phpVar(vars[key]);
220
- }
221
- return result;
222
- }
223
- function stringToBase64(str) {
224
- return bytesToBase64(new TextEncoder().encode(str));
225
- }
226
- function bytesToBase64(bytes) {
227
- const binString = String.fromCodePoint(...bytes);
228
- return btoa(binString);
229
- }
230
- export {
231
- Semaphore,
232
- basename,
233
- createSpawnHandler,
234
- dirname,
235
- joinPaths,
236
- normalizePath,
237
- phpVar,
238
- phpVars,
239
- randomFilename,
240
- randomString
241
- };
1
+ "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;
package/index.mjs ADDED
@@ -0,0 +1,185 @@
1
+ class y {
2
+ constructor({ concurrency: t }) {
3
+ this._running = 0, this.concurrency = t, this.queue = [];
4
+ }
5
+ get running() {
6
+ return this._running;
7
+ }
8
+ async acquire() {
9
+ for (; ; )
10
+ if (this._running >= this.concurrency)
11
+ await new Promise((t) => this.queue.push(t));
12
+ else {
13
+ this._running++;
14
+ let t = !1;
15
+ return () => {
16
+ t || (t = !0, this._running--, this.queue.length > 0 && this.queue.shift()());
17
+ };
18
+ }
19
+ }
20
+ async run(t) {
21
+ const s = await this.acquire();
22
+ try {
23
+ return await t();
24
+ } finally {
25
+ s();
26
+ }
27
+ }
28
+ }
29
+ function p(...e) {
30
+ let t = e.join("/");
31
+ const s = t[0] === "/", i = t.substring(t.length - 1) === "/";
32
+ return t = f(t), !t && !s && (t = "."), t && i && (t += "/"), t;
33
+ }
34
+ function P(e) {
35
+ if (e === "/")
36
+ return "/";
37
+ e = f(e);
38
+ const t = e.lastIndexOf("/");
39
+ return t === -1 ? "" : t === 0 ? "/" : e.substr(0, t);
40
+ }
41
+ function m(e) {
42
+ if (e === "/")
43
+ return "/";
44
+ e = f(e);
45
+ const t = e.lastIndexOf("/");
46
+ return t === -1 ? e : e.substr(t + 1);
47
+ }
48
+ function f(e) {
49
+ const t = e[0] === "/";
50
+ return e = h(
51
+ e.split("/").filter((s) => !!s),
52
+ !t
53
+ ).join("/"), (t ? "/" : "") + e.replace(/\/$/, "");
54
+ }
55
+ function h(e, t) {
56
+ let s = 0;
57
+ for (let i = e.length - 1; i >= 0; i--) {
58
+ const r = e[i];
59
+ r === "." ? e.splice(i, 1) : r === ".." ? (e.splice(i, 1), s++) : s && (e.splice(i, 1), s--);
60
+ }
61
+ if (t)
62
+ for (; s; s--)
63
+ e.unshift("..");
64
+ return e;
65
+ }
66
+ function d(e) {
67
+ let i = 0, r = "";
68
+ const l = [];
69
+ let n = "";
70
+ for (let u = 0; u < e.length; u++) {
71
+ const o = e[u];
72
+ o === "\\" ? ((e[u + 1] === '"' || e[u + 1] === "'") && u++, n += e[u]) : i === 0 ? o === '"' || o === "'" ? (i = 1, r = o) : o.match(/\s/) ? (n.trim().length && l.push(n.trim()), n = o) : l.length && !n ? n = l.pop() + o : n += o : i === 1 && (o === r ? (i = 0, r = "") : n += o);
73
+ }
74
+ return n && l.push(n.trim()), l;
75
+ }
76
+ function x(e) {
77
+ return function(t, s = [], i = {}) {
78
+ const r = new E(), l = new a(r);
79
+ return setTimeout(async () => {
80
+ let n = [];
81
+ if (s.length)
82
+ n = [t, ...s];
83
+ else if (typeof t == "string")
84
+ n = d(t);
85
+ else if (Array.isArray(t))
86
+ n = t;
87
+ else
88
+ throw new Error("Invalid command ", t);
89
+ await e(n, l, i), r.emit("spawn", !0);
90
+ }), r;
91
+ };
92
+ }
93
+ class c {
94
+ constructor() {
95
+ this.listeners = {};
96
+ }
97
+ emit(t, s) {
98
+ this.listeners[t] && this.listeners[t].forEach(function(i) {
99
+ i(s);
100
+ });
101
+ }
102
+ on(t, s) {
103
+ this.listeners[t] || (this.listeners[t] = []), this.listeners[t].push(s);
104
+ }
105
+ }
106
+ class a extends c {
107
+ constructor(t) {
108
+ super(), this.childProcess = t, this.exited = !1, this.stdinData = [], t.on("stdin", (s) => {
109
+ this.stdinData ? this.stdinData.push(s.slice()) : this.emit("stdin", s);
110
+ });
111
+ }
112
+ stdout(t) {
113
+ typeof t == "string" && (t = new TextEncoder().encode(t)), this.childProcess.stdout.emit("data", t);
114
+ }
115
+ stdoutEnd() {
116
+ this.childProcess.stdout.emit("end", {});
117
+ }
118
+ stderr(t) {
119
+ typeof t == "string" && (t = new TextEncoder().encode(t)), this.childProcess.stderr.emit("data", t);
120
+ }
121
+ stderrEnd() {
122
+ this.childProcess.stderr.emit("end", {});
123
+ }
124
+ exit(t) {
125
+ this.exited || (this.exited = !0, this.childProcess.emit("exit", t));
126
+ }
127
+ flushStdin() {
128
+ if (this.stdinData)
129
+ for (let t = 0; t < this.stdinData.length; t++)
130
+ this.emit("stdin", this.stdinData[t]);
131
+ this.stdinData = null;
132
+ }
133
+ }
134
+ let g = 9743;
135
+ class E extends c {
136
+ constructor(t = g++) {
137
+ super(), this.pid = t, this.stdout = new c(), this.stderr = new c();
138
+ const s = this;
139
+ this.stdin = {
140
+ write: (i) => {
141
+ s.emit("stdin", i);
142
+ }
143
+ };
144
+ }
145
+ }
146
+ function w(e = 36, t = "!@#$%^&*()_+=-[]/.,<>?") {
147
+ const s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + t;
148
+ let i = "";
149
+ for (let r = e; r > 0; --r)
150
+ i += s[Math.floor(Math.random() * s.length)];
151
+ return i;
152
+ }
153
+ function T() {
154
+ return w(36, "-_");
155
+ }
156
+ function D(e) {
157
+ return `json_decode(base64_decode('${_(
158
+ JSON.stringify(e)
159
+ )}'), true)`;
160
+ }
161
+ function S(e) {
162
+ const t = {};
163
+ for (const s in e)
164
+ t[s] = D(e[s]);
165
+ return t;
166
+ }
167
+ function _(e) {
168
+ return O(new TextEncoder().encode(e));
169
+ }
170
+ function O(e) {
171
+ const t = String.fromCodePoint(...e);
172
+ return btoa(t);
173
+ }
174
+ export {
175
+ y as Semaphore,
176
+ m as basename,
177
+ x as createSpawnHandler,
178
+ P as dirname,
179
+ p as joinPaths,
180
+ f as normalizePath,
181
+ D as phpVar,
182
+ S as phpVars,
183
+ T as randomFilename,
184
+ w as randomString
185
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/util",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
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": "0a8916ec08aa5f02de5fea308b287171e7948e4b",
15
+ "gitHead": "eb45486a61d06a8a9e62cbd65348bfb274ff9e7c",
16
16
  "engines": {
17
17
  "node": ">=18.18.2",
18
18
  "npm": ">=8.11.0"
package/README.md DELETED
@@ -1,11 +0,0 @@
1
- # php-wasm-util
2
-
3
- This library was generated with [Nx](https://nx.dev).
4
-
5
- ## Building
6
-
7
- Run `nx build php-wasm-util` to build the library.
8
-
9
- ## Running unit tests
10
-
11
- Run `nx test php-wasm-util` to execute the unit tests via [Jest](https://jestjs.io).