@gjsify/native-fs-bridge 0.6.0 → 0.7.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/lib/esm/index.js CHANGED
@@ -1 +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};
1
+ import"./_virtual/_rolldown/runtime.js";import{assertNativeScript as e,isAndroid as t}from"@gjsify/native-platform";function androidIO(){return java.io}function iosFileManager(){return NSFileManager.defaultManager}const n={ENOENT:`no such file or directory`,EEXIST:`file already exists`,EACCES:`permission denied`};function fsError(e,t,r){let i=Error(`${e}: ${n[e]??e}, ${t} '${r}'`);return i.code=e,i.path=r,i.syscall=t,i}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=androidIO(),a=new i.File(e);if(!a.exists())return r(fsError(`ENOENT`,`open`,e));let o=new i.FileInputStream(a),s=[],c=Array.from({length:4096}),l;for(;(l=o.read(c))!==-1;)for(let e=0;e<l;e++)s.push(c[e]);o.close(),n(t?bytesToString(s,t):new Uint8Array(s))}catch(e){r(e instanceof Error?e:Error(String(e)))}})}function androidWriteFile(e,t,n=`utf8`){return new Promise((r,i)=>{try{let i=androidIO(),a=new i.File(e),o=a.getParentFile();o!==null&&!o.exists()&&o.mkdirs();let s=new i.FileOutputStream(a,!1),c=typeof t==`string`?stringToBytes(t,n):Array.from(t);s.write(c),s.close(),r()}catch(e){i(e instanceof Error?e:Error(String(e)))}})}function androidReaddir(e){return new Promise((t,n)=>{try{let r=new(androidIO()).File(e);if(!r.exists()||!r.isDirectory())return n(fsError(`ENOENT`,`scandir`,e));let i=r.list();t(i===null?[]:Array.from(i))}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function androidStat(e){return new Promise((t,n)=>{try{let r=new(androidIO()).File(e);if(!r.exists())return n(fsError(`ENOENT`,`stat`,e));let i=r.isFile()?r.length():0,a=new Date(r.lastModified()),o=r.isFile(),s=r.isDirectory();t({size:i,mtime:a,isFile:()=>o,isDirectory:()=>s})}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function androidMkdir(e,t){return new Promise((n,r)=>{try{let i=new(androidIO()).File(e);if(i.exists())return t?.recursive?n():r(fsError(`EEXIST`,`mkdir`,e));if(!i.mkdirs())return r(fsError(`EACCES`,`mkdir`,e));n()}catch(e){r(e instanceof Error?e:Error(String(e)))}})}function androidUnlink(e){return new Promise((t,n)=>{try{let r=new(androidIO()).File(e);if(!r.exists())return n(fsError(`ENOENT`,`unlink`,e));if(!r.delete())return n(fsError(`EACCES`,`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=iosFileManager().contentsAtPath_(e);if(i===null)return r(fsError(`ENOENT`,`open`,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,o=a.byteOffset===0&&a.byteLength===a.buffer.byteLength?a:a.slice();if(!globalThis.NSData.dataWithBytesLength_(o.buffer,o.byteLength).writeToFileAtomically_(e,!0))return i(fsError(`EACCES`,`open`,e));r()}catch(e){i(e instanceof Error?e:Error(String(e)))}})}function iosReaddir(e){return new Promise((t,n)=>{try{let r=iosFileManager().contentsOfDirectoryAtPathError_(e,null);if(r===null)return n(fsError(`ENOENT`,`scandir`,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=iosFileManager().attributesOfItemAtPathError_(e,null);if(r===null)return n(fsError(`ENOENT`,`stat`,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=iosFileManager();if(i.fileExistsAtPath_(e))return t?.recursive?n():r(fsError(`EEXIST`,`mkdir`,e));if(!i.createDirectoryAtPathWithIntermediateDirectoriesAttributesError_(e,t?.recursive??!1,null,null))return r(fsError(`EACCES`,`mkdir`,e));n()}catch(e){r(e instanceof Error?e:Error(String(e)))}})}function iosUnlink(e){return new Promise((t,n)=>{try{let r=iosFileManager();if(!r.fileExistsAtPath_(e))return n(fsError(`ENOENT`,`unlink`,e));if(!r.removeItemAtPathError_(e,null))return n(fsError(`EACCES`,`unlink`,e));t()}catch(e){n(e instanceof Error?e:Error(String(e)))}})}function readFile(n,r){e();let i=r==null?null:typeof r==`string`?r:r.encoding??null;return t?androidReadFile(n,i):iosReadFile(n,i)}function writeFile(n,r,i){e();let a=i==null?`utf8`:typeof i==`string`?i:i.encoding??`utf8`;return t?androidWriteFile(n,r,a):iosWriteFile(n,r,a)}function readdir(n,r){return e(),t?androidReaddir(n):iosReaddir(n)}function stat(n){return e(),t?androidStat(n):iosStat(n)}function mkdir(n,r){return e(),t?androidMkdir(n,r):iosMkdir(n,r)}function unlink(n){return e(),t?androidUnlink(n):iosUnlink(n)}function exists(t){return e(),stat(t).then(()=>!0,()=>!1)}export{exists,mkdir,readFile,readdir,stat,unlink,writeFile};
@@ -33,7 +33,7 @@ export declare function writeFile(path: string, data: string | Uint8Array, optio
33
33
  /**
34
34
  * Read the contents of a directory.
35
35
  */
36
- export declare function readdir(path: string, options?: ReaddirOptions | null): Promise<string[]>;
36
+ export declare function readdir(path: string, _options?: ReaddirOptions | null): Promise<string[]>;
37
37
  /**
38
38
  * Get file or directory statistics.
39
39
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/native-fs-bridge",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "NativeScript native bridge for node:fs/promises — Android (java.io.File) + iOS (NSFileManager)",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -35,11 +35,14 @@
35
35
  "ios"
36
36
  ],
37
37
  "devDependencies": {
38
- "@gjsify/cli": "^0.6.0",
39
- "@gjsify/unit": "^0.6.0",
38
+ "@gjsify/cli": "^0.7.0",
39
+ "@gjsify/unit": "^0.7.0",
40
40
  "@types/node": "^25.9.2",
41
41
  "typescript": "^6.0.3"
42
42
  },
43
+ "dependencies": {
44
+ "@gjsify/native-platform": "^0.7.0"
45
+ },
43
46
  "gjsify": {
44
47
  "runtimes": {
45
48
  "gjs": "none",