Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin): prevent NPE on writable stream #102

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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