Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP TypeScript code refactor #1128

Closed
wants to merge 86 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
34a20ab
support TypeScript
trim21 May 2, 2023
8b52976
diff
trim21 May 2, 2023
3e94577
Merge remote-tracking branch 'upstream/master' into ts
trim21 May 2, 2023
c21c335
fix lint
trim21 May 2, 2023
9844650
run on nodejs20
trim21 May 2, 2023
c03390b
fix ci
trim21 May 2, 2023
dcc4191
eslinl rule diff
trim21 May 2, 2023
56af6f2
add npm run build to "prepublishOnly"
trim21 May 2, 2023
d6289be
add eslint rule to check import/export
trim21 May 2, 2023
8edcee8
add eslint rule to disable default export
trim21 May 2, 2023
e1ad697
simplify eslint rule
trim21 May 2, 2023
ad13a82
Update CONTRIBUTING.md
trim21 May 2, 2023
6783ac0
tsconfig
trim21 May 2, 2023
8944f93
declarationMap
trim21 May 2, 2023
73c2144
migrate
trim21 May 2, 2023
ca09d53
fix
trim21 May 2, 2023
346e9ec
type
trim21 May 2, 2023
955efa8
refactor
trim21 May 2, 2023
97bd96b
assert
trim21 May 2, 2023
4dd8ae4
refactor
trim21 May 2, 2023
8df43cd
refactor
trim21 May 2, 2023
f6551a6
set readonly
trim21 May 2, 2023
21e070a
migrate helpers.js to ts
trim21 May 5, 2023
37e6528
no need @internal on un-exported variable
trim21 May 5, 2023
af73d77
remove qs
trim21 May 5, 2023
367ae09
export all
trim21 May 5, 2023
1a23207
timeout
trim21 May 5, 2023
c9d7977
fix pipesetup
trim21 May 5, 2023
a7edf5e
dead code
trim21 May 5, 2023
be1c8de
use type narrow
trim21 May 5, 2023
e77d3e5
types
trim21 May 5, 2023
be6135d
remove type without actually value
trim21 May 5, 2023
4fc5d37
region
trim21 May 5, 2023
457594a
export type
trim21 May 5, 2023
13c4ccb
no need to split
trim21 May 5, 2023
411b222
Merge branch 'master' into ts/helpers
trim21 May 5, 2023
01e5281
type check
trim21 May 5, 2023
9959a78
use enum
trim21 May 5, 2023
ebb50aa
filename
trim21 May 5, 2023
a45f7e0
diff
trim21 May 5, 2023
ca38e38
deprecated type
trim21 May 5, 2023
deb282e
helper file
trim21 May 5, 2023
767303e
fix
trim21 May 5, 2023
f9fe211
Update src/helpers.ts
trim21 May 5, 2023
4f3629a
perfer const in typescript
trim21 May 5, 2023
2815daa
fix pipesetup type
trim21 May 5, 2023
3f4cdf7
type rename
trim21 May 6, 2023
53d161f
fix type
trim21 May 6, 2023
9fe8a3d
fix type
trim21 May 6, 2023
d6cb219
ci
trim21 May 6, 2023
7d0f34c
chore: upgrade deps
trim21 May 6, 2023
5aebc4d
upgrade old deps
trim21 May 6, 2023
d2052b5
ignore pnpm lock
trim21 May 6, 2023
8195e79
speedup build
trim21 May 6, 2023
1712bd5
bucket and object js required
trim21 May 6, 2023
aafe0fa
fix request header
trim21 May 6, 2023
401aded
fix enc headers
trim21 May 6, 2023
826eda1
fix enc type
trim21 May 6, 2023
4ec735a
import first
trim21 May 6, 2023
e98ed16
import as es
trim21 May 6, 2023
d3cd80d
move to internal
trim21 May 6, 2023
c3899a1
Merge branch 'ts/helpers' into refactor-client
trim21 May 6, 2023
3dadd5e
type name
trim21 May 6, 2023
6b83b7d
fix arg name
trim21 May 6, 2023
cad3f60
fix metadata
trim21 May 6, 2023
535cbb8
fix
trim21 May 6, 2023
76afbc3
move files
trim21 May 6, 2023
1561886
fix
trim21 May 6, 2023
96b07de
fix
trim21 May 6, 2023
ab54a82
type-name
trim21 May 6, 2023
b00873e
use eventemitter3
trim21 May 9, 2023
e68ddfd
Merge branch 'master' into refactor-client
trim21 May 13, 2023
ea7500a
fix
trim21 May 13, 2023
38adaf0
remove qs
trim21 May 13, 2023
fd7a065
Merge branch 'master' into refactor-client
trim21 May 18, 2023
09a74f2
tsc
trim21 May 18, 2023
08b999b
fix fs/promise in nodejs 12
trim21 May 18, 2023
a99fc6e
dupe code
trim21 May 18, 2023
f659a06
diff
trim21 May 18, 2023
0460739
diff
trim21 May 18, 2023
9e5f09a
merge
trim21 May 21, 2023
ce87fb8
Merge branch 'master' into refactor-client
trim21 May 21, 2023
2781c16
diff
trim21 May 21, 2023
68650a8
Merge remote-tracking branch 'upstream/master' into refactor-client
trim21 May 21, 2023
63c715c
Merge branch 'master' into refactor-client
trim21 Nov 5, 2023
99386cd
fix
trim21 Nov 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# copy this file as .env for testing env

SERVER_ENDPOINT=""
ACCESS_KEY=""
SECRET_KEY=""
65 changes: 61 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,35 @@ module.exports = {
mocha: true,
es6: true,
},
ignorePatterns: ['src/test/*.*', 'examples/**/*'],
overrides: [],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier', // This should be the last entry.
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'simple-import-sort'],
plugins: ['@typescript-eslint', 'simple-import-sort', 'unused-imports', 'import', 'unicorn'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 8,
ecmaVersion: 'latest',
},
ignorePatterns: ['examples/**/*', 'dist/**/*'],
settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts'],
},
// we need to config this so import are fully specified
// otherwise @babel/register can't handle TypeScript files
'import/resolver': {
typescript: {
alwaysTryTypes: false,
extensionAlias: {
'.js': ['.js'],
},
extensions: ['.ts', '.js', '.mjs'],
fullySpecified: true,
enforceExtension: true,
},
},
},
rules: {
'no-console': ['error'],
Expand All @@ -34,6 +51,9 @@ module.exports = {
'rest-spread-spacing': 0, // ["error", "never"],
'no-multi-spaces': 0, // ["warn", { ignoreEOLComments: false }],

// import node stdlib as `node:...`
// don't worry, babel will remove these prefix.
'unicorn/prefer-node-protocol': 'error',
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
indent: 'off',
Expand Down Expand Up @@ -67,5 +87,42 @@ module.exports = {

'no-extra-parens': 0,
'@typescript-eslint/no-extra-parens': 0,
'import/namespace': 'error',
'import/default': 'error',
'import/named': 'error',
// default export confuse esm/cjs interop
'import/no-default-export': 'error',
'import/extensions': ['error', 'always'],
'@typescript-eslint/consistent-type-imports': [
'error',
{
prefer: 'type-imports',
fixStyle: 'separate-type-imports',
},
],
'unused-imports/no-unused-imports': 'error',
'import/no-amd': 'error',
},
overrides: [
{
files: ['./src/**/*', './tests/**/*'],
rules: {
'import/no-commonjs': 'error',
},
},
{
files: ['./tests/**/*'],
rules: {
'no-empty-function': 0,
'@typescript-eslint/no-empty-function': 0,
},
},
{
files: ['./types/**/*'],
rules: {
'@typescript-eslint/no-unused-vars': 0,
'@typescript-eslint/no-explicit-any': 0,
},
},
],
}
5 changes: 3 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ jobs:

- run: npm ci

- run: npm run compile
- run: npm run browserify
- run: npm run type-check

- run: npm run build
4 changes: 2 additions & 2 deletions .github/workflows/nodejs-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ on:
- master

jobs:
build:
test:
name: Test on node ${{ matrix.node_version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 3
matrix:
node_version: [12.x, 14.x, 16.x, 17.x, 18.x, 19.x]
node_version: [12.x, 14.x, 16.x, 17.x, 18.x, 20.x]
os: [windows-latest]
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
max-parallel: 3
matrix:
node_version: [12.x, 14.x, 16.x, 17.x, 18.x, 19.x]
node_version: [12.x, 14.x, 16.x, 17.x, 18.x, 20.x]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Logs
logs
*.log
.vscode/
.idea/
.DS_Store
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

/.env
/dist/
yarn.lock
.yarn/
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run type-check
npm run lint-staged
7 changes: 7 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
spec: 'tests/**/*.js',
exit: true,
reporter: 'spec',
ui: 'bdd',
require: ['dotenv/config', 'source-map-support/register', './babel-register.js'],
}
14 changes: 11 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
### Setup your minio-js Github Repository
Fork [minio-js upstream](https://github.com/minio/minio-js/fork) source repository to your own personal repository.

MinIO Javascript library uses gulp for its dependency management http://gulpjs.com/

```bash
$ git clone https://github.com/$USER_ID/minio-js
$ cd minio-js
$ npm install
$ gulp
$ npm test
$ npm build
...
```

Expand All @@ -21,3 +20,12 @@ $ gulp
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request

### Style Guide

We are currently migrating from JavaScript to TypeScript, so **All Source should be written in [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)**

That means only use nodejs `require` in js config file like `.eslintrc.js`

You should always fully specify your import path extension,
which means you should write `import {} from "errors.ts"` for `errors.ts` file, do not write `import {} from "errors.js"`.
18 changes: 14 additions & 4 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# For maintainers only
MinIO JS SDK uses [npm4+](https://www.npmjs.org/) build system.
Development of MinIO JS SDK require nodejs14+ and [npm7+](https://www.npmjs.org/).

## Responsibilities
Go through [Maintainer Responsibility Guide](https://gist.github.com/abperiasamy/f4d9b31d3186bbd26522).
Expand All @@ -11,12 +11,22 @@ $ git clone [email protected]:minio/minio-js
$ cd minio-js
```

### Build and verify
Run `install` gulp task to build and verify the SDK.
```sh
### Install deps
```shell
$ npm install
```

### Testing
```shell
$ npm test
```

### Build
Build project for release
```sh
$ npm run build
```

## Publishing new release
Edit `package.json` version and all other files to the latest version as shown below.
```sh
Expand Down
17 changes: 17 additions & 0 deletions babel-register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// fix babel register doesn't transform TypeScript
//
// https://github.com/babel/babel/issues/8962#issuecomment-443135379

// eslint-disable-next-line @typescript-eslint/no-var-requires,import/no-commonjs
const register = require('@babel/register')

register({
extensions: ['.ts', '.js'],
plugins: [
'@upleveled/remove-node-prefix', // lower version of node (<14) doesn't support require('node:fs')
],
presets: [
['@babel/preset-typescript', { allExtensions: true }],
['@babel/preset-env', { targets: { node: 'current' }, modules: 'cjs' }],
],
})
122 changes: 122 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* eslint-disable no-console */
import { execSync } from 'node:child_process'
import * as fs from 'node:fs'
import * as fsp from 'node:fs/promises'
import * as path from 'node:path'

import * as babel from '@babel/core'
import * as fsWalk from '@nodelib/fs.walk'

const pkg = JSON.parse(fs.readFileSync('package.json').toString())

/**
* @param {'esm'|'cjs'} module
*/
function options(module) {
return {
sourceMaps: 'inline',
plugins: [
['@upleveled/remove-node-prefix'],
[
'replace-import-extension',
{
extMapping: {
'.ts': extMap[module],
'.js': extMap[module],
},
},
],
[
'babel-plugin-transform-replace-expressions',
{
replace: {
'process.env.MINIO_JS_PACKAGE_VERSION': JSON.stringify(pkg.version),
},
},
],
],
presets: [
['@babel/env', { targets: { node: '8' }, modules: module === 'esm' ? false : module }],
['@babel/preset-typescript'],
],
}
}

const extMap = { cjs: '.js', esm: '.mjs' }

async function buildFiles({ files, module, outDir }) {
console.log(`building for ${module}`)
execSync(`npx tsc --outDir ${outDir}`, { stdio: 'inherit' })

const opt = options(module)
for (const file of files) {
if (!file.dirent.isFile()) {
continue
}

if (file.path.endsWith('.d.ts')) {
continue
}

const outFilePath = path.join(outDir, path.relative('src/', file.path))
const outDirPath = path.dirname(outFilePath)

await fsp.mkdir(outDirPath, { recursive: true })

try {
const result = await babel.transformAsync(fs.readFileSync(file.path).toString(), {
filename: file.path,
...opt,
})

const distCodePath = outFilePath.replace(/\.[tj]s$/g, extMap[module])

fs.writeFileSync(distCodePath, result.code)
} catch (e) {
console.error(`failed to transpile ${file.path}`)
throw e
}
}
}

async function main() {
await fsp.rm('dist', { recursive: true, force: true })

const entries = fsWalk.walkSync('src/')
await buildFiles({
files: entries,
module: 'cjs',
outDir: './dist/main/',
})

await buildFiles({
files: entries,
module: 'esm',
outDir: './dist/esm/',
})

for (const file of fsWalk.walkSync('dist/esm/')) {
if (file.dirent.isDirectory()) {
continue
}

if (!file.path.endsWith('.d.ts')) {
continue
}

const fileContent = fs.readFileSync(file.path).toString()

const mts = babel.transformSync(fileContent, {
filename: file.path,
sourceMaps: true,
plugins: [['@babel/plugin-syntax-typescript'], ['replace-import-extension', { extMapping: { '.ts': '.mjs' } }]],
})

await fsp.unlink(file.path)

const outFilePath = file.path.slice(0, file.path.length - '.d.ts'.length) + '.d.mts'
await fsp.writeFile(outFilePath, mts.code)
}
}

await main()
Loading