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

feat: web server #152

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,31 @@ packages/**/native-src/android/**/.classpath
packages/**/native-src/ios/**/xcuserdata/
packages/**/native-src/ios/**/project.xcworkspace/
packages/**/native-src/ios/**/build


# src-native
packages/**/src-native/dist

# src-native
packages/**/src-native/**/target

# src-native/android
packages/**/src-native/android/**/*.iml
packages/**/src-native/android/**/.gradle
packages/**/src-native/android/**/local.properties
packages/**/src-native/android/**/.idea/workspace.xml
packages/**/src-native/android/**/.idea/libraries
packages/**/src-native/android/**/.DS_Store
packages/**/src-native/android/**/build
packages/**/src-native/android/**/bin
packages/**/src-native/android/**/captures
packages/**/src-native/android/**/.externalNativeBuild
packages/**/src-native/android/**/.settings
packages/**/src-native/android/**/.project
packages/**/src-native/android/**/.classpath

# src-native/ios
packages/**/src-native/web-server-ios/**/xcuserdata/
packages/**/src-native/web-server-ios/**/project.xcworkspace/
packages/**/src-native/web-server-ios/**/build
packages/**/src-native/web-server-ios/**/dist
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [@nstudio/nativescript-qr](packages/nativescript-qr/README.md)
- [@nstudio/nativescript-tracking-transparency](packages/nativescript-tracking-transparency/README.md)
- [@nstudio/nativescript-walletconnect](packages/nativescript-walletconnect/README.md)
- [@nstudio/nativescript-web-server](packages/nativescript-web-server/README.md)

# How to use?

Expand Down
3 changes: 2 additions & 1 deletion apps/demo-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"@nstudio/nativescript-plaid": "file:../../dist/packages/nativescript-plaid",
"@nstudio/nativescript-qr": "file:../../dist/packages/nativescript-qr",
"@nstudio/nativescript-tracking-transparency": "file:../../dist/packages/nativescript-tracking-transparency",
"@nstudio/nativescript-walletconnect": "file:../../dist/packages/nativescript-walletconnect"
"@nstudio/nativescript-walletconnect": "file:../../dist/packages/nativescript-walletconnect",
"@nstudio/nativescript-web-server": "file:../../dist/packages/nativescript-web-server"
},
"devDependencies": {
"@nativescript/android": "~8.6.0",
Expand Down
3 changes: 2 additions & 1 deletion apps/demo-angular/src/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const routes: Routes = [
{ path: 'nativescript-plaid', loadChildren: () => import('./plugin-demos/nativescript-plaid.module').then(m => m.NativescriptPlaidModule) },
{ path: 'nativescript-qr', loadChildren: () => import('./plugin-demos/nativescript-qr.module').then(m => m.NativescriptQrModule) },
{ path: 'nativescript-tracking-transparency', loadChildren: () => import('./plugin-demos/nativescript-tracking-transparency.module').then(m => m.NativescriptTrackingTransparencyModule) },
{ path: 'nativescript-walletconnect', loadChildren: () => import('./plugin-demos/nativescript-walletconnect.module').then(m => m.NativescriptWalletconnectModule) }
{ path: 'nativescript-walletconnect', loadChildren: () => import('./plugin-demos/nativescript-walletconnect.module').then(m => m.NativescriptWalletconnectModule) },
{ path: 'nativescript-web-server', loadChildren: () => import('./plugin-demos/nativescript-web-server.module').then(m => m.NativescriptWebServerModule) }
];

@NgModule({
Expand Down
3 changes: 3 additions & 0 deletions apps/demo-angular/src/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export class HomeComponent {
},
{
name: 'nativescript-walletconnect'
},
{
name: 'nativescript-web-server'
}
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ActionBar title="nativescript-web-server" class="action-bar"> </ActionBar>
<StackLayout class="p-20">
<ScrollView class="h-full">
<StackLayout>
<Button text="Test nativescript-web-server" (tap)="demoShared.testIt()" class="btn btn-primary"></Button>
</StackLayout>
</ScrollView>
</StackLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component, NgZone } from '@angular/core';
import { DemoSharedNativescriptWebServer } from '@demo/shared';
import { } from '@nstudio/nativescript-web-server';

@Component({
selector: 'demo-nativescript-web-server',
templateUrl: 'nativescript-web-server.component.html',
})
export class NativescriptWebServerComponent {

demoShared: DemoSharedNativescriptWebServer;

constructor(private _ngZone: NgZone) {}

ngOnInit() {
this.demoShared = new DemoSharedNativescriptWebServer();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
import { NativeScriptCommonModule, NativeScriptRouterModule } from '@nativescript/angular';
import { NativescriptWebServerComponent } from './nativescript-web-server.component';

@NgModule({
imports: [NativeScriptCommonModule, NativeScriptRouterModule.forChild([{ path: '', component: NativescriptWebServerComponent }])],
declarations: [NativescriptWebServerComponent],
schemas: [ NO_ERRORS_SCHEMA]
})
export class NativescriptWebServerModule {}
7 changes: 4 additions & 3 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"@nstudio/nativescript-cardview": "file:../../packages/nativescript-cardview",
"@nstudio/nativescript-carousel": "file:../../packages/nativescript-carousel",
"@nstudio/nativescript-checkbox": "file:../../packages/nativescript-checkbox",
"@nstudio/nativescript-dynatrace": "file:../../packages/nativescript-dynatrace",
"@nstudio/nativescript-embrace": "file:../../packages/nativescript-embrace",
"@nstudio/nativescript-dynatrace": "file:../../dist/packages/nativescript-dynatrace",
"@nstudio/nativescript-embrace": "file:../../dist/packages/nativescript-embrace",
"@nstudio/nativescript-exoplayer": "file:../../packages/nativescript-exoplayer",
"@nstudio/nativescript-fancyalert": "file:../../packages/nativescript-fancyalert",
"@nstudio/nativescript-filterable-listpicker": "file:../../packages/nativescript-filterable-listpicker",
Expand All @@ -29,7 +29,8 @@
"@nstudio/nativescript-plaid": "file:../../packages/nativescript-plaid",
"@nstudio/nativescript-qr": "file:../../packages/nativescript-qr",
"@nstudio/nativescript-tracking-transparency": "file:../../packages/nativescript-tracking-transparency",
"@nstudio/nativescript-walletconnect": "file:../../packages/nativescript-walletconnect"
"@nstudio/nativescript-walletconnect": "file:../../packages/nativescript-walletconnect",
"@nstudio/nativescript-web-server": "file:../../packages/nativescript-web-server"
},
"devDependencies": {
"@nativescript/android": "~8.6.0",
Expand Down
1 change: 1 addition & 0 deletions apps/demo/src/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<Button text="nativescript-qr" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="nativescript-tracking-transparency" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="nativescript-walletconnect" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
<Button text="nativescript-web-server" tap="{{ viewDemo }}" class="btn btn-primary view-demo"/>
</StackLayout>
</ScrollView>
</StackLayout>
Expand Down
12 changes: 12 additions & 0 deletions apps/demo/src/plugin-demos/nativescript-web-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Observable, EventData, Page } from '@nativescript/core';
import { DemoSharedNativescriptWebServer } from '@demo/shared';
import { } from '@nstudio/nativescript-web-server';

export function navigatingTo(args: EventData) {
const page = <Page>args.object;
page.bindingContext = new DemoModel();
}

export class DemoModel extends DemoSharedNativescriptWebServer {

}
15 changes: 15 additions & 0 deletions apps/demo/src/plugin-demos/nativescript-web-server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
<Page.actionBar>
<ActionBar title="nativescript-web-server" icon="" class="action-bar">
</ActionBar>
</Page.actionBar>
<StackLayout class="p-20">
<StackLayout>
<Button text="Start nativescript-web-server" tap="{{ start }}" class="btn btn-primary"/>
<Label text="{{ 'Server Status: ' + status }}" class="btn btn-primary"/>
<Button text="Get Status nativescript-web-server" tap="{{ getStatus }}" class="btn btn-primary"/>
<Button text="Stop nativescript-web-server" tap="{{ stop }}" class="btn btn-primary"/>
<WebView loaded="{{ loaded }}" width="100%" height="300" />
</StackLayout>
</StackLayout>
</Page>
12 changes: 12 additions & 0 deletions apps/demo/src/web_assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>
7 changes: 7 additions & 0 deletions apps/demo/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,12 @@ module.exports = (env) => {
context: webpack.Utils.project.getProjectFilePath('node_modules')
});

webpack.Utils.addCopyRule({
from: '../src/web_assets',
to: 'web_assets',
context: webpack.Utils.project.getProjectFilePath('node_modules')
});


return webpack.resolveConfig();
};
18 changes: 18 additions & 0 deletions packages/nativescript-web-server/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*", "node_modules/**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
13 changes: 13 additions & 0 deletions packages/nativescript-web-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @nstudio/nativescript-web-server

```javascript
npm install @nstudio/nativescript-web-server
```

## Usage

// TODO

## License

Apache License Version 2.0
7 changes: 7 additions & 0 deletions packages/nativescript-web-server/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum ServerStatus {
Inactive,
Active,
Crashed,
Starting,
Stopping,
}
59 changes: 59 additions & 0 deletions packages/nativescript-web-server/index.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
declare const io;
import { Observable } from '@nativescript/core';
import { ServerStatus } from './common';

export class Server {
private server;
constructor(config: { logger?: boolean; path: string; directory: string; index?: string; hostName?: string; port: number; workers?: number; showFiles?: boolean }) {
this.server = new io.nstudio.plugins.webserver.Server(config.logger ?? false, config.path, config.directory, config.index ?? null, config.hostName ?? '127.0.0.1', config.port ?? 8080, config.workers ?? 2, config.showFiles ?? false);
}

get status(): ServerStatus {
return this.server.getStatus() as never;
}

setStatusChangeListener(value: (status: ServerStatus) => void) {
this.server.setStatusCallback(
new io.nstudio.plugins.webserver.Server.StatusCallback({
onChange(status: ServerStatus) {
value(status);
},
})
);
}

start() {
return new Promise<void>((resolve, reject) => {
this.server.start(
new io.nstudio.plugins.webserver.Server.Callback({
onSuccess() {
resolve();
},
onError(error: string) {
reject(error);
},
})
);
});
}
stop(wait = false) {
return new Promise<void>((resolve, reject) => {
this.server.stop(
wait,
new io.nstudio.plugins.webserver.Server.Callback({
onSuccess() {
resolve();
},
onError(error: string) {
reject(error);
},
})
);
});
}
}

export class Client {}

export class WebSocketServer extends Observable {
}
37 changes: 37 additions & 0 deletions packages/nativescript-web-server/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Observable } from '@nativescript/core';
export class Server {
private server: NSCServer;
constructor(config: { logger?: boolean; path: string; directory: string; index?: string; hostName?: string; port?: number; workers?: number; showFiles?: boolean });

get status(): ServerStatus;

setStatusChangeListener(value: (status: ServerStatus) => void);

start(): Promise<void>;

stop(wait?: bool): Promise<void>;
}

export class Client {
readonly id: number;
}

export class WebSocketServer extends Observable {
constructor(config: { logger?: boolean; path: string; maxPayload?: number; hostName?: string; port?: number; workers?: number; autoPong?: boolean });

broadcast(data: string | ArrayBuffer | Uint8Array | Uint8ClampedArray);

send(id: number, data: string | ArrayBuffer | Uint8Array | Uint8ClampedArray);

ping(id: number, data?: ArrayBuffer | Uint8Array | Uint8ClampedArray);

pong(id: number, data?: ArrayBuffer | Uint8Array | Uint8ClampedArray);

readonly status: ServerStatus;

setStatusChangeListener(value: (status: ServerStatus) => void);

start(): Promise<void>;

stop(wait?: bool): Promise<void>;
}
Loading