Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
2a15423c1d | |||
d3e854ccc7 | |||
45b0b5d011 | |||
5f370bce10 | |||
32f1eb7c3c | |||
d24352a289 | |||
e6c15b2d81 | |||
4d6ae3ba87 | |||
68ac5e6ec2 | |||
d4401172e6 | |||
1887645625 | |||
9e8b30391a | |||
817a660662 | |||
5a037699b5 | |||
9552cb9de6 |
16
.gitignore
vendored
16
.gitignore
vendored
@ -1,8 +1,20 @@
|
|||||||
.nogit/
|
.nogit/
|
||||||
node_modules/
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
pages/
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
.yarn/
|
.yarn/
|
||||||
dist/
|
|
||||||
.cache/
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_*/
|
||||||
|
|
||||||
|
# custom
|
125
.gitlab-ci.yml
125
.gitlab-ci.yml
@ -1,119 +1,122 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- pnpm install -g pnpm
|
||||||
|
- pnpm install -g @shipzone/npmci
|
||||||
|
- npmci npm prepare
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
# ====================
|
||||||
|
# security stage
|
||||||
|
# ====================
|
||||||
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command pnpm audit --audit-level=high --prod
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
snyk:
|
auditDevDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm install -g snyk
|
- npmci command pnpm audit --audit-level=high --dev
|
||||||
- npmci command npm install --ignore-scripts
|
|
||||||
- npmci command snyk test
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci node install stable
|
||||||
- npmci node install lts
|
- npmci npm install
|
||||||
- npmci npm install
|
- npmci npm test
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
testBuild:
|
||||||
testSTABLE:
|
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci node install stable
|
||||||
- npmci node install stable
|
- npmci npm install
|
||||||
- npmci npm install
|
- npmci npm build
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# metadata stage
|
# metadata stage
|
||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
only:
|
||||||
- docker:stable-dind
|
- tags
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g typescript
|
||||||
- docker run
|
- npmci npm prepare
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci npm install
|
||||||
--volume "$PWD":/code
|
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- priv
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g typedoc typescript
|
- npmci node install stable
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
@ -121,5 +124,5 @@ pages:
|
|||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"command": "npm test",
|
||||||
|
"name": "Run npm test",
|
||||||
|
"request": "launch",
|
||||||
|
"type": "node-terminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -1,6 +1,28 @@
|
|||||||
{
|
{
|
||||||
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "code.foss.global",
|
||||||
|
"gitscope": "push.rocks",
|
||||||
|
"gitrepo": "webdetector",
|
||||||
|
"description": "A library to detect web environments and PWA installation status within a browser.",
|
||||||
|
"npmPackagename": "@push.rocks/webdetector",
|
||||||
|
"license": "MIT",
|
||||||
|
"keywords": [
|
||||||
|
"web environment detection",
|
||||||
|
"PWA status",
|
||||||
|
"online status check",
|
||||||
|
"platform detection",
|
||||||
|
"network status",
|
||||||
|
"browser detection"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [],
|
"npmGlobalTools": [],
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"tsdoc": {
|
||||||
|
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
8291
package-lock.json
generated
8291
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
61
package.json
61
package.json
@ -1,31 +1,56 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/webdetector",
|
"name": "@push.rocks/webdetector",
|
||||||
"version": "1.0.5",
|
"version": "2.0.3",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "detect different environments within the browser",
|
"description": "A library to detect web environments and PWA installation status within a browser.",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(parcel build test/index.html --out-dir ./test/dist)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild --web)",
|
"build": "(tsbuild --web)",
|
||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.66",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tsrun": "^1.2.42",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@gitzone/tstest": "^1.0.74",
|
||||||
"@types/node": "^10.11.7",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"parcel-bundler": "^1.11.0",
|
"@types/node": "^20.2.6"
|
||||||
"tslint": "^5.11.0",
|
|
||||||
"tslint-config-prettier": "^1.15.0"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.2",
|
"@pushrocks/smartdelay": "^3.0.1",
|
||||||
"@pushrocks/smartrx": "^2.0.3"
|
"@pushrocks/smartrx": "^3.0.1"
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 version"
|
"last 1 version"
|
||||||
]
|
],
|
||||||
}
|
"type": "module",
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
],
|
||||||
|
"keywords": [
|
||||||
|
"web environment detection",
|
||||||
|
"PWA status",
|
||||||
|
"online status check",
|
||||||
|
"platform detection",
|
||||||
|
"network status",
|
||||||
|
"browser detection"
|
||||||
|
],
|
||||||
|
"homepage": "https://code.foss.global/push.rocks/webdetector",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://code.foss.global/push.rocks/webdetector.git"
|
||||||
|
}
|
||||||
|
}
|
4450
pnpm-lock.yaml
generated
Normal file
4450
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
103
readme.md
Normal file
103
readme.md
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
# @push.rocks/webdetector
|
||||||
|
detect different environments within the browser
|
||||||
|
|
||||||
|
## Install
|
||||||
|
To install `@push.rocks/webdetector`, you can use npm or yarn. Run the following command in your terminal:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install @push.rocks/webdetector --save
|
||||||
|
```
|
||||||
|
or if you prefer using yarn:
|
||||||
|
```bash
|
||||||
|
yarn add @push.rocks/webdetector
|
||||||
|
```
|
||||||
|
|
||||||
|
This package is designed to work in a browser environment.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
The `@push.rocks/webdetector` module provides a powerful toolset to detect various aspects of the user's browser environment, such as online status, platform type, and PWA (Progressive Web App) installation status. It leverages modern web technologies and observables for real-time detection and updates.
|
||||||
|
|
||||||
|
### Importing the Module
|
||||||
|
First, you need to import the module in your project using ESM syntax:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { WebDetector } from '@push.rocks/webdetector';
|
||||||
|
```
|
||||||
|
|
||||||
|
### Initializing WebDetector
|
||||||
|
To start utilizing WebDetector, you need to create an instance of it by passing configuration options. The most crucial option is the `checkOnlineUrl`, which is used to verify internet connectivity.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const webDetector = new WebDetector({
|
||||||
|
checkOnlineUrl: 'https://example.com', // URL to check online status
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking Online Status
|
||||||
|
WebDetector can check if the browser can reach the internet. This is not a mere online/offline status provided by the browser but a more reliable check that attempts to fetch a predetermined URL.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
async function checkOnlineStatus() {
|
||||||
|
const isOnline = await webDetector.isOnline();
|
||||||
|
console.log(`Online status: ${isOnline}`);
|
||||||
|
}
|
||||||
|
checkOnlineStatus();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Observing Real-Time Online Status
|
||||||
|
WebDetector allows you to observe changes in online status in real-time, which is useful for applications that need to react to connectivity changes promptly.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
webDetector.startPeriodicChecks();
|
||||||
|
webDetector.onlineObservable.subscribe((state) => {
|
||||||
|
console.log(`Current state: ${state}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remember to stop the periodic checks when they are no longer needed
|
||||||
|
webDetector.stopPeriodicChecks();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Detecting the Platform
|
||||||
|
You can easily detect the user's platform (e.g., iOS, Android, Windows, etc.) using WebDetector.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const platform = webDetector.platform.detectPlatform();
|
||||||
|
console.log(`Platform detected: ${platform}`);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Checking PWA Installation Status
|
||||||
|
For Progressive Web Apps, it's vital to know if the user has installed the app. WebDetector provides a straightforward way to check this.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const isPwaInstalled = webDetector.pwa.isInstalled();
|
||||||
|
console.log(`Is PWA Installed: ${isPwaInstalled}`);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Comprehensive Observables Integration
|
||||||
|
`@push.rocks/webdetector` integrates seamlessly with observables provided by RxJS through `@pushrocks/smartrx`, making it easy to integrate with frameworks that use observables for state management.
|
||||||
|
|
||||||
|
### Conclusion
|
||||||
|
`@push.rocks/webdetector` is a versatile module that helps detect various aspects of the browser environment, offering tools for checking connectivity, platform type, and PWA installation status. By utilizing observables for real-time updates, it fits well into modern web application architectures.
|
||||||
|
|
||||||
|
Remember, the accurate detection and reaction to the user's environment enhance the user experience significantly, making `@push.rocks/webdetector` a valuable addition to your web development toolkit.
|
||||||
|
|
||||||
|
For more advanced use cases, refer to the official documentation and source code available on GitHub and GitLab. Your contributions and feedback are welcome to help improve this project further.
|
||||||
|
|
||||||
|
## License and Legal Information
|
||||||
|
|
||||||
|
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||||
|
|
||||||
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
### Trademarks
|
||||||
|
|
||||||
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||||
|
|
||||||
|
### Company Information
|
||||||
|
|
||||||
|
Task Venture Capital GmbH
|
||||||
|
Registered at District court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
|
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||||
|
|
||||||
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
@ -1,7 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<script src="./test.ts"></script>
|
|
||||||
</head>
|
|
||||||
<body></body>
|
|
||||||
</html>
|
|
37
test/test.both.ts
Normal file
37
test/test.both.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
|
import * as webdetector from '../ts/webdetector.classes.webdetector.js';
|
||||||
|
let testWebDetector: webdetector.WebDetector;
|
||||||
|
|
||||||
|
tap.test('first test', async () => {
|
||||||
|
testWebDetector = new webdetector.WebDetector({
|
||||||
|
checkOnlineUrl: 'https://pubapi.lossless.one',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(testWebDetector).toBeInstanceOf(webdetector.WebDetector);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should be online', async () => {
|
||||||
|
const onlineResultBoolean = await testWebDetector.isOnline();
|
||||||
|
console.log('browser is online:');
|
||||||
|
console.log(onlineResultBoolean);
|
||||||
|
testWebDetector.startPeriodicChecks();
|
||||||
|
testWebDetector.onlineObservable.subscribe((state) => {
|
||||||
|
console.log(state);
|
||||||
|
});
|
||||||
|
testWebDetector.stopPeriodicChecks();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should detect the platform', async () => {
|
||||||
|
const platform = testWebDetector.platform.detectPlatform();
|
||||||
|
console.log(platform);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should detect if the app is installed', async () => {
|
||||||
|
const isInstalled = testWebDetector.pwa.isInstalled();
|
||||||
|
expect(isInstalled).toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('hi');
|
||||||
|
|
||||||
|
tap.start();
|
17
test/test.ts
17
test/test.ts
@ -1,17 +0,0 @@
|
|||||||
import * as webdetector from '../ts/index'
|
|
||||||
|
|
||||||
console.log('hi');
|
|
||||||
|
|
||||||
const run = async () => {
|
|
||||||
const testWebDetector = new webdetector.WebDetector({
|
|
||||||
checkOnlineUrl: 'https://pubapi.lossless.one'
|
|
||||||
});
|
|
||||||
const onlineResultBoolean = await testWebDetector.isOnline();
|
|
||||||
console.log('browser is online:')
|
|
||||||
console.log(onlineResultBoolean);
|
|
||||||
testWebDetector.startPeriodicChecks();
|
|
||||||
testWebDetector.onlineObservable.subscribe((state) => {
|
|
||||||
console.log(state);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
run();
|
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/webdetector',
|
||||||
|
version: '2.0.3',
|
||||||
|
description: 'detect different environments within the browser'
|
||||||
|
}
|
57
ts/index.ts
57
ts/index.ts
@ -1,56 +1 @@
|
|||||||
import * as plugins from './webdetector.plugins';
|
export * from './webdetector.classes.webdetector.js';
|
||||||
|
|
||||||
import {throttleTime } from 'rxjs/operators';
|
|
||||||
|
|
||||||
export interface IWebDetectorOptions {
|
|
||||||
checkOnlineUrl: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class WebDetector {
|
|
||||||
options: IWebDetectorOptions;
|
|
||||||
private onlineObservableIntake = new plugins.smartrx.ObservableIntake();
|
|
||||||
onlineObservable = this.onlineObservableIntake.observable.pipe(throttleTime(10000));
|
|
||||||
latestState: 'online' | 'offline' = 'online';
|
|
||||||
|
|
||||||
constructor(optionsArg: IWebDetectorOptions) {
|
|
||||||
this.options = optionsArg;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
async isOnline() {
|
|
||||||
let reachesInternet: boolean = false;
|
|
||||||
const controller = new AbortController();
|
|
||||||
const fetchPromise = fetch(this.options.checkOnlineUrl, { signal: controller.signal });
|
|
||||||
const timeout = setTimeout(() => {
|
|
||||||
controller.abort();
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
await fetchPromise.then(async response => {
|
|
||||||
reachesInternet = true
|
|
||||||
}).catch(err => {
|
|
||||||
// console.log(`request to ${this.options.checkOnlineUrl} failed}`)
|
|
||||||
});
|
|
||||||
const latestLocalState = (() => {
|
|
||||||
if(reachesInternet) {
|
|
||||||
return 'online'
|
|
||||||
} else {
|
|
||||||
return 'offline'
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
if(latestLocalState !== this.latestState) {
|
|
||||||
|
|
||||||
this.onlineObservableIntake.push(this.latestState);
|
|
||||||
}
|
|
||||||
this.latestState = latestLocalState;
|
|
||||||
return reachesInternet;
|
|
||||||
}
|
|
||||||
|
|
||||||
async startPeriodicChecks() {
|
|
||||||
while (true) {
|
|
||||||
await this.isOnline();
|
|
||||||
await plugins.smartdelay.delayFor(3000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
25
ts/webdetector.classes.platform.ts
Normal file
25
ts/webdetector.classes.platform.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
export type TWebPlatform = 'android' | 'ios' | 'windows' | 'mac' | 'linux' | 'unknown';
|
||||||
|
|
||||||
|
export class Platform {
|
||||||
|
public detectPlatform(): TWebPlatform {
|
||||||
|
const userAgent = globalThis?.navigator?.userAgent?.toLowerCase();
|
||||||
|
|
||||||
|
if (!userAgent) {
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (/android/.test(userAgent)) {
|
||||||
|
return 'android';
|
||||||
|
} else if (/iphone|ipad|ipod/.test(userAgent)) {
|
||||||
|
return 'ios';
|
||||||
|
} else if (/win/.test(userAgent)) {
|
||||||
|
return 'windows';
|
||||||
|
} else if (/mac/.test(userAgent)) {
|
||||||
|
return 'mac';
|
||||||
|
} else if (/linux/.test(userAgent)) {
|
||||||
|
return 'linux';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
}
|
12
ts/webdetector.classes.pwa.ts
Normal file
12
ts/webdetector.classes.pwa.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
export class Pwa {
|
||||||
|
public isInstalled(): boolean {
|
||||||
|
let isInstalled: boolean;
|
||||||
|
if (globalThis?.matchMedia?.('(display-mode: standalone)').matches) {
|
||||||
|
isInstalled = true;
|
||||||
|
console.log('PWA installed');
|
||||||
|
} else {
|
||||||
|
isInstalled = false;
|
||||||
|
}
|
||||||
|
return isInstalled;
|
||||||
|
}
|
||||||
|
}
|
71
ts/webdetector.classes.webdetector.ts
Normal file
71
ts/webdetector.classes.webdetector.ts
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import * as plugins from './webdetector.plugins.js';
|
||||||
|
|
||||||
|
import { Platform } from './webdetector.classes.platform.js';
|
||||||
|
import { Pwa } from './webdetector.classes.pwa.js';
|
||||||
|
|
||||||
|
export interface IWebDetectorOptions {
|
||||||
|
checkOnlineUrl: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WebDetector {
|
||||||
|
// subclasses
|
||||||
|
public platform = new Platform();
|
||||||
|
public pwa = new Pwa();
|
||||||
|
|
||||||
|
options: IWebDetectorOptions;
|
||||||
|
private onlineObservableIntake = new plugins.smartrx.ObservableIntake();
|
||||||
|
public onlineObservable = this.onlineObservableIntake.observable.pipe(
|
||||||
|
plugins.smartrx.rxjs.ops.throttleTime(10000)
|
||||||
|
);
|
||||||
|
latestState: 'online' | 'offline' = 'online';
|
||||||
|
|
||||||
|
constructor(optionsArg: IWebDetectorOptions) {
|
||||||
|
this.options = optionsArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
async isOnline() {
|
||||||
|
let reachesInternet: boolean = false;
|
||||||
|
const controller = new AbortController();
|
||||||
|
const fetchPromise = fetch(this.options.checkOnlineUrl, { signal: controller.signal });
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
controller.abort();
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
await fetchPromise
|
||||||
|
.then(async (response) => {
|
||||||
|
reachesInternet = true;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
// console.log(`request to ${this.options.checkOnlineUrl} failed}`)
|
||||||
|
});
|
||||||
|
const latestLocalState = (() => {
|
||||||
|
if (reachesInternet) {
|
||||||
|
return 'online';
|
||||||
|
} else {
|
||||||
|
return 'offline';
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
if (latestLocalState !== this.latestState) {
|
||||||
|
this.onlineObservableIntake.push(this.latestState);
|
||||||
|
}
|
||||||
|
this.latestState = latestLocalState;
|
||||||
|
return reachesInternet;
|
||||||
|
}
|
||||||
|
|
||||||
|
private periodicChecksRunning = false;
|
||||||
|
|
||||||
|
public async startPeriodicChecks() {
|
||||||
|
this.periodicChecksRunning = true;
|
||||||
|
while (this.periodicChecksRunning) {
|
||||||
|
await this.isOnline();
|
||||||
|
await plugins.smartdelay.delayFor(3000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async stopPeriodicChecks() {
|
||||||
|
this.periodicChecksRunning = false;
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,4 @@
|
|||||||
import * as smartdelay from '@pushrocks/smartdelay';
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartrx from '@pushrocks/smartrx';
|
import * as smartrx from '@pushrocks/smartrx';
|
||||||
|
|
||||||
export {
|
export { smartdelay, smartrx };
|
||||||
smartdelay,
|
|
||||||
smartrx
|
|
||||||
}
|
|
||||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"verbatimModuleSyntax": true
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"dist_*/**/*.d.ts"
|
||||||
|
]
|
||||||
|
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user