-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
deno-io.ts
71 lines (65 loc) · 2.04 KB
/
deno-io.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { PlatformIO } from './platform-io';
interface Path {
resolve(base: string, path: string): string;
dirname(uri: string): string;
}
/**
* # DenoIO
*
* *I/O service for [Deno](https://deno.land/).*
*
* The most common use of the I/O service is to read/write a {@link Document} with a given path.
* Methods are also available for converting in-memory representations of raw glTF files, both
* binary (*Uint8Array*) and JSON ({@link JSONDocument}).
*
* _*NOTICE:* Support for the Deno environment is currently experimental. See
* [glTF-Transform#457](https://github.com/donmccurdy/glTF-Transform/issues/457)._
*
* Usage:
*
* ```typescript
* import { DenoIO } from 'https://esm.sh/@gltf-transform/core';
* import * as path from 'https://deno.land/std/path/mod.ts';
*
* const io = new DenoIO(path);
*
* // Read.
* let document;
* document = io.read('model.glb'); // → Document
* document = io.readBinary(glb); // Uint8Array → Document
*
* // Write.
* const glb = io.writeBinary(document); // Document → Uint8Array
* ```
*
* @category I/O
*/
export class DenoIO extends PlatformIO {
private _path: Path;
constructor(path: unknown) {
super();
this._path = path as Path;
}
protected async readURI(uri: string, type: 'view'): Promise<Uint8Array>;
protected async readURI(uri: string, type: 'text'): Promise<string>;
protected async readURI(uri: string, type: 'view' | 'text'): Promise<Uint8Array | string> {
// TODO(cleanup): The @ts-ignore rules below are necessary for typedoc, but not for normal
// compilation with microbundle. Clean this up when possible.
switch (type) {
case 'view':
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return Deno.readFile(uri);
case 'text':
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return Deno.readTextFile(uri);
}
}
protected resolve(base: string, path: string): string {
return this._path.resolve(base, path);
}
protected dirname(uri: string): string {
return this._path.dirname(uri);
}
}