dtunnel-sdk 1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 DTunnel0
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,144 @@
1
+ # DTunnel SDK
2
+
3
+ SDK JavaScript/TypeScript para consumir a bridge Android (`window.Dt...`) usada no WebView do DTunnel.
4
+
5
+ Inclui:
6
+ - wrappers dos objetos nativos (`sdk.config`, `sdk.main`, `sdk.text`, `sdk.app`, `sdk.android`)
7
+ - eventos nativos com API semantica (`sdk.on('vpnState', ...)`)
8
+ - tipagem completa para TypeScript
9
+ - helper para React em `dtunnel-sdk/react`
10
+
11
+ ## Instalacao (npm)
12
+
13
+ ```bash
14
+ npm install dtunnel-sdk
15
+ ```
16
+
17
+ ## Uso com TypeScript
18
+
19
+ ```ts
20
+ import DTunnelSDK from 'dtunnel-sdk';
21
+
22
+ const sdk = new DTunnelSDK({
23
+ strict: false,
24
+ autoRegisterNativeEvents: true,
25
+ });
26
+
27
+ sdk.on('vpnState', (event) => {
28
+ console.log(event.payload);
29
+ });
30
+ ```
31
+
32
+ ## Uso com React + TypeScript
33
+
34
+ ```tsx
35
+ import { DTunnelSDKProvider, useDTunnelEvent } from 'dtunnel-sdk/react';
36
+
37
+ function VpnStateLabel() {
38
+ useDTunnelEvent('vpnState', (event) => {
39
+ console.log('VPN:', event.payload);
40
+ });
41
+
42
+ return <span>Escutando eventos...</span>;
43
+ }
44
+
45
+ export function App() {
46
+ return (
47
+ <DTunnelSDKProvider options={{ strict: false }}>
48
+ <VpnStateLabel />
49
+ </DTunnelSDKProvider>
50
+ );
51
+ }
52
+ ```
53
+
54
+ ## Uso via script (CDN)
55
+
56
+ ```html
57
+ <script src="https://cdn.jsdelivr.net/gh/DTunnel0/DTunnelSDK@main/sdk/dtunnel-sdk.js"></script>
58
+ <script>
59
+ const sdk = new DTunnelSDK({ strict: false, autoRegisterNativeEvents: true });
60
+ </script>
61
+ ```
62
+
63
+ ## Eventos semanticos
64
+
65
+ - `vpnState`
66
+ - `vpnStartedSuccess`
67
+ - `vpnStoppedSuccess`
68
+ - `newLog`
69
+ - `configClick`
70
+ - `checkUserStarted`
71
+ - `checkUserResult`
72
+ - `checkUserError`
73
+ - `messageError`
74
+ - `showSuccessToast`
75
+ - `showErrorToast`
76
+ - `notification`
77
+
78
+ ## Estrutura principal
79
+
80
+ - runtime CJS/global: `sdk/dtunnel-sdk.js`
81
+ - runtime ESM: `sdk/dtunnel-sdk.mjs`
82
+ - tipos TS: `sdk/dtunnel-sdk.d.ts`
83
+ - helpers React: `react/index.js`, `react/index.mjs`, `react/index.d.ts`
84
+ - exemplos completos: `examples/`
85
+
86
+ ## Exemplos completos
87
+
88
+ - CDN (JavaScript puro): `examples/cdn/index.html`
89
+ - TypeScript (Vite): `examples/typescript`
90
+ - React + TypeScript (Vite): `examples/react-typescript`
91
+ - Guia geral: `examples/README.md`
92
+
93
+ ### Regra para WebView
94
+
95
+ Sempre gere/entregue **um unico `index.html`** com todo CSS/JS embutido para carregar no WebView.
96
+
97
+ - CDN: `examples/cdn/index.html` ja atende esse formato.
98
+ - TypeScript e React + TypeScript: rode `npm run build:webview` no exemplo para gerar `webview/index.html`.
99
+ - Atalho na raiz para gerar ambos: `npm run examples:webview`.
100
+
101
+ Guia rapido:
102
+
103
+ ```bash
104
+ # TypeScript
105
+ cd examples/typescript
106
+ npm install
107
+ npm run dev
108
+ ```
109
+
110
+ ```bash
111
+ # React + TypeScript
112
+ cd examples/react-typescript
113
+ npm install
114
+ npm run dev
115
+ ```
116
+
117
+ ## Publicacao de release
118
+
119
+ Com o repositorio limpo e commitado:
120
+
121
+ ```bash
122
+ npm run release:sdk -- --version 1.1.0
123
+ ```
124
+
125
+ ## Publicacao no npm
126
+
127
+ Scripts de release/publicacao sao cross-platform (Linux, macOS e Windows) via Node.js.
128
+
129
+ ```bash
130
+ npm login
131
+ npm run publish:npm
132
+ ```
133
+
134
+ Opcional (tag diferente de `latest`):
135
+
136
+ ```bash
137
+ npm run publish:npm -- --tag next
138
+ ```
139
+
140
+ Dry-run:
141
+
142
+ ```bash
143
+ npm run publish:npm:dry
144
+ ```
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "dtunnel-sdk",
3
+ "version": "1.1.0",
4
+ "description": "JavaScript/TypeScript SDK for the DTunnel Android WebView bridge.",
5
+ "license": "MIT",
6
+ "type": "commonjs",
7
+ "main": "./sdk/dtunnel-sdk.js",
8
+ "module": "./sdk/dtunnel-sdk.mjs",
9
+ "types": "./sdk/dtunnel-sdk.d.ts",
10
+ "scripts": {
11
+ "examples:webview": "npm --prefix examples/typescript run build:webview && npm --prefix examples/react-typescript run build:webview",
12
+ "publish:npm": "node ./scripts/publish-npm.mjs",
13
+ "publish:npm:dry": "node ./scripts/publish-npm.mjs --dry-run --skip-auth",
14
+ "release:sdk": "node ./scripts/release-sdk.mjs"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./sdk/dtunnel-sdk.d.ts",
19
+ "import": "./sdk/dtunnel-sdk.mjs",
20
+ "require": "./sdk/dtunnel-sdk.js",
21
+ "default": "./sdk/dtunnel-sdk.js"
22
+ },
23
+ "./react": {
24
+ "types": "./react/index.d.ts",
25
+ "import": "./react/index.mjs",
26
+ "require": "./react/index.js",
27
+ "default": "./react/index.js"
28
+ },
29
+ "./sdk/dtunnel-sdk.js": "./sdk/dtunnel-sdk.js",
30
+ "./sdk/dtunnel-sdk.d.ts": "./sdk/dtunnel-sdk.d.ts"
31
+ },
32
+ "files": [
33
+ "sdk",
34
+ "react",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "keywords": [
39
+ "dtunnel",
40
+ "sdk",
41
+ "webview",
42
+ "android",
43
+ "vpn",
44
+ "typescript",
45
+ "react"
46
+ ],
47
+ "peerDependencies": {
48
+ "react": ">=18"
49
+ },
50
+ "peerDependenciesMeta": {
51
+ "react": {
52
+ "optional": true
53
+ }
54
+ },
55
+ "engines": {
56
+ "node": ">=16"
57
+ },
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "git+https://github.com/DTunnel0/DTunnelSDK.git"
61
+ }
62
+ }
@@ -0,0 +1,64 @@
1
+ import type { Context, ReactElement, ReactNode } from 'react';
2
+ import type {
3
+ DTunnelAnyNativeEventEnvelope,
4
+ DTunnelAnySemanticEventEnvelope,
5
+ DTunnelCallbackName,
6
+ DTunnelErrorEvent,
7
+ DTunnelNativeEventByCallback,
8
+ DTunnelSDK,
9
+ DTunnelSDKOptions,
10
+ DTunnelSemanticEventEnvelope,
11
+ DTunnelSemanticEventName,
12
+ } from '../sdk/dtunnel-sdk';
13
+
14
+ export interface DTunnelSDKProviderProps {
15
+ children?: ReactNode;
16
+ sdk?: DTunnelSDK;
17
+ options?: DTunnelSDKOptions;
18
+ }
19
+
20
+ export declare const DTunnelSDKContext: Context<DTunnelSDK | null>;
21
+
22
+ export declare function DTunnelSDKProvider(
23
+ props: DTunnelSDKProviderProps,
24
+ ): ReactElement;
25
+
26
+ export declare function useDTunnelSDK(): DTunnelSDK;
27
+
28
+ export declare function useDTunnelEvent<E extends DTunnelSemanticEventName>(
29
+ eventName: E,
30
+ listener: (event: DTunnelSemanticEventEnvelope<E>) => void,
31
+ ): void;
32
+
33
+ export declare function useDTunnelEvent(
34
+ eventName: 'nativeEvent',
35
+ listener: (event: DTunnelAnySemanticEventEnvelope) => void,
36
+ ): void;
37
+
38
+ export declare function useDTunnelEvent<E extends DTunnelCallbackName>(
39
+ eventName: `native:${E}`,
40
+ listener: (event: DTunnelNativeEventByCallback<E>) => void,
41
+ ): void;
42
+
43
+ export declare function useDTunnelEvent(
44
+ eventName: `native:${string}`,
45
+ listener: (event: DTunnelAnyNativeEventEnvelope) => void,
46
+ ): void;
47
+
48
+ export declare function useDTunnelEvent(
49
+ eventName: 'error',
50
+ listener: (event: DTunnelErrorEvent) => void,
51
+ ): void;
52
+
53
+ export declare function useDTunnelEvent(
54
+ eventName: string,
55
+ listener: (event: unknown) => void,
56
+ ): void;
57
+
58
+ export declare function useDTunnelNativeEvent(
59
+ listener: (event: DTunnelAnySemanticEventEnvelope) => void,
60
+ ): void;
61
+
62
+ export declare function useDTunnelError(
63
+ listener: (event: DTunnelErrorEvent) => void,
64
+ ): void;
package/react/index.js ADDED
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+
3
+ const React = require('react');
4
+ const sdkModule = require('../sdk/dtunnel-sdk.js');
5
+
6
+ const DTunnelSDK =
7
+ (sdkModule && (sdkModule.DTunnelSDK || sdkModule.default)) ||
8
+ (typeof globalThis !== 'undefined' ? globalThis.DTunnelSDK : undefined);
9
+
10
+ if (typeof DTunnelSDK !== 'function') {
11
+ throw new Error(
12
+ 'DTunnelSDK nao foi encontrado. Importe "dtunnel-sdk" antes de usar "dtunnel-sdk/react".',
13
+ );
14
+ }
15
+
16
+ const DTunnelSDKContext = React.createContext(null);
17
+
18
+ function DTunnelSDKProvider(props) {
19
+ const sdk = props.sdk || null;
20
+ const options = props.options || {};
21
+ const children = props.children;
22
+
23
+ const createdSdkRef = React.useRef(null);
24
+
25
+ if (!sdk && !createdSdkRef.current) {
26
+ createdSdkRef.current = new DTunnelSDK(options);
27
+ }
28
+
29
+ const value = sdk || createdSdkRef.current;
30
+
31
+ React.useEffect(() => {
32
+ if (sdk) return undefined;
33
+
34
+ return () => {
35
+ if (
36
+ createdSdkRef.current &&
37
+ typeof createdSdkRef.current.destroy === 'function'
38
+ ) {
39
+ createdSdkRef.current.destroy();
40
+ }
41
+ };
42
+ }, [sdk]);
43
+
44
+ return React.createElement(DTunnelSDKContext.Provider, { value }, children);
45
+ }
46
+
47
+ function useDTunnelSDK() {
48
+ const sdk = React.useContext(DTunnelSDKContext);
49
+ if (!sdk) {
50
+ throw new Error('useDTunnelSDK precisa estar dentro de <DTunnelSDKProvider>.');
51
+ }
52
+ return sdk;
53
+ }
54
+
55
+ function useDTunnelEvent(eventName, listener) {
56
+ const sdk = useDTunnelSDK();
57
+ const listenerRef = React.useRef(listener);
58
+
59
+ React.useEffect(() => {
60
+ listenerRef.current = listener;
61
+ }, [listener]);
62
+
63
+ React.useEffect(() => {
64
+ const unsubscribe = sdk.on(eventName, (event) => {
65
+ listenerRef.current(event);
66
+ });
67
+ return unsubscribe;
68
+ }, [sdk, eventName]);
69
+ }
70
+
71
+ function useDTunnelNativeEvent(listener) {
72
+ useDTunnelEvent('nativeEvent', listener);
73
+ }
74
+
75
+ function useDTunnelError(listener) {
76
+ useDTunnelEvent('error', listener);
77
+ }
78
+
79
+ module.exports = {
80
+ DTunnelSDKContext,
81
+ DTunnelSDKProvider,
82
+ useDTunnelSDK,
83
+ useDTunnelEvent,
84
+ useDTunnelNativeEvent,
85
+ useDTunnelError,
86
+ };
@@ -0,0 +1,80 @@
1
+ import React, { createContext, useContext, useEffect, useRef } from 'react';
2
+ import { DTunnelSDK } from '../sdk/dtunnel-sdk.mjs';
3
+
4
+ if (typeof DTunnelSDK !== 'function') {
5
+ throw new Error(
6
+ 'DTunnelSDK nao foi encontrado. Importe "dtunnel-sdk" antes de usar "dtunnel-sdk/react".',
7
+ );
8
+ }
9
+
10
+ const DTunnelSDKContext = createContext(null);
11
+
12
+ function DTunnelSDKProvider(props) {
13
+ const sdk = props.sdk ?? null;
14
+ const options = props.options ?? {};
15
+ const children = props.children;
16
+
17
+ const createdSdkRef = useRef(null);
18
+
19
+ if (!sdk && !createdSdkRef.current) {
20
+ createdSdkRef.current = new DTunnelSDK(options);
21
+ }
22
+
23
+ const value = sdk ?? createdSdkRef.current;
24
+
25
+ useEffect(() => {
26
+ if (sdk) return undefined;
27
+
28
+ return () => {
29
+ if (
30
+ createdSdkRef.current &&
31
+ typeof createdSdkRef.current.destroy === 'function'
32
+ ) {
33
+ createdSdkRef.current.destroy();
34
+ }
35
+ };
36
+ }, [sdk]);
37
+
38
+ return React.createElement(DTunnelSDKContext.Provider, { value }, children);
39
+ }
40
+
41
+ function useDTunnelSDK() {
42
+ const sdk = useContext(DTunnelSDKContext);
43
+ if (!sdk) {
44
+ throw new Error('useDTunnelSDK precisa estar dentro de <DTunnelSDKProvider>.');
45
+ }
46
+ return sdk;
47
+ }
48
+
49
+ function useDTunnelEvent(eventName, listener) {
50
+ const sdk = useDTunnelSDK();
51
+ const listenerRef = useRef(listener);
52
+
53
+ useEffect(() => {
54
+ listenerRef.current = listener;
55
+ }, [listener]);
56
+
57
+ useEffect(() => {
58
+ const unsubscribe = sdk.on(eventName, (event) => {
59
+ listenerRef.current(event);
60
+ });
61
+ return unsubscribe;
62
+ }, [sdk, eventName]);
63
+ }
64
+
65
+ function useDTunnelNativeEvent(listener) {
66
+ useDTunnelEvent('nativeEvent', listener);
67
+ }
68
+
69
+ function useDTunnelError(listener) {
70
+ useDTunnelEvent('error', listener);
71
+ }
72
+
73
+ export {
74
+ DTunnelSDKContext,
75
+ DTunnelSDKProvider,
76
+ useDTunnelSDK,
77
+ useDTunnelEvent,
78
+ useDTunnelNativeEvent,
79
+ useDTunnelError,
80
+ };
package/sdk/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # DTunnel SDK (Runtime)
2
+
3
+ Arquivos do runtime e tipagem:
4
+
5
+ - `dtunnel-sdk.js`: build principal (script tag + CommonJS)
6
+ - `dtunnel-sdk.mjs`: entrada ESM para bundlers
7
+ - `dtunnel-sdk.d.ts`: tipagem TypeScript
8
+
9
+ ## Inicializacao
10
+
11
+ ```ts
12
+ import DTunnelSDK from 'dtunnel-sdk';
13
+
14
+ const sdk = new DTunnelSDK({
15
+ strict: false,
16
+ autoRegisterNativeEvents: true,
17
+ });
18
+ ```
19
+
20
+ ## Modulos
21
+
22
+ - `sdk.config`
23
+ - `sdk.main`
24
+ - `sdk.text`
25
+ - `sdk.app`
26
+ - `sdk.android`
27
+
28
+ ## Eventos
29
+
30
+ Use `sdk.on('<evento>', handler)` com:
31
+
32
+ - `vpnState`
33
+ - `vpnStartedSuccess`
34
+ - `vpnStoppedSuccess`
35
+ - `newLog`
36
+ - `configClick`
37
+ - `checkUserStarted`
38
+ - `checkUserResult`
39
+ - `checkUserError`
40
+ - `messageError`
41
+ - `showSuccessToast`
42
+ - `showErrorToast`
43
+ - `notification`
44
+
45
+ ## Erros
46
+
47
+ - `strict: true`: lanca excecao (`DTunnelBridgeError`)
48
+ - `strict: false`: retorna `null` e publica evento `error`
49
+
50
+ ## Exemplos completos
51
+
52
+ - CDN: `examples/cdn/index.html`
53
+ - TypeScript: `examples/typescript`
54
+ - React + TypeScript: `examples/react-typescript`
55
+ - Guia geral: `examples/README.md`
56
+
57
+ Regra para WebView:
58
+ - entregue sempre um unico `index.html` com CSS/JS embutido.
59
+ - nos exemplos TypeScript/React, use `npm run build:webview` para gerar `webview/index.html`.
60
+ - atalho na raiz: `npm run examples:webview`.
61
+
62
+ Publicacao npm (raiz do repo):
63
+ - scripts de publicacao/release sao cross-platform (Linux, macOS e Windows).
64
+ - `npm login`
65
+ - `npm run publish:npm`