koffi-cream 2.11.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.
Files changed (4) hide show
  1. package/README.md +59 -0
  2. package/index.d.ts +169 -0
  3. package/index.js +51 -0
  4. package/package.json +56 -0
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Koffi-Cream
2
+
3
+ A lighter packaging of Niels Martignène's excellent [Koffi](https://koffi.dev).
4
+
5
+ Just `npm install koffi-cream` instead of `koffi` and use it like the real thing:
6
+
7
+ ```ts
8
+ import koffi from 'koffi-cream'
9
+
10
+ const lib = koffi.lib('some_lib')
11
+ const fn = lib.func('int some_func(int a, int b)')
12
+ // etc.
13
+ ```
14
+
15
+ ## The why and the how
16
+
17
+ Most importantly: **this package is neither a fork nor a patch. It *is* the original Koffi**, only packaged differently to avoid downloading a megalithic[^1] package full of unnecessary files[^2].
18
+
19
+ `koffi-cream` repackages Koffi using the same strategy as many popular packages in the JavaScript community like `esbuild` or `swc`: by leveraging the `optionalDependencies`, `os`, `cpu` and `libc` properties in `package.json`.
20
+
21
+ This way, when you install `koffi-cream`, your package manager will only download and install the build that is right for your platform. For example, on Windows AMD/Intel 64 bit, your package manager will install:
22
+ - `koffi-cream` (this package): 3.4 kB compressed / 12.8 kB uncompressed
23
+ - `@septh/koffi-win32-x64`: 455 kB compressed / 2.3 MB uncompressed
24
+
25
+ **That's 97% off compared to the original Koffi package!**
26
+
27
+ [^1]: As of 2.11.0, Koffi weights 15 MB compressed and 75 MB uncompressed!
28
+ [^2]: Koffi's package includes 16 natives binaries (of which 15 won't work on your platform), the build tools and the full source code!
29
+
30
+ ## Available packages
31
+
32
+ `koffi-cream` only offers a subset of Koffi's 16 builds:
33
+
34
+ * [@septh/koffi-darwin-arm64](https://www.npmjs.com/package/@septh/koffi-darwin-arm64)
35
+ * [@septh/koffi-darwin-x64](https://www.npmjs.com/package/@septh/koffi-darwin-x64)
36
+ * [@septh/koffi-freebsd-arm64](https://www.npmjs.com/package/@septh/koffi-freebsd-arm64)
37
+ * [@septh/koffi-freebsd-x64](https://www.npmjs.com/package/@septh/koffi-freebsd-x64)
38
+ * [@septh/koffi-linux-arm64](https://www.npmjs.com/package/@septh/koffi-linux-arm64)
39
+ * [@septh/koffi-linux-riscv64](https://www.npmjs.com/package/@septh/koffi-linux-riscv64)
40
+ * [@septh/koffi-linux-x64-glibc](https://www.npmjs.com/package/@septh/koffi-linux-x64-glibc)
41
+ * [@septh/koffi-linux-x64-musl](https://www.npmjs.com/package/@septh/koffi-linux-x64-musl)
42
+ * [@septh/koffi-openbsd-x64](https://www.npmjs.com/package/@septh/koffi-openbsd-x64)
43
+ * [@septh/koffi-win32-arm64](https://www.npmjs.com/package/@septh/koffi-win32-arm64)
44
+ * [@septh/koffi-win32-x64](https://www.npmjs.com/package/@septh/koffi-win32-x64)
45
+
46
+ I do not plan to add other builds, especially not the 32 bit binaries. If you need these, stick with Koffi.
47
+
48
+ ## Version numbering
49
+
50
+ To make things easy on the user, `koffi-cream`'s version number will always be aligned with Koffi's.
51
+
52
+ Hence, the first version available of `koffi-cream` is 2.11.0 because that version ships with Koffi 2.11.0.
53
+
54
+ ## Related
55
+
56
+ - The discussion at https://github.com/Koromix/koffi/issues/201 explains why I decided to create `koffi-cream` myself.
57
+
58
+ ## License
59
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,169 @@
1
+ // Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ // this software and associated documentation files (the “Software”), to deal in
5
+ // the Software without restriction, including without limitation the rights to use,
6
+ // copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ // Software, and to permit persons to whom the Software is furnished to do so,
8
+ // subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in all
11
+ // copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ // OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ declare module 'koffi-cream' {
23
+ export function load(path: string | null): IKoffiLib;
24
+
25
+ interface IKoffiCType { __brand: 'IKoffiCType' }
26
+ interface IKoffiPointerCast { __brand: 'IKoffiPointerCast' }
27
+ interface IKoffiRegisteredCallback { __brand: 'IKoffiRegisteredCallback' }
28
+
29
+ type PrimitiveKind = 'Void' | 'Bool' | 'Int8' | 'UInt8' | 'Int16' | 'Int16S' | 'UInt16' | 'UInt16S' |
30
+ 'Int32' | 'Int32S' | 'UInt32' | 'UInt32S' | 'Int64' | 'Int64S' | 'UInt64' | 'UInt64S' |
31
+ 'String' | 'String16' | 'Pointer' | 'Record' | 'Union' | 'Array' | 'Float32' | 'Float64' |
32
+ 'Prototype' | 'Callback';
33
+ type ArrayHint = 'Array' | 'Typed' | 'String';
34
+
35
+ type TypeSpec = string | IKoffiCType;
36
+ type TypeSpecWithAlignment = TypeSpec | [number, TypeSpec];
37
+ type TypeInfo = {
38
+ name: string;
39
+ primitive: PrimitiveKind;
40
+ size: number;
41
+ alignment: number;
42
+ disposable: boolean;
43
+ length?: number;
44
+ hint?: ArrayHint;
45
+ ref?: IKoffiCType;
46
+ members?: Record<string, { name: string, type: IKoffiCType, offset: number }>;
47
+ };
48
+ type KoffiFunction = {
49
+ (...args: any[]) : any;
50
+ async: (...args: any[]) => any;
51
+ info: {
52
+ name: string,
53
+ arguments: IKoffiCType[],
54
+ result: IKoffiCType
55
+ };
56
+ };
57
+
58
+ export type KoffiFunc<T extends (...args: any) => any> = T & {
59
+ async: (...args: [...Parameters<T>, (err: any, result: ReturnType<T>) => void]) => void;
60
+ info: {
61
+ name: string;
62
+ arguments: IKoffiCType[];
63
+ result: IKoffiCType;
64
+ };
65
+ };
66
+
67
+ export interface IKoffiLib {
68
+ func(definition: string): KoffiFunction;
69
+ func(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
70
+ func(convention: string, name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
71
+
72
+ /** @deprecated */ cdecl(definition: string): KoffiFunction;
73
+ /** @deprecated */ cdecl(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
74
+ /** @deprecated */ stdcall(definition: string): KoffiFunction;
75
+ /** @deprecated */ stdcall(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
76
+ /** @deprecated */ fastcall(definition: string): KoffiFunction;
77
+ /** @deprecated */ fastcall(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
78
+ /** @deprecated */ thiscall(definition: string): KoffiFunction;
79
+ /** @deprecated */ thiscall(name: string, result: TypeSpec, arguments: TypeSpec[]): KoffiFunction;
80
+
81
+ symbol(name: string, type: TypeSpec): any;
82
+
83
+ unload(): void;
84
+ }
85
+
86
+ export function struct(name: string, def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
87
+ export function struct(def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
88
+ export function pack(name: string, def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
89
+ export function pack(def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
90
+
91
+ export function union(name: string, def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
92
+ export function union(def: Record<string, TypeSpecWithAlignment>): IKoffiCType;
93
+
94
+ export class Union {
95
+ constructor(type: TypeSpec);
96
+ [s: string]: any;
97
+ }
98
+
99
+ export function array(ref: TypeSpec, len: number, hint?: ArrayHint | null): IKoffiCType;
100
+
101
+ export function opaque(name: string): IKoffiCType;
102
+ export function opaque(): IKoffiCType;
103
+ /** @deprecated */ export function handle(name: string): IKoffiCType;
104
+ /** @deprecated */ export function handle(): IKoffiCType;
105
+
106
+ export function pointer(ref: TypeSpec): IKoffiCType;
107
+ export function pointer(ref: TypeSpec, asteriskCount?: number): IKoffiCType;
108
+ export function pointer(name: string, ref: TypeSpec, asteriskCount?: number): IKoffiCType;
109
+
110
+ export function out(type: TypeSpec): IKoffiCType;
111
+ export function inout(type: TypeSpec): IKoffiCType;
112
+
113
+ export function disposable(type: TypeSpec): IKoffiCType;
114
+ export function disposable(name: string, type: TypeSpec): IKoffiCType;
115
+ export function disposable(name: string, type: TypeSpec, freeFunction: Function): IKoffiCType;
116
+
117
+ export function proto(definition: string): IKoffiCType;
118
+ export function proto(name: string, result: TypeSpec, arguments: TypeSpec[]): IKoffiCType;
119
+ export function proto(convention: string, name: string, result: TypeSpec, arguments: TypeSpec[]): IKoffiCType;
120
+ /** @deprecated */ export function callback(definition: string): IKoffiCType;
121
+ /** @deprecated */ export function callback(name: string, result: TypeSpec, arguments: TypeSpec[]): IKoffiCType;
122
+ /** @deprecated */ export function callback(convention: string, name: string, result: TypeSpec, arguments: TypeSpec[]): IKoffiCType;
123
+
124
+ export function register(callback: Function, type: TypeSpec): IKoffiRegisteredCallback;
125
+ export function register(thisValue: any, callback: Function, type: TypeSpec): IKoffiRegisteredCallback;
126
+ export function unregister(callback: IKoffiRegisteredCallback): void;
127
+
128
+ export function as(value: any, type: TypeSpec): IKoffiPointerCast;
129
+ export function decode(value: any, type: TypeSpec): any;
130
+ export function decode(value: any, type: TypeSpec, len: number): any;
131
+ export function decode(value: any, offset: number, type: TypeSpec): any;
132
+ export function decode(value: any, offset: number, type: TypeSpec, len: number): any;
133
+ export function address(value: any): bigint;
134
+ export function call(value: any, type: TypeSpec, ...args: any[]): any;
135
+ export function encode(ref: any, type: TypeSpec, value: any): void;
136
+ export function encode(ref: any, type: TypeSpec, value: any, len: number): void;
137
+ export function encode(ref: any, offset: number, type: TypeSpec): void;
138
+ export function encode(ref: any, offset: number, type: TypeSpec, value: any): void;
139
+ export function encode(ref: any, offset: number, type: TypeSpec, value: any, len: number): void;
140
+
141
+ export function sizeof(type: TypeSpec): number;
142
+ export function alignof(type: TypeSpec): number;
143
+ export function offsetof(type: TypeSpec): number;
144
+ export function resolve(type: TypeSpec): IKoffiCType;
145
+ export function introspect(type: TypeSpec): TypeInfo;
146
+
147
+ export function alias(name: string, type: TypeSpec): IKoffiCType;
148
+
149
+ export function config(): Record<string, unknown>;
150
+ export function config(cfg: Record<string, unknown>): Record<string, unknown>;
151
+ export function stats(): Record<string, unknown>;
152
+
153
+ export function alloc(type: TypeSpec, length: number): any;
154
+ export function free(value: any): void;
155
+
156
+ export function errno(): number;
157
+ export function errno(value: number): number;
158
+
159
+ export function reset(): void;
160
+
161
+ export const internal: Boolean;
162
+ export const extension: String;
163
+
164
+ export const os: {
165
+ errno: Record<string, number>
166
+ };
167
+
168
+ export const types: Record<string, IKoffiCType>;
169
+ }
package/index.js ADDED
@@ -0,0 +1,51 @@
1
+ // Copyright 2023 Niels Martignène <niels.martignene@protonmail.com>
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ // this software and associated documentation files (the “Software”), to deal in
5
+ // the Software without restriction, including without limitation the rights to use,
6
+ // copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ // Software, and to permit persons to whom the Software is furnished to do so,
8
+ // subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in all
11
+ // copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
14
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
+ // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
+ // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
+ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
+ // OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ // Adapted by Stephan 'Septh' Schreiber
23
+ const { platform, arch } = require('node:process')
24
+ const { deprecate } = require('node:util')
25
+
26
+ let native
27
+ if (platform === 'linux' && arch === 'x64') {
28
+ try {
29
+ native = require(`@septh/koffi-linux-x64-glibc`)
30
+ }
31
+ catch {
32
+ native = require(`@septh/koffi-linux-x64-musl`)
33
+ }
34
+ }
35
+ else native = require(`@septh/koffi-${platform}-${arch}`)
36
+
37
+ module.exports = {
38
+ ...native,
39
+
40
+ // Deprecated functions in Koffi 2.x
41
+ handle: deprecate(native.opaque, "The koffi.handle() function was deprecated in Koffi 2.1, use koffi.opaque() instead", "KOFFI001"),
42
+ callback: deprecate(native.proto, "The koffi.callback() function was deprecated in Koffi 2.4, use koffi.proto() instead", "KOFFI002"),
43
+ load: (...args) => {
44
+ const lib = native.load(...args)
45
+ lib.cdecl = deprecate((...args2) => lib.func("__cdecl", ...args2), "The koffi.cdecl() function was deprecated in Koffi 2.7, use koffi.func(...) instead", "KOFFI003")
46
+ lib.stdcall = deprecate((...args2) => lib.func("__stdcall", ...args2), 'The koffi.stdcall() function was deprecated in Koffi 2.7, use koffi.func("__stdcall", ...) instead', "KOFFI004")
47
+ lib.fastcall = deprecate((...args2) => lib.func("__fastcall", ...args2), 'The koffi.fastcall() function was deprecated in Koffi 2.7, use koffi.func("__fastcall", ...) instead', "KOFFI005")
48
+ lib.thiscall = deprecate((...args2) => lib.func("__thiscall", ...args2), 'The koffi.thiscall() function was deprecated in Koffi 2.7, use koffi.func("__thiscall", ...) instead', "KOFFI006")
49
+ return lib
50
+ }
51
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "koffi-cream",
3
+ "version": "2.11.0",
4
+ "description": "A lighter packaging for Koffi, the fast and simple C FFI (foreign function interface) for Node.js",
5
+ "author": {
6
+ "name": "Stephan 'Septh' Schreiber",
7
+ "email": "septh@sfr.fr",
8
+ "url": "https://github.com/Septh"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "Niels Martignène",
13
+ "email": "niels.martignene@protonmail.com",
14
+ "url": "https://koromix.dev/"
15
+ }
16
+ ],
17
+ "keywords": [
18
+ "foreign",
19
+ "function",
20
+ "interface",
21
+ "ffi",
22
+ "binding",
23
+ "c",
24
+ "napi",
25
+ "koffi"
26
+ ],
27
+ "homepage": "https://github.com/Septh/koffi-cream#readme",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/Septh/koffi-cream.git",
31
+ "directory": "packages/koffi-cream"
32
+ },
33
+ "license": "MIT",
34
+ "engines": {
35
+ "node": "^12.22.0 || ^14.17.0 || ^15.12.0 || >= 16",
36
+ "npm": ">= 10.4.0",
37
+ "pnpm": ">= 7.1.0",
38
+ "yarn": ">= 3.2.0"
39
+ },
40
+ "type": "commonjs",
41
+ "main": "index.js",
42
+ "types": "index.d.ts",
43
+ "optionalDependencies": {
44
+ "@septh/koffi-darwin-arm64": "2.11.0",
45
+ "@septh/koffi-darwin-x64": "2.11.0",
46
+ "@septh/koffi-freebsd-arm64": "2.11.0",
47
+ "@septh/koffi-freebsd-x64": "2.11.0",
48
+ "@septh/koffi-linux-arm64": "2.11.0",
49
+ "@septh/koffi-linux-riscv64": "2.11.0",
50
+ "@septh/koffi-linux-x64-glibc": "2.11.0",
51
+ "@septh/koffi-linux-x64-musl": "2.11.0",
52
+ "@septh/koffi-openbsd-x64": "2.11.0",
53
+ "@septh/koffi-win32-arm64": "2.11.0",
54
+ "@septh/koffi-win32-x64": "2.11.0"
55
+ }
56
+ }