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

fix: make it run in nodejs #5

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/FUNDING.yml

This file was deleted.

23 changes: 0 additions & 23 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

8 changes: 7 additions & 1 deletion mod.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*! to-sync. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */

// Use the native Worker if available, otherwise use the polyfill
const Work = globalThis.Worker || await import('whatwg-worker').then(m => m.default)
// const Work = globalThis.Worker || await import('whatwg-worker').then(m => m.default)
const Work = await import('/Users/jimmywarting/git/web-worker/node-worker.js').then(m => m.default)

function createWorker (signal) {
// Create a shared buffer to communicate with the worker thread
Expand All @@ -24,6 +25,7 @@ function createWorker (signal) {
worker.postMessage({ port: remotePort, code: source, ab }, [remotePort])

return function runSync (...args) {
Atomics.store(int32, 0, 0)
// Send the arguments to the worker thread
localPort.postMessage(args)
// Wait for the worker thread to send the result back
Expand All @@ -35,6 +37,10 @@ function createWorker (signal) {
let bytesLeft = int32[0]
const ok = int32[1]

if (bytesLeft === -1) {
return new Uint8Array(0)
}

// Allocate a new Uint8Array to store the result
const result = new Uint8Array(bytesLeft)
let offset = 0
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "await-sync",
"version": "1.0.1",
"version": "1.0.2",
"description": "Perform async work synchronously using web worker and SharedArrayBuffer",
"main": "mod.js",
"type": "module",
Expand Down Expand Up @@ -50,6 +50,7 @@
},
"devDependencies": {
"make-synchronous": "^1.0.0",
"synckit": "^0.8.5"
"synckit": "^0.8.5",
"tinylet": "^0.1.0"
}
}
34 changes: 26 additions & 8 deletions test/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Buffer } from 'node:buffer'
import { createWorker } from '../mod.js'
import makeSynchronous from 'make-synchronous'
import { createSyncFn } from 'synckit'

import { redlet } from 'tinylet'

// the worker path must be absolute
const workerPath = new URL('./synkit-worker.js', import.meta.url).toString().slice(7)
Expand All @@ -21,10 +21,17 @@ const jim = createWorker()(async path => {
return fs.readFile(new URL(path))
})

// Runs in a worker thread and uses Atomics.wait() to block the current thread.
const redletReader = redlet(async (path) => {
const fs = await import('fs/promises')
return fs.readFile(new URL(path))
})

const control = Buffer.from(readFileSync(new URL(path))).toString()
console.assert(Buffer.from(awaitSync(path)).toString() === control, 'should return the same data')
console.assert(Buffer.from(jim(path)).toString() === control, 'should return the same data')
console.assert(Buffer.from(sin(path)).toString() === control, 'should return the same data')
// console.assert(Buffer.from(awaitSync(path)).toString() === control, 'should return the same data')
// console.assert(Buffer.from(jim(path)).toString() === control, 'should return the same data')
// console.assert(Buffer.from(sin(path)).toString() === control, 'should return the same data')
// console.assert(Buffer.from(redletReader(path)).toString() === control, 'should return the same data')

let i

Expand All @@ -33,19 +40,30 @@ console.time('fs.readFileSync')
while (i--) readFileSync(new URL(path))
console.timeEnd('fs.readFileSync')

globalThis?.gc()

i = 100
console.time('redletReader')
while (i--) redletReader(path)
console.timeEnd('redletReader')

globalThis?.gc()

i = 100
console.time('synkit')
while (i--) awaitSync(path)
console.timeEnd('synkit')

globalThis?.gc()

i = 100
console.time('to-sync')
console.time('await-sync')
while (i--) jim(path)
console.timeEnd('to-sync')
console.timeEnd('await-sync')

globalThis?.gc()

i = 100
console.time('make-syncronous')
while (i--) sin(path)
console.timeEnd('make-syncronous')

process.exit()
13 changes: 12 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ const fn = awaitSync(async function (pkg) {
return textEncoder.encode(str)
}, r => new TextDecoder().decode(r))

const returnsEmptyData = awaitSync(async function () {
return new Uint8Array(0)
})

console.assert(returnsEmptyData().byteLength === 0, 'empty byteLength should be 0')

const pkg = fn(new URL('../package.json', import.meta.url) + '')
ctrl.abort()
const json = JSON.parse(pkg)
console.assert(json.name === 'await-sync', 'should return the same data')

if (json.name === 'await-sync') {
console.log('test completed')
} else {
throw new Error('test failed')
}
14 changes: 10 additions & 4 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ addEventListener('message', async evt => {
const int32 = new Int32Array(ab, 0, 2)

/** @param {Uint8Array} buf */
const write2 = buf => {
const write = buf => {
let bytesLeft = buf.byteLength
let offset = 0

if (bytesLeft === 0) {
int32[0] = -1
Atomics.notify(int32, 0)
}

while (bytesLeft > 0) {
int32[0] = bytesLeft
const chunkSize = Math.min(bytesLeft, data.byteLength)
Expand All @@ -24,8 +29,9 @@ addEventListener('message', async evt => {
}
}

const blob = new Blob([code], { type: 'text/javascript' })
const url = URL.createObjectURL(blob)
// const blob = new Blob([code], { type: 'text/javascript' })
// const url = URL.createObjectURL(blob)
const url = "data:text/javascript," + encodeURIComponent(code)
const { default: fn } = await import(url)

port.onmessage = async function onmessage (evt) {
Expand All @@ -47,6 +53,6 @@ addEventListener('message', async evt => {
})

int32[1] = ok
write2(u8)
write(u8)
}
})