@openai/agents-core
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows.
159 lines • 5.74 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Permissions = exports.DEFAULT_SANDBOX_ENTRY_PERMISSIONS = exports.FileMode = void 0;
exports.permissionsForSandboxEntry = permissionsForSandboxEntry;
exports.normalizePermissions = normalizePermissions;
var FileMode;
(function (FileMode) {
FileMode[FileMode["ALL"] = 7] = "ALL";
FileMode[FileMode["NONE"] = 0] = "NONE";
FileMode[FileMode["READ"] = 4] = "READ";
FileMode[FileMode["WRITE"] = 2] = "WRITE";
FileMode[FileMode["EXEC"] = 1] = "EXEC";
})(FileMode || (exports.FileMode = FileMode = {}));
exports.DEFAULT_SANDBOX_ENTRY_PERMISSIONS = {
owner: FileMode.ALL,
group: FileMode.READ | FileMode.EXEC,
other: FileMode.READ | FileMode.EXEC,
directory: false,
};
function permissionsForSandboxEntry(permissions) {
return new Permissions(permissions ?? exports.DEFAULT_SANDBOX_ENTRY_PERMISSIONS);
}
class Permissions {
owner;
group;
other;
directory;
sticky;
constructor(init = {}) {
if (init instanceof Permissions) {
this.owner = init.owner;
this.group = init.group;
this.other = init.other;
this.directory = init.directory;
this.sticky = init.sticky;
return;
}
if (typeof init === 'string') {
const parsed = Permissions.fromString(init);
this.owner = parsed.owner;
this.group = parsed.group;
this.other = parsed.other;
this.directory = parsed.directory;
this.sticky = parsed.sticky;
return;
}
if (typeof init === 'number') {
const parsed = Permissions.fromMode(init);
this.owner = parsed.owner;
this.group = parsed.group;
this.other = parsed.other;
this.directory = parsed.directory;
this.sticky = parsed.sticky;
return;
}
this.owner = normalizePermissionBits(init.owner ?? FileMode.ALL, 'owner');
this.group = normalizePermissionBits(init.group ?? FileMode.NONE, 'group');
this.other = normalizePermissionBits(init.other ?? FileMode.NONE, 'other');
this.directory = init.directory ?? false;
if (init.sticky !== undefined && typeof init.sticky !== 'boolean') {
throw new TypeError('Permission sticky must be a boolean.');
}
this.sticky = init.sticky ?? false;
}
static fromMode(mode) {
return new Permissions({
owner: (mode >> 6) & 0b111,
group: (mode >> 3) & 0b111,
other: mode & 0b111,
directory: (mode & 0o40000) !== 0,
sticky: (mode & 0o1000) !== 0,
});
}
static fromString(value) {
const permissions = value.length === 11 && /[.@+]$/u.test(value) ? value.slice(0, -1) : value;
if (permissions.length !== 10) {
throw new Error(`Invalid permissions string length: ${value}`);
}
if (permissions[0] !== 'd' && permissions[0] !== '-') {
throw new Error(`Invalid permissions type: ${value}`);
}
return new Permissions({
directory: permissions[0] === 'd',
sticky: permissions[9] === 't' || permissions[9] === 'T',
owner: parsePermissionTriplet(permissions.slice(1, 4), ['s', 'S']),
group: parsePermissionTriplet(permissions.slice(4, 7), ['s', 'S']),
other: parsePermissionTriplet(permissions.slice(7, 10), ['t', 'T']),
});
}
toMode() {
return ((this.directory ? 0o40000 : 0) |
(this.sticky ? 0o1000 : 0) |
(this.owner << 6) |
(this.group << 3) |
this.other);
}
normalized() {
return {
owner: this.owner,
group: this.group,
other: this.other,
directory: this.directory,
...(this.sticky ? { sticky: true } : {}),
};
}
toString() {
return `${this.directory ? 'd' : '-'}${formatPermissionTriplet(this.owner)}${formatPermissionTriplet(this.group)}${formatPermissionTriplet(this.other, this.sticky)}`;
}
}
exports.Permissions = Permissions;
function normalizePermissions(permissions) {
return new Permissions(permissions).normalized();
}
function normalizePermissionBits(value, label) {
if (!Number.isInteger(value) || value < 0 || value > 0b111) {
throw new Error(`Permission ${label} bits must be an integer from 0 to 7.`);
}
return value;
}
function parsePermissionTriplet(value, specialExecChars) {
if (value.length !== 3) {
throw new Error(`Invalid permissions triplet: ${value}`);
}
let mode = 0;
if (value[0] === 'r') {
mode |= FileMode.READ;
}
else if (value[0] !== '-') {
throw new Error(`Invalid read flag: ${value}`);
}
if (value[1] === 'w') {
mode |= FileMode.WRITE;
}
else if (value[1] !== '-') {
throw new Error(`Invalid write flag: ${value}`);
}
const [withExec, withoutExec] = specialExecChars;
if (value[2] === 'x' || value[2] === withExec) {
mode |= FileMode.EXEC;
}
else if (value[2] !== '-' && value[2] !== withoutExec) {
throw new Error(`Invalid exec flag: ${value}`);
}
return mode;
}
function formatPermissionTriplet(value, sticky = false) {
return [
value & FileMode.READ ? 'r' : '-',
value & FileMode.WRITE ? 'w' : '-',
sticky
? value & FileMode.EXEC
? 't'
: 'T'
: value & FileMode.EXEC
? 'x'
: '-',
].join('');
}
//# sourceMappingURL=permissions.js.map