Skip to content

Commit 465f75a

Browse files
authored
feat: add selfsignedOptions option (#8)
1 parent a1dd3e7 commit 465f75a

6 files changed

Lines changed: 78 additions & 5 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ pluginBasicSsl({
6666
});
6767
```
6868

69+
### selfsignedOptions
70+
71+
Options passing to `selfsigned`, see [selfsigned - Options](https://github.com/jfromaniello/selfsigned?tab=readme-ov-file#options) for details.
72+
73+
- **Type:** `SelfsignedOptions`
74+
- **Default:**
75+
76+
```ts
77+
const defaultOptions = {
78+
days: 30,
79+
keySize: 2048,
80+
};
81+
```
82+
83+
- **Example:**
84+
85+
```ts
86+
pluginBasicSsl({
87+
selfsignedOptions: {
88+
days: 100,
89+
},
90+
});
91+
```
92+
6993
## License
7094

7195
[MIT](./LICENSE).

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { RsbuildPlugin } from '@rsbuild/core';
2+
import type { SelfsignedOptions } from 'selfsigned';
23
import { resolveHttpsConfig } from './util.js';
34

45
export const PLUGIN_BASIC_SSL_NAME = 'rsbuild:basic-ssl';
@@ -14,6 +15,10 @@ export type PluginBasicSslOptions = {
1415
* @default __dirname
1516
*/
1617
outputPath?: string;
18+
/**
19+
* Options passing to `selfsigned`.
20+
*/
21+
selfsignedOptions?: SelfsignedOptions;
1722
};
1823

1924
export const pluginBasicSsl = (

src/util.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ export const resolveHttpsConfig = async (
3333
options.filename ?? 'fake-cert.pem',
3434
);
3535

36+
const selfsignedOptions = {
37+
days: 30,
38+
keySize: 2048,
39+
...options.selfsignedOptions,
40+
};
41+
3642
if (fs.existsSync(certPath)) {
3743
const stats = await fs.promises.stat(certPath);
3844
const timeDiff = Date.now() - stats.mtimeMs;
3945
const daysDiff = timeDiff / (1000 * 60 * 60 * 24);
4046

4147
// Default validity period is 30 days
42-
if (daysDiff < 30) {
48+
if (daysDiff < selfsignedOptions.days) {
4349
const content = await fs.promises.readFile(certPath, {
4450
encoding: 'utf-8',
4551
});
@@ -52,10 +58,7 @@ export const resolveHttpsConfig = async (
5258

5359
const pem = selfsigned.generate(
5460
[{ name: 'commonName', value: 'localhost' }],
55-
{
56-
days: 30,
57-
keySize: 2048,
58-
},
61+
selfsignedOptions,
5962
);
6063

6164
const content = pem.private + pem.cert;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { dirname, join } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { expect, test } from '@playwright/test';
4+
import { createRsbuild } from '@rsbuild/core';
5+
import { pluginBasicSsl } from '../../dist';
6+
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
9+
test('should print HTTPS server URLs when custom selfsigned options', async () => {
10+
const rsbuild = await createRsbuild({
11+
cwd: __dirname,
12+
rsbuildConfig: {
13+
plugins: [
14+
pluginBasicSsl({
15+
selfsignedOptions: {
16+
days: 1,
17+
},
18+
}),
19+
],
20+
server: {
21+
port: 3300,
22+
},
23+
},
24+
});
25+
26+
const { server, urls } = await rsbuild.startDevServer();
27+
28+
await new Promise((resolve) => {
29+
rsbuild.onDevCompileDone(resolve);
30+
});
31+
32+
expect(urls.every((url) => url.startsWith('https'))).toBeTruthy();
33+
34+
await server.close();
35+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { pluginBasicSsl } from '@rsbuild/plugin-basic-ssl';
2+
3+
export default {
4+
plugins: [pluginBasicSsl()],
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('1');

0 commit comments

Comments
 (0)