Skip to content

Commit f7acab6

Browse files
authored
Type definitions (#103)
1 parent 56d2092 commit f7acab6

19 files changed

+2397
-2
lines changed
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Publish type definitions to npm
2+
inputs:
3+
VERSION:
4+
description: Packages version
5+
required: true
6+
NODE_AUTH_TOKEN:
7+
description: Node auth token
8+
required: true
9+
runs:
10+
using: composite
11+
steps:
12+
- name: Set variables
13+
id: vars
14+
shell: bash
15+
run: |
16+
echo "PKGS=buffer url" >> "${GITHUB_OUTPUT}"
17+
18+
- name: Set versions
19+
shell: bash
20+
run: |
21+
jq --arg version "${{ inputs.VERSION }}" '.version = $version' global-types/package.json > global-types/tmp.json && mv global-types/tmp.json global-types/package.json
22+
for pkg in ${{ steps.vars.outputs.PKGS }}; do
23+
jq --arg version "${{ inputs.VERSION }}" '.version = $version' $pkg/types/package.json > $pkg/types/tmp.json && mv $pkg/types/tmp.json $pkg/types/package.json
24+
done
25+
- name: Update dependency versions
26+
shell: bash
27+
run: |
28+
for pkg in ${{ steps.vars.outputs.PKGS }}; do
29+
jq --arg version "${{ inputs.VERSION }}" '.dependencies."@dop251/types-goja_nodejs-global" = $version' $pkg/types/package.json > $pkg/types/tmp.json && mv $pkg/types/tmp.json $pkg/types/package.json
30+
done
31+
- name: Setup nodejs
32+
uses: actions/setup-node@v2
33+
with:
34+
node-version: '22'
35+
registry-url: 'https://registry.npmjs.org'
36+
- name: Publish the packages
37+
shell: bash
38+
run: |
39+
cd global-types
40+
npm publish --access=public
41+
cd -
42+
for pkg in ${{ steps.vars.outputs.PKGS }}; do
43+
cd $pkg/types
44+
npm publish --access=public
45+
cd -
46+
done
47+
env:
48+
NODE_AUTH_TOKEN: ${{ inputs.NODE_AUTH_TOKEN }}

.github/workflows/main.yml

+44-1
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,54 @@ jobs:
2626
- name: Run staticcheck
2727
uses: dominikh/staticcheck-action@v1.3.1
2828
with:
29-
version: "2024.1.1"
29+
version: "2025.1.1"
3030
install-go: false
3131
cache-key: ${{ matrix.go-version }}
3232
if: ${{ matrix.go-version == '1.x' }}
3333
- name: Run tests
3434
env:
3535
GOARCH: ${{ matrix.arch }}
3636
run: go test -vet=off ./...
37+
38+
test-types:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- uses: actions/checkout@v4
42+
- run: |
43+
npm ci
44+
npm test --workspaces
45+
46+
publish-types-tagged:
47+
name: 'Publish type definitions to npm (tagged)'
48+
needs: [test, test-types]
49+
runs-on: ubuntu-latest
50+
if: startsWith(github.ref, 'refs/tags/v')
51+
steps:
52+
- uses: actions/checkout@v4
53+
- name: Extract version
54+
run: |
55+
VERSION=$(echo "${{ github.ref }}" | sed 's|refs/tags/v||')
56+
if [[ "$VERSION" == [0-9].* ]]; then
57+
echo "version=$VERSION" >> $GITHUB_ENV
58+
fi
59+
- uses: './.github/actions/publish-types'
60+
if: env.version != ''
61+
with:
62+
VERSION: ${{ env.version }}
63+
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
64+
65+
publish-types-untagged:
66+
name: 'Publish type definitions to npm (untagged master)'
67+
needs: [ test, test-types ]
68+
runs-on: ubuntu-latest
69+
if: "!startsWith(github.ref, 'refs/tags/v') && github.ref_name == 'master'"
70+
steps:
71+
- uses: actions/checkout@v4
72+
- name: Extract version
73+
run: |
74+
VERSION=0.0.0-$(git log -1 --format=%cd --date=format:%Y%m%d%H%M%S)-$(git rev-parse --short=12 HEAD)
75+
echo "version=$VERSION" >> $GITHUB_ENV
76+
- uses: './.github/actions/publish-types'
77+
with:
78+
VERSION: ${{ env.version }}
79+
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
*.iml
33
vendor/*
4+
node_modules

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Nodejs compatibility library for Goja
22
====
33

4-
This is a collection of Goja modules that provide nodejs compatibility.
4+
This is a collection of [Goja](https://github.com/dop251/goja) modules that provide nodejs compatibility.
55

66
Example:
77

@@ -29,4 +29,18 @@ func main() {
2929
}
3030
```
3131

32+
Type Definitions
33+
---
34+
35+
Type definitions are published to https://npmjs.com as @dop251/types-goja_nodejs-MODULE.
36+
They only include what's been implemented so far.
37+
38+
To make use of them you need to install the appropriate modules and add `node_modules/@dop251` to `typeRoots` in `tsconfig.json`.
39+
40+
I didn't want to add those to DefinitelyTyped partly because I don't think they really belong there,
41+
and partly because I'd like to fully control the release cycle, i.e. publish the modules by an automated CI job and
42+
exactly at the same time as the Go code is released.
43+
44+
And the reason for splitting them into different packages is that the modules can be enabled or disabled individually, unlike in nodejs.
45+
3246
More modules will be added. Contributions welcome too.

buffer/types/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Type definitions for the goja_nodejs buffer module.
2+
3+
This package contains type definitions which only include features
4+
currently implemented by the goja_nodejs buffer module.
5+
6+
### Install
7+
8+
```shell
9+
npm install --save-dev @dop251/types-goja_nodejs-buffer
10+
```

buffer/types/buffer.buffer.d.ts

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
declare module "buffer" {
2+
type ImplicitArrayBuffer<T extends WithImplicitCoercion<ArrayBufferLike>> = T extends
3+
{ valueOf(): infer V extends ArrayBufferLike } ? V : T;
4+
global {
5+
interface BufferConstructor {
6+
// see buffer.d.ts for implementation shared with all TypeScript versions
7+
8+
/**
9+
* Allocates a new buffer containing the given {str}.
10+
*
11+
* @param str String to store in buffer.
12+
* @param encoding encoding to use, optional. Default is 'utf8'
13+
* @deprecated since v10.0.0 - Use `Buffer.from(string[, encoding])` instead.
14+
*/
15+
new(str: string, encoding?: BufferEncoding): Buffer<ArrayBuffer>;
16+
/**
17+
* Allocates a new buffer containing the given {array} of octets.
18+
*
19+
* @param array The octets to store.
20+
* @deprecated since v10.0.0 - Use `Buffer.from(array)` instead.
21+
*/
22+
new(array: ArrayLike<number>): Buffer<ArrayBuffer>;
23+
/**
24+
* Produces a Buffer backed by the same allocated memory as
25+
* the given {ArrayBuffer}/{SharedArrayBuffer}.
26+
*
27+
* @param arrayBuffer The ArrayBuffer with which to share memory.
28+
* @deprecated since v10.0.0 - Use `Buffer.from(arrayBuffer[, byteOffset[, length]])` instead.
29+
*/
30+
new<TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(arrayBuffer: TArrayBuffer): Buffer<TArrayBuffer>;
31+
/**
32+
* Allocates a new `Buffer` using an `array` of bytes in the range `0` – `255`.
33+
* Array entries outside that range will be truncated to fit into it.
34+
*
35+
* ```js
36+
* import { Buffer } from 'node:buffer';
37+
*
38+
* // Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.
39+
* const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
40+
* ```
41+
*
42+
* If `array` is an `Array`-like object (that is, one with a `length` property of
43+
* type `number`), it is treated as if it is an array, unless it is a `Buffer` or
44+
* a `Uint8Array`. This means all other `TypedArray` variants get treated as an
45+
* `Array`. To create a `Buffer` from the bytes backing a `TypedArray`, use
46+
* `Buffer.copyBytesFrom()`.
47+
*
48+
* A `TypeError` will be thrown if `array` is not an `Array` or another type
49+
* appropriate for `Buffer.from()` variants.
50+
*
51+
* `Buffer.from(array)` and `Buffer.from(string)` may also use the internal
52+
* `Buffer` pool like `Buffer.allocUnsafe()` does.
53+
* @since v5.10.0
54+
*/
55+
from(array: WithImplicitCoercion<ArrayLike<number>>): Buffer<ArrayBuffer>;
56+
/**
57+
* This creates a view of the `ArrayBuffer` without copying the underlying
58+
* memory. For example, when passed a reference to the `.buffer` property of a
59+
* `TypedArray` instance, the newly created `Buffer` will share the same
60+
* allocated memory as the `TypedArray`'s underlying `ArrayBuffer`.
61+
*
62+
* ```js
63+
* import { Buffer } from 'node:buffer';
64+
*
65+
* const arr = new Uint16Array(2);
66+
*
67+
* arr[0] = 5000;
68+
* arr[1] = 4000;
69+
*
70+
* // Shares memory with `arr`.
71+
* const buf = Buffer.from(arr.buffer);
72+
*
73+
* console.log(buf);
74+
* // Prints: <Buffer 88 13 a0 0f>
75+
*
76+
* // Changing the original Uint16Array changes the Buffer also.
77+
* arr[1] = 6000;
78+
*
79+
* console.log(buf);
80+
* // Prints: <Buffer 88 13 70 17>
81+
* ```
82+
*
83+
* The optional `byteOffset` and `length` arguments specify a memory range within
84+
* the `arrayBuffer` that will be shared by the `Buffer`.
85+
*
86+
* ```js
87+
* import { Buffer } from 'node:buffer';
88+
*
89+
* const ab = new ArrayBuffer(10);
90+
* const buf = Buffer.from(ab, 0, 2);
91+
*
92+
* console.log(buf.length);
93+
* // Prints: 2
94+
* ```
95+
*
96+
* A `TypeError` will be thrown if `arrayBuffer` is not an `ArrayBuffer` or a
97+
* `SharedArrayBuffer` or another type appropriate for `Buffer.from()`
98+
* variants.
99+
*
100+
* It is important to remember that a backing `ArrayBuffer` can cover a range
101+
* of memory that extends beyond the bounds of a `TypedArray` view. A new
102+
* `Buffer` created using the `buffer` property of a `TypedArray` may extend
103+
* beyond the range of the `TypedArray`:
104+
*
105+
* ```js
106+
* import { Buffer } from 'node:buffer';
107+
*
108+
* const arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements
109+
* const arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements
110+
* console.log(arrA.buffer === arrB.buffer); // true
111+
*
112+
* const buf = Buffer.from(arrB.buffer);
113+
* console.log(buf);
114+
* // Prints: <Buffer 63 64 65 66>
115+
* ```
116+
* @since v5.10.0
117+
* @param arrayBuffer An `ArrayBuffer`, `SharedArrayBuffer`, for example the
118+
* `.buffer` property of a `TypedArray`.
119+
* @param byteOffset Index of first byte to expose. **Default:** `0`.
120+
* @param length Number of bytes to expose. **Default:**
121+
* `arrayBuffer.byteLength - byteOffset`.
122+
*/
123+
from<TArrayBuffer extends WithImplicitCoercion<ArrayBufferLike>>(
124+
arrayBuffer: TArrayBuffer,
125+
byteOffset?: number,
126+
length?: number,
127+
): Buffer<ImplicitArrayBuffer<TArrayBuffer>>;
128+
/**
129+
* Creates a new `Buffer` containing `string`. The `encoding` parameter identifies
130+
* the character encoding to be used when converting `string` into bytes.
131+
*
132+
* ```js
133+
* import { Buffer } from 'node:buffer';
134+
*
135+
* const buf1 = Buffer.from('this is a tést');
136+
* const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
137+
*
138+
* console.log(buf1.toString());
139+
* // Prints: this is a tést
140+
* console.log(buf2.toString());
141+
* // Prints: this is a tést
142+
* console.log(buf1.toString('latin1'));
143+
* // Prints: this is a tést
144+
* ```
145+
*
146+
* A `TypeError` will be thrown if `string` is not a string or another type
147+
* appropriate for `Buffer.from()` variants.
148+
*
149+
* `Buffer.from(string)` may also use the internal `Buffer` pool like
150+
* `Buffer.allocUnsafe()` does.
151+
* @since v5.10.0
152+
* @param string A string to encode.
153+
* @param encoding The encoding of `string`. **Default:** `'utf8'`.
154+
*/
155+
from(string: WithImplicitCoercion<string>, encoding?: BufferEncoding): Buffer<ArrayBuffer>;
156+
/**
157+
* Allocates a new `Buffer` of `size` bytes. If `fill` is `undefined`, the`Buffer` will be zero-filled.
158+
*
159+
* ```js
160+
* import { Buffer } from 'node:buffer';
161+
*
162+
* const buf = Buffer.alloc(5);
163+
*
164+
* console.log(buf);
165+
* // Prints: <Buffer 00 00 00 00 00>
166+
* ```
167+
*
168+
* If `size` is larger than {@link constants.MAX_LENGTH} or smaller than 0, `ERR_OUT_OF_RANGE` is thrown.
169+
*
170+
* If `fill` is specified, the allocated `Buffer` will be initialized by calling `buf.fill(fill)`.
171+
*
172+
* ```js
173+
* import { Buffer } from 'node:buffer';
174+
*
175+
* const buf = Buffer.alloc(5, 'a');
176+
*
177+
* console.log(buf);
178+
* // Prints: <Buffer 61 61 61 61 61>
179+
* ```
180+
*
181+
* If both `fill` and `encoding` are specified, the allocated `Buffer` will be
182+
* initialized by calling `buf.fill(fill, encoding)`.
183+
*
184+
* ```js
185+
* import { Buffer } from 'node:buffer';
186+
*
187+
* const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
188+
*
189+
* console.log(buf);
190+
* // Prints: <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
191+
* ```
192+
*
193+
* Calling `Buffer.alloc()` can be measurably slower than the alternative `Buffer.allocUnsafe()` but ensures that the newly created `Buffer` instance
194+
* contents will never contain sensitive data from previous allocations, including
195+
* data that might not have been allocated for `Buffer`s.
196+
*
197+
* A `TypeError` will be thrown if `size` is not a number.
198+
* @since v5.10.0
199+
* @param size The desired length of the new `Buffer`.
200+
* @param [fill=0] A value to pre-fill the new `Buffer` with.
201+
* @param [encoding='utf8'] If `fill` is a string, this is its encoding.
202+
*/
203+
alloc(size: number, fill?: string | Uint8Array | number, encoding?: BufferEncoding): Buffer<ArrayBuffer>;
204+
}
205+
interface Buffer<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> extends Uint8Array<TArrayBuffer> {
206+
// see buffer.d.ts for implementation shared with all TypeScript versions
207+
208+
}
209+
}
210+
}

0 commit comments

Comments
 (0)