Reject invalid types for puter.fs.upload() items or .write() data

Previously, this would crash in upload() when trying to iterate the
`entries` array, which is undefined when the `items` parameter is an
unsupported type.
This commit is contained in:
Sam Atkins 2024-05-17 14:38:46 +01:00
parent a9c89cef19
commit 03fe3b6a0d
2 changed files with 10 additions and 1 deletions

View File

@ -122,7 +122,11 @@ const upload = async function(items, dirPath, options = {}){
entries[i].filepath = entries[i].name;
entries[i].fullPath = entries[i].name;
}
}
}
// Anything else is invalid
else {
return error({ code: 'field_invalid', message: 'upload() items parameter is an invalid type' });
}
// Will hold directories and files to be uploaded
let dirs = [];

View File

@ -46,6 +46,11 @@ const write = async function (targetPath, data, options = {}) {
if(!data)
data = new File([data ?? ''], filename);
// data should be a File now. If it's not, it's an unsupported type
if (!(data instanceof File)) {
throw new Error({ code: 'field_invalid', message: 'write() data parameter is an invalid type' });
}
// perform upload
return this.upload(data, parent, options);
}