@platformatic/tanstack 0.0.1 → 3.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,159 @@
1
+ import { cleanBasePath, createServerListener, ensureTrailingSlash, errors, getServerUrl } from '@platformatic/basic'
2
+ import { importFile, resolvePackageViaESM } from '@platformatic/basic/lib/utils.js'
3
+ import { ensureLoggableError } from '@platformatic/foundation'
4
+ import { ViteCapability } from '@platformatic/vite'
5
+ import inject from 'light-my-request'
6
+ import { existsSync } from 'node:fs'
7
+ import { readFile } from 'node:fs/promises'
8
+ import { dirname, resolve } from 'node:path'
9
+ import { satisfies } from 'semver'
10
+ import { packageJson } from './schema.js'
11
+
12
+ const supportedVersions = '^1.0.0'
13
+
14
+ export class TanstackCapability extends ViteCapability {
15
+ #tanstack
16
+ #basePath
17
+ #server
18
+ #dispatcher
19
+
20
+ constructor (root, config, context) {
21
+ super(root, config, context)
22
+ this.type = 'tanstack'
23
+ this.version = packageJson.version
24
+ }
25
+
26
+ async init () {
27
+ await super.init()
28
+
29
+ if (!this.isProduction) {
30
+ this.#tanstack = resolve(dirname(await resolvePackageViaESM(this.root, '@tanstack/react-start')), '../..')
31
+ const tanstackPackage = JSON.parse(await readFile(resolve(this.#tanstack, 'package.json'), 'utf-8'))
32
+
33
+ if (!satisfies(tanstackPackage.version, supportedVersions)) {
34
+ throw new errors.UnsupportedVersion('@tanstack/react-start', tanstackPackage.version, supportedVersions)
35
+ }
36
+ }
37
+
38
+ const config = this.config
39
+ this.#basePath = config.application?.basePath
40
+ ? ensureTrailingSlash(cleanBasePath(config.application?.basePath))
41
+ : undefined
42
+
43
+ this.registerGlobals({ basePath: this.#basePath })
44
+
45
+ this.subprocessTerminationSignal = 'SIGKILL'
46
+ }
47
+
48
+ async start ({ listen }) {
49
+ // Make this idempotent
50
+ /* c8 ignore next 3 */
51
+ if (this.url) {
52
+ return this.url
53
+ }
54
+
55
+ await super._start({ listen })
56
+
57
+ const config = this.config
58
+ const command = config.application.commands[this.isProduction ? 'production' : 'development']
59
+
60
+ if (command) {
61
+ return this.startWithCommand(command)
62
+ } else if (!this.isProduction) {
63
+ return super.start({ listen })
64
+ }
65
+
66
+ await this.#startProduction({ listen })
67
+ }
68
+
69
+ async #startProduction () {
70
+ const config = this.config
71
+ const outputDirectory = resolve(this.root, config.application.outputDirectory)
72
+ this.verifyOutputDirectory(outputDirectory)
73
+
74
+ const buildInfoPath = resolve(outputDirectory, '.platformatic-build.json')
75
+
76
+ if (!this.#basePath && existsSync(buildInfoPath)) {
77
+ try {
78
+ const buildInfo = JSON.parse(await readFile(buildInfoPath, 'utf-8'))
79
+ this.#basePath = buildInfo.basePath
80
+ } catch (e) {
81
+ globalThis.platformatic.logger.error({ err: ensureLoggableError(e) }, 'Reading build info failed.')
82
+ }
83
+ }
84
+
85
+ const serverOptions = this.serverConfig
86
+ const serverPromise = createServerListener(
87
+ serverOptions?.port ?? true,
88
+ serverOptions?.hostname ?? true,
89
+ typeof serverOptions?.backlog === 'number' ? { backlog: serverOptions.backlog } : {}
90
+ )
91
+
92
+ await importFile(resolve(outputDirectory, 'server/index.mjs'))
93
+
94
+ this.#server = await serverPromise
95
+ this.#dispatcher = this.#server.listeners('request')[0]
96
+ this.url = getServerUrl(this.#server)
97
+ await this._collectMetrics()
98
+
99
+ return this.url
100
+ }
101
+
102
+ async stop () {
103
+ const hasChildrenManager = !!this.childManager
104
+ await super.stop()
105
+
106
+ // ViteCapability.stop already stops child processs
107
+ if (hasChildrenManager || !this.isProduction) {
108
+ return
109
+ }
110
+
111
+ /* c8 ignore next 3 */
112
+ if (!this.#server?.listening) {
113
+ return
114
+ }
115
+
116
+ return new Promise((resolve, reject) => {
117
+ this.#server.close(error => {
118
+ /* c8 ignore next 3 */
119
+ if (error) {
120
+ return reject(error)
121
+ }
122
+
123
+ resolve()
124
+ })
125
+ })
126
+ }
127
+
128
+ getMeta () {
129
+ if (!this.isProduction) {
130
+ return super.getMeta()
131
+ }
132
+
133
+ return {
134
+ gateway: {
135
+ tcp: typeof this.url !== 'undefined',
136
+ url: this.url,
137
+ prefix: this.basePath ?? this.#basePath,
138
+ wantsAbsoluteUrls: true,
139
+ needsRootTrailingSlash: true
140
+ }
141
+ }
142
+ }
143
+
144
+ async inject (injectParams, onInject) {
145
+ if (!this.isProduction) {
146
+ return super.inject(injectParams, onInject)
147
+ }
148
+
149
+ const res = await inject(this.#dispatcher, injectParams, onInject)
150
+
151
+ /* c8 ignore next 3 */
152
+ if (onInject) {
153
+ return
154
+ } // Since inject might be called from the main thread directly via ITC, let's clean it up
155
+
156
+ const { statusCode, headers, body, payload, rawPayload } = res
157
+ return { statusCode, headers, body, payload, rawPayload }
158
+ }
159
+ }
package/lib/schema.js ADDED
@@ -0,0 +1,34 @@
1
+ import { schemaComponents as basicSchemaComponents } from '@platformatic/basic'
2
+ import { schemaComponents as utilsSchemaComponents } from '@platformatic/foundation'
3
+ import { schemaComponents as viteSchemaComponents } from '@platformatic/vite'
4
+ import { readFileSync } from 'node:fs'
5
+ import { resolve } from 'node:path'
6
+
7
+ export const packageJson = JSON.parse(readFileSync(resolve(import.meta.dirname, '../package.json'), 'utf8'))
8
+ export const version = packageJson.version
9
+
10
+ export const schemaComponents = {}
11
+
12
+ export const schema = {
13
+ $id: `https://schemas.platformatic.dev/@platformatic/tanstack/${packageJson.version}.json`,
14
+ $schema: 'http://json-schema.org/draft-07/schema#',
15
+ title: 'Platformatic TanStack Config',
16
+ type: 'object',
17
+ properties: {
18
+ $schema: {
19
+ type: 'string'
20
+ },
21
+ logger: utilsSchemaComponents.logger,
22
+ server: utilsSchemaComponents.server,
23
+ watch: basicSchemaComponents.watch,
24
+ application: basicSchemaComponents.buildableApplication,
25
+ runtime: utilsSchemaComponents.wrappedRuntime,
26
+ vite: viteSchemaComponents.vite
27
+ },
28
+ additionalProperties: false
29
+ }
30
+
31
+ /* c8 ignore next 3 */
32
+ if (process.argv[1] === import.meta.filename) {
33
+ console.log(JSON.stringify(schema, null, 2))
34
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/tanstack",
3
- "version": "0.0.1",
3
+ "version": "3.27.0",
4
4
  "description": "Platformatic TanStack Capability",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -14,7 +14,43 @@
14
14
  "url": "https://github.com/platformatic/platformatic/issues"
15
15
  },
16
16
  "homepage": "https://github.com/platformatic/platformatic#readme",
17
+ "dependencies": {
18
+ "semver": "^7.6.3",
19
+ "@platformatic/basic": "3.27.0",
20
+ "@platformatic/foundation": "3.27.0",
21
+ "@platformatic/vite": "3.27.0"
22
+ },
23
+ "devDependencies": {
24
+ "@tanstack/react-router": "^1.139.7",
25
+ "@tanstack/react-start": "^1.139.8",
26
+ "@tanstack/solid-router": "^1.139.12",
27
+ "@tanstack/solid-start": "^1.139.12",
28
+ "@vitejs/plugin-react": "^5.1.1",
29
+ "cleaner-spec-reporter": "^0.5.0",
30
+ "eslint": "9",
31
+ "fastify": "^5.0.0",
32
+ "json-schema-to-typescript": "^15.0.1",
33
+ "neostandard": "^0.12.0",
34
+ "nitro": "3.0.1-alpha.1",
35
+ "react": "^19.1.0",
36
+ "react-dom": "^19.1.0",
37
+ "solid-js": "^1.9.10",
38
+ "typescript": "^5.5.4",
39
+ "vite": "^7.2.4",
40
+ "vite-plugin-solid": "^2.11.10",
41
+ "vite-tsconfig-paths": "^5.1.4",
42
+ "ws": "^8.18.0",
43
+ "@platformatic/gateway": "3.27.0",
44
+ "@platformatic/service": "3.27.0"
45
+ },
17
46
  "engines": {
18
47
  "node": ">=22.19.0"
48
+ },
49
+ "scripts": {
50
+ "test": "node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js test/**/*.test.js",
51
+ "gen-schema": "node lib/schema.js > schema.json",
52
+ "gen-types": "json2ts > config.d.ts < schema.json",
53
+ "build": "npm run gen-schema && npm run gen-types",
54
+ "lint": "eslint"
19
55
  }
20
56
  }