@php-wasm/util 0.3.1 → 0.5.1

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 (2) hide show
  1. package/index.js +126 -34
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  // packages/php-wasm/util/src/lib/semaphore.ts
2
2
  var Semaphore = class {
3
- _running = 0;
4
- concurrency;
5
- queue;
6
3
  constructor({ concurrency }) {
4
+ this._running = 0;
7
5
  this.concurrency = concurrency;
8
6
  this.queue = [];
9
7
  }
@@ -40,22 +38,51 @@ var Semaphore = class {
40
38
  }
41
39
  };
42
40
 
43
- // packages/php-wasm/util/src/lib/join-paths.ts
41
+ // packages/php-wasm/util/src/lib/paths.ts
44
42
  function joinPaths(...paths) {
45
43
  let path = paths.join("/");
46
- const isAbsolute = path.charAt(0) === "/";
44
+ const isAbsolute = path[0] === "/";
47
45
  const trailingSlash = path.substring(path.length - 1) === "/";
48
- path = normalizePathsArray(
49
- path.split("/").filter((p) => !!p),
50
- !isAbsolute
51
- ).join("/");
46
+ path = normalizePath(path);
52
47
  if (!path && !isAbsolute) {
53
48
  path = ".";
54
49
  }
55
50
  if (path && trailingSlash) {
56
51
  path += "/";
57
52
  }
58
- return (isAbsolute ? "/" : "") + path;
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(/\/$/, "");
59
86
  }
60
87
  function normalizePathsArray(parts, allowAboveRoot) {
61
88
  let up = 0;
@@ -79,33 +106,87 @@ function normalizePathsArray(parts, allowAboveRoot) {
79
106
  return parts;
80
107
  }
81
108
 
82
- // packages/php-wasm/util/src/lib/php-vars.ts
83
- var literal = Symbol("literal");
84
- function phpVar(value) {
85
- if (typeof value === "string") {
86
- if (value.startsWith("$")) {
87
- return value;
88
- } else {
89
- return JSON.stringify(value);
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(() => {
115
+ program(command, processApi);
116
+ });
117
+ return childProcess;
118
+ };
119
+ }
120
+ var ProcessApi = class {
121
+ constructor(childProcess) {
122
+ this.childProcess = childProcess;
123
+ this.exited = false;
124
+ this.stdinData = [];
125
+ childProcess.on("stdin", (data) => {
126
+ this.stdinData.push(data);
127
+ });
128
+ }
129
+ stdout(data) {
130
+ if (typeof data === "string") {
131
+ data = new TextEncoder().encode(data);
90
132
  }
91
- } else if (typeof value === "number") {
92
- return value.toString();
93
- } else if (Array.isArray(value)) {
94
- const phpArray = value.map(phpVar).join(", ");
95
- return `array(${phpArray})`;
96
- } else if (value === null) {
97
- return "null";
98
- } else if (typeof value === "object") {
99
- if (literal in value) {
100
- return value.toString();
101
- } else {
102
- const phpAssocArray = Object.entries(value).map(([key, val]) => `${JSON.stringify(key)} => ${phpVar(val)}`).join(", ");
103
- return `array(${phpAssocArray})`;
133
+ this.childProcess.stdout.emit("data", data);
134
+ }
135
+ stderr(data) {
136
+ if (typeof data === "string") {
137
+ data = new TextEncoder().encode(data);
138
+ }
139
+ this.childProcess.stderr.emit("data", data);
140
+ }
141
+ exit(code) {
142
+ if (!this.exited) {
143
+ this.exited = true;
144
+ this.childProcess.emit("exit", code);
145
+ }
146
+ }
147
+ flushStdin() {
148
+ const data = this.stdinData.join("");
149
+ this.stdinData = [];
150
+ return data;
151
+ }
152
+ };
153
+ var EventEmitter = class {
154
+ constructor() {
155
+ this.listeners = {};
156
+ }
157
+ emit(eventName, data) {
158
+ if (this.listeners[eventName]) {
159
+ this.listeners[eventName].forEach(function(listener) {
160
+ listener(data);
161
+ });
162
+ }
163
+ }
164
+ on(eventName, listener) {
165
+ if (!this.listeners[eventName]) {
166
+ this.listeners[eventName] = [];
104
167
  }
105
- } else if (typeof value === "function") {
106
- return value();
168
+ this.listeners[eventName].push(listener);
107
169
  }
108
- throw new Error(`Unsupported value: ${value}`);
170
+ };
171
+ var ChildProcess = class extends EventEmitter {
172
+ constructor() {
173
+ super();
174
+ this.stdout = new EventEmitter();
175
+ this.stderr = new EventEmitter();
176
+ const self = this;
177
+ this.stdin = {
178
+ write: (data) => {
179
+ self.emit("stdin", data);
180
+ }
181
+ };
182
+ }
183
+ };
184
+
185
+ // packages/php-wasm/util/src/lib/php-vars.ts
186
+ function phpVar(value) {
187
+ return `json_decode(base64_decode('${stringToBase64(
188
+ JSON.stringify(value)
189
+ )}'), true)`;
109
190
  }
110
191
  function phpVars(vars) {
111
192
  const result = {};
@@ -114,9 +195,20 @@ function phpVars(vars) {
114
195
  }
115
196
  return result;
116
197
  }
198
+ function stringToBase64(str) {
199
+ return bytesToBase64(new TextEncoder().encode(str));
200
+ }
201
+ function bytesToBase64(bytes) {
202
+ const binString = String.fromCodePoint(...bytes);
203
+ return btoa(binString);
204
+ }
117
205
  export {
118
206
  Semaphore,
207
+ basename,
208
+ createSpawnHandler,
209
+ dirname,
119
210
  joinPaths,
211
+ normalizePath,
120
212
  phpVar,
121
213
  phpVars
122
214
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/util",
3
- "version": "0.3.1",
3
+ "version": "0.5.1",
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": "73b285f8b496c7442474fd7325b3f6b1287cbf95",
15
+ "gitHead": "6c2256a8f3b6bafb2d9a5860f236b00832839769",
16
16
  "engines": {
17
17
  "node": ">=16.15.1",
18
18
  "npm": ">=8.11.0"