From e7393f19b84e2b3ffdac7b48c7a1442e7cbabaf4 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sat, 2 Apr 2022 22:27:38 +0800 Subject: [PATCH] chore: export extractProperties (#76) --- .changeset/tasty-maps-smoke.md | 5 +++++ package.json | 2 +- src/index.ts | 30 +++++++----------------------- src/types.ts | 2 +- test/fn.spec.ts | 20 ++++++++++++++++++-- 5 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 .changeset/tasty-maps-smoke.md diff --git a/.changeset/tasty-maps-smoke.md b/.changeset/tasty-maps-smoke.md new file mode 100644 index 000000000..39b385a2e --- /dev/null +++ b/.changeset/tasty-maps-smoke.md @@ -0,0 +1,5 @@ +--- +"synckit": patch +--- + +chore: export extractProperties diff --git a/package.json b/package.json index b69c64e8b..db54cc9f2 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,7 @@ ] }, "typeCoverage": { - "atLeast": 99.5, + "atLeast": 99.67, "cache": true, "detail": true, "ignoreAsAssertion": true, diff --git a/src/index.ts b/src/index.ts index cacf64119..279be6465 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,31 +48,14 @@ export interface SynckitOptions { // MessagePort doesn't copy the properties of Error objects. We still want // error objects to have extra properties such as "warnings" so implement the // property copying manually. -const extractProperties = (object: unknown): Record => { - const properties: Record = {} +export const extractProperties = (object?: T): T | undefined => { if (object && typeof object === 'object') { + const properties = {} as T for (const key in object) { - properties[key] = (object as Record)[key] + properties[key as keyof T] = object[key] } + return properties } - return properties -} - -// MessagePort doesn't copy the properties of Error objects. We still want -// error objects to have extra properties such as "warnings" so implement the -// property copying manually. -const applyProperties = ( - object: T, - properties?: Record, -): T => { - if (!properties) { - return object - } - // eslint-disable-next-line sonar/for-in - for (const key in properties) { - ;(object as Record)[key] = properties[key] - } - return object } export function createSyncFn( @@ -166,7 +149,8 @@ function startWorkerThread>( result, error, properties, - } = receiveMessageOnPort(mainPort)!.message as WorkerToMainMessage + } = (receiveMessageOnPort(mainPort) as { message: WorkerToMainMessage }) + .message /* istanbul ignore if */ if (id !== id2) { @@ -174,7 +158,7 @@ function startWorkerThread>( } if (error) { - throw applyProperties(error, properties) + throw Object.assign(error, properties) } return result! diff --git a/src/types.ts b/src/types.ts index ebd3901e2..e2a199b4d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -32,7 +32,7 @@ export interface WorkerData { export interface DataMessage { result?: T error?: unknown - properties?: Record + properties?: unknown } export interface WorkerToMainMessage extends DataMessage { diff --git a/test/fn.spec.ts b/test/fn.spec.ts index 8e6ea7aac..d7143c277 100644 --- a/test/fn.spec.ts +++ b/test/fn.spec.ts @@ -2,12 +2,13 @@ import { createRequire } from 'module' import { jest } from '@jest/globals' -import { createSyncFn } from 'synckit' +import { createSyncFn, extractProperties } from 'synckit' type AsyncWorkerFn = (result: T, timeout?: number) => Promise beforeEach(() => { jest.resetModules() + delete process.env.SYNCKIT_BUFFER_SIZE delete process.env.SYNCKIT_TIMEOUT @@ -45,7 +46,7 @@ test('createSyncFn', () => { expect(syncFn3(2)).toBe(2) expect(syncFn3(5, 0)).toBe(5) - expect(() => errSyncFn()).toThrow('Worker Error') + expect(() => errSyncFn()).toThrowErrorMatchingInlineSnapshot(`"Worker Error"`) const syncFn4 = createSyncFn(workerCjsPath) @@ -66,3 +67,18 @@ test('timeout', async () => { 'Internal error: Atomics.wait() failed: timed-out', ) }) + +test('extractProperties', () => { + expect(extractProperties()).toBeUndefined() + expect(extractProperties({})).toEqual({}) + expect(extractProperties(new Error('message'))).toEqual({}) + expect( + extractProperties( + Object.assign(new Error('message'), { + code: 'CODE', + }), + ), + ).toEqual({ + code: 'CODE', + }) +})