Skip to content

Commit 60537af

Browse files
dan1wangflexdinesh
authored andcommitted
Add Web Worker support (fix #3)
1 parent 5d90b75 commit 60537af

File tree

9 files changed

+128
-3
lines changed

9 files changed

+128
-3
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
test
2+
browser-test

browser-test/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Web Workers test</title>
5+
</head>
6+
<body>
7+
<h1>Browser Test</h1>
8+
<textarea id="console" style="width:50em;height:20em;"></textarea>
9+
10+
<script src="index.js" type="module"></script>
11+
</body>
12+
</html>

browser-test/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
import { isBrowser, isWebWorker, isNode } from './browser-or-node.js';
4+
5+
console.log = (message) => {
6+
document.getElementById('console').value += message + '\n';
7+
}
8+
9+
console.log('Web page reports:');
10+
console.log(' isBrowser: ' + isBrowser);
11+
console.log(' isWebWorker: ' + isWebWorker);
12+
console.log(' isNode: ' + isNode);
13+
14+
if (window.Worker) {
15+
const myWorker = new Worker("worker.js");
16+
17+
myWorker.onmessage = function(e) {
18+
let res = e.data;
19+
console.log('Web worker reports:');
20+
console.log(' isBrowser: ' + e.data[0]);
21+
console.log(' isWebWorker: ' + e.data[1]);
22+
console.log(' isNode: ' + e.data[2]);
23+
}
24+
myWorker.postMessage(['Report, please']);
25+
26+
} else {
27+
console.log('Your browser doesn\'t support web workers.')
28+
}

browser-test/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "browser-or-node",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "server.js",
6+
"author": "Daniel Wang",
7+
"license": "MIT",
8+
"dependencies": {
9+
"express": "^4.16.4",
10+
"path": "^0.12.7"
11+
}
12+
}

browser-test/server.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const express = require('express');
2+
const path = require('path');
3+
4+
const app = express();
5+
6+
// app.use(express.static(path.join(__dirname, 'public')));
7+
// app.use('/static', express.static(path.join(__dirname, 'public')));
8+
9+
app.get('/', function(req, res) {
10+
res.sendFile(path.join(__dirname + '/index.html'));
11+
});
12+
13+
app.get('/worker.js', function(req, res) {
14+
res.sendFile(path.join(__dirname + '/worker.js'));
15+
});
16+
17+
/*
18+
app.get('/worker.js', function(req, res) {
19+
res.sendFile(path.join(__dirname + '../../src/index.js'));
20+
});
21+
*/
22+
app.get('/index.js', function(req, res) {
23+
res.sendFile(path.join(__dirname + '/index.js'));
24+
});
25+
26+
app.get('/browser-or-node.js', function(req, res) {
27+
res.sendFile(path.join(__dirname + '../../src/index.js'));
28+
});
29+
30+
app.listen(3000, () => {
31+
console.log('http://localhost:3000');
32+
});

browser-test/worker.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// HELP!
2+
// self.importScripts(...) imports the script, but then Chrome throws
3+
// an error on export {...} syntax
4+
5+
// self.importScripts('browser-or-node.js');
6+
7+
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
8+
9+
/* eslint-disable no-restricted-globals */
10+
const isWebWorker =
11+
typeof self === 'object' &&
12+
self.constructor &&
13+
self.constructor.name === 'DedicatedWorkerGlobalScope';
14+
/* eslint-enable no-restricted-globals */
15+
16+
const isNode =
17+
typeof process !== 'undefined' &&
18+
process.versions != null &&
19+
process.versions.node != null;
20+
21+
onmessage = function(e) {
22+
postMessage( [isBrowser, isWebWorker, isNode] );
23+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browser-or-node",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Check where the code is running in the browser or node.js",
55
"main": "./lib/index.js",
66
"scripts": {
@@ -31,6 +31,9 @@
3131
"is browser node"
3232
],
3333
"author": "Dineshkumar Pandiyan <[email protected]>",
34+
"contributors": [
35+
"Daniel Wang <[email protected]> (https://github.com/dan1wang/)"
36+
],
3437
"license": "MIT",
3538
"bugs": {
3639
"url": "https://github.com/flexdinesh/browser-or-node/issues"

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
/* global window */
1+
/* global window self */
22

33
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
44

5+
/* eslint-disable no-restricted-globals */
6+
const isWebWorker =
7+
typeof self === 'object' &&
8+
self.constructor &&
9+
self.constructor.name === 'DedicatedWorkerGlobalScope';
10+
/* eslint-enable no-restricted-globals */
11+
512
const isNode =
613
typeof process !== 'undefined' &&
714
process.versions != null &&
815
process.versions.node != null;
916

1017
export {
1118
isBrowser,
19+
isWebWorker,
1220
isNode
1321
};

test/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { assert } from 'chai';
2-
import { isBrowser, isNode } from '../src';
2+
import { isBrowser, isWebBrowser, isNode } from '../src';
3+
4+
console.log(isNode);
35

46
describe('Browser or Node.js', () => {
57

@@ -12,4 +14,8 @@ describe('Browser or Node.js', () => {
1214
assert(isBrowser === false, 'isBrowser didn\'t work :(');
1315
});
1416

17+
it('should check web worker env', () => {
18+
assert(isNode === true, 'isWebWorker didn\'t work :(');
19+
});
20+
1521
});

0 commit comments

Comments
 (0)