diff --git a/package.json b/package.json index 3918074..ca06d22 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "xlsx": "^0.18.5" }, "devDependencies": { - "@cool-vue/vite-plugin": "^8.1.0", + "@cool-vue/vite-plugin": "^8.1.1", "@intlify/unplugin-vue-i18n": "^6.0.3", "@rushstack/eslint-patch": "^1.10.5", "@tsconfig/node20": "^20.1.4", diff --git a/packages/vite-plugin/dist/index.js b/packages/vite-plugin/dist/index.js index 6f431e4..8ef90d2 100644 --- a/packages/vite-plugin/dist/index.js +++ b/packages/vite-plugin/dist/index.js @@ -855,7 +855,7 @@ withFileTypes: true, }); // 获取当前目录的模块名 - const moduleName = dir.match(/[\/\\](?:src\/(?:plugins|modules)\/)([^\/\\]+)/)?.[1] || ""; + const moduleName = dir.match(/[/\\](?:src[/\\](?:plugins|modules)[/\\])([^/\\]+)/)?.[1] || ""; for (const d of dirs) { if (d.isDirectory()) { arr.push(...findSvg(dir + d.name + "/")); diff --git a/packages/vite-plugin/package.json b/packages/vite-plugin/package.json index 223a415..7b1266f 100644 --- a/packages/vite-plugin/package.json +++ b/packages/vite-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cool-vue/vite-plugin", - "version": "8.1.0", + "version": "8.1.1", "description": "cool-admin、cool-uni builder", "types": "./dist/index.d.ts", "main": "/dist/index.js", diff --git a/packages/vite-plugin/src/svg/index.ts b/packages/vite-plugin/src/svg/index.ts index 4a58b55..88df6d3 100644 --- a/packages/vite-plugin/src/svg/index.ts +++ b/packages/vite-plugin/src/svg/index.ts @@ -13,7 +13,7 @@ function findSvg(dir: string) { }); // 获取当前目录的模块名 - const moduleName = dir.match(/[\/\\](?:src\/(?:plugins|modules)\/)([^\/\\]+)/)?.[1] || ""; + const moduleName = dir.match(/[/\\](?:src[/\\](?:plugins|modules)[/\\])([^/\\]+)/)?.[1] || ""; for (const d of dirs) { if (d.isDirectory()) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c18ac37..43b452e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -82,8 +82,8 @@ importers: version: 0.18.5 devDependencies: '@cool-vue/vite-plugin': - specifier: ^8.1.0 - version: 8.1.0 + specifier: ^8.1.1 + version: 8.1.1 '@intlify/unplugin-vue-i18n': specifier: ^6.0.3 version: 6.0.8(@vue/compiler-dom@3.5.13)(eslint@9.25.1(jiti@1.21.7))(rollup@4.40.0)(typescript@5.5.4)(vue-i18n@11.1.3(vue@3.5.13(typescript@5.5.4)))(vue@3.5.13(typescript@5.5.4)) @@ -334,8 +334,8 @@ packages: '@cool-vue/crud@8.0.2': resolution: {integrity: sha512-7fXGfn1Li0n6vtHSj40mu2VuZV2L/T/HDllJXHMKq2ODo9EAttjcuTN/Z8b3KuOFHYpWp5iN+FqIzRNGEDxjUQ==} - '@cool-vue/vite-plugin@8.1.0': - resolution: {integrity: sha512-ewMqT7PhHLOQc3JzBbfm9ddVvlcJ5lpNEdHNNVJ6GO2x1H810auhqvbMbZsuxfgYOnj3EToCSuqFVY/wqVejcA==} + '@cool-vue/vite-plugin@8.1.1': + resolution: {integrity: sha512-s4F2NHUZ1Dc7KjEumzzbnOkKPw0Fdb2GwdWmgTpaxwBg9BTpiVA/N+92kqh8mlSmRJjn/tMMaEUInxQzbLQ0TQ==} '@ctrl/tinycolor@3.6.1': resolution: {integrity: sha512-SITSV6aIXsuVNV3f3O0f2n/cgyEDWoSqtZMYiAmcsYHydcKrOz3gUxB/iXd/Qf08+IZX4KpgNbvUdMBmWz+kcA==} @@ -675,42 +675,36 @@ packages: engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] - libc: [glibc] '@parcel/watcher-linux-arm-musl@2.5.1': resolution: {integrity: sha512-6E+m/Mm1t1yhB8X412stiKFG3XykmgdIOqhjWj+VL8oHkKABfu/gjFj8DvLrYVHSBNC+/u5PeNrujiSQ1zwd1Q==} engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] - libc: [musl] '@parcel/watcher-linux-arm64-glibc@2.5.1': resolution: {integrity: sha512-LrGp+f02yU3BN9A+DGuY3v3bmnFUggAITBGriZHUREfNEzZh/GO06FF5u2kx8x+GBEUYfyTGamol4j3m9ANe8w==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] - libc: [glibc] '@parcel/watcher-linux-arm64-musl@2.5.1': resolution: {integrity: sha512-cFOjABi92pMYRXS7AcQv9/M1YuKRw8SZniCDw0ssQb/noPkRzA+HBDkwmyOJYp5wXcsTrhxO0zq1U11cK9jsFg==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] - libc: [musl] '@parcel/watcher-linux-x64-glibc@2.5.1': resolution: {integrity: sha512-GcESn8NZySmfwlTsIur+49yDqSny2IhPeZfXunQi48DMugKeZ7uy1FX83pO0X22sHntJ4Ub+9k34XQCX+oHt2A==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] - libc: [glibc] '@parcel/watcher-linux-x64-musl@2.5.1': resolution: {integrity: sha512-n0E2EQbatQ3bXhcH2D1XIAANAcTZkQICBPVaxMeaCVBtOpBZpWJuf7LwyWPSBDITb7In8mqQgJ7gH8CILCURXg==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] - libc: [musl] '@parcel/watcher-win32-arm64@2.5.1': resolution: {integrity: sha512-RFzklRvmc3PkjKjry3hLF9wD7ppR4AKcWNzH7kXR7GUe0Igb3Nz8fyPwtZCSquGrhU5HhUNDr/mKBqj7tqA2Vw==} @@ -788,67 +782,56 @@ packages: resolution: {integrity: sha512-y/qUMOpJxBMy8xCXD++jeu8t7kzjlOCkoxxajL58G62PJGBZVl/Gwpm7JK9+YvlB701rcQTzjUZ1JgUoPTnoQA==} cpu: [arm] os: [linux] - libc: [glibc] '@rollup/rollup-linux-arm-musleabihf@4.40.0': resolution: {integrity: sha512-GoCsPibtVdJFPv/BOIvBKO/XmwZLwaNWdyD8TKlXuqp0veo2sHE+A/vpMQ5iSArRUz/uaoj4h5S6Pn0+PdhRjg==} cpu: [arm] os: [linux] - libc: [musl] '@rollup/rollup-linux-arm64-gnu@4.40.0': resolution: {integrity: sha512-L5ZLphTjjAD9leJzSLI7rr8fNqJMlGDKlazW2tX4IUF9P7R5TMQPElpH82Q7eNIDQnQlAyiNVfRPfP2vM5Avvg==} cpu: [arm64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-arm64-musl@4.40.0': resolution: {integrity: sha512-ATZvCRGCDtv1Y4gpDIXsS+wfFeFuLwVxyUBSLawjgXK2tRE6fnsQEkE4csQQYWlBlsFztRzCnBvWVfcae/1qxQ==} cpu: [arm64] os: [linux] - libc: [musl] '@rollup/rollup-linux-loongarch64-gnu@4.40.0': resolution: {integrity: sha512-wG9e2XtIhd++QugU5MD9i7OnpaVb08ji3P1y/hNbxrQ3sYEelKJOq1UJ5dXczeo6Hj2rfDEL5GdtkMSVLa/AOg==} cpu: [loong64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-powerpc64le-gnu@4.40.0': resolution: {integrity: sha512-vgXfWmj0f3jAUvC7TZSU/m/cOE558ILWDzS7jBhiCAFpY2WEBn5jqgbqvmzlMjtp8KlLcBlXVD2mkTSEQE6Ixw==} cpu: [ppc64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-riscv64-gnu@4.40.0': resolution: {integrity: sha512-uJkYTugqtPZBS3Z136arevt/FsKTF/J9dEMTX/cwR7lsAW4bShzI2R0pJVw+hcBTWF4dxVckYh72Hk3/hWNKvA==} cpu: [riscv64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-riscv64-musl@4.40.0': resolution: {integrity: sha512-rKmSj6EXQRnhSkE22+WvrqOqRtk733x3p5sWpZilhmjnkHkpeCgWsFFo0dGnUGeA+OZjRl3+VYq+HyCOEuwcxQ==} cpu: [riscv64] os: [linux] - libc: [musl] '@rollup/rollup-linux-s390x-gnu@4.40.0': resolution: {integrity: sha512-SpnYlAfKPOoVsQqmTFJ0usx0z84bzGOS9anAC0AZ3rdSo3snecihbhFTlJZ8XMwzqAcodjFU4+/SM311dqE5Sw==} cpu: [s390x] os: [linux] - libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.40.0': resolution: {integrity: sha512-RcDGMtqF9EFN8i2RYN2W+64CdHruJ5rPqrlYw+cgM3uOVPSsnAQps7cpjXe9be/yDp8UC7VLoCoKC8J3Kn2FkQ==} cpu: [x64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-x64-musl@4.40.0': resolution: {integrity: sha512-HZvjpiUmSNx5zFgwtQAV1GaGazT2RWvqeDi0hV+AtC8unqqDSsaFjPxfsO6qPtKRRg25SisACWnJ37Yio8ttaw==} cpu: [x64] os: [linux] - libc: [musl] '@rollup/rollup-win32-arm64-msvc@4.40.0': resolution: {integrity: sha512-UtZQQI5k/b8d7d3i9AZmA/t+Q4tk3hOC0tMOMSq2GlMYOfxbesxG4mJSeDp0EHs30N9bsfwUvs3zF4v/RzOeTQ==} @@ -3090,7 +3073,7 @@ snapshots: - '@vue/composition-api' - typescript - '@cool-vue/vite-plugin@8.1.0': + '@cool-vue/vite-plugin@8.1.1': dependencies: '@vue/compiler-sfc': 3.5.13 axios: 1.8.4 diff --git a/src/cool/service/index.ts b/src/cool/service/index.ts index 8dd7ac6..1474751 100644 --- a/src/cool/service/index.ts +++ b/src/cool/service/index.ts @@ -7,3 +7,4 @@ export const service: Eps.Service = hmr.getData('service', { }); export * from './base'; +export * from './stream'; diff --git a/src/cool/service/stream.ts b/src/cool/service/stream.ts new file mode 100644 index 0000000..c941719 --- /dev/null +++ b/src/cool/service/stream.ts @@ -0,0 +1,103 @@ +import { useBase } from '/$/base'; +import { config } from '/@/config'; + +export function useStream() { + const { user } = useBase(); + let abortController: AbortController | null = null; + + // 调用 + async function invoke({ + url, + method = 'POST', + data, + cb + }: { + url: string; + method?: string; + data?: any; + cb?: (result: any) => void; + }) { + abortController = new AbortController(); + + let cacheText = ''; + + return fetch(config.baseUrl + url, { + method, + headers: { + Authorization: user.token, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data), + signal: abortController?.signal + }) + .then(res => { + if (res.body) { + const reader = res.body.getReader(); + const decoder = new TextDecoder('utf-8'); + const stream = new ReadableStream({ + start(controller) { + function push() { + reader.read().then(({ done, value }) => { + if (done) { + controller.close(); + return; + } + + let text = decoder.decode(value, { stream: true }); + + if (cb) { + if (cacheText) { + text = cacheText + text; + } + + if (text.indexOf('data:') == 0) { + text = '\n\n' + text; + } + + try { + const arr = text + .split(/\n\ndata:/g) + .filter(Boolean) + .map(e => JSON.parse(e)); + + arr.forEach(cb); + + cacheText = ''; + } catch (err) { + console.error('[parse text]', text); + cacheText = text; + } + } + + controller.enqueue(text); + push(); + }); + } + push(); + } + }); + + return new Response(stream); + } + + return res; + }) + .catch(err => { + console.error(err); + throw err; + }); + } + + // 取消 + function cancel() { + if (abortController) { + abortController.abort(); + abortController = null; + } + } + + return { + invoke, + cancel + }; +} diff --git a/src/plugins/upload/index.ts b/src/plugins/upload/index.ts index 04bca77..f37497e 100644 --- a/src/plugins/upload/index.ts +++ b/src/plugins/upload/index.ts @@ -1 +1,2 @@ export * from './utils'; +export * from './hooks';