@effect/platform-node
Advanced tools
Comparing version 0.10.4 to 0.10.5
@@ -60,2 +60,7 @@ /** | ||
* @since 1.0.0 | ||
* @category model | ||
*/ | ||
SeekMode, | ||
/** | ||
* @since 1.0.0 | ||
* @category constructor | ||
@@ -62,0 +67,0 @@ */ |
@@ -107,3 +107,3 @@ "use strict"; | ||
const nodeClose = (0, _Effectify.effectify)(NFS.close, (0, _error.handleErrnoException)("FileSystem", method), handleBadArgument(method)); | ||
return (path, options) => Effect.map(fd => makeFile(FileSystem.FileDescriptor(fd)))(Effect.acquireRelease(nodeOpen(path, options?.flag ?? "r", options?.mode), fd => Effect.orDie(nodeClose(fd)))); | ||
return (path, options) => Effect.map(fd => makeFile(FileSystem.FileDescriptor(fd), options?.flag?.startsWith("a") ?? false))(Effect.acquireRelease(nodeOpen(path, options?.flag ?? "r", options?.mode), fd => Effect.orDie(nodeClose(fd)))); | ||
}; | ||
@@ -122,5 +122,8 @@ const open = /*#__PURE__*/openFactory("open"); | ||
class FileImpl { | ||
constructor(fd) { | ||
constructor(fd, append) { | ||
this.fd = fd; | ||
this.append = append; | ||
this[_a] = _Function.identity; | ||
this.semaphore = Effect.unsafeMakeSemaphore(1); | ||
this.position = 0n; | ||
} | ||
@@ -130,14 +133,26 @@ get stat() { | ||
} | ||
read(buffer, options) { | ||
return Effect.map(nodeRead(this.fd, { | ||
seek(offset, from) { | ||
return this.semaphore.withPermits(1)(Effect.sync(() => { | ||
if (from === "start") { | ||
this.position = offset; | ||
} else if (from === "current") { | ||
this.position = this.position + offset; | ||
} | ||
return this.position; | ||
})); | ||
} | ||
read(buffer) { | ||
return this.semaphore.withPermits(1)(Effect.map(Effect.suspend(() => nodeRead(this.fd, { | ||
buffer, | ||
length: options?.length ? Number(options.length) : undefined, | ||
position: options?.offset | ||
}), FileSystem.Size); | ||
position: this.position | ||
})), bytesRead => { | ||
const sizeRead = FileSystem.Size(bytesRead); | ||
this.position = this.position + sizeRead; | ||
return sizeRead; | ||
})); | ||
} | ||
readAlloc(size, options) { | ||
return Effect.flatMap(Effect.sync(() => Buffer.allocUnsafeSlow(Number(size))), buffer => Effect.map(nodeReadAlloc(this.fd, { | ||
readAlloc(size) { | ||
return this.semaphore.withPermits(1)(Effect.flatMap(Effect.sync(() => Buffer.allocUnsafeSlow(Number(size))), buffer => Effect.map(nodeReadAlloc(this.fd, { | ||
buffer, | ||
length: options?.length ? Number(options.length) : undefined, | ||
position: options?.offset | ||
position: this.position | ||
}), bytesRead => { | ||
@@ -147,2 +162,3 @@ if (bytesRead === 0) { | ||
} | ||
this.position = this.position + BigInt(bytesRead); | ||
if (bytesRead === Number(size)) { | ||
@@ -154,12 +170,25 @@ return Option.some(buffer); | ||
return Option.some(dst); | ||
})); | ||
}))); | ||
} | ||
truncate(length) { | ||
return nodeTruncate(this.fd, length ? Number(length) : undefined); | ||
return this.semaphore.withPermits(1)(Effect.map(nodeTruncate(this.fd, length ? Number(length) : undefined), () => { | ||
if (!this.append) { | ||
const len = BigInt(length ?? 0); | ||
if (this.position > len) { | ||
this.position = len; | ||
} | ||
} | ||
})); | ||
} | ||
write(buffer) { | ||
return Effect.map(nodeWrite(this.fd, buffer), FileSystem.Size); | ||
return this.semaphore.withPermits(1)(Effect.map(Effect.suspend(() => nodeWrite(this.fd, buffer, undefined, undefined, this.append ? undefined : Number(this.position))), bytesWritten => { | ||
const sizeWritten = FileSystem.Size(bytesWritten); | ||
if (!this.append) { | ||
this.position = this.position + sizeWritten; | ||
} | ||
return sizeWritten; | ||
})); | ||
} | ||
writeAll(buffer) { | ||
return Effect.flatMap(nodeWriteAll(this.fd, buffer), bytesWritten => { | ||
writeAllChunk(buffer) { | ||
return Effect.flatMap(Effect.suspend(() => nodeWriteAll(this.fd, buffer, undefined, undefined, this.append ? undefined : Number(this.position))), bytesWritten => { | ||
if (bytesWritten === 0) { | ||
@@ -173,11 +202,15 @@ return Effect.fail(Error.SystemError({ | ||
})); | ||
} else if (bytesWritten < buffer.length) { | ||
return this.writeAll(buffer.subarray(bytesWritten)); | ||
} | ||
return Effect.unit; | ||
if (!this.append) { | ||
this.position = this.position + BigInt(bytesWritten); | ||
} | ||
return bytesWritten < buffer.length ? this.writeAllChunk(buffer.subarray(bytesWritten)) : Effect.unit; | ||
}); | ||
} | ||
writeAll(buffer) { | ||
return this.semaphore.withPermits(1)(this.writeAllChunk(buffer)); | ||
} | ||
} | ||
_a = FileSystem.FileTypeId; | ||
return fd => new FileImpl(fd); | ||
return (fd, append) => new FileImpl(fd, append); | ||
})(); | ||
@@ -184,0 +217,0 @@ // == makeTempFile |
@@ -6,7 +6,7 @@ "use strict"; | ||
}); | ||
exports.layerWin32 = exports.layerPosix = exports.layer = exports.Path = void 0; | ||
var _Context = /*#__PURE__*/require("@effect/data/Context"); | ||
exports.layerWin32 = exports.layerPosix = exports.layer = void 0; | ||
var Effect = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/io/Effect")); | ||
var Layer = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/io/Layer")); | ||
var _Error = /*#__PURE__*/require("@effect/platform/Error"); | ||
var _Path = /*#__PURE__*/require("@effect/platform/Path"); | ||
var NodePath = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("node:path")); | ||
@@ -16,5 +16,2 @@ var NodeUrl = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("node:url")); | ||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
/** @internal */ | ||
const Path = /*#__PURE__*/(0, _Context.Tag)(); | ||
exports.Path = Path; | ||
const fromFileUrl = url => Effect.try({ | ||
@@ -37,3 +34,3 @@ try: () => NodeUrl.fileURLToPath(url), | ||
/** @internal */ | ||
const layerPosix = /*#__PURE__*/Layer.succeed(Path, /*#__PURE__*/Path.of({ | ||
const layerPosix = /*#__PURE__*/Layer.succeed(_Path.Path, /*#__PURE__*/_Path.Path.of({ | ||
...NodePath.posix, | ||
@@ -45,3 +42,3 @@ fromFileUrl, | ||
exports.layerPosix = layerPosix; | ||
const layerWin32 = /*#__PURE__*/Layer.succeed(Path, /*#__PURE__*/Path.of({ | ||
const layerWin32 = /*#__PURE__*/Layer.succeed(_Path.Path, /*#__PURE__*/_Path.Path.of({ | ||
...NodePath.win32, | ||
@@ -53,3 +50,3 @@ fromFileUrl, | ||
exports.layerWin32 = layerWin32; | ||
const layer = /*#__PURE__*/Layer.succeed(Path, /*#__PURE__*/Path.of({ | ||
const layer = /*#__PURE__*/Layer.succeed(_Path.Path, /*#__PURE__*/_Path.Path.of({ | ||
...NodePath, | ||
@@ -56,0 +53,0 @@ fromFileUrl, |
{ | ||
"name": "@effect/platform-node", | ||
"version": "0.10.4", | ||
"version": "0.10.5", | ||
"license": "MIT", | ||
@@ -13,3 +13,3 @@ "repository": { | ||
"@effect/stream": "^0.33.0", | ||
"@effect/platform": "^0.10.3" | ||
"@effect/platform": "^0.10.4" | ||
}, | ||
@@ -16,0 +16,0 @@ "publishConfig": { |
@@ -65,2 +65,7 @@ /** | ||
* @since 1.0.0 | ||
* @category model | ||
*/ | ||
SeekMode, | ||
/** | ||
* @since 1.0.0 | ||
* @category constructor | ||
@@ -67,0 +72,0 @@ */ |
@@ -187,3 +187,3 @@ import { identity, pipe } from "@effect/data/Function" | ||
), | ||
Effect.map((fd) => makeFile(FileSystem.FileDescriptor(fd))) | ||
Effect.map((fd) => makeFile(FileSystem.FileDescriptor(fd), options?.flag?.startsWith("a") ?? false)) | ||
) | ||
@@ -225,4 +225,8 @@ } | ||
private readonly semaphore = Effect.unsafeMakeSemaphore(1) | ||
private position: bigint = 0n | ||
constructor( | ||
readonly fd: FileSystem.File.Descriptor | ||
readonly fd: FileSystem.File.Descriptor, | ||
private readonly append: boolean | ||
) {} | ||
@@ -234,18 +238,36 @@ | ||
read( | ||
buffer: Uint8Array, | ||
options?: FileSystem.FileReadOptions | ||
) { | ||
return Effect.map( | ||
nodeRead(this.fd, { | ||
buffer, | ||
length: options?.length ? Number(options.length) : undefined, | ||
position: options?.offset | ||
}), | ||
FileSystem.Size | ||
seek(offset: FileSystem.Size, from: FileSystem.SeekMode) { | ||
return this.semaphore.withPermits(1)( | ||
Effect.sync(() => { | ||
if (from === "start") { | ||
this.position = offset | ||
} else if (from === "current") { | ||
this.position = this.position + offset | ||
} | ||
return this.position | ||
}) | ||
) | ||
} | ||
readAlloc(size: FileSystem.Size, options?: FileSystem.FileReadOptions | undefined) { | ||
return Effect.flatMap( | ||
read(buffer: Uint8Array) { | ||
return this.semaphore.withPermits(1)( | ||
Effect.map( | ||
Effect.suspend(() => | ||
nodeRead(this.fd, { | ||
buffer, | ||
position: this.position | ||
}) | ||
), | ||
(bytesRead) => { | ||
const sizeRead = FileSystem.Size(bytesRead) | ||
this.position = this.position + sizeRead | ||
return sizeRead | ||
} | ||
) | ||
) | ||
} | ||
readAlloc(size: FileSystem.Size) { | ||
return this.semaphore.withPermits(1)(Effect.flatMap( | ||
Effect.sync(() => Buffer.allocUnsafeSlow(Number(size))), | ||
@@ -256,6 +278,5 @@ (buffer) => | ||
buffer, | ||
length: options?.length ? Number(options.length) : undefined, | ||
position: options?.offset | ||
position: this.position | ||
}), | ||
(bytesRead) => { | ||
(bytesRead): Option.Option<Buffer> => { | ||
if (bytesRead === 0) { | ||
@@ -265,2 +286,3 @@ return Option.none() | ||
this.position = this.position + BigInt(bytesRead) | ||
if (bytesRead === Number(size)) { | ||
@@ -275,16 +297,41 @@ return Option.some(buffer) | ||
) | ||
) | ||
)) | ||
} | ||
truncate(length?: FileSystem.Size) { | ||
return nodeTruncate(this.fd, length ? Number(length) : undefined) | ||
return this.semaphore.withPermits(1)( | ||
Effect.map(nodeTruncate(this.fd, length ? Number(length) : undefined), () => { | ||
if (!this.append) { | ||
const len = BigInt(length ?? 0) | ||
if (this.position > len) { | ||
this.position = len | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
write(buffer: Uint8Array) { | ||
return Effect.map(nodeWrite(this.fd, buffer), FileSystem.Size) | ||
return this.semaphore.withPermits(1)( | ||
Effect.map( | ||
Effect.suspend(() => | ||
nodeWrite(this.fd, buffer, undefined, undefined, this.append ? undefined : Number(this.position)) | ||
), | ||
(bytesWritten) => { | ||
const sizeWritten = FileSystem.Size(bytesWritten) | ||
if (!this.append) { | ||
this.position = this.position + sizeWritten | ||
} | ||
return sizeWritten | ||
} | ||
) | ||
) | ||
} | ||
writeAll(buffer: Uint8Array): Effect.Effect<never, Error.PlatformError, void> { | ||
private writeAllChunk(buffer: Uint8Array): Effect.Effect<never, Error.PlatformError, void> { | ||
return Effect.flatMap( | ||
nodeWriteAll(this.fd, buffer), | ||
Effect.suspend(() => | ||
nodeWriteAll(this.fd, buffer, undefined, undefined, this.append ? undefined : Number(this.position)) | ||
), | ||
(bytesWritten) => { | ||
@@ -299,12 +346,19 @@ if (bytesWritten === 0) { | ||
})) | ||
} else if (bytesWritten < buffer.length) { | ||
return this.writeAll(buffer.subarray(bytesWritten)) | ||
} | ||
return Effect.unit | ||
if (!this.append) { | ||
this.position = this.position + BigInt(bytesWritten) | ||
} | ||
return bytesWritten < buffer.length ? this.writeAllChunk(buffer.subarray(bytesWritten)) : Effect.unit | ||
} | ||
) | ||
} | ||
writeAll(buffer: Uint8Array) { | ||
return this.semaphore.withPermits(1)(this.writeAllChunk(buffer)) | ||
} | ||
} | ||
return (fd: FileSystem.File.Descriptor): FileSystem.File => new FileImpl(fd) | ||
return (fd: FileSystem.File.Descriptor, append: boolean): FileSystem.File => new FileImpl(fd, append) | ||
})() | ||
@@ -311,0 +365,0 @@ |
@@ -1,12 +0,8 @@ | ||
import { Tag } from "@effect/data/Context" | ||
import * as Effect from "@effect/io/Effect" | ||
import * as Layer from "@effect/io/Layer" | ||
import { BadArgument } from "@effect/platform/Error" | ||
import type { Path as _Path } from "@effect/platform/Path" | ||
import { Path } from "@effect/platform/Path" | ||
import * as NodePath from "node:path" | ||
import * as NodeUrl from "node:url" | ||
/** @internal */ | ||
export const Path = Tag<_Path>() | ||
const fromFileUrl = (url: URL): Effect.Effect<never, BadArgument, string> => | ||
@@ -13,0 +9,0 @@ Effect.try({ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
225297
3671
Updated@effect/platform@^0.10.4