@chatbridge/runtime 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/auth-store.d.ts +20 -0
- package/dist/auth-store.js +35 -0
- package/dist/browser-runtime.d.ts +20 -0
- package/dist/browser-runtime.js +38 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/provider-name.d.ts +3 -0
- package/dist/provider-name.js +8 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 7milch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface AuthStoreOptions {
|
|
2
|
+
/** Directory name under the base dir, e.g. "chatbridge" or "company-ai". */
|
|
3
|
+
configDir: string;
|
|
4
|
+
/** Provider name; becomes the state file name. */
|
|
5
|
+
providerName: string;
|
|
6
|
+
/** Base directory; defaults to ~/.config. Overridable for tests. */
|
|
7
|
+
baseDir?: string;
|
|
8
|
+
}
|
|
9
|
+
/** Persists Playwright storageState JSON with restrictive permissions.
|
|
10
|
+
* The content is sensitive (session cookies): never log it. */
|
|
11
|
+
export declare class AuthStore {
|
|
12
|
+
private readonly dir;
|
|
13
|
+
private readonly file;
|
|
14
|
+
constructor(opts: AuthStoreOptions);
|
|
15
|
+
path(): string;
|
|
16
|
+
has(): boolean;
|
|
17
|
+
save(state: unknown): Promise<void>;
|
|
18
|
+
load(): Promise<unknown>;
|
|
19
|
+
clear(): Promise<void>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { validateProviderName } from "./provider-name.js";
|
|
6
|
+
/** Persists Playwright storageState JSON with restrictive permissions.
|
|
7
|
+
* The content is sensitive (session cookies): never log it. */
|
|
8
|
+
export class AuthStore {
|
|
9
|
+
dir;
|
|
10
|
+
file;
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
validateProviderName(opts.providerName);
|
|
13
|
+
const base = opts.baseDir ?? join(homedir(), ".config");
|
|
14
|
+
this.dir = join(base, opts.configDir, "auth");
|
|
15
|
+
this.file = join(this.dir, `${opts.providerName}.json`);
|
|
16
|
+
}
|
|
17
|
+
path() {
|
|
18
|
+
return this.file;
|
|
19
|
+
}
|
|
20
|
+
has() {
|
|
21
|
+
return existsSync(this.file);
|
|
22
|
+
}
|
|
23
|
+
async save(state) {
|
|
24
|
+
await mkdir(this.dir, { recursive: true, mode: 0o700 });
|
|
25
|
+
await chmod(this.dir, 0o700); // mkdir mode is masked by umask; enforce
|
|
26
|
+
await writeFile(this.file, JSON.stringify(state), { mode: 0o600 });
|
|
27
|
+
await chmod(this.file, 0o600);
|
|
28
|
+
}
|
|
29
|
+
async load() {
|
|
30
|
+
return JSON.parse(await readFile(this.file, "utf8"));
|
|
31
|
+
}
|
|
32
|
+
async clear() {
|
|
33
|
+
await rm(this.file, { force: true });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Provider } from "@chatbridge/provider";
|
|
2
|
+
import { type Page } from "playwright";
|
|
3
|
+
import type { AuthStore } from "./auth-store.js";
|
|
4
|
+
export interface LaunchOptions {
|
|
5
|
+
headless: boolean;
|
|
6
|
+
provider: Provider;
|
|
7
|
+
authStore: AuthStore;
|
|
8
|
+
}
|
|
9
|
+
/** Owns the Playwright lifecycle: browser, context (with restored auth
|
|
10
|
+
* state), and a single page handed to Provider methods. */
|
|
11
|
+
export declare class BrowserRuntime {
|
|
12
|
+
private readonly browser;
|
|
13
|
+
private readonly context;
|
|
14
|
+
readonly page: Page;
|
|
15
|
+
private readonly authStore;
|
|
16
|
+
private constructor();
|
|
17
|
+
static launch(opts: LaunchOptions): Promise<BrowserRuntime>;
|
|
18
|
+
saveAuthState(): Promise<void>;
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { chromium, } from "playwright";
|
|
2
|
+
/** Owns the Playwright lifecycle: browser, context (with restored auth
|
|
3
|
+
* state), and a single page handed to Provider methods. */
|
|
4
|
+
export class BrowserRuntime {
|
|
5
|
+
browser;
|
|
6
|
+
context;
|
|
7
|
+
page;
|
|
8
|
+
authStore;
|
|
9
|
+
constructor(browser, context, page, authStore) {
|
|
10
|
+
this.browser = browser;
|
|
11
|
+
this.context = context;
|
|
12
|
+
this.page = page;
|
|
13
|
+
this.authStore = authStore;
|
|
14
|
+
}
|
|
15
|
+
static async launch(opts) {
|
|
16
|
+
const browser = await chromium.launch({ headless: opts.headless });
|
|
17
|
+
const storageState = opts.authStore.has()
|
|
18
|
+
? // Playwright accepts a file path for storageState.
|
|
19
|
+
opts.authStore.path()
|
|
20
|
+
: undefined;
|
|
21
|
+
try {
|
|
22
|
+
const context = await browser.newContext({ storageState });
|
|
23
|
+
const page = await context.newPage();
|
|
24
|
+
return new BrowserRuntime(browser, context, page, opts.authStore);
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
// Never leak a launched browser process when context/page setup fails.
|
|
28
|
+
await browser.close();
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async saveAuthState() {
|
|
33
|
+
await this.authStore.save(await this.context.storageState());
|
|
34
|
+
}
|
|
35
|
+
async close() {
|
|
36
|
+
await this.browser.close();
|
|
37
|
+
}
|
|
38
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const PROVIDER_NAME = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
2
|
+
/** Provider names become auth-state file names, so they must be plain,
|
|
3
|
+
* lowercase, and free of path separators. Rejected, never rewritten. */
|
|
4
|
+
export function validateProviderName(name) {
|
|
5
|
+
if (!PROVIDER_NAME.test(name)) {
|
|
6
|
+
throw new RangeError(`Invalid provider name ${JSON.stringify(name)}: must match ${PROVIDER_NAME}`);
|
|
7
|
+
}
|
|
8
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@chatbridge/runtime",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Playwright runtime for chatbridge: browser lifecycle and auth-state persistence",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/7milch/chatbridge-cli.git",
|
|
9
|
+
"directory": "packages/runtime"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["chatbridge", "playwright", "chat"],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"playwright": "1.63.0",
|
|
28
|
+
"@chatbridge/provider": "0.1.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@chatbridge/example-dummy-chat": "0.0.0"
|
|
32
|
+
}
|
|
33
|
+
}
|