Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for async transferHandle serializer/deserializer #646

Open
poesterlin opened this issue Oct 6, 2023 · 2 comments
Open

Add support for async transferHandle serializer/deserializer #646

poesterlin opened this issue Oct 6, 2023 · 2 comments

Comments

@poesterlin
Copy link

Im trying to create a transferHandle for Origin Private File System (OPFS) file handles since they are not cloneable in all browsers. This is the approach I tried:

/**
 * Check if the given value is a FileHandle and if it needs to be serialized.
 * @param value 
 * @returns 
 */
function canHandle(value: unknown): value is FileSystemFileHandle {
	const isFileHandle = value instanceof FileSystemFileHandle
	if (!isFileHandle) {
		return false;
	}

	try {
		// if we can clone the value, there is no need to serialize it
		structuredClone(value);
		return false;
	} catch {
		// if we can't clone the value, we need to serialize it
	}

	return true;
}
transferHandlers.set('FileHandle', {
	canHandle,
	serialize: async (ev) => {
		const root = await navigator.storage.getDirectory();
		return [await root.resolve(ev), []];
	},
	deserialize: async (path) => {
		let directory = await navigator.storage.getDirectory();
		const fileName = path.pop();

		for (const part of path) {
			directory = await directory.getDirectoryHandle(part, { create: true });
		}

		return directory.getDirectoryHandle(fileName, { create: true });
	}
} satisfies TransferHandler<FileSystemFileHandle, string[]>);

However, I've run into an issue where the serializer and deserializer functions are not compatible with Promises. This is causing the transferHandle to not work as expected.

I'm wondering if there is another way to solve this issue or if I need to do the serialization manually. Any help or suggestions would be greatly appreciated.

@benjamind
Copy link
Collaborator

When you say not compatible with Promises what do you mean exactly? Can you post an example somewhere to test out?

@taro-28
Copy link

taro-28 commented Apr 11, 2024

Hi! We encountered the same issue and forked to enable the use of async functions with serialize/deserialization.
I hope this will be helpful to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants