knitting 0.1.46
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 +202 -0
- package/README.md +632 -0
- package/knitting.d.ts +4 -0
- package/knitting.js +5 -0
- package/map.md +264 -0
- package/package.json +77 -0
- package/prebuilds/darwin-arm64-node-127/knitting_shared_memory.node +0 -0
- package/prebuilds/darwin-arm64-node-127/knitting_shm.node +0 -0
- package/prebuilds/darwin-arm64-node-137/knitting_shared_memory.node +0 -0
- package/prebuilds/darwin-arm64-node-137/knitting_shm.node +0 -0
- package/prebuilds/darwin-x64-node-127/knitting_shared_memory.node +0 -0
- package/prebuilds/darwin-x64-node-127/knitting_shm.node +0 -0
- package/prebuilds/darwin-x64-node-137/knitting_shared_memory.node +0 -0
- package/prebuilds/darwin-x64-node-137/knitting_shm.node +0 -0
- package/prebuilds/linux-x64-node-127/knitting_shared_memory.node +0 -0
- package/prebuilds/linux-x64-node-127/knitting_shm.node +0 -0
- package/prebuilds/linux-x64-node-137/knitting_shared_memory.node +0 -0
- package/prebuilds/linux-x64-node-137/knitting_shm.node +0 -0
- package/process-shared-buffer.d.ts +1 -0
- package/process-shared-buffer.js +1 -0
- package/scripts/build-native-addons.ts +295 -0
- package/src/api.d.ts +55 -0
- package/src/api.js +384 -0
- package/src/common/envelope.d.ts +11 -0
- package/src/common/envelope.js +8 -0
- package/src/common/module-url.d.ts +1 -0
- package/src/common/module-url.js +24 -0
- package/src/common/node-compat.d.ts +20 -0
- package/src/common/node-compat.js +24 -0
- package/src/common/path-canonical.d.ts +6 -0
- package/src/common/path-canonical.js +41 -0
- package/src/common/runtime.d.ts +15 -0
- package/src/common/runtime.js +91 -0
- package/src/common/shared-buffer-region.d.ts +11 -0
- package/src/common/shared-buffer-region.js +21 -0
- package/src/common/shared-buffer-text.d.ts +16 -0
- package/src/common/shared-buffer-text.js +65 -0
- package/src/common/task-source.d.ts +2 -0
- package/src/common/task-source.js +79 -0
- package/src/common/task-symbol.d.ts +1 -0
- package/src/common/task-symbol.js +1 -0
- package/src/common/with-resolvers.d.ts +9 -0
- package/src/common/with-resolvers.js +23 -0
- package/src/common/worker-runtime.d.ts +40 -0
- package/src/common/worker-runtime.js +52 -0
- package/src/connections/bun.d.ts +20 -0
- package/src/connections/bun.js +159 -0
- package/src/connections/deno.d.ts +20 -0
- package/src/connections/deno.js +150 -0
- package/src/connections/file-descriptor.d.ts +37 -0
- package/src/connections/file-descriptor.js +139 -0
- package/src/connections/index.d.ts +3 -0
- package/src/connections/index.js +3 -0
- package/src/connections/node-addons.d.ts +5 -0
- package/src/connections/node-addons.js +43 -0
- package/src/connections/node.d.ts +29 -0
- package/src/connections/node.js +59 -0
- package/src/connections/posix.d.ts +31 -0
- package/src/connections/posix.js +71 -0
- package/src/connections/process-shared-buffer.d.ts +67 -0
- package/src/connections/process-shared-buffer.js +267 -0
- package/src/connections/types.d.ts +48 -0
- package/src/connections/types.js +37 -0
- package/src/error.d.ts +13 -0
- package/src/error.js +49 -0
- package/src/ipc/tools/ring-queue.d.ts +33 -0
- package/src/ipc/tools/ring-queue.js +159 -0
- package/src/ipc/transport/shared-memory.d.ts +25 -0
- package/src/ipc/transport/shared-memory.js +35 -0
- package/src/knitting_shared_memory.cc +436 -0
- package/src/knitting_shm.cc +476 -0
- package/src/memory/byte-carpet.d.ts +73 -0
- package/src/memory/byte-carpet.js +157 -0
- package/src/memory/lock.d.ts +190 -0
- package/src/memory/lock.js +856 -0
- package/src/memory/payload-config.d.ts +22 -0
- package/src/memory/payload-config.js +67 -0
- package/src/memory/payloadCodec.d.ts +46 -0
- package/src/memory/payloadCodec.js +1157 -0
- package/src/memory/regionRegistry.d.ts +17 -0
- package/src/memory/regionRegistry.js +285 -0
- package/src/memory/shared-buffer-io.d.ts +53 -0
- package/src/memory/shared-buffer-io.js +380 -0
- package/src/permission/index.d.ts +2 -0
- package/src/permission/index.js +2 -0
- package/src/permission/protocol.d.ts +166 -0
- package/src/permission/protocol.js +640 -0
- package/src/runtime/balancer.d.ts +19 -0
- package/src/runtime/balancer.js +149 -0
- package/src/runtime/dispatcher.d.ts +34 -0
- package/src/runtime/dispatcher.js +142 -0
- package/src/runtime/inline-executor.d.ts +10 -0
- package/src/runtime/inline-executor.js +270 -0
- package/src/runtime/pool.d.ts +43 -0
- package/src/runtime/pool.js +922 -0
- package/src/runtime/tx-queue.d.ts +25 -0
- package/src/runtime/tx-queue.js +144 -0
- package/src/shared/abortSignal.d.ts +23 -0
- package/src/shared/abortSignal.js +126 -0
- package/src/types.d.ts +283 -0
- package/src/types.js +2 -0
- package/src/worker/composable-runners.d.ts +12 -0
- package/src/worker/composable-runners.js +105 -0
- package/src/worker/loop.d.ts +2 -0
- package/src/worker/loop.js +453 -0
- package/src/worker/rx-queue.d.ts +22 -0
- package/src/worker/rx-queue.js +124 -0
- package/src/worker/safety/index.d.ts +4 -0
- package/src/worker/safety/index.js +4 -0
- package/src/worker/safety/performance.d.ts +1 -0
- package/src/worker/safety/performance.js +17 -0
- package/src/worker/safety/process.d.ts +2 -0
- package/src/worker/safety/process.js +79 -0
- package/src/worker/safety/startup.d.ts +16 -0
- package/src/worker/safety/startup.js +30 -0
- package/src/worker/safety/worker-data.d.ts +2 -0
- package/src/worker/safety/worker-data.js +36 -0
- package/src/worker/task-loader.d.ts +26 -0
- package/src/worker/task-loader.js +66 -0
- package/src/worker/timers.d.ts +18 -0
- package/src/worker/timers.js +97 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { HEADER_SLOT_STRIDE_U32, HEADER_STATIC_PAYLOAD_U32, LockBound, } from "./lock.js";
|
|
2
|
+
import { getStridedSlotByteOffset } from "./byte-carpet.js";
|
|
3
|
+
import { IS_BUN, createSharedArrayBuffer, growSharedArrayBuffer, } from "../common/runtime.js";
|
|
4
|
+
import { resolvePayloadBufferOptions, } from "./payload-config.js";
|
|
5
|
+
import { isSharedBuffer, toSharedBufferRegion, } from "../common/shared-buffer-region.js";
|
|
6
|
+
const page = 1024 * 4;
|
|
7
|
+
const textEncode = new TextEncoder();
|
|
8
|
+
const textDecode = new TextDecoder();
|
|
9
|
+
const DYNAMIC_HEADER_BYTES = 64;
|
|
10
|
+
const DYNAMIC_SAFE_PADDING_BYTES = page;
|
|
11
|
+
const alignUpto64 = (n) => (n + (64 - 1)) & ~(64 - 1);
|
|
12
|
+
const isExactUint8Array = (src) => src.constructor === Uint8Array;
|
|
13
|
+
const canonicalDynamicUint8Array = (src) => isExactUint8Array(src)
|
|
14
|
+
? src
|
|
15
|
+
: new Uint8Array(src.buffer, src.byteOffset, src.byteLength);
|
|
16
|
+
const isSharedBufferEncodeIntoError = (error) => error instanceof TypeError;
|
|
17
|
+
const isSharedBufferDecodeError = (error) => error instanceof TypeError;
|
|
18
|
+
const getBufferCtor = () => {
|
|
19
|
+
const bufferCtor = globalThis.Buffer;
|
|
20
|
+
if (typeof bufferCtor?.from !== "function" ||
|
|
21
|
+
typeof bufferCtor?.allocUnsafe !== "function" ||
|
|
22
|
+
typeof bufferCtor?.allocUnsafeSlow !== "function") {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
return bufferCtor;
|
|
26
|
+
};
|
|
27
|
+
const manualEncodeInto = (str, target) => {
|
|
28
|
+
let read = 0;
|
|
29
|
+
let written = 0;
|
|
30
|
+
for (const char of str) {
|
|
31
|
+
const encoded = textEncode.encode(char);
|
|
32
|
+
if (written + encoded.byteLength > target.byteLength)
|
|
33
|
+
break;
|
|
34
|
+
target.set(encoded, written);
|
|
35
|
+
written += encoded.byteLength;
|
|
36
|
+
read += char.length;
|
|
37
|
+
}
|
|
38
|
+
return { read, written };
|
|
39
|
+
};
|
|
40
|
+
const fallbackEncodeInto = (str, target) => {
|
|
41
|
+
const scratch = new Uint8Array(target.byteLength);
|
|
42
|
+
const result = typeof textEncode.encodeInto === "function"
|
|
43
|
+
? textEncode.encodeInto(str, scratch)
|
|
44
|
+
: manualEncodeInto(str, scratch);
|
|
45
|
+
if (result.written > 0) {
|
|
46
|
+
target.set(scratch.subarray(0, result.written), 0);
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
const fallbackDecode = (bytes) => textDecode.decode(bytes.slice());
|
|
51
|
+
const sharedBufferEncodeInto = (str, target, textCompat) => {
|
|
52
|
+
if (typeof textEncode.encodeInto !== "function") {
|
|
53
|
+
return fallbackEncodeInto(str, target);
|
|
54
|
+
}
|
|
55
|
+
if (textCompat?.encodeInto === true) {
|
|
56
|
+
return textEncode.encodeInto(str, target);
|
|
57
|
+
}
|
|
58
|
+
if (textCompat?.encodeInto === false)
|
|
59
|
+
return fallbackEncodeInto(str, target);
|
|
60
|
+
try {
|
|
61
|
+
return textEncode.encodeInto(str, target);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (!isSharedBufferEncodeIntoError(error))
|
|
65
|
+
throw error;
|
|
66
|
+
return fallbackEncodeInto(str, target);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
const sharedBufferDecode = (bytes, textCompat) => {
|
|
70
|
+
if (textCompat?.decode === true)
|
|
71
|
+
return textDecode.decode(bytes);
|
|
72
|
+
if (textCompat?.decode === false)
|
|
73
|
+
return fallbackDecode(bytes);
|
|
74
|
+
try {
|
|
75
|
+
return textDecode.decode(bytes);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (!isSharedBufferDecodeError(error))
|
|
79
|
+
throw error;
|
|
80
|
+
return fallbackDecode(bytes);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
export const createSharedDynamicBufferIO = ({ sab, payloadConfig, textCompat, }) => {
|
|
84
|
+
const payloadRegion = sab === undefined
|
|
85
|
+
? undefined
|
|
86
|
+
: toSharedBufferRegion(sab);
|
|
87
|
+
const hasExplicitRegion = sab !== undefined && !isSharedBuffer(sab);
|
|
88
|
+
const hasExternalArrayBuffer = payloadRegion?.sab instanceof ArrayBuffer &&
|
|
89
|
+
!(payloadRegion.sab instanceof SharedArrayBuffer);
|
|
90
|
+
const forceFixedRegion = hasExplicitRegion || hasExternalArrayBuffer;
|
|
91
|
+
const regionByteLength = payloadRegion?.byteLength;
|
|
92
|
+
const bufferCtor = (IS_BUN &&
|
|
93
|
+
payloadRegion?.sab instanceof ArrayBuffer &&
|
|
94
|
+
!(payloadRegion.sab instanceof SharedArrayBuffer))
|
|
95
|
+
? undefined
|
|
96
|
+
: getBufferCtor();
|
|
97
|
+
const resolvedPayload = resolvePayloadBufferOptions({
|
|
98
|
+
sab: payloadRegion?.sab,
|
|
99
|
+
options: !forceFixedRegion || regionByteLength === undefined
|
|
100
|
+
? payloadConfig
|
|
101
|
+
: {
|
|
102
|
+
...payloadConfig,
|
|
103
|
+
mode: "fixed",
|
|
104
|
+
payloadInitialBytes: payloadConfig?.payloadInitialBytes ??
|
|
105
|
+
regionByteLength,
|
|
106
|
+
payloadMaxByteLength: payloadConfig?.payloadMaxByteLength ??
|
|
107
|
+
regionByteLength,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
const canGrow = resolvedPayload.mode === "growable";
|
|
111
|
+
let lockSAB = payloadRegion?.sab ??
|
|
112
|
+
(canGrow
|
|
113
|
+
? createSharedArrayBuffer(resolvedPayload.payloadInitialBytes, resolvedPayload.payloadMaxByteLength)
|
|
114
|
+
: createSharedArrayBuffer(resolvedPayload.payloadInitialBytes));
|
|
115
|
+
let baseByteOffset = payloadRegion?.byteOffset ?? 0;
|
|
116
|
+
let backingByteLength = payloadRegion?.byteLength ?? lockSAB.byteLength;
|
|
117
|
+
let u8 = new Uint8Array(lockSAB, baseByteOffset + DYNAMIC_HEADER_BYTES, Math.max(0, backingByteLength - DYNAMIC_HEADER_BYTES));
|
|
118
|
+
const requireBufferView = bufferCtor
|
|
119
|
+
? (buffer, byteOffset) => {
|
|
120
|
+
const view = bufferCtor.from(buffer, byteOffset + DYNAMIC_HEADER_BYTES);
|
|
121
|
+
if (view.buffer !== buffer) {
|
|
122
|
+
throw new Error("Buffer view does not alias shared buffer");
|
|
123
|
+
}
|
|
124
|
+
return view;
|
|
125
|
+
}
|
|
126
|
+
: undefined;
|
|
127
|
+
let buf = requireBufferView?.(lockSAB, baseByteOffset);
|
|
128
|
+
let f64 = new Float64Array(lockSAB, baseByteOffset + DYNAMIC_HEADER_BYTES, Math.max(0, backingByteLength - DYNAMIC_HEADER_BYTES) >>> 3);
|
|
129
|
+
const capacityBytes = () => backingByteLength - DYNAMIC_HEADER_BYTES;
|
|
130
|
+
const ensureCapacity = (neededBytes) => {
|
|
131
|
+
if (capacityBytes() >= neededBytes)
|
|
132
|
+
return true;
|
|
133
|
+
if (!canGrow)
|
|
134
|
+
return false;
|
|
135
|
+
try {
|
|
136
|
+
if (!(lockSAB instanceof SharedArrayBuffer))
|
|
137
|
+
return false;
|
|
138
|
+
lockSAB = growSharedArrayBuffer(lockSAB, alignUpto64(DYNAMIC_HEADER_BYTES + neededBytes + DYNAMIC_SAFE_PADDING_BYTES));
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
baseByteOffset = 0;
|
|
144
|
+
backingByteLength = lockSAB.byteLength;
|
|
145
|
+
u8 = new Uint8Array(lockSAB, baseByteOffset + DYNAMIC_HEADER_BYTES, backingByteLength - DYNAMIC_HEADER_BYTES);
|
|
146
|
+
buf = requireBufferView?.(lockSAB, baseByteOffset);
|
|
147
|
+
f64 = new Float64Array(lockSAB, baseByteOffset + DYNAMIC_HEADER_BYTES, (backingByteLength - DYNAMIC_HEADER_BYTES) >>> 3);
|
|
148
|
+
return true;
|
|
149
|
+
};
|
|
150
|
+
const readUtf8 = (start, end) => {
|
|
151
|
+
if (!buf) {
|
|
152
|
+
return sharedBufferDecode(u8.subarray(start, end), textCompat);
|
|
153
|
+
}
|
|
154
|
+
return buf.toString("utf8", start, end);
|
|
155
|
+
};
|
|
156
|
+
const writeBinary = (src, start = 0) => {
|
|
157
|
+
const bytes = canonicalDynamicUint8Array(src);
|
|
158
|
+
if (!ensureCapacity(start + bytes.byteLength)) {
|
|
159
|
+
return -1;
|
|
160
|
+
}
|
|
161
|
+
u8.set(bytes, start);
|
|
162
|
+
return bytes.byteLength;
|
|
163
|
+
};
|
|
164
|
+
const writeBuffer = (src, start = 0) => {
|
|
165
|
+
const bytes = src.byteLength;
|
|
166
|
+
if (!ensureCapacity(start + bytes)) {
|
|
167
|
+
return -1;
|
|
168
|
+
}
|
|
169
|
+
u8.set(src, start);
|
|
170
|
+
return bytes;
|
|
171
|
+
};
|
|
172
|
+
const writeArrayBuffer = (src, start = 0) => {
|
|
173
|
+
const bytes = src.byteLength;
|
|
174
|
+
if (!ensureCapacity(start + bytes)) {
|
|
175
|
+
return -1;
|
|
176
|
+
}
|
|
177
|
+
u8.set(new Uint8Array(src), start);
|
|
178
|
+
return bytes;
|
|
179
|
+
};
|
|
180
|
+
const write8Binary = (src, start = 0) => {
|
|
181
|
+
const bytes = src.byteLength;
|
|
182
|
+
if (!ensureCapacity(start + bytes)) {
|
|
183
|
+
return -1;
|
|
184
|
+
}
|
|
185
|
+
f64.set(src, start >>> 3);
|
|
186
|
+
return bytes;
|
|
187
|
+
};
|
|
188
|
+
const readBytesCopy = (start, end) => u8.slice(start, end);
|
|
189
|
+
const readBytesView = (start, end) => u8.subarray(start, end);
|
|
190
|
+
const readBytesBufferCopy = (start, end) => {
|
|
191
|
+
if (!bufferCtor || !buf)
|
|
192
|
+
return readBytesCopy(start, end);
|
|
193
|
+
const length = Math.max(0, (end - start) | 0);
|
|
194
|
+
const out = bufferCtor.allocUnsafe(length);
|
|
195
|
+
if (length === 0)
|
|
196
|
+
return out;
|
|
197
|
+
buf.copy(out, 0, start, end);
|
|
198
|
+
return out;
|
|
199
|
+
};
|
|
200
|
+
const readBytesArrayBufferCopy = (start, end) => {
|
|
201
|
+
if (!bufferCtor || !buf) {
|
|
202
|
+
const out = readBytesCopy(start, end);
|
|
203
|
+
return out.buffer;
|
|
204
|
+
}
|
|
205
|
+
const length = Math.max(0, (end - start) | 0);
|
|
206
|
+
if (length === 0)
|
|
207
|
+
return new ArrayBuffer(0);
|
|
208
|
+
const out = bufferCtor.allocUnsafeSlow(length);
|
|
209
|
+
buf.copy(out, 0, start, end);
|
|
210
|
+
return out.buffer;
|
|
211
|
+
};
|
|
212
|
+
const read8BytesFloatCopy = (start, end) => f64.slice(start >>> 3, end >>> 3);
|
|
213
|
+
const read8BytesFloatView = (start, end) => f64.subarray(start >>> 3, end >>> 3);
|
|
214
|
+
const writeUtf8 = (str, start, reservedBytes = str.length * 3) => {
|
|
215
|
+
if (!ensureCapacity(start + reservedBytes)) {
|
|
216
|
+
return -1;
|
|
217
|
+
}
|
|
218
|
+
const target = u8.subarray(start, start + reservedBytes);
|
|
219
|
+
if (!buf) {
|
|
220
|
+
const { read, written } = sharedBufferEncodeInto(str, target, textCompat);
|
|
221
|
+
if (read !== str.length)
|
|
222
|
+
return -1;
|
|
223
|
+
return written;
|
|
224
|
+
}
|
|
225
|
+
const { read, written } = textEncode.encodeInto(str, target);
|
|
226
|
+
if (read !== str.length)
|
|
227
|
+
return -1;
|
|
228
|
+
return written;
|
|
229
|
+
};
|
|
230
|
+
return {
|
|
231
|
+
readUtf8,
|
|
232
|
+
writeBinary,
|
|
233
|
+
writeBuffer,
|
|
234
|
+
writeArrayBuffer,
|
|
235
|
+
write8Binary,
|
|
236
|
+
readBytesCopy,
|
|
237
|
+
readBytesView,
|
|
238
|
+
readBytesBufferCopy,
|
|
239
|
+
readBufferCopy: readBytesBufferCopy,
|
|
240
|
+
readBytesArrayBufferCopy,
|
|
241
|
+
readArrayBufferCopy: readBytesArrayBufferCopy,
|
|
242
|
+
read8BytesFloatCopy,
|
|
243
|
+
read8BytesFloatView,
|
|
244
|
+
writeUtf8,
|
|
245
|
+
};
|
|
246
|
+
};
|
|
247
|
+
// it has to be convert it to 8
|
|
248
|
+
export const createSharedStaticBufferIO = ({ headersBuffer, slotStrideU32, textCompat, }) => {
|
|
249
|
+
const bufferCtor = getBufferCtor();
|
|
250
|
+
const buffer = headersBuffer instanceof Uint32Array
|
|
251
|
+
? headersBuffer.buffer
|
|
252
|
+
: headersBuffer;
|
|
253
|
+
const baseByteOffset = headersBuffer instanceof Uint32Array
|
|
254
|
+
? headersBuffer.byteOffset
|
|
255
|
+
: 0;
|
|
256
|
+
const u32Bytes = Uint32Array.BYTES_PER_ELEMENT;
|
|
257
|
+
const slotStride = slotStrideU32 ?? HEADER_SLOT_STRIDE_U32;
|
|
258
|
+
const writableBytes = HEADER_STATIC_PAYLOAD_U32 * u32Bytes;
|
|
259
|
+
const baseU8 = new Uint8Array(buffer, baseByteOffset);
|
|
260
|
+
const baseBuf = bufferCtor?.from(buffer, baseByteOffset);
|
|
261
|
+
const baseF64 = new Float64Array(buffer, baseByteOffset, (buffer.byteLength - baseByteOffset) >>> 3);
|
|
262
|
+
const slotStartBytes = (at) => getStridedSlotByteOffset({
|
|
263
|
+
slotIndex: at,
|
|
264
|
+
slotStrideU32: slotStride,
|
|
265
|
+
baseByteOffset,
|
|
266
|
+
baseU32: LockBound.header,
|
|
267
|
+
});
|
|
268
|
+
const slotByteOffsets = new Uint32Array(LockBound.slots);
|
|
269
|
+
for (let i = 0; i < LockBound.slots; i++) {
|
|
270
|
+
slotByteOffsets[i] = slotStartBytes(i) - baseByteOffset;
|
|
271
|
+
}
|
|
272
|
+
const canWrite = (start, length) => (start | 0) >= 0 && (start + length) <= writableBytes;
|
|
273
|
+
const writeUtf8 = (str, at) => {
|
|
274
|
+
const start = slotByteOffsets[at];
|
|
275
|
+
const target = baseU8.subarray(start, start + writableBytes);
|
|
276
|
+
if (!baseBuf) {
|
|
277
|
+
const { read, written } = sharedBufferEncodeInto(str, target, textCompat);
|
|
278
|
+
if (read !== str.length)
|
|
279
|
+
return -1;
|
|
280
|
+
return written;
|
|
281
|
+
}
|
|
282
|
+
const { read, written } = textEncode.encodeInto(str, target);
|
|
283
|
+
if (read !== str.length)
|
|
284
|
+
return -1;
|
|
285
|
+
return written;
|
|
286
|
+
};
|
|
287
|
+
const readUtf8 = (start, end, at) => {
|
|
288
|
+
const slotStart = slotByteOffsets[at];
|
|
289
|
+
if (!baseBuf) {
|
|
290
|
+
return sharedBufferDecode(baseU8.subarray(slotStart + start, slotStart + end), textCompat);
|
|
291
|
+
}
|
|
292
|
+
return baseBuf.toString("utf8", slotStart + start, slotStart + end);
|
|
293
|
+
};
|
|
294
|
+
const writeBinary = (src, at, start = 0) => {
|
|
295
|
+
baseU8.set(src, slotByteOffsets[at] + start);
|
|
296
|
+
return src.byteLength;
|
|
297
|
+
};
|
|
298
|
+
const writeBuffer = (src, at, start = 0) => {
|
|
299
|
+
baseU8.set(src, slotByteOffsets[at] + start);
|
|
300
|
+
return src.byteLength;
|
|
301
|
+
};
|
|
302
|
+
const writeArrayBuffer = (src, at, start = 0) => {
|
|
303
|
+
const bytes = src.byteLength;
|
|
304
|
+
baseU8.set(new Uint8Array(src), slotByteOffsets[at] + start);
|
|
305
|
+
return bytes;
|
|
306
|
+
};
|
|
307
|
+
const writeExactUint8Array = (src, at, start = 0) => {
|
|
308
|
+
baseU8.set(src, slotByteOffsets[at] + start);
|
|
309
|
+
return src.byteLength;
|
|
310
|
+
};
|
|
311
|
+
const writeUint8Array = (src, at, start = 0) => {
|
|
312
|
+
if (!isExactUint8Array(src))
|
|
313
|
+
return -1;
|
|
314
|
+
return writeExactUint8Array(src, at, start);
|
|
315
|
+
};
|
|
316
|
+
const write8Binary = (src, at, start = 0) => {
|
|
317
|
+
const bytes = src.byteLength;
|
|
318
|
+
if (!canWrite(start, bytes))
|
|
319
|
+
return -1;
|
|
320
|
+
baseF64.set(src, (slotByteOffsets[at] + start) >>> 3);
|
|
321
|
+
return bytes;
|
|
322
|
+
};
|
|
323
|
+
const readBytesCopy = (start, end, at) => baseU8.slice(slotByteOffsets[at] + start, slotByteOffsets[at] + end);
|
|
324
|
+
const readBytesView = (start, end, at) => baseU8.subarray(slotByteOffsets[at] + start, slotByteOffsets[at] + end);
|
|
325
|
+
const readBytesBufferCopy = (start, end, at) => {
|
|
326
|
+
if (!bufferCtor || !baseBuf)
|
|
327
|
+
return readBytesCopy(start, end, at);
|
|
328
|
+
const length = end - start;
|
|
329
|
+
const out = bufferCtor.allocUnsafe(length);
|
|
330
|
+
const slotStart = slotByteOffsets[at];
|
|
331
|
+
baseBuf.copy(out, 0, slotStart + start, slotStart + end);
|
|
332
|
+
return out;
|
|
333
|
+
};
|
|
334
|
+
const readUint8ArrayBufferCopy = (start, end, at) => {
|
|
335
|
+
if (!bufferCtor)
|
|
336
|
+
return readBytesCopy(start, end, at);
|
|
337
|
+
const bytes = readBytesBufferCopy(start, end, at);
|
|
338
|
+
return new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
339
|
+
};
|
|
340
|
+
const readUint8ArraySliceCopy = (start, end, at) => readBytesCopy(start, end, at);
|
|
341
|
+
const readUint8ArrayCopy = IS_BUN
|
|
342
|
+
? readUint8ArraySliceCopy
|
|
343
|
+
: readUint8ArrayBufferCopy;
|
|
344
|
+
const readBytesArrayBufferCopy = (start, end, at) => {
|
|
345
|
+
if (!bufferCtor || !baseBuf) {
|
|
346
|
+
const out = readBytesCopy(start, end, at);
|
|
347
|
+
return out.buffer;
|
|
348
|
+
}
|
|
349
|
+
const length = Math.max(0, (end - start) | 0);
|
|
350
|
+
if (length === 0)
|
|
351
|
+
return new ArrayBuffer(0);
|
|
352
|
+
const out = bufferCtor.allocUnsafeSlow(length);
|
|
353
|
+
const slotStart = slotByteOffsets[at];
|
|
354
|
+
baseBuf.copy(out, 0, slotStart + start, slotStart + end);
|
|
355
|
+
return out.buffer;
|
|
356
|
+
};
|
|
357
|
+
const read8BytesFloatCopy = (start, end, at) => baseF64.slice((slotByteOffsets[at] + start) >>> 3, (slotByteOffsets[at] + end) >>> 3);
|
|
358
|
+
const read8BytesFloatView = (start, end, at) => baseF64.subarray((slotByteOffsets[at] + start) >>> 3, (slotByteOffsets[at] + end) >>> 3);
|
|
359
|
+
return {
|
|
360
|
+
writeUtf8,
|
|
361
|
+
readUtf8,
|
|
362
|
+
writeBinary,
|
|
363
|
+
writeBuffer,
|
|
364
|
+
writeArrayBuffer,
|
|
365
|
+
writeExactUint8Array,
|
|
366
|
+
writeUint8Array,
|
|
367
|
+
write8Binary,
|
|
368
|
+
readBytesCopy,
|
|
369
|
+
readBytesView,
|
|
370
|
+
readBytesBufferCopy,
|
|
371
|
+
readBufferCopy: readBytesBufferCopy,
|
|
372
|
+
readUint8ArrayCopy,
|
|
373
|
+
readUint8ArrayBufferCopy,
|
|
374
|
+
readBytesArrayBufferCopy,
|
|
375
|
+
readArrayBufferCopy: readBytesArrayBufferCopy,
|
|
376
|
+
read8BytesFloatCopy,
|
|
377
|
+
read8BytesFloatView,
|
|
378
|
+
maxBytes: writableBytes,
|
|
379
|
+
};
|
|
380
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { resolvePermissionProtocol, toRuntimePermissionFlags, } from "./protocol.js";
|
|
2
|
+
export type { PermissionPath, PermissionMode, PermissionLegacyMode, SysApiName, NodePermissionSettings, DenoPermissionSettings, PermissionEnvironment, PermissionProtocol, PermissionProtocolInput, ResolvedPermissionProtocol, } from "./protocol.js";
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
type PermissionPath = string | URL;
|
|
2
|
+
type NodePermissionSettings = {
|
|
3
|
+
allowWorker?: boolean;
|
|
4
|
+
allowChildProcess?: boolean;
|
|
5
|
+
allowAddons?: boolean;
|
|
6
|
+
allowWasi?: boolean;
|
|
7
|
+
};
|
|
8
|
+
type DenoPermissionSettings = {
|
|
9
|
+
lock?: boolean | PermissionPath;
|
|
10
|
+
frozen?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Legacy compatibility: superseded by top-level `run`.
|
|
13
|
+
*/
|
|
14
|
+
allowRun?: boolean;
|
|
15
|
+
};
|
|
16
|
+
type SysApiName = "hostname" | "osRelease" | "osUptime" | "loadavg" | "networkInterfaces" | "systemMemoryInfo" | "uid" | "gid";
|
|
17
|
+
type PermissionEnvironment = {
|
|
18
|
+
allow?: string[] | true;
|
|
19
|
+
deny?: string[];
|
|
20
|
+
files?: PermissionPath | PermissionPath[];
|
|
21
|
+
};
|
|
22
|
+
type PermissionMode = "strict" | "unsafe" | "custom";
|
|
23
|
+
type PermissionLegacyMode = "off";
|
|
24
|
+
type PermissionProtocol = {
|
|
25
|
+
/**
|
|
26
|
+
* `strict` = hardened defaults, `unsafe` = full access,
|
|
27
|
+
* `custom` = strict baseline with user overrides.
|
|
28
|
+
*/
|
|
29
|
+
mode?: PermissionMode;
|
|
30
|
+
/**
|
|
31
|
+
* Console access for worker task code.
|
|
32
|
+
* Defaults to `false` in strict/custom mode, `true` in unsafe mode.
|
|
33
|
+
*/
|
|
34
|
+
console?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Base directory used to resolve relative paths.
|
|
37
|
+
* Defaults to the current shell working directory.
|
|
38
|
+
*/
|
|
39
|
+
cwd?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Read allow-list. `true` means unrestricted access.
|
|
42
|
+
*/
|
|
43
|
+
read?: PermissionPath[] | true;
|
|
44
|
+
/**
|
|
45
|
+
* Write allow-list. `true` means unrestricted access.
|
|
46
|
+
*/
|
|
47
|
+
write?: PermissionPath[] | true;
|
|
48
|
+
/**
|
|
49
|
+
* Explicit deny-read entries.
|
|
50
|
+
*/
|
|
51
|
+
denyRead?: PermissionPath[];
|
|
52
|
+
/**
|
|
53
|
+
* Explicit deny-write entries.
|
|
54
|
+
*/
|
|
55
|
+
denyWrite?: PermissionPath[];
|
|
56
|
+
/**
|
|
57
|
+
* Network allow-list. `true` means unrestricted access.
|
|
58
|
+
*/
|
|
59
|
+
net?: string[] | true;
|
|
60
|
+
/**
|
|
61
|
+
* Explicit network deny-list.
|
|
62
|
+
*/
|
|
63
|
+
denyNet?: string[];
|
|
64
|
+
/**
|
|
65
|
+
* Allowed import hostnames. `true` means unrestricted import hosts.
|
|
66
|
+
*/
|
|
67
|
+
allowImport?: string[] | true;
|
|
68
|
+
/**
|
|
69
|
+
* Environment permission settings.
|
|
70
|
+
*/
|
|
71
|
+
env?: PermissionEnvironment;
|
|
72
|
+
/**
|
|
73
|
+
* Subprocess allow-list. `true` means unrestricted access.
|
|
74
|
+
*/
|
|
75
|
+
run?: string[] | true;
|
|
76
|
+
/**
|
|
77
|
+
* Explicit subprocess deny-list.
|
|
78
|
+
*/
|
|
79
|
+
denyRun?: string[];
|
|
80
|
+
/**
|
|
81
|
+
* Whether worker spawning is allowed.
|
|
82
|
+
*/
|
|
83
|
+
workers?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* FFI allow-list or toggle.
|
|
86
|
+
*/
|
|
87
|
+
ffi?: PermissionPath[] | boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Explicit FFI deny-list.
|
|
90
|
+
*/
|
|
91
|
+
denyFfi?: PermissionPath[];
|
|
92
|
+
/**
|
|
93
|
+
* System API allow-list. `true` means unrestricted access.
|
|
94
|
+
*/
|
|
95
|
+
sys?: SysApiName[] | true;
|
|
96
|
+
/**
|
|
97
|
+
* Explicit system API deny-list.
|
|
98
|
+
*/
|
|
99
|
+
denySys?: SysApiName[];
|
|
100
|
+
/**
|
|
101
|
+
* Whether WASI is allowed.
|
|
102
|
+
*/
|
|
103
|
+
wasi?: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Backward-compat runtime overrides.
|
|
106
|
+
*/
|
|
107
|
+
node?: NodePermissionSettings;
|
|
108
|
+
deno?: DenoPermissionSettings;
|
|
109
|
+
};
|
|
110
|
+
type PermissionProtocolInput = PermissionMode | PermissionLegacyMode | PermissionProtocol;
|
|
111
|
+
type L3RuntimeKeys = {
|
|
112
|
+
deno: string[];
|
|
113
|
+
node: string[];
|
|
114
|
+
};
|
|
115
|
+
type ResolvedPermissionProtocol = {
|
|
116
|
+
enabled: boolean;
|
|
117
|
+
mode: PermissionMode;
|
|
118
|
+
unsafe: boolean;
|
|
119
|
+
allowConsole: boolean;
|
|
120
|
+
cwd: string;
|
|
121
|
+
read: string[];
|
|
122
|
+
readAll: boolean;
|
|
123
|
+
write: string[];
|
|
124
|
+
writeAll: boolean;
|
|
125
|
+
denyRead: string[];
|
|
126
|
+
denyWrite: string[];
|
|
127
|
+
net: string[];
|
|
128
|
+
netAll: boolean;
|
|
129
|
+
denyNet: string[];
|
|
130
|
+
allowImport: string[];
|
|
131
|
+
allowImportAll: boolean;
|
|
132
|
+
env: {
|
|
133
|
+
allow: string[];
|
|
134
|
+
allowAll: boolean;
|
|
135
|
+
deny: string[];
|
|
136
|
+
files: string[];
|
|
137
|
+
};
|
|
138
|
+
envFiles: string[];
|
|
139
|
+
run: string[];
|
|
140
|
+
runAll: boolean;
|
|
141
|
+
denyRun: string[];
|
|
142
|
+
workers: boolean;
|
|
143
|
+
ffi: string[];
|
|
144
|
+
ffiAll: boolean;
|
|
145
|
+
denyFfi: string[];
|
|
146
|
+
sys: SysApiName[];
|
|
147
|
+
sysAll: boolean;
|
|
148
|
+
denySys: SysApiName[];
|
|
149
|
+
wasi: boolean;
|
|
150
|
+
lockFiles: {
|
|
151
|
+
deno?: string;
|
|
152
|
+
};
|
|
153
|
+
node: Required<NodePermissionSettings> & {
|
|
154
|
+
flags: string[];
|
|
155
|
+
};
|
|
156
|
+
deno: Required<Omit<DenoPermissionSettings, "lock">> & {
|
|
157
|
+
flags: string[];
|
|
158
|
+
};
|
|
159
|
+
l3: L3RuntimeKeys;
|
|
160
|
+
};
|
|
161
|
+
export declare const resolvePermissionProtocol: ({ permission, modules, }: {
|
|
162
|
+
permission?: PermissionProtocolInput;
|
|
163
|
+
modules?: string[];
|
|
164
|
+
}) => ResolvedPermissionProtocol | undefined;
|
|
165
|
+
export declare const toRuntimePermissionFlags: (protocol: ResolvedPermissionProtocol | undefined) => string[];
|
|
166
|
+
export type { PermissionPath, PermissionMode, PermissionLegacyMode, SysApiName, NodePermissionSettings, DenoPermissionSettings, PermissionEnvironment, PermissionProtocol, PermissionProtocolInput, ResolvedPermissionProtocol, };
|