命令行参数

以下说明的是命令行参数类型

必填

  • 必填的参数
    • 可以是位置
    • 可以是以长选项、短选项的形式

位置示例

1
2
3
4
5
6
7
8
9
new Command({
'name': 'app',
'des': 'simple app test'
}).addArgs({
'name': 'name',
'location': true,

'format': 'string'
})
阅读全文 »

环境

  • nextjs version: 14.2.8

  • antd version: 5.20.5

问题及解决

问题

开始 NextJS 的入手 demo, npx install antd --save 后,在 pages/index.tsc 中使用:

1
2
3
4
5
6
7
8
9
10
import { Button } from 'antd';


export default function Page() {

<div className="App">
<Button type="primary">Button</Button>
</div>
return <h1>Hello, Next.js!</h1>
}

npm run build 报错

如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   Collecting page data  ./Users/ayal/us-dev/projects/next/cms/node_modules/rc-util/es/omit.js:1
export default function omit(obj, fields) {
^^^^^^

SyntaxError: Unexpected token 'export'
at internalCompileFunction (node:internal/vm:128:18)
at wrapSafe (node:internal/modules/cjs/loader:1279:20)
at Module._compile (node:internal/modules/cjs/loader:1331:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
at Module.load (node:internal/modules/cjs/loader:1205:32)
at Module._load (node:internal/modules/cjs/loader:1021:12)
at Module.require (node:internal/modules/cjs/loader:1230:19)
at mod.require (/Users/ayal/us-dev/projects/next/cms/node_modules/next/dist/server/require-hook.js:65:28)
at require (node:internal/modules/helpers:179:18)
at 3998 (/Users/ayal/us-dev/projects/next/cms/.next/server/pages/index.js:1:788)

原因及解决

翻了一下版本内容,大概原因是 构建工具无法识别 es 模块,需要将 antd (其他第三方库如果遇到该问题处理方法类似)加入
next.config.mjs -> nextConfig.transpilePackages 中, 如下:

1
2
3
4
5
6
7
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: [ "antd", "@ant-design", "rc-util" ],
};

export default nextConfig;

参考

背景

最近工作中遇到关于时间戳与时间字符串的互相转换,期望不使用第三方库实现功能,在此做一下解决方案记录;

Q1: 为什么不是直接使用 new Date()

A1: 对于北京时间来说,我们都知道偏移量是 UTC+8,且没有冬令时夏令时问题,时间戳与字符串互转,new Date()即可解决问题,但是对于存在冬令时夏令时的时区来说,比如 America/Los_Angeles,每年 3 月 10 号 02:00 时之后,偏移量为 UTC-7,每年 11 月 03 2 点恢复为 UTC-8,不同时区,冬令时和夏令时的节点不同。收集所有时区的不同时间段的数据,可以应对任意时区,任意时间戳对应的偏移量;

第三方库可以快速解决此问题,参考 moment.js,此处分享的是不使用第三方库的思路与做法。

1
npm install moment-timezone --save 

夏令时和冬令时是什么

高纬度和中纬度的许多国家为了充分利用夏季的太阳光照,节约照明用电,而又不变动作息时间,实行夏令时。即在夏季到来前,把时针拨快一小时,到下半季秋季来临前,再把时针拨回一小时。实行夏令时的日期一般是:4-9月(北半球)10-3月(南半球)。

阅读全文 »

TCP

TCP 粘包是什么?有什么解决方案?尝试基于 TCP 设计一种解决粘包问题的双向块传输协议(Chunk Transport Protocol),并使用 TypeScript + NodeJS 实现一份使用 TCP 长连接的版本。

A:TCP 是一种面向字节流、具有可靠性、有序的、速度慢特性的协议。

TCP 粘包是一种传输过程中的现象,表现为发送方的后一包数据的头部接着前一包数据的尾部,称作TCP粘包;

为什么会出现这种现象?因为 TCP 是一种面向字节流的协议,发送方的数据可能被分割成多个份数据进行传输,IP数据包的负载大概为 1460 字节,当数据远远小于 1460 字节时,为了避免浪费网络 io,TCPNagle 算法开启时,当前一份数据小于 1460 字节时,此时会等待第二份数据包再进行发送,这就是TCP粘包。

当然,开启 Nagle 算法时,如果迟迟等不到第二份数据包,等待超时,一般为 200 ms,此时也会立刻发送这一份数据。

解决方案:

  • 固定包长的数据包:在发送方分包分片,不足的包长用特殊字符代替,在接收方重新组装包片;
  • 以指定字符(串)为包的结束标志,如换行符、空格符等;
  • 用包头和包体的格式,在包头说明包体有多大;
阅读全文 »

背景

当拿到一台新的服务器时,我们第一件事可以做什么?

  • 检查配置

  • 设置免密登录

  • 禁止密码登录、禁止 root 远程登录

阅读全文 »

mongoexport 导出时可以规定需要导出的字段 mongodb

官方文档介绍如下:

1
2
3
4
5
6
7
8
9
10
--fields=<field1[,field2]>, -f=<field1[,field2]>
Specifies a field or fields to include in the export. Use a comma separated list of fields to specify multiple fields.

If any of your field names include white space, use quotation marks to enclose the field list. For example, if you wished to export two fields, phone and user number, you would specify --fields "phone,user number".

For csv output formats, mongoexport includes only the specified field(s), and the specified field(s) can be a field within a sub-document.

For JSON output formats, mongoexport includes only the specified field(s) and the _id field, and if the specified field(s) is a field within a sub-document, the mongoexport includes the sub-document with all its fields, not just the specified field within the document.

See: Export Data in CSV Format using --fields option for sample usage.

但我的业务是导出所有字段,除了_id,用 –fields 太过麻烦,一是我也不知道有什么字段;二是在字段很多的情况下就难处理;

查了一下,在https://stackoverflow.com/questions/12976145/mongoexport-without-id-field 找到了答案:

阅读全文 »
0%