current position:Home>PipyJS - Functional web programming language

PipyJS - Functional web programming language

2022-09-23 09:25:14InfoQ

这篇文章主要介绍 
Pipy
 以及强大的 PipyJS - There is no garbage collection overhead、Lightweight functional JavaScript 解释引擎.

Pipy 概览

Pipy 是一个
开源的
、轻量级、高性能、模块化、Programmable cloud-native network stream processor,Suitable for edge routing、Load balancing and proxy solutions、API 网关、静态 HTTP 服务器、服务网格边车、Strategy as code and other application scenarios.
The technical jargon is a bit too much,我们继续深入了解一下.

轻量级

Pipy Executable files only 10MB 左右,没有任何外部依赖,Runs with very little memory.

高性能

Pipy 是使用 C++ and based on async I/O 库 Asio 开发的.

模块化

Pipy The core uses a modular design,Provides a large number of reusable small modules(过滤器).These filters can be assembled into pipes,Network data flows through these pipes and is processed.

流处理器

Pipy Abstract network stream data bytes into events,Manipulate network flows in an event-driven manner.Provides an abstraction for event-driven pipelines,Pipes use input streams、Perform user-defined transformations and output streams.

可编程

Pipy The underlying details are shielded from the outside,Provides a puzzle-like programmatic approach to achieving business goals.Pipy is through the powerful PipyJS 实现的,It is also what we will introduce in detail next.

为什么使用 JavaScript

Just like the title says,PipyJS 是基于 JavaScript 的.Pipy Decided to use something similar JavaScript Scripting languages ​​are there for a reason:
  • JavaScript 可能是
    The most widely spoken language in the world
  • 它的语法有 C 和 Java 的影子,So it is not unfamiliar to most programmers
  • Pipy 作为流处理器,This means that there is a lot of data transformation and reorganization work.通过 JavaScript Provides a lot of powerful syntactic sugar that can be easily handled,比如
    展开语法
    解构赋值
    、以及数组的 
    map()
    reduce()
     等操作
  • JSON As the most widely used data format on the web,JavaScript Provides native support for easy operation
  • Last and most important,JavaScript 包含了“函数式编程”范式
Pipy It's like some pipes connected to each other.Each pipeline consists of a series of
过滤器
组成,Each filter is like taking input and returning a specific result
函数
.for this design pattern,Suppose your goal is to keep it coherent and simple,Functional programming languages ​​are the best fit Pipy 的.

PipyJS 介绍

Pipy 通过自研的 PipyJS 组件来解析 JavaScript,该组件是 Pipy 代码库的一部分,But don't rely on it.
PipyJS is a small and embeddable JavaScript 引擎,专为高性能而设计,There is no garbage collection overhead.它支持 ECMAScript 标准的子集,And boldly expanded in some ways.Pipy 拥抱
纯函数
式 JavaScript,在 PipyJS 中,
Everything is an expression
.

数据类型

与标准 JavaScript 一样, PipyJS 支持 4 basic types and structures of data 
对象
 类型.
  • 基本类型Undefined:Unique value when the variable is not initialized 
    undefinde
    .布尔类型 Boolean:
    true
     和 
    false
    .数字类型 Number:64 位双精度浮点数,如 
    123
    0xabc
    3.1415926
    1e6
    .字符串类型 String:Unicode 字符序列.
  • 结构化数据类型Null 对象:唯一值 
    null
    .用户定义的 POD(plain old data) ,如 
    { x: 1.2, y: 300 }
    内置对象:比如 
    Array
    RegExp
    .函数:比如 
    (a, b) => a * a + b * b
    .

操作符

PipyJS All standards are supported JavaScript 操作符,包括一些在 ES2020 才引入的,比如 
可选链操作符
 和 
空值合并运算符
.

全局变量

全局变量在 PipyJS 中也叫做 
上下文变量
.这里的
上下文
The concept is equivalent to that in high concurrency network programming
连接
,Each connection is independent of each other
状态
.在 PipyJS 中,These isolated states are kept in global variables for convenience,That's why we sometimes call it
上下文变量
.It's also easy to understand why PipyJS Global variables and standards JavaScript 不同,Simply put, the value is not the same in different connections.这点上,有点像
线程本地存储
Global variables via built-in functions 
pipy()
 来定义,It is usually the first function called in the script.
pipy({
 _myGlobalVariable: undefined
})
为了方便起见,Global variables use the underscore as the first character of the variable name,Although not language-enforced.
The scope of global variables is a single file or
模块
,Can pass between multiple modules 
export()
 和 
import()
 来共享.
// file A
pipy().export('namespace-1', {
 __myGlobalVariable: undefined
})

// file B
pipy().import({
 __myGlobalVariable: 'namespace-1'
})
为了方便起见,Exported global variable names start with a double underscore,但不强制.

本地变量

在 PipyJS 中,We use function parameters nested within the scope of the local variable function as local variables.
void ((
 x, y, z, // declare local variables as function arguments
) => (
 x = 0,
 y = 0,
 z = 0 // initialize and use the variables in the function body
))() // Don't miss the () to invoke the function right away!
Suppose you want to return the result after the above expression is executed,You need to delete the beginning 
void

分支

在 PipyJS 中,Everything is an expression.There are no code blocks or flow control,不能写 
if
 或者 
for
 语句.But that doesn't mean you can't write branches and loops,But just in a different form:
函数式
.
Simple branches can use logical operators 
&&
.
res.status === 200 && (_result = 'OK', console.log('Success.'))

// That's equivalent to:
if (res.status === 200) {
 _result = 'OK';
 console.log('Success.');
}
注意:上面是 PipyJS 分支语法,下面是标准 JavaScript 语法(下同).
Multiple choice branches can combine logical operators 
&&
 和 
||
 组合使用.
(res.status === 200) && (
 _result = 'OK'
) ||
(res.status === 404) && (
 _result = 'Not found'
) || (
 _result = ''
)

// That's equivalent to:
if (res.status === 200) {
 _result = 'OK';
} else if (res.status === 404) {
 _result = 'Not found';
} else {
 _result = '';
}

循环

可以使用 
Array.forEach()
 to implement a simple bounded loop.
new Array(100).fill(0).forEach(
 (_, i) => (
 console.log(i)
 )
)

// That's equivalent to:
for (let i = 0; i < 100; i++) {
 console.log(i);
}
或者,For general conditional loops,可以使用
内置的
函数&nbsp;
repeat()
.
void ((
 n, i
) => (
 n = i = 1,
 repeat(
 () => (
 n *= i,
 i += 1,
 i <= 10
 )
 )
))()

// That's equivalent to:
let n = 1, i = 1;
while (i <= 10) {
 n *= i;
 i += 1;
}

派生 ECMAScript

PipyJS Designed for speed,Its structure is critical for writing high-performance network stream processing logic.下面重点介绍 PipyJS 与标准 ECMAScript different or not implemented:
  • 面向对象编程(OOP)结构 - There are no user-defined types or constructors,没有
    原型
    系统
  • 控制流
  • 关键字
    break
    &nbsp;,&nbsp;
    case
    &nbsp;,&nbsp;
    catch
    &nbsp;,&nbsp;
    continue
    &nbsp;,&nbsp;
    debugger
    &nbsp;,&nbsp;
    default
    &nbsp;,&nbsp;
    do
    &nbsp;,&nbsp;
    else
    &nbsp;,&nbsp;
    finally
    &nbsp;,&nbsp;
    function
    &nbsp;,&nbsp;
    for
    &nbsp;,&nbsp;
    if
    &nbsp;,&nbsp;
    return
    &nbsp;,&nbsp;
    switch
    &nbsp;,&nbsp;
    throw
    &nbsp;,&nbsp;
    try
    &nbsp;,&nbsp;
    while
    &nbsp;,&nbsp;
    with
    &nbsp;,&nbsp;
    yield
    &nbsp;,&nbsp;
    class
    &nbsp;,&nbsp;
    import
    &nbsp;,&nbsp;
    export
    &nbsp;,&nbsp;
    extends
    &nbsp;,&nbsp;
    static
    &nbsp;,&nbsp;
    super
  • 类型系统
    BigInt
    &nbsp;and&nbsp;
    SymbolStrings
    &nbsp;内部存储为&nbsp;
    UTF-8
    ,Can be used in scripts&nbsp;
    UTF-32
    &nbsp;来访问.比如&nbsp;
    &quot;&quot;.length
    &nbsp;在标准 JavaScript 中是 2,但在 PipyJS 中是 1
  • 变量 - There are no keywords for declaring variables&nbsp;
    var
    &nbsp;或者&nbsp;
    let
尽管 PipyJS derived from standard ECMAScript,但是通过
函数式
mechanism to fill in the gaps.

如何使用

现在你应该对 PipyJS 有了一定的了解,Next we look at how to write code and run it.
首先
下载
Binaries for the corresponding platform,将下面的代码保存为 ”hello.js“.
pipy()

.listen(8080)
 .serveHTTP(
 new Message('Hi, there!\n')
 )
Then execute the script with the binary you just downloaded.
pipy hello.js

2022-02-23 14:14:33.315 [INF] [config]
2022-02-23 14:14:33.316 [INF] [config] Module /hello.js
2022-02-23 14:14:33.316 [INF] [config] ================
2022-02-23 14:14:33.316 [INF] [config]
2022-02-23 14:14:33.316 [INF] [config] [Listen on 8080 at 0.0.0.0]
2022-02-23 14:14:33.316 [INF] [config] ----->|
2022-02-23 14:14:33.316 [INF] [config] |
2022-02-23 14:14:33.316 [INF] [config] serveHTTP
2022-02-23 14:14:33.316 [INF] [config] |
2022-02-23 14:14:33.316 [INF] [config] <-----|
2022-02-23 14:14:33.316 [INF] [config]
2022-02-23 14:14:33.316 [INF] [listener] Listening on port 8080 at 0.0.0.0
This script will listen&nbsp;
8080
&nbsp;端口,并返回信息&nbsp;
Hi, there!
.
curl http://localhost:8080
Hi, there!

总结

Pipy
&nbsp;是有&nbsp;
Flomesh
&nbsp;开源的、Extremely fast and lightweight network traffic processor,适用于多种场景.Pipy Actively developed and maintained by full-time committers and contributors,Although still an early version,But it has been battle-tested in production environments of multiple commercial customers.its creator and maintainer&nbsp;
Flomesh.cn
&nbsp;提供了以 Pipy as the core business-grade solution.
这篇文章对 Pipy A brief overview and high-level introduction are given.在&nbsp;
GitHub 页面
Tutorials and documentation can be found on ,也可以通过 Pipy admin console interface to access.Pipy The community welcomes development contributions、Try a specific scenario、Or provide an introduction and feedback.

copyright notice
author[InfoQ],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/266/202209230910157654.html

Random recommended