This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Node.js environment
- Loading branch information
Showing
9 changed files
with
171 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains 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 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,36 @@ | ||
import initPCLCore from '@/bind/build/pcl-core'; | ||
import { Emscripten } from '@/types'; | ||
import { InitOptions } from './base'; | ||
|
||
let isInitialized = false; | ||
|
||
async function init(options?: InitOptions) { | ||
const { | ||
arrayBuffer, | ||
url, | ||
fetchOptions: fetchSettings = { credentials: 'same-origin' }, | ||
} = options ?? {}; | ||
|
||
const moduleOptions: Emscripten.ModuleOpts = { | ||
wasmBinary: arrayBuffer, | ||
locateFile: (path, scriptDirectory) => url || scriptDirectory + path, | ||
fetchSettings, | ||
}; | ||
|
||
try { | ||
window.__PCLCore__ = await initPCLCore(moduleOptions); | ||
isInitialized = true; | ||
options?.onsuccess?.(); | ||
} catch (error) { | ||
options?.onerror?.(error); | ||
throw error; | ||
} | ||
} | ||
|
||
function destroy() { | ||
window.__PCLCore__ = null; | ||
isInitialized = false; | ||
} | ||
|
||
export { init, destroy, isInitialized, InitOptions }; | ||
export * from './base'; |
This file contains 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,49 @@ | ||
import initPCLCore from '@/bind/build/pcl-core.node'; | ||
import { Emscripten } from '@/types'; | ||
import { InitOptions } from './base'; | ||
import https from 'http'; | ||
|
||
let isInitialized = false; | ||
|
||
async function init(options?: InitOptions) { | ||
const { arrayBuffer, path, url } = options ?? {}; | ||
|
||
let wasmBinary = arrayBuffer; | ||
if (url) { | ||
wasmBinary = await fetchWASM(url); | ||
} | ||
|
||
const moduleOptions: Emscripten.ModuleOpts = { | ||
wasmBinary, | ||
locateFile: (p, scriptDirectory) => path || scriptDirectory + p, | ||
}; | ||
|
||
try { | ||
(global as any).__PCLCore__ = await initPCLCore(moduleOptions); | ||
isInitialized = true; | ||
options?.onsuccess?.(); | ||
} catch (error) { | ||
options?.onerror?.(error); | ||
throw error; | ||
} | ||
} | ||
|
||
function fetchWASM(url: string): Promise<ArrayBuffer> { | ||
return new Promise((resolve, reject) => { | ||
https | ||
.get(url, (res) => { | ||
res.on('data', (data) => { | ||
resolve(data); | ||
}); | ||
}) | ||
.on('error', reject); | ||
}); | ||
} | ||
|
||
function destroy() { | ||
(global as any).__PCLCore__ = null; | ||
isInitialized = false; | ||
} | ||
|
||
export { init, destroy, isInitialized, InitOptions }; | ||
export * from './base'; |
c52850f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
pcl-js – ./
pcl-js-darkce.vercel.app
pcl.js.org
pcl-js-git-master-darkce.vercel.app
pcljs.org
www.pcljs.org