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

Addressable post messages. #259

Merged
merged 25 commits into from
Jul 30, 2020
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
strategy:
matrix:
node-version: [10.x]
timeout-minutes: 30
name: CI
steps:
- uses: actions/checkout@v1
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ coverage/
yarn-error.log
.DS_Store
report.html
/packages/addressable/report.cjs.html
/packages/addressable/report.iffe.html
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"start": "http-server -a localhost -p 8000",
"start-node-example": "lerna run start-node-example",
"bootstrap": "lerna bootstrap --npm-client=yarn",
"build": "lerna run build",
"build": "lerna run --stream --concurrency 1 build",
"test": "lerna run test",
"test-dom-env": "lerna run test-dom-env",
"test-node-env": "lerna run test-node-env",
Expand All @@ -48,9 +48,9 @@
"lerna": "^3.13.1",
"prettier": "^1.16.4",
"pretty-quick": "^1.10.0",
"remark-cli": "^6.0.1",
"remark-lint": "^6.0.4",
"remark-preset-lint-recommended": "^3.0.2",
"remark-cli": "^8.0.1",
"remark-lint": "^7.0.1",
"remark-preset-lint-recommended": "^4.0.1",
"rimraf": "^2.6.3",
"tslint-config-prettier": "^1.17.0"
}
Expand Down
58 changes: 58 additions & 0 deletions packages/addressable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Addressable

A lightweight standalone micro lib to create addressable post messages.
Let say you have some post messages on the main thread a couple of IFrames and a few web workers.
Sending message between this element will be a nightmare!

The solution, **addresses** like a plan tcp/ip network, or the WEB

- This package is independent it maintained under scalecube-js only for convenience.

# API

Add thread to network; you must add it to main thread in order iframe/workers will work

```ts
import '@scalecube/addressable';
```

listen for messages on address

```ts
import {listen} from '@scalecube/addressable';
listen("address", (msg, port: MessagePort)=>{port.postMessage("pong")});
```

connect to address

```ts
import {connect} from '@scalecube/addressable';
const port = connect("address");
port.addEventListener("message", console.log);
port.postMessage("ping");
```

Cleaning connections
```ts
import {connect, listen} from '@scalecube/addressable';
listen("address", (msg, port: MessagePort) => {
if( msg === "ping" ) {
port.postMessage("pong");
} else if (msg === "close") {
port.close();
}
});

const port = connect("address");
port.addEventListener("message", console.log);
port.postMessage("ping");
port.postMessage("close");
port.close();
```

# How it's works

- Each time you import addressable it will create global event listener in each thread
- Threads will register them self by sending a message to main thread
- Each time new listener added it will spread to main thread and to all thread from there
- When connect is called it will create a message channel and give a port to listen callback and open side
3 changes: 3 additions & 0 deletions packages/addressable/e2eSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('./e2eStart');

module.exports = require('jest-environment-puppeteer').setup;
7 changes: 7 additions & 0 deletions packages/addressable/e2eStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { exec } = require('child_process');

const start = exec('yarn start', { cwd: '../../' });

module.exports = {
start,
};
3 changes: 3 additions & 0 deletions packages/addressable/e2eTeardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const p = require('./e2eStart');
p.start.kill('SIGQUIT');
module.exports = require('jest-environment-puppeteer').teardown;
6 changes: 6 additions & 0 deletions packages/addressable/jest-puppeteer.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
testRegex: '(\\.|/)browser\\.ts$',
preset: 'jest-puppeteer',
globalSetup: './e2eSetup.js',
globalTeardown: './e2eTeardown.js',
};
13 changes: 13 additions & 0 deletions packages/addressable/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
transform: {
'.(ts|tsx)': 'ts-jest',
},
testRegex: '(\\.|/)spec\\.ts$',
testPathIgnorePatterns: ['<rootDir>/es/', '<rootDir>/lib/', '<rootDir>/node_modules/'],
moduleFileExtensions: ['ts', 'tsx', 'js'],
moduleDirectories: ['node_modules', 'src'],
globals: {
isNodeEvn: false,
},
setupFilesAfterEnv: ['<rootDir>/tests/messageChannelMock.ts'],
};
51 changes: 51 additions & 0 deletions packages/addressable/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@scalecube/addressable",
"version": "0.2.9",
"private": false,
"unpkg": "dist/index.js",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"dist"
],
"license": "MIT",
"scripts": {
"clean": "rimraf node_modules && rimraf .cache && rimraf lib && rimraf es",
"build": "rimraf .cache && yarn build:dist && yarn build:cjs",
"build:dist": "rimraf dist && rollup -c rollup.iife.config.js",
"build:cjs": "rimraf lib && rollup -c rollup.cjs.config.js",
"fixture": "yarn build && yarn fixture:build",
"fixture:build": "rimraf tests/fixtures/pingPong/dist && parcel build tests/fixtures/pingPong/pingPong.ts --no-source-maps --experimental-scope-hoisting --out-dir tests/fixtures/pingPong/dist",
"lint": "tslint '{src,tests}/**/*.{ts,tsx}' --fix",
"prettier": "prettier --write '{src,tests}/**/*.{ts,tsx}'",
"test": "yarn test:unit && yarn test:browser",
"test:unit": "jest --config jest.config.js",
"test:browser": "yarn fixture && jest --forceExit --config jest-puppeteer.config.js",
"doc": "typedoc ./src --out ./doc --mode file --name 'Scalecube API' --hideGenerator --readme ./README.md"
},
"author": "Scalecube (https://github.com/scalecube/scalecube-js)",
"dependencies": {},
"devDependencies": {
"@scalecube/utils": "^0.2.9",
"@types/expect-puppeteer": "^4.4.3",
"@types/jest-environment-puppeteer": "^4.3.2",
"@types/puppeteer": "^3.0.1",
"jest-puppeteer": "^4.4.0",
"puppeteer": "^5.2.1",
"rollup": "^1.27.4",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-filesize": "^6.1.1",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.27.1",
"rollup-plugin-uglify-es": "^0.0.1",
"rollup-plugin-visualizer": "^2.6.0",
"tslint": "^5.11.0",
"typedoc": "^0.14.2",
"typescript": "^3.2.4"
}
}
58 changes: 58 additions & 0 deletions packages/addressable/rollup.cjs.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import visualizer from 'rollup-plugin-visualizer';
import typescript from 'rollup-plugin-typescript2';
import tscompile from 'typescript';
import filesize from 'rollup-plugin-filesize';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import pkg from './package.json';
import babel from 'rollup-plugin-babel';

export default {
input: 'src/index.ts',
output: [
{
file: pkg.main,
format: 'cjs',
sourcemap: false,
},
],
plugins: [
commonjs({
include: /node_modules/,
browser: true,
namedExports: {
'rsocket-types': ['CONNECTION_STATUS'],
},
}),
resolve(),
babel({
plugins: ['@babel/plugin-transform-arrow-functions'],
babelrc: false,
runtimeHelpers: true,
presets: [
[
'@babel/preset-env',
{
modules: false,
spec: true,
forceAllTransforms: true,
targets: {
chrome: '29',
ie: '11',
},
},
],
],
}),
visualizer({
filename: 'report.cjs.html',
title: 'Browser - cjs',
}),
typescript({
typescript: tscompile,
clean: true,
}),
// global(),
filesize(),
],
};
61 changes: 61 additions & 0 deletions packages/addressable/rollup.iife.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import visualizer from 'rollup-plugin-visualizer';
import typescript from 'rollup-plugin-typescript2';
import tscompile from 'typescript';
import filesize from 'rollup-plugin-filesize';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import pkg from './package.json';
import replace from 'rollup-plugin-replace';
import { terser } from 'rollup-plugin-terser';
import babel from 'rollup-plugin-babel';

export default {
input: 'src/index.ts',
output: [
{
name: 'sc',
file: pkg.unpkg,
format: 'iife',
sourcemap: false,
},
],
plugins: [
commonjs({
include: /node_modules/,
browser: true,
}),
resolve(),
babel({
plugins: ['@babel/plugin-transform-arrow-functions'],
babelrc: true,
runtimeHelpers: true,
presets: [
[
'@babel/preset-env',
{
modules: false,
spec: true,
forceAllTransforms: true,
targets: {
chrome: '29',
ie: '11',
},
},
],
],
}),
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
visualizer({
filename: 'report.iffe.html',
title: 'Microservice - iife',
}),
typescript({
typescript: tscompile,
clean: true,
}),
terser(),
filesize(),
],
};
Loading