|
| 1 | +/** |
| 2 | + * Simple Buffer polyfill for browser compatibility |
| 3 | + * This provides the minimal Buffer interface needed by PostcodeClient |
| 4 | + */ |
| 5 | + |
| 6 | +export class BufferPolyfill { |
| 7 | + private data: Uint8Array; |
| 8 | + |
| 9 | + constructor(data: Uint8Array | ArrayBuffer | number[]) { |
| 10 | + if (data instanceof ArrayBuffer) { |
| 11 | + this.data = new Uint8Array(data); |
| 12 | + } else if (Array.isArray(data)) { |
| 13 | + this.data = new Uint8Array(data); |
| 14 | + } else { |
| 15 | + this.data = data; |
| 16 | + } |
| 17 | + |
| 18 | + // Make it behave like an array for buffer[index] access |
| 19 | + return new Proxy(this, { |
| 20 | + get(target, prop) { |
| 21 | + if (typeof prop === "string" && /^\d+$/.test(prop)) { |
| 22 | + const index = parseInt(prop, 10); |
| 23 | + return target.data[index]; |
| 24 | + } |
| 25 | + return (target as any)[prop]; |
| 26 | + }, |
| 27 | + has(target, prop) { |
| 28 | + if (typeof prop === "string" && /^\d+$/.test(prop)) { |
| 29 | + const index = parseInt(prop, 10); |
| 30 | + return index >= 0 && index < target.data.length; |
| 31 | + } |
| 32 | + return prop in target; |
| 33 | + }, |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + static from(data: Uint8Array | ArrayBuffer | number[]): BufferPolyfill { |
| 38 | + return new BufferPolyfill(data); |
| 39 | + } |
| 40 | + |
| 41 | + static alloc(size: number, fill?: number): BufferPolyfill { |
| 42 | + const data = new Uint8Array(size); |
| 43 | + if (fill !== undefined) { |
| 44 | + data.fill(fill); |
| 45 | + } |
| 46 | + return new BufferPolyfill(data); |
| 47 | + } |
| 48 | + |
| 49 | + static concat(buffers: BufferPolyfill[]): BufferPolyfill { |
| 50 | + const totalLength = buffers.reduce((sum, buf) => sum + buf.length, 0); |
| 51 | + const result = new Uint8Array(totalLength); |
| 52 | + let offset = 0; |
| 53 | + |
| 54 | + for (const buffer of buffers) { |
| 55 | + result.set(buffer.data, offset); |
| 56 | + offset += buffer.length; |
| 57 | + } |
| 58 | + |
| 59 | + return new BufferPolyfill(result); |
| 60 | + } |
| 61 | + |
| 62 | + get length(): number { |
| 63 | + return this.data.length; |
| 64 | + } |
| 65 | + |
| 66 | + readUInt8(offset: number): number { |
| 67 | + return this.data[offset] || 0; |
| 68 | + } |
| 69 | + |
| 70 | + readUInt16LE(offset: number): number { |
| 71 | + return (this.data[offset] || 0) | ((this.data[offset + 1] || 0) << 8); |
| 72 | + } |
| 73 | + |
| 74 | + readUInt32LE(offset: number): number { |
| 75 | + return ( |
| 76 | + ((this.data[offset] || 0) | |
| 77 | + ((this.data[offset + 1] || 0) << 8) | |
| 78 | + ((this.data[offset + 2] || 0) << 16) | |
| 79 | + ((this.data[offset + 3] || 0) << 24)) >>> |
| 80 | + 0 |
| 81 | + ); // Convert to unsigned 32-bit |
| 82 | + } |
| 83 | + |
| 84 | + readInt32LE(offset: number): number { |
| 85 | + const value = this.readUInt32LE(offset); |
| 86 | + // Convert unsigned to signed |
| 87 | + return value > 0x7fffffff ? value - 0x100000000 : value; |
| 88 | + } |
| 89 | + |
| 90 | + readUIntLE(offset: number, byteLength: number): number { |
| 91 | + let value = 0; |
| 92 | + for (let i = 0; i < byteLength; i++) { |
| 93 | + value |= (this.data[offset + i] || 0) << (i * 8); |
| 94 | + } |
| 95 | + return value >>> 0; |
| 96 | + } |
| 97 | + |
| 98 | + readIntLE(offset: number, byteLength: number): number { |
| 99 | + const value = this.readUIntLE(offset, byteLength); |
| 100 | + const maxValue = Math.pow(2, byteLength * 8 - 1); |
| 101 | + return value >= maxValue ? value - Math.pow(2, byteLength * 8) : value; |
| 102 | + } |
| 103 | + |
| 104 | + toString(encoding: string, start?: number, end?: number): string { |
| 105 | + const slice = this.data.slice(start, end); |
| 106 | + if (encoding === "ascii") { |
| 107 | + return String.fromCharCode(...slice); |
| 108 | + } |
| 109 | + throw new Error(`Encoding ${encoding} not supported`); |
| 110 | + } |
| 111 | + |
| 112 | + subarray(start?: number, end?: number): Uint8Array { |
| 113 | + return this.data.subarray(start, end); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// Make Buffer available globally for the postcode client |
| 118 | +declare global { |
| 119 | + interface Window { |
| 120 | + Buffer: typeof BufferPolyfill; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +if (typeof window !== "undefined") { |
| 125 | + window.Buffer = BufferPolyfill as any; |
| 126 | +} |
0 commit comments