Skip to content

Commit c1e507e

Browse files
committed
Generating client side code again
1 parent 5774172 commit c1e507e

24 files changed

+3127
-4425
lines changed

.babelrc

Lines changed: 0 additions & 8 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
node_modules
22

3-
client-side/upload-download.js
4-
compiled/*-unminified.js
3+
client-side/upload-download.ts
4+
compiled/dist
55
test/**/*.js
66
typings/*.js
77

client-side/binary-ajax.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

client-side/common.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

client-side/common.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from '../lib/assert'
2+
import Type from '../types/Type'
3+
4+
assert(typeof ArrayBuffer !== 'undefined', 'ArrayBuffer not supported')
5+
assert(typeof fetch !== 'undefined', 'fetch() not supported')
6+
assert(typeof Map !== 'undefined', 'Map not supported')
7+
assert(typeof Set !== 'undefined', 'Set not supported')
8+
assert(typeof Symbol !== 'undefined', 'Symbol not supported')
9+
assert(typeof Uint8Array !== 'undefined', 'Uint8Array not supported')
10+
assert(typeof WeakMap !== 'undefined', 'WeakMap not supported')
11+
12+
export * from '../types'
13+
export * from '../recursive-registry'
14+
export {assert}
15+
export {Type}

client-side/download.js

Lines changed: 0 additions & 90 deletions
This file was deleted.

client-side/download.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as base64 from 'base64-js'
2+
import {assert, Type} from './common'
3+
import * as r from '../read'
4+
5+
interface Sig {
6+
sig: string
7+
}
8+
interface SigAndType extends Sig {
9+
type: Type<any>
10+
}
11+
interface TypeCache {
12+
[name: string]: SigAndType
13+
}
14+
interface SigAndBuffer extends Sig {
15+
type: string
16+
}
17+
interface ComposedCache {
18+
[name: string]: SigAndBuffer
19+
}
20+
21+
const typeCache: TypeCache = {}
22+
function saveTypeCache() {
23+
const composedCache: ComposedCache = {}
24+
for (const type in typeCache) {
25+
composedCache[type] = {
26+
sig: typeCache[type].sig,
27+
type: base64.fromByteArray(new Uint8Array(typeCache[type].type.toBuffer()))
28+
}
29+
}
30+
localStorage.typeCache = JSON.stringify(composedCache)
31+
}
32+
if (localStorage.typeCache) {
33+
const composedCache: ComposedCache = JSON.parse(localStorage.typeCache)
34+
for (const typeName in composedCache) {
35+
typeCache[typeName] = {
36+
sig: composedCache[typeName].sig,
37+
type: r.type(base64.toByteArray(composedCache[typeName].type).buffer)
38+
}
39+
}
40+
}
41+
42+
export interface DownloadOptions {
43+
name: string
44+
url: string
45+
options?: RequestInit
46+
}
47+
export function download({name, url, options}: DownloadOptions): Promise<any> {
48+
assert.instanceOf(name, String)
49+
assert.instanceOf(url, String)
50+
options = options || {}
51+
assert.instanceOf(options, Object)
52+
const typeInCache = typeCache[name]
53+
if (typeInCache) {
54+
if (options.headers) {
55+
const {headers} = options
56+
if (headers.constructor !== Headers) options.headers = new Headers(headers)
57+
}
58+
else options.headers = new Headers
59+
;(options.headers as Headers).set('sig', typeCache[name].sig)
60+
}
61+
return fetch(url, options)
62+
.then(response => {
63+
if (!response.ok) throw new Error('Received status of ' + String(response.status))
64+
const sig = response.headers.get('sig')!
65+
if (typeInCache && typeInCache.sig === sig) {
66+
return response.arrayBuffer()
67+
.then(buffer => {
68+
const value = r.value({buffer, type: typeInCache.type})
69+
return Promise.resolve(value)
70+
})
71+
}
72+
else {
73+
return response.arrayBuffer()
74+
.then(buffer => {
75+
const readType = r._consumeType(buffer, 0)
76+
const type = readType.value
77+
const value = r.value({buffer, offset: readType.length, type})
78+
typeCache[name] = {sig, type}
79+
saveTypeCache()
80+
return Promise.resolve(value)
81+
})
82+
}
83+
})
84+
}
85+
export * from './common'
86+
(window as any).sb = exports

client-side/upload.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

client-side/upload.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {assert, Type} from './common'
2+
import AbstractType from '../types/abstract'
3+
4+
export interface UploadOptions<E> {
5+
type: Type<E>
6+
value: E
7+
url: string
8+
options: RequestInit
9+
}
10+
export function upload<E>({type, value, url, options}: UploadOptions<E>): Promise<Response> {
11+
assert.instanceOf(type, AbstractType)
12+
assert.instanceOf(url, String)
13+
assert.instanceOf(options, Object)
14+
if (options.method !== 'POST') assert.fail('Must use POST when uploading')
15+
options.body = type.valueBuffer(value)
16+
return fetch(url, options)
17+
}
18+
export * from './common'
19+
(window as any).sb = exports

0 commit comments

Comments
 (0)