npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

await-sync

v1.0.3

Published

Perform async work synchronously using web worker and SharedArrayBuffer

Downloads

31

Readme

await-sync

Make an asynchronous function synchronous

The only cross compatible solution that works fine in Deno, NodeJS and also Web Workers

The benefit of this package over other desync or synckit, make-synchronous and others libs is that this only uses web tech like Worker and SharedArrayBuffer instead of spawning new processes or using any nodes specific like: receiveMessageOnPort(port) or WorkerData to transfer the data. therefor this also runs fine in other environment too even inside Web workers (but requires some Security steps

What more? well, it uses a more enhanced Worker inside of NodeJS that make it more rich by supplementing it with a own https:// loader so you can import things from cdn You can also even do import('blob:uuid')

Install

npm install await-sync

Usage

import { createWorker } from 'await-sync'

const awaitSync = createWorker()

const get = awaitSync(async url => {
  const res = await fetch(url)
  const ab = await res.arrayBuffer()

  // Must return Uint8Array.
  return new Uint8Array(ab)
})

const uint8 = get('https://httpbin.org/get')
const blob = new Blob([uint8])
const arrayBuffer = uint8.buffer
const text = new TextDecoder().decode(uint8)
const json = JSON.parse(text)

// way read blob sync, which you can't do with other desync libs
// Thanks to PostMessage and StructuralClone
const readBlobSync = toSync(async blob => blob.arrayBuffer().then(ab => new Uint8Array(ab)))
const u8 = readBlobSync(new Blob(['abc'])) // Uint8Array([97,98,99])

API

toSync(fn, formatter?)

Returns a wrapped version of the given async function which executes synchronously. This means no other code will execute (not even async code) until the given async function is done.

The given function is executed in a separate Worker thread, so you cannot use any variables/imports from outside the scope of the function. You can pass in arguments to the function. To import dependencies, use await import(…) in the function body.

The argument you supply to the returned wrapped function is send via postMessage instead of using Uint8Array and Atomics so they are structural clone'able

But the response in the given function must always return a Uint8Array b/c there is no other way to transfer the data over in a synchronous other than blocking the main thread with Atomic.wait()

Use a (de)serializer

If you only using this in NodeJS then there is a grate built in v8 (de)serializer It supports most values, but not functions and symbols.

import { deserialize } from 'node:v8'

const get = awaitSync(async url => {
  const { serialize } = await import('node:v8')
	const res = await fetch(url)
  const json = await res.json()

  return serialize(json)
}, deserialize)

const json = get('https://httpbin.org/get') // JSON

For the most part i reccommend just sticking to JSON.parse and stringify and TextDecoder/Encoder. But if you need to transfer more structural data in other env. too then use something like cbox-x or other binary representations, here is a list of alternatives

Misc

If two separate functions imports the same ESM then it will only be loaded once. That's b/c they share the same worker thread. The web workers are never terminated. so the first call may take a bit longer but the 2nd won't

const fn1 = awaitSync(async () => {
  globalThis.xyz ??= 0
  console.log(globalThis.xyz++)

  const util = await import('./util.js')
  return new Uint8Array()
})

const fn2 = awaitSync(async () => {
  globalThis.xyz ??= 0
  console.log(globalThis.xyz++)

  const util = await import('./util.js')
  return new Uint8Array()
})

fn1() // Warm up - 527ms (logs: 0)
fn1() // instant - 24ms  (logs: 1)
fn2() // instant - 21ms  (logs: 2)

To terminate the worker, pass in a signal and kill it using a AbortController

const ctrl = new AbortController()
createWorker(ctrl.signal)
ctrl.abort()