zero-com 0.0.1 → 0.0.2

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 CHANGED
@@ -1,12 +1,12 @@
1
- # Zero RPC
1
+ # Zero-com
2
2
 
3
- Zero-byte no-lib utility for transparently linking client-side and server-side code residing in the same full-stack project via RPC.
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 ZeroRpcWebpackPlugin({
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-rpc does not define any transport layer, it allows you to define a new one or reuse your own.
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.RPC_CLIENT_SEND` all mesages from client-side will be sent using this function.
40
- - `global.RPC_CLIENT_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.
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.RPC_CLIENT_SEND = async ({ method, params }) {
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.RPC_CLIENT_REGISTRY[message.method]
53
+ const func = global.ZERO_COM_SERVER_REGISTRY[message.method]
54
54
  return func(...message.params)
55
55
  }
56
56
  ```
package/index.ts CHANGED
@@ -19,7 +19,7 @@ export type Message = {
19
19
  [key: string]: any
20
20
  }
21
21
 
22
- export class ZeroRpcWebpackPlugin {
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 RPC_CLIENT_SEND = 'RPC_CLIENT_SEND'
43
- const RPC_SERVER_REGISTRY = 'RPC_SERVER_REGISTRY'
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 = ZeroRpcWebpackPlugin.name
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.${RPC_CLIENT_SEND}({method: '${method}', params: [${funcParams}]})`
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.${RPC_SERVER_REGISTRY}['${method}'] = ${funcName}`)
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.${RPC_SERVER_REGISTRY}) global.${RPC_SERVER_REGISTRY} = Object.create(null); ${chunks.join(',')}`
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: RPC_CLIENT_SEND, replacement: `__RPC_CLIENT_SEND_${this.compilationId}` },
125
- { target: RPC_SERVER_REGISTRY, replacement: `__RPC_SERVER_REGISTRY_${this.compilationId}` }
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) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zero-com",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "build": "npx tsc"
package/index.js DELETED
@@ -1,123 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ZeroRpcWebpackPlugin = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const minimatch_1 = require("minimatch");
9
- const path_1 = __importDefault(require("path"));
10
- const ts_morph_1 = require("ts-morph");
11
- const typescript_1 = __importDefault(require("typescript"));
12
- class ZeroRpcWebpackPlugin {
13
- constructor(options) {
14
- this.options = Object.assign(Object.assign({ development: true }, options), { patterns: Object.assign({}, options.patterns) });
15
- this.compilationId = String(Math.floor(Math.random() * 1000000));
16
- this.clientPattern = this.options.patterns.client;
17
- this.serverPattern = this.options.patterns.server;
18
- }
19
- apply(compiler) {
20
- const RPC_CLIENT_SEND = 'RPC_CLIENT_SEND';
21
- const RPC_SERVER_REGISTRY = 'RPC_SERVER_REGISTRY';
22
- const pluginName = ZeroRpcWebpackPlugin.name;
23
- const { webpack } = compiler;
24
- const { RawSource } = webpack.sources;
25
- compiler.hooks.normalModuleFactory.tap(pluginName, (nmf) => {
26
- nmf.hooks.beforeResolve.tap(pluginName, (resolveData) => {
27
- const absolutePath = path_1.default.resolve(resolveData.context, resolveData.request);
28
- const isServerFile = (0, minimatch_1.minimatch)(absolutePath, path_1.default.join(compiler.context, this.serverPattern));
29
- if (!isServerFile)
30
- return;
31
- const requestedFromClient = (0, minimatch_1.minimatch)(resolveData.contextInfo.issuer, path_1.default.join(compiler.context, this.clientPattern));
32
- const tsPath = absolutePath + '.ts';
33
- const jsPath = absolutePath + '.js';
34
- const mjsPath = absolutePath + '.mjs';
35
- let resolvedPath = '';
36
- if (fs_1.default.existsSync(tsPath)) {
37
- resolvedPath = tsPath;
38
- }
39
- else if (fs_1.default.existsSync(jsPath)) {
40
- resolvedPath = jsPath;
41
- }
42
- else if (fs_1.default.existsSync(mjsPath)) {
43
- resolvedPath = mjsPath;
44
- }
45
- else {
46
- throw new Error('Unable to resolve: ' + absolutePath);
47
- }
48
- const originalContent = fs_1.default.readFileSync(resolvedPath, 'utf8');
49
- const project = new ts_morph_1.Project({
50
- compilerOptions: {
51
- target: typescript_1.default.ScriptTarget.ES2017,
52
- module: typescript_1.default.ModuleKind.ESNext,
53
- },
54
- });
55
- const sourceFile = project.createSourceFile(absolutePath, originalContent, { overwrite: true });
56
- let newModuleContent = '';
57
- if (requestedFromClient) {
58
- const generatedFunctions = [];
59
- sourceFile.getFunctions().forEach(func => {
60
- if (func.isExported() && func.isAsync()) {
61
- const funcName = String(func.getName());
62
- const lineNumber = func.getStartLineNumber();
63
- const funcParams = func.getParameters().map(p => p.getName()).join(', ');
64
- const method = formatMethodName(funcName, path_1.default.relative(compiler.context, absolutePath), lineNumber);
65
- const newFunctionBody = `return window.${RPC_CLIENT_SEND}({method: '${method}', params: [${funcParams}]})`;
66
- func.setBodyText(newFunctionBody);
67
- generatedFunctions.push(func.getText());
68
- console.log('client:', method);
69
- }
70
- });
71
- newModuleContent = generatedFunctions.join('\n\n');
72
- }
73
- else {
74
- const chunks = [];
75
- sourceFile.getFunctions().forEach(func => {
76
- if (func.isExported() && func.isAsync()) {
77
- const funcName = String(func.getName());
78
- const lineNumber = func.getStartLineNumber();
79
- const method = formatMethodName(funcName, path_1.default.relative(compiler.context, absolutePath), lineNumber);
80
- chunks.push(`global.${RPC_SERVER_REGISTRY}['${method}'] = ${funcName}`);
81
- console.log('server:', method);
82
- }
83
- });
84
- newModuleContent = `${originalContent} if (!global.${RPC_SERVER_REGISTRY}) global.${RPC_SERVER_REGISTRY} = Object.create(null); ${chunks.join(',')}`;
85
- }
86
- project.createSourceFile(absolutePath + '.ts', newModuleContent, { overwrite: true });
87
- const result = project.emitToMemory();
88
- const newContent = result.getFiles()[0].text;
89
- const inlineLoader = `data:text/javascript,${encodeURIComponent(newContent)}`;
90
- resolveData.request = inlineLoader;
91
- });
92
- });
93
- if (this.options.development)
94
- return;
95
- const replacements = [
96
- { target: RPC_CLIENT_SEND, replacement: `__RPC_CLIENT_SEND_${this.compilationId}` },
97
- { target: RPC_SERVER_REGISTRY, replacement: `__RPC_SERVER_REGISTRY_${this.compilationId}` }
98
- ];
99
- compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
100
- compilation.hooks.processAssets.tap({ name: pluginName, stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE }, (assets) => {
101
- for (const assetName in assets) {
102
- if (assetName.endsWith('.js')) {
103
- let assetSource = String(assets[assetName].source());
104
- let modified = false;
105
- replacements.forEach(({ target, replacement }) => {
106
- if (assetSource.includes(target)) {
107
- assetSource = assetSource.replaceAll(target, replacement);
108
- modified = true;
109
- }
110
- });
111
- if (modified) {
112
- compilation.updateAsset(assetName, new RawSource(assetSource));
113
- }
114
- }
115
- }
116
- });
117
- });
118
- }
119
- }
120
- exports.ZeroRpcWebpackPlugin = ZeroRpcWebpackPlugin;
121
- const formatMethodName = (funcName, path, line) => {
122
- return `${funcName}@${path}:${line}`;
123
- };