Skip to content

Commit da19076

Browse files
committed
fix: extract and apply error properties
1 parent 581c908 commit da19076

10 files changed

Lines changed: 5204 additions & 4245 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'synckit': patch
3+
---
4+
5+
fix: extract and apply error properties

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616
runs-on: ubuntu-latest
1717
steps:
1818
- name: Checkout Repo
19-
uses: actions/checkout@master
19+
uses: actions/checkout@v3
2020

2121
- name: Setup Node.js ${{ matrix.node }}
22-
uses: actions/setup-node@master
22+
uses: actions/setup-node@v3
2323
with:
2424
node-version: ${{ matrix.node }}
2525
cache: yarn
@@ -28,7 +28,7 @@ jobs:
2828
run: echo "$(yarn global bin)" >> $GITHUB_PATH
2929

3030
- name: Install Dependencies
31-
run: yarn --frozen-lockfile
31+
run: yarn --frozen-lockfile --ignore-engines
3232

3333
- name: Build, Lint and test
3434
run: |

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ jobs:
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout Repo
14-
uses: actions/checkout@v2
14+
uses: actions/checkout@v3
1515
with:
1616
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
1717
fetch-depth: 0
1818

19-
- name: Setup Node.js 14.x
20-
uses: actions/setup-node@v2
19+
- name: Setup Node.js 16.x
20+
uses: actions/setup-node@v3
2121
with:
22-
node-version: 14.x
22+
node-version: 16.x
2323
cache: yarn
2424

2525
- name: Install Dependencies

.remarkrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"plugins": [
3-
"@1stg/remark-config"
3+
"@1stg/config"
44
]
55
}

benchmarks/benchmark.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const kebabCase = text =>
4242

4343
class Benchmark {
4444
/**
45-
* @param { Object.<string, PerfResult> } perfResults
45+
* @param { Record<string, PerfResult> } perfResults
4646
*/
4747
constructor(perfResults) {
4848
const keys = Object.keys(perfResults)

benchmarks/benchmark.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { fileURLToPath } from 'url'
55

66
const RUN_TIMES = +process.env.RUN_TIMES || 1000
77

8-
// @ts-expect-error -- no idea
98
const __filename = fileURLToPath(import.meta.url)
109

1110
/**
@@ -16,9 +15,9 @@ const __filename = fileURLToPath(import.meta.url)
1615
const perfCase = async name => {
1716
const loadStartTime = performance.now()
1817

19-
const syncFn = (
20-
await import(`./${name}.${name === 'synckit' ? 'js' : 'cjs'}`)
21-
).default
18+
const syncFn =
19+
// eslint-disable-next-line unicorn/no-await-expression-member
20+
(await import(`./${name}.${name === 'synckit' ? 'js' : 'cjs'}`)).default
2221

2322
const loadTime = performance.now() - loadStartTime
2423

@@ -48,7 +47,7 @@ const kebabCase = text =>
4847

4948
class Benchmark {
5049
/**
51-
* @param { Object.<string, PerfResult> } perfResults
50+
* @param { Record.<string, PerfResult> } perfResults
5251
*/
5352
constructor(perfResults) {
5453
const keys = Object.keys(perfResults)

package.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
"author": "JounQin <admin@1stg.me>",
88
"license": "MIT",
99
"engines": {
10-
"node": ">=12.3"
10+
"node": ">=12.20"
1111
},
1212
"main": "./lib/index.cjs",
1313
"module": "./lib/index.js",
1414
"exports": {
1515
"import": "./lib/index.js",
16-
"require": "./lib/index.cjs"
16+
"require": "./lib/index.cjs",
17+
"types": "./lib/index.d.ts"
1718
},
18-
"types": "lib",
19+
"types": "./lib/index.d.ts",
1920
"files": [
2021
"lib",
2122
"!*.tsbuildinfo"
@@ -55,23 +56,23 @@
5556
"tslib": "^2.3.1"
5657
},
5758
"devDependencies": {
58-
"@1stg/lib-config": "^4.1.2",
59-
"@changesets/changelog-github": "^0.4.1",
60-
"@changesets/cli": "^2.17.0",
61-
"@types/jest": "^27.0.2",
62-
"@types/node": "^16.11.0",
63-
"@types/uuid": "^8.3.1",
64-
"clean-publish": "^3.4.1",
65-
"deasync": "^0.1.23",
59+
"@1stg/lib-config": "^5.3.0",
60+
"@changesets/changelog-github": "^0.4.4",
61+
"@changesets/cli": "^2.22.0",
62+
"@types/jest": "^27.4.1",
63+
"@types/node": "^17.0.23",
64+
"@types/uuid": "^8.3.4",
65+
"clean-publish": "^4.0.0",
66+
"deasync": "^0.1.26",
6667
"sync-threads": "^1.0.1",
6768
"ts-expect": "^1.3.0",
68-
"ts-jest": "^27.0.6",
69-
"ts-node": "^10.3.0",
70-
"type-coverage": "^2.18.2",
71-
"typescript": "^4.4.4"
69+
"ts-jest": "^27.1.4",
70+
"ts-node": "^10.7.0",
71+
"type-coverage": "^2.21.1",
72+
"typescript": "^4.6.3"
7273
},
7374
"resolutions": {
74-
"prettier": "^2.4.1",
75+
"prettier": "^2.6.1",
7576
"tslib": "^2.3.1"
7677
},
7778
"commitlint": {
@@ -104,7 +105,7 @@
104105
]
105106
},
106107
"typeCoverage": {
107-
"atLeast": 99.46,
108+
"atLeast": 99.5,
108109
"cache": true,
109110
"detail": true,
110111
"ignoreAsAssertion": true,

src/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ export interface SynckitOptions {
4545
execArgv?: string[]
4646
}
4747

48+
// MessagePort doesn't copy the properties of Error objects. We still want
49+
// error objects to have extra properties such as "warnings" so implement the
50+
// property copying manually.
51+
const extractProperties = (object: unknown): Record<string, unknown> => {
52+
const properties: Record<string, unknown> = {}
53+
if (object && typeof object === 'object') {
54+
for (const key in object) {
55+
properties[key] = (object as Record<string, unknown>)[key]
56+
}
57+
}
58+
return properties
59+
}
60+
61+
// MessagePort doesn't copy the properties of Error objects. We still want
62+
// error objects to have extra properties such as "warnings" so implement the
63+
// property copying manually.
64+
const applyProperties = <T>(
65+
object: T,
66+
properties?: Record<string, unknown>,
67+
): T => {
68+
if (!properties) {
69+
return object
70+
}
71+
// eslint-disable-next-line sonar/for-in
72+
for (const key in properties) {
73+
;(object as Record<string, unknown>)[key] = properties[key]
74+
}
75+
return object
76+
}
77+
4878
export function createSyncFn<T extends AnyAsyncFn>(
4979
workerPath: string,
5080
bufferSize?: number,
@@ -135,6 +165,7 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
135165
id: id2,
136166
result,
137167
error,
168+
properties,
138169
} = receiveMessageOnPort(mainPort)!.message as WorkerToMainMessage<R>
139170

140171
/* istanbul ignore if */
@@ -143,7 +174,7 @@ function startWorkerThread<R, T extends AnyAsyncFn<R>>(
143174
}
144175

145176
if (error) {
146-
throw error
177+
throw applyProperties(error, properties)
147178
}
148179

149180
return result!
@@ -177,6 +208,7 @@ export function runAsWorker<
177208
msg = {
178209
id,
179210
error,
211+
properties: extractProperties(error),
180212
}
181213
}
182214
workerPort.postMessage(msg)

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface WorkerData {
3232
export interface DataMessage<T> {
3333
result?: T
3434
error?: unknown
35+
properties?: Record<string, unknown>
3536
}
3637

3738
export interface WorkerToMainMessage<T = unknown> extends DataMessage<T> {

0 commit comments

Comments
 (0)