portpreview 0.1.0 → 0.2.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.
@@ -0,0 +1,81 @@
1
+ import { mkdirSync, readFileSync, writeFileSync, chmodSync, unlinkSync, existsSync } from 'node:fs'
2
+ import os from 'node:os'
3
+ import path from 'node:path'
4
+
5
+ const TOKEN_PREFIX = 'pp_live_'
6
+
7
+ /**
8
+ * @returns {string}
9
+ */
10
+ export function defaultConfigPath() {
11
+ if (process.env.PORTPREVIEW_CONFIG?.trim()) {
12
+ return process.env.PORTPREVIEW_CONFIG.trim()
13
+ }
14
+
15
+ const home = os.homedir()
16
+
17
+ if (process.platform === 'win32') {
18
+ const localAppData = process.env.LOCALAPPDATA ?? path.join(home, 'AppData', 'Local')
19
+ return path.join(localAppData, 'portpreview', 'config.json')
20
+ }
21
+
22
+ if (process.platform === 'darwin') {
23
+ return path.join(home, 'Library', 'Application Support', 'portpreview', 'config.json')
24
+ }
25
+
26
+ return path.join(home, '.config', 'portpreview', 'config.json')
27
+ }
28
+
29
+ /**
30
+ * @param {string} token
31
+ */
32
+ export function assertValidToken(token) {
33
+ const value = token.trim()
34
+ if (!value.startsWith(TOKEN_PREFIX) || value.length < 40 || value.length > 100) {
35
+ throw new Error('Invalid token. Copy the CLI token from your PortPreview dashboard.')
36
+ }
37
+ }
38
+
39
+ /**
40
+ * @returns {{ token?: string }}
41
+ */
42
+ export function readUserConfig() {
43
+ const configPath = defaultConfigPath()
44
+ if (!existsSync(configPath)) return {}
45
+
46
+ try {
47
+ const parsed = JSON.parse(readFileSync(configPath, 'utf8'))
48
+ if (typeof parsed?.token !== 'string') return {}
49
+ const token = parsed.token.trim()
50
+ return token ? { token } : {}
51
+ } catch {
52
+ return {}
53
+ }
54
+ }
55
+
56
+ /**
57
+ * @returns {string | undefined}
58
+ */
59
+ export function readStoredToken() {
60
+ return readUserConfig().token
61
+ }
62
+
63
+ /**
64
+ * @param {string} token
65
+ */
66
+ export function saveToken(token) {
67
+ assertValidToken(token)
68
+ const configPath = defaultConfigPath()
69
+ mkdirSync(path.dirname(configPath), { recursive: true, mode: 0o700 })
70
+
71
+ const payload = `${JSON.stringify({ token: token.trim() }, null, 2)}\n`
72
+ writeFileSync(configPath, payload, { encoding: 'utf8', mode: 0o600 })
73
+ chmodSync(configPath, 0o600)
74
+ }
75
+
76
+ export function clearStoredToken() {
77
+ const configPath = defaultConfigPath()
78
+ if (!existsSync(configPath)) return false
79
+ unlinkSync(configPath)
80
+ return true
81
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "portpreview",
3
- "version": "0.1.0",
4
- "description": "Expose a local port through a public HTTPS URL — webhook debugging, OAuth callbacks, and dev previews.",
3
+ "version": "0.2.0",
4
+ "description": "Instant secure previews for localhost apps — expose a local port through a public HTTPS URL.",
5
5
  "type": "module",
6
6
  "main": "./lib/index.mjs",
7
7
  "exports": {
@@ -10,20 +10,28 @@
10
10
  "./package.json": "./package.json"
11
11
  },
12
12
  "bin": {
13
- "portpreview": "bin/portpreview.mjs"
13
+ "portpreview": "./bin/portpreview.mjs"
14
14
  },
15
15
  "files": [
16
16
  "bin/portpreview.mjs",
17
- "lib",
18
- "README.md",
19
- "LICENSE"
17
+ "lib/cli-main.mjs",
18
+ "lib/client-info.mjs",
19
+ "lib/config-cli.mjs",
20
+ "lib/config.mjs",
21
+ "lib/geo.mjs",
22
+ "lib/http-stream.mjs",
23
+ "lib/index.mjs",
24
+ "lib/protocol.mjs",
25
+ "lib/tunnel-client.mjs",
26
+ "lib/user-config.mjs",
27
+ "LICENSE",
28
+ "README.md"
20
29
  ],
21
- "publishConfig": {
22
- "access": "public",
23
- "registry": "https://registry.npmjs.org/"
24
- },
25
30
  "scripts": {
26
31
  "prepublishOnly": "npm test",
32
+ "start": "node bin/portpreview.mjs",
33
+ "gateway": "node bin/gateway.mjs",
34
+ "telegram-daemon": "node bin/telegram-daemon.mjs",
27
35
  "test": "node --test test/*.test.mjs"
28
36
  },
29
37
  "keywords": [
@@ -32,9 +40,7 @@
32
40
  "preview",
33
41
  "webhook",
34
42
  "devtools",
35
- "cli",
36
- "ngrok",
37
- "localtunnel"
43
+ "cli"
38
44
  ],
39
45
  "engines": {
40
46
  "node": ">=18"
@@ -49,6 +55,7 @@
49
55
  "url": "https://github.com/elemops/portpreview/issues"
50
56
  },
51
57
  "dependencies": {
58
+ "node-telegram-bot-api": "^1.2.0",
52
59
  "ws": "^8.19.0"
53
60
  }
54
61
  }