@heyputer/puter.js 2.0.1 → 2.0.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.
- package/dist/puter.js +4 -0
- package/package.json +4 -4
- package/src/bg.png +0 -0
- package/src/bg.webp +0 -0
- package/src/index.js +165 -165
- package/src/lib/APICallLogger.js +110 -0
- package/src/lib/EventListener.js +51 -0
- package/src/lib/RequestError.js +6 -0
- package/src/lib/filesystem/APIFS.js +73 -0
- package/src/lib/filesystem/CacheFS.js +243 -0
- package/src/lib/filesystem/PostMessageFS.js +40 -0
- package/src/lib/filesystem/definitions.js +39 -0
- package/src/lib/path.js +509 -0
- package/src/lib/polyfills/localStorage.js +92 -0
- package/src/lib/polyfills/xhrshim.js +233 -0
- package/src/lib/socket.io/socket.io.esm.min.js +7 -0
- package/src/lib/socket.io/socket.io.esm.min.js.map +1 -0
- package/src/lib/socket.io/socket.io.js +4385 -0
- package/src/lib/socket.io/socket.io.js.map +1 -0
- package/src/lib/socket.io/socket.io.min.js +7 -0
- package/src/lib/socket.io/socket.io.min.js.map +1 -0
- package/src/lib/socket.io/socket.io.msgpack.min.js +7 -0
- package/src/lib/socket.io/socket.io.msgpack.min.js.map +1 -0
- package/src/lib/utils.js +620 -0
- package/src/lib/xdrpc.js +104 -0
- package/src/modules/AI.js +680 -0
- package/src/modules/Apps.js +215 -0
- package/src/modules/Auth.js +171 -0
- package/src/modules/Debug.js +39 -0
- package/src/modules/Drivers.js +278 -0
- package/src/modules/FSItem.js +139 -0
- package/src/modules/FileSystem/index.js +187 -0
- package/src/modules/FileSystem/operations/copy.js +64 -0
- package/src/modules/FileSystem/operations/deleteFSEntry.js +59 -0
- package/src/modules/FileSystem/operations/getReadUrl.js +42 -0
- package/src/modules/FileSystem/operations/mkdir.js +62 -0
- package/src/modules/FileSystem/operations/move.js +75 -0
- package/src/modules/FileSystem/operations/read.js +46 -0
- package/src/modules/FileSystem/operations/readdir.js +102 -0
- package/src/modules/FileSystem/operations/rename.js +58 -0
- package/src/modules/FileSystem/operations/sign.js +103 -0
- package/src/modules/FileSystem/operations/space.js +40 -0
- package/src/modules/FileSystem/operations/stat.js +95 -0
- package/src/modules/FileSystem/operations/symlink.js +55 -0
- package/src/modules/FileSystem/operations/upload.js +440 -0
- package/src/modules/FileSystem/operations/write.js +65 -0
- package/src/modules/FileSystem/utils/getAbsolutePathForApp.js +21 -0
- package/src/modules/Hosting.js +138 -0
- package/src/modules/KV.js +301 -0
- package/src/modules/OS.js +95 -0
- package/src/modules/Perms.js +109 -0
- package/src/modules/PuterDialog.js +481 -0
- package/src/modules/Threads.js +75 -0
- package/src/modules/UI.js +1555 -0
- package/src/modules/Util.js +38 -0
- package/src/modules/Workers.js +120 -0
- package/src/modules/networking/PSocket.js +87 -0
- package/src/modules/networking/PTLS.js +100 -0
- package/src/modules/networking/PWispHandler.js +89 -0
- package/src/modules/networking/parsers.js +157 -0
- package/src/modules/networking/requests.js +282 -0
- package/src/safeLoadPuter.cjs +29 -0
- package/src/services/APIAccess.js +46 -0
- package/src/services/FSRelay.js +20 -0
- package/src/services/Filesystem.js +122 -0
- package/src/services/NoPuterYet.js +20 -0
- package/src/services/XDIncoming.js +44 -0
- package/index.d.ts +0 -479
package/src/lib/path.js
ADDED
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
// import {cwd} from './env.js'
|
|
2
|
+
let cwd;
|
|
3
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
4
|
+
//
|
|
5
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
6
|
+
// copy of this software and associated documentation files (the
|
|
7
|
+
// "Software"), to deal in the Software without restriction, including
|
|
8
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
10
|
+
// persons to whom the Software is furnished to do so, subject to the
|
|
11
|
+
// following conditions:
|
|
12
|
+
//
|
|
13
|
+
// The above copyright notice and this permission notice shall be included
|
|
14
|
+
// in all copies or substantial portions of the Software.
|
|
15
|
+
//
|
|
16
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
17
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
19
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
20
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
21
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
22
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
//'use strict';
|
|
25
|
+
|
|
26
|
+
const
|
|
27
|
+
CHAR_UPPERCASE_A = 65,
|
|
28
|
+
CHAR_LOWERCASE_A = 97,
|
|
29
|
+
CHAR_UPPERCASE_Z = 90,
|
|
30
|
+
CHAR_LOWERCASE_Z = 122,
|
|
31
|
+
CHAR_DOT = 46,
|
|
32
|
+
CHAR_FORWARD_SLASH = 47,
|
|
33
|
+
CHAR_BACKWARD_SLASH = 92,
|
|
34
|
+
CHAR_COLON = 58,
|
|
35
|
+
CHAR_QUESTION_MARK = 63;
|
|
36
|
+
|
|
37
|
+
function isPathSeparator(code) {
|
|
38
|
+
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isPosixPathSeparator(code) {
|
|
42
|
+
return code === CHAR_FORWARD_SLASH;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Resolves . and .. elements in a path with directory names
|
|
46
|
+
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
|
|
47
|
+
let res = '';
|
|
48
|
+
let lastSegmentLength = 0;
|
|
49
|
+
let lastSlash = -1;
|
|
50
|
+
let dots = 0;
|
|
51
|
+
let code = 0;
|
|
52
|
+
for (let i = 0; i <= path.length; ++i) {
|
|
53
|
+
if (i < path.length)
|
|
54
|
+
code = path.charCodeAt(i);
|
|
55
|
+
else if (isPathSeparator(code))
|
|
56
|
+
break;
|
|
57
|
+
else
|
|
58
|
+
code = CHAR_FORWARD_SLASH;
|
|
59
|
+
|
|
60
|
+
if (isPathSeparator(code)) {
|
|
61
|
+
if (lastSlash === i - 1 || dots === 1) {
|
|
62
|
+
// NOOP
|
|
63
|
+
} else if (dots === 2) {
|
|
64
|
+
if (res.length < 2 || lastSegmentLength !== 2 ||
|
|
65
|
+
res.charCodeAt( res.length - 1) !== CHAR_DOT ||
|
|
66
|
+
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
|
|
67
|
+
if (res.length > 2) {
|
|
68
|
+
const lastSlashIndex = res.lastIndexOf(separator);
|
|
69
|
+
if (lastSlashIndex === -1) {
|
|
70
|
+
res = '';
|
|
71
|
+
lastSegmentLength = 0;
|
|
72
|
+
} else {
|
|
73
|
+
res = res.slice(0, lastSlashIndex);
|
|
74
|
+
lastSegmentLength =
|
|
75
|
+
res.length - 1 - res.lastIndexOf(res, separator);
|
|
76
|
+
}
|
|
77
|
+
lastSlash = i;
|
|
78
|
+
dots = 0;
|
|
79
|
+
continue;
|
|
80
|
+
} else if (res.length !== 0) {
|
|
81
|
+
res = '';
|
|
82
|
+
lastSegmentLength = 0;
|
|
83
|
+
lastSlash = i;
|
|
84
|
+
dots = 0;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (allowAboveRoot) {
|
|
89
|
+
res += res.length > 0 ? `${separator}..` : '..';
|
|
90
|
+
lastSegmentLength = 2;
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
if (res.length > 0)
|
|
94
|
+
res += `${separator}${path.slice(lastSlash + 1, i)}`;
|
|
95
|
+
else
|
|
96
|
+
res = path.slice(lastSlash + 1, i);
|
|
97
|
+
lastSegmentLength = i - lastSlash - 1;
|
|
98
|
+
}
|
|
99
|
+
lastSlash = i;
|
|
100
|
+
dots = 0;
|
|
101
|
+
} else if (code === CHAR_DOT && dots !== -1) {
|
|
102
|
+
++dots;
|
|
103
|
+
} else {
|
|
104
|
+
dots = -1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return res;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const path = {
|
|
111
|
+
// path.resolve([from ...], to)
|
|
112
|
+
resolve(...args) {
|
|
113
|
+
let resolvedPath = '';
|
|
114
|
+
let resolvedAbsolute = false;
|
|
115
|
+
|
|
116
|
+
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
|
117
|
+
// orig const path = i >= 0 ? args[i] : posixCwd();
|
|
118
|
+
const path = i >= 0 ? args[i] : (cwd !== undefined ? cwd : '/');
|
|
119
|
+
// const path = i >= 0 ? args[i] : '/';
|
|
120
|
+
|
|
121
|
+
// Skip empty entries
|
|
122
|
+
if (path.length === 0) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
127
|
+
resolvedAbsolute =
|
|
128
|
+
path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// At this point the path should be resolved to a full absolute path, but
|
|
132
|
+
// handle relative paths to be safe (might happen when process.cwd() fails)
|
|
133
|
+
|
|
134
|
+
// Normalize the path
|
|
135
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/',
|
|
136
|
+
isPosixPathSeparator);
|
|
137
|
+
|
|
138
|
+
if (resolvedAbsolute) {
|
|
139
|
+
return `/${resolvedPath}`;
|
|
140
|
+
}
|
|
141
|
+
return resolvedPath.length > 0 ? resolvedPath : '.';
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
normalize(path) {
|
|
145
|
+
if (path.length === 0)
|
|
146
|
+
return '.';
|
|
147
|
+
|
|
148
|
+
const isAbsolute =
|
|
149
|
+
path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
|
150
|
+
const trailingSeparator =
|
|
151
|
+
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
|
|
152
|
+
|
|
153
|
+
// Normalize the path
|
|
154
|
+
path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
|
|
155
|
+
|
|
156
|
+
if (path.length === 0) {
|
|
157
|
+
if (isAbsolute)
|
|
158
|
+
return '/';
|
|
159
|
+
return trailingSeparator ? './' : '.';
|
|
160
|
+
}
|
|
161
|
+
if (trailingSeparator)
|
|
162
|
+
path += '/';
|
|
163
|
+
|
|
164
|
+
return isAbsolute ? `/${path}` : path;
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
isAbsolute(path) {
|
|
168
|
+
return path.length > 0 &&
|
|
169
|
+
path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
join(...args) {
|
|
173
|
+
if (args.length === 0)
|
|
174
|
+
return '.';
|
|
175
|
+
let joined;
|
|
176
|
+
for (let i = 0; i < args.length; ++i) {
|
|
177
|
+
const arg = args[i];
|
|
178
|
+
if (arg.length > 0) {
|
|
179
|
+
if (joined === undefined)
|
|
180
|
+
joined = arg;
|
|
181
|
+
else
|
|
182
|
+
joined += `/${arg}`;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (joined === undefined)
|
|
186
|
+
return '.';
|
|
187
|
+
return path.normalize(joined);
|
|
188
|
+
},
|
|
189
|
+
|
|
190
|
+
relative(from, to) {
|
|
191
|
+
if (from === to)
|
|
192
|
+
return '';
|
|
193
|
+
|
|
194
|
+
// Trim leading forward slashes.
|
|
195
|
+
from = path.resolve(from);
|
|
196
|
+
to = path.resolve(to);
|
|
197
|
+
|
|
198
|
+
if (from === to)
|
|
199
|
+
return '';
|
|
200
|
+
|
|
201
|
+
const fromStart = 1;
|
|
202
|
+
const fromEnd = from.length;
|
|
203
|
+
const fromLen = fromEnd - fromStart;
|
|
204
|
+
const toStart = 1;
|
|
205
|
+
const toLen = to.length - toStart;
|
|
206
|
+
|
|
207
|
+
// Compare paths to find the longest common path from root
|
|
208
|
+
const length = (fromLen < toLen ? fromLen : toLen);
|
|
209
|
+
let lastCommonSep = -1;
|
|
210
|
+
let i = 0;
|
|
211
|
+
for (; i < length; i++) {
|
|
212
|
+
const fromCode = from.charCodeAt(fromStart + i);
|
|
213
|
+
if (fromCode !== to.charCodeAt(toStart + i))
|
|
214
|
+
break;
|
|
215
|
+
else if (fromCode === CHAR_FORWARD_SLASH)
|
|
216
|
+
lastCommonSep = i;
|
|
217
|
+
}
|
|
218
|
+
if (i === length) {
|
|
219
|
+
if (toLen > length) {
|
|
220
|
+
if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
|
|
221
|
+
// We get here if `from` is the exact base path for `to`.
|
|
222
|
+
// For example: from='/foo/bar'; to='/foo/bar/baz'
|
|
223
|
+
return to.slice(toStart + i + 1);
|
|
224
|
+
}
|
|
225
|
+
if (i === 0) {
|
|
226
|
+
// We get here if `from` is the root
|
|
227
|
+
// For example: from='/'; to='/foo'
|
|
228
|
+
return to.slice(toStart + i);
|
|
229
|
+
}
|
|
230
|
+
} else if (fromLen > length) {
|
|
231
|
+
if (from.charCodeAt(fromStart + i) ===
|
|
232
|
+
CHAR_FORWARD_SLASH) {
|
|
233
|
+
// We get here if `to` is the exact base path for `from`.
|
|
234
|
+
// For example: from='/foo/bar/baz'; to='/foo/bar'
|
|
235
|
+
lastCommonSep = i;
|
|
236
|
+
} else if (i === 0) {
|
|
237
|
+
// We get here if `to` is the root.
|
|
238
|
+
// For example: from='/foo/bar'; to='/'
|
|
239
|
+
lastCommonSep = 0;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
let out = '';
|
|
245
|
+
// Generate the relative path based on the path difference between `to`
|
|
246
|
+
// and `from`.
|
|
247
|
+
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
|
|
248
|
+
if (i === fromEnd ||
|
|
249
|
+
from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
|
|
250
|
+
out += out.length === 0 ? '..' : '/..';
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Lastly, append the rest of the destination (`to`) path that comes after
|
|
255
|
+
// the common path parts.
|
|
256
|
+
return `${out}${to.slice(toStart + lastCommonSep)}`;
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
toNamespacedPath(path) {
|
|
260
|
+
// Non-op on posix systems
|
|
261
|
+
return path;
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
dirname(path) {
|
|
265
|
+
if (path.length === 0)
|
|
266
|
+
return '.';
|
|
267
|
+
const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
|
268
|
+
let end = -1;
|
|
269
|
+
let matchedSlash = true;
|
|
270
|
+
for (let i = path.length - 1; i >= 1; --i) {
|
|
271
|
+
if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
|
|
272
|
+
if (!matchedSlash) {
|
|
273
|
+
end = i;
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
} else {
|
|
277
|
+
// We saw the first non-path separator
|
|
278
|
+
matchedSlash = false;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (end === -1)
|
|
283
|
+
return hasRoot ? '/' : '.';
|
|
284
|
+
if (hasRoot && end === 1)
|
|
285
|
+
return '//';
|
|
286
|
+
return path.slice(0, end);
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
basename(path, ext) {
|
|
290
|
+
let start = 0;
|
|
291
|
+
let end = -1;
|
|
292
|
+
let matchedSlash = true;
|
|
293
|
+
|
|
294
|
+
if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
|
|
295
|
+
if (ext === path)
|
|
296
|
+
return '';
|
|
297
|
+
let extIdx = ext.length - 1;
|
|
298
|
+
let firstNonSlashEnd = -1;
|
|
299
|
+
for (let i = path.length - 1; i >= 0; --i) {
|
|
300
|
+
const code = path.charCodeAt(i);
|
|
301
|
+
if (code === CHAR_FORWARD_SLASH) {
|
|
302
|
+
// If we reached a path separator that was not part of a set of path
|
|
303
|
+
// separators at the end of the string, stop now
|
|
304
|
+
if (!matchedSlash) {
|
|
305
|
+
start = i + 1;
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
} else {
|
|
309
|
+
if (firstNonSlashEnd === -1) {
|
|
310
|
+
// We saw the first non-path separator, remember this index in case
|
|
311
|
+
// we need it if the extension ends up not matching
|
|
312
|
+
matchedSlash = false;
|
|
313
|
+
firstNonSlashEnd = i + 1;
|
|
314
|
+
}
|
|
315
|
+
if (extIdx >= 0) {
|
|
316
|
+
// Try to match the explicit extension
|
|
317
|
+
if (code === ext.charCodeAt(extIdx)) {
|
|
318
|
+
if (--extIdx === -1) {
|
|
319
|
+
// We matched the extension, so mark this as the end of our path
|
|
320
|
+
// component
|
|
321
|
+
end = i;
|
|
322
|
+
}
|
|
323
|
+
} else {
|
|
324
|
+
// Extension does not match, so our result is the entire path
|
|
325
|
+
// component
|
|
326
|
+
extIdx = -1;
|
|
327
|
+
end = firstNonSlashEnd;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (start === end)
|
|
334
|
+
end = firstNonSlashEnd;
|
|
335
|
+
else if (end === -1)
|
|
336
|
+
end = path.length;
|
|
337
|
+
return path.slice(start, end);
|
|
338
|
+
}
|
|
339
|
+
for (let i = path.length - 1; i >= 0; --i) {
|
|
340
|
+
if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
|
|
341
|
+
// If we reached a path separator that was not part of a set of path
|
|
342
|
+
// separators at the end of the string, stop now
|
|
343
|
+
if (!matchedSlash) {
|
|
344
|
+
start = i + 1;
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
} else if (end === -1) {
|
|
348
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
349
|
+
// path component
|
|
350
|
+
matchedSlash = false;
|
|
351
|
+
end = i + 1;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (end === -1)
|
|
356
|
+
return '';
|
|
357
|
+
return path.slice(start, end);
|
|
358
|
+
},
|
|
359
|
+
|
|
360
|
+
extname(path) {
|
|
361
|
+
let startDot = -1;
|
|
362
|
+
let startPart = 0;
|
|
363
|
+
let end = -1;
|
|
364
|
+
let matchedSlash = true;
|
|
365
|
+
// Track the state of characters (if any) we see before our first dot and
|
|
366
|
+
// after any path separator we find
|
|
367
|
+
let preDotState = 0;
|
|
368
|
+
for (let i = path.length - 1; i >= 0; --i) {
|
|
369
|
+
const code = path.charCodeAt(i);
|
|
370
|
+
if (code === CHAR_FORWARD_SLASH) {
|
|
371
|
+
// If we reached a path separator that was not part of a set of path
|
|
372
|
+
// separators at the end of the string, stop now
|
|
373
|
+
if (!matchedSlash) {
|
|
374
|
+
startPart = i + 1;
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (end === -1) {
|
|
380
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
381
|
+
// extension
|
|
382
|
+
matchedSlash = false;
|
|
383
|
+
end = i + 1;
|
|
384
|
+
}
|
|
385
|
+
if (code === CHAR_DOT) {
|
|
386
|
+
// If this is our first dot, mark it as the start of our extension
|
|
387
|
+
if (startDot === -1)
|
|
388
|
+
startDot = i;
|
|
389
|
+
else if (preDotState !== 1)
|
|
390
|
+
preDotState = 1;
|
|
391
|
+
} else if (startDot !== -1) {
|
|
392
|
+
// We saw a non-dot and non-path separator before our dot, so we should
|
|
393
|
+
// have a good chance at having a non-empty extension
|
|
394
|
+
preDotState = -1;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (startDot === -1 ||
|
|
399
|
+
end === -1 ||
|
|
400
|
+
// We saw a non-dot character immediately before the dot
|
|
401
|
+
preDotState === 0 ||
|
|
402
|
+
// The (right-most) trimmed path component is exactly '..'
|
|
403
|
+
(preDotState === 1 &&
|
|
404
|
+
startDot === end - 1 &&
|
|
405
|
+
startDot === startPart + 1)) {
|
|
406
|
+
return '';
|
|
407
|
+
}
|
|
408
|
+
return path.slice(startDot, end);
|
|
409
|
+
},
|
|
410
|
+
|
|
411
|
+
format: _format.bind( null, '/'),
|
|
412
|
+
|
|
413
|
+
parse(path) {
|
|
414
|
+
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
|
|
415
|
+
if (path.length === 0)
|
|
416
|
+
return ret;
|
|
417
|
+
const isAbsolute =
|
|
418
|
+
path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
|
419
|
+
let start;
|
|
420
|
+
if (isAbsolute) {
|
|
421
|
+
ret.root = '/';
|
|
422
|
+
start = 1;
|
|
423
|
+
} else {
|
|
424
|
+
start = 0;
|
|
425
|
+
}
|
|
426
|
+
let startDot = -1;
|
|
427
|
+
let startPart = 0;
|
|
428
|
+
let end = -1;
|
|
429
|
+
let matchedSlash = true;
|
|
430
|
+
let i = path.length - 1;
|
|
431
|
+
|
|
432
|
+
// Track the state of characters (if any) we see before our first dot and
|
|
433
|
+
// after any path separator we find
|
|
434
|
+
let preDotState = 0;
|
|
435
|
+
|
|
436
|
+
// Get non-dir info
|
|
437
|
+
for (; i >= start; --i) {
|
|
438
|
+
const code = path.charCodeAt(i);
|
|
439
|
+
if (code === CHAR_FORWARD_SLASH) {
|
|
440
|
+
// If we reached a path separator that was not part of a set of path
|
|
441
|
+
// separators at the end of the string, stop now
|
|
442
|
+
if (!matchedSlash) {
|
|
443
|
+
startPart = i + 1;
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (end === -1) {
|
|
449
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
450
|
+
// extension
|
|
451
|
+
matchedSlash = false;
|
|
452
|
+
end = i + 1;
|
|
453
|
+
}
|
|
454
|
+
if (code === CHAR_DOT) {
|
|
455
|
+
// If this is our first dot, mark it as the start of our extension
|
|
456
|
+
if (startDot === -1)
|
|
457
|
+
startDot = i;
|
|
458
|
+
else if (preDotState !== 1)
|
|
459
|
+
preDotState = 1;
|
|
460
|
+
} else if (startDot !== -1) {
|
|
461
|
+
// We saw a non-dot and non-path separator before our dot, so we should
|
|
462
|
+
// have a good chance at having a non-empty extension
|
|
463
|
+
preDotState = -1;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
if (end !== -1) {
|
|
468
|
+
const start = startPart === 0 && isAbsolute ? 1 : startPart;
|
|
469
|
+
if (startDot === -1 ||
|
|
470
|
+
// We saw a non-dot character immediately before the dot
|
|
471
|
+
preDotState === 0 ||
|
|
472
|
+
// The (right-most) trimmed path component is exactly '..'
|
|
473
|
+
(preDotState === 1 &&
|
|
474
|
+
startDot === end - 1 &&
|
|
475
|
+
startDot === startPart + 1)) {
|
|
476
|
+
ret.base = ret.name = path.slice(start, end);
|
|
477
|
+
} else {
|
|
478
|
+
ret.name = path.slice(start, startDot);
|
|
479
|
+
ret.base = path.slice(start, end);
|
|
480
|
+
ret.ext = path.slice(startDot, end);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (startPart > 0)
|
|
485
|
+
ret.dir = path.slice(0, startPart - 1);
|
|
486
|
+
else if (isAbsolute)
|
|
487
|
+
ret.dir = '/';
|
|
488
|
+
|
|
489
|
+
return ret;
|
|
490
|
+
},
|
|
491
|
+
|
|
492
|
+
sep: '/',
|
|
493
|
+
delimiter: ':',
|
|
494
|
+
win32: null,
|
|
495
|
+
posix: null
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
function _format(sep, pathObject) {
|
|
499
|
+
validateObject(pathObject, 'pathObject');
|
|
500
|
+
const dir = pathObject.dir || pathObject.root;
|
|
501
|
+
const base = pathObject.base ||
|
|
502
|
+
`${pathObject.name || ''}${pathObject.ext || ''}`;
|
|
503
|
+
if (!dir) {
|
|
504
|
+
return base;
|
|
505
|
+
}
|
|
506
|
+
return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export default path
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// https://github.com/gr2m/localstorage-memory under MIT
|
|
2
|
+
|
|
3
|
+
const root = {};
|
|
4
|
+
var localStorageMemory = {}
|
|
5
|
+
var cache = {}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* number of stored items.
|
|
9
|
+
*/
|
|
10
|
+
localStorageMemory.length = 0
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* returns item for passed key, or null
|
|
14
|
+
*
|
|
15
|
+
* @para {String} key
|
|
16
|
+
* name of item to be returned
|
|
17
|
+
* @returns {String|null}
|
|
18
|
+
*/
|
|
19
|
+
localStorageMemory.getItem = function (key) {
|
|
20
|
+
if (key in cache) {
|
|
21
|
+
return cache[key]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* sets item for key to passed value, as String
|
|
29
|
+
*
|
|
30
|
+
* @para {String} key
|
|
31
|
+
* name of item to be set
|
|
32
|
+
* @para {String} value
|
|
33
|
+
* value, will always be turned into a String
|
|
34
|
+
* @returns {undefined}
|
|
35
|
+
*/
|
|
36
|
+
localStorageMemory.setItem = function (key, value) {
|
|
37
|
+
if (typeof value === 'undefined') {
|
|
38
|
+
localStorageMemory.removeItem(key)
|
|
39
|
+
} else {
|
|
40
|
+
if (!(cache.hasOwnProperty(key))) {
|
|
41
|
+
localStorageMemory.length++
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
cache[key] = '' + value
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* removes item for passed key
|
|
50
|
+
*
|
|
51
|
+
* @para {String} key
|
|
52
|
+
* name of item to be removed
|
|
53
|
+
* @returns {undefined}
|
|
54
|
+
*/
|
|
55
|
+
localStorageMemory.removeItem = function (key) {
|
|
56
|
+
if (cache.hasOwnProperty(key)) {
|
|
57
|
+
delete cache[key]
|
|
58
|
+
localStorageMemory.length--
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* returns name of key at passed index
|
|
64
|
+
*
|
|
65
|
+
* @para {Number} index
|
|
66
|
+
* Position for key to be returned (starts at 0)
|
|
67
|
+
* @returns {String|null}
|
|
68
|
+
*/
|
|
69
|
+
localStorageMemory.key = function (index) {
|
|
70
|
+
return Object.keys(cache)[index] || null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* removes all stored items and sets length to 0
|
|
75
|
+
*
|
|
76
|
+
* @returns {undefined}
|
|
77
|
+
*/
|
|
78
|
+
localStorageMemory.clear = function () {
|
|
79
|
+
cache = {}
|
|
80
|
+
localStorageMemory.length = 0
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (typeof exports === 'object') {
|
|
84
|
+
module.exports = localStorageMemory
|
|
85
|
+
} else {
|
|
86
|
+
root.localStorage = localStorageMemory
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
export default localStorageMemory;
|
|
91
|
+
|
|
92
|
+
|