Compare commits

...

67 Commits

Author SHA1 Message Date
李华桥
55792e2f41 chore: replace v char in tag name 2023-12-21 10:35:30 +08:00
李华桥
04e7a9fc14 ci: use setup version action to init version 2023-12-21 10:19:19 +08:00
EYHN
fcc3e9e069 feat(core): add syncing progress (#5356)
![CleanShot 2023-12-20 at 16.54.30@2x.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/g3jz87HxbjOJpXV3FPT7/318a2d3c-14ea-48db-907a-881886d605e7.png)
2023-12-20 09:25:06 +00:00
JimmFly
9981c24120 fix(core): escape special characters for cmdk item values (#5353)
![image](https://github.com/toeverything/AFFiNE/assets/102217452/301ef02e-f0df-4a1b-843f-240cad44af0f)
2023-12-20 08:20:21 +00:00
Joooye_34
a4f31df192 chore: update basic version to 0.11.0 (#5355) 2023-12-20 08:10:42 +00:00
Joooye_34
80eeb2ddc7 feat: only follow serverUrlPrefix at redirect to client (#5295) (#5354)
feat: only follow `serverUrlPrefix` at redirect to client (#5295)

fix: use secure websocket (#5297)
2023-12-20 07:52:57 +00:00
LongYinan
800ea0abf1 fix(core): remove ses lockdown (#5350) 2023-12-20 04:11:34 +00:00
Joooye_34
e3882f9648 feat: bump bs (#5346)
Change history: a781985...8254dc9
2023-12-20 02:43:01 +00:00
LongYinan
30e62bd2c6 fix(core): downgrade ses (#5347)
ses@1 makes [this line](https://github.com/lit/lit/blob/lit-html%403.1.0/packages/reactive-element/src/reactive-element.ts#L406) throw an error: `TypeError: Cannot add property metadata, object is not extensible.`
2023-12-20 02:28:58 +00:00
DarkSky
33a589a8ba feat: onboarding electron redirect (#5327) 2023-12-19 13:54:43 +00:00
DarkSky
8ea910a2bb feat: onboarding page (#5277) 2023-12-19 13:54:41 +00:00
Yifeng Wang
31b1b2dade feat: bump blocksuite (#5343) 2023-12-19 21:53:57 +08:00
LongYinan
36653e79d2 fix(core): dedupe ses versions (#5342) 2023-12-19 13:31:37 +00:00
Cats Juice
197d1d4136 feat(core): adjust ui for new design (#5322)
feat(core): add bg and hover state for onboarding

feat(core): adjust onboarding styles for web

feat(core): add get started page for onboarding
2023-12-19 10:28:11 +00:00
LongYinan
07f10f55bf fix: cargo deps security alert (#5340)
- Close https://github.com/toeverything/AFFiNE/security/dependabot/55
2023-12-19 10:17:03 +00:00
LongYinan
6ca725343a chore: bump up ses version to v1 (#5282)
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [ses](https://togithub.com/Agoric/SES-shim/tree/master/packages/ses#readme) ([source](https://togithub.com/endojs/endo)) | [`^0.18.8` -> `^1.0.0`](https://renovatebot.com/diffs/npm/ses/0.18.8/1.0.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/ses/1.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/ses/1.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/ses/0.18.8/1.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/ses/0.18.8/1.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>endojs/endo (ses)</summary>

### [`v1.0.0`](https://togithub.com/endojs/endo/compare/ses@0.18.8...ses@1.0.0)

[Compare Source](https://togithub.com/endojs/endo/compare/ses@0.18.8...ses@1.0.0)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/toeverything/AFFiNE).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy44Ny4yIiwidXBkYXRlZEluVmVyIjoiMzcuOTMuMSIsInRhcmdldEJyYW5jaCI6ImNhbmFyeSJ9-->
2023-12-19 09:41:44 +00:00
Peng Xiao
d03567f689 fix(electron): onboarding display issue on Windows (#5320) 2023-12-19 09:32:12 +00:00
Peng Xiao
128f8066c3 fix(electron): main window should be opened first before destroying onboard window (#5319)
The issue listed on the title will prevent main window from showing on windows.
2023-12-19 09:22:44 +00:00
Cats Juice
e10609276d feat(core): add toggle workspace dialog (#5312) 2023-12-19 09:12:26 +00:00
3720
b9345e8d21 fix(core): collections initialized logic (#5310)
Collections YArray should be initialized only when the user operates on it, local state can't be trusted
2023-12-19 09:02:01 +00:00
Cats Juice
55818539af feat(core): basic page/edgeless toggle animation (#5283) 2023-12-19 08:48:54 +00:00
JimmFly
4b0ca06d80 feat(core): adjust empty favourites style (#5323)
close TOV-147
2023-12-19 08:37:35 +00:00
JimmFly
38617abc17 fix(component): fix incorrect input component width and height styling (#5292)
after:

https://github.com/toeverything/AFFiNE/assets/102217452/5d8f51c5-c7a6-4ec8-b2b0-7f1391f045c7
2023-12-19 08:27:46 +00:00
Cats Juice
d9f1cc60b9 feat(core): onboarding paper unfolding animation (#5264) 2023-12-19 07:18:06 +00:00
Cats Juice
841385666e feat(core): onboarding paper enter animation (#5248) 2023-12-19 07:18:00 +00:00
Cats Juice
15dd20ef48 feat(electron): onboarding at first launch logic for client and web (#5183)
- Added a simple abstraction of persistent storage class.
- Different persistence solutions are provided for web and client.
    - web: stored in localStorage
    - client: stored in the application directory as `.json` file
- Define persistent app-config schema
- Add a new hook that can interactive with persistent-app-config reactively
2023-12-19 07:17:54 +00:00
DarkSky
e0d328676d feat: add quota for old users (#5318) 2023-12-19 07:06:26 +00:00
JimmFly
6748e7ba42 chore(server): remove early access tips from invitation emails (#5314)
close TOV-177
2023-12-19 06:56:22 +00:00
Peng Xiao
a815fd6b9a feat(core): ai poc (#5317) 2023-12-19 05:13:29 +00:00
Peng Xiao
408b84109b fix(storybook): disable cloud for storybook (#5330) 2023-12-19 02:07:17 +00:00
EYHN
c7fe42a5b9 chore: bump up vitest monorepo to v1 (major) (#5217)
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@vitest/coverage-istanbul](https://togithub.com/vitest-dev/vitest/tree/main/packages/coverage-istanbul#readme) ([source](https://togithub.com/vitest-dev/vitest/tree/HEAD/packages/coverage-istanbul)) | [`0.34.6` -> `1.0.4`](https://renovatebot.com/diffs/npm/@vitest%2fcoverage-istanbul/0.34.6/1.0.4) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@vitest%2fcoverage-istanbul/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@vitest%2fcoverage-istanbul/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@vitest%2fcoverage-istanbul/0.34.6/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@vitest%2fcoverage-istanbul/0.34.6/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) |
| [@vitest/ui](https://togithub.com/vitest-dev/vitest/tree/main/packages/ui#readme) ([source](https://togithub.com/vitest-dev/vitest/tree/HEAD/packages/ui)) | [`0.34.6` -> `1.0.4`](https://renovatebot.com/diffs/npm/@vitest%2fui/0.34.6/1.0.4) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@vitest%2fui/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@vitest%2fui/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@vitest%2fui/0.34.6/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@vitest%2fui/0.34.6/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) |
| [vitest](https://togithub.com/vitest-dev/vitest) ([source](https://togithub.com/vitest-dev/vitest/tree/HEAD/packages/vitest)) | [`0.34.6` -> `1.0.4`](https://renovatebot.com/diffs/npm/vitest/0.34.6/1.0.4) | [![age](https://developer.mend.io/api/mc/badges/age/npm/vitest/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/vitest/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/vitest/0.34.6/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/vitest/0.34.6/1.0.4?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>vitest-dev/vitest (@&#8203;vitest/coverage-istanbul)</summary>

### [`v1.0.4`](https://togithub.com/vitest-dev/vitest/releases/tag/v1.0.4)

[Compare Source](https://togithub.com/vitest-dev/vitest/compare/v1.0.3...v1.0.4)

The previous release was built incorrectly and didn't include the performance fix. This release fixes that.

#####    🐞 Bug Fixes

-   **cli**: `--coverage.all=false` resolved incorrectly  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4697](https://togithub.com/vitest-dev/vitest/issues/4697) [<samp>(a7931)</samp>](https://togithub.com/vitest-dev/vitest/commit/a7931bbf)

#####    🏎 Performance

-   **reporters**: Downgrade `log-update` to v5  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4711](https://togithub.com/vitest-dev/vitest/issues/4711) [<samp>(13ff9)</samp>](https://togithub.com/vitest-dev/vitest/commit/13ff97a3)

#####     [View changes on GitHub](https://togithub.com/vitest-dev/vitest/compare/v1.0.3...v1.0.4)

### [`v1.0.3`](https://togithub.com/vitest-dev/vitest/releases/tag/v1.0.3)

[Compare Source](https://togithub.com/vitest-dev/vitest/compare/v1.0.2...v1.0.3)

#####    🐞 Bug Fixes

-   Correct package exports  -  by [@&#8203;userquin](https://togithub.com/userquin) in [https://github.com/vitest-dev/vitest/issues/4707](https://togithub.com/vitest-dev/vitest/issues/4707) [<samp>(37388)</samp>](https://togithub.com/vitest-dev/vitest/commit/37388d69)
-   **runner**: Fix async fixture teardown  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) in [https://github.com/vitest-dev/vitest/issues/4700](https://togithub.com/vitest-dev/vitest/issues/4700) [<samp>(92afd)</samp>](https://togithub.com/vitest-dev/vitest/commit/92afd54c)
-   **vitest**: Correctly filter changed files when Vitest workspace is used  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4693](https://togithub.com/vitest-dev/vitest/issues/4693) [<samp>(34135)</samp>](https://togithub.com/vitest-dev/vitest/commit/3413518b)

#####    🏎 Performance

-   **reporters**: Downgrade `log-update` to v5  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4711](https://togithub.com/vitest-dev/vitest/issues/4711) [<samp>(13ff9)</samp>](https://togithub.com/vitest-dev/vitest/commit/13ff97a3)

#####     [View changes on GitHub](https://togithub.com/vitest-dev/vitest/compare/v1.0.2...v1.0.3)

### [`v1.0.2`](https://togithub.com/vitest-dev/vitest/releases/tag/v1.0.2)

[Compare Source](https://togithub.com/vitest-dev/vitest/compare/v1.0.1...v1.0.2)

#####    🐞 Bug Fixes

-   Don't check if vite is installed  -  by [@&#8203;wojtekmaj](https://togithub.com/wojtekmaj) in [https://github.com/vitest-dev/vitest/issues/4659](https://togithub.com/vitest-dev/vitest/issues/4659) [<samp>(775e2)</samp>](https://togithub.com/vitest-dev/vitest/commit/775e2014)
-   Fix ensurePackageInstalled on Yarn PnP  -  by [@&#8203;wojtekmaj](https://togithub.com/wojtekmaj) in [https://github.com/vitest-dev/vitest/issues/4657](https://togithub.com/vitest-dev/vitest/issues/4657) [<samp>(574cc)</samp>](https://togithub.com/vitest-dev/vitest/commit/574cc7d0)
-   Apply `stripSnapshotIndentation` for thrown snapshot  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) in [https://github.com/vitest-dev/vitest/issues/4663](https://togithub.com/vitest-dev/vitest/issues/4663) [<samp>(74820)</samp>](https://togithub.com/vitest-dev/vitest/commit/748205dc)
-   **cli**:
    -   Prompted packages fail to install  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4593](https://togithub.com/vitest-dev/vitest/issues/4593) [<samp>(a9908)</samp>](https://togithub.com/vitest-dev/vitest/commit/a9908453)
-   **expect**:
    -   Apply `URL` equality check only when `URL` is available  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) in [https://github.com/vitest-dev/vitest/issues/4670](https://togithub.com/vitest-dev/vitest/issues/4670) [<samp>(43783)</samp>](https://togithub.com/vitest-dev/vitest/commit/43783cfe)
-   **runner**:
    -   Improve fixture error messages  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4673](https://togithub.com/vitest-dev/vitest/issues/4673) [<samp>(1e4aa)</samp>](https://togithub.com/vitest-dev/vitest/commit/1e4aa8e4)
    -   Fix fixture cleanup when test times out  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) in [https://github.com/vitest-dev/vitest/issues/4679](https://togithub.com/vitest-dev/vitest/issues/4679) [<samp>(e7c5e)</samp>](https://togithub.com/vitest-dev/vitest/commit/e7c5e1f7)
-   **vitest**:
    -   Support new Request('/api') in happy-dom  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4671](https://togithub.com/vitest-dev/vitest/issues/4671) [<samp>(6e6ee)</samp>](https://togithub.com/vitest-dev/vitest/commit/6e6ee10e)
    -   Skip processing getter in auto-mocked constructor call  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) in [https://github.com/vitest-dev/vitest/issues/4677](https://togithub.com/vitest-dev/vitest/issues/4677) [<samp>(cb786)</samp>](https://togithub.com/vitest-dev/vitest/commit/cb7864aa)

#####     [View changes on GitHub](https://togithub.com/vitest-dev/vitest/compare/v1.0.1...v1.0.2)

### [`v1.0.1`](https://togithub.com/vitest-dev/vitest/releases/tag/v1.0.1)

[Compare Source](https://togithub.com/vitest-dev/vitest/compare/v1.0.0...v1.0.1)

#####    🐞 Bug Fixes

-   Bump vitest packages `peerDependencies` versions  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4654](https://togithub.com/vitest-dev/vitest/issues/4654) [<samp>(42070)</samp>](https://togithub.com/vitest-dev/vitest/commit/420707fc)

#####     [View changes on GitHub](https://togithub.com/vitest-dev/vitest/compare/v1.0.0...v1.0.1)

### [`v1.0.0`](https://togithub.com/vitest-dev/vitest/releases/tag/v1.0.0)

[Compare Source](https://togithub.com/vitest-dev/vitest/compare/v0.34.6...v1.0.0)

Vitest 1.0 is here! This release page lists all changes made to the project during the beta. For the migration guide, please refer to the [documentation](https://vitest.dev/guide/migration.html#migrating-from-vitest-0-34-6).

#####    🚨 Breaking Changes

-   Add support for `pool` and `poolOptions`, remove old flags  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4172](https://togithub.com/vitest-dev/vitest/issues/4172) [<samp>(114a9)</samp>](https://togithub.com/vitest-dev/vitest/commit/114a993c)
-   Support multiple parallel `child_process`  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/3925](https://togithub.com/vitest-dev/vitest/issues/3925) [<samp>(8b4a4)</samp>](https://togithub.com/vitest-dev/vitest/commit/8b4a44ad)
-   Make snapshots more visually pleasing by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/pull/3961](https://togithub.com/vitest-dev/vitest/pull/3961)
-   Set `vitest` peer dependency range for sub packages  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4299](https://togithub.com/vitest-dev/vitest/issues/4299) [<samp>(cd03c)</samp>](https://togithub.com/vitest-dev/vitest/commit/cd03cb51)
-   Bump minimum node version to 18 and match Vite 5 requirement  -  by [@&#8203;ghiscoding](https://togithub.com/ghiscoding) in [https://github.com/vitest-dev/vitest/issues/4296](https://togithub.com/vitest-dev/vitest/issues/4296) [<samp>(263b7)</samp>](https://togithub.com/vitest-dev/vitest/commit/263b7167)
-   Remove deprecated node loader  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4371](https://togithub.com/vitest-dev/vitest/issues/4371) [<samp>(29299)</samp>](https://togithub.com/vitest-dev/vitest/commit/29299f3c)
-   Move browser providers to [@&#8203;vitest/browser](https://togithub.com/vitest/browser) package  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4364](https://togithub.com/vitest-dev/vitest/issues/4364) [<samp>(5cdeb)</samp>](https://togithub.com/vitest-dev/vitest/commit/5cdeb558)
-   Remove EnhancedSpy type, deprecate SpyInstance, improve mocks and vi documentation  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) and [@&#8203;dammy001](https://togithub.com/dammy001) in [https://github.com/vitest-dev/vitest/issues/4400](https://togithub.com/vitest-dev/vitest/issues/4400) [<samp>(d40b3)</samp>](https://togithub.com/vitest-dev/vitest/commit/d40b3a58)
-   `expect().toContain()` can handle classList, Node.contains, and any array-like structure. This means you cannot use it to check if one object is a subset of another - use `expect().toMatchObject()` in that case  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4239](https://togithub.com/vitest-dev/vitest/issues/4239) [<samp>(ce84f)</samp>](https://togithub.com/vitest-dev/vitest/commit/ce84f069)
-   **runner**: Correctly process custom tasks, update runner hooks naming by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/pull/4076](https://togithub.com/vitest-dev/vitest/pull/4076)
-   **coverage**:
    -   glob based coverage thresholds by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/pull/4442](https://togithub.com/vitest-dev/vitest/pull/4442) [<samp>(18300)</samp>](4953410e8d)
    -   Use `transformMode` and workspace project based source maps  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4309](https://togithub.com/vitest-dev/vitest/issues/4309) [<samp>(28109cc)</samp>](https://togithub.com/vitest-dev/vitest/commit/28109cc)
    -   Enable `coverage.all` by default  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4265](https://togithub.com/vitest-dev/vitest/issues/4265) [<samp>(5a741)</samp>](https://togithub.com/vitest-dev/vitest/commit/5a741ca2)

#####    🚀 Features

-   Add Marko example and include code coverage for Marko files  -  by [@&#8203;DylanPiercey](https://togithub.com/DylanPiercey) in [https://github.com/vitest-dev/vitest/issues/4263](https://togithub.com/vitest-dev/vitest/issues/4263) [<samp>(eac77)</samp>](https://togithub.com/vitest-dev/vitest/commit/eac77765)
-   Update magic-string  -  by [@&#8203;bluwy](https://togithub.com/bluwy) in [https://github.com/vitest-dev/vitest/issues/4345](https://togithub.com/vitest-dev/vitest/issues/4345) [<samp>(fde18)</samp>](https://togithub.com/vitest-dev/vitest/commit/fde1843e)
-   Implement provide/inject API to transfer data from the main thread  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4422](https://togithub.com/vitest-dev/vitest/issues/4422) [<samp>(a7522)</samp>](https://togithub.com/vitest-dev/vitest/commit/a75228f1)
-   Improve expectTypeOf error messages  -  by [@&#8203;mmkal](https://togithub.com/mmkal), **Misha Kaletsky** and [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4206](https://togithub.com/vitest-dev/vitest/issues/4206) [<samp>(18300)</samp>](https://togithub.com/vitest-dev/vitest/commit/183005e9)
-   Add test.sequential() api  -  by [@&#8203;dsyddall](https://togithub.com/dsyddall) in [https://github.com/vitest-dev/vitest/issues/4512](https://togithub.com/vitest-dev/vitest/issues/4512) [<samp>(c3619)</samp>](https://togithub.com/vitest-dev/vitest/commit/c3619c78)
-   Allow custom pools  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4417](https://togithub.com/vitest-dev/vitest/issues/4417) [<samp>(a3fd5)</samp>](https://togithub.com/vitest-dev/vitest/commit/a3fd5f85)
-   Add --project option to limit what projects are running  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va), [@&#8203;dammy001](https://togithub.com/dammy001) and [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4561](https://togithub.com/vitest-dev/vitest/issues/4561) [<samp>(58ef5)</samp>](https://togithub.com/vitest-dev/vitest/commit/58ef51a9)
-   **benchmark**:
    -   Move importTinybench to runner  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) in [https://github.com/vitest-dev/vitest/issues/4376](https://togithub.com/vitest-dev/vitest/issues/4376) [<samp>(c36d2)</samp>](https://togithub.com/vitest-dev/vitest/commit/c36d2b97)
-   **browser**:
    -   Support "none" provider and update lit example to use it  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4427](https://togithub.com/vitest-dev/vitest/issues/4427) [<samp>(d03a2)</samp>](https://togithub.com/vitest-dev/vitest/commit/d03a2a21)
-   **coverage**:
    -   Support `/* v8 ignore...` ignore hints  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4573](https://togithub.com/vitest-dev/vitest/issues/4573) [<samp>(f9e4a)</samp>](https://togithub.com/vitest-dev/vitest/commit/f9e4ad83)
-   **expect**:
    -   Support `expect.closeTo` api  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) and **golebiowskib** in [https://github.com/vitest-dev/vitest/issues/4260](https://togithub.com/vitest-dev/vitest/issues/4260) [<samp>(7f91c)</samp>](https://togithub.com/vitest-dev/vitest/commit/7f91c6f6)
    -   Compare URL objects by href  -  by [@&#8203;kleinfreund](https://togithub.com/kleinfreund) and [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4615](https://togithub.com/vitest-dev/vitest/issues/4615) [<samp>(f7a73)</samp>](https://togithub.com/vitest-dev/vitest/commit/f7a73338)
-   **snapshot**:
    -   Add option to configure snapshot directory  -  by [@&#8203;d3lm](https://togithub.com/d3lm) in [https://github.com/vitest-dev/vitest/issues/4651](https://togithub.com/vitest-dev/vitest/issues/4651) [<samp>(20b2a)</samp>](https://togithub.com/vitest-dev/vitest/commit/20b2a857)
-   **vite-node**:
    -   Support import.meta.hot.off for vite 5  -  by [@&#8203;bluwy](https://togithub.com/bluwy) in [https://github.com/vitest-dev/vitest/issues/4315](https://togithub.com/vitest-dev/vitest/issues/4315) [<samp>(01b1c)</samp>](https://togithub.com/vitest-dev/vitest/commit/01b1c55c)
-   **vitest**:
    -   Expose getBenchFn, getBenchOptions  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4208](https://togithub.com/vitest-dev/vitest/issues/4208) [<samp>(8e5e4)</samp>](https://togithub.com/vitest-dev/vitest/commit/8e5e42dc)
    -   Run typecheck during tests  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4324](https://togithub.com/vitest-dev/vitest/issues/4324) [<samp>(a1aad)</samp>](https://togithub.com/vitest-dev/vitest/commit/a1aadd71)
    -   Filter stacktraces  -  by [@&#8203;clarkf](https://togithub.com/clarkf) in [https://github.com/vitest-dev/vitest/issues/1999](https://togithub.com/vitest-dev/vitest/issues/1999) and [https://github.com/vitest-dev/vitest/issues/4338](https://togithub.com/vitest-dev/vitest/issues/4338) [<samp>(6b734)</samp>](https://togithub.com/vitest-dev/vitest/commit/6b73473f)
    -   Expose execArgv to the different pools  -  by [@&#8203;adriencaccia](https://togithub.com/adriencaccia) in [https://github.com/vitest-dev/vitest/issues/4383](https://togithub.com/vitest-dev/vitest/issues/4383) [<samp>(9021e)</samp>](https://togithub.com/vitest-dev/vitest/commit/9021e8b8)

#####    🐞 Bug Fixes

-   Add multiple globals in VM+JSDOM  -  by [@&#8203;nstepien](https://togithub.com/nstepien) in [https://github.com/vitest-dev/vitest/issues/4199](https://togithub.com/vitest-dev/vitest/issues/4199) and [https://github.com/vitest-dev/vitest/issues/4202](https://togithub.com/vitest-dev/vitest/issues/4202) [<samp>(fc947)</samp>](https://togithub.com/vitest-dev/vitest/commit/fc947ce6)
-   Ignore "plugins" field in snapshotFormat option  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4204](https://togithub.com/vitest-dev/vitest/issues/4204) [<samp>(db1ff)</samp>](https://togithub.com/vitest-dev/vitest/commit/db1ff438)
-   `nextTick` mocking error message to mention correct config option  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4215](https://togithub.com/vitest-dev/vitest/issues/4215) [<samp>(98fe3)</samp>](https://togithub.com/vitest-dev/vitest/commit/98fe3d55)
-   Export VitestUtils interface  -  by [@&#8203;fbritoferreira](https://togithub.com/fbritoferreira) in [https://github.com/vitest-dev/vitest/issues/4301](https://togithub.com/vitest-dev/vitest/issues/4301) [<samp>(b1439)</samp>](https://togithub.com/vitest-dev/vitest/commit/b1439852)
-   Assertion diff message handle non-writable sub-properties  -  by [@&#8203;bfamchon](https://togithub.com/bfamchon) in [https://github.com/vitest-dev/vitest/issues/4278](https://togithub.com/vitest-dev/vitest/issues/4278) [<samp>(7e1a0)</samp>](https://togithub.com/vitest-dev/vitest/commit/7e1a0f83)
-   Don't bundle import from rollup  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4392](https://togithub.com/vitest-dev/vitest/issues/4392) [<samp>(3b584)</samp>](https://togithub.com/vitest-dev/vitest/commit/3b58487b)
-   Support accessing fixture at same index of dependency fixture  -  by [@&#8203;dsyddall](https://togithub.com/dsyddall) in [https://github.com/vitest-dev/vitest/issues/4387](https://togithub.com/vitest-dev/vitest/issues/4387) [<samp>(4cd1d)</samp>](https://togithub.com/vitest-dev/vitest/commit/4cd1d3ce)
-   Make asynchronous fixtures work concurrently  -  by [@&#8203;dsyddall](https://togithub.com/dsyddall) in [https://github.com/vitest-dev/vitest/issues/4403](https://togithub.com/vitest-dev/vitest/issues/4403) [<samp>(3c9f9)</samp>](https://togithub.com/vitest-dev/vitest/commit/3c9f920a)
-   Coverage.100 crash when using as an cli argument  -  by [@&#8203;marcelobotega](https://togithub.com/marcelobotega) in [https://github.com/vitest-dev/vitest/issues/4346](https://togithub.com/vitest-dev/vitest/issues/4346) [<samp>(0db38)</samp>](https://togithub.com/vitest-dev/vitest/commit/0db386dc)
-   Support typechecking with Yarn PnP  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4412](https://togithub.com/vitest-dev/vitest/issues/4412) [<samp>(1ecbe)</samp>](https://togithub.com/vitest-dev/vitest/commit/1ecbe74d)
-   Support accessing task from test context without accessing fixtures  -  by [@&#8203;dsyddall](https://togithub.com/dsyddall) in [https://github.com/vitest-dev/vitest/issues/4419](https://togithub.com/vitest-dev/vitest/issues/4419) [<samp>(3397f)</samp>](https://togithub.com/vitest-dev/vitest/commit/3397fdc4)
-   Copy custom asymmetric matchers to local `expect`  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) in [https://github.com/vitest-dev/vitest/issues/4405](https://togithub.com/vitest-dev/vitest/issues/4405) [<samp>(9fe38)</samp>](https://togithub.com/vitest-dev/vitest/commit/9fe38737)
-   Apply serializer to `Error` instance for thrown snapshot  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) and [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4396](https://togithub.com/vitest-dev/vitest/issues/4396) [<samp>(ac309)</samp>](https://togithub.com/vitest-dev/vitest/commit/ac309726)
-   Throw an error when running "vitest typecheck"  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4439](https://togithub.com/vitest-dev/vitest/issues/4439) [<samp>(7f502)</samp>](https://togithub.com/vitest-dev/vitest/commit/7f502299)
-   Don't expand snapshot diff by default  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4430](https://togithub.com/vitest-dev/vitest/issues/4430) [<samp>(8983c)</samp>](https://togithub.com/vitest-dev/vitest/commit/8983cd48)
-   Handle errors thrown in fixtures  -  by [@&#8203;dsyddall](https://togithub.com/dsyddall) [<samp>(f6844)</samp>](https://togithub.com/vitest-dev/vitest/commit/f6844ad6)
-   Default --open to !process.env.CI  -  by [@&#8203;collinstevens](https://togithub.com/collinstevens) in [https://github.com/vitest-dev/vitest/issues/4477](https://togithub.com/vitest-dev/vitest/issues/4477) [<samp>(088a0)</samp>](https://togithub.com/vitest-dev/vitest/commit/088a047d)
-   Disable ESBuild when user config disables it  -  by [@&#8203;Namchee](https://togithub.com/Namchee) in [https://github.com/vitest-dev/vitest/issues/4492](https://togithub.com/vitest-dev/vitest/issues/4492) [<samp>(9abde)</samp>](https://togithub.com/vitest-dev/vitest/commit/9abde204)
-   Inherit concurrent/sequential in nested suites  -  by [@&#8203;dsyddall](https://togithub.com/dsyddall) in [https://github.com/vitest-dev/vitest/issues/4482](https://togithub.com/vitest-dev/vitest/issues/4482) [<samp>(ca168)</samp>](https://togithub.com/vitest-dev/vitest/commit/ca168a14)
-   Provide customTesters to asymmetric matchers  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) [<samp>(ac665)</samp>](https://togithub.com/vitest-dev/vitest/commit/ac665c96)
-   Apply `retry` and `bail` from test config file  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) in [https://github.com/vitest-dev/vitest/issues/4530](https://togithub.com/vitest-dev/vitest/issues/4530) [<samp>(94f9a)</samp>](https://togithub.com/vitest-dev/vitest/commit/94f9a3ca)
-   Respect trailing slash when filtering by file path  -  by [@&#8203;ibuibu](https://togithub.com/ibuibu) in [https://github.com/vitest-dev/vitest/issues/4538](https://togithub.com/vitest-dev/vitest/issues/4538) [<samp>(f377a)</samp>](https://togithub.com/vitest-dev/vitest/commit/f377a3bf)
-   Date prototype when using setSystemTime  -  by [@&#8203;spiroka](https://togithub.com/spiroka) in [https://github.com/vitest-dev/vitest/issues/4584](https://togithub.com/vitest-dev/vitest/issues/4584) [<samp>(3f8c3)</samp>](https://togithub.com/vitest-dev/vitest/commit/3f8c3fb1)
-   BrowserTestRunner called incorrect super methods  -  by [@&#8203;samthor](https://togithub.com/samthor) in [https://github.com/vitest-dev/vitest/issues/4632](https://togithub.com/vitest-dev/vitest/issues/4632) [<samp>(8385c)</samp>](https://togithub.com/vitest-dev/vitest/commit/8385c981)
-   Set process name for idle workers  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4641](https://togithub.com/vitest-dev/vitest/issues/4641) [<samp>(eca25)</samp>](https://togithub.com/vitest-dev/vitest/commit/eca25dc9)
-   **bench**:
    -   Extract ChainableBenchmarkAPI type  -  by [@&#8203;dsyddall](https://togithub.com/dsyddall) in [https://github.com/vitest-dev/vitest/issues/4537](https://togithub.com/vitest-dev/vitest/issues/4537) [<samp>(79e9b)</samp>](https://togithub.com/vitest-dev/vitest/commit/79e9bfaa)
-   **browser**:
    -   Improve error handling and don't rely on Node.js builtin modules in browser mode  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4244](https://togithub.com/vitest-dev/vitest/issues/4244) [<samp>(e7e8c)</samp>](https://togithub.com/vitest-dev/vitest/commit/e7e8c3cc)
    -   Disable hijacking ES modules until vi.mock is implemented  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4414](https://togithub.com/vitest-dev/vitest/issues/4414) [<samp>(ab556)</samp>](https://togithub.com/vitest-dev/vitest/commit/ab556376)
    -   Add vitest/ imports to entries  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4514](https://togithub.com/vitest-dev/vitest/issues/4514) [<samp>(648bc)</samp>](https://togithub.com/vitest-dev/vitest/commit/648bccb9)
    -   Wait until vite finishes prebundling of vitest dependencies  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4518](https://togithub.com/vitest-dev/vitest/issues/4518) [<samp>(56ee7)</samp>](https://togithub.com/vitest-dev/vitest/commit/56ee7026)
    -   Allow for `pretty-format` as a sibling dependency  -  by [@&#8203;nicojs](https://togithub.com/nicojs) in [https://github.com/vitest-dev/vitest/issues/4590](https://togithub.com/vitest-dev/vitest/issues/4590) [<samp>(ed50a)</samp>](https://togithub.com/vitest-dev/vitest/commit/ed50a944)
    -   Don't go into an infinite reload loop, don't fail if "error" event is caught  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4618](https://togithub.com/vitest-dev/vitest/issues/4618) [<samp>(ec3d6)</samp>](https://togithub.com/vitest-dev/vitest/commit/ec3d6949)
    -   Respect "server" option in vite config  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4627](https://togithub.com/vitest-dev/vitest/issues/4627) [<samp>(723f6)</samp>](https://togithub.com/vitest-dev/vitest/commit/723f65b9)
-   **cli**:
    -   Do not capture `stdin` when in run mode  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4310](https://togithub.com/vitest-dev/vitest/issues/4310) [<samp>(fc51a)</samp>](https://togithub.com/vitest-dev/vitest/commit/fc51ad04)
-   **config**:
    -   Type issue of `pool` and `poolMatchGlobs` in defineConfig  -  by [@&#8203;InfiniteXyy](https://togithub.com/InfiniteXyy) in [https://github.com/vitest-dev/vitest/issues/4282](https://togithub.com/vitest-dev/vitest/issues/4282) [<samp>(9112c)</samp>](https://togithub.com/vitest-dev/vitest/commit/9112cc96)
-   **coverage**:
    -   `thresholdAutoUpdate` to detect zero limits  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4287](https://togithub.com/vitest-dev/vitest/issues/4287) [<samp>(a29fe)</samp>](https://togithub.com/vitest-dev/vitest/commit/a29fecee)
    -   Exclude files and directories starting with dot by default  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4428](https://togithub.com/vitest-dev/vitest/issues/4428) [<samp>(b3327)</samp>](https://togithub.com/vitest-dev/vitest/commit/b3327a64)
    -   Improve memory usage by writing temporary files on file system  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4603](https://togithub.com/vitest-dev/vitest/issues/4603) [<samp>(4166c)</samp>](https://togithub.com/vitest-dev/vitest/commit/4166c413)
-   **deps**:
    -   Update dependency v8-to-istanbul to ^9.2.0  -  by [@&#8203;renovate](https://togithub.com/renovate)\[bot] in[https://github.com/vitest-dev/vitest/issues/4583](https://togithub.com/vitest-dev/vitest/issues/4583)3 [<samp>(a70f2)</samp>](https://togithub.com/vitest-dev/vitest/commit/a70f216d)
    -   Update dependency std-env to ^3.5.0  -  by [@&#8203;renovate](https://togithub.com/renovate)\[bot] in[https://github.com/vitest-dev/vitest/issues/4582](https://togithub.com/vitest-dev/vitest/issues/4582)2 [<samp>(1fdd6)</samp>](https://togithub.com/vitest-dev/vitest/commit/1fdd6fe2)
-   **example**:
    -   Resolve type error  -  by [@&#8203;jqkk](https://togithub.com/jqkk) in [https://github.com/vitest-dev/vitest/issues/4515](https://togithub.com/vitest-dev/vitest/issues/4515) [<samp>(2d1b4)</samp>](https://togithub.com/vitest-dev/vitest/commit/2d1b4785)
-   **expect**:
    -   Publish types file  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) [<samp>(5996c)</samp>](https://togithub.com/vitest-dev/vitest/commit/5996c8c0)
    -   `Object.freeze` breaks `toEqual`  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) in [https://github.com/vitest-dev/vitest/issues/4303](https://togithub.com/vitest-dev/vitest/issues/4303) [<samp>(a4501)</samp>](https://togithub.com/vitest-dev/vitest/commit/a4501d6b)
    -   Publish semantically correct chai types  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4322](https://togithub.com/vitest-dev/vitest/issues/4322) [<samp>(80a70)</samp>](https://togithub.com/vitest-dev/vitest/commit/80a706a1)
    -   Print full error if promise is rejected  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4467](https://togithub.com/vitest-dev/vitest/issues/4467) [<samp>(cadb9)</samp>](https://togithub.com/vitest-dev/vitest/commit/cadb9cd3)
    -   Don't fail when using jest expect  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4517](https://togithub.com/vitest-dev/vitest/issues/4517) [<samp>(60d6d)</samp>](https://togithub.com/vitest-dev/vitest/commit/60d6d173)
-   **happy-dom**:
    -   Don't crash when calling useFakeTimers with empty config  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4214](https://togithub.com/vitest-dev/vitest/issues/4214) [<samp>(2e1a1)</samp>](https://togithub.com/vitest-dev/vitest/commit/2e1a1bd4)
-   **jsdom**:
    -   Don't go into an infinite recusion when calling atob  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) [<samp>(da794)</samp>](https://togithub.com/vitest-dev/vitest/commit/da7949dc)
-   **runner**:
    -   Fixture needs to be initialized for each test  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) in [https://github.com/vitest-dev/vitest/issues/4250](https://togithub.com/vitest-dev/vitest/issues/4250) [<samp>(76a93)</samp>](https://togithub.com/vitest-dev/vitest/commit/76a93298)
    -   Nested tests should throw errors  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) in [https://github.com/vitest-dev/vitest/issues/4262](https://togithub.com/vitest-dev/vitest/issues/4262) [<samp>(8ac9f)</samp>](https://togithub.com/vitest-dev/vitest/commit/8ac9f8b1)
    -   Removes deprecated `error` option from TaskResult  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) in [https://github.com/vitest-dev/vitest/issues/4313](https://togithub.com/vitest-dev/vitest/issues/4313) [<samp>(4cee6)</samp>](https://togithub.com/vitest-dev/vitest/commit/4cee6711)
    -   Preserve fixtures when calling runif and skipif  -  by [@&#8203;dsyddall](https://togithub.com/dsyddall) in [https://github.com/vitest-dev/vitest/issues/4585](https://togithub.com/vitest-dev/vitest/issues/4585) and [https://github.com/vitest-dev/vitest/issues/4591](https://togithub.com/vitest-dev/vitest/issues/4591) [<samp>(515ea)</samp>](https://togithub.com/vitest-dev/vitest/commit/515eadf9)
    -   PassWithNoTests option not work  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) in [https://github.com/vitest-dev/vitest/issues/4553](https://togithub.com/vitest-dev/vitest/issues/4553) [<samp>(8d183)</samp>](https://togithub.com/vitest-dev/vitest/commit/8d183da4)
-   **vite-node**:
    -   Have a separate cache for web/ssr transforms  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4221](https://togithub.com/vitest-dev/vitest/issues/4221) [<samp>(ca5db)</samp>](https://togithub.com/vitest-dev/vitest/commit/ca5dbef4)
    -   Mjs files watch not work  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) in [https://github.com/vitest-dev/vitest/issues/3982](https://togithub.com/vitest-dev/vitest/issues/3982) [<samp>(77ea9)</samp>](https://togithub.com/vitest-dev/vitest/commit/77ea9326)
-   **vitest**:
    -   Make [@&#8203;types/node](https://togithub.com/types/node) optional  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4210](https://togithub.com/vitest-dev/vitest/issues/4210) [<samp>(a5383)</samp>](https://togithub.com/vitest-dev/vitest/commit/a5383c2d)
    -   Inline chai types instead of using [@&#8203;types/chai](https://togithub.com/types/chai)  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4209](https://togithub.com/vitest-dev/vitest/issues/4209) [<samp>(5f477)</samp>](https://togithub.com/vitest-dev/vitest/commit/5f4774fc)
    -   Don't initialize globalSetup if workspace doesn't run tests  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4213](https://togithub.com/vitest-dev/vitest/issues/4213) [<samp>(06461)</samp>](https://togithub.com/vitest-dev/vitest/commit/0646197e)
    -   Deduplicate vitest when running globally or in a workspace  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4238](https://togithub.com/vitest-dev/vitest/issues/4238) [<samp>(93504)</samp>](https://togithub.com/vitest-dev/vitest/commit/93504619)
    -   Print file path instead of "unknown test" when logging  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) in [https://github.com/vitest-dev/vitest/issues/4146](https://togithub.com/vitest-dev/vitest/issues/4146) [<samp>(ec2e8)</samp>](https://togithub.com/vitest-dev/vitest/commit/ec2e8040)
    -   Failed to load custom environment from js/ts file  -  by [@&#8203;Dunqing](https://togithub.com/Dunqing) and [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4255](https://togithub.com/vitest-dev/vitest/issues/4255) [<samp>(da8d0)</samp>](https://togithub.com/vitest-dev/vitest/commit/da8d0570)
    -   Support assets in new URL in Vite 5  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4258](https://togithub.com/vitest-dev/vitest/issues/4258) [<samp>(d280f)</samp>](https://togithub.com/vitest-dev/vitest/commit/d280f489)
    -   Correctly hoist `vi.hoisted` if assigned  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4285](https://togithub.com/vitest-dev/vitest/issues/4285) [<samp>(ff93a)</samp>](https://togithub.com/vitest-dev/vitest/commit/ff93a573)
    -   Run globalSetup from the root config even if it's not in a workspace  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4325](https://togithub.com/vitest-dev/vitest/issues/4325) [<samp>(4293e)</samp>](https://togithub.com/vitest-dev/vitest/commit/4293e1b1)
    -   Pass correct mode in vitest config function  -  by [@&#8203;adriencaccia](https://togithub.com/adriencaccia) in [https://github.com/vitest-dev/vitest/issues/4399](https://togithub.com/vitest-dev/vitest/issues/4399) [<samp>(b8ca3)</samp>](https://togithub.com/vitest-dev/vitest/commit/b8ca3873)
    -   Throw an error if vitest is imported using require()  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4466](https://togithub.com/vitest-dev/vitest/issues/4466) [<samp>(e5cf1)</samp>](https://togithub.com/vitest-dev/vitest/commit/e5cf1418)
    -   Use correct type for defineProject to allow usage in mergeConfig  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4498](https://togithub.com/vitest-dev/vitest/issues/4498) [<samp>(7dee8)</samp>](https://togithub.com/vitest-dev/vitest/commit/7dee832d)
    -   Throw an error if Vite wasn't able to resolve aliased path  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4503](https://togithub.com/vitest-dev/vitest/issues/4503) [<samp>(50333)</samp>](https://togithub.com/vitest-dev/vitest/commit/503331d8)
    -   Improve vi.waitUntil type to excude falsy types  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4572](https://togithub.com/vitest-dev/vitest/issues/4572) [<samp>(23652)</samp>](https://togithub.com/vitest-dev/vitest/commit/23652300)
    -   Add import-meta.d.ts  -  by [@&#8203;macdaddyaz](https://togithub.com/macdaddyaz) in [https://github.com/vitest-dev/vitest/issues/4571](https://togithub.com/vitest-dev/vitest/issues/4571) [<samp>(dd802)</samp>](https://togithub.com/vitest-dev/vitest/commit/dd80288f)
    -   Correctly support CSS variable when using happy-dom  -  by [@&#8203;sheremet-va](https://togithub.com/sheremet-va) in [https://github.com/vitest-dev/vitest/issues/4601](https://togithub.com/vitest-dev/vitest/issues/4601) [<samp>(9fbf3)</samp>](https://togithub.com/vitest-dev/vitest/commit/9fbf39af)
    -   Pass correct server options in workspace  -  by [@&#8203;hironytic](https://togithub.com/hironytic) in [https://github.com/vitest-dev/vitest/issues/4539](https://togithub.com/vitest-dev/vitest/issues/4539) and [https://github.com/vitest-dev/vitest/issues/4540](https://togithub.com/vitest-dev/vitest/issues/4540) [<samp>(241a8)</samp>](https://togithub.com/vitest-dev/vitest/commit/241a8c13)
    -   Independently mock each instance's methods for mocked class  -  by [@&#8203;hi-ogawa](https://togithub.com/hi-ogawa) in [https://github.com/vitest-dev/vitest/issues/4564](https://togithub.com/vitest-dev/vitest/issues/4564) [<samp>(05b05)</samp>](https://togithub.com/vitest-dev/vitest/commit/05b0521c)
-   **vm**:
    -   Remove sequencer usage from createVmThreadsPool function  -  by [@&#8203;mhogeveen](https://togithub.com/mhogeveen) in [https://github.com/vitest-dev/vitest/issues/4638](https://togithub.com/vitest-dev/vitest/issues/4638) [<samp>(54d52)</samp>](https://togithub.com/vitest-dev/vitest/commit/54d52d44)

#####    🏎 Performance

-   Update `log-update` v9  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4390](https://togithub.com/vitest-dev/vitest/issues/4390) [<samp>(ba1df)</samp>](https://togithub.com/vitest-dev/vitest/commit/ba1df849)
-   Close pool early in run-mode  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4623](https://togithub.com/vitest-dev/vitest/issues/4623) [<samp>(e0e20)</samp>](https://togithub.com/vitest-dev/vitest/commit/e0e20176)
-   **coverage-istanbul**: `all: true` instruments already instrumented files  -  by [@&#8203;AriPerkkio](https://togithub.com/AriPerkkio) in [https://github.com/vitest-dev/vitest/issues/4552](https://togithub.com/vitest-dev/vitest/issues/4552) [<samp>(d1e1b)</samp>](https://togithub.com/vitest-dev/vitest/commit/d1e1bc90)

#####     [View changes on GitHub](https://togithub.com/vitest-dev/vitest/compare/v0.34.7...v1.0.0)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/toeverything/AFFiNE).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy44MS4zIiwidXBkYXRlZEluVmVyIjoiMzcuOTMuMSIsInRhcmdldEJyYW5jaCI6ImNhbmFyeSJ9-->
2023-12-18 13:46:29 +00:00
JimmFly
cef9e0539d fix(storybook): fix test (#5325)
<img width="440" alt="image" src="https://github.com/toeverything/AFFiNE/assets/102217452/329f9c12-cc0b-4aae-9352-3811ab0a27a6">
2023-12-18 13:36:56 +00:00
EYHN
a1c9ac80d8 ci: fix e2e (#5329) 2023-12-18 12:24:48 +00:00
EYHN
1b5837e545 ci: fix oxlint version (#5328) 2023-12-18 20:10:01 +08:00
LongYinan
a3d4c5c709 chore(server): split gcloud sql proxy into a separate deployment (#5101)
After merge this pull request, need to change the `DATABASE_URL` in environment secrets (stable/beta) from `127.0.0.1` to `affine-cloud-sql-proxy`
2023-12-15 13:28:12 +00:00
Peng Xiao
fc56a53acd fix(core): page mode switch sometimes not working (#5306)
Should not pass inline object without memo into `InternalLottie`.
cdc96876b0/packages/frontend/component/src/components/internal-lottie/index.tsx (L77)

In the detail page when during syncing on the cloud, the detail page will be re-rendered constantly because of `useCurrentSyncEngineStatus` hook, which will then cause `PageSwitchItem` to re-render and forcing the internal lottie state to reset. As a result the click event may not be captured somehow.
2023-12-15 08:21:41 +00:00
EYHN
fe2851d3e9 refactor: workspace manager (#5060) 2023-12-15 07:20:50 +00:00
Yifeng Wang
af15aa06d4 feat: bump blocksuite (#5286)
Co-authored-by: donteatfriedrice <huisheng.chen7788@outlook.com>
2023-12-15 12:57:52 +08:00
LongYinan
136b4ccb4e chore: bump up github/codeql-action action to v3 (#5298)
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github/codeql-action](https://togithub.com/github/codeql-action) | action | major | `v2` -> `v3` |

---

### Release Notes

<details>
<summary>github/codeql-action (github/codeql-action)</summary>

### [`v3`](https://togithub.com/github/codeql-action/compare/v2...v3)

[Compare Source](https://togithub.com/github/codeql-action/compare/v2...v3)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/toeverything/AFFiNE).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy44Ny4yIiwidXBkYXRlZEluVmVyIjoiMzcuOTMuMSIsInRhcmdldEJyYW5jaCI6ImNhbmFyeSJ9-->
2023-12-14 13:52:18 +00:00
DarkSky
e9dfa93b52 feat: add cache for blob query (#5178) 2023-12-14 09:50:56 +00:00
DarkSky
0c2d2f8d16 feat: struct type feature config (#5142) 2023-12-14 09:50:52 +00:00
DarkSky
2b7f6f8b74 feat: integrate new modules (#5087) 2023-12-14 09:50:46 +00:00
DarkSky
a93c12e122 feat: user usage gql & test case improve (#5076) 2023-12-14 09:50:42 +00:00
DarkSky
ad23ead5e4 feat: integrate user usage into apis (#5075) 2023-12-14 09:50:37 +00:00
Peng Xiao
63de73a815 fix: width blink in side bar (#5291) 2023-12-14 09:20:18 +00:00
Peng Xiao
c66781970b feat(core): add useQueryImmutable (#5299) 2023-12-14 08:04:50 +00:00
Peng Xiao
b925731bf7 fix: add sidebar toggle and windows controls for empty collections page (#5304)
Before this change, when the user gets to an empty collection page & hide the sidebar, there is no sidebar toggle any longer.
Also added windows app control on windows.
2023-12-14 07:13:01 +00:00
Peng Xiao
3efcdc0cc5 fix: detail page missing background (#5303)
before

![CleanShot 2023-12-14 at 14.43.16@2x.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/T2klNLEk0wxLh4NRDzhk/53900c68-c050-4336-80fb-cd121dcf4d53.png)

after

![CleanShot 2023-12-14 at 14.42.44@2x.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/T2klNLEk0wxLh4NRDzhk/bd820718-8032-4a30-b250-6541084830be.png)
2023-12-14 06:50:21 +00:00
Peng Xiao
0dc9358972 fix: page title too long style (#5302)
![CleanShot 2023-12-14 at 14.14.30@2x.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/T2klNLEk0wxLh4NRDzhk/72beba1a-28a2-4192-a676-eea476140940.png)
2023-12-14 06:31:59 +00:00
EYHN
8aac1e09e2 feat(server): independent websocket room for block and awareness (#5285) 2023-12-13 10:31:07 +00:00
DarkSky
77a5552dcd feat: user usage init (#5074) 2023-12-13 09:21:14 +00:00
Peng Xiao
098787bd0c fix(core): collection modal position after modal style changes (#5289)
fix https://github.com/toeverything/AFFiNE/issues/5270
fix TOV-161
2023-12-13 08:14:55 +00:00
Peng Xiao
cd2efb4f0b chore: remove react-resizable-panels (#5284) 2023-12-13 07:52:02 +00:00
Peng Xiao
ce64685176 refactor(core): side bar resizing (#5280)
Rewrite sidebar panel using a customized react-resizable-panels version that supports sidebar pixel sizing (not using flex percentages).

Now the left & right sidebar using the same `ResizePanel` impl.

fix https://github.com/toeverything/AFFiNE/issues/5271
fix TOV-163
fix TOV-146
fix TOV-168
fix TOV-109
fix TOV-165
2023-12-13 07:52:01 +00:00
Peng Xiao
2a9a6855f4 test(core): rewrite some flaky assertions (#5287) 2023-12-13 07:51:59 +00:00
Peng Xiao
ad2c254ca3 fix(core): simple history entry position (#5290)
fix AFF-377
2023-12-13 07:33:15 +00:00
LongYinan
e4369c7f0b chore: bump up @endo/static-module-record version to v1 (#5281)
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@endo/static-module-record](https://togithub.com/endojs/endo/tree/master/packages/static-module-record#readme) ([source](https://togithub.com/endojs/endo)) | [`^0.8.2` -> `^1.0.0`](https://renovatebot.com/diffs/npm/@endo%2fstatic-module-record/0.8.2/1.0.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@endo%2fstatic-module-record/1.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@endo%2fstatic-module-record/1.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@endo%2fstatic-module-record/0.8.2/1.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@endo%2fstatic-module-record/0.8.2/1.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>endojs/endo (@&#8203;endo/static-module-record)</summary>

### [`v1.0.0`](https://togithub.com/endojs/endo/compare/@endo/static-module-record@0.8.2...@endo/static-module-record@1.0.0)

[Compare Source](https://togithub.com/endojs/endo/compare/@endo/static-module-record@0.8.2...@endo/static-module-record@1.0.0)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/toeverything/AFFiNE).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy44Ny4yIiwidXBkYXRlZEluVmVyIjoiMzcuODcuMiIsInRhcmdldEJyYW5jaCI6ImNhbmFyeSJ9-->
2023-12-13 07:24:11 +00:00
Peng Xiao
883ab46557 fix(core): bg color issues in transparent mode (#5278)
fix the following style issue
![CleanShot 2023-12-13 at 00.06.13@2x.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/T2klNLEk0wxLh4NRDzhk/46f5e766-e6c9-4965-bab7-6fd6dbb2c651.png)
2023-12-13 07:14:24 +00:00
JimmFly
7d32ddf539 fix(core): fix window cannot be dragged on the collection page (#5269)
close #5268
close TOV-162

https://github.com/toeverything/AFFiNE/assets/102217452/d8f606c0-4c18-4a7e-be3f-d2e611ffecbf
2023-12-13 05:26:28 +00:00
Peng Xiao
31dc1f5e00 fix(electron): use dynamic load for exposed meta (#5251)
There is high possibilities of  circular dependencies when importing `exposed-meta` module. Change it to dynamic import to mitigate the issue..
2023-12-13 05:17:17 +00:00
Peng Xiao
c9f900b69c fix(core): page header style changes (#5279)
![CleanShot 2023-12-13 at 00.09.19@2x.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/T2klNLEk0wxLh4NRDzhk/476fe4de-4066-4b1d-8823-d23a31ef692b.png)
2023-12-13 05:06:29 +00:00
regischen
738302be40 fix(edgeless): remove stale template (#5275) 2023-12-13 12:54:26 +08:00
liuyi
797cd5c6eb fix(server): avoid repeatly register providers (#5265) 2023-12-13 02:12:38 +00:00
JimmFly
f4a52c031f feat(core): support sidebar page item dnd (#5132)
Added the ability to drag page items from the `all pages` view to the sidebar, including `favourites,` `collection` and `trash`. Page items in `favourites` and `collection` can also be dragged between each other. However, linked subpages cannot be dragged.

Additionally, an operation menu and ‘add’ button have been provided for the sidebar’s page items, enabling the addition of a subpage, renaming, deletion or removal from the sidebar.

On the code front, the `useSidebarDrag` hooks have been implemented for consolidating drag events. The functions `getDragItemId` and `getDropItemId` have been created, and they accept type and ID to obtain itemId.

https://github.com/toeverything/AFFiNE/assets/102217452/d06bac18-3c28-41c9-a7d4-72de955d7b11
2023-12-12 16:04:58 +00:00
EYHN
b782b3fb1b fix(core): polling to search in cmdk (#5274)
This is a temporary solution until https://github.com/toeverything/blocksuite/issues/5668 be solved.
2023-12-12 18:42:07 +08:00
Flrande
9aa33d0228 feat: bump blocksuite (#5267)
Change history: 2b5bb47...2b3d2ba
2023-12-12 06:18:30 +00:00
liuyi
bf97a07d1f fix(server): use last update creating time as snasphot update timestamp (#5266) 2023-12-12 06:03:34 +00:00
550 changed files with 18632 additions and 12268 deletions

View File

@@ -28,6 +28,7 @@ const {
REDIS_PASSWORD,
STRIPE_API_KEY,
STRIPE_WEBHOOK_KEY,
STATIC_IP_NAME,
} = process.env;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -35,17 +36,13 @@ const buildType = BUILD_TYPE || 'canary';
const isProduction = buildType === 'stable';
const isBeta = buildType === 'beta';
const isInternal = buildType === 'internal';
const createHelmCommand = ({ isDryRun }) => {
const flag = isDryRun ? '--dry-run' : '--atomic';
const imageTag = `${buildType}-${GIT_SHORT_HASH}`;
const staticIpName = isProduction
? 'affine-cluster-production'
: isBeta
? 'affine-cluster-beta'
: 'affine-cluster-dev';
const redisAndPostgres =
isProduction || isBeta
isProduction || isBeta || isInternal
? [
`--set-string global.database.url=${DATABASE_URL}`,
`--set-string global.database.user=${DATABASE_USERNAME}`,
@@ -59,26 +56,32 @@ const createHelmCommand = ({ isDryRun }) => {
]
: [];
const serviceAnnotations =
isProduction || isBeta
isProduction || isBeta || isInternal
? [
`--set-json web.service.annotations=\"{ \\"cloud.google.com/neg\\": \\"{\\\\\\"ingress\\\\\\": true}\\" }\"`,
`--set-json graphql.serviceAccount.annotations=\"{ \\"iam.gke.io/gcp-service-account\\": \\"${CLOUD_SQL_IAM_ACCOUNT}\\" }\"`,
`--set-json graphql.service.annotations=\"{ \\"cloud.google.com/neg\\": \\"{\\\\\\"ingress\\\\\\": true}\\" }\"`,
`--set-json sync.serviceAccount.annotations=\"{ \\"iam.gke.io/gcp-service-account\\": \\"${CLOUD_SQL_IAM_ACCOUNT}\\" }\"`,
`--set-json sync.service.annotations=\"{ \\"cloud.google.com/neg\\": \\"{\\\\\\"ingress\\\\\\": true}\\" }\"`,
`--set-json cloud-sql-proxy.serviceAccount.annotations=\"{ \\"iam.gke.io/gcp-service-account\\": \\"${CLOUD_SQL_IAM_ACCOUNT}\\" }\"`,
`--set-json cloud-sql-proxy.nodeSelector=\"{ \\"iam.gke.io/gke-metadata-server-enabled\\": \\"true\\" }\"`,
]
: [];
const webReplicaCount = isProduction ? 3 : isBeta ? 2 : 2;
const graphqlReplicaCount = isProduction ? 10 : isBeta ? 5 : 2;
const syncReplicaCount = isProduction ? 10 : isBeta ? 5 : 2;
const namespace = isProduction ? 'production' : isBeta ? 'beta' : 'dev';
const namespace = isProduction
? 'production'
: isBeta
? 'beta'
: isInternal
? 'internal'
: 'dev';
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const host = DEPLOY_HOST || CANARY_DEPLOY_HOST;
const deployCommand = [
`helm upgrade --install affine .github/helm/affine`,
`--namespace ${namespace}`,
`--set global.ingress.enabled=true`,
`--set-json global.ingress.annotations=\"{ \\"kubernetes.io/ingress.class\\": \\"gce\\", \\"kubernetes.io/ingress.allow-http\\": \\"true\\", \\"kubernetes.io/ingress.global-static-ip-name\\": \\"${staticIpName}\\" }\"`,
`--set-json global.ingress.annotations=\"{ \\"kubernetes.io/ingress.class\\": \\"gce\\", \\"kubernetes.io/ingress.allow-http\\": \\"true\\", \\"kubernetes.io/ingress.global-static-ip-name\\": \\"${STATIC_IP_NAME}\\" }\"`,
`--set-string global.ingress.host="${host}"`,
`--set-string global.version="${APP_VERSION}"`,
...redisAndPostgres,

View File

@@ -0,0 +1,20 @@
name: Setup Version
description: 'Setup Version'
runs:
using: 'composite'
steps:
- name: 'Write Version'
id: version
shell: bash
run: |
if [ "${{ github.ref_type }}" == "tag" ]; then
APP_VERSION=$(echo "${{ github.ref_name }}" | sed 's/^v//')
else
PACKAGE_VERSION=$(node -p "require('./package.json').version")
TIME_VERSION=$(date +%Y%m%d%H%M)
GIT_SHORT_HASH=$(git rev-parse --short HEAD)
APP_VERSION=$PACKAGE_VERSION-nightly-$TIME_VERSION-$GIT_SHORT_HASH
fi
echo $APP_VERSION
echo "APP_VERSION=$APP_VERSION" >> "$GITHUB_OUTPUT"
./scripts/set-version.sh $APP_VERSION

View File

@@ -3,4 +3,4 @@ name: affine
description: AFFiNE cloud chart
type: application
version: 0.0.0
appVersion: "0.10.3-canary.2"
appVersion: "0.11.0"

View File

@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/

View File

@@ -0,0 +1,6 @@
apiVersion: v2
name: cloud-sql-proxy
description: Google Cloud SQL Proxy
type: application
version: 0.0.0
appVersion: "2.8.1"

View File

@@ -0,0 +1,18 @@
{{- if .Values.global.database.gcloud.enabled -}}
1. Get the application URL by running these commands:
{{- if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "gcloud-sql-proxy.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "gcloud-sql-proxy.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "gcloud-sql-proxy.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "gcloud-sql-proxy.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}
{{- end }}

View File

@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "gcloud-sql-proxy.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "gcloud-sql-proxy.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "gcloud-sql-proxy.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Common labels
*/}}
{{- define "gcloud-sql-proxy.labels" -}}
helm.sh/chart: {{ include "gcloud-sql-proxy.chart" . }}
{{ include "gcloud-sql-proxy.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "gcloud-sql-proxy.selectorLabels" -}}
app.kubernetes.io/name: {{ include "gcloud-sql-proxy.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{/*
Create the name of the service account to use
*/}}
{{- define "gcloud-sql-proxy.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "gcloud-sql-proxy.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

View File

@@ -0,0 +1,132 @@
{{- if .Values.global.database.gcloud.enabled -}}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "gcloud-sql-proxy.fullname" . }}
labels:
{{- include "gcloud-sql-proxy.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "gcloud-sql-proxy.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "gcloud-sql-proxy.labels" . | nindent 8 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "gcloud-sql-proxy.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
args:
- "--address"
- "0.0.0.0"
- "--structured-logs"
- "--auto-iam-authn"
- "{{ .Values.global.database.gcloud.connectionName }}"
env:
# Enable HTTP healthchecks on port 9801. This enables /liveness,
# /readiness and /startup health check endpoints. Allow connections
# listen for connections on any interface (0.0.0.0) so that the
# k8s management components can reach these endpoints.
- name: CSQL_PROXY_HEALTH_CHECK
value: "true"
- name: CSQL_PROXY_HTTP_PORT
value: "9801"
- name: CSQL_PROXY_HTTP_ADDRESS
value: 0.0.0.0
ports:
- name: cloud-sql-proxy
containerPort: {{ .Values.global.database.gcloud.proxyPort }}
protocol: TCP
- containerPort: 9801
protocol: TCP
# The /startup probe returns OK when the proxy is ready to receive
# connections from the application. In this example, k8s will check
# once a second for 60 seconds.
startupProbe:
failureThreshold: 60
httpGet:
path: /startup
port: 9801
scheme: HTTP
periodSeconds: 1
successThreshold: 1
timeoutSeconds: 10
# The /liveness probe returns OK as soon as the proxy application has
# begun its startup process and continues to return OK until the
# process stops.
livenessProbe:
failureThreshold: 3
httpGet:
path: /liveness
port: 9801
scheme: HTTP
# The probe will be checked every 10 seconds.
periodSeconds: 10
# Number of times the probe is allowed to fail before the transition
# from healthy to failure state.
#
# If periodSeconds = 60, 5 tries will result in five minutes of
# checks. The proxy starts to refresh a certificate five minutes
# before its expiration. If those five minutes lapse without a
# successful refresh, the liveness probe will fail and the pod will be
# restarted.
successThreshold: 1
# The probe will fail if it does not respond in 10 seconds
timeoutSeconds: 10
readinessProbe:
# The /readiness probe returns OK when the proxy can establish
# a new connections to its databases.
httpGet:
path: /readiness
port: 9801
initialDelaySeconds: 10
periodSeconds: 10
timeoutSeconds: 10
# Number of times the probe must report success to transition from failure to healthy state.
# Defaults to 1 for readiness probe.
successThreshold: 1
failureThreshold: 6
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.volumeMounts }}
volumeMounts:
{{- toYaml . | nindent 12 }}
{{- end }}
{{- with .Values.volumes }}
volumes:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- end }}

View File

@@ -0,0 +1,17 @@
{{- if .Values.global.database.gcloud.enabled -}}
apiVersion: v1
kind: Service
metadata:
name: {{ include "gcloud-sql-proxy.fullname" . }}
labels:
{{- include "gcloud-sql-proxy.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.global.database.port }}
targetPort: cloud-sql-proxy
protocol: TCP
name: cloud-sql-proxy
selector:
{{- include "gcloud-sql-proxy.selectorLabels" . | nindent 4 }}
{{- end }}

View File

@@ -0,0 +1,15 @@
{{- if .Values.global.database.gcloud.enabled -}}
{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ include "gcloud-sql-proxy.serviceAccountName" . }}
labels:
{{- include "gcloud-sql-proxy.labels" . | nindent 4 }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
automountServiceAccountToken: {{ .Values.serviceAccount.automount }}
{{- end }}
{{- end }}

View File

@@ -0,0 +1,17 @@
{{- if .Values.global.database.gcloud.enabled -}}
apiVersion: v1
kind: Pod
metadata:
name: "{{ include "gcloud-sql-proxy.fullname" . }}-test-connection"
labels:
{{- include "gcloud-sql-proxy.labels" . | nindent 4 }}
annotations:
"helm.sh/hook": test
spec:
containers:
- name: wget
image: busybox
command: ['wget']
args: ['{{ include "gcloud-sql-proxy.fullname" . }}:{{ .Values.service.port }}']
restartPolicy: Never
{{- end }}

View File

@@ -0,0 +1,40 @@
replicaCount: 3
image:
# the tag is defined as chart appVersion.
repository: gcr.io/cloud-sql-connectors/cloud-sql-proxy
pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
create: true
automount: true
annotations: {}
name: ""
podAnnotations: {}
podLabels: {}
podSecurityContext:
fsGroup: 2000
securityContext:
runAsNonRoot: true
service:
type: ClusterIP
port: 5432
resources:
limits:
memory: "4Gi"
cpu: "2"
volumes: []
volumeMounts: []
nodeSelector: {}
tolerations: []
affinity: {}

View File

@@ -3,4 +3,9 @@ name: graphql
description: AFFiNE GraphQL server
type: application
version: 0.0.0
appVersion: "0.10.3-canary.2"
appVersion: "0.11.0"
dependencies:
- name: gcloud-sql-proxy
version: 0.0.0
repository: "file://../gcloud-sql-proxy"
condition: .global.database.gcloud.enabled

View File

@@ -189,20 +189,6 @@ spec:
initialDelaySeconds: {{ .Values.probe.initialDelaySeconds }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{ if .Values.global.database.gcloud.enabled }}
- name: cloud-sql-proxy
image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.6.0
args:
- "--structured-logs"
- "--auto-iam-authn"
- "{{ .Values.global.database.gcloud.connectionName }}"
securityContext:
runAsNonRoot: true
resources:
requests:
memory: "2Gi"
cpu: "1"
{{ end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}

View File

@@ -1,6 +1,11 @@
apiVersion: v2
name: sync
description: A Helm chart for Kubernetes
description: AFFiNE Sync Server
type: application
version: 0.0.0
appVersion: "0.10.3-canary.2"
appVersion: "0.11.0"
dependencies:
- name: gcloud-sql-proxy
version: 0.0.0
repository: "file://../gcloud-sql-proxy"
condition: .global.database.gcloud.enabled

View File

@@ -82,20 +82,6 @@ spec:
initialDelaySeconds: {{ .Values.probe.initialDelaySeconds }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{ if .Values.global.database.gcloud.enabled }}
- name: cloud-sql-proxy
image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.6.0
args:
- "--structured-logs"
- "--auto-iam-authn"
- "{{ .Values.global.database.gcloud.connectionName }}"
securityContext:
runAsNonRoot: true
resources:
requests:
memory: "2Gi"
cpu: "1"
{{ end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}

View File

@@ -16,6 +16,8 @@ global:
cloudSqlInternal: ''
connectionName: ''
serviceAccount: ''
cloudProxyReplicas: 3
proxyPort: '5432'
redis:
enabled: true
host: 'redis-master'

View File

@@ -45,7 +45,7 @@ jobs:
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -58,7 +58,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3
# Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
@@ -71,7 +71,7 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
lint:
name: Lint
runs-on: ubuntu-latest
@@ -80,7 +80,7 @@ jobs:
- uses: actions/checkout@v4
- name: Run oxlint
# oxlint is fast, so wrong code will fail quickly
run: yarn dlx $(node -e "console.log(require('./package.json').scripts['lint:ox'])")
run: yarn dlx $(node -e "console.log(require('./package.json').scripts['lint:ox'].replace('oxlint', 'oxlint@' + require('./package.json').devDependencies.oxlint))")
- name: Setup Node.js
uses: ./.github/actions/setup-node
with:
@@ -374,7 +374,9 @@ jobs:
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
- name: Run init-db script
run: yarn workspace @affine/server exec node --loader ts-node/esm/transpile-only ./scripts/init-db.ts
run: |
yarn workspace @affine/server data-migration run
yarn workspace @affine/server exec node --loader ts-node/esm/transpile-only ./scripts/init-db.ts
env:
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
@@ -464,7 +466,9 @@ jobs:
DATABASE_URL: postgresql://affine:affine@localhost:5432/affine
- name: Run init-db script
run: yarn workspace @affine/server exec node --loader ts-node/esm/transpile-only ./scripts/init-db.ts
run: |
yarn workspace @affine/server data-migration run
yarn workspace @affine/server exec node --loader ts-node/esm/transpile-only ./scripts/init-db.ts
- name: Download storage.node
uses: actions/download-artifact@v3
with:

View File

@@ -4,10 +4,14 @@ on:
workflow_dispatch:
inputs:
flavor:
description: 'Build type (canary, beta, or stable)'
type: string
description: 'Select what enverionment to deploy to'
type: choice
default: canary
options:
- canary
- beta
- stable
- internal
env:
APP_NAME: affine
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
@@ -18,6 +22,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Version
uses: ./.github/actions/setup-version
- name: Setup Node.js
uses: ./.github/actions/setup-node
with:
@@ -36,6 +42,8 @@ jobs:
environment: ${{ github.event.inputs.flavor }}
steps:
- uses: actions/checkout@v4
- name: Setup Version
uses: ./.github/actions/setup-version
- name: Setup Node.js
uses: ./.github/actions/setup-node
- name: Build Plugins
@@ -67,6 +75,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Setup Version
uses: ./.github/actions/setup-version
- name: Setup Node.js
uses: ./.github/actions/setup-node
- name: Build Rust
@@ -88,6 +98,8 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Setup Version
uses: ./.github/actions/setup-version
- name: Setup Node.js
uses: ./.github/actions/setup-node
- name: Build Rust
@@ -207,12 +219,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: setup deploy version
id: version
run: |
export APP_VERSION=`node -e "console.log(require('./package.json').version)"`
echo $APP_VERSION
echo "APP_VERSION=$APP_VERSION" >> "$GITHUB_OUTPUT"
- name: Setup Version
uses: ./.github/actions/setup-version
- name: Deploy to ${{ github.event.inputs.flavor }}
uses: ./.github/actions/deploy
with:
@@ -249,3 +257,4 @@ jobs:
CLOUD_SQL_IAM_ACCOUNT: ${{ secrets.CLOUD_SQL_IAM_ACCOUNT }}
STRIPE_API_KEY: ${{ secrets.STRIPE_API_KEY }}
STRIPE_WEBHOOK_KEY: ${{ secrets.STRIPE_WEBHOOK_KEY }}
STATIC_IP_NAME: ${{ secrets.STATIC_IP_NAME }}

View File

@@ -11,7 +11,7 @@ jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy
environment: production
environment: stable
steps:
- uses: actions/checkout@v4
- name: Publish

8
Cargo.lock generated
View File

@@ -3318,18 +3318,18 @@ dependencies = [
[[package]]
name = "zerocopy"
version = "0.7.26"
version = "0.7.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e97e415490559a91254a2979b4829267a57d2fcd741a98eee8b722fb57289aa0"
checksum = "1c4061bedbb353041c12f413700357bec76df2c7e2ca8e4df8bac24c6bf68e3d"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.26"
version = "0.7.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd7e48ccf166952882ca8bd778a43502c64f33bf94c12ebe2a7f08e5a0f6689f"
checksum = "b3c129550b3e6de3fd0ba67ba5c81818f9805e58b8d7fee80a3a59d2c9fc601a"
dependencies = [
"proc-macro2",
"quote",

View File

@@ -1,6 +1,6 @@
{
"name": "@affine/monorepo",
"version": "0.10.3-canary.2",
"version": "0.11.0",
"private": true,
"author": "toeverything",
"license": "MIT",
@@ -33,7 +33,7 @@
"lint:eslint:fix": "yarn lint:eslint --fix",
"lint:prettier": "prettier --ignore-unknown --cache --check .",
"lint:prettier:fix": "prettier --ignore-unknown --cache --write .",
"lint:ox": "oxlint --deny-warnings --import-plugin -D correctness -D nursery -D prefer-array-some -D no-useless-promise-resolve-reject -A no-undef -A consistent-type-exports -A default -A named -A ban-ts-comment",
"lint:ox": "oxlint --deny-warnings -D correctness -D nursery -D prefer-array-some -D no-useless-promise-resolve-reject -A no-undef -A consistent-type-exports -A default -A named -A ban-ts-comment",
"lint": "yarn lint:eslint && yarn lint:prettier",
"lint:fix": "yarn lint:eslint:fix && yarn lint:prettier:fix",
"test": "vitest --run",
@@ -78,8 +78,8 @@
"@vanilla-extract/vite-plugin": "^3.9.2",
"@vanilla-extract/webpack-plugin": "^2.3.1",
"@vitejs/plugin-react-swc": "^3.5.0",
"@vitest/coverage-istanbul": "0.34.6",
"@vitest/ui": "0.34.6",
"@vitest/coverage-istanbul": "1.0.4",
"@vitest/ui": "1.0.4",
"electron": "^27.1.0",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
@@ -100,7 +100,7 @@
"nx": "^17.1.3",
"nx-cloud": "^16.5.2",
"nyc": "^15.1.0",
"oxlint": "^0.0.18",
"oxlint": "0.0.21",
"prettier": "^3.1.0",
"semver": "^7.5.4",
"serve": "^14.2.1",
@@ -111,7 +111,7 @@
"vite-plugin-istanbul": "^5.0.0",
"vite-plugin-static-copy": "^1.0.0",
"vite-tsconfig-paths": "^4.2.1",
"vitest": "0.34.6",
"vitest": "1.0.4",
"vitest-fetch-mock": "^0.2.2",
"vitest-mock-extended": "^1.3.1"
},

View File

@@ -0,0 +1,45 @@
/*
Warnings:
- You are about to drop the `user_feature_gates` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "user_feature_gates" DROP CONSTRAINT "user_feature_gates_user_id_fkey";
-- DropTable
DROP TABLE "user_feature_gates";
-- CreateTable
CREATE TABLE "user_features" (
"id" SERIAL NOT NULL,
"user_id" VARCHAR(36) NOT NULL,
"feature_id" INTEGER NOT NULL,
"reason" VARCHAR NOT NULL,
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"expired_at" TIMESTAMPTZ(6),
"activated" BOOLEAN NOT NULL DEFAULT false,
CONSTRAINT "user_features_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "features" (
"id" SERIAL NOT NULL,
"feature" VARCHAR NOT NULL,
"version" INTEGER NOT NULL DEFAULT 0,
"type" INTEGER NOT NULL,
"configs" JSON NOT NULL,
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "features_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "features_feature_version_key" ON "features"("feature", "version");
-- AddForeignKey
ALTER TABLE "user_features" ADD CONSTRAINT "user_features_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "user_features" ADD CONSTRAINT "user_features_feature_id_fkey" FOREIGN KEY ("feature_id") REFERENCES "features"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -1,7 +1,7 @@
{
"name": "@affine/server",
"private": true,
"version": "0.10.3-canary.2",
"version": "0.11.0",
"description": "Affine Node.js server",
"type": "module",
"bin": {
@@ -82,7 +82,8 @@
"socket.io": "^4.7.2",
"stripe": "^14.5.0",
"ws": "^8.14.2",
"yjs": "^13.6.10"
"yjs": "^13.6.10",
"zod": "^3.22.4"
},
"devDependencies": {
"@affine-test/kit": "workspace:*",
@@ -135,7 +136,8 @@
"ENABLE_LOCAL_EMAIL": "true",
"OAUTH_EMAIL_LOGIN": "noreply@toeverything.info",
"OAUTH_EMAIL_PASSWORD": "affine",
"OAUTH_EMAIL_SENDER": "noreply@toeverything.info"
"OAUTH_EMAIL_SENDER": "noreply@toeverything.info",
"FEATURES_EARLY_ACCESS_PREVIEW": "false"
}
},
"nodemonConfig": {

View File

@@ -22,7 +22,7 @@ model User {
accounts Account[]
sessions Session[]
features UserFeatureGates[]
features UserFeatures[]
customer UserStripeCustomer?
subscription UserSubscription?
invoices UserInvoice[]
@@ -113,15 +113,48 @@ model WorkspacePageUserPermission {
@@map("workspace_page_user_permissions")
}
model UserFeatureGates {
id String @id @default(uuid()) @db.VarChar
userId String @map("user_id") @db.VarChar
feature String @db.VarChar
reason String @db.VarChar
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
// feature gates is a way to enable/disable features for a user
// for example:
// - early access is a feature that allow some users to access the insider version
// - pro plan is a quota that allow some users access to more resources after they pay
model UserFeatures {
id Int @id @default(autoincrement())
userId String @map("user_id") @db.VarChar(36)
featureId Int @map("feature_id") @db.Integer
@@map("user_feature_gates")
// we will record the reason why the feature is enabled/disabled
// for example:
// - pro_plan_v1: "user buy the pro plan"
reason String @db.VarChar
// record the quota enabled time
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
// record the quota expired time, pay plan is a subscription, so it will expired
expiredAt DateTime? @map("expired_at") @db.Timestamptz(6)
// whether the feature is activated
// for example:
// - if we switch the user to another plan, we will set the old plan to deactivated, but dont delete it
activated Boolean @default(false)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
feature Features @relation(fields: [featureId], references: [id], onDelete: Cascade)
@@map("user_features")
}
model Features {
id Int @id @default(autoincrement())
feature String @db.VarChar
version Int @default(0) @db.Integer
// 0: feature, 1: quota
type Int @db.Integer
// configs, define by feature conntroller
configs Json @db.Json
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
UserFeatureGates UserFeatures[]
@@unique([feature, version])
@@map("features")
}
model Account {

View File

@@ -8,6 +8,20 @@ async function main() {
data: {
...userA,
password: await hash(userA.password),
features: {
create: {
reason: 'created by api sign up',
activated: true,
feature: {
connect: {
feature_version: {
feature: 'free_plan_v1',
version: 1,
},
},
},
},
},
},
});
}

View File

@@ -1,7 +1,8 @@
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { AppController } from './app.controller';
import { CacheModule } from './cache';
import { CacheInterceptor, CacheModule } from './cache';
import { ConfigModule } from './config';
import { EventModule } from './event';
import { BusinessModules } from './modules';
@@ -23,6 +24,12 @@ const BasicModules = [
];
@Module({
providers: [
{
provide: APP_INTERCEPTOR,
useClass: CacheInterceptor,
},
],
imports: [...BasicModules, ...BusinessModules],
controllers: [AppController],
})

View File

@@ -22,3 +22,5 @@ const CacheProvider: FactoryProvider = {
})
export class CacheModule {}
export { LocalCache as Cache };
export { CacheInterceptor, MakeCache, PreventCache } from './interceptor';

View File

@@ -0,0 +1,99 @@
import {
CallHandler,
ExecutionContext,
Injectable,
Logger,
NestInterceptor,
SetMetadata,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { GqlContextType, GqlExecutionContext } from '@nestjs/graphql';
import { mergeMap, Observable, of } from 'rxjs';
import { LocalCache } from './cache';
export const MakeCache = (key: string[], args?: string[]) =>
SetMetadata('cacheKey', [key, args]);
export const PreventCache = (key: string[], args?: string[]) =>
SetMetadata('preventCache', [key, args]);
type CacheConfig = [string[], string[]?];
@Injectable()
export class CacheInterceptor implements NestInterceptor {
private readonly logger = new Logger(CacheInterceptor.name);
constructor(
private readonly reflector: Reflector,
private readonly cache: LocalCache
) {}
async intercept(
ctx: ExecutionContext,
next: CallHandler<any>
): Promise<Observable<any>> {
const key = this.reflector.get<CacheConfig | undefined>(
'cacheKey',
ctx.getHandler()
);
const preventKey = this.reflector.get<CacheConfig | undefined>(
'preventCache',
ctx.getHandler()
);
if (preventKey) {
this.logger.debug(`prevent cache: ${JSON.stringify(preventKey)}`);
const key = await this.getCacheKey(ctx, preventKey);
if (key) {
await this.cache.delete(key);
}
return next.handle();
} else if (!key) {
return next.handle();
}
const cacheKey = await this.getCacheKey(ctx, key);
if (!cacheKey) {
return next.handle();
}
const cachedData = await this.cache.get(cacheKey);
if (cachedData) {
this.logger.debug('cache hit', cacheKey, cachedData);
return of(cachedData);
} else {
return next.handle().pipe(
mergeMap(async result => {
this.logger.debug('cache miss', cacheKey, result);
await this.cache.set(cacheKey, result);
return result;
})
);
}
}
private async getCacheKey(
ctx: ExecutionContext,
config: CacheConfig
): Promise<string | null> {
const [key, params] = config;
if (!params) {
return key.join(':');
} else if (ctx.getType<GqlContextType>() === 'graphql') {
const args = GqlExecutionContext.create(ctx).getArgs();
const cacheKey = params
.map(name => args[name])
.filter(v => v)
.join(':');
if (cacheKey) {
return [...key, cacheKey].join(':');
} else {
return key.join(':');
}
}
return null;
}
}

View File

@@ -16,6 +16,8 @@ export enum ExternalAccount {
firebase = 'firebase',
}
export type ServerFlavor = 'allinone' | 'graphql' | 'sync' | 'selfhosted';
type EnvConfigType = 'string' | 'int' | 'float' | 'boolean';
type ConfigPaths = LeafPaths<
Omit<
@@ -186,11 +188,6 @@ export interface AFFiNEConfig {
fs: {
path: string;
};
/**
* default storage quota
* @default 10 * 1024 * 1024 * 1024 (10GB)
*/
quota: number;
};
/**
@@ -345,6 +342,11 @@ export interface AFFiNEConfig {
doc: {
manager: {
/**
* Whether auto merge updates into doc snapshot.
*/
enableUpdateAutoMerging: boolean;
/**
* How often the [DocManager] will start a new turn of merging pending updates into doc snapshot.
*

View File

@@ -7,9 +7,12 @@ import { join } from 'node:path';
import parse from 'parse-duration';
import pkg from '../../package.json' assert { type: 'json' };
import type { AFFiNEConfig } from './def';
import type { AFFiNEConfig, ServerFlavor } from './def';
import { applyEnvToConfig } from './env';
export const SERVER_FLAVOR = (process.env.SERVER_FLAVOR ??
'allinone') as ServerFlavor;
// Don't use this in production
export const examplePrivateKey = `-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIEtyAJLIULkphVhqXqxk4Nr8Ggty3XLwUJWBxzAWCWTMoAoGCCqGSM49
@@ -55,7 +58,6 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
AFFINE_SERVER_HOST: 'host',
AFFINE_SERVER_SUB_PATH: 'path',
AFFINE_ENV: 'affineEnv',
AFFINE_FREE_USER_QUOTA: 'objectStorage.quota',
DATABASE_URL: 'db.url',
ENABLE_R2_OBJECT_STORAGE: ['objectStorage.r2.enabled', 'boolean'],
R2_OBJECT_STORAGE_ACCOUNT_ID: 'objectStorage.r2.accountId',
@@ -189,8 +191,6 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
fs: {
path: join(homedir(), '.affine-storage'),
},
// 10GB
quota: 10 * 1024 * 1024 * 1024,
},
rateLimiter: {
ttl: 60,
@@ -206,6 +206,7 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
},
doc: {
manager: {
enableUpdateAutoMerging: SERVER_FLAVOR !== 'sync',
updatePollInterval: 3000,
experimentalMergeWithJwstCodec: false,
},

View File

@@ -73,3 +73,4 @@ export class ConfigModule {
}
export type { AFFiNEConfig } from './def';
export { SERVER_FLAVOR } from './default';

View File

@@ -14,7 +14,7 @@ interface Migration {
down: (db: PrismaService) => Promise<void>;
}
async function collectMigrations(): Promise<Migration[]> {
export async function collectMigrations(): Promise<Migration[]> {
const folder = join(fileURLToPath(import.meta.url), '../../migrations');
const migrationFiles = readdirSync(folder)
@@ -64,35 +64,8 @@ export class RunCommand extends CommandRunner {
continue;
}
this.logger.log(`Running ${migration.name}...`);
const record = await this.db.dataMigration.create({
data: {
name: migration.name,
startedAt: new Date(),
},
});
await this.runMigration(migration);
try {
await migration.up(this.db);
} catch (e) {
await this.db.dataMigration.delete({
where: {
id: record.id,
},
});
await migration.down(this.db);
this.logger.error('Failed to run data migration', e);
process.exit(1);
}
await this.db.dataMigration.update({
where: {
id: record.id,
},
data: {
finishedAt: new Date(),
},
});
done.push(migration);
}
@@ -101,6 +74,56 @@ export class RunCommand extends CommandRunner {
this.logger.log(`${migration.name}`);
});
}
async runOne(name: string) {
const migrations = await collectMigrations();
const migration = migrations.find(m => m.name === name);
if (!migration) {
throw new Error(`Unknown migration name: ${name}.`);
}
const exists = await this.db.dataMigration.count({
where: {
name: migration.name,
},
});
if (exists) return;
await this.runMigration(migration);
}
private async runMigration(migration: Migration) {
this.logger.log(`Running ${migration.name}...`);
const record = await this.db.dataMigration.create({
data: {
name: migration.name,
startedAt: new Date(),
},
});
try {
await migration.up(this.db);
} catch (e) {
await this.db.dataMigration.delete({
where: {
id: record.id,
},
});
await migration.down(this.db);
this.logger.error('Failed to run data migration', e);
process.exit(1);
}
await this.db.dataMigration.update({
where: {
id: record.id,
},
data: {
finishedAt: new Date(),
},
});
}
}
@Command({

View File

@@ -0,0 +1,122 @@
import { Prisma } from '@prisma/client';
import {
CommonFeature,
FeatureKind,
Features,
FeatureType,
} from '../../modules/features';
import { Quotas } from '../../modules/quota/schema';
import { PrismaService } from '../../prisma';
export class UserFeaturesInit1698652531198 {
// do the migration
static async up(db: PrismaService) {
// upgrade features from lower version to higher version
for (const feature of Features) {
await upsertFeature(db, feature);
}
await migrateNewFeatureTable(db);
for (const quota of Quotas) {
await upsertFeature(db, quota);
}
}
// revert the migration
static async down(_db: PrismaService) {
// TODO: revert the migration
}
}
// upgrade features from lower version to higher version
async function upsertFeature(
db: PrismaService,
feature: CommonFeature
): Promise<void> {
const hasEqualOrGreaterVersion =
(await db.features.count({
where: {
feature: feature.feature,
version: {
gte: feature.version,
},
},
})) > 0;
// will not update exists version
if (!hasEqualOrGreaterVersion) {
await db.features.create({
data: {
feature: feature.feature,
type: feature.type,
version: feature.version,
configs: feature.configs as Prisma.InputJsonValue,
},
});
}
}
async function migrateNewFeatureTable(prisma: PrismaService) {
const waitingList = await prisma.newFeaturesWaitingList.findMany();
for (const oldUser of waitingList) {
const user = await prisma.user.findFirst({
where: {
email: oldUser.email,
},
});
if (user) {
const hasEarlyAccess = await prisma.userFeatures.count({
where: {
userId: user.id,
feature: {
feature: FeatureType.EarlyAccess,
},
activated: true,
},
});
if (hasEarlyAccess === 0) {
await prisma.$transaction(async tx => {
const latestFlag = await tx.userFeatures.findFirst({
where: {
userId: user.id,
feature: {
feature: FeatureType.EarlyAccess,
type: FeatureKind.Feature,
},
activated: true,
},
orderBy: {
createdAt: 'desc',
},
});
if (latestFlag) {
return latestFlag.id;
} else {
return tx.userFeatures
.create({
data: {
reason: 'Early access user',
activated: true,
user: {
connect: {
id: user.id,
},
},
feature: {
connect: {
feature_version: {
feature: FeatureType.EarlyAccess,
version: 1,
},
type: FeatureKind.Feature,
},
},
},
})
.then(r => r.id);
}
});
}
}
}
}

View File

@@ -0,0 +1,37 @@
import { QuotaType } from '../../modules/quota/types';
import { PrismaService } from '../../prisma';
export class OldUserFeature1702620653283 {
// do the migration
static async up(db: PrismaService) {
await db.$transaction(async tx => {
const latestFreePlan = await tx.features.findFirstOrThrow({
where: { feature: QuotaType.FreePlanV1 },
orderBy: { version: 'desc' },
select: { id: true },
});
// find all users that don't have any features
const userIds = await db.user.findMany({
where: { NOT: { features: { some: { NOT: { id: { gt: 0 } } } } } },
select: { id: true },
});
console.log(`migrating ${userIds.join('|')} users`);
await tx.userFeatures.createMany({
data: userIds.map(({ id: userId }) => ({
userId,
featureId: latestFreePlan.id,
reason: 'old user feature migration',
activated: true,
})),
});
});
}
// revert the migration
// WARN: this will drop all user features
static async down(db: PrismaService) {
await db.userFeatures.deleteMany({});
}
}

View File

@@ -28,12 +28,10 @@ export class EventEmitter {
}
}
export const OnEvent = (
export const OnEvent = RawOnEvent as (
event: Event,
opts?: Parameters<typeof RawOnEvent>[1]
) => {
return RawOnEvent(event, opts);
};
) => MethodDecorator;
@Global()
@Module({

View File

@@ -29,6 +29,7 @@ import { GQLLoggerPlugin } from './graphql/logger-plugin';
context: ({ req, res }: { req: Request; res: Response }) => ({
req,
res,
isAdminQuery: false,
}),
plugins: [new GQLLoggerPlugin()],
};

View File

@@ -72,15 +72,11 @@ export class MailService {
invitationInfo.workspace.name
}</span></p><p style="margin-top:8px;margin-bottom:0;">Click button to join this workspace</p>`;
const subContent =
'Currently, AFFiNE Cloud is in the early access stage. Only Early Access Sponsors can register and log in to AFFiNE Cloud. <a href="https://community.affine.pro/c/insider-general/" style="color: #1e67af" >Please click here for more information.</a>';
const html = emailTemplate({
title: 'You are invited!',
content,
buttonContent: 'Accept & Join',
buttonUrl,
subContent,
});
return this.sendMail({

View File

@@ -11,8 +11,8 @@ import Google from 'next-auth/providers/google';
import { Config } from '../../config';
import { PrismaService } from '../../prisma';
import { SessionService } from '../../session';
import { NewFeaturesKind } from '../users/types';
import { isStaff } from '../users/utils';
import { FeatureType } from '../features';
import { Quota_FreePlanV1 } from '../quota';
import { MailService } from './mailer';
import {
decode,
@@ -44,6 +44,17 @@ export const NextAuthOptionsProvider: FactoryProvider<NextAuthOptions> = {
email: data.email,
avatarUrl: '',
emailVerified: data.emailVerified,
features: {
create: {
reason: 'created by email sign up',
activated: true,
feature: {
connect: {
feature_version: Quota_FreePlanV1,
},
},
},
},
};
if (data.email && !data.name) {
userData.name = data.email.split('@')[0];
@@ -223,18 +234,23 @@ export const NextAuthOptionsProvider: FactoryProvider<NextAuthOptions> = {
}
const email = profile?.email ?? user.email;
if (email) {
if (isStaff(email)) {
return true;
}
return prisma.newFeaturesWaitingList
.findUnique({
// FIXME: cannot inject FeatureManagementService here
// it will cause prisma.account to be undefined
// then prismaAdapter.getUserByAccount will throw error
if (email.endsWith('@toeverything.info')) return true;
return prisma.userFeatures
.count({
where: {
email,
type: NewFeaturesKind.EarlyAccess,
user: {
email,
},
feature: {
feature: FeatureType.EarlyAccess,
},
activated: true,
},
})
.then(user => !!user)
.catch(() => false);
.then(count => count > 0);
}
return false;
},
@@ -242,6 +258,10 @@ export const NextAuthOptionsProvider: FactoryProvider<NextAuthOptions> = {
return url;
},
};
nextAuthOptions.pages = {
newUser: '/auth/onboarding',
};
return nextAuthOptions;
},
inject: [Config, PrismaService, MailService, SessionService],

View File

@@ -19,7 +19,7 @@ import { nanoid } from 'nanoid';
import { Config } from '../../config';
import { SessionService } from '../../session';
import { CloudThrottlerGuard, Throttle } from '../../throttler';
import { UserType } from '../users/resolver';
import { UserType } from '../users';
import { Auth, CurrentUser } from './guard';
import { AuthService } from './service';

View File

@@ -14,6 +14,7 @@ import { nanoid } from 'nanoid';
import { Config } from '../../config';
import { PrismaService } from '../../prisma';
import { verifyChallengeResponse } from '../../storage';
import { Quota_FreePlanV1 } from '../quota';
import { MailService } from './mailer';
export type UserClaim = Pick<
@@ -190,6 +191,17 @@ export class AuthService {
name,
email,
password: hashedPassword,
features: {
create: {
reason: 'created by api sign up',
activated: true,
feature: {
connect: {
feature_version: Quota_FreePlanV1,
},
},
},
},
},
});
}
@@ -209,6 +221,17 @@ export class AuthService {
data: {
name: 'Unnamed',
email,
features: {
create: {
reason: 'created by invite sign up',
activated: true,
feature: {
connect: {
feature_version: Quota_FreePlanV1,
},
},
},
},
},
});
}
@@ -258,6 +281,7 @@ export class AuthService {
},
});
}
async changeEmail(id: string, newEmail: string): Promise<User> {
const user = await this.prisma.user.findUnique({
where: {

View File

@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common';
import { Field, ObjectType, Query } from '@nestjs/graphql';
export const { SERVER_FLAVOR } = process.env;
import { SERVER_FLAVOR } from '../config';
@ObjectType()
export class ServerConfigType {
@@ -19,7 +19,7 @@ export class ServerConfigResolver {
serverConfig(): ServerConfigType {
return {
version: AFFiNE.version,
flavor: SERVER_FLAVOR || 'allinone',
flavor: SERVER_FLAVOR,
};
}
}

View File

@@ -7,7 +7,7 @@ import { Config } from '../../config';
import { type EventPayload, OnEvent } from '../../event';
import { metrics } from '../../metrics';
import { PrismaService } from '../../prisma';
import { SubscriptionStatus } from '../payment/service';
import { QuotaService } from '../quota';
import { Permission } from '../workspaces/types';
import { isEmptyBuffer } from './manager';
@@ -16,7 +16,8 @@ export class DocHistoryManager {
private readonly logger = new Logger(DocHistoryManager.name);
constructor(
private readonly config: Config,
private readonly db: PrismaService
private readonly db: PrismaService,
private readonly quota: QuotaService
) {}
@OnEvent('workspace.deleted')
@@ -222,9 +223,6 @@ export class DocHistoryManager {
return history.timestamp;
}
/**
* @todo(@darkskygit) refactor with [Usage Control] system
*/
async getExpiredDateFromNow(workspaceId: string) {
const permission = await this.db.workspaceUserPermission.findFirst({
select: {
@@ -241,25 +239,8 @@ export class DocHistoryManager {
throw new Error('Workspace owner not found');
}
const sub = await this.db.userSubscription.findFirst({
select: {
id: true,
},
where: {
userId: permission.userId,
status: SubscriptionStatus.Active,
},
});
return new Date(
Date.now() +
1000 *
60 *
60 *
24 *
// 30 days for subscription user, 7 days for free user
(sub ? 30 : 7)
);
const quota = await this.quota.getUserQuota(permission.userId);
return quota.feature.historyPeriodFromNow;
}
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT /* everyday at 12am */)

View File

@@ -1,38 +1,14 @@
import { DynamicModule } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { QuotaModule } from '../quota';
import { DocHistoryManager } from './history';
import { DocManager } from './manager';
export class DocModule {
/**
* @param automation whether enable update merging automation logic
*/
private static defModule(automation = true): DynamicModule {
return {
module: DocModule,
providers: [
{
provide: 'DOC_MANAGER_AUTOMATION',
useValue: automation,
},
DocManager,
DocHistoryManager,
],
exports: [DocManager, DocHistoryManager],
};
}
static forRoot() {
return this.defModule();
}
static forSync(): DynamicModule {
return this.defModule(false);
}
static forFeature(): DynamicModule {
return this.defModule(false);
}
}
@Module({
imports: [QuotaModule],
providers: [DocManager, DocHistoryManager],
exports: [DocManager, DocHistoryManager],
})
export class DocModule {}
export { DocHistoryManager, DocManager };

View File

@@ -1,5 +1,4 @@
import {
Inject,
Injectable,
Logger,
OnModuleDestroy,
@@ -97,8 +96,6 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
private busy = false;
constructor(
@Inject('DOC_MANAGER_AUTOMATION')
private readonly automation: boolean,
private readonly db: PrismaService,
private readonly config: Config,
private readonly cache: Cache,
@@ -106,7 +103,7 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
) {}
onModuleInit() {
if (this.automation) {
if (this.config.doc.manager.enableUpdateAutoMerging) {
this.logger.log('Use Database');
this.setup();
}
@@ -464,6 +461,9 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
workspaceId: string,
guid: string,
doc: Doc,
// we always delay the snapshot update to avoid db overload,
// so the value of `updatedAt` will not be accurate to user's real action time
updatedAt: Date,
initialSeq?: number
) {
return this.lockSnapshotForUpsert(workspaceId, guid, async () => {
@@ -502,6 +502,7 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
data: {
blob,
state,
updatedAt,
},
});
@@ -521,6 +522,8 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
blob,
state,
seq: initialSeq,
createdAt: updatedAt,
updatedAt,
},
});
@@ -565,7 +568,13 @@ export class DocManager implements OnModuleInit, OnModuleDestroy {
...updates.map(u => u.blob)
);
const done = await this.upsert(workspaceId, id, doc, last.seq);
const done = await this.upsert(
workspaceId,
id,
doc,
last.createdAt,
last.seq
);
if (done) {
if (snapshot) {

View File

@@ -0,0 +1,78 @@
import { PrismaService } from '../../prisma';
import { Feature, FeatureSchema, FeatureType } from './types';
class FeatureConfig {
readonly config: Feature;
constructor(data: any) {
const config = FeatureSchema.safeParse(data);
if (config.success) {
this.config = config.data;
} else {
throw new Error(`Invalid quota config: ${config.error.message}`);
}
}
/// feature name of quota
get name() {
return this.config.feature;
}
}
export class EarlyAccessFeatureConfig extends FeatureConfig {
constructor(data: any) {
super(data);
if (this.config.feature !== FeatureType.EarlyAccess) {
throw new Error('Invalid feature config: type is not EarlyAccess');
}
}
checkWhiteList(email: string) {
for (const domain in this.config.configs.whitelist) {
if (email.endsWith(domain)) {
return true;
}
}
return false;
}
}
const FeatureConfigMap = {
[FeatureType.EarlyAccess]: EarlyAccessFeatureConfig,
};
const FeatureCache = new Map<
number,
InstanceType<(typeof FeatureConfigMap)[FeatureType]>
>();
export async function getFeature(prisma: PrismaService, featureId: number) {
const cachedQuota = FeatureCache.get(featureId);
if (cachedQuota) {
return cachedQuota;
}
const feature = await prisma.features.findFirst({
where: {
id: featureId,
},
});
if (!feature) {
// this should unreachable
throw new Error(`Quota config ${featureId} not found`);
}
const ConfigClass = FeatureConfigMap[feature.feature as FeatureType];
if (!ConfigClass) {
throw new Error(`Feature config ${featureId} not found`);
}
const config = new ConfigClass(feature);
// we always edit quota config as a new quota config
// so we can cache it by featureId
FeatureCache.set(featureId, config);
return config;
}

View File

@@ -0,0 +1,21 @@
import { Module } from '@nestjs/common';
import { PrismaService } from '../../prisma';
import { FeatureManagementService } from './management';
import { FeatureService } from './service';
/**
* Feature module provider pre-user feature flag management.
* includes:
* - feature query/update/permit
* - feature statistics
*/
@Module({
providers: [FeatureService, FeatureManagementService],
exports: [FeatureService, FeatureManagementService],
})
export class FeatureModule {}
export { type CommonFeature, commonFeatureSchema } from './types';
export { FeatureKind, Features, FeatureType } from './types';
export { FeatureManagementService, FeatureService, PrismaService };

View File

@@ -0,0 +1,89 @@
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { Config } from '../../config';
import { PrismaService } from '../../prisma';
import { EarlyAccessFeatureConfig } from './feature';
import { FeatureService } from './service';
import { FeatureType } from './types';
enum NewFeaturesKind {
EarlyAccess,
}
@Injectable()
export class FeatureManagementService implements OnModuleInit {
protected logger = new Logger(FeatureManagementService.name);
private earlyAccessFeature?: EarlyAccessFeatureConfig;
constructor(
private readonly feature: FeatureService,
private readonly prisma: PrismaService,
private readonly config: Config
) {}
async onModuleInit() {
this.earlyAccessFeature = await this.feature.getFeature(
FeatureType.EarlyAccess
);
}
// ======== Admin ========
// todo(@darkskygit): replace this with abac
isStaff(email: string) {
return this.earlyAccessFeature?.checkWhiteList(email) ?? false;
}
// ======== Early Access ========
async addEarlyAccess(userId: string) {
return this.feature.addUserFeature(
userId,
FeatureType.EarlyAccess,
1,
'Early access user'
);
}
async removeEarlyAccess(userId: string) {
return this.feature.removeUserFeature(userId, FeatureType.EarlyAccess);
}
async listEarlyAccess() {
return this.feature.listFeatureUsers(FeatureType.EarlyAccess);
}
/// check early access by email
async canEarlyAccess(email: string) {
if (this.config.featureFlags.earlyAccessPreview && !this.isStaff(email)) {
const user = await this.prisma.user.findFirst({
where: {
email,
},
});
if (user) {
const canEarlyAccess = await this.feature
.hasFeature(user.id, FeatureType.EarlyAccess)
.catch(() => false);
if (canEarlyAccess) {
return true;
}
// TODO: Outdated, switch to feature gates
const oldCanEarlyAccess = await this.prisma.newFeaturesWaitingList
.findUnique({
where: { email, type: NewFeaturesKind.EarlyAccess },
})
.then(x => !!x)
.catch(() => false);
if (oldCanEarlyAccess) {
this.logger.warn(
`User ${email} has early access in old table but not in new table`
);
}
return oldCanEarlyAccess;
}
return false;
} else {
return true;
}
}
}

View File

@@ -0,0 +1,184 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../prisma';
import { UserType } from '../users/types';
import { getFeature } from './feature';
import { FeatureKind, FeatureType } from './types';
@Injectable()
export class FeatureService {
constructor(private readonly prisma: PrismaService) {}
async getFeaturesVersion() {
const features = await this.prisma.features.findMany({
where: {
type: FeatureKind.Feature,
},
select: {
feature: true,
version: true,
},
});
return features.reduce(
(acc, feature) => {
acc[feature.feature] = feature.version;
return acc;
},
{} as Record<string, number>
);
}
async getFeature(feature: FeatureType) {
const data = await this.prisma.features.findFirst({
where: {
feature,
type: FeatureKind.Feature,
},
select: { id: true },
orderBy: {
version: 'desc',
},
});
if (data) {
return getFeature(this.prisma, data.id);
}
return undefined;
}
async addUserFeature(
userId: string,
feature: FeatureType,
version: number,
reason: string,
expiredAt?: Date | string
) {
return this.prisma.$transaction(async tx => {
const latestFlag = await tx.userFeatures.findFirst({
where: {
userId,
feature: {
feature,
type: FeatureKind.Feature,
},
activated: true,
},
orderBy: {
createdAt: 'desc',
},
});
if (latestFlag) {
return latestFlag.id;
} else {
return tx.userFeatures
.create({
data: {
reason,
expiredAt,
activated: true,
user: {
connect: {
id: userId,
},
},
feature: {
connect: {
feature_version: {
feature,
version,
},
type: FeatureKind.Feature,
},
},
},
})
.then(r => r.id);
}
});
}
async removeUserFeature(userId: string, feature: FeatureType) {
return this.prisma.userFeatures
.updateMany({
where: {
userId,
feature: {
feature,
type: FeatureKind.Feature,
},
activated: true,
},
data: {
activated: false,
},
})
.then(r => r.count);
}
async getUserFeatures(userId: string) {
const features = await this.prisma.userFeatures.findMany({
where: {
user: { id: userId },
feature: {
type: FeatureKind.Feature,
},
},
select: {
activated: true,
reason: true,
createdAt: true,
expiredAt: true,
featureId: true,
},
});
const configs = await Promise.all(
features.map(async feature => ({
...feature,
feature: await getFeature(this.prisma, feature.featureId),
}))
);
return configs.filter(feature => !!feature.feature);
}
async listFeatureUsers(feature: FeatureType): Promise<UserType[]> {
return this.prisma.userFeatures
.findMany({
where: {
activated: true,
feature: {
feature: feature,
type: FeatureKind.Feature,
},
},
select: {
user: {
select: {
id: true,
name: true,
avatarUrl: true,
email: true,
emailVerified: true,
createdAt: true,
},
},
},
})
.then(users => users.map(user => user.user));
}
async hasFeature(userId: string, feature: FeatureType) {
return this.prisma.userFeatures
.count({
where: {
userId,
activated: true,
feature: {
feature,
type: FeatureKind.Feature,
},
},
})
.then(count => count > 0);
}
}

View File

@@ -0,0 +1,65 @@
import { URL } from 'node:url';
import { z } from 'zod';
/// ======== common schema ========
export enum FeatureKind {
Feature,
Quota,
}
export const commonFeatureSchema = z.object({
feature: z.string(),
type: z.nativeEnum(FeatureKind),
version: z.number(),
configs: z.unknown(),
});
export type CommonFeature = z.infer<typeof commonFeatureSchema>;
/// ======== feature define ========
export enum FeatureType {
EarlyAccess = 'early_access',
}
function checkHostname(host: string) {
try {
return new URL(`https://${host}`).hostname === host;
} catch (_) {
return false;
}
}
const featureEarlyAccess = z.object({
feature: z.literal(FeatureType.EarlyAccess),
configs: z.object({
whitelist: z
.string()
.startsWith('@')
.refine(domain => checkHostname(domain.slice(1)))
.array(),
}),
});
export const Features: Feature[] = [
{
feature: FeatureType.EarlyAccess,
type: FeatureKind.Feature,
version: 1,
configs: {
whitelist: ['@toeverything.info'],
},
},
];
/// ======== schema infer ========
export const FeatureSchema = commonFeatureSchema
.extend({
type: z.literal(FeatureKind.Feature),
})
.and(z.discriminatedUnion('feature', [featureEarlyAccess]));
export type Feature = z.infer<typeof FeatureSchema>;

View File

@@ -1,10 +1,12 @@
import { DynamicModule, Type } from '@nestjs/common';
import { ScheduleModule } from '@nestjs/schedule';
import { SERVER_FLAVOR } from '../config';
import { GqlModule } from '../graphql.module';
import { SERVER_FLAVOR, ServerConfigModule } from './config';
import { ServerConfigModule } from './config';
import { DocModule } from './doc';
import { PaymentModule } from './payment';
import { QuotaModule } from './quota';
import { SelfHostedModule } from './self-hosted';
import { SyncModule } from './sync';
import { UsersModule } from './users';
@@ -14,7 +16,7 @@ const BusinessModules: (Type | DynamicModule)[] = [];
switch (SERVER_FLAVOR) {
case 'sync':
BusinessModules.push(SyncModule, DocModule.forSync());
BusinessModules.push(SyncModule, DocModule);
break;
case 'selfhosted':
BusinessModules.push(
@@ -25,7 +27,7 @@ switch (SERVER_FLAVOR) {
WorkspaceModule,
UsersModule,
SyncModule,
DocModule.forRoot()
DocModule
);
break;
case 'graphql':
@@ -35,8 +37,9 @@ switch (SERVER_FLAVOR) {
GqlModule,
WorkspaceModule,
UsersModule,
DocModule.forRoot(),
PaymentModule
DocModule,
PaymentModule,
QuotaModule
);
break;
case 'allinone':
@@ -47,8 +50,9 @@ switch (SERVER_FLAVOR) {
GqlModule,
WorkspaceModule,
UsersModule,
QuotaModule,
SyncModule,
DocModule.forRoot(),
DocModule,
PaymentModule
);
break;

View File

@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { UsersModule } from '../users';
import { FeatureModule } from '../features';
import { QuotaModule } from '../quota';
import { SubscriptionResolver, UserSubscriptionResolver } from './resolver';
import { ScheduleManager } from './schedule';
import { SubscriptionService } from './service';
@@ -8,7 +9,7 @@ import { StripeProvider } from './stripe';
import { StripeWebhook } from './webhook';
@Module({
imports: [UsersModule],
imports: [FeatureModule, QuotaModule],
providers: [
ScheduleManager,
StripeProvider,

View File

@@ -1,6 +1,7 @@
import { HttpStatus } from '@nestjs/common';
import {
Args,
Context,
Field,
Int,
Mutation,
@@ -254,8 +255,13 @@ export class UserSubscriptionResolver {
constructor(private readonly db: PrismaService) {}
@ResolveField(() => UserSubscriptionType, { nullable: true })
async subscription(@CurrentUser() me: User, @Parent() user: User) {
if (me.id !== user.id) {
async subscription(
@Context() ctx: { isAdminQuery: boolean },
@CurrentUser() me: User,
@Parent() user: User
) {
// allow admin to query other user's subscription
if (!ctx.isAdminQuery && me.id !== user.id) {
throw new GraphQLError(
'You are not allowed to access this subscription',
{

View File

@@ -11,7 +11,8 @@ import Stripe from 'stripe';
import { Config } from '../../config';
import { PrismaService } from '../../prisma';
import { UsersService } from '../users';
import { FeatureManagementService } from '../features';
import { QuotaService, QuotaType } from '../quota';
import { ScheduleManager } from './schedule';
const OnEvent = (
@@ -60,6 +61,11 @@ export enum SubscriptionStatus {
Trialing = 'trialing',
}
const SubscriptionActivated: Stripe.Subscription.Status[] = [
SubscriptionStatus.Active,
SubscriptionStatus.Trialing,
];
export enum InvoiceStatus {
Draft = 'draft',
Open = 'open',
@@ -82,8 +88,9 @@ export class SubscriptionService {
config: Config,
private readonly stripe: Stripe,
private readonly db: PrismaService,
private readonly user: UsersService,
private readonly scheduleManager: ScheduleManager
private readonly scheduleManager: ScheduleManager,
private readonly features: FeatureManagementService,
private readonly quota: QuotaService
) {
this.paymentConfig = config.payment;
@@ -471,6 +478,16 @@ export class SubscriptionService {
}
}
private getPlanQuota(plan: SubscriptionPlan) {
if (plan === SubscriptionPlan.Free) {
return QuotaType.FreePlanV1;
} else if (plan === SubscriptionPlan.Pro) {
return QuotaType.ProPlanV1;
} else {
throw new Error(`Unknown plan: ${plan}`);
}
}
private async saveSubscription(
user: User,
subscription: Stripe.Subscription,
@@ -483,23 +500,28 @@ export class SubscriptionService {
subscription = await this.stripe.subscriptions.retrieve(subscription.id);
}
// get next bill date from upcoming invoice
// see https://stripe.com/docs/api/invoices/upcoming
let nextBillAt: Date | null = null;
if (
(subscription.status === SubscriptionStatus.Active ||
subscription.status === SubscriptionStatus.Trialing) &&
!subscription.canceled_at
) {
nextBillAt = new Date(subscription.current_period_end * 1000);
}
const price = subscription.items.data[0].price;
if (!price.lookup_key) {
throw new Error('Unexpected subscription with no key');
}
const [plan, recurring] = decodeLookupKey(price.lookup_key);
const planActivated = SubscriptionActivated.includes(subscription.status);
let nextBillAt: Date | null = null;
if (planActivated) {
// update user's quota if plan activated
await this.quota.switchUserQuota(user.id, this.getPlanQuota(plan));
// get next bill date from upcoming invoice
// see https://stripe.com/docs/api/invoices/upcoming
if (!subscription.canceled_at) {
nextBillAt = new Date(subscription.current_period_end * 1000);
}
} else {
// switch to free plan if subscription is canceled
await this.quota.switchUserQuota(user.id, QuotaType.FreePlanV1);
}
const commonData = {
start: new Date(subscription.current_period_start * 1000),
@@ -658,7 +680,7 @@ export class SubscriptionService {
user: User,
couponType: CouponType
): Promise<string | null> {
const earlyAccess = await this.user.isEarlyAccessUser(user.email);
const earlyAccess = await this.features.canEarlyAccess(user.email);
if (earlyAccess) {
try {
const coupon = await this.stripe.coupons.retrieve(couponType);

View File

@@ -0,0 +1,5 @@
export const OneKB = 1024;
export const OneMB = OneKB * OneKB;
export const OneGB = OneKB * OneMB;
export const OneDay = 1000 * 60 * 60 * 24;
export const ByteUnit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

View File

@@ -0,0 +1,21 @@
import { Module } from '@nestjs/common';
import { PermissionService } from '../workspaces/permission';
import { QuotaService } from './service';
import { QuotaManagementService } from './storage';
/**
* Quota module provider pre-user quota management.
* includes:
* - quota query/update/permit
* - quota statistics
*/
@Module({
providers: [PermissionService, QuotaService, QuotaManagementService],
exports: [QuotaService, QuotaManagementService],
})
export class QuotaModule {}
export { QuotaManagementService, QuotaService };
export { Quota_FreePlanV1, Quota_ProPlanV1, Quotas } from './schema';
export { QuotaType } from './types';

View File

@@ -0,0 +1,81 @@
import { PrismaService } from '../../prisma';
import { formatDate, formatSize, Quota, QuotaSchema } from './types';
const QuotaCache = new Map<number, QuotaConfig>();
export class QuotaConfig {
readonly config: Quota;
static async get(prisma: PrismaService, featureId: number) {
const cachedQuota = QuotaCache.get(featureId);
if (cachedQuota) {
return cachedQuota;
}
const quota = await prisma.features.findFirst({
where: {
id: featureId,
},
});
if (!quota) {
throw new Error(`Quota config ${featureId} not found`);
}
const config = new QuotaConfig(quota);
// we always edit quota config as a new quota config
// so we can cache it by featureId
QuotaCache.set(featureId, config);
return config;
}
private constructor(data: any) {
const config = QuotaSchema.safeParse(data);
if (config.success) {
this.config = config.data;
} else {
throw new Error(
`Invalid quota config: ${config.error.message}, ${JSON.stringify(
data
)})}`
);
}
}
/// feature name of quota
get name() {
return this.config.feature;
}
get blobLimit() {
return this.config.configs.blobLimit;
}
get storageQuota() {
return this.config.configs.storageQuota;
}
get historyPeriod() {
return this.config.configs.historyPeriod;
}
get historyPeriodFromNow() {
return new Date(Date.now() + this.historyPeriod);
}
get memberLimit() {
return this.config.configs.memberLimit;
}
get humanReadable() {
return {
name: this.config.configs.name,
blobLimit: formatSize(this.blobLimit),
storageQuota: formatSize(this.storageQuota),
historyPeriod: formatDate(this.historyPeriod),
memberLimit: this.memberLimit.toString(),
};
}
}

View File

@@ -0,0 +1,50 @@
import { FeatureKind } from '../features';
import { OneDay, OneGB, OneMB } from './constant';
import { Quota, QuotaType } from './types';
export const Quotas: Quota[] = [
{
feature: QuotaType.FreePlanV1,
type: FeatureKind.Quota,
version: 1,
configs: {
// quota name
name: 'Free',
// single blob limit 10MB
blobLimit: 10 * OneMB,
// total blob limit 10GB
storageQuota: 10 * OneGB,
// history period of validity 7 days
historyPeriod: 7 * OneDay,
// member limit 3
memberLimit: 3,
},
},
{
feature: QuotaType.ProPlanV1,
type: FeatureKind.Quota,
version: 1,
configs: {
// quota name
name: 'Pro',
// single blob limit 100MB
blobLimit: 100 * OneMB,
// total blob limit 100GB
storageQuota: 100 * OneGB,
// history period of validity 30 days
historyPeriod: 30 * OneDay,
// member limit 10
memberLimit: 10,
},
},
];
export const Quota_FreePlanV1 = {
feature: Quotas[0].feature,
version: Quotas[0].version,
};
export const Quota_ProPlanV1 = {
feature: Quotas[1].feature,
version: Quotas[1].version,
};

View File

@@ -0,0 +1,147 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../prisma';
import { FeatureKind } from '../features';
import { QuotaConfig } from './quota';
import { QuotaType } from './types';
@Injectable()
export class QuotaService {
constructor(private readonly prisma: PrismaService) {}
// get activated user quota
async getUserQuota(userId: string) {
const quota = await this.prisma.userFeatures.findFirst({
where: {
user: {
id: userId,
},
feature: {
type: FeatureKind.Quota,
},
activated: true,
},
select: {
reason: true,
createdAt: true,
expiredAt: true,
featureId: true,
},
});
if (!quota) {
// this should unreachable
throw new Error(`User ${userId} has no quota`);
}
const feature = await QuotaConfig.get(this.prisma, quota.featureId);
return { ...quota, feature };
}
// get user all quota records
async getUserQuotas(userId: string) {
const quotas = await this.prisma.userFeatures.findMany({
where: {
user: {
id: userId,
},
feature: {
type: FeatureKind.Quota,
},
},
select: {
activated: true,
reason: true,
createdAt: true,
expiredAt: true,
featureId: true,
},
});
const configs = await Promise.all(
quotas.map(async quota => {
try {
return {
...quota,
feature: await QuotaConfig.get(this.prisma, quota.featureId),
};
} catch (_) {}
return null as unknown as typeof quota & {
feature: QuotaConfig;
};
})
);
return configs.filter(quota => !!quota);
}
// switch user to a new quota
// currently each user can only have one quota
async switchUserQuota(
userId: string,
quota: QuotaType,
reason?: string,
expiredAt?: Date
) {
await this.prisma.$transaction(async tx => {
const latestPlanVersion = await tx.features.aggregate({
where: {
feature: quota,
},
_max: {
version: true,
},
});
// we will deactivate all exists quota for this user
await tx.userFeatures.updateMany({
where: {
id: undefined,
userId,
feature: {
type: FeatureKind.Quota,
},
},
data: {
activated: false,
},
});
await tx.userFeatures.create({
data: {
user: {
connect: {
id: userId,
},
},
feature: {
connect: {
feature_version: {
feature: quota,
version: latestPlanVersion._max.version || 1,
},
type: FeatureKind.Quota,
},
},
reason: reason ?? 'switch quota',
activated: true,
expiredAt,
},
});
});
}
async hasQuota(userId: string, quota: QuotaType) {
return this.prisma.userFeatures
.count({
where: {
userId,
feature: {
feature: quota,
type: FeatureKind.Quota,
},
activated: true,
},
})
.then(count => count > 0);
}
}

View File

@@ -0,0 +1,54 @@
import type { Storage } from '@affine/storage';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';
import { StorageProvide } from '../../storage';
import { PermissionService } from '../workspaces/permission';
import { QuotaService } from './service';
@Injectable()
export class QuotaManagementService {
constructor(
private readonly quota: QuotaService,
private readonly permissions: PermissionService,
@Inject(StorageProvide) private readonly storage: Storage
) {}
async getUserQuota(userId: string) {
const quota = await this.quota.getUserQuota(userId);
return {
name: quota.feature.name,
reason: quota.reason,
createAt: quota.createdAt,
expiredAt: quota.expiredAt,
blobLimit: quota.feature.blobLimit,
storageQuota: quota.feature.storageQuota,
};
}
// TODO: lazy calc, need to be optimized with cache
async getUserUsage(userId: string) {
const workspaces = await this.permissions.getOwnedWorkspaces(userId);
return this.storage.blobsSize(workspaces);
}
// get workspace's owner quota and total size of used
// quota was apply to owner's account
async getWorkspaceUsage(workspaceId: string) {
const { user: owner } =
await this.permissions.getWorkspaceOwner(workspaceId);
if (!owner) throw new NotFoundException('Workspace owner not found');
const { storageQuota } = await this.getUserQuota(owner.id);
// get all workspaces size of owner used
const usageSize = await this.getUserUsage(owner.id);
return { quota: storageQuota, size: usageSize };
}
async checkBlobQuota(workspaceId: string, size: number) {
const { quota, size: usageSize } =
await this.getWorkspaceUsage(workspaceId);
return quota - (size + usageSize);
}
}

View File

@@ -0,0 +1,50 @@
import { z } from 'zod';
import { commonFeatureSchema, FeatureKind } from '../features';
import { ByteUnit, OneDay, OneKB } from './constant';
/// ======== quota define ========
export enum QuotaType {
FreePlanV1 = 'free_plan_v1',
ProPlanV1 = 'pro_plan_v1',
}
const quotaPlan = z.object({
feature: z.enum([QuotaType.FreePlanV1, QuotaType.ProPlanV1]),
configs: z.object({
name: z.string(),
blobLimit: z.number().positive().int(),
storageQuota: z.number().positive().int(),
historyPeriod: z.number().positive().int(),
memberLimit: z.number().positive().int(),
}),
});
/// ======== schema infer ========
export const QuotaSchema = commonFeatureSchema
.extend({
type: z.literal(FeatureKind.Quota),
})
.and(z.discriminatedUnion('feature', [quotaPlan]));
export type Quota = z.infer<typeof QuotaSchema>;
/// ======== utils ========
export function formatSize(bytes: number, decimals: number = 2): string {
if (bytes === 0) return '0 B';
const dm = decimals < 0 ? 0 : decimals;
const i = Math.floor(Math.log(bytes) / Math.log(OneKB));
return (
parseFloat((bytes / Math.pow(OneKB, i)).toFixed(dm)) + ' ' + ByteUnit[i]
);
}
export function formatDate(ms: number): string {
return `${(ms / OneDay).toFixed(0)} days`;
}

View File

@@ -114,8 +114,8 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
}
@Auth()
@SubscribeMessage('client-handshake')
async handleClientHandShake(
@SubscribeMessage('client-handshake-sync')
async handleClientHandshakeSync(
@CurrentUser() user: UserType,
@MessageBody() workspaceId: string,
@ConnectedSocket() client: Socket
@@ -127,7 +127,7 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
);
if (canWrite) {
await client.join(workspaceId);
await client.join(`${workspaceId}:sync`);
return {
data: {
clientId: client.id,
@@ -140,13 +140,71 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
}
}
@SubscribeMessage('client-leave')
async handleClientLeave(
@Auth()
@SubscribeMessage('client-handshake-awareness')
async handleClientHandshakeAwareness(
@CurrentUser() user: UserType,
@MessageBody() workspaceId: string,
@ConnectedSocket() client: Socket
): Promise<EventResponse<{ clientId: string }>> {
const canWrite = await this.permissions.tryCheckWorkspace(
workspaceId,
user.id,
Permission.Write
);
if (canWrite) {
await client.join(`${workspaceId}:awareness`);
return {
data: {
clientId: client.id,
},
};
} else {
return {
error: new AccessDeniedError(workspaceId),
};
}
}
/**
* @deprecated use `client-handshake-sync` and `client-handshake-awareness` instead
*/
@Auth()
@SubscribeMessage('client-handshake')
async handleClientHandShake(
@CurrentUser() user: UserType,
@MessageBody()
workspaceId: string,
@ConnectedSocket() client: Socket
): Promise<EventResponse<{ clientId: string }>> {
const canWrite = await this.permissions.tryCheckWorkspace(
workspaceId,
user.id,
Permission.Write
);
if (canWrite) {
await client.join([`${workspaceId}:sync`, `${workspaceId}:awareness`]);
return {
data: {
clientId: client.id,
},
};
} else {
return {
error: new AccessDeniedError(workspaceId),
};
}
}
@SubscribeMessage('client-leave-sync')
async handleLeaveSync(
@MessageBody() workspaceId: string,
@ConnectedSocket() client: Socket
): Promise<EventResponse> {
if (client.rooms.has(workspaceId)) {
await client.leave(workspaceId);
if (client.rooms.has(`${workspaceId}:sync`)) {
await client.leave(`${workspaceId}:sync`);
return {};
} else {
return {
@@ -155,6 +213,38 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
}
}
@SubscribeMessage('client-leave-awareness')
async handleLeaveAwareness(
@MessageBody() workspaceId: string,
@ConnectedSocket() client: Socket
): Promise<EventResponse> {
if (client.rooms.has(`${workspaceId}:awareness`)) {
await client.leave(`${workspaceId}:awareness`);
return {};
} else {
return {
error: new NotInWorkspaceError(workspaceId),
};
}
}
/**
* @deprecated use `client-leave-sync` and `client-leave-awareness` instead
*/
@SubscribeMessage('client-leave')
async handleClientLeave(
@MessageBody() workspaceId: string,
@ConnectedSocket() client: Socket
): Promise<EventResponse> {
if (client.rooms.has(`${workspaceId}:sync`)) {
await client.leave(`${workspaceId}:sync`);
}
if (client.rooms.has(`${workspaceId}:awareness`)) {
await client.leave(`${workspaceId}:awareness`);
}
return {};
}
/**
* This is the old version of the `client-update` event without any data protocol.
* It only exists for backwards compatibility to adapt older clients.
@@ -175,7 +265,7 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
},
@ConnectedSocket() client: Socket
) {
if (!client.rooms.has(workspaceId)) {
if (!client.rooms.has(`${workspaceId}:sync`)) {
this.logger.verbose(
`Client ${client.id} tried to push update to workspace ${workspaceId} without joining it first`
);
@@ -185,12 +275,12 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
const docId = new DocID(guid, workspaceId);
client
.to(docId.workspace)
.to(`${docId.workspace}:sync`)
.emit('server-update', { workspaceId, guid, update });
// broadcast to all clients with newer version that only listen to `server-updates`
client
.to(docId.workspace)
.to(`${docId.workspace}:sync`)
.emit('server-updates', { workspaceId, guid, updates: [update] });
const buf = Buffer.from(update, 'base64');
@@ -219,7 +309,7 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
stateVector?: string;
}
): Promise<{ missing: string; state?: string } | false> {
if (!client.rooms.has(workspaceId)) {
if (!client.rooms.has(`${workspaceId}:sync`)) {
const canRead = await this.permissions.tryCheckWorkspace(
workspaceId,
user.id
@@ -264,7 +354,7 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
},
@ConnectedSocket() client: Socket
): Promise<EventResponse<{ accepted: true }>> {
if (!client.rooms.has(workspaceId)) {
if (!client.rooms.has(`${workspaceId}:sync`)) {
return {
error: new NotInWorkspaceError(workspaceId),
};
@@ -272,7 +362,7 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
const docId = new DocID(guid, workspaceId);
client
.to(docId.workspace)
.to(`${docId.workspace}:sync`)
.emit('server-updates', { workspaceId, guid, updates });
const buffers = updates.map(update => Buffer.from(update, 'base64'));
@@ -301,7 +391,7 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
stateVector?: string;
}
): Promise<EventResponse<{ missing: string; state?: string }>> {
if (!client.rooms.has(workspaceId)) {
if (!client.rooms.has(`${workspaceId}:sync`)) {
const canRead = await this.permissions.tryCheckWorkspace(
workspaceId,
user.id
@@ -343,8 +433,8 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
@MessageBody() workspaceId: string,
@ConnectedSocket() client: Socket
): Promise<EventResponse<{ clientId: string }>> {
if (client.rooms.has(workspaceId)) {
client.to(workspaceId).emit('new-client-awareness-init');
if (client.rooms.has(`${workspaceId}:awareness`)) {
client.to(`${workspaceId}:awareness`).emit('new-client-awareness-init');
return {
data: {
clientId: client.id,
@@ -362,9 +452,9 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
@MessageBody() message: { workspaceId: string; awarenessUpdate: string },
@ConnectedSocket() client: Socket
): Promise<EventResponse> {
if (client.rooms.has(message.workspaceId)) {
if (client.rooms.has(`${message.workspaceId}:awareness`)) {
client
.to(message.workspaceId)
.to(`${message.workspaceId}:awareness`)
.emit('server-awareness-broadcast', message);
return {};
} else {

View File

@@ -5,7 +5,7 @@ import { PermissionService } from '../../workspaces/permission';
import { EventsGateway } from './events.gateway';
@Module({
imports: [DocModule.forFeature()],
imports: [DocModule],
providers: [EventsGateway, PermissionService],
})
export class EventsModule {}

View File

@@ -1,42 +0,0 @@
type FeatureEarlyAccessPreview = {
whitelist: RegExp[];
};
type FeatureStorageLimit = {
storageQuota: number;
};
type UserFeatureGate = {
earlyAccessPreview: FeatureEarlyAccessPreview;
freeUser: FeatureStorageLimit;
proUser: FeatureStorageLimit;
};
const UserLevel = {
freeUser: {
storageQuota: 10 * 1024 * 1024 * 1024,
},
proUser: {
storageQuota: 100 * 1024 * 1024 * 1024,
},
} satisfies Pick<UserFeatureGate, 'freeUser' | 'proUser'>;
export function getStorageQuota(features: string[]) {
for (const feature of features) {
if (feature in UserLevel) {
return UserLevel[feature as keyof typeof UserLevel].storageQuota;
}
}
return null;
}
const UserType = {
earlyAccessPreview: {
whitelist: [/@toeverything\.info$/],
},
} satisfies Pick<UserFeatureGate, 'earlyAccessPreview'>;
export const FeatureGates = {
...UserType,
...UserLevel,
} satisfies UserFeatureGate;

View File

@@ -1,15 +1,17 @@
import { Module } from '@nestjs/common';
import { FeatureModule } from '../features';
import { QuotaModule } from '../quota';
import { StorageModule } from '../storage';
import { UserResolver } from './resolver';
import { UsersService } from './users';
@Module({
imports: [StorageModule],
imports: [StorageModule, FeatureModule, QuotaModule],
providers: [UserResolver, UsersService],
exports: [UsersService],
})
export class UsersModule {}
export { UserType } from './resolver';
export { UserType } from './types';
export { UsersService } from './users';

View File

@@ -6,13 +6,10 @@ import {
} from '@nestjs/common';
import {
Args,
Field,
ID,
Context,
Int,
Mutation,
ObjectType,
Query,
registerEnumType,
ResolveField,
Resolver,
} from '@nestjs/graphql';
@@ -24,60 +21,12 @@ import { PrismaService } from '../../prisma/service';
import { CloudThrottlerGuard, Throttle } from '../../throttler';
import type { FileUpload } from '../../types';
import { Auth, CurrentUser, Public, Publicable } from '../auth/guard';
import { AuthService } from '../auth/service';
import { FeatureManagementService } from '../features';
import { QuotaService } from '../quota';
import { StorageService } from '../storage/storage.service';
import { NewFeaturesKind } from './types';
import { DeleteAccount, RemoveAvatar, UserQuotaType, UserType } from './types';
import { UsersService } from './users';
import { isStaff } from './utils';
registerEnumType(NewFeaturesKind, {
name: 'NewFeaturesKind',
});
@ObjectType()
export class UserType implements Partial<User> {
@Field(() => ID)
id!: string;
@Field({ description: 'User name' })
name!: string;
@Field({ description: 'User email' })
email!: string;
@Field(() => String, { description: 'User avatar url', nullable: true })
avatarUrl: string | null = null;
@Field(() => Date, { description: 'User email verified', nullable: true })
emailVerified: Date | null = null;
@Field({ description: 'User created date', nullable: true })
createdAt!: Date;
@Field(() => Boolean, {
description: 'User password has been set',
nullable: true,
})
hasPassword?: boolean;
}
@ObjectType()
export class DeleteAccount {
@Field()
success!: boolean;
}
@ObjectType()
export class RemoveAvatar {
@Field()
success!: boolean;
}
@ObjectType()
export class AddToNewFeaturesWaitingList {
@Field()
email!: string;
@Field(() => NewFeaturesKind, { description: 'New features kind' })
type!: NewFeaturesKind;
}
/**
* User resolver
@@ -88,9 +37,12 @@ export class AddToNewFeaturesWaitingList {
@Resolver(() => UserType)
export class UserResolver {
constructor(
private readonly auth: AuthService,
private readonly prisma: PrismaService,
private readonly storage: StorageService,
private readonly users: UsersService
private readonly users: UsersService,
private readonly feature: FeatureManagementService,
private readonly quota: QuotaService
) {}
@Throttle({
@@ -138,7 +90,7 @@ export class UserResolver {
})
@Public()
async user(@Args('email') email: string) {
if (!(await this.users.canEarlyAccess(email))) {
if (!(await this.feature.canEarlyAccess(email))) {
return new GraphQLError(
`You don't have early access permission\nVisit https://community.affine.pro/c/insider-general/ for more information`,
{
@@ -158,6 +110,14 @@ export class UserResolver {
return user;
}
@Throttle({ default: { limit: 10, ttl: 60 } })
@ResolveField(() => UserQuotaType, { name: 'quota', nullable: true })
async getQuota(@CurrentUser() me: User) {
const quota = await this.quota.getUserQuota(me.id);
return quota.feature;
}
@Throttle({ default: { limit: 10, ttl: 60 } })
@ResolveField(() => Int, {
name: 'invoiceCount',
@@ -233,27 +193,60 @@ export class UserResolver {
ttl: 60,
},
})
@Mutation(() => AddToNewFeaturesWaitingList)
async addToNewFeaturesWaitingList(
@CurrentUser() user: UserType,
@Args('type', {
type: () => NewFeaturesKind,
})
type: NewFeaturesKind,
@Mutation(() => Int)
async addToEarlyAccess(
@CurrentUser() currentUser: UserType,
@Args('email') email: string
): Promise<AddToNewFeaturesWaitingList> {
if (!isStaff(user.email)) {
): Promise<number> {
if (!this.feature.isStaff(currentUser.email)) {
throw new ForbiddenException('You are not allowed to do this');
}
await this.prisma.newFeaturesWaitingList.create({
data: {
email,
type,
},
});
return {
email,
type,
};
const user = await this.users.findUserByEmail(email);
if (user) {
return this.feature.addEarlyAccess(user.id);
} else {
const user = await this.auth.createAnonymousUser(email);
return this.feature.addEarlyAccess(user.id);
}
}
@Throttle({
default: {
limit: 10,
ttl: 60,
},
})
@Mutation(() => Int)
async removeEarlyAccess(
@CurrentUser() currentUser: UserType,
@Args('email') email: string
): Promise<number> {
if (!this.feature.isStaff(currentUser.email)) {
throw new ForbiddenException('You are not allowed to do this');
}
const user = await this.users.findUserByEmail(email);
if (!user) {
throw new BadRequestException(`User ${email} not found`);
}
return this.feature.removeEarlyAccess(user.id);
}
@Throttle({
default: {
limit: 10,
ttl: 60,
},
})
@Query(() => [UserType])
async earlyAccessUsers(
@Context() ctx: { isAdminQuery: boolean },
@CurrentUser() user: UserType
): Promise<UserType[]> {
if (!this.feature.isStaff(user.email)) {
throw new ForbiddenException('You are not allowed to do this');
}
// allow query other user's subscription
ctx.isAdminQuery = true;
return this.feature.listEarlyAccess();
}
}

View File

@@ -1,3 +1,79 @@
export enum NewFeaturesKind {
EarlyAccess,
import { Field, Float, ID, ObjectType } from '@nestjs/graphql';
import type { User } from '@prisma/client';
@ObjectType('UserQuotaHumanReadable')
export class UserQuotaHumanReadableType {
@Field({ name: 'name' })
name!: string;
@Field({ name: 'blobLimit' })
blobLimit!: string;
@Field({ name: 'storageQuota' })
storageQuota!: string;
@Field({ name: 'historyPeriod' })
historyPeriod!: string;
@Field({ name: 'memberLimit' })
memberLimit!: string;
}
@ObjectType('UserQuota')
export class UserQuotaType {
@Field({ name: 'name' })
name!: string;
@Field(() => Float, { name: 'blobLimit' })
blobLimit!: number;
@Field(() => Float, { name: 'storageQuota' })
storageQuota!: number;
@Field(() => Float, { name: 'historyPeriod' })
historyPeriod!: number;
@Field({ name: 'memberLimit' })
memberLimit!: number;
@Field({ name: 'humanReadable' })
humanReadable!: UserQuotaHumanReadableType;
}
@ObjectType()
export class UserType implements Partial<User> {
@Field(() => ID)
id!: string;
@Field({ description: 'User name' })
name!: string;
@Field({ description: 'User email' })
email!: string;
@Field(() => String, { description: 'User avatar url', nullable: true })
avatarUrl: string | null = null;
@Field(() => Date, { description: 'User email verified', nullable: true })
emailVerified: Date | null = null;
@Field({ description: 'User created date', nullable: true })
createdAt!: Date;
@Field(() => Boolean, {
description: 'User password has been set',
nullable: true,
})
hasPassword?: boolean;
}
@ObjectType()
export class DeleteAccount {
@Field()
success!: boolean;
}
@ObjectType()
export class RemoveAvatar {
@Field()
success!: boolean;
}

View File

@@ -1,51 +1,10 @@
import { Injectable } from '@nestjs/common';
import { Config } from '../../config';
import { PrismaService } from '../../prisma';
import { getStorageQuota } from './gates';
import { NewFeaturesKind } from './types';
import { isStaff } from './utils';
@Injectable()
export class UsersService {
constructor(
private readonly prisma: PrismaService,
private readonly config: Config
) {}
async canEarlyAccess(email: string) {
if (this.config.featureFlags.earlyAccessPreview && !isStaff(email)) {
return this.isEarlyAccessUser(email);
} else {
return true;
}
}
async isEarlyAccessUser(email: string) {
return this.prisma.newFeaturesWaitingList
.count({
where: { email, type: NewFeaturesKind.EarlyAccess },
})
.then(count => count > 0)
.catch(() => false);
}
async getStorageQuotaById(id: string) {
const features = await this.prisma.user
.findUnique({
where: { id },
select: {
features: {
select: {
feature: true,
},
},
},
})
.then(user => user?.features.map(f => f.feature) ?? []);
return getStorageQuota(features) || this.config.objectStorage.quota;
}
constructor(private readonly prisma: PrismaService) {}
async findUserByEmail(email: string) {
return this.prisma.user

View File

@@ -1,3 +0,0 @@
export function isStaff(email: string) {
return email.endsWith('@toeverything.info');
}

View File

@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { DocModule } from '../doc';
import { QuotaModule } from '../quota';
import { UsersService } from '../users';
import { WorkspacesController } from './controller';
import { DocHistoryResolver } from './history.resolver';
@@ -8,7 +9,7 @@ import { PermissionService } from './permission';
import { PagePermissionResolver, WorkspaceResolver } from './resolver';
@Module({
imports: [DocModule.forFeature()],
imports: [DocModule, QuotaModule],
controllers: [WorkspacesController],
providers: [
WorkspaceResolver,

View File

@@ -26,6 +26,18 @@ export class PermissionService {
return data?.type as Permission;
}
async getOwnedWorkspaces(userId: string) {
return this.prisma.workspaceUserPermission
.findMany({
where: {
userId,
accepted: true,
type: Permission.Owner,
},
})
.then(data => data.map(({ workspaceId }) => workspaceId));
}
async getWorkspaceOwner(workspaceId: string) {
return this.prisma.workspaceUserPermission.findFirstOrThrow({
where: {

View File

@@ -33,6 +33,7 @@ import type {
import GraphQLUpload from 'graphql-upload/GraphQLUpload.mjs';
import { applyUpdate, Doc } from 'yjs';
import { MakeCache, PreventCache } from '../../cache';
import { EventEmitter } from '../../event';
import { PrismaService } from '../../prisma';
import { StorageProvide } from '../../storage';
@@ -42,8 +43,8 @@ import { DocID } from '../../utils/doc';
import { Auth, CurrentUser, Public } from '../auth';
import { MailService } from '../auth/mailer';
import { AuthService } from '../auth/service';
import { UsersService } from '../users';
import { UserType } from '../users/resolver';
import { QuotaManagementService } from '../quota';
import { UsersService, UserType } from '../users';
import { PermissionService, PublicPageMode } from './permission';
import { Permission } from './types';
import { defaultWorkspaceAvatar } from './utils';
@@ -148,6 +149,7 @@ export class WorkspaceResolver {
private readonly permissions: PermissionService,
private readonly users: UsersService,
private readonly event: EventEmitter,
private readonly quota: QuotaManagementService,
@Inject(StorageProvide) private readonly storage: Storage
) {}
@@ -233,6 +235,14 @@ export class WorkspaceResolver {
}));
}
@ResolveField(() => Int, {
description: 'Blobs size of workspace',
complexity: 2,
})
async blobsSize(@Parent() workspace: WorkspaceType) {
return this.storage.blobsSize([workspace.id]);
}
@Query(() => Boolean, {
description: 'Get is owner of workspace',
complexity: 2,
@@ -647,6 +657,7 @@ export class WorkspaceResolver {
@Query(() => [String], {
description: 'List blobs of workspace',
})
@MakeCache(['blobs'], ['workspaceId'])
async listBlobs(
@CurrentUser() user: UserType,
@Args('workspaceId') workspaceId: string
@@ -656,36 +667,9 @@ export class WorkspaceResolver {
return this.storage.listBlobs(workspaceId);
}
@Query(() => WorkspaceBlobSizes)
async collectBlobSizes(
@CurrentUser() user: UserType,
@Args('workspaceId') workspaceId: string
) {
await this.permissions.checkWorkspace(workspaceId, user.id);
return this.storage.blobsSize([workspaceId]).then(size => ({ size }));
}
@Query(() => WorkspaceBlobSizes)
async collectAllBlobSizes(@CurrentUser() user: UserType) {
const workspaces = await this.prisma.workspaceUserPermission
.findMany({
where: {
userId: user.id,
accepted: true,
type: Permission.Owner,
},
select: {
workspace: {
select: {
id: true,
},
},
},
})
.then(data => data.map(({ workspace }) => workspace.id));
const size = await this.storage.blobsSize(workspaces);
const size = await this.quota.getUserUsage(user.id);
return { size };
}
@@ -693,7 +677,7 @@ export class WorkspaceResolver {
async checkBlobSize(
@CurrentUser() user: UserType,
@Args('workspaceId') workspaceId: string,
@Args('size', { type: () => Float }) size: number
@Args('size', { type: () => Float }) blobSize: number
) {
const canWrite = await this.permissions.tryCheckWorkspace(
workspaceId,
@@ -701,18 +685,14 @@ export class WorkspaceResolver {
Permission.Write
);
if (canWrite) {
const { user } = await this.permissions.getWorkspaceOwner(workspaceId);
if (user) {
const quota = await this.users.getStorageQuotaById(user.id);
const { size: currentSize } = await this.collectAllBlobSizes(user);
return { size: quota - (size + currentSize) };
}
const size = await this.quota.checkBlobQuota(workspaceId, blobSize);
return { size };
}
return false;
}
@Mutation(() => String)
@PreventCache(['blobs'], ['workspaceId'])
async setBlob(
@CurrentUser() user: UserType,
@Args('workspaceId') workspaceId: string,
@@ -725,14 +705,12 @@ export class WorkspaceResolver {
Permission.Write
);
// quota was apply to owner's account
const { user: owner } =
await this.permissions.getWorkspaceOwner(workspaceId);
if (!owner) return new NotFoundException('Workspace owner not found');
const quota = await this.users.getStorageQuotaById(owner.id);
const { size } = await this.collectAllBlobSizes(owner);
const { quota, size } = await this.quota.getWorkspaceUsage(workspaceId);
const checkExceeded = (recvSize: number) => {
if (!quota) {
throw new ForbiddenException('cannot find user quota');
}
if (size + recvSize > quota) {
this.logger.log(
`storage size limit exceeded: ${size + recvSize} > ${quota}`
@@ -774,6 +752,7 @@ export class WorkspaceResolver {
}
@Mutation(() => Boolean)
@PreventCache(['blobs'], ['workspaceId'])
async deleteBlob(
@CurrentUser() user: UserType,
@Args('workspaceId') workspaceId: string,

View File

@@ -10,6 +10,23 @@ type ServerConfigType {
flavor: String!
}
type UserQuotaHumanReadable {
name: String!
blobLimit: String!
storageQuota: String!
historyPeriod: String!
memberLimit: String!
}
type UserQuota {
name: String!
blobLimit: Float!
storageQuota: Float!
historyPeriod: Float!
memberLimit: Int!
humanReadable: UserQuotaHumanReadable!
}
type UserType {
id: ID!
@@ -31,6 +48,7 @@ type UserType {
"""User password has been set"""
hasPassword: Boolean
token: TokenType!
quota: UserQuota
"""Get user invoice count"""
invoiceCount: Int!
@@ -51,17 +69,6 @@ type RemoveAvatar {
success: Boolean!
}
type AddToNewFeaturesWaitingList {
email: String!
"""New features kind"""
type: NewFeaturesKind!
}
enum NewFeaturesKind {
EarlyAccess
}
type TokenType {
token: String!
refresh: String!
@@ -196,6 +203,9 @@ type WorkspaceType {
"""Owner of workspace"""
owner: UserType!
"""Blobs size of workspace"""
blobsSize: Int!
"""Shared pages of workspace"""
sharedPages: [String!]! @deprecated(reason: "use WorkspaceType.publicPages")
@@ -269,7 +279,6 @@ type Query {
"""List blobs of workspace"""
listBlobs(workspaceId: String!): [String!]!
collectBlobSizes(workspaceId: String!): WorkspaceBlobSizes!
collectAllBlobSizes: WorkspaceBlobSizes!
checkBlobSize(workspaceId: String!, size: Float!): WorkspaceBlobSizes!
@@ -278,6 +287,7 @@ type Query {
"""Get user by email"""
user(email: String!): UserType
earlyAccessUsers: [UserType!]!
prices: [SubscriptionPrice!]!
}
@@ -315,7 +325,8 @@ type Mutation {
"""Remove user avatar"""
removeAvatar: RemoveAvatar!
deleteAccount: DeleteAccount!
addToNewFeaturesWaitingList(type: NewFeaturesKind!, email: String!): AddToNewFeaturesWaitingList!
addToEarlyAccess(email: String!): Int!
removeEarlyAccess(email: String!): Int!
"""Create a subscription checkout link of stripe"""
checkout(recurring: SubscriptionRecurring!, idempotencyKey: String!): String!

View File

@@ -9,14 +9,13 @@ import {
import Redis from 'ioredis';
import { ThrottlerStorageRedisService } from 'nestjs-throttler-storage-redis';
import { Config, ConfigModule } from './config';
import { Config } from './config';
import { getRequestResponseFromContext } from './utils/nestjs';
@Global()
@Module({
imports: [
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [Config],
useFactory: (config: Config): ThrottlerModuleOptions => {
const options: ThrottlerModuleOptions = {

View File

@@ -12,6 +12,7 @@ import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import request from 'supertest';
import { AppModule } from '../src/app';
import { FeatureManagementService } from '../src/modules/features';
import { PrismaService } from '../src/prisma/service';
const gql = '/graphql';
@@ -45,6 +46,13 @@ class FakePrisma {
},
};
}
get newFeaturesWaitingList() {
return {
async findUnique() {
return null;
},
};
}
}
test.beforeEach(async t => {
@@ -53,6 +61,8 @@ test.beforeEach(async t => {
})
.overrideProvider(PrismaService)
.useClass(FakePrisma)
.overrideProvider(FeatureManagementService)
.useValue({ canEarlyAccess: () => true })
.compile();
t.context.app = module.createNestApplication({
cors: true,

View File

@@ -9,11 +9,13 @@ import ava, { type TestFn } from 'ava';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppModule } from '../src/app';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { MailService } from '../src/modules/auth/mailer';
import { AuthService } from '../src/modules/auth/service';
import {
changeEmail,
createWorkspace,
initFeatureConfigs,
sendChangeEmail,
sendVerifyChangeEmail,
signUp,
@@ -37,6 +39,7 @@ test.beforeEach(async t => {
await client.$disconnect();
const module = await Test.createTestingModule({
imports: [AppModule],
providers: [RevertCommand, RunCommand],
}).compile();
const app = module.createNestApplication();
app.use(
@@ -52,6 +55,9 @@ test.beforeEach(async t => {
t.context.app = app;
t.context.auth = auth;
t.context.mail = mail;
// init features
await initFeatureConfigs(module);
});
test.afterEach(async t => {

View File

@@ -4,6 +4,7 @@ import { PrismaClient } from '@prisma/client';
import test from 'ava';
import { ConfigModule } from '../src/config';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { GqlModule } from '../src/graphql.module';
import { AuthModule } from '../src/modules/auth';
import { AuthResolver } from '../src/modules/auth/resolver';
@@ -11,6 +12,7 @@ import { AuthService } from '../src/modules/auth/service';
import { PrismaModule } from '../src/prisma';
import { mintChallengeResponse, verifyChallengeResponse } from '../src/storage';
import { RateLimiterModule } from '../src/throttler';
import { initFeatureConfigs } from './utils';
let authService: AuthService;
let authResolver: AuthResolver;
@@ -40,10 +42,15 @@ test.beforeEach(async () => {
GqlModule,
AuthModule,
RateLimiterModule,
RevertCommand,
RunCommand,
],
}).compile();
authService = module.get(AuthService);
authResolver = module.get(AuthResolver);
// init features
await initFeatureConfigs(module);
});
test.afterEach.always(async () => {

View File

@@ -14,10 +14,16 @@ import {
import { CacheModule } from '../src/cache';
import { Config, ConfigModule } from '../src/config';
import {
collectMigrations,
RevertCommand,
RunCommand,
} from '../src/data/commands/run';
import { EventModule } from '../src/event';
import { DocManager, DocModule } from '../src/modules/doc';
import { QuotaModule } from '../src/modules/quota';
import { PrismaModule, PrismaService } from '../src/prisma';
import { flushDB } from './utils';
import { FakeStorageModule, flushDB } from './utils';
const createModule = () => {
return Test.createTestingModule({
@@ -25,8 +31,12 @@ const createModule = () => {
PrismaModule,
CacheModule,
EventModule,
QuotaModule,
FakeStorageModule.forRoot(),
ConfigModule.forRoot(),
DocModule.forRoot(),
DocModule,
RevertCommand,
RunCommand,
],
}).compile();
};
@@ -45,6 +55,13 @@ test.beforeEach(async () => {
app = m.createNestApplication();
app.enableShutdownHooks();
await app.init();
// init features
const run = m.get(RunCommand);
const revert = m.get(RevertCommand);
const migrations = await collectMigrations();
await Promise.allSettled(migrations.map(m => revert.run([m.name])));
await run.run();
});
test.afterEach.always(async () => {

View File

@@ -6,6 +6,7 @@ import request from 'supertest';
import { AppModule } from '../src/app';
import { ExceptionLogger } from '../src/middleware/exception-logger';
import { FeatureManagementService } from '../src/modules/features';
import { PrismaService } from '../src/prisma';
const gql = '/graphql';
@@ -38,6 +39,8 @@ test.beforeEach(async () => {
})
.overrideProvider(PrismaService)
.useClass(FakePrisma)
.overrideProvider(FeatureManagementService)
.useValue({})
.compile();
app = module.createNestApplication({
cors: true,

View File

@@ -0,0 +1,143 @@
/// <reference types="../src/global.d.ts" />
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import { ConfigModule } from '../src/config';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { AuthModule } from '../src/modules/auth';
import { AuthService } from '../src/modules/auth/service';
import {
FeatureManagementService,
FeatureModule,
FeatureService,
FeatureType,
} from '../src/modules/features';
import { PrismaModule } from '../src/prisma';
import { RateLimiterModule } from '../src/throttler';
import { initFeatureConfigs } from './utils';
const test = ava as TestFn<{
auth: AuthService;
feature: FeatureService;
early_access: FeatureManagementService;
app: TestingModule;
}>;
// cleanup database before each test
test.beforeEach(async () => {
const client = new PrismaClient();
await client.$connect();
await client.user.deleteMany({});
await client.$disconnect();
});
test.beforeEach(async t => {
const module = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
auth: {
accessTokenExpiresIn: 1,
refreshTokenExpiresIn: 1,
leeway: 1,
},
host: 'example.org',
https: true,
featureFlags: {
earlyAccessPreview: true,
},
}),
PrismaModule,
AuthModule,
FeatureModule,
RateLimiterModule,
RevertCommand,
RunCommand,
],
}).compile();
t.context.app = module;
t.context.auth = module.get(AuthService);
t.context.feature = module.get(FeatureService);
t.context.early_access = module.get(FeatureManagementService);
// init features
await initFeatureConfigs(module);
});
test.afterEach.always(async t => {
await t.context.app.close();
});
test('should be able to set feature', async t => {
const { auth, feature } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const f1 = await feature.getUserFeatures(u1.id);
t.is(f1.length, 0, 'should be empty');
await feature.addUserFeature(u1.id, FeatureType.EarlyAccess, 1, 'test');
const f2 = await feature.getUserFeatures(u1.id);
t.is(f2.length, 1, 'should have 1 feature');
t.is(f2[0].feature.name, FeatureType.EarlyAccess, 'should be early access');
});
test('should be able to check early access', async t => {
const { auth, feature, early_access } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const f1 = await early_access.canEarlyAccess(u1.email);
t.false(f1, 'should not have early access');
await early_access.addEarlyAccess(u1.id);
const f2 = await early_access.canEarlyAccess(u1.email);
t.true(f2, 'should have early access');
const f3 = await feature.listFeatureUsers(FeatureType.EarlyAccess);
t.is(f3.length, 1, 'should have 1 user');
t.is(f3[0].id, u1.id, 'should be the same user');
});
test('should be able revert quota', async t => {
const { auth, feature, early_access } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const f1 = await early_access.canEarlyAccess(u1.email);
t.false(f1, 'should not have early access');
await early_access.addEarlyAccess(u1.id);
const f2 = await early_access.canEarlyAccess(u1.email);
t.true(f2, 'should have early access');
const q1 = await early_access.listEarlyAccess();
t.is(q1.length, 1, 'should have 1 user');
t.is(q1[0].id, u1.id, 'should be the same user');
await early_access.removeEarlyAccess(u1.id);
const f3 = await early_access.canEarlyAccess(u1.email);
t.false(f3, 'should not have early access');
const q2 = await early_access.listEarlyAccess();
t.is(q2.length, 0, 'should have no user');
const q3 = await feature.getUserFeatures(u1.id);
t.is(q3.length, 1, 'should have 1 feature');
t.is(q3[0].feature.name, FeatureType.EarlyAccess, 'should be early access');
t.is(q3[0].activated, false, 'should be deactivated');
});
test('should be same instance after reset the feature', async t => {
const { auth, feature, early_access } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
await early_access.addEarlyAccess(u1.id);
const f1 = (await feature.getUserFeatures(u1.id))[0];
await early_access.removeEarlyAccess(u1.id);
await early_access.addEarlyAccess(u1.id);
const f2 = (await feature.getUserFeatures(u1.id))[1];
t.is(f1.feature, f2.feature, 'should be same instance');
});

View File

@@ -8,8 +8,9 @@ import * as Sinon from 'sinon';
import { ConfigModule } from '../src/config';
import type { EventPayload } from '../src/event';
import { DocHistoryManager } from '../src/modules/doc';
import { QuotaModule } from '../src/modules/quota';
import { PrismaModule, PrismaService } from '../src/prisma';
import { flushDB } from './utils';
import { FakeStorageModule, flushDB } from './utils';
let app: INestApplication;
let m: TestingModule;
@@ -20,7 +21,13 @@ let db: PrismaService;
test.beforeEach(async () => {
await flushDB();
m = await Test.createTestingModule({
imports: [PrismaModule, ScheduleModule.forRoot(), ConfigModule.forRoot()],
imports: [
PrismaModule,
QuotaModule,
FakeStorageModule.forRoot(),
ScheduleModule.forRoot(),
ConfigModule.forRoot(),
],
providers: [DocHistoryManager],
}).compile();
@@ -277,8 +284,8 @@ test('should be able to recover from history', async t => {
t.is(history2.timestamp.getTime(), snapshot.updatedAt.getTime());
// new history data force created with snapshot state before recovered
t.deepEqual(history2?.blob, Buffer.from([1, 1]));
t.deepEqual(history2?.state, Buffer.from([1, 1]));
t.deepEqual(history2.blob, Buffer.from([1, 1]));
t.deepEqual(history2.state, Buffer.from([1, 1]));
});
test('should be able to cleanup expired history', async t => {

View File

@@ -11,11 +11,13 @@ import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import { ConfigModule } from '../src/config';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { GqlModule } from '../src/graphql.module';
import { AuthModule } from '../src/modules/auth';
import { AuthService } from '../src/modules/auth/service';
import { PrismaModule } from '../src/prisma';
import { RateLimiterModule } from '../src/throttler';
import { initFeatureConfigs } from './utils';
const test = ava as TestFn<{
auth: AuthService;
@@ -45,8 +47,12 @@ test.beforeEach(async t => {
AuthModule,
RateLimiterModule,
],
providers: [RevertCommand, RunCommand],
}).compile();
t.context.auth = t.context.module.get(AuthService);
// init features
await initFeatureConfigs(t.context.module);
});
test.afterEach.always(async t => {

View File

@@ -9,6 +9,7 @@ import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppModule } from '../src/app';
import { MailService } from '../src/modules/auth/mailer';
import { FeatureManagementService } from '../src/modules/features';
import { PrismaService } from '../src/prisma';
import { createWorkspace, getInviteInfo, inviteUser, signUp } from './utils';
@@ -100,6 +101,8 @@ test.beforeEach(async t => {
})
.overrideProvider(PrismaService)
.useValue(FakePrisma)
.overrideProvider(FeatureManagementService)
.useValue({})
.compile();
const app = module.createNestApplication();
app.use(

View File

@@ -0,0 +1,133 @@
/// <reference types="../src/global.d.ts" />
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import ava, { type TestFn } from 'ava';
import { ConfigModule } from '../src/config';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { AuthModule } from '../src/modules/auth';
import { AuthService } from '../src/modules/auth/service';
import {
QuotaManagementService,
QuotaModule,
Quotas,
QuotaService,
QuotaType,
} from '../src/modules/quota';
import { PrismaModule } from '../src/prisma';
import { RateLimiterModule } from '../src/throttler';
import { FakeStorageModule, initFeatureConfigs } from './utils';
const test = ava as TestFn<{
auth: AuthService;
quota: QuotaService;
storageQuota: QuotaManagementService;
app: TestingModule;
}>;
// cleanup database before each test
test.beforeEach(async () => {
const client = new PrismaClient();
await client.$connect();
await client.user.deleteMany({});
await client.$disconnect();
});
test.beforeEach(async t => {
const module = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
auth: {
accessTokenExpiresIn: 1,
refreshTokenExpiresIn: 1,
leeway: 1,
},
host: 'example.org',
https: true,
}),
PrismaModule,
AuthModule,
QuotaModule,
FakeStorageModule.forRoot(),
RateLimiterModule,
RevertCommand,
RunCommand,
],
}).compile();
const quota = module.get(QuotaService);
const storageQuota = module.get(QuotaManagementService);
const auth = module.get(AuthService);
t.context.app = module;
t.context.quota = quota;
t.context.storageQuota = storageQuota;
t.context.auth = auth;
// init features
await initFeatureConfigs(module);
});
test.afterEach.always(async t => {
await t.context.app.close();
});
test('should be able to set quota', async t => {
const { auth, quota } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const q1 = await quota.getUserQuota(u1.id);
t.truthy(q1, 'should have quota');
t.is(q1?.feature.name, QuotaType.FreePlanV1, 'should be free plan');
await quota.switchUserQuota(u1.id, QuotaType.ProPlanV1);
const q2 = await quota.getUserQuota(u1.id);
t.is(q2?.feature.name, QuotaType.ProPlanV1, 'should be pro plan');
const fail = quota.switchUserQuota(u1.id, 'not_exists_plan_v1' as QuotaType);
await t.throwsAsync(fail, { instanceOf: Error }, 'should throw error');
});
test('should be able to check storage quota', async t => {
const { auth, quota, storageQuota } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const q1 = await storageQuota.getUserQuota(u1.id);
t.is(q1?.blobLimit, Quotas[0].configs.blobLimit, 'should be free plan');
t.is(q1?.storageQuota, Quotas[0].configs.storageQuota, 'should be free plan');
await quota.switchUserQuota(u1.id, QuotaType.ProPlanV1);
const q2 = await storageQuota.getUserQuota(u1.id);
t.is(q2?.blobLimit, Quotas[1].configs.blobLimit, 'should be pro plan');
t.is(q2?.storageQuota, Quotas[1].configs.storageQuota, 'should be pro plan');
});
test('should be able revert quota', async t => {
const { auth, quota, storageQuota } = t.context;
const u1 = await auth.signUp('DarkSky', 'darksky@example.org', '123456');
const q1 = await storageQuota.getUserQuota(u1.id);
t.is(q1?.blobLimit, Quotas[0].configs.blobLimit, 'should be free plan');
t.is(q1?.storageQuota, Quotas[0].configs.storageQuota, 'should be free plan');
await quota.switchUserQuota(u1.id, QuotaType.ProPlanV1);
const q2 = await storageQuota.getUserQuota(u1.id);
t.is(q2?.blobLimit, Quotas[1].configs.blobLimit, 'should be pro plan');
t.is(q2?.storageQuota, Quotas[1].configs.storageQuota, 'should be pro plan');
await quota.switchUserQuota(u1.id, QuotaType.FreePlanV1);
const q3 = await storageQuota.getUserQuota(u1.id);
t.is(q3?.blobLimit, Quotas[0].configs.blobLimit, 'should be free plan');
const quotas = await quota.getUserQuotas(u1.id);
t.is(quotas.length, 3, 'should have 3 quotas');
t.is(quotas[0].feature.name, QuotaType.FreePlanV1, 'should be free plan');
t.is(quotas[1].feature.name, QuotaType.ProPlanV1, 'should be pro plan');
t.is(quotas[2].feature.name, QuotaType.FreePlanV1, 'should be free plan');
t.is(quotas[0].activated, false, 'should be activated');
t.is(quotas[1].activated, false, 'should be activated');
t.is(quotas[2].activated, true, 'should be activated');
});

View File

@@ -6,7 +6,8 @@ import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import request from 'supertest';
import { AppModule } from '../src/app';
import { currentUser, signUp } from './utils';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { currentUser, initFeatureConfigs, signUp } from './utils';
let app: INestApplication;
@@ -21,6 +22,7 @@ test.beforeEach(async () => {
test.beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
providers: [RevertCommand, RunCommand],
}).compile();
app = module.createNestApplication();
app.use(
@@ -30,6 +32,9 @@ test.beforeEach(async () => {
})
);
await app.init();
// init features
await initFeatureConfigs(module);
});
test.afterEach.always(async () => {

View File

@@ -1,587 +0,0 @@
import { randomUUID } from 'node:crypto';
import type { INestApplication } from '@nestjs/common';
import { hashSync } from '@node-rs/argon2';
import { PrismaClient, type User } from '@prisma/client';
import request from 'supertest';
import type { TokenType } from '../src/modules/auth';
import type { UserType } from '../src/modules/users';
import type { InvitationType, WorkspaceType } from '../src/modules/workspaces';
const gql = '/graphql';
async function signUp(
app: INestApplication,
name: string,
email: string,
password: string
): Promise<UserType & { token: TokenType }> {
const res = await request(app.getHttpServer())
.post(gql)
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
signUp(name: "${name}", email: "${email}", password: "${password}") {
id, name, email, token { token }
}
}
`,
})
.expect(200);
return res.body.data.signUp;
}
async function currentUser(app: INestApplication, token: string) {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
currentUser {
id, name, email, emailVerified, avatarUrl, createdAt, hasPassword,
token { token }
}
}
`,
})
.expect(200);
return res.body.data.currentUser;
}
async function createWorkspace(
app: INestApplication,
token: string
): Promise<WorkspaceType> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.field(
'operations',
JSON.stringify({
name: 'createWorkspace',
query: `mutation createWorkspace($init: Upload!) {
createWorkspace(init: $init) {
id
}
}`,
variables: { init: null },
})
)
.field('map', JSON.stringify({ '0': ['variables.init'] }))
.attach('0', Buffer.from([0, 0]), 'init.data')
.expect(200);
return res.body.data.createWorkspace;
}
export async function getWorkspacePublicPages(
app: INestApplication,
token: string,
workspaceId: string
) {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
workspace(id: "${workspaceId}") {
publicPages {
id
mode
}
}
}
`,
})
.expect(200);
return res.body.data.workspace.publicPages;
}
async function getWorkspace(
app: INestApplication,
token: string,
workspaceId: string,
skip = 0,
take = 8
): Promise<WorkspaceType> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
workspace(id: "${workspaceId}") {
id, members(skip: ${skip}, take: ${take}) { id, name, email, permission, inviteId }
}
}
`,
})
.expect(200);
return res.body.data.workspace;
}
async function getPublicWorkspace(
app: INestApplication,
workspaceId: string
): Promise<WorkspaceType> {
const res = await request(app.getHttpServer())
.post(gql)
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
publicWorkspace(id: "${workspaceId}") {
id
}
}
`,
})
.expect(200);
return res.body.data.publicWorkspace;
}
async function updateWorkspace(
app: INestApplication,
token: string,
workspaceId: string,
isPublic: boolean
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
updateWorkspace(input: { id: "${workspaceId}", public: ${isPublic} }) {
public
}
}
`,
})
.expect(200);
return res.body.data.updateWorkspace.public;
}
async function inviteUser(
app: INestApplication,
token: string,
workspaceId: string,
email: string,
permission: string,
sendInviteMail = false
): Promise<string> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
invite(workspaceId: "${workspaceId}", email: "${email}", permission: ${permission}, sendInviteMail: ${sendInviteMail})
}
`,
})
.expect(200);
return res.body.data.invite;
}
async function acceptInviteById(
app: INestApplication,
workspaceId: string,
inviteId: string,
sendAcceptMail = false
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
acceptInviteById(workspaceId: "${workspaceId}", inviteId: "${inviteId}", sendAcceptMail: ${sendAcceptMail})
}
`,
})
.expect(200);
return res.body.data.acceptInviteById;
}
async function leaveWorkspace(
app: INestApplication,
token: string,
workspaceId: string,
sendLeaveMail = false
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
leaveWorkspace(workspaceId: "${workspaceId}", workspaceName: "test workspace", sendLeaveMail: ${sendLeaveMail})
}
`,
})
.expect(200);
return res.body.data.leaveWorkspace;
}
async function revokeUser(
app: INestApplication,
token: string,
workspaceId: string,
userId: string
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
revoke(workspaceId: "${workspaceId}", userId: "${userId}")
}
`,
})
.expect(200);
return res.body.data.revoke;
}
async function publishPage(
app: INestApplication,
token: string,
workspaceId: string,
pageId: string
) {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
publishPage(workspaceId: "${workspaceId}", pageId: "${pageId}") {
id
mode
}
}
`,
})
.expect(200);
return res.body.errors?.[0]?.message || res.body.data?.publishPage;
}
async function revokePublicPage(
app: INestApplication,
token: string,
workspaceId: string,
pageId: string
) {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
revokePublicPage(workspaceId: "${workspaceId}", pageId: "${pageId}") {
id
mode
public
}
}
`,
})
.expect(200);
return res.body.errors?.[0]?.message || res.body.data?.revokePublicPage;
}
async function listBlobs(
app: INestApplication,
token: string,
workspaceId: string
): Promise<string[]> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
listBlobs(workspaceId: "${workspaceId}")
}
`,
})
.expect(200);
return res.body.data.listBlobs;
}
async function collectBlobSizes(
app: INestApplication,
token: string,
workspaceId: string
): Promise<number> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.send({
query: `
query {
collectBlobSizes(workspaceId: "${workspaceId}") {
size
}
}
`,
})
.expect(200);
return res.body.data.collectBlobSizes.size;
}
async function collectAllBlobSizes(
app: INestApplication,
token: string
): Promise<number> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.send({
query: `
query {
collectAllBlobSizes {
size
}
}
`,
})
.expect(200);
return res.body.data.collectAllBlobSizes.size;
}
async function checkBlobSize(
app: INestApplication,
token: string,
workspaceId: string,
size: number
): Promise<number> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.send({
query: `query checkBlobSize($workspaceId: String!, $size: Float!) {
checkBlobSize(workspaceId: $workspaceId, size: $size) {
size
}
}`,
variables: { workspaceId, size },
})
.expect(200);
return res.body.data.checkBlobSize.size;
}
async function setBlob(
app: INestApplication,
token: string,
workspaceId: string,
buffer: Buffer
): Promise<string> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.field(
'operations',
JSON.stringify({
name: 'setBlob',
query: `mutation setBlob($blob: Upload!) {
setBlob(workspaceId: "${workspaceId}", blob: $blob)
}`,
variables: { blob: null },
})
)
.field('map', JSON.stringify({ '0': ['variables.blob'] }))
.attach('0', buffer, 'blob.data')
.expect(200);
return res.body.data.setBlob;
}
async function flushDB() {
const client = new PrismaClient();
await client.$connect();
const result: { tablename: string }[] =
await client.$queryRaw`SELECT tablename
FROM pg_catalog.pg_tables
WHERE schemaname != 'pg_catalog'
AND schemaname != 'information_schema'`;
// remove all table data
await client.$executeRawUnsafe(
`TRUNCATE TABLE ${result
.map(({ tablename }) => tablename)
.filter(name => !name.includes('migrations'))
.join(', ')}`
);
await client.$disconnect();
}
async function getInviteInfo(
app: INestApplication,
token: string,
inviteId: string
): Promise<InvitationType> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
getInviteInfo(inviteId: "${inviteId}") {
workspace {
id
name
avatar
}
user {
id
name
avatarUrl
}
}
}
`,
})
.expect(200);
return res.body.data.getInviteInfo;
}
async function sendChangeEmail(
app: INestApplication,
userToken: string,
email: string,
callbackUrl: string
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(userToken, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
sendChangeEmail(email: "${email}", callbackUrl: "${callbackUrl}")
}
`,
})
.expect(200);
return res.body.data.sendChangeEmail;
}
async function sendVerifyChangeEmail(
app: INestApplication,
userToken: string,
token: string,
email: string,
callbackUrl: string
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(userToken, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
sendVerifyChangeEmail(token:"${token}", email: "${email}", callbackUrl: "${callbackUrl}")
}
`,
})
.expect(200);
return res.body.data.sendVerifyChangeEmail;
}
async function changeEmail(
app: INestApplication,
userToken: string,
token: string
): Promise<UserType & { token: TokenType }> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(userToken, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
changeEmail(token: "${token}") {
id
name
avatarUrl
email
}
}
`,
})
.expect(200);
return res.body.data.changeEmail;
}
export class FakePrisma {
fakeUser: User = {
id: randomUUID(),
name: 'Alex Yang',
avatarUrl: '',
email: 'alex.yang@example.org',
password: hashSync('123456'),
emailVerified: new Date(),
createdAt: new Date(),
};
get user() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const prisma = this;
return {
async findFirst() {
return prisma.fakeUser;
},
async findUnique() {
return this.findFirst();
},
async update() {
return this.findFirst();
},
};
}
}
export {
acceptInviteById,
changeEmail,
checkBlobSize,
collectAllBlobSizes,
collectBlobSizes,
createWorkspace,
currentUser,
flushDB,
getInviteInfo,
getPublicWorkspace,
getWorkspace,
inviteUser,
leaveWorkspace,
listBlobs,
publishPage,
revokePublicPage,
revokeUser,
sendChangeEmail,
sendVerifyChangeEmail,
setBlob,
signUp,
updateWorkspace,
};

View File

@@ -0,0 +1,112 @@
import type { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { gql } from './common';
export async function listBlobs(
app: INestApplication,
token: string,
workspaceId: string
): Promise<string[]> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
listBlobs(workspaceId: "${workspaceId}")
}
`,
})
.expect(200);
return res.body.data.listBlobs;
}
export async function getWorkspaceBlobsSize(
app: INestApplication,
token: string,
workspaceId: string
): Promise<number> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.send({
query: `
query {
workspace(id: "${workspaceId}") {
blobsSize
}
}
`,
})
.expect(200);
return res.body.data.workspace.blobsSize;
}
export async function collectAllBlobSizes(
app: INestApplication,
token: string
): Promise<number> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.send({
query: `
query {
collectAllBlobSizes {
size
}
}
`,
})
.expect(200);
return res.body.data.collectAllBlobSizes.size;
}
export async function checkBlobSize(
app: INestApplication,
token: string,
workspaceId: string,
size: number
): Promise<number> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.send({
query: `query checkBlobSize($workspaceId: String!, $size: Float!) {
checkBlobSize(workspaceId: $workspaceId, size: $size) {
size
}
}`,
variables: { workspaceId, size },
})
.expect(200);
return res.body.data.checkBlobSize.size;
}
export async function setBlob(
app: INestApplication,
token: string,
workspaceId: string,
buffer: Buffer
): Promise<string> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.field(
'operations',
JSON.stringify({
name: 'setBlob',
query: `mutation setBlob($blob: Upload!) {
setBlob(workspaceId: "${workspaceId}", blob: $blob)
}`,
variables: { blob: null },
})
)
.field('map', JSON.stringify({ '0': ['variables.blob'] }))
.attach('0', buffer, 'blob.data')
.expect(200);
return res.body.data.setBlob;
}

View File

@@ -0,0 +1 @@
export const gql = '/graphql';

View File

@@ -0,0 +1,5 @@
export * from './blobs';
export * from './invite';
export * from './user';
export * from './utils';
export * from './workspace';

View File

@@ -0,0 +1,121 @@
import type { INestApplication } from '@nestjs/common';
import request from 'supertest';
import type { InvitationType } from '../../src/modules/workspaces';
import { gql } from './common';
export async function inviteUser(
app: INestApplication,
token: string,
workspaceId: string,
email: string,
permission: string,
sendInviteMail = false
): Promise<string> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
invite(workspaceId: "${workspaceId}", email: "${email}", permission: ${permission}, sendInviteMail: ${sendInviteMail})
}
`,
})
.expect(200);
return res.body.data.invite;
}
export async function acceptInviteById(
app: INestApplication,
workspaceId: string,
inviteId: string,
sendAcceptMail = false
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
acceptInviteById(workspaceId: "${workspaceId}", inviteId: "${inviteId}", sendAcceptMail: ${sendAcceptMail})
}
`,
})
.expect(200);
return res.body.data.acceptInviteById;
}
export async function leaveWorkspace(
app: INestApplication,
token: string,
workspaceId: string,
sendLeaveMail = false
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
leaveWorkspace(workspaceId: "${workspaceId}", workspaceName: "test workspace", sendLeaveMail: ${sendLeaveMail})
}
`,
})
.expect(200);
return res.body.data.leaveWorkspace;
}
export async function revokeUser(
app: INestApplication,
token: string,
workspaceId: string,
userId: string
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
revoke(workspaceId: "${workspaceId}", userId: "${userId}")
}
`,
})
.expect(200);
return res.body.data.revoke;
}
export async function getInviteInfo(
app: INestApplication,
token: string,
inviteId: string
): Promise<InvitationType> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
getInviteInfo(inviteId: "${inviteId}") {
workspace {
id
name
avatar
}
user {
id
name
avatarUrl
}
}
}
`,
})
.expect(200);
return res.body.data.getInviteInfo;
}

View File

@@ -0,0 +1,117 @@
import type { INestApplication } from '@nestjs/common';
import request from 'supertest';
import type { TokenType } from '../../src/modules/auth';
import type { UserType } from '../../src/modules/users';
import { gql } from './common';
export async function signUp(
app: INestApplication,
name: string,
email: string,
password: string
): Promise<UserType & { token: TokenType }> {
const res = await request(app.getHttpServer())
.post(gql)
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
signUp(name: "${name}", email: "${email}", password: "${password}") {
id, name, email, token { token }
}
}
`,
})
.expect(200);
return res.body.data.signUp;
}
export async function currentUser(app: INestApplication, token: string) {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
currentUser {
id, name, email, emailVerified, avatarUrl, createdAt, hasPassword,
token { token }
}
}
`,
})
.expect(200);
return res.body.data.currentUser;
}
export async function sendChangeEmail(
app: INestApplication,
userToken: string,
email: string,
callbackUrl: string
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(userToken, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
sendChangeEmail(email: "${email}", callbackUrl: "${callbackUrl}")
}
`,
})
.expect(200);
return res.body.data.sendChangeEmail;
}
export async function sendVerifyChangeEmail(
app: INestApplication,
userToken: string,
token: string,
email: string,
callbackUrl: string
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(userToken, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
sendVerifyChangeEmail(token:"${token}", email: "${email}", callbackUrl: "${callbackUrl}")
}
`,
})
.expect(200);
return res.body.data.sendVerifyChangeEmail;
}
export async function changeEmail(
app: INestApplication,
userToken: string,
token: string
): Promise<UserType & { token: TokenType }> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(userToken, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
changeEmail(token: "${token}") {
id
name
avatarUrl
email
}
}
`,
})
.expect(200);
return res.body.data.changeEmail;
}

View File

@@ -0,0 +1,82 @@
import { randomUUID } from 'node:crypto';
import type { DynamicModule, FactoryProvider } from '@nestjs/common';
import { TestingModule } from '@nestjs/testing';
import { hashSync } from '@node-rs/argon2';
import { PrismaClient, type User } from '@prisma/client';
import { RevertCommand, RunCommand } from '../../src/data/commands/run';
import { StorageProvide } from '../../src/storage';
export async function flushDB() {
const client = new PrismaClient();
await client.$connect();
const result: { tablename: string }[] =
await client.$queryRaw`SELECT tablename
FROM pg_catalog.pg_tables
WHERE schemaname != 'pg_catalog'
AND schemaname != 'information_schema'`;
// remove all table data
await client.$executeRawUnsafe(
`TRUNCATE TABLE ${result
.map(({ tablename }) => tablename)
.filter(name => !name.includes('migrations'))
.join(', ')}`
);
await client.$disconnect();
}
export class FakePrisma {
fakeUser: User = {
id: randomUUID(),
name: 'Alex Yang',
avatarUrl: '',
email: 'alex.yang@example.org',
password: hashSync('123456'),
emailVerified: new Date(),
createdAt: new Date(),
};
get user() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const prisma = this;
return {
async findFirst() {
return prisma.fakeUser;
},
async findUnique() {
return this.findFirst();
},
async update() {
return this.findFirst();
},
};
}
}
export class FakeStorageModule {
static forRoot(): DynamicModule {
const storageProvider: FactoryProvider = {
provide: StorageProvide,
useFactory: async () => {
return null;
},
};
return {
global: true,
module: FakeStorageModule,
providers: [storageProvider],
exports: [storageProvider],
};
}
}
export async function initFeatureConfigs(module: TestingModule) {
const run = module.get(RunCommand);
const revert = module.get(RevertCommand);
await Promise.allSettled([revert.run(['UserFeaturesInit1698652531198'])]);
await run.runOne('UserFeaturesInit1698652531198');
}

View File

@@ -0,0 +1,172 @@
import type { INestApplication } from '@nestjs/common';
import request from 'supertest';
import type { WorkspaceType } from '../../src/modules/workspaces';
import { gql } from './common';
export async function createWorkspace(
app: INestApplication,
token: string
): Promise<WorkspaceType> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.field(
'operations',
JSON.stringify({
name: 'createWorkspace',
query: `mutation createWorkspace($init: Upload!) {
createWorkspace(init: $init) {
id
}
}`,
variables: { init: null },
})
)
.field('map', JSON.stringify({ '0': ['variables.init'] }))
.attach('0', Buffer.from([0, 0]), 'init.data')
.expect(200);
return res.body.data.createWorkspace;
}
export async function getWorkspacePublicPages(
app: INestApplication,
token: string,
workspaceId: string
) {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
workspace(id: "${workspaceId}") {
publicPages {
id
mode
}
}
}
`,
})
.expect(200);
return res.body.data.workspace.publicPages;
}
export async function getWorkspace(
app: INestApplication,
token: string,
workspaceId: string,
skip = 0,
take = 8
): Promise<WorkspaceType> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
workspace(id: "${workspaceId}") {
id, members(skip: ${skip}, take: ${take}) { id, name, email, permission, inviteId }
}
}
`,
})
.expect(200);
return res.body.data.workspace;
}
export async function getPublicWorkspace(
app: INestApplication,
workspaceId: string
): Promise<WorkspaceType> {
const res = await request(app.getHttpServer())
.post(gql)
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
query {
publicWorkspace(id: "${workspaceId}") {
id
}
}
`,
})
.expect(200);
return res.body.data.publicWorkspace;
}
export async function updateWorkspace(
app: INestApplication,
token: string,
workspaceId: string,
isPublic: boolean
): Promise<boolean> {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
updateWorkspace(input: { id: "${workspaceId}", public: ${isPublic} }) {
public
}
}
`,
})
.expect(200);
return res.body.data.updateWorkspace.public;
}
export async function publishPage(
app: INestApplication,
token: string,
workspaceId: string,
pageId: string
) {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
publishPage(workspaceId: "${workspaceId}", pageId: "${pageId}") {
id
mode
}
}
`,
})
.expect(200);
return res.body.errors?.[0]?.message || res.body.data?.publishPage;
}
export async function revokePublicPage(
app: INestApplication,
token: string,
workspaceId: string,
pageId: string
) {
const res = await request(app.getHttpServer())
.post(gql)
.auth(token, { type: 'bearer' })
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
.send({
query: `
mutation {
revokePublicPage(workspaceId: "${workspaceId}", pageId: "${pageId}") {
id
mode
public
}
}
`,
})
.expect(200);
return res.body.errors?.[0]?.message || res.body.data?.revokePublicPage;
}

View File

@@ -6,17 +6,21 @@ import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import request from 'supertest';
import { AppModule } from '../src/app';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { QuotaService, QuotaType } from '../src/modules/quota';
import {
checkBlobSize,
collectAllBlobSizes,
collectBlobSizes,
createWorkspace,
getWorkspaceBlobsSize,
initFeatureConfigs,
listBlobs,
setBlob,
signUp,
} from './utils';
let app: INestApplication;
let quota: QuotaService;
const client = new PrismaClient();
@@ -33,6 +37,7 @@ test.beforeEach(async () => {
test.beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
providers: [RevertCommand, RunCommand],
}).compile();
app = module.createNestApplication();
app.use(
@@ -41,6 +46,11 @@ test.beforeEach(async () => {
maxFiles: 5,
})
);
quota = module.get(QuotaService);
// init features
await initFeatureConfigs(module);
await app.init();
});
@@ -103,7 +113,7 @@ test('should calc blobs size', async t => {
const buffer2 = Buffer.from([0, 1]);
await setBlob(app, u1.token.token, workspace.id, buffer2);
const size = await collectBlobSizes(app, u1.token.token, workspace.id);
const size = await getWorkspaceBlobsSize(app, u1.token.token, workspace.id);
t.is(size, 4, 'failed to collect blob sizes');
});
@@ -143,3 +153,39 @@ test('should calc all blobs size', async t => {
);
t.is(size2, -1, 'failed to check blob size');
});
test('should be able calc quota after switch plan', async t => {
const u1 = await signUp(app, 'darksky', 'darksky@affine.pro', '1');
const workspace1 = await createWorkspace(app, u1.token.token);
const buffer1 = Buffer.from([0, 0]);
await setBlob(app, u1.token.token, workspace1.id, buffer1);
const buffer2 = Buffer.from([0, 1]);
await setBlob(app, u1.token.token, workspace1.id, buffer2);
const workspace2 = await createWorkspace(app, u1.token.token);
const buffer3 = Buffer.from([0, 0]);
await setBlob(app, u1.token.token, workspace2.id, buffer3);
const buffer4 = Buffer.from([0, 1]);
await setBlob(app, u1.token.token, workspace2.id, buffer4);
const size1 = await checkBlobSize(
app,
u1.token.token,
workspace1.id,
10 * 1024 * 1024 * 1024 - 8
);
t.is(size1, 0, 'failed to check free plan blob size');
quota.switchUserQuota(u1.id, QuotaType.ProPlanV1);
const size2 = await checkBlobSize(
app,
u1.token.token,
workspace1.id,
100 * 1024 * 1024 * 1024 - 8
);
t.is(size2, 0, 'failed to check pro plan blob size');
});

View File

@@ -9,12 +9,14 @@ import ava, { type TestFn } from 'ava';
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import { AppModule } from '../src/app';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import { MailService } from '../src/modules/auth/mailer';
import { AuthService } from '../src/modules/auth/service';
import {
acceptInviteById,
createWorkspace,
getWorkspace,
initFeatureConfigs,
inviteUser,
leaveWorkspace,
revokeUser,
@@ -39,6 +41,7 @@ test.beforeEach(async t => {
await client.$disconnect();
const module = await Test.createTestingModule({
imports: [AppModule],
providers: [RevertCommand, RunCommand],
}).compile();
const app = module.createNestApplication();
app.use(
@@ -51,9 +54,13 @@ test.beforeEach(async t => {
const auth = module.get(AuthService);
const mail = module.get(MailService);
t.context.app = app;
t.context.auth = auth;
t.context.mail = mail;
// init features
await initFeatureConfigs(module);
});
test.afterEach.always(async t => {

View File

@@ -4,6 +4,8 @@ import ava, { type TestFn } from 'ava';
import { stub } from 'sinon';
import { AppModule } from '../src/app';
import { FeatureManagementService } from '../src/modules/features';
import { Quotas } from '../src/modules/quota';
import { UsersService } from '../src/modules/users';
import { PermissionService } from '../src/modules/workspaces/permission';
import { WorkspaceResolver } from '../src/modules/workspaces/resolver';
@@ -20,6 +22,9 @@ class FakePermission {
user: new FakePrisma().fakeUser,
};
}
async getOwnedWorkspaces() {
return [''];
}
}
const fakeUserService = {
@@ -42,6 +47,36 @@ test.beforeEach(async t => {
return [];
},
},
userFeatures: {
async count() {
return 1;
},
async findFirst() {
return {
createdAt: new Date(),
expiredAt: new Date(),
reason: '',
feature: Quotas[0],
};
},
},
features: {
async findFirst() {
return {
id: 0,
feature: 'free_plan_v1',
version: 1,
type: 1,
configs: {
name: 'Free',
blobLimit: 1,
storageQuota: 1,
historyPeriod: 1,
memberLimit: 3,
},
};
},
},
})
.overrideProvider(PermissionService)
.useClass(FakePermission)
@@ -53,6 +88,8 @@ test.beforeEach(async t => {
return 1024 * 10;
},
})
.overrideProvider(FeatureManagementService)
.useValue({})
.compile();
t.context.app = module.createNestApplication();
t.context.resolver = t.context.app.get(WorkspaceResolver);

View File

@@ -6,12 +6,14 @@ import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import request from 'supertest';
import { AppModule } from '../src/app';
import { RevertCommand, RunCommand } from '../src/data/commands/run';
import {
acceptInviteById,
createWorkspace,
currentUser,
getPublicWorkspace,
getWorkspacePublicPages,
initFeatureConfigs,
inviteUser,
publishPage,
revokePublicPage,
@@ -34,6 +36,7 @@ test.beforeEach(async t => {
await client.$disconnect();
const module = await Test.createTestingModule({
imports: [AppModule],
providers: [RevertCommand, RunCommand],
}).compile();
const app = module.createNestApplication();
app.use(
@@ -45,6 +48,9 @@ test.beforeEach(async t => {
await app.init();
t.context.client = client;
t.context.app = app;
// init features
await initFeatureConfigs(module);
});
test.afterEach.always(async t => {

View File

@@ -1,6 +1,6 @@
{
"name": "@affine/storage",
"version": "0.10.3-canary.2",
"version": "0.11.0",
"engines": {
"node": ">= 10.16.0 < 11 || >= 11.8.0"
},

View File

@@ -8,5 +8,5 @@
"react": "18.2.0",
"react-dom": "18.2.0"
},
"version": "0.10.3-canary.2"
"version": "0.11.0"
}

View File

@@ -7,7 +7,7 @@
},
"devDependencies": {
"@types/debug": "^4.1.9",
"vitest": "0.34.6"
"vitest": "1.0.4"
},
"version": "0.10.3-canary.2"
"version": "0.11.0"
}

Some files were not shown because too many files have changed in this diff Show More