-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathrequest.js
364 lines (340 loc) · 14.5 KB
/
request.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Defines the protocol used to communicate between JS and NaCL.
* This should be consistent with cpp/request.h.
* @namespace
*/
unpacker.request = {
/**
* Defines request ids. Every key should be unique and the same as the keys
* on the NaCL side.
* @enum {string}
*/
Key: {
// Mandatory keys for all unpacking operations.
OPERATION: 'operation', // Should be a unpacker.request.Operation.
FILE_SYSTEM_ID: 'file_system_id', // Should be a string.
REQUEST_ID: 'request_id', // Should be a string.
// Optional keys unique to unpacking operations.
METADATA: 'metadata', // Should be a dictionary.
ARCHIVE_SIZE: 'archive_size', // Should be a string as only int is
// supported by pp::Var on C++.
INDEX: 'index', // Should be a string. Same reason as ARCHIVE_SIZE.
ENCODING: 'encoding', // Should be a string.
OPEN_REQUEST_ID: 'open_request_id', // Should be a string, just like
// REQUEST_ID.
READ_FILE_DATA: 'read_file_data', // Should be an ArrayBuffer.
HAS_MORE_DATA: 'has_more_data', // Should be a boolean.
PASSPHRASE: 'passphrase', // Should be a string.
// Mandatory keys for all packing operations.
COMPRESSOR_ID: 'compressor_id', // Should be an int.
// Optional keys unique to packing operations.
ENTRY_ID: 'entry_id', // Should be an int.
PATHNAME: 'pathname', // should be a string.
FILE_SIZE: 'file_size', // should be a string. Same reason
// as ARCHIVE_SIZE.
IS_DIRECTORY: 'is_directory', // should be a boolean.
MODIFICATION_TIME: 'modification_time', // should be a string.
// (mm/dd/yy h:m:s)
HAS_ERROR: 'has_error', // Should be a boolean Sent from JS
// to NaCL.
// Optional keys used for both packing and unpacking operations.
ERROR: 'error', // Should be a string.
CHUNK_BUFFER: 'chunk_buffer', // Should be an ArrayBuffer.
OFFSET: 'offset', // Should be a string. Same reason as ARCHIVE_SIZE.
LENGTH: 'length', // Should be a string. Same reason as ARCHIVE_SIZE.
SRC_FILE: 'src_file', // Should be a string.
SRC_LINE: 'src_line', // Should be a int.
SRC_FUNC: 'src_func', // Should be a string.
MESSAGE: 'message', // Should be a string.
},
/**
* Defines request operations. These operation should be the same as the
* operations on the NaCL side. FILE_SYSTEM_ID and REQUEST_ID are mandatory
* for all unpack requests, while COMPRESSOR_ID is required for all pack
* requests. All the values of unpacking operations must be smaller than any
* packing operation (except errors).
* @enum {number}
*/
Operation: {
READ_METADATA: 0,
READ_METADATA_DONE: 1,
READ_CHUNK: 2,
READ_CHUNK_DONE: 3,
READ_CHUNK_ERROR: 4,
READ_PASSPHRASE: 5,
READ_PASSPHRASE_DONE: 6,
READ_PASSPHRASE_ERROR: 7,
CLOSE_VOLUME: 8,
OPEN_FILE: 9,
OPEN_FILE_DONE: 10,
CLOSE_FILE: 11,
CLOSE_FILE_DONE: 12,
READ_FILE: 13,
READ_FILE_DONE: 14,
CONSOLE_LOG: 15,
CONSOLE_DEBUG: 16,
CREATE_ARCHIVE: 17,
CREATE_ARCHIVE_DONE: 18,
ADD_TO_ARCHIVE: 19,
ADD_TO_ARCHIVE_DONE: 20,
READ_FILE_CHUNK: 21,
READ_FILE_CHUNK_DONE: 22,
WRITE_CHUNK: 23,
WRITE_CHUNK_DONE: 24,
CLOSE_ARCHIVE: 25,
CLOSE_ARCHIVE_DONE: 26,
FILE_SYSTEM_ERROR: -1,
COMPRESSOR_ERROR: -2
},
/**
* Operations greater than or equal to this value are for packing.
* @const {number}
*/
MINIMUM_PACK_REQUEST_VALUE: 17,
/**
* Return true if the given operation is related to packing.
* @param {!unpacker.request.Operation} operation
* @return {boolean}
*/
isPackRequest: function(operation) {
return unpacker.request.MINIMUM_PACK_REQUEST_VALUE <= operation ||
operation == unpacker.request.Operation.COMPRESSOR_ERROR;
},
/**
* Creates a basic request with mandatory fields.
* @param {!unpacker.request.Operation} operation
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {!unpacker.types.RequestId} requestId The request id. Should be
* unique only per file system.
* @private
* @return {!Object} A new request with mandatory fields.
*/
createBasic_: function(operation, fileSystemId, requestId) {
var basicRequest = {};
basicRequest[unpacker.request.Key.OPERATION] = operation;
basicRequest[unpacker.request.Key.FILE_SYSTEM_ID] = fileSystemId;
basicRequest[unpacker.request.Key.REQUEST_ID] = requestId.toString();
return basicRequest;
},
/**
* Creates a read metadata request.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {!unpacker.types.RequestId} requestId
* @param {string} encoding Default encoding for the archive.
* @param {number} archiveSize The size of the archive for fileSystemId.
* @return {!Object} A read metadata request.
*/
createReadMetadataRequest: function(fileSystemId, requestId, encoding,
archiveSize) {
var readMetadataRequest = unpacker.request.createBasic_(
unpacker.request.Operation.READ_METADATA, fileSystemId, requestId);
readMetadataRequest[unpacker.request.Key.ENCODING] = encoding;
readMetadataRequest[unpacker.request.Key.ARCHIVE_SIZE] =
archiveSize.toString();
return readMetadataRequest;
},
/**
* Creates a read chunk done response. This is a response to a READ_CHUNK
* request from NaCl.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {!unpacker.types.RequestId} requestId
* @param {!ArrayBuffer} buffer A buffer containing the data that was read.
* @param {number} readOffset The offset from where buffer starts. This is
* required for distinguishing multiple read chunk requests done in
* parallel for different offsets.
* @return {!Object} A read chunk done response.
*/
createReadChunkDoneResponse: function(fileSystemId, requestId, buffer,
readOffset) {
var response = unpacker.request.createBasic_(
unpacker.request.Operation.READ_CHUNK_DONE, fileSystemId, requestId);
response[unpacker.request.Key.CHUNK_BUFFER] = buffer;
response[unpacker.request.Key.OFFSET] = readOffset.toString();
return response;
},
/**
* Creates a read chunk error response. This is a response to a READ_CHUNK
* request from NaCl in case of any errors in order for NaCl to cleanup
* resources.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {!unpacker.types.RequestId} requestId
* @return {!Object} A read chunk error response.
*/
createReadChunkErrorResponse: function(fileSystemId, requestId) {
return unpacker.request.createBasic_(
unpacker.request.Operation.READ_CHUNK_ERROR, fileSystemId, requestId);
},
/**
* Creates a read passphrase done response. This is a response to a
* READ_PASSPHRASE request from NaCl.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {!unpacker.types.RequestId} requestId
* @param {string} passphrase The passphrase.
* @return {!Object} A read passphrase done response.
*/
createReadPassphraseDoneResponse: function(fileSystemId, requestId,
passphrase) {
var response = unpacker.request.createBasic_(
unpacker.request.Operation.READ_PASSPHRASE_DONE, fileSystemId,
requestId);
response[unpacker.request.Key.PASSPHRASE] = passphrase;
return response;
},
/**
* Creates a read passphrase error response. This is a response to a
* READ_PASSPHRASE request from NaCl in case of any errors in order for NaCl
* to cleanup resources.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {!unpacker.types.RequestId} requestId
* @return {!Object} A read passphrase error response.
*/
createReadPassphraseErrorResponse: function(fileSystemId, requestId) {
return unpacker.request.createBasic_(
unpacker.request.Operation.READ_PASSPHRASE_ERROR, fileSystemId,
requestId);
},
/**
* Creates a request to close a volume related to a fileSystemId.
* Can be called after any request.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @return {!Object} A close volume request.
*/
createCloseVolumeRequest: function(fileSystemId) {
return unpacker.request.createBasic_(
unpacker.request.Operation.CLOSE_VOLUME, fileSystemId, -1);
},
/**
* Creates an open file request.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {number} index The index of the file in the header list.
* @param {string} encoding Default encoding for the archive.
* @param {number} archiveSize The size of the volume's archive.
* @return {!Object} An open file request.
*/
createOpenFileRequest: function(fileSystemId, requestId, index, encoding,
archiveSize) {
var openFileRequest = unpacker.request.createBasic_(
unpacker.request.Operation.OPEN_FILE, fileSystemId, requestId);
openFileRequest[unpacker.request.Key.INDEX] = index.toString();
openFileRequest[unpacker.request.Key.ENCODING] = encoding;
openFileRequest[unpacker.request.Key.ARCHIVE_SIZE] = archiveSize.toString();
return openFileRequest;
},
/**
* Creates a close file request.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {!unpacker.types.RequestId} requestId
* @param {!unpacker.types.RequestId} openRequestId
* @return {!Object} A close file request.
*/
createCloseFileRequest: function(fileSystemId, requestId, openRequestId) {
var closeFileRequest = unpacker.request.createBasic_(
unpacker.request.Operation.CLOSE_FILE, fileSystemId, requestId);
closeFileRequest[unpacker.request.Key.OPEN_REQUEST_ID] =
openRequestId.toString();
return closeFileRequest;
},
/**
* Creates a read file request.
* @param {!unpacker.types.FileSystemId} fileSystemId
* @param {!unpacker.types.RequestId} requestId
* @param {!unpacker.types.RequestId} openRequestId
* @param {number} offset The offset from where read is done.
* @param {number} length The number of bytes required.
* @return {!Object} A read file request.
*/
createReadFileRequest: function(fileSystemId, requestId, openRequestId,
offset, length) {
var readFileRequest = unpacker.request.createBasic_(
unpacker.request.Operation.READ_FILE, fileSystemId, requestId);
readFileRequest[unpacker.request.Key.OPEN_REQUEST_ID] =
openRequestId.toString();
readFileRequest[unpacker.request.Key.OFFSET] = offset.toString();
readFileRequest[unpacker.request.Key.LENGTH] = length.toString();
return readFileRequest;
},
/**
* Creates a create archive request for compressor.
* @param {!unpacker.types.CompressorId} compressorId
* @return {!Object} A create archive request.
*/
createCreateArchiveRequest: function(compressorId) {
var request = {};
request[unpacker.request.Key.OPERATION] =
unpacker.request.Operation.CREATE_ARCHIVE;
request[unpacker.request.Key.COMPRESSOR_ID] = compressorId;
return request;
},
/**
* Creates an add to archive request for compressor.
* @param {!unpacker.types.CompressorId} compressorId
* @param {!unpacker.types.EntryId} entryId
* @param {string} pathname The relative path of the entry.
* @param {number} fileSize The size of the entry.
* @param {string} modificationTime The modification time of the entry.
* @param {boolean} isDirectory Whether the entry is a directory or not.
* @return {!Object} An add to archive request.
*/
createAddToArchiveRequest: function(compressorId, entryId, pathname,
fileSize, modificationTime, isDirectory) {
var request = {};
request[unpacker.request.Key.OPERATION] =
unpacker.request.Operation.ADD_TO_ARCHIVE;
request[unpacker.request.Key.COMPRESSOR_ID] = compressorId;
request[unpacker.request.Key.ENTRY_ID] = entryId;
request[unpacker.request.Key.PATHNAME] = pathname.toString();
request[unpacker.request.Key.FILE_SIZE] = fileSize.toString();
request[unpacker.request.Key.MODIFICATION_TIME] =
modificationTime.toString();
request[unpacker.request.Key.IS_DIRECTORY] = isDirectory;
return request;
},
/**
* Creates a read file chunk response for compressor.
* @param {!unpacker.types.CompressorId} compressorId
* @param {number} length The number of bytes read from the entry.
* @param {!ArrayBuffer} buffer A buffer containing the data that was read.
* @return {!Object} A read file chunk done response.
*/
createReadFileChunkDoneResponse: function(compressorId, length, buffer) {
var response = {};
response[unpacker.request.Key.OPERATION] =
unpacker.request.Operation.READ_FILE_CHUNK_DONE;
response[unpacker.request.Key.COMPRESSOR_ID] = compressorId;
response[unpacker.request.Key.LENGTH] = length.toString();
response[unpacker.request.Key.CHUNK_BUFFER] = buffer;
return response;
},
/**
* Creates a write chunk done response for compressor.
* @param {!unpacker.types.CompressorId} compressorId
* @param {number} length The number of bytes written onto the archive file.
* @return {!Object} A write chunk done response.
*/
createWriteChunkDoneResponse: function(compressorId, length) {
var response = {};
response[unpacker.request.Key.OPERATION] =
unpacker.request.Operation.WRITE_CHUNK_DONE;
response[unpacker.request.Key.COMPRESSOR_ID] = compressorId;
response[unpacker.request.Key.LENGTH] = length.toString();
return response;
},
/**
* Creates a close archive request for compressor.
* @param {!unpacker.types.CompressorId} compressorId
* @param {boolean} hasError True if some error occurred.
* @return {!Object} A close archive request.
*/
createCloseArchiveRequest: function(compressorId, hasError) {
var request = {};
request[unpacker.request.Key.OPERATION] =
unpacker.request.Operation.CLOSE_ARCHIVE;
request[unpacker.request.Key.COMPRESSOR_ID] = compressorId;
request[unpacker.request.Key.HAS_ERROR] = hasError;
return request;
}
};