@gjsify/fs 0.4.23 → 0.4.25
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/file-handle.js +1 -1
- package/package.json +8 -8
package/lib/esm/file-handle.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./_virtual/_rolldown/runtime.js";import{encodeUint8Array as e,getEncodingFromOptions as t}from"./encoding.js";import{normalizePath as n}from"./utils.js";import{ReadStream as r}from"./read-stream.js";import{WriteStream as i}from"./write-stream.js";import{BigIntStats as a,STAT_ATTRIBUTES as o,Stats as s}from"./stats.js";import{mapOpenError as c,openIOChannel as l,resolveIOMode as u,shouldCreate as d}from"./file-handle-open.js";import{chmodSync as f,chownSync as p}from"./sync.js";import m from"@girs/glib-2.0";import h from"@girs/gio-2.0";import{Buffer as g}from"node:buffer";import{createInterface as _}from"node:readline";var v=class FileHandle{options;_file;_ioStream=null;_readStream=null;_gFile;_ioMode;_ioLock=Promise.resolve();static instances={};constructor(e){this.options=e,this.options.flags||=`r`,this.options.mode||=438;let t=n(e.path),r=d(e.flags),i=u(e.flags);try{this._file=l(t,i,r)}catch(e){throw c(e,t)}return this._file.set_encoding(null),this.fd=this._file.unix_get_fd(),this._gFile=h.File.new_for_path(t),this._ioMode=i,FileHandle.instances[this.fd]=this,FileHandle.getInstance(this.fd)}_getReadStream(){return this._ioStream?{input:this._ioStream.get_input_stream(),seekable:this._ioStream}:this._ioMode===`r`?(this._readStream||=this._gFile.read(null),{input:this._readStream,seekable:this._readStream}):(this._ioStream=this._gFile.open_readwrite(null),{input:this._ioStream.get_input_stream(),seekable:this._ioStream})}_getWriteStream(){if(this._ioStream)return this._ioStream;if(this._ioMode===`r`)throw Error(`FileHandle opened read-only; cannot write`);return this._ioStream=this._gFile.open_readwrite(null),this._ioStream}_serialize(e){let t=this._ioLock.catch(()=>{}).then(e);return this._ioLock=t,t}fd;static getInstance(e){if(!FileHandle.instances[e])throw Error(`No instance found for fd!`);return FileHandle.instances[e]}async appendFile(e,n){let r=t(n);typeof e==`string`&&(e=g.from(e)),r&&this._file.set_encoding(r);let[i,a]=this._file.write_chars(e,e.length);if(i===m.IOStatus.ERROR)throw Error(`Error on append to file!`)}async chown(e,t){p(n(this.options.path),e,t)}async chmod(e){f(n(this.options.path),e)}createReadStream(e){return new r(this.options.path,e)}createWriteStream(e){return new i(this.options.path,e)}async datasync(){this._file.flush()}async sync(){this._file.flush()}async read(...e){let t,n,r,i;if(typeof e[0]==`object`&&!(e[0]instanceof Uint8Array)&&!(e[0]instanceof g)){let a=e[0];t=a.buffer,n=a.offset,r=a.length,i=a.position}else t=e[0],n=e[1],r=e[2],i=e[3];let a=t,o=n??0,s=r??a?.byteLength??65536,c=i??0;return this._serialize(async()=>{let{input:e,seekable:n}=this._getReadStream();n.seek(BigInt(c),m.SeekType.SET,null);let r=e.read_bytes(s,null).get_data(),i=r?.length??0;return a&&r&&i>0&&a.set(r,o),{bytesRead:i,buffer:t}})}readableWebStream(){let e=globalThis.ReadableStream;if(typeof e!=`function`)throw Error(`readableWebStream() requires a global ReadableStream. Import "node:stream/web" or "@gjsify/streams" before calling this method.`);return new e}async readFile(n){let r=t(n,`buffer`);r&&this._file.set_encoding(r);let[i,a]=this._file.read_to_end();if(i===m.IOStatus.ERROR)throw Error(`Error on read from file!`);return e(r,a)}readLines(e){return _({input:this.createReadStream(e),crlfDelay:1/0})}async stat(e){let t=await new Promise((e,t)=>{this._gFile.query_info_async(o,h.FileQueryInfoFlags.NONE,m.PRIORITY_DEFAULT,null,(n,r)=>{try{e(this._gFile.query_info_finish(r))}catch(e){t(e)}})}),r=n(this.options.path);return e?.bigint?new a(t,r):new s(t,r)}async truncate(e=0){let t=Math.max(0,e);this._file.flush(),this._getWriteStream().get_output_stream().truncate(t,null)}async utimes(e,t){let{utimesSync:r}=await import(`./utimes.js`);r(n(this.options.path),e,t)}async writeFile(e,n){let r=t(n),i;i=typeof e==`string`?g.from(e,r||`utf8`):e,this._file.seek_position(0,m.SeekType.SET);let[a]=this._file.write_chars(i,i.length);if(a===m.IOStatus.ERROR)throw Error(`Error writing to file!`);this._file.flush()}async write(e,...n){let r=null,i=null,a=null,o=null;typeof e==`string`?(r=n[0],i=n[1]):(a=n[0],o=n[1],r=n[2]),i=t(i,typeof e==`string`?`utf8`:null);let s;s=typeof e==`string`?new TextEncoder().encode(e):e;let c=a??0,l=o??s.byteLength-c,u=s.slice(c,c+l),d=r??0;return{bytesWritten:await this._serialize(async()=>{let e=this._getWriteStream();e.seek(BigInt(d),m.SeekType.SET,null);let t=e.get_output_stream(),n=await new Promise((e,n)=>{t.write_bytes_async(new m.Bytes(u),m.PRIORITY_DEFAULT,null,(r,i)=>{try{e(t.write_bytes_finish(i))}catch(e){n(e)}})});return await new Promise((e,n)=>{t.flush_async(m.PRIORITY_DEFAULT,null,(r,i)=>{try{t.flush_finish(i),e()}catch(e){n(e)}})}),n}),buffer:e}}async writev(e,t){let n=0;for(let r of e){let e=g.from(r.buffer,r.byteOffset,r.byteLength),i=await this.write(e,0,e.byteLength,t==null?null:t+n);n+=i.bytesWritten}return{bytesWritten:n,buffers:e}}async readv(e,t){let n=0;for(let r of e){let e=await this.read({buffer:g.from(r.buffer,r.byteOffset,r.byteLength),position:t==null?null:t+n});if(n+=e.bytesRead,e.bytesRead<r.byteLength)break}return{bytesRead:n,buffers:e}}_flushSync(){this._file.flush()}_closeSync(){try{this._ioStream?.close(null)}catch{}try{this._readStream?.close(null)}catch{}this._ioStream=null,this._readStream=null;try{this._file.shutdown(!0)}catch{}delete
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";import{encodeUint8Array as e,getEncodingFromOptions as t}from"./encoding.js";import{normalizePath as n}from"./utils.js";import{ReadStream as r}from"./read-stream.js";import{WriteStream as i}from"./write-stream.js";import{BigIntStats as a,STAT_ATTRIBUTES as o,Stats as s}from"./stats.js";import{mapOpenError as c,openIOChannel as l,resolveIOMode as u,shouldCreate as d}from"./file-handle-open.js";import{chmodSync as f,chownSync as p}from"./sync.js";import m from"@girs/glib-2.0";import h from"@girs/gio-2.0";import{Buffer as g}from"node:buffer";import{createInterface as _}from"node:readline";var v=class FileHandle{options;_file;_ioStream=null;_readStream=null;_gFile;_ioMode;_ioLock=Promise.resolve();static instances={};constructor(e){this.options=e,this.options.flags||=`r`,this.options.mode||=438;let t=n(e.path),r=d(e.flags),i=u(e.flags);try{this._file=l(t,i,r)}catch(e){throw c(e,t)}return this._file.set_encoding(null),this.fd=this._file.unix_get_fd(),this._gFile=h.File.new_for_path(t),this._ioMode=i,FileHandle.instances[this.fd]=this,FileHandle.getInstance(this.fd)}_getReadStream(){return this._ioStream?{input:this._ioStream.get_input_stream(),seekable:this._ioStream}:this._ioMode===`r`?(this._readStream||=this._gFile.read(null),{input:this._readStream,seekable:this._readStream}):(this._ioStream=this._gFile.open_readwrite(null),{input:this._ioStream.get_input_stream(),seekable:this._ioStream})}_getWriteStream(){if(this._ioStream)return this._ioStream;if(this._ioMode===`r`)throw Error(`FileHandle opened read-only; cannot write`);return this._ioStream=this._gFile.open_readwrite(null),this._ioStream}_serialize(e){let t=this._ioLock.catch(()=>{}).then(e);return this._ioLock=t,t}fd;static getInstance(e){if(!FileHandle.instances[e])throw Error(`No instance found for fd!`);return FileHandle.instances[e]}async appendFile(e,n){let r=t(n);typeof e==`string`&&(e=g.from(e)),r&&this._file.set_encoding(r);let[i,a]=this._file.write_chars(e,e.length);if(i===m.IOStatus.ERROR)throw Error(`Error on append to file!`)}async chown(e,t){p(n(this.options.path),e,t)}async chmod(e){f(n(this.options.path),e)}createReadStream(e){return new r(this.options.path,e)}createWriteStream(e){return new i(this.options.path,e)}async datasync(){this._file.flush()}async sync(){this._file.flush()}async read(...e){let t,n,r,i;if(typeof e[0]==`object`&&!(e[0]instanceof Uint8Array)&&!(e[0]instanceof g)){let a=e[0];t=a.buffer,n=a.offset,r=a.length,i=a.position}else t=e[0],n=e[1],r=e[2],i=e[3];let a=t,o=n??0,s=r??a?.byteLength??65536,c=i??0;return this._serialize(async()=>{let{input:e,seekable:n}=this._getReadStream();n.seek(BigInt(c),m.SeekType.SET,null);let r=e.read_bytes(s,null).get_data(),i=r?.length??0;return a&&r&&i>0&&a.set(r,o),{bytesRead:i,buffer:t}})}readableWebStream(){let e=globalThis.ReadableStream;if(typeof e!=`function`)throw Error(`readableWebStream() requires a global ReadableStream. Import "node:stream/web" or "@gjsify/streams" before calling this method.`);return new e}async readFile(n){let r=t(n,`buffer`);r&&this._file.set_encoding(r);let[i,a]=this._file.read_to_end();if(i===m.IOStatus.ERROR)throw Error(`Error on read from file!`);return e(r,a)}readLines(e){return _({input:this.createReadStream(e),crlfDelay:1/0})}async stat(e){let t=await new Promise((e,t)=>{this._gFile.query_info_async(o,h.FileQueryInfoFlags.NONE,m.PRIORITY_DEFAULT,null,(n,r)=>{try{e(this._gFile.query_info_finish(r))}catch(e){t(e)}})}),r=n(this.options.path);return e?.bigint?new a(t,r):new s(t,r)}async truncate(e=0){let t=Math.max(0,e);this._file.flush(),this._getWriteStream().get_output_stream().truncate(t,null)}async utimes(e,t){let{utimesSync:r}=await import(`./utimes.js`);r(n(this.options.path),e,t)}async writeFile(e,n){let r=t(n),i;i=typeof e==`string`?g.from(e,r||`utf8`):e,this._file.seek_position(0,m.SeekType.SET);let[a]=this._file.write_chars(i,i.length);if(a===m.IOStatus.ERROR)throw Error(`Error writing to file!`);this._file.flush()}async write(e,...n){let r=null,i=null,a=null,o=null;typeof e==`string`?(r=n[0],i=n[1]):(a=n[0],o=n[1],r=n[2]),i=t(i,typeof e==`string`?`utf8`:null);let s;s=typeof e==`string`?new TextEncoder().encode(e):e;let c=a??0,l=o??s.byteLength-c,u=s.slice(c,c+l),d=r??0;return{bytesWritten:await this._serialize(async()=>{let e=this._getWriteStream();e.seek(BigInt(d),m.SeekType.SET,null);let t=e.get_output_stream(),n=await new Promise((e,n)=>{t.write_bytes_async(new m.Bytes(u),m.PRIORITY_DEFAULT,null,(r,i)=>{try{e(t.write_bytes_finish(i))}catch(e){n(e)}})});return await new Promise((e,n)=>{t.flush_async(m.PRIORITY_DEFAULT,null,(r,i)=>{try{t.flush_finish(i),e()}catch(e){n(e)}})}),n}),buffer:e}}async writev(e,t){let n=0;for(let r of e){let e=g.from(r.buffer,r.byteOffset,r.byteLength),i=await this.write(e,0,e.byteLength,t==null?null:t+n);n+=i.bytesWritten}return{bytesWritten:n,buffers:e}}async readv(e,t){let n=0;for(let r of e){let e=await this.read({buffer:g.from(r.buffer,r.byteOffset,r.byteLength),position:t==null?null:t+n});if(n+=e.bytesRead,e.bytesRead<r.byteLength)break}return{bytesRead:n,buffers:e}}_flushSync(){this._file.flush()}_closeSync(){try{this._ioStream?.close(null)}catch{}try{this._readStream?.close(null)}catch{}this._ioStream=null,this._readStream=null;try{this._file.shutdown(!0)}catch{}let e=FileHandle;delete e.instances[this.fd]}_readSync(e,t,n,r){let i=this._gFile.read(null);try{r!==null&&r>=0&&i.seek(r,m.SeekType.SET,null);let a=i.read_bytes(n,null).get_data();return new Uint8Array(e.buffer,e.byteOffset+t).set(a.subarray(0,a.length)),a.length}finally{i.close(null)}}_writeSync(e,t){let n=this._gFile.open_readwrite(null);try{return t!==null&&t>=0&&n.seek(t,m.SeekType.SET,null),n.get_output_stream().write_bytes(m.Bytes.new(e),null)}finally{n.close(null)}}async close(){try{this._ioStream?.close(null)}catch{}try{this._readStream?.close(null)}catch{}this._ioStream=null,this._readStream=null;try{this._file.shutdown(!0)}catch{}}async[Symbol.asyncDispose](){await this.close()}pull(...e){throw Error('FileHandle.pull() is not implemented in @gjsify/fs yet — requires `node:stream/iter` (Node 25.9+) which @gjsify/stream has not ported. Track gjsify Open TODO "Node 25 stream/iter integration".')}writer(e){throw Error('FileHandle.writer() is not implemented in @gjsify/fs yet — requires `node:stream/iter` (Node 25.9+) which @gjsify/stream has not ported. Track gjsify Open TODO "Node 25 stream/iter integration".')}};export{v as FileHandle};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/fs",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.25",
|
|
4
4
|
"description": "Node.js fs module for Gjs",
|
|
5
5
|
"module": "lib/esm/index.js",
|
|
6
6
|
"types": "lib/types/index.d.ts",
|
|
@@ -37,18 +37,18 @@
|
|
|
37
37
|
"fs"
|
|
38
38
|
],
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@gjsify/cli": "^0.4.
|
|
41
|
-
"@gjsify/unit": "^0.4.
|
|
40
|
+
"@gjsify/cli": "^0.4.25",
|
|
41
|
+
"@gjsify/unit": "^0.4.25",
|
|
42
42
|
"@types/node": "^25.9.1",
|
|
43
43
|
"typescript": "^6.0.3"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@girs/gio-2.0": "2.88.0-4.0.1",
|
|
47
47
|
"@girs/glib-2.0": "2.88.0-4.0.1",
|
|
48
|
-
"@gjsify/buffer": "^0.4.
|
|
49
|
-
"@gjsify/events": "^0.4.
|
|
50
|
-
"@gjsify/stream": "^0.4.
|
|
51
|
-
"@gjsify/url": "^0.4.
|
|
52
|
-
"@gjsify/utils": "^0.4.
|
|
48
|
+
"@gjsify/buffer": "^0.4.25",
|
|
49
|
+
"@gjsify/events": "^0.4.25",
|
|
50
|
+
"@gjsify/stream": "^0.4.25",
|
|
51
|
+
"@gjsify/url": "^0.4.25",
|
|
52
|
+
"@gjsify/utils": "^0.4.25"
|
|
53
53
|
}
|
|
54
54
|
}
|