question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement FileReaderSync

See original GitHub issue

This would solve…

Currently I am doing something that involves keeping everything out of Promise land, however requires reading a Blob

The implementation should look like…

Should look much like the FileReader however have sync behavior. https://developer.mozilla.org/en-US/docs/Web/API/FileReaderSync

I have also considered…

Currently attempting to make a workaround with the given FileReader available with Unidici as of now.

Additional context

ex.

function mockFormDataToString(this: FormData) {
	const entries = [];
	for (const [key, value] of this.entries()) {
		if (value instanceof Blob) {
			const reader = new FileReaderSync();
			reader.readAsText(value);
			const result = reader.result;

			entries.push([key, result]);
		} else {
			entries.push([key, value]);
		}
	}
	return JSON.stringify({
		__formdata: entries,
	});
}

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Reactions:2
  • Comments:26 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
jimmywartingcommented, Dec 22, 2022

I may have a solution up my sleeve. just going to try it out first

Update: looking good so far… just one more sec to tidy up things.

1reaction
KhafraDevcommented, Dec 23, 2022

Here’s an alternative way that does the bare minimum:

index.js
const { Worker, receiveMessageOnPort } = require('worker_threads')
const { join } = require('path')

const blob = new Blob([Buffer.alloc(2 ** 31 - 1).fill('abcdefg')])

const shared = new SharedArrayBuffer(4)
const { port1: localPort, port2: workerPort } = new MessageChannel()

const path = join(__dirname, 'worker.mjs')

const w = new Worker(path, {
  workerData: { shared, blob, port: workerPort },
  transferList: [workerPort]
})

const int32 = new Int32Array(shared)
Atomics.wait(int32, 0, 0)

const { message } = receiveMessageOnPort(localPort)

console.log(message)
worker.mjs
import { workerData } from 'worker_threads'

const { shared, blob, port } = workerData

port.postMessage({
    body: await blob.arrayBuffer()
})

const int32 = new Int32Array(shared)
Atomics.notify(int32, 0)

Read more comments on GitHub >

github_iconTop Results From Across the Web

FileReaderSync - Web APIs - MDN Web Docs
desktop desktop Chrome Edge FileReaderSync Full support. Chrome7. Toggle history Full support. Edge12. T... FileReaderSync() constructor Full support. Chrome7. Toggle history Full support. Edge12. T...
Read more >
FileReaderSync - Brian Grinstead
I have been working on updating the FileReader.js JavaScript file reading library lately to help out with a few different projects.
Read more >
FileReaderSync - JavaScript: The Definitive Guide, 6th Edition ...
FileReaderSync is a synchronous version of the FileReader API, available only to Worker threads. The synchronous API is easier to use than the...
Read more >
FileReaderSync
The FileReaderSync interface allows to read File or Blob objects in a synchronous way.
Read more >
FileReaderSync | Can I use... Support tables for HTML5, CSS3 ...
FileReaderSync. - WD. Allows files to be read synchronously in Web Workers. Usage % of. all users, all tracked, tracked desktop, tracked mobile....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found