@xylabs/threads 4.6.4 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/master/implementation.browser.mjs +89 -0
- package/dist/browser/master/implementation.browser.mjs.map +1 -0
- package/dist/browser/worker/worker.browser.mjs +291 -0
- package/dist/browser/worker/worker.browser.mjs.map +1 -0
- package/dist/neutral/index.mjs +1022 -0
- package/dist/neutral/index.mjs.map +1 -0
- package/dist/neutral/master/implementation.mjs +264 -0
- package/dist/neutral/master/implementation.mjs.map +1 -0
- package/dist/neutral/master/index.mjs +988 -0
- package/dist/neutral/master/index.mjs.map +1 -0
- package/dist/neutral/master/pool.mjs +579 -0
- package/dist/neutral/master/pool.mjs.map +1 -0
- package/dist/neutral/master/register.mjs +272 -0
- package/dist/neutral/master/register.mjs.map +1 -0
- package/dist/neutral/master/spawn.mjs +412 -0
- package/dist/neutral/master/spawn.mjs.map +1 -0
- package/dist/neutral/master/thread.mjs +29 -0
- package/dist/neutral/master/thread.mjs.map +1 -0
- package/dist/neutral/observable-promise.mjs +132 -0
- package/dist/neutral/observable-promise.mjs.map +1 -0
- package/dist/neutral/observable.mjs +31 -0
- package/dist/neutral/observable.mjs.map +1 -0
- package/dist/node/master/implementation.node.mjs +154 -0
- package/dist/node/master/implementation.node.mjs.map +1 -0
- package/dist/node/worker/worker.node.mjs +304 -0
- package/dist/node/worker/worker.node.mjs.map +1 -0
- package/dist/{common.d.ts → types/common.d.ts} +5 -1
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/{master → types/master}/get-bundle-url.browser.d.ts +1 -0
- package/dist/types/master/get-bundle-url.browser.d.ts.map +1 -0
- package/dist/{master → types/master}/implementation.browser.d.ts +2 -1
- package/dist/types/master/implementation.browser.d.ts.map +1 -0
- package/dist/{master → types/master}/implementation.d.ts +4 -1
- package/dist/types/master/implementation.d.ts.map +1 -0
- package/dist/{master → types/master}/implementation.node.d.ts +2 -1
- package/dist/types/master/implementation.node.d.ts.map +1 -0
- package/dist/types/master/index.d.ts +13 -0
- package/dist/types/master/index.d.ts.map +1 -0
- package/dist/{master → types/master}/invocation-proxy.d.ts +2 -1
- package/dist/types/master/invocation-proxy.d.ts.map +1 -0
- package/dist/{master → types/master}/pool-types.d.ts +16 -1
- package/dist/types/master/pool-types.d.ts.map +1 -0
- package/dist/types/master/pool.d.ts +93 -0
- package/dist/types/master/pool.d.ts.map +1 -0
- package/dist/types/master/register.d.ts +2 -0
- package/dist/types/master/register.d.ts.map +1 -0
- package/dist/{master → types/master}/spawn.d.ts +12 -2
- package/dist/types/master/spawn.d.ts.map +1 -0
- package/dist/types/master/thread.d.ts +13 -0
- package/dist/types/master/thread.d.ts.map +1 -0
- package/dist/{observable-promise.d.ts → types/observable-promise.d.ts} +14 -0
- package/dist/types/observable-promise.d.ts.map +1 -0
- package/dist/types/observable.d.ts +21 -0
- package/dist/types/observable.d.ts.map +1 -0
- package/dist/{ponyfills.d.ts → types/ponyfills.d.ts} +1 -0
- package/dist/types/ponyfills.d.ts.map +1 -0
- package/dist/types/promise.d.ts +6 -0
- package/dist/types/promise.d.ts.map +1 -0
- package/dist/{serializers.d.ts → types/serializers.d.ts} +1 -0
- package/dist/types/serializers.d.ts.map +1 -0
- package/dist/{symbols.d.ts → types/symbols.d.ts} +1 -0
- package/dist/types/symbols.d.ts.map +1 -0
- package/dist/types/transferable.d.ts +43 -0
- package/dist/types/transferable.d.ts.map +1 -0
- package/dist/types/{master.d.ts → types/master.d.ts} +17 -3
- package/dist/types/types/master.d.ts.map +1 -0
- package/dist/types/{messages.d.ts → types/messages.d.ts} +1 -0
- package/dist/types/types/messages.d.ts.map +1 -0
- package/dist/types/{worker.d.ts → types/worker.d.ts} +1 -0
- package/dist/types/types/worker.d.ts.map +1 -0
- package/dist/types/worker/WorkerGlobalScope.d.ts +6 -0
- package/dist/types/worker/WorkerGlobalScope.d.ts.map +1 -0
- package/dist/types/worker/expose.d.ts +4 -0
- package/dist/types/worker/expose.d.ts.map +1 -0
- package/dist/types/worker/worker.browser.d.ts +14 -0
- package/dist/types/worker/worker.browser.d.ts.map +1 -0
- package/dist/types/worker/worker.node.d.ts +25 -0
- package/dist/types/worker/worker.node.d.ts.map +1 -0
- package/package.json +66 -77
- package/src/common.ts +10 -6
- package/src/index.ts +10 -9
- package/src/master/get-bundle-url.browser.ts +2 -1
- package/src/master/implementation.browser.ts +2 -2
- package/src/master/implementation.node.ts +19 -96
- package/src/master/implementation.ts +2 -2
- package/src/master/index.ts +7 -7
- package/src/master/invocation-proxy.ts +6 -6
- package/src/master/pool-types.ts +1 -1
- package/src/master/pool.ts +14 -13
- package/src/master/register.ts +2 -1
- package/src/master/spawn.ts +8 -8
- package/src/master/thread.ts +2 -2
- package/src/observable-promise.ts +3 -2
- package/src/serializers.ts +1 -1
- package/src/transferable.ts +2 -1
- package/src/types/master.ts +3 -3
- package/src/worker/WorkerGlobalScope.ts +5 -0
- package/src/worker/expose.ts +234 -0
- package/src/worker/is-observable.d.ts +7 -0
- package/src/worker/{implementation.browser.ts → worker.browser.ts} +26 -10
- package/src/worker/{implementation.worker_threads.ts → worker.node.ts} +30 -12
- package/types/is-observable.d.ts +1 -1
- package/xy.config.ts +24 -0
- package/dist/common.js +0 -16
- package/dist/esm/common.js +0 -16
- package/dist/esm/index.js +0 -26
- package/dist/esm/master/get-bundle-url.browser.js +0 -25
- package/dist/esm/master/implementation.browser.js +0 -65
- package/dist/esm/master/implementation.js +0 -43
- package/dist/esm/master/implementation.node.js +0 -205
- package/dist/esm/master/index.js +0 -14
- package/dist/esm/master/invocation-proxy.js +0 -121
- package/dist/esm/master/pool-types.js +0 -14
- package/dist/esm/master/pool.js +0 -262
- package/dist/esm/master/register.js +0 -11
- package/dist/esm/master/spawn.js +0 -114
- package/dist/esm/master/thread.js +0 -18
- package/dist/esm/observable-promise.js +0 -132
- package/dist/esm/observable.js +0 -33
- package/dist/esm/ponyfills.js +0 -20
- package/dist/esm/promise.js +0 -23
- package/dist/esm/serializers.js +0 -41
- package/dist/esm/symbols.js +0 -8
- package/dist/esm/transferable.js +0 -25
- package/dist/esm/types/master.js +0 -9
- package/dist/esm/types/messages.js +0 -16
- package/dist/esm/types/worker.js +0 -2
- package/dist/esm/worker/bundle-entry.js +0 -26
- package/dist/esm/worker/implementation.browser.js +0 -24
- package/dist/esm/worker/implementation.js +0 -19
- package/dist/esm/worker/implementation.tiny-worker.js +0 -37
- package/dist/esm/worker/implementation.worker_threads.js +0 -41
- package/dist/esm/worker/index.js +0 -174
- package/dist/esm/worker_threads.js +0 -13
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -26
- package/dist/master/get-bundle-url.browser.js +0 -25
- package/dist/master/implementation.browser.js +0 -65
- package/dist/master/implementation.js +0 -43
- package/dist/master/implementation.node.js +0 -205
- package/dist/master/index.d.ts +0 -10
- package/dist/master/index.js +0 -14
- package/dist/master/invocation-proxy.js +0 -121
- package/dist/master/pool-types.js +0 -14
- package/dist/master/pool.d.ts +0 -50
- package/dist/master/pool.js +0 -262
- package/dist/master/register.d.ts +0 -1
- package/dist/master/register.js +0 -11
- package/dist/master/spawn.js +0 -114
- package/dist/master/thread.d.ts +0 -8
- package/dist/master/thread.js +0 -18
- package/dist/observable-promise.js +0 -132
- package/dist/observable.d.ts +0 -11
- package/dist/observable.js +0 -33
- package/dist/ponyfills.js +0 -20
- package/dist/promise.d.ts +0 -1
- package/dist/promise.js +0 -23
- package/dist/serializers.js +0 -41
- package/dist/symbols.js +0 -8
- package/dist/transferable.d.ts +0 -9
- package/dist/transferable.js +0 -25
- package/dist/types/master.js +0 -9
- package/dist/types/messages.js +0 -16
- package/dist/types/worker.js +0 -2
- package/dist/worker/bundle-entry.d.ts +0 -1
- package/dist/worker/bundle-entry.js +0 -26
- package/dist/worker/implementation.browser.d.ts +0 -6
- package/dist/worker/implementation.browser.js +0 -24
- package/dist/worker/implementation.d.ts +0 -3
- package/dist/worker/implementation.js +0 -19
- package/dist/worker/implementation.tiny-worker.d.ts +0 -6
- package/dist/worker/implementation.tiny-worker.js +0 -37
- package/dist/worker/implementation.worker_threads.d.ts +0 -8
- package/dist/worker/implementation.worker_threads.js +0 -41
- package/dist/worker/index.d.ts +0 -5
- package/dist/worker/index.js +0 -174
- package/dist/worker_threads.d.ts +0 -8
- package/dist/worker_threads.js +0 -13
- package/observable.d.ts +0 -2
- package/observable.js +0 -2
- package/observable.mjs +0 -4
- package/register.d.ts +0 -2
- package/register.js +0 -2
- package/register.mjs +0 -1
- package/rollup.config.js +0 -16
- package/src/worker/bundle-entry.ts +0 -10
- package/src/worker/implementation.tiny-worker.ts +0 -55
- package/src/worker/implementation.ts +0 -23
- package/src/worker/index.ts +0 -230
- package/src/worker_threads.ts +0 -27
- package/test/lib/index.ts +0 -1
- package/test/lib/serialization.ts +0 -38
- package/test/observable-promise.test.ts +0 -205
- package/test/observable.test.ts +0 -87
- package/test/pool.test.ts +0 -183
- package/test/serialization.test.ts +0 -23
- package/test/spawn.chromium.mocha.ts +0 -53
- package/test/spawn.test.ts +0 -87
- package/test/streaming.test.ts +0 -29
- package/test/transferables.test.ts +0 -71
- package/test/workers/arraybuffer-xor.ts +0 -10
- package/test/workers/count-to-five.ts +0 -12
- package/test/workers/counter.ts +0 -19
- package/test/workers/faulty-function.ts +0 -5
- package/test/workers/hello-world.ts +0 -5
- package/test/workers/increment.ts +0 -8
- package/test/workers/minmax.ts +0 -25
- package/test/workers/serialization.ts +0 -13
- package/test/workers/top-level-throw.ts +0 -1
- package/test-tooling/rollup/app.js +0 -21
- package/test-tooling/rollup/rollup.config.ts +0 -14
- package/test-tooling/rollup/worker.js +0 -7
- package/test-tooling/tsconfig/minimal.ts +0 -12
- package/test-tooling/webpack/addition-worker.ts +0 -9
- package/test-tooling/webpack/app-with-inlined-worker.ts +0 -28
- package/test-tooling/webpack/app.ts +0 -61
- package/test-tooling/webpack/pool-worker.ts +0 -5
- package/test-tooling/webpack/raw-loader.d.ts +0 -4
- package/test-tooling/webpack/webpack.chromium.mocha.ts +0 -21
- package/test-tooling/webpack/webpack.node.config.js +0 -29
- package/test-tooling/webpack/webpack.web.config.js +0 -28
- package/types/webworker.d.ts +0 -9
- package/worker.d.ts +0 -2
- package/worker.js +0 -2
- package/worker.mjs +0 -6
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import-x/no-internal-modules */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
|
-
|
|
5
|
-
import { expect, test } from 'vitest'
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
spawn, Thread, Transfer, Worker,
|
|
9
|
-
} from '../src/index'
|
|
10
|
-
import type { XorBuffer } from './workers/arraybuffer-xor.ts'
|
|
11
|
-
|
|
12
|
-
type SpyInit<Args extends any[], OriginalReturn, NewReturn> = (originalFn: (...args: Args) => OriginalReturn) => (...args: Args) => NewReturn
|
|
13
|
-
|
|
14
|
-
const arrayBufferPlaceholder = Symbol('ArrayBufferPlaceholder')
|
|
15
|
-
|
|
16
|
-
function spyOn<Args extends any[], OriginalReturn, NewReturn>(
|
|
17
|
-
target: (...args: Args) => OriginalReturn,
|
|
18
|
-
spy: SpyInit<Args, OriginalReturn, NewReturn>,
|
|
19
|
-
): (...args: Args) => NewReturn {
|
|
20
|
-
return spy(target)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function replaceArrayBufferWithPlaceholder<In extends any>(
|
|
24
|
-
obj: In,
|
|
25
|
-
arrayBuffer: ArrayBufferLike,
|
|
26
|
-
): In extends ArrayBufferLike ? In | typeof arrayBufferPlaceholder : In {
|
|
27
|
-
if ((obj as any) === arrayBuffer) {
|
|
28
|
-
return arrayBufferPlaceholder as any
|
|
29
|
-
} else if (Array.isArray(obj)) {
|
|
30
|
-
return (obj as any[]).map(element => replaceArrayBufferWithPlaceholder(element, arrayBuffer)) as any
|
|
31
|
-
} else if (obj && typeof obj === 'object') {
|
|
32
|
-
const result: In = Object.create(Object.getPrototypeOf(obj))
|
|
33
|
-
|
|
34
|
-
for (const key of Object.getOwnPropertyNames(obj)) {
|
|
35
|
-
(result as any)[key] = replaceArrayBufferWithPlaceholder((obj as any)[key], arrayBuffer)
|
|
36
|
-
}
|
|
37
|
-
return result as any
|
|
38
|
-
} else {
|
|
39
|
-
return obj as any
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
test('can pass transferable objects on thread call', async () => {
|
|
44
|
-
const testData = new ArrayBuffer(64)
|
|
45
|
-
|
|
46
|
-
const worker = new Worker('./workers/arraybuffer-xor.ts')
|
|
47
|
-
const postMessageCalls: Array<any[]> = []
|
|
48
|
-
|
|
49
|
-
worker.postMessage = spyOn(worker.postMessage.bind(worker), postMessage => (...args) => {
|
|
50
|
-
postMessageCalls.push(replaceArrayBufferWithPlaceholder(args, testData))
|
|
51
|
-
return postMessage(...args)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
const xorBuffer = await spawn<XorBuffer>(worker)
|
|
55
|
-
const returnedBuffer = await xorBuffer(Transfer(testData), 15)
|
|
56
|
-
|
|
57
|
-
expect(returnedBuffer.byteLength).toBe(64)
|
|
58
|
-
expect(postMessageCalls.length).toBe(1)
|
|
59
|
-
expect(postMessageCalls[0].length).toBe(2)
|
|
60
|
-
expect(postMessageCalls[0][0]).toEqual({
|
|
61
|
-
args: [arrayBufferPlaceholder, 15],
|
|
62
|
-
method: undefined,
|
|
63
|
-
type: 'run',
|
|
64
|
-
uid: postMessageCalls[0][0].uid,
|
|
65
|
-
})
|
|
66
|
-
expect(postMessageCalls[0][1]).toEqual([arrayBufferPlaceholder])
|
|
67
|
-
|
|
68
|
-
await Thread.terminate(xorBuffer)
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
test.todo('can pass transferable objects as observable values')
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { expose, Transfer } from '../../src/worker'
|
|
2
|
-
|
|
3
|
-
function xor(buffer: ArrayBufferLike, value: number) {
|
|
4
|
-
const view = new Uint8Array(buffer)
|
|
5
|
-
for (const [offset, byte] of view.entries()) view.set([byte ^ value], offset)
|
|
6
|
-
return Transfer(buffer)
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
expose(xor)
|
|
10
|
-
export type XorBuffer = typeof xor
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Observable } from 'observable-fns'
|
|
2
|
-
|
|
3
|
-
import { expose } from '../../src/worker'
|
|
4
|
-
|
|
5
|
-
expose(function countToFive() {
|
|
6
|
-
return new Observable((observer) => {
|
|
7
|
-
for (let counter = 1; counter <= 5; counter++) {
|
|
8
|
-
observer.next(counter)
|
|
9
|
-
}
|
|
10
|
-
observer.complete()
|
|
11
|
-
})
|
|
12
|
-
})
|
package/test/workers/counter.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { expose } from '../../src/worker'
|
|
2
|
-
|
|
3
|
-
let currentCount = 0
|
|
4
|
-
|
|
5
|
-
const counter = {
|
|
6
|
-
decrement() {
|
|
7
|
-
return --currentCount
|
|
8
|
-
},
|
|
9
|
-
getCount() {
|
|
10
|
-
return currentCount
|
|
11
|
-
},
|
|
12
|
-
increment() {
|
|
13
|
-
return ++currentCount
|
|
14
|
-
},
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
expose(counter)
|
|
18
|
-
|
|
19
|
-
export type Counter = typeof counter
|
package/test/workers/minmax.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line import-x/no-internal-modules
|
|
2
|
-
import { Observable, Subject } from '../../src/observable'
|
|
3
|
-
import { expose } from '../../src/worker'
|
|
4
|
-
|
|
5
|
-
let max = Number.NEGATIVE_INFINITY
|
|
6
|
-
let min = Number.POSITIVE_INFINITY
|
|
7
|
-
|
|
8
|
-
let subject = new Subject()
|
|
9
|
-
|
|
10
|
-
const minmax = {
|
|
11
|
-
finish() {
|
|
12
|
-
subject.complete()
|
|
13
|
-
subject = new Subject()
|
|
14
|
-
},
|
|
15
|
-
push(value) {
|
|
16
|
-
max = Math.max(max, value)
|
|
17
|
-
min = Math.min(min, value)
|
|
18
|
-
subject.next({ max, min })
|
|
19
|
-
},
|
|
20
|
-
values() {
|
|
21
|
-
return Observable.from(subject)
|
|
22
|
-
},
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
expose(minmax)
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import-x/no-internal-modules */
|
|
2
|
-
/* eslint-disable require-await */
|
|
3
|
-
|
|
4
|
-
import { expose, registerSerializer } from '../../src/worker'
|
|
5
|
-
import { Foo, fooSerializer } from '../lib/serialization'
|
|
6
|
-
|
|
7
|
-
registerSerializer(fooSerializer)
|
|
8
|
-
|
|
9
|
-
async function run(foo: Foo<string>) {
|
|
10
|
-
return new Foo(foo.getValue() + foo.getValue())
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
expose(run)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
throw new Error('Top-level worker error')
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-restricted-imports */
|
|
2
|
-
import {
|
|
3
|
-
spawn, Thread, Worker,
|
|
4
|
-
} from '../..'
|
|
5
|
-
|
|
6
|
-
async function run() {
|
|
7
|
-
const add = await spawn(new Worker('./worker.js'))
|
|
8
|
-
const result = await add(2, 3)
|
|
9
|
-
await Thread.terminate(add)
|
|
10
|
-
return result
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
run()
|
|
14
|
-
.then((result) => {
|
|
15
|
-
console.log(`Result: ${result}`)
|
|
16
|
-
puppet.exit(0)
|
|
17
|
-
})
|
|
18
|
-
.catch((error) => {
|
|
19
|
-
console.error(error)
|
|
20
|
-
puppet.exit(1)
|
|
21
|
-
})
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import commonjs from '@rollup/plugin-commonjs'
|
|
2
|
-
import { nodeResolve } from '@rollup/plugin-node-resolve'
|
|
3
|
-
|
|
4
|
-
export default {
|
|
5
|
-
plugins: [
|
|
6
|
-
nodeResolve({
|
|
7
|
-
browser: true,
|
|
8
|
-
mainFields: ['module', 'main'],
|
|
9
|
-
preferBuiltins: true,
|
|
10
|
-
}),
|
|
11
|
-
|
|
12
|
-
commonjs(),
|
|
13
|
-
],
|
|
14
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-restricted-imports */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
3
|
-
import {
|
|
4
|
-
spawn, Thread, Worker,
|
|
5
|
-
} from '../..'
|
|
6
|
-
|
|
7
|
-
async function main() {
|
|
8
|
-
const helloWorld = await spawn(new Worker('./workers/hello-world.ts'))
|
|
9
|
-
await Thread.terminate(helloWorld)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
main()
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/* eslint-disable import-x/no-unresolved */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
|
|
4
|
-
/// <reference types="./raw-loader" />
|
|
5
|
-
|
|
6
|
-
import AdditionWorkerNodeBundle from 'raw-loader!./dist/addition-worker.node/worker.js'
|
|
7
|
-
import AdditionWorkerWebBundle from 'raw-loader!./dist/addition-worker.web/worker.js'
|
|
8
|
-
|
|
9
|
-
import { BlobWorker, spawn } from '../../src/index'
|
|
10
|
-
|
|
11
|
-
const AdditionWorkerBundle = (process as any).browser ? AdditionWorkerWebBundle : AdditionWorkerNodeBundle
|
|
12
|
-
type AdditionWorker = (a: number, b: number) => number
|
|
13
|
-
|
|
14
|
-
async function test() {
|
|
15
|
-
// We also want to test if referencing multiple different workers in a module
|
|
16
|
-
// built using webpack works
|
|
17
|
-
|
|
18
|
-
const add = await spawn<AdditionWorker>(BlobWorker.fromText(AdditionWorkerBundle))
|
|
19
|
-
const result = await add(2, 3)
|
|
20
|
-
|
|
21
|
-
if (result !== 5) {
|
|
22
|
-
throw new Error('Unexpected result returned by addition worker: ' + result)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return 'test succeeded'
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export default test
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
isWorkerRuntime, Pool, spawn, Worker,
|
|
5
|
-
} from '../../src/index'
|
|
6
|
-
|
|
7
|
-
type AdditionWorker = (a: number, b: number) => number
|
|
8
|
-
type HelloWorker = (text: string) => string
|
|
9
|
-
|
|
10
|
-
async function test() {
|
|
11
|
-
const pool = Pool(() => spawn<HelloWorker>(new Worker('./pool-worker')))
|
|
12
|
-
const results = await Promise.all([
|
|
13
|
-
pool.queue(hello => hello('World')),
|
|
14
|
-
pool.queue(hello => hello('World')),
|
|
15
|
-
pool.queue(hello => hello('World')),
|
|
16
|
-
pool.queue(hello => hello('World')),
|
|
17
|
-
])
|
|
18
|
-
await pool.terminate()
|
|
19
|
-
|
|
20
|
-
for (const result of results) {
|
|
21
|
-
if (result !== 'Hello, World') {
|
|
22
|
-
throw new Error('Unexpected result returned by pool worker: ' + result)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async function test2() {
|
|
28
|
-
// We also want to test if referencing multiple different workers in a module
|
|
29
|
-
// built using webpack works
|
|
30
|
-
|
|
31
|
-
const add = await spawn<AdditionWorker>(new Worker('./addition-worker'))
|
|
32
|
-
const result = await add(2, 3)
|
|
33
|
-
|
|
34
|
-
if (result !== 5) {
|
|
35
|
-
throw new Error('Unexpected result returned by addition worker: ' + result)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async function test3() {
|
|
40
|
-
if (!(process as any).browser) {
|
|
41
|
-
// Running workers from remote URLs is disabled in node.js
|
|
42
|
-
return
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const hello = await spawn<HelloWorker>(new Worker('https://infallible-turing-115958.netlify.com/hello-worker.js'))
|
|
46
|
-
const result = await hello('World')
|
|
47
|
-
|
|
48
|
-
if (result !== 'Hello, World') {
|
|
49
|
-
throw new Error('Unexpected result returned by hello worker: ' + result)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function test4() {
|
|
54
|
-
if (isWorkerRuntime() !== false) {
|
|
55
|
-
throw new Error('Expected isWorkerRuntime() to return false. Got: ' + isWorkerRuntime())
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const testAll = () => Promise.all([test(), test2(), test3(), test4()])
|
|
60
|
-
|
|
61
|
-
export default testAll
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
2
|
-
// NOTE:
|
|
3
|
-
// We are gonna test the bundles previously built by the AVA tests (see webpack.test.ts)
|
|
4
|
-
|
|
5
|
-
describe('threads webpack browser bundle', function () {
|
|
6
|
-
this.timeout(80_000)
|
|
7
|
-
|
|
8
|
-
it('works fine', async function () {
|
|
9
|
-
const bundle = require('./dist/app.web/main')
|
|
10
|
-
await bundle.test()
|
|
11
|
-
})
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
describe('threads webpack browser bundle with inlined worker', function () {
|
|
15
|
-
this.timeout(80_000)
|
|
16
|
-
|
|
17
|
-
it('works fine', async function () {
|
|
18
|
-
const bundle = require('./dist/app-inlined.web/main')
|
|
19
|
-
await bundle.test()
|
|
20
|
-
})
|
|
21
|
-
})
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
2
|
-
const path = require('node:path')
|
|
3
|
-
const ThreadsPlugin = require('threads-plugin')
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
context: __dirname,
|
|
7
|
-
devtool: false,
|
|
8
|
-
entry: require.resolve('./app.ts'),
|
|
9
|
-
externals: { 'tiny-worker': 'tiny-worker' },
|
|
10
|
-
mode: 'development',
|
|
11
|
-
module: {
|
|
12
|
-
rules: [
|
|
13
|
-
{
|
|
14
|
-
loader: 'ts-loader',
|
|
15
|
-
options: { compilerOptions: { module: 'esnext' } },
|
|
16
|
-
test: /\.ts$/,
|
|
17
|
-
},
|
|
18
|
-
],
|
|
19
|
-
},
|
|
20
|
-
output: {
|
|
21
|
-
library: 'test',
|
|
22
|
-
libraryExport: 'default',
|
|
23
|
-
libraryTarget: 'commonjs',
|
|
24
|
-
path: path.resolve(__dirname, './dist/app.node'),
|
|
25
|
-
},
|
|
26
|
-
plugins: [new ThreadsPlugin()],
|
|
27
|
-
resolve: { extensions: ['.js', '.ts'] },
|
|
28
|
-
target: 'node',
|
|
29
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
2
|
-
const path = require('node:path')
|
|
3
|
-
const ThreadsPlugin = require('threads-plugin')
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
context: __dirname,
|
|
7
|
-
devtool: false,
|
|
8
|
-
entry: require.resolve('./app.ts'),
|
|
9
|
-
mode: 'development',
|
|
10
|
-
module: {
|
|
11
|
-
rules: [
|
|
12
|
-
{
|
|
13
|
-
loader: 'ts-loader',
|
|
14
|
-
options: { compilerOptions: { module: 'esnext' } },
|
|
15
|
-
test: /\.ts$/,
|
|
16
|
-
},
|
|
17
|
-
],
|
|
18
|
-
},
|
|
19
|
-
output: {
|
|
20
|
-
library: 'test',
|
|
21
|
-
libraryExport: 'default',
|
|
22
|
-
libraryTarget: 'commonjs',
|
|
23
|
-
path: path.resolve(__dirname, './dist/app.web'),
|
|
24
|
-
},
|
|
25
|
-
plugins: [new ThreadsPlugin()],
|
|
26
|
-
resolve: { extensions: ['.js', '.ts'] },
|
|
27
|
-
target: 'web',
|
|
28
|
-
}
|
package/types/webworker.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
/// <reference no-default-lib="true"/>
|
|
3
|
-
/// <reference lib="webworker" />
|
|
4
|
-
|
|
5
|
-
interface WorkerGlobalScope {
|
|
6
|
-
addEventListener(eventName: string, listener: (event: Event) => void): void
|
|
7
|
-
postMessage(message: any, transferables?: any[]): void
|
|
8
|
-
removeEventListener(eventName: string, listener: (event: Event) => void): void
|
|
9
|
-
}
|
package/worker.d.ts
DELETED
package/worker.js
DELETED
package/worker.mjs
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import * as WorkerContext from './dist/worker/index.js'
|
|
2
|
-
|
|
3
|
-
export const expose = WorkerContext.expose
|
|
4
|
-
export const registerSerializer = WorkerContext.registerSerializer
|
|
5
|
-
export const Transfer = WorkerContext.Transfer
|
|
6
|
-
export const isWorkerRuntime = WorkerContext.isWorkerRuntime
|