Skip to content

Commit

Permalink
fix(plugin): prevent NPE on writable stream
Browse files Browse the repository at this point in the history
Function has been deprecated. Use the `install` function instead as follows:
```diff
setupNodeEvents(on) {
  install(on);
-  // bind to the event we care about
-  on('before:browser:launch', (browser = {}, launchOptions) => {
-    ensureBrowserFlags(browser, launchOptions);
-    return launchOptions;
-  });
}
```
In case of any issues please refer to cypress-io/cypress#5240
  • Loading branch information
derevnjuk committed Nov 9, 2022
1 parent 068871c commit adf22f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
29 changes: 6 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,25 @@ npm i --save-dev @neuralegion/cypress-har-generator
Next, go to the cypress's directory and put this code is in your `cypress/plugins/index.js` file:

```js
const {
install,
ensureBrowserFlags
} = require('@neuralegion/cypress-har-generator');
const { install } = require('@neuralegion/cypress-har-generator');

module.exports = (on, config) => {
install(on, config);

on('before:browser:launch', (browser = {}, launchOptions) => {
ensureBrowserFlags(browser, launchOptions);
return launchOptions;
});
module.exports = on => {
install(on);
};
```

> The plugins file is no longer supported as of Cypress version 10.0.0. Instead, you have to update your `cypress.config.js` as follows (for details see [the migration guide](https://docs.cypress.io/guides/references/migration-guide#Plugins-File-Removed)):
>
> ```js
> const { defineConfig } = require('cypress');
> const {
> install,
> ensureBrowserFlags
> } = require('@neuralegion/cypress-har-generator');
> const { install } = require('@neuralegion/cypress-har-generator');
>
> module.exports = defineConfig({
> // setupNodeEvents can be defined in either
> // the e2e or component configuration
> e2e: {
> setupNodeEvents(on, config) {
> install(on, config);
>
> on('before:browser:launch', (browser = {}, launchOptions) => {
> ensureBrowserFlags(browser, launchOptions);
>
> return launchOptions;
> });
> setupNodeEvents(on) {
> install(on);
> }
> }
> });
Expand Down
12 changes: 10 additions & 2 deletions src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ export class Plugin {
await this.networkObservable?.unsubscribe();
delete this.networkObservable;

if (this.buffer) {
this.buffer.end();
}

if (this.tmpPath) {
await this.fileManager.removeFile(this.tmpPath);
delete this.buffer;
}

delete this.buffer;
}

private async buildHar(): Promise<string | undefined> {
Expand Down Expand Up @@ -146,7 +151,10 @@ export class Plugin {
return this.networkObservable.subscribe(async (request: NetworkRequest) => {
const entry = await new EntryBuilder(request).build();
const entryStr = JSON.stringify(entry);
this.buffer.write(`${entryStr}${EOL}`);
// @ts-expect-error type mismatch
if (this.buffer && !this.buffer.closed) {
this.buffer.write(`${entryStr}${EOL}`);
}
});
}

Expand Down
26 changes: 26 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,34 @@ export const install = (on: Cypress.PluginEvents): void => {
recordHar: (options: RecordOptions): Promise<void> =>
plugin.recordHar(options)
});

on(
'before:browser:launch',
(
browser: Cypress.Browser | null,
launchOptions: Cypress.BrowserLaunchOptions
) => {
ensureBrowserFlags((browser ?? {}) as Cypress.Browser, launchOptions);

return launchOptions;
}
);
};

/**
* Function has been deprecated. Use {@link install} instead as follows:
* ```diff
* setupNodeEvents(on) {
* install(on);
* - // bind to the event we care about
* - on('before:browser:launch', (browser = {}, launchOptions) => {
* - ensureBrowserFlags(browser, launchOptions);
* - return launchOptions;
* - });
* }
* ```
* In case of any issues please refer to {@link https://github.com/cypress-io/cypress/issues/5240}
*/
export const ensureBrowserFlags = (
browser: Cypress.Browser,
launchOptions: Cypress.BrowserLaunchOptions
Expand Down

0 comments on commit adf22f5

Please sign in to comment.