@golar/plugin 0.0.1
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/LICENSE +21 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +201 -0
- package/package.json +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 auvred <https://github.com/auvred>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
type Mapping = {
|
|
3
|
+
sourceOffset: number;
|
|
4
|
+
serviceOffset: number;
|
|
5
|
+
sourceLength: number;
|
|
6
|
+
serviceLength?: number | undefined;
|
|
7
|
+
};
|
|
8
|
+
type IgnoreDirectiveMapping = {
|
|
9
|
+
serviceOffset: number;
|
|
10
|
+
serviceLength: number;
|
|
11
|
+
};
|
|
12
|
+
type ExpectErrorDirectiveMapping = {
|
|
13
|
+
sourceOffset: number;
|
|
14
|
+
serviceOffset: number;
|
|
15
|
+
sourceLength: number;
|
|
16
|
+
serviceLength: number;
|
|
17
|
+
};
|
|
18
|
+
type ScriptKind = 'js' | 'jsx' | 'ts' | 'tsx';
|
|
19
|
+
type ServiceCodeError = {
|
|
20
|
+
message: string;
|
|
21
|
+
start: number;
|
|
22
|
+
end: number;
|
|
23
|
+
};
|
|
24
|
+
type ServiceCode = ({
|
|
25
|
+
serviceText: string;
|
|
26
|
+
scriptKind: ScriptKind; /** @default false */
|
|
27
|
+
declarationFile?: boolean | undefined;
|
|
28
|
+
} & ({
|
|
29
|
+
mappings: Mapping[];
|
|
30
|
+
errors?: never;
|
|
31
|
+
ignoreMappings?: IgnoreDirectiveMapping[] | undefined;
|
|
32
|
+
expectErrorMappings?: ExpectErrorDirectiveMapping[] | undefined;
|
|
33
|
+
})) | {
|
|
34
|
+
mappings?: never;
|
|
35
|
+
errors: ServiceCodeError[];
|
|
36
|
+
};
|
|
37
|
+
type Promisable<T> = T | Promise<T>;
|
|
38
|
+
type CreatePluginOptions = {
|
|
39
|
+
filename: string;
|
|
40
|
+
/**
|
|
41
|
+
* @example ['.vue']
|
|
42
|
+
*/
|
|
43
|
+
extraExtensions?: string[] | undefined;
|
|
44
|
+
createServiceCode: (cwd: string, configFileName: string | null, fileName: string, sourceText: string) => Promisable<ServiceCode>;
|
|
45
|
+
};
|
|
46
|
+
declare function createPlugin(opts: CreatePluginOptions): void;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { CreatePluginOptions, ExpectErrorDirectiveMapping, IgnoreDirectiveMapping, Mapping, Promisable, ScriptKind, ServiceCode, ServiceCodeError, createPlugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import assert from "node:assert";
|
|
4
|
+
import worker_threads from "node:worker_threads";
|
|
5
|
+
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
const HEADER_SIZE = 5;
|
|
8
|
+
const MSG_KIND = {
|
|
9
|
+
CREATE_SERVICE_CODE: 0,
|
|
10
|
+
CREATE_SERVICE_CODE_RESPONSE: 1
|
|
11
|
+
};
|
|
12
|
+
const SERVICE_CODE_PROPERTIES = {
|
|
13
|
+
ERROR: 1,
|
|
14
|
+
SOURCE_MAP: 2,
|
|
15
|
+
DECLARATION_FILE: 4
|
|
16
|
+
};
|
|
17
|
+
const SCRIPT_KIND = {
|
|
18
|
+
js: 0,
|
|
19
|
+
jsx: 1,
|
|
20
|
+
ts: 2,
|
|
21
|
+
tsx: 3
|
|
22
|
+
};
|
|
23
|
+
function createPlugin(opts) {
|
|
24
|
+
if (worker_threads.isMainThread) {
|
|
25
|
+
const workers = new Array(Math.max(Math.min(os.cpus().length / 2, 4), 1)).fill(null).map(() => {
|
|
26
|
+
const w = {
|
|
27
|
+
busy: false,
|
|
28
|
+
worker: new worker_threads.Worker(opts.filename).on("message", () => {
|
|
29
|
+
const task = taskQueue.shift();
|
|
30
|
+
if (task == null) w.busy = false;
|
|
31
|
+
else w.worker.postMessage(task);
|
|
32
|
+
})
|
|
33
|
+
};
|
|
34
|
+
return w;
|
|
35
|
+
});
|
|
36
|
+
const taskQueue = [];
|
|
37
|
+
let recvBuffer = Buffer.allocUnsafe(1024 * 1024);
|
|
38
|
+
let recvBufferLen = 0;
|
|
39
|
+
function ensureRecvBuffer(n) {
|
|
40
|
+
if (recvBuffer.buffer.byteLength < n) {
|
|
41
|
+
const newBuffer = Buffer.allocUnsafe(n);
|
|
42
|
+
recvBuffer.copy(newBuffer);
|
|
43
|
+
recvBuffer = newBuffer;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
{
|
|
47
|
+
const initialization = JSON.stringify({ extraExtensions: opts.extraExtensions ?? [] });
|
|
48
|
+
recvBuffer.writeUint32LE(Buffer.byteLength(initialization));
|
|
49
|
+
process.stdout.write(recvBuffer.subarray(0, 4));
|
|
50
|
+
process.stdout.write(initialization);
|
|
51
|
+
}
|
|
52
|
+
process.stdin.on("data", (data) => {
|
|
53
|
+
assert.ok(data instanceof Buffer, "Data is expected to be buffer");
|
|
54
|
+
ensureRecvBuffer(recvBufferLen + data.byteLength);
|
|
55
|
+
data.copy(recvBuffer, recvBufferLen);
|
|
56
|
+
recvBufferLen += data.byteLength;
|
|
57
|
+
let msgBuffer = recvBuffer.subarray(0, recvBufferLen);
|
|
58
|
+
let readOffset = 0;
|
|
59
|
+
while (true) {
|
|
60
|
+
if (msgBuffer.byteLength < HEADER_SIZE) break;
|
|
61
|
+
const payloadLen = msgBuffer.readUInt32LE(1);
|
|
62
|
+
if (msgBuffer.byteLength < payloadLen + HEADER_SIZE) break;
|
|
63
|
+
const worker = workers.find(({ busy }) => !busy);
|
|
64
|
+
if (worker != null) {
|
|
65
|
+
worker.busy = true;
|
|
66
|
+
worker.worker.postMessage(msgBuffer.subarray(0, payloadLen + HEADER_SIZE));
|
|
67
|
+
} else taskQueue.push(Buffer.copyBytesFrom(msgBuffer, 0, payloadLen + HEADER_SIZE));
|
|
68
|
+
readOffset += payloadLen + HEADER_SIZE;
|
|
69
|
+
msgBuffer = msgBuffer.subarray(payloadLen + HEADER_SIZE);
|
|
70
|
+
}
|
|
71
|
+
if (readOffset > 0) {
|
|
72
|
+
recvBufferLen -= readOffset;
|
|
73
|
+
recvBuffer.copyWithin(0, readOffset);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
const { parentPort } = worker_threads;
|
|
78
|
+
assert.ok(parentPort);
|
|
79
|
+
let sendBuffer = Buffer.allocUnsafe(1024 * 1024);
|
|
80
|
+
function prepareSendBuffer(msgKind, payloadLen) {
|
|
81
|
+
if (sendBuffer.buffer.byteLength < HEADER_SIZE + payloadLen) sendBuffer = Buffer.allocUnsafe(HEADER_SIZE + payloadLen);
|
|
82
|
+
sendBuffer.writeUInt8(msgKind);
|
|
83
|
+
sendBuffer.writeUInt32LE(payloadLen, 1);
|
|
84
|
+
return sendBuffer.subarray(0, HEADER_SIZE + payloadLen);
|
|
85
|
+
}
|
|
86
|
+
parentPort.on("message", async function(data) {
|
|
87
|
+
const buf = Buffer.from(data);
|
|
88
|
+
switch (buf.readUInt8(0)) {
|
|
89
|
+
case MSG_KIND.CREATE_SERVICE_CODE: {
|
|
90
|
+
let offset = HEADER_SIZE;
|
|
91
|
+
const reqId = buf.readBigUInt64LE(offset);
|
|
92
|
+
const cwdLen = buf.readUInt32LE(offset += 8);
|
|
93
|
+
const cwd = buf.subarray(offset += 4, offset + cwdLen).toString("utf8");
|
|
94
|
+
offset += cwdLen;
|
|
95
|
+
const configFileNameLen = buf.readUInt32LE(offset);
|
|
96
|
+
const configFileName = buf.subarray(offset += 4, offset + configFileNameLen).toString("utf8");
|
|
97
|
+
offset += configFileNameLen;
|
|
98
|
+
const fileNameLen = buf.readUInt32LE(offset);
|
|
99
|
+
const fileName = buf.subarray(offset += 4, offset + fileNameLen).toString("utf8");
|
|
100
|
+
offset += fileNameLen;
|
|
101
|
+
const sourceTextLen = buf.readUInt32LE(offset);
|
|
102
|
+
const sourceText = buf.subarray(offset += 4, offset + sourceTextLen).toString("utf8");
|
|
103
|
+
offset += sourceTextLen;
|
|
104
|
+
const resp = await opts.createServiceCode(cwd, configFileName === "/dev/null/inferred" ? null : configFileName, fileName, sourceText);
|
|
105
|
+
let properties = 0;
|
|
106
|
+
if ("errors" in resp) {
|
|
107
|
+
const errorLocationsUtf8 = mapIndicesToUtf8(sourceText, resp.errors[Symbol.iterator]().flatMap((e) => [e.start, e.end]));
|
|
108
|
+
const errorsLen = resp.errors.reduce((sum, e) => sum + 4 + Buffer.byteLength(e.message) + 4 + 4, 0);
|
|
109
|
+
const sendBuffer = prepareSendBuffer(MSG_KIND.CREATE_SERVICE_CODE_RESPONSE, 13 + errorsLen);
|
|
110
|
+
offset = HEADER_SIZE;
|
|
111
|
+
offset = sendBuffer.writeBigUInt64LE(reqId, offset);
|
|
112
|
+
offset = sendBuffer.writeUInt8(properties | SERVICE_CODE_PROPERTIES.ERROR, offset);
|
|
113
|
+
offset = sendBuffer.writeUInt32LE(resp.errors.length, offset);
|
|
114
|
+
for (const err of resp.errors) {
|
|
115
|
+
offset = sendBuffer.writeUInt32LE(Buffer.byteLength(err.message), offset);
|
|
116
|
+
offset += sendBuffer.write(err.message, offset);
|
|
117
|
+
offset = sendBuffer.writeUInt32LE(errorLocationsUtf8(err.start), offset);
|
|
118
|
+
offset = sendBuffer.writeUInt32LE(errorLocationsUtf8(err.end), offset);
|
|
119
|
+
}
|
|
120
|
+
process.stdout.write(Buffer.copyBytesFrom(sendBuffer));
|
|
121
|
+
parentPort.postMessage(null);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (resp.declarationFile) properties |= SERVICE_CODE_PROPERTIES.DECLARATION_FILE;
|
|
125
|
+
const serviceTextLen = Buffer.byteLength(resp.serviceText);
|
|
126
|
+
const mappingsLen = resp.mappings.length * 16;
|
|
127
|
+
const ignoreMappingsLen = (resp.ignoreMappings?.length ?? 0) * 8;
|
|
128
|
+
const expectErrorMappingsLen = (resp.expectErrorMappings?.length ?? 0) * 16;
|
|
129
|
+
const sendBuffer = prepareSendBuffer(MSG_KIND.CREATE_SERVICE_CODE_RESPONSE, 14 + serviceTextLen + 4 + mappingsLen + 4 + ignoreMappingsLen + 4 + expectErrorMappingsLen);
|
|
130
|
+
const writeUint32 = (os.endianness() === "LE" ? sendBuffer.writeUint32LE : sendBuffer.writeUint32BE).bind(sendBuffer);
|
|
131
|
+
offset = HEADER_SIZE;
|
|
132
|
+
offset = sendBuffer.writeBigUInt64LE(reqId, offset);
|
|
133
|
+
offset = sendBuffer.writeUInt8(properties, offset);
|
|
134
|
+
offset = sendBuffer.writeUInt8(SCRIPT_KIND[resp.scriptKind], offset);
|
|
135
|
+
offset = sendBuffer.writeUInt32LE(serviceTextLen, offset);
|
|
136
|
+
offset += sendBuffer.write(resp.serviceText, offset);
|
|
137
|
+
const sourceIndicesUtf8 = mapIndicesToUtf8(sourceText, function* () {
|
|
138
|
+
for (const m of resp.mappings) {
|
|
139
|
+
yield m.sourceOffset;
|
|
140
|
+
yield m.sourceOffset + m.sourceLength;
|
|
141
|
+
}
|
|
142
|
+
for (const m of resp.expectErrorMappings ?? []) {
|
|
143
|
+
yield m.sourceOffset;
|
|
144
|
+
yield m.sourceOffset + m.sourceLength;
|
|
145
|
+
}
|
|
146
|
+
}());
|
|
147
|
+
const serviceIndicesUtf8 = mapIndicesToUtf8(resp.serviceText, function* () {
|
|
148
|
+
for (const m of resp.mappings) {
|
|
149
|
+
yield m.serviceOffset;
|
|
150
|
+
yield m.serviceOffset + (m.serviceLength ?? m.sourceLength);
|
|
151
|
+
}
|
|
152
|
+
for (const m of resp.ignoreMappings ?? []) {
|
|
153
|
+
yield m.serviceOffset;
|
|
154
|
+
yield m.serviceOffset + m.serviceLength;
|
|
155
|
+
}
|
|
156
|
+
for (const m of resp.expectErrorMappings ?? []) {
|
|
157
|
+
yield m.serviceOffset;
|
|
158
|
+
yield m.serviceOffset + m.serviceLength;
|
|
159
|
+
}
|
|
160
|
+
}());
|
|
161
|
+
offset = sendBuffer.writeUInt32LE(resp.mappings.length, offset);
|
|
162
|
+
for (const m of resp.mappings) {
|
|
163
|
+
const sourceOffsetUtf8 = sourceIndicesUtf8(m.sourceOffset);
|
|
164
|
+
const serviceOffsetUtf8 = serviceIndicesUtf8(m.serviceOffset);
|
|
165
|
+
offset = writeUint32(sourceOffsetUtf8, offset);
|
|
166
|
+
offset = writeUint32(serviceOffsetUtf8, offset);
|
|
167
|
+
offset = writeUint32(sourceIndicesUtf8(m.sourceOffset + m.sourceLength) - sourceOffsetUtf8, offset);
|
|
168
|
+
offset = writeUint32(serviceIndicesUtf8(m.serviceOffset + (m.serviceLength ?? m.sourceLength)) - serviceOffsetUtf8, offset);
|
|
169
|
+
}
|
|
170
|
+
offset = sendBuffer.writeUInt32LE(resp.ignoreMappings?.length ?? 0, offset);
|
|
171
|
+
for (const m of resp.ignoreMappings ?? []) {
|
|
172
|
+
const serviceOffsetUtf8 = serviceIndicesUtf8(m.serviceOffset);
|
|
173
|
+
offset = writeUint32(serviceOffsetUtf8, offset);
|
|
174
|
+
offset = writeUint32(serviceIndicesUtf8(m.serviceOffset + m.serviceLength) - serviceOffsetUtf8, offset);
|
|
175
|
+
}
|
|
176
|
+
offset = sendBuffer.writeUInt32LE(resp.expectErrorMappings?.length ?? 0, offset);
|
|
177
|
+
for (const m of resp.expectErrorMappings ?? []) {
|
|
178
|
+
const sourceOffsetUtf8 = sourceIndicesUtf8(m.sourceOffset);
|
|
179
|
+
const serviceOffsetUtf8 = serviceIndicesUtf8(m.serviceOffset);
|
|
180
|
+
offset = writeUint32(sourceOffsetUtf8, offset);
|
|
181
|
+
offset = writeUint32(serviceOffsetUtf8, offset);
|
|
182
|
+
offset = writeUint32(sourceIndicesUtf8(m.sourceOffset + m.sourceLength) - sourceOffsetUtf8, offset);
|
|
183
|
+
offset = writeUint32(serviceIndicesUtf8(m.serviceOffset + m.serviceLength) - serviceOffsetUtf8, offset);
|
|
184
|
+
}
|
|
185
|
+
process.stdout.write(Buffer.copyBytesFrom(sendBuffer));
|
|
186
|
+
parentPort.postMessage(null);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function mapIndicesToUtf8(text, indices) {
|
|
193
|
+
const mapped = /* @__PURE__ */ new Map();
|
|
194
|
+
const sorted = Array.from(new Set(indices)).sort((a, b) => a - b);
|
|
195
|
+
let utf8Offset = 0;
|
|
196
|
+
for (const [i, idx] of sorted.entries()) mapped.set(idx, utf8Offset += Buffer.byteLength(text.slice(sorted[i - 1] ?? 0, idx)));
|
|
197
|
+
return mapped.get.bind(mapped);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//#endregion
|
|
201
|
+
export { createPlugin };
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@golar/plugin",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./dist/index.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"./dist"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "auvred <aauvred@gmail.com> (https://github.com/auvred)",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/auvred/golar.git",
|
|
16
|
+
"directory": "packages/plugin"
|
|
17
|
+
},
|
|
18
|
+
"bugs": "https://github.com/auvred/golar/issues",
|
|
19
|
+
"homepage": "https://github.com/auvred/golar/tree/main/packages/plugin#readme",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
}
|
|
23
|
+
}
|