Skip to content

Commit e758b45

Browse files
committed
feat: update articles
1 parent a37c9ab commit e758b45

4 files changed

Lines changed: 262 additions & 0 deletions

File tree

‎Java/并发编程/Netty/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Netty
2+
3+
Netty 是一个异步网络库,使 JAVA NIO 的功能更好用。
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Web 框架
2+
3+
First you need to handle HTTP data with a Netty ChannelPipeline using a pipeline factory:
4+
5+
```java
6+
public class DefaultNettyServletPipelineFactory implements ChannelPipelineFactory {
7+
...
8+
public ChannelPipeline getPipeline() throws Exception {
9+
ChannelPipeline pipeline = pipeline();
10+
11+
pipeline.addLast("decoder", new HttpRequestDecoder());
12+
pipeline.addLast("encoder", new HttpResponseEncoder());
13+
pipeline.addLast("deflater", new HttpContentCompressor());
14+
pipeline.addLast("handler", servletHandler); // will convert http request to servlet request
15+
return pipeline;
16+
}
17+
```
18+
19+
Then you will need a NettyServletHandler that converts Netty HttpRequest to a Servlet request:
20+
21+
```java
22+
public class NettyServletHandler extends SimpleChannelUpstreamHandler {
23+
...
24+
@Override
25+
public void messageReceived(ChannelHandlerContext context, MessageEvent event) throws Exception {
26+
HttpRequest request = (HttpRequest) event.getMessage();
27+
// Then get URL, method, headers, ... and pass the values to the Servlet container.
28+
}
29+
```
30+
31+
You will also need a method to start the server:
32+
33+
```java
34+
public void startServer(int port, String pipelineFactory) throws Exception {
35+
ServerBootstrap server = new ServerBootstrap(
36+
new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
37+
38+
if (pipelineFactory == null) { // If the user doesn't have a specific pipeline use the default one
39+
pipelineFactory = "org.xins.common.servlet.container.DefaultNettyServletPipelineFactory";
40+
}
41+
DefaultNettyServletPipelineFactory pipelineFactoryClass = (DefaultNettyServletPipelineFactory) Class.forName(pipelineFactory).newInstance();
42+
pipelineFactoryClass.setServletHandler(this);
43+
44+
server.setPipelineFactory(pipelineFactoryClass);
45+
46+
server.bind(new InetSocketAddress(port));
47+
}
48+
```

‎JavaScript/Babel/README.md‎

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
[![返回目录](https://i.postimg.cc/JzFTMvjF/image.png)](https://github.com/wx-chevalier/Awesome-CheatSheets)
2+
3+
# Babel CheatSheet
4+
5+
Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. Babel 主要通过以下方式来保证代码的运行:
6+
7+
- Transform syntax
8+
- Polyfill features that are missing in your target environment (through @babel/polyfill)
9+
10+
# Babel 7
11+
12+
## Configuration | 基本配置
13+
14+
```sh
15+
$ npm install --save-dev @babel/core @babel/cli @babel/preset-env
16+
$ npm install --save @babel/polyfill
17+
```
18+
19+
然后在根目录添加 babel.config.js 或者 `.babel.rc`:
20+
21+
```js
22+
const presets = [
23+
[
24+
'@babel/env',
25+
{
26+
targets: {
27+
edge: '17',
28+
firefox: '60',
29+
chrome: '67',
30+
safari: '11.1'
31+
},
32+
useBuiltIns: 'usage'
33+
}
34+
]
35+
];
36+
37+
module.exports = { presets };
38+
```
39+
40+
然后可以使用 Babel 命令行工具来转换文件:
41+
42+
```sh
43+
$ ./node_modules/.bin/babel src --out-dir lib
44+
```
45+
46+
也可以使用编程方式进行转换:
47+
48+
```js
49+
const babel = require('@babel/core');
50+
51+
babel.transform('code', optionsObject);
52+
```
53+
54+
值得一提的是,在 Babel 7 中,env 会根据浏览器的支持情况以及实际的代码使用来选择性的引入 Pollyfill 文件:
55+
56+
```js
57+
// 使用了如下代码
58+
Promise.resolve().finally();
59+
60+
// 会转化为如下形式
61+
require('core-js/modules/es.promise.finally');
62+
63+
Promise.resolve().finally();
64+
```
65+
66+
我们也可以在单个配置文件中,针对不同的环境定制不同的插件方案,Babel 默认按照如下方式加载环境变量:
67+
68+
```js
69+
process.env.BABEL_ENV || process.env.NODE_ENV || 'development';
70+
```
71+
72+
```json
73+
// .babel.rc
74+
{
75+
"presets": [
76+
...
77+
],
78+
"plugins": [
79+
...
80+
],
81+
"env": {
82+
"production": {
83+
"presets": ["react-optimize"],
84+
"plugins": [
85+
...
86+
]
87+
},
88+
"test": {
89+
"presets": [["env"], "react"]
90+
}
91+
}
92+
}
93+
```
94+
95+
我们也可以为不同的子目录设置不同的 Babel 配置:
96+
97+
```json
98+
{
99+
"babelrcRoots": [
100+
// Keep the root as a root
101+
".",
102+
103+
// Also consider monorepo packages "root" and load their .babelrc files.
104+
"./packages/*"
105+
]
106+
}
107+
```
108+
109+
## Modules
110+
111+
### polyfill
112+
113+
Babel includes a polyfill that includes a custom regenerator runtime and core-js.
114+
115+
This will emulate a full ES2015+ environment (no < Stage 4 proposals) and is intended to be used in an application rather than a library/tool. (this polyfill is automatically loaded when using babel-node).
116+
117+
This means you can use new built-ins like Promise or WeakMap, static methods like Array.from or Object.assign, instance methods like Array.prototype.includes, and generator functions (provided you use the regenerator plugin). The polyfill adds to the global scope as well as native prototypes like String in order to do this.
118+
119+
The polyfill is provided as a convienence but you should use it with @babel/preset-env and the useBuiltIns option so that it doesn't include the whole polyfill which isn't always needed. Otherwise, we would recommend you import the individual polyfills manually.
120+
121+
With webpack and When used alongside @babel/preset-env,, there are multiple ways to include the polyfills:
122+
123+
- If useBuiltIns: 'usage' is specified in .babelrc then do not include @babel/polyfill in either webpack.config.js entry array nor source. Note, @babel/polyfill still needs to be installed.
124+
125+
- If useBuiltIns: 'entry' is specified in .babelrc then include @babel/polyfill at the top of the entry point to your application via require or import as discussed above.
126+
127+
- If useBuiltIns key is not specified or it is explicitly set with useBuiltIns: false in your .babelrc, add @babel/polyfill directly to the entry array in your webpack.config.js.
128+
129+
我们也可以手动地载入这些 Polyfills:
130+
131+
```js
132+
require('@babel/polyfill');
133+
134+
import '@babel/polyfill';
135+
136+
// webpack.config.js
137+
module.exports = {
138+
entry: ['@babel/polyfill', './app/js']
139+
};
140+
```
141+
142+
### transform-runtime
143+
144+
A plugin that enables the re-use of Babel's injected helper code to save on codesize.
145+
146+
```sh
147+
$ npm install --save-dev @babel/plugin-transform-runtime
148+
149+
$ npm install --save @babel/runtime
150+
```
151+
152+
The transformation plugin is typically used only in development, but the runtime itself will be depended on by your deployed code.
153+
154+
```json
155+
// babel.rc
156+
{
157+
"plugins": [
158+
[
159+
"@babel/plugin-transform-runtime",
160+
{
161+
"corejs": false,
162+
"helpers": true,
163+
"regenerator": true,
164+
"useESModules": false
165+
}
166+
]
167+
]
168+
}
169+
```
170+
171+
Babel uses very small helpers for common functions such as \_extend. By default this will be added to every file that requires it. This duplication is sometimes unnecessary, especially when your application is spread out over multiple files.
172+
173+
This is where the @babel/plugin-transform-runtime plugin comes in: all of the helpers will reference the module @babel/runtime to avoid duplication across your compiled output. The runtime will be compiled into your build.
174+
175+
### register
176+
177+
You can use Babel is through the require hook. The require hook will bind itself to node's require and automatically compile files on the fly.
178+
179+
```sh
180+
npm install @babel/core @babel/register --save-dev
181+
```
182+
183+
```js
184+
// entry.js
185+
require('@babel/register');
186+
```
187+
188+
All subsequent files required by node with the extensions .es6, .es, .jsx, .mjs, and .js will be transformed by Babel.
189+
190+
## Plugins & Presets
191+
192+
### React
193+
194+
```sh
195+
$ npm install --save-dev @babel/preset-react
196+
```
197+
198+
```json
199+
{
200+
"presets": [
201+
[
202+
"@babel/preset-react",
203+
{
204+
"pragma": "dom", // default pragma is React.createElement
205+
"pragmaFrag": "DomFrag", // default is React.Fragment
206+
"throwIfNamespace": false // defaults to true
207+
}
208+
]
209+
]
210+
}
211+
```
File renamed without changes.

0 commit comments

Comments
 (0)