/
util.ts
45 lines (37 loc) · 1.49 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import type { NodeFsaContext } from './types';
/**
* Creates a new {@link NodeFsaContext}.
*/
export const ctx = (partial: Partial<NodeFsaContext> = {}): NodeFsaContext => {
return {
separator: '/',
syncHandleAllowed: false,
mode: 'read',
...partial,
};
};
export const basename = (path: string, separator: string) => {
if (path[path.length - 1] === separator) path = path.slice(0, -1);
const lastSlashIndex = path.lastIndexOf(separator);
return lastSlashIndex === -1 ? path : path.slice(lastSlashIndex + 1);
};
const nameRegex = /^(\.{1,2})$|^(.*([\/\\]).*)$/;
export const assertName = (name: string, method: string, klass: string) => {
const isInvalid = !name || nameRegex.test(name);
if (isInvalid) throw new TypeError(`Failed to execute '${method}' on '${klass}': Name is not allowed.`);
};
export const assertCanWrite = (mode: 'read' | 'readwrite') => {
if (mode !== 'readwrite')
throw new DOMException(
'The request is not allowed by the user agent or the platform in the current context.',
'NotAllowedError',
);
};
export const newNotFoundError = () =>
new DOMException(
'A requested file or directory could not be found at the time an operation was processed.',
'NotFoundError',
);
export const newTypeMismatchError = () =>
new DOMException('The path supplied exists, but was not an entry of requested type.', 'TypeMismatchError');
export const newNotAllowedError = () => new DOMException('Permission not granted.', 'NotAllowedError');