| 38 | } |
| 39 | |
| 40 | export class PlatformNode implements Platform { |
| 41 | private textEncoder: TextEncoder; |
| 42 | // tslint:disable-next-line:no-any |
| 43 | util: any; |
| 44 | |
| 45 | constructor() { |
| 46 | // tslint:disable-next-line:no-require-imports |
| 47 | this.util = require('util'); |
| 48 | // According to the spec, the built-in encoder can do only UTF-8 encoding. |
| 49 | // https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/TextEncoder |
| 50 | this.textEncoder = new this.util.TextEncoder(); |
| 51 | } |
| 52 | |
| 53 | fetch(path: string, requestInits?: RequestInit): Promise<Response> { |
| 54 | if (env().global.fetch != null) { |
| 55 | return env().global.fetch(path, requestInits); |
| 56 | } |
| 57 | |
| 58 | if (systemFetch == null) { |
| 59 | systemFetch = getNodeFetch.importFetch(); |
| 60 | } |
| 61 | return systemFetch(path, requestInits); |
| 62 | } |
| 63 | |
| 64 | now(): number { |
| 65 | const time = process.hrtime(); |
| 66 | return time[0] * 1000 + time[1] / 1000000; |
| 67 | } |
| 68 | |
| 69 | encode(text: string, encoding: string): Uint8Array { |
| 70 | if (encoding !== 'utf-8' && encoding !== 'utf8') { |
| 71 | throw new Error( |
| 72 | `Node built-in encoder only supports utf-8, but got ${encoding}`); |
| 73 | } |
| 74 | return this.textEncoder.encode(text); |
| 75 | } |
| 76 | decode(bytes: Uint8Array, encoding: string): string { |
| 77 | if (bytes.length === 0) { |
| 78 | return ''; |
| 79 | } |
| 80 | return new this.util.TextDecoder(encoding).decode(bytes); |
| 81 | } |
| 82 | isTypedArray(a: unknown): a is Float32Array | Int32Array | Uint8Array |
| 83 | | Uint8ClampedArray { |
| 84 | return this.util.types.isFloat32Array(a) |
| 85 | || this.util.types.isInt32Array(a) |
| 86 | || this.util.types.isUint8Array(a) |
| 87 | || this.util.types.isUint8ClampedArray(a); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (env().get('IS_NODE') && !env().get('IS_BROWSER')) { |
| 92 | env().setPlatform('node', new PlatformNode()); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…