@zenfs/core
Version:
A filesystem, anywhere
56 lines (55 loc) • 1.63 kB
JavaScript
import { createCredentials } from './credentials.js';
/**
* Symbol used for context branding
* @internal @hidden
*/
const kIsContext = Symbol('ZenFSContext');
/**
* The default/global context.
* @internal @hidden
* @category Contexts
*/
export const defaultContext = {
[kIsContext]: true,
id: 0,
root: '/',
pwd: '/',
credentials: createCredentials({ uid: 0, gid: 0 }),
descriptors: new Map(),
parent: null,
children: [],
};
export function contextOf($) {
return typeof $ === 'object' && $ !== null && kIsContext in $ ? $ : defaultContext;
}
// 0 is reserved for the global/default context
let _nextId = 1;
/**
* Create a blank FS Context
* @internal
* @category Contexts
* @todo Make sure parent root can't be escaped
*
* This exists so that `kIsContext` is not exported and to make sure the context is "secure".
*/
export function createChildContext(parent, init = {}) {
const { root = parent.root, pwd = parent.pwd, credentials = structuredClone(parent.credentials) } = init;
const ctx = {
[kIsContext]: true,
id: _nextId++,
root,
pwd,
credentials: createCredentials(credentials),
descriptors: new Map(),
parent: parent,
children: [],
};
Object.defineProperties(ctx, {
id: { configurable: false, writable: false },
credentials: { configurable: false, writable: false },
descriptors: { configurable: false, writable: false },
parent: { configurable: false, writable: false },
children: { configurable: false, writable: false },
});
return ctx;
}