-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoff-url.js
207 lines (176 loc) · 5.11 KB
/
off-url.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const urlencode = require('urlencode')
let _serverAddress = new WeakMap()
let _applicationPath = new WeakMap()
let _contentType = new WeakMap()
let _hash = new WeakMap()
let _streamLength = new WeakMap()
let _fileHash = new WeakMap()
let _descriptorHash = new WeakMap()
let _tupleBlock1 = new WeakMap()
let _tupleBlock2 = new WeakMap()
let _tupleBlock3 = new WeakMap()
let _streamOffset = new WeakMap()
let _streamOffsetLength = new WeakMap()
let _fileName = new WeakMap()
let _version = new WeakMap()
const _regEx = /\/offsystem\/v3\/([-+.\w]+\/[-+.\w]+)\/(\d+)\/([123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+)\/([123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+)\/([^ !$`&*()+]*|\\[ !$`&*()+]*)+/
module.exports = class OffUrl {
constructor (options) {
_serverAddress.set(this, 'http://localhost:23402')
_applicationPath.set(this, 'offsystem')
_contentType.set(this, 'application/octet-stream')
_streamOffset.set(this, 0)
_streamOffsetLength.set(this, 0)
_streamLength.set(this, 0)
_version.set(this, 'v3')
}
toString () { // this is a version 3 url
return [ this.serverAddress, this.applicationPath, this.version, this.contentType, this.streamLength, this.fileHash, this.descriptorHash, urlencode(this.fileName) ].join('/')
}
get serverAddress () {
return _serverAddress.get(this)
}
set serverAddress (value) {
if (typeof value !== 'string') {
throw new Error('Server address must be a string')
}
_serverAddress.set(this, value)
}
get applicationPath () {
return _applicationPath.get(this)
}
set applicationPath (value) {
if (typeof value !== 'string') {
throw new Error('Application path must be a string')
}
_applicationPath.set(this, value)
}
get contentType () {
return _contentType.get(this)
}
set contentType (value) {
if (typeof value !== 'string') {
throw new Error('Content type must be a string')
}
_contentType.set(this, value)
}
get streamLength () {
return _streamLength.get(this)
}
set streamLength (value) {
if (!Number.isInteger(value)) {
throw new Error('Stream length must be an integer')
}
_streamLength.set(this, value)
}
get hash () {
return _hash.get(this)
}
set hash (value) {
if (typeof value !== 'string') {
throw new Error('Hash must be a string')
}
_hash.set(this, value)
}
get fileHash () {
return _fileHash.get(this)
}
set fileHash (value) {
if (typeof value !== 'string') {
throw new Error('File hash must be a string')
}
_fileHash.set(this, value)
}
get descriptorHash () {
return _descriptorHash.get(this)
}
set descriptorHash (value) {
if (typeof value !== 'string') {
throw new Error('Descriptor hash must be a string')
}
_descriptorHash.set(this, value)
}
get tupleBlock1 () {
return _tupleBlock1.get(this)
}
set tupleBlock1 (value) {
if (typeof value !== 'string') {
throw new Error('First tuple must be a string')
}
_tupleBlock1.set(this, value)
}
get tupleBlock2 () {
return _tupleBlock1.get(this)
}
set tupleBlock2 (value) {
if (typeof value !== 'string') {
throw new Error('Second tuple must be a string')
}
_tupleBlock2.set(this, value)
}
get tupleBlock3 () {
return _tupleBlock3.get(this)
}
set tupleBlock3 (value) {
if (typeof value !== 'string') {
throw new Error('Third tuple must be a string')
}
_tupleBlock3.set(this, value)
}
get streamOffset () {
return _streamOffset.get(this) || 0
}
set streamOffset (value) {
if (!Number.isInteger(value)) {
throw new Error('Stream offset must be an integer')
}
_streamOffset.set(this, value)
}
get streamOffsetLength () {
return _streamOffsetLength.get(this) || this.streamLength
}
set streamOffsetLength (value) {
if (!Number.isInteger(value)) {
throw new Error('Stream offset length must be an integer')
}
_streamOffsetLength.set(this, value)
}
get fileName () {
return _fileName.get(this)
}
set fileName (value) {
if (typeof value !== 'string') {
throw new Error('File name must be a string')
}
_fileName.set(this, value)
}
get version () {
return _version.get(this)
}
set version (value) {
if (typeof value !== 'string') {
throw new Error('version must be a string')
}
_version.set(this, value)
}
static parse (url) {
if (typeof url !== 'string') {
throw new Error('url must be a string')
}
let matches = url.match(_regEx)
if (matches) {
let url = new OffUrl()
url.contentType = matches[ 1 ]
url.streamLength = parseInt(matches[ 2 ])
url.fileHash = matches[ 3 ]
url.descriptorHash = matches[ 4 ]
url.fileName = matches[ 5 ]
return url
} else {
throw new Error('Invalid url')
}
}
static get regEx () {
return _regEx
}
}