-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interceptors to filesystem (#206)
- Loading branch information
1 parent
e6e77b1
commit 0b6d63b
Showing
91 changed files
with
858 additions
and
457 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
wasi-emscripten-fs/src/commonMain/kotlin/FileSystemEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.filesystem | ||
|
||
import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemCommonConfig | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl.FileSystemEngineConfig | ||
|
||
public interface FileSystemEngine<E : FileSystemEngineConfig> { | ||
@InternalWasmSqliteHelperApi | ||
public fun create( | ||
commonConfig: FileSystemCommonConfig, | ||
engineConfig: E.() -> Unit, | ||
): FileSystem | ||
} |
26 changes: 26 additions & 0 deletions
26
wasi-emscripten-fs/src/commonMain/kotlin/FileSystemInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
@file:Suppress("WRONG_MULTIPLE_MODIFIERS_ORDER") | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.filesystem | ||
|
||
import arrow.core.Either | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation | ||
|
||
public interface FileSystemInterceptor { | ||
public fun <I : Any, E : FileSystemOperationError, R : Any> intercept( | ||
chain: Chain<I, E, R>, | ||
): Either<E, R> | ||
|
||
public interface Chain<I : Any, out E : FileSystemOperationError, out R : Any> { | ||
public val operation: FileSystemOperation<I, E, R> | ||
public val input: I | ||
|
||
public fun proceed(input: I): Either<E, R> | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemCommonConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl | ||
|
||
import ru.pixnews.wasm.sqlite.open.helper.common.api.InternalWasmSqliteHelperApi | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor | ||
|
||
@InternalWasmSqliteHelperApi | ||
public interface FileSystemCommonConfig { | ||
public val interceptors: List<FileSystemInterceptor> | ||
} |
46 changes: 46 additions & 0 deletions
46
wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemConfigBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl | ||
|
||
import arrow.core.Either | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor.Chain | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError | ||
|
||
@FileSystemDsl | ||
public class FileSystemConfigBlock<E : FileSystemEngineConfig> { | ||
private val _interceptors: MutableList<FileSystemInterceptor> = mutableListOf() | ||
internal val interceptors: List<FileSystemInterceptor> get() = _interceptors | ||
|
||
internal var engineConfig: E.() -> Unit = {} | ||
private set | ||
|
||
public fun addInterceptor(interceptor: FileSystemInterceptor) { | ||
_interceptors += interceptor | ||
} | ||
|
||
public inline fun addInterceptor( | ||
crossinline block: (chain: Chain<Any, FileSystemOperationError, Any>) -> Either<FileSystemOperationError, *>, | ||
): Unit = addInterceptor( | ||
object : FileSystemInterceptor { | ||
override fun <I : Any, E : FileSystemOperationError, R : Any> intercept( | ||
chain: Chain<I, E, R>, | ||
): Either<E, R> { | ||
@Suppress("UNCHECKED_CAST") | ||
return block(chain as Chain<Any, FileSystemOperationError, Any>) as Either<E, R> | ||
} | ||
}, | ||
) | ||
|
||
public fun engine(block: E.() -> Unit) { | ||
val oldConfig = engineConfig | ||
engineConfig = { | ||
oldConfig() | ||
block() | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemDsl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl | ||
|
||
@DslMarker | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPEALIAS, AnnotationTarget.TYPE, AnnotationTarget.FUNCTION) | ||
public annotation class FileSystemDsl |
10 changes: 10 additions & 0 deletions
10
wasi-emscripten-fs/src/commonMain/kotlin/dsl/FileSystemEngineConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.dsl | ||
|
||
@FileSystemDsl | ||
public interface FileSystemEngineConfig |
58 changes: 58 additions & 0 deletions
58
wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/DelegateOperationsFileSystem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs | ||
|
||
import arrow.core.Either | ||
import arrow.core.left | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystem | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor.Chain | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.NotImplemented | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation | ||
|
||
internal class DelegateOperationsFileSystem( | ||
private val operations: Map<FileSystemOperation<*, *, *>, FileSystemOperationHandler<*, *, *>>, | ||
interceptors: List<FileSystemInterceptor>, | ||
) : FileSystem { | ||
private val interceptors: List<FileSystemInterceptor> = buildList { | ||
addAll(interceptors) | ||
add(ExecuteOperationInterceptor(operations)) | ||
} | ||
|
||
override fun <I : Any, E : FileSystemOperationError, R : Any> execute( | ||
operation: FileSystemOperation<I, E, R>, | ||
input: I, | ||
): Either<E, R> { | ||
val chain = InterceptorChain( | ||
operation = operation, | ||
input = input, | ||
interceptors = interceptors, | ||
) | ||
return chain.proceed(input) | ||
} | ||
|
||
override fun isOperationSupported(operation: FileSystemOperation<*, *, *>): Boolean { | ||
return operations.containsKey(operation) | ||
} | ||
|
||
override fun close() = Unit | ||
|
||
private class ExecuteOperationInterceptor( | ||
private val operations: Map<FileSystemOperation<*, *, *>, FileSystemOperationHandler<*, *, *>>, | ||
) : FileSystemInterceptor { | ||
@Suppress("UNCHECKED_CAST") | ||
override fun <I : Any, E : FileSystemOperationError, R : Any> intercept(chain: Chain<I, E, R>): Either<E, R> { | ||
val handler = operations[chain.operation] as? FileSystemOperationHandler<I, E, R> | ||
if (handler == null) { | ||
return NotImplemented.left() as Either<E, Nothing> | ||
} | ||
|
||
return handler.invoke(chain.input) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
wasi-emscripten-fs/src/commonMain/kotlin/internal/delegatefs/InterceptorChain.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2024, the wasm-sqlite-open-helper project authors and contributors. Please see the AUTHORS file | ||
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package ru.pixnews.wasm.sqlite.open.helper.host.filesystem.internal.delegatefs | ||
|
||
import arrow.core.Either | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.FileSystemInterceptor | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.error.FileSystemOperationError | ||
import ru.pixnews.wasm.sqlite.open.helper.host.filesystem.op.FileSystemOperation | ||
|
||
internal class InterceptorChain<I : Any, out E : FileSystemOperationError, out R : Any>( | ||
override val operation: FileSystemOperation<I, E, R>, | ||
override val input: I, | ||
private val interceptors: List<FileSystemInterceptor>, | ||
private val index: Int = 0, | ||
) : FileSystemInterceptor.Chain<I, E, R> { | ||
override fun proceed(input: I): Either<E, R> { | ||
val interceptor = interceptors.getOrNull(index) ?: error("End of interceptor chain") | ||
val next = InterceptorChain( | ||
interceptors = interceptors, | ||
index = index + 1, | ||
operation = operation, | ||
input = input, | ||
) | ||
return interceptor.intercept(next) | ||
} | ||
} |
Oops, something went wrong.