Skip to content

Commit

Permalink
⬆️ Update to new NodeJS 16 stream promises API
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinGrote committed Mar 13, 2022
1 parent de4dfcf commit bd55931
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 109 deletions.
45 changes: 8 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"preview": true,
"repository": "https://github.com/pester/vscode-adapter",
"engines": {
"vscode": "^1.60.0"
"vscode": "^1.66.0"
},
"contributes": {
"configuration": {
Expand Down Expand Up @@ -100,11 +100,11 @@
"vscode:prepublish": "npm run -S esbuild-base -- --minify"
},
"devDependencies": {
"@types/byline": "^4.2.33",
"@types/debounce-promise": "^3.1.4",
"@types/glob": "^7.2.0",
"@types/jest": "^27.4.0",
"@types/node": "^16.11.26",
"@types/readline-transform": "^1.0.1",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"@vscode/test-electron": "^2.1.3",
Expand All @@ -124,8 +124,6 @@
"vscode-dts": "^0.3.3"
},
"dependencies": {
"@types/readline-transform": "^1.0.1",
"byline": "^5.0.0",
"debounce-promise": "^3.1.2",
"lookpath": "^1.2.2",
"path": "^0.12.7",
Expand Down
14 changes: 7 additions & 7 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { createStream } from 'byline'
import { PassThrough, pipeline, Writable } from 'stream'
import ReadlineTransform from 'readline-transform'
import { PassThrough, Readable, Transform, Writable } from 'stream'
import { pipeline as pipelineAsPromise } from 'stream/promises'
import { ILogObject, Logger, TTransportLogger } from 'tslog'
import { promisify } from 'util'
import { OutputChannel, window } from 'vscode'

const pipelineAsPromise = promisify(pipeline)

/**
* Writes TSLog Pretty Print messages to the supplied stream
*
Expand All @@ -27,9 +25,11 @@ class PrettyPrintTransport
dateTimePattern: 'hour:minute:second.millisecond'
})

pipelineAsPromise(
// Workaround for https://github.com/nodejs/node/issues/40191
// FIXME: When VScode is based on NodeJS 16.14+
pipelineAsPromise<Readable, Transform, Writable>(
this.prettyLogInput,
createStream(), //Breaks the stream into new lines
new ReadlineTransform(),
outStream
).catch(err => {
throw new Error(err)
Expand Down
33 changes: 23 additions & 10 deletions src/powershell.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { execSync } from 'child_process'
import { finished, pipeline, Readable } from 'stream'
import { promisify } from 'util'
import ReadlineTransform from 'readline-transform'
import { Readable } from 'stream'
import { pipeline } from 'stream/promises'
import { createJsonParseTransform, PowerShell, PSOutput } from './powershell'

const pipelineWithPromise = promisify(pipeline)
const isFinished = promisify(finished)
// jest.setTimeout(30000)

describe('jsonParseTransform', () => {
Expand All @@ -15,28 +14,32 @@ describe('jsonParseTransform', () => {
it('object', async () => {
const source = Readable.from(['{"Test": 5}'])
const jsonPipe = createJsonParseTransform()
await pipelineWithPromise(source, jsonPipe)
await pipeline(source, jsonPipe)
const result = jsonPipe.read()
expect(result).toStrictEqual<TestObject>({ Test: 5 })
})

it('empty', async () => {
const source = Readable.from([''])
const source = Readable.from(['']).pipe(
new ReadlineTransform({ skipEmpty: false })
)
const jsonPipe = createJsonParseTransform()

try {
await pipelineWithPromise(source, jsonPipe)
await pipeline(source, jsonPipe)
} catch (err) {
expect(err.message).toMatch('Unexpected end')
}
})

it('syntaxError', async () => {
const source = Readable.from(['"Test":5}'])
const source = Readable.from(['"Test":5}']).pipe(
new ReadlineTransform({ skipEmpty: false })
)
const jsonPipe = createJsonParseTransform()

try {
await pipelineWithPromise(source, jsonPipe)
await pipeline(source, jsonPipe)
} catch (err) {
expect(err.message).toMatch('Unexpected token')
}
Expand All @@ -51,6 +54,11 @@ describe('run', () => {
afterEach(() => {
ps.dispose()
})
it('finished', async () => {
const streams = new PSOutput()
await ps.run(`'JEST'`, streams)
// This test times out if it doesn't execute successfully
})
it('success', done => {
const streams = new PSOutput()
streams.success.on('data', data => {
Expand Down Expand Up @@ -91,7 +99,9 @@ describe('run', () => {
streams.error.on('data', data => {
expect(data).toBe('oops!')
})

await ps.run(`1..32 | Write-Host;Write-Error 'oops!';'JEST';1..2`, streams)
console.log('done')
})
})

Expand Down Expand Up @@ -129,7 +139,10 @@ describe('exec', () => {
/** If cancelExisting is used, ensure the first is closed quickly */
it('CancelExisting', async () => {
const result = ps.exec(`'Item';sleep 5;'ThisItemShouldNotEmit'`, true)
await new Promise(r => setTimeout(r, 500))
//FIXME: This is a race condition on slower machines that makes this test fail intermittently
// If Item hasn't been emitted yet from the pipeline
// This should instead watch for Item and then cancel existing once received
await new Promise(r => setTimeout(r, 600))
const result2 = ps.exec(`'Item'`, true)
const awaitedResult = await result
const awaitedResult2 = await result2
Expand Down
Loading

0 comments on commit bd55931

Please sign in to comment.