Skip to content

Commit

Permalink
NAPI: added some File-related methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Feb 25, 2025
1 parent 6c1e254 commit 4d43659
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 33 deletions.
34 changes: 23 additions & 11 deletions src/libopencor/locAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@ import libOpenCOR from 'libopencor'
// @ts-expect-error (window.locAPI may or may not be defined and that is why we test it)
const _locAPI = window.locAPI ?? (await libOpenCOR())

class LOCAPI {
// Some general methods.
// Some general methods.

cppVersion(): boolean {
return electronAPI !== undefined
}
export function cppVersion(): boolean {
return electronAPI !== undefined
}

wasmVersion(): boolean {
return !this.cppVersion()
export function wasmVersion(): boolean {
return !cppVersion()
}

export function version(): string {
return cppVersion() ? _locAPI.version() : _locAPI.versionString()
}

// File API.

export class File {
_path: string
_contents: Uint8Array

constructor(path: string, contents?: Uint8Array) {
this._path = path
this._contents = contents ?? new Uint8Array()
}

version(): string {
return this.cppVersion() ? _locAPI.version() : _locAPI.versionString()
contents(): Uint8Array {
return this._contents
}
}

export const locAPI: LOCAPI = new LOCAPI()
8 changes: 8 additions & 0 deletions src/libopencor/src/file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "file.h"

#include <libopencor>

void fileCreate(const Napi::CallbackInfo &pInfo)
{
libOpenCOR::File::create(pInfo[0].ToString().Utf8Value());
}
3 changes: 3 additions & 0 deletions src/libopencor/src/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <napi.h>

void fileCreate(const Napi::CallbackInfo &pInfo);
9 changes: 9 additions & 0 deletions src/libopencor/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#include "file.h"
#include "version.h"

Napi::Object init(Napi::Env pEnv, Napi::Object pExports)
{
// Note: src/preload/index.ts must be in sync with this file.

// File API.

pExports.Set(Napi::String::New(pEnv, "fileCreate"), Napi::Function::New(pEnv, fileCreate));

// Version API.

pExports.Set(Napi::String::New(pEnv, "version"), Napi::Function::New(pEnv, version));

return pExports;
Expand Down
4 changes: 3 additions & 1 deletion src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ electron.contextBridge.exposeInMainWorld('electronAPI', {
})
})

// Give our renderer process access to the C++ version of libOpenCOR.
// Give our renderer process access to the native node module for libOpenCOR.
// Note: src/libopencor/src/main.cpp must be in sync with this file.

electron.contextBridge.exposeInMainWorld('locAPI', {
fileCreate: () => loc.fileCreate(),
version: () => loc.version()
})
44 changes: 24 additions & 20 deletions src/renderer/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as vue from 'vue'
import { fileContents, filePath, isRemoteFilePath, toastLife } from './common'
import { electronAPI } from '../../electronAPI'
import * as locAPI from '../../libopencor/locAPI'
const toast = useToast()
Expand Down Expand Up @@ -87,7 +88,7 @@ function onSettings(): void {
// Open a file.
function openFile(filePath: string, fileContentsPromise?: Promise<Uint8Array>): void {
if (fileContentsPromise !== undefined) {
function addToast(file: locAPI.File) {
function topContents(contents: string): string {
const numberOfBytesShown = 100
Expand All @@ -97,25 +98,31 @@ function openFile(filePath: string, fileContentsPromise?: Promise<Uint8Array>):
)
}
toast.add({
severity: 'info',
summary: 'Opening a file',
detail:
filePath +
'\n\nRaw contents:\n' +
topContents(new TextDecoder().decode(file.contents())) +
'\n\nUint8Array:\n' +
topContents(String(file.contents())) +
'\n\nBase64:\n' +
topContents(btoa(file.contents().reduce((data, byte) => data + String.fromCharCode(byte), ''))),
life: toastLife
})
}
if (fileContentsPromise !== undefined) {
showSpinningWheel()
fileContentsPromise
.then((fileContents) => {
const file = new locAPI.File(filePath, fileContents)
hideSpinningWheel()
toast.add({
severity: 'info',
summary: 'Opening a file',
detail:
filePath +
'\n\nRaw contents:\n' +
topContents(new TextDecoder().decode(fileContents)) +
'\n\nUint8Array:\n' +
topContents(String(fileContents)) +
'\n\nBase64:\n' +
topContents(btoa(fileContents.reduce((data, byte) => data + String.fromCharCode(byte), ''))),
life: toastLife
})
addToast(file)
})
.catch((error: unknown) => {
hideSpinningWheel()
Expand All @@ -132,16 +139,13 @@ function openFile(filePath: string, fileContentsPromise?: Promise<Uint8Array>):
showSpinningWheel()
}
const file = new locAPI.File(filePath)
if (isRemoteFilePath(filePath)) {
hideSpinningWheel()
}
toast.add({
severity: 'info',
summary: 'Opening a file',
detail: filePath,
life: toastLife
})
addToast(file)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/components/AboutDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

<script setup lang="ts">
import { electronAPI } from '../../../electronAPI'
import { locAPI } from '../../../libopencor/locAPI'
import * as locAPI from '../../../libopencor/locAPI'
defineEmits(['close'])
Expand Down

0 comments on commit 4d43659

Please sign in to comment.