@use-voltra/ios-server 2.0.0 → 2.1.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.
- package/package.json +5 -4
- package/src/index.ts +191 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@use-voltra/ios-server",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Voltra server rendering for iOS",
|
|
5
5
|
"main": "build/cjs/index.js",
|
|
6
6
|
"module": "build/esm/index.js",
|
|
@@ -16,12 +16,13 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"build",
|
|
19
|
+
"src",
|
|
19
20
|
"README.md"
|
|
20
21
|
],
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"@use-voltra/
|
|
23
|
-
"@use-voltra/core": "^2.
|
|
24
|
-
"@use-voltra/
|
|
23
|
+
"@use-voltra/ios": "^2.1.0",
|
|
24
|
+
"@use-voltra/core": "^2.1.0",
|
|
25
|
+
"@use-voltra/server": "^2.1.0"
|
|
25
26
|
},
|
|
26
27
|
"keywords": [
|
|
27
28
|
"voltra",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
|
|
3
|
+
import { promisify } from 'node:util'
|
|
4
|
+
import { brotliCompress, constants } from 'node:zlib'
|
|
5
|
+
|
|
6
|
+
import { type ComponentRegistry, createVoltraRenderer, ensurePayloadWithinBudget } from '@use-voltra/core'
|
|
7
|
+
import { getComponentId, type LiveActivityVariants, type WidgetVariants } from '@use-voltra/ios'
|
|
8
|
+
import type {
|
|
9
|
+
WidgetRenderRequest,
|
|
10
|
+
WidgetUpdateExpressHandler,
|
|
11
|
+
WidgetUpdateHandler,
|
|
12
|
+
WidgetUpdateNodeHandler,
|
|
13
|
+
} from '@use-voltra/server'
|
|
14
|
+
import {
|
|
15
|
+
createWidgetUpdateExpressHandler,
|
|
16
|
+
createWidgetUpdateHandler,
|
|
17
|
+
createWidgetUpdateNodeHandler,
|
|
18
|
+
} from '@use-voltra/server'
|
|
19
|
+
import type { ReactNode } from 'react'
|
|
20
|
+
|
|
21
|
+
export { Voltra } from '@use-voltra/ios'
|
|
22
|
+
export type { LiveActivityVariants, WidgetVariants }
|
|
23
|
+
export type {
|
|
24
|
+
WidgetRenderRequest,
|
|
25
|
+
WidgetUpdateExpressHandler,
|
|
26
|
+
WidgetUpdateHandler,
|
|
27
|
+
WidgetUpdateNodeHandler,
|
|
28
|
+
} from '@use-voltra/server'
|
|
29
|
+
export type { WidgetPlatform, WidgetTheme } from '@use-voltra/server'
|
|
30
|
+
|
|
31
|
+
type LiveActivityJson = {
|
|
32
|
+
v: number
|
|
33
|
+
s?: Record<string, unknown>[]
|
|
34
|
+
e?: unknown[]
|
|
35
|
+
ls?: unknown
|
|
36
|
+
ls_background_tint?: string
|
|
37
|
+
isl_keyline_tint?: string
|
|
38
|
+
isl_exp_c?: unknown
|
|
39
|
+
isl_exp_l?: unknown
|
|
40
|
+
isl_exp_t?: unknown
|
|
41
|
+
isl_exp_b?: unknown
|
|
42
|
+
isl_cmp_l?: unknown
|
|
43
|
+
isl_cmp_t?: unknown
|
|
44
|
+
isl_min?: unknown
|
|
45
|
+
saf_sm?: unknown
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type LockScreenVariantObject = {
|
|
49
|
+
content?: ReactNode
|
|
50
|
+
activityBackgroundTint?: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const defaultComponentRegistry: ComponentRegistry = {
|
|
54
|
+
getComponentId,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const brotliCompressAsync = promisify(brotliCompress)
|
|
58
|
+
|
|
59
|
+
function isLockScreenVariantObject(value: LiveActivityVariants['lockScreen']): value is LockScreenVariantObject {
|
|
60
|
+
return typeof value === 'object' && value !== null && ('content' in value || 'activityBackgroundTint' in value)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const compressPayload = async (jsonString: string): Promise<string> => {
|
|
64
|
+
const jsonBuffer = Buffer.from(jsonString, 'utf8')
|
|
65
|
+
|
|
66
|
+
const compressedBuffer = await brotliCompressAsync(jsonBuffer, {
|
|
67
|
+
params: {
|
|
68
|
+
[constants.BROTLI_PARAM_QUALITY]: 2,
|
|
69
|
+
[constants.BROTLI_PARAM_SIZE_HINT]: jsonBuffer.length,
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
return compressedBuffer.toString('base64')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const renderWidgetToJson = (variants: WidgetVariants): Record<string, any> => {
|
|
77
|
+
const renderer = createVoltraRenderer(defaultComponentRegistry)
|
|
78
|
+
|
|
79
|
+
for (const [family, content] of Object.entries(variants) as [string, WidgetVariants[keyof WidgetVariants]][]) {
|
|
80
|
+
if (content !== undefined && content !== null) {
|
|
81
|
+
renderer.addRootNode(family, content as NonNullable<WidgetVariants[keyof WidgetVariants]>)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return renderer.render()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const renderWidgetToString = (variants: WidgetVariants): string => {
|
|
89
|
+
return JSON.stringify(renderWidgetToJson(variants))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const renderLiveActivityToJson = (variants: LiveActivityVariants): LiveActivityJson => {
|
|
93
|
+
const renderer = createVoltraRenderer(defaultComponentRegistry)
|
|
94
|
+
|
|
95
|
+
if (variants.lockScreen) {
|
|
96
|
+
const lockScreenVariant = variants.lockScreen
|
|
97
|
+
if (isLockScreenVariantObject(lockScreenVariant)) {
|
|
98
|
+
if (lockScreenVariant.content) {
|
|
99
|
+
renderer.addRootNode('ls', lockScreenVariant.content)
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
renderer.addRootNode('ls', lockScreenVariant as ReactNode)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (variants.island) {
|
|
107
|
+
if (variants.island.expanded) {
|
|
108
|
+
if (variants.island.expanded.center) {
|
|
109
|
+
renderer.addRootNode('isl_exp_c', variants.island.expanded.center)
|
|
110
|
+
}
|
|
111
|
+
if (variants.island.expanded.leading) {
|
|
112
|
+
renderer.addRootNode('isl_exp_l', variants.island.expanded.leading)
|
|
113
|
+
}
|
|
114
|
+
if (variants.island.expanded.trailing) {
|
|
115
|
+
renderer.addRootNode('isl_exp_t', variants.island.expanded.trailing)
|
|
116
|
+
}
|
|
117
|
+
if (variants.island.expanded.bottom) {
|
|
118
|
+
renderer.addRootNode('isl_exp_b', variants.island.expanded.bottom)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (variants.island.compact) {
|
|
122
|
+
if (variants.island.compact.leading) {
|
|
123
|
+
renderer.addRootNode('isl_cmp_l', variants.island.compact.leading)
|
|
124
|
+
}
|
|
125
|
+
if (variants.island.compact.trailing) {
|
|
126
|
+
renderer.addRootNode('isl_cmp_t', variants.island.compact.trailing)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (variants.island.minimal) {
|
|
130
|
+
renderer.addRootNode('isl_min', variants.island.minimal)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (variants.supplementalActivityFamilies?.small) {
|
|
135
|
+
renderer.addRootNode('saf_sm', variants.supplementalActivityFamilies.small)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const result = renderer.render() as LiveActivityJson
|
|
139
|
+
|
|
140
|
+
if (
|
|
141
|
+
variants.lockScreen &&
|
|
142
|
+
typeof variants.lockScreen === 'object' &&
|
|
143
|
+
'activityBackgroundTint' in variants.lockScreen &&
|
|
144
|
+
variants.lockScreen.activityBackgroundTint
|
|
145
|
+
) {
|
|
146
|
+
result.ls_background_tint = variants.lockScreen.activityBackgroundTint
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (variants.island?.keylineTint) {
|
|
150
|
+
result.isl_keyline_tint = variants.island.keylineTint
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return result
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export const renderLiveActivityToString = async (variants: LiveActivityVariants): Promise<string> => {
|
|
157
|
+
const jsonString = JSON.stringify(renderLiveActivityToJson(variants))
|
|
158
|
+
const compressedBase64 = await compressPayload(jsonString)
|
|
159
|
+
ensurePayloadWithinBudget(compressedBase64)
|
|
160
|
+
|
|
161
|
+
return compressedBase64
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface IOSWidgetUpdateHandlerOptions {
|
|
165
|
+
render: (request: WidgetRenderRequest) => Promise<WidgetVariants | null> | WidgetVariants | null
|
|
166
|
+
validateToken?: (token: string) => Promise<boolean> | boolean
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function toSharedOptions(options: IOSWidgetUpdateHandlerOptions) {
|
|
170
|
+
return {
|
|
171
|
+
validateToken: options.validateToken,
|
|
172
|
+
renderIos: async (request: WidgetRenderRequest) => {
|
|
173
|
+
const variants = await options.render(request)
|
|
174
|
+
return variants ? renderWidgetToString(variants) : null
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function createIOSWidgetUpdateHandler(options: IOSWidgetUpdateHandlerOptions): WidgetUpdateHandler {
|
|
180
|
+
return createWidgetUpdateHandler(toSharedOptions(options))
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function createIOSWidgetUpdateNodeHandler(options: IOSWidgetUpdateHandlerOptions): WidgetUpdateNodeHandler {
|
|
184
|
+
return createWidgetUpdateNodeHandler(toSharedOptions(options))
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function createIOSWidgetUpdateExpressHandler(
|
|
188
|
+
options: IOSWidgetUpdateHandlerOptions
|
|
189
|
+
): WidgetUpdateExpressHandler {
|
|
190
|
+
return createWidgetUpdateExpressHandler(toSharedOptions(options))
|
|
191
|
+
}
|