Commit 68ec0e9e authored by nanahira's avatar nanahira

first

parents
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
/tests
webpack.config.js
dist/*
build/*
*.js
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir : __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
stages:
- build
- deploy
variables:
GIT_DEPTH: "1"
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
.build-image:
stage: build
script:
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
build-x86:
extends: .build-image
tags:
- docker
variables:
TARGET_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86
build-arm:
extends: .build-image
tags:
- docker-arm
variables:
TARGET_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
.deploy:
stage: deploy
tags:
- docker
script:
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86
- docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
- docker manifest create $TARGET_IMAGE --amend $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86 --amend
$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
- docker manifest push $TARGET_IMAGE
deploy_latest:
extends: .deploy
variables:
TARGET_IMAGE: $CI_REGISTRY_IMAGE:latest
only:
- master
deploy_branch:
extends: .deploy
variables:
TARGET_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
stages:
- install
- build
- deploy
variables:
GIT_DEPTH: "1"
npm_ci:
stage: install
tags:
- linux
script:
- npm ci
artifacts:
paths:
- node_modules
.build_base:
stage: build
tags:
- linux
dependencies:
- npm_ci
build:
extends: .build_base
script: npm run build
artifacts:
paths:
- dist/
upload_to_minio:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- aws s3 --endpoint=https://minio.momobako.com:9000 sync --delete dist/ s3://nanahira/path
only:
- master
stages:
- install
- build
- deploy
variables:
GIT_DEPTH: "1"
npm_ci:
stage: install
tags:
- linux
script:
- npm ci
artifacts:
paths:
- node_modules
.build_base:
stage: build
tags:
- linux
dependencies:
- npm_ci
build:
extends:
- .build_base
script:
- npm run build
artifacts:
paths:
- dist/
unit-test:
extends:
- .build_base
script:
- npm run test
deploy_npm:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- apt update;apt -y install coreutils
- echo $NPMRC | base64 --decode > ~/.npmrc
- npm publish . --access public && curl -X PUT "https://registry-direct.npmmirror.com/$(cat package.json | jq '.name' | sed 's/\"//g')/sync?sync_upstream=true" || true
only:
- master
/install-npm.sh
.git*
/data
/output
/config.yaml
.idea
.dockerignore
Dockerfile
/src
/coverage
/tests
/dist/tests
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:lts-trixie-slim as base
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/log/*
WORKDIR /usr/src/app
COPY ./package*.json ./
FROM base as builder
RUN npm ci && npm cache clean --force
COPY . ./
RUN npm run build
FROM base
ENV NODE_ENV production
RUN npm ci && npm cache clean --force
COPY --from=builder /usr/src/app/dist ./dist
CMD [ "npm", "start" ]
The MIT License (MIT)
Copyright (c) 2021 Nanahira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# nfkit
Common kits
\ No newline at end of file
export * from './src/workflow';
#!/bin/bash
npm i --save-exact --save-dev eslint@8.22.0
npm install --save-dev \
@types/node \
typescript \
'@typescript-eslint/eslint-plugin@^6.0.0' \
'@typescript-eslint/parser@^6.0.0 '\
'eslint-config-prettier@^9.0.0' \
'eslint-plugin-prettier@^5.0.0' \
prettier \
jest \
@types/jest \
ts-jest \
rimraf
This diff is collapsed.
{
"name": "nfkit",
"description": "Common kits",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"compile:cjs": "esbuild index.ts --outfile=dist/index.cjs --bundle --sourcemap --platform=node --target=es2019 --external:js-base64",
"compile:esm": "esbuild index.ts --outfile=dist/index.mjs --bundle --sourcemap --platform=neutral --target=esnext --external:js-base64",
"compile:types": "tsc --emitDeclarationOnly --declaration",
"build": "rimraf dist && npm run compile:cjs && npm run compile:esm && npm run compile:types",
"test": "jest --passWithNoTests",
"start": "node dist/index.js"
},
"repository": {
"type": "git",
"url": "https://code.moenext.com/nanahira/nfkit.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"keywords": [],
"bugs": {
"url": "https://code.moenext.com/nanahira/nfkit/issues"
},
"homepage": "https://code.moenext.com/nanahira/nfkit",
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "tests",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"devDependencies": {
"@types/jest": "^30.0.0",
"@types/node": "^24.9.1",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"esbuild": "^0.25.11",
"esbuild-register": "^3.6.0",
"eslint": "8.22.0",
"eslint-config-prettier": "^9.1.2",
"eslint-plugin-prettier": "^5.5.4",
"jest": "^30.2.0",
"prettier": "^3.6.2",
"rimraf": "^6.0.1",
"ts-jest": "^29.4.5",
"typescript": "^5.9.3"
}
}
// ========== 类型工具 ==========
type AwaitedLike<T> = T extends Promise<infer R> ? R : T;
type AnyFn = (...args: any[]) => any;
type Chain<T> = Promise<AwaitedLike<T>> &
(T extends AnyFn
? (...a: Parameters<T>) => Chain<AwaitedLike<ReturnType<T>>>
: {}) & {
[K in keyof T]: T[K] extends (...a: infer A) => infer R
? (...a: A) => Chain<AwaitedLike<R>>
: Chain<AwaitedLike<T[K]>>;
};
// ========== 核心:不可变节点 ==========
class Node {
private memo?: Promise<any>;
constructor(
private readonly source: any, // 只有根节点有意义;子节点可复用根的 source
private readonly parent: Node | null, // 前缀
private readonly op: ((x: any) => any) | null, // 从 parent 结果到本节点结果的变换
) {}
run(): Promise<any> {
if (!this.memo) {
this.memo = (async () => {
if (!this.parent) {
// 根:把 source 解一次即可
return await this.source;
}
// 先拿到父节点结果(会记忆化)
const base = await this.parent.run();
// 应用本节点的变换(允许返回同步或异步)
return this.op ? this.op(base) : base;
})();
}
return this.memo;
}
// 工厂:衍生子节点(分叉 = 复用同一个 parent)
child(op: (x: any) => any): Node {
return new Node(this.source, this, op);
}
// 拿到“相同节点”的句柄(显式 fork)
fork(): Node {
return new Node(this.source, this.parent, this.op);
}
}
// ========== 对外 API:workflow ==========
export function workflow<T>(source: T | Promise<T>): Chain<T> {
const root = new Node(source, null, null);
return makeProxy<T>(root) as any;
}
// ========== 代理层:把 Node 暴露成链式 API ==========
function makeProxy<T>(node: Node): Chain<T> {
const runOnce = () => node.run(); // 记忆化在 Node 内部
const rootHandler: ProxyHandler<any> = {
get(_t, prop) {
// 结束信号:所有 then/catch/finally 复用同一个 Promise
if (prop === 'then')
return (res: any, rej?: any) => runOnce().then(res, rej);
if (prop === 'catch') return (rej: any) => runOnce().catch(rej);
if (prop === 'finally') return (fin: any) => runOnce().finally(fin);
// 访问一个属性:生成一个“Get”子节点,但不执行
return linkProxy(prop);
},
// 支持对“当前值”直接二次调用: foo()()
apply(_t, _this, argArray) {
const child = node.child((cur: any) => {
if (typeof cur !== 'function')
throw new TypeError('Current value is not callable');
return cur(...argArray);
});
return makeProxy<any>(child);
},
};
const linkProxy = (prop: PropertyKey) =>
new Proxy(function () {}, {
// 把 “.foo(…args)” 记录为 Call
apply(_t, _this, argArray) {
const child = node.child((cur: any) => {
const fn = (cur as any)[prop];
if (typeof fn !== 'function') {
throw new TypeError(`${String(prop)} is not a function`);
}
return fn.apply(cur, argArray);
});
return makeProxy<any>(child);
},
// 把 “.bar” 记录为 Get;继续深入时在这个 Get 的结果上再处理
get(_t, next) {
if (next === 'then')
return (r: any, j?: any) =>
node
.child((cur: any) => (cur as any)[prop])
.run()
.then(r, j);
if (next === 'catch')
return (rej: any) =>
node
.child((cur: any) => (cur as any)[prop])
.run()
.catch(rej);
if (next === 'finally')
return (fin: any) =>
node
.child((cur: any) => (cur as any)[prop])
.run()
.finally(fin);
const child = node.child((cur: any) => (cur as any)[prop]);
return (makeProxy as any)(child)[next];
},
});
return new Proxy(function () {}, rootHandler) as any;
}
This diff is collapsed.
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts",
"test/**/*.ts",
"tests/**/*.ts"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment