@gjsify/native-fs-bridge 0.6.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 @@
|
|
|
1
|
+
var e=Object.defineProperty,__name=(t,n)=>e(t,`name`,{value:n,configurable:!0});export{__name};
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";const e=typeof java<`u`,t=e||typeof NSFileManager<`u`;function assertNativeScript(){if(!t)throw Error(`Platform not supported`)}function stringToBytes(e,t=`utf8`){let n=[];if(t===`base64`){let t=atob(e);for(let e=0;e<t.length;e++)n.push(t.charCodeAt(e))}else{let t=new TextEncoder().encode(e);for(let e=0;e<t.length;e++)n.push(t[e])}return n}function bytesToString(e,t=`utf8`){let n=new Uint8Array(e);if(t===`base64`){let e=``;for(let t=0;t<n.length;t++)e+=String.fromCharCode(n[t]);return btoa(e)}return new TextDecoder(t).decode(n)}function androidReadFile(e,t){return new Promise((n,r)=>{try{let i=java.io.File,a=java.io.FileInputStream,o=new i(e);if(!o.exists())return r(Object.assign(Error(`ENOENT: no such file or directory, open '${e}'`),{code:`ENOENT`,path:e}));let s=new a(o),c=[],l=Array(4096),u;for(;(u=s.read(l))!==-1;)for(let e=0;e<u;e++)c.push(l[e]);s.close(),n(t?bytesToString(c,t):new Uint8Array(c))}catch(e){r(e instanceof Error?e:Error(String(e)))}})}function androidWriteFile(e,t,n=`utf8`){return new Promise((r,i)=>{try{let i=java.io.File,a=java.io.FileOutputStream,o=new i(e),s=o.getParentFile();s!==null&&!s.exists()&&s.mkdirs();let c=new a(o,!1),l=typeof t==`string`?stringToBytes(t,n):Array.from(t);c.write(l),c.close(),r()}catch(e){i(e instanceof Error?e:Error(String(e)))}})}function androidReaddir(e){return new Promise((t,n)=>{try{let r=java.io.File,i=new r(e);if(!i.exists()||!i.isDirectory())return n(Object.assign(Error(`ENOENT: no such file or directory, scandir '${e}'`),{code:`ENOENT`,path:e}));let a=i.list();t(a===null?[]:Array.from(a))}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function androidStat(e){return new Promise((t,n)=>{try{let r=java.io.File,i=new r(e);if(!i.exists())return n(Object.assign(Error(`ENOENT: no such file or directory, stat '${e}'`),{code:`ENOENT`,path:e}));let a=i.isFile()?i.length():0,o=new Date(i.lastModified()),s=i.isFile(),c=i.isDirectory();t({size:a,mtime:o,isFile:()=>s,isDirectory:()=>c})}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function androidMkdir(e,t){return new Promise((n,r)=>{try{let i=java.io.File,a=new i(e);if(a.exists())return t?.recursive?n():r(Object.assign(Error(`EEXIST: file already exists, mkdir '${e}'`),{code:`EEXIST`,path:e}));if(!a.mkdirs())return r(Error(`EACCES: permission denied, mkdir '${e}'`));n()}catch(e){r(e instanceof Error?e:Error(String(e)))}})}function androidUnlink(e){return new Promise((t,n)=>{try{let r=java.io.File,i=new r(e);if(!i.exists())return n(Object.assign(Error(`ENOENT: no such file or directory, unlink '${e}'`),{code:`ENOENT`,path:e}));if(!i.delete())return n(Error(`EACCES: permission denied, unlink '${e}'`));t()}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function iosReadFile(e,t){return new Promise((n,r)=>{try{let i=NSFileManager.defaultManager.contentsAtPath_(e);if(i===null)return r(Object.assign(Error(`ENOENT: no such file or directory, open '${e}'`),{code:`ENOENT`,path:e}));let a=i.bytes,o=new Uint8Array(a,0,i.length);n(t?bytesToString(Array.from(o),t):new Uint8Array(o))}catch(e){r(e instanceof Error?e:Error(String(e)))}})}function iosWriteFile(e,t,n=`utf8`){return new Promise((r,i)=>{try{let a=typeof t==`string`?new Uint8Array(stringToBytes(t,n)):t;if(!globalThis.NSData.dataWithBytesLength_(a.buffer,a.byteLength).writeToFileAtomically_(e,!0))return i(Error(`EACCES: permission denied, open '${e}'`));r()}catch(e){i(e instanceof Error?e:Error(String(e)))}})}function iosReaddir(e){return new Promise((t,n)=>{try{let r=NSFileManager.defaultManager.contentsOfDirectoryAtPathError_(e,null);if(r===null)return n(Object.assign(Error(`ENOENT: no such file or directory, scandir '${e}'`),{code:`ENOENT`,path:e}));t(Array.from(r))}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function iosStat(e){return new Promise((t,n)=>{try{let r=NSFileManager.defaultManager.attributesOfItemAtPathError_(e,null);if(r===null)return n(Object.assign(Error(`ENOENT: no such file or directory, stat '${e}'`),{code:`ENOENT`,path:e}));let i=r.NSFileType,a=i===`NSFileTypeRegular`,o=i===`NSFileTypeDirectory`;t({size:r.NSFileSize??0,mtime:new Date(r.NSFileModificationDate??0),isFile:()=>a,isDirectory:()=>o})}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function iosMkdir(e,t){return new Promise((n,r)=>{try{let i=NSFileManager.defaultManager;if(i.fileExistsAtPath_(e))return t?.recursive?n():r(Object.assign(Error(`EEXIST: file already exists, mkdir '${e}'`),{code:`EEXIST`,path:e}));if(!i.createDirectoryAtPathWithIntermediateDirectoriesAttributesError_(e,t?.recursive??!1,null,null))return r(Error(`EACCES: permission denied, mkdir '${e}'`));n()}catch(e){r(e instanceof Error?e:Error(String(e)))}})}function iosUnlink(e){return new Promise((t,n)=>{try{let r=NSFileManager.defaultManager;if(!r.fileExistsAtPath_(e))return n(Object.assign(Error(`ENOENT: no such file or directory, unlink '${e}'`),{code:`ENOENT`,path:e}));if(!r.removeItemAtPathError_(e,null))return n(Error(`EACCES: permission denied, unlink '${e}'`));t()}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function readFile(t,n){assertNativeScript();let r=n==null?null:typeof n==`string`?n:n.encoding??null;return e?androidReadFile(t,r):iosReadFile(t,r)}function writeFile(t,n,r){assertNativeScript();let i=r==null?`utf8`:typeof r==`string`?r:r.encoding??`utf8`;return e?androidWriteFile(t,n,i):iosWriteFile(t,n,i)}function readdir(t,n){return assertNativeScript(),e?androidReaddir(t):iosReaddir(t)}function stat(t){return assertNativeScript(),e?androidStat(t):iosStat(t)}function mkdir(t,n){return assertNativeScript(),e?androidMkdir(t,n):iosMkdir(t,n)}function unlink(t){return assertNativeScript(),e?androidUnlink(t):iosUnlink(t)}function exists(e){return assertNativeScript(),stat(e).then(()=>!0,()=>!1)}export{exists,mkdir,readFile,readdir,stat,unlink,writeFile};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export interface ReadFileOptions {
|
|
2
|
+
encoding?: BufferEncoding | null;
|
|
3
|
+
flag?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface WriteFileOptions {
|
|
6
|
+
encoding?: BufferEncoding | null;
|
|
7
|
+
flag?: string;
|
|
8
|
+
mode?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface MkdirOptions {
|
|
11
|
+
recursive?: boolean;
|
|
12
|
+
mode?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface ReaddirOptions {
|
|
15
|
+
encoding?: BufferEncoding | null;
|
|
16
|
+
withFileTypes?: false;
|
|
17
|
+
}
|
|
18
|
+
export interface StatResult {
|
|
19
|
+
size: number;
|
|
20
|
+
mtime: Date;
|
|
21
|
+
isFile(): boolean;
|
|
22
|
+
isDirectory(): boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Read the entire contents of a file.
|
|
26
|
+
* Returns a string when an encoding is given, Uint8Array otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export declare function readFile(path: string, options?: ReadFileOptions | BufferEncoding | null): Promise<string | Uint8Array>;
|
|
29
|
+
/**
|
|
30
|
+
* Write data to a file, replacing the file if it already exists.
|
|
31
|
+
*/
|
|
32
|
+
export declare function writeFile(path: string, data: string | Uint8Array, options?: WriteFileOptions | BufferEncoding | null): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Read the contents of a directory.
|
|
35
|
+
*/
|
|
36
|
+
export declare function readdir(path: string, options?: ReaddirOptions | null): Promise<string[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Get file or directory statistics.
|
|
39
|
+
*/
|
|
40
|
+
export declare function stat(path: string): Promise<StatResult>;
|
|
41
|
+
/**
|
|
42
|
+
* Create a directory.
|
|
43
|
+
*/
|
|
44
|
+
export declare function mkdir(path: string, options?: MkdirOptions): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Remove a file.
|
|
47
|
+
*/
|
|
48
|
+
export declare function unlink(path: string): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Check whether a path exists (resolves true/false, never rejects).
|
|
51
|
+
*/
|
|
52
|
+
export declare function exists(path: string): Promise<boolean>;
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gjsify/native-fs-bridge",
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "NativeScript native bridge for node:fs/promises — Android (java.io.File) + iOS (NSFileManager)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "lib/esm/index.js",
|
|
7
|
+
"types": "lib/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"default": "./lib/esm/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
|
|
19
|
+
"check": "gjsify tsc --noEmit",
|
|
20
|
+
"build": "gjsify run build:gjsify && gjsify run build:types",
|
|
21
|
+
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
22
|
+
"build:types": "gjsify tsc",
|
|
23
|
+
"build:test": "gjsify run build:test:gjs && gjsify run build:test:node",
|
|
24
|
+
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
25
|
+
"build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
|
|
26
|
+
"test": "gjsify run build:gjsify && gjsify run build:test && gjsify run test:node && gjsify run test:gjs",
|
|
27
|
+
"test:gjs": "gjsify run test.gjs.mjs",
|
|
28
|
+
"test:node": "node test.node.mjs"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"gjs",
|
|
32
|
+
"nativescript",
|
|
33
|
+
"fs",
|
|
34
|
+
"android",
|
|
35
|
+
"ios"
|
|
36
|
+
],
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@gjsify/cli": "^0.6.0",
|
|
39
|
+
"@gjsify/unit": "^0.6.0",
|
|
40
|
+
"@types/node": "^25.9.2",
|
|
41
|
+
"typescript": "^6.0.3"
|
|
42
|
+
},
|
|
43
|
+
"gjsify": {
|
|
44
|
+
"runtimes": {
|
|
45
|
+
"gjs": "none",
|
|
46
|
+
"node": "none",
|
|
47
|
+
"browser": "none",
|
|
48
|
+
"nativescript": "native"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/gjsify/gjsify.git",
|
|
55
|
+
"directory": "packages/nativescript-bridge/fs"
|
|
56
|
+
},
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/gjsify/gjsify/issues"
|
|
59
|
+
},
|
|
60
|
+
"homepage": "https://github.com/gjsify/gjsify/tree/main/packages/nativescript-bridge/fs#readme"
|
|
61
|
+
}
|