-
Notifications
You must be signed in to change notification settings - Fork 123
feat: initial commit for web-core-server #772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PupilTong
merged 6 commits into
lynx-family:main
from
PupilTong:p/hw/ssr-initial-commit
May 12, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f8ed67
feat: initial commit for web-core-server
PupilTong 8d4f5a3
fix
PupilTong abb1f71
ignore vitest files for playwright
PupilTong a2dbf65
fix bench
PupilTong 2d09735
exclude bench
PupilTong 0e3f903
avoid to bench io
PupilTong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@lynx-js/web-worker-runtime": patch | ||
| --- | ||
|
|
||
| feat: return the offscreenDocument instance for startMainThread() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@lynx-js/offscreen-document": patch | ||
| --- | ||
|
|
||
| feat: support OffscreenDocument.innerHTML |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| build | ||
| Makefile | ||
| CMakeFiles |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "name": "@lynx-js/web-core-server", | ||
| "version": "0.12.0", | ||
| "private": false, | ||
| "description": "", | ||
| "keywords": [], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/lynx-family/lynx-stack.git", | ||
| "directory": "packages/web-platform/web-core-server" | ||
| }, | ||
| "license": "Apache-2.0", | ||
| "type": "module", | ||
| "main": "dist/index.js", | ||
| "typings": "dist/index.d.ts", | ||
| "files": [ | ||
| "dist", | ||
| "!dist/**/*.js.map", | ||
| "LICENSE.txt", | ||
| "Notice.txt", | ||
| "CHANGELOG.md", | ||
| "README.md" | ||
| ], | ||
| "devDependencies": { | ||
| "@lynx-js/web-constants": "workspace:*", | ||
| "@lynx-js/web-worker-rpc": "workspace:*", | ||
| "@lynx-js/web-worker-runtime": "workspace:*" | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
packages/web-platform/web-core-server/src/createLynxView.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { | ||
| flushElementTreeEndpoint, | ||
| mainThreadStartEndpoint, | ||
| type MainThreadStartConfigs, | ||
| } from '@lynx-js/web-constants'; | ||
| import { Rpc } from '@lynx-js/web-worker-rpc'; | ||
| import { startMainThread } from '@lynx-js/web-worker-runtime'; | ||
| import { loadTemplate } from './utils/loadTemplate.js'; | ||
|
|
||
| interface LynxViewConfig extends | ||
| Pick< | ||
| MainThreadStartConfigs, | ||
| 'browserConfig' | 'tagMap' | 'initData' | 'globalProps' | 'template' | ||
| > | ||
| { | ||
| } | ||
|
|
||
| export function createLynxView( | ||
| config: LynxViewConfig, | ||
| ) { | ||
| const { | ||
| template: rawTemplate, | ||
| browserConfig, | ||
| tagMap, | ||
| initData, | ||
| globalProps, | ||
| } = config; | ||
|
|
||
| const mainToUIChannel = new MessageChannel(); | ||
| const mainWithBackgroundChannel = new MessageChannel(); | ||
| const mainToUIMessagePort = mainToUIChannel.port2; | ||
| const uiToMainRpc = new Rpc(mainToUIChannel.port1, 'main-to-ui'); | ||
| const { docu: offscreenDocument } = startMainThread( | ||
| mainToUIMessagePort, | ||
| mainWithBackgroundChannel.port2, | ||
| ); | ||
| const { promise: firstPaintReadyPromise, resolve: firstPaintReady } = Promise | ||
| .withResolvers<void>(); | ||
| const template = loadTemplate(rawTemplate); | ||
| const mainThreadStart = uiToMainRpc.createCall(mainThreadStartEndpoint); | ||
| mainThreadStart({ | ||
| template, | ||
| initData, | ||
| globalProps, | ||
| browserConfig, | ||
| nativeModulesMap: {}, // the bts won't start | ||
| napiModulesMap: {}, // the bts won't start | ||
| tagMap, | ||
| }); | ||
| uiToMainRpc.registerHandler( | ||
| flushElementTreeEndpoint, | ||
| () => { | ||
| firstPaintReady(); | ||
| }, | ||
| ); | ||
|
|
||
| async function renderToString(): Promise<string> { | ||
| await firstPaintReadyPromise; | ||
| return offscreenDocument.innerHTML; | ||
| } | ||
| return { | ||
| renderToString, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { createLynxView } from './createLynxView.js'; | ||
|
|
||
| export { createLynxView }; | ||
|
|
||
| if (!globalThis.requestAnimationFrame) { | ||
| globalThis.requestAnimationFrame = (cb: FrameRequestCallback) => { | ||
| return setTimeout(cb, 0); | ||
| }; | ||
| globalThis.cancelAnimationFrame = (id: number) => { | ||
| clearTimeout(id); | ||
| }; | ||
| } |
143 changes: 143 additions & 0 deletions
143
packages/web-platform/web-core-server/src/utils/loadTemplate.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { globalMuteableVars, type LynxTemplate } from '@lynx-js/web-constants'; | ||
|
|
||
| const templateCache: Map<LynxTemplate, LynxTemplate> = new Map(); | ||
|
|
||
| function createJsModuleUrl(content: string): string { | ||
| const dataUrl = `data:text/javascript,${encodeURIComponent(content)}`; | ||
| return dataUrl; | ||
| } | ||
|
|
||
| function generateJavascriptUrl<T extends Record<string, string>>( | ||
| obj: T, | ||
| injectVars: string[], | ||
| injectWithBind: string[], | ||
| muteableVars: readonly string[], | ||
| ) { | ||
| injectVars = injectVars.concat(muteableVars); | ||
| return Object.fromEntries( | ||
| Object.entries(obj).map(([name, content]) => { | ||
| return [ | ||
| name, | ||
| createJsModuleUrl( | ||
| [ | ||
| 'globalThis.module.exports = function(lynx_runtime) {', | ||
| 'const module= {exports:{}};let exports = module.exports;', | ||
| 'var {', | ||
| injectVars.join(','), | ||
| '} = lynx_runtime;', | ||
| ...injectWithBind.map((nm) => | ||
| `const ${nm} = lynx_runtime.${nm}?.bind(lynx_runtime);` | ||
| ), | ||
| ';var globDynamicComponentEntry = \'__Card__\';', | ||
| 'var {__globalProps} = lynx;', | ||
| 'lynx_runtime._updateVars=()=>{', | ||
| ...muteableVars.map((nm) => | ||
| `${nm} = lynx_runtime.__lynxGlobalBindingValues.${nm};` | ||
| ), | ||
| '};\n', | ||
| content, | ||
| '\n return module.exports;}', | ||
| ].join(''), | ||
| ), | ||
| ]; | ||
| }), | ||
| ) as T; | ||
| } | ||
|
|
||
| const mainThreadInjectVars = [ | ||
| 'lynx', | ||
| 'globalThis', | ||
| '_ReportError', | ||
| '__AddConfig', | ||
| '__AddDataset', | ||
| '__GetAttributes', | ||
| '__GetComponentID', | ||
| '__GetDataByKey', | ||
| '__GetDataset', | ||
| '__GetElementConfig', | ||
| '__GetElementUniqueID', | ||
| '__GetID', | ||
| '__GetTag', | ||
| '__SetAttribute', | ||
| '__SetConfig', | ||
| '__SetDataset', | ||
| '__SetID', | ||
| '__UpdateComponentID', | ||
| '__GetConfig', | ||
| '__UpdateListCallbacks', | ||
| '__AppendElement', | ||
| '__ElementIsEqual', | ||
| '__FirstElement', | ||
| '__GetChildren', | ||
| '__GetParent', | ||
| '__InsertElementBefore', | ||
| '__LastElement', | ||
| '__NextElement', | ||
| '__RemoveElement', | ||
| '__ReplaceElement', | ||
| '__ReplaceElements', | ||
| '__SwapElement', | ||
| '__CreateComponent', | ||
| '__CreateElement', | ||
| '__CreatePage', | ||
| '__CreateView', | ||
| '__CreateText', | ||
| '__CreateRawText', | ||
| '__CreateImage', | ||
| '__CreateScrollView', | ||
| '__CreateWrapperElement', | ||
| '__CreateList', | ||
| '__AddEvent', | ||
| '__GetEvent', | ||
| '__GetEvents', | ||
| '__SetEvents', | ||
| '__AddClass', | ||
| '__SetClasses', | ||
| '__GetClasses', | ||
| '__AddInlineStyle', | ||
| '__SetInlineStyles', | ||
| '__SetCSSId', | ||
| '__OnLifecycleEvent', | ||
| '__FlushElementTree', | ||
| '__LoadLepusChunk', | ||
| 'SystemInfo', | ||
| ]; | ||
|
|
||
| const backgroundInjectVars = [ | ||
| 'NativeModules', | ||
| 'globalThis', | ||
| 'lynx', | ||
| 'lynxCoreInject', | ||
| 'SystemInfo', | ||
| ]; | ||
|
|
||
| const backgroundInjectWithBind = [ | ||
| 'Card', | ||
| 'Component', | ||
| ]; | ||
|
|
||
| export function loadTemplate( | ||
| rawTemplate: LynxTemplate, | ||
| ): LynxTemplate { | ||
| const decodedTemplate: LynxTemplate = templateCache.get(rawTemplate) ?? { | ||
| ...rawTemplate, | ||
| lepusCode: generateJavascriptUrl( | ||
| rawTemplate.lepusCode, | ||
| mainThreadInjectVars, | ||
| [], | ||
| globalMuteableVars, | ||
| ), | ||
| manifest: generateJavascriptUrl( | ||
| rawTemplate.manifest, | ||
| backgroundInjectVars, | ||
| backgroundInjectWithBind, | ||
| [], | ||
| ), | ||
| }; | ||
| templateCache.set(rawTemplate, decodedTemplate); | ||
| /** | ||
| * This will cause a memory leak, which is expected. | ||
| * We cannot ensure that the `URL.createObjectURL` created url will never be used, therefore here we keep it for the entire lifetime of this page. | ||
| */ | ||
| return decodedTemplate; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "extends": "../tsconfig.json", | ||
| "compilerOptions": { | ||
| "composite": true, | ||
| "rootDir": "./src", | ||
| "outDir": "./dist", | ||
| "lib": ["ESNext", "WebWorker"], | ||
| "exactOptionalPropertyTypes": true, | ||
| }, | ||
| "include": ["src"], | ||
| "references": [ | ||
| { "path": "../web-constants/tsconfig.json" }, | ||
| { "path": "../web-worker-rpc/tsconfig.json" }, | ||
| { "path": "../web-worker-runtime/tsconfig.json" }, | ||
| { "path": "../offscreen-document/tsconfig.json" }, | ||
| ], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/web-platform/web-tests/tests/__snapshots__/server.vitest.spec.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.