Import ISA Project Format 1.1.0
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { validateArchivePath } from './format.js';
|
||||
import { readAuthoritativeFile } from './shared-read.js';
|
||||
import { readKvSnapshot } from './kv-snapshot.js';
|
||||
|
||||
/** Reads through the calling extension's own host grant. The folder can also
|
||||
* be the root of an open project session. No source is opened for writing. */
|
||||
export function createSharedFolderReader({bridge,scope,folder,assertCurrent = () => {}}) {
|
||||
if (typeof bridge?.invoke !== 'function' || !scope?.workspaceId || !scope?.groupId) {
|
||||
throw new TypeError('Host bridge and launch scope required');
|
||||
}
|
||||
if (typeof folder?.path !== 'string' || !folder.path.startsWith('/') ||
|
||||
(folder.workspaceId && folder.workspaceId !== scope.workspaceId) ||
|
||||
(folder.groupId && folder.groupId !== scope.groupId)) {
|
||||
throw new TypeError('Invalid shared folder for this launch');
|
||||
}
|
||||
const root = '/' + validateArchivePath(folder.path.slice(1).replace(/\/$/, ''));
|
||||
const launch = Object.freeze({workspaceId:scope.workspaceId,groupId:scope.groupId});
|
||||
const path = relative => root + '/' + validateArchivePath(relative);
|
||||
async function invoke(command,args) {
|
||||
assertCurrent();
|
||||
const result = await bridge.invoke(command,{...args,...launch});
|
||||
assertCurrent();
|
||||
return result;
|
||||
}
|
||||
const readFile = relative => readAuthoritativeFile(invoke,path(relative),assertCurrent);
|
||||
return {
|
||||
async listFiles(relative = '', recursive = false) {
|
||||
const directory = relative ? path(relative) : root;
|
||||
const inventory = await invoke('filesystem_list_dir',{path:directory,recursive,cachePolicy:'dashboard-required'});
|
||||
if (inventory?.authoritative !== true || !Array.isArray(inventory.files) ||
|
||||
inventory.files.some(file => file?.path !== directory && !file?.path?.startsWith(directory + '/'))) {
|
||||
throw new Error('Shared folder inventory unavailable');
|
||||
}
|
||||
return inventory.files.filter(file => file.path !== directory);
|
||||
},
|
||||
readFile,
|
||||
async readJson(relative) {
|
||||
const file = await readFile(relative);
|
||||
if (file.status === 'absent') return file;
|
||||
let text = file.text;
|
||||
if (typeof text !== 'string') {
|
||||
if (typeof file.bytesBase64 !== 'string') throw new Error('Shared JSON file content unavailable');
|
||||
const bytes = Uint8Array.from(atob(file.bytesBase64), character => character.charCodeAt(0));
|
||||
text = new TextDecoder('utf-8',{fatal:true}).decode(bytes);
|
||||
}
|
||||
return {...file,value:JSON.parse(text)};
|
||||
},
|
||||
async readKv(relative) {
|
||||
return readKvSnapshot(bridge,launch,path(relative),assertCurrent);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user