zero-com 0.0.1 → 0.0.3
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/README.md +8 -8
- package/index.js +11 -11
- package/index.ts +9 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
# Zero
|
|
1
|
+
# Zero-com
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
It is a zero-byte no-lib utility for transparently communicating client-side and server-side modules residing in the same full-stack project.
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
7
|
Webpack config.
|
|
8
8
|
```js
|
|
9
|
-
new
|
|
9
|
+
new ZeroComWebpackPlugin({
|
|
10
10
|
development: true,
|
|
11
11
|
patterns: {
|
|
12
12
|
client: 'src/client/**',
|
|
@@ -34,14 +34,14 @@ import { getPhones } '../server/phones'
|
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
## Trasport layer
|
|
37
|
-
Zero-
|
|
37
|
+
Zero-com does not define any transport layer, it allows you to define a new one or reuse your own.
|
|
38
38
|
|
|
39
|
-
- `window.
|
|
40
|
-
- `global.
|
|
39
|
+
- `window.ZERO_COM_CLIENT_SEND` all mesages from client-side will be sent using this function.
|
|
40
|
+
- `global.ZERO_COM_SERVER_REGISTRY` object available on the server-side of which the keys are the name of the methods and the values are the functions to be executed.
|
|
41
41
|
|
|
42
42
|
Client side example.
|
|
43
43
|
```js
|
|
44
|
-
window.
|
|
44
|
+
window.ZERO_COM_CLIENT_SEND = async ({ method, params }) {
|
|
45
45
|
// -> send the message to server
|
|
46
46
|
// <- return response
|
|
47
47
|
}
|
|
@@ -50,7 +50,7 @@ window.RPC_CLIENT_SEND = async ({ method, params }) {
|
|
|
50
50
|
Server side example.
|
|
51
51
|
```js
|
|
52
52
|
const someCustomHandler = (message) => {
|
|
53
|
-
const func = global.
|
|
53
|
+
const func = global.ZERO_COM_SERVER_REGISTRY[message.method]
|
|
54
54
|
return func(...message.params)
|
|
55
55
|
}
|
|
56
56
|
```
|
package/index.js
CHANGED
|
@@ -3,13 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.ZeroComWebpackPlugin = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const minimatch_1 = require("minimatch");
|
|
9
9
|
const path_1 = __importDefault(require("path"));
|
|
10
10
|
const ts_morph_1 = require("ts-morph");
|
|
11
11
|
const typescript_1 = __importDefault(require("typescript"));
|
|
12
|
-
class
|
|
12
|
+
class ZeroComWebpackPlugin {
|
|
13
13
|
constructor(options) {
|
|
14
14
|
this.options = Object.assign(Object.assign({ development: true }, options), { patterns: Object.assign({}, options.patterns) });
|
|
15
15
|
this.compilationId = String(Math.floor(Math.random() * 1000000));
|
|
@@ -17,9 +17,9 @@ class ZeroRpcWebpackPlugin {
|
|
|
17
17
|
this.serverPattern = this.options.patterns.server;
|
|
18
18
|
}
|
|
19
19
|
apply(compiler) {
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const pluginName =
|
|
20
|
+
const ZERO_COM_CLIENT_SEND = 'ZERO_COM_CLIENT_SEND';
|
|
21
|
+
const ZERO_COM_SERVER_REGISTRY = 'ZERO_COM_SERVER_REGISTRY';
|
|
22
|
+
const pluginName = ZeroComWebpackPlugin.name;
|
|
23
23
|
const { webpack } = compiler;
|
|
24
24
|
const { RawSource } = webpack.sources;
|
|
25
25
|
compiler.hooks.normalModuleFactory.tap(pluginName, (nmf) => {
|
|
@@ -62,7 +62,7 @@ class ZeroRpcWebpackPlugin {
|
|
|
62
62
|
const lineNumber = func.getStartLineNumber();
|
|
63
63
|
const funcParams = func.getParameters().map(p => p.getName()).join(', ');
|
|
64
64
|
const method = formatMethodName(funcName, path_1.default.relative(compiler.context, absolutePath), lineNumber);
|
|
65
|
-
const newFunctionBody = `return window.${
|
|
65
|
+
const newFunctionBody = `return window.${ZERO_COM_CLIENT_SEND}({method: '${method}', params: [${funcParams}]})`;
|
|
66
66
|
func.setBodyText(newFunctionBody);
|
|
67
67
|
generatedFunctions.push(func.getText());
|
|
68
68
|
console.log('client:', method);
|
|
@@ -77,11 +77,11 @@ class ZeroRpcWebpackPlugin {
|
|
|
77
77
|
const funcName = String(func.getName());
|
|
78
78
|
const lineNumber = func.getStartLineNumber();
|
|
79
79
|
const method = formatMethodName(funcName, path_1.default.relative(compiler.context, absolutePath), lineNumber);
|
|
80
|
-
chunks.push(`global.${
|
|
80
|
+
chunks.push(`global.${ZERO_COM_SERVER_REGISTRY}['${method}'] = ${funcName}`);
|
|
81
81
|
console.log('server:', method);
|
|
82
82
|
}
|
|
83
83
|
});
|
|
84
|
-
newModuleContent = `${originalContent} if (!global.${
|
|
84
|
+
newModuleContent = `${originalContent} if (!global.${ZERO_COM_SERVER_REGISTRY}) global.${ZERO_COM_SERVER_REGISTRY} = Object.create(null); ${chunks.join(',')}`;
|
|
85
85
|
}
|
|
86
86
|
project.createSourceFile(absolutePath + '.ts', newModuleContent, { overwrite: true });
|
|
87
87
|
const result = project.emitToMemory();
|
|
@@ -93,8 +93,8 @@ class ZeroRpcWebpackPlugin {
|
|
|
93
93
|
if (this.options.development)
|
|
94
94
|
return;
|
|
95
95
|
const replacements = [
|
|
96
|
-
{ target:
|
|
97
|
-
{ target:
|
|
96
|
+
{ target: ZERO_COM_CLIENT_SEND, replacement: `__ZERO_COM_CLIENT_SEND_${this.compilationId}` },
|
|
97
|
+
{ target: ZERO_COM_SERVER_REGISTRY, replacement: `__ZERO_COM_SERVER_REGISTRY_${this.compilationId}` }
|
|
98
98
|
];
|
|
99
99
|
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
|
|
100
100
|
compilation.hooks.processAssets.tap({ name: pluginName, stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE }, (assets) => {
|
|
@@ -117,7 +117,7 @@ class ZeroRpcWebpackPlugin {
|
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
|
-
exports.
|
|
120
|
+
exports.ZeroComWebpackPlugin = ZeroComWebpackPlugin;
|
|
121
121
|
const formatMethodName = (funcName, path, line) => {
|
|
122
122
|
return `${funcName}@${path}:${line}`;
|
|
123
123
|
};
|
package/index.ts
CHANGED
|
@@ -19,7 +19,7 @@ export type Message = {
|
|
|
19
19
|
[key: string]: any
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export class
|
|
22
|
+
export class ZeroComWebpackPlugin {
|
|
23
23
|
private options: Options
|
|
24
24
|
private compilationId: string
|
|
25
25
|
private clientPattern: string
|
|
@@ -39,10 +39,10 @@ export class ZeroRpcWebpackPlugin {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
apply(compiler: Compiler) {
|
|
42
|
-
const
|
|
43
|
-
const
|
|
42
|
+
const ZERO_COM_CLIENT_SEND = 'ZERO_COM_CLIENT_SEND'
|
|
43
|
+
const ZERO_COM_SERVER_REGISTRY = 'ZERO_COM_SERVER_REGISTRY'
|
|
44
44
|
|
|
45
|
-
const pluginName =
|
|
45
|
+
const pluginName = ZeroComWebpackPlugin.name
|
|
46
46
|
const { webpack } = compiler
|
|
47
47
|
const { RawSource } = webpack.sources
|
|
48
48
|
|
|
@@ -89,7 +89,7 @@ export class ZeroRpcWebpackPlugin {
|
|
|
89
89
|
const lineNumber = func.getStartLineNumber()
|
|
90
90
|
const funcParams = func.getParameters().map(p => p.getName()).join(', ')
|
|
91
91
|
const method = formatMethodName(funcName, path.relative(compiler.context, absolutePath), lineNumber)
|
|
92
|
-
const newFunctionBody = `return window.${
|
|
92
|
+
const newFunctionBody = `return window.${ZERO_COM_CLIENT_SEND}({method: '${method}', params: [${funcParams}]})`
|
|
93
93
|
func.setBodyText(newFunctionBody)
|
|
94
94
|
generatedFunctions.push(func.getText())
|
|
95
95
|
console.log('client:', method)
|
|
@@ -103,11 +103,11 @@ export class ZeroRpcWebpackPlugin {
|
|
|
103
103
|
const funcName = String(func.getName())
|
|
104
104
|
const lineNumber = func.getStartLineNumber()
|
|
105
105
|
const method = formatMethodName(funcName, path.relative(compiler.context, absolutePath), lineNumber)
|
|
106
|
-
chunks.push(`global.${
|
|
106
|
+
chunks.push(`global.${ZERO_COM_SERVER_REGISTRY}['${method}'] = ${funcName}`)
|
|
107
107
|
console.log('server:', method)
|
|
108
108
|
}
|
|
109
109
|
})
|
|
110
|
-
newModuleContent = `${originalContent} if (!global.${
|
|
110
|
+
newModuleContent = `${originalContent} if (!global.${ZERO_COM_SERVER_REGISTRY}) global.${ZERO_COM_SERVER_REGISTRY} = Object.create(null); ${chunks.join(',')}`
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
project.createSourceFile(absolutePath + '.ts', newModuleContent, { overwrite: true })
|
|
@@ -121,8 +121,8 @@ export class ZeroRpcWebpackPlugin {
|
|
|
121
121
|
if (this.options.development) return
|
|
122
122
|
|
|
123
123
|
const replacements = [
|
|
124
|
-
{ target:
|
|
125
|
-
{ target:
|
|
124
|
+
{ target: ZERO_COM_CLIENT_SEND, replacement: `__ZERO_COM_CLIENT_SEND_${this.compilationId}` },
|
|
125
|
+
{ target: ZERO_COM_SERVER_REGISTRY, replacement: `__ZERO_COM_SERVER_REGISTRY_${this.compilationId}` }
|
|
126
126
|
]
|
|
127
127
|
|
|
128
128
|
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
|